PageRenderTime 24ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://github.com/CCI-Studios/Wee-Magazine
PHP | 459 lines | 234 code | 75 blank | 150 comment | 60 complexity | 661deb4cc9e867ca207679b51d973966 MD5 | raw file
  1. <?php
  2. /**
  3. * @package JCE
  4. * @copyright Copyright (c) 2009-2012 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. //wfimport('editor.libraries.classes.error');
  13. wfimport('editor.libraries.classes.utility');
  14. wfimport('editor.libraries.classes.token');
  15. wfimport('editor.libraries.classes.document');
  16. wfimport('editor.libraries.classes.view');
  17. wfimport('editor.libraries.classes.tabs');
  18. wfimport('editor.libraries.classes.request');
  19. /**
  20. * JCE class
  21. *
  22. * @static
  23. * @package JCE
  24. * @since 1.5
  25. */
  26. class WFEditor extends JObject {
  27. /**
  28. * @var varchar
  29. */
  30. private $_version = '2.2.7.2';
  31. /**
  32. * Constructor activating the default information of the class
  33. *
  34. * @access protected
  35. */
  36. public function __construct($config = array()) {
  37. $this->setProperties($config);
  38. }
  39. /**
  40. * Returns a reference to a editor object
  41. *
  42. * This method must be invoked as:
  43. * <pre> $browser =JContentEditor::getInstance();</pre>
  44. *
  45. * @access public
  46. * @return JCE The editor object.
  47. */
  48. public function & getInstance($config = array()) {
  49. static $instance;
  50. if (!is_object($instance)) {
  51. $instance = new WFEditor($config);
  52. }
  53. return $instance;
  54. }
  55. /**
  56. * Get the current version
  57. * @access protected
  58. * @return string
  59. */
  60. protected function getVersion() {
  61. return $this->get('_version');
  62. }
  63. /**
  64. * Get the Super Administrator status
  65. *
  66. * Determine whether the user is a Super Administrator
  67. * @access protected
  68. * @return boolean
  69. */
  70. protected function isSuperAdmin() {
  71. $user = JFactory::getUser();
  72. if (WF_JOOMLA15) {
  73. return (strtolower($user->usertype) == 'superadministrator' || strtolower($user->usertype) == 'super administrator' || $user->gid == 25) ? true : false;
  74. }
  75. return false;
  76. }
  77. /**
  78. * Get an appropriate editor profile
  79. * @access public
  80. * @return $profile Object
  81. */
  82. public function getProfile() {
  83. static $profile;
  84. if (!is_object($profile)) {
  85. $mainframe = JFactory::getApplication();
  86. $db = JFactory::getDBO();
  87. $user = JFactory::getUser();
  88. $option = $this->getComponentOption();
  89. $query = 'SELECT * FROM #__wf_profiles'
  90. . ' WHERE published = 1'
  91. . ' ORDER BY ordering ASC';
  92. $db->setQuery($query);
  93. $profiles = $db->loadObjectList();
  94. if ($option == 'com_jce') {
  95. $component_id = JRequest::getInt('component_id');
  96. if ($component_id) {
  97. $component = WFExtensionHelper::getComponent($component_id);
  98. $option = isset($component->element) ? $component->element : $component->option;
  99. }
  100. }
  101. /*$items = array();
  102. $name = JRequest::getWord('profile');
  103. if ($name) {
  104. for ($i = 0; $i < count($profiles); $i++) {
  105. if ($profiles[$i]->name == $name) {
  106. $items[] = $profiles[$i];
  107. }
  108. }
  109. }
  110. // revert if no name or valid items
  111. if (!$name || empty($items)) {
  112. $items = $profiles;
  113. }*/
  114. $area = $mainframe->isAdmin() ? 2 : 1;
  115. foreach ($profiles as $item) {
  116. // check if option is in list
  117. $isComponent = in_array($option, explode(',', $item->components));
  118. // Set area default as Front-end / Back-end
  119. if (!isset($item->area) || $item->area == '') {
  120. $item->area = 0;
  121. }
  122. if ($item->area == $area || $item->area == 0) {
  123. // Check user
  124. if ($user->id && in_array($user->id, explode(',', $item->users))) {
  125. if ($item->components) {
  126. if ($isComponent) {
  127. $profile = $item;
  128. return $profile;
  129. }
  130. } else {
  131. $profile = $item;
  132. return $profile;
  133. }
  134. }
  135. // Joomla! 1.6+
  136. if (method_exists('JUser', 'getAuthorisedGroups')) {
  137. $keys = $user->getAuthorisedGroups();
  138. } else {
  139. $keys = array($user->gid);
  140. }
  141. if ($item->types) {
  142. $groups = array_intersect($keys, explode(',', $item->types));
  143. if (!empty($groups)) {
  144. // Check components
  145. if ($item->components) {
  146. if ($isComponent) {
  147. $profile = $item;
  148. return $profile;
  149. }
  150. } else {
  151. $profile = $item;
  152. return $profile;
  153. }
  154. }
  155. }
  156. // Check components only
  157. if ($item->components && $isComponent) {
  158. $profile = $item;
  159. return $profile;
  160. }
  161. }
  162. }
  163. return null;
  164. }
  165. return $profile;
  166. }
  167. /**
  168. * Get the component option
  169. * @access private
  170. * @return string
  171. */
  172. private function getComponentOption() {
  173. $option = JRequest::getCmd('option', '');
  174. switch ($option) {
  175. case 'com_section' :
  176. $option = 'com_content';
  177. break;
  178. case 'com_categories' :
  179. $section = JRequest::getVar('section');
  180. if ($section) {
  181. $option = $section;
  182. }
  183. break;
  184. }
  185. return $option;
  186. }
  187. /**
  188. * Get editor parameters
  189. * @access public
  190. * @param array $options
  191. * @return object
  192. */
  193. public function getParams($options = array()) {
  194. static $params;
  195. if (!isset($params)) {
  196. $params = array();
  197. }
  198. // set blank key if not set
  199. if (!isset($options['key'])) {
  200. $options['key'] = '';
  201. }
  202. // set blank path if not set
  203. if (!isset($options['path'])) {
  204. $options['path'] = '';
  205. }
  206. $signature = serialize($options);
  207. if (empty($params[$signature])) {
  208. wfimport('admin.helpers.extension');
  209. // get component
  210. $component = WFExtensionHelper::getComponent();
  211. // get params data for this profile
  212. $profile = $this->getProfile();
  213. $profile_params = array();
  214. $component_params = array();
  215. if (!empty($component->params)) {
  216. $component_params = json_decode($component->params, true);
  217. // set null as array
  218. if (!$component_params) {
  219. $component_params = array();
  220. }
  221. }
  222. if ($profile) {
  223. $profile_params = json_decode($profile->params, true);
  224. // set null as array
  225. if (!$profile_params) {
  226. $profile_params = array();
  227. }
  228. }
  229. // merge data and convert to json string
  230. $data = WFParameter::mergeParams($component_params, $profile_params);
  231. $params[$signature] = new WFParameter($data, $options['path'], $options['key']);
  232. }
  233. return $params[$signature];
  234. }
  235. /**
  236. * Remove linebreaks and carriage returns from a parameter value
  237. *
  238. * @return The modified value
  239. * @param string The parameter value
  240. */
  241. protected function cleanParam($param) {
  242. if (is_array($param)) {
  243. $param = implode('|', $param);
  244. }
  245. return trim(preg_replace('/\n|\r|\t(\r\n)[\s]+/', '', $param));
  246. }
  247. /**
  248. * Get a parameter by key
  249. * @param $key Parameter key eg: editor.width
  250. * @param $fallback Fallback value
  251. * @param $default Default value
  252. */
  253. public function getParam($key, $fallback = '', $default = '', $type = 'string', $allowempty = true) {
  254. // get all keys
  255. $keys = explode('.', $key);
  256. // remove base key eg: 'editor'
  257. $base = array_shift($keys);
  258. // get params for base key
  259. $params = self::getParams(array('key' => $base));
  260. // get a parameter
  261. $param = $params->get($keys, $fallback, $allowempty);
  262. if (is_string($param) && $type == 'string') {
  263. $param = self::cleanParam($param);
  264. }
  265. if (is_numeric($default)) {
  266. $default = (float) $default;
  267. }
  268. if (is_numeric($param)) {
  269. $param = (float) $param;
  270. }
  271. if ($param === $default) {
  272. return '';
  273. }
  274. if ($type == 'boolean') {
  275. $param = (bool) $param;
  276. }
  277. return $param;
  278. }
  279. protected function checkLanguage($tag) {
  280. $file = JPATH_SITE . '/language/' . $tag . '/' . $tag . '.com_jce.xml';
  281. if (file_exists($file)) {
  282. wfimport('admin.helpers.xml');
  283. $xml = WFXMLHelper::getXML($file);
  284. if ($xml) {
  285. $version = WFXMLHelper::getAttribute($xml, 'version');
  286. if ($version == '2.0') {
  287. return true;
  288. }
  289. }
  290. }
  291. return false;
  292. }
  293. /**
  294. * Load a language file
  295. *
  296. * @param string $prefix Language prefix
  297. * @param object $path[optional] Base path
  298. */
  299. protected function loadLanguage($prefix, $path = JPATH_SITE) {
  300. $language = JFactory::getLanguage();
  301. $tag = $this->getLanguageTag();
  302. $language->load($prefix, $path, $tag, true);
  303. }
  304. /**
  305. * Return the curernt language code
  306. *
  307. * @access public
  308. * @return language code
  309. */
  310. public function getLanguageDir() {
  311. $language = JFactory::getLanguage();
  312. $tag = $this->getLanguageTag();
  313. if ($language->getTag() == $tag) {
  314. return $language->isRTL() ? 'rtl' : 'ltr';
  315. }
  316. return 'ltr';
  317. }
  318. /**
  319. * Return the curernt language code
  320. *
  321. * @access public
  322. * @return language code
  323. */
  324. protected function getLanguageTag() {
  325. $language = JFactory::getLanguage();
  326. $tag = $language->getTag();
  327. static $_language;
  328. if (!isset($_lanugage)) {
  329. if ($this->checkLanguage($tag)) {
  330. $_language = $tag;
  331. } else {
  332. $_language = 'en-GB';
  333. }
  334. }
  335. return $_language;
  336. }
  337. /**
  338. * Return the curernt language code
  339. *
  340. * @access public
  341. * @return language code
  342. */
  343. public function getLanguage() {
  344. $tag = $this->getLanguageTag();
  345. return substr($tag, 0, strpos($tag, '-'));
  346. }
  347. /**
  348. * Named wrapper to check access to a feature
  349. *
  350. * @access public
  351. * @return string
  352. */
  353. public function checkUser() {
  354. return $this->getProfile();
  355. }
  356. /**
  357. * XML encode a string.
  358. *
  359. * @access public
  360. * @param string String to encode
  361. * @return string Encoded string
  362. */
  363. public function xmlEncode($string) {
  364. return preg_replace(array('/&/', '/</', '/>/', '/\'/', '/"/'), array('&amp;', '&lt;', '&gt;', '&apos;', '&quot;'), $string);
  365. }
  366. /**
  367. * XML decode a string.
  368. *
  369. * @access public
  370. * @param string String to decode
  371. * @return string Decoded string
  372. */
  373. public function xmlDecode($string) {
  374. return preg_replace(array('&amp;', '&lt;', '&gt;', '&apos;', '&quot;'), array('/&/', '/</', '/>/', '/\'/', '/"/'), $string);
  375. }
  376. protected function log($file, $msg) {
  377. jimport('joomla.error.log');
  378. $log = JLog::getInstance($file);
  379. $log->addEntry(array('comment' => 'LOG: ' . $msg));
  380. }
  381. }
  382. ?>