PageRenderTime 23ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/ext-4.1.0_b3/docs/extjs/examples/tree/get-nodes.php

https://bitbucket.org/srogerf/javascript
PHP | 80 lines | 71 code | 7 blank | 2 comment | 17 complexity | 26c101e3d420de31e52c08fe16ad582e MD5 | raw file
  1. <?php
  2. // from php manual page
  3. function formatBytes($val, $digits = 3, $mode = 'SI', $bB = 'B'){ //$mode == 'SI'|'IEC', $bB == 'b'|'B'
  4. $si = array('', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y');
  5. $iec = array('', 'Ki', 'Mi', 'Gi', 'Ti', 'Pi', 'Ei', 'Zi', 'Yi');
  6. switch(strtoupper($mode)) {
  7. case 'SI' : $factor = 1000; $symbols = $si; break;
  8. case 'IEC' : $factor = 1024; $symbols = $iec; break;
  9. default : $factor = 1000; $symbols = $si; break;
  10. }
  11. switch($bB) {
  12. case 'b' : $val *= 8; break;
  13. default : $bB = 'B'; break;
  14. }
  15. for($i=0;$i<count($symbols)-1 && $val>=$factor;$i++)
  16. $val /= $factor;
  17. $p = strpos($val, '.');
  18. if($p !== false && $p > $digits) $val = round($val);
  19. elseif($p !== false) $val = round($val, $digits-$p);
  20. return round($val, $digits) . ' ' . $symbols[$i] . $bB;
  21. }
  22. // grab the custom params
  23. $path = isset($_REQUEST['path'])&&$_REQUEST['path'] == 'extjs' ? '../../../' : '../../';
  24. $node = isset($_REQUEST['node']) ? $_REQUEST['node'] : '';
  25. $isXml = isset($_REQUEST['isXml']);
  26. if(strpos($node, '..') !== false){
  27. die('Nice try buddy.');
  28. }
  29. $nodes = array();
  30. $directory = $path.$node;
  31. if (is_dir($directory)){
  32. $d = dir($directory);
  33. while($f = $d->read()){
  34. if($f == '.' || $f == '..' || substr($f, 0, 1) == '.') continue;
  35. $filename = $directory . '/' . $f;
  36. date_default_timezone_set('America/Los_Angeles');
  37. $lastmod = date('M j, Y, g:i a', filemtime($filename));
  38. if(is_dir($directory.'/'.$f)){
  39. $qtip = 'Type: Folder<br />Last Modified: '.$lastmod;
  40. $nodes[] = array(
  41. 'text' => $f,
  42. 'id' => $node.'/'.$f,
  43. 'cls' => 'folder'
  44. );
  45. } else {
  46. $size = formatBytes(filesize($filename), 2);
  47. $qtip = 'Type: JavaScript File<br />Last Modified: '.$lastmod.'<br />Size: '.$size;
  48. $nodes[] = array(
  49. 'text' => $f,
  50. 'id' => $node.'/'.$f,
  51. 'leaf' => true,
  52. 'cls' => 'file'
  53. );
  54. }
  55. }
  56. $d->close();
  57. }
  58. if ($isXml) {
  59. $xmlDoc = new DOMDocument();
  60. $root = $xmlDoc->appendChild($xmlDoc->createElement("nodes"));
  61. foreach ($nodes as $node) {
  62. $xmlNode = $root->appendChild($xmlDoc->createElement("node"));
  63. $xmlNode->appendChild($xmlDoc->createElement("text", $node['text']));
  64. $xmlNode->appendChild($xmlDoc->createElement("id", $node['id']));
  65. $xmlNode->appendChild($xmlDoc->createElement("cls", $node['cls']));
  66. $xmlNode->appendChild($xmlDoc->createElement("leaf", isset($node['leaf'])));
  67. }
  68. header("Content-Type: text/xml");
  69. $xmlDoc->formatOutput = true;
  70. echo $xmlDoc->saveXml();
  71. } else {
  72. echo json_encode($nodes);
  73. }