PageRenderTime 49ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/libs/SEOframework/Dir.php

http://sifo.googlecode.com/
PHP | 138 lines | 71 code | 16 blank | 51 comment | 13 complexity | efee3132d9508a63e2a9ca4edfeb4c4b MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0, BSD-3-Clause
  1. <?php
  2. /**
  3. * LICENSE
  4. *
  5. * Copyright 2010 Albert Lombarte
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. */
  20. /**
  21. * Old-school Directory class.
  22. *
  23. * TODO: Migrate to SPL when PHP 5.3 is found easily on shared hostings.
  24. * Directory interaction for those not being able to use RecursiveDirectoryIterator
  25. * @deprecated Use Directory.php for new implementations.
  26. */
  27. class Dir
  28. {
  29. /**
  30. * List of files and dires skipped in scans.
  31. *
  32. * @var array
  33. */
  34. protected $ignored_files = array( '.', '..', '.svn', '.git', '.DS_Store', 'Thumbs.db', '_smarty' );
  35. /**
  36. * Returns an array containing a list of files found recursively for a given path.
  37. *
  38. * Every item in the array has a subarray containing the following keys:
  39. * "name" => Filename or Dirname
  40. * "relative" => Path relative to the starting point
  41. * "absolute" => Absolute path
  42. * "folder" => Parent folder
  43. *
  44. * @param string $base_path Path to starting directory.
  45. * @param string $relative_path Relative path added to base_path. If you have /path/to/dir and specify "dir" as relative_path then will list files in /path/to as dir/file1, dir/file2 ...
  46. */
  47. public function getFileListRecursive( $base_path, $relative_path = "" )
  48. {
  49. // If base path ends in / remove it.
  50. if ( substr( $base_path, -1, 1 ) == "/" )
  51. {
  52. $base_path = substr( $base_path, 0, -1 );
  53. }
  54. // If relative path starts in / remove it.
  55. if ( substr( $relative_path, 0, 1 ) == "/" )
  56. {
  57. $relative_path = substr( $relative_path, 1 );
  58. }
  59. $path = $base_path . "/" . $relative_path;
  60. if ( !is_dir( $path ) )
  61. {
  62. return false;
  63. }
  64. $list = array();
  65. $directory = opendir( "$path" );
  66. while ( $file = readdir( $directory ) )
  67. {
  68. if ( !in_array( $file, $this->ignored_files ) )
  69. {
  70. $f = $path . "/" . $file;
  71. $f = preg_replace( '/(\/){2,}/', '/', $f ); // Replace double slashes.
  72. if ( is_file( $f ) )
  73. {
  74. $list[] = array( "filename" => $file, "relative" => $relative_path . "/$file", "absolute" => $f, "folder"=> $relative_path );
  75. }
  76. if ( is_dir( $f ) ) // Ignore _smarty dir
  77. {
  78. $list = array_merge( $list, $this->getFileListRecursive( $base_path, $relative_path . "/$file" ) ); // Recursive call.
  79. }
  80. }
  81. }
  82. closedir( $directory );
  83. sort( $list );
  84. return $list ;
  85. }
  86. /**
  87. * Get subdirs
  88. *
  89. * @param string $path
  90. * @param string $relative_path
  91. * @return array
  92. */
  93. public function getDirs( $path )
  94. {
  95. if ( !is_dir( $path ) )
  96. {
  97. return false;
  98. }
  99. $list = array();
  100. $directory = opendir( "$path" );
  101. while ( $file = readdir( $directory ) )
  102. {
  103. if ( !in_array( $file, $this->ignored_files ) )
  104. {
  105. $f = $path . "/" . $file;
  106. $f = preg_replace( '/(\/){2,}/', '/', $f ); // Replace double slashes.
  107. if ( is_dir( $f ) )
  108. {
  109. $list[] = $file;
  110. }
  111. }
  112. }
  113. closedir( $directory );
  114. sort( $list );
  115. return $list;
  116. }
  117. public function setIgnore( Array $ignored_files )
  118. {
  119. $this->ignored_files = $ignored_files;
  120. }
  121. }
  122. ?>