1 <?php
2 3 4 5 6 7 8
9
10 defined('JPATH_PLATFORM') or die;
11
12 use Joomla\Registry\Registry;
13
14 15 16 17 18
19 abstract class JModuleHelper
20 {
21 22 23 24 25 26 27 28 29 30
31 public static function &getModule($name, $title = null)
32 {
33 $result = null;
34 $modules =& static::load();
35 $total = count($modules);
36
37 for ($i = 0; $i < $total; $i++)
38 {
39
40 if ($modules[$i]->name === $name || $modules[$i]->module === $name)
41 {
42
43 if (!$title || $modules[$i]->title === $title)
44 {
45
46 $result = &$modules[$i];
47 break;
48 }
49 }
50 }
51
52
53 if ($result === null && strpos($name, 'mod_') === 0)
54 {
55 $result = new stdClass;
56 $result->id = 0;
57 $result->title = '';
58 $result->module = $name;
59 $result->position = '';
60 $result->content = '';
61 $result->showtitle = 0;
62 $result->control = '';
63 $result->params = '';
64 }
65
66 return $result;
67 }
68
69 70 71 72 73 74 75 76 77
78 public static function &getModules($position)
79 {
80 $position = strtolower($position);
81 $result = array();
82 $input = JFactory::getApplication()->input;
83
84 $modules =& static::load();
85
86 $total = count($modules);
87
88 for ($i = 0; $i < $total; $i++)
89 {
90 if ($modules[$i]->position === $position)
91 {
92 $result[] = &$modules[$i];
93 }
94 }
95
96 if (count($result) === 0)
97 {
98 if ($input->getBool('tp') && JComponentHelper::getParams('com_templates')->get('template_positions_display'))
99 {
100 $result[0] = static::getModule('mod_' . $position);
101 $result[0]->title = $position;
102 $result[0]->content = $position;
103 $result[0]->position = $position;
104 }
105 }
106
107 return $result;
108 }
109
110 111 112 113 114 115 116 117 118 119 120 121
122 public static function isEnabled($module)
123 {
124 $result = static::getModule($module);
125
126 return $result !== null && $result->id !== 0;
127 }
128
129 130 131 132 133 134 135 136 137 138
139 public static function renderModule($module, $attribs = array())
140 {
141 static $chrome;
142
143
144 if (!is_object($module) || !isset($module->module) || !isset($module->params))
145 {
146 if (JDEBUG)
147 {
148 JLog::addLogger(array('text_file' => 'jmodulehelper.log.php'), JLog::ALL, array('modulehelper'));
149 JLog::add('JModuleHelper::renderModule($module) expects a module object', JLog::DEBUG, 'modulehelper');
150 }
151
152 return;
153 }
154
155 if (JDEBUG)
156 {
157 JProfiler::getInstance('Application')->mark('beforeRenderModule ' . $module->module . ' (' . $module->title . ')');
158 }
159
160 $app = JFactory::getApplication();
161
162
163 $scope = $app->scope;
164
165
166 $app->scope = $module->module;
167
168
169 $params = new Registry($module->params);
170
171
172 $template = $app->getTemplate();
173
174
175 $module->module = preg_replace('/[^A-Z0-9_\.-]/i', '', $module->module);
176 $path = JPATH_BASE . '/modules/' . $module->module . '/' . $module->module . '.php';
177
178
179 if (file_exists($path))
180 {
181 $lang = JFactory::getLanguage();
182
183 $coreLanguageDirectory = JPATH_BASE;
184 $extensionLanguageDirectory = dirname($path);
185
186 $langPaths = $lang->getPaths();
187
188
189 if (!$langPaths || (!isset($langPaths[$coreLanguageDirectory]) && !isset($langPaths[$extensionLanguageDirectory])))
190 {
191
192 $lang->load($module->module, $coreLanguageDirectory, null, false, true) ||
193 $lang->load($module->module, $extensionLanguageDirectory, null, false, true);
194 }
195
196 $content = '';
197 ob_start();
198 include $path;
199 $module->content = ob_get_contents() . $content;
200 ob_end_clean();
201 }
202
203
204 if (!$chrome)
205 {
206 $chrome = array();
207 }
208
209 include_once JPATH_THEMES . '/system/html/modules.php';
210 $chromePath = JPATH_THEMES . '/' . $template . '/html/modules.php';
211
212 if (!isset($chrome[$chromePath]))
213 {
214 if (file_exists($chromePath))
215 {
216 include_once $chromePath;
217 }
218
219 $chrome[$chromePath] = true;
220 }
221
222
223 $paramsChromeStyle = $params->get('style');
224
225 if ($paramsChromeStyle)
226 {
227 $attribs['style'] = preg_replace('/^(system|' . $template . ')\-/i', '', $paramsChromeStyle);
228 }
229
230
231 if (!isset($attribs['style']))
232 {
233 $attribs['style'] = 'none';
234 }
235
236
237 if ($app->input->getBool('tp') && JComponentHelper::getParams('com_templates')->get('template_positions_display'))
238 {
239 $attribs['style'] .= ' outline';
240 }
241
242
243 $app->triggerEvent('onRenderModule', array(&$module, &$attribs));
244
245 if ($module === null || !isset($module->content))
246 {
247 return '';
248 }
249
250 foreach (explode(' ', $attribs['style']) as $style)
251 {
252 $chromeMethod = 'modChrome_' . $style;
253
254
255 if (function_exists($chromeMethod))
256 {
257 $module->style = $attribs['style'];
258
259 ob_start();
260 $chromeMethod($module, $params, $attribs);
261 $module->content = ob_get_contents();
262 ob_end_clean();
263 }
264 }
265
266
267 $app->scope = $scope;
268
269 $app->triggerEvent('onAfterRenderModule', array(&$module, &$attribs));
270
271 if (JDEBUG)
272 {
273 JProfiler::getInstance('Application')->mark('afterRenderModule ' . $module->module . ' (' . $module->title . ')');
274 }
275
276 return $module->content;
277 }
278
279 280 281 282 283 284 285 286 287 288
289 public static function getLayoutPath($module, $layout = 'default')
290 {
291 $template = JFactory::getApplication()->getTemplate();
292 $defaultLayout = $layout;
293
294 if (strpos($layout, ':') !== false)
295 {
296
297 $temp = explode(':', $layout);
298 $template = $temp[0] === '_' ? $template : $temp[0];
299 $layout = $temp[1];
300 $defaultLayout = $temp[1] ?: 'default';
301 }
302
303
304 $tPath = JPATH_THEMES . '/' . $template . '/html/' . $module . '/' . $layout . '.php';
305 $bPath = JPATH_BASE . '/modules/' . $module . '/tmpl/' . $defaultLayout . '.php';
306 $dPath = JPATH_BASE . '/modules/' . $module . '/tmpl/default.php';
307
308
309 if (file_exists($tPath))
310 {
311 return $tPath;
312 }
313
314 if (file_exists($bPath))
315 {
316 return $bPath;
317 }
318
319 return $dPath;
320 }
321
322 323 324 325 326 327 328 329
330 protected static function &_load()
331 {
332 return static::load();
333 }
334
335 336 337 338 339 340 341
342 protected static function &load()
343 {
344 static $modules;
345
346 if (isset($modules))
347 {
348 return $modules;
349 }
350
351 $app = JFactory::getApplication();
352
353 $modules = null;
354
355 $app->triggerEvent('onPrepareModuleList', array(&$modules));
356
357
358 if (!is_array($modules))
359 {
360 $modules = static::getModuleList();
361 }
362
363 $app->triggerEvent('onAfterModuleList', array(&$modules));
364
365 $modules = static::cleanModuleList($modules);
366
367 $app->triggerEvent('onAfterCleanModuleList', array(&$modules));
368
369 return $modules;
370 }
371
372 373 374 375 376
377 public static function getModuleList()
378 {
379 $app = JFactory::getApplication();
380 $Itemid = $app->input->getInt('Itemid', 0);
381 $groups = implode(',', JFactory::getUser()->getAuthorisedViewLevels());
382 $lang = JFactory::getLanguage()->getTag();
383 $clientId = (int) $app->getClientId();
384
385
386 $cacheId = $groups . $clientId . $Itemid;
387
388 $db = JFactory::getDbo();
389
390 $query = $db->getQuery(true)
391 ->select('m.id, m.title, m.module, m.position, m.content, m.showtitle, m.params, mm.menuid')
392 ->from('#__modules AS m')
393 ->join('LEFT', '#__modules_menu AS mm ON mm.moduleid = m.id')
394 ->where('m.published = 1')
395 ->join('LEFT', '#__extensions AS e ON e.element = m.module AND e.client_id = m.client_id')
396 ->where('e.enabled = 1');
397
398 $date = JFactory::getDate();
399 $now = $date->toSql();
400 $nullDate = $db->getNullDate();
401 $query->where('(m.publish_up = ' . $db->quote($nullDate) . ' OR m.publish_up <= ' . $db->quote($now) . ')')
402 ->where('(m.publish_down = ' . $db->quote($nullDate) . ' OR m.publish_down >= ' . $db->quote($now) . ')')
403 ->where('m.access IN (' . $groups . ')')
404 ->where('m.client_id = ' . $clientId)
405 ->where('(mm.menuid = ' . $Itemid . ' OR mm.menuid <= 0)');
406
407
408 if ($app->isClient('site') && $app->getLanguageFilter())
409 {
410 $query->where('m.language IN (' . $db->quote($lang) . ',' . $db->quote('*') . ')');
411 $cacheId .= $lang . '*';
412 }
413
414 $query->order('m.position, m.ordering');
415
416
417 $db->setQuery($query);
418
419 try
420 {
421
422 $cache = JFactory::getCache('com_modules', 'callback');
423
424 $modules = $cache->get(array($db, 'loadObjectList'), array(), md5($cacheId), false);
425 }
426 catch (RuntimeException $e)
427 {
428 JLog::add(JText::sprintf('JLIB_APPLICATION_ERROR_MODULE_LOAD', $e->getMessage()), JLog::WARNING, 'jerror');
429
430 return array();
431 }
432
433 return $modules;
434 }
435
436 437 438 439 440 441 442
443 public static function cleanModuleList($modules)
444 {
445
446 $Itemid = JFactory::getApplication()->input->getInt('Itemid');
447 $negId = $Itemid ? -(int) $Itemid : false;
448 $clean = array();
449 $dupes = array();
450
451 foreach ($modules as $i => $module)
452 {
453
454 $negHit = ($negId === (int) $module->menuid);
455
456 if (isset($dupes[$module->id]))
457 {
458
459
460 if ($negHit)
461 {
462 unset($clean[$module->id]);
463 }
464
465 continue;
466 }
467
468 $dupes[$module->id] = true;
469
470
471 if ($negHit)
472 {
473 continue;
474 }
475
476 $module->name = substr($module->module, 4);
477 $module->style = null;
478 $module->position = strtolower($module->position);
479
480 $clean[$module->id] = $module;
481 }
482
483 unset($dupes);
484
485
486 return array_values($clean);
487 }
488
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509
510 public static function moduleCache($module, $moduleparams, $cacheparams)
511 {
512 if (!isset($cacheparams->modeparams))
513 {
514 $cacheparams->modeparams = null;
515 }
516
517 if (!isset($cacheparams->cachegroup))
518 {
519 $cacheparams->cachegroup = $module->module;
520 }
521
522 $user = JFactory::getUser();
523 $conf = JFactory::getConfig();
524
525
526 $cache = JFactory::getCache($cacheparams->cachegroup, 'callback');
527
528
529 if ($moduleparams->get('owncache', null) === '0' || $conf->get('caching') == 0 || $user->get('id'))
530 {
531 $cache->setCaching(false);
532 }
533
534
535 $cache->setLifeTime($moduleparams->get('cache_time', $conf->get('cachetime') * 60) / 60);
536
537 $wrkaroundoptions = array('nopathway' => 1, 'nohead' => 0, 'nomodules' => 1, 'modulemode' => 1, 'mergehead' => 1);
538
539 $wrkarounds = true;
540 $view_levels = md5(serialize($user->getAuthorisedViewLevels()));
541
542 switch ($cacheparams->cachemode)
543 {
544 case 'id':
545 $ret = $cache->get(
546 array($cacheparams->class, $cacheparams->method),
547 $cacheparams->methodparams,
548 $cacheparams->modeparams,
549 $wrkarounds,
550 $wrkaroundoptions
551 );
552 break;
553
554 case 'safeuri':
555 $secureid = null;
556
557 if (is_array($cacheparams->modeparams))
558 {
559 $input = JFactory::getApplication()->input;
560 $uri = $input->getArray();
561 $safeuri = new stdClass;
562 $noHtmlFilter = JFilterInput::getInstance();
563
564 foreach ($cacheparams->modeparams as $key => $value)
565 {
566
567 if (isset($uri[$key]))
568 {
569 $safeuri->$key = $noHtmlFilter->clean($uri[$key], $value);
570 }
571 }
572 }
573
574 $secureid = md5(serialize(array($safeuri, $cacheparams->method, $moduleparams)));
575 $ret = $cache->get(
576 array($cacheparams->class, $cacheparams->method),
577 $cacheparams->methodparams,
578 $module->id . $view_levels . $secureid,
579 $wrkarounds,
580 $wrkaroundoptions
581 );
582 break;
583
584 case 'static':
585 $ret = $cache->get(
586 array($cacheparams->class, $cacheparams->method),
587 $cacheparams->methodparams,
588 $module->module . md5(serialize($cacheparams->methodparams)),
589 $wrkarounds,
590 $wrkaroundoptions
591 );
592 break;
593
594
595 case 'oldstatic':
596 $ret = $cache->get(
597 array($cacheparams->class, $cacheparams->method),
598 $cacheparams->methodparams,
599 $module->id . $view_levels,
600 $wrkarounds,
601 $wrkaroundoptions
602 );
603 break;
604
605 case 'itemid':
606 default:
607 $ret = $cache->get(
608 array($cacheparams->class, $cacheparams->method),
609 $cacheparams->methodparams,
610 $module->id . $view_levels . JFactory::getApplication()->input->getInt('Itemid', null),
611 $wrkarounds,
612 $wrkaroundoptions
613 );
614 break;
615 }
616
617 return $ret;
618 }
619 }
620