-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathnamespaceFinder.php
104 lines (89 loc) · 3.24 KB
/
namespaceFinder.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php
/**
* Plugin nspages : Displays nicely a list of the pages of a namespace
*
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
* @author Guillaume Turri <[email protected]>
*/
if(!defined('DOKU_INC')) die();
class namespaceFinder {
private $wantedNs;
private $isSafe;
/**
* Resolves the namespace on construction
*
* @param string $path the namespace link
*/
function __construct($path){
$this->wantedNs = $this->computeWantedNs($path);
$this->sanitizeNs();
}
private function computeWantedNs($wantedNS){
global $ID;
// Convert all other separators to colons
// Nb: slashes should be accepted as separator too as they can be a legit separator when the DW conf "useslash" is on. (and anyway slashes are not allowed in page name
// (see https://www.dokuwiki.org/pagename ) so it's the only correct way to deal with it.
// But we don't need to str_replace it because we don't go through "cleanID" (which would handle it when the conf is off) and because we never remove nor escape the slashes
// before they are converted to a FS path
$wantedNS = str_replace(';', ':', $wantedNS); // accepted by DW as namespace separator according to https://www.dokuwiki.org/pagename
$result = '';
if($wantedNS == '') {
$wantedNS = $this->getCurrentNamespace();
}
if( $this->isRelativePath($wantedNS) ) {
$result = getNS($ID);
// normalize initial dots ( ..:..abc -> ..:..:abc )
$wantedNS = preg_replace('/^((\.+:)*)(\.+)(?=[^:\.])/', '\1\3:', $wantedNS);
} elseif ( $this->isPageRelativePath($wantedNS) ) {
$result = $ID;
$wantedNS = substr($wantedNS, 1);
}
$result .= ':'.$wantedNS.':';
return $result;
}
private function getCurrentNamespace(){
return '.';
}
private function isRelativePath($path){
return $path[0] == '.';
}
private function isPageRelativePath($path){
return $path[0] == '~';
}
/**
* Get rid of '..'.
* Therefore, provides a ns which passes the cleanid() function,
*/
private function sanitizeNs(){
$ns = explode(':', $this->wantedNs);
for($i = 0; $i < count($ns); $i++) {
if($ns[$i] === '' || $ns[$i] === '.') {
array_splice($ns, $i, 1);
$i--;
} else if($ns[$i] == '..') {
if($i == 0) {
//the first can't be '..', to stay inside 'data/pages'
break;
} else {
//simplify the path, getting rid of 'ns:..'
array_splice($ns, $i - 1, 2);
$i -= 2;
}
}
}
$this->isSafe = (count($ns) == 0 || $ns[0] != '..');
$this->wantedNs = implode(':', $ns);
}
function getWantedNs(){
return $this->wantedNs;
}
function isNsSafe(){
return $this->isSafe;
}
function getWantedDirectory(){
return $this->namespaceToDirectory($this->wantedNs);
}
static function namespaceToDirectory($ns){
return utf8_encodeFN(str_replace(':', '/', $ns));
}
}