/public/javascripts/dojo/dojox/data/demos/stores/filestore_dojoxdata.php

http://enginey.googlecode.com/ · PHP · 179 lines · 174 code · 2 blank · 3 comment · 1 complexity · 94d96f23417b07bf432f086185fc5134 MD5 · raw file

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