Discuz 7.2坑爹集锦-PHP篇3........
类型: 变量使用
坑爹指数: ★★★
代码:- include/post.func.php=216
- $anew['perm'] = $allowsetattachperm ? $anew['perm'] : 0;
- 代码: include/post.func.php=472
- $attach['perm'] = $allowsetattachperm ? intval($attachperm[$key]) : 0;
复制代码 点评: 同上
-------------------------------------------------------------------------------------------------------------------------
类型: 变量使用
坑爹指数: ★★
代码: pm.php=47
Php代码- $pmstatus = uc_pm_checknew($discuz_uid, 4);
- $filter = !emptyempty($filter) && in_array($filter, array('newpm', 'privatepm', 'announcepm')) ? $filter : ($pmstatus['newpm'] ? 'newpm' : 'privatepm');
- $pmstatus = uc_pm_checknew($discuz_uid, 4);
- $filter = !empty($filter) && in_array($filter, array('newpm', 'privatepm', 'announcepm')) ? $filter : ($pmstatus['newpm'] ? 'newpm' : 'privatepm');
复制代码 点评: 未对返回值$pmstatus['newpm']有效性进行判断
-------------------------------------------------------------------------------------------------------------------------
类型: 变量使用
坑爹指数: ★★
代码: pm.php=61
Php代码- foreach($ucdata['data'] as $pm) {
- ....
- }
- foreach($ucdata['data'] as $pm) {
- ....
- }
复制代码 点评: 未对 $ucdata变量'data'键有效做判断就直接开始循环,相当于对一个可能不存在的变量进行访问并迭代。问题出在line49调用uc_pm_list()对$ucdata赋值,而ucc/control/pm.php: onls() 函数返回值$result未初始化'data'键名。
虽然PHP是若类型,但好歹对函数返回值先做个判断再操作吧。偷懒也就少些几行代码,可调试维护时花的时间就多了。
-------------------------------------------------------------------------------------------------------------------------
类型: 字符处理
坑爹指数: ★★★★
症状: 边栏模块最新帖最新回复对标题中单引号显示为'
点评: 不知道为何一直没修复这个bug,难道是我修改其他代码关联影响到这儿?反正根源是DZ在入库时htmlspecialchars()只对双引号处理而未对单引号转义
FIX: 修改如下文件调用带ENT_QUOTES参数的htmlspecialchars()函数来替代str_replace()函数处理
Php代码- include/request.func.php
- $datalist[$data['tid']]['subject'] = isset($data['subject']) ? str_replace('\\\'', ''', addslashes($data['subject'])) : NULL;
- include/request.func.php
- $datalist[$data['tid']]['subject'] = isset($data['subject']) ? str_replace('\\\'', ''', addslashes($data['subject'])) : NULL;
复制代码 FIXTO:
Php代码- $datalist[$data['tid']]['subject'] = isset($data['subject']) ? htmlspecialchars(htmlspecialchars_decode($data['subject']), ENT_QUOTES) : NULL;
- 然后修改global.func.php, ucs/mode/base.php, ucclient/mode/base.php的cutstr()函数:
- //$string = str_replace(array('&', '"', '<', '>', '''), array('&', '"', '<', '>', '\''), $string);
- $string = htmlspecialchars_decode($string, ENT_QUOTES);
- ....
- //$strcut = str_replace(array('&', '"', '<', '>', '\''), array('&', '"', '<', '>', '''), $strcut);
- $strcut = htmlspecialchars($strcut, ENT_QUOTES);
- $datalist[$data['tid']]['subject'] = isset($data['subject']) ? htmlspecialchars(htmlspecialchars_decode($data['subject']), ENT_QUOTES) : NULL;
- 然后修改global.func.php, ucs/mode/base.php, ucclient/mode/base.php的cutstr()函数:
- //$string = str_replace(array('&', '"', '<', '>', '''), array('&', '"', '<', '>', '\''), $string);
- $string = htmlspecialchars_decode($string, ENT_QUOTES);
- ....
- //$strcut = str_replace(array('&', '"', '<', '>', '\''), array('&', '"', '<', '>', '''), $strcut);
- $strcut = htmlspecialchars($strcut, ENT_QUOTES);
复制代码 -------------------------------------------------------------------------------------------------------------------------
类型: 页面效果
坑爹指数: ★
症状: 当bbcodeoff时帖子中‘最后修改’的标签混乱
FIX: include/discuzcode.func.php: 添加一段判断
line126开始的判断拆分开
Php代码- if(!$bbcodeoff && $allowbbcode) {// line126
- ....
- } // line201
- if(!$bbcodeoff && $allowbbcode) {// line126
- ....
- } // line201
复制代码 修改成
Php代码- if($allowbbcode) { // line126
- if (!$bbcodeoff) {
- .....
- }
- // 添加开始
- elseif ($bbcodeoff && substr($message, 0, 5) === '[i=s]') { // allow parse '[i=s]last modified by [/i]' even if bbcodeoff
- $message = preg_replace('/^\[i=s\](.*)\[\/i\]/', '<i class="pstatus">\\1</i>', $message );
- } //添加结束
- } // line201+n
- if($allowbbcode) { // line126
- if (!$bbcodeoff) {
- .....
- }
- // 添加开始
- elseif ($bbcodeoff && substr($message, 0, 5) === '[i=s]') { // allow parse '[i=s]last modified by [/i]' even if bbcodeoff
- $message = preg_replace('/^\[i=s\](.*)\[\/i\]/', '<i class="pstatus">\\1</i>', $message );
- } //添加结束
- } // line201+n
复制代码 -------------------------------------------------------------------------------------------------------------------------
类型: 逻辑错误
坑爹指数: ★★★★
代码: topicadmin.php ~320 分割主题
Php代码- $db->query("UPDATE {$tablepre}posts SET first='1', subject='$subject' WHERE fid='$waiting_fid' AND pid='".$splitauthors[0]['pid']."'" );
- $db->query("UPDATE {$tablepre}posts SET first='1', subject='$subject' WHERE fid='$waiting_fid' AND pid='".$splitauthors[0]['pid']."'" );
复制代码 点评: first='1'只设置了一次,如果分割主题时选择包含了1楼那么原主题内变成1楼的帖子的first依然为0. 本来在不支持事务的MyISAM引擎上做分隔主题这种操作就具有一定危险性,不过DZ更直接增加了这个这个机率。提醒你分割主题时不要把顶楼分割出去哟,不然剩下变成1楼的帖子将会成为孤儿。多来几次你就会明确记住这个准则了,也不会因为数据库偶尔非原子性操作带来的随机故障而烦恼。这多么简单啊。呵呵
FIX: line327
Php代码
$db->query("UPDATE {$tablepre}posts SET subject='".addslashes($thread['subject'])."' WHERE pid='$fpost[pid]'");
$db->query("UPDATE {$tablepre}posts SET subject='".addslashes($thread['subject'])."' WHERE pid='$fpost[pid]'");
修改为
Php代码
$db->query("UPDATE {$tablepre}posts SET first=1, subject='".addslashes($thread['subject'])."' WHERE pid='$fpost[pid]'");
$db->query("UPDATE {$tablepre}posts SET first=1, subject='".addslashes($thread['subject'])."' WHERE pid='$fpost[pid]'");
-------------------------------------------------------------------------------------------------------------------------
类型: 执行流程
坑爹指数: ★★★
代码: include/common.inc.php 349
Php代码
$forum = $db->fetch_first("SELECT t.tid, t.closed,".(defined('SQL_ADD_THREAD') ? SQL_ADD_THREAD : '')." f.*, ff.* $accessadd1 $modadd1, f.fid AS fid
FROM {$tablepre}threads t ....
$tid = $forum['tid'];
$forum = $db->fetch_first("SELECT t.tid, t.closed,".(defined('SQL_ADD_THREAD') ? SQL_ADD_THREAD : '')." f.*, ff.* $accessadd1 $modadd1, f.fid AS fid
FROM {$tablepre}threads t ....
$tid = $forum['tid']; 点评: 如果查询结果空$forum将会false,不做判断而直接赋值给$tid会出错,否则就可能要继续执行到后继的viewthreads.php中的判断,浪费系统资源。另外viewthreads.php 也未对$tid判断即以此为条件直接查询,徒增DB负担(MySQL会有 ‘Impossible WHERE noticed after reading const tables’ )
FIX: 应该查询结束后立即对$forum做判断并设置一个变量作标志再考虑给$tid赋值然后在当前页面最底部判断,如果标志真则立即输出404头直接退出。
-------------------------------------------------------------------------------------------------------------------------
类型: 未知
坑爹指数: ★★★★
代码: uc_client/model/note.php=64
Php代码- foreach((array)$this->apps as $appid => $app) {
- $appid = $app['appid']; <---------??
- if($appid == intval($appid)) {
- if($appids && !in_array($appid, $appids)) {
- $appadd[] = 'app'.$appid."='1'";
- } else {
- $varadd[] = "('noteexists{$appid}', '1')";
- }
- }
- }
- foreach((array)$this->apps as $appid => $app) {
- $appid = $app['appid']; <---------??
- if($appid == intval($appid)) {
- if($appids && !in_array($appid, $appids)) {
- $appadd[] = 'app'.$appid."='1'";
- } else {
- $varadd[] = "('noteexists{$appid}', '1')";
- }
- }
- }
复制代码 点评: 一直没研究明白这个赋值要表达什么意思。难道这位当时正在韩大嘴语录,看到“瞄的是A,想的是B,解说的是C,观众以为是D,其实指的是E”这一段,顿悟,遂看到是代码,想的是妹妹,说的是工资,同事以为是八卦,领导以为是抽风~
FIX: 以我类人猿的智商估计可能是这样:
Php代码- foreach((array)$this->apps as $appid => $app) {
- if(intval($appid) == $app['appid']) { // 帮你精简一行代码
- if($appids && !in_array($appid, $appids)) {
- $appadd[] = 'app'.$appid."='1'";
- } else {
- $varadd[] = "('noteexists{$appid}', '1')";
- }
- }
- }
- foreach((array)$this->apps as $appid => $app) {
- if(intval($appid) == $app['appid']) { // 帮你精简一行代码
- if($appids && !in_array($appid, $appids)) {
- $appadd[] = 'app'.$appid."='1'";
- } else {
- $varadd[] = "('noteexists{$appid}', '1')";
- }
- }
- }
复制代码 本来计划单独开一PHP优化篇。后来发现下面坑爹代码多数会影响性能(PHP以及数据库执行),修复了bug即优化。故合并为一篇。
补充个优化PHP的:
如果你的服务器http server支持Gzip/deflate压缩,那么就使用http serer提供的功能,并到后台,全局-优化设置-服务器优化把“页面 Gzip 压缩”选项设定为否。
如果设定“是”,那么将使用DZ提供的一个gzip PHP插件来实现压缩页面。缺点是耗费PHP脚本执行时间,对于nginx+php-fpm模式运行更容易出现502错误。
版权曾经拥有,欢迎网上分享
|