Discuz 7.2坑爹集錦-PHP篇3........
類型: 變量使用
坑爹指數(shù): ★★★
代碼:- include/post.func.php=216
- $anew['perm'] = $allowsetattachperm ? $anew['perm'] : 0;
- 代碼: include/post.func.php=472
- $attach['perm'] = $allowsetattachperm ? intval($attachperm[$key]) : 0;
復(fù)制代碼 點評: 同上
-------------------------------------------------------------------------------------------------------------------------
類型: 變量使用
坑爹指數(shù): ★★
代碼: 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');
復(fù)制代碼 點評: 未對返回值$pmstatus['newpm']有效性進(jìn)行判斷
-------------------------------------------------------------------------------------------------------------------------
類型: 變量使用
坑爹指數(shù): ★★
代碼: pm.php=61
Php代碼- foreach($ucdata['data'] as $pm) {
- ....
- }
- foreach($ucdata['data'] as $pm) {
- ....
- }
復(fù)制代碼 點評: 未對 $ucdata變量'data'鍵有效做判斷就直接開始循環(huán),相當(dāng)于對一個可能不存在的變量進(jìn)行訪問并迭代。問題出在line49調(diào)用uc_pm_list()對$ucdata賦值,而ucc/control/pm.php: onls() 函數(shù)返回值$result未初始化'data'鍵名。
雖然PHP是若類型,但好歹對函數(shù)返回值先做個判斷再操作吧。偷懶也就少些幾行代碼,可調(diào)試維護(hù)時花的時間就多了。
-------------------------------------------------------------------------------------------------------------------------
類型: 字符處理
坑爹指數(shù): ★★★★
癥狀: 邊欄模塊最新帖最新回復(fù)對標(biāo)題中單引號顯示為'
點評: 不知道為何一直沒修復(fù)這個bug,難道是我修改其他代碼關(guān)聯(lián)影響到這兒?反正根源是DZ在入庫時htmlspecialchars()只對雙引號處理而未對單引號轉(zhuǎn)義
FIX: 修改如下文件調(diào)用帶ENT_QUOTES參數(shù)的htmlspecialchars()函數(shù)來替代str_replace()函數(shù)處理
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;
復(fù)制代碼 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()函數(shù):
- //$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()函數(shù):
- //$string = str_replace(array('&', '"', '<', '>', '''), array('&', '"', '<', '>', '\''), $string);
- $string = htmlspecialchars_decode($string, ENT_QUOTES);
- ....
- //$strcut = str_replace(array('&', '"', '<', '>', '\''), array('&', '"', '<', '>', '''), $strcut);
- $strcut = htmlspecialchars($strcut, ENT_QUOTES);
復(fù)制代碼 -------------------------------------------------------------------------------------------------------------------------
類型: 頁面效果
坑爹指數(shù): ★
癥狀: 當(dāng)bbcodeoff時帖子中‘最后修改’的標(biāo)簽混亂
FIX: include/discuzcode.func.php: 添加一段判斷
line126開始的判斷拆分開
Php代碼- if(!$bbcodeoff && $allowbbcode) {// line126
- ....
- } // line201
- if(!$bbcodeoff && $allowbbcode) {// line126
- ....
- } // line201
復(fù)制代碼 修改成
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 );
- } //添加結(jié)束
- } // 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 );
- } //添加結(jié)束
- } // line201+n
復(fù)制代碼 -------------------------------------------------------------------------------------------------------------------------
類型: 邏輯錯誤
坑爹指數(shù): ★★★★
代碼: 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']."'" );
復(fù)制代碼 點評: first='1'只設(shè)置了一次,如果分割主題時選擇包含了1樓那么原主題內(nèi)變成1樓的帖子的first依然為0. 本來在不支持事務(wù)的MyISAM引擎上做分隔主題這種操作就具有一定危險性,不過DZ更直接增加了這個這個機(jī)率。提醒你分割主題時不要把頂樓分割出去喲,不然剩下變成1樓的帖子將會成為孤兒。多來幾次你就會明確記住這個準(zhǔn)則了,也不會因為數(shù)據(jù)庫偶爾非原子性操作帶來的隨機(jī)故障而煩惱。這多么簡單啊。呵呵
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]'");
-------------------------------------------------------------------------------------------------------------------------
類型: 執(zhí)行流程
坑爹指數(shù): ★★★
代碼: 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']; 點評: 如果查詢結(jié)果空$forum將會false,不做判斷而直接賦值給$tid會出錯,否則就可能要繼續(xù)執(zhí)行到后繼的viewthreads.php中的判斷,浪費(fèi)系統(tǒng)資源。另外viewthreads.php 也未對$tid判斷即以此為條件直接查詢,徒增DB負(fù)擔(dān)(MySQL會有 ‘Impossible WHERE noticed after reading const tables’ )
FIX: 應(yīng)該查詢結(jié)束后立即對$forum做判斷并設(shè)置一個變量作標(biāo)志再考慮給$tid賦值然后在當(dāng)前頁面最底部判斷,如果標(biāo)志真則立即輸出404頭直接退出。
-------------------------------------------------------------------------------------------------------------------------
類型: 未知
坑爹指數(shù): ★★★★
代碼: 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')";
- }
- }
- }
復(fù)制代碼 點評: 一直沒研究明白這個賦值要表達(dá)什么意思。難道這位當(dāng)時正在韓大嘴語錄,看到“瞄的是A,想的是B,解說的是C,觀眾以為是D,其實指的是E”這一段,頓悟,遂看到是代碼,想的是妹妹,說的是工資,同事以為是八卦,領(lǐng)導(dǎo)以為是抽風(fēng)~
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')";
- }
- }
- }
復(fù)制代碼 本來計劃單獨(dú)開一PHP優(yōu)化篇。后來發(fā)現(xiàn)下面坑爹代碼多數(shù)會影響性能(PHP以及數(shù)據(jù)庫執(zhí)行),修復(fù)了bug即優(yōu)化。故合并為一篇。
補(bǔ)充個優(yōu)化PHP的:
如果你的服務(wù)器http server支持Gzip/deflate壓縮,那么就使用http serer提供的功能,并到后臺,全局-優(yōu)化設(shè)置-服務(wù)器優(yōu)化把“頁面 Gzip 壓縮”選項設(shè)定為否。
如果設(shè)定“是”,那么將使用DZ提供的一個gzip PHP插件來實現(xiàn)壓縮頁面。缺點是耗費(fèi)PHP腳本執(zhí)行時間,對于nginx+php-fpm模式運(yùn)行更容易出現(xiàn)502錯誤。
版權(quán)曾經(jīng)擁有,歡迎網(wǎng)上分享
|