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