Ignore:
Timestamp:
03/19/2009 03:14:56 PM (3 years ago)
Author:
amir
Message:

Original text is now encoded as base64 text to avoid encoding/decoding on the client side.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/WordPress/plugin/transposh/transposh.php

    r76 r79  
    220220/* 
    221221 * Fetch translation from db or cache. 
    222  * Returns the translated string or NULL if not available. 
    223  * TODO: // will an array work? 
     222 * Returns An array that contains the translated string and it source.  
     223 *         Will return NULL if no translation is available. 
    224224 */ 
    225225function fetch_translation($original) 
     
    228228    $translated = NULL; 
    229229    logger("Enter " . __METHOD__ . ": $original", 4); 
    230     logger("Original was: $original", 3); 
    231     $original = $wpdb->escape(html_entity_decode($original, ENT_NOQUOTES, 'UTF-8')); 
    232     logger("Original is: $original", 3); 
     230     
     231    //The original is saved in db in its escaped form 
     232    $original = $wpdb->escape($original); 
     233     
    233234    if(ENABLE_APC && function_exists('apc_fetch')) 
    234235    { 
     
    335336    $url = $home_url . '/index.php'; 
    336337 
     338    //Use base64 encoding to make that when the page is translated (i.e. update_translation) we 
     339    //get back exactlly the same string without having the client decode/encode it in anyway.  
     340    $token = base64_encode($original); 
     341     
    337342    //For use in javascript, make the following changes: 
    338343    //1. Add slashes to escape the inner text 
     
    351356    } 
    352357 
    353     $img = "<img src=\"$plugin_url/translate$add_img.png\" alt=\"translate\" id=\"" . IMG_PREFIX . "$segment_id\" 
     358    $img = "<img src=\"$plugin_url/translate$add_img.png\" token=\"$token\" alt=\"translate\" id=\"" . IMG_PREFIX . "$segment_id\" 
    354359           onclick=\"translate_dialog('$original','$translation','$segment_id'); return false;\" 
    355360           onmouseover=\"hint('$original'); return true;\" 
    356361           onmouseout=\"nd()\" />"; 
    357  
    358     return $img; 
     362     
     363        return $img; 
    359364} 
    360365 
     
    405410 
    406411/* 
    407  * Helper function for annoying strings from php escape (%u2019) 
    408  */ 
    409 function utf8_urldecode($str) { 
    410     $str = preg_replace("/%u([0-9a-f]{3,4})/i","&#x\\1;",urldecode($str)); 
    411     return html_entity_decode($str,null,'UTF-8');; 
    412   } 
    413  
    414 /* 
    415412 * A new translation has been posted, update the translation database. 
    416413 * 
     
    421418 
    422419    $ref=getenv('HTTP_REFERER'); 
    423     $original = $_POST['original']; 
     420    $original =  base64_decode($_POST['token'], TRUE); 
    424421    $translation = $_POST['translation']; 
    425422    $lang = $_POST['lang']; 
     
    439436 
    440437    //Decode & remove already escaped character to avoid double escaping 
    441     // TODO: remove logging? 
    442     logger("origwas:" .$original,4); 
    443     $original = utf8_urldecode($original); 
    444     logger("orig2:" .$original,4); 
    445     $original    = $wpdb->escape(stripslashes(urldecode(html_entity_decode($original, null, 'UTF-8')))); 
    446     logger("orig:" .$original,4); 
    447438    $translation = $wpdb->escape(htmlspecialchars(stripslashes(urldecode($translation)))); 
    448  
    449     //TODO: Check more escaping... 
    450  
     439     
     440    //The original content is encoded as base64 before it is sent (i.e. token), after we 
     441    //decode it should just the same after it was parsed.   
     442    $original = $wpdb->escape($original); 
     443     
    451444    $update = "REPLACE INTO  $table_name (original, translated, lang, source) 
    452445                VALUES ('" . $original . "','" . $translation . "','" . $lang . "','" . $source . "')"; 
     
    462455        { 
    463456            apc_delete($original .'___'. $lang); 
    464            // TODO: update cache 
    465             //$rc = apc_store($original .'___'. $lang, $cache_entry, 3600); 
    466             //if($rc === TRUE) 
    467             //{ 
    468 //              logger("Stored in cache: $original => $translated", 3); 
    469             //} 
    470457        } 
    471458 
     
    635622                $table_name = $wpdb->prefix . TRANSLATIONS_TABLE; 
    636623 
    637     //if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) 
    638     //{ 
    639624        logger("Attempting to create table $table_name", 0); 
    640625        $sql = "CREATE TABLE $table_name (original VARCHAR(256) NOT NULL, 
     
    647632        dbDelta($sql); 
    648633 
    649                 // TODO: remove this? 
    650         //Verify that newly created table is ready for use. 
    651         //$insert = "INSERT INTO " . $table_name . " (original, translated, lang) " . 
    652         //"VALUES ('Hello','Hi There','zz')"; 
    653  
    654         //$result = $wpdb->query($insert); 
    655  
    656         //if($result === FALSE) 
    657         //{ 
    658             //logger("Error failed to create $table_name !!!", 0); 
    659         //} 
    660         //else 
    661         //{ 
    662                 // logger("Table $table_name was created successfuly", 0); 
    663         //} 
    664     //} 
    665634 
    666635        $table_name = $wpdb->prefix . TRANSLATIONS_LOG; 
    667636 
    668     //if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) 
    669     //{ 
    670637        logger("Attempting to create table $table_name", 0); 
    671638        $sql = "CREATE TABLE " . $table_name . " (original VARCHAR(256) NOT NULL, 
     
    679646 
    680647        dbDelta($sql); 
    681         // TODO: check 
    682648                update_option(TRANSPOSH_DB_VERSION, DB_VERSION); 
    683649    } 
     
    757723 
    758724    if (function_exists('deactivate_plugins') ) { 
    759         deactivate_plugins("transposh/translate.php", "translate.php"); 
     725        deactivate_plugins(get_plugin_name(), "translate.php"); 
    760726        echo '<br> This plugin has been automatically deactivated.'; 
    761727    } 
     
    788754    } 
    789755 
    790     // TODO: This is wrong? and also deactivation fails 
    791     if (get_option(TRANSPOSH_DB_VERSION) == NULL) 
     756    if ($db_version != DB_VERSION) 
    792757    { 
    793758        $admin_msg = "Failed to locate the translation table  <em> " . TRANSLATIONS_TABLE . "</em> in local database. <br>"; 
Note: See TracChangeset for help on using the changeset viewer.