PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/components/com_jce/editor/libraries/classes/editor.php

https://bitbucket.org/pastor399/newcastleunifc
PHP | 321 lines | 173 code | 61 blank | 87 comment | 48 complexity | aab37a93abf4024ace78b5e3af8da7da MD5 | raw file
  1. <?php
  2. /**
  3. * @package JCE
  4. * @copyright Copyright (c) 2009-2013 Ryan Demmer. All rights reserved.
  5. * @license GNU/GPL 2 or later - http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  6. * JCE is free software. This version may have been modified pursuant
  7. * to the GNU General Public License, and as distributed it includes or
  8. * is derivative of works licensed under the GNU General Public License or
  9. * other free or open source software licenses.
  10. */
  11. defined('_JEXEC') or die('RESTRICTED');
  12. /**
  13. * JCE class
  14. *
  15. * @static
  16. * @package JCE
  17. * @since 1.5
  18. */
  19. class WFEditor extends JObject {
  20. // Editor version
  21. protected $_version = '2.3.2.4';
  22. // Editor instance
  23. protected static $instance;
  24. // Editor Profile
  25. protected static $profile;
  26. // Editor Params
  27. protected static $params = array();
  28. /**
  29. * Constructor activating the default information of the class
  30. *
  31. * @access protected
  32. */
  33. public function __construct($config = array()) {
  34. $this->setProperties($config);
  35. }
  36. /**
  37. * Returns a reference to a editor object
  38. *
  39. * This method must be invoked as:
  40. * <pre> $browser =JContentEditor::getInstance();</pre>
  41. *
  42. * @access public
  43. * @return JCE The editor object.
  44. */
  45. public static function getInstance($config = array()) {
  46. if (!isset(self::$instance)) {
  47. self::$instance = new WFEditor($config);
  48. }
  49. return self::$instance;
  50. }
  51. /**
  52. * Get the current version
  53. * @access protected
  54. * @return string
  55. */
  56. public function getVersion() {
  57. return preg_replace('#[^a-z0-9]+#i', '', $this->get('_version'));
  58. }
  59. /**
  60. * Get an appropriate editor profile
  61. * @access public
  62. * @return $profile Object
  63. */
  64. public function getProfile($plugin = null) {
  65. if (!isset(self::$profile)) {
  66. $mainframe = JFactory::getApplication();
  67. $db = JFactory::getDBO();
  68. $user = JFactory::getUser();
  69. $option = $this->getComponentOption();
  70. $query = $db->getQuery(true);
  71. if (is_object($query)) {
  72. $query->select('*')->from('#__wf_profiles')->where('published = 1')->order('ordering ASC');
  73. } else {
  74. $query = 'SELECT * FROM #__wf_profiles'
  75. . ' WHERE published = 1'
  76. . ' ORDER BY ordering ASC';
  77. }
  78. $db->setQuery($query);
  79. $profiles = $db->loadObjectList();
  80. if ($option == 'com_jce') {
  81. $component_id = JRequest::getInt('component_id');
  82. if ($component_id) {
  83. $component = WFExtensionHelper::getComponent($component_id);
  84. $option = isset($component->element) ? $component->element : $component->option;
  85. }
  86. }
  87. // get the Joomla! area (admin or site)
  88. $area = $mainframe->isAdmin() ? 2 : 1;
  89. if (!class_exists('Mobile_Detect')) {
  90. // load mobile detect class
  91. require_once(dirname(__FILE__) . '/mobile.php');
  92. }
  93. $mobile = new Mobile_Detect();
  94. // set device values
  95. if ($mobile->isMobile()) {
  96. $device = 'phone';
  97. } else if ($mobile->isTablet()) {
  98. $device = 'tablet';
  99. } else {
  100. $device = 'desktop';
  101. }
  102. // Joomla! 1.6+
  103. if (method_exists('JUser', 'getAuthorisedGroups')) {
  104. $keys = $user->getAuthorisedGroups();
  105. } else {
  106. $keys = array($user->gid);
  107. }
  108. foreach ($profiles as $item) {
  109. // at least one user group or user must be set
  110. if (empty($item->types) && empty($item->users)) {
  111. continue;
  112. }
  113. // check user groups - a value should always be set
  114. $groups = array_intersect($keys, explode(',', $item->types));
  115. // user not in the current group...
  116. if (empty($groups)) {
  117. // no additional users set or no user match
  118. if (empty($item->users) || in_array($user->id, explode(',', $item->users)) === false) {
  119. continue;
  120. }
  121. }
  122. // check component
  123. if ($item->components && in_array($option, explode(',', $item->components)) === false) {
  124. continue;
  125. }
  126. // set device default as 'desktop,tablet,mobile'
  127. if (!isset($item->device) || empty($item->device)) {
  128. $item->device = 'desktop,tablet,phone';
  129. }
  130. // check device
  131. if (in_array($device, explode(',', $item->device)) === false) {
  132. continue;
  133. }
  134. // check area
  135. if (!empty($item->area) && (int) $item->area != $area) {
  136. continue;
  137. }
  138. // check for individual plugin - use Editor Model as it adds "core" plugins to profile set
  139. if ($plugin) {
  140. wfimport('admin.models.editor');
  141. $model = new WFModelEditor();
  142. $plugins = (array) $model->getPlugins();
  143. if (in_array($plugin, $plugins) === false) {
  144. continue;
  145. }
  146. }
  147. // assign item to profile
  148. self::$profile = $item;
  149. // return
  150. return self::$profile;
  151. }
  152. return null;
  153. }
  154. return self::$profile;
  155. }
  156. /**
  157. * Get the component option
  158. * @access private
  159. * @return string
  160. */
  161. private function getComponentOption() {
  162. $option = JRequest::getCmd('option', '');
  163. switch ($option) {
  164. case 'com_section' :
  165. $option = 'com_content';
  166. break;
  167. case 'com_categories' :
  168. $section = JRequest::getCmd('section');
  169. if ($section) {
  170. $option = $section;
  171. }
  172. break;
  173. }
  174. return $option;
  175. }
  176. /**
  177. * Get editor parameters
  178. * @access public
  179. * @param array $options
  180. * @return object
  181. */
  182. public function getParams($options = array()) {
  183. if (!isset(self::$params)) {
  184. self::$params = array();
  185. }
  186. // set blank key if not set
  187. if (!isset($options['key'])) {
  188. $options['key'] = '';
  189. }
  190. // set blank path if not set
  191. if (!isset($options['path'])) {
  192. $options['path'] = '';
  193. }
  194. $plugin = JRequest::getCmd('plugin');
  195. if ($plugin) {
  196. $options['plugin'] = $plugin;
  197. }
  198. $signature = serialize($options);
  199. if (empty(self::$params[$signature])) {
  200. wfimport('admin.helpers.extension');
  201. // get component
  202. $component = WFExtensionHelper::getComponent();
  203. // get params data for this profile
  204. $profile = $this->getProfile($plugin);
  205. $profile_params = array();
  206. $component_params = array();
  207. if (!empty($component->params)) {
  208. $component_params = json_decode($component->params, true);
  209. // set null as array
  210. if (!$component_params) {
  211. $component_params = array();
  212. }
  213. }
  214. if ($profile) {
  215. $profile_params = json_decode($profile->params, true);
  216. // set null as array
  217. if (!$profile_params) {
  218. $profile_params = array();
  219. }
  220. }
  221. // merge data and convert to json string
  222. $data = WFParameter::mergeParams($component_params, $profile_params);
  223. self::$params[$signature] = new WFParameter($data, $options['path'], $options['key']);
  224. }
  225. return self::$params[$signature];
  226. }
  227. /**
  228. * Get a parameter by key
  229. * @param $key Parameter key eg: editor.width
  230. * @param $fallback Fallback value
  231. * @param $default Default value
  232. */
  233. public function getParam($key, $fallback = '', $default = '', $type = 'string', $allowempty = true) {
  234. // get all keys
  235. $keys = explode('.', $key);
  236. // remove base key eg: 'editor'
  237. $base = array_shift($keys);
  238. // get params for base key
  239. $params = self::getParams(array('key' => $base));
  240. // get a parameter
  241. $param = $params->get($keys, $fallback, $allowempty);
  242. if (is_string($param) && $type == 'string') {
  243. $param = trim(preg_replace('#[\n\r\t]+#', '', $param));
  244. }
  245. if (is_numeric($default)) {
  246. $default = (float) $default;
  247. }
  248. if (is_numeric($param)) {
  249. $param = (float) $param;
  250. }
  251. if ($param === $default) {
  252. return '';
  253. }
  254. if ($type == 'boolean') {
  255. $param = (bool) $param;
  256. }
  257. return $param;
  258. }
  259. }
  260. ?>