歡迎光臨, 訪客. 請先 登入註冊一個帳號.
四月 29, 2024, 10:48:43 下午
19595 文章 在 3865 主題 由 4580 會員
最新註冊會員: aa123aa1
LifeType 中文開發論壇  |  開發  |  核心補強  |  [patch] 對 \ 字元的處理 « 上篇主題 下篇主題 »
頁: [1]
作者 主題: [patch] 對 \ 字元的處理  (閱讀 9253 次)
twu2
初級會員
**
文章: 61



檢視個人資料 個人網站
« 於: 三月 05, 2006, 01:41:06 下午 »

如果 PHP 的 magic_quotes_gpc 打開時, LifeType 會在 qstr() 對 SQL 指令的字串用 stripslashes() 移除 \ 字元. 所以會影響文章內有 \ 字元時, 存入資料庫前會被移除. (且不管 magic_quotes_gpc 是否打開, 在 EditPost 動作去 update 資料時, 一定都會用 stripslashes() 處理)
就算 magic_quotes_gpc 關閉時, 上述 update 指令同樣會用 stripslashes() 處理過而造成 \ 消失. 而 AddPost 的動作, 雖然沒有做 stripslashes() 的動作, 但是 \ 字元在 PHP 字串中有特別的用意, 沒有改成 \\ 來表示一個 \ 字元時, 實際存入資料庫後, \ 字元也會與後頭的字元被當成跳脫字元給處理掉而不見.

magic_quotes_gpc 應該只會對 ', ", \ 及 NUL 字元加上 \ 字元.
stripslashes() 並非是只針對上述四個字元來移除 \ 字元, 這個函式是用來還原 addslashes() 所產生的字串而設計的, 直接用在轉換 magic_quotes_gpc 應該是不適用的.

這個 patch, 會把 update post 的 stripslashes() 移除. 然後在 qstr() 中, 用 str_replace() 針對 ', ", \ 字元在 magic_quotes_gpc 開啟時特別處理. 最後在傳回字串前, 再把 \ 改成 \\.

目前在我的網站上頭看起來是可以正常運作的.
詳細說明可參考 http://blog.teatime.com.tw/post/1/30
這個 patch 也 submit 到 http://bugs.lifetype.net

程式碼:
diff -Nur class.orig/action/admin/adminupdatepostaction.class.php class/action/admin/adminupdatepostaction.class.php
--- class.orig/action/admin/adminupdatepostaction.class.php 2006-03-04 22:21:01.434358824 +0800
+++ class/action/admin/adminupdatepostaction.class.php 2006-03-05 14:13:59.971805184 +0800
@@ -88,9 +88,9 @@
             }
 
             // if we got it, update some fields
-            $post->setTopic( stripslashes($this->_postTopic));
             $postText = $this->_postText.POST_EXTENDED_TEXT_MODIFIER.$this->_postExtendedText;
-            $post->setText( stripslashes($postText));
+            $post->setTopic( $this->_postTopic );
+            $post->setText( $postText );
             $post->setCategoryIds( $this->_postCategories );
             $post->setStatus( $this->_postStatus );
             $post->setDateObject( $this->_postTimestamp );
diff -Nur class.orig/database/db.class.php class/database/db.class.php
--- class.orig/database/db.class.php 2006-03-04 22:19:56.205520124 +0800
+++ class/database/db.class.php 2006-03-05 14:13:05.191183578 +0800
@@ -93,9 +93,13 @@
  function qstr($string) {
 
  if (get_magic_quotes_gpc()) {
- $string = stripslashes($string);
+ //$string = stripslashes($string);
+                 $string = str_replace('\\\\', '\\', $string);
+                $string = str_replace("\\'", "'", $string);
+                  $string = str_replace('\\"', '"', $string);
  }
 
+ $string = str_replace("\\", "\\\\", $string);
  $string = str_replace("'", "''", $string);
 
  return $string;
已記錄

markwu
系統管理員
超級會員
*****
文章: 3928


Mark Wu


檢視個人資料 個人網站
« 回覆文章 #1 於: 三月 05, 2006, 11:45:49 下午 »

Hi twu2:

已經 commit 進 SVN rev 3051 了!謝謝!

Mark
已記錄

lss
我不是被~拉~~出來的,不要叫我大大!
總版主
超級會員
*****
文章: 1511



檢視個人資料 個人網站
« 回覆文章 #2 於: 三月 06, 2006, 07:31:07 上午 »

崇拜~~~

以後,貼程式碼就不會有 \ 消失的問題了。

lss
已記錄

沒找過 常見問題集或不知道 如何在 LifeType 中文開發論壇發問的人,恕不回答問題
twu2
初級會員
**
文章: 61



檢視個人資料 個人網站
« 回覆文章 #3 於: 三月 06, 2006, 09:42:45 上午 »

另外, 這個不知道算不算 bug.
如果把 xhtml 的轉換功能打開時, 會把 & 這個字串轉換成 & 字元.
所以 \ 這個字串, 會被轉換成 \ 後存入資料庫, 後頭再顯示時就變成 \ 字元, 而非 \ 了.

我目前是把 xhtml 轉換的功能 disable 就不會發生這個問題了.

再舉個例子, 如果我要顯示 & 這個字串, 在編輯器輸入後, 會被轉成 & 這個 html code, 但是在 xhtml filter 運作後, 會被改成 & 這個字串. 所以實際存入資料庫的是 & 而不是 &.
結果就是, 當顯示這個文章時, 這個 & 字串就被顯示成 & 而已. 而不是我要的 & 字串了.

以結果來說, 這個轉換是不正常的. 在 xhtml filter 應該要避免轉換 & 到 &. 或者應該檢查 & 後頭的字串是否會在轉換成 & 之後, 又變成一個特殊意義的字. 如果是, 就不應該轉換.

我有 submit 這個問題到 http://bugs.lifetype.net/view.php?id=853

« 最後編輯時間: 三月 06, 2006, 10:36:59 上午 由 twu2 » 已記錄

頁: [1]
LifeType 中文開發論壇  |  開發  |  核心補強  |  [patch] 對 \ 字元的處理 « 上篇主題 下篇主題 »
    前往: