Пример #1
0
 /**
  * getFromWysiwygZip
  * ZIPファイルにまとめられているWysiwygエディタの内容を読み取ります。
  * 中に添付ファイルがある場合は、それらの取り込みも行います。
  *
  * 1.指定されたパスにあるZIPファイルを読み取り、
  * 2.中に添付ファイルがあるようなら、それをDB、また所定のフォルダに正しく取込み
  * 3.Wysiwygテキスト文字列のUPされたファイルパスを適宜書き直し
  * 4.そのテキスト文字列を返す
  *
  * @param string $zipFilePath Zipファイルへのパス
  * @param string $fileName Zipファイルの中にある読み取るべきファイル名
  * @return string wysiswyg editor data
  * @throws InternalErrorException
  */
 public function getFromWysiwygZip($zipFilePath, $fileName = 'document.txt')
 {
     // ZIPファイル解凍
     $unZip = new UnZip($zipFilePath);
     $tmpFolder = $unZip->extract();
     if ($tmpFolder === false) {
         return false;
     }
     // $fileの中を開く
     $filePath = $unZip->path . DS . $fileName;
     $file = new File($filePath);
     $data = $file->read();
     $roomId = Current::read('Room.id');
     // uploadFile登録に必要な data(block_key)を作成する。
     $uploadBlockKey = ['UploadFile' => ['block_key' => Current::read('Block.key'), 'room_id' => Current::read('Block.room_id')]];
     // 添付ファイルがある場合に備えて準備
     // imgタグやリンクaタグを1行ずつに分解する
     $tmpStr = str_replace('<img', "\n<img", $data);
     $tmpStr = str_replace('<a', "\n<a", $tmpStr);
     $tmpStrArr = explode("\n", $tmpStr);
     $retStr = '';
     // 1行ずつ処理
     foreach ($tmpStrArr as $line) {
         // wysiwyg行があるか?
         $matchCount = preg_match(self::WYSIWYG_FILE_KEY_PATTERN, $line, $matches);
         // ある
         if ($matchCount > 0) {
             // その中に書かれているwysiwygで設定されたファイルのリスト(uploadId)を得る
             $uploadId = $matches[3];
             // imageなのかfileなのか
             if (preg_match('/^<img/', $matches[1])) {
                 $type = 'image';
             } else {
                 $type = 'file';
             }
             // uploadIdに一致するファイルを取り出す
             $upFileNames = $tmpFolder->find($uploadId . '.*');
             if (empty($upFileNames)) {
                 CakeLog::error('Can not find wysiwyg file ' . $uploadId);
                 throw new InternalErrorException();
             }
             // そのファイルは現在テンポラリフォルダにあるので、そのパス取得
             $upFile = new File($tmpFolder->path . DS . $upFileNames[0]);
             // そのファイルをUPLOAD処理する
             $uploadedFile = $this->UploadFile->registByFile($upFile, 'wysiwyg', null, 'Wysiwyg.file', $uploadBlockKey);
             if (!$uploadedFile) {
                 CakeLog::error('Can not upload wysiwyg file ' . $uploadId);
                 throw new InternalErrorException();
             }
             // wysiwygのパス情報を新ルームIDと新UPLOADIDに差し替える
             $line = sprintf('%s/wysiwyg/%s/download/%d/%d%s', $matches[1], $type, $roomId, $uploadedFile['UploadFile']['id'], $matches[4]);
         }
         // wysiwygテキスト再構築
         $retStr .= $line;
     }
     // 構築したテキストを返す
     return $retStr;
 }
Пример #2
0
 /**
  * キャビネットファイルのUnzip
  *
  * @param Model $model CabinetFile
  * @param array $cabinetFile CabinetFileデータ
  * @return bool
  * @throws InternalErrorException
  */
 public function unzip(Model $model, $cabinetFile)
 {
     $model->begin();
     try {
         // テンポラリフォルダにunzip
         $zipPath = WWW_ROOT . $cabinetFile['UploadFile']['file']['path'] . $cabinetFile['UploadFile']['file']['id'] . DS . $cabinetFile['UploadFile']['file']['real_file_name'];
         //debug($zipPath);
         App::uses('UnZip', 'Files.Utility');
         $unzip = new UnZip($zipPath);
         $tmpFolder = $unzip->extract();
         if ($tmpFolder === false) {
             throw new InternalErrorException('UnZip Failed.');
         }
         $parentCabinetFolder = $model->find('first', ['conditions' => ['CabinetFileTree.id' => $cabinetFile['CabinetFileTree']['parent_id']]]);
         // unzipされたファイル拡張子のバリデーション
         // unzipされたファイルのファイルサイズバリデーション
         $files = $tmpFolder->findRecursive();
         $unzipTotalSize = 0;
         foreach ($files as $file) {
             //
             $unzipTotalSize += filesize($file);
             // ここでは拡張子だけチェックする
             $extension = pathinfo($file, PATHINFO_EXTENSION);
             if (!$model->isAllowUploadFileExtension($extension)) {
                 // NG
                 $model->validationErrors = [__d('cabinets', 'Unzip failed. Contains does not allow file format.')];
                 return false;
             }
         }
         // ルームファイルサイズ制限
         $maxRoomDiskSize = Current::read('Space.room_disk_size');
         if ($maxRoomDiskSize !== null) {
             // nullだったらディスクサイズ制限なし。null以外ならディスクサイズ制限あり
             // 解凍後の合計
             // 現在のルームファイルサイズ
             $roomId = Current::read('Room.id');
             $roomFileSize = $model->getTotalSizeByRoomId($roomId);
             if ($roomFileSize + $unzipTotalSize > $maxRoomDiskSize) {
                 $model->validationErrors[] = __d('cabinets', 'Failed to expand. The total size exceeds the limit.<br />' . 'The total size limit is %s (%s left).', CakeNumber::toReadableSize($roomFileSize + $unzipTotalSize), CakeNumber::toReadableSize($maxRoomDiskSize));
                 return false;
             }
         }
         // 再帰ループで登録処理
         list($folders, $files) = $tmpFolder->read(true, false, true);
         foreach ($files as $file) {
             $this->_addFileFromPath($model, $parentCabinetFolder, $file);
         }
         foreach ($folders as $folder) {
             $this->_addFolderFromPath($model, $parentCabinetFolder, $folder);
         }
     } catch (Exception $e) {
         return $model->rollback($e);
     }
     $model->commit();
     return true;
 }
