1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpTek\Exodus\Processor; |
4
|
|
|
|
5
|
|
|
use PhpTek\Exodus\Tool\StaticSiteUrlProcessor; |
6
|
|
|
use SilverStripe\Core\Injector\Injectable; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Base processor: Drops file extensions. |
10
|
|
|
*/ |
11
|
|
|
class StaticSiteURLProcessorDropExtensions implements StaticSiteUrlProcessor |
12
|
|
|
{ |
13
|
|
|
use Injectable; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* |
17
|
|
|
* @return string |
18
|
|
|
*/ |
19
|
|
|
public function getName(): string |
20
|
|
|
{ |
21
|
|
|
return "Simple Processor"; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* |
26
|
|
|
* @return string |
27
|
|
|
*/ |
28
|
|
|
public function getDescription(): string |
29
|
|
|
{ |
30
|
|
|
return "Removes extensions and trailing slashes."; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* |
35
|
|
|
* @param array $urlData |
36
|
|
|
* @return array |
37
|
|
|
*/ |
38
|
|
|
public function processURL(array $urlData): array |
39
|
|
|
{ |
40
|
|
|
if (!is_array($urlData) || empty($urlData['url'])) { |
|
|
|
|
41
|
|
|
return []; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$url = ''; |
45
|
|
|
|
46
|
|
|
// With query string |
47
|
|
|
if (preg_match("#^([^?]*)\?(.*)$#", $urlData['url'], $matches)) { |
48
|
|
|
$url = $matches[1]; |
49
|
|
|
$qs = $matches[2]; |
50
|
|
|
$url = preg_replace("#\.[^./?]*$#", "$1", $url); |
51
|
|
|
$url = $this->postProcessUrl($url); |
52
|
|
|
|
53
|
|
|
return [ |
54
|
|
|
'url' => "$url?$qs", |
55
|
|
|
'mime' => $urlData['mime'], |
56
|
|
|
]; |
57
|
|
|
} else { |
58
|
|
|
// No query string |
59
|
|
|
$url = $urlData['url']; |
60
|
|
|
$url = preg_replace("#\.[^./?]*$#", "$1", $url); |
61
|
|
|
|
62
|
|
|
return [ |
63
|
|
|
'url' => $this->postProcessUrl($url), |
64
|
|
|
'mime' => $urlData['mime'], |
65
|
|
|
]; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Post-processes urls for common issues like encoded brackets and slashes that we wish to apply to all URL |
71
|
|
|
* Processors. |
72
|
|
|
* |
73
|
|
|
* @param string $url |
74
|
|
|
* @return string |
75
|
|
|
* @todo Instead of testing for arbitrary URL irregularities, |
76
|
|
|
* can we not just clean-out chars that don't adhere to HTTP1.1 or the appropriate RFC? |
77
|
|
|
*/ |
78
|
|
|
private function postProcessUrl(string $url): string |
79
|
|
|
{ |
80
|
|
|
// Replace all encoded slashes with non-encoded versions |
81
|
|
|
$noSlashes = str_ireplace('%2f', '/', $url); |
82
|
|
|
// Replace all types of brackets |
83
|
|
|
$noBrackets = str_replace(array('%28', '(', ')'), '', $noSlashes); |
84
|
|
|
// Return, ensuring $url never has >1 consecutive slashes e.g. /blah//test |
85
|
|
|
return preg_replace("#[^:]/{2,}#", '/', $noBrackets); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|