PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/editors/jce/libraries/classes/editor.php

https://gitlab.com/endomorphosis/OLAAaction
PHP | 670 lines | 373 code | 40 blank | 257 comment | 47 complexity | 155b2dff8ba1ecbe08103acf64ce1570 MD5 | raw file
  1. <?php
  2. /**
  3. * @version $Id: editor.php 109 2009-06-21 19:24:41Z happynoodleboy $
  4. * @package JCE
  5. * @copyright Copyright (C) 2005 - 2009 Ryan Demmer. All rights reserved.
  6. * @author Ryan Demmer
  7. * @license GNU/GPL
  8. * JCE is free software. This version may have been modified pursuant
  9. * to the GNU General Public License, and as distributed it includes or
  10. * is derivative of works licensed under the GNU General Public License or
  11. * other free or open source software licenses.
  12. */
  13. defined('_JEXEC') or die('Restricted access');
  14. /**
  15. * JCE class
  16. *
  17. * @static
  18. * @package JCE
  19. * @since 1.5
  20. */
  21. class JContentEditor extends JObject
  22. {
  23. /*
  24. * @var varchar
  25. */
  26. var $version = '1.5.6';
  27. /*
  28. * @var varchar
  29. */
  30. var $site_url = null;
  31. /*
  32. * @var varchar
  33. */
  34. var $group = null;
  35. /*
  36. * @var object
  37. */
  38. var $params = null;
  39. /*
  40. * @var array
  41. */
  42. var $plugins = array();
  43. /*
  44. * @var varchar
  45. */
  46. var $url = array();
  47. /*
  48. * @var varchar
  49. */
  50. var $request = null;
  51. /*
  52. * @var array
  53. */
  54. var $scripts = array();
  55. /*
  56. * @var array
  57. */
  58. var $css = array();
  59. /*
  60. * @var boolean
  61. */
  62. var $_debug = false;
  63. /**
  64. * Constructor activating the default information of the class
  65. *
  66. * @access protected
  67. */
  68. function __construct($config = array())
  69. {
  70. global $mainframe;
  71. $this->setProperties($config);
  72. // Get user group
  73. $this->group = $this->getUserGroup();
  74. // Get editor and group params
  75. $this->params = $this->getEditorParams();
  76. }
  77. /**
  78. * Returns a reference to a editor object
  79. *
  80. * This method must be invoked as:
  81. * <pre> $browser = &JContentEditor::getInstance();</pre>
  82. *
  83. * @access public
  84. * @return JCE The editor object.
  85. * @since 1.5
  86. */
  87. function &getInstance()
  88. {
  89. static $instance;
  90. if (!is_object($instance)) {
  91. $instance = new JContentEditor();
  92. }
  93. return $instance;
  94. }
  95. /**
  96. * Get the current version
  97. * @return Version
  98. */
  99. function getVersion()
  100. {
  101. // remove dots and return version
  102. return str_replace('.', '', $this->version);
  103. }
  104. /**
  105. * Get the current users group if any
  106. *
  107. * @access public
  108. * @return group or false
  109. */
  110. function getUserGroup()
  111. {
  112. $db =& JFactory::getDBO();
  113. $user =& JFactory::getUser();
  114. $option = JRequest::getCmd('option');
  115. if ($this->group) {
  116. return $this->group;
  117. }
  118. $query = 'SELECT *'
  119. . ' FROM #__jce_groups'
  120. . ' WHERE published = 1'
  121. . ' ORDER BY ordering ASC'
  122. ;
  123. $db->setQuery($query);
  124. $groups = $db->loadObjectList();
  125. foreach ($groups as $group) {
  126. $components = ($option == 'com_jce') ? true : in_array($option, explode(',', $group->components));
  127. // Check user
  128. if (in_array($user->id, explode(',', $group->users))) {
  129. // Check components
  130. if ($group->components) {
  131. if ($components) {
  132. return $group;
  133. }
  134. }else{
  135. return $group;
  136. }
  137. }
  138. // Check usertype
  139. if (in_array($user->gid, explode(',', $group->types))) {
  140. // Check components
  141. if ($group->components) {
  142. if ($components) {
  143. return $group;
  144. }
  145. }else{
  146. return $group;
  147. }
  148. }
  149. // Check components only
  150. if ($group->components && $components) {
  151. return $group;
  152. }
  153. }
  154. return null;
  155. }
  156. /**
  157. * Get the Super Administrator status
  158. *
  159. * Determine whether the user is a Super Administrator
  160. *
  161. * @return boolean
  162. */
  163. function isSuperAdmin()
  164. {
  165. $user =& JFactory::getUser();
  166. return (strtolower($user->usertype) == 'superadministrator' || strtolower($user->usertype) == 'super administrator' || $user->gid == 25) ? true : false;
  167. }
  168. /**
  169. * Filter (remove) a parameter from a parameter string
  170. * @return string Filtered parameter String
  171. * @param object $params
  172. * @param object $key
  173. */
  174. function filterParams($params, $key)
  175. {
  176. $params = explode("\n", $params);
  177. $return = array();
  178. foreach($params as $param) {
  179. if (preg_match('/'.$key.'/i', $param)) {
  180. $return[] = $param;
  181. }
  182. }
  183. return implode("\n", $return);
  184. }
  185. /**
  186. * Return the JCE Editor's parameters
  187. *
  188. * @return object
  189. */
  190. function getEditorParams()
  191. {
  192. $db =& JFactory::getDBO();
  193. if (isset($this->params)) {
  194. return $this->params;
  195. }
  196. $e_params = '';
  197. $g_params = '';
  198. $query = 'SELECT params FROM #__plugins'
  199. . ' WHERE element = '. $db->Quote('jce')
  200. . ' AND folder = '. $db->Quote('editors')
  201. . ' AND published = 1'
  202. . ' LIMIT 1'
  203. ;
  204. $db->setQuery($query);
  205. $e_params = $db->loadResult();
  206. // check if group params available
  207. if ($this->group) {
  208. $g_params = $this->filterParams($this->group->params, 'editor');
  209. }
  210. return new JParameter($e_params . $g_params);
  211. }
  212. /**
  213. * Get an Editor Parameter by key
  214. *
  215. * @return string Editor Parameter
  216. * @param string $key The parameter key
  217. * @param string $default[optional] Value if no result
  218. */
  219. function getEditorParam($key, $default = '', $fallback = '')
  220. {
  221. $params = $this->getEditorParams();
  222. return $this->getParam($params, $key, $default, $fallback);
  223. }
  224. /**
  225. * Return the plugin parameter object
  226. *
  227. * @access public
  228. * @param string The plugin
  229. * @return The parameter object
  230. */
  231. function getPluginParams($plugin)
  232. {
  233. $params = '';
  234. if ($this->group) {
  235. $params = $this->filterParams($this->group->params, $plugin);
  236. }
  237. return new JParameter($params);
  238. }
  239. /**
  240. * Get a group parameter from plugin and/or editor parameters
  241. *
  242. * @access public
  243. * @param string The parameter name
  244. * @param string The default value
  245. * @return string
  246. */
  247. function getSharedParam($plugin, $param, $default = '')
  248. {
  249. $e_params = $this->getEditorParams();
  250. $p_params = $this->getPluginParams($plugin);
  251. $ret = $p_params->get($plugin . '_' . $param, '');
  252. if ($ret == '') {
  253. $ret = $e_params->get('editor_' . $param, $default);
  254. }
  255. return $this->cleanParam($ret);
  256. }
  257. /**
  258. * Add a plugin to the plugins array
  259. *
  260. * @return null
  261. * @param array $plugins
  262. */
  263. function addPlugins($plugins)
  264. {
  265. $plugins = (array) $plugins;
  266. $this->plugins = array_unique(array_merge($this->plugins, $plugins));
  267. }
  268. /**
  269. * Return a list of published JCE plugins
  270. *
  271. * @access public
  272. * @return string list
  273. */
  274. function getPlugins()
  275. {
  276. $db =& JFactory::getDBO();
  277. $plugins = array();
  278. if ($this->group) {
  279. $query = "SELECT name"
  280. . " FROM #__jce_plugins"
  281. . " WHERE published = 1"
  282. . " AND type = 'plugin'"
  283. . " AND id IN (". $this->group->plugins. ")"
  284. ;
  285. $db->setQuery($query);
  286. $plugins = $db->loadResultArray();
  287. }
  288. return array_merge($plugins, $this->plugins);
  289. }
  290. /**
  291. * Get a list of editor font families
  292. *
  293. * @return string font family list
  294. * @param string $add Font family to add
  295. * @param string $remove Font family to remove
  296. */
  297. function getEditorFonts($add, $remove)
  298. {
  299. $add = explode(';', $this->getEditorParam('editor_theme_advanced_fonts_add', ''));
  300. $remove = preg_split('/[;,]+/', $this->getEditorParam('editor_theme_advanced_fonts_remove', ''));
  301. // Default font list
  302. $fonts = array(
  303. 'Andale Mono=andale mono,times',
  304. 'Arial=arial,helvetica,sans-serif',
  305. 'Arial Black=arial black,avant garde',
  306. 'Book Antiqua=book antiqua,palatino',
  307. 'Comic Sans MS=comic sans ms,sans-serif',
  308. 'Courier New=courier new,courier',
  309. 'Georgia=georgia,palatino',
  310. 'Helvetica=helvetica',
  311. 'Impact=impact,chicago',
  312. 'Symbol=symbol',
  313. 'Tahoma=tahoma,arial,helvetica,sans-serif',
  314. 'Terminal=terminal,monaco',
  315. 'Times New Roman=times new roman,times',
  316. 'Trebuchet MS=trebuchet ms,geneva',
  317. 'Verdana=verdana,geneva',
  318. 'Webdings=webdings',
  319. 'Wingdings=wingdings,zapf dingbats'
  320. );
  321. if (count($remove)) {
  322. foreach($fonts as $key => $value) {
  323. foreach($remove as $gone) {
  324. if ($gone) {
  325. if (preg_match('/^'. $gone .'=/i', $value)) {
  326. // Remove family
  327. unset($fonts[$key]);
  328. }
  329. }
  330. }
  331. }
  332. }
  333. foreach($add as $new) {
  334. // Add new font family
  335. if (preg_match('/([^\=]+)(\=)([^\=]+)/', trim($new)) && !in_array($new, $fonts)) {
  336. $fonts[] = $new;
  337. }
  338. }
  339. natcasesort($fonts);
  340. return implode(';', $fonts);
  341. }
  342. /**
  343. * Return the curernt language code
  344. *
  345. * @access public
  346. * @return language code
  347. */
  348. function getLanguageDir()
  349. {
  350. $language =& JFactory::getLanguage();
  351. return $language->isRTL() ? 'rtl' : 'ltr';
  352. }
  353. /**
  354. * Return the curernt language code
  355. *
  356. * @access public
  357. * @return language code
  358. */
  359. function getLanguageTag()
  360. {
  361. $language =& JFactory::getLanguage();
  362. if ($language->isRTL()) {
  363. return 'en-GB';
  364. }
  365. return $language->getTag();
  366. }
  367. /**
  368. * Return the curernt language code
  369. *
  370. * @access public
  371. * @return language code
  372. */
  373. function getLanguage()
  374. {
  375. $tag = $this->getLanguageTag();
  376. if (file_exists(JPATH_SITE .DS. 'language' .DS. $tag .DS. $tag .'.com_jce.xml')) {
  377. return substr($tag, 0, strpos($tag, '-'));
  378. }
  379. return 'en';
  380. }
  381. /**
  382. * Load a language file
  383. *
  384. * @param string $prefix Language prefix
  385. * @param object $path[optional] Base path
  386. */
  387. function loadLanguage($prefix, $path = JPATH_SITE)
  388. {
  389. $language =& JFactory::getLanguage();
  390. $language->load($prefix, $path);
  391. }
  392. /**
  393. * Return the current site template name
  394. *
  395. * @access public
  396. */
  397. function getSiteTemplate()
  398. {
  399. $db =& JFactory::getDBO();
  400. $query = 'SELECT template'
  401. . ' FROM #__templates_menu'
  402. . ' WHERE client_id = 0'
  403. . ' AND menuid = 0'
  404. ;
  405. $db->setQuery($query);
  406. return $db->loadResult();
  407. }
  408. function getSkin()
  409. {
  410. return $this->params->get('editor_inlinepopups_skin', 'clearlooks2');
  411. }
  412. /**
  413. * Remove keys from an array
  414. *
  415. * @return $array by reference
  416. * @param arrau $array Array to edit
  417. * @param array $keys Keys to remove
  418. */
  419. function removeKeys(&$array, $keys)
  420. {
  421. if (!is_array($keys)) {
  422. $keys = array($keys);
  423. }
  424. $array = array_diff($array, $keys);
  425. }
  426. /**
  427. * Add keys to an array
  428. *
  429. * @return The string list with added key or the key
  430. * @param string The array
  431. * @param string The keys to add
  432. */
  433. function addKeys(&$array, $keys)
  434. {
  435. if (!is_array($keys)) {
  436. $keys = array($keys);
  437. }
  438. $array = array_unique(array_merge($array, $keys));
  439. }
  440. /**
  441. * Remove linebreaks and carriage returns from a parameter value
  442. *
  443. * @return The modified value
  444. * @param string The parameter value
  445. */
  446. function cleanParam($param)
  447. {
  448. return trim(preg_replace('/\n|\r|\t(\r\n)[\s]+/', '', $param));
  449. }
  450. /**
  451. * Get a JCE editor or plugin parameter
  452. *
  453. * @param object The parameter object
  454. * @param string The parameter object key
  455. * @param string The parameter default value
  456. * @param string The parameter default value
  457. * @access public
  458. * @return The parameter
  459. */
  460. function getParam($params, $key, $p, $t = '')
  461. {
  462. $v = JContentEditor::cleanParam($params->get($key, $p));
  463. return ($v == $t) ? '' : $v;
  464. }
  465. /**
  466. * Return a string of JCE Commands to be removed
  467. *
  468. * @access public
  469. * @return The string list
  470. */
  471. function getRemovePlugins()
  472. {
  473. $db =& JFactory::getDBO();
  474. $query = "SELECT name"
  475. . "\n FROM #__jce_plugins"
  476. . "\n WHERE type = 'command'"
  477. . "\n AND published = 0"
  478. ;
  479. $db->setQuery($query);
  480. $remove = $db->loadResultArray();
  481. if ($remove) {
  482. return implode(',', $remove);
  483. }else{
  484. return '';
  485. }
  486. }
  487. /**
  488. * Return a list of icons for each JCE editor row
  489. *
  490. * @access public
  491. * @param string The number of rows
  492. * @return The row array
  493. */
  494. function getRows()
  495. {
  496. $db =& JFactory::getDBO();
  497. $rows = array();
  498. if ($this->group) {
  499. // Get all plugins that are in the group rows list
  500. $query = "SELECT id, icon"
  501. . " FROM #__jce_plugins"
  502. . " WHERE published = 1"
  503. . " AND id IN (". str_replace(';', ',', $this->group->rows) .")"
  504. ;
  505. $db->setQuery($query);
  506. $icons = $db->loadObjectList();
  507. $lists = explode(';', $this->group->rows);
  508. if ($icons) {
  509. for($i=1; $i<=count($lists); $i++) {
  510. $x = $i - 1;
  511. $items = explode(',', $lists[$x]);
  512. $result = array();
  513. // I'm sure you can use array_walk for this but I just can't figure out how!
  514. foreach($items as $item) {
  515. // Add support for spacer
  516. if ($item == '00') {
  517. $result[] = '|';
  518. }else{
  519. foreach($icons as $icon) {
  520. if ($icon->id == $item) {
  521. $result[] = $icon->icon;
  522. }
  523. }
  524. }
  525. }
  526. $rows[$i] = implode(',', $result);
  527. }
  528. }
  529. }else{
  530. $num = intval($this->params->get('editor_layout_rows', '5'));
  531. for($i=1; $i<=$num; $i++) {
  532. $query = "SELECT icon"
  533. . " FROM #__jce_plugins"
  534. . " WHERE published = 1"
  535. . " AND row = ".$i
  536. . " ORDER BY ordering ASC"
  537. ;
  538. $db->setQuery($query);
  539. $result = $db->loadResultArray();
  540. if ($result) {
  541. $rows[$i] = implode(',', $result);
  542. }
  543. }
  544. }
  545. return $rows;
  546. }
  547. /**
  548. * Determine whether a plugin or command is loaded
  549. *
  550. * @access public
  551. * @param string The plugin
  552. * @return boolean
  553. */
  554. function isLoaded($plugin)
  555. {
  556. $db =& JFactory::getDBO();
  557. $query = 'SELECT count(id)'
  558. . ' FROM #__jce_plugins'
  559. . ' WHERE name = '. $db->Quote($plugin)
  560. . ' AND published = 1'
  561. . ' AND id IN ('. str_replace(';', ',', $this->group->rows) .')'
  562. ;
  563. $db->setQuery($query);
  564. return $db->loadResult() ? true : false;
  565. }
  566. /**
  567. * Get all loaded plugins config options
  568. *
  569. * @access public
  570. * @param array vars passed by reference
  571. */
  572. function getPluginConfig(&$vars)
  573. {
  574. // Store path
  575. $path = JPATH_PLUGINS .DS. 'editors' .DS. 'jce' .DS. 'tiny_mce' .DS. 'plugins';
  576. $plugins = $this->getPlugins();
  577. foreach($plugins as $plugin) {
  578. $file = $path .DS. $plugin .DS. 'classes' .DS. 'config.php';
  579. if (file_exists($file)) {
  580. require_once($file);
  581. // Create class name
  582. $class = ucfirst($plugin . 'Config');
  583. // Check class and method
  584. if(class_exists($class)){
  585. if (method_exists(new $class, 'getConfig')) {
  586. call_user_func_array(array($class, 'getConfig'), array(&$vars));
  587. }
  588. }
  589. }
  590. }
  591. }
  592. /**
  593. * Named wrapper to check access to a feature
  594. *
  595. * @access public
  596. * @param string The feature to check, eg: upload
  597. * @param string The defalt value
  598. * @return string
  599. */
  600. function checkUser()
  601. {
  602. if ($this->group) {
  603. return true;
  604. }
  605. return false;
  606. }
  607. /**
  608. * XML encode a string.
  609. *
  610. * @access public
  611. * @param string String to encode
  612. * @return string Encoded string
  613. */
  614. function xmlEncode($string)
  615. {
  616. return preg_replace(array('/&/', '/</', '/>/', '/\'/', '/"/'), array('&amp;', '&lt;', '&gt;', '&apos;', '&quot;'), $string);
  617. }
  618. /**
  619. * XML decode a string.
  620. *
  621. * @access public
  622. * @param string String to decode
  623. * @return string Decoded string
  624. */
  625. function xmlDecode($string)
  626. {
  627. return preg_replace(array('&amp;', '&lt;', '&gt;', '&apos;', '&quot;'), array('/&/', '/</', '/>/', '/\'/', '/"/'), $string);
  628. }
  629. }
  630. ?>