/** * 备份数据库 * @param string $type * @param array $tables * @param string $path * @return array */ public function backupDB($type = "系统自动备份", $tables = array(), $path = DB_Backup_PATH) { $M = M(); function_exists('set_time_limit') && set_time_limit(0); //防止备份数据过程超时 /** * 如果备份文件夹不存在,则自动建立 */ if (!is_dir(DB_Backup_PATH)) { File::makeDir(DB_Backup_PATH, 0777); } G('Backup_start'); $pre = "# -----------------------------------------------------------\n" . "# " . get_opinion('title') . " database backup files\n" . "# URL: " . get_opinion('site_url') . "\n" . "# Type: {$type}\n"; $MySQLLogic = new \Common\Util\MySQLUtil(); $bdTable = $MySQLLogic->backupTable($tables); //取得表结构信息 $outPut = ""; $file_n = 1; $backedTable = array(); foreach ($tables as $table) { $backedTable[] = $table; $outPut .= "\n\n# 数据库表:{$table} 数据信息\n"; $tableInfo = $M->query("SHOW TABLE STATUS LIKE '{$table}'"); $page = ceil($tableInfo[0]['Rows'] / 10000) - 1; for ($i = 0; $i <= $page; $i++) { $query = $M->query("SELECT * FROM {$table} LIMIT " . $i * 10000 . ", 10000"); foreach ($query as $val) { $temSql = ""; $tn = 0; $temSql = ''; foreach ($val as $v) { $temSql .= $tn == 0 ? "" : ","; $temSql .= $v == '' ? "''" : "'{$v}'"; $tn++; } $temSql = "INSERT INTO `{$table}` VALUES ({$temSql});\n"; $sqlNo = "\n# Time: " . date("Y-m-d H:i:s") . "\n" . "# -----------------------------------------------------------\n" . "# 当前SQL卷标:#{$file_n}\n# -----------------------------------------------------------\n\n\n"; if ($file_n == 1) { $sqlNo = "# Description:当前SQL文件包含了表:" . implode("、", $tables) . "的结构信息,表:" . implode("、", $backedTable) . "的数据" . $sqlNo; } else { $sqlNo = "# Description:当前SQL文件包含了表:" . implode("、", $backedTable) . "的数据" . $sqlNo; } if (strlen($pre) + strlen($sqlNo) + strlen($bdTable) + strlen($outPut) + strlen($temSql) > get_opinion("sqlFileSize")) { $file_name = $path . "_" . $file_n . ".sql"; $outPut = $file_n == 1 ? $pre . $sqlNo . $bdTable . $outPut : $pre . $sqlNo . $outPut; //file_put_contents($file, $outPut, FILE_APPEND); //TODO file_put_contents-->> File::writeFile需要测试 File::writeFile($file_name, $outPut); $bdTable = $outPut = ""; $backedTable = array(); $backedTable[] = $table; $file_n++; } $outPut .= $temSql; } } } if (strlen($bdTable . $outPut) > 0) { $sqlNo = "\n# Time: " . date("Y-m-d H:i:s") . "\n" . "# -----------------------------------------------------------\n" . "# 当前SQL卷标:#{$file_n}\n# -----------------------------------------------------------\n\n\n"; if ($file_n == 1) { $sqlNo = "# Description:当前SQL文件包含了表:" . implode("、", $tables) . "的结构信息,表:" . implode("、", $backedTable) . "的数据" . $sqlNo; } else { $sqlNo = "# Description:当前SQL文件包含了表:" . implode("、", $backedTable) . "的数据" . $sqlNo; } $file_name = $path . "_" . $file_n . ".sql"; $outPut = $file_n == 1 ? $pre . $sqlNo . $bdTable . $outPut : $pre . $sqlNo . $outPut; // file_put_contents($file_name, $outPut, FILE_APPEND); File::writeFile($file_name, $outPut); $file_n++; } G('Backup_end'); $res = array("status" => 1, "info" => "成功备份所选数据库表结构和数据,本次备份共生成了" . ($file_n - 1) . "个SQL文件。耗时:" . G('Backup_start', 'Backup_end') . "秒", "url" => U('Admin/Data/restore')); return $res; }
/** * 远程抓取 * @param $uri * @param $config */ public function getRemoteImage2($uri, $config) { //忽略抓取时间限制 set_time_limit(0); //ue_separate_ue ue用于传递数据分割符号 $imgUrls = explode("ue_separate_ue", $uri); $tmpNames = array(); foreach ($imgUrls as $imgUrl) { //http开头验证 if (strpos($imgUrl, "http") !== 0) { array_push($tmpNames, "https error"); continue; } //sae环境 不兼容 if (!defined('SAE_TMP_PATH')) { //获取请求头 $heads = get_headers($imgUrl); //死链检测 if (!(stristr($heads[0], "200") && stristr($heads[0], "OK"))) { array_push($tmpNames, "get_headers error"); continue; } } //格式验证(扩展名验证和Content-Type验证) $fileType = strtolower(strrchr($imgUrl, '.')); if (!in_array($fileType, $config['allowFiles']) || stristr($heads['Content-Type'], "image")) { array_push($tmpNames, "Content-Type error"); continue; } //打开输出缓冲区并获取远程图片 ob_start(); $context = stream_context_create(array('http' => array('follow_location' => false))); //请确保php.ini中的fopen wrappers已经激活 readfile($imgUrl, false, $context); $img = ob_get_contents(); ob_end_clean(); //大小验证 $uriSize = strlen($img); //得到图片大小 $allowSize = 1024 * $config['maxSize']; if ($uriSize > $allowSize) { array_push($tmpNames, "maxSize error"); continue; } $savePath = $config['savePath']; if (!defined('SAE_TMP_PATH')) { //非SAE //创建保存位置 if (!file_exists($savePath)) { mkdir($savePath, 0777, true); } //写入文件 $tmpName = $savePath . rand(1, 10000) . time() . strrchr($imgUrl, '.'); try { File::writeFile($tmpName, $img, "a"); array_push($tmpNames, __ROOT__ . '/' . $tmpName); } catch (\Exception $e) { array_push($tmpNames, "error"); } } else { //SAE $Storage = new \SaeStorage(); $domain = get_opinion('SaeStorage'); $destFileName = 'remote/' . date('Y') . '/' . date('m') . '/' . rand(1, 10000) . time() . strrchr($imgUrl, '.'); $result = $Storage->write($domain, $destFileName, $img, -1); Log::write('$destFileName:' . $destFileName); if ($result) { array_push($tmpNames, $result); } else { array_push($tmpNames, "not supported"); } } } /** * 返回数据格式 * { * 'url' : '新地址一ue_separate_ue新地址二ue_separate_ue新地址三', * 'srcUrl': '原始地址一ue_separate_ue原始地址二ue_separate_ue原始地址三', * 'tip' : '状态提示' * } */ //save file info here $return_data['url'] = implode("ue_separate_ue", $tmpNames); $return_data['tip'] = '远程图片抓取成功!'; $return_data['srcUrl'] = $uri; $this->ajaxReturn($return_data); // echo "{'url':'" . implode("ue_separate_ue", $tmpNames) . "','tip':'远程图片抓取成功!','srcUrl':'" . $uri . "'}"; }
/** * */ public function updateHandle() { G("UpdateHandle"); $message = ""; $version = I('get.version'); $now_version = get_opinion('software_build', true); $url = Server_API . 'api/update/' . $now_version . '/'; $json = json_decode(file_get_contents($url), true); G("GetJson"); $message .= "下载Index文件成功,用时 " . G("UpdateHandle", "getJson") . "秒<br />"; if (empty($json)) { $message .= "连接主升级服务器出错,使用备用服务器<br />"; // try backup $url = Server_API2 . 'api/update/' . $now_version . '/'; $json = json_decode(file_get_contents($url), true); G("GetJson"); if (empty($json)) { $this->error('连接升级服务器出错'); } } $target_version_info = $json['file_list'][$version]; if (!empty($target_version_info)) { File::mkDir(WEB_CACHE_PATH); G("WebCache"); $message .= "清空WEB_CACHE_PATH,用时 " . G("GetJson", "WebCache") . "秒<br />"; $file_downloaded = WEB_CACHE_PATH . $target_version_info['file_name']; $file = file_get_contents($target_version_info['file_url']); if (File::writeFile($file_downloaded, $file)) { G("DownFile"); $message .= "下载升级文件成功,用时 " . G("WebCache", "DownFile") . "秒<br />"; } else { $this->error('下载文件失败'); } //calculate md5 of file $file_md5 = md5_file($file_downloaded); G("MD5"); $message .= "文件MD5值: {$file_md5} ,用时 " . G("DownFile", "MD5") . "秒<br />"; //todo 系统备份 $System = new SystemEvent(); //$System->backupFile(); G("BackupFile"); $message .= "系统备份已跳过 ,用时 " . G("MD5", "BackupFile") . "秒<br />"; $zip = new \ZipArchive(); //新建一个ZipArchive的对象 if ($zip->open($file_downloaded) === true) { $zip->extractTo(WEB_ROOT); //假设解压缩到在当前路径下/文件夹内 $zip->close(); //关闭处理的zip文件 File::delFile($file_downloaded); G("UnzipFile"); $message .= "解压成功 ,用时 " . G("BackupFile", "UnzipFile") . "秒<br />"; $System->clearCacheAll(); $message .= "清空缓存成功 <br />"; } else { $this->error('文件损坏'); } $old_build = get_opinion('software_build'); $new_build = $target_version_info['build_to']; set_opinion('software_version', $target_version_info['version_to']); set_opinion('software_build', $target_version_info['build_to']); set_opinion('db_build', $target_version_info['build_to']); if (File::file_exists(Upgrade_PATH . 'init.php')) { include Upgrade_PATH . 'init.php'; if (function_exists("upgrade_" . $old_build . "_to_" . $new_build)) { $fuction_name = "upgrade_" . $old_build . "_to_" . $new_build; G("FunctionStart"); call_user_func($fuction_name); G("FunctionEnd"); $message .= "处理升级函数 ,用时 " . G("FunctionStart", "FunctionEnd") . "秒 <br />"; } } $this->updateComplete('升级成功' . $target_version_info['build_to'] . "<br />" . $message); } else { $this->error('升级出错'); } }
/** * */ public function updateHandle() { $version = I('get.version'); $now_version = get_opinion('software_build', true); $url = Server_API . 'api/update/' . $now_version . '/'; $json = json_decode(file_get_contents($url), true); $target_version_info = $json['file_list'][$version]; if (!empty($target_version_info)) { File::mkDir(WEB_CACHE_PATH); $file_downloaded = WEB_CACHE_PATH . $target_version_info['file_name']; $file = file_get_contents($target_version_info['file_url']); File::writeFile($file_downloaded, $file); //todo 系统备份 $System = new SystemEvent(); //$System->backupFile(); $zip = new \ZipArchive(); //新建一个ZipArchive的对象 if ($zip->open($file_downloaded) === true) { $zip->extractTo(WEB_ROOT); //假设解压缩到在当前路径下/文件夹内 $zip->close(); //关闭处理的zip文件 File::delFile($file_downloaded); $System->clearCacheAll(); } else { $this->error('文件损坏'); } $old_build = get_opinion('software_build'); $new_build = $target_version_info['build_to']; set_opinion('software_version', $target_version_info['version_to']); set_opinion('software_build', $target_version_info['build_to']); if (File::file_exists(Upgrade_PATH . 'init.php')) { include Upgrade_PATH . 'init.php'; if (function_exists("upgrade_" . $old_build . "_to_" . $new_build)) { $fuction_name = "upgrade_" . $old_build . "_to_" . $new_build; call_user_func($fuction_name); } } $this->success('升级成功' . $target_version_info['build_to'], U('Admin/Index/updateComplete')); } else { $this->error('升级出错'); } }
/** * */ public function step5() { File::delAll(RUNTIME_PATH); File::delAll(LOG_PATH); File::delAll(WEB_CACHE_PATH); File::delAll(WEB_ROOT . 'Data/Cache'); File::delAll(WEB_ROOT . 'Data/Temp'); //A('Install/Test')->init($key = 'zts'); $Access = new AccessEvent(); $Access->initAdmin(); $Access->initWeixin(); // File::delAll(WEB_ROOT . 'Data/Install'); if (File::writeFile(WEB_ROOT . 'Data/Install/install.lock', 'installed', 'w+')) { C('URL_MODEL', 3); $this->success('GreenCMS安装成功,5秒钟返回首页', U('Home/Index/index'), 5); } }
/** * */ public function step5() { File::delAll(RUNTIME_PATH); File::delAll(LOG_PATH); File::delAll(WEB_CACHE_PATH); File::delAll(WEB_ROOT . 'Data/Cache'); File::delAll(WEB_ROOT . 'Data/Temp'); // $dirs = array(); // // array_push($dirs, WEB_ROOT . 'Extend'); // array_push($dirs, WEB_ROOT . 'Public'); // array_push($dirs, WEB_ROOT . 'Upload'); // array_push($dirs, WEB_ROOT . 'Data/Cache'); // array_push($dirs, WEB_ROOT . 'Data/Temp'); // array_push($dirs, LOG_PATH); // array_push($dirs, RUNTIME_PATH); // array_push($dirs, WEB_CACHE_PATH); // array_push($dirs, DB_Backup_PATH); // array_push($dirs, System_Backup_PATH); // array_push($dirs, Upgrade_PATH); //build_dir_secure($dirs); //A('Install/Test')->init($key = 'zts'); // $Access = new AccessEvent(); // $Access->initAdmin(); // $Access->initWeixin(); // File::delAll(WEB_ROOT . 'Install/Data'); if (File::writeFile(WEB_ROOT . 'Data/Install.lock', 'installed', 'w+')) { C('URL_MODEL', 3); $this->success('GreenCMS安装成功,5秒钟返回首页', get_opinion("site_url"), 5); } }