forked from gturri/nspages
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnamespaceFinder.php
79 lines (68 loc) · 1.98 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
<?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;
function __construct($path){
$this->wantedNs = $this->computeWantedNs($path);
$this->sanitizeNs();
}
private function computeWantedNs($path){
global $ID;
$result = '';
$wantedNS = trim($path);
if($wantedNS == '') {
$wantedNS = $this->getCurrentNamespace();
}
if( $this->isRelativePath($wantedNS) ) {
$result = getNS($ID);
}
$result .= ':'.$wantedNS.':';
return $result;
}
private function getCurrentNamespace(){
return '.';
}
private function isRelativePath($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 = ($ns[0] != '..');
$this->wantedNs = implode(':', $ns);
}
function getWantedNs(){
return $this->wantedNs;
}
function isNsSafe(){
return $this->isSafe;
}
function getWantedDirectory(){
return utf8_encodeFN(str_replace(':', '/', $this->wantedNs));
}
}