PageRenderTime 43ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

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

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