PageRenderTime 101ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 1ms

/administrator/components/com_jce/models/editor.php

https://github.com/srgg6701/auction-ruseasons
PHP | 1118 lines | 689 code | 212 blank | 217 comment | 131 complexity | e5ed72f428f0a9abf69fa6bb9f213928 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-3.0, LGPL-2.1, BSD-3-Clause, JSON
  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('admin.classes.model');
  13. wfimport('admin.classes.text');
  14. wfimport('admin.helpers.xml');
  15. wfimport('admin.helpers.extension');
  16. wfimport('editor.libraries.classes.token');
  17. wfimport('editor.libraries.classes.editor');
  18. jimport('joomla.application.component.model');
  19. class WFModelEditor extends WFModelBase {
  20. /**
  21. * Profile object
  22. *
  23. * @var object
  24. */
  25. private $profile = null;
  26. /**
  27. * Array of linked scripts
  28. *
  29. * @var array
  30. */
  31. protected $scripts = array();
  32. /**
  33. * Array of linked style sheets
  34. *
  35. * @var array
  36. */
  37. protected $stylesheets = array();
  38. /**
  39. * Array of included style declarations
  40. *
  41. * @var array
  42. */
  43. protected $styles = array();
  44. /**
  45. * Array of scripts placed in the header
  46. *
  47. * @var array
  48. */
  49. protected $javascript = array();
  50. private function addScript($url) {
  51. $this->scripts[] = $url;
  52. }
  53. private function addStyleSheet($url) {
  54. $this->stylesheets[] = $url;
  55. }
  56. private function addScriptDeclaration($text) {
  57. $this->javascript[] = $text;
  58. }
  59. private function addStyleDeclaration($text) {
  60. $this->styles[] = $text;
  61. }
  62. public function __construct() {
  63. $wf = WFEditor::getInstance();
  64. $this->profile = $wf->getProfile();
  65. }
  66. public function buildEditor() {
  67. // get document
  68. $document = JFactory::getDocument();
  69. // get an editor instance
  70. $wf = WFEditor::getInstance();
  71. // get current component
  72. $option = JRequest::getCmd('option');
  73. $component = WFExtensionHelper::getComponent(null, $option);
  74. // get default settings
  75. $settings = $this->getEditorSettings();
  76. // set default component id
  77. $component_id = 0;
  78. $component_id = isset($component->extension_id) ? $component->extension_id : ($component->id ? $component->id : 0);
  79. $version = $this->getVersion();
  80. // settings array for jce, tinymce etc
  81. $init = array();
  82. // if a profile is set
  83. if (is_object($this->profile)) {
  84. jimport('joomla.filesystem.folder');
  85. $settings = array_merge($settings, array('theme' => 'advanced', 'component_id' => $component_id, 'plugins' => $this->getPlugins()), $this->getToolbar());
  86. // Theme and skins
  87. $theme = array(
  88. 'toolbar_location' => array('top', 'top', 'string'),
  89. 'toolbar_align' => array('left', 'left', 'string'),
  90. 'statusbar_location' => array('bottom', 'bottom', 'string'),
  91. 'path' => array(1, 1, 'boolean'),
  92. 'resizing' => array(1, 0, 'boolean'),
  93. 'resize_horizontal' => array(1, 1, 'boolean'),
  94. 'resizing_use_cookie' => array(1, 1, 'boolean')
  95. );
  96. // set rows key to apss to plugin config
  97. $settings['rows'] = $this->profile->rows;
  98. foreach ($theme as $k => $v) {
  99. $settings['theme_advanced_' . $k] = $wf->getParam('editor.' . $k, $v[0], $v[1], $v[2]);
  100. }
  101. if (!$wf->getParam('editor.use_cookies', 1)) {
  102. $settings['theme_advanced_resizing_use_cookie'] = false;
  103. }
  104. $settings['width'] = $wf->getParam('editor.width');
  105. $settings['height'] = $wf->getParam('editor.height');
  106. // 'Look & Feel'
  107. $skin = explode('.', $wf->getParam('editor.toolbar_theme', 'default', 'default'));
  108. $settings['skin'] = $skin[0];
  109. $settings['skin_variant'] = isset($skin[1]) ? $skin[1] : '';
  110. $settings['body_class'] = $wf->getParam('editor.content_style_reset', $wf->getParam('editor.highcontrast', 0)) == 1 ? 'mceContentReset' : '';
  111. $settings['body_id'] = $wf->getParam('editor.body_id', '');
  112. $settings['content_css'] = $this->getStyleSheets();
  113. // Editor Toggle
  114. $settings['toggle'] = $wf->getParam('editor.toggle', 1, 1);
  115. $settings['toggle_label'] = htmlspecialchars($wf->getParam('editor.toggle_label', '[Toggle Editor]', '[Toggle Editor]'));
  116. $settings['toggle_state'] = $wf->getParam('editor.toggle_state', 1, 1);
  117. }// end profile
  118. //Other - user specified
  119. $userParams = $wf->getParam('editor.custom_config', '');
  120. $baseParams = array('mode', 'cleanup_callback', 'save_callback', 'file_browser_callback', 'urlconverter_callback', 'onpageload', 'oninit', 'editor_selector');
  121. if ($userParams) {
  122. $userParams = explode(';', $userParams);
  123. foreach ($userParams as $userParam) {
  124. $keys = explode(':', $userParam);
  125. if (!in_array(trim($keys[0]), $baseParams)) {
  126. $settings[trim($keys[0])] = count($keys) > 1 ? trim($keys[1]) : '';
  127. }
  128. }
  129. }
  130. // set compression states
  131. $compress = array('javascript' => intval($wf->getParam('editor.compress_javascript', 0)), 'css' => intval($wf->getParam('editor.compress_css', 0)));
  132. // create token
  133. $token = WFToken::getToken();
  134. $query = array('component_id' => $component_id, 'version' => $version);
  135. $query[$token] = 1;
  136. // set compression
  137. if ($compress['css']) {
  138. $this->addStyleSheet(JURI::base(true) . '/index.php?option=com_jce&view=editor&layout=editor&task=pack&type=css&component_id=' . $component_id . '&' . $token . '=1&version=' . $version);
  139. } else {
  140. // CSS
  141. $this->addStyleSheet($this->getURL(true) . '/libraries/css/editor.css?version=' . $version);
  142. //$this->addStyleSheet($this->getURL(true) . '/libraries/bootstrap/css/bootstrap.css?version=' . $version);
  143. // get plugin styles
  144. $this->getPluginStyles($settings);
  145. // get font-face and google fonts
  146. $fonts = trim(self::getCustomFonts($this->getStyleSheets(true)));
  147. if (!empty($fonts)) {
  148. $this->addStyleDeclaration($fonts);
  149. }
  150. }
  151. // set compression
  152. if ($compress['javascript']) {
  153. $this->addScript(JURI::base(true) . '/index.php?option=com_jce&view=editor&layout=editor&task=pack&component_id=' . $component_id . '&' . $token . '=1&version=' . $version);
  154. } else {
  155. $this->addScript($this->getURL(true) . '/tiny_mce/tiny_mce.js?version=' . $version);
  156. // Editor
  157. $this->addScript($this->getURL(true) . '/libraries/js/editor.js?version=' . $version);
  158. if (array_key_exists('language_load', $settings)) {
  159. // language
  160. $this->addScript(JURI::base(true) . '/index.php?option=com_jce&view=editor&layout=editor&task=loadlanguages&component_id=' . $component_id . '&' . $token . '=1&version=' . $version);
  161. }
  162. }
  163. // Get all optional plugin configuration options
  164. $this->getPluginConfig($settings);
  165. // pass compresison states to settings
  166. $settings['compress'] = json_encode($compress);
  167. // check for language files
  168. $this->checkLanguages($settings);
  169. $output = "";
  170. $i = 1;
  171. foreach ($settings as $k => $v) {
  172. // If the value is an array, implode!
  173. if (is_array($v)) {
  174. $v = ltrim(implode(',', $v), ',');
  175. }
  176. // Value must be set
  177. if ($v !== '') {
  178. // objects or arrays or functions or regular expression
  179. if (preg_match('/(\[[^\]*]\]|\{[^\}]*\}|function\([^\}]*\}|^#(.*)#$)/', $v)) {
  180. // replace hash delimiters with / for javascript regular expression
  181. $v = preg_replace('@^#(.*)#$@', '/$1/', $v);
  182. }
  183. // boolean
  184. else if (is_bool($v) === true) {
  185. $v = $v ? 'true' : 'false';
  186. }
  187. // stringified booleans
  188. else if ($v === "true" || $v === "false") {
  189. $v = $v === "true" ? 'true' : 'false';
  190. }
  191. // anything that is not solely an integer
  192. else if (!is_numeric($v)) {
  193. if (strpos($v, '"') === 0) {
  194. $v = '"' . trim($v, '"') . '"';
  195. } else {
  196. $v = '"' . str_replace('"', '\"', $v) . '"';
  197. }
  198. }
  199. $output .= "\t\t\t" . $k . ": " . $v . "";
  200. if ($i < count($settings)) {
  201. $output .= ",\n";
  202. }
  203. }
  204. // Must have 3 rows, even if 2 are blank!
  205. if (preg_match('/theme_advanced_buttons([1-3])/', $k) && $v == '') {
  206. $output .= "\t\t\t" . $k . ": \"\"";
  207. if ($i < count($settings)) {
  208. $output .= ",\n";
  209. }
  210. }
  211. $i++;
  212. }
  213. $tinymce = "{\n";
  214. $tinymce .= preg_replace('/,?\n?$/', '', $output) . "
  215. }";
  216. $init[] = $tinymce;
  217. $this->addScriptDeclaration("\n\t\ttry{WFEditor.init(" . implode(',', $init) . ");}catch(e){console.debug(e);}\n");
  218. if (is_object($this->profile)) {
  219. if ($wf->getParam('editor.callback_file')) {
  220. $this->addScript(JURI::root(true) . '/' . $wf->getParam('editor.callback_file'));
  221. }
  222. // add callback file if exists
  223. if (is_file(JPATH_SITE . '/media/jce/js/editor.js')) {
  224. $this->addScript(JURI::root(true) . '/media/jce/js/editor.js');
  225. }
  226. // add custom editor.css if exists
  227. if (is_file(JPATH_SITE . '/media/jce/css/editor.css')) {
  228. $this->addStyleSheet(JURI::root(true) . '/media/jce/css/editor.css');
  229. }
  230. }
  231. return $this->getOutput();
  232. }
  233. private function getOutput() {
  234. $document = JFactory::getDocument();
  235. $end = $document->_getLineEnd();
  236. $tab = $document->_getTab();
  237. $output = '';
  238. foreach ($this->stylesheets as $stylesheet) {
  239. $output .= $tab . '<link rel="stylesheet" href="' . $stylesheet . '" type="text/css" />' . $end;
  240. }
  241. foreach ($this->scripts as $script) {
  242. $output .= $tab . '<script type="text/javascript" src="' . $script . '"></script>' . $end;
  243. }
  244. foreach ($this->javascript as $script) {
  245. $output .= $tab . '<script type="text/javascript">' . $script . '</script>' . $end;
  246. }
  247. foreach ($this->styles as $style) {
  248. $output .= $tab . '<style type="text/css">' . $style . '</style>' . $end;
  249. }
  250. return $output;
  251. }
  252. /**
  253. * Check the current language pack exists and is complete
  254. * @param array $settings Settings array
  255. * @return void
  256. */
  257. private function checkLanguages(&$settings) {
  258. $plugins = array();
  259. $language = $settings['language'];
  260. // only if languages are loaded and not english
  261. if (array_key_exists('language_load', $settings) === false && $language != 'en') {
  262. jimport('joomla.filesystem.file');
  263. // check main languages and reset to english
  264. if (!JFile::exists(WF_EDITOR . '/tiny_mce/langs/' . $language . '.js') || !JFile::exists(WF_EDITOR_THEMES . '/advanced/langs/' . $language . '.js')) {
  265. $settings['language'] = 'en';
  266. return;
  267. }
  268. foreach ((array) $settings['plugins'] as $plugin) {
  269. $path = WF_EDITOR_PLUGINS . '/' . $plugin;
  270. // if english file exists then the installed language file should too
  271. if (JFile::exists($path . '/langs/en.js') && !JFile::exists($path . '/langs/' . $language . '.js')) {
  272. $plugins[] = $plugin;
  273. }
  274. }
  275. }
  276. $settings['skip_plugin_languages'] = $plugins;
  277. }
  278. /**
  279. * Get the current version
  280. * @return Version
  281. */
  282. private function getVersion() {
  283. $xml = WFXMLHelper::parseInstallManifest(JPATH_ADMINISTRATOR . '/components/com_jce/jce.xml');
  284. // return cleaned version number or date
  285. $version = preg_replace('/[^0-9a-z]/i', '', $xml['version']);
  286. if (!$version) {
  287. return date('Y-m-d', strtotime('today'));
  288. }
  289. return $version;
  290. }
  291. /**
  292. * Get default settings array
  293. * @return array
  294. */
  295. public function getEditorSettings() {
  296. wfimport('editor.libraries.classes.token');
  297. $wf = WFEditor::getInstance();
  298. $language = JFactory::getLanguage();
  299. $settings = array(
  300. 'token' => WFToken::getToken(),
  301. 'base_url' => JURI::root(),
  302. 'language' => $wf->getLanguage(),
  303. //'language_load' => false,
  304. 'directionality' => $language->isRTL() ? 'rtl' : 'ltr',
  305. 'theme' => 'none',
  306. 'plugins' => ''
  307. );
  308. if (WF_INI_LANG || $wf->getParam('editor.compress_javascript', 0)) {
  309. $settings['language_load'] = false;
  310. }
  311. return $settings;
  312. }
  313. /**
  314. * Return a list of icons for each JCE editor row
  315. *
  316. * @access public
  317. * @param string The number of rows
  318. * @return The row array
  319. */
  320. private function getToolbar() {
  321. wfimport('admin.models.plugins');
  322. $model = new WFModelPlugins();
  323. $wf = WFEditor::getInstance();
  324. $rows = array('theme_advanced_buttons1' => '', 'theme_advanced_buttons2' => '', 'theme_advanced_buttons3' => '');
  325. // we need a profile object and some defined rows
  326. if (!is_object($this->profile) || empty($this->profile->rows)) {
  327. return $rows;
  328. }
  329. // get plugins
  330. $plugins = $model->getPlugins();
  331. // get core commands
  332. $commands = $model->getCommands();
  333. // merge plugins and commands
  334. $icons = array_merge($commands, $plugins);
  335. // create an array of rows
  336. $lists = explode(';', $this->profile->rows);
  337. // backwards compatability map
  338. $map = array(
  339. 'paste' => 'clipboard',
  340. 'spacer' => '|'
  341. );
  342. $x = 0;
  343. for ($i = 1; $i <= count($lists); $i++) {
  344. $buttons = array();
  345. $items = explode(',', $lists[$x]);
  346. foreach ($items as $item) {
  347. // set the plugin/command name
  348. $name = $item;
  349. // map legacy values etc.
  350. if (array_key_exists($item, $map)) {
  351. $item = $map[$item];
  352. }
  353. // get buttons
  354. if (array_key_exists($item, $icons)) {
  355. $item = $icons[$item]->icon;
  356. }
  357. // check for custom plugin buttons
  358. if (array_key_exists($name, $plugins)) {
  359. $custom = $wf->getParam($name . '.buttons');
  360. if ($custom) {
  361. $a = array();
  362. foreach (explode(',', $item) as $s) {
  363. if (in_array($s, (array) $custom) || $s === "|") {
  364. $a[] = $s;
  365. }
  366. }
  367. $item = implode(',', $a);
  368. // remove leading or trailing |
  369. $item = trim($item, '|');
  370. }
  371. }
  372. // remove double |
  373. $item = preg_replace('#(\|,)+#', '|,', $item);
  374. $buttons[] = $item;
  375. }
  376. if (!empty($buttons)) {
  377. $rows['theme_advanced_buttons' . $i] = implode(',', $buttons);
  378. }
  379. $x++;
  380. }
  381. return $rows;
  382. }
  383. /**
  384. * Return a list of published JCE plugins
  385. *
  386. * @access public
  387. * @return string list
  388. */
  389. private function getPlugins() {
  390. jimport('joomla.filesystem.file');
  391. $return = array();
  392. if (is_object($this->profile)) {
  393. $wf = WFEditor::getInstance();
  394. $plugins = explode(',', $this->profile->plugins);
  395. $plugins = array_unique(array_merge(array('autolink', 'cleanup', 'core', 'code', 'dragupload', 'format'), $plugins));
  396. // add advlists plugin if lists are loaded
  397. if (in_array('lists', $plugins)) {
  398. $plugins[] = 'advlist';
  399. }
  400. // Load wordcount if path is enabled
  401. if ($wf->getParam('editor.path', 1)) {
  402. $plugins[] = 'wordcount';
  403. }
  404. foreach ($plugins as $plugin) {
  405. $path = WF_EDITOR_PLUGINS . '/' . $plugin;
  406. // check plugin is correctly installed and is a tinymce plugin, ie: it has an editor_plugin.js file
  407. if (JFile::exists($path . '/editor_plugin.js')) {
  408. $return[] = $plugin;
  409. }
  410. }
  411. }
  412. return $return;
  413. }
  414. /**
  415. * Get all loaded plugins config options
  416. *
  417. * @access public
  418. * @param array $settings passed by reference
  419. */
  420. private function getPluginConfig(&$settings) {
  421. $plugins = $settings['plugins'];
  422. if ($plugins && is_array($plugins)) {
  423. foreach ($plugins as $plugin) {
  424. $file = WF_EDITOR_PLUGINS . '/' . $plugin . '/classes/config.php';
  425. if (file_exists($file)) {
  426. require_once ($file);
  427. // Create class name
  428. $classname = 'WF' . ucfirst($plugin) . 'PluginConfig';
  429. // Check class and method
  430. if (class_exists($classname) && method_exists(new $classname, 'getConfig')) {
  431. call_user_func_array(array($classname, 'getConfig'), array(&$settings));
  432. }
  433. }
  434. }
  435. }
  436. }
  437. /**
  438. * Get all loaded plugins styles
  439. *
  440. * @access public
  441. * @param array $settings passed by reference
  442. */
  443. private function getPluginStyles($settings) {
  444. $plugins = $settings['plugins'];
  445. if ($plugins && is_array($plugins)) {
  446. foreach ($plugins as $plugin) {
  447. $file = WF_EDITOR_PLUGINS . '/' . $plugin . '/classes/config.php';
  448. if (file_exists($file)) {
  449. require_once ($file);
  450. // Create class name
  451. $classname = 'WF' . ucfirst($plugin) . 'PluginConfig';
  452. // Check class and method
  453. if (class_exists($classname) && method_exists(new $classname, 'getStyles')) {
  454. call_user_func(array($classname, 'getStyles'));
  455. }
  456. }
  457. }
  458. }
  459. }
  460. /**
  461. * Remove keys from an array
  462. *
  463. * @return $array by reference
  464. * @param arrau $array Array to edit
  465. * @param array $keys Keys to remove
  466. */
  467. public function removeKeys(&$array, $keys) {
  468. if (!is_array($keys)) {
  469. $keys = array($keys);
  470. }
  471. $array = array_diff($array, $keys);
  472. }
  473. /**
  474. * Add keys to an array
  475. *
  476. * @return The string list with added key or the key
  477. * @param string The array
  478. * @param string The keys to add
  479. */
  480. public function addKeys(&$array, $keys) {
  481. if (!is_array($keys)) {
  482. $keys = array($keys);
  483. }
  484. $array = array_unique(array_merge($array, $keys));
  485. }
  486. /**
  487. * Get a list of editor font families
  488. *
  489. * @return string font family list
  490. * @param string $add Font family to add
  491. * @param string $remove Font family to remove
  492. */
  493. public function getEditorFonts() {
  494. $wf = WFEditor::getInstance();
  495. $add = explode(';', $wf->getParam('editor.theme_advanced_fonts_add', ''));
  496. $remove = preg_split('/[;,]+/', $wf->getParam('editor.theme_advanced_fonts_remove', ''));
  497. // Default font list
  498. $fonts = array('Andale Mono=andale mono,times', 'Arial=arial,helvetica,sans-serif', 'Arial Black=arial black,avant garde', 'Book Antiqua=book antiqua,palatino', 'Comic Sans MS=comic sans ms,sans-serif', 'Courier New=courier new,courier', 'Georgia=georgia,palatino', 'Helvetica=helvetica', 'Impact=impact,chicago', 'Symbol=symbol', 'Tahoma=tahoma,arial,helvetica,sans-serif', 'Terminal=terminal,monaco', 'Times New Roman=times new roman,times', 'Trebuchet MS=trebuchet ms,geneva', 'Verdana=verdana,geneva', 'Webdings=webdings', 'Wingdings=wingdings,zapf dingbats');
  499. if (count($remove)) {
  500. foreach ($fonts as $key => $value) {
  501. foreach ($remove as $gone) {
  502. if ($gone) {
  503. if (preg_match('/^' . $gone . '=/i', $value)) {
  504. // Remove family
  505. unset($fonts[$key]);
  506. }
  507. }
  508. }
  509. }
  510. }
  511. foreach ($add as $new) {
  512. // Add new font family
  513. if (preg_match('/([^\=]+)(\=)([^\=]+)/', trim($new)) && !in_array($new, $fonts)) {
  514. $fonts[] = $new;
  515. }
  516. }
  517. natcasesort($fonts);
  518. return implode(';', $fonts);
  519. }
  520. /**
  521. * Return the current site template name
  522. *
  523. * @access public
  524. */
  525. private function getSiteTemplates() {
  526. $db = JFactory::getDBO();
  527. $app = JFactory::getApplication();
  528. $id = 0;
  529. if ($app->isSite()) {
  530. $menus = JSite::getMenu();
  531. $menu = $menus->getActive();
  532. if ($menu) {
  533. $id = isset($menu->template_style_id) ? $menu->template_style_id : $menu->id;
  534. }
  535. }
  536. $query = $db->getQuery(true);
  537. if (is_object($query)) {
  538. $query->select('id, template')->from('#__template_styles')->where(array('client_id = 0', 'home = 1'));
  539. } else {
  540. $query = 'SELECT menuid as id, template' . ' FROM #__templates_menu' . ' WHERE client_id = 0';
  541. }
  542. $db->setQuery($query);
  543. $templates = $db->loadObjectList();
  544. $assigned = array();
  545. foreach ($templates as $template) {
  546. if ($id == $template->id) {
  547. array_unshift($assigned, $template->template);
  548. } else {
  549. $assigned[] = $template->template;
  550. }
  551. }
  552. // return templates
  553. return $assigned;
  554. }
  555. private function getStyleSheets($absolute = false) {
  556. jimport('joomla.filesystem.folder');
  557. jimport('joomla.filesystem.file');
  558. $wf = WFEditor::getInstance();
  559. // use system template as default
  560. $url = 'templates/system/css';
  561. // use 'system' as default
  562. $template = 'system';
  563. // use system editor.css as default
  564. $styles = 'templates/system/css/editor.css';
  565. // stylesheets
  566. $stylesheets = array();
  567. // files
  568. $files = array();
  569. // get templates
  570. $templates = $this->getSiteTemplates();
  571. foreach ($templates as $item) {
  572. // Template CSS
  573. $path = JPATH_SITE . '/templates/' . $item;
  574. // get the first path that exists
  575. if (is_dir($path)) {
  576. // assign template
  577. $template = $item;
  578. // assign url
  579. $url = "templates/" . $template . "/css";
  580. break;
  581. }
  582. }
  583. $global = intval($wf->getParam('editor.content_css', 1));
  584. $profile = intval($wf->getParam('editor.profile_content_css', 2));
  585. switch ($global) {
  586. // Custom template css files
  587. case 0 :
  588. // use getParam so result is cleaned
  589. $global_custom = $wf->getParam('editor.content_css_custom', '');
  590. // Replace $template variable with site template name
  591. $global_custom = str_replace('$template', $template, $global_custom);
  592. // explode to array
  593. $files = explode(',', $global_custom);
  594. break;
  595. // Template css (template.css or template_css.css)
  596. case 1 :
  597. // Joomla! 1.5 standard
  598. $file = 'template.css';
  599. $css = array();
  600. if (JFolder::exists($path)) {
  601. $css = JFolder::files($path, '(base|core|template|template_css)\.css$', false, true);
  602. }
  603. if (!empty($css)) {
  604. // use the first result
  605. $file = $css[0];
  606. }
  607. // check for php version
  608. if (JFile::exists($file . '.php')) {
  609. $file = $file . '.php';
  610. }
  611. $files[] = $url . '/' . basename($file);
  612. break;
  613. // Nothing, use system default
  614. case 2 :
  615. $files[] = 'templates/system/css/editor.css';
  616. break;
  617. }
  618. switch ($profile) {
  619. // add to global config value
  620. case 0 :
  621. case 1 :
  622. $profile_custom = $wf->getParam('editor.profile_content_css_custom', '');
  623. // Replace $template variable with site template name (defaults to 'system')
  624. $profile_custom = str_replace('$template', $template, $profile_custom);
  625. // explode to array
  626. $profile_custom = explode(',', $profile_custom);
  627. // add to existing list
  628. if ($profile == 0) {
  629. $files = array_merge($files, $profile_custom);
  630. // overwrite global config value
  631. } else {
  632. $files = $profile_custom;
  633. }
  634. break;
  635. // inherit global config value
  636. case 2 :
  637. break;
  638. }
  639. // remove duplicates
  640. $files = array_unique($files);
  641. // get the root directory
  642. $root = $absolute ? JPATH_SITE : JURI::root(true);
  643. // check for existence of each file and make array of stylesheets
  644. foreach ($files as $file) {
  645. if ($file && JFile::exists(JPATH_SITE . '/' . $file)) {
  646. $stylesheets[] = $root . '/' . $file;
  647. }
  648. }
  649. // remove duplicates
  650. $stylesheets = array_unique($stylesheets);
  651. // default editor stylesheet
  652. if (count($stylesheets)) {
  653. $styles = implode(',', $stylesheets);
  654. }
  655. return $styles;
  656. }
  657. /**
  658. * Import CSS from a file
  659. * @param $data Data from file
  660. * @param file File path where data comes from
  661. */
  662. private static function importCss($data, $path) {
  663. if (preg_match_all('#@import url\([\'"]?([^\'"\)]+)[\'"]?\);#i', $data, $matches)) {
  664. $fonts = array();
  665. foreach ($matches[1] as $match) {
  666. if (strpos($match, 'http') === false) {
  667. $fonts[] = self::importFontFace(realpath($path . '/' . $match));
  668. }
  669. if (strpos($match, '://fonts.googleapis.com') !== false) {
  670. array_unshift($fonts, '@import url(' . $match . ');');
  671. }
  672. }
  673. return implode("\n", $fonts);
  674. }
  675. return '';
  676. }
  677. private static function importFontFace($file) {
  678. jimport('joomla.filesystem.file');
  679. $content = '';
  680. if (is_file($file)) {
  681. $content .= @JFile::read($file);
  682. }
  683. if ($content) {
  684. // @import
  685. if (strpos($content, '@import') !== false) {
  686. return self::importCss($content, dirname($file));
  687. }
  688. // @font-face
  689. if (strpos($content, '@font-face') !== false) {
  690. $font = '';
  691. preg_match_all('#\@font-face\s*\{([^}]+)\}#', $content, $matches, PREG_SET_ORDER);
  692. if ($matches) {
  693. $url = str_replace('\\', '/', str_replace(JPATH_SITE, JURI::root(true), dirname($file)));
  694. if ($url) {
  695. $url .= '/';
  696. }
  697. foreach ($matches as $match) {
  698. $font .= preg_replace('#url\(([\'"]?)#', 'url($1' . $url, $match[0]);
  699. }
  700. }
  701. return $font;
  702. }
  703. }
  704. return '';
  705. }
  706. private static function getCustomFonts($files) {
  707. $fonts = array();
  708. foreach ((array) $files as $file) {
  709. $font = self::importFontFace($file);
  710. if ($font) {
  711. if (strpos($font, '@import') !== false) {
  712. array_unshift($fonts, $font);
  713. } else {
  714. $fonts[] = $font;
  715. }
  716. }
  717. }
  718. if (!empty($fonts)) {
  719. return "/* @font-face and Google Font rules for JCE */" . "\n" . str_replace("\n\n", "\n", implode("\n", $fonts));
  720. }
  721. return '';
  722. }
  723. private function getURL($relative = false) {
  724. if ($relative) {
  725. return JURI::root(true) . '/components/com_jce/editor';
  726. }
  727. return JURI::root() . 'components/com_jce/editor';
  728. }
  729. /**
  730. * Pack / compress editor files
  731. */
  732. public function pack() {
  733. // check token
  734. WFToken::checkToken('GET') or die('RESTRICTED');
  735. $wf = WFEditor::getInstance();
  736. require_once (JPATH_COMPONENT_ADMINISTRATOR . '/classes/packer.php');
  737. $type = JRequest::getWord('type', 'javascript');
  738. // javascript
  739. $packer = new WFPacker(array('type' => $type));
  740. $themes = 'none';
  741. $plugins = array();
  742. $languages = $wf->getLanguage();
  743. $suffix = JRequest::getWord('suffix', '');
  744. $component_id = JRequest::getInt('component_id', 0);
  745. // if a profile is set
  746. if ($this->profile) {
  747. $themes = 'advanced';
  748. $plugins = $this->getPlugins();
  749. }
  750. $languages = explode(',', $languages);
  751. $themes = explode(',', $themes);
  752. // toolbar theme
  753. $toolbar = explode('.', $wf->getParam('editor.toolbar_theme', 'default'));
  754. switch ($type) {
  755. case 'language' :
  756. $files = array();
  757. if (WF_INI_LANG) {
  758. $data = $this->loadLanguages(array(), array(), '(^dlg$|_dlg$)', true);
  759. $packer->setText($data);
  760. } else {
  761. // Add core languages
  762. foreach ($languages as $language) {
  763. $file = WF_EDITOR . '/' . "tiny_mce/langs/" . $language . ".js";
  764. if (!JFile::exists($file)) {
  765. $file = WF_EDITOR . '/' . "tiny_mce/langs/en.js";
  766. }
  767. $files[] = $file;
  768. }
  769. // Add themes
  770. foreach ($themes as $theme) {
  771. foreach ($languages as $language) {
  772. $file = WF_EDITOR . '/' . "tiny_mce/themes/" . $theme . "/langs/" . $language . ".js";
  773. if (!JFile::exists($file)) {
  774. $file = WF_EDITOR . '/' . "tiny_mce/themes/" . $theme . "/langs/en.js";
  775. }
  776. $files[] = $file;
  777. }
  778. }
  779. // Add plugins
  780. foreach ($plugins as $plugin) {
  781. foreach ($languages as $language) {
  782. $file = WF_EDITOR . '/' . "tiny_mce/plugins/" . $plugin . "/langs/" . $language . ".js";
  783. if (!JFile::exists($file)) {
  784. $file = WF_EDITOR . '/' . "tiny_mce/plugins/" . $plugin . "/langs/en.js";
  785. }
  786. if (JFile::exists($file)) {
  787. $files[] = $file;
  788. }
  789. }
  790. }
  791. // reset type
  792. $type = 'javascript';
  793. }
  794. break;
  795. case 'javascript' :
  796. $files = array();
  797. // add core file
  798. $files[] = WF_EDITOR . '/' . "tiny_mce/tiny_mce" . $suffix . ".js";
  799. if (!WF_INI_LANG) {
  800. // Add core languages
  801. foreach ($languages as $language) {
  802. $file = WF_EDITOR . '/' . "tiny_mce/langs/" . $language . ".js";
  803. if (!JFile::exists($file)) {
  804. $file = WF_EDITOR . '/' . "tiny_mce/langs/en.js";
  805. }
  806. $files[] = $file;
  807. }
  808. }
  809. // Add themes
  810. foreach ($themes as $theme) {
  811. $files[] = WF_EDITOR . '/' . "tiny_mce/themes/" . $theme . "/editor_template" . $suffix . ".js";
  812. if (!WF_INI_LANG) {
  813. foreach ($languages as $language) {
  814. $file = WF_EDITOR . '/' . "tiny_mce/themes/" . $theme . "/langs/" . $language . ".js";
  815. if (!JFile::exists($file)) {
  816. $file = WF_EDITOR . '/' . "tiny_mce/themes/" . $theme . "/langs/en.js";
  817. }
  818. $files[] = $file;
  819. }
  820. }
  821. }
  822. // Add plugins
  823. foreach ($plugins as $plugin) {
  824. $files[] = WF_EDITOR . '/' . "tiny_mce/plugins/" . $plugin . "/editor_plugin" . $suffix . ".js";
  825. if (!WF_INI_LANG) {
  826. foreach ($languages as $language) {
  827. $file = WF_EDITOR . '/' . "tiny_mce/plugins/" . $plugin . "/langs/" . $language . ".js";
  828. if (!JFile::exists($file)) {
  829. $file = WF_EDITOR . '/' . "tiny_mce/plugins/" . $plugin . "/langs/en.js";
  830. }
  831. if (JFile::exists($file)) {
  832. $files[] = $file;
  833. }
  834. }
  835. }
  836. }
  837. // add Editor file
  838. $files[] = WF_EDITOR . '/libraries/js/editor.js';
  839. if (WF_INI_LANG) {
  840. wfimport('admin.classes.language');
  841. $parser = new WFLanguageParser();
  842. $data = $parser->load();
  843. $packer->setContentEnd($data);
  844. }
  845. break;
  846. case 'css' :
  847. $context = JRequest::getWord('context', 'editor');
  848. if ($context == 'content') {
  849. $files = array();
  850. $files[] = WF_EDITOR_THEMES . '/' . $themes[0] . '/skins/' . $toolbar[0] . '/content.css';
  851. // get template stylesheets
  852. $styles = explode(',', $this->getStyleSheets(true));
  853. foreach ($styles as $style) {
  854. if (JFile::exists($style)) {
  855. $files[] = $style;
  856. }
  857. }
  858. // load content styles dor each plugin if they exist
  859. foreach ($plugins as $plugin) {
  860. $content = WF_EDITOR_PLUGINS . '/' . $plugin . '/css/content.css';
  861. if (JFile::exists($content)) {
  862. $files[] = $content;
  863. }
  864. }
  865. } else {
  866. $files = array();
  867. $files[] = WF_EDITOR_LIBRARIES . '/css/editor.css';
  868. $dialog = $wf->getParam('editor.dialog_theme', 'jce');
  869. $files[] = WF_EDITOR_THEMES . '/' . $themes[0] . '/skins/' . $toolbar[0] . '/ui.css';
  870. if (isset($toolbar[1])) {
  871. $files[] = WF_EDITOR_THEMES . '/' . $themes[0] . '/skins/' . $toolbar[0] . '/ui_' . $toolbar[1] . '.css';
  872. }
  873. // get external styles from config class for each plugin
  874. foreach ($plugins as $plugin) {
  875. $class = WF_EDITOR_PLUGINS . '/' . $plugin . '/classes/config.php';
  876. if (JFile::exists($class)) {
  877. require_once ($class);
  878. $classname = 'WF' . ucfirst($plugin) . 'PluginConfig';
  879. if (class_exists($classname) && method_exists(new $classname, 'getStyles')) {
  880. $files = array_merge($files, (array) call_user_func(array($classname, 'getStyles')));
  881. }
  882. }
  883. }
  884. $fonts = trim(self::getCustomFonts($this->getStyleSheets(true)));
  885. if (!empty($fonts)) {
  886. $packer->setContentEnd($fonts);
  887. }
  888. }
  889. break;
  890. }
  891. $packer->setFiles($files);
  892. $packer->pack();
  893. }
  894. public function loadLanguages() {
  895. wfimport('admin.classes.language');
  896. $parser = new WFLanguageParser();
  897. $data = $parser->load();
  898. $parser->output($data);
  899. }
  900. public function getToken($id) {
  901. return '<input type="hidden" id="wf_' . $id . '_token" name="' . WFToken::getToken() . '" value="1" />';
  902. }
  903. }
  904. ?>