protected function setUp()
 {
     self::$TMP_DIR = realpath(dirname(__FILE__)) . "/../../../tmp";
     SC_Helper_FileManager::deleteFile(self::$TMP_DIR);
     mkdir(self::$TMP_DIR, 0777, true);
     // parent::setUp();
 }
 public function testSfEncodeFile_ファイルが正常に開けた場合_ファイルがエンコードされ出力先のパスが取得できる()
 {
     $outdir = realpath(dirname(__FILE__)) . "/../../../tmp/enc_output/";
     SC_Helper_FileManager::deleteFile($outdir);
     mkdir($outdir, 0777, TRUE);
     $indir = realpath(dirname(__FILE__)) . "/../../../tmp/enc_input/";
     SC_Helper_FileManager::deleteFile($indir);
     mkdir($indir, 0777, TRUE);
     $filepath = $indir . 'test.txt';
     $fp_out = fopen($filepath, 'w');
     fwrite($fp_out, 'こんにちは');
     fclose($fp_out);
     $this->expected = array('filename' => $outdir . 'enc_test.txt', 'content' => 'こんにちは');
     $this->actual['filename'] = SC_Utils::sfEncodeFile($filepath, 'euc-jp', $outdir);
     $fp_in = fopen($outdir . 'enc_test.txt', 'r');
     $this->actual['content'] = mb_convert_encoding(fread($fp_in, 100), 'utf8', 'euc-jp');
     fclose($fp_in);
     $this->verify();
 }
 /**
  * 指定したディレクトリのバイト数を取得する.
  *
  * @param string $dir ディレクトリ
  * @return void
  */
 function sfGetDirSize($dir)
 {
     $bytes = 0;
     if (file_exists($dir)) {
         // ディレクトリの場合下層ファイルの総量を取得
         if (is_dir($dir)) {
             $handle = opendir($dir);
             while ($file = readdir($handle)) {
                 // 行末の/を取り除く
                 $dir = rtrim($dir, '/');
                 $path = $dir . '/' . $file;
                 if ($file != '..' && $file != '.' && !is_dir($path)) {
                     $bytes += filesize($path);
                 } else {
                     if (is_dir($path) && $file != '..' && $file != '.') {
                         // 下層ファイルのバイト数を取得する為、再帰的に呼び出す。
                         $bytes += SC_Helper_FileManager::sfGetDirSize($path);
                     }
                 }
             }
         } else {
             // ファイルの場合
             $bytes = filesize($dir);
         }
     }
     // ディレクトリ(ファイル)が存在しない場合は0byteを返す
     if ($bytes == '') {
         $bytes = 0;
     }
     return $bytes;
 }
 /**
  * DBおよびTPL_PKG_PATHにテンプレートパッケージを追加する.
  *
  * @param object $objForm SC_FormParamのインスタンス
  * @param object $objUpFile SC_UploadFileのインスタンス
  * @return void
  */
 function lfAddTemplates($objForm, $objUpFile)
 {
     $template_code = $objForm->getValue('template_code');
     $template_dir = SMARTY_TEMPLATES_DIR . $template_code;
     $compile_dir = DATA_PATH . "Smarty/templates_c/" . $template_code;
     // フォルダ作成
     if (!file_exists($template_dir)) {
         mkdir($template_dir);
     }
     if (!file_exists($compile_dir)) {
         mkdir($compile_dir);
     }
     // 一時フォルダから保存ディレクトリへ移動
     $objUpFile->moveTempFile();
     // 解凍
     SC_Helper_FileManager::unpackFile($template_dir . "/" . $_FILES['template_file']['name']);
     // ユーザデータの下のファイルをコピーする
     $from_dir = SMARTY_TEMPLATES_DIR . $template_code . "/_packages/";
     $to_dir = USER_PATH . "packages/" . $template_code . "/";
     SC_Utils::sfMakeDir($to_dir);
     SC_Utils::sfCopyDir($from_dir, $to_dir);
     // DBにテンプレート情報を保存
     $this->lfRegisterTemplates($objForm->getHashArray());
 }
 public function testCopyDirectory_コピー先のファイルが元々存在する場合_上書きされる()
 {
     /**
      * tests/tmp/src
      *             /dir10
      *             /dir20/dir21
      *                   /file22.txt
      */
     mkdir(self::$TMP_DIR . "/src", 0700, true);
     mkdir(self::$TMP_DIR . "/src/dir10", 0700, true);
     mkdir(self::$TMP_DIR . "/src/dir20", 0700, true);
     mkdir(self::$TMP_DIR . "/src/dir20/dir21", 0700, true);
     $fp = fopen(self::$TMP_DIR . "/src/dir20/file22.txt", "w");
     fwrite($fp, "ec-cube test");
     fclose($fp);
     mkdir(self::$TMP_DIR . "/dst");
     mkdir(self::$TMP_DIR . "/dst/dir20");
     $fp_dist = fopen(self::$TMP_DIR . "/dst/dir20/file22.txt", "w");
     fwrite($fp_dist, "hello");
     fclose($fp_dist);
     SC_Utils::copyDirectory(self::$TMP_DIR . "/src/", self::$TMP_DIR . "/dst/");
     $this->expected = array("dir10", "dir20", "dir21", "file22.txt", "ec-cube test");
     $this->actual = array();
     Test_Utils::array_append($this->actual, Test_Utils::mapCols(SC_Helper_FileManager::sfGetFileList(self::$TMP_DIR . "/dst"), "file_name"));
     Test_Utils::array_append($this->actual, Test_Utils::mapCols(SC_Helper_FileManager::sfGetFileList(self::$TMP_DIR . "/dst/dir20"), "file_name"));
     $fp_final = fopen(self::$TMP_DIR . "/dst/dir20/file22.txt", "r");
     $read_result = fread($fp_final, 100);
     fclose($fp_final);
     $this->actual[] = $read_result;
     $this->verify('コピー先のファイル一覧');
 }
 /**
  * Page のプロセス.
  *
  * @return void
  */
 function process()
 {
     // 認証可否の判定
     $objSession = new SC_Session();
     SC_Utils::sfIsSuccess($objSession);
     // uniqidをテンプレートへ埋め込み
     $this->uniqid = $objSession->getUniqId();
     $objView = new SC_AdminView();
     switch ($this->lfGetMode()) {
         // 登録ボタン押下時
         case 'register':
             // 画面遷移の正当性チェック
             if (!SC_Utils::sfIsValidTransition($objSession)) {
                 sfDispError('');
             }
             // パラメータ検証
             $objForm = $this->lfInitRegister();
             if ($objForm->checkError()) {
                 SC_Utils_Ex::sfDispError('');
             }
             $template_code = $objForm->getValue('template_code');
             $this->tpl_select = $template_code;
             if ($template_code == "") {
                 $template_code = "default";
             }
             // DBへ使用するテンプレートを登録
             $this->lfRegisterTemplate($template_code);
             // XXX コンパイルファイルのクリア処理を行う
             $objView->_smarty->clear_compiled_tpl();
             // common.cssの内容を更新
             $this->lfChangeCommonCss($template_code);
             // テンプレートのコピー
             $this->lfCopyTemplate($template_code);
             // ブロック位置を更新
             $this->lfChangeBloc($template_code);
             // 完了メッセージ
             $this->tpl_onload = "alert('登録が完了しました。');";
             break;
             // 削除ボタン押下時
         // 削除ボタン押下時
         case 'delete':
             // 画面遷移の正当性チェック
             if (!SC_Utils::sfIsValidTransition($objSession)) {
                 SC_Utils::sfDispError('');
             }
             // パラメータ検証
             $objForm = $this->lfInitDelete();
             if ($objForm->checkError()) {
                 SC_Utils::sfDispError('');
             }
             //現在使用中のテンプレートとデフォルトのテンプレートは削除できないようにする
             $template_code = $objForm->getValue('template_code_temp');
             if ($template_code == TEMPLATE_NAME || $template_code == DEFAULT_TEMPLATE_NAME) {
                 $this->tpl_onload = "alert('選択中のテンプレートは削除出来ません');";
                 break;
             }
             $this->lfDeleteTemplate($template_code);
             break;
             // downloadボタン押下時
         // downloadボタン押下時
         case 'download':
             // 画面遷移の正当性チェック
             if (!SC_Utils::sfIsValidTransition($objSession)) {
                 SC_Utils::sfDispError('');
             }
             // パラメータ検証
             $objForm = $this->lfInitDownload();
             $template_code = $objForm->getValue('template_code_temp');
             // ユーザデータの下のファイルも保存する。
             $from_dir = USER_TEMPLATE_PATH . $template_code . "/";
             $to_dir = SMARTY_TEMPLATES_DIR . $template_code . "/_packages/";
             SC_Utils::sfMakeDir($to_dir);
             SC_Utils::sfCopyDir($from_dir, $to_dir);
             SC_Helper_FileManager::downloadArchiveFiles(SMARTY_TEMPLATES_DIR . $template_code);
             break;
             // プレビューボタン押下時
         // プレビューボタン押下時
         case 'preview':
             break;
         default:
             break;
     }
     // defaultパラメータのセット
     $this->templates = $this->lfGetAllTemplates();
     $this->now_template = TEMPLATE_NAME;
     // 画面の表示
     $objView->assignobj($this);
     $objView->display(MAIN_FRAME);
 }
 public function testSfCopyDir_上書きフラグがONかつ書き込み権限がない場合_同名ファイルが上書きされない()
 {
     mkdir(self::$TMP_DIR . "/src", 0777, true);
     mkdir(self::$TMP_DIR . "/dst", 0777, true);
     // コピー先も作成しておく
     $fp = fopen(self::$TMP_DIR . "/src/test.txt", "w");
     fwrite($fp, "hello");
     fclose($fp);
     // 上書きされないファイル
     $test_file = self::$TMP_DIR . "/dst/test.txt";
     $fp = fopen($test_file, "w");
     fwrite($fp, "good morning");
     fclose($fp);
     chmod($test_file, 0444);
     // いったん読取専用にする
     $src = self::$TMP_DIR . "/src/";
     $dst = self::$TMP_DIR . "/dst/";
     $this->expected = array('dir_exists' => TRUE, 'files' => array('test.txt'), 'file_content' => 'good morning');
     SC_Utils::sfCopyDir($src, $dst, '', TRUE);
     $this->actual['dir_exists'] = is_dir($dst);
     $this->actual['files'] = Test_Utils::mapCols(SC_Helper_FileManager::sfGetFileList($dst), 'file_name');
     $fp = fopen($test_file, "r");
     $this->actual['file_content'] = fread($fp, 100);
     chmod($test_file, 0777);
     // verifyする前にパーミッションを戻す
     $this->verify();
 }