Changeset 1694 for branches/3.0

Show
Ignore:
Timestamp:
02/12/2007 11:52:54 PM (22 months ago)
Author:
wei
Message:

Fix #527

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • branches/3.0/framework/I18N/core/MessageSource_MySQL.php

    r1397 r1694  
    3030/** 
    3131 * MessageSource_MySQL class. 
    32  *  
     32 * 
    3333 * Retrive the message translation from a MySQL database. 
    3434 * 
     
    4343        /** 
    4444         * The datasource string, full DSN to the database. 
    45          * @var string  
     45         * @var string 
    4646         */ 
    4747        protected $source; 
    48                  
     48 
    4949        /** 
    5050         * The DSN array property, parsed by PEAR's DB DSN parser. 
    51          * @var array  
     51         * @var array 
    5252         */ 
    5353        protected $dns; 
    54          
     54 
    5555        /** 
    5656         * A resource link to the database 
    57          * @var db  
     57         * @var db 
    5858         */ 
    5959        protected $db; 
     
    6666        function __construct($source) 
    6767        { 
    68                 $this->source = (string)$source;                 
     68                $this->source = (string)$source; 
    6969                $this->dns = parseDSN($this->source); 
    7070                $this->db = $this->connect(); 
    7171        } 
    72          
     72 
    7373        /** 
    7474         * Destructor, close the database connection. 
    7575         */ 
    76         function __destruct()  
     76        function __destruct() 
    7777        { 
    7878                @mysql_close($this->db); 
    7979        } 
    80          
     80 
    8181        /** 
    8282         * Connect to the MySQL datasource 
    83          * @return resource MySQL connection.  
     83         * @return resource MySQL connection. 
    8484         * @throws Exception, connection and database errors. 
    8585         */ 
     
    8787        { 
    8888                /*static $conn; 
    89                  
     89 
    9090                if(!is_null($conn)) 
    9191                        return $conn; 
    9292                */ 
    9393                $dsninfo = $this->dns; 
    94                  
    95         if (isset($dsninfo['protocol']) && $dsninfo['protocol'] == 'unix')  
     94 
     95        if (isset($dsninfo['protocol']) && $dsninfo['protocol'] == 'unix') 
    9696            $dbhost = ':' . $dsninfo['socket']; 
    97         else  
     97        else 
    9898        { 
    9999                        $dbhost = $dsninfo['hostspec'] ? $dsninfo['hostspec'] : 'localhost'; 
    100             if (!empty($dsninfo['port']))  
     100            if (!empty($dsninfo['port'])) 
    101101                $dbhost .= ':' . $dsninfo['port']; 
    102102        } 
    103103        $user = $dsninfo['username']; 
    104         $pw = $dsninfo['password'];              
    105          
     104        $pw = $dsninfo['password']; 
     105 
    106106        $connect_function = 'mysql_connect'; 
    107107 
     
    113113            $conn = @$connect_function($dbhost); 
    114114        else 
    115             $conn = false;         
    116                      
    117         if (empty($conn))  
     115            $conn = false; 
     116 
     117        if (empty($conn)) 
    118118        { 
    119119                throw new Exception('Error in connecting to '.$dsninfo); 
    120120        } 
    121          
    122         if ($dsninfo['database'])  
     121 
     122        if ($dsninfo['database']) 
    123123        { 
    124                 if (!@mysql_select_db($dsninfo['database'], $conn))  
     124                if (!@mysql_select_db($dsninfo['database'], $conn)) 
    125125                        throw new Exception('Error in connecting database, dns:'. 
    126126                                                                $dsninfo); 
     
    128128        else 
    129129                throw new Exception('Please provide a database for message'. 
    130                                                         ' translation.');                                                        
     130                                                        ' translation.'); 
    131131       return $conn; 
    132132        } 
    133          
     133 
    134134        /** 
    135135         * Get the database connection. 
    136          * @return db database connection.  
     136         * @return db database connection. 
    137137         */ 
    138138        public function connection() 
     
    140140                return $this->db; 
    141141        } 
    142          
    143         /** 
    144          * Get an array of messages for a particular catalogue and cultural  
     142 
     143        /** 
     144         * Get an array of messages for a particular catalogue and cultural 
    145145         * variant. 
    146146         * @param string the catalogue name + variant 
    147147         * @return array translation messages. 
    148          */              
     148         */ 
    149149        protected function &loadData($variant) 
    150         {                
    151                 $variant = mysql_escape_string($variant); 
    152                  
    153                 $statement =  
     150        { 
     151                $variant = mysql_real_escape_string($variant); 
     152 
     153                $statement = 
    154154                        "SELECT t.id, t.source, t.target, t.comments 
    155155                                FROM trans_unit t, catalogue c 
    156156                                WHERE c.cat_id =  t.cat_id 
    157                                         AND c.name = '{$variant}'  
     157                                        AND c.name = '{$variant}' 
    158158                                ORDER BY id ASC"; 
    159                          
     159 
    160160                $rs = mysql_query($statement,$this->db); 
    161                          
     161 
    162162                $result = array(); 
    163                  
     163 
    164164                while($row = mysql_fetch_array($rs,MYSQL_NUM)) 
    165165                { 
     
    169169                        $result[$source][] = $row[3]; //comments 
    170170                } 
    171                  
     171 
    172172                return $result; 
    173173        } 
    174          
     174 
    175175        /** 
    176176         * Get the last modified unix-time for this particular catalogue+variant. 
     
    178178         * @param string catalogue+variant 
    179179         * @return int last modified in unix-time format. 
    180          */      
     180         */ 
    181181        protected function getLastModified($source) 
    182182        { 
    183                 $source = mysql_escape_string($source); 
     183                $source = mysql_real_escape_string($source); 
    184184 
    185185                $rs = mysql_query( 
    186186                        "SELECT date_modified FROM catalogue WHERE name = '{$source}'", 
    187187                        $this->db); 
    188                          
     188 
    189189                $result = $rs ? intval(mysql_result($rs,0)) : 0; 
    190                          
    191                 return $result;                  
    192         } 
    193                  
     190 
     191                return $result; 
     192        } 
     193 
    194194        /** 
    195195         * Check if a particular catalogue+variant exists in the database. 
    196196         * @param string catalogue+variant 
    197          * @return boolean true if the catalogue+variant is in the database,  
     197         * @return boolean true if the catalogue+variant is in the database, 
    198198         * false otherwise. 
    199          */      
     199         */ 
    200200        protected function isValidSource($variant) 
    201201        { 
    202                 $variant = mysql_escape_string ($variant); 
    203  
    204                 $rs = mysql_query(  
     202                $variant = mysql_real_escape_string ($variant); 
     203 
     204                $rs = mysql_query( 
    205205                        "SELECT COUNT(*) FROM catalogue WHERE name = '{$variant}'", 
    206206                        $this->db); 
    207                          
     207 
    208208                $row = mysql_fetch_array($rs,MYSQL_NUM); 
    209                  
     209 
    210210                $result = $row && $row[0] == '1'; 
    211211 
    212212                return $result; 
    213213        } 
    214          
     214 
    215215        /** 
    216216         * Get all the variants of a particular catalogue. 
    217217         * @param string catalogue name 
    218          * @return array list of all variants for this catalogue.  
    219          */       
     218         * @return array list of all variants for this catalogue. 
     219         */ 
    220220        protected function getCatalogueList($catalogue) 
    221221        { 
    222222                $variants = explode('_',$this->culture); 
    223                  
     223 
    224224                $catalogues = array($catalogue); 
    225225 
    226226                $variant = null; 
    227                                  
     227 
    228228                for($i = 0, $k = count($variants); $i < $k; ++$i) 
    229                 {        
     229                { 
    230230                        if(isset($variants[$i]{0})) 
    231231                        { 
     
    234234                        } 
    235235                } 
    236                 return array_reverse($catalogues);       
    237         }        
    238          
     236                return array_reverse($catalogues); 
     237        } 
     238 
    239239        /** 
    240240         * Retrive catalogue details, array($cat_id, $variant, $count). 
    241241         * @param string catalogue 
    242          * @return array catalogue details, array($cat_id, $variant, $count).  
     242         * @return array catalogue details, array($cat_id, $variant, $count). 
    243243         */ 
    244244        private function getCatalogueDetails($catalogue='messages') 
     
    248248 
    249249                $variant = $catalogue.'.'.$this->culture; 
    250                  
    251                 $name = mysql_escape_string($this->getSource($variant));         
    252                                  
     250 
     251                $name = mysql_real_escape_string($this->getSource($variant)); 
     252 
    253253                $rs = mysql_query("SELECT cat_id 
    254254                                        FROM catalogue WHERE name = '{$name}'", $this->db); 
    255                  
     255 
    256256                if(mysql_num_rows($rs) != 1) 
    257257                        return false; 
    258                  
     258 
    259259                $cat_id = intval(mysql_result($rs,0)); 
    260                  
     260 
    261261                //first get the catalogue ID 
    262262                $rs = mysql_query( 
     
    266266 
    267267                $count = intval(mysql_result($rs,0)); 
    268          
     268 
    269269                return array($cat_id, $variant, $count); 
    270         }        
    271          
     270        } 
     271 
    272272        /** 
    273273         * Update the catalogue last modified time. 
    274          * @return boolean true if updated, false otherwise.  
     274         * @return boolean true if updated, false otherwise. 
    275275         */ 
    276276        private function updateCatalogueTime($cat_id, $variant) 
    277277        { 
    278278                $time = time(); 
    279                  
    280                 $result = mysql_query("UPDATE catalogue  
     279 
     280                $result = mysql_query("UPDATE catalogue 
    281281                                                        SET date_modified = {$time} 
    282282                                                        WHERE cat_id = {$cat_id}", $this->db); 
    283                          
     283 
    284284                if(!empty($this->cache)) 
    285                         $this->cache->clean($variant, $this->culture);   
    286                  
     285                        $this->cache->clean($variant, $this->culture); 
     286 
    287287                return $result; 
    288         }        
    289          
    290         /** 
    291          * Save the list of untranslated blocks to the translation source.  
     288        } 
     289 
     290        /** 
     291         * Save the list of untranslated blocks to the translation source. 
    292292         * If the translation was not found, you should add those 
    293293         * strings to the translation source via the <b>append()</b> method. 
     
    298298        { 
    299299                $messages = $this->untranslated; 
    300                  
    301                 if(count($messages) <= 0) return false;          
    302  
    303                 $details = $this->getCatalogueDetails($catalogue);       
    304                  
     300 
     301                if(count($messages) <= 0) return false; 
     302 
     303                $details = $this->getCatalogueDetails($catalogue); 
     304 
    305305                if($details) 
    306306                        list($cat_id, $variant, $count) = $details; 
    307307                else 
    308                         return false;                                    
    309                  
     308                        return false; 
     309 
    310310                if($cat_id <= 0) return false; 
    311311                $inserted = 0; 
     
    316316                { 
    317317                        $count++; $inserted++; 
    318                         $message = mysql_escape_string($message); 
     318                        $message = mysql_real_escape_string($message); 
    319319                        $statement = "INSERT INTO trans_unit 
    320320                                (cat_id,id,source,date_added) VALUES 
     
    323323                } 
    324324                if($inserted > 0) 
    325                         $this->updateCatalogueTime($cat_id, $variant);                   
     325                        $this->updateCatalogueTime($cat_id, $variant); 
    326326 
    327327                return $inserted > 0; 
    328         }        
    329          
     328        } 
     329 
    330330        /** 
    331331         * Delete a particular message from the specified catalogue. 
    332332         * @param string the source message to delete. 
    333333         * @param string the catalogue to delete from. 
    334          * @return boolean true if deleted, false otherwise.  
     334         * @return boolean true if deleted, false otherwise. 
    335335         */ 
    336336        function delete($message, $catalogue='messages') 
     
    341341                else 
    342342                        return false; 
    343                          
    344                 $text = mysql_escape_string($message); 
    345                  
     343 
     344                $text = mysql_real_escape_string($message); 
     345 
    346346                $statement = "DELETE FROM trans_unit WHERE 
    347347                                                cat_id = {$cat_id} AND source = '{$message}'"; 
    348348                $deleted = false; 
    349                                  
     349 
    350350                mysql_query($statement, $this->db); 
    351351 
    352352                if(mysql_affected_rows($this->db) == 1) 
    353                         $deleted = $this->updateCatalogueTime($cat_id, $variant);                
    354                                                          
     353                        $deleted = $this->updateCatalogueTime($cat_id, $variant); 
     354 
    355355                return $deleted; 
    356356 
    357357        } 
    358                  
     358 
    359359        /** 
    360360         * Update the translation. 
     
    363363         * @param string comments 
    364364         * @param string the catalogue of the translation. 
    365          * @return boolean true if translation was updated, false otherwise.  
    366          */      
     365         * @return boolean true if translation was updated, false otherwise. 
     366         */ 
    367367        function update($text, $target, $comments, $catalogue='messages') 
    368368        { 
     
    372372                else 
    373373                        return false; 
    374                  
    375                 $comments = mysql_escape_string($comments); 
    376                 $target = mysql_escape_string($target); 
    377                 $text = mysql_escape_string($text); 
    378                  
     374 
     375                $comments = mysql_real_escape_string($comments); 
     376                $target = mysql_real_escape_string($target); 
     377                $text = mysql_real_escape_string($text); 
     378 
    379379                $time = time(); 
    380                                  
     380 
    381381                $statement = "UPDATE trans_unit SET 
    382382                                                target = '{$target}', 
    383383                                                comments = '{$comments}', 
    384384                                                date_modified = '{$time}' 
    385                                         WHERE cat_id = {$cat_id}  
     385                                        WHERE cat_id = {$cat_id} 
    386386                                                AND source = '{$text}'"; 
    387                  
     387 
    388388                $updated = false; 
    389                  
     389 
    390390                mysql_query($statement, $this->db); 
    391391                if(mysql_affected_rows($this->db) == 1) 
    392392                        $updated = $this->updateCatalogueTime($cat_id, $variant); 
    393                                          
     393 
    394394                return $updated; 
    395395        } 
    396          
     396 
    397397        /** 
    398398         * Returns a list of catalogue as key and all it variants as value. 
    399          * @return array list of catalogues  
     399         * @return array list of catalogues 
    400400         */ 
    401401        function catalogues() 
     
    408408                        $details = explode('.',$row[0]); 
    409409                        if(!isset($details[1])) $details[1] = null; 
    410                          
     410 
    411411                        $result[] = $details; 
    412412                } 
    413413                return $result; 
    414414        } 
    415          
     415 
    416416} 
    417417