plugin('AutoBackup'); $current = Typecho_Date::gmtTime(); //当前时间 $config_file = dirname(__FILE__).'/config.xml'; $xml = simplexml_load_file($config_file); $lasttime = intval($xml->lasttime); if($type==0){ if ($lasttime < 0 || ($current - $lasttime) < $configs->circle * 24 * 60 * 60) { return $contents; } } $file_path = self::create_sql(); //获取备份语句 $xml->lasttime = time(); $xml = $xml->asXML(); $fp = fopen($config_file, 'wb'); fwrite($fp, $xml); fclose($fp); //将备份文件发送至设置的邮箱 $smtp = array(); $smtp['site'] = $options->title; $smtp['attach'] = $file_path; $smtp['attach_name'] = "AutoBackup".date("Ymd", $current).".zip"; if (!function_exists('gzopen')) { $smtp['attach_name'] = "AutoBackup".date("Ymd", $current).".sql"; } //获取SMTP设置 $smtp['user'] = $configs->user; $smtp['pass'] = $configs->pass; $smtp['host'] = $configs->host; $smtp['port'] = $configs->port; $format = "format"; if ($configs->subject != "") { $smtp['subject'] = date("Ymd").'-'.$configs->subject.'-数据库备份文件'; }else { $smtp['subject'] = date("Ymd").'-'.$options->title.'-数据库备份文件'; } $smtp['AltBody'] = ""; $smtp['body'] = '


 '.date("Y年m月d日").'

这是从'.$smtp["site"].'由Typecho AutoBackup插件自动发送的数据库备份文件,备份文件详见邮件附件!

该邮件由您的Typecho博客'.$smtp["site"].'使用的插件AutoBackup发出
如果你没有做相关设置,请联系邮件来源地址'.$smtp["user"].'

'; if($configs->mail != "") { $email_to=$configs->mail; }else { $select = Typecho_Widget::widget('Widget_Abstract_Users')->select()->where('uid',1); $result = $db->query($select); $row = $db->fetchRow($result); $email_to = $row['mail']; } $smtp['to']=$email_to; $smtp['from']=$email_to; self::SendMail($smtp); unlink($file_path); return $contents; } /** * 生成备份sql语句 * * @param string $tables */ private static function create_sql(){ $configs = Helper::options()->plugin('AutoBackup'); $tables = $configs->tables; if (!is_array($tables)){echo "你没有选择任何表"; exit;} $db = Typecho_Db::get(); $sql = "-- Typecho AutoBackup\r\n-- version 1.2.0\r\n-- 生成日期: ".date("Y年m月d日 H:i:s")."\r\n-- 使用说明:创建一个数据库,然后导入文件\r\n\r\n"; foreach ($tables as $table) { //循环获取数据库中数据 $sql .= "\r\nDROP TABLE IF EXISTS ".$table.";\r\n"; $create_sql = $db->fetchRow($db->query("SHOW CREATE TABLE `" . $table . "`")); $sql .= $create_sql['Create Table'].";\r\n"; $result = $db->query($db->select()->from($table)); while ($row = $db->fetchRow($result)) { foreach ($row as $key=>$value) { //每次取一行数据 $keys[] = "`".$key."`"; //字段存入数组 $values[] = "'".addslashes($value)."'"; //值存入数组 } $sql .= "insert into `".$table."` (".implode(",", $keys).") values (".implode(",", $values).");\r\n"; //生成插入语句 //清空字段和值数组 unset($keys); unset($values); } } $file_path = dirname(__FILE__)."/backupfiles/". md5($configs->pass . time()) . ".sql"; file_put_contents($file_path, $sql); if (!function_exists('gzopen')) { return $file_path; } require_once('pclzip.lib.php'); $zip = new PclZip(dirname(__FILE__) . "/backupfiles/" . md5($configs->pass . time()) . ".zip"); $zip->create($file_path, PCLZIP_OPT_REMOVE_PATH, dirname(__FILE__) . "/backupfiles/"); unlink($file_path); return $zip->zipname; } /** * 发送邮件 * * @access public * @param array $smtp 邮件信息 * @return void */ private static function SendMail($smtp) { $options = Helper::options(); // 获取插件配置 $SMTPSecure = $options->plugin('AutoBackup')->SMTPSecure; // SMTP 加密类型 'ssl' or 'tls'. try { $STMPHost = $smtp['host'];//SMTP服务器地址 $SMTPPort = $smtp['port'];//端口 $SMTPUserName = $smtp['user'];//用户名 $SMTPPassword = $smtp['pass'];//邮箱秘钥 $SMTPSecure = $SMTPSecure;//加密方式 $fromMail = $smtp['user'];//发件邮箱 $fromName = '备份小助手';//发件人名字 $fromMailr = $smtp['from'];//收件人邮箱 // Server settings $mail = new PHPMailer(true); $mail->CharSet = PHPMailer::CHARSET_UTF8; $mail->Encoding = PHPMailer::ENCODING_BASE64; $mail->isSMTP(); $mail->Host = $STMPHost; // SMTP 服务地址 $mail->SMTPAuth = true; // 开启认证 $mail->Username = $SMTPUserName; // SMTP 用户名 $mail->Password = $SMTPPassword; // SMTP 密码 $mail->SMTPSecure = $SMTPSecure; // SMTP 加密类型 $mail->Port = $SMTPPort; // SMTP 端口 $mail->setFrom($fromMail, $fromName);//发件人 $mail->addAddress($fromMailr); $mail->Subject = $smtp['subject']; $mail->isHTML(); // 邮件为HTML格式 // 邮件内容 $mail->Body = $smtp['AltBody'].$smtp['body']; $mail->AddAttachment($smtp['attach'], $smtp['attach_name']); $mail->send(); } catch (Exception $e) { echo "网络故障,发送失败!"; exit($e); } } } ?>