/_plugins_/tinymce/tinymce_1_9_1_et_plus/inc/dirtool.php

https://bitbucket.org/pombredanne/spip-zone-treemap · PHP · 195 lines · 131 code · 18 blank · 46 comment · 31 complexity · 8aef286915a6d7a2312a7c4a708a05b3 MD5 · raw file

  1. <?php
  2. /* Do not remove or alter this section***************************
  3. ************************Class Description************************
  4. dirtool (c) Nov 2005 Uwe Stein
  5. dirtool allows to copy, delete and move complete directory-trees
  6. *****************************************************************
  7. ************************ sorry and thx ***************************
  8. Please excuse errors in this text. English isnt my native language,
  9. and so suggestions about the code and the spelling are welcome
  10. *********************Contact and Bug report***********************
  11. contact me using the "contact-button" at one of my packages at phpclasses.org
  12. ********************Licence****************************************
  13. This software is covered by The GNU General Public License (GPL)
  14. ***************************************************************
  15. **************End of do not remove or alter section*************************/
  16. /* CDE : ACTi : modified preg_replace by str_replace */
  17. // get the value of slash according to the OS
  18. function slash(){
  19. if(isset($_SERVER['OS']))
  20. return "\\";
  21. else
  22. return "/";
  23. }
  24. class dirtool {
  25. var $path;
  26. var $from;
  27. var $to;
  28. var $aContent= array();
  29. var $debug = FALSE;
  30. function dirtool($path) {
  31. if (!is_dir($path))
  32. die("<br><strong>$path is NOT a directory</strong>");
  33. $this->path = realpath($path);
  34. $from = "";
  35. $to="";
  36. // read the directory
  37. $slash = slash();
  38. $verz=opendir ($path);
  39. while ($file = readdir ($verz)) {
  40. if ($file != "." && $file != "..") {
  41. $tmp = $this->path.$slash.$file;
  42. if (is_dir($tmp))
  43. $this->aContent[] = new dirtool($tmp);
  44. else
  45. $this->aContent[] = $tmp;
  46. } //if
  47. } // while
  48. closedir($verz);
  49. } //End of func directory
  50. function move($newLocation) {
  51. $perm = fileperms($this->path);
  52. $this->copy($newLocation,$perm); // :-)
  53. $this->delete();
  54. chmod($newLocation,$perm);
  55. }
  56. function copy($path, $mode, $from = "") {
  57. $this->copy_tree($path, $mode, $from = "");
  58. $this->copy_files($path, $mode, $from = "");
  59. } // End of func copy
  60. function delete(){
  61. $this->delete_files();
  62. // because $this contains still the files in the array aContent
  63. // a new dir-object is created. The new one, read the tree again and contains only the
  64. //subdirectories (the files are now deleted )
  65. $dummy = new dirtool($this->path);
  66. $dummy->debug($this->debug); // copy the debug-state
  67. $dummy->delete_tree();
  68. }
  69. function copy_tree($path, $mode, $from = "") {
  70. if (!mkdir($path, $mode))
  71. die ("Error: directory $path could not be created");
  72. if ($this->debug == TRUE)
  73. echo "<br>Directory <b>$path</b> created";
  74. chmod($path,$mode);
  75. // at the first loop of recursiv callings keep the "$from-path
  76. //if ($from == "")
  77. $this->from = $this->path;
  78. $this->to = $path;
  79. // walk through the array aContent and create all directories
  80. for ($i=0; $i < count($this->aContent); $i++) {
  81. if (is_object($this->aContent[$i])) {
  82. //$pattern = "^".$this->from."^"; //mise en commentaire ACTi 14/06/2007
  83. $pattern = $this->from;
  84. $replace = $this->to;
  85. //$dirToCreate = preg_replace($pattern,$replace, $this->aContent[$i]->path ); //mise en commentaire ACTi 14/06/2007
  86. $dirToCreate = str_replace($pattern,$replace, $this->aContent[$i]->path );
  87. // call copy recursively to create the new directory and process the next level
  88. $this->aContent[$i]->copy_tree($dirToCreate, $mode,$this->from);
  89. } // End if ...
  90. } // End for ....
  91. clearstatcache();
  92. } // End of func copy_tree
  93. function copy_files ($path, $mode, $from = "") {
  94. for ($i=0; $i < count($this->aContent); $i++) {
  95. // if it is a dir-object, call copy recursively
  96. if (is_object($this->aContent[$i])) {
  97. ////$pattern = "^".$this->from."^"; //mise en commentaire ACTi 14/06/2007
  98. $pattern = $this->from;
  99. $replace = $this->to;
  100. //$newpath = preg_replace($pattern,$replace, $this->aContent[$i]->path ); //mise en commentaire ACTi 14/06/2007
  101. $newpath = str_replace($pattern,$replace, $this->aContent[$i]->path );
  102. // call copy recursively to enter the sub-dir and process the next level
  103. $this->aContent[$i]->copy_files($newpath, $mode,$this->from);
  104. } // End if ...
  105. // if it is a file, copy
  106. else {
  107. // save the fileperms
  108. $perms = fileperms($this->aContent[$i]);
  109. $src = $this->aContent[$i];
  110. //$pattern = "^".$this->from."^"; //mise en commentaire ACTi 14/06/2007
  111. $pattern = $this->from;
  112. $replace = $this->to;
  113. //$dest = preg_replace($pattern,$replace, $this->aContent[$i] ); //mise en commentaire ACTi 14/06/2007
  114. $dest = str_replace($pattern,$replace, $this->aContent[$i] );
  115. copy($src,$dest);
  116. if ($this->debug == TRUE)
  117. echo "<br><b>$src</b> copied to <b>$dest</b>";
  118. chmod($dest,$perms);
  119. }
  120. } // End for.....
  121. clearstatcache();
  122. } // End of func copy files
  123. // deletes all dirs and subdirs --> assumes that there are no files in the tree
  124. function delete_tree() {
  125. //remove all entries by calling delete_tree for each member in the dir
  126. if (count($this->aContent)) {
  127. while (count($this->aContent)) {
  128. $this->aContent[0]->delete_tree();
  129. array_shift($this->aContent);
  130. }
  131. }
  132. if (!rmdir($this->path)) {
  133. $mess = "<br>could not remove dir ".$this->path;
  134. die($mess);
  135. }
  136. if ($this->debug == TRUE) {
  137. echo "<br>Directory <b> ".$this->path."</b> removed";
  138. }
  139. return;
  140. } // End of func delete_tree
  141. // deletes all files in the complete tree
  142. function delete_files() {
  143. for ($i=0; $i < count($this->aContent); $i++) {
  144. // if it is a dir-objekt, call delete_files recursively
  145. if (is_object($this->aContent[$i])) {
  146. $pattern = "^".$this->from."^";
  147. $replace = $this->to;
  148. $newpath = preg_replace($pattern,$replace, $this->aContent[$i]->path );
  149. // call copy recursively to enter the sub-dir and process the next level
  150. $this->aContent[$i]->delete_files($newpath, $mode,$this->from);
  151. } // End if ...
  152. // if it is a file, delete
  153. else {
  154. if (!@unlink( $this->aContent[$i] )) {
  155. $mess = "<b>removing file ".$this->aContent[$i]." failed Please check the fileperms";
  156. $mess .=" and try again</b>";
  157. echo $mess;
  158. exit;
  159. }
  160. if ($this->debug == TRUE) {
  161. echo "<br>File<b> ".$this->aContent[$i]."</b> removed";
  162. }
  163. }
  164. } // End for.....
  165. clearstatcache();
  166. } // End func delete files
  167. function debug($bool=TRUE){
  168. $this->debug = $bool;
  169. }
  170. } // End of class dirtool
  171. ?>