チェンジセット 94: trunk/install/php/foltialib.php

差分発生行の前後
無視リスト:
コミット日時:
2010/02/28 22:28:26 (14 年前)
コミッタ:
sorshi
ログメッセージ:

SQLite採用

ファイル:

凡例:

変更無し
追加
削除
更新
コピー
移動
  • trunk/install/php/foltialib.php

    r89 r94  
    9393                } 
    9494        } 
    95          
    96         /* LIKE 用の文字列のエスケープ */ 
    97         function escape_like($sql, $quote = TRUE) { 
    98                 return ($quote ? "'" : "") . 
    99                        str_replace(array("\\\\",     "%"    , "_"    ), 
    100                                    array("\\\\\\\\", "\\\\%", "\\\\_"), 
    101                                    pg_escape_string($sql)) . 
    102                        ($quote ? "'" : ""); 
    103         } 
    104          
     95 
    10596        /* SQL 文字列のエスケープ */ 
    10697        function escape_string($sql, $quote = FALSE) { 
     
    10899                        return "null"; 
    109100                } 
     101                if (preg_match("/^pgsql/", DSN)){ 
    110102                return ($quote ? "'" : "") . 
    111103                       pg_escape_string($sql) . 
    112104                       ($quote ? "'" : ""); 
    113         } 
     105                }else if (preg_match("/^sqlite/", DSN)){ 
     106                /*      return ($quote ? "'" : "") . 
     107                                sqlite_escape_string($sql) . 
     108                                ($quote ? "'" : ""); 
     109                */ 
     110                return($sql); 
     111                }else{ 
     112                        return "null"; 
     113                } 
     114        }  
    114115         
    115116        /* SQL 数値のエスケープ */ 
     
    124125        } 
    125126         
    126         /* PostgreSQL サーバに接続 */ 
     127        /* DBに接続 */ 
    127128        function m_connect() {  
    128 /*              $con = @pg_connect("host=".DBHOST ." dbname=".DATABASE_NAME . 
    129                                    " user=".USER_NAME . 
    130                                    " password=".USER_PASSWORD); 
    131 */ 
    132                 $con = @pg_pconnect("host=".DBHOST ." dbname=".DATABASE_NAME . 
    133                                    " user=".USER_NAME . 
    134                                    " password=".USER_PASSWORD); 
    135  
    136  
    137                 if (!$con) { 
    138                         die_exit("データベースに接続出来ませんでした。"); 
     129        try { 
     130                $dbh = new PDO(DSN); 
     131                $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
     132                return($dbh); 
     133        } catch (PDOException $e) { 
     134                die_exit($e->getMessage() . ": データベースに接続出来ませんでした。"); 
    139135                } 
    140136                /* データベースと、PHP の内部文字コードが違う場合 */ 
    141                 return($con); 
    142137        } 
    143138 
    144139        /* データベースとの接続を切り離す */ 
    145         function m_close($con) { 
    146                 return @pg_close($con); 
    147         } 
    148  
    149         /* SQL 文を実行 */ 
    150         function m_query($con, $query, $errmessage) { 
    151                 $rtn = @pg_query($con, $query); 
    152                 if (!$rtn) { 
     140function m_close($dbh) { 
     141        return null; 
     142        } 
     143 
     144//旧関数 sql_queryに置き換え 
     145function m_query($dbh, $query, $errmessage) { 
     146        try { 
     147                $rtn = $dbh->query($query); 
     148                return($rtn); 
     149        } catch (PDOException $e) { 
    153150                        /* エラーメッセージに SQL 文を出すのはセキュリティ上良くない!! */ 
    154151                        $msg = $errmessage . "<br>\n" . 
    155                                @pg_last_error($con) . "<br>\n" . 
     152                    $e->getMessage() . "<br>\n" . 
     153                    var_export($e->errorInfo, true) . "<br>\n" . 
    156154                               "<small><code>" . htmlspecialchars($query) . 
    157155                               "</code></small>\n"; 
    158                                $rtn = @pg_query($con, "rollback");//04.4.8 
    159                        m_close($con)
     156//              $dbh->rollBack(); 
     157                $dbh = null
    160158                        die_exit($msg); 
    161159                } 
     160        } 
     161/* SQL 文を実行 */ 
     162function sql_query($dbh, $query, $errmessage,$paramarray) { 
     163        try { 
     164                $rtn = $dbh->prepare("$query"); 
     165                $rtn->execute($paramarray); 
    162166                return($rtn); 
     167        } catch (PDOException $e) { 
     168                        /* エラーメッセージに SQL 文を出すのはセキュリティ上良くない!! */ 
     169                        $msg = $errmessage . "<br>\n" . 
     170                    $e->getMessage() . "<br>\n" . 
     171                    var_export($e->errorInfo, true) . "<br>\n" . 
     172                               "<small><code>" . htmlspecialchars($query) . 
     173                               "</code></small>\n"; 
     174//              $dbh->rollBack(); 
     175                $dbh = null; 
     176                        die_exit($msg); 
     177                } 
    163178        } 
    164179 
     
    166181        function m_showtable($rs) { 
    167182                /* 検索件数 */ 
    168                $maxrows = pg_num_rows($rs)
     183        $maxrows = 0
    169184                 
    170                 if ($maxrows == 0) { 
     185        $rowdata = $rs->fetch(); 
     186        if (! $rowdata) { 
    171187                        echo("<p class=\"msg\">データが存在しません</p>\n"); 
    172188                        return 0; 
     
    174190                 
    175191                /* フィールド数 */ 
    176                $maxcols = pg_num_fields($rs); 
     192        $maxcols = $rs->columnCount(); 
    177193                ?> 
    178194<table class="list" summary="データ検索結果を表示" border="1"> 
     
    183199                                for ($col = 1; $col < $maxcols; $col++) { 
    184200                                        /* pg_field_name() はフィールド名を返す */ 
    185                                         $f_name = htmlspecialchars(pg_field_name($rs, $col)); 
     201                     $meta = $rs->getColumnMeta($col); 
     202                     $f_name = htmlspecialchars($meta["name"]); 
    186203                                        echo("<th abbr=\"$f_name\">$f_name</th>\n"); 
    187204                                } 
     
    192209                <?php 
    193210                        /* テーブルのデータを出力 */ 
    194                         for ($row = 0; $row < $maxrows; $row++) { /* 行に対応 */ 
     211              do { 
     212                      $maxrows++; 
     213 
    195214                                echo("<tr>\n"); 
    196                                 /* pg_fetch_row で一行取り出す */ 
    197                                 $rowdata = pg_fetch_row($rs, $row); 
    198215                                /* 1列目にリンクを張る */ 
    199216                                echo("<td><a href=\"edit.php?q_code=" . 
     
    204221                                } 
    205222                                echo("</tr>\n"); 
    206                        } 
     223              } while ($rowdata = $rs->fetch()); 
    207224                ?> 
    208225        </tbody> 
     
    212229        } 
    213230 
    214         /* 指定したコードのデータを表示 */ 
    215         function m_viewdata($con, $code) { 
    216                 /* コードに該当するデータを検索 */ 
    217                 $query = " 
     231 
     232function m_viewdata($dbh, $code) { 
     233 
     234/*これ使ってないよね? 
     235 
     236        $query = " 
    218237select p.code 
    219238      ,p.name 
     
    222241      ,s.name as job 
    223242      ,p.profile 
    224       ,to_char(p.editdate, 'YYYY/MM/DD HH24:MI:SS') as editdate 
     243      ,datetime(p.editdate) as editdate 
    225244  from inet_profile p left join inet_job s on p.job = s.code 
    226245 where p.code = $code"; 
    227                 $rs = m_query($con, $query, "個人情報の取得に失敗しました。"); 
    228                 if (pg_num_rows($rs) == 0) { 
     246        $rs = m_query($dbh, $query, "個人情報の取得に失敗しました。"); 
     247        $rowdata = $rs->fetch(); 
     248        if (! $rowdata) { 
    229249                        echo("<p class=\"msg\">データが存在しません</p>\n"); 
    230250                        return FALSE; 
    231251                } 
    232252                 
    233                 /* フィールド数 */ 
    234                 $maxcols = pg_num_fields($rs); 
    235                 /* 先頭行 */ 
    236                 $rowdata = pg_fetch_row($rs, 0); 
     253                // フィールド数  
     254        $maxcols = $rs->columnCount(); 
    237255                ?> 
    238256<table class="view" summary="データベース上のデータを表示" border="1"> 
    239257        <tr> 
    240                 <td class="name"><?= htmlspecialchars(pg_field_name($rs, 1)) ?></td> 
     258             <?php $meta = $rs->getColumnMeta(1); ?> 
     259             <td class="name"><?= htmlspecialchars($meta["name"]) ?></td> 
    241260                <td><a href="edit.php?q_code=<?= $rowdata[0] ?>" 
    242261                     ><?= htmlspecialchars($rowdata[1]) ?></a></td> 
    243262        </tr> 
    244         <?php for ($col = 2; $col < $maxcols; $col++) { ?> 
     263             <?php for ($col = 2; $col < $maxcols; $col++) { 
     264                $meta = $rs->getColumnMeta($col); ?> 
    245265        <tr> 
    246                <td class="name"><?= htmlspecialchars(pg_field_name($rs, $col)) ?></td> 
     266            <td class="name"><?= htmlspecialchars($meta["name"]) ?></td> 
    247267                <td><?= htmlspecialchars($rowdata[$col]) ?></td> 
    248268        </tr> 
     
    250270</table> 
    251271                <?php 
    252                 /* クエリーを解放 */ 
    253                 pg_free_result($rs); 
    254                  
     272                // クエリーを解放  
     273      $rs = null; 
    255274                return TRUE; 
     275*/ 
    256276        } 
    257277         
     
    294314        "; 
    295315        $rs = m_query($con, $query, "DBクエリに失敗しました"); 
    296         $maxrows = pg_num_rows($rs); 
    297 if ($maxrows == 0) { 
     316        $rowdata = $rs->fetch(); 
     317       if (! $rowdata) { 
    298318                print("番組データがありません<BR>");                        
    299319}else{ 
     
    301321//print "<ul><!-- ($maxrows) $query -->\n"; 
    302322 
    303 for ($row = 0; $row < $maxrows; $row++) {  
    304           
    305 $rowdata = pg_fetch_row($rs, $row); 
    306  
     323                do { 
    307324$printstarttime = substr($rowdata[0],8,2) . ":" .  substr($rowdata[0],10,2); 
    308325$tdclass = "t".substr($rowdata[0],8,2) .  substr($rowdata[0],10,2); 
     
    323340</li>\n"; 
    324341*/ 
    325 }//for 
     342                } while ($rowdata = $rs->fetch());//do 
    326343//print "</ul>\n"; 
    327344print "</table>\n"; 
     
    429446global $recfolderpath,$recfolderpath; 
    430447 
    431         exec ( "df -h  $recfolderpath | grep $recfolderpath", $hdfreearea); 
    432         $freearea = preg_split ("/[\s,]+/", $hdfreearea[0]); 
     448//      exec ( "df -h  $recfolderpath | grep $recfolderpath", $hdfreearea); 
     449//      $freearea = preg_split ("/[\s,]+/", $hdfreearea[0]); 
     450        exec ( "df -hP  $recfolderpath", $hdfreearea); 
     451        $freearea = preg_split ("/[\s,]+/", $hdfreearea[count($hdfreearea)-1]); 
    433452 
    434453    return $freearea; 
     
    557576        "; 
    558577        $useraccount = m_query($con, $query, "DBクエリに失敗しました"); 
    559         $useraccountrows = pg_num_rows($useraccount); 
    560          
    561         if ($useraccountrows == 1 ){ 
    562                 $rowdata = pg_fetch_row($useraccount, 0); 
     578                $rowdata = $useraccount->fetch(); 
     579                if (! $rowdata) { 
     580                        header("HTTP/1.0 401 Unauthorized"); 
     581                        redirectlogin(); 
     582                } 
     583         
    563584                $memberid = $rowdata[0]; 
    564585                $userclass = $rowdata[1]; 
    565586                $username =  $rowdata[2]; 
    566587                $dbpasswd = $rowdata[3]; 
    567         }else{ 
     588 
     589                $rowdata = $useraccount->fetch(); 
     590                if ($rowdata) { 
    568591                header("HTTP/1.0 401 Unauthorized"); 
    569                 //print "<!-- DEBUG DB record error ($useraccountrows)-->"; 
    570592                redirectlogin(); 
    571         }//end if 
    572  
     593                } 
    573594 
    574595// passwdをdbから取りだし 
     
    627648        "; 
    628649                $useraccount = m_query($con, $query, "DBクエリに失敗しました"); 
    629         $useraccountrows = pg_num_rows($useraccount); 
    630          
    631         if ($useraccountrows == 1 ){ 
    632                 $rowdata = pg_fetch_row($useraccount, 0); 
    633                 //$userclass = $rowdata[1]; 
    634                 return ($rowdata[1]); 
    635         }else{ 
    636         return (99);//エラー 
    637         }//end if 
     650                $rowdata = $useraccount->fetch(); 
     651                if (! $rowdata) { 
     652                        return (99); 
     653                } 
     654         
     655                $userclass = $rowdata[1]; 
     656 
     657                $rowdata = $useraccount->fetch(); 
     658                if ($rowdata) { 
     659                        return (99); 
     660                } 
     661 
     662                return ($userclass); 
    638663         
    639664}else{ 
     
    655680        "; 
    656681                $useraccount = m_query($con, $query, "DBクエリに失敗しました"); 
    657         $useraccountrows = pg_num_rows($useraccount); 
    658          
    659         if ($useraccountrows == 1 ){ 
    660                 $rowdata = pg_fetch_row($useraccount, 0); 
    661                 //$userclass = $rowdata[1]; 
    662                 return ($rowdata[0]); 
    663         }else{ 
     682                $rowdata = $useraccount->fetch(); 
     683                if (! $rowdata) { 
    664684        return (-1);//エラー 
    665         }//end if 
     685                } 
     686 
     687                $memberid = $rowdata[0]; 
     688 
     689                $rowdata = $useraccount->fetch(); 
     690                if ($rowdata) { 
     691                        return (-1); 
     692                } 
     693 
     694                return ($memberid); 
    666695         
    667696}else{ 
     
    682711        "; 
    683712                $useraccount = m_query($con, $query, "DBクエリに失敗しました"); 
    684         $useraccountrows = pg_num_rows($useraccount); 
    685          
    686         if ($useraccountrows == 1 ){ 
    687                 $rowdata = pg_fetch_row($useraccount, 0); 
    688                 return ($rowdata[2]); 
     713                $rowdata = $useraccount->fetch(); 
     714                if (! $rowdata) { 
     715        return ("");//エラー 
     716                } 
     717         
     718                $name = $rowdata[2]; 
     719 
     720                $rowdata = $useraccount->fetch(); 
     721                if ($rowdata) { 
     722        return (""); 
     723                } 
     724 
     725                return ($name); 
     726 
    689727        }else{ 
    690         return ("");//エラー 
     728               return (""); 
    691729        }//end if 
    692          
    693 }else{ 
    694         return (""); 
    695 }//end if 
    696  
    697  
    698730 
    699731}//end function getmemberid2name 
track feed