PageRenderTime 140ms CodeModel.GetById 35ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://github.com/mblodau/GMTJ
PHP | 583 lines | 322 code | 83 blank | 178 comment | 28 complexity | 923726b78dd3e077e443e3ba795f6899 MD5 | raw file
  1. <?php
  2. /**
  3. * @version $Id: document.php 221 2011-06-11 17:30:33Z happy_noodle_boy $
  4. * @package JCE
  5. * @copyright Copyright (C) 2005 - 2009 Ryan Demmer. All rights reserved.
  6. * @author Ryan Demmer
  7. * @license GNU/GPL
  8. * JCE is free software. This version may have been modified pursuant
  9. * to the GNU General Public License, and as distributed it includes or
  10. * is derivative of works licensed under the GNU General Public License or
  11. * other free or open source software licenses.
  12. */
  13. // no direct access
  14. defined('_JEXEC') or die('ERROR_403');
  15. class WFDocument extends JObject
  16. {
  17. /**
  18. * Array of linked scripts
  19. *
  20. * @var array
  21. * @access private
  22. */
  23. var $_scripts = array();
  24. /**
  25. * Array of scripts placed in the header
  26. *
  27. * @var array
  28. * @access private
  29. */
  30. var $_script = array();
  31. /**
  32. * Array of linked style sheets
  33. *
  34. * @var array
  35. * @access private
  36. */
  37. var $_styles = array();
  38. /**
  39. * Array of head items
  40. *
  41. * @var array
  42. * @access private
  43. */
  44. var $_head = array();
  45. /**
  46. * Body content
  47. *
  48. * @var array
  49. * @access private
  50. */
  51. var $_body = '';
  52. /**
  53. * Document title
  54. *
  55. * @var string
  56. * @access public
  57. */
  58. var $title = '';
  59. /**
  60. * Document version
  61. *
  62. * @var string
  63. * @access public
  64. */
  65. var $version = '000000';
  66. /**
  67. * Constructor activating the default information of the class
  68. *
  69. * @access protected
  70. */
  71. function __construct($config = array())
  72. {
  73. parent::__construct();
  74. // set document title
  75. if (isset($config['title'])) {
  76. $this->setTitle($config['title']);
  77. }
  78. $this->setProperties($config);
  79. }
  80. /**
  81. * Returns a reference to a plugin object
  82. *
  83. * This method must be invoked as:
  84. * <pre> $advlink =AdvLink::getInstance();</pre>
  85. *
  86. * @access public
  87. * @return JCE The editor object.
  88. * @since 1.5
  89. */
  90. function &getInstance($config = array())
  91. {
  92. static $instance;
  93. if (!is_object($instance)) {
  94. $instance = new WFDocument($config);
  95. }
  96. return $instance;
  97. }
  98. function setTitle($title)
  99. {
  100. $this->title = $title;
  101. }
  102. function getTitle()
  103. {
  104. return $this->title;
  105. }
  106. function setName($name)
  107. {
  108. $this->name = $name;
  109. }
  110. function getName()
  111. {
  112. return $this->name;
  113. }
  114. function getURL($relative = false)
  115. {
  116. if ($relative) {
  117. return JURI::root(true) . '/components/com_jce/editor';
  118. }
  119. return JURI::root() . 'components/com_jce/editor';
  120. }
  121. /**
  122. * Return the curernt language code
  123. *
  124. * @access public
  125. * @return language code
  126. */
  127. function getLanguageDir()
  128. {
  129. $language = JFactory::getLanguage();
  130. return $language->isRTL() ? 'rtl' : 'ltr';
  131. }
  132. /**
  133. * Return the curernt language code
  134. *
  135. * @access public
  136. * @return language code
  137. */
  138. function getLanguageTag()
  139. {
  140. $language = JFactory::getLanguage();
  141. if ($language->isRTL()) {
  142. return 'en-GB';
  143. }
  144. return $language->getTag();
  145. }
  146. /**
  147. * Return the curernt language code
  148. *
  149. * @access public
  150. * @return language code
  151. */
  152. function getLanguage()
  153. {
  154. $tag = $this->getLanguageTag();
  155. if (file_exists(JPATH_SITE .DS. 'language' .DS. $tag .DS. $tag .'.com_jce.xml')) {
  156. return substr($tag, 0, strpos($tag, '-'));
  157. }
  158. return 'en';
  159. }
  160. /**
  161. * Returns a JCE resource url
  162. *
  163. * @access public
  164. * @param string The path to resolve eg: libaries
  165. * @param boolean Create a relative url
  166. * @return full url
  167. * @since 1.5
  168. */
  169. function getBaseURL($path, $type = '')
  170. {
  171. static $url;
  172. if (!isset($url)) {
  173. $url = array();
  174. }
  175. $signature = serialize(array($type, $path));
  176. // Check if value is already stored
  177. if (!isset($url[$signature])) {
  178. // get the plugin name using this document instance
  179. $plugin = $this->get('name');
  180. $base = $this->getURL(true) . '/';
  181. $parts = explode('.', $path);
  182. $path = array_shift($parts);
  183. switch ($path) {
  184. // JCE root folder
  185. case 'jce':
  186. $pre = $base . '';
  187. break;
  188. // JCE libraries resource folder
  189. default:
  190. case 'libraries':
  191. $pre = $base . 'libraries/' . $type;
  192. break;
  193. // TinyMCE folder
  194. case 'tiny_mce':
  195. $pre = $base . 'tiny_mce';
  196. break;
  197. // JCE current plugin folder
  198. case 'plugins':
  199. $pre = $base . 'tiny_mce/plugins/' . $plugin . '/' . $type;
  200. break;
  201. // Extensions folder
  202. case 'extensions':
  203. $pre = $base . 'extensions';
  204. break;
  205. case 'joomla':
  206. return JURI::root(true);
  207. break;
  208. case 'media':
  209. return JURI::root(true) . '/media/system';
  210. break;
  211. case 'component':
  212. $pre = JURI::root(true) . '/administrator/components/com_jce/media/' . $type;
  213. break;
  214. default:
  215. $pre = $base . $path;
  216. break;
  217. }
  218. if (count($parts)) {
  219. $pre = rtrim($pre, '/') . '/' . implode('/', $parts);
  220. }
  221. // Store url
  222. $url[$signature] = $pre;
  223. }
  224. return $url[$signature];
  225. }
  226. /**
  227. * Convert a url to path
  228. *
  229. * @access public
  230. * @param string The url to convert
  231. * @return Full path to file
  232. * @since 1.5
  233. */
  234. function urlToPath($url)
  235. {
  236. jimport('joomla.filesystem.path');
  237. $bool = strpos($url, JURI::root()) === false;
  238. return WFUtility::makePath(JPATH_SITE, JPath::clean(str_replace(JURI::root($bool), '', $url)));
  239. }
  240. /**
  241. * Returns an image url
  242. *
  243. * @access public
  244. * @param string The file to load including path and extension eg: libaries.image.gif
  245. * @return Image url
  246. * @since 1.5
  247. */
  248. function image($image, $root = 'libraries')
  249. {
  250. $parts = explode('.', $image);
  251. $parts = preg_replace('#[^A-Z0-9-_]#i', '', $parts);
  252. $ext = array_pop($parts);
  253. $name = trim(array_pop($parts), '/');
  254. $parts[] = 'img';
  255. $parts[] = $name . "." . $ext;
  256. return $this->getBaseURL($root) . implode('/', $parts);
  257. }
  258. function removeScript($file, $root = 'libraries')
  259. {
  260. $file = $this->buildScriptPath($file, $root);
  261. unset($this->_scripts[$file]);
  262. }
  263. function removeCss($file, $root = 'libraries')
  264. {
  265. $file = $this->buildStylePath($file, $root);
  266. unset($this->_styles[$file]);
  267. }
  268. function buildScriptPath($file, $root)
  269. {
  270. $file = preg_replace('#[^A-Z0-9-_\/\.]#i', '', $file);
  271. // get base dir
  272. $base = dirname($file);
  273. // remove extension if present
  274. $file = basename($file, '.js');
  275. // strip . and trailing /
  276. $file = trim(trim($base, '.'), '/') . '/' . $file . '.js';
  277. // remove leading and trailing slashes
  278. $file = trim($file, '/');
  279. // create path
  280. $file = $this->getBaseURL($root, 'js') . '/' . $file;
  281. return $file;
  282. }
  283. function buildStylePath($file, $root)
  284. {
  285. $file = preg_replace('#[^A-Z0-9-_\/\.]#i', '', $file);
  286. // get base dir
  287. $base = dirname($file);
  288. // remove extension if present
  289. $file = basename($file, '.css');
  290. // strip . and trailing /
  291. $file = trim(trim($base, '.'), '/') . '/' . $file . '.css';
  292. // remove leading and trailing slashes
  293. $file = trim($file, '/');
  294. // create path
  295. $file = $this->getBaseURL($root, 'css') . '/' . $file;
  296. return $file;
  297. }
  298. /**
  299. * Loads a javascript file
  300. *
  301. * @access public
  302. * @param string The file to load including path eg: libaries.manager
  303. * @param boolean Debug mode load src file
  304. * @return echo script html
  305. * @since 1.5
  306. */
  307. function addScript($files, $root = 'libraries', $type = 'text/javascript')
  308. {
  309. $files = (array) $files;
  310. foreach ($files as $file) {
  311. // external link
  312. if (preg_match('#^(http:)?\/\/#i', $file)) {
  313. $this->_scripts[$file] = $type;
  314. } else {
  315. $file = $this->buildScriptPath($file, $root);
  316. // store path
  317. $this->_scripts[$file] = $type;
  318. }
  319. }
  320. }
  321. /**
  322. * Loads a css file
  323. *
  324. * @access public
  325. * @param string The file to load including path eg: libaries.manager
  326. * @param string Root folder
  327. * @return echo css html
  328. * @since 1.5
  329. */
  330. function addStyleSheet($files, $root = 'libraries', $type = 'text/css')
  331. {
  332. $files = (array) $files;
  333. jimport('joomla.environment.browser');
  334. $browser = JBrowser::getInstance();
  335. foreach ($files as $file) {
  336. $url = $this->buildStylePath($file, $root);
  337. // store path
  338. $this->_styles[$url] = $type;
  339. if ($browser->getBrowser() == 'msie') {
  340. // All versions
  341. $file = $file . '_ie.css';
  342. $path = $this->urlToPath($url);
  343. if (file_exists(dirname($path) . DS . $file)) {
  344. $this->_styles[dirname($url) . '/' . $file] = $type;
  345. }
  346. }
  347. }
  348. }
  349. function addScriptDeclaration($content, $type = 'text/javascript')
  350. {
  351. if (!isset($this->_script[strtolower($type)])) {
  352. $this->_script[strtolower($type)] = $content;
  353. } else {
  354. $this->_script[strtolower($type)] .= chr(13) . $content;
  355. }
  356. }
  357. function getScripts()
  358. {
  359. return $this->_scripts;
  360. }
  361. function getStyleSheets()
  362. {
  363. return $this->_styles;
  364. }
  365. /**
  366. * Setup head data
  367. *
  368. * @access public
  369. * @since 1.5
  370. */
  371. function setHead($data)
  372. {
  373. if (is_array($data)) {
  374. $this->_head = array_merge($this->_head, $data);
  375. } else {
  376. $this->_head[] = $data;
  377. }
  378. }
  379. /**
  380. * Render document head data
  381. */
  382. private function getHead()
  383. {
  384. $output = '';
  385. $version = $this->get('version', '000000');
  386. $output .= '<title>' . $this->getTitle() . ' : ' . $version . '</title>' . "\n";
  387. $layout = JRequest::getWord('layout');
  388. $item = JRequest::getWord($layout);
  389. $standalone = '';
  390. $dialog = '';
  391. if (JRequest::getWord('dialog')) {
  392. $dialog = '&dialog=' . JRequest::getWord('dialog');
  393. }
  394. // set standalone mode (for File Browser etc)
  395. if ($this->get('standalone') == 1) {
  396. $standalone = '&standalone=1';
  397. }
  398. // Render scripts
  399. $stamp = preg_match('/\d+/', $version) ? '?version=' . $version : '';
  400. // get token
  401. $token = WFToken::getToken();
  402. if ($this->get('compress_javascript', 0)) {
  403. $script = JURI::base(true) . '/index.php?option=com_jce&view=editor&layout='.$layout.'&'.$layout.'=' . $item . $dialog . $standalone . '&task=pack&' . $token . '=1';
  404. $output .= "\t\t<script type=\"text/javascript\" src=\"" . $script . "\"></script>\n";
  405. } else {
  406. foreach ($this->_scripts as $src => $type) {
  407. $output .= "\t\t<script type=\"" . $type . "\" src=\"" . $src . $stamp . "\"></script>\n";
  408. }
  409. }
  410. if ($this->get('compress_css', 0)) {
  411. $file = JURI::base(true) . '/index.php?option=com_jce&view=editor&layout='.$layout.'&'.$layout.'=' . $item . $dialog . $standalone . '&task=pack&type=css&' . $token . '=1';
  412. $output .= "\t\t<link href=\"" . $file . "\" rel=\"stylesheet\" type=\"text/css\" />\n";
  413. } else {
  414. foreach ($this->_styles as $k => $v) {
  415. $output .= "\t\t<link href=\"" . $k . $stamp . "\" rel=\"stylesheet\" type=\"" . $v . "\" />\n";
  416. }
  417. }
  418. // Script declarations
  419. foreach ($this->_script as $type => $content) {
  420. $output .= "\t\t<script type=\"" . $type . "\">" . $content . "</script>";
  421. }
  422. // Other head data
  423. foreach ($this->_head as $head) {
  424. $output .= "\t" . $head . "\n";
  425. }
  426. return $output;
  427. }
  428. public function setBody($data = '')
  429. {
  430. $this->_body = $data;
  431. }
  432. private function getBody()
  433. {
  434. return $this->_body;
  435. }
  436. private function loadData()
  437. {
  438. //get the file content
  439. ob_start();
  440. require_once(WF_EDITOR_LIBRARIES .DS. 'views' .DS. 'plugin' .DS. 'index.php');
  441. $data = ob_get_contents();
  442. ob_end_clean();
  443. return $data;
  444. }
  445. /**
  446. * Render the document
  447. */
  448. public function render()
  449. {
  450. // assign language
  451. $this->language = $this->getLanguage();
  452. $this->direction = $this->getLanguageDir();
  453. // load template data
  454. $output = $this->loadData();
  455. $output = $this->parseData($output);
  456. exit($output);
  457. }
  458. private function parseData($data)
  459. {
  460. $data = preg_replace_callback('#<!-- \[head\] -->#', array($this, 'getHead'), $data);
  461. $data = preg_replace_callback('#<!-- \[body\] -->#', array($this, 'getBody'), $data);
  462. return $data;
  463. }
  464. /**
  465. * pack function for plugins
  466. */
  467. public function pack($minify = true, $gzip = false)
  468. {
  469. if (JRequest::getCmd('task') == 'pack') {
  470. // check token
  471. WFToken::checkToken('GET') or die('RESTRICTED');
  472. wfimport('admin.classes.packer');
  473. $component = WFExtensionHelper::getComponent();
  474. $params = new WFParameter($component->params);
  475. $type = JRequest::getWord('type', 'javascript');
  476. // javascript
  477. $packer = new WFPacker(array(
  478. 'type' => $type
  479. ));
  480. $files = array();
  481. switch ($type) {
  482. case 'javascript':
  483. foreach ($this->getScripts() as $script => $type) {
  484. $script .= preg_match('/\.js$/', $script) ? '' : '.js';
  485. $files[] = $this->urlToPath($script);
  486. }
  487. break;
  488. case 'css':
  489. foreach ($this->getStyleSheets() as $style => $type) {
  490. $style .= preg_match('/\.css$/', $style) ? '' : '.css';
  491. $files[] = $this->urlToPath($style);
  492. }
  493. break;
  494. }
  495. $packer->setFiles($files);
  496. $packer->pack($minify, $gzip);
  497. }
  498. }
  499. }
  500. ?>