原文地址:点击打开链接
今天无聊,想看看phpcmsv9是怎么在后台配置参数然后生成配置文件的,所以查看了下地址例如:
http:/index.php?m=admin&c=setting&a=init&menuid=959&pc_hash=UNMyqC
可以看出它是调用admin里面的一个setting.php文件的一个init的方法,由这个方法调用的模板又可以看出,所有设置的参数都由form传给了setting.php的save方法。
save方法如下:
public function save() {
$setting = array();
$setting['admin_email'] = is_email($_POST['setting']['admin_email']) ? trim($_POST['setting']['admin_email']) : showmessage(L('email_illegal'),HTTP_REFERER);
$setting['maxloginfailedtimes'] = intval($_POST['setting']['maxloginfailedtimes']);
$setting['minrefreshtime'] = intval($_POST['setting']['minrefreshtime']);
$setting['mail_type'] = intval($_POST['setting']['mail_type']);
$setting['mail_server'] = trim($_POST['setting']['mail_server']);
$setting['mail_port'] = intval($_POST['setting']['mail_port']);
$setting['mail_user'] = trim($_POST['setting']['mail_user']);
$setting['mail_auth'] = intval($_POST['setting']['mail_auth']);
$setting['mail_from'] = trim($_POST['setting']['mail_from']);
$setting['mail_password'] = trim($_POST['setting']['mail_password']);
$setting['errorlog_size'] = trim($_POST['setting']['errorlog_size']);
$setting = array2string($setting);
$this->db->update(array('setting'=>$setting), array('module'=>'admin')); //存入admin模块setting字段
//如果开始盛大通行证接入,判断服务器是否支持curl
$snda_error = '';
if($_POST['setconfig']['snda_akey'] || $_POST['setconfig']['snda_skey']) {
if(function_exists('curl_init') == FALSE) {
$snda_error = L('snda_need_curl_init');
$_POST['setconfig']['snda_enable'] = 0;
}
}
set_config($_POST['setconfig']); //保存进config文件
$this->setcache();
showmessage(L('setting_succ').$snda_error, HTTP_REFERER);
}
仔细看了一下,这函数最好会调用一个set_config的方法把post过来的变量保存到config文件里面。
然后我再看看这个set_config究竟写了什么,经过查找了一下,发现它是在/phpcms/modules/admin/functions/global.func.php里面,set_config函数如下:
function set_config($config, $filename="system") {
$configfile = CACHE_PATH.'configs'.DIRECTORY_SEPARATOR.$filename.'.php';
if(!is_writable($configfile)) showmessage('Please chmod '.$configfile.' to 0777 !');
$pattern = $replacement = array();
foreach($config as $k=>$v) {
if(in_array($k,array('js_path','css_path','img_path','attachment_stat','admin_log','gzip','errorlog','phpsso','phpsso_appid','phpsso_api_url','phpsso_auth_key','phpsso_version','connect_enable', 'upload_url','sina_akey', 'sina_skey', 'snda_enable', 'snda_status', 'snda_akey', 'snda_skey', 'qq_akey', 'qq_skey'))) {
$v = trim($v);
$configs[$k] = $v;
$pattern[$k] = "/'".$k."'\s*=>\s*([']?)[^']*([']?)(\s*),/is";
$replacement[$k] = "'".$k."' => \${1}".$v."\${2}\${3},";
}
}
$str = file_get_contents($configfile);
$str = preg_replace($pattern, $replacement, $str);
return pc_base::load_config('system','lock_ex') ? file_put_contents($configfile, $str, LOCK_EX) : file_put_contents($configfile, $str);
}
终于发现,原来它是用pre_replace函数来替换配置文件的内容,具体怎么用就查查正则表达式咯。
最好file_put_contents一下就ok。个人意见:phpcmsv9里面的正则已经写得很好了,需要的话,直接参考来用就行了。配置文件的格式来来去去都这样,变化不大。