Skip to content

Commit e269098

Browse files
authored
Merge pull request #1985 from jim-parry/fix/filter-match
Fix filter processing. Fixes #1907
2 parents cbe4b1d + 39f2861 commit e269098

File tree

2 files changed

+233
-108
lines changed

2 files changed

+233
-108
lines changed

system/Filters/Filters.php

Lines changed: 73 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<?php
2-
32
/**
43
* CodeIgniter
54
*
@@ -317,9 +316,7 @@ public function enableFilter(string $name, string $when = 'before')
317316
*/
318317
public function getArguments(string $key = null)
319318
{
320-
return is_null($key)
321-
? $this->arguments
322-
: $this->arguments[$key];
319+
return is_null($key) ? $this->arguments : $this->arguments[$key];
323320
}
324321

325322
//--------------------------------------------------------------------
@@ -334,80 +331,44 @@ protected function processGlobals(string $uri = null)
334331
return;
335332
}
336333

337-
// Before
338-
if (isset($this->config->globals['before']))
334+
$uri = strtolower(trim($uri, '/ '));
335+
336+
// Add any global filters, unless they are excluded for this URI
337+
$sets = [
338+
'before',
339+
'after',
340+
];
341+
foreach ($sets as $set)
339342
{
340-
// Take any 'except' routes into consideration
341-
foreach ($this->config->globals['before'] as $alias => $rules)
343+
if (isset($this->config->globals[$set]))
342344
{
343-
if (! is_array($rules) || ! array_key_exists('except', $rules))
344-
{
345-
continue;
346-
}
347-
348-
$rules = $rules['except'];
349-
350-
if (is_string($rules))
345+
// look at each alias in the group
346+
foreach ($this->config->globals[$set] as $alias => $rules)
351347
{
352-
$rules = [$rules];
353-
}
354-
355-
foreach ($rules as $path)
356-
{
357-
// Prep it for regex
358-
$path = strtolower(str_replace('/*', '*', $path));
359-
$path = trim(str_replace('*', '.+', $path), '/ ');
360-
361-
// Path doesn't match the URI? continue on...
362-
if (preg_match('#' . $path . '#', $uri, $match) !== 1)
348+
$keep = true;
349+
if (is_array($rules))
363350
{
364-
continue;
351+
// see if it should be excluded
352+
if (isset($rules['except']))
353+
{
354+
// grab the exclusion rules
355+
$check = $rules['except'];
356+
if ($this->pathApplies($uri, $check))
357+
{
358+
$keep = false;
359+
}
360+
}
365361
}
366-
367-
unset($this->config->globals['before'][$alias]);
368-
break;
369-
}
370-
}
371-
372-
$this->filters['before'] = array_merge($this->filters['before'], $this->config->globals['before']);
373-
}
374-
375-
// After
376-
if (isset($this->config->globals['after']))
377-
{
378-
// Take any 'except' routes into consideration
379-
foreach ($this->config->globals['after'] as $alias => $rules)
380-
{
381-
if (! is_array($rules) || ! array_key_exists('except', $rules))
382-
{
383-
continue;
384-
}
385-
386-
$rules = $rules['except'];
387-
388-
if (is_string($rules))
389-
{
390-
$rules = [$rules];
391-
}
392-
393-
foreach ($rules as $path)
394-
{
395-
// Prep it for regex
396-
$path = strtolower(str_replace('/*', '*', $path));
397-
$path = trim(str_replace('*', '.+', $path), '/ ');
398-
399-
// Path doesn't match the URI? continue on...
400-
if (preg_match('#' . $path . '#', $uri, $match) !== 1)
362+
else
401363
{
402-
continue;
364+
$alias = $rules; // simple name of filter to apply
365+
}
366+
if ($keep)
367+
{
368+
$this->filters[$set][] = $alias;
403369
}
404-
405-
unset($this->config->globals['after'][$alias]);
406-
break;
407370
}
408371
}
409-
410-
$this->filters['after'] = array_merge($this->filters['after'], $this->config->globals['after']);
411372
}
412373
}
413374

@@ -441,53 +402,64 @@ protected function processFilters(string $uri = null)
441402

442403
$uri = strtolower(trim($uri, '/ '));
443404

444-
$matches = [];
445-
405+
// Add any filters that apply to this URI
446406
foreach ($this->config->filters as $alias => $settings)
447407
{
448-
// Before
408+
// Look for inclusion rules
449409
if (isset($settings['before']))
450410
{
451-
foreach ($settings['before'] as $path)
411+
$path = $settings['before'];
412+
if ($this->pathApplies($uri, $path))
452413
{
453-
// Prep it for regex
454-
$path = strtolower(str_replace('/*', '*', $path));
455-
$path = trim(str_replace('*', '.+', $path), '/ ');
456-
457-
if (preg_match('#' . $path . '#', $uri) !== 1)
458-
{
459-
continue;
460-
}
461-
462-
$matches[] = $alias;
414+
$this->filters['before'][] = $alias;
463415
}
464-
465-
$this->filters['before'] = array_merge($this->filters['before'], $matches);
466-
$matches = [];
467416
}
468-
469-
// After
470417
if (isset($settings['after']))
471418
{
472-
foreach ($settings['after'] as $path)
419+
$path = $settings['after'];
420+
if ($this->pathApplies($uri, $path))
473421
{
474-
// Prep it for regex
475-
$path = strtolower(str_replace('/*', '*', $path));
476-
$path = trim(str_replace('*', '.+', $path), '/ ');
422+
$this->filters['after'][] = $alias;
423+
}
424+
}
425+
}
426+
}
477427

478-
if (preg_match('#' . $path . '#', $uri) !== 1)
479-
{
480-
continue;
481-
}
428+
/**
429+
* Check paths for match for URI
430+
*
431+
* @param string $uri URI to test against
432+
* @param mixed $paths The path patterns to test
433+
* @return boolean True if any of the paths apply to the URI
434+
*/
435+
private function pathApplies(string $uri, $paths)
436+
{
437+
// empty path matches all
438+
if (empty($paths))
439+
{
440+
return true;
441+
}
482442

483-
$matches[] = $alias;
484-
}
443+
// make sure the paths are iterable
444+
if (is_string($paths))
445+
{
446+
$paths = [$paths];
447+
}
485448

486-
$this->filters['after'] = array_merge($this->filters['after'], $matches);
487-
$matches = [];
449+
// treat each paths as pseudo-regex
450+
foreach ($paths as $path)
451+
{
452+
// need to escape path separators
453+
$path = str_replace('/', '\/', trim($path, '/ '));
454+
// need to make pseudo wildcard real
455+
$path = strtolower(str_replace('*', '.*', $path));
456+
// Does this rule apply here?
457+
if (preg_match('#' . $path . '#', $uri, $match) === 1)
458+
{
459+
return true;
488460
}
489461
}
462+
return false;
490463
}
491464

492-
//--------------------------------------------------------------------
493465
}

0 commit comments

Comments
 (0)