Beispiel #1
0
 function exportUploadedFiles($gid)
 {
     require_once "File/Archive.php";
     File_Archive::setOption('zipCompressionLevel', 0);
     $baseDir = CUSTOMER_IMAGE_DIR . $gid;
     $files = list_files_dir($baseDir);
     File_Archive::extract($files, File_Archive::toArchive("campaign_" . $gid . "_uploadedfiles_" . date('dFY') . ".zip", File_Archive::toOutput()));
 }
<?php

/**
* FIXME ... just fixme, like 99% of it. Count this as a draft or something. 
* 
*  
* 
* @package test
*
* Trying out File_Archive
*/
require_once '../../autoload.php';
try {
    Module::require('File_Archive');
} catch (Exception $e) {
    die('Script requires PEAR File_Archive to work.');
}
// Set cache dir for Cache Lite
$options = array('cacheDir' => 'tmp');
if (Module::isLoaded('Cache_Lite')) {
    File_Archive::setOption('cache', new Cache_Lite($options));
}
File_Archive::setOption('zipCompressionLevel', 0);
// $files is an array of path to the files that must be added to the archive
File_Archive::extract($files, File_Archive::toArchive('myFiles.zip', File_Archive::toOutput()));
 /**
  * フルバックアップ処理
  * @return boolean true or false
  * @access	private
  */
 function _fullBackUp()
 {
     $config = $this->configView->getConfigByConfname(_SYS_CONF_MODID, "db_kind");
     if ($config) {
         $db_kind = $config["conf_value"];
     } else {
         $db_kind = _DEFAULT_SQL_KIND;
     }
     $backup_class_path = WEBAPP_DIR . "/components/database/" . $db_kind . "/Backup.class.php";
     if (!file_exists($backup_class_path)) {
         if ($db_kind == "mysqli") {
             $db_kind = "mysql";
             $backup_class_path = WEBAPP_DIR . "/components/database/" . $db_kind . "/Backup.class.php";
             if (!file_exists($backup_class_path)) {
                 $this->errorList->add("backup", BACKUP_UNSUPPORTED_DB);
                 return false;
             }
         } else {
             $this->errorList->add("backup", BACKUP_UNSUPPORTED_DB);
             return false;
         }
     }
     include_once $backup_class_path;
     $backup =& new Database_Backup();
     $adodb = $this->db->getAdoDbObject();
     //
     // uploadsテーブルInsert
     //
     $file_path = "backup/";
     $extension = BACKUP_COMPRESS_EXT;
     $download_action_name = BACKUP_DOWNLOAD_ACTION_NAME;
     $file_name = BACKUP_FULL_FILE_NAME . "." . BACKUP_COMPRESS_EXT;
     $mimetype = $this->uploadsView->mimeinfo("type", $file_name);
     $garbage_flag = _ON;
     $params = array('room_id' => 0, 'module_id' => $this->module_id, 'unique_id' => 0, 'file_name' => $file_name, 'physical_file_name' => "", 'file_path' => $file_path, 'action_name' => $download_action_name, 'file_size' => 0, 'mimetype' => $mimetype, 'extension' => $extension, 'garbage_flag' => $garbage_flag);
     $upload_id = $this->uploadsAction->insUploads($params);
     if ($upload_id === false) {
         return false;
     }
     $this->_upload_id = $upload_id;
     $params = array('upload_id' => $upload_id, 'site_id' => $this->session->getParameter("_site_id"), 'url' => '', 'parent_id' => 0, 'thread_num' => 0, 'space_type' => _OFF, 'private_flag' => _OFF, 'room_id' => 0);
     $result = $this->db->insertExecute("backup_uploads", $params, true);
     if ($result === false) {
         $this->_delUploads();
         return false;
     }
     // バックアップ処理中をセッションに保持
     $this->session->setParameter(array("backup", "backingup", $this->_upload_id), _ON);
     $temporary_file_path_dir = WEBAPP_DIR . "/templates_c/" . BACKUP_BACKUP_DIR_NAME . "/";
     if (file_exists($temporary_file_path_dir)) {
         // 現在、同じバックアップファイル作成中
         // エラーとする
         $this->_delUploads();
         $this->errorList->add("backup", BACKUP_BACKINGUP);
         return false;
     }
     if (!mkdir($temporary_file_path_dir, 0777)) {
         $this->_delUploads();
         $this->errorList->add("backup", BACKUP_MKDIR_ERROR);
         return false;
     }
     $temporary_file_path = $temporary_file_path_dir . $upload_id . "/";
     if (!mkdir($temporary_file_path, 0777)) {
         $this->_delUploads();
         $this->errorList->add("backup", BACKUP_MKDIR_ERROR);
         return false;
     }
     $tables_name = $adodb->MetaTables(false, false, $this->db->getPrefix() . "%");
     $db_prefix = $this->db->getPrefix();
     $dump_handle = fopen($temporary_file_path . BACKUP_FULL_SQL_FILE_NAME, "w");
     if (!$dump_handle) {
         $this->_delUploads();
         $this->errorList->add("backup", BACKUP_MKDIR_ERROR);
         return false;
     }
     //
     // SQLダンプファイル作成
     //
     foreach ($tables_name as $table_name) {
         if ($table_name == $this->db->getPrefix() . "session") {
             fwrite($dump_handle, $backup->getBackupSqlDump($table_name, true, true, false));
         } else {
             fwrite($dump_handle, $backup->getBackupSqlDump($table_name));
         }
     }
     fclose($dump_handle);
     $startIndexDirs = split('/', START_INDEX_DIR);
     $htdocsDirs = split('/', HTDOCS_DIR);
     $baseDirs = split('/', BASE_DIR);
     $installIncDirs = split('/', INSTALL_INC_DIR);
     $styleDirs = split('/', STYLE_DIR);
     $uploadDirs = split('/', FILEUPLOADS_DIR);
     $archiveBaseDir = '';
     $directory = $startIndexDirs[0];
     while ($directory == $htdocsDirs[0] && $directory == $installIncDirs[0] && $directory == $baseDirs[0] && $directory == $styleDirs[0] && $directory == $uploadDirs[0]) {
         $archiveBaseDir = $directory . '/';
         array_shift($startIndexDirs);
         array_shift($htdocsDirs);
         array_shift($baseDirs);
         array_shift($installIncDirs);
         array_shift($styleDirs);
         array_shift($uploadDirs);
         if (!isset($startIndexDirs[0])) {
             break;
         }
         $directory = $startIndexDirs[0];
     }
     $source = array();
     $source[] = $temporary_file_path . BACKUP_FULL_SQL_FILE_NAME;
     $archiveDir = isset($startIndexDirs[0]) ? $archiveBaseDir . implode('/', $startIndexDirs) : substr($archiveBaseDir, 0, strlen($archiveBaseDir) - 1);
     $this->_readStartFile($source, START_INDEX_DIR, $archiveDir);
     if (file_exists(dirname(START_INDEX_DIR) . INDEX_FILE_NAME)) {
         $handle = fopen(dirname(START_INDEX_DIR) . INDEX_FILE_NAME, "r");
         if ($handle) {
             $contents = fread($handle, 94);
             fclose($handle);
             if (strpos($contents, 'NetCommons2.0')) {
                 $source[] = File_Archive::read(dirname(START_INDEX_DIR) . INDEX_FILE_NAME, dirname($archiveDir) . INDEX_FILE_NAME);
             }
         }
     } elseif (file_exists(START_INDEX_DIR . '/htdocs' . INDEX_FILE_NAME)) {
         $source[] = File_Archive::read(START_INDEX_DIR . '/htdocs' . INDEX_FILE_NAME, $archiveDir . '/htdocs' . INDEX_FILE_NAME);
     }
     $archiveDir = isset($htdocsDirs[0]) ? $archiveBaseDir . implode('/', $htdocsDirs) : substr($archiveBaseDir, 0, strlen($archiveBaseDir) - 1);
     $this->_readStartFile($source, HTDOCS_DIR, $archiveDir);
     $source[] = File_Archive::read(START_INDEX_DIR . INDEX_FILE_NAME, $archiveDir . INDEX_FILE_NAME);
     $source[] = File_Archive::read(HTDOCS_DIR . '/images', $archiveDir . '/images');
     $source[] = File_Archive::read(HTDOCS_DIR . '/js', $archiveDir . '/js');
     $source[] = File_Archive::read(HTDOCS_DIR . '/themes', $archiveDir . '/themes');
     if (_FULLBACKUP_OUTPUT_SOURCE) {
         $archiveDir = $archiveBaseDir . implode('/', $baseDirs) . '/';
         $source[] = File_Archive::read(BASE_DIR . '/' . MAPLE_DIR, $archiveDir . basename(MAPLE_DIR));
         $webappDirHandle = opendir(WEBAPP_DIR);
         while (false !== ($file = readdir($webappDirHandle))) {
             if ($file == '.' || $file == '..' || $file == 'config' || $file == 'style' || $file == 'uploads' || $file == 'templates_c') {
                 continue;
             }
             $filePath = WEBAPP_DIR . '/' . $file;
             $source[] = File_Archive::read($filePath, $archiveDir . basename(WEBAPP_DIR) . '/' . $file);
         }
         closedir($webappDirHandle);
         $configDirHandle = opendir(WEBAPP_DIR . '/config');
         while (false !== ($file = readdir($configDirHandle))) {
             if ($file == '.' || $file == '..') {
                 continue;
             }
             if ($file == 'install.inc.php') {
                 $filePath = WEBAPP_DIR . '/config/install.inc.dist.php';
                 $source[] = File_Archive::read($filePath, $archiveDir . basename(WEBAPP_DIR) . '/config/install.inc.php');
                 continue;
             }
             $filePath = WEBAPP_DIR . '/config/' . $file;
             $source[] = File_Archive::read($filePath, $archiveDir . basename(WEBAPP_DIR) . '/config/' . $file);
         }
         closedir($configDirHandle);
     }
     $archiveDir = $archiveBaseDir . implode('/', $installIncDirs) . '/';
     $source[] = File_Archive::read(INSTALL_INC_DIR . '/install.inc.php', $archiveDir . '/install.inc.php');
     $archiveDir = $archiveBaseDir . implode('/', $styleDirs);
     $source[] = File_Archive::read(STYLE_DIR, $archiveDir);
     $archiveDir = $archiveBaseDir . implode('/', $uploadDirs);
     $uploadDirHandle = opendir(FILEUPLOADS_DIR);
     $upload_dir_flag = false;
     while (false !== ($file = readdir($uploadDirHandle))) {
         if ($file == '.' || $file == '..' || $file == 'backup') {
             continue;
         }
         $filePath = FILEUPLOADS_DIR . $file;
         $source[] = File_Archive::read($filePath, $archiveDir . $file);
         $upload_dir_flag = true;
     }
     closedir($uploadDirHandle);
     $backupDir = FILEUPLOADS_DIR . 'backup';
     if (is_dir($backupDir)) {
         $uploadIDs = array();
         $params = array(_OFF);
         $sql = "SELECT upload_id " . "FROM {backup_uploads} " . "WHERE space_type = ?";
         $uploadIDs = $this->db->execute($sql, $params);
         if ($uploadIDs === false) {
             $this->db->addError();
             return false;
         }
         $backupDirHandle = opendir($backupDir);
         while (false !== ($file = readdir($backupDirHandle))) {
             if ($file == '.' || $file == '..') {
                 continue;
             }
             $uploadID = str_replace('.' . BACKUP_COMPRESS_EXT, '', $file);
             $needle = array('upload_id' => $uploadID);
             if (in_array($needle, $uploadIDs)) {
                 continue;
             }
             $filePath = $backupDir . '/' . $file;
             $source[] = File_Archive::read($filePath, $archiveDir . basename($backupDir) . '/' . $file);
             $upload_dir_flag = true;
         }
         closedir($backupDirHandle);
     }
     File_Archive::setOption("tmpDirectory", $temporary_file_path);
     $target_file = FILEUPLOADS_DIR . "backup/" . $upload_id . "." . $extension;
     $dest = File_Archive::toArchive($target_file, File_Archive::toFiles());
     if ($upload_dir_flag === false) {
         // uploadsされたものが1件も無い場合
         $archiveDir = $archiveBaseDir . implode('/', $uploadDirs);
         $archiveDir = substr($archiveDir, 0, strlen($archiveDir) - 1);
         $temp_stat = stat(FILEUPLOADS_DIR);
         $stat = array(2 => $temp_stat[2], "mode" => $temp_stat["mode"]);
         $dest->newFile($archiveDir, $stat);
     }
     if (_FULLBACKUP_OUTPUT_SOURCE) {
         $archiveDir = $archiveBaseDir . implode('/', $baseDirs) . '/' . basename(WEBAPP_DIR) . '/' . 'templates_c';
         $temp_stat = stat(WEBAPP_DIR . "/templates_c");
         $stat = array(2 => $temp_stat[2], "mode" => $temp_stat["mode"]);
         $dest->newFile($archiveDir, $stat);
     }
     File_Archive::extract($source, $dest);
     // file_size 更新
     $params = array('file_size' => $this->fileView->getSize($target_file), 'garbage_flag' => _OFF);
     $where_params = array("upload_id" => $upload_id);
     $result = $this->uploadsAction->updUploads($params, $where_params);
     if ($result === false) {
         $this->_delUploads();
         return false;
     }
     //
     // ダンプファイル削除
     //
     unlink($temporary_file_path . BACKUP_FULL_SQL_FILE_NAME);
     rmdir($temporary_file_path);
     rmdir(WEBAPP_DIR . "/templates_c/" . BACKUP_BACKUP_DIR_NAME . "/");
     return true;
 }