PageRenderTime 430ms CodeModel.GetById 31ms RepoModel.GetById 5ms app.codeStats 0ms

/extensions/Modules/translator/classes/translator.php

https://github.com/bobnet/PhreeBooksERP
PHP | 242 lines | 207 code | 11 blank | 24 comment | 47 complexity | a0ec0afbddba432a00a5cbd1b92dee18 MD5 | raw file
  1. <?php
  2. // +-----------------------------------------------------------------+
  3. // | PhreeBooks Open Source ERP |
  4. // +-----------------------------------------------------------------+
  5. // | Copyright(c) 2008-2014 PhreeSoft (www.PhreeSoft.com) |
  6. // +-----------------------------------------------------------------+
  7. // | This program is free software: you can redistribute it and/or |
  8. // | modify it under the terms of the GNU General Public License as |
  9. // | published by the Free Software Foundation, either version 3 of |
  10. // | the License, or any later version. |
  11. // | |
  12. // | This program is distributed in the hope that it will be useful, |
  13. // | but WITHOUT ANY WARRANTY; without even the implied warranty of |
  14. // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
  15. // | GNU General Public License for more details. |
  16. // +-----------------------------------------------------------------+
  17. // Path: /modules/phreedom/classes/translator.php
  18. //
  19. class translator {
  20. function __construct() {
  21. global $db;
  22. }
  23. function fetch_stats($mod, $lang, $ver) {
  24. global $db, $messageStack;
  25. $total = 0;
  26. $trans = 0;
  27. $result = $db->Execute("select translated from " . TABLE_TRANSLATOR . "
  28. where module = '" . $mod . "' and language = '" . $lang . "' and version = '" . $ver . "'");
  29. while(!$result->EOF) {
  30. if ($result->fields['translated'] == '1') $trans++;
  31. $total++;
  32. $result->MoveNext();
  33. }
  34. if ($total == 0) $total++;
  35. return array('total' => $total, 'trans' => $trans);
  36. }
  37. function upload_language($dir_dest, $mod, $lang) {
  38. global $db, $backup, $messageStack;
  39. $upload_filename = DIR_FS_MY_FILES . 'translator/translate.zip';
  40. if (!validate_upload('zipfile', 'zip', 'zip')) {
  41. $messageStack->add(TEXT_IMP_ERMSG7, 'error');
  42. return false;
  43. }
  44. if (file_exists($upload_filename)) unlink ($upload_filename);
  45. if (!copy($_FILES['zipfile']['tmp_name'], $upload_filename)) {
  46. $messageStack->add('Error copying to ' . $upload_filename, 'error');
  47. return false;
  48. }
  49. if (!is_dir($dir_dest)) mkdir($dir_dest);
  50. if ($backup->unzip_file($upload_filename, $dir_dest)) {
  51. $messageStack->add('Error unzipping file', 'error');
  52. return false;
  53. }
  54. $this->import_language($dir_dest, $mod, $lang);
  55. if (file_exists($upload_filename)) unlink ($upload_filename);
  56. $backup->delete_dir($dir_dest); // remove unzipped files
  57. return true;
  58. }
  59. function import_language($dir_source=DIR_FS_MODULES, $mod='all', $lang='en_us', $ver=false, $module_dir=false, $chk_method=false, $method_dir=false) {
  60. global $db, $messageStack;
  61. if (!is_dir($dir_source)) return;
  62. $files = scandir($dir_source);
  63. foreach ($files as $file) {
  64. if ($file == "." || $file == "..") continue;
  65. if (is_file($dir_source . $file) && substr($dir_source . $file, -4) == '.php') {
  66. $langfile = file_get_contents($dir_source . $file);
  67. if (!$ver) { // try to pull version form language file, upload mode
  68. $temp = substr($langfile, strpos($langfile, 'Version:')+8, 5);
  69. $temp = preg_replace("/[^0-9.]+/", "", $temp);
  70. $ver = $temp > 0 ? $temp : '0.1';
  71. }
  72. $pathtofile = str_replace(DIR_FS_ADMIN, '', $dir_source . $file);
  73. $langtemp = $this->extractUTF8($langfile);
  74. // preg_match_all("|define\('(.*)',[\s]*'(.*)'\);|imU", $langfile, $langtemp); // broken for UTF-8
  75. $db->Execute("DELETE FROM ".TABLE_TRANSLATOR." WHERE module='$mod' AND language='$lang' AND version='$ver' AND pathtofile='$pathtofile'");
  76. foreach ($langtemp as $const => $value) {
  77. if ($const == 'GEN_COUNTRY_CODE')echo 'writing const = '.$const.' with value = '.$value.'<br>';
  78. $sql = "INSERT INTO ".TABLE_TRANSLATOR." SET
  79. module='$mod', language='$lang', version='$ver', pathtofile='$pathtofile',
  80. defined_constant='".db_input($const)."', translation='".db_input($value)."', translated='1'";
  81. $db->Execute($sql);
  82. }
  83. } elseif (is_dir($dir_source . $file)) {
  84. $tmp_module = $mod;
  85. $tmp_module_dir = $module_dir;
  86. $tmp_chk_method = $chk_method;
  87. $tmp_method_dir = $method_dir;
  88. if ($module_dir) {
  89. $tmp_module = $file;
  90. $tmp_module_dir = false;
  91. $tmp_chk_method = true;
  92. } elseif ($chk_method) {
  93. if ($file == 'methods' || $file == 'dashboards') {
  94. $tmp_chk_method = false;
  95. $tmp_method_dir = true;
  96. }
  97. } elseif ($method_dir) {
  98. $tmp_method_dir = false;
  99. $tmp_module = $mod . '-' . $file;
  100. } elseif ($file == 'soap' || $file == 'install') {
  101. $tmp_module = $file;
  102. } elseif ($file == 'modules') {
  103. $tmp_module_dir = true;
  104. }
  105. //echo 'looking at mod = ' . $mod . ' and dir = ' . $dir_source . $file . '/' . '<br>';
  106. $this->import_language($dir_source . $file . "/", $tmp_module, $lang, $ver, $tmp_module_dir, $tmp_chk_method, $tmp_method_dir);
  107. }
  108. }
  109. }
  110. function convert_language($mod, $lang, $source = 'en_us', $history = '', $subs = array()) {
  111. global $db, $messageStack;
  112. // retrieve highest version
  113. $result = $db->Execute("select max(version) as version from " . TABLE_TRANSLATOR . "
  114. where module = '" . $mod . "' and language = '" . $source . "'");
  115. if ($result->RecordCount() == 0) {
  116. $messageStack->add(TRANS_ERROR_NO_SOURCE,'error');
  117. return false;
  118. }
  119. $ver = $result->fields['version'];
  120. // delete all from the version being written, prevents dups
  121. $db->Execute("delete from " . TABLE_TRANSLATOR . "
  122. where module = '" . $mod . "' and language = '" . $lang . "' and version = '" . $ver . "'");
  123. // load the source language
  124. $result = $db->Execute("select pathtofile, defined_constant, translation from " . TABLE_TRANSLATOR . "
  125. where module = '" . $mod . "' and language = '" . $source . "' and version = '" . $ver . "' order by id");
  126. while (!$result->EOF) {
  127. // fix some fields
  128. $const = $result->fields['defined_constant'];
  129. $trans = $result->fields['translation'];
  130. $translated = false;
  131. if (isset($subs[$history][$const])) {
  132. $temp = $this->pull_latest_ver($subs[$history][$const], $subs[$source][$const], $trans);
  133. $trans = $temp['translation'];
  134. $translated = $temp['translated'];
  135. }
  136. if (isset($subs[$lang][$const])) {
  137. $temp = $this->pull_latest_ver($subs[$lang][$const], $subs[$source][$const], $trans);
  138. $trans = $temp['translation'];
  139. $translated = $temp['translated'];
  140. }
  141. $path = str_replace($source, $lang, $result->fields['pathtofile']);
  142. $sql = "INSERT INTO " . TABLE_TRANSLATOR . " set
  143. module = '" . $mod . "',
  144. language = '" . $lang . "',
  145. version = '" . $ver . "',
  146. pathtofile = '" . $path . "',
  147. defined_constant = '" . db_input($const) . "',
  148. translation = '" . db_input($trans) . "',
  149. translated = '" . $translated . "'";
  150. $db->Execute($sql);
  151. $result->MoveNext();
  152. }
  153. return true;
  154. }
  155. function export_language($mod, $lang, $ver, $hide_error = false) {
  156. global $db, $backup, $messageStack;
  157. $result = $db->Execute("select pathtofile, defined_constant, translation from " . TABLE_TRANSLATOR . "
  158. where module = '" . $mod . "' and language = '" . $lang . "' and version = '" . $ver . "'");
  159. if ($result->RecordCount() == 0) {
  160. if (!$hide_error) $messageStack->add(GEN_BACKUP_DOWNLOAD_EMPTY,'error');
  161. return false; // no rows, return
  162. }
  163. $output = array();
  164. $header = '<' . '?' . 'php' . chr(10);
  165. $header .= '// +-----------------------------------------------------------------+' . chr(10);
  166. $header .= '// ' . TRANSLATION_HEADER . chr(10);
  167. $header .= '// Generated: ' . date('Y-m-d h:i:s') . chr(10);
  168. $header .= '// Module/Method: ' . $mod . chr(10);
  169. $header .= '// ISO Language: ' . $lang . chr(10);
  170. $header .= '// Version: ' . $ver . chr(10);
  171. $header .= '// +-----------------------------------------------------------------+' . chr(10);
  172. while (!$result->EOF) {
  173. if (!isset($output[$result->fields['pathtofile']])) {
  174. $output[$result->fields['pathtofile']] = $header;
  175. $output[$result->fields['pathtofile']] .= '// Path: /' . $result->fields['pathtofile'] . chr(10) . chr(10);
  176. }
  177. $temp = 'define(\'' . $result->fields['defined_constant'] . '\',\'';
  178. $temp .= addslashes($result->fields['translation']) . '\');';
  179. $output[$result->fields['pathtofile']] .= $temp . chr(10);
  180. $result->MoveNext();
  181. }
  182. foreach ($output as $path => $content) {
  183. $content .= chr(10) . '?' . '>' . chr(10); // terminate the file
  184. $new_dir = $backup->source_dir . substr ($path, 0, strrpos($path, '/'));
  185. $filename = substr ($path, strrpos($path,'/')+1);
  186. if (!is_dir($new_dir)) mkdir($new_dir, 0777, true);
  187. if (!$fp = fopen($new_dir . '/' . $filename, 'w')) {
  188. if (!$hide_error) $messageStack->add('Error opening ' . $new_dir . '/' . $filename,'error');
  189. return false;
  190. }
  191. fwrite($fp, $content);
  192. fclose($fp);
  193. }
  194. return true;
  195. }
  196. function pull_latest_ver($versions, $sources, $cur_trans) {
  197. $translation = '';
  198. $translated = '0';
  199. if (is_array($versions)) {
  200. krsort($versions);
  201. foreach ($versions as $ver => $value) { // just need the first value, i.e. highest version
  202. $translation = $value;
  203. $translated = $sources[$ver] == $cur_trans ? '1' : '0';
  204. break;
  205. }
  206. }
  207. return array('translation' => $translation, 'translated' => $translated);
  208. }
  209. function extractUTF8($langFile) { // handles both single and double quotes
  210. // replaced preg_match_all which failed for unicode characters
  211. $runaway = 0;
  212. $output = array();
  213. while(true) {
  214. if ($runaway++ > 50000) break;
  215. if (strpos($langFile, 'define') === false) break;
  216. $langFile = trim(substr($langFile, strpos($langFile, 'define')+6)); // find first define
  217. $langFile = trim(substr($langFile, 1)); // remove '('
  218. $quotChar = $langFile[0]; // quote character for constant
  219. $const = substr($langFile, 1, strpos($langFile, $quotChar, 1)-1); // from after first quotechar to just before second
  220. $langFile = trim(substr($langFile, strpos($langFile, $quotChar, 1)+1)); // remove constant and quotes from input string
  221. $langFile = trim(substr($langFile, 1)); // remove ',' between define statement
  222. $quotChar = $langFile[0]; // quote character for value
  223. $value = trim(substr($langFile, 1, strpos($langFile, ');'))); // ASSUME define statment ends with no space between ) and ;
  224. $value = substr($value, 0, strrpos($value, $quotChar)); // remove closing quote character
  225. $langFile = trim(substr($langFile, strpos($langFile, ');')+2)); // remove ');' at end of define
  226. $output[$const] = $value;
  227. }
  228. return $output;
  229. }
  230. }
  231. ?>