/includes/functions/func_languages.php

https://github.com/lewellyn/TrellisDesk · PHP · 522 lines · 315 code · 135 blank · 72 comment · 76 complexity · 590e354e6a2bf8a0fa465ee9c4d12111 MD5 · raw file

  1. <?php
  2. /**
  3. * Trellis Desk
  4. *
  5. * @copyright Copyright (C) 2009-2012 ACCORD5. All rights reserved.
  6. * @license GNU General Public License version 3 or later; see LICENSE.txt
  7. */
  8. class td_func_languages {
  9. public $error = '';
  10. #=======================================
  11. # @ Get Languages
  12. #=======================================
  13. public function get($input)
  14. {
  15. $return = array();
  16. $this->trellis->db->construct( array(
  17. 'select' => $input['select'],
  18. 'from' => 'languages',
  19. 'where' => $input['where'],
  20. 'order' => $input['order'],
  21. 'limit' => $input['limit'],
  22. ) );
  23. $this->trellis->db->execute();
  24. if ( ! $this->trellis->db->get_num_rows() ) return false;
  25. while ( $l = $this->trellis->db->fetch_row() )
  26. {
  27. $return[ $l['id'] ] = $l;
  28. }
  29. return $return;
  30. }
  31. #=======================================
  32. # @ Get Single Language
  33. #=======================================
  34. public function get_single($select, $where='')
  35. {
  36. $this->trellis->db->construct( array(
  37. 'select' => $select,
  38. 'from' => 'languages',
  39. 'where' => $where,
  40. 'limit' => array( 0, 1 ),
  41. ) );
  42. $this->trellis->db->execute();
  43. if ( ! $this->trellis->db->get_num_rows() ) return false;
  44. return $this->trellis->db->fetch_row();
  45. }
  46. #=======================================
  47. # @ Get Single Language By ID
  48. #=======================================
  49. public function get_single_by_id($select, $id)
  50. {
  51. return $this->get_single( $select, array( 'id', '=', intval( $id ) ) );
  52. }
  53. #=======================================
  54. # @ Add Language
  55. #=======================================
  56. public function add($data)
  57. {
  58. $fields = array(
  59. 'key' => 'string',
  60. 'name' => 'string',
  61. );
  62. $this->trellis->db->construct( array(
  63. 'insert' => 'languages',
  64. 'set' => $this->trellis->process_data( $fields, $data ),
  65. ) );
  66. $this->trellis->db->execute();
  67. return $this->trellis->db->get_insert_id();
  68. }
  69. #=======================================
  70. # @ Edit Language
  71. #=======================================
  72. public function edit($data, $folder, $file)
  73. {
  74. $path = TD_PATH .'languages/'. $folder .'/'. $file;
  75. if ( ! is_file( $path ) ) return false;
  76. if ( ! $this->writeable( $folder, $file ) ) return false;
  77. if ( ! $handle = fopen( $path, 'wb' ) ) return false;
  78. $file_start = "<?php\n\n/*\n#======================================================\n";
  79. $file_start .= "| | Trellis Desk Language File\n";
  80. $file_start .= "| | ". $file ."\n";
  81. $file_start .= "#======================================================\n*/\n\n";
  82. $file_start .= "\$lang = array(\n\n";
  83. fwrite( $handle, $file_start );
  84. ksort( $data );
  85. foreach( $data as $k => $l )
  86. {
  87. fwrite( $handle, "'". $k ."' => '". addslashes( $l ) ."',\n" );
  88. }
  89. fwrite( $handle, "\n);\n\n?>" );
  90. @fclose( $handle );
  91. return true;
  92. }
  93. #=======================================
  94. # @ Edit Properties
  95. #=======================================
  96. public function edit_prop($data, $id)
  97. {
  98. if ( ! $id = intval( $id ) ) return false;
  99. $fields = array(
  100. 'key' => 'string',
  101. 'name' => 'string',
  102. );
  103. $this->trellis->db->construct( array(
  104. 'update' => 'languages',
  105. 'set' => $this->trellis->process_data( $fields, $data ),
  106. 'where' => array( 'id', '=', $id ),
  107. 'limit' => array( 1 ),
  108. ) );
  109. $this->trellis->db->execute();
  110. return $this->trellis->db->get_affected_rows();
  111. }
  112. #=======================================
  113. # @ Delete Language
  114. #=======================================
  115. public function delete($id, $switchto)
  116. {
  117. if ( ! $id = intval( $id ) ) return false;
  118. if ( ! $switchto = intval( $switchto ) ) return false;
  119. if ( ! $l = $this->trellis->func->languages->get_single_by_id( array( 'id', 'key' ), $id ) ) return false;
  120. $dir = TD_PATH .'languages/'. $l['key'];
  121. if ( ! is_dir( $dir ) || ! is_writable( $dir ) ) return false;
  122. if ( ! $handle = opendir( $dir ) ) return false;
  123. while ( false !== ( $file = readdir( $handle ) ) )
  124. {
  125. if ( is_file( $dir .'/'. $file ) && $file != '.' && $file != '..' )
  126. {
  127. if ( ! is_writable( $dir .'/'. $file ) ) return false;
  128. if ( ! @unlink( $dir .'/'. $file ) ) return false;
  129. }
  130. }
  131. if ( ! @rmdir( $dir ) ) return false;
  132. $this->trellis->db->construct( array(
  133. 'update' => 'users',
  134. 'set' => array( 'lang' => $switchto ),
  135. 'where' => array( 'lang', '=', $id ),
  136. ) );
  137. $this->trellis->db->execute();
  138. $this->trellis->db->construct( array(
  139. 'delete' => 'languages',
  140. 'where' => array( 'id', '=', $id ),
  141. 'limit' => array( 1 ),
  142. ) );
  143. $this->trellis->db->execute();
  144. return $this->trellis->db->get_affected_rows();
  145. }
  146. #=======================================
  147. # @ Default Language
  148. #=======================================
  149. public function set_default($id)
  150. {
  151. if ( ! $id = intval( $id ) ) return false;
  152. $this->trellis->db->construct( array(
  153. 'update' => 'languages',
  154. 'set' => array( 'default' => 0 ),
  155. 'where' => array( 'default', '=', 1 ),
  156. 'limit' => array( 1 ),
  157. ) );
  158. $this->trellis->db->execute();
  159. $this->trellis->db->construct( array(
  160. 'update' => 'languages',
  161. 'set' => array( 'default' => 1 ),
  162. 'where' => array( 'id', '=', $id ),
  163. 'limit' => array( 1 ),
  164. ) );
  165. $this->trellis->db->execute();
  166. return $this->trellis->db->get_affected_rows();
  167. }
  168. #=======================================
  169. # @ Import
  170. #=======================================
  171. public function import( $file )
  172. {
  173. if ( ! $xml = @simplexml_load_file( $file ) ) return false;
  174. if ( $xml->getName() != 'language' ) return false;
  175. if ( ! ( $key = $this->trellis->sanitize_data( $xml['key'] ) ) ) return false;
  176. if ( ! ( $name = $this->trellis->sanitize_data( $xml['name'] ) ) ) return false;
  177. if ( ! ( $version = $this->trellis->sanitize_data( $xml['version'] ) ) ) return false;
  178. if ( ! ( $exported = $this->trellis->sanitize_data( $xml['exported'] ) ) ) return false;
  179. //if ( ! ( $key = $this->trellis->sanitize_data( base64_decode( $xml['key'] ) ) ) ) return false;
  180. //if ( ! ( $name = $this->trellis->sanitize_data( base64_decode( $xml['name'] ) ) ) ) return false;
  181. //if ( ! ( $version = $this->trellis->sanitize_data( base64_decode( $xml['version'] ) ) ) ) return false;
  182. //if ( ! ( $exported = $this->trellis->sanitize_data( base64_decode( $xml['exported'] ) ) ) ) return false;
  183. if ( ! $this->check_key( $key ) ) return false;
  184. if ( $version != $this->trellis->version_number ) return false;
  185. $i = '';
  186. while ( is_dir( ( $dir = TD_PATH .'languages/'. $key . $i ) ) )
  187. {
  188. $i++;
  189. }
  190. $key = $key.$i;
  191. if ( ! @mkdir( $dir, 0755 ) ) return false;
  192. if ( ! is_dir( $dir ) || ! is_writable( $dir ) ) return false;
  193. foreach ( $xml as $f )
  194. {
  195. if ( $f->getName() != 'file' ) return false;
  196. if ( ! ( $this->trellis->sanitize_data( $file = $f['name'] ) ) ) return false;
  197. //if ( ! ( $this->trellis->sanitize_data( $file = base64_decode( $f['name'] ) ) ) ) return false;
  198. if ( ! $handle = fopen( $dir .'/'. $file, 'wb' ) ) return false;
  199. $file_start = "<?php\n\n/*\n#======================================================\n";
  200. $file_start .= "| | Trellis Desk Language File\n";
  201. $file_start .= "| | ". $file ."\n";
  202. $file_start .= "#======================================================\n*/\n\n";
  203. if ( $file != 'lang_email_content.php' )
  204. {
  205. $file_start .= "\$lang = array(\n\n";
  206. }
  207. fwrite( $handle, $file_start );
  208. foreach ( $f as $b )
  209. {
  210. if ( $b->getName() != 'bit' ) return false;
  211. if ( ! ( $this->trellis->sanitize_data( $bk = $b->key ) ) ) return false;
  212. //if ( ! ( $this->trellis->sanitize_data( $bk = base64_decode( $b->key ) ) ) ) return false;
  213. if ( ! ( $this->trellis->sanitize_data( $bv = $b->value ) ) ) return false;
  214. //if ( ! ( $this->trellis->sanitize_data( $bv = base64_decode( $b->value ) ) ) ) return false;
  215. if ( $file == 'lang_email_content.php' )
  216. {
  217. if ( substr( $bk, -4, 4 ) == '_sub' )
  218. {
  219. fwrite( $handle, "\$lang['". $bk ."'] = \"". $bv ."\";\n\n" );
  220. }
  221. else
  222. {
  223. fwrite( $handle, "\$lang['". $bk ."'] = <<<EOF\n". $bv ."\nEOF;\n\n" );
  224. }
  225. }
  226. else
  227. {
  228. fwrite( $handle, "'". $bk ."' => '". addslashes( $this->convert_html( $bv ) ) ."',\n" );
  229. }
  230. }
  231. if ( $file == 'lang_email_content.php' )
  232. {
  233. fwrite( $handle, "?>" );
  234. }
  235. else
  236. {
  237. fwrite( $handle, "\n);\n\n?>" );
  238. }
  239. @fclose( $handle );
  240. }
  241. if ( ! $id = $this->add( array( 'key' => $key, 'name' => $name ) ) ) return false;
  242. return array( 'id' => $id, 'name' => $name );
  243. }
  244. #=======================================
  245. # @ Export
  246. #=======================================
  247. public function export( $id )
  248. {
  249. if ( ! $l = $this->trellis->func->languages->get_single_by_id( array( 'id', 'key', 'name' ), $id ) ) return false;
  250. $dir = TD_PATH .'languages/'. $l['key'];
  251. if ( ! is_readable( $dir ) || ! is_dir( $dir ) ) return false;
  252. $files = array();
  253. if ( ! $handle = opendir( $dir ) ) return false;
  254. while ( false !== ( $file = readdir( $handle ) ) )
  255. {
  256. if ( is_file( $dir .'/'. $file ) && is_readable( $dir .'/'. $file ) && ( strrchr( $file, "." ) == '.php' ) )
  257. {
  258. $files[] = $file;
  259. }
  260. }
  261. if ( empty( $files ) ) return false;
  262. $doc = new DOMDocument();
  263. $doc->formatOutput = true;
  264. $pack = $doc->createElement( 'language' );
  265. $doc->appendChild( $pack );
  266. $key = $doc->createAttribute( 'key' );
  267. $pack->appendChild( $key );
  268. //$key->appendChild( $doc->createTextNode( base64_encode( $l['key'] ) ) );
  269. $key->appendChild( $doc->createTextNode( $l['key'] ) ) ;
  270. $name = $doc->createAttribute( 'name' );
  271. $pack->appendChild( $name );
  272. $name->appendChild( $doc->createTextNode( $l['name'] ) ) ;
  273. //$name->appendChild( $doc->createTextNode( base64_encode( $l['name'] ) ) );
  274. $version = $doc->createAttribute( 'version' );
  275. $pack->appendChild( $version );
  276. $version->appendChild( $doc->createTextNode( $this->trellis->version_number ) ) ;
  277. //$version->appendChild( $doc->createTextNode( base64_encode( $this->trellis->version_number ) ) );
  278. $exported = $doc->createAttribute( 'exported' );
  279. $pack->appendChild( $exported );
  280. $exported->appendChild( $doc->createTextNode( time() ) ) ;
  281. //$exported->appendChild( $doc->createTextNode( base64_encode( time() ) ) );
  282. foreach( $files as &$f )
  283. {
  284. $file = $doc->createElement( 'file' );
  285. $pack->appendChild( $file );
  286. $name = $doc->createAttribute( 'name' );
  287. $file->appendChild( $name );
  288. $name->appendChild( $doc->createTextNode( $f ) );
  289. //$name->appendChild( $doc->createTextNode( base64_encode( $f ) ) );
  290. require $dir .'/'. $f;
  291. foreach ( $lang as $k => &$v )
  292. {
  293. $bit = $doc->createElement( 'bit' );
  294. $file->appendChild( $bit );
  295. $key = $doc->createElement( 'key' );
  296. $bit->appendChild( $key );
  297. $key->appendChild( $doc->createTextNode( $k ) );
  298. //$key->appendChild( $doc->createTextNode( base64_encode( $k ) ) );
  299. $value = $doc->createElement( 'value' );
  300. $bit->appendChild( $value );
  301. $value->appendChild( $doc->createTextNode( $v ) );
  302. //$value->appendChild( $doc->createTextNode( base64_encode( $v ) ) );
  303. }
  304. unset( $lang );
  305. }
  306. header( 'Content-type: text/xml' );
  307. header( 'Content-Disposition: attachment; filename="td_lang_'. $l['key'] .'.xml"' );
  308. print $doc->saveXML();
  309. $this->trellis->shut_down();
  310. exit();
  311. }
  312. #=======================================
  313. # @ Files
  314. #=======================================
  315. public function files($folder)
  316. {
  317. $dir = TD_PATH .'languages/'. $folder;
  318. if ( ! is_readable( $dir ) || ! is_dir( $dir ) ) return false;
  319. $files = array();
  320. if ( ! $handle = opendir( $dir ) ) return false;
  321. while ( false !== ( $file = readdir( $handle ) ) )
  322. {
  323. if ( is_file( $dir .'/'. $file ) && ( strrchr( $file, "." ) == '.php' ) && $file != "lang_email_content.php" )
  324. {
  325. $files[] = $file;
  326. }
  327. }
  328. if ( empty( $files ) ) return false;
  329. return $files;
  330. }
  331. #=======================================
  332. # @ Bits
  333. #=======================================
  334. public function bits($folder, $file)
  335. {
  336. $file = TD_PATH .'languages/'. $folder .'/'. $file;
  337. if ( ! is_readable( $file ) || ! is_file( $file ) ) return false;
  338. require $file;
  339. return $lang;
  340. }
  341. #=======================================
  342. # @ Files
  343. #=======================================
  344. public function writeable($folder, $file)
  345. {
  346. $file = TD_PATH .'languages/'. $folder .'/'. $file;
  347. return is_writable( $file );
  348. }
  349. #=======================================
  350. # @ Prepare Html
  351. #=======================================
  352. public function prepare_html( $data )
  353. {
  354. $data = str_replace( '&', '&amp;', $data );
  355. $data = str_replace( '\'', '&#039;', $data );
  356. $data = str_replace( '\'', '&#39;', $data );
  357. $data = str_replace( '"', '&quot;', $data );
  358. $data = str_replace( '<', '&lt;', $data );
  359. $data = str_replace( '>', '&gt;', $data );
  360. $data = str_replace( '(', '&#40;', $data );
  361. $data = str_replace( ')', '&#41;', $data );
  362. return $data;
  363. }
  364. #=======================================
  365. # @ Convert Html
  366. #=======================================
  367. public function convert_html( $data )
  368. {
  369. $data = str_replace( '&amp;', '&', $data );
  370. $data = str_replace( '&#039;', '\'', $data );
  371. $data = str_replace( '&#39;', '\'', $data );
  372. $data = str_replace( '&quot;', '"', $data );
  373. $data = str_replace( '&lt;', '<', $data );
  374. $data = str_replace( '&gt;', '>', $data );
  375. $data = str_replace( '&#40;', '(', $data );
  376. $data = str_replace( '&#41;', ')', $data );
  377. return $data;
  378. }
  379. #=======================================
  380. # @ Check Key
  381. #=======================================
  382. public function check_key( $key )
  383. {
  384. return preg_match( '/^[a-z0-9]*$/', $key ) ;
  385. }
  386. }
  387. ?>