PageRenderTime 54ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

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

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