Guest User

Untitled

a guest
Jan 24th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.87 KB | None | 0 0
  1. <?php
  2. /*
  3. * Directory enumeration and presentation for jQuery TreeView Widget
  4. * Author: Ryan Bales, 2011
  5. */
  6. class DirectoryEnumerator
  7. {
  8. private $enumeration_list = array();
  9. private $directory;
  10. private $public_directory;
  11.  
  12. /**
  13. * __construct
  14. * The constructor
  15. * @param string $directory
  16. * @access public
  17. * @return void
  18. */
  19. public function __construct($directory=null,$public_directory=null)
  20. {
  21. if( $directory === null )
  22. $this->directory = SITE_LOC;
  23. else
  24. $this->directory = $directory;
  25. if( $public_directory === null )
  26. $this->public_directory = '';
  27. else
  28. $this->public_directory = $public_directory;
  29. }
  30. /**
  31. * enumerate
  32. * Bootstraps recursive enumeration
  33. * @access public
  34. * @return void
  35. */
  36. public function enumerate()
  37. {
  38. return $this->recursively_enumerate($this->directory);
  39. }
  40. /**
  41. * getRecursiveTree
  42. * Method outputs the directory tree for jQuery's TreeView plugin
  43. * @param array $directory
  44. * @access public
  45. * @return void
  46. */
  47. public function getRecursiveTree($directory,$parent=null)
  48. {
  49. if( $parent === null )
  50. $parent = $this->public_directory;
  51. // create the output buffer
  52. $ob = "";
  53. foreach( $directory as $child_name => $child )
  54. {
  55. if( is_array($child) && !empty($child) )
  56. {
  57. $ob .= '<li class="closed">';
  58. } else {
  59. $ob .= '<li>';
  60. }
  61. if( is_array($child) )
  62. {
  63. $ob .= '<span class="folder">' . $child_name . '</span>';
  64. // recurse
  65. $ob .= "<ul>" . $this->getRecursiveTree($child,implode('/',array($parent,$child_name))) . "</ul>";
  66. }
  67. else {
  68. // push file object
  69. $ob .= '<li><span class="file"><a href="' . implode('/',array($parent,$child)) . '">' . $child . '</a></span></li>';
  70. }
  71. $ob .= "</li>";
  72. }
  73. return $ob;
  74. }
  75. /**
  76. * recursively_enumerate
  77. * Recursively enumerates a directory structure, returning a dictionary
  78. * Directories are arrays, empty or containing their files, and files are simple strings
  79. * @param string $directory
  80. * @access private
  81. * @return array
  82. */
  83. private function recursively_enumerate($directory)
  84. {
  85. $directory_name_parts = explode('/',$directory);
  86. $directory_basename = (!empty($directory_name_parts)) ? $directory_name_parts[(count($directory_name_parts)-1)] : null;
  87. if( $directory_basename !== null )
  88. {
  89. // create the return object
  90. $directory_list = array();
  91. // scan the directory
  92. $children = scandir($directory);
  93. foreach($children as $child) {
  94. // don't recurse up the directory tree or stall or search DS_Store
  95. if( !in_array($child,array('.','..','.DS_Store')) )
  96. {
  97. $child_pathname = sprintf("%s/%s",$directory,$child);
  98. if( is_dir($child_pathname) )
  99. {
  100. // recurse
  101. $directory_list[$child] = $this->recursively_enumerate($child_pathname);
  102. }
  103. elseif( is_file($child_pathname) )
  104. {
  105. // push
  106. array_push($directory_list,$child);
  107. }
  108. }
  109. }
  110. return $directory_list;
  111. }
  112. }
  113. }
Add Comment
Please, Sign In to add comment