This repository has been archived by the owner on Oct 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDecliser.php
137 lines (114 loc) · 3.27 KB
/
Decliser.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
class Decliser {
public $config;
public $currentDir= NULL;
public $files = [];
public $breadcrumb;
public function __construct($config) {
$this->config = $config;
$this->currentDir = $this->config['root_directory'];
if (isset($_GET['url'])) {
$url = $_GET['url'];
$this->currentDir = $url;
}
return $this->LoadDir($this->currentDir);
}
public function LoadDir($path)
{
$files = $this->__scanDir($path);
if (!empty($files)) {
$files = $this->__ignored($files);
foreach ($files as $file) {
$filePath= realpath($this->currentDir.'/'.$file);
$fileUrl = $this->config['directory_url'];
if ($this->config['root_directory'] != '' && $this->currentDir != '.' && $this->currentDir != './') {
if (is_dir($filePath)) {
$fileUrl .= '?url=' . $this->currentDir . '/' . $file;
}
else {
$fileUrl .= $this->currentDir . '/' . $file;
}
}
else {
if (is_dir($filePath)) {
$fileUrl .= '?url=' . $file;
}
else{
$fileUrl .= $file;
}
}
$fileExt = ( is_file($filePath) ) ? $this->__getFileExt( $file ) : 'dir';
$type = (is_dir($filePath)) ? 'directory' : 'file';
$target = (!is_dir($filePath) && $this->config['open_new_tab'] === TRUE) ? '_blank' : '_self';
$fileList[$file] = [
'name' => $file,
'type' => $type,
'path' => $filePath,
'url' => $fileUrl,
'target'=> $target,
'ext' => $fileExt
];
}
}
$this->files = @$fileList;
$this->breadcrumb = $this->breadcrumb();
}
protected function __scanDir($dir){
if (strstr($dir,'../')) return false;
if ($dir == '/' || $dir == '') {
$dir = $this->config['root_directory'];
$this->currentDir = $dir;
}
if (in_array($dir, $this->config['hide_directories'])) return false;
// if (in_array($stripped, $this->config['hide_directories'])) return;
// if (! file_exists($dir) || !is_dir($dir)) {
// return false;
// }
return scandir($dir);
}
protected function __ignored($files){
foreach ($files as $key => $file) {
if (is_dir(realpath($file)) && in_array($file, $this->config['hide_directories']))
unset($files[$key]);
if ($this->config['hide_dotdir'] === TRUE) {
if ( is_dir(realpath($file)) && substr($file, 0, 1) == '.' ) {
unset($files[$key]);
}
}
if (in_array($file, $this->config['hide_files'])) unset($files[$key]);
if ( $this->config['hide_dotfiles'] === TRUE) {
if ( is_file(realpath($file)) && substr($file, 0, 1) == '.' ) {
unset($files[$key]);
}
}
if ( ! is_dir( realpath( $file ) ) ) {
$file_ext = $this->__getFileExt($file);
if (in_array($file_ext, $this->config['hide_by_ext'])) unset($files[$key]);
}
}
return $files;
}
private function breadcrumb(){
$dir = $this->currentDir;
$breadcrumb = [];
$breadcrumb['./'] = 'home';
if (substr_count($dir,'/') >= 0) {
$items = explode('/', $dir);
$items = array_filter($items);
$path = '';
foreach ($items as $item) {
if ($item == '.' || $item == '..') {
continue;
}
$path .= rawurlencode($item) . '/';
$breadcrumb[$path] = $item;
}
$breadcrumb = array_filter($breadcrumb);
return $breadcrumb;
}
}
protected function __getFileExt($filename)
{
return substr( strrchr( $filename,'.' ),1 );
}
}