Advertisement
Guest User

Untitled

a guest
Jun 21st, 2013
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 20.22 KB | None | 0 0
  1. <?php
  2. /**
  3.  * PIO MySQL API
  4.  *
  5.  * 提供存取以 MySQL 資料庫構成的資料結構後端的物件
  6.  *
  7.  * @package PMCLibrary
  8.  * @version $Id: pio.mysql.php 837 2012-12-27 13:51:28Z scribe $
  9.  * @date $Date: 2012-12-27 21:51:28 +0800 (週四, 27 十二月 2012) $
  10.  * @deprecated
  11.  */
  12.  
  13. class PIOmysql implements IPIO {
  14.     var $ENV, $username, $password, $server, $dbname, $tablename; // Local Constant
  15.     var $con, $prepared, $useTransaction; // Local Global
  16.  
  17.     function PIOmysql($connstr='', $ENV){
  18.         $this->ENV = $ENV;
  19.         $this->prepared = 0;
  20.         if($connstr) $this->dbConnect($connstr);
  21.     }
  22.  
  23.     /* private 攔截SQL錯誤 */
  24.     function _error_handler(array $errarray, $query=''){
  25.         $err = sprintf('%s on line %d.', $errarray[0], $errarray[1]);
  26.         if (defined('DEBUG') && DEBUG) {
  27.             $err .= sprintf(PHP_EOL."Description: #%d: %s".PHP_EOL.
  28.                 "SQL: %s", mysql_errno(), mysql_error(), $query);
  29.         }
  30.         throw new RuntimeException($err, mysql_errno());
  31.     }
  32.  
  33.     /* private 使用SQL字串和MySQL伺服器要求 */
  34.     function _mysql_call($query, $errarray=false){
  35.         $resource = mysql_query($query);
  36.         if(is_array($errarray) && $resource===false) $this->_error_handler($errarray, $query);
  37.         else return $resource;
  38.     }
  39.  
  40.     /* private 由資源輸出陣列 */
  41.     function _ArrangeArrayStructure($line){
  42.         $posts = array();
  43.         while($row = mysql_fetch_array($line, MYSQL_ASSOC)) $posts[] = $row;
  44.         mysql_free_result($line);
  45.         return $posts;
  46.     }
  47.  
  48.     /* PIO模組版本 */
  49.     function pioVersion(){
  50.         return '0.6 (v20121213)';
  51.     }
  52.  
  53.     /* 處理連線字串/連接 */
  54.     function dbConnect($connStr){
  55.         // 格式: mysql://帳號:密碼@伺服器位置:埠號(可省略)/資料庫/資料表/
  56.         // 示例: mysql://pixmicat:1234@127.0.0.1/pixmicat_use/imglog/
  57.         if(preg_match('/^mysql:\/\/(.*)\:(.*)\@(.*(?:\:[0-9]+)?)\/(.*)\/(.*)\/$/i', $connStr, $linkinfos)){
  58.             $this->username = $linkinfos[1]; // 登入帳號
  59.             $this->password = $linkinfos[2]; // 登入密碼
  60.             $this->server = $linkinfos[3]; // 登入伺服器 (含埠號)
  61.             $this->dbname = $linkinfos[4]; // 資料庫名稱
  62.             $this->tablename = $linkinfos[5]; // 資料表名稱
  63.         }
  64.     }
  65.  
  66.     /* 初始化 */
  67.     function dbInit($isAddInitData=true){
  68.         $this->dbPrepare();
  69.         if(mysql_num_rows(mysql_query("SHOW TABLES LIKE '".$this->tablename."'"))!=1){ // 資料表不存在
  70.             if(version_compare(mysql_get_server_info(), '5.5', '>=')){ // 5.5+
  71.                 $result = "CREATE TABLE ".$this->tablename." (primary key(no),
  72.     index (resto),index (root),index (time),
  73.     no int(1) not null auto_increment,
  74.     resto int(1) not null,
  75.     root timestamp null DEFAULT 0,
  76.     time int(1) not null,
  77.     md5chksum varchar(32) not null,
  78.     category varchar(255) not null,
  79.     tim bigint(1) not null,
  80.     ext varchar(4) not null,
  81.     imgw smallint(1) not null,
  82.     imgh smallint(1) not null,
  83.     imgsize varchar(10) not null,
  84.     tw smallint(1) not null,
  85.     th smallint(1) not null,
  86.     pwd varchar(8) not null,
  87.     now varchar(255) not null,
  88.     name varchar(255) not null,
  89.     email varchar(255) not null,
  90.     sub varchar(255) not null,
  91.     com text not null,
  92.     host varchar(255) not null,
  93.     status varchar(255) not null)
  94.     ENGINE = MYISAM
  95.     COMMENT = 'PIO Structure V3'";
  96.             }else{ // 5.5 以前版本
  97.                 $result = "CREATE TABLE ".$this->tablename." (primary key(no),
  98.     index (resto),index (root),index (time),
  99.     no int(1) not null auto_increment,
  100.     resto int(1) not null,
  101.     root timestamp(14) null DEFAULT 0,
  102.     time int(1) not null,
  103.     md5chksum varchar(32) not null,
  104.     category varchar(255) not null,
  105.     tim bigint(1) not null,
  106.     ext varchar(4) not null,
  107.     imgw smallint(1) not null,
  108.     imgh smallint(1) not null,
  109.     imgsize varchar(10) not null,
  110.     tw smallint(1) not null,
  111.     th smallint(1) not null,
  112.     pwd varchar(8) not null,
  113.     now varchar(255) not null,
  114.     name varchar(255) not null,
  115.     email varchar(255) not null,
  116.     sub varchar(255) not null,
  117.     com text not null,
  118.     host varchar(255) not null,
  119.     status varchar(255) not null)
  120.     TYPE = MYISAM
  121.     COMMENT = 'PIO Structure V3'";
  122.             }
  123.  
  124.             $result2 = @mysql_query("SHOW CHARACTER SET like 'utf8'"); // 是否支援UTF-8 (MySQL 4.1.1開始支援)
  125.             if($result2 && mysql_num_rows($result2)){
  126.                 $result .= ' CHARACTER SET utf8 COLLATE utf8_general_ci'; // 資料表追加UTF-8編碼
  127.                 mysql_free_result($result2);
  128.             }
  129.             mysql_query($result); // 正式新增資料表
  130.             // 追加一筆新資料
  131.             if($isAddInitData) $this->addPost(1, 0, '', '', 0, '', 0, 0, '', 0, 0, '', '05/01/01(六)00:00', $this->ENV['NONAME'], '', $this->ENV['NOTITLE'], $this->ENV['NOCOMMENT'], '');
  132.             $this->dbCommit();
  133.         }
  134.     }
  135.  
  136.     /* 準備/讀入 */
  137.     function dbPrepare($reload=false,$transaction=false){
  138.         if($this->prepared) return true;
  139.  
  140.         if(@!$this->con = mysql_connect($this->server, $this->username, $this->password)) $this->_error_handler(array('Open database failed', __LINE__));
  141.         @mysql_select_db($this->dbname, $this->con);
  142.         @mysql_query("SET NAMES 'utf8'"); // MySQL資料以UTF-8模式傳送
  143.         $this->useTransaction = $transaction;
  144.         if($transaction) @mysql_query('START TRANSACTION'); // 啟動交易性能模式
  145.  
  146.         $this->prepared = 1;
  147.     }
  148.  
  149.     /* 提交/儲存 */
  150.     function dbCommit(){
  151.         if(!$this->prepared) return false;
  152.         if($this->useTransaction) @mysql_query('COMMIT'); // 交易性能模式提交
  153.     }
  154.  
  155.     /* 資料表維護 */
  156.     function dbMaintanence($action, $doit=false){
  157.         switch($action) {
  158.             case 'optimize':
  159.                 if($doit){
  160.                     $this->dbPrepare(false);
  161.                     if($this->_mysql_call('OPTIMIZE TABLES '.$this->tablename)) return true;
  162.                     else return false;
  163.                 }else return true; // 支援最佳化資料表
  164.                 break;
  165.             case 'check':
  166.                 if($doit){
  167.                     $this->dbPrepare(false);
  168.                     if($rs=$this->_mysql_call('CHECK TABLE '.$this->tablename)){
  169.                         mysql_data_seek($rs, mysql_num_rows($rs)-1);
  170.                         $row = mysql_fetch_assoc($rs);
  171.                         return 'Table '.$row['Table'].': '.$row['Msg_type'].' = '.$row['Msg_text'];
  172.                     }
  173.                     else return false;
  174.                 }else return true; // 支援檢查資料表
  175.                 break;
  176.             case 'repair':
  177.                 if($doit){
  178.                     $this->dbPrepare(false);
  179.                     if($rs=$this->_mysql_call('REPAIR TABLE '.$this->tablename)){
  180.                         mysql_data_seek($rs, mysql_num_rows($rs)-1);
  181.                         $row = mysql_fetch_assoc($rs);
  182.                         return 'Table '.$row['Table'].': '.$row['Msg_type'].' = '.$row['Msg_text'];
  183.                     }
  184.                     else return false;
  185.                 }else return true; // 支援修復資料表
  186.                 break;
  187.             case 'export':
  188.                 if($doit){
  189.                     $this->dbPrepare(false);
  190.                     $gp = gzopen('piodata.log.gz', 'w9');
  191.                     gzwrite($gp, $this->dbExport());
  192.                     gzclose($gp);
  193.                     return '<a href="piodata.log.gz">下載 piodata.log.gz 中介檔案</a>';
  194.                 }else return true; // 支援匯出資料
  195.                 break;
  196.             default: return false; // 不支援
  197.         }
  198.     }
  199.  
  200.     /* 匯入資料來源 */
  201.     function dbImport($data){
  202.         $this->dbInit(false); // 僅新增結構不新增資料
  203.         $data = explode("\r\n", $data);
  204.         $data_count = count($data) - 1;
  205.         $replaceComma = create_function('$txt', 'return str_replace("&#44;", ",", $txt);');
  206.         for($i = 0; $i < $data_count; $i++){
  207.             $line = array_map($replaceComma, explode(',', $data[$i])); // 取代 &#44; 為 ,
  208.             if ($line[2] == '0') $line[2] = '0000-00-00 00:00:00';
  209.             $SQL = 'INSERT INTO '.$this->tablename.' (no,resto,root,time,md5chksum,category,tim,ext,imgw,imgh,imgsize,tw,th,pwd,now,name,email,sub,com,host,status) VALUES ('.
  210.     $line[0].','.
  211.     $line[1].',\''.
  212.     $line[2].'\','.
  213.     substr($line[5], 0, 10).',\''.
  214.     mysql_real_escape_string($line[3], $this->con).'\',\''.
  215.     mysql_real_escape_string($line[4], $this->con).'\','.
  216.     $line[5].',\''.mysql_real_escape_string($line[6], $this->con).'\','.
  217.     $line[7].','.$line[8].',\''.mysql_real_escape_string($line[9], $this->con).'\','.$line[10].','.$line[11].',\''.
  218.     mysql_real_escape_string($line[12], $this->con).'\',\''.
  219.     mysql_real_escape_string($line[13], $this->con).'\',\''.
  220.     mysql_real_escape_string($line[14], $this->con).'\',\''.
  221.     mysql_real_escape_string($line[15], $this->con).'\',\''.
  222.     mysql_real_escape_string($line[16], $this->con).'\',\''.
  223.     mysql_real_escape_string($line[17], $this->con).'\',\''.
  224.     mysql_real_escape_string($line[18], $this->con).'\',\''.
  225.     mysql_real_escape_string($line[19], $this->con).'\')';
  226.             $this->_mysql_call($SQL, array('Import a new post failed', __LINE__));
  227.         }
  228.         $this->dbCommit(); // 送交
  229.         return true;
  230.     }
  231.  
  232.     /* 匯出資料來源 */
  233.     function dbExport(){
  234.         if(!$this->prepared) $this->dbPrepare();
  235.         $line = $this->_mysql_call('SELECT no,resto,root,md5chksum,category,tim,ext,imgw,imgh,imgsize,tw,th,pwd,now,name,email,sub,com,host,status FROM '.$this->tablename.' ORDER BY no DESC',
  236.             array('Export posts failed', __LINE__));
  237.         $data = '';
  238.         $replaceComma = create_function('$txt', 'return str_replace(",", "&#44;", $txt);');
  239.         while($row = mysql_fetch_array($line, MYSQL_ASSOC)){
  240.             $row = array_map($replaceComma, $row); // 取代 , 為 &#44;
  241.             if($row['root']=='0000-00-00 00:00:00') $row['root'] = '0'; // 初始值設為 0
  242.             $data .= rtrim(implode(',', $row)).",\r\n";
  243.         }
  244.         mysql_free_result($line);
  245.         return $data;
  246.     }
  247.  
  248.     /* 文章數目 */
  249.     function postCount($resno=0){
  250.         if(!$this->prepared) $this->dbPrepare();
  251.  
  252.         if($resno){ // 回傳討論串總文章數目
  253.             $line = $this->_mysql_call('SELECT COUNT(no) FROM '.$this->tablename.' WHERE resto = '.intval($resno),
  254.                 array('Fetch count in thread failed', __LINE__));
  255.             $countline = mysql_result($line, 0) + 1;
  256.         }else{ // 回傳總文章數目
  257.             $line = $this->_mysql_call('SELECT COUNT(no) FROM '.$this->tablename, array('Fetch count of posts failed', __LINE__));
  258.             $countline = mysql_result($line, 0);
  259.         }
  260.         mysql_free_result($line);
  261.         return $countline;
  262.     }
  263.  
  264.     /* 討論串數目 */
  265.     function threadCount(){
  266.         if(!$this->prepared) $this->dbPrepare();
  267.  
  268.         $tree = $this->_mysql_call('SELECT COUNT(no) FROM '.$this->tablename.' WHERE resto = 0',
  269.             array('Fetch count of threads failed', __LINE__));
  270.         $counttree = mysql_result($tree, 0); mysql_free_result($tree); // 計算討論串目前資料筆數
  271.         return $counttree;
  272.     }
  273.  
  274.     /* 取得最後文章編號 */
  275.     function getLastPostNo($state){
  276.         if(!$this->prepared) $this->dbPrepare();
  277.  
  278.         if($state=='afterCommit'){ // 送出後的最後文章編號
  279.             $tree = $this->_mysql_call('SELECT MAX(no) FROM '.$this->tablename, array('Get the last No. failed', __LINE__));
  280.             $lastno = mysql_result($tree, 0); mysql_free_result($tree);
  281.             return $lastno;
  282.         }else return 0; // 其他狀態沒用
  283.     }
  284.  
  285.     /* 輸出文章清單 */
  286.     function fetchPostList($resno=0, $start=0, $amount=0){
  287.         if(!$this->prepared) $this->dbPrepare();
  288.  
  289.         $line = array();
  290.         $resno = intval($resno);
  291.         if($resno){ // 輸出討論串的結構 (含自己, EX : 1,2,3,4,5,6)
  292.             $tmpSQL = 'SELECT no FROM '.$this->tablename.' WHERE no = '.$resno.' OR resto = '.$resno.' ORDER BY no';
  293.         }else{ // 輸出所有文章編號,新的在前
  294.             $tmpSQL = 'SELECT no FROM '.$this->tablename.' ORDER BY no DESC';
  295.             $start = intval($start); $amount = intval($amount);
  296.             if($amount) $tmpSQL .= " LIMIT {$start}, {$amount}"; // 有指定數量才用 LIMIT
  297.         }
  298.         $tree = $this->_mysql_call($tmpSQL, array('Fetch post list failed', __LINE__));
  299.         while($rows = mysql_fetch_row($tree)) $line[] = $rows[0]; // 迴圈
  300.         mysql_free_result($tree);
  301.         return $line;
  302.     }
  303.  
  304.     /* 輸出討論串清單 */
  305.     function fetchThreadList($start=0, $amount=0, $isDESC=false){
  306.         if(!$this->prepared) $this->dbPrepare();
  307.  
  308.         $start = intval($start); $amount = intval($amount);
  309.         $treeline = array();
  310.         $tmpSQL = 'SELECT no FROM '.$this->tablename.' WHERE resto = 0 ORDER BY '.($isDESC ? 'no' : 'root').' DESC';
  311.         if($amount) $tmpSQL .= " LIMIT {$start}, {$amount}"; // 有指定數量才用 LIMIT
  312.         $tree = $this->_mysql_call($tmpSQL, array('Fetch thread list failed', __LINE__));
  313.         while($rows = mysql_fetch_row($tree)) $treeline[] = $rows[0]; // 迴圈
  314.         mysql_free_result($tree);
  315.         return $treeline;
  316.     }
  317.  
  318.     /* 輸出文章 */
  319.     function fetchPosts($postlist,$fields='*'){
  320.         if(!$this->prepared) $this->dbPrepare();
  321.  
  322.         if(is_array($postlist)){ // 取多串
  323.             $pno = implode(',', $postlist); // ID字串
  324.             $tmpSQL = 'SELECT '.$fields.' FROM '.$this->tablename.' WHERE no IN ('.$pno.') ORDER BY no';
  325.             if(count($postlist) > 1){ if($postlist[0] > $postlist[1]) $tmpSQL .= ' DESC'; } // 由大排到小
  326.         }else $tmpSQL = 'SELECT '.$fields.' FROM '.$this->tablename.' WHERE no = '.intval($postlist); // 取單串
  327.         $line = $this->_mysql_call($tmpSQL, array('Fetch the post content failed', __LINE__));
  328.         return $this->_ArrangeArrayStructure($line); // 輸出陣列結構
  329.     }
  330.  
  331.     /* 刪除舊附件 (輸出附件清單) */
  332.     function delOldAttachments($total_size, $storage_max, $warnOnly=true){
  333.         $FileIO = PMCLibrary::getFileIOInstance();
  334.         if(!$this->prepared) $this->dbPrepare();
  335.  
  336.         $arr_warn = $arr_kill = array(); // 警告 / 即將被刪除標記陣列
  337.         $result = $this->_mysql_call('SELECT no,ext,tim FROM '.$this->tablename.' WHERE ext <> \'\' ORDER BY no',
  338.             array('Get old posts failed', __LINE__));
  339.         while(list($dno, $dext, $dtim) = mysql_fetch_row($result)){ // 個別跑舊文迴圈
  340.             $dfile = $dtim.$dext; // 附加檔案名稱
  341.             $dthumb = $FileIO->resolveThumbName($dtim); // 預覽檔案名稱
  342.             if($FileIO->imageExists($dfile)){ $total_size -= $FileIO->getImageFilesize($dfile) / 1024; $arr_kill[] = $dno; $arr_warn[$dno] = 1; } // 標記刪除
  343.             if($dthumb && $FileIO->imageExists($dthumb)) $total_size -= $FileIO->getImageFilesize($dthumb) / 1024;
  344.             if($total_size < $storage_max) break;
  345.         }
  346.         mysql_free_result($result);
  347.         return $warnOnly ? $arr_warn : $this->removeAttachments($arr_kill);
  348.     }
  349.  
  350.     /* 刪除文章 */
  351.     function removePosts($posts){
  352.         if(!$this->prepared) $this->dbPrepare();
  353.         if(count($posts)==0) return array();
  354.  
  355.         $files = $this->removeAttachments($posts, true); // 先遞迴取得刪除文章及其回應附件清單
  356.         $pno = implode(', ', $posts); // ID字串
  357.         $this->_mysql_call('DELETE FROM '.$this->tablename.' WHERE no IN ('.$pno.') OR resto IN('.$pno.')',
  358.             array('Delete old posts and replies failed', __LINE__)); // 刪掉文章
  359.         return $files;
  360.     }
  361.  
  362.     /* 刪除附件 (輸出附件清單) */
  363.     function removeAttachments($posts, $recursion=false){
  364.         $FileIO = PMCLibrary::getFileIOInstance();
  365.         if(!$this->prepared) $this->dbPrepare();
  366.         if(count($posts)==0) return array();
  367.  
  368.         $files = array();
  369.         $pno = implode(', ', $posts); // ID字串
  370.         if($recursion) $tmpSQL = 'SELECT ext,tim FROM '.$this->tablename.' WHERE (no IN ('.$pno.') OR resto IN('.$pno.")) AND ext <> ''"; // 遞迴取出 (含回應附件)
  371.         else $tmpSQL = 'SELECT ext,tim FROM '.$this->tablename.' WHERE no IN ('.$pno.") AND ext <> ''"; // 只有指定的編號
  372.  
  373.         $result = $this->_mysql_call($tmpSQL, array('Get attachments of the post failed', __LINE__));
  374.         while(list($dext, $dtim) = mysql_fetch_row($result)){ // 個別跑迴圈
  375.             $dfile = $dtim.$dext; // 附加檔案名稱
  376.             $dthumb = $FileIO->resolveThumbName($dtim); // 預覽檔案名稱
  377.             if($FileIO->imageExists($dfile)) $files[] = $dfile;
  378.             if($dthumb && $FileIO->imageExists($dthumb)) $files[] = $dthumb;
  379.         }
  380.         mysql_free_result($result);
  381.         return $files;
  382.     }
  383.  
  384.     /* 新增文章/討論串 */
  385.     function addPost($no, $resto, $md5chksum, $category, $tim, $ext, $imgw, $imgh, $imgsize, $tw, $th, $pwd, $now, $name, $email, $sub, $com, $host, $age=false, $status=''){
  386.         if(!$this->prepared) $this->dbPrepare();
  387.  
  388.         $time = (int)substr($tim, 0, -3); // 13位數的數字串是檔名,10位數的才是時間數值
  389.         $updatetime = gmdate('Y-m-d H:i:s'); // 更動時間 (UTC)
  390.         $resto = intval($resto);
  391.         if($resto){ // 新增回應
  392.             $root = '0000-00-00 00:00:00';
  393.             if($age){ // 推文
  394.                 $this->_mysql_call('UPDATE '.$this->tablename.' SET root = "'.$updatetime.'" WHERE no = '.$resto,
  395.                     array('Push the post failed', __LINE__)); // 將被回應的文章往上移動
  396.             }
  397.         }else $root = $updatetime; // 新增討論串, 討論串最後被更新時間
  398.  
  399.         $query = 'INSERT INTO '.$this->tablename.' (resto,root,time,md5chksum,category,tim,ext,imgw,imgh,imgsize,tw,th,pwd,now,name,email,sub,com,host,status) VALUES ('.
  400.     $resto.','. // 回應編號
  401.     "'$root',". // 最後更新時間
  402.     $time.','. // 發文時間數值
  403.     "'$md5chksum',". // 附加檔案md5
  404.     "'".mysql_real_escape_string($category, $this->con)."',". // 分類標籤
  405.     "'$tim', '$ext',". // 附加檔名
  406.     (int)$imgw.','.(int)$imgh.",'".$imgsize."',".(int)$tw.','.(int)$th.','. // 圖檔長寬及檔案大小;預覽圖長寬
  407.     "'".mysql_real_escape_string($pwd, $this->con)."',".
  408.     "'$now',". // 時間(含ID)字串
  409.     "'".mysql_real_escape_string($name, $this->con)."',".
  410.     "'".mysql_real_escape_string($email, $this->con)."',".
  411.     "'".mysql_real_escape_string($sub, $this->con)."',".
  412.     "'".mysql_real_escape_string($com, $this->con)."',".
  413.     "'".mysql_real_escape_string($host, $this->con)."', '".mysql_real_escape_string($status, $this->con)."')";
  414.         $this->_mysql_call($query, array('Insert a new post failed', __LINE__));
  415.     }
  416.  
  417.     /* 檢查是否連續投稿 */
  418.     function isSuccessivePost($lcount, $com, $timestamp, $pass, $passcookie, $host, $isupload){
  419.         $FileIO = PMCLibrary::getFileIOInstance();
  420.         if(!$this->prepared) $this->dbPrepare();
  421.  
  422.         if(!$this->ENV['PERIOD.POST']) return false; // 關閉連續投稿檢查
  423.         $timestamp = intval($timestamp);
  424.         $tmpSQL = 'SELECT pwd,host FROM '.$this->tablename.' WHERE time > '.($timestamp - (int)$this->ENV['PERIOD.POST']); // 一般投稿時間檢查
  425.         if($isupload) $tmpSQL .= ' OR time > '.($timestamp - (int)$this->ENV['PERIOD.IMAGEPOST']); // 附加圖檔的投稿時間檢查 (與下者兩者擇一)
  426.         else $tmpSQL .= ' OR md5(com) = "'.md5($com).'"'; // 內文一樣的檢查 (與上者兩者擇一)
  427.  
  428.         $result = $this->_mysql_call($tmpSQL, array('Get the post to check the succession failed', __LINE__));
  429.         while(list($lpwd, $lhost) = mysql_fetch_row($result)){
  430.             // 判斷為同一人發文且符合連續投稿條件
  431.             if($host==$lhost || $pass==$lpwd || $passcookie==$lpwd) return true;
  432.         }
  433.         return false;
  434.     }
  435.  
  436.     /* 檢查是否重複貼圖 */
  437.     function isDuplicateAttachment($lcount, $md5hash){
  438.         $FileIO = PMCLibrary::getFileIOInstance();
  439.         if(!$this->prepared) $this->dbPrepare();
  440.  
  441.         $result = $this->_mysql_call('SELECT tim,ext FROM '.$this->tablename." WHERE ext <> '' AND md5chksum = '$md5hash' ORDER BY no DESC",
  442.             array('Get the post to check the duplicate attachment failed', __LINE__));
  443.         while(list($ltim, $lext) = mysql_fetch_row($result)){
  444.             if($FileIO->imageExists($ltim.$lext)) return true; // 有相同檔案
  445.         }
  446.         return false;
  447.     }
  448.  
  449.     /* 有此討論串? */
  450.     function isThread($no){
  451.         if(!$this->prepared) $this->dbPrepare();
  452.  
  453.         $result = $this->_mysql_call('SELECT no FROM '.$this->tablename.' WHERE no = '.intval($no).' AND resto = 0');
  454.         return mysql_fetch_array($result) ? true : false;
  455.     }
  456.  
  457.     /* 搜尋文章 */
  458.     function searchPost($keyword, $field, $method){
  459.         if(!$this->prepared) $this->dbPrepare();
  460.  
  461.         $keyword_cnt = count($keyword);
  462.         $SearchQuery = 'SELECT * FROM '.$this->tablename." WHERE {$field} LIKE '%".mysql_real_escape_string($keyword[0], $this->con)."%'";
  463.         if($keyword_cnt > 1){
  464.             for($i = 1; $i < $keyword_cnt; $i++){
  465.                 $SearchQuery .= " {$method} {$field} LIKE '%".mysql_real_escape_string($keyword[$i], $this->con)."%'"; // 多重字串交集 / 聯集搜尋
  466.             }
  467.         }
  468.         $SearchQuery .= ' ORDER BY no DESC'; // 按照號碼大小排序
  469.         $line = $this->_mysql_call($SearchQuery, array('Search the post failed', __LINE__));
  470.         return $this->_ArrangeArrayStructure($line); // 輸出陣列結構
  471.     }
  472.  
  473.     /* 搜尋類別標籤 */
  474.     function searchCategory($category){
  475.         if(!$this->prepared) $this->dbPrepare();
  476.  
  477.         $foundPosts = array();
  478.         $SearchQuery = 'SELECT no FROM '.$this->tablename." WHERE lower(category) LIKE '%,".strtolower(mysql_real_escape_string($category, $this->con)).",%' ORDER BY no DESC";
  479.         $line = $this->_mysql_call($SearchQuery, array('Search the category failed', __LINE__));
  480.         while($rows = mysql_fetch_row($line)) $foundPosts[] = $rows[0];
  481.         mysql_free_result($line);
  482.         return $foundPosts;
  483.     }
  484.  
  485.     /* 取得文章屬性 */
  486.     function getPostStatus($status){
  487.         return new FlagHelper($status); // 回傳 FlagHelper 物件
  488.     }
  489.  
  490.     /* 更新文章 */
  491.     function updatePost($no, $newValues){
  492.         if(!$this->prepared) $this->dbPrepare();
  493.  
  494.         $no = intval($no);
  495.         $chk = array('resto', 'md5chksum', 'category', 'tim', 'ext', 'imgw', 'imgh', 'imgsize', 'tw', 'th', 'pwd', 'now', 'name', 'email', 'sub', 'com', 'host', 'status');
  496.         foreach($chk as $c){
  497.             if(isset($newValues[$c])){
  498.                 $this->_mysql_call('UPDATE '.$this->tablename." SET $c = '".mysql_real_escape_string($newValues[$c], $this->con)."', root = root WHERE no = ".$no,
  499.                     array('Update the field of the post failed', __LINE__)); // 更新討論串屬性
  500.             }
  501.         }
  502.     }
  503.  
  504.     /* 設定文章屬性 */
  505.     function setPostStatus($no, $newStatus){
  506.         $this->updatePost($no, array('status' => $newStatus));
  507.     }
  508. }
  509. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement