PageRenderTime 27ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/core/components/File.class.php

http://plant.googlecode.com/
PHP | 278 lines | 102 code | 44 blank | 132 comment | 21 complexity | 1f56e7b432c2efb8bfd7529dfd093100 MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php
  2. /**
  3. * File.class.php
  4. *
  5. * @package plant_core
  6. * @subpackage components
  7. */
  8. /**
  9. * File Wrapper
  10. *
  11. * Wraps an file and provides easy access methods to much-used file functions, like
  12. * path access, etc.
  13. *
  14. * @author Ivo Janssen <foxxyz@gmail.com>
  15. * @copyright Copyright (c) 2009, Ivo Janssen
  16. * @license http://opensource.org/licenses/gpl-3.0.html GNU General Public License, version 3
  17. * @package plant_core
  18. * @subpackage components
  19. * @version 1.1
  20. */
  21. class File {
  22. /**
  23. * Known file types for automatic typecasting
  24. * @var array
  25. */
  26. static public $fileTypes = array(
  27. "avi" => "video",
  28. "flv" => "video",
  29. "gif" => "image",
  30. "jpeg" => "image",
  31. "jpg" => "image",
  32. "jpe" => "image",
  33. "mov" => "video",
  34. "m4v" => "video",
  35. "mp4" => "video",
  36. "mpe" => "video",
  37. "mpeg" => "video",
  38. "mpg" => "video",
  39. "png" => "image",
  40. "wmv" => "video",
  41. );
  42. /**
  43. * Full path to the file
  44. * @var string
  45. */
  46. protected $path;
  47. /**
  48. * Type (extension) of the file
  49. * @var string
  50. */
  51. protected $type;
  52. /**
  53. * Generic wrapper
  54. *
  55. * Automatically typecasts for well-known extensions
  56. * @return File (or a subclass of File)
  57. */
  58. public static function wrap($path) {
  59. // Find extension
  60. $type = substr(strrchr($path, "."), 1);
  61. // Automatically typecast for well-known extensions
  62. if ($type && array_key_exists($type, File::$fileTypes)) {
  63. $subClass = ucfirst(File::$fileTypes[$type]) . "File";
  64. if (class_exists($subClass)) return new $subClass($path);
  65. }
  66. // Otherwise just return File
  67. return new File($path);
  68. }
  69. /**
  70. * Constructor
  71. *
  72. * Turn a file into a File object
  73. *
  74. * @param string $path The path to the file
  75. * @return File
  76. * @uses File::$path
  77. * @uses File::$type
  78. */
  79. public function __construct($path) {
  80. if (!is_string($path)) throw new Exception("Path must be a string!");
  81. // Autodetect type from file path
  82. $type = substr(strrchr($path, "."), 1);
  83. // Set path, type
  84. $this->path = $path;
  85. $this->type = $type;
  86. }
  87. /**
  88. * Delete this file
  89. *
  90. * Warning! This method will actually remove the file from disk.
  91. *
  92. * @return bool
  93. * @uses LOCAL_SITE_ROOT
  94. * @uses config()
  95. * @uses File::$path
  96. */
  97. public function delete() {
  98. return @unlink(config("LOCAL_SITE_ROOT") . $this->path);
  99. }
  100. /**
  101. * Check if this file exists
  102. *
  103. * @return bool
  104. * @uses getURL()
  105. * @uses LOCAL_SITE_ROOT
  106. * @uses config()
  107. * @uses File::$path
  108. */
  109. public function exists() {
  110. // If this has a protocol indicator, use headers to check existence
  111. if (strpos($this->path, "://") !== false) {
  112. $fileHeaders = @get_headers($this->getURL());
  113. if (!preg_match("|200|", $fileHeaders[0])) return false;
  114. return true;
  115. }
  116. // Else just do a regular file_exists
  117. return file_exists(config("LOCAL_SITE_ROOT") . $this->path);
  118. }
  119. /**
  120. * Get the modification time of this file
  121. *
  122. * @return int Unix timestamp of modification time
  123. */
  124. public function getModificationTime() {
  125. return filemtime($this->getURL("local"));
  126. }
  127. /**
  128. * Get the filename of this file
  129. *
  130. * Options for example file "/www/htdocs/index.html":
  131. * base Returns "index.html"
  132. * file Returns "index"
  133. *
  134. * @param string $which Which part to return [file|base]
  135. * @return string Name of file
  136. * @uses File::$path
  137. */
  138. public function getName($which = "file") {
  139. switch($which) {
  140. case "base":
  141. return pathinfo($this->path, PATHINFO_BASENAME);
  142. case "file":
  143. default:
  144. return pathinfo($this->path, PATHINFO_FILENAME);
  145. }
  146. }
  147. /**
  148. * Get the byte size of this file
  149. *
  150. * @param string $format Format to return [bytes|clean]
  151. * @return int|string|bool Size in bytes or a clean size in a string, FALSE on error
  152. * @uses File::$type
  153. */
  154. public function getSize($format = "clean") {
  155. // If this has a protocol indicator, use headers to check file size
  156. if (strpos($this->path, "://") !== false) {
  157. $fileHeaders = @get_headers($this->getURL(), 1);
  158. if (!isset($fileHeaders["Content-Length"])) return false;
  159. $size = intval($fileHeaders["Content-Length"]);
  160. }
  161. // Otherwise do a local filesize check
  162. else {
  163. $size = filesize($this->getURL("local"));
  164. if ($size === false) return false;
  165. }
  166. switch($format) {
  167. case "bytes":
  168. return $size;
  169. case "clean":
  170. default:
  171. // Files bigger than 1 meg
  172. if ($size >= pow(1024,2)) return round($size / pow(1024,2), 2) . "MB";
  173. // Files bigger than 1 KB
  174. if ($size >= 1024) return round($size / 1024) . "KB";
  175. // Files smaller than 1 KB
  176. return ($size . " bytes");
  177. }
  178. }
  179. /**
  180. * Get the type/extension of this file
  181. *
  182. * @return bool|string Returns the extension of the file, or FALSE if unknown type.
  183. * @uses File::$type
  184. */
  185. public function getType() {
  186. if (!isset($this->type) || !$this->type) return false;
  187. return $this->type;
  188. }
  189. /**
  190. * Get the path to this file
  191. *
  192. * @param string $which Which path to retrieve. Possible values are <kbd>local</kbd> for the local path (like '/usr/home/www/etc/') or <kbd>remote</kbd> for the URL that can be accessed from anywhere. Defaults to <kbd>remote</kbd>.
  193. * @param bool $checkExistence Whether to check the existence of this file before returning the path to it. Defaults to TRUE.
  194. * @return string The path to this file
  195. * @uses LOCAL_SITE_ROOT
  196. * @uses REMOTE_SITE_ROOT
  197. * @uses File::$path
  198. * @uses config()
  199. */
  200. public function getURL($which = "remote", $checkExistence = true) {
  201. // If the path has a starting slash, just return with no checking
  202. if ($this->path[0] == "/" || strpos($this->path, "://") !== false) return $this->path;
  203. // Determine which path to prefix
  204. switch($which) {
  205. case "local":
  206. $pathPrefix = config("LOCAL_SITE_ROOT");
  207. break;
  208. case "remote":
  209. default:
  210. $pathPrefix = config("REMOTE_SITE_ROOT");
  211. }
  212. // Return path if it exists... all is good
  213. if ($this->exists() || !$checkExistence) return $pathPrefix . $this->path;
  214. // Throw not found exception
  215. throw new Exception("File at '" . config("LOCAL_SITE_ROOT") . $this->path . "' not found!");
  216. }
  217. /**
  218. * Rename this file
  219. *
  220. * @param string $newPath The new path to rename this file to, starting from the website root (EG <kbd>content/products/jacket.png</kbd>)
  221. * @return bool
  222. * @uses LOCAL_SITE_ROOT
  223. * @uses Image::$path
  224. * @uses config()
  225. * @uses getURL()
  226. */
  227. public function rename($newPath) {
  228. if (!is_string($newPath) || !$newPath) throw new Exception("New path to rename to must be a string!");
  229. @rename($this->getURL("local", false), config("LOCAL_SITE_ROOT") . $newPath);
  230. $this->path = $newPath;
  231. return true;
  232. }
  233. }
  234. ?>