|
14 | 14 | if ( count( $parts ) > 1 ) { |
15 | 15 | // remove last word, adding new slug |
16 | 16 | array_pop( $parts ); |
17 | | - $check = implode( '/', $parts ) . $slug; |
| 17 | + $check = A::join( $parts, '/' ) . $slug; |
18 | 18 | } else { |
19 | 19 | $check = $slug; |
20 | 20 | } |
|
24 | 24 | break; |
25 | 25 | } |
26 | 26 |
|
27 | | -// loop through all existing routes, pick |
28 | | -// first word and add to array |
| 27 | +// loop through all existing routes and store full patterns |
29 | 28 | foreach ( kirby()->routes() as $route ) { |
30 | 29 | $pattern = $route[ 'pattern' ]; |
31 | | - $patternArray = array_values( array_filter( explode( '/', $pattern ), 'strlen' ) ); |
| 30 | + $patternArray = Str::split( $pattern, '/' ); |
32 | 31 |
|
33 | 32 | // avoiding empty arrays |
34 | 33 | if ( count( $patternArray ) > 0 ) { |
35 | | - $protected[] = $patternArray[ 0 ]; |
| 34 | + $protected[] = A::join( $patternArray, '/' ); |
36 | 35 | } |
37 | 36 | } |
| 37 | + |
38 | 38 | // merge, keep unique, and remove empty |
39 | | -$protected = array_values( array_unique( array_merge( $userProtected, $protected ) ) ); |
| 39 | +$protected = array_values( array_unique( A::merge( $userProtected, $protected ) ) ); |
| 40 | + |
| 41 | +// break down the check path into all levels |
| 42 | +$checkParts = Str::split( $check, '/' ); |
| 43 | +$levelsToCheck = []; |
| 44 | +for ( $i = 0; $i < count( $checkParts ); $i++ ) { |
| 45 | + $levelsToCheck[] = A::join( A::slice( $checkParts, 0, $i + 1 ), '/' ); |
| 46 | +} |
40 | 47 |
|
41 | | -// loop through all routes + user protected |
42 | | -// and throw error if it’s a match |
43 | | -foreach ( $protected as $item ) { |
44 | | - if ( $check == $item ) { |
| 48 | +// check each level against protected routes |
| 49 | +foreach ( $levelsToCheck as $level ) { |
| 50 | + if ( in_array( $level, $protected ) ) { |
45 | 51 | throw new Exception( t( 'andreasnymark.kirby-route-protect.protected', 'The URL-appendix is protected or already in use.' ) ); |
46 | 52 | } |
47 | 53 | } |
|
0 commit comments