PageRenderTime 61ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

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

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