sablog-x的安装文件install的分析
<?php
error_reporting(7);
define('SABLOG_ROOT', TRUE);
ob_start();
//ob_start() 输出缓存
// 允许程序在 register_globals = off 的环境下工作
//function_exists()检查函数是否存在支持
//ini_get()获取配置文件php.ini的某选项的值,替换函数get_cfg_var();
//addslashes -- 使用反斜线引用字符串
//
//extract(addslashes($_POST)); --处理POST表单
//把客户端<FORM METHOD="POST"...>表单中的变量名取出来。
//
//extract(addslashes($_GET)); --处理GET表单
//把客户端<FORM METHOD="GET"...>表单中的变量名取出来。
//譬如变量$_POST['不良倾向'];
//extract后
//该变量就成为$不良倾向;
if (function_exists('ini_get')) {
$onoff = ini_get('register_globals');
} else {
$onoff = get_cfg_var('register_globals');
}
if ($onoff != 1) {
@extract($_POST, EXTR_SKIP);
@extract($_GET, EXTR_SKIP);
}
// stripslashes_array去除转义字符
//intval -- 获取变量的整数值
//is_string
//is_array
function stripslashes_array(&$array) {
while(list($key,$var) = each($array)) {
if ($key != 'argc' && $key != 'argv' && (strtoupper($key) != $key || ''.intval($key) == "$key")) {
if (is_string($var)) {
$array[$key] = stripslashes($var);
}
if (is_array($var)){
$array[$key] = stripslashes_array($var);
}
}
}
return $array;
}
// get_magic_quotes_gpc() 判断 magic_quotes_gpc 状态
if (get_magic_quotes_gpc()) {
$_GET = stripslashes_array($_GET);
$_POST = stripslashes_array($_POST);
}
//set_magic_quotes_runtime(0);
//set_magic_quotes_runtime是用来设置PHP 环境配置的变量 magic_quotes_runtime 值。
//0-关闭 1-打开
//
//程序中检测状态用get_magic_quotes_runtime,返回 0 表示关闭本功能;返回 1 表示本功能打开。若 magic_quotes_runtime 打开时,所有外部引入的数据库资料或者文件等等都会自动转为含有反斜线溢出字符的资料。
//当你的数据中有一些
//\
//"
//'
//这样的字符要写入到数据库里面,又想不被过滤掉的时候,它就很有用,会在这些字符前加上\
//
//中国\地大物博"哈哈"
//
//中国\\地大物博\"哈哈\
set_magic_quotes_runtime(0);
$step = (isset($_GET['step'])) ? $_GET['step'] : $_POST['step'];
$php_self = $_SERVER['PHP_SELF'] ? $_SERVER['PHP_SELF'] : $_SERVER['SCRIPT_NAME'];
$dbcharset = 'utf8';
$configfile = '../admin/config.php';
$sqlfile = 'install.sql';
if(!is_readable($sqlfile)) {
exit('数据库文件不存在或者读取失败');
}
//fopen(文件,权限)
//fread()读取
//fclose()关闭
//is_readable()判断是否存在或者是否能读
//file_exists()文件是否存在
//is_writeable()是否可写?
$fp = fopen($sqlfile, 'rb');
$sql = fread($fp, 2048000);
fclose($fp);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>SaBlog-X安装脚本</title>
<link href="install.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="main">
<form method="post" action="<?php echo $php_self;?>">
<p class="title">SaBlog-X V1.2安装向导</p>
<hr noshade="noshade" />
<?php
if (empty($step) || $step == '1') {
?>
<p class="title">第一步:安装须知</p>
<p>欢迎使用 SaBlog-X V1.2,中本脚本将帮助您将程序完整地安装在您的服务器内。请您先确认以下安装配置: </p>
<ul>
<li>MySQL 主机名称/IP 地址 </li>
<li>MySQL 用户名和密码 </li>
<li>MySQL 数据库名称 (如果没有创建新数据库的权限) </li>
<li>./admin/backupdata 目录权限为 0777 (*nix系统) </li>
<li>./attachments 目录权限为 0777 (*nix系统) </li>
<li>./admin/config.php 文件权限为 0777 (*nix系统) </li>
<li>./cache 目录权限为 0777 (*nix系统) </li>
<li>./templates 及此文件夹下的所有文件(夹)权限为 0777 (*nix系统) - <strong>强烈不推荐</strong></li>
</ul>
<p>如果您无法确认以上的配置信息, 请与您的服务商联系, 我们无法为您提供任何帮助.</p>
<hr noshade="noshade" />
<p align="right">
<input type="hidden" name="step" value="2" />
<input class="formbutton" type="submit" value="下一步" />
</p>
<?php
} elseif ($step == '2') {
$exist_error = FALSE;
$write_error = FALSE;
if (file_exists($configfile)) {
$fileexists = result(1, 0);
} else {
$fileexists = result(0, 0);
$exist_error = TRUE;
}
if (is_writeable($configfile)) {
$filewriteable = result(1, 0);
} else {
$filewriteable = result(0, 0);
$write_error = TRUE;
}
if ($exist_error) {
$config_info = 'config.php 文件不存在, 无法继续.';
} elseif($write_error) {
$config_info = '安装向导无法写入配置文件, 请修改配置文件权限.';
}
?>
<p class="title">第二步:配置数据库信息</p>
<p>config.php 存在检查 <?php echo $fileexists;?></p>
<p>config.php 可写检查 <?php echo $filewriteable;?></p>
<?php
if ($config_info) {
?>
<p><?php echo $config_info;?></p>
<hr noshade="noshade" />
<p align="right">
<input class="formbutton" type="button" value="上一步" onclick="history.back(1)" />
</p>
<?php
} else {
?>
<hr noshade="noshade" />
<table width="100%" border="0" cellspacing="0" cellpadding="4">
<tr>
<td width="30%" nowrap>服务器地址:</td>
<td><input type="text" value="" name="servername" class="formfield" style="width:150px"> 一般是 localhost</td>
</tr>
<tr>
<td width="30%" nowrap>数据库名:</td>
<td><input type="text" value="" name="dbname" class="formfield" style="width:150px"></td>
</tr>
<tr>
<td width="30%" nowrap>数据库用户名:</td>
<td><input type="text" value="" name="dbusername" class="formfield" style="width:150px"></td>
</tr>
<tr>
<td width="30%" nowrap>数据库用户密码:</td>
<td><input type="password" value="" name="dbpassword" class="formfield" style="width:150px"></td>
</tr>
<tr>
<td width="30%" nowrap>数据表前缀:</td>
<td><input type="text" value="" name="db_prefix" class="formfield" style="width:150px"> 不填则默认是 sablog_</td>
</tr>
<tr>
<td width="30%" nowrap>密匙:</td>
<td><input type="text" value="" name="dellog_pass" class="formfield" style="width:150px"></td>
</tr>
<tr>
<td colspan="2">要删除后台的管理记录以及登陆记录, 以及各种危险操作, 必须输入此密匙.</td>
</tr>
</table>
<p> </p>
<p>如果您无法确认以上的配置信息, 请与您的服务商联系, 我们无法为您提供任何帮助.</p>
<hr noshade="noshade" />
<p align="right">
<input type="hidden" name="step" value="3" />
<input class="formbutton" type="submit" value="下一步" />
</p>
<?php
}
} elseif ($step == '3') {
if(trim($_POST['dbname']) == "" || trim($_POST['servername']) == "" || trim($_POST['dbusername']) == "" || trim($_POST['dellog_pass'] == "")){
?>
<p>请返回并确认所有选项均已填写.</p>
<hr noshade="noshade" />
<p align="right">
<input class="formbutton" type="button" value="上一步" onclick="history.back(1)" />
</p>
<?php
} else {
?>
<p class="title">第三步:设置管理员账号</p>
<?php
if(is_writeable($configfile)) {
//trim()除去字符串两端的空格 、rtrim()去除尾部空格 itrim ()去除首部空格;str_replace()去除字符串中间空格
$servername = trim($_POST['servername']);
$dbusername = trim($_POST['dbusername']);
$dbpassword = trim($_POST['dbpassword']);
$dbname = trim($_POST['dbname']);
$db_prefix = trim($_POST['db_prefix']);
$dellog_pass = trim($_POST['dellog_pass']);
$db_prefix = $db_prefix ? $db_prefix : 'sablog_';
$fp = @fopen($configfile, 'r');
$filecontent = @fread($fp, @filesize($configfile));
@fclose($fp);
//preg_replace(正则匹配的源字符串,替换后新字符串,所在文件名) 替换函数.注意正则表示
$filecontent = preg_replace("/[$]servername\s*\=\s*[\"'].*?[\"']/is", "\$servername = '$servername'", $filecontent);
$filecontent = preg_replace("/[$]dbusername\s*\=\s*[\"'].*?[\"']/is", "\$dbusername = '$dbusername'", $filecontent);
$filecontent = preg_replace("/[$]dbpassword\s*\=\s*[\"'].*?[\"']/is", "\$dbpassword = '$dbpassword'", $filecontent);
$filecontent = preg_replace("/[$]dbname\s*\=\s*[\"'].*?[\"']/is", "\$dbname = '$dbname'", $filecontent);
$filecontent = preg_replace("/[$]db_prefix\s*\=\s*[\"'].*?[\"']/is", "\$db_prefix = '$db_prefix'", $filecontent);
$filecontent = preg_replace("/[$]dellog_pass\s*\=\s*[\"'].*?[\"']/is", "\$dellog_pass = '$dellog_pass'", $filecontent);
$fp = @fopen($configfile, 'w');
@fwrite($fp, trim($filecontent));
@fclose($fp);
}
include ($configfile);
include ('../include/func_db_mysql.php');
$DB = new DB_MySQL;
$DB->connect($servername, $dbusername, $dbpassword, $dbname, $usepconnect);
unset($servername, $dbusername, $dbpassword, $usepconnect);
$msg = '';
$quit = FALSE;
$curr_os = PHP_OS;
$curr_php_version = PHP_VERSION;
if($curr_php_version < '4.0.6') {
$msg .= "<font color=\"#FF0000\">您的PHP版本低于4.0.6, 无法使用 SaBlog-X</font><br />";
$quit = TRUE;
}
[ 本帖最后由 大虫 于 2007-1-29 22:41 编辑 ]
$query = $DB->query("SELECT VERSION()");
$curr_mysql_version = $DB->result($query, 0);
if($curr_mysql_version < '3.23') {
$msg .= "<font color=\"#FF0000\">您的MySQL版本低于3.23, 由于程序没有经过此平台的测试, 建议您换 MySQL4 的数据库服务器.</font><br />";
$quit = TRUE;
}
if(strstr($db_prefix, '.')) {
$msg .= "<font color=\"#FF0000\">您指定的数据表前缀包含点字符,请返回修改.</font><br />";
$quit = TRUE;
}
$DB->select_db($dbname);
if($DB->geterrdesc()) {
if(mysql_get_server_info() > '4.1') {
$DB->query("CREATE DATABASE $dbname DEFAULT CHARACTER SET $dbcharset");
} else {
$DB->query("CREATE DATABASE $dbname");
}
if($DB->geterrdesc()) {
$msg .= "<font color=\"#FF0000\">指定的数据库不存在, 系统也无法自动建立, 无法安装 SaBlog-X.</font><br />";
$quit = TRUE;
} else {
$DB->select_db($dbname);
$msg .= "成功建立指定数据库<br />";
}
}
$query - $DB->query("SELECT COUNT(*) FROM ".$db_prefix."settings", 'SILENT');
if(!$DB->geterrdesc()) {
$msg .= "<font color=\"#FF0000\">数据库中已经安装过 SaBlog-X, 继续安装会清空原有数据.</font><br />";
$alert = " onclick=\"return confirm('继续安装会清空全部原有数据, 您确定要继续吗?');\"";
} else {
$alert = '';
}
if($quit) {
$msg .= "<font color=\"#FF0000\">由于您目录属性或服务器配置原因, 无法继续安装 SaBlog-X, 请仔细阅读安装说明.</font>";
} else {
$msg .= "您的服务器可以安装和使用 SaBlog-X, 请进入下一步安装.";
}
if ($msg) {
echo "<p>".$msg."</p>";
}
if($quit) {
?>
<p align="right">
<input type="button" value="重新执行此步骤" onclick="javascript: window.location=('install.php?step=environment');"> <input type="button" value="退出" onclick="javascript: window.close();">
</p>
<?
} else {
?>
<p> ;</p>
<table width="100%" border="0" cellspacing="0" cellpadding="4">
<tr>
<td width="30%" nowrap>用户名:</td>
<td><input type="text" value="" name="username" class="formfield" style="width:150px"></td>
</tr>
<tr>
<td width="30%" nowrap>昵称:</td>
<td><input type="text" value="" name="nickname" class="formfield" style="width:150px"> 可选</td>
</tr>
<tr>
<td width="30%" nowrap>密码:</td>
<td><input type="password" value="" name="password" class="formfield" style="width:150px"></td>
</tr>
<tr>
<td width="30%" nowrap>确认密码:</td>
<td><input type="password" value="" name="comfirpassword" class="formfield" style="width:150px"></td>
</tr>
</table>
<p> ;</p>
<hr noshade="noshade" />
<p align="right">
<input type="hidden" name="step" value="4" />
<input class="formbutton" type="submit" value="下一步" <?php echo $alert;?> />
</p>
<?php
}
}
} elseif ($step == '4') {
//addslashes()自动将字符串的'等符号加上转义符.注意:该函数无法阻拦;号的注入
$username = addslashes(trim($_POST['username']));
$nickname = addslashes(trim($_POST['nickname']));
$password = addslashes(trim($_POST['password']));
$comfirpassword = addslashes(trim($_POST['comfirpassword']));
?>
<p class="title">第四步:检查信息合法性</p>
<?php
if (trim($_POST['username']) == "" || trim($_POST['password']) == "" || trim($_POST['comfirpassword']) == "") {
$msg = "<p>请返回并输入所有必填选项, 请返回重新输入.</p>";
$quit = TRUE;
} elseif (strlen($_POST['password']) < 8) {
$msg = "<p>从系统的安全角度考虑, 密码长度不能少于8字节, 请返回重新输入.</p>";
$quit = TRUE;
} elseif ($_POST['password'] != $_POST['comfirpassword']) {
$msg = "<p>两个输入的密码不相同, 请返回重新输入.</p>";
$quit = TRUE;
} else {
$msg = "<p>检查信息合法性... 成功</p>";
$quit = FALSE;
}
if ($quit) {
echo $msg;
?>
<hr noshade="noshade" />
<p align="right">
<input class="formbutton" type="button" value="上一步" onclick="history.back(1)" />
</p>
<?php
} else {
echo $msg;
?>
<p> ;</p>
<p>用户名: <?php echo $username;?><input type="hidden" name="username" value="<?php echo $username;?>" /></p>
<?php
if ($nickname) {
?>
<p>昵称: <?php echo $nickname;?><input type="hidden" name="nickname" value="<?php echo $nickname;?>" /></p>
<?php
}
?>
<p>密码: <?php echo $password;?><input type="hidden" name="password" value="<?php echo $password;?>" /></p>
<p> ;</p>
<p>核对无误后点击下一步开始导入数据.</p>
<hr noshade="noshade" />
<p align="right">
<input type="hidden" name="step" value="5" />
<input class="formbutton" type="submit" value="下一步" />
</p>
<?php
}
} elseif ($step == '5') {
$username = addslashes(trim($_POST['username']));
$nickname = addslashes(trim($_POST['nickname']));
$password = addslashes(trim($_POST['password']));
?>
<p class="title">第五步:导入数据</p>
<p>
<?php
include ($configfile);
include ('../include/func_db_mysql.php');
$DB = new DB_MySQL;
$DB->connect($servername, $dbusername, $dbpassword, $dbname, $usepconnect);
unset($servername, $dbusername, $dbpassword, $usepconnect);
runquery($sql);
$DB->query("INSERT INTO ".$db_prefix."admin (username, nickname, password, allowarticle, allowattachment, allowcache, allowcategory, allowcomment, allowconfigurate, allowdatabase, allowlinks, allowlog, allowtags, allowtemplate, allowtrackback, allowuser) VALUES ('".$username."', '".$nickname."', '".md5($password)."', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1')");
?>
</p>
<p>共创建了<?php echo $tablenum;?>个数据表.</p>
<hr noshade="noshade" />
<p>安装程序已经顺利执行完毕,请尽快删除整个 install 目录,以免被他人恶意利用。</p>
<p>感谢您使用Sa系列Web应用程序.</p>
<p> ;</p>
<p>用户名: <?php echo $username;?></p>
<p>密码: <?php echo $password;?></p>
<p> ;</p>
<p><a href="../">点击这里进入首页</a><br /><a href="../admin/admincp.php">点击这里进入后台</a></p>
<hr noshade="noshade" />
<p align="right"><a href="http://www.4ngel.net">Welcome to Security Angel Team</a></p>
<?php
}
?>
</form>
</div>
<strong>Powered by SaBlog-X 1.2 (C) 2003-2005 Security Angel Team</strong>
</body>
</html>
<?php
function result($result = 1, $output = 1) {
if($result) {
$text = '... <font color="#0000EE">Yes</font><br />';
if(!$output) {
return $text;
}
echo $text;
} else {
$text = '... <font color="#FF0000">No</font><br />';
if(!$output) {
return $text;
}
echo $text;
}
}
function runquery($sql) {
global $dbcharset, $db_prefix, $DB, $tablenum;
// str_replace(代替换字符串,替换字符串,字符串):将所有的代替换字符串替换成新的。
// 返回:替换后的字符串
//
// explode--使用一个字符串分割另一个字符串
//描述
//array explode ( string separator, string string [, int limit] )
//此函数返回由字符串组成的数组,每个元素都是 string 的一个子串,它们被字符串 separator 作为边界点分割出来。如果设置了 limit 参数,则返回的数组包含最多 limit 个元素,而最后那个元素将包含 string 的剩余部分。
//
//如果 separator 为空字符串(""),explode() 将返回 FALSE。如果 separator 所包含的值在 string 中找不到,那么 explode() 将返回包含 string 单个元素的数组。
//
//如果 limit 参数是负数,则返回除了最后的 limit 个元素外的所有元素。
//substr(字符串,起始位,末位);截取字符串
$sql = str_replace("\r", "\n", str_replace(' sablog_', ' '.$db_prefix, $sql));
$ret = array();
$num = 0;
foreach(explode(";\n", trim($sql)) as $query) {
$queries = explode("\n", trim($query));
foreach($queries as $query) {
$ret[$num] .= $query == '#' ? '' : $query;
}
$num++;
}
unset($sql);
foreach($ret as $query) {
$query = trim($query);
if($query) {
if(substr($query, 0, 12) == 'CREATE TABLE') {
$name = preg_replace("/CREATE TABLE (+) .*/is", "\\1", $query);
echo '创建表 '.$name.' ... <font color="#0000EE">成功</font><br />';
$DB->query(createtable($query, $dbcharset));
$tablenum++;
} else {
$DB->query($query);
}
}
}
}
//strtoupper() Make a string uppercase
function createtable($sql, $dbcharset) {
$type = strtoupper(preg_replace("/^\s*CREATE TABLE\s+.+\s+\(.+?\).*(ENGINE|TYPE)\s*=\s*(+?).*$/isU", "\\2", $sql));
$type = in_array($type, array('MYISAM', 'HEAP')) ? $type : 'MYISAM';
return preg_replace("/^\s*(CREATE TABLE\s+.+\s+\(.+?\)).*$/isU", "\\1", $sql).
(mysql_get_server_info() > '4.1' ? " ENGINE=$type DEFAULT CHARSET=$dbcharset" : " TYPE=$type");
}
?> sax的后台看着恐怖,眼都花了。
准备学习UI。。。。
回复 #3 esnak 的帖子
-_-!!!!牛就一嗰字吖........... 我看不懂哪里有漏洞偶想拜师学习PHP 为什么sablog转帖都不会自动整理,像其它blog帖上去就好了? 原帖由 nuet 于 2007-1-30 04:51 发表
我看不懂哪里有漏洞
偶想拜师学习PHP
大虫没搞安全检测,只是分析源代码! 原帖由 伤心♂oO○ 于 2007-1-30 11:47 发表
大虫没搞安全检测,只是分析源代码!
呵呵。
bingo
说对了
页:
[1]