This repository has been archived by the owner on Apr 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcontroller.php
executable file
·114 lines (107 loc) · 3.51 KB
/
controller.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
<?php
/*
* Copyright (c) Codiad & Andr3as, distributed
* as-is and without warranty under the MIT License.
* See http://opensource.org/licenses/MIT for more information.
* This information must remain intact.
*/
error_reporting(0);
require_once('../../common.php');
checkSession();
switch($_GET['action']) {
case 'getFiles':
if (isset($_GET['path'])) {
$file = getWorkspacePath($_GET['path']);
$all = scanProject($file);
$result = array();
foreach($all as $one) {
//Get file info
$ext = getExtension($one);
$path = substr($one, strlen($file)+1);
if (isset($result[$ext])) {
array_push($result[$ext], $path);
} else {
$result[$ext] = array($path);
}
}
echo json_encode($result);
} else {
echo '{"status":"error","message":"Missing Parameter"}';
}
break;
default:
echo '{"status":"error","message":"No Type"}';
break;
}
//////////////////////////////////////////////////////////
//
// Scan folder
//
// @param {string} $path Path of the file or project
// @returns {array} Array of files, recursivly
//
//////////////////////////////////////////////////////////
function scanProject($path) {
$completeArray = array();
$files = scandir($path);
foreach ($files as $file) {
//filter . and ..
$longPath = $path."/".$file;
if ($file != "." && $file != ".." && !is_link($longPath)) {
//check if $file is a folder
if (is_dir($longPath)) {
//scan dir
$parsedArray = scanProject($longPath);
$completeArray = array_merge($completeArray, $parsedArray);
} else {
array_push($completeArray, $longPath);
}
}
}
return $completeArray;
}
//////////////////////////////////////////////////////////
//
// Get extension of file
//
// @param {string} $path Path of file
// @returns {string} Extension of file
//
//////////////////////////////////////////////////////////
function getExtension($path) {
$name = basename($path);
$pos = strrpos($name, '.');
if ($pos !== false) {
return substr($name, $pos + 1);
} else {
return "no-ext";
}
}
//////////////////////////////////////////////////////////
//
// Get path of file or project
//
// @param {string} $path Info of the file or project
// @returns {string} Path of file or project
//
//////////////////////////////////////////////////////////
function getWorkspacePath($path) {
//Security check
if (!Common::checkPath($path)) {
die('{"status":"error","message":"Invalid path"}');
}
if (strpos($path, "/") === 0) {
//Unix absolute path
return $path;
}
if (strpos($path, ":/") !== false) {
//Windows absolute path
return $path;
}
if (strpos($path, ":\\") !== false) {
//Windows absolute path
return $path;
}
return WORKSPACE . "/" . $path;
}
?>