PageRenderTime 40ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/libraries/legacy/module/helper.php

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