' . $attachlist . ''; } /** * アクション型 */ function plugin_attachlist_action() { global $vars; if (empty($vars['page'])) return attachlist_clear_all_cache(); $msg = 'ファイルの一括操作'; if (isset($vars['pcmd'])) { switch($vars['pcmd']) { case 'upload': $attach = plugin_attach_action(); $form = array('msg' => $attach['msg'], 'body' => $attach['body'] . plugin_attachlist_convert()); return $form; case 'confirm': return attachlist_confirmation($msg, $vars['page']); } } else { return attachlist_authentification($msg, $vars['page']); } } /** * アップロードフォルダとキャッシュの更新日時を比較 * * @param string $dir アップロードフォルダのパス * @param string $cache キャッシュのパス * @return bool $is_fresh キャッシュが新しいかどうか */ function attachlist_is_fresh_cache($dir, $cache) { $t_dir = filemtime($dir); $t_cache = filemtime($cache); $is_fresh = $t_dir < $t_cache; return $is_fresh; } /** * キャッシュの更新 * * @param string $page 対象のページ名 * @param string $dir 添付ファイルのあるディレクトリ * @param string $cache 添付ファイル一覧のキャッシュのパス * @return string $body html_convert済みの添付ファイル一覧 */ function attachlist_update_cache($page, $dir, $cache) { // キャッシュフォルダの確認と作成 if (! file_exists(ATTACHLIST_CACHE_DIR) && ATTACHLIST_ALLOW_CACHE) { mkdir(ATTACHLIST_CACHE_DIR, 0755); chmod(ATTACHLIST_CACHE_DIR, 0755); } // 各添付ファイルの情報を取得する $files = attachlist_get_files($page, $dir); if (empty($files)) return; // 添付ファイルの情報をテーブルに整形 $uri = get_base_uri(PKWK_URI_ABSOLUTE); $e_page = rawurlencode($page); $ref = '[[%name%:' . $uri . '?cmd=attach&pcmd=open&file=%ename%&refer=' . $e_page . ']]'; $info = '&size(12){[[[詳細:' . $uri . '?cmd=attach&pcmd=info&file=%ename%&refer=' . $e_page . ']]]};'; $body = '|~ファイル名|~ファイルサイズ|~アップロード日時|h' . "\n"; $body .= '|380|SIZE(14):RIGHT:200|SIZE(14):CENTER:200|c' . "\n"; foreach ($files as $file) { $e_name = rawurlencode($file['name']); $body .= '|' . str_replace('%name%', $file['name'], str_replace('%ename%', $e_name, $ref)) . ' ' . str_replace('%ename%', $e_name, $info) . '|' . $file['size'] . '|' . $file['time'] . '|' . "\n"; } $ctrl = 'ファイル数:' . count($files) . ' [[[ファイルの一括操作>' . $uri . '?cmd=attachlist&page=' . $e_page . ']]]'; $body = convert_html($ctrl . "\n" . $body); // キャッシュの生成 if (ATTACHLIST_ALLOW_CACHE) file_put_contents($cache, $body); return $body; } /** * ページに添付されたファイルの情報を取得する * * @param string $page 対象のページ名 * @param string $dir 添付ファイルのあるディレクトリ * @return array $files 各添付ファイルの情報 */ function attachlist_get_files($page, $dir) { // ページに添付されたファイルの一覧を取得 $pattern = $dir . encode($page) . '_' . '*'; $s_files = glob($pattern); $files = array(); foreach ($s_files as $i => $file) { // 各ファイルの情報を取得 preg_match('/.+_([^\.]+)$/', $file, $matches); if (empty($matches[1])) continue; $files[$i]['name'] = decode($matches[1]); $files[$i]['time'] = format_date(filemtime($file) - LOCALZONE); $files[$i]['size'] = attachlist_get_filesize($file); } return $files; } /** * 添付ファイルのサイズを取得する * * @param string $file 添付ファイルのパス * @return void */ function attachlist_get_filesize($file) { $size = filesize($file); if (ATTACHLIST_DISPLAY_BYTE) { // バイト表示 return number_format($size, 1) . ' B'; } else { return number_format($size / 1024, 1) . ' KB'; } } /** * ファイル一括操作の管理パスワード認証 * * @param string $msg タブに表示する文章 * @param string $page 対象のページ名 * @return array 各種フォーム */ function attachlist_authentification($msg, $page) { global $vars; // ページ名のチェック if (! is_page($page)) { $body = '

ページ "' . htmlsc($page) . '" は存在しません

'; return array('msg' => $msg, 'body' => $body); } // 認証用フォームの作成 $auth_failed = '

パスワードが違います

' . "\n"; $body = << EOD; // パスワードのチェック if ($vars['pass']) { if (pkwk_login($vars['pass'])) { // パスワードがあっていれば選択用フォームを表示 return array('msg' => $msg, 'body' => attachlist_listup_files($page)); } else { return array('msg' => $msg, 'body' => $auth_failed . $body); } } else { return array('msg' => $msg, 'body' => $body); } } /** * チェックボックス付きの添付ファイル一覧を取得 * * @param string $page 対象のページ名 * @return string $body 添付ファイルの一覧 */ function attachlist_listup_files($page) { // 各添付ファイルの情報を取得 $files = attachlist_get_files($page, UPLOAD_DIR); $body = ''; foreach ($files as $i => $file) { // 凍結されているファイルにマークを追加 $obj = new AttachFile($page, $file['name'], 0); $obj->getstatus(); $freezed = $obj->status['freeze'] ? '*' : ''; // チェックボックス付きのリストを作成 $body .= '
  • ' . $file['name'] . '' . $freezed . '
  • ' . "\n"; } $body = ''; // 全選択/解除用スクリプト $js = << const check_all = document.querySelector("#check_all"); const check_list = document.querySelectorAll(".check_list"); check_all.addEventListener('change', () => { if (check_all.checked) { check_list.forEach (checkbox => (checkbox.checked = true)); } else { check_list.forEach (checkbox => (checkbox.checked = false)); } }); EOD; // 選択用フォームの作成 $body = <<* = 凍結されたファイル

    $body
    $js EOD; return $body; } /** * 操作するファイルの最終確認 * * @param string $msg タブに表示する文章 * @param string $page 対象のページ名 * @return array 各種フォームもしくは操作完了のメッセージ */ function attachlist_confirmation($msg, $page) { global $vars; // モード選択 $mode = isset($vars['mode']) ? htmlsc($vars['mode']) : ''; // 選択した添付ファイルのリストを作成 $files = []; if ($vars['re']) { // 正規表現での指定 $pattern = '/' . str_replace('/', '\/', $vars['re']) . '/'; $allfiles = attachlist_get_files($page, UPLOAD_DIR); foreach ($allfiles as $file) { if (preg_match($pattern, $file['name'])) { $files[] = $file['name']; } } } elseif ($vars['file']) { // 手動選択 $files = $vars['file']; } // ファイルが一つも選択されていなかった場合はエラー if (empty($files)) { return array('msg' => $msg, 'body' => '

    ファイルが選択されていません

    '); } $targets = ''; foreach ($files as $i => $val) { $targets .= '
  • ' . $val . '
  • ' . "\n"; } $targets = '
      ' . $targets . '
    '; // 最終確認用フォームの作成 $auth_failed = '

    パスワードが違います

    ' . "\n"; $body = <<以下のファイルを{$mode}します

    $targets
    EOD; // パスワードのチェック if ($vars['pass']) { if (pkwk_login($vars['pass'])) { // パスワードがあっていればファイルの操作を開始 if (! empty($mode)) $body = attachlist_manage_files($mode, $page); return array('msg' => $msg, 'body' => $body); } else { return array('msg' => $msg, 'body' => $auth_failed . $body); } } else { return array('msg' => $msg, 'body' => $body); } } /** * 添付ファイルの一括操作 * * @param string $mode 削除/凍結/解凍 * @param string $page 対象のページ * @return string $body 操作したファイルの一覧 */ function attachlist_manage_files($mode, $page) { global $vars, $_attach_messages; $files = $vars['file']; $result = array(); $lines = array(); // 行う処理の判別 switch($mode) { case '削除': $pcmd = 'delete'; break; case '凍結': $pcmd = 'freeze'; break; case '解凍': $pcmd = 'unfreeze'; break; default : return '

    不明な処理:' . htmlsc($mode) . '

    '; } // attachの処理成功時のメッセージ $success = $_attach_messages['msg_' . $pcmd . 'd']; // 使用するattachの関数 if ($pcmd == 'unfreeze') { $attach = 'attach_freeze'; } else { $attach = 'attach_' . $pcmd; } // ファイルごとに処理 foreach ($files as $vars['file']) { $file = $vars['file']; if ($pcmd == 'delete') { $result = $attach(); } else { $result = $attach($pcmd == 'freeze' ? true : false); } if ($result['msg'] == $success) { // 処理成功のメッセージを受け取ったらファイル名を記録 $lines[] = $file; } else if ($result['msg'] == $_attach_messages['msg_info']) { // 凍結されたファイルを削除しようとした場合のメッセージ return $_attach_messages['msg_isfreeze']; } else { return $result['msg']; } } // 処理したファイルの一覧を表示 $body =''; foreach ($lines as $line) { $body .= '
  • ' . $line . '
  • ' . "\n"; } $body = '
      ' . "\n" . $body . "\n" . '
    '; $body = '

    以下のファイルを' . $mode . 'しました

    ' . "\n" . $body . "\n" ; $body .= '

    ページに戻る

    '; return $body; } /** * キャッシュファイルの一括削除 * * @return array キャッシュクリアの確認・完了画面 */ function attachlist_clear_all_cache() { global $vars; $msg = 'キャッシュのクリア'; $pattern = ATTACHLIST_CACHE_DIR . '*.dat'; // 認証用フォームの作成 $auth_failed = '

    パスワードが違います

    ' . "\n"; $body = <<添付ファイル一覧のキャッシュをクリアします

    EOD; // パスワードのチェック if ($vars['pass']) { if (pkwk_login($vars['pass'])) { // 認証できたらキャッシュの検索開始 $caches = glob($pattern); if (empty($caches)) { // datファイルがなければ終了 $body = '

    キャッシュが見つかりませんでした

    '; } else { // datファイルがあればそれらを削除 $body = '

    以下のページのキャッシュを削除しました

    ' . "\n"; foreach ($caches as $i => $cache) { preg_match('/.+\/(.+).dat/', $cache, $matches); $page = decode($matches[1]); $attrs = get_page_link_a_attrs($page); if (unlink($cache)) { // 削除に成功したページをリストアップ $body .= '

  • ' . $page . '
  • ' . "\n"; } else { // 削除に失敗したら処理を終了 $body = '

    "' . htmlsc(decode($matches[1])) . '" のキャッシュが削除できませんでした

    '; break; } } } return array('msg' => $msg, 'body' => $body); } else { return array('msg' => $msg, 'body' => $auth_failed . $body); } } else { return array('msg' => $msg, 'body' => $body); } }