This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | namespace Elgg; |
||
3 | |||
4 | /** |
||
5 | * WARNING: API IN FLUX. DO NOT USE DIRECTLY. |
||
6 | * |
||
7 | * Use the elgg_* versions instead. |
||
8 | * |
||
9 | * @todo 1.10 remove deprecated view injections |
||
10 | * @todo inject/remove dependencies: $CONFIG, hooks, site_url |
||
11 | * |
||
12 | * @access private |
||
13 | * |
||
14 | * @package Elgg.Core |
||
15 | * @subpackage Views |
||
16 | * @since 1.9.0 |
||
17 | */ |
||
18 | class ViewsService { |
||
19 | |||
20 | protected $config_wrapper; |
||
21 | protected $site_url_wrapper; |
||
22 | protected $user_wrapper; |
||
23 | protected $user_wrapped; |
||
24 | |||
25 | /** |
||
26 | * @see \Elgg\ViewsService::fileExists |
||
27 | * @var array |
||
28 | */ |
||
29 | protected $file_exists_cache = array(); |
||
30 | |||
31 | /** |
||
32 | * Global Elgg configuration |
||
33 | * |
||
34 | * @var \stdClass |
||
35 | */ |
||
36 | private $CONFIG; |
||
37 | |||
38 | /** |
||
39 | * @var PluginHooksService |
||
40 | */ |
||
41 | private $hooks; |
||
42 | |||
43 | /** |
||
44 | * @var Logger |
||
45 | */ |
||
46 | private $logger; |
||
47 | |||
48 | /** |
||
49 | * @var array |
||
50 | */ |
||
51 | private $overriden_locations = array(); |
||
52 | |||
53 | /** |
||
54 | * Constructor |
||
55 | * |
||
56 | * @param \Elgg\PluginHooksService $hooks The hooks service |
||
57 | * @param \Elgg\Logger $logger Logger |
||
58 | */ |
||
59 | 14 | public function __construct(\Elgg\PluginHooksService $hooks, \Elgg\Logger $logger) { |
|
60 | 14 | global $CONFIG; |
|
61 | 14 | $this->CONFIG = $CONFIG; |
|
62 | 14 | $this->hooks = $hooks; |
|
63 | 14 | $this->logger = $logger; |
|
64 | 14 | } |
|
65 | |||
66 | /** |
||
67 | * Get the user object in a wrapper |
||
68 | * |
||
69 | * @return \Elgg\DeprecationWrapper|null |
||
70 | */ |
||
71 | 5 | protected function getUserWrapper() { |
|
72 | 5 | $user = _elgg_services()->session->getLoggedInUser(); |
|
73 | 5 | if ($user) { |
|
74 | if ($user !== $this->user_wrapped) { |
||
75 | $warning = 'Use elgg_get_logged_in_user_entity() rather than assuming elgg_view() ' |
||
76 | . 'populates $vars["user"]'; |
||
77 | $this->user_wrapper = new \Elgg\DeprecationWrapper($user, $warning, 1.8); |
||
78 | $this->user_wrapped = $user; |
||
79 | } |
||
80 | $user = $this->user_wrapper; |
||
81 | } |
||
82 | 5 | return $user; |
|
83 | } |
||
84 | |||
85 | /** |
||
86 | * @access private |
||
87 | */ |
||
88 | 13 | public function autoregisterViews($view_base, $folder, $base_location_path, $viewtype) { |
|
89 | 13 | $handle = opendir($folder); |
|
90 | 13 | if ($handle) { |
|
91 | 13 | while ($view = readdir($handle)) { |
|
92 | 13 | if (!empty($view_base)) { |
|
93 | 13 | $view_base_new = $view_base . "/"; |
|
94 | 13 | } else { |
|
95 | 13 | $view_base_new = ""; |
|
96 | } |
||
97 | |||
98 | 13 | if (substr($view, 0, 1) !== '.') { |
|
99 | 13 | if (is_dir($folder . "/" . $view)) { |
|
100 | 13 | $this->autoregisterViews($view_base_new . $view, $folder . "/" . $view, |
|
101 | 13 | $base_location_path, $viewtype); |
|
102 | 13 | } else { |
|
103 | 13 | $this->setViewLocation($view_base_new . basename($view, '.php'), |
|
104 | 13 | $base_location_path, $viewtype); |
|
105 | } |
||
106 | 13 | } |
|
107 | 13 | } |
|
108 | 13 | return true; |
|
109 | } |
||
110 | return false; |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * @access private |
||
115 | */ |
||
116 | 8 | public function getViewLocation($view, $viewtype = '') { |
|
117 | |||
118 | |||
119 | 8 | if (empty($viewtype)) { |
|
120 | $viewtype = elgg_get_viewtype(); |
||
121 | } |
||
122 | |||
123 | 8 | if (!isset($this->CONFIG->views->locations[$viewtype][$view])) { |
|
124 | 1 | if (!isset($this->CONFIG->viewpath)) { |
|
125 | 1 | return dirname(dirname(dirname(__FILE__))) . "/views/"; |
|
126 | } else { |
||
127 | return $this->CONFIG->viewpath; |
||
128 | } |
||
129 | } else { |
||
130 | 8 | return $this->CONFIG->views->locations[$viewtype][$view]; |
|
131 | } |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * @access private |
||
136 | */ |
||
137 | 13 | public function setViewLocation($view, $location, $viewtype = '') { |
|
138 | |||
139 | |||
140 | 13 | if (empty($viewtype)) { |
|
141 | $viewtype = 'default'; |
||
142 | } |
||
143 | |||
144 | 13 | if (!isset($this->CONFIG->views)) { |
|
145 | 13 | $this->CONFIG->views = new \stdClass; |
|
146 | 13 | } |
|
147 | |||
148 | 13 | if (!isset($this->CONFIG->views->locations)) { |
|
149 | 13 | $this->CONFIG->views->locations = array($viewtype => array($view => $location)); |
|
150 | |||
151 | 13 | } else if (!isset($this->CONFIG->views->locations[$viewtype])) { |
|
152 | $this->CONFIG->views->locations[$viewtype] = array($view => $location); |
||
153 | |||
154 | } else { |
||
155 | 13 | if (isset($this->CONFIG->views->locations[$viewtype][$view])) { |
|
156 | $this->overriden_locations[$viewtype][$view][] = $this->CONFIG->views->locations[$viewtype][$view]; |
||
157 | } |
||
158 | |||
159 | 13 | $this->CONFIG->views->locations[$viewtype][$view] = $location; |
|
160 | } |
||
161 | 13 | } |
|
162 | |||
163 | /** |
||
164 | * @access private |
||
165 | */ |
||
166 | 2 | View Code Duplication | public function registerViewtypeFallback($viewtype) { |
0 ignored issues
–
show
|
|||
167 | |||
168 | |||
169 | 2 | if (!isset($this->CONFIG->viewtype)) { |
|
170 | 2 | $this->CONFIG->viewtype = new \stdClass; |
|
171 | 2 | } |
|
172 | |||
173 | 2 | if (!isset($this->CONFIG->viewtype->fallback)) { |
|
174 | 2 | $this->CONFIG->viewtype->fallback = array(); |
|
175 | 2 | } |
|
176 | |||
177 | 2 | $this->CONFIG->viewtype->fallback[] = $viewtype; |
|
178 | 2 | } |
|
179 | |||
180 | /** |
||
181 | * @access private |
||
182 | */ |
||
183 | 4 | public function doesViewtypeFallback($viewtype) { |
|
184 | |||
185 | |||
186 | 4 | if (isset($this->CONFIG->viewtype) && isset($this->CONFIG->viewtype->fallback)) { |
|
187 | 2 | return in_array($viewtype, $this->CONFIG->viewtype->fallback); |
|
188 | } |
||
189 | |||
190 | 2 | return false; |
|
191 | } |
||
192 | |||
193 | /** |
||
194 | * Display a view with a deprecation notice. No missing view NOTICE is logged |
||
195 | * |
||
196 | * @see elgg_view() |
||
197 | * |
||
198 | * @param string $view The name and location of the view to use |
||
199 | * @param array $vars Variables to pass to the view |
||
200 | * @param string $suggestion Suggestion with the deprecation message |
||
201 | * @param string $version Human-readable *release* version: 1.7, 1.8, ... |
||
202 | * |
||
203 | * @return string The parsed view |
||
204 | * @access private |
||
205 | */ |
||
206 | public function renderDeprecatedView($view, array $vars, $suggestion, $version) { |
||
207 | $rendered = $this->renderView($view, $vars, false, '', false); |
||
208 | if ($rendered) { |
||
209 | elgg_deprecated_notice("The $view view has been deprecated. $suggestion", $version, 3); |
||
210 | } |
||
211 | return $rendered; |
||
212 | } |
||
213 | |||
214 | /** |
||
215 | * @access private |
||
216 | */ |
||
217 | 5 | public function renderView($view, array $vars = array(), $bypass = false, $viewtype = '', $issue_missing_notice = true) { |
|
218 | |||
219 | |||
220 | 5 | if (!is_string($view) || !is_string($viewtype)) { |
|
221 | $this->logger->log("View and Viewtype in views must be a strings: $view", 'NOTICE'); |
||
222 | return ''; |
||
223 | } |
||
224 | // basic checking for bad paths |
||
225 | 5 | if (strpos($view, '..') !== false) { |
|
226 | return ''; |
||
227 | } |
||
228 | |||
229 | 5 | if (!is_array($vars)) { |
|
230 | $this->logger->log("Vars in views must be an array: $view", 'ERROR'); |
||
231 | $vars = array(); |
||
232 | } |
||
233 | |||
234 | // Get the current viewtype |
||
235 | 5 | if ($viewtype === '' || !_elgg_is_valid_viewtype($viewtype)) { |
|
236 | 4 | $viewtype = elgg_get_viewtype(); |
|
237 | 4 | } |
|
238 | |||
239 | // allow altering $vars |
||
240 | $vars_hook_params = [ |
||
241 | 5 | 'view' => $view, |
|
242 | 5 | 'vars' => $vars, |
|
243 | 5 | 'viewtype' => $viewtype, |
|
244 | 5 | ]; |
|
245 | 5 | $vars = $this->hooks->trigger('view_vars', $view, $vars_hook_params, $vars); |
|
246 | |||
247 | 5 | $view_orig = $view; |
|
248 | |||
249 | // Trigger the pagesetup event |
||
250 | 5 | if (!isset($this->CONFIG->pagesetupdone) && $this->CONFIG->boot_complete) { |
|
251 | $this->CONFIG->pagesetupdone = true; |
||
252 | _elgg_services()->events->trigger('pagesetup', 'system'); |
||
253 | } |
||
254 | |||
255 | // @warning - plugin authors: do not expect user, config, and url to be |
||
256 | // set by elgg_view() in the future. Instead, use elgg_get_logged_in_user_entity(), |
||
257 | // elgg_get_config(), and elgg_get_site_url() in your views. |
||
258 | 5 | if (!isset($vars['user'])) { |
|
259 | 5 | $vars['user'] = $this->getUserWrapper(); |
|
260 | 5 | } |
|
261 | 5 | View Code Duplication | if (!isset($vars['config'])) { |
262 | 5 | if (!$this->config_wrapper) { |
|
263 | 5 | $warning = 'Do not rely on $vars["config"] or $CONFIG being available in views'; |
|
264 | 5 | $this->config_wrapper = new \Elgg\DeprecationWrapper($this->CONFIG, $warning, 1.8); |
|
265 | 5 | } |
|
266 | 5 | $vars['config'] = $this->config_wrapper; |
|
267 | 5 | } |
|
268 | 5 | View Code Duplication | if (!isset($vars['url'])) { |
269 | 5 | if (!$this->site_url_wrapper) { |
|
270 | 5 | $warning = 'Do not rely on $vars["url"] being available in views'; |
|
271 | 5 | $this->site_url_wrapper = new \Elgg\DeprecationWrapper(elgg_get_site_url(), $warning, 1.8); |
|
272 | 5 | } |
|
273 | 5 | $vars['url'] = $this->site_url_wrapper; |
|
274 | 5 | } |
|
275 | |||
276 | // full_view is the new preferred key for full view on entities @see elgg_view_entity() |
||
277 | // check if full_view is set because that means we've already rewritten it and this is |
||
278 | // coming from another view passing $vars directly. |
||
279 | 5 | if (isset($vars['full']) && !isset($vars['full_view'])) { |
|
280 | elgg_deprecated_notice("Use \$vars['full_view'] instead of \$vars['full']", 1.8, 2); |
||
281 | $vars['full_view'] = $vars['full']; |
||
282 | } |
||
283 | 5 | if (isset($vars['full_view'])) { |
|
284 | $vars['full'] = $vars['full_view']; |
||
285 | } |
||
286 | |||
287 | // internalname => name (1.8) |
||
288 | 5 | View Code Duplication | if (isset($vars['internalname']) && !isset($vars['__ignoreInternalname']) && !isset($vars['name'])) { |
289 | elgg_deprecated_notice('You should pass $vars[\'name\'] now instead of $vars[\'internalname\']', 1.8, 2); |
||
290 | $vars['name'] = $vars['internalname']; |
||
291 | 5 | } elseif (isset($vars['name'])) { |
|
292 | if (!isset($vars['internalname'])) { |
||
293 | $vars['__ignoreInternalname'] = ''; |
||
294 | } |
||
295 | $vars['internalname'] = $vars['name']; |
||
296 | } |
||
297 | |||
298 | // internalid => id (1.8) |
||
299 | 5 | View Code Duplication | if (isset($vars['internalid']) && !isset($vars['__ignoreInternalid']) && !isset($vars['name'])) { |
300 | elgg_deprecated_notice('You should pass $vars[\'id\'] now instead of $vars[\'internalid\']', 1.8, 2); |
||
301 | $vars['id'] = $vars['internalid']; |
||
302 | 5 | } elseif (isset($vars['id'])) { |
|
303 | if (!isset($vars['internalid'])) { |
||
304 | $vars['__ignoreInternalid'] = ''; |
||
305 | } |
||
306 | $vars['internalid'] = $vars['id']; |
||
307 | } |
||
308 | |||
309 | // If it's been requested, pass off to a template handler instead |
||
310 | 5 | if ($bypass == false && isset($this->CONFIG->template_handler) && !empty($this->CONFIG->template_handler)) { |
|
0 ignored issues
–
show
|
|||
311 | $template_handler = $this->CONFIG->template_handler; |
||
312 | if (is_callable($template_handler)) { |
||
313 | return call_user_func($template_handler, $view, $vars); |
||
314 | } |
||
315 | } |
||
316 | |||
317 | // Set up any extensions to the requested view |
||
318 | 5 | if (isset($this->CONFIG->views->extensions[$view])) { |
|
319 | $viewlist = $this->CONFIG->views->extensions[$view]; |
||
320 | } else { |
||
321 | 5 | $viewlist = array(500 => $view); |
|
322 | } |
||
323 | |||
324 | 5 | $content = ''; |
|
325 | 5 | foreach ($viewlist as $view) { |
|
326 | |||
327 | 5 | $rendering = $this->renderViewFile($view, $vars, $viewtype, $issue_missing_notice); |
|
328 | 5 | if ($rendering !== false) { |
|
329 | 4 | $content .= $rendering; |
|
330 | 4 | continue; |
|
331 | } |
||
332 | |||
333 | // attempt to load default view |
||
334 | 1 | if ($viewtype !== 'default' && $this->doesViewtypeFallback($viewtype)) { |
|
335 | |||
336 | 1 | $rendering = $this->renderViewFile($view, $vars, 'default', $issue_missing_notice); |
|
337 | 1 | if ($rendering !== false) { |
|
338 | 1 | $content .= $rendering; |
|
339 | 1 | } |
|
340 | 1 | } |
|
341 | 5 | } |
|
342 | |||
343 | // Plugin hook |
||
344 | 5 | $params = array('view' => $view_orig, 'vars' => $vars, 'viewtype' => $viewtype); |
|
345 | 5 | $content = $this->hooks->trigger('view', $view_orig, $params, $content); |
|
346 | |||
347 | // backward compatibility with less granular hook will be gone in 2.0 |
||
348 | 5 | $content_tmp = $this->hooks->trigger('display', 'view', $params, $content); |
|
349 | |||
350 | 5 | if ($content_tmp !== $content) { |
|
351 | $content = $content_tmp; |
||
352 | elgg_deprecated_notice('The display:view plugin hook is deprecated by view:view_name', 1.8); |
||
353 | } |
||
354 | |||
355 | 5 | return $content; |
|
356 | } |
||
357 | |||
358 | /** |
||
359 | * Wrapper for file_exists() that caches false results (the stat cache only caches true results). |
||
360 | * This saves us from many unneeded file stat calls when a common view uses a fallback. |
||
361 | * |
||
362 | * @param string $path Path to the file |
||
363 | * @return bool |
||
364 | */ |
||
365 | 9 | protected function fileExists($path) { |
|
366 | 9 | if (!isset($this->file_exists_cache[$path])) { |
|
367 | 9 | $this->file_exists_cache[$path] = file_exists($path); |
|
368 | 9 | } |
|
369 | 9 | return $this->file_exists_cache[$path]; |
|
370 | } |
||
371 | |||
372 | /** |
||
373 | * Includes view PHP or static file |
||
374 | * |
||
375 | * @param string $view The view name |
||
376 | * @param array $vars Variables passed to view |
||
377 | * @param string $viewtype The viewtype |
||
378 | * @param bool $issue_missing_notice Log a notice if the view is missing |
||
379 | * |
||
380 | * @return string|false output generated by view file inclusion or false |
||
381 | */ |
||
382 | 5 | private function renderViewFile($view, array $vars, $viewtype, $issue_missing_notice) { |
|
383 | 5 | $view_location = $this->getViewLocation($view, $viewtype); |
|
384 | |||
385 | // @warning - plugin authors: do not expect $CONFIG to be available in views |
||
386 | // in the future. Instead, use elgg_get_config() in your views. |
||
387 | // Note: this is intentionally a local var. |
||
388 | 5 | $CONFIG = $this->config_wrapper; |
|
389 | |||
390 | 5 | if ($this->fileExists("{$view_location}$viewtype/$view.php")) { |
|
391 | 4 | ob_start(); |
|
392 | 4 | include("{$view_location}$viewtype/$view.php"); |
|
393 | 4 | return ob_get_clean(); |
|
394 | 2 | } else if ($this->fileExists("{$view_location}$viewtype/$view")) { |
|
395 | 1 | return file_get_contents("{$view_location}$viewtype/$view"); |
|
396 | } else { |
||
397 | 1 | if ($issue_missing_notice) { |
|
398 | 1 | $this->logger->log("$viewtype/$view view does not exist.", 'NOTICE'); |
|
399 | 1 | } |
|
400 | 1 | return false; |
|
401 | } |
||
402 | } |
||
403 | |||
404 | /** |
||
405 | * @access private |
||
406 | */ |
||
407 | 4 | public function viewExists($view, $viewtype = '', $recurse = true) { |
|
408 | |||
409 | 4 | if (empty($view) || !is_string($view)) { |
|
410 | 1 | return false; |
|
411 | } |
||
412 | |||
413 | // Detect view type |
||
414 | 3 | if ($viewtype === '' || !_elgg_is_valid_viewtype($viewtype)) { |
|
415 | 2 | $viewtype = elgg_get_viewtype(); |
|
416 | 2 | } |
|
417 | |||
418 | 3 | if (!isset($this->CONFIG->views->locations[$viewtype][$view])) { |
|
419 | 1 | if (!isset($this->CONFIG->viewpath)) { |
|
420 | 1 | $location = dirname(dirname(dirname(__FILE__))) . "/views/"; |
|
421 | 1 | } else { |
|
422 | $location = $this->CONFIG->viewpath; |
||
423 | } |
||
424 | 1 | } else { |
|
425 | 3 | $location = $this->CONFIG->views->locations[$viewtype][$view]; |
|
426 | } |
||
427 | |||
428 | 3 | if ($this->fileExists("{$location}$viewtype/$view.php") || |
|
429 | 3 | $this->fileExists("{$location}$viewtype/$view")) { |
|
430 | 3 | return true; |
|
431 | } |
||
432 | |||
433 | // If we got here then check whether this exists as an extension |
||
434 | // We optionally recursively check whether the extended view exists also for the viewtype |
||
435 | 1 | if ($recurse && isset($this->CONFIG->views->extensions[$view])) { |
|
436 | foreach ($this->CONFIG->views->extensions[$view] as $view_extension) { |
||
437 | // do not recursively check to stay away from infinite loops |
||
438 | if ($this->viewExists($view_extension, $viewtype, false)) { |
||
439 | return true; |
||
440 | } |
||
441 | } |
||
442 | } |
||
443 | |||
444 | // Now check if the default view exists if the view is registered as a fallback |
||
445 | 1 | if ($viewtype != 'default' && $this->doesViewtypeFallback($viewtype)) { |
|
446 | 1 | return $this->viewExists($view, 'default'); |
|
447 | } |
||
448 | |||
449 | return false; |
||
450 | |||
451 | } |
||
452 | |||
453 | /** |
||
454 | * @access private |
||
455 | */ |
||
456 | 1 | public function extendView($view, $view_extension, $priority = 501, $viewtype = '') { |
|
457 | |||
458 | |||
459 | 1 | if (!isset($this->CONFIG->views)) { |
|
460 | $this->CONFIG->views = (object) array( |
||
461 | 'extensions' => array(), |
||
462 | ); |
||
463 | $this->CONFIG->views->extensions[$view][500] = (string) $view; |
||
464 | } else { |
||
465 | 1 | if (!isset($this->CONFIG->views->extensions[$view])) { |
|
466 | 1 | $this->CONFIG->views->extensions[$view][500] = (string) $view; |
|
467 | 1 | } |
|
468 | } |
||
469 | |||
470 | // raise priority until it doesn't match one already registered |
||
471 | 1 | while (isset($this->CONFIG->views->extensions[$view][$priority])) { |
|
472 | $priority++; |
||
473 | } |
||
474 | |||
475 | 1 | $this->CONFIG->views->extensions[$view][$priority] = (string) $view_extension; |
|
476 | 1 | ksort($this->CONFIG->views->extensions[$view]); |
|
477 | |||
478 | 1 | } |
|
479 | |||
480 | /** |
||
481 | * @access private |
||
482 | */ |
||
483 | 1 | public function unextendView($view, $view_extension) { |
|
484 | |||
485 | |||
486 | 1 | if (!isset($this->CONFIG->views)) { |
|
487 | return false; |
||
488 | } |
||
489 | |||
490 | 1 | if (!isset($this->CONFIG->views->extensions)) { |
|
491 | return false; |
||
492 | } |
||
493 | |||
494 | 1 | if (!isset($this->CONFIG->views->extensions[$view])) { |
|
495 | return false; |
||
496 | } |
||
497 | |||
498 | 1 | $priority = array_search($view_extension, $this->CONFIG->views->extensions[$view]); |
|
499 | 1 | if ($priority === false) { |
|
500 | 1 | return false; |
|
501 | } |
||
502 | |||
503 | 1 | unset($this->CONFIG->views->extensions[$view][$priority]); |
|
504 | |||
505 | 1 | return true; |
|
506 | } |
||
507 | |||
508 | /** |
||
509 | * @access private |
||
510 | */ |
||
511 | 1 | View Code Duplication | public function registerCacheableView($view) { |
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
512 | |||
513 | |||
514 | 1 | if (!isset($this->CONFIG->views)) { |
|
515 | $this->CONFIG->views = new \stdClass; |
||
516 | } |
||
517 | |||
518 | 1 | if (!isset($this->CONFIG->views->simplecache)) { |
|
519 | $this->CONFIG->views->simplecache = array(); |
||
520 | } |
||
521 | |||
522 | 1 | $this->CONFIG->views->simplecache[$view] = true; |
|
523 | 1 | } |
|
524 | |||
525 | /** |
||
526 | * @access private |
||
527 | */ |
||
528 | 2 | public function isCacheableView($view) { |
|
529 | |||
530 | |||
531 | 2 | if (!isset($this->CONFIG->views)) { |
|
532 | $this->CONFIG->views = new \stdClass; |
||
533 | } |
||
534 | |||
535 | 2 | if (!isset($this->CONFIG->views->simplecache)) { |
|
536 | 2 | $this->CONFIG->views->simplecache = array(); |
|
537 | 2 | } |
|
538 | |||
539 | 2 | if (isset($this->CONFIG->views->simplecache[$view])) { |
|
540 | 1 | return true; |
|
541 | } else { |
||
542 | 2 | $currentViewtype = elgg_get_viewtype(); |
|
543 | 2 | $viewtypes = array($currentViewtype); |
|
544 | |||
545 | 2 | if ($this->doesViewtypeFallback($currentViewtype) && $currentViewtype != 'default') { |
|
546 | $viewtypes[] = 'defaut'; |
||
547 | } |
||
548 | |||
549 | // If a static view file is found in any viewtype, it's considered cacheable |
||
550 | 2 | foreach ($viewtypes as $viewtype) { |
|
551 | 2 | $view_file = $this->getViewLocation($view, $viewtype) . "$viewtype/$view"; |
|
552 | 2 | if ($this->fileExists($view_file)) { |
|
553 | 1 | return true; |
|
554 | } |
||
555 | 1 | } |
|
556 | |||
557 | // Assume not-cacheable by default |
||
558 | 1 | return false; |
|
559 | } |
||
560 | } |
||
561 | |||
562 | /** |
||
563 | * Register a plugin's views |
||
564 | * |
||
565 | * @param string $path Base path of the plugin |
||
566 | * @param string $failed_dir This var is set to the failed directory if registration fails |
||
567 | * @return bool |
||
568 | * |
||
569 | * @access private |
||
570 | */ |
||
571 | public function registerPluginViews($path, &$failed_dir = '') { |
||
572 | $view_dir = "$path/views/"; |
||
573 | |||
574 | // plugins don't have to have views. |
||
575 | if (!is_dir($view_dir)) { |
||
576 | return true; |
||
577 | } |
||
578 | |||
579 | // but if they do, they have to be readable |
||
580 | $handle = opendir($view_dir); |
||
581 | if (!$handle) { |
||
582 | $failed_dir = $view_dir; |
||
583 | return false; |
||
584 | } |
||
585 | |||
586 | while (false !== ($view_type = readdir($handle))) { |
||
587 | $view_type_dir = $view_dir . $view_type; |
||
588 | |||
589 | if ('.' !== substr($view_type, 0, 1) && is_dir($view_type_dir)) { |
||
590 | if ($this->autoregisterViews('', $view_type_dir, $view_dir, $view_type)) { |
||
591 | elgg_register_viewtype($view_type); |
||
592 | } else { |
||
593 | $failed_dir = $view_type_dir; |
||
594 | return false; |
||
595 | } |
||
596 | } |
||
597 | } |
||
598 | |||
599 | return true; |
||
600 | } |
||
601 | |||
602 | /** |
||
603 | * Get views overridden by setViewLocation() calls. |
||
604 | * |
||
605 | * @return array |
||
606 | * |
||
607 | * @access private |
||
608 | */ |
||
609 | public function getOverriddenLocations() { |
||
610 | return $this->overriden_locations; |
||
611 | } |
||
612 | |||
613 | /** |
||
614 | * Set views overridden by setViewLocation() calls. |
||
615 | * |
||
616 | * @param array $locations |
||
617 | * @return void |
||
618 | * |
||
619 | * @access private |
||
620 | */ |
||
621 | public function setOverriddenLocations(array $locations) { |
||
622 | $this->overriden_locations = $locations; |
||
623 | } |
||
624 | } |
||
625 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.