PageRenderTime 42ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

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

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