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 class JComponentHelper
20 {
21 22 23 24 25 26
27 protected static $components = array();
28
29 30 31 32 33 34 35 36 37 38
39 public static function getComponent($option, $strict = false)
40 {
41 if (!isset(static::$components[$option]))
42 {
43 if (static::load($option))
44 {
45 $result = static::$components[$option];
46 }
47 else
48 {
49 $result = new JComponentRecord;
50 $result->enabled = $strict ? false : true;
51 $result->setParams(new Registry);
52 }
53 }
54 else
55 {
56 $result = static::$components[$option];
57 }
58
59 return $result;
60 }
61
62 63 64 65 66 67 68 69 70
71 public static function isEnabled($option)
72 {
73 return (bool) static::getComponent($option, true)->enabled;
74 }
75
76 77 78 79 80 81 82 83 84
85 public static function isInstalled($option)
86 {
87 $db = JFactory::getDbo();
88
89 return (int) $db->setQuery(
90 $db->getQuery(true)
91 ->select('COUNT(' . $db->quoteName('extension_id') . ')')
92 ->from($db->quoteName('#__extensions'))
93 ->where($db->quoteName('element') . ' = ' . $db->quote($option))
94 ->where($db->quoteName('type') . ' = ' . $db->quote('component'))
95 )->loadResult();
96 }
97
98 99 100 101 102 103 104 105 106 107 108
109 public static function getParams($option, $strict = false)
110 {
111 return static::getComponent($option, $strict)->params;
112 }
113
114 115 116 117 118 119 120 121 122
123 public static function filterText($text)
124 {
125
126 $text = JFilterInput::getInstance()->emailToPunycode($text);
127
128
129 $config = static::getParams('com_config');
130 $user = JFactory::getUser();
131 $userGroups = JAccess::getGroupsByUser($user->get('id'));
132
133 $filters = $config->get('filters');
134
135 $blackListTags = array();
136 $blackListAttributes = array();
137
138 $customListTags = array();
139 $customListAttributes = array();
140
141 $whiteListTags = array();
142 $whiteListAttributes = array();
143
144 $whiteList = false;
145 $blackList = false;
146 $customList = false;
147 $unfiltered = false;
148
149
150
151 foreach ($userGroups as $groupId)
152 {
153
154 if (!isset($filters->$groupId))
155 {
156 continue;
157 }
158
159
160 $filterData = $filters->$groupId;
161 $filterType = strtoupper($filterData->filter_type);
162
163 if ($filterType === 'NH')
164 {
165
166 }
167 elseif ($filterType === 'NONE')
168 {
169
170 $unfiltered = true;
171 }
172 else
173 {
174
175
176 $tags = explode(',', $filterData->filter_tags);
177 $attributes = explode(',', $filterData->filter_attributes);
178 $tempTags = array();
179 $tempAttributes = array();
180
181 foreach ($tags as $tag)
182 {
183 $tag = trim($tag);
184
185 if ($tag)
186 {
187 $tempTags[] = $tag;
188 }
189 }
190
191 foreach ($attributes as $attribute)
192 {
193 $attribute = trim($attribute);
194
195 if ($attribute)
196 {
197 $tempAttributes[] = $attribute;
198 }
199 }
200
201
202
203 if ($filterType === 'BL')
204 {
205 $blackList = true;
206 $blackListTags = array_merge($blackListTags, $tempTags);
207 $blackListAttributes = array_merge($blackListAttributes, $tempAttributes);
208 }
209 elseif ($filterType === 'CBL')
210 {
211
212 if ($tempTags || $tempAttributes)
213 {
214 $customList = true;
215 $customListTags = array_merge($customListTags, $tempTags);
216 $customListAttributes = array_merge($customListAttributes, $tempAttributes);
217 }
218 }
219 elseif ($filterType === 'WL')
220 {
221 $whiteList = true;
222 $whiteListTags = array_merge($whiteListTags, $tempTags);
223 $whiteListAttributes = array_merge($whiteListAttributes, $tempAttributes);
224 }
225 }
226 }
227
228
229 $blackListTags = array_unique($blackListTags);
230 $blackListAttributes = array_unique($blackListAttributes);
231 $customListTags = array_unique($customListTags);
232 $customListAttributes = array_unique($customListAttributes);
233 $whiteListTags = array_unique($whiteListTags);
234 $whiteListAttributes = array_unique($whiteListAttributes);
235
236 if (!$unfiltered)
237 {
238
239 if ($customList)
240 {
241 $filter = JFilterInput::getInstance(array(), array(), 1, 1);
242
243
244 if ($customListTags)
245 {
246 $filter->tagBlacklist = $customListTags;
247 }
248
249 if ($customListAttributes)
250 {
251 $filter->attrBlacklist = $customListAttributes;
252 }
253 }
254
255 elseif ($blackList)
256 {
257
258 $blackListTags = array_diff($blackListTags, $whiteListTags);
259 $blackListAttributes = array_diff($blackListAttributes, $whiteListAttributes);
260
261 $filter = JFilterInput::getInstance($blackListTags, $blackListAttributes, 1, 1);
262
263
264 if ($whiteListTags)
265 {
266 $filter->tagBlacklist = array_diff($filter->tagBlacklist, $whiteListTags);
267 }
268
269 if ($whiteListAttributes)
270 {
271 $filter->attrBlacklist = array_diff($filter->attrBlacklist, $whiteListAttributes);
272 }
273 }
274
275 elseif ($whiteList)
276 {
277
278 $filter = JFilterInput::getInstance($whiteListTags, $whiteListAttributes, 0, 0, 0);
279 }
280
281 else
282 {
283 $filter = JFilterInput::getInstance();
284 }
285
286 $text = $filter->clean($text, 'html');
287 }
288
289 return $text;
290 }
291
292 293 294 295 296 297 298 299 300 301 302
303 public static function renderComponent($option, $params = array())
304 {
305 $app = JFactory::getApplication();
306
307
308 $template = $app->getTemplate(true)->template;
309 $lang = JFactory::getLanguage();
310 $lang->load('tpl_' . $template, JPATH_BASE, null, false, true)
311 || $lang->load('tpl_' . $template, JPATH_THEMES . "/$template", null, false, true);
312
313 if (empty($option))
314 {
315 throw new JComponentExceptionMissing(JText::_('JLIB_APPLICATION_ERROR_COMPONENT_NOT_FOUND'), 404);
316 }
317
318 if (JDEBUG)
319 {
320 JProfiler::getInstance('Application')->mark('beforeRenderComponent ' . $option);
321 }
322
323
324 $scope = $app->scope;
325
326
327 $app->scope = $option;
328
329
330 $option = preg_replace('/[^A-Z0-9_\.-]/i', '', $option);
331 $file = substr($option, 4);
332
333
334 if (!defined('JPATH_COMPONENT'))
335 {
336 define('JPATH_COMPONENT', JPATH_BASE . '/components/' . $option);
337 }
338
339 if (!defined('JPATH_COMPONENT_SITE'))
340 {
341 define('JPATH_COMPONENT_SITE', JPATH_SITE . '/components/' . $option);
342 }
343
344 if (!defined('JPATH_COMPONENT_ADMINISTRATOR'))
345 {
346 define('JPATH_COMPONENT_ADMINISTRATOR', JPATH_ADMINISTRATOR . '/components/' . $option);
347 }
348
349 $path = JPATH_COMPONENT . '/' . $file . '.php';
350
351
352 if (!static::isEnabled($option) || !file_exists($path))
353 {
354 throw new JComponentExceptionMissing(JText::_('JLIB_APPLICATION_ERROR_COMPONENT_NOT_FOUND'), 404);
355 }
356
357
358 $lang->load($option, JPATH_BASE, null, false, true) || $lang->load($option, JPATH_COMPONENT, null, false, true);
359
360
361 $contents = null;
362
363
364 $contents = static::executeComponent($path);
365
366
367 $app->scope = $scope;
368
369 if (JDEBUG)
370 {
371 JProfiler::getInstance('Application')->mark('afterRenderComponent ' . $option);
372 }
373
374 return $contents;
375 }
376
377 378 379 380 381 382 383 384 385
386 protected static function executeComponent($path)
387 {
388 ob_start();
389 require_once $path;
390
391 return ob_get_clean();
392 }
393
394 395 396 397 398 399 400 401 402 403
404 protected static function _load($option)
405 {
406 return static::load($option);
407 }
408
409 410 411 412 413 414 415 416 417 418
419 protected static function load($option)
420 {
421 try
422 {
423 JLog::add(
424 sprintf(
425 'Passing a parameter into %s() is deprecated and will be removed in 4.0. Read %s::$components directly after loading the data.',
426 __METHOD__,
427 __CLASS__
428 ),
429 JLog::WARNING,
430 'deprecated'
431 );
432 }
433 catch (RuntimeException $e)
434 {
435
436 }
437
438 $loader = function ()
439 {
440 $db = JFactory::getDbo();
441 $query = $db->getQuery(true)
442 ->select($db->quoteName(array('extension_id', 'element', 'params', 'enabled'), array('id', 'option', null, null)))
443 ->from($db->quoteName('#__extensions'))
444 ->where($db->quoteName('type') . ' = ' . $db->quote('component'));
445 $db->setQuery($query);
446
447 return $db->loadObjectList('option', 'JComponentRecord');
448 };
449
450
451 $cache = JFactory::getCache('_system', 'callback');
452
453 try
454 {
455 static::$components = $cache->get($loader, array(), __METHOD__);
456 }
457 catch (JCacheException $e)
458 {
459 static::$components = $loader();
460 }
461
462 if (empty(static::$components[$option]))
463 {
464 465 466 467 468 469
470
471 if (JFactory::$language)
472 {
473 $msg = JText::sprintf('JLIB_APPLICATION_ERROR_COMPONENT_NOT_LOADING', $option, JText::_('JLIB_APPLICATION_ERROR_COMPONENT_NOT_FOUND'));
474 }
475 else
476 {
477 $msg = sprintf('Error loading component: %1$s, %2$s', $option, 'Component not found.');
478 }
479
480 JLog::add($msg, JLog::WARNING, 'jerror');
481
482 return false;
483 }
484
485 return true;
486 }
487
488 489 490 491 492 493 494
495 public static function getComponents()
496 {
497 if (empty(static::$components))
498 {
499 static::load('*');
500 }
501
502 return static::$components;
503 }
504 }
505