Skip to content

Commit 8a63551

Browse files
committed
Check for multiple levels, not only first part
1 parent e51873b commit 8a63551

File tree

3 files changed

+18
-12
lines changed

3 files changed

+18
-12
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
A [Kirby](https://getkirby.com) plugin, protecting panel user from creating page URL in conflict with routes.
66

7-
**Version tested:** 3.9.0, 4.0.1, 5.0.0-beta.4
7+
**Version tested:** 3.9.0, 4.0.1, 5.0.4
88

99
## Installation
1010

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "andreasnymark/kirby-route-protect",
33
"description": "Protect panel user from creating page URL in conflict with routes.",
44
"homepage": "https://github.com/andreasnymark/kirby-route-protect",
5-
"version": "1.0.1",
5+
"version": "1.1.0",
66
"license": "MIT",
77
"type": "kirby-plugin",
88
"authors": [

route-protect.php

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
if ( count( $parts ) > 1 ) {
1515
// remove last word, adding new slug
1616
array_pop( $parts );
17-
$check = implode( '/', $parts ) . $slug;
17+
$check = A::join( $parts, '/' ) . $slug;
1818
} else {
1919
$check = $slug;
2020
}
@@ -24,24 +24,30 @@
2424
break;
2525
}
2626

27-
// loop through all existing routes, pick
28-
// first word and add to array
27+
// loop through all existing routes and store full patterns
2928
foreach ( kirby()->routes() as $route ) {
3029
$pattern = $route[ 'pattern' ];
31-
$patternArray = array_values( array_filter( explode( '/', $pattern ), 'strlen' ) );
30+
$patternArray = Str::split( $pattern, '/' );
3231

3332
// avoiding empty arrays
3433
if ( count( $patternArray ) > 0 ) {
35-
$protected[] = $patternArray[ 0 ];
34+
$protected[] = A::join( $patternArray, '/' );
3635
}
3736
}
37+
3838
// 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+
}
4047

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 ) ) {
4551
throw new Exception( t( 'andreasnymark.kirby-route-protect.protected', 'The URL-appendix is protected or already in use.' ) );
4652
}
4753
}

0 commit comments

Comments
 (0)