PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/application/libraries/joomla/application/module/helper.php

https://github.com/nikosdion/Akeeba-Example
PHP | 515 lines | 321 code | 73 blank | 121 comment | 52 complexity | 452d6ab772c9392e8e20b9d807a9b7f1 MD5 | raw file
  1. <?php
  2. /**
  3. * @package Joomla.Platform
  4. * @subpackage Application
  5. *
  6. * @copyright Copyright (C) 2005 - 2011 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE
  8. */
  9. defined('JPATH_PLATFORM') or die();
  10. jimport('joomla.application.component.helper');
  11. /**
  12. * Module helper class
  13. *
  14. * @package Joomla.Platform
  15. * @subpackage Application
  16. * @since 11.1
  17. */
  18. abstract class JModuleHelper
  19. {
  20. /**
  21. * Get module by name (real, eg 'Breadcrumbs' or folder, eg 'mod_breadcrumbs')
  22. *
  23. * @param string $name The name of the module
  24. * @param string $title The title of the module, optional
  25. *
  26. * @return object The Module object
  27. *
  28. * @since 11.1
  29. */
  30. public static function &getModule($name, $title = null)
  31. {
  32. $result = null;
  33. $modules = JModuleHelper::_load();
  34. $total = count($modules);
  35. for ($i = 0; $i < $total; $i++)
  36. {
  37. // Match the name of the module
  38. if ($modules[$i]->name == $name || $modules[$i]->module == $name)
  39. {
  40. // Match the title if we're looking for a specific instance of the module
  41. if (!$title || $modules[$i]->title == $title)
  42. {
  43. // Found it
  44. $result = &$modules[$i];
  45. break; // Found it
  46. }
  47. }
  48. }
  49. // If we didn't find it, and the name is mod_something, create a dummy object
  50. if (is_null($result) && substr($name, 0, 4) == 'mod_')
  51. {
  52. $result = new stdClass();
  53. $result->id = 0;
  54. $result->title = '';
  55. $result->module = $name;
  56. $result->position = '';
  57. $result->content = '';
  58. $result->showtitle = 0;
  59. $result->control = '';
  60. $result->params = '';
  61. $result->user = 0;
  62. }
  63. return $result;
  64. }
  65. /**
  66. * Get modules by position
  67. *
  68. * @param string $position The position of the module
  69. *
  70. * @return array An array of module objects
  71. *
  72. * @since 11.1
  73. */
  74. public static function &getModules($position)
  75. {
  76. $app = JFactory::getApplication();
  77. $position = strtolower($position);
  78. $result = array();
  79. $modules = JModuleHelper::_load();
  80. $total = count($modules);
  81. for ($i = 0; $i < $total; $i++)
  82. {
  83. if ($modules[$i]->position == $position)
  84. {
  85. $result[] = &$modules[$i];
  86. }
  87. }
  88. if (count($result) == 0)
  89. {
  90. if (JRequest::getBool('tp') && JComponentHelper::getParams('com_templates')->get('template_positions_display'))
  91. {
  92. $result[0] = JModuleHelper::getModule('mod_' . $position);
  93. $result[0]->title = $position;
  94. $result[0]->content = $position;
  95. $result[0]->position = $position;
  96. }
  97. }
  98. return $result;
  99. }
  100. /**
  101. * Checks if a module is enabled
  102. *
  103. * @param string $module The module name
  104. *
  105. * @return boolean
  106. *
  107. * @since 11.1
  108. */
  109. public static function isEnabled($module)
  110. {
  111. $result = JModuleHelper::getModule($module);
  112. return !is_null($result);
  113. }
  114. /**
  115. * Render the module.
  116. *
  117. * @param object $module A module object.
  118. * @param array $attribs An array of attributes for the module (probably from the XML).
  119. *
  120. * @return string The HTML content of the module output.
  121. *
  122. * @since 11.1
  123. */
  124. public static function renderModule($module, $attribs = array())
  125. {
  126. static $chrome;
  127. if (constant('JDEBUG'))
  128. {
  129. JProfiler::getInstance('Application')->mark('beforeRenderModule ' . $module->module . ' (' . $module->title . ')');
  130. }
  131. $option = JRequest::getCmd('option');
  132. $app = JFactory::getApplication();
  133. // Record the scope.
  134. $scope = $app->scope;
  135. // Set scope to component name
  136. $app->scope = $module->module;
  137. // Get module parameters
  138. $params = new JRegistry();
  139. $params->loadString($module->params);
  140. // Get module path
  141. $module->module = preg_replace('/[^A-Z0-9_\.-]/i', '', $module->module);
  142. $path = JPATH_BASE . '/modules/' . $module->module . '/' . $module->module . '.php';
  143. // Load the module
  144. if (!$module->user && file_exists($path))
  145. {
  146. $lang = JFactory::getLanguage();
  147. // 1.5 or Core then 1.6 3PD
  148. $lang->load($module->module, JPATH_BASE, null, false, false) ||
  149. $lang->load($module->module, dirname($path), null, false, false) ||
  150. $lang->load($module->module, JPATH_BASE, $lang->getDefault(), false, false) ||
  151. $lang->load($module->module, dirname($path), $lang->getDefault(), false, false);
  152. $content = '';
  153. ob_start();
  154. include $path;
  155. $module->content = ob_get_contents() . $content;
  156. ob_end_clean();
  157. }
  158. // Load the module chrome functions
  159. if (!$chrome)
  160. {
  161. $chrome = array();
  162. }
  163. include_once JPATH_THEMES . '/system/html/modules.php';
  164. $chromePath = JPATH_THEMES . '/' . $app->getTemplate() . '/html/modules.php';
  165. if (!isset($chrome[$chromePath]))
  166. {
  167. if (file_exists($chromePath))
  168. {
  169. include_once $chromePath;
  170. }
  171. $chrome[$chromePath] = true;
  172. }
  173. // Make sure a style is set
  174. if (!isset($attribs['style']))
  175. {
  176. $attribs['style'] = 'none';
  177. }
  178. // Dynamically add outline style
  179. if (JRequest::getBool('tp') && JComponentHelper::getParams('com_templates')->get('template_positions_display'))
  180. {
  181. $attribs['style'] .= ' outline';
  182. }
  183. foreach (explode(' ', $attribs['style']) as $style)
  184. {
  185. $chromeMethod = 'modChrome_' . $style;
  186. // Apply chrome and render module
  187. if (function_exists($chromeMethod))
  188. {
  189. $module->style = $attribs['style'];
  190. ob_start();
  191. $chromeMethod($module, $params, $attribs);
  192. $module->content = ob_get_contents();
  193. ob_end_clean();
  194. }
  195. }
  196. //revert the scope
  197. $app->scope = $scope;
  198. if (constant('JDEBUG'))
  199. {
  200. JProfiler::getInstance('Application')->mark('afterRenderModule ' . $module->module . ' (' . $module->title . ')');
  201. }
  202. return $module->content;
  203. }
  204. /**
  205. * Get the path to a layout for a module
  206. *
  207. * @param string $module The name of the module
  208. * @param string $layout The name of the module layout. If alternative layout, in the form template:filename.
  209. *
  210. * @return string The path to the module layout
  211. *
  212. * @since 11.1
  213. */
  214. public static function getLayoutPath($module, $layout = 'default')
  215. {
  216. $template = JFactory::getApplication()->getTemplate();
  217. $defaultLayout = $layout;
  218. if (strpos($layout, ':') !== false)
  219. {
  220. // Get the template and file name from the string
  221. $temp = explode(':', $layout);
  222. $template = ($temp[0] == '_') ? $template : $temp[0];
  223. $layout = $temp[1];
  224. $defaultLayout = ($temp[1]) ? $temp[1] : 'default';
  225. }
  226. // Build the template and base path for the layout
  227. $tPath = JPATH_THEMES . '/' . $template . '/html/' . $module . '/' . $layout . '.php';
  228. $bPath = JPATH_BASE . '/modules/' . $module . '/tmpl/' . $defaultLayout . '.php';
  229. // If the template has a layout override use it
  230. if (file_exists($tPath))
  231. {
  232. return $tPath;
  233. }
  234. else
  235. {
  236. return $bPath;
  237. }
  238. }
  239. /**
  240. * Load published modules.
  241. *
  242. * @return array
  243. *
  244. * @since 11.1
  245. */
  246. protected static function &_load()
  247. {
  248. static $clean;
  249. if (isset($clean))
  250. {
  251. return $clean;
  252. }
  253. $Itemid = JRequest::getInt('Itemid');
  254. $app = JFactory::getApplication();
  255. $user = JFactory::getUser();
  256. $groups = implode(',', $user->getAuthorisedViewLevels());
  257. $lang = JFactory::getLanguage()->getTag();
  258. $clientId = (int) $app->getClientId();
  259. $cache = JFactory::getCache('com_modules', '');
  260. $cacheid = md5(serialize(array($Itemid, $groups, $clientId, $lang)));
  261. if (!($clean = $cache->get($cacheid)))
  262. {
  263. $db = JFactory::getDbo();
  264. $query = $db->getQuery(true);
  265. $query->select('m.id, m.title, m.module, m.position, m.content, m.showtitle, m.params, mm.menuid');
  266. $query->from('#__modules AS m');
  267. $query->join('LEFT', '#__modules_menu AS mm ON mm.moduleid = m.id');
  268. $query->where('m.published = 1');
  269. $query->join('LEFT', '#__extensions AS e ON e.element = m.module AND e.client_id = m.client_id');
  270. $query->where('e.enabled = 1');
  271. $date = JFactory::getDate();
  272. $now = $date->toMySQL();
  273. $nullDate = $db->getNullDate();
  274. $query->where('(m.publish_up = ' . $db->Quote($nullDate) . ' OR m.publish_up <= ' . $db->Quote($now) . ')');
  275. $query->where('(m.publish_down = ' . $db->Quote($nullDate) . ' OR m.publish_down >= ' . $db->Quote($now) . ')');
  276. $query->where('m.access IN (' . $groups . ')');
  277. $query->where('m.client_id = ' . $clientId);
  278. $query->where('(mm.menuid = ' . (int) $Itemid . ' OR mm.menuid <= 0)');
  279. // Filter by language
  280. if ($app->isSite() && $app->getLanguageFilter())
  281. {
  282. $query->where('m.language IN (' . $db->Quote($lang) . ',' . $db->Quote('*') . ')');
  283. }
  284. $query->order('m.position, m.ordering');
  285. // Set the query
  286. $db->setQuery($query);
  287. $modules = $db->loadObjectList();
  288. $clean = array();
  289. if ($db->getErrorNum())
  290. {
  291. JError::raiseWarning(500, JText::sprintf('JLIB_APPLICATION_ERROR_MODULE_LOAD', $db->getErrorMsg()));
  292. return $clean;
  293. }
  294. // Apply negative selections and eliminate duplicates
  295. $negId = $Itemid ? -(int) $Itemid : false;
  296. $dupes = array();
  297. for ($i = 0, $n = count($modules); $i < $n; $i++)
  298. {
  299. $module = &$modules[$i];
  300. // The module is excluded if there is an explicit prohibition or if
  301. // the Itemid is missing or zero and the module is in exclude mode.
  302. $negHit = ($negId === (int) $module->menuid) || (!$negId && (int) $module->menuid < 0);
  303. if (isset($dupes[$module->id]))
  304. {
  305. // If this item has been excluded, keep the duplicate flag set,
  306. // but remove any item from the cleaned array.
  307. if ($negHit)
  308. {
  309. unset($clean[$module->id]);
  310. }
  311. continue;
  312. }
  313. $dupes[$module->id] = true;
  314. // Only accept modules without explicit exclusions.
  315. if (!$negHit)
  316. {
  317. $module->name = substr($module->module, 4);
  318. $module->style = null;
  319. $module->position = strtolower($module->position);
  320. $clean[$module->id] = $module;
  321. }
  322. }
  323. unset($dupes);
  324. // Return to simple indexing that matches the query order.
  325. $clean = array_values($clean);
  326. $cache->store($clean, $cacheid);
  327. }
  328. return $clean;
  329. }
  330. /**
  331. * Module cache helper
  332. *
  333. * Caching modes:
  334. * To be set in XML:
  335. * 'static' One cache file for all pages with the same module parameters
  336. * 'oldstatic' 1.5 definition of module caching, one cache file for all pages
  337. * with the same module id and user aid,
  338. * 'itemid' Changes on itemid change, to be called from inside the module:
  339. * 'safeuri' Id created from $cacheparams->modeparams array,
  340. * 'id' Module sets own cache id's
  341. *
  342. * @param object $module Module object
  343. * @param object $moduleparams Module parameters
  344. * @param object $cacheparams Module cache parameters - id or url parameters, depending on the module cache mode
  345. *
  346. * @return string
  347. *
  348. * @since 11.1
  349. *
  350. * @link JFilterInput::clean()
  351. */
  352. public static function moduleCache($module, $moduleparams, $cacheparams)
  353. {
  354. if (!isset($cacheparams->modeparams))
  355. {
  356. $cacheparams->modeparams = null;
  357. }
  358. if (!isset($cacheparams->cachegroup))
  359. {
  360. $cacheparams->cachegroup = $module->module;
  361. }
  362. $user = JFactory::getUser();
  363. $cache = JFactory::getCache($cacheparams->cachegroup, 'callback');
  364. $conf = JFactory::getConfig();
  365. // Turn cache off for internal callers if parameters are set to off and for all logged in users
  366. if ($moduleparams->get('owncache', null) === 0 || $conf->get('caching') == 0 || $user->get('id'))
  367. {
  368. $cache->setCaching(false);
  369. }
  370. // module cache is set in seconds, global cache in minutes, setLifeTime works in minutes
  371. $cache->setLifeTime($moduleparams->get('cache_time', $conf->get('cachetime') * 60) / 60);
  372. $wrkaroundoptions = array('nopathway' => 1, 'nohead' => 0, 'nomodules' => 1, 'modulemode' => 1, 'mergehead' => 1);
  373. $wrkarounds = true;
  374. $view_levels = md5(serialize($user->getAuthorisedViewLevels()));
  375. switch ($cacheparams->cachemode)
  376. {
  377. case 'id':
  378. $ret = $cache->get(
  379. array($cacheparams->class, $cacheparams->method),
  380. $cacheparams->methodparams,
  381. $cacheparams->modeparams,
  382. $wrkarounds,
  383. $wrkaroundoptions
  384. );
  385. break;
  386. case 'safeuri':
  387. $secureid = null;
  388. if (is_array($cacheparams->modeparams))
  389. {
  390. $uri = JRequest::get();
  391. $safeuri = new stdClass();
  392. foreach ($cacheparams->modeparams as $key => $value)
  393. {
  394. // Use int filter for id/catid to clean out spamy slugs
  395. if (isset($uri[$key]))
  396. {
  397. $safeuri->$key = JRequest::_cleanVar($uri[$key], 0, $value);
  398. }
  399. }
  400. }
  401. $secureid = md5(serialize(array($safeuri, $cacheparams->method, $moduleparams)));
  402. $ret = $cache->get(
  403. array($cacheparams->class, $cacheparams->method),
  404. $cacheparams->methodparams,
  405. $module->id . $view_levels . $secureid,
  406. $wrkarounds,
  407. $wrkaroundoptions
  408. );
  409. break;
  410. case 'static':
  411. $ret = $cache->get(
  412. array($cacheparams->class,
  413. $cacheparams->method),
  414. $cacheparams->methodparams,
  415. $module->module . md5(serialize($cacheparams->methodparams)),
  416. $wrkarounds,
  417. $wrkaroundoptions
  418. );
  419. break;
  420. case 'oldstatic': // provided for backward compatibility, not really usefull
  421. $ret = $cache->get(
  422. array($cacheparams->class, $cacheparams->method),
  423. $cacheparams->methodparams,
  424. $module->id . $view_levels,
  425. $wrkarounds,
  426. $wrkaroundoptions
  427. );
  428. break;
  429. case 'itemid':
  430. default:
  431. $ret = $cache->get(
  432. array($cacheparams->class, $cacheparams->method),
  433. $cacheparams->methodparams,
  434. $module->id . $view_levels . JRequest::getVar('Itemid', null, 'default', 'INT'),
  435. $wrkarounds,
  436. $wrkaroundoptions
  437. );
  438. break;
  439. }
  440. return $ret;
  441. }
  442. }