/newscoop/javascript/tinymce/plugins/campsiteattachment/classes/Files.php

https://github.com/strogo/Newscoop · PHP · 215 lines · 114 code · 25 blank · 76 comment · 26 complexity · 41fa8b03e138512ceec9ba3a9c9701c8 MD5 · raw file

  1. <?php
  2. /**
  3. * File Utilities.
  4. * @author $Author: paul $
  5. * @version $Id: Files.php 5087 2006-06-01 21:54:08Z paul $
  6. * @package ImageManager
  7. */
  8. define('FILE_ERROR_NO_SOURCE', 100);
  9. define('FILE_ERROR_COPY_FAILED', 101);
  10. define('FILE_ERROR_DST_DIR_FAILED', 102);
  11. define('FILE_COPY_OK', 103);
  12. /**
  13. * File Utilities
  14. * @author $Author: paul $
  15. * @version $Id: Files.php 5087 2006-06-01 21:54:08Z paul $
  16. * @package ImageManager
  17. * @subpackage files
  18. */
  19. class Files
  20. {
  21. /**
  22. * Copy a file from source to destination. If unique == true, then if
  23. * the destination exists, it will be renamed by appending an increamenting
  24. * counting number.
  25. * @param string $source where the file is from, full path to the files required
  26. * @param string $destination_file name of the new file, just the filename
  27. * @param string $destination_dir where the files, just the destination dir,
  28. * e.g., /www/html/gallery/
  29. * @param boolean $unique create unique destination file if true.
  30. * @return string the new copied filename, else error if anything goes bad.
  31. */
  32. function copyFile($source, $destination_dir, $destination_file, $unique=true)
  33. {
  34. if(!(file_exists($source) && is_file($source)))
  35. return FILE_ERROR_NO_SOURCE;
  36. $destination_dir = Files::fixPath($destination_dir);
  37. if(!is_dir($destination_dir))
  38. Return FILE_ERROR_DST_DIR_FAILED;
  39. $filename = Files::escape($destination_file);
  40. if($unique)
  41. {
  42. $dotIndex = strrpos($destination_file, '.');
  43. $ext = '';
  44. if(is_int($dotIndex))
  45. {
  46. $ext = substr($destination_file, $dotIndex);
  47. $base = substr($destination_file, 0, $dotIndex);
  48. }
  49. $counter = 0;
  50. while(is_file($destination_dir.$filename))
  51. {
  52. $counter++;
  53. $filename = $base.'_'.$counter.$ext;
  54. }
  55. }
  56. if (!copy($source, $destination_dir.$filename))
  57. return FILE_ERROR_COPY_FAILED;
  58. //verify that it copied, new file must exists
  59. if (is_file($destination_dir.$filename))
  60. Return $filename;
  61. else
  62. return FILE_ERROR_COPY_FAILED;
  63. }
  64. /**
  65. * Create a new folder.
  66. * @param string $newFolder specifiy the full path of the new folder.
  67. * @return boolean true if the new folder is created, false otherwise.
  68. */
  69. function createFolder($newFolder)
  70. {
  71. mkdir ($newFolder, 0777);
  72. return chmod($newFolder, 0777);
  73. }
  74. /**
  75. * Escape the filenames, any non-word characters will be
  76. * replaced by an underscore.
  77. * @param string $filename the orginal filename
  78. * @return string the escaped safe filename
  79. */
  80. function escape($filename)
  81. {
  82. Return preg_replace('/[^\w\._]/', '_', $filename);
  83. }
  84. /**
  85. * Delete a file.
  86. * @param string $file file to be deleted
  87. * @return boolean true if deleted, false otherwise.
  88. */
  89. function delFile($file)
  90. {
  91. if(is_file($file))
  92. Return unlink($file);
  93. else
  94. Return false;
  95. }
  96. /**
  97. * Delete folder(s), can delete recursively.
  98. * @param string $folder the folder to be deleted.
  99. * @param boolean $recursive if true, all files and sub-directories
  100. * are delete. If false, tries to delete the folder, can throw
  101. * error if the directory is not empty.
  102. * @return boolean true if deleted.
  103. */
  104. function delFolder($folder, $recursive=false)
  105. {
  106. $deleted = true;
  107. if($recursive)
  108. {
  109. $d = dir($folder);
  110. while (false !== ($entry = $d->read()))
  111. {
  112. if ($entry != '.' && $entry != '..')
  113. {
  114. $obj = Files::fixPath($folder).$entry;
  115. //var_dump($obj);
  116. if (is_file($obj))
  117. {
  118. $deleted &= Files::delFile($obj);
  119. }
  120. else if(is_dir($obj))
  121. {
  122. $deleted &= Files::delFolder($obj, $recursive);
  123. }
  124. }
  125. }
  126. $d->close();
  127. }
  128. //$folder= $folder.'/thumbs';
  129. //var_dump($folder);
  130. if(is_dir($folder))
  131. $deleted &= rmdir($folder);
  132. else
  133. $deleted &= false;
  134. Return $deleted;
  135. }
  136. /**
  137. * Append a / to the path if required.
  138. * @param string $path the path
  139. * @return string path with trailing /
  140. */
  141. function fixPath($path)
  142. {
  143. //append a slash to the path if it doesn't exists.
  144. if(!(substr($path,-1) == '/'))
  145. $path .= '/';
  146. Return $path;
  147. }
  148. /**
  149. * Concat two paths together. Basically $pathA+$pathB
  150. * @param string $pathA path one
  151. * @param string $pathB path two
  152. * @return string a trailing slash combinded path.
  153. */
  154. function makePath($pathA, $pathB)
  155. {
  156. $pathA = Files::fixPath($pathA);
  157. if(substr($pathB,0,1)=='/')
  158. $pathB = substr($pathB,1);
  159. Return Files::fixPath($pathA.$pathB);
  160. }
  161. /**
  162. * Similar to makePath, but the second parameter
  163. * is not only a path, it may contain say a file ending.
  164. * @param string $pathA the leading path
  165. * @param string $pathB the ending path with file
  166. * @return string combined file path.
  167. */
  168. function makeFile($pathA, $pathB)
  169. {
  170. $pathA = Files::fixPath($pathA);
  171. if(substr($pathB,0,1)=='/')
  172. $pathB = substr($pathB,1);
  173. Return $pathA.$pathB;
  174. }
  175. /**
  176. * Format the file size, limits to Mb.
  177. * @param int $size the raw filesize
  178. * @return string formated file size.
  179. */
  180. function formatSize($size)
  181. {
  182. if($size < 1024)
  183. return $size.' bytes';
  184. else if($size >= 1024 && $size < 1024*1024)
  185. return sprintf('%01.2f',$size/1024.0).' Kb';
  186. else
  187. return sprintf('%01.2f',$size/(1024.0*1024)).' Mb';
  188. }
  189. }
  190. ?>