Changeset 16262


Ignore:
Timestamp:
2007/10/05 16:06:21 (16 years ago)
Author:
nanasess
Message:

リファクタリング

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/feature-module-update/data/class/pages/admin/contents/LC_Page_Admin_Contents_FileManager.php

    r15674 r16262  
    88// {{{ requires 
    99require_once(CLASS_PATH . "pages/LC_Page.php"); 
     10require_once(CLASS_PATH . "helper_extends/SC_Helper_FileManager_Ex.php"); 
    1011 
    1112/** 
     
    5152        $objView = new SC_AdminView(); 
    5253        $objQuery = new SC_Query(); 
     54        $objFileManager = new SC_Helper_FileManager_Ex(); 
    5355 
    5456        if (!isset($_POST['mode'])) $_POST['mode'] = ""; 
     
    102104                } else { 
    103105                    // ファイルダウンロード 
    104                     $this->sfDownloadFile($_POST['select_file']); 
     106                    $objFileManager->sfDownloadFile($_POST['select_file']); 
    105107                    exit; 
    106108                } 
     
    112114            $arrErr = $this->lfErrorCheck(); 
    113115            if(!is_array($arrErr)) { 
    114                 $this->sfDeleteDir($_POST['select_file']); 
     116                $objFileManager->sfDeleteDir($_POST['select_file']); 
    115117            } 
    116118            break; 
     
    122124                $create_dir = ereg_replace("/$", "", $now_dir); 
    123125                // ファイル作成 
    124                 if(!$this->sfCreateFile($create_dir."/".$_POST['create_file'], 0755)) { 
     126                if(!$objFileManager->sfCreateFile($create_dir."/".$_POST['create_file'], 0755)) { 
    125127                    // 作成エラー 
    126128                    $arrErr['create_file'] = "※ ".$_POST['create_file']."の作成に失敗しました。<br/>"; 
     
    159161 
    160162        // 現在のディレクトリ配下のファイル一覧を取得 
    161         $this->arrFileList = $this->sfGetFileList($now_dir); 
     163        $this->arrFileList = $objFileManager->sfGetFileList($now_dir); 
    162164        $this->tpl_is_top_dir = $is_top_dir; 
    163165        $this->tpl_parent_dir = $parent_dir; 
     
    177179        // ツリー配列作成用 javascript 
    178180        if (!isset($_POST['tree_status'])) $_POST['tree_status'] = ""; 
    179         $arrTree = $this->sfGetFileTree($top_dir, $_POST['tree_status']); 
     181        $arrTree = $objFileManager->sfGetFileTree($top_dir, $_POST['tree_status']); 
    180182        $this->tpl_javascript .= "arrTree = new Array();\n"; 
    181183        foreach($arrTree as $arrVal) { 
     
    249251        return $parent_dir; 
    250252    } 
    251  
    252     /* 
    253      * 関数名:sfGetFileList() 
    254      * 説明 :指定パス配下のディレクトリ取得 
    255      * 引数1 :取得するディレクトリパス 
    256      */ 
    257     function sfGetFileList($dir) { 
    258         $arrFileList = array(); 
    259         $arrDirList = array(); 
    260  
    261         if (is_dir($dir)) { 
    262             if ($dh = opendir($dir)) { 
    263                 $cnt = 0; 
    264                 // 行末の/を取り除く 
    265                 while (($file = readdir($dh)) !== false) $arrDir[] = $file; 
    266                 $dir = ereg_replace("/$", "", $dir); 
    267                 // アルファベットと数字でソート 
    268                 natcasesort($arrDir); 
    269                 foreach($arrDir as $file) { 
    270                     // ./ と ../を除くファイルのみを取得 
    271                     if($file != "." && $file != "..") { 
    272  
    273                         $path = $dir."/".$file; 
    274                         // SELECT内の見た目を整えるため指定文字数で切る 
    275                         $file_name = SC_Utils_Ex::sfCutString($file, FILE_NAME_LEN); 
    276                         $file_size = SC_Utils_Ex::sfCutString($this->sfGetDirSize($path), FILE_NAME_LEN); 
    277                         $file_time = date("Y/m/d", filemtime($path)); 
    278  
    279                         // ディレクトリとファイルで格納配列を変える 
    280                         if(is_dir($path)) { 
    281                             $arrDirList[$cnt]['file_name'] = $file; 
    282                             $arrDirList[$cnt]['file_path'] = $path; 
    283                             $arrDirList[$cnt]['file_size'] = $file_size; 
    284                             $arrDirList[$cnt]['file_time'] = $file_time; 
    285                             $arrDirList[$cnt]['is_dir'] = true; 
    286                         } else { 
    287                             $arrFileList[$cnt]['file_name'] = $file; 
    288                             $arrFileList[$cnt]['file_path'] = $path; 
    289                             $arrFileList[$cnt]['file_size'] = $file_size; 
    290                             $arrFileList[$cnt]['file_time'] = $file_time; 
    291                             $arrFileList[$cnt]['is_dir'] = false; 
    292                         } 
    293                         $cnt++; 
    294                     } 
    295                 } 
    296                 closedir($dh); 
    297             } 
    298         } 
    299  
    300         // フォルダを先頭にしてマージ 
    301         return array_merge($arrDirList, $arrFileList); 
    302     } 
    303  
    304     /* 
    305      * 関数名:sfGetDirSize() 
    306      * 説明 :指定したディレクトリのバイト数を取得 
    307      * 引数1 :ディレクトリ 
    308      */ 
    309     function sfGetDirSize($dir) { 
    310         $bytes = 0; 
    311         if(file_exists($dir)) { 
    312             // ディレクトリの場合下層ファイルの総量を取得 
    313             if (is_dir($dir)) { 
    314                 $handle = opendir($dir); 
    315                 while ($file = readdir($handle)) { 
    316                     // 行末の/を取り除く 
    317                     $dir = ereg_replace("/$", "", $dir); 
    318                     $path = $dir."/".$file; 
    319                     if ($file != '..' && $file != '.' && !is_dir($path)) { 
    320                         $bytes += filesize($path); 
    321                     } else if (is_dir($path) && $file != '..' && $file != '.') { 
    322                         // 下層ファイルのバイト数を取得する為、再帰的に呼び出す。 
    323                         $bytes += $this->sfGetDirSize($path); 
    324                     } 
    325                 } 
    326             } else { 
    327                 // ファイルの場合 
    328                 $bytes = filesize($dir); 
    329             } 
    330         } 
    331         // ディレクトリ(ファイル)が存在しない場合は0byteを返す 
    332         if($bytes == "") $bytes = 0; 
    333  
    334         return $bytes; 
    335     } 
    336  
    337     /* 
    338      * 関数名:sfDeleteDir() 
    339      * 説明 :指定したディレクトリを削除 
    340      * 引数1 :削除ファイル 
    341      */ 
    342     function sfDeleteDir($dir) { 
    343         $arrResult = array(); 
    344         if(file_exists($dir)) { 
    345             // ディレクトリかチェック 
    346             if (is_dir($dir)) { 
    347                 if ($handle = opendir("$dir")) { 
    348                     $cnt = 0; 
    349                     while (false !== ($item = readdir($handle))) { 
    350                         if ($item != "." && $item != "..") { 
    351                             if (is_dir("$dir/$item")) { 
    352                                 sfDeleteDir("$dir/$item"); 
    353                             } else { 
    354                                 $arrResult[$cnt]['result'] = @unlink("$dir/$item"); 
    355                                 $arrResult[$cnt]['file_name'] = "$dir/$item"; 
    356                             } 
    357                         } 
    358                         $cnt++; 
    359                     } 
    360                 } 
    361                 closedir($handle); 
    362                 $arrResult[$cnt]['result'] = @rmdir($dir); 
    363                 $arrResult[$cnt]['file_name'] = "$dir/$item"; 
    364             } else { 
    365                 // ファイル削除 
    366                 $arrResult[0]['result'] = @unlink("$dir"); 
    367                 $arrResult[0]['file_name'] = "$dir"; 
    368             } 
    369         } 
    370  
    371         return $arrResult; 
    372     } 
    373  
    374     /* 
    375      * 関数名:sfGetFileTree() 
    376      * 説明 :ツリー生成用配列取得(javascriptに渡す用) 
    377      * 引数1 :ディレクトリ 
    378      * 引数2 :現在のツリーの状態開いているフォルダのパスが | 区切りで格納 
    379      */ 
    380     function sfGetFileTree($dir, $tree_status) { 
    381  
    382         $cnt = 0; 
    383         $arrTree = array(); 
    384         $default_rank = count(split('/', $dir)); 
    385  
    386         // 文末の/を取り除く 
    387         $dir = ereg_replace("/$", "", $dir); 
    388         // 最上位層を格納(user_data/) 
    389         if($this->sfDirChildExists($dir)) { 
    390             $arrTree[$cnt]['type'] = "_parent"; 
    391         } else { 
    392             $arrTree[$cnt]['type'] = "_child"; 
    393         } 
    394         $arrTree[$cnt]['path'] = $dir; 
    395         $arrTree[$cnt]['rank'] = 0; 
    396         $arrTree[$cnt]['count'] = $cnt; 
    397         // 初期表示はオープン 
    398         if($_POST['mode'] != '') { 
    399             $arrTree[$cnt]['open'] = $this->lfIsFileOpen($dir, $tree_status); 
    400         } else { 
    401             $arrTree[$cnt]['open'] = true; 
    402         } 
    403         $cnt++; 
    404  
    405         $this->sfGetFileTreeSub($dir, $default_rank, $cnt, $arrTree, $tree_status); 
    406  
    407         return $arrTree; 
    408     } 
    409  
    410     /* 
    411      * 関数名:sfGetFileTree() 
    412      * 説明 :ツリー生成用配列取得(javascriptに渡す用) 
    413      * 引数1 :ディレクトリ 
    414      * 引数2 :デフォルトの階層(/区切りで 0,1,2・・・とカウント) 
    415      * 引数3 :連番 
    416      * 引数4 :現在のツリーの状態開いているフォルダのパスが | 区切りで格納 
    417      */ 
    418     function sfGetFileTreeSub($dir, $default_rank, &$cnt, &$arrTree, $tree_status) { 
    419  
    420         if(file_exists($dir)) { 
    421             if ($handle = opendir("$dir")) { 
    422                 while (false !== ($item = readdir($handle))) $arrDir[] = $item; 
    423                 // アルファベットと数字でソート 
    424                 natcasesort($arrDir); 
    425                 foreach($arrDir as $item) { 
    426                     if ($item != "." && $item != "..") { 
    427                         // 文末の/を取り除く 
    428                         $dir = ereg_replace("/$", "", $dir); 
    429                         $path = $dir."/".$item; 
    430                         // ディレクトリのみ取得 
    431                         if (is_dir($path)) { 
    432                             $arrTree[$cnt]['path'] = $path; 
    433                             if($this->sfDirChildExists($path)) { 
    434                                 $arrTree[$cnt]['type'] = "_parent"; 
    435                             } else { 
    436                                 $arrTree[$cnt]['type'] = "_child"; 
    437                             } 
    438  
    439                             // 階層を割り出す 
    440                             $arrCnt = split('/', $path); 
    441                             $rank = count($arrCnt); 
    442                             $arrTree[$cnt]['rank'] = $rank - $default_rank + 1; 
    443                             $arrTree[$cnt]['count'] = $cnt; 
    444                             // フォルダが開いているか 
    445                             $arrTree[$cnt]['open'] = $this->lfIsFileOpen($path, $tree_status); 
    446                             $cnt++; 
    447                             // 下層ディレクトリ取得の為、再帰的に呼び出す 
    448                             $this->sfGetFileTreeSub($path, $default_rank, $cnt, $arrTree, $tree_status); 
    449                         } 
    450                     } 
    451                 } 
    452             } 
    453             closedir($handle); 
    454         } 
    455     } 
    456  
    457     /* 
    458      * 関数名:sfDirChildExists() 
    459      * 説明 :指定したディレクトリ配下にファイルがあるか 
    460      * 引数1 :ディレクトリ 
    461      */ 
    462     function sfDirChildExists($dir) { 
    463         if(file_exists($dir)) { 
    464             if (is_dir($dir)) { 
    465                 $handle = opendir($dir); 
    466                 while ($file = readdir($handle)) { 
    467                     // 行末の/を取り除く 
    468                     $dir = ereg_replace("/$", "", $dir); 
    469                     $path = $dir."/".$file; 
    470                     if ($file != '..' && $file != '.' && is_dir($path)) { 
    471                         return true; 
    472                     } 
    473                 } 
    474             } 
    475         } 
    476  
    477         return false; 
    478     } 
    479  
    480     /* 
    481      * 関数名:lfIsFileOpen() 
    482      * 説明 :指定したファイルが前回開かれた状態にあったかチェック 
    483      * 引数1 :ディレクトリ 
    484      * 引数2 :現在のツリーの状態開いているフォルダのパスが | 区切りで格納 
    485      */ 
    486     function lfIsFileOpen($dir, $tree_status) { 
    487         $arrTreeStatus = split('\|', $tree_status); 
    488         if(in_array($dir, $arrTreeStatus)) { 
    489             return true; 
    490         } 
    491  
    492         return false; 
    493     } 
    494  
    495     /* 
    496      * 関数名:sfDownloadFile() 
    497      * 引数1 :ファイルパス 
    498      * 説明 :ファイルのダウンロード 
    499      */ 
    500     function sfDownloadFile($file) { 
    501         // ファイルの場合はダウンロードさせる 
    502         Header("Content-disposition: attachment; filename=".basename($file)); 
    503         Header("Content-type: application/octet-stream; name=".basename($file)); 
    504         Header("Cache-Control: "); 
    505         Header("Pragma: "); 
    506         echo ($this->sfReadFile($file)); 
    507     } 
    508  
    509     /* 
    510      * 関数名:sfCreateFile() 
    511      * 引数1 :ファイルパス 
    512      * 引数2 :パーミッション 
    513      * 説明 :ファイル作成 
    514      */ 
    515     function sfCreateFile($file, $mode = "") { 
    516         // 行末の/を取り除く 
    517         if($mode != "") { 
    518             $ret = @mkdir($file, $mode); 
    519         } else { 
    520             $ret = @mkdir($file); 
    521         } 
    522  
    523         return $ret; 
    524     } 
    525  
    526     /* 
    527      * 関数名:sfReadFile() 
    528      * 引数1 :ファイルパス 
    529      * 説明 :ファイル読込 
    530      */ 
    531     function sfReadFile($filename) { 
    532         $str = ""; 
    533         // バイナリモードでオープン 
    534         $fp = @fopen($filename, "rb" ); 
    535         //ファイル内容を全て変数に読み込む 
    536         if($fp) { 
    537             $str = @fread($fp, filesize($filename)+1); 
    538         } 
    539         @fclose($fp); 
    540  
    541         return $str; 
    542     } 
    543253} 
    544254?> 
Note: See TracChangeset for help on using the changeset viewer.