/includes/functions/func_skins.php

https://github.com/lewellyn/TrellisDesk · PHP · 976 lines · 620 code · 244 blank · 112 comment · 174 complexity · fc7a61fc21039b53bc1751b4169b3967 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_skins {
  9. public $error = '';
  10. #=======================================
  11. # @ Get Skins
  12. #=======================================
  13. public function get($input)
  14. {
  15. $return = array();
  16. $this->trellis->db->construct( array(
  17. 'select' => $input['select'],
  18. 'from' => 'skins',
  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 ( $s = $this->trellis->db->fetch_row() )
  26. {
  27. $return[ $s['id'] ] = $s;
  28. }
  29. return $return;
  30. }
  31. #=======================================
  32. # @ Get Single Skin
  33. #=======================================
  34. public function get_single($select, $where='')
  35. {
  36. $this->trellis->db->construct( array(
  37. 'select' => $select,
  38. 'from' => 'skins',
  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 Skin 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. # @ Get Template
  55. #=======================================
  56. public function get_template($id, $file)
  57. {
  58. $file = TD_SKIN .'s'. $id .'/templates/'. $file;
  59. if ( ! is_readable( $file ) || ! is_file( $file ) ) return false;
  60. return file_get_contents( $file );
  61. }
  62. #=======================================
  63. # @ Get CSS
  64. #=======================================
  65. public function get_css($id, $file)
  66. {
  67. $file = TD_SKIN .'s'. $id .'/css/'. $file;
  68. if ( ! is_readable( $file ) || ! is_file( $file ) ) return false;
  69. return file_get_contents( $file );
  70. }
  71. #=======================================
  72. # @ Get Wrapper
  73. #=======================================
  74. public function get_wrapper($id)
  75. {
  76. $file = TD_SKIN .'s'. $id .'/templates/wrapper.tpl';
  77. if ( ! is_readable( $file ) || ! is_file( $file ) ) return false;
  78. return file_get_contents( $file );
  79. }
  80. #=======================================
  81. # @ Add Skin
  82. #=======================================
  83. public function add($data)
  84. {
  85. $fields = array(
  86. 'name' => 'string',
  87. );
  88. $this->trellis->db->construct( array(
  89. 'insert' => 'skins',
  90. 'set' => $this->trellis->process_data( $fields, $data ),
  91. ) );
  92. $this->trellis->db->execute();
  93. return $this->trellis->db->get_insert_id();
  94. }
  95. #=======================================
  96. # @ Edit Skin
  97. #=======================================
  98. public function edit($data, $id, $file)
  99. {
  100. $path = TD_SKIN .'s'. $id .'/templates/'. $file;
  101. if ( ! is_file( $path ) ) return false;
  102. if ( ! $this->writeable( $id, $file ) ) return false;
  103. return file_put_contents( $path, $data );
  104. }
  105. #=======================================
  106. # @ Edit CSS
  107. #=======================================
  108. public function edit_css($data, $id, $file)
  109. {
  110. $path = TD_SKIN .'s'. $id .'/css/'. $file;
  111. if ( ! is_file( $path ) ) return false;
  112. if ( ! $this->writeable_css( $id, $file ) ) return false;
  113. return file_put_contents( $path, $data );
  114. }
  115. #=======================================
  116. # @ Edit Wrapper
  117. #=======================================
  118. public function edit_wrapper($data, $id)
  119. {
  120. $path = TD_SKIN .'s'. $id .'/templates/wrapper.tpl';
  121. if ( ! is_file( $path ) ) return false;
  122. if ( ! $this->writeable_wrapper( $id ) ) return false;
  123. return file_put_contents( $path, $data );
  124. }
  125. #=======================================
  126. # @ Edit Properties
  127. #=======================================
  128. public function edit_prop($data, $id)
  129. {
  130. if ( ! $id = intval( $id ) ) return false;
  131. $fields = array(
  132. 'name' => 'string',
  133. );
  134. $this->trellis->db->construct( array(
  135. 'update' => 'skins',
  136. 'set' => $this->trellis->process_data( $fields, $data ),
  137. 'where' => array( 'id', '=', $id ),
  138. 'limit' => array( 1 ),
  139. ) );
  140. $this->trellis->db->execute();
  141. return $this->trellis->db->get_affected_rows();
  142. }
  143. #=======================================
  144. # @ Delete Skin
  145. #=======================================
  146. public function delete($id, $switchto)
  147. {
  148. if ( ! $id = intval( $id ) ) return false;
  149. if ( ! $switchto = intval( $switchto ) ) return false;
  150. $dir = TD_PATH .'skins/s'. $id;
  151. if ( ! is_dir( $dir ) || ! is_writable( $dir ) ) return false;
  152. if ( ! $this->_rrmdir( $dir ) ) return false;
  153. $this->trellis->db->construct( array(
  154. 'update' => 'users',
  155. 'set' => array( 'skin' => $switchto ),
  156. 'where' => array( 'skin', '=', $id ),
  157. ) );
  158. $this->trellis->db->execute();
  159. $this->trellis->db->construct( array(
  160. 'delete' => 'skins',
  161. 'where' => array( 'id', '=', $id ),
  162. 'limit' => array( 1 ),
  163. ) );
  164. $this->trellis->db->execute();
  165. return $this->trellis->db->get_affected_rows();
  166. }
  167. #=======================================
  168. # @ Recursive rmdir
  169. #=======================================
  170. private function _rrmdir($dir)
  171. {
  172. if ( ! $handle = opendir( $dir ) ) return false;
  173. while ( false !== ( $file = readdir( $handle ) ) )
  174. {
  175. if ( $file != '.' && $file != '..' )
  176. {
  177. if ( is_file( $dir .'/'. $file ) )
  178. {
  179. if ( ! is_writable( $dir .'/'. $file ) ) return false;
  180. if ( ! @unlink( $dir .'/'. $file ) ) return false;
  181. }
  182. elseif ( is_dir( $dir .'/'. $file ) )
  183. {
  184. if ( ! $this->_rrmdir( $dir .'/'. $file ) ) return false;
  185. }
  186. }
  187. }
  188. return @rmdir( $dir );
  189. }
  190. #=======================================
  191. # @ Default Skin
  192. #=======================================
  193. public function set_default($id)
  194. {
  195. if ( ! $id = intval( $id ) ) return false;
  196. $this->trellis->db->construct( array(
  197. 'update' => 'skins',
  198. 'set' => array( 'default' => 0 ),
  199. 'where' => array( 'default', '=', 1 ),
  200. 'limit' => array( 1 ),
  201. ) );
  202. $this->trellis->db->execute();
  203. $this->trellis->db->construct( array(
  204. 'update' => 'skins',
  205. 'set' => array( 'default' => 1 ),
  206. 'where' => array( 'id', '=', $id ),
  207. 'limit' => array( 1 ),
  208. ) );
  209. $this->trellis->db->execute();
  210. return $this->trellis->db->get_affected_rows();
  211. }
  212. #=======================================
  213. # @ Import
  214. #=======================================
  215. public function import( $file, $options )
  216. {
  217. if ( ! $xml = @simplexml_load_file( $file ) ) return false;
  218. if ( $xml->getName() != 'skin' ) return false;
  219. if ( ! ( $version = $this->trellis->sanitize_data( base64_decode( $xml['version'] ) ) ) ) return false;
  220. if ( ! ( $exported = $this->trellis->sanitize_data( base64_decode( $xml['exported'] ) ) ) ) return false;
  221. #if ( $version != $this->trellis->version_number ) return false; // TODO: check min version requirement
  222. if ( ! $info = base64_decode( next($xml) ) ) return false;
  223. if ( ! $infoxml = @simplexml_load_string( $info ) ) return false;
  224. $info = array();
  225. foreach ( $infoxml as $ik => $iv ) {
  226. $info[ (string)$ik ] = (string)( $this->trellis->sanitize_data( $iv[0] ) );
  227. }
  228. if ( ! $info['name'] ) return false;
  229. if ( ! ( $id = $this->add( array( 'name' => $info['name'] ) ) ) ) return false;
  230. if ( ! @mkdir( ( $dir = TD_SKIN .'s'. $id ), 0755, true ) ) return false;
  231. if ( ! is_dir( $dir ) || ! is_writable( $dir ) ) return false;
  232. if ( ! @file_put_contents( $dir .'/info.xml', $infoxml->saveXML() ) ) return false;
  233. foreach ( $xml as $f )
  234. {
  235. $func = null;
  236. $folder = null;
  237. if ( $f->getName() == 'templates' )
  238. {
  239. $func = '_import_template';
  240. }
  241. elseif ( $f->getName() == 'css' && $options['css'] )
  242. {
  243. $func = '_import_css';
  244. }
  245. elseif ( $f->getName() == 'images' && $options['images'] )
  246. {
  247. $func = '_import_image';
  248. }
  249. elseif ( $f->getName() == 'scripts' && $options['scripts'] )
  250. {
  251. $func = '_import_script';
  252. }
  253. elseif ( $f->getName() == 'other' && $options['other'] )
  254. {
  255. $func = '_import_other';
  256. }
  257. if ( $f['folder'] ) {
  258. $folder = base64_decode( (string)$f['folder'] );
  259. }
  260. if ( ! $func ) continue;
  261. foreach ( $f as $c ) {
  262. $this->$func($c, $dir, $folder);
  263. }
  264. }
  265. // TODO: create index.html files?
  266. return array( 'id' => $id, 'name' => $info['name'] );
  267. }
  268. #=======================================
  269. # @ Import Template
  270. #=======================================
  271. private function _import_template(&$c, $dir, $folder)
  272. {
  273. $dir .= '/templates';
  274. if ( $folder ) $dir .= '/'. $folder;
  275. if ( ! is_dir( $dir ) ) {
  276. if ( ! @mkdir( $dir, 0755 ) ) return false;
  277. }
  278. if ( ! is_writable( $dir ) ) return false;
  279. if ( ! ( $file = base64_decode( (string)$c['name'] ) ) ) return false; // TODO: security - check filename to make sure it is safe
  280. if ( ! ( $content = base64_decode( (string)$c->content ) ) ) return false;
  281. return @file_put_contents( $dir .'/'. $file .'.tpl', $content );
  282. }
  283. #=======================================
  284. # @ Import CSS
  285. #=======================================
  286. private function _import_css(&$c, $dir, $folder)
  287. {
  288. $dir .= '/css';
  289. if ( $folder ) $dir .= '/'. $folder;
  290. if ( ! is_dir( $dir ) ) {
  291. if ( ! @mkdir( $dir, 0755, true ) ) return false;
  292. }
  293. if ( ! is_writable( $dir ) ) return false;
  294. if ( ! ( $file = base64_decode( (string)$c['name'] ) ) ) return false; // TODO: security - check filename to make sure it is safe
  295. if ( ! ( $content = base64_decode( (string)$c->content ) ) ) return false;
  296. return @file_put_contents( $dir .'/'. $file .'.css', $content );
  297. }
  298. #=======================================
  299. # @ Import Image
  300. #=======================================
  301. private function _import_image(&$c, $dir, $folder)
  302. {
  303. $dir .= '/images';
  304. if ( $folder ) $dir .= '/'. $folder;
  305. if ( ! is_dir( $dir ) ) {
  306. if ( ! @mkdir( $dir, 0755, true ) ) return false;
  307. }
  308. if ( ! is_writable( $dir ) ) return false;
  309. if ( ! ( $file = base64_decode( (string)$c['name'] ) ) ) return false; // TODO: security - check filename to make sure it is safe
  310. if ( ! ( $content = base64_decode( (string)$c->content ) ) ) return false;
  311. return @file_put_contents( $dir .'/'. $file, $content );
  312. }
  313. #=======================================
  314. # @ Import Script
  315. #=======================================
  316. private function _import_script(&$c, $dir, $folder)
  317. {
  318. $dir .= '/scripts';
  319. if ( $folder ) $dir .= '/'. $folder;
  320. if ( ! is_dir( $dir ) ) {
  321. if ( ! @mkdir( $dir, 0755, true ) ) return false;
  322. }
  323. if ( ! is_writable( $dir ) ) return false;
  324. if ( ! ( $file = base64_decode( (string)$c['name'] ) ) ) return false; // TODO: security - check filename to make sure it is safe
  325. if ( ! ( $content = base64_decode( (string)$c->content ) ) ) return false;
  326. return @file_put_contents( $dir .'/'. $file .'.js', $content );
  327. }
  328. #=======================================
  329. # @ Import Other
  330. #=======================================
  331. private function _import_other(&$c, $dir, $folder)
  332. {
  333. $dir .= '/assets';
  334. if ( $folder ) $dir .= '/'. $folder;
  335. if ( ! is_dir( $dir ) ) {
  336. if ( ! @mkdir( $dir, 0755, true ) ) return false;
  337. }
  338. if ( ! is_writable( $dir ) ) return false;
  339. if ( ! ( $file = base64_decode( (string)$c['name'] ) ) ) return false; // TODO: security - check filename to make sure it is safe
  340. if ( ! ( $content = base64_decode( (string)$c->content ) ) ) return false;
  341. return @file_put_contents( $dir .'/'. $file, $content );
  342. }
  343. #=======================================
  344. # @ Export
  345. #=======================================
  346. public function export( $id, $options )
  347. {
  348. if ( ! $s = $this->trellis->func->skins->get_single_by_id( array( 'id', 'name' ), $id ) ) return false;
  349. if ( ! $options['name'] ) $options['name'] = $s['name'];
  350. $base_dir = TD_SKIN .'s'. $id .'/';
  351. $infodoc = new DOMDocument();
  352. $infodoc->formatOutput = true;
  353. $skin = $infodoc->createElement( 'skin' );
  354. $infodoc->appendChild( $skin );
  355. $name = $infodoc->createElement( 'name' );
  356. $skin->appendChild( $name );
  357. $name->appendChild( $infodoc->createTextNode( $options['name'] ) );
  358. $description = $infodoc->createElement( 'description' );
  359. $skin->appendChild( $description );
  360. $description->appendChild( $infodoc->createTextNode( $options['description'] ) );
  361. $author = $infodoc->createElement( 'author' );
  362. $skin->appendChild( $author );
  363. $author->appendChild( $infodoc->createTextNode( $options['author'] ) );
  364. $copyright = $infodoc->createElement( 'copyright' );
  365. $skin->appendChild( $copyright );
  366. $copyright->appendChild( $infodoc->createTextNode( $options['copyright'] ) );
  367. $version = $infodoc->createElement( 'version' );
  368. $skin->appendChild( $version );
  369. $version->appendChild( $infodoc->createTextNode( $options['version'] ) );
  370. $version_human = $infodoc->createElement( 'version_human' );
  371. $skin->appendChild( $version_human );
  372. $version_human->appendChild( $infodoc->createTextNode( $options['version_human'] ) );
  373. $td_version_min = $infodoc->createElement( 'td_version_min' );
  374. $skin->appendChild( $td_version_min );
  375. $td_version_min->appendChild( $infodoc->createTextNode( $options['td_version_min'] ) );
  376. $td_version_min_human = $infodoc->createElement( 'td_version_min_human' );
  377. $skin->appendChild( $td_version_min_human );
  378. $td_version_min_human->appendChild( $infodoc->createTextNode( $options['td_version_min_human'] ) );
  379. $infoxml = $infodoc->saveXML();
  380. $doc = new DOMDocument();
  381. $doc->formatOutput = true;
  382. $pack = $doc->createElement( 'skin' );
  383. $doc->appendChild( $pack );
  384. $version = $doc->createAttribute( 'version' );
  385. $pack->appendChild( $version );
  386. $version->appendChild( $doc->createTextNode( base64_encode( $this->trellis->version_number ) ) );
  387. $exported = $doc->createAttribute( 'exported' );
  388. $pack->appendChild( $exported );
  389. $exported->appendChild( $doc->createTextNode( base64_encode( time() ) ) );
  390. $info = $doc->createElement( 'info' );
  391. $pack->appendChild( $info );
  392. $info->appendChild( $doc->createTextNode( base64_encode( $infoxml ) ) );
  393. $dir = $base_dir .'templates';
  394. if ( ! $this->_export_templates($doc, $pack, $dir, $dir) ) return false;
  395. if ( $options['css'] )
  396. {
  397. $dir = $base_dir .'css';
  398. $this->_export_css($doc, $pack, $dir, $dir);
  399. }
  400. if ( $options['images'] )
  401. {
  402. $dir = $base_dir .'images';
  403. $this->_export_images($doc, $pack, $dir, $dir);
  404. }
  405. if ( $options['scripts'] )
  406. {
  407. $dir = $base_dir .'scripts';
  408. $this->_export_scripts($doc, $pack, $dir, $dir);
  409. }
  410. if ( $options['other'] )
  411. {
  412. $dir = $base_dir .'assets';
  413. $this->_export_other($doc, $pack, $dir, $dir);
  414. }
  415. header( 'Content-type: text/xml' );
  416. header( 'Content-Disposition: attachment; filename="td_skin.xml"' );
  417. print $doc->saveXML();
  418. $this->trellis->shut_down();
  419. exit();
  420. }
  421. #=======================================
  422. # @ Export Templates
  423. #=======================================
  424. private function _export_templates(&$doc, &$pack, $dir, $base)
  425. {
  426. $files = array();
  427. if ( ! $handle = opendir( $dir ) ) return false;
  428. while ( false !== ( $file = readdir( $handle ) ) )
  429. {
  430. if ( $file != '.' && $file != '..' && is_dir( $dir .'/'. $file ) )
  431. {
  432. #$this->_export_templates($doc, $pack, $dir .'/'. $file, $base);
  433. }
  434. elseif ( is_file( $dir .'/'. $file ) && is_readable( $dir .'/'. $file ) && ( strrchr( $file, "." ) == '.tpl' ) )
  435. {
  436. $files[] = $file;
  437. }
  438. }
  439. if ( empty( $files ) ) return false;
  440. $templates = $doc->createElement( 'templates' );
  441. $pack->appendChild( $templates );
  442. $folder = preg_replace( '/^'. str_replace( '/', '\\/', $base ) .'/', '', $dir );
  443. if ( $folder )
  444. {
  445. $path = $doc->createAttribute( 'folder' );
  446. $templates->appendChild( $path );
  447. $path->appendChild( $doc->createTextNode( base64_encode( trim( $folder, '/' ) ) ) );
  448. }
  449. foreach( $files as &$f )
  450. {
  451. $file = $doc->createElement( 'file' );
  452. $templates->appendChild( $file );
  453. $name = $doc->createAttribute( 'name' );
  454. $file->appendChild( $name );
  455. $name->appendChild( $doc->createTextNode( base64_encode( basename( $f, '.tpl' ) ) ) );
  456. $content = $doc->createElement( 'content' );
  457. $file->appendChild( $content );
  458. $content->appendChild( $doc->createTextNode( base64_encode( @file_get_contents( $dir .'/'. $f ) ) ) );
  459. }
  460. return true;
  461. }
  462. #=======================================
  463. # @ Export CSS
  464. #=======================================
  465. private function _export_css(&$doc, &$pack, $dir, $base)
  466. {
  467. if ( ! is_dir( $dir ) ) return false;
  468. $files = array();
  469. if ( ! $handle = opendir( $dir ) ) return false;
  470. while ( false !== ( $file = readdir( $handle ) ) )
  471. {
  472. if ( $file != '.' && $file != '..' && is_dir( $dir .'/'. $file ) )
  473. {
  474. $this->_export_css($doc, $pack, $dir .'/'. $file, $base);
  475. }
  476. elseif ( is_file( $dir .'/'. $file ) && is_readable( $dir .'/'. $file ) && ( strrchr( $file, "." ) == '.css' ) )
  477. {
  478. $files[] = $file;
  479. }
  480. }
  481. if ( empty( $files ) ) return false;
  482. $css = $doc->createElement( 'css' );
  483. $pack->appendChild( $css );
  484. $folder = preg_replace( '/^'. str_replace( '/', '\\/', $base ) .'/', '', $dir );
  485. if ( $folder )
  486. {
  487. $path = $doc->createAttribute( 'folder' );
  488. $css->appendChild( $path );
  489. $path->appendChild( $doc->createTextNode( base64_encode( trim( $folder, '/' ) ) ) );
  490. }
  491. foreach( $files as &$f )
  492. {
  493. $file = $doc->createElement( 'file' );
  494. $css->appendChild( $file );
  495. $name = $doc->createAttribute( 'name' );
  496. $file->appendChild( $name );
  497. $name->appendChild( $doc->createTextNode( base64_encode( basename( $f, '.css' ) ) ) );
  498. $content = $doc->createElement( 'content' );
  499. $file->appendChild( $content );
  500. $content->appendChild( $doc->createTextNode( base64_encode( @file_get_contents( $dir .'/'. $f ) ) ) );
  501. }
  502. }
  503. #=======================================
  504. # @ Export Images
  505. #=======================================
  506. private function _export_images(&$doc, &$pack, $dir, $base)
  507. {
  508. if ( ! is_dir( $dir ) ) return false;
  509. $files = array();
  510. $img_exts = array( '.bmp', '.gif', '.jpg', '.jpeg', '.png', '.psd', '.tiff' );
  511. if ( ! $handle = opendir( $dir ) ) return false;
  512. while ( false !== ( $file = readdir( $handle ) ) )
  513. {
  514. if ( $file != '.' && $file != '..' && is_dir( $dir .'/'. $file ) )
  515. {
  516. $this->_export_images($doc, $pack, $dir .'/'. $file, $base);
  517. }
  518. elseif ( is_file( $dir .'/'. $file ) && is_readable( $dir .'/'. $file ) && ( in_array( strrchr( $file, "." ), $img_exts ) ) )
  519. {
  520. $files[] = $file;
  521. }
  522. }
  523. if ( empty( $files ) ) return false;
  524. $images = $doc->createElement( 'images' );
  525. $pack->appendChild( $images );
  526. $folder = preg_replace( '/^'. str_replace( '/', '\\/', $base ) .'/', '', $dir );
  527. if ( $folder )
  528. {
  529. $path = $doc->createAttribute( 'folder' );
  530. $images->appendChild( $path );
  531. $path->appendChild( $doc->createTextNode( base64_encode( trim( $folder, '/' ) ) ) );
  532. }
  533. foreach( $files as &$f )
  534. {
  535. $file = $doc->createElement( 'file' );
  536. $images->appendChild( $file );
  537. $name = $doc->createAttribute( 'name' );
  538. $file->appendChild( $name );
  539. $name->appendChild( $doc->createTextNode( base64_encode( basename( $f ) ) ) );
  540. $content = $doc->createElement( 'content' );
  541. $file->appendChild( $content );
  542. $content->appendChild( $doc->createTextNode( base64_encode( @file_get_contents( $dir .'/'. $f ) ) ) );
  543. }
  544. return true;
  545. }
  546. #=======================================
  547. # @ Export Scripts
  548. #=======================================
  549. private function _export_scripts(&$doc, &$pack, $dir, $base)
  550. {
  551. if ( ! is_dir( $dir ) ) return false;
  552. $files = array();
  553. if ( ! $handle = opendir( $dir ) ) return false;
  554. while ( false !== ( $file = readdir( $handle ) ) )
  555. {
  556. if ( $file != '.' && $file != '..' && is_dir( $dir .'/'. $file ) )
  557. {
  558. $this->_export_scripts($doc, $pack, $dir .'/'. $file, $base);
  559. }
  560. elseif ( is_file( $dir .'/'. $file ) && is_readable( $dir .'/'. $file ) && ( strrchr( $file, "." ) == '.js' ) )
  561. {
  562. $files[] = $file;
  563. }
  564. }
  565. if ( empty( $files ) ) return false;
  566. $scripts = $doc->createElement( 'scripts' );
  567. $pack->appendChild( $scripts );
  568. $folder = preg_replace( '/^'. str_replace( '/', '\\/', $base ) .'/', '', $dir );
  569. if ( $folder )
  570. {
  571. $path = $doc->createAttribute( 'folder' );
  572. $scripts->appendChild( $path );
  573. $path->appendChild( $doc->createTextNode( base64_encode( trim( $folder, '/' ) ) ) );
  574. }
  575. foreach( $files as &$f )
  576. {
  577. $file = $doc->createElement( 'file' );
  578. $scripts->appendChild( $file );
  579. $name = $doc->createAttribute( 'name' );
  580. $file->appendChild( $name );
  581. $name->appendChild( $doc->createTextNode( base64_encode( basename( $f, '.js' ) ) ) );
  582. $content = $doc->createElement( 'content' );
  583. $file->appendChild( $content );
  584. $content->appendChild( $doc->createTextNode( base64_encode( @file_get_contents( $dir .'/'. $f ) ) ) );
  585. }
  586. return true;
  587. }
  588. #=======================================
  589. # @ Export Other
  590. #=======================================
  591. # TODO: repating code... we can make the common code into another function
  592. private function _export_other(&$doc, &$pack, $dir, $base)
  593. {
  594. if ( ! is_dir( $dir ) ) return false;
  595. $files = array();
  596. if ( ! $handle = opendir( $dir ) ) return false;
  597. while ( false !== ( $file = readdir( $handle ) ) )
  598. {
  599. if ( $file != '.' && $file != '..' && is_dir( $dir .'/'. $file ) )
  600. {
  601. if ( strpos( $file, $base))
  602. $this->_export_other($doc, $pack, $dir .'/'. $file, $base);
  603. }
  604. elseif ( is_file( $dir .'/'. $file ) && is_readable( $dir .'/'. $file ) )
  605. {
  606. $files[] = $file;
  607. }
  608. }
  609. if ( empty( $files ) ) return false;
  610. $other = $doc->createElement( 'other' );
  611. $pack->appendChild( $other );
  612. $folder = preg_replace( '/^'. str_replace( '/', '\\/', $base ) .'/', '', $dir );
  613. if ( $folder )
  614. {
  615. $path = $doc->createAttribute( 'folder' );
  616. $other->appendChild( $path );
  617. $path->appendChild( $doc->createTextNode( base64_encode( trim( $folder, '/' ) ) ) );
  618. }
  619. foreach( $files as &$f )
  620. {
  621. $file = $doc->createElement( 'file' );
  622. $other->appendChild( $file );
  623. $name = $doc->createAttribute( 'name' );
  624. $file->appendChild( $name );
  625. $name->appendChild( $doc->createTextNode( base64_encode( basename( $f ) ) ) );
  626. $content = $doc->createElement( 'content' );
  627. $file->appendChild( $content );
  628. $content->appendChild( $doc->createTextNode( base64_encode( @file_get_contents( $dir .'/'. $f ) ) ) );
  629. }
  630. return true;
  631. }
  632. #=======================================
  633. # @ Files
  634. #=======================================
  635. public function files($id)
  636. {
  637. $dir = TD_SKIN .'s'. $id .'/templates/';
  638. if ( ! is_readable( $dir ) || ! is_dir( $dir ) ) return false;
  639. $files = array();
  640. if ( ! $handle = opendir( $dir ) ) return false;
  641. while ( false !== ( $file = readdir( $handle ) ) )
  642. {
  643. if ( is_file( $dir .'/'. $file ) && ( strrchr( $file, "." ) == '.tpl' ) && $file != "wrapper.tpl" )
  644. {
  645. $files[] = $file;
  646. }
  647. }
  648. if ( empty( $files ) ) return false;
  649. return $files;
  650. }
  651. #=======================================
  652. # @ Files CSS
  653. #=======================================
  654. public function files_css($id)
  655. {
  656. $dir = TD_SKIN .'s'. $id .'/css/';
  657. if ( ! is_readable( $dir ) || ! is_dir( $dir ) ) return false;
  658. $files = array();
  659. if ( ! $handle = opendir( $dir ) ) return false;
  660. while ( false !== ( $file = readdir( $handle ) ) )
  661. {
  662. if ( is_file( $dir .'/'. $file ) && ( strrchr( $file, "." ) == '.css' ) )
  663. {
  664. $files[] = $file;
  665. }
  666. }
  667. if ( empty( $files ) ) return false;
  668. return $files;
  669. }
  670. #=======================================
  671. # @ Writeable
  672. #=======================================
  673. public function writeable($id, $file)
  674. {
  675. $file = TD_SKIN .'s'. $id .'/templates/'. $file;
  676. return is_writable( $file );
  677. }
  678. #=======================================
  679. # @ Writeable CSS
  680. #=======================================
  681. public function writeable_css($id, $file)
  682. {
  683. $file = TD_SKIN .'s'. $id .'/css/'. $file;
  684. return is_writable( $file );
  685. }
  686. #=======================================
  687. # @ Writeable Wrapper
  688. #=======================================
  689. public function writeable_wrapper($id)
  690. {
  691. $file = TD_SKIN .'s'. $id .'/templates/wrapper.tpl';
  692. return is_writable( $file );
  693. }
  694. #=======================================
  695. # @ Prepare Html
  696. #=======================================
  697. public function prepare_html( $data )
  698. {
  699. $data = str_replace( '&', '&amp;', $data );
  700. $data = str_replace( '\'', '&#039;', $data );
  701. $data = str_replace( '\'', '&#39;', $data );
  702. $data = str_replace( '"', '&quot;', $data );
  703. $data = str_replace( '<', '&lt;', $data );
  704. $data = str_replace( '>', '&gt;', $data );
  705. $data = str_replace( '(', '&#40;', $data );
  706. $data = str_replace( ')', '&#41;', $data );
  707. return $data;
  708. }
  709. #=======================================
  710. # @ Convert Html
  711. #=======================================
  712. public function convert_html( $data )
  713. {
  714. $data = str_replace( '&amp;', '&', $data );
  715. $data = str_replace( '&#039;', '\'', $data );
  716. $data = str_replace( '&#39;', '\'', $data );
  717. $data = str_replace( '&quot;', '"', $data );
  718. $data = str_replace( '&lt;', '<', $data );
  719. $data = str_replace( '&gt;', '>', $data );
  720. $data = str_replace( '&#40;', '(', $data );
  721. $data = str_replace( '&#41;', ')', $data );
  722. return $data;
  723. }
  724. #=======================================
  725. # @ Check Key
  726. #=======================================
  727. public function check_key( $key )
  728. {
  729. return preg_match( '/^[a-z0-9]*$/', $key ) ;
  730. }
  731. }
  732. ?>