/modules/trivial/filesystem.php

https://github.com/dionyziz/blogcube · PHP · 157 lines · 99 code · 14 blank · 44 comment · 35 complexity · 3fabcf6f6ac49da17e462fdaa1cd7f09 MD5 · raw file

  1. <?php
  2. /*
  3. Developer: Makis
  4. ==COPYTREE class==
  5. Recursively copies all files from $sourcepath (fullpath)
  6. to $destpath (fullpath)
  7. excluding all directories that are items of the array $excludepaths (fullpath)
  8. and all files (or directories) globally, whose names are items of the array $excludefiles
  9. SAMPLE USAGE:
  10. 1) Initialization
  11. a) No Exclusions
  12. $copytree = New CopyTree( '/tmp/copytree', '/tmp' );
  13. b) With Exclusions
  14. $copytree = New CopyTree(
  15. '/tmp/copytree',
  16. '/tmp',
  17. array('/tmp/copytree/deleted'),
  18. array('.htaccess', '.svn')
  19. );
  20. 2) Layers of copying modes
  21. Layer #0) Add new: Copy all files WITHOUT overwriting existing ones.
  22. Layer #1) Freshen: Copy ONLY EXISTING files, overwriting them (don't copy files that do not exist in destination).
  23. Layer #2) Overwrite: Copy all files OVERWRITING existing ones.
  24. Usage of layers:
  25. Case a) Multilayered:
  26. //initialized, as described above
  27. $copytree->CopyLayer( 0 ); //add new files only (not affecting the system)
  28. //now, lock the system
  29. $copytree->CopyLayer( 1 ); //modify existing files (needs to be locked)
  30. //ready, unlock system
  31. Case b) One layer only
  32. //initialized, as described above
  33. $copytree->CopyLayer(2); //copy overwriting everything (needs to be locked)
  34. 3) Debugging
  35. After initialization, you can use $copytree->DebugMode( $level ) to set the debug level to either 0, 1, or 2:
  36. 0) No debug messages
  37. 1) Echo filenames and actions (verbose)
  38. 2) Also set everyone +rwx permissions, to make the copied files easily erasable after the test
  39. */
  40. if ( isset( $_GET['copytree'] ) ) {
  41. $copytree = New CopyTree( 'uploads', '/tmp/copytree', explode( '|', $_GET['copytree'] ), explode('|', '.svn|.htaccess') );
  42. $copytree->DebugMode( 2 );
  43. $copytree->CopyLayer( 0 );
  44. die();
  45. }
  46. Class CopyTree {
  47. private $mFiles;
  48. private $mSourcePath;
  49. private $mDestPath;
  50. private $mExcludePaths;
  51. private $mExcludeFiles;
  52. private $mDebugMode=0;
  53. public function GetResponse() {
  54. return $this->mResult;
  55. }
  56. public function DebugMode( $level='' ) {
  57. if ( $level == '' )
  58. return $this->mDebugMode;
  59. $this->mDebugMode = $level;
  60. return true;
  61. }
  62. //constructor
  63. public function CopyTree( $sourcepath, $destpath, $excludepaths=array(), $excludefiles=array() ) {
  64. $this->mSourcePath = $sourcepath;
  65. $this->mDestPath = $destpath;
  66. $this->mExcludePaths = $excludepaths;
  67. $this->mExcludeFiles = $excludefiles;
  68. if( !is_dir( $this->mSourcePath ) || !is_dir( $this->mDestPath ) || !is_array( $this->mExcludePaths ) || !is_array( $this->mExcludeFiles ) ) {
  69. die( 'Invalid paths or wrong parameters for CopyTree!'); //return false;
  70. }
  71. $this->mFiles = array();
  72. $this->mFiles = $this->getFilesListing( $this->mSourcePath );
  73. sort( $this->mFiles );
  74. }
  75. public function CopyLayer( $mode ) {
  76. if ( $this->mDebugMode>1 ) $perm = 0777; else $perm = 0; //debug: a+rwx
  77. foreach ( $this->mFiles as $src ) {
  78. $dest = $this->mDestPath . substr( $src, strlen( $this->mSourcePath ) );
  79. if ( $this->mDebugMode>0 ) { //verbose
  80. echo "Copying: ";
  81. if ( is_dir( $src ) ) echo ">"; echo nl2br( "$src --> $dest... " );
  82. }
  83. /* MODES (Layers) */
  84. if ( file_exists( $dest ) ) {
  85. if ( $mode == 0 ) {
  86. if ( $this->mDebugMode>0 ) echo nl2br( "skipped\n" ); //verbose
  87. continue; //don't overwrite
  88. }
  89. } else {
  90. if ( $mode == 1 ) {
  91. if ( $this->mDebugMode>0 ) echo nl2br( "not added\n" ); //verbose
  92. continue; //don't add, only freshen existing
  93. }
  94. }
  95. if ( is_file( $src ) ) {
  96. if ( @copy( $src, $dest ) ) {
  97. if ($perm > 0) chmod( $dest, $perm ); //debug: permissions
  98. touch( $dest, filemtime( $src ) ); // to track last modified time
  99. }
  100. else
  101. die( 'cannot copy file from '.$src.' to '.$dest );
  102. }
  103. if ( is_dir( $src ) && !is_dir( $dest ) ) {
  104. if ( @mkdir( $dest ) ) {
  105. if ($perm > 0) chmod( $dest, $perm ); //debug: permissions
  106. }
  107. else
  108. die( 'cannot create directory '.$dest );
  109. }
  110. if ( $this->mDebugMode>0 ) echo nl2br( "done\n" ); //verbose
  111. }
  112. return true;
  113. }
  114. private function getFilesListing( $directory ) {
  115. if( $dir = opendir( $directory ) ) {
  116. // Create an array for all files found
  117. $tmp = Array();
  118. // Add the files
  119. while( $file = readdir( $dir ) ) {
  120. if(
  121. $file != "." &&
  122. $file != ".." &&
  123. in_array( "$directory/$file", $this->mExcludePaths ) === false &&
  124. in_array( $file, $this->mExcludeFiles ) === false
  125. )
  126. {
  127. // If it's a directory, list all files within it
  128. if( is_dir( "$directory/$file" ) ) {
  129. $tmp2 = $this->getFilesListing( "$directory/$file" );
  130. if( is_array( $tmp2 ) ) {
  131. $tmp = array_merge( $tmp, $tmp2 );
  132. }
  133. }
  134. array_push( $tmp, "$directory/$file" );
  135. }
  136. }
  137. closedir( $dir );
  138. return $tmp;
  139. }
  140. }
  141. }
  142. ?>