PageRenderTime 45ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

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

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