0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

kintone で JavaScript / CSS ファイルを全部ダウンロードする PHP プログラム

0
Last updated at Posted at 2024-12-11

前回記事の PHP 版です。

あまりやらない人もいるかもしれませんが、PHP のプログラムをコマンドプロンプトで実行する場合は、以下を入力します。

php 01_file_list.php

 

・kintone で JavaScript / CSS ファイルを全部ダウンロードする

05_file_download.php
<?php
$appno = "123";
//$appno = "124";
//$appno = "125";
//$appno = "126";

$text = 'UserName:YourPassWord';

$option = array(
    'http'=>array(
        'method'=>"GET",
        'header'=>"Host: sample.cybozu.com:443\r\n" .
                  "X-Cybozu-Authorization: " . base64_encode($text) . "\r\n",
                  "Content-Type: application/json\r\n"
    )
);

$context = stream_context_create($option);

$response = file_get_contents("https://sample.cybozu.com/k/v1/app/customize.json?app=" . $appno , false, $context);

$data = json_decode($response, true);

foreach (['js', 'css'] as $type) {
    if (isset($data['desktop'][$type]) && is_array($data['desktop'][$type])) {
        foreach ($data['desktop'][$type] as $fileData) {
            $file = $fileData['file'] ?? [];
            $name = $file['name'] ?? '名前なし';
            $size = $file['size'] ?? 'サイズ不明';
            $fileKey = $file['fileKey'] ?? 'ファイルキーなし';

            if (!is_numeric(substr($name, 0, 3)) || intval(substr($name, 0, 3)) < 100) {
                // don't display
            //} elseif (pathinfo($name, PATHINFO_EXTENSION) === 'css') {
            //    // don't display
            } else {

                //echo $fileKey . "\n";
                $url2 = "https://sample.cybozu.com/k/v1/file.json";

                $headers2 = [
                    "X-Cybozu-Authorization: " . base64_encode($text),
                    "Content-Type: application/json"
                ];

                $body2 = json_encode([
                    "fileKey" => $fileKey
                ]);

                $option2 = [
                    'http' => [
                        'method' => "GET",
                        'header' => implode("\r\n", $headers2),
                        'content' => $body2,
                    ]
                ];

                $context2 = stream_context_create($option2);
                $response2 = file_get_contents($url2 , false, $context2);

                $newlineCount = substr_count($response2, "\r\n");
                $size2 = $size - $newlineCount;
                echo $name . ", " . $size . ", " . $size2 . "\n";

                if ($response2 !== false) {
                    file_put_contents($name, $response2);
                    //echo "レスポンスがファイル '{$name}' に保存されました。\n";
                } else {
                    //echo "リクエストに失敗しました。\n";
                }
            }
        }
    }
}
?>

 

・ローカルPCで保存している同ファイルのサイズを表示する

02_file_local.php
<?php
$folderPath = 'C:\Repos\App\123';
//$folderPath = 'C:\Repos\App\124';
//$folderPath = 'C:\Repos\App\125';
//$folderPath = 'C:\Repos\App\126';

$files = scandir($folderPath);

foreach ($files as $file) {
    $filePath = $folderPath . DIRECTORY_SEPARATOR . $file;

    if (is_file($filePath)) {
        $size = filesize($filePath);

        $fileContent = file_get_contents($filePath);
        $newlineCount = substr_count($fileContent, "\n");
        $size = $size - $newlineCount;

        if (!is_numeric(substr($file, 0, 3)) || intval(substr($file, 0, 3)) < 100) {
            // don't display
        //} elseif (pathinfo($file, PATHINFO_EXTENSION) === 'css') {
        //    // don't display
        } else {
            echo "$file, $size\n";
        }

    }
}
?>

 

・単に kintone の JavaScript / CSS ファイル一覧を表示する

01_file_list.php
<?php
$api_url = "https://sample.cybozu.com/k/v1/app/customize.json?app=123";
//$api_url = "https://sample.cybozu.com/k/v1/app/customize.json?app=124";
//$api_url = "https://sample.cybozu.com/k/v1/app/customize.json?app=125";
//$api_url = "https://sample.cybozu.com/k/v1/app/customize.json?app=126";

$text = 'UserName:YourPassWord';

$opts = array(
    'http'=>array(
        'method'=>"GET",
        'header'=>"Host: sample.cybozu.com:443\r\n" .
                  "X-Cybozu-Authorization: " . base64_encode($text) . "\r\n" ,
                  "Content-Type: application/json\r\n"
    )
);

$context = stream_context_create($opts);

$response = file_get_contents($api_url, false, $context);

$data = json_decode($response, true);

foreach (['js', 'css'] as $type) {
    if (isset($data['desktop'][$type]) && is_array($data['desktop'][$type])) {
        foreach ($data['desktop'][$type] as $fileData) {
            $file = $fileData['file'] ?? [];
            $name = $file['name'] ?? '名前なし';
            $size = $file['size'] ?? 'サイズ不明';
            if (!is_numeric(substr($name, 0, 3)) || intval(substr($name, 0, 3)) < 100) {
                // don't display
            //} elseif (pathinfo($name, PATHINFO_EXTENSION) === 'css') {
            //    // don't display
            } else {
                echo $name . ", " . $size . "\n";
            }
        }
    }
}
?>

 

(補足)
05_file_download.php では、CRLF の数を引き算しているため、size と size2 の値が異なる場合、そのテキストファイルの改行は CRLF と判断できます。
02_file_local.php では(CRLF または)LF の数を引き算しています。そのため、ローカルPCのテキストファイルが CRLF の場合であっても、上記の size2 の値と一致していれば、内容は同じと判断できます。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?