PHP备份MSQL源码(2)
}
}
else
{
if($_SESSION['admin']) header("Location:?action=export");
include template('login');
}
break;
default:
header("Location:?action=export");
break;
}
function message($msg,$url_forward='./',$ms=1250)
{
global $charset;
include template("message");
exit;
}
function daddslashes($string, $force = 0)
{
global $magic_quotes_gpc;
if(!$magic_quotes_gpc || $force)
{
if(!is_array($string)) return addslashes($string);
foreach($string as $key => $val) $string[$key] = daddslashes($val, $force);
}
return $string;
}
function file_down($file,$filename='')
{
file_exists($file) or message('文件不存在');
$filename = $filename ? $filename : basename($file);
$filetype = fileext($filename);
$filesize = filesize($file);
header('Cache-control: max-age=31536000');
header('Expires: '.gmdate('D, d M Y H:i:s', time() + 31536000).' GMT');
header('Content-Encoding: none');
header('Content-Length: '.$filesize);
header('Content-Disposition: attachment; filename='.$filename);
header('Content-Type: '.$filetype);
readfile($file);
exit;
}
function fileext($filename)
{
return trim(substr(strrchr($filename, '.'), 1));
}
function cache_read($file, $mode = 'i')
{
$cachefile = './data/'.$file;
if(!file_exists($cachefile)) return array();
return $mode == 'i' ? include $cachefile : file_get_contents($cachefile);
}
function cache_write($file, $string, $type = 'array')
{
if(is_array($string))
{
$type = strtolower($type);
if($type == 'array')
{
$string = "<?php\n return ".var_export($string,TRUE).";\n?>";
}
elseif($type == 'constant')
{
$data='';
foreach($string as $key => $value) $data .= "define('".strtoupper($key)."','".addslashes($value)."');\n";
$string = "<?php\n".$data."\n?>";
}
}
file_put_contents('./data/'.$file, $string);
}
function cache_delete($file)
{
return @unlink('./data/'.$file);
}
function sql_dumptable($table, $startfrom = 0, $currsize = 0)
{
global $db, $sizelimit, $startrow;
if(!isset($tabledump)) $tabledump = '';
$offset = 100;
if(!$startfrom)
{
$tabledump = "DROP TABLE IF EXISTS $table;\n";
$createtable = $db->query("SHOW CREATE TABLE $table");
$create = $db->fetch_row($createtable);
$tabledump .= $create[1].";\n\n";
}
$tabledumped = 0;
$numrows = $offset;
while($currsize + strlen($tabledump) < $sizelimit * 1000 && $numrows == $offset)
{
$tabledumped = 1;
$rows = $db->query("SELECT * FROM $table LIMIT $startfrom, $offset");
$numfields = $db->num_fields($rows);
$numrows = $db->num_rows($rows);
while ($row = $db->fetch_row($rows))
{
$comma = "";
$tabledump .= "INSERT INTO $table VALUES(";
for($i = 0; $i < $numfields; $i++)
{
$tabledump .= $comma."'".mysql_escape_string($row[$i])."'";
$comma = ",";
}
$tabledump .= ");\n";
}
$startfrom += $offset;
}
$startrow = $startfrom;
$tabledump .= "\n";
return $tabledump;
}
function sql_execute($sql)
{
global $db;
$sqls = sql_split($sql);
if(is_array($sqls))
{
foreach($sqls as $sql)
{
if(trim($sql) != '')
{
$db->query($sql);
}
}
}
else
{
$db->query($sqls);
}
return true;
}
function sql_split($sql)
{
global $db_charset, $db;
if($db->version() > '4.1' && $db_charset)
{
$sql = preg_replace("/TYPE=(InnoDB|MyISAM)( DEFAULT CHARSET=[^; ]+)?/", "TYPE=\\1 DEFAULT CHARSET=".$db_charset,$sql);
}
$sql = str_replace("\r", "\n", $sql);
$ret = array();
$num = 0;
$queriesarray = explode(";\n", trim($sql));
unset($sql);
foreach($queriesarray as $query)
{
$ret[$num] = '';
$queries = explode("\n", trim($query));
$queries = array_filter($queries);
foreach($queries as $query)
{
$str1 = substr($query, 0, 1);
if($str1 != '#' && $str1 != '-') $ret[$num] .= $query;
}
$num++;
}
return($ret);
}
function template($tpl)
{
return './templates/'.$tpl.'.tpl.php';
}
/**
* Mysql 数据库类
*/
class db
{
/**
* MySQL 连接标识
* @var resource
*/
var $connid;
/**
* 整型变量用来计算被执行的sql语句数量
* @var int
*/
var $querynum = 0;
/**
* 数据库连接,返回数据库连接标识符
* @param string 数据库服务器主机
* @param string 数据库服务器帐号
* @param string 数据库服务器密码
* @param string 数据库名
* @param bool 是否保持持续连接,1为持续连接,0为非持续连接
* @return link_identifier
*/
function connect($dbhost, $dbuser, $dbpw, $dbname, $pconnect = 0)
{
global $db_charset;
$func = $pconnect == 1 ? 'mysql_pconnect' : 'mysql_connect';
if(!$this->connid = @$func($dbhost, $dbuser, $dbpw))
{
$this->halt('Can not connect to MySQL server');
}
// 当mysql版本为4.1以上时,启用数据库字符集设置
if($this->version() > '4.1' && $db_charset)
{
mysql_query("SET NAMES '".$db_charset."'" , $this->connid);
}
// 当mysql版本为5.0以上时,设置sql mode
if($this->version() > '5.0')
{
mysql_query("SET sql_mode=''" , $this->connid);
}
if($dbname)
{
if(!@mysql_select_db($dbname , $this->connid))
{
$this->halt('Cannot use database '.$dbname);
}
}
return $this->connid;
}
/**
* 选择数据库
* @param string 数据库名
*/
function select_db($dbname)
{
return mysql_select_db($dbname , $this->connid);
}
/**
* 执行sql语句
* @param string sql语句
* @return resource
*/
function query($sql , $type = '')
{
$func = $type == 'UNBUFFERED' ? 'mysql_unbuffered_query' : 'mysql_query';
if(!($query = $func($sql , $this->connid)) && $type != 'SILENT')
{
$this->halt('MySQL Query Error', $sql);
}
$this->querynum++;
return $query;
}
/**
* 执行sql语句,只得到一条记录
* @param string sql语句
* @param string 默认为空,可选值为 CACHE UNBUFFERED
* @param int Cache以秒为单位的生命周期
* @return array
*/
function get_one($sql, $type = '', $expires = 3600, $dbname = '')
{
$query = $this->query($sql, $type, $expires, $dbname);
$rs = $this->fetch_array($query);
$this->free_result($query);
return $rs ;
}
/**
* 从结果集中取得一行作为关联数组
* @param resource 数据库查询结果资源
* @param string 定义返回类型
* @return array
*/
function fetch_array($query, $result_type = MYSQL_ASSOC)
{
return mysql_fetch_array($query, $result_type);
}
/**
* 取得前一次 MySQL 操作所影响的记录行数
* @return int
*/
function affected_rows()
{
return mysql_affected_rows($this->connid);
}
/**
* 取得结果集中行的数目
* @return int
*/
function num_rows($query)
{
return mysql_num_rows($query);
}
/**
* 返回结果集中字段的数目
* @return int
*/
function num_fields($query)
{
return mysql_num_fields($query);
}
/**
* @return array
*/
function result($query, $row)
{
return @mysql_result($query, $row);
}
function free_result($query)
{
return mysql_free_result($query);
}
/**
* 取得上一步 INSERT 操作产生的 ID
* @return int
*/
function insert_id()
{
return mysql_insert_id($this->connid);
}
/**
* @return array
*/
function fetch_row($query)
{
return mysql_fetch_row($query);
}
/**
* @return string
*/
function version()
{
return mysql_get_server_info($this->connid);
}
function close()
{
return mysql_close($this->connid);
}
/**
* @return string
*/
function error()
{
return @mysql_error($this->connid);
}
/**
* @return int
*/
function errno()
{
return intval(@mysql_errno($this->connid)) ;
}
/**
* 显示mysql错误信息
*/
function halt($message = '', $sql = '')
{
exit("MySQL Query:$sql <br> MySQL Error:".$this->error()." <br> MySQL Errno:".$this->errno()." <br> Message:$message");
}
}
?>
- 最新评论