PHPv1.0-后台添加频道-非官方补丁--逍遥最新修改版
本帖最后由 youyusj 于 2010-9-14 11:35 编辑引言:昨天发布此补丁后发现问题依然很多,今天发布最新修改版的补丁
大家如果选择使用,希望做好备份工作,否则出了问题哭都没地方哦(开个玩笑)~
5楼有完整代码
现在的后台是不可以添加频道的,无论填写什么都会提示该目录已存在,同时也不能删除频道,修改方法如下:
打开后台文件:websys_channel.php
第8行开始
//如果是修改则不用判断目录是否存在!
if($id!=''){
$sql="select * from {$cfg['tb_pre']}channel where c_id!=$id and c_name='$name'";
}else{
if(is_dir(FR_ROOT."/$channeldir")){showmsg("目录已经存在!","-1",0,2000);exit;}
if($channeltype==2){
//如果添加外部频道只检查频道名称是否存在
$sql="select * from {$cfg['tb_pre']}channel where c_name='$name'";
}else{
$channeldir = preg_replace("/[^a-zA-Z\.-]/i",'',cleartags($channeldir));
if($channeldir==''){showmsg("频道目录名必须为英文字符!","-1",0,2000);exit;}
$sql="select * from {$cfg['tb_pre']}channel where c_name='$name' or c_channeldir='$channeldir'";
}
}
注意看红色的这一句,它就是导致不能添加频道的“主谋”
修改方法为
只要在这句前面加上一个判断
if($channeldir!='')
当$channeldir的值为空时跳过检测语句就可以了,造成这个错误的原因也是因为$channeldir为空引起的
修改后的代码为:
//如果是修改则不用判断目录是否存在!
if($id!=''){
$sql="select * from {$cfg['tb_pre']}channel where c_id!=$id and c_name='$name'";
}else{
if($channeldir!=''){
if(is_dir(FR_ROOT."/$channeldir")){showmsg("目录已经存在!","-1",0,2000);exit;}
}
if($channeltype==2){
//如果添加外部频道只检查频道名称是否存在
$sql="select * from {$cfg['tb_pre']}channel where c_name='$name'";
}else{
// $channeldir = preg_replace("/[^a-zA-Z\.-]/i",'',cleartags($channeldir));
if($channeldir==''){showmsg("频道目录名必须为英文字符!","-1",0,2000);exit;}
$sql="select * from {$cfg['tb_pre']}channel where c_name='$name' or c_channeldir='$channeldir'";
}
}
上面代码红色部分为修改后的代码,解决添加频道提示目录已存在的问题
蓝色代码部分呢是我个人用的,只是把这段转换目录名的代码给注释掉,让它不执行
原因是他不可以添加数字目录比如:hao123,最终会转换为hao,不人性化,所以我把这句给注释掉了
个人根据自己的需要决定是否要修改蓝色部分,修改的方法只是在前面加“//”,并没有做代码上的改动
本来到这里应该完成工作了,可是有朋友反应添加频道后网站就打不开了,我一测试果然如此,首页文件及模板都被替换掉了
所以上面的工作完成后还得接着完成下面的工作才能不出错
查找以下代码
//复制文件
$loadfile=file_get(FR_ROOT."/article/index.php");
$loadfile=str_replace('$cid=10;','$cid='.$cid.';',$loadfile);
$loadfile=str_replace('\'article\');','\''.$channeldir.'\');',$loadfile);
file_put(FR_ROOT."/$channeldir/index.php",$loadfile);
$loadfile=file_get(FR_ROOT."/article/list.php");
$loadfile=str_replace('$cid=10;','$cid='.$cid.';',$loadfile);
$loadfile=str_replace('\'article\');','\''.$channeldir.'\');',$loadfile);
file_put(FR_ROOT."/$channeldir/list.php",$loadfile);
$loadfile=file_get(FR_ROOT."/article/article.php");
$loadfile=str_replace('$cid=10;','$cid='.$cid.';',$loadfile);
$loadfile=str_replace('\'article\');','\''.$channeldir.'\');',$loadfile);
file_put(FR_ROOT."/$channeldir/article.php",$loadfile);
//复制模版
dir_copy(FR_ROOT.'/templates/'.$cfg['template'].'/article',FR_ROOT.'/templates/'.$cfg['template'].'/'.$channeldir.'');
注意看红色代码部分,大家就不难发现问题了,原因是当$channeldir为空时,相应红色代码部分就变成了根目录,那如果根目录里面有同名的文件则就会被替换掉了。
造成这个问题的原因和上面的一样就是没有对$channeldir做判断。修改的方法也很简单,同上面一样,加一个判断
修改后的代码为
//复制文件
//避免根目录文件被覆盖
if($channeldir!=''){
$loadfile=file_get(FR_ROOT."/article/index.php");
$loadfile=str_replace('$cid=10;','$cid='.$cid.';',$loadfile);
$loadfile=str_replace('\'article\');','\''.$channeldir.'\');',$loadfile);
file_put(FR_ROOT."/$channeldir/index.php",$loadfile);
$loadfile=file_get(FR_ROOT."/article/list.php");
$loadfile=str_replace('$cid=10;','$cid='.$cid.';',$loadfile);
$loadfile=str_replace('\'article\');','\''.$channeldir.'\');',$loadfile);
file_put(FR_ROOT."/$channeldir/list.php",$loadfile);
$loadfile=file_get(FR_ROOT."/article/article.php");
$loadfile=str_replace('$cid=10;','$cid='.$cid.';',$loadfile);
$loadfile=str_replace('\'article\');','\''.$channeldir.'\');',$loadfile);
file_put(FR_ROOT."/$channeldir/article.php",$loadfile);
//复制模版
dir_copy(FR_ROOT.'/templates/'.$cfg['template'].'/article',FR_ROOT.'/templates/'.$cfg['template'].'/'.$channeldir.'');
}
只是添加了红色部分的判断
接下来就是频道的删除了
查找以下代码
elseif($do=="del"){
$id = empty($id) ? '' :intval($id);
if($id!=''){
$rs = $db->get_one("select c_channeldir,c_channeltype from {$cfg['tb_pre']}channel where c_channeltype!=0 and c_id='$id'");
if($rs){
if($rs['c_channeltype']==1){
dir_delete(FR_ROOT."/$rs");
dir_delete(FR_ROOT.'/templates/'.$cfg['template'].'/'.$rs['c_channeldir']);
}
$db ->query("delete from {$cfg['tb_pre']}channel where c_id in ($cids)");
}
}
红色部分就是引起错误的原因,c_id in ($cids),因为$cids的值为空所以会出错MYSQL错误,因为频道也没有批量删除功能,没有做相应的处理,这就导致错误发生了,
修改方法:
将红色部分代码修改为
$db ->query("delete from {$cfg['tb_pre']}channel where c_id ='$id'");
就可以了
以上就是后台频道的添加,删除补丁,本人也是PHP新手
如果说明有误还请高手指正,不要见笑,小弟会虚心接受
PS:本人只是在官方的基础上稍做修改,只适用于暂时使用,对修改引起的任何错误本人不作承担,此补丁只是用来应急,还请等待官方正式补丁! 不错 占个位置 很不错啊~为民谋福利 附上本人修改后的完整代码(因为没有附件上传权限,所以只能帖代码,大家复制替换即可)
<?php
require_once(dirname(__FILE__).\'/../config.inc.php\');
require_once(dirname(__FILE__).\'/inc/common.php\');
aPurview(301);
if(empty($do)) $do= \'\';
if($do==\\\"savedata\\\"){
$id = empty($id) ? \'\' :intval($id);
//如果是修改则不用判断目录是否存在!
if($id!=\'\'){
$sql=\\\"select * from {$cfg[\'tb_pre\']}channel where c_id!=$id and c_name=\'$name\'\\\";
}else{
if($channeldir!=\'\'){
if(is_dir(FR_ROOT.\\\"/$channeldir\\\")){showmsg(\\\"目录已经存在!\\\",\\\"-1\\\",0,2000);exit;}
}
if($channeltype==2){
//如果添加外部频道只检查频道名称是否存在
$sql=\\\"select * from {$cfg[\'tb_pre\']}channel where c_name=\'$name\'\\\";
}else{
// $channeldir = preg_replace(\\\"/[^a-zA-Z\\.-]/i\\\",\'\',cleartags($channeldir));
if($channeldir==\'\'){showmsg(\\\"频道目录名必须为英文字符!\\\",\\\"-1\\\",0,2000);exit;}
$sql=\\\"select * from {$cfg[\'tb_pre\']}channel where c_name=\'$name\' or c_channeldir=\'$channeldir\'\\\";
}
}
$rs = $db->get_one($sql);
if($rs){
if($channeltype==2){
showmsg(\\\"对不起该频道已经存在!\\\",\\\"-1\\\",0,2000);exit;
}else{
showmsg(\\\"对不起该频道或目录已经存在!\\\",\\\"-1\\\",0,2000);exit;
}
}
$rs = $db->get_one(\\\"select max(c_order) as m from {$cfg[\'tb_pre\']}channel\\\");
$max=$rs?$rs[\'m\']+1:0;
if($name==\'\'){showmsg(\\\"频道名称不能为!\\\",\\\"-1\\\",0,2000);exit;}
if($channeltype!=2){
$sqlstr=Array(\'c_name\',\'c_shortname\',\'c_itemunit\',\'c_readme\',\'c_keywords\',\'c_description\',\'c_opentype\',\'c_channeltype\',\'c_linkurl\',\'c_channeldir\',\'c_moduletype\',\'c_disabled\',\'c_shownav\',\'c_createhtml\',\'c_fileext\',\'c_listfiletype\',\'c_structuretype\',\'c_filenametype\');
}else{
$sqlstr=Array(\'c_name\',\'c_readme\',\'c_keywords\',\'c_description\',\'c_opentype\',\'c_channeltype\',\'c_linkurl\',\'c_disabled\',\'c_shownav\');
}
if($id!=\'\'){
$sqls=\'\';
foreach($sqlstr as $v){
$s=str_replace(\'c_\',\'\',$v);
if(isset($$s)){
$sqls.=\\\"$v=\'\\\".$$s.\\\"\',\\\";
}
}
$sqls=substr($sqls,0,-1);
$db ->query(\\\"update {$cfg[\'tb_pre\']}channel set $sqls where c_id=\'$id\'\\\");
}else{
$sqls=$sqlss=\'\';
foreach($sqlstr as $v){
$s=str_replace(\'c_\',\'\',$v);
if(isset($$s)){
$sqls.=\\\"$v,\\\";
if($s==\'readme\'||$s==\'keywords\'||$s==\'description\'){$sqlss.=\\\"\'\\\".replace_strbox($$s).\\\"\',\\\";}else{$sqlss.=\\\"\'\\\".cleartags($$s).\\\"\',\\\";}
}
}
$sqls.=\\\"c_order,\\\";$sqlss.=\\\"$max,\\\";
$sqls=substr($sqls,0,-1);$sqlss=substr($sqlss,0,-1);
$db ->query(\\\"INSERT INTO {$cfg[\'tb_pre\']}channel ($sqls) VALUES($sqlss)\\\");
$cid=$db ->insert_id();
//复制文件
//避免根目录文件被覆盖
if($channeldir!=\'\'){
$loadfile=file_get(FR_ROOT.\\\"/article/index.php\\\");
$loadfile=str_replace(\'$cid=10;\',\'$cid=\'.$cid.\';\',$loadfile);
$loadfile=str_replace(\'\\\'article\\\');\',\'\\\'\'.$channeldir.\'\\\');\',$loadfile);
file_put(FR_ROOT.\\\"/$channeldir/index.php\\\",$loadfile);
$loadfile=file_get(FR_ROOT.\\\"/article/list.php\\\");
$loadfile=str_replace(\'$cid=10;\',\'$cid=\'.$cid.\';\',$loadfile);
$loadfile=str_replace(\'\\\'article\\\');\',\'\\\'\'.$channeldir.\'\\\');\',$loadfile);
file_put(FR_ROOT.\\\"/$channeldir/list.php\\\",$loadfile);
$loadfile=file_get(FR_ROOT.\\\"/article/article.php\\\");
$loadfile=str_replace(\'$cid=10;\',\'$cid=\'.$cid.\';\',$loadfile);
$loadfile=str_replace(\'\\\'article\\\');\',\'\\\'\'.$channeldir.\'\\\');\',$loadfile);
file_put(FR_ROOT.\\\"/$channeldir/article.php\\\",$loadfile);
//复制模版
dir_copy(FR_ROOT.\'/templates/\'.$cfg[\'template\'].\'/article\',FR_ROOT.\'/templates/\'.$cfg[\'template\'].\'/\'.$channeldir.\'\');
}
}
showmsg(\\\"操作成功!\\\",\\\"websys_channel.php\\\");exit;
}elseif($do==\\\"stop\\\"){
$id = empty($id) ? \'\' :intval($id);
$id;;$db ->query(\\\"update {$cfg[\'tb_pre\']}channel set c_disabled=1 where c_id=\'$id\'\\\");
showmsg(\\\"操作成功!\\\",\\\"websys_channel.php\\\");exit;
}elseif($do==\\\"start\\\"){
$id = empty($id) ? \'\' :intval($id);
$id;;$db ->query(\\\"update {$cfg[\'tb_pre\']}channel set c_disabled=0 where c_id=\'$id\'\\\");
showmsg(\\\"操作成功!\\\",\\\"websys_channel.php\\\");exit;
}elseif($do==\\\"del\\\"){
$id = empty($id) ? \'\' :intval($id);
if($id!=\'\'){
$rs = $db->get_one(\\\"select c_channeldir,c_channeltype from {$cfg[\'tb_pre\']}channel where c_channeltype!=0 and c_id=\'$id\'\\\");
if($rs){
if($rs[\'c_channeltype\']==1){
dir_delete(FR_ROOT.\\\"/$rs\\\");
dir_delete(FR_ROOT.\'/templates/\'.$cfg[\'template\'].\'/\'.$rs[\'c_channeldir\']);
}
$db ->query(\\\"delete from {$cfg[\'tb_pre\']}channel where c_id =\'$id\'\\\");
}
}
showmsg(\\\"操作成功!\\\",\\\"websys_channel.php\\\");exit;
}elseif($do==\\\"add\\\"){
$sqlstr=Array(\'c_id\',\'c_name\',\'c_shortname\',\'c_itemunit\',\'c_readme\',\'c_keywords\',\'c_description\',\'c_order\',\'c_opentype\',\'c_channeltype\',\'c_linkurl\',\'c_channeldir\',\'c_moduletype\',\'c_disabled\',\'c_shownav\',\'c_createhtml\',\'c_fileext\',\'c_listfiletype\',\'c_structuretype\',\'c_filenametype\');
$sqls=\'\';
$id = empty($id) ? \'\' :intval($id);
if($id!=\'\'){
foreach($sqlstr as $v) $sqls.=\\\"$v,\\\";
$sqls=substr($sqls,0,-1);
$rs = $db->get_one(\\\"select $sqls from {$cfg[\'tb_pre\']}channel where c_id=$id limit 0,1\\\");
if($rs){
foreach($sqlstr as $v){
$s=str_replace(\'c_\',\'\',$v);
$$s=$rs[$v];
}
}else{
foreach($sqlstr as $v){
$s=str_replace(\'c_\',\'\',$v);
$$s=\'\';
}
$channeltype=2;
}
}else{
foreach($sqlstr as $v){
$s=str_replace(\'c_\',\'\',$v);
$$s=\'\';
}
$channeltype=2;
}
$cid=$num=$pass=\\\"\\\";
$validnum=0;
$enddate=dtime(0,3);
}elseif($do==\\\"saveorder\\\"){
$id = empty($id) ? array() : $id;
$s_count=count($id);
if ($s_count>0){
for($s=0;$s<$s_count;$s++){
if (!is_numeric($order[$s])) {showmsg(\'排序只能为数字!\',\'-1\');exit();}
$orders=$order[$s]?intval($order[$s]):0;
$rs=$db ->query(\\\"update {$cfg[\'tb_pre\']}channel set c_order=\'$order[$s]\' where c_id=$id[$s]\\\");
}
}
if($rs){
showmsg(\\\"操作成功!\\\",\\\"websys_channel.php\\\");exit;
}else{
showmsg(\'操作失败!原因:\'.$db ->error(),\\\"javascript:;\\\");exit;
}
}else{
$rsdb=array();
$sqladd=\'\';
$sql=\\\"select * from {$cfg[\'tb_pre\']}channel order by c_order\\\";
$query=$db->query($sql);
while($row=$db->fetch_array($query)){
$rsdb[]=$row;
}
}
include(\'templets/websys_channel.htm\');
?>
页:
[1]