PageRenderTime 50ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/libraries/legacy/module/helper.php

http://github.com/joomla/joomla-platform
PHP | 519 lines | 321 code | 76 blank | 122 comment | 47 complexity | abd9e38ecb36085dbeb78aed0adaadfd MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * @package Joomla.Legacy
  4. * @subpackage Module
  5. *
  6. * @copyright Copyright (C) 2005 - 2013 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. * @deprecated 13.3
  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 =& self::_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;
  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. }
  62. return $result;
  63. }
  64. /**
  65. * Get modules by position
  66. *
  67. * @param string $position The position of the module
  68. *
  69. * @return array An array of module objects
  70. *
  71. * @since 11.1
  72. */
  73. public static function &getModules($position)
  74. {
  75. $position = strtolower($position);
  76. $result = array();
  77. $input = JFactory::getApplication()->input;
  78. $modules =& self::_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 ($input->getBool('tp') && JComponentHelper::getParams('com_templates')->get('template_positions_display'))
  90. {
  91. $result[0] = self::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 = self::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. if (file_exists($path))
  143. {
  144. $lang = JFactory::getLanguage();
  145. // 1.5 or Core then 1.6 3PD
  146. $lang->load($module->module, JPATH_BASE, null, false, false) ||
  147. $lang->load($module->module, dirname($path), null, false, false) ||
  148. $lang->load($module->module, JPATH_BASE, $lang->getDefault(), false, false) ||
  149. $lang->load($module->module, dirname($path), $lang->getDefault(), false, false);
  150. $content = '';
  151. ob_start();
  152. include $path;
  153. $module->content = ob_get_contents() . $content;
  154. ob_end_clean();
  155. }
  156. // Load the module chrome functions
  157. if (!$chrome)
  158. {
  159. $chrome = array();
  160. }
  161. include_once JPATH_THEMES . '/system/html/modules.php';
  162. $chromePath = JPATH_THEMES . '/' . $app->getTemplate() . '/html/modules.php';
  163. if (!isset($chrome[$chromePath]))
  164. {
  165. if (file_exists($chromePath))
  166. {
  167. include_once $chromePath;
  168. }
  169. $chrome[$chromePath] = true;
  170. }
  171. // Make sure a style is set
  172. if (!isset($attribs['style']))
  173. {
  174. $attribs['style'] = 'none';
  175. }
  176. // Dynamically add outline style
  177. if ($app->input->getBool('tp') && JComponentHelper::getParams('com_templates')->get('template_positions_display'))
  178. {
  179. $attribs['style'] .= ' outline';
  180. }
  181. foreach (explode(' ', $attribs['style']) as $style)
  182. {
  183. $chromeMethod = 'modChrome_' . $style;
  184. // Apply chrome and render module
  185. if (function_exists($chromeMethod))
  186. {
  187. $module->style = $attribs['style'];
  188. ob_start();
  189. $chromeMethod($module, $params, $attribs);
  190. $module->content = ob_get_contents();
  191. ob_end_clean();
  192. }
  193. }
  194. // Revert the scope
  195. $app->scope = $scope;
  196. if (constant('JDEBUG'))
  197. {
  198. JProfiler::getInstance('Application')->mark('afterRenderModule ' . $module->module . ' (' . $module->title . ')');
  199. }
  200. return $module->content;
  201. }
  202. /**
  203. * Get the path to a layout for a module
  204. *
  205. * @param string $module The name of the module
  206. * @param string $layout The name of the module layout. If alternative layout, in the form template:filename.
  207. *
  208. * @return string The path to the module layout
  209. *
  210. * @since 11.1
  211. */
  212. public static function getLayoutPath($module, $layout = 'default')
  213. {
  214. $template = JFactory::getApplication()->getTemplate();
  215. $defaultLayout = $layout;
  216. if (strpos($layout, ':') !== false)
  217. {
  218. // Get the template and file name from the string
  219. $temp = explode(':', $layout);
  220. $template = ($temp[0] == '_') ? $template : $temp[0];
  221. $layout = $temp[1];
  222. $defaultLayout = ($temp[1]) ? $temp[1] : 'default';
  223. }
  224. // Build the template and base path for the layout
  225. $tPath = JPATH_THEMES . '/' . $template . '/html/' . $module . '/' . $layout . '.php';
  226. $bPath = JPATH_BASE . '/modules/' . $module . '/tmpl/' . $defaultLayout . '.php';
  227. $dPath = JPATH_BASE . '/modules/' . $module . '/tmpl/default.php';
  228. // If the template has a layout override use it
  229. if (file_exists($tPath))
  230. {
  231. return $tPath;
  232. }
  233. elseif (file_exists($bPath))
  234. {
  235. return $bPath;
  236. }
  237. else
  238. {
  239. return $dPath;
  240. }
  241. }
  242. /**
  243. * Load published modules.
  244. *
  245. * @return array
  246. *
  247. * @since 11.1
  248. */
  249. protected static function &_load()
  250. {
  251. static $clean;
  252. if (isset($clean))
  253. {
  254. return $clean;
  255. }
  256. $app = JFactory::getApplication();
  257. $Itemid = $app->input->getInt('Itemid');
  258. $user = JFactory::getUser();
  259. $groups = implode(',', $user->getAuthorisedViewLevels());
  260. $lang = JFactory::getLanguage()->getTag();
  261. $clientId = (int) $app->getClientId();
  262. $db = JFactory::getDbo();
  263. $query = $db->getQuery(true);
  264. $query->select('m.id, m.title, m.module, m.position, m.content, m.showtitle, m.params, mm.menuid');
  265. $query->from('#__modules AS m');
  266. $query->join('LEFT', '#__modules_menu AS mm ON mm.moduleid = m.id');
  267. $query->where('m.published = 1');
  268. $query->join('LEFT', '#__extensions AS e ON e.element = m.module AND e.client_id = m.client_id');
  269. $query->where('e.enabled = 1');
  270. $date = JFactory::getDate();
  271. $now = $date->toSql();
  272. $nullDate = $db->getNullDate();
  273. $query->where('(m.publish_up = ' . $db->Quote($nullDate) . ' OR m.publish_up <= ' . $db->Quote($now) . ')');
  274. $query->where('(m.publish_down = ' . $db->Quote($nullDate) . ' OR m.publish_down >= ' . $db->Quote($now) . ')');
  275. $query->where('m.access IN (' . $groups . ')');
  276. $query->where('m.client_id = ' . $clientId);
  277. $query->where('(mm.menuid = ' . (int) $Itemid . ' OR mm.menuid <= 0)');
  278. // Filter by language
  279. if ($app->isSite() && $app->getLanguageFilter())
  280. {
  281. $query->where('m.language IN (' . $db->Quote($lang) . ',' . $db->Quote('*') . ')');
  282. }
  283. $query->order('m.position, m.ordering');
  284. // Set the query
  285. $db->setQuery($query);
  286. $clean = array();
  287. try
  288. {
  289. $modules = $db->loadObjectList();
  290. }
  291. catch (RuntimeException $e)
  292. {
  293. JLog::add(JText::sprintf('JLIB_APPLICATION_ERROR_MODULE_LOAD', $e->getMessage()), JLog::WARNING, 'jerror');
  294. return $clean;
  295. }
  296. // Apply negative selections and eliminate duplicates
  297. $negId = $Itemid ? -(int) $Itemid : false;
  298. $dupes = array();
  299. for ($i = 0, $n = count($modules); $i < $n; $i++)
  300. {
  301. $module = &$modules[$i];
  302. // The module is excluded if there is an explicit prohibition
  303. $negHit = ($negId === (int) $module->menuid);
  304. if (isset($dupes[$module->id]))
  305. {
  306. // If this item has been excluded, keep the duplicate flag set,
  307. // but remove any item from the cleaned array.
  308. if ($negHit)
  309. {
  310. unset($clean[$module->id]);
  311. }
  312. continue;
  313. }
  314. $dupes[$module->id] = true;
  315. // Only accept modules without explicit exclusions.
  316. if (!$negHit)
  317. {
  318. $module->name = substr($module->module, 4);
  319. $module->style = null;
  320. $module->position = strtolower($module->position);
  321. $clean[$module->id] = $module;
  322. }
  323. }
  324. unset($dupes);
  325. // Return to simple indexing that matches the query order.
  326. $clean = array_values($clean);
  327. return $clean;
  328. }
  329. /**
  330. * Module cache helper
  331. *
  332. * Caching modes:
  333. * To be set in XML:
  334. * 'static' One cache file for all pages with the same module parameters
  335. * 'oldstatic' 1.5 definition of module caching, one cache file for all pages
  336. * with the same module id and user aid,
  337. * 'itemid' Changes on itemid change, to be called from inside the module:
  338. * 'safeuri' Id created from $cacheparams->modeparams array,
  339. * 'id' Module sets own cache id's
  340. *
  341. * @param object $module Module object
  342. * @param object $moduleparams Module parameters
  343. * @param object $cacheparams Module cache parameters - id or url parameters, depending on the module cache mode
  344. *
  345. * @return string
  346. *
  347. * @since 11.1
  348. *
  349. * @link JFilterInput::clean()
  350. */
  351. public static function moduleCache($module, $moduleparams, $cacheparams)
  352. {
  353. if (!isset($cacheparams->modeparams))
  354. {
  355. $cacheparams->modeparams = null;
  356. }
  357. if (!isset($cacheparams->cachegroup))
  358. {
  359. $cacheparams->cachegroup = $module->module;
  360. }
  361. $user = JFactory::getUser();
  362. $cache = JFactory::getCache($cacheparams->cachegroup, 'callback');
  363. $conf = JFactory::getConfig();
  364. // Turn cache off for internal callers if parameters are set to off and for all logged in users
  365. if ($moduleparams->get('owncache', null) === '0' || $conf->get('caching') == 0 || $user->get('id'))
  366. {
  367. $cache->setCaching(false);
  368. }
  369. // Module cache is set in seconds, global cache in minutes, setLifeTime works in minutes
  370. $cache->setLifeTime($moduleparams->get('cache_time', $conf->get('cachetime') * 60) / 60);
  371. $wrkaroundoptions = array('nopathway' => 1, 'nohead' => 0, 'nomodules' => 1, 'modulemode' => 1, 'mergehead' => 1);
  372. $wrkarounds = true;
  373. $view_levels = md5(serialize($user->getAuthorisedViewLevels()));
  374. switch ($cacheparams->cachemode)
  375. {
  376. case 'id':
  377. $ret = $cache->get(
  378. array($cacheparams->class, $cacheparams->method),
  379. $cacheparams->methodparams,
  380. $cacheparams->modeparams,
  381. $wrkarounds,
  382. $wrkaroundoptions
  383. );
  384. break;
  385. case 'safeuri':
  386. $secureid = null;
  387. if (is_array($cacheparams->modeparams))
  388. {
  389. $uri = JFactory::getApplication()->input->request;
  390. $safeuri = new stdClass;
  391. foreach ($cacheparams->modeparams as $key => $value)
  392. {
  393. // Use int filter for id/catid to clean out spamy slugs
  394. if ($uri->get($key, '') !== '')
  395. {
  396. $noHtmlFilter = JFilterInput::getInstance();
  397. $safeuri->$key = $noHtmlFilter->clean($uri[$key], $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. // Provided for backward compatibility, not really useful.
  421. case 'oldstatic':
  422. $ret = $cache->get(
  423. array($cacheparams->class, $cacheparams->method),
  424. $cacheparams->methodparams,
  425. $module->id . $view_levels,
  426. $wrkarounds,
  427. $wrkaroundoptions
  428. );
  429. break;
  430. case 'itemid':
  431. default:
  432. $ret = $cache->get(
  433. array($cacheparams->class, $cacheparams->method),
  434. $cacheparams->methodparams,
  435. $module->id . $view_levels . JFactory::getApplication()->input->getInt('Itemid', null),
  436. $wrkarounds,
  437. $wrkaroundoptions
  438. );
  439. break;
  440. }
  441. return $ret;
  442. }
  443. }