Пример #3
0
/**
 * Extract zip data.
 *
 * - If destination is null, return a listing.
 *
 * @package Packages
 * @param string $data
 * @param string $destination
 * @param bool $single_file
 * @param bool $overwrite
 * @param string[]|null $files_to_extract
 */
function read_zip_data($data, $destination, $single_file = false, $overwrite = false, $files_to_extract = null)
{
    require_once SUBSDIR . '/UnZip.class.php';
    $unzip = new UnZip($data, $destination, $single_file, $overwrite, $files_to_extract);
    return $unzip->read_zip_data();
}
 /**
  * _createFromTemplate
  *
  * @return array RegistrationData
  */
 protected function _createFromTemplate()
 {
     // 登録フォームデータをUPLOADされた登録フォームテンプレートファイルのデータをもとにして作成する
     // テンプレートからの作成の場合、テンプレートファイルがUPLOADされてくる
     // アップされたファイルをもとに、登録フォームデータを解凍、取得し、
     // そのデータから今回作成する登録フォームデータ基本構成を作成し返す
     if (empty($this->data['ActionRegistrationAdd']['template_file']['name'])) {
         $this->validationErrors['template_file'][] = __d('registrations', 'Please input template file.');
         return null;
     }
     try {
         // アップロードファイルを受け取り、
         // エラーチェックはない。ここでのエラー時はInternalErrorExceptionとなる
         $uploadFile = new TemporaryUploadFile($this->data['ActionRegistrationAdd']['template_file']);
         // アップロードファイル解凍
         $unZip = new UnZip($uploadFile->path);
         $temporaryFolder = $unZip->extract();
         // エラーチェック
         if (!$temporaryFolder) {
             $this->validationErrors['template_file'][] = __d('registrations', 'illegal import file.');
             return null;
         }
         // フィンガープリント確認
         $fingerPrint = $this->__checkFingerPrint($temporaryFolder->path);
         if ($fingerPrint === false) {
             $this->validationErrors['template_file'][] = __d('registrations', 'illegal import file.');
             return null;
         }
         // 登録フォームテンプレートファイル本体をテンポラリフォルダに展開する。
         $registrationZip = new UnZip($temporaryFolder->path . DS . RegistrationsComponent::REGISTRATION_TEMPLATE_FILENAME);
         if (!$registrationZip->extract()) {
             $this->validationErrors['template_file'][] = __d('registrations', 'illegal import file.');
             return null;
         }
         // jsonファイルを読み取り、PHPオブジェクトに変換
         $jsonFilePath = $registrationZip->path . DS . RegistrationsComponent::REGISTRATION_JSON_FILENAME;
         $jsonFile = new File($jsonFilePath);
         $jsonData = $jsonFile->read();
         $jsonRegistration = json_decode($jsonData, true);
     } catch (Exception $ex) {
         $this->validationErrors['template_file'][] = __d('registrations', 'file upload error.');
         return null;
     }
     // 初めにファイルに記載されている登録フォームプラグインのバージョンと
     // 現サイトの登録フォームプラグインのバージョンを突合し、差分がある場合はインポート処理を中断する。
     if ($this->_checkVersion($jsonRegistration) === false) {
         $this->validationErrors['template_file'][] = __d('registrations', 'version is different.');
         return null;
     }
     // バージョンが一致した場合、登録フォームデータをメモリ上に構築
     $registrations = $this->_getRegistrations($registrationZip->path, $jsonRegistration['Registrations'], $fingerPrint);
     // 現在の言語環境にマッチしたデータを返す
     return $registrations[0];
 }