如果在WHM中创建帐户时选择了某个计划,我正在设置一个postwwwacct脚本来设置预先填充了一些内容,预装插件等的WordPress安装。
这个WordPress安装基本上是从我可以更改/更新的默认安装克隆的。我的默认需求发生了变化。
我可能是一名中级PHP开发人员,但我是这类根级别hashbang PHP的新手,所以我想知道的是我写的脚本是否安全,以及是否有任何应该做得更好/不同的明显事情。
这就是我所拥有的:
#!/usr/bin/php -q
<?php
/**
* This script will automatically set up a mirror of default.example.com in a newly created account
**/
// Set up our variables to be usable by PHP
$opts = array();
$argv0 = array_shift($argv);
while(count($argv)) {
$key = array_shift($argv);
$value = array_shift($argv);
$opts[$key] = $value;
}
// Only do it if the plan is "WP Unlimited"
if($opts['plan'] !== "WP Unlimited") exit();
// Set up a few variables
$db_user = 'root';
$db_pass = 'ROOTPASSWORD';
$db_create = $opts['user'] . '_wp';
// Copy files recursively from /home/default/public_html/
exec("cp -R /home/default/public_html/* /home/{$opts['user']}/public_html; chown -R {$opts['user']}:{$opts['user']} /home/{$opts['user']}/public_html/*");
// Set up database
$conn = mysql_connect('localhost', $db_user, $db_pass);
mysql_query("CREATE DATABASE $db_create", $conn);
// Dump data from default.example.com into new DB
exec("mysqldump -h localhost -u $db_user -p$db_pass default_wp | mysql -h localhost -u $db_user -p$db_pass $db_create");
// Use WordPress' built-in configuration file maker to write config file
$_POST['dbname'] = $db_create;
$_POST['uname'] = $opts['user'];
$_POST['pwd'] = $opts['pass'];
$_POST['dbhost'] = 'localhost';
shell_exec("/home/{$opts['user']}/public_html/wp-admin/setup-config.php?step=2");
?>
我在这里问两个因为我不确定脚本中shell_exec()
s的安全性,还因为我必须创建帐户才能测试它,我宁愿避免任何我可能犯过的愚蠢错误,而且我的服务器上没有十亿个假帐户:)
谢谢!
答案 0 :(得分:0)
任何有像exec这样的shell命令的东西只是回答你的问题是不安全的。话虽如此,没有一种优雅的方式与WHM等进行通信。但是你可以将你的POST变量包含在mysql_real_escape_string中以防止SQL注入攻击。
$_POST['dbname'] = mysql_real_escape_string($db_create);
$_POST['uname'] = mysql_real_escape_string($opts['user']);
$_POST['pwd'] = mysql_real_escape_string($opts['pass']);
$_POST['dbhost'] = 'localhost';