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

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

https://github.com/CCI-Studios/Wee-Magazine
PHP | 475 lines | 233 code | 87 blank | 155 comment | 48 complexity | cda10bf1c22b501a9d9dd7087cf36079 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.editor');
  13. if (!defined('WF_INI_LANG')) {
  14. define('WF_INI_LANG', 0);
  15. }
  16. /**
  17. * JCE class
  18. *
  19. * @package JCE Site
  20. */
  21. class WFEditorPlugin extends WFEditor {
  22. private $_alerts = array();
  23. /**
  24. * Constructor activating the default information of the class
  25. *
  26. * @access public
  27. */
  28. function __construct($config = array()) {
  29. // Call parent
  30. parent::__construct();
  31. $db = JFactory::getDBO();
  32. // get plugin name
  33. $plugin = JRequest::getCmd('plugin');
  34. // check plugin
  35. if ($this->checkPlugin($plugin)) {
  36. $this->set('name', $plugin);
  37. if (!array_key_exists('type', $config)) {
  38. $config['type'] = 'standard';
  39. }
  40. if (!array_key_exists('base_path', $config)) {
  41. $config['base_path'] = WF_EDITOR_PLUGINS . '/' . $plugin;
  42. }
  43. if (!defined('WF_EDITOR_PLUGIN')) {
  44. define('WF_EDITOR_PLUGIN', $config['base_path']);
  45. }
  46. if (!array_key_exists('view_path', $config)) {
  47. $config['view_path'] = WF_EDITOR_PLUGINS . '/' . $plugin;
  48. }
  49. if (!array_key_exists('layout', $config)) {
  50. $config['layout'] = 'default';
  51. }
  52. if (!array_key_exists('template_path', $config)) {
  53. $config['template_path'] = WF_EDITOR_PLUGIN . '/tmpl';
  54. }
  55. $this->setProperties($config);
  56. } else {
  57. die(JError::raiseError(403, 'RESTRICTED ACCESS'));
  58. }
  59. }
  60. /**
  61. * Returns a reference to a editor object
  62. *
  63. * This method must be invoked as:
  64. * <pre> $browser =JCE::getInstance();</pre>
  65. *
  66. * @access public
  67. * @return JCE The editor object.
  68. * @since 1.5
  69. */
  70. public function & getInstance($config = array()) {
  71. static $instance;
  72. if (!is_object($instance)) {
  73. $instance = new WFEditorPlugin($config);
  74. }
  75. return $instance;
  76. }
  77. /**
  78. * Get plugin View
  79. * @access public
  80. * @return WFView
  81. */
  82. public function & getView() {
  83. static $view;
  84. if (!is_object($view)) {
  85. // create plugin view
  86. $view = new WFView(array(
  87. 'view_path' => $this->get('base_path'),
  88. 'template_path' => $this->get('template_path'),
  89. 'name' => $this->get('name'),
  90. 'layout' => $this->get('layout')
  91. ));
  92. $view->assign('plugin', $this);
  93. }
  94. return $view;
  95. }
  96. private function isRequest() {
  97. $format = JRequest::getWord('format');
  98. return ($format == 'json' || $format == 'raw') && (JRequest::getVar('json') || JRequest::getWord('action'));
  99. }
  100. public function execute() {
  101. WFToken::checkToken() or die('RESTRICTED ACCESS');
  102. // JSON request or upload action
  103. if ($this->isRequest()) {
  104. $request = WFRequest::getInstance();
  105. $request->process();
  106. } else {
  107. $version = $this->getVersion();
  108. $name = $this->getName();
  109. // process javascript languages
  110. if (JRequest::getWord('task') == 'loadlanguages') {
  111. jimport('joomla.application.component.model');
  112. JModel::addIncludePath(JPATH_COMPONENT_ADMINISTRATOR . '/models');
  113. $model = JModel::getInstance('editor', 'WFModel');
  114. $files = array();
  115. $section = array('dlg', $name . '_dlg');
  116. $ini = JPATH_SITE . '/language/en-GB/en-GB.com_jce_' . $name . '.ini';
  117. if (is_file($ini)) {
  118. $files[] = $ini;
  119. $language = JFactory::getLanguage();
  120. $tag = $language->getTag();
  121. // non-english language
  122. if ($tag != 'en-GB') {
  123. $ini = JPATH_SITE . '/language/' . $tag . '/' . $tag . '.com_jce_' . $name . '.ini';
  124. if (is_file($ini)) {
  125. $files[] = $ini;
  126. }
  127. }
  128. }
  129. if (method_exists($model, 'loadLanguages')) {
  130. $model->loadLanguages($files, $section);
  131. }
  132. }
  133. $this->loadLanguage('com_jce', JPATH_ADMINISTRATOR);
  134. // Load Plugin language
  135. $this->loadPluginLanguage();
  136. $xml = WFXMLHelper::parseInstallManifest(WF_EDITOR_PLUGIN . '/' . $name . '.xml');
  137. if (isset($xml['version'])) {
  138. $version = $xml['version'];
  139. }
  140. // create the document
  141. $document = WFDocument::getInstance(array(
  142. 'version' => $version,
  143. 'title' => WFText::_('WF_' . strtoupper($this->getName() . '_TITLE')),
  144. 'name' => $name,
  145. 'language' => $this->getLanguageTag(),
  146. 'direction' => $this->getLanguageDir(),
  147. 'compress_javascript' => $this->getParam('editor.compress_javascript', 0),
  148. 'compress_css' => $this->getParam('editor.compress_css', 0)
  149. ));
  150. // set standalone mode
  151. $document->set('standalone', JRequest::getInt('standalone', 0));
  152. // create display
  153. $this->display();
  154. $document = WFDocument::getInstance();
  155. // set standalone mode (for File Browser etc)
  156. if ($document->get('standalone') == 1) {
  157. // remove some scripts
  158. $document->removeScript('tiny_mce_popup', 'tiny_mce');
  159. $document->removeScript('tiny_mce_utils', 'libraries');
  160. }
  161. // pack assets if required
  162. $document->pack(true, $this->getParam('editor.compress_gzip', 0));
  163. // get the view
  164. $view = $this->getView();
  165. // set body output
  166. $document->setBody($view->loadTemplate());
  167. // render document
  168. $document->render();
  169. }
  170. }
  171. /**
  172. * Display plugin
  173. * @access private
  174. */
  175. public function display() {
  176. jimport('joomla.filesystem.folder');
  177. $document = WFDocument::getInstance();
  178. $document->addScript(array('tiny_mce_popup'), 'tiny_mce');
  179. if (WF_INI_LANG) {
  180. // ini language
  181. $document->addScript(array('index.php?option=com_jce&view=editor&' . $document->getQueryString(array('task' => 'loadlanguages'))), 'joomla');
  182. }
  183. // jquery versions
  184. $jquery = array('jquery/jquery-' . WF_JQUERY . '.min.js', 'jquery/jquery-ui-' . WF_JQUERYUI . '.custom.min.js');
  185. $document->addScript($jquery, 'libraries');
  186. $document->addScript(array(
  187. 'html5',
  188. 'select',
  189. 'tips',
  190. 'tiny_mce_utils',
  191. 'plugin'
  192. ), 'libraries');
  193. // get UI Theme
  194. $theme = $this->getParam('editor.dialog_theme', 'jce');
  195. $ui = JFolder::files(WF_EDITOR_LIBRARIES . '/css/jquery/' . $theme, '\.css$');
  196. $document->addStyleSheet(array(
  197. 'jquery/' . $theme . '/' . basename($ui[0], '.css'),
  198. 'plugin'
  199. ), 'libraries');
  200. // add custom plugin.css if exists
  201. if (is_file(JPATH_SITE . '/media/jce/css/plugin.css')) {
  202. $document->addStyleSheet(array('media/jce/css/plugin.css'), 'joomla');
  203. }
  204. }
  205. /**
  206. * Return the plugin name
  207. * @access public
  208. * @return string
  209. */
  210. public function getName() {
  211. return $this->get('name');
  212. }
  213. /**
  214. * Get default values for a plugin.
  215. * Key / Value pairs will be retrieved from the profile or plugin manifest
  216. * @access public
  217. * @param array $defaults
  218. * @return array
  219. */
  220. public function getDefaults($defaults = array()) {
  221. $name = $this->getName();
  222. $params = $this->getParams(array(
  223. 'key' => $name,
  224. 'path' => WF_EDITOR_PLUGIN . '/' . $name . '.xml'
  225. ));
  226. return array_merge($defaults, (array) $params->getAll('defaults'));
  227. }
  228. /**
  229. * Check the user is in an authorized group
  230. * Check the users group is authorized to use the plugin
  231. *
  232. * @access public
  233. * @return boolean
  234. */
  235. public function checkPlugin($plugin = null) {
  236. if ($plugin) {
  237. // check existence of plugin directory
  238. if (is_dir(WF_EDITOR_PLUGINS . '/' . $plugin) || is_dir(JPATH_PLUGINS . '/jce/' . $plugin)) {
  239. // check profile
  240. $profile = $this->getProfile();
  241. return is_object($profile) && isset($profile->id) && $profile->published = 1 && in_array($plugin, explode(',', $profile->plugins));
  242. }
  243. }
  244. return false;
  245. }
  246. /**
  247. * Load current plugin language file
  248. * @access private
  249. */
  250. private function loadPluginLanguage() {
  251. $this->loadLanguage('com_jce_' . trim($this->getName()));
  252. }
  253. /**
  254. * Add an alert array to the stack
  255. *
  256. * @access private
  257. * @param object $class Alert classname
  258. * @param object $title Alert title
  259. * @param object $text Alert text
  260. */
  261. protected function addAlert($class = 'info', $title = '', $text = '') {
  262. $alerts = $this->getAlerts();
  263. $alerts[] = array(
  264. 'class' => $class,
  265. 'title' => $title,
  266. 'text' => $text
  267. );
  268. $this->set('_alerts', $alerts);
  269. }
  270. /**
  271. * Get current alerts
  272. * @access private
  273. * @return array Alerts
  274. */
  275. private function getAlerts() {
  276. return $this->get('_alerts');
  277. }
  278. /**
  279. * Convert a url to path
  280. *
  281. * @access public
  282. * @param string The url to convert
  283. * @return string Full path to file
  284. */
  285. public function urlToPath($url) {
  286. $document = WFDocument::getInstance();
  287. return $document->urlToPath($url);
  288. }
  289. /**
  290. * Returns an image url
  291. *
  292. * @access public
  293. * @param string The file to load including path and extension eg: libaries.image.gif
  294. * @return string Image url
  295. */
  296. public function image($image, $root = 'libraries') {
  297. $document = WFDocument::getInstance();
  298. return $document->image($image, $root);
  299. }
  300. /**
  301. * Load a plugin extension
  302. *
  303. * @access protected
  304. */
  305. protected function getExtensions($arguments) {
  306. return array();
  307. }
  308. /**
  309. * Load & Call an extension
  310. *
  311. * @access protected
  312. * @param array $config
  313. * @return array
  314. */
  315. protected function loadExtensions($config = array()) {
  316. return array();
  317. }
  318. /**
  319. * Compile plugin settings from defaults and alerts
  320. *
  321. * @access public
  322. * @param array $settings
  323. * @return array
  324. */
  325. public function getSettings($settings = array()) {
  326. $default = array(
  327. 'alerts' => $this->getAlerts(),
  328. 'defaults' => $this->getDefaults()
  329. );
  330. $settings = array_merge($default, $settings);
  331. return $settings;
  332. }
  333. /**
  334. * Get a parameter by key
  335. *
  336. * @access public
  337. * @param string $key Parameter key eg: editor.width
  338. * @param mixed $fallback Fallback value
  339. * @param mixed $default Default value
  340. * @param string $type Variable type eg: string, boolean, integer, array
  341. * @param bool $allowempty
  342. * @return mixed
  343. */
  344. public function getParam($key, $fallback = '', $default = '', $type = 'string', $allowempty = true) {
  345. // get plugin name
  346. $name = $this->getName();
  347. // get all keys
  348. $keys = explode('.', $key);
  349. // root key set
  350. if ($keys[0] === 'editor' || $keys[0] === $name) {
  351. return parent::getParam($key, $fallback, $default, $type, $allowempty);
  352. // no root key set, treat as shared param
  353. } else {
  354. // get all params
  355. $params = parent::getParams();
  356. // check plugin param and fallback to editor param
  357. $param = $params->get($name . '.' . $key, $params->get('editor.' . $key, $fallback, $allowempty), $allowempty);
  358. if (is_string($param) && $type === 'string') {
  359. $param = self::cleanParam($param);
  360. }
  361. if (is_numeric($default) && $type === 'integer') {
  362. $default = (float) $default;
  363. }
  364. if (is_numeric($param) && $type === 'integer') {
  365. $param = (float) $param;
  366. }
  367. if ($param === $default) {
  368. return '';
  369. }
  370. if ($type == 'boolean') {
  371. $param = (bool) $param;
  372. }
  373. return $param;
  374. }
  375. }
  376. /**
  377. * Named wrapper to check access to a feature
  378. *
  379. * @access public
  380. * @param string The feature to check, eg: upload
  381. * @param mixed The defalt value
  382. * @return Boolean
  383. */
  384. public function checkAccess($option, $default = 0) {
  385. return (bool) self::getParam($option, $default);
  386. }
  387. }
  388. ?>