PageRenderTime 48ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 1ms

/mambots/editors/jce/jscripts/tiny_mce/libraries/classes/jce.class.php

https://bitbucket.org/dgough/annamaria-daneswood-25102012
PHP | 776 lines | 662 code | 36 blank | 78 comment | 78 complexity | e16c54023f1ffb15e762ef14cd7de719 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. defined( '_VALID_MOS' ) or die( 'Restricted Access.' );
  3. class JCE{
  4. var $id;
  5. var $usertype;
  6. var $username;
  7. var $plugin;
  8. var $tiny_url;
  9. var $tiny_path;
  10. var $lib_url;
  11. var $lib_path;
  12. var $plugin_path;
  13. var $plugin_url;
  14. var $language;
  15. var $request;
  16. //Constructor
  17. function JCE(){
  18. global $my, $database, $mainframe;
  19. $this->usertype = strtolower( $my->usertype );
  20. $this->username = $my->username;
  21. if( !$my->id ){
  22. $gid = 0;
  23. }else{
  24. $query = "SELECT gid"
  25. . "\n FROM #__users"
  26. . "\n WHERE id = '".$my->id."' LIMIT 1";
  27. ;
  28. $database->setQuery( $query );
  29. $gid = $database->loadResult();
  30. }
  31. $this->id = intval( $gid );
  32. }
  33. function getUserName(){
  34. return $this->username;
  35. }
  36. function getUserType(){
  37. return $this->usertype;
  38. }
  39. function isAdmin(){
  40. return ( $this->usertype == 'superadministrator' || $this->usertype == 'super administrator' || $this->id == 25 ) ? true : false;
  41. }
  42. function authCheck( $lvl ){
  43. return ( $this->isAdmin() || ( intval( $lvl ) != 99 && $this->id >= intval( $lvl ) ) ) ? true : false;
  44. }
  45. //Return JCE mambot paramters
  46. function getParams(){
  47. global $database;
  48. $query = "SELECT id"
  49. . "\n FROM #__mambots"
  50. . "\n WHERE element = 'jce'"
  51. . "\n AND folder = 'editors'"
  52. ;
  53. $database->setQuery( $query );
  54. $id = $database->loadResult();
  55. $mambot = new mosMambot( $database );
  56. $mambot->load( $id );
  57. $params = new mosParameters( $mambot->params );
  58. return $params;
  59. }
  60. //Return a list of published plugins
  61. function getPlugins( $exclude ){
  62. global $database;
  63. $query = "SELECT plugin"
  64. . "\n FROM #__jce_plugins"
  65. . "\n WHERE access <= '".$this->id."' AND published = 1 AND type = 'plugin'"
  66. ;
  67. $database->setQuery( $query );
  68. $plugins = $database->loadResultArray();
  69. if( $exclude ){
  70. foreach( $exclude as $excluded ){
  71. if( in_array( $excluded, $plugins ) && in_array( $excluded . '_ext', $plugins )){
  72. unset( $plugins[array_search( $excluded, $plugins )] );
  73. }
  74. }
  75. }
  76. return implode( ',', $plugins );
  77. }
  78. //Return a list of installed languages
  79. function getLangs(){
  80. global $database;
  81. $query = "SELECT lang"
  82. . "\n FROM #__jce_langs"
  83. ;
  84. $database->setQuery( $query );
  85. return implode( ',', $database->loadResultArray() );
  86. }
  87. //Return a the published language
  88. function getLanguage(){
  89. global $database;
  90. $query = "SELECT lang"
  91. . "\n FROM #__jce_langs"
  92. . "\n WHERE published = 1"
  93. ;
  94. $database->setQuery( $query );
  95. $this->language = $database->loadResult();
  96. return $this->language;
  97. }
  98. function getPluginLanguage(){
  99. $l = ( $this->language ) ? $this->language : $this->getLanguage();
  100. if( file_exists( $this->getPluginPath() . "/langs/" . $l . ".php" ) ){
  101. return $l;
  102. }else{
  103. return "en";
  104. }
  105. }
  106. function removeKey( $array, $key ){
  107. if( in_array( $key, $array ) ){
  108. unset( $array[$key] );
  109. }
  110. }
  111. function addKey( $string, $key, $separator ){
  112. if( $string ){
  113. $array = explode( $separator, $string );
  114. $array[] = $key;
  115. return implode( $separator, $array );
  116. }else{
  117. return $key;
  118. }
  119. }
  120. function getBool( $string ){
  121. return intval( $string ) ? 'true' : 'false';
  122. }
  123. function cleanParam( $param ){
  124. $search = array ('@<[\/\!]*?[^<>]*?>@si','@/\n|\r|(\r\n)/m[\s]+@','@([\r\n])[\s]+@');
  125. $replace = array ('','','');
  126. return preg_replace($search, $replace, $param);
  127. }
  128. //Return a string of commands to be removed
  129. function getRemovePlugins(){
  130. global $database;
  131. $query = "SELECT plugin"
  132. . "\n FROM #__jce_plugins"
  133. . "\n WHERE type = 'command'"
  134. . "\n AND published = 0"
  135. // . "\n OR access > '" . $this->id . "'"
  136. ;
  137. $database->setQuery( $query );
  138. $remove = $database->loadResultArray();
  139. if( $remove ){
  140. return implode( ',', $remove );
  141. }else{
  142. return '';
  143. }
  144. }
  145. //Return a an array of buttons for a specified row
  146. function getRow( $row ){
  147. global $database;
  148. $query = "SELECT icon"
  149. . "\n FROM #__jce_plugins"
  150. . "\n WHERE access <= '".$this->id."'"
  151. . "\n AND published = 1"
  152. . "\n AND row = " . $row . ""
  153. . "\n AND icon != ''"
  154. . "\n ORDER BY ordering ASC"
  155. ;
  156. $database->setQuery( $query );
  157. return implode( ',', $database->loadResultArray() );
  158. }
  159. //Return a string of extended elements for a plugin
  160. function getElements(){
  161. global $database;
  162. $params = $this->getParams();
  163. $jce_elements = explode( ',', $this->cleanParam( $params->get( 'extended_elements', '' ) ) );
  164. $query = "SELECT elements"
  165. . "\n FROM #__jce_plugins"
  166. . "\n WHERE elements != ''"
  167. . "\n AND published = 1"
  168. . "\n AND access <= '".$this->id."'"
  169. ;
  170. $database->setQuery( $query );
  171. $plugin_elements = $database->loadResultArray();
  172. $elements = array_merge( $jce_elements, $plugin_elements );
  173. return implode( ',', $elements );
  174. }
  175. function getPluginParams( $plugin='' ){
  176. global $database;
  177. if( !$plugin ) $plugin = $this->plugin;
  178. require_once( $this->getParamsPath() . '/plugins.class.php' );
  179. $plugins = new jcePlugins( $database );
  180. $query = "SELECT id FROM #__jce_plugins WHERE plugin = '" . $plugin . "' AND published = 1 LIMIT 1";
  181. $database->setQuery( $query );
  182. $id = $database->loadResult();
  183. $plugins->load( $id );
  184. $params = new mosParameters( $plugins->params );
  185. return $params;
  186. }
  187. //Boolean - is a plugin loaded?
  188. function isLoaded( $plugin ){
  189. global $database;
  190. $query = "SELECT id"
  191. . "\n FROM #__jce_plugins"
  192. . "\n WHERE plugin = '" . $plugin . "'"
  193. . "\n AND published = 1 LIMIT 1"
  194. ;
  195. $database->setQuery( $query );
  196. $id = $database->loadResult();
  197. if( $id ){
  198. return true;
  199. }else{
  200. return false;
  201. }
  202. }
  203. //Set plugin as current
  204. function setPlugin( $plugin ){
  205. $this->plugin = $plugin;
  206. }
  207. //Return current plugin
  208. function getPlugin(){
  209. return $this->plugin;
  210. }
  211. function getAuthOption( $key, $def, $type='bool' ){
  212. $params = $this->getPluginParams();
  213. if( $type == 'int' ){
  214. return $this->authCheck( $params->get( $key, $def ) ) === true ? 1 : 0;
  215. }
  216. return $this->authCheck( $params->get( $key, $def ) );
  217. }
  218. function getUserPath(){
  219. $params = $this->getPluginParams();
  220. $t = $params->get( 'dir_type', 'level' );
  221. //Default
  222. $path = JFile::makeSafe( $this->usertype );
  223. switch( $t )
  224. {
  225. case 'level' :
  226. $path = JFile::makeSafe( $this->usertype );
  227. break;
  228. case 'name' :
  229. $path = JFile::makeSafe( $this->username );
  230. break;
  231. case 'level_name' :
  232. $path = JPath::makePath( JFile::makeSafe( $this->usertype ), JFile::makeSafe( $this->username ) );
  233. break;
  234. case 'folder' :
  235. $path = $params->get( 'user_folder', '' );
  236. break;
  237. }
  238. return $path;
  239. }
  240. function getUserDir( $base_dir ){
  241. global $mainframe;
  242. $path = $this->getUserPath();
  243. $folder = JPath::makePath( $base_dir, $path );
  244. $full = JPath::makePath( $mainframe->getCfg( 'absolute_path' ), $folder );
  245. if( !JFolder::exists( $full ) ){
  246. JFolder::createFolder( $full );
  247. }
  248. return $folder;
  249. }
  250. function getBaseDir( $create ){
  251. $params = $this->getPluginParams();
  252. $base_dir = $params->get( 'dir', '/images/stories' );
  253. if( $params->get( 'user_dir', '0' ) && !$this->authCheck( intval( $params->get( 'user_dir_level', '18' ) ) ) ){
  254. if( $create ){
  255. $base_dir = $this->getUserDir( $base_dir );
  256. }else{
  257. $base_dir = JPath::makePath( $base_dir, $this->getUserPath() );
  258. }
  259. }
  260. return $base_dir;
  261. }
  262. /**
  263. * Borrowed from libraries/joomla/environment/uri.php in Joomla! 1.5.7
  264. * @copyright Copyright (C) 2005 - 2008 Open Source Matters. All rights reserved.
  265. * @license GNU/GPL, see LICENSE.php
  266. * Joomla! is free software. This version may have been modified pursuant
  267. * to the GNU General Public License, and as distributed it includes or
  268. * is derivative of works licensed under the GNU General Public License or
  269. * other free or open source software licenses.
  270. */
  271. function getURI( $root = true ){
  272. if (strpos(php_sapi_name(), 'cgi') !== false && !empty($_SERVER['REQUEST_URI'])) {
  273. //Apache CGI
  274. $url = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
  275. } else {
  276. //Others
  277. $url = rtrim(dirname($_SERVER['SCRIPT_NAME']), '/\\');
  278. }
  279. if( $root ){
  280. return str_replace( '/administrator', '', $url );
  281. }
  282. return $url;
  283. }
  284. function getToken(){
  285. global $database, $mainframe, $my;
  286. $plugin = mosGetParam( $_REQUEST, 'plugin' );
  287. $query = 'SELECT session_id FROM #__session WHERE userid = ' . (int) $my->id;
  288. $database->setQuery( $query );
  289. return 'j' . md5( $mainframe->getCfg('secret') . $plugin . $database->loadResult() );
  290. }
  291. function checkToken(){
  292. $token = mosGetParam( $_REQUEST, 'token' );
  293. return $this->getToken() == $token;
  294. }
  295. function getTinyUrl(){
  296. global $mainframe;
  297. if( !$this->tiny_url ){
  298. $this->tiny_url = $this->getURI() . "/mambots/editors/jce/jscripts/tiny_mce";
  299. }
  300. return $this->tiny_url;
  301. }
  302. function getTinyPath(){
  303. global $mainframe;
  304. $this->tiny_path = $mainframe->getCfg('absolute_path') . "/mambots/editors/jce/jscripts/tiny_mce";
  305. return $this->tiny_path;
  306. }
  307. function getLibUrl(){
  308. $this->lib_url = $this->getTinyUrl() . "/libraries";
  309. return $this->lib_url;
  310. }
  311. function getLibPath(){
  312. $this->lib_path = $this->getTinyPath() . "/libraries";
  313. return $this->lib_path;
  314. }
  315. function getPluginUrl(){
  316. $this->plugin_url = $this->getTinyUrl() . "/plugins/" .$this->plugin;
  317. return $this->plugin_url;
  318. }
  319. function getPluginPath(){
  320. $this->plugin_path = $this->getTinyPath() . "/plugins/" .$this->plugin;
  321. return $this->plugin_path;
  322. }
  323. function getPluginFile( $file ){
  324. return 'index2.php?option=com_jce&no_html=1&task=plugin&plugin=' . $this->plugin . '&token='. $this->getToken() .'&file=' . $file;
  325. }
  326. function getParamsPath(){
  327. global $mainframe;
  328. return $mainframe->getCfg('absolute_path') . '/administrator/components/com_jce/plugins';
  329. }
  330. function printTinyJs( $file ){
  331. $url = $this->getTinyUrl() . "/" . $file . ".js";
  332. echo "<script language=\"javascript\" type=\"text/javascript\" src=\"" . $url . "\"></script>\n";
  333. }
  334. function printPluginJs( $file ){
  335. $url = $this->getPluginUrl() . "/jscripts/" . $file . ".js";
  336. echo "<script language=\"javascript\" type=\"text/javascript\" src=\"" . $url . "\"></script>\n";
  337. }
  338. function printLibJs( $file ){
  339. $url = $this->getLibUrl() . "/jscripts/" . $file . ".js";
  340. echo "<script language=\"javascript\" type=\"text/javascript\" src=\"" . $url . "\"></script>\n";
  341. }
  342. function printPluginCss( $file, $ie=false ){
  343. $url = $this->getPluginUrl() . "/css/" . $file . ".css";
  344. echo "<link href=\"" . $url . "\" rel=\"stylesheet\" type=\"text/css\" />\n";
  345. if( $ie ){
  346. echo "<!--[if IE 6]><link href=\"" . $this->getPluginUrl() . "/css/" . $file . "_ie6.css\" rel=\"stylesheet\" type=\"text/css\" /><![endif]-->\n";
  347. echo "<!--[if IE 7]><link href=\"" . $this->getPluginUrl() . "/css/" . $file . "_ie7.css\" rel=\"stylesheet\" type=\"text/css\" /><![endif]-->\n";
  348. }
  349. }
  350. function printLibCss( $file, $ie=false ){
  351. $url = $this->getLibUrl() . "/css/" . $file . ".css";
  352. echo "<link href=\"" . $url . "\" rel=\"stylesheet\" type=\"text/css\" />\n";
  353. if( $ie ){
  354. echo "<!--[if IE 6]><link href=\"" . $this->getLibUrl() . "/css/" . $file . "_ie6.css\" rel=\"stylesheet\" type=\"text/css\" /><![endif]-->\n";
  355. echo "<!--[if IE 7]><link href=\"" . $this->getLibUrl() . "/css/" . $file . "_ie7.css\" rel=\"stylesheet\" type=\"text/css\" /><![endif]-->\n";
  356. }
  357. }
  358. function getTinyImg( $image ){
  359. return $this->getTinyUrl() . "/themes/advanced/images/" . $image;
  360. }
  361. function getLibImg( $image ){
  362. return $this->getLibUrl() . "/images/" . $image;
  363. }
  364. function getPluginImg( $image ){
  365. return $this->getPluginUrl() . "/images/" . $image;
  366. }
  367. function getHelpImg( $image ){
  368. return $this->getPluginUrl() . "/docs/" . $this->getHelpLang() . "/images/" . $image;
  369. }
  370. function getFileIcon( $ext ){
  371. if( JFile::exists( $this->getLibPath() . '/images/icons/' . $ext . '.gif' )){
  372. return $this->getLibImg( 'icons/' . $ext . '.gif' );
  373. }elseif( JFile::exists( $this->getPluginPath() . '/images/icons/' . $ext . '.gif' )){
  374. return $this->getPluginImg( 'icons/' . $ext . '.gif' );
  375. }else{
  376. return $this->getLibImg( 'icons/def.gif' );
  377. }
  378. }
  379. function translate( $v, $def='' ){
  380. global $cl, $pl;
  381. if( isset( $cl[$v] ) ){
  382. return $cl[$v];
  383. }
  384. if( isset( $pl[$v] ) ){
  385. return $pl[$v];
  386. }
  387. if( $def ){
  388. return $def;
  389. }else{
  390. return $v;
  391. }
  392. }
  393. function sortType(){
  394. global $cl;
  395. echo "<div id=\"sortTypeDiv\" onselectstart=\"return false\" class=\"sortDesc\">\n";
  396. echo "<div class=\"sortLabel\">" . $cl['type'] . "</div>\n";
  397. echo "</div>\n";
  398. }
  399. function sortName(){
  400. global $cl;
  401. echo "<div id=\"sortNameDiv\" onselectstart=\"return false\" class=\"sortDesc\">\n";
  402. echo "<div class=\"sortLabel\">" . $cl['name'] . "</div>\n";
  403. echo "</div>\n";
  404. }
  405. function searchDiv(){
  406. echo "<div id=\"searchDiv\">";
  407. echo "<div id=\"searchValueLabel\"><img src=\"" . $this->getLibImg('search.gif') . "\" width=\"16\" height=\"16\" alt=\"" . $this->translate('search') . "\" title=\"" . $this->translate('search') . "\" style=\"vertical-align:middle;\" /></div>";
  408. echo "<input type=\"text\" id=\"searchValue\" style=\"width: 170px;\" ONKEYUP=\"searchFile(this.value);\" />";
  409. echo "</div>";
  410. }
  411. function colorPicker( $name, $def, $func='' ){
  412. $html = '<table border="0" cellpadding="0" cellspacing="0">'."\n";
  413. $html .= '<tr>'."\n";
  414. $html .= '<td><input type="text" size="10" id="' . $name . '" name="' . $name . '" value="' . $def . '" onChange="document.getElementById(\'' . $name . '_pick\').style.backgroundColor=this.value;" /></td>'."\n";
  415. $html .= '<td>&nbsp;<img onclick="colorPicker.show(\'' . $name . '\', \'' . $func . '\');" class="colorpicker" style="background-color:' . $def . ';" name="' . $name . '_pick" id="' . $name . '_pick" src="' . $this->getLibImg('color.gif') . '" width="20" height="20" border="0" /></td>'."\n";
  416. $html .= '</tr>'."\n";
  417. $html .='</table>'."\n";
  418. return $html;
  419. }
  420. function editTools(){
  421. global $cl;
  422. $html = '';
  423. if( $this->getAuthOption('file_rename', '18') ){
  424. $html .= '<div class="editIcon" id="renIcon"><a href="javascript:void(0)" id="renLink" class="tools" onClick="renameFile();"><img src="' . $this->getLibImg('rename.gif') . '" alt="' . $cl['rename'] . '" title="' . $cl['rename'] . '" width="20" height="20" /></a></div>'."\n";
  425. }
  426. if( $this->getAuthOption('file_delete', '18') ){
  427. $html .= '<div id="delIcon" class="editIcon"><a href="javascript:void(0)" id="delLink" title="' . $cl['delete'] . '" onClick="deleteFile();" class="tools"><img src="' . $this->getLibImg('delete.gif') . '" height="20" width="20" border="0" alt="' . $cl['delete'] . '" /></a> </div>'."\n";
  428. }
  429. if( $this->getAuthOption('file_move', '18') ){
  430. $html .= '<div id="copyIcon" class="editIcon"><a href="javascript:void(0)" id="copyLink" title="' . $cl['copy'] . '" onClick="copyFile();" class="tools"><img src="' . $this->getLibImg('copy.gif') . '" height="20" width="20" border="0" alt="' . $cl['copy'] . '" /></a></div>'."\n";
  431. $html .= '<div id="cutIcon" class="editIcon"><a href="javascript:void(0)" id="cutLink" title="' . $cl['cut'] . '" onClick="cutFile();" class="tools"><img src="' . $this->getLibImg('cut.gif') . '" height="20" width="20" border="0" alt="' . $cl['cut'] . '" /></a></div>'."\n";
  432. $html .= '<div id="pasteIcon" class="editIcon"><a href="javascript:void(0)" id="pasteLink" title="' . $cl['paste'] . '" onClick="pasteFile();" class="tools"><img src="' . $this->getLibImg('paste.gif') . '" height="20" width="20" border="0" alt="' . $cl['paste'] . '" /></a> </div>'."\n";
  433. }
  434. if( $this->getAuthOption('folder_delete', '18') ){
  435. $html .= '<div id="delDirIcon" class="editIcon"><a href="javascript:void(0)" id="delDirLink" title="' . $cl['delete'] . '" onClick="deleteFolder();" class="tools"><img src="' . $this->getLibImg('delete.gif') . '" height="20" width="20" border="0" alt="' . $cl['delete'] . '" /></a> </div>'."\n";
  436. }
  437. if( $this->getAuthOption('folder_rename', '18') ){
  438. $html .= '<div id="renDirIcon" class="editIcon"><a href="javascript:void(0)" id="renDirLink" title="' . $cl['rename'] . '" onClick="renameFolder();" class="tools"><img src="' . $this->getLibImg('rename.gif') . '" height="20" width="20" border="0" alt="' . $cl['rename'] . '" /></a> </div>'."\n";
  439. }
  440. return $html;
  441. }
  442. //Ajax functions
  443. function setAjax( $function ){
  444. if( is_array( $function ) ){
  445. $this->request[$function[0]] = array( $function[1], $function[2] );
  446. }else{
  447. $this->request[$function] = $function;
  448. }
  449. }
  450. function ajaxhtmlentities( $html ){
  451. global $cl;
  452. if(function_exists( 'jcehtmlentities' )){
  453. return jcehtmlentities( $html );
  454. }else{
  455. return htmlentities( $html, ENT_QUOTES, $cl['iso'] );
  456. }
  457. }
  458. function ajaxHTML( $html ){
  459. global $cl;
  460. $params = $this->getParams();
  461. if( strtolower( $params->get( 'charset', $cl['iso'] ) ) == 'utf-8' ){
  462. return addslashes( $html );
  463. }else{
  464. $html = addslashes( $this->ajaxhtmlentities( $html ) );
  465. return str_replace( array( '&lt;', '&gt;' ), array( '<', '>' ), $html );
  466. //return str_replace( '&', '&#38;', addslashes( $this->ajaxhtmlentities( $html ) ) );
  467. }
  468. }
  469. function processAjax(){
  470. global $cl;
  471. $params = $this->getParams();
  472. header("Content-Type: text/html; charset=". $params->get('charset', $cl['iso']) ."");
  473. $GLOBALS['ajaxErrorHandlerText'] = "";
  474. set_error_handler('ajaxErrorHandler');
  475. $fn = mosGetParam( $_POST, 'ajaxfn' );
  476. $args = mosGetParam( $_POST, 'ajaxargs', '' );
  477. $txt = '';
  478. if( $fn ){
  479. if( array_key_exists( $fn, $this->request ) ){
  480. $txt = call_user_func_array( $this->request[$fn], $args );
  481. if( !empty( $GLOBALS['ajaxErrorHandlerText'] ) ){
  482. $txt = "<script>alert('**PHP Error Messages:**" . $this->ajaxHTML( $GLOBALS['ajaxErrorHandlerText'] ) . "');</script>";
  483. }
  484. }else{
  485. $txt = "<script>alert('Cannot call function ". $this->ajaxHTML( $fn ) .". Function not registered!');</script>";
  486. }
  487. print $txt;
  488. exit();
  489. }
  490. }
  491. }
  492. function ajaxErrorHandler( $errno, $errstr, $errfile, $errline ){
  493. $errorReporting = error_reporting();
  494. if ( ( $errno & $errorReporting ) == 0 ) return;
  495. if ( $errno == E_NOTICE ){
  496. $errTypeStr = "NOTICE";
  497. }else if ( $errno == E_WARNING ){
  498. $errTypeStr = "WARNING";
  499. }else if ( $errno == E_USER_NOTICE ){
  500. $errTypeStr = "USER NOTICE";
  501. }else if ( $errno == E_USER_WARNING ){
  502. $errTypeStr = "USER WARNING";
  503. }else if ( $errno == E_USER_ERROR ){
  504. $errTypeStr = "USER FATAL ERROR";
  505. }else if ( $errno == E_STRICT ){
  506. return;
  507. }else{
  508. $errTypeStr = "UNKNOWN: $errno";
  509. }
  510. $GLOBALS['ajaxErrorHandlerText'] .= "$errTypeStr $errstr Error in line $errline of file $errfile";
  511. }
  512. class Manager{
  513. //Configuration array.
  514. var $base_dir;
  515. var $base_url;
  516. /**
  517. * Get the base directory.
  518. * @return string base dir, see config.inc.php
  519. */
  520. function getBaseDir(){
  521. global $mainframe;
  522. return JPath::makePath( $mainframe->getCfg('absolute_path'), $this->base_dir );
  523. }
  524. /**
  525. * Get the base URL.
  526. * @return string base url, see config.inc.php
  527. */
  528. function getBaseURL(){
  529. global $mainframe;
  530. return JPath::makePath( $mainframe->getCfg('live_site'), $this->base_url );
  531. }
  532. /**
  533. * Get a list of dirs in the base dir
  534. * @return array $dirs
  535. */
  536. function getDirs(){
  537. $list = JFolder::listFolderTree( $this->getBaseDir(), '.' );
  538. $dirs = array();
  539. if( $list ){
  540. foreach( $list as $dir ){
  541. $dir['relname'] = str_replace( "\\", "/", $dir['relname']);
  542. $dirs[] = str_replace( $this->base_dir, '', '/'.$dir['relname'] );
  543. }
  544. }
  545. return $dirs;
  546. }
  547. function getFiles( $relative, $filter ){
  548. $path = JPath::makePath( $this->getBaseDir(), $relative );
  549. $list = JFolder::files( $path, $filter );
  550. if( !empty( $list ) ){
  551. for ($i = 0; $i < count( $list ); $i++) {
  552. $file = $list[$i];
  553. $fullpath = JPath::makePath( $path, $file );
  554. $files[] = array(
  555. 'name' => $file,
  556. 'relative' => JPath::makePath( $relative, $file ),
  557. 'ext' => JFile::getExt( $file ),
  558. 'short_name' => JFile::stripExt( $file )
  559. );
  560. }
  561. return $files;
  562. }else{
  563. return $list;
  564. }
  565. }
  566. function getFolders( $relative ){
  567. $path = JPath::makePath( $this->getBaseDir(), $relative );
  568. $list = JFolder::folders( $path );
  569. if( !empty( $list ) ){
  570. for ($i = 0; $i < count( $list ); $i++) {
  571. $folder = $list[$i];
  572. $fullpath = JPath::makePath( $path, $folder );
  573. $folders[] = array(
  574. 'name' => $folder,
  575. 'relative' => JPath::makePath( $relative, $folder )
  576. );
  577. }
  578. return $folders;
  579. }else{
  580. return $list;
  581. }
  582. }
  583. function dirTree(){
  584. $dirs = JFolder::listFolderTree( $this->getBaseDir(), '.' );
  585. $d = "";
  586. foreach( $dirs as $tree ){
  587. $id = $tree['id'];
  588. $parent = $tree['parent'];
  589. $name = $tree['name'];
  590. $relative = str_replace( "\\", "/", $tree['relname']);
  591. $relative = str_replace( $this->base_dir, '', '/' . $relative );
  592. $did = $id + 1;
  593. $d .= "parent.jce.tree.add('i" . $id . "','i" . $parent . "','" . $name . "', 'javascript:changeDir(\'". $relative ."\')', '". $relative ."');\n";
  594. }
  595. return $d;
  596. }
  597. function doUpload( $dir, $file, $name, $ext, $max_size, $overwrite ){
  598. global $cl;
  599. $error = false;
  600. $allowable = explode( ',', $ext );
  601. $fileExt = JFile::getExt( $file['name'] );
  602. $match = in_array( $fileExt, $allowable );
  603. if( $file['size'] > $max_size )
  604. {
  605. $error = $cl['upload_size_err'];
  606. }elseif( !$match ){
  607. $error = $cl['upload_ext_err'];
  608. }else{
  609. $path = JPath::makePath( $this->getBaseDir(), $dir );
  610. $file_path = JPath::makePath( $path, JFile::makeSafe( $name . '.' . $fileExt ) );
  611. $result = JFile::upload( $file['tmp_name'], $file_path, $overwrite );
  612. if( !JFile::exists( $result ) ){
  613. $error = $result;
  614. }
  615. }
  616. return $error;
  617. }
  618. /**
  619. * Delete the relative file(s).
  620. * @param $files the relative path to the file name or comma seperated list of multiple paths.
  621. * @return string $error on failure.
  622. */
  623. function deleteFiles( $files ){
  624. global $cl;
  625. $error = false;
  626. $files = explode( ",", $files );
  627. foreach( $files as $file ){
  628. $fullpath = JPath::makePath( $this->getBaseDir(), $file );
  629. if( JFile::exists( $fullpath ) ){
  630. if( @!JFile::delete( $fullpath ) ){
  631. $error = $cl['del_file_err'];
  632. }
  633. }
  634. }
  635. }
  636. /**
  637. * Delete a folder
  638. * @param string $relative The relative path of the folder to delete
  639. * @return string $error on failure
  640. */
  641. function deleteFolder( $relative ){
  642. global $cl;
  643. $error = false;
  644. $folder = rawurldecode( $relative );
  645. $folder = JPath::makePath( $this->getBaseDir(), $folder );
  646. if( JFile::countFiles( $folder, '^[(index.html)]' ) != 0 || JFolder::countDirs( $folder ) != 0 ){
  647. $error = $cl['not_empty_err'];
  648. }else{
  649. if( @!JFolder::delete( $folder ) ){
  650. $error = $cl['del_dir_err'];
  651. }
  652. }
  653. return $error;
  654. }
  655. /*
  656. * Rename a file.
  657. * @param string $src The relative path of the source file
  658. * @param string $dest The name of the new file
  659. * @return string $error
  660. */
  661. function renameFile( $src, $dest ){
  662. global $cl;
  663. $error = false;
  664. $src = JPath::makePath( $this->getBaseDir(), $src );
  665. $dir = dirname( $src );
  666. $ext = JFile::getExt( $src );
  667. $dest = JPath::makePath( $dir, $dest.'.'.$ext );
  668. $error = JFile::rename( $src, $dest );
  669. return $error;
  670. }
  671. /*
  672. * Rename a file.
  673. * @param string $src The relative path of the source file
  674. * @param string $dest The name of the new file
  675. * @return string $error
  676. */
  677. function renameDir( $src, $dest ){
  678. global $cl;
  679. $error = false;
  680. $src = JPath::makePath( $this->getBaseDir(), $src );
  681. $dir = dirname( $src );
  682. $dest = JPath::makePath( $dir, $dest );
  683. $error = JFolder::rename( $src, $dest );
  684. return $error;
  685. }
  686. /*
  687. * Copy a file.
  688. * @param string $files The relative file or comma seperated list of files
  689. * @param string $dest The relative path of the destination dir
  690. * @return string $error on failure
  691. */
  692. function copy( $files, $dest_dir ){
  693. global $cl;
  694. $error = false;
  695. $files = explode( ",", $files );
  696. foreach( $files as $file ){
  697. $filepath = JPath::makePath( $dest_dir, basename( $file ) );
  698. $src = JPath::makePath( $this->getBaseDir(), $file );
  699. $dest = JPath::makePath( $this->getBaseDir(), $filepath );
  700. $error = JFile::copy( $src, $dest );
  701. }
  702. return $error;
  703. }
  704. /*
  705. * Copy a file.
  706. * @param string $files The relative file or comma seperated list of files
  707. * @param string $dest The relative path of the destination dir
  708. * @return string $error on failure
  709. */
  710. function move( $files, $dest_dir ){
  711. global $cl;
  712. $error = false;
  713. $files = explode( ",", $files );
  714. foreach( $files as $file ){
  715. $filepath = JPath::makePath( $dest_dir, basename( $file ) );
  716. $src = JPath::makePath( $this->getBaseDir(), $file );
  717. $dest = JPath::makePath( $this->getBaseDir(), $filepath );
  718. $error = JFile::rename( $src, $dest );
  719. }
  720. return $error;
  721. }
  722. /**
  723. * New folder
  724. * @param string $dir The base dir
  725. * @param string $new_dir The folder to be created
  726. * @return string $error on failure
  727. */
  728. function newFolder( $dir, $new_dir )
  729. {
  730. global $cl;
  731. $error = false;
  732. $folder = JPath::makePath( $dir, JFile::makeSafe( $new_dir ) );
  733. $folder = JPath::makePath( $this->getBaseDir(), $folder );
  734. if( !JFolder::createFolder( $folder ) ){
  735. $error = $cl['new_dir_err'];
  736. }
  737. return $error;
  738. }
  739. }
  740. ?>