PageRenderTime 40ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/administrator/components/com_joomlapack/includes/CFSAbstraction.php

https://bitbucket.org/dgough/annamaria-daneswood-25102012
PHP | 166 lines | 22 code | 7 blank | 137 comment | 7 complexity | 744aa61079a39b6532b3437ad274abbb MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * @package JoomlaPack
  4. * @subpackage BaseClasses
  5. * @copyright Copyright (C) 2006-2008 JoomlaPack Developers. All rights reserved.
  6. * @version $Id$
  7. * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL
  8. *
  9. * JoomlaPack is free software. This version may have been modified pursuant
  10. * to the GNU General Public License, and as distributed it includes or
  11. * is derivative of works licensed under the GNU General Public License or
  12. * other free or open source software licenses.
  13. **/
  14. // ensure this file is being included by a parent file - Joomla! 1.0.x and 1.5 compatible
  15. (defined( '_VALID_MOS' ) || defined('_JEXEC')) or die( 'Direct Access to this location is not allowed.' );
  16. /**
  17. * Filesystem Abstraction Module
  18. *
  19. * Provides filesystem handling functions in a compatible manner, depending on server's capabilities
  20. */
  21. class CFSAbstraction {
  22. /**
  23. * Should we use glob() ?
  24. * @var boolean
  25. */
  26. var $_globEnable;
  27. /**
  28. * Public constructor for CFSAbstraction class. Does some heuristics to figure out the
  29. * server capabilities and setup internal variables
  30. */
  31. function CFSAbstraction()
  32. {
  33. // Don't use glob if it's disabled or if opendir is available
  34. $this->_globEnable = function_exists('glob');
  35. if( function_exists('opendir') && function_exists('readdir') && function_exists('closedir') )
  36. $this->_globEnable = false;
  37. }
  38. /**
  39. * Searches the given directory $dirName for files and folders and returns a multidimensional array.
  40. * If the directory is not accessible, returns FALSE
  41. *
  42. * @param string $dirName
  43. * @param string $shellFilter
  44. * @return array See function description for details
  45. */
  46. function getDirContents( $dirName, $shellFilter = null )
  47. {
  48. if ($this->_globEnable) {
  49. return $this->_getDirContents_glob( $dirName, $shellFilter );
  50. } else {
  51. return $this->_getDirContents_opendir( $dirName, $shellFilter );
  52. }
  53. }
  54. // ============================================================================
  55. // PRIVATE SECTION
  56. // ============================================================================
  57. /**
  58. * Searches the given directory $dirName for files and folders and returns a multidimensional array.
  59. * If the directory is not accessible, returns FALSE. This function uses the PHP glob() function.
  60. * @return array See function description for details
  61. */
  62. function _getDirContents_glob( $dirName, $shellFilter = null )
  63. {
  64. if (is_null($shellFilter)) {
  65. // Get folder contents
  66. $allFilesAndDirs1 = @glob($dirName . "/*"); // regular files
  67. $allFilesAndDirs2 = @glob($dirName . "/.*"); // *nix hidden files
  68. // Try to merge the arrays
  69. if ($allFilesAndDirs1 === false) {
  70. if ($allFilesAndDirs2 === false) {
  71. $allFilesAndDirs = false;
  72. } else {
  73. $allFilesAndDirs = $allFilesAndDirs2;
  74. }
  75. } elseif ($allFilesAndDirs2 === false) {
  76. $allFilesAndDirs = $allFilesAndDirs1;
  77. } else {
  78. $allFilesAndDirs = @array_merge($allFilesAndDirs1, $allFilesAndDirs2);
  79. }
  80. // Free unused arrays
  81. unset($allFilesAndDirs1);
  82. unset($allFilesAndDirs2);
  83. } else {
  84. $allFilesAndDirs = @glob($dirName . "/$shellFilter"); // filtered files
  85. }
  86. // Check for unreadable directories
  87. if ( $allFilesAndDirs === FALSE ) {
  88. return FALSE;
  89. }
  90. // Populate return array
  91. $retArray = array();
  92. foreach($allFilesAndDirs as $filename) {
  93. $filename = CJVAbstract::TranslateWinPath( $filename );
  94. $newEntry['name'] = $filename;
  95. $newEntry['type'] = filetype( $filename );
  96. if ($newEntry['type'] == "file") {
  97. $newEntry['size'] = filesize( $filename );
  98. } else {
  99. $newEntry['size'] = 0;
  100. }
  101. $retArray[] = $newEntry;
  102. }
  103. return $retArray;
  104. }
  105. function _getDirContents_opendir( $dirName, $shellFilter = null )
  106. {
  107. $handle = @opendir( $dirName );
  108. // If directory is not accessible, just return FALSE
  109. if ($handle === FALSE) {
  110. return FALSE;
  111. }
  112. // Initialize return array
  113. $retArray = array();
  114. while( !( ( $filename = readdir($handle) ) === false) ) {
  115. $match = is_null( $shellFilter );
  116. $match = (!$match) ? fnmatch($shellFilter, $filename) : true;
  117. if ($match) {
  118. $filename = CJVAbstract::TranslateWinPath( $dirName . "/" . $filename );
  119. $newEntry['name'] = $filename;
  120. $newEntry['type'] = @filetype( $filename );
  121. if ($newEntry['type'] !== FALSE) {
  122. // FIX 1.1.0 Stable - When open_basedir restrictions are in effect, an attempt to read <root>/.. could result into failure of the backup. This fix is a simplistic workaround.
  123. if ($newEntry['type'] == 'file') {
  124. $newEntry['size'] = @filesize( $filename );
  125. } else {
  126. $newEntry['size'] = 0;
  127. }
  128. $retArray[] = $newEntry;
  129. }
  130. }
  131. }
  132. closedir($handle);
  133. return $retArray;
  134. }
  135. }
  136. // FIX 1.1.0 -- fnmatch not available on non-POSIX systems
  137. // Thanks to soywiz@php.net for this usefull alternative function [http://gr2.php.net/fnmatch]
  138. if (!function_exists('fnmatch')) {
  139. function fnmatch($pattern, $string) {
  140. return @preg_match(
  141. '/^' . strtr(addcslashes($pattern, '/\\.+^$(){}=!<>|'),
  142. array('*' => '.*', '?' => '.?')) . '$/i', $string
  143. );
  144. }
  145. }
  146. ?>