Debian Codegraph @

dojox/data/demos/stores/filestore_dojoxdata.php

raw symbols SCIP
1 <?php
2 //Define the root directory to use for this service.
3 //All file lookups are relative to this path.
4 $rootDir = "../..";
5
6 require_once("filestore_funcs.php");
7
8 //Extract the query, if any.
9 $query = false;
10 if (array_key_exists("query", $_GET)) {
11 $query = $_GET['query'];
12 $query = str_replace("\\\"", "\"", $query);
13 $query = json_decode($query, true);
14 }
15 //Extract relevant query options.
16 $queryOptions = json_decode("{}");
17 $deep = false;
18 $ignoreCase = false;
19 if (array_key_exists("queryOptions", $_GET)) {
20 $queryOptions = $_GET['queryOptions'];
21 $queryOptions = str_replace("\\\"", "\"", $queryOptions);
22 $queryOptions = json_decode($queryOptions);
23 if (property_exists($queryOptions, "deep")) {
24 $deep = $queryOptions->deep;
25 }
26 if (property_exists($queryOptions, "ignoreCase")) {
27 $ignoreCase = $queryOptions->ignoreCase;
28 }
29 }
30
31 //Extract non-dojo.data spec config options.
32 $expand = false;
33 $dirsOnly = false;
34 $showHiddenFiles = false;
35 $options = array();
36 if (array_key_exists("options", $_GET)) {
37 $options = $_GET['options'];
38 $options = str_replace("\\\"", "\"", $options);
39 $options = json_decode($options);
40 if (array_search("expand", $options) > -1) {
41 $expand = true;
42 }
43 if (array_search("dirsOnly", $options) > -1) {
44 $dirsOnly = true;
45 }
46 if (array_search("showHiddenFiles", $options) > -1) {
47 $showHiddenFiles = true;
48 }
49 }
50
51
52 //See if a specific file was requested, or if it is just a query for files.
53 $path = false;
54 if (array_key_exists("path", $_GET)) {
55 $path = $_GET['path'];
56 }
57
58 if (!is_string($path)) {
59
60 $files = array();
61
62 //Handle query for files. Must try to generate patterns over the query
63 //attributes.
64 $patterns = array();
65 if (is_array($query)) {
66 //Generate a series of RegExp patterns as necessary.
67 $keys = array_keys($query);
68 $total = count($keys);
69 if ($total > 0) {
70 for ($i = 0; $i < $total; $i++) {
71 $key = $keys[$i];
72 $pattern = $query[$key];
73 if (is_string($pattern)) {
74 $patterns[$key] = patternToRegExp($pattern);
75 }
76 }
77 $files = matchFiles($query, $patterns, $ignoreCase, ".", $rootDir, $deep, $dirsOnly, $expand, $showHiddenFiles);
78 } else {
79 $files = getAllFiles(".",$rootDir,$deep,$dirsOnly,$expand,$showHiddenFiles);
80 }
81 }else{
82 $files = getAllFiles(".",$rootDir,$deep,$dirsOnly,$expand,$showHiddenFiles);
83 }
84
85 $total = count($files);
86
87 //Handle the sorting and paging.
88 $sortSpec = false;
89 if (array_key_exists("sort", $_GET)) {
90 $sortSpec = $_GET['sort'];
91 $sortSpec = str_replace("\\\"", "\"", $sortSpec);
92 $sortSpec = json_decode($sortSpec);
93 }
94
95 if ($sortSpec != null) {
96 $comparator = createComparator($sortSpec);
97 usort($files,array($comparator, "compare"));
98 }
99
100 //Page, if necessary.
101 if (array_key_exists("start", $_GET)) {
102 $start = $_GET['start'];
103 if (!is_numeric($start)) {
104 $start = 0;
105 }
106 $files = array_slice($files, $start);
107 }
108 if (array_key_exists("count", $_GET)) {
109 $count = $_GET['count'];
110 if (!is_numeric($count)) {
111 $count = $total;
112 }
113 $files = array_slice($files, 0, $count);
114 }
115
116 $result;
117 $result->total = $total;
118 $result->items = $files;
119 header("Content-Type", "text/json");
120 print("/* ".json_encode($result)." */");
121 } else {
122 //Query of a specific file (useful for fetchByIdentity and loadItem)
123
124 //Make sure the path isn't trying to walk out of the rooted directory
125 //As defined by $rootDir in the top of the php script.
126 $rootPath = realPath($rootDir);
127 $fullPath = realPath($rootPath."/".$path);
128
129 if ($fullPath !== false) {
130 if (strpos($fullPath,$rootPath) === 0) {
131 //Root the path into the tree cleaner.
132 if (strlen($fullPath) == strlen($rootPath)) {
133 $path = ".";
134 } else {
135 //Fix the path to relative of root and put back into UNIX style (even if windows).
136 $path = substr($fullPath,(strlen($rootPath) + 1),strlen($fullPath));
137 $path = str_replace("\\", "/", $path);
138 }
139
140 if (file_exists($fullPath)) {
141 $arr = split("/", $path);
142 $size = count($arr);
143
144 if ($size > 0) {
145 $fName = $arr[$size - 1];
146 if ($size == 1) {
147 print("Setting path to: .");
148 $path = ".";
149 } else {
150 $path = $arr[0];
151 }
152 for ($i = 1; $i < ($size - 1); $i++) {
153 $path = $path."/".$arr[$i];
154 }
155 $file = generateFileObj($fName, $path, $rootDir, $expand,$showHiddenFiles);
156 header("Content-Type", "text/json");
157 print("/* ".json_encode($file)." */");
158 } else {
159 header("HTTP/1.0 404 Not Found");
160 header("Status: 404 Not Found");
161 print("<b>Cannot access file: [".htmlentities($path)."]<b>");
162 }
163 } else {
164 header("HTTP/1.0 404 Not Found");
165 header("Status: 404 Not Found");
166 print("<b>Cannot access file: [".htmlentities($path)."]<b>");
167 }
168 } else {
169 header("HTTP/1.0 403 Forbidden");
170 header("Status: 403 Forbidden");
171 print("<b>Cannot access file: [".htmlentities($path)."]. It is outside of the root of the file service.<b>");
172 }
173 } else {
174 header("HTTP/1.0 404 Not Found");
175 header("Status: 404 Not Found");
176 print("<b>Cannot access file: [".htmlentities($path)."]<b>");
177 }
178 }
179 ?>