Advertisement
Guest User

LWT Google API

a guest
Jul 19th, 2016
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.29 KB | None | 0 0
  1. <?php
  2.  
  3. /**************************************************************
  4. "Learning with Texts" (LWT) is free and unencumbered software
  5. released into the PUBLIC DOMAIN.
  6.  
  7. Anyone is free to copy, modify, publish, use, compile, sell, or
  8. distribute this software, either in source code form or as a
  9. compiled binary, for any purpose, commercial or non-commercial,
  10. and by any means.
  11.  
  12. In jurisdictions that recognize copyright laws, the author or
  13. authors of this software dedicate any and all copyright
  14. interest in the software to the public domain. We make this
  15. dedication for the benefit of the public at large and to the
  16. detriment of our heirs and successors. We intend this
  17. dedication to be an overt act of relinquishment in perpetuity
  18. of all present and future rights to this software under
  19. copyright law.
  20.  
  21. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  23. WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
  24. AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE
  25. FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  27. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  28. THE SOFTWARE.
  29.  
  30. For more information, please refer to [http://unlicense.org/].
  31. ***************************************************************/
  32.  
  33. require_once( 'settings.inc.php' );
  34. require_once( 'connect.inc.php' );
  35. require_once( 'dbutils.inc.php' );
  36. require_once( 'utilities.inc.php' );
  37.  
  38. $ok = FALSE;
  39.  
  40. $from = trim(stripTheSlashesIfNeeded($_REQUEST["sl"]));
  41. $dest = trim(stripTheSlashesIfNeeded($_REQUEST["tl"]));
  42. $phrase = mb_strtolower(trim(stripTheSlashesIfNeeded($_REQUEST["text"])), 'UTF-8');
  43.  
  44. pagestart_nobody('');
  45. //$titletext = '<a href="https://translate.google.com/?ie=UTF-8&sl=' . $from . '&tl=' . $dest . '&text=' . $phrase . '">Google Translate (' . tohtml($from) . "-" . tohtml($dest) . "):  &nbsp; <span class=\"red2\">" . tohtml($phrase) . "</span></a>";
  46. $titletext = 'Google Translate (' . tohtml($from) . "-" . tohtml($dest) . "):  &nbsp; <span class=\"red2\">" . tohtml($phrase) . "</span>";
  47.  
  48. echo '<h3>' . $titletext . '</h3>';
  49. echo '<p>(Click on <img src="icn/tick-button.png" title="Choose" alt="Choose" /> to copy word(s) into above term)<br />&nbsp;</p>';
  50.  
  51. ?>
  52. <script type="text/javascript">
  53. //<![CDATA[
  54. function addTranslation (s) {
  55.     var w = window.parent.frames['ro'];
  56.     if (typeof w == 'undefined') w = window.opener;
  57.     if (typeof w == 'undefined') {
  58.         alert ('Translation can not be copied!');
  59.         return;
  60.     }
  61.     var c = w.document.forms[0].WoTranslation;
  62.     if (typeof c != 'object') {
  63.         alert ('Translation can not be copied!');
  64.         return;
  65.     }
  66.     var oldValue = c.value;
  67.     if (oldValue.trim() == '') {
  68.         c.value = s;
  69.         w.makeDirty();
  70.     }
  71.     else {
  72.         if (oldValue.indexOf(s) == -1) {
  73.             c.value = oldValue + ' / ' + s;
  74.             w.makeDirty();
  75.         }
  76.         else {
  77.             if (confirm('"' + s + '" seems already to exist as a translation.\nInsert anyway?')) {
  78.                 c.value = oldValue + ' / ' + s;
  79.                 w.makeDirty();
  80.             }
  81.         }
  82.     }
  83. }
  84. //]]>
  85. </script>
  86. <?php
  87.  
  88. if ($from != '' && $dest != '' && $phrase != '') {
  89.  
  90.     $ggl_data = file_get_contents('https://translate.googleapis.com/translate_a/single?client=gtx&sl=' . urlencode($from) . '&tl=' . urlencode($dest) . '&dt=t&q=' . urlencode($phrase) . '&ie=UTF-8&oe=UTF-8');
  91.  
  92.     function get_string_between($string, $start, $end){
  93.         $string = " ".$string;
  94.         $ini = strpos($string,$start);
  95.         if ($ini == 0) return "";
  96.         $ini += strlen($start);
  97.         $len = strpos($string,$end,$ini) - $ini;
  98.         return substr($string,$ini,$len);
  99.     }
  100.     $word = get_string_between($ggl_data,'[[["','","');
  101.  
  102.     if(! ($ggl_data === FALSE)) {
  103.         $ok = TRUE;
  104.     }
  105.    
  106. }
  107.  
  108. if ( $ok ) {
  109.  
  110.     if ($word != $phrase) {
  111.  
  112.         echo "<p>\n";
  113.         $word = strtolower(trim(strip_tags($word)));
  114.         echo '<span class="click" onclick="addTranslation(' . prepare_textdata_js($word) . ');"><img src="icn/tick-button.png" title="Copy" alt="Copy" /> &nbsp; ' . $word . '</span><br />' . "\n";
  115.         echo "</p>";
  116.        
  117.     } else {
  118.        
  119.         echo '<p>No translations found (' . tohtml($from) . '-' . tohtml($dest) . ').<br />&nbsp;</p>';
  120.    
  121.     }
  122.    
  123. } else {
  124.  
  125.     echo '<p>Retrieval error (' . tohtml($from) . '-' . tohtml($dest) . '). Possible reason: Terrible code.</p>';
  126.  
  127. }
  128.  
  129. pageend();
  130.  
  131. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement