PageRenderTime 38ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/administrator/components/com_widgetkit/helpers/system.php

https://bitbucket.org/organicdevelopment/joomla-2.5
PHP | 300 lines | 128 code | 65 blank | 107 comment | 23 complexity | 49b8770c8db06f72c923f7913be71111 MD5 | raw file
Possible License(s): LGPL-3.0, GPL-2.0, MIT, BSD-3-Clause, LGPL-2.1
  1. <?php
  2. /**
  3. * @package Widgetkit
  4. * @author YOOtheme http://www.yootheme.com
  5. * @copyright Copyright (C) YOOtheme GmbH
  6. * @license http://www.gnu.org/licenses/gpl.html GNU/GPL
  7. */
  8. /*
  9. Class: SystemWidgetkitHelper
  10. System helper class
  11. */
  12. class SystemWidgetkitHelper extends WidgetkitHelper {
  13. /* application */
  14. public $application;
  15. /* document */
  16. public $document;
  17. /* language */
  18. public $language;
  19. /* system path */
  20. public $path;
  21. /* system url */
  22. public $url;
  23. /* options */
  24. public $options;
  25. /* cache path */
  26. public $cache_path;
  27. /* CSRF token */
  28. public $token;
  29. /*
  30. Function: Constructor
  31. Class Constructor.
  32. */
  33. public function __construct($widgetkit) {
  34. parent::__construct($widgetkit);
  35. // init vars
  36. $this->application = JFactory::getApplication();
  37. $this->document = JFactory::getDocument();
  38. $this->language = JFactory::getLanguage();
  39. $this->path = JPATH_ROOT;
  40. $this->url = rtrim(JURI::root(false), '/');
  41. $this->options = $this['data']->create($this->_getParams());
  42. $this->cache_path = $this->path.'/cache/widgetkit';
  43. // set cache directory
  44. if (!file_exists($this->cache_path)) {
  45. JFolder::create($this->cache_path);
  46. }
  47. }
  48. /*
  49. Function: init
  50. Initialize system
  51. Returns:
  52. Void
  53. */
  54. public function init() {
  55. // set translations
  56. $this->language->load('widgetkit', $this['path']->path('widgetkit:'), null, true);
  57. // set paths
  58. $this['path']->register($this->path, 'site');
  59. $this['path']->register($this->path.'/media/widgetkit', 'widgetkit');
  60. $this['path']->register($this->path.'/media/widgetkit/widgets', 'widgets');
  61. $this['path']->register($this->path.'/modules', 'modules');
  62. $this['path']->register($this->path.'/'.JComponentHelper::getParams('com_media')->get('file_path'), 'media');
  63. $this['path']->register($this->cache_path, 'cache');
  64. // load widgets
  65. foreach ($this['path']->dirs('widgets:') as $name) {
  66. if ($file = $this['path']->path("widgets:{$name}/{$name}.php")) {
  67. require_once($file);
  68. }
  69. }
  70. // is admin ?
  71. if ($this->application->isAdmin() && $this['request']->get('option', 'string') == 'com_widgetkit') {
  72. // get token
  73. $this->token = JFactory::getSession()->getFormToken();
  74. // cache writable ?
  75. if (!file_exists($this->cache_path) || !is_writable($this->cache_path)) {
  76. $this->application->enqueueMessage("Widgetkit cache folder is not writable! Please check directory permissions ({$this->cache_path})", 'notice');
  77. }
  78. // load editor
  79. $this['editor']->init();
  80. // add stylesheets/javascripts
  81. $this['asset']->addFile('css', 'widgetkit:css/admin.css');
  82. $this['asset']->addFile('css', 'widgetkit:css/system.css');
  83. $this['asset']->addFile('js', 'widgetkit:js/jquery.ui.js');
  84. $this['asset']->addFile('js', 'widgetkit:js/jquery.plugins.js');
  85. $this['asset']->addFile('js', 'widgetkit:js/admin.js');
  86. $this['asset']->addString('js', 'var widgetkitajax = "'.$this['system']->link(array('ajax' => true)).'";');
  87. // get request vars
  88. $task = $this['request']->get('task', 'string');
  89. // get version
  90. $this["version"] = ($path = $this['path']->path('widgetkit:widgetkit.xml')) && ($xml = simplexml_load_file($path)) ? (string) $xml->version[0] : '';
  91. // trigger event
  92. $this['event']->trigger('admin');
  93. // execute task
  94. echo $this['template']->render($task ? 'task' : 'dashboard', compact('task'));
  95. // add assets
  96. $this['template']->render('assets');
  97. // check for updates
  98. if ($xmlpath = $this['path']->path('widgetkit:widgetkit.xml')) {
  99. $xml = $this['dom']->create($xmlpath, 'xml');
  100. // update check
  101. if ($url = $xml->first('updateUrl')->text()) {
  102. // create check url
  103. $url = sprintf('%s?application=%s&version=%s&format=raw', $url, 'widgetkit_j17', urlencode($xml->first('version')->text()));
  104. // only check once a day
  105. $hash = md5($url.date('Y-m-d'));
  106. if ($this['option']->get("update_check") != $hash) {
  107. if ($request = $this['http']->get($url)) {
  108. $this['option']->set("update_check", $hash);
  109. $this['option']->set("update_data", $request['body']);
  110. }
  111. }
  112. // decode response and set message
  113. if (($data = json_decode($this['option']->get("update_data"))) && $data->status == 'update-available') {
  114. $this->application->enqueueMessage($data->message, 'notice');
  115. }
  116. }
  117. }
  118. }
  119. // is site ?
  120. if ($this->application->isSite() && is_a($this->document, 'JDocumentHTML')) {
  121. $this['asset']->addString("js", 'window["WIDGETKIT_URL"]="'.$this['path']->url("widgetkit:").'";');
  122. $this['asset']->addString("js", 'function wk_ajax_render_url(widgetid){ return "'.JRoute::_("index.php?option=com_widgetkit&tmpl=raw&id=").'"+widgetid}');
  123. // set direction
  124. $this->options->set('direction', $this->document->direction);
  125. // add stylesheets/javascripts
  126. $this['asset']->addFile('css', 'widgetkit:css/widgetkit.css');
  127. $this['asset']->addFile('js', 'widgetkit:js/jquery.plugins.js');
  128. $this['asset']->addFile('js', 'widgetkit:js/responsive.js');
  129. if ($this->options->get('direction') == 'rtl') {
  130. $this['asset']->addFile('css', 'widgetkit:css/rtl.css');
  131. }
  132. // trigger event
  133. $this['event']->trigger('site');
  134. // add assets
  135. $this['template']->render('assets');
  136. $this['event']->bind('widgetoutput', array($this,"_applycontentplugins"));
  137. }
  138. }
  139. /*
  140. Function: link
  141. Get link to system related resources.
  142. Parameters:
  143. $query - HTTP query options
  144. Returns:
  145. String
  146. */
  147. public function link($query = array()) {
  148. // build query
  149. $query = array_merge(array('option' => $this['request']->get('option', 'string')), $query);
  150. if (isset($query['ajax'])) {
  151. $query = array_merge(array('format' => 'raw'), $query);
  152. }
  153. return $this->url.'/administrator/index.php?'.http_build_query($query, '', '&');
  154. }
  155. /*
  156. Function: saveOptions
  157. Save plugin options
  158. Returns:
  159. Void
  160. */
  161. public function saveOptions() {
  162. $this->_setParams((string) $this->options);
  163. }
  164. /*
  165. Function: checkToken
  166. Checks the CSRF token
  167. Returns:
  168. Boolean
  169. */
  170. public function checkToken($token){
  171. return $this->token == $token;
  172. }
  173. /*
  174. Function: __
  175. Retrieve translated strings
  176. Returns:
  177. String
  178. */
  179. public function __($string) {
  180. return JText::_($string);
  181. }
  182. /*
  183. Function: _getParams
  184. Get parameter from database
  185. Returns:
  186. String
  187. */
  188. protected function _getParams() {
  189. $db = JFactory::getDBO();
  190. $db->setQuery("SELECT params FROM #__extensions AS e WHERE e.element='com_widgetkit'");
  191. return $db->loadResult();
  192. }
  193. /*
  194. Function: _saveParams
  195. Set parameter in database
  196. Returns:
  197. Boolean
  198. */
  199. protected function _setParams($params) {
  200. $db = JFactory::getDBO();
  201. $db->setQuery(sprintf("UPDATE #__extensions AS e SET e.params='%s' WHERE e.element='com_widgetkit'", $db->getEscaped($params)));
  202. return $db->query();
  203. }
  204. /*
  205. Function: _applycontentplugins
  206. Apply content plugins
  207. Returns:
  208. Void
  209. */
  210. public function _applycontentplugins(&$text) {
  211. jimport('joomla.html.parameter');
  212. if (!class_exists("plgContentWidgetkit_Content")) {
  213. JPluginHelper::importPlugin('content');
  214. }
  215. $params = new JParameter();
  216. $article = new stdClass();
  217. $dispatcher = JDispatcher::getInstance();
  218. $wkplugin = new plgContentWidgetkit_Content($dispatcher);
  219. $posplugin = new plgContentLoadmodule($dispatcher);
  220. $article->text = $text;
  221. $posplugin->params = $params;
  222. $posplugin->onContentPrepare('widgetkit', $article, $params, 0);
  223. $wkplugin->onContentPrepare('widgetkit', $article, $params, 0);
  224. $text = $article->text;
  225. }
  226. }