/application/libraries/Engine/Vfs/Adapter/System.php

https://github.com/grandison/budo16 · PHP · 374 lines · 246 code · 85 blank · 43 comment · 39 complexity · c1cbd8b48aa9747aec85314f7a0e05f8 MD5 · raw file

  1. <?php
  2. /**
  3. * SocialEngine
  4. *
  5. * @category Engine
  6. * @package Engine_Vfs
  7. * @copyright Copyright 2006-2010 Webligo Developments
  8. * @license http://www.socialengine.net/license/
  9. * @version $Id: System.php 7614 2010-10-08 21:57:01Z john $
  10. * @author John Boehr <j@webligo.com>
  11. */
  12. //require_once 'Engine/Vfs/Adapter/Abstract.php';
  13. //require_once 'Engine/Vfs/Adapter/LocalAbstract.php';
  14. //require_once 'Engine/Vfs/Adapter/Exception.php';
  15. //require_once 'Engine/Vfs/Directory/Standard.php';
  16. //require_once 'Engine/Vfs/Info/Standard.php';
  17. //require_once 'Engine/Vfs/Object/System.php';
  18. /**
  19. * @category Engine
  20. * @package Engine_Vfs
  21. * @copyright Copyright 2006-2010 Webligo Developments
  22. * @license http://www.socialengine.net/license/
  23. * @author John Boehr <j@webligo.com>
  24. */
  25. class Engine_Vfs_Adapter_System extends Engine_Vfs_Adapter_LocalAbstract
  26. {
  27. public function __construct(array $config = null)
  28. {
  29. parent::__construct($config);
  30. $this->_directorySeparator = DIRECTORY_SEPARATOR;
  31. }
  32. public function getResource()
  33. {
  34. return $this;
  35. }
  36. // Informational
  37. public function exists($path)
  38. {
  39. $path = $this->path($path);
  40. return file_exists($path);
  41. }
  42. public function isDirectory($path)
  43. {
  44. $path = $this->path($path);
  45. return is_dir($path);
  46. }
  47. public function isFile($path)
  48. {
  49. $path = $this->path($path);
  50. return is_file($path);
  51. }
  52. public function getSystemType()
  53. {
  54. if( null === $this->_systemType ) {
  55. $systype = php_uname('s');
  56. $this->_systemType = self::processSystemType($systype);
  57. }
  58. return $this->_systemType;
  59. }
  60. public function stat($path)
  61. {
  62. $path = $this->path($path);
  63. $stat = stat($path);
  64. // Missing
  65. if( !$stat ) {
  66. return array(
  67. 'name' => basename($path),
  68. 'path' => $path,
  69. 'exists' => false,
  70. );
  71. }
  72. // Get extra
  73. $type = filetype($path);
  74. $rights = substr(sprintf('%o', fileperms($path)), -4);
  75. // Process stat
  76. $info = array(
  77. // General
  78. 'name' => basename($path),
  79. 'path' => $path,
  80. 'exists' => true,
  81. 'type' => $type,
  82. // Stat
  83. 'uid' => $stat['uid'],
  84. 'gid' => $stat['gid'],
  85. 'size' => $stat['size'],
  86. 'atime' => $stat['atime'],
  87. 'mtime' => $stat['mtime'],
  88. 'ctime' => $stat['ctime'],
  89. // Perms
  90. 'rights' => $rights,
  91. 'readable' => is_readable($path),
  92. 'writable' => is_writable($path),
  93. 'executable' => is_executable($path),
  94. );
  95. return $info;
  96. }
  97. // General
  98. public function copy($sourcePath, $destPath)
  99. {
  100. $sourcePath = $this->path($sourcePath);
  101. $destPath = $this->path($destPath);
  102. $return = @copy($sourcePath, $destPath);
  103. if( !$return ) {
  104. throw new Engine_Vfs_Adapter_Exception(sprintf('Unable to copy "%s" to "%s"', $sourcePath, $destPath));
  105. }
  106. return $return;
  107. }
  108. public function get($local, $path)
  109. {
  110. $path = $this->path($path);
  111. $return = @copy($path, $local);
  112. if( !$return ) {
  113. throw new Engine_Vfs_Adapter_Exception(sprintf('Unable to get "%s" to "%s"', $path, $local));
  114. }
  115. return $return;
  116. }
  117. public function getContents($path)
  118. {
  119. $path = $this->path($path);
  120. $return = @file_get_contents($path);
  121. if( false === $return ) {
  122. throw new Engine_Vfs_Adapter_Exception(sprintf('Unable to get contents of "%s"', $path));
  123. }
  124. return $return;
  125. }
  126. public function mode($path, $mode, $recursive = false)
  127. {
  128. $path = $this->path($path);
  129. $return = @chmod($path, self::processMode($mode));
  130. if( !$return ) {
  131. throw new Engine_Vfs_Adapter_Exception(sprintf('Unable to change mode on "%s"', $path));
  132. }
  133. if( $recursive ) {
  134. $info = $this->info($path);
  135. if( $info->isDirectory() ) {
  136. foreach( $info->getChildren() as $child ) {
  137. $return &= $this->mode($child->getPath(), $mode, true);
  138. }
  139. }
  140. }
  141. return $return;
  142. }
  143. public function move($oldPath, $newPath)
  144. {
  145. $oldPath = $this->path($oldPath);
  146. $newPath = $this->path($newPath);
  147. $return = @rename($oldPath, $newPath);
  148. if( !$return ) {
  149. throw new Engine_Vfs_Adapter_Exception(sprintf('Unable to rename "%s" to "%s"', $oldPath, $newPath));
  150. }
  151. return $return;
  152. }
  153. public function put($path, $local)
  154. {
  155. $path = $this->path($path);
  156. $return = @copy($local, $path);
  157. if( !$return ) {
  158. throw new Engine_Vfs_Adapter_Exception(sprintf('Unable to put "%s" to "%s"', $local, $path));
  159. }
  160. // Apply umask
  161. try {
  162. $this->mode($path, $this->getUmask(0666));
  163. } catch( Exception $e ) {
  164. // Silence
  165. }
  166. return $return;
  167. }
  168. public function putContents($path, $data)
  169. {
  170. $path = $this->path($path);
  171. $return = @file_put_contents($path, $data);
  172. if( false === $return ) {
  173. throw new Engine_Vfs_Adapter_Exception(sprintf('Unable to put contents to "%s"', $path));
  174. }
  175. // Apply umask
  176. try {
  177. $this->mode($path, $this->getUmask(0666));
  178. } catch( Exception $e ) {
  179. // Silence
  180. }
  181. return $return;
  182. }
  183. public function unlink($path)
  184. {
  185. $path = $this->path($path);
  186. $return = @unlink($path);
  187. if( false === $return ) {
  188. throw new Engine_Vfs_Adapter_Exception(sprintf('Unable to unlink "%s"', $path));
  189. }
  190. return $return;
  191. }
  192. // Directories
  193. public function changeDirectory($directory)
  194. {
  195. $directory = $this->path($directory);
  196. if( $this->isDirectory($directory) ) {
  197. $this->_path = rtrim($directory, '/\\');
  198. return true;
  199. } else {
  200. return false;
  201. }
  202. }
  203. public function listDirectory($directory, $details = false)
  204. {
  205. $directory = $this->path($directory);
  206. $children = array();
  207. foreach( scandir($directory) as $child ) {
  208. if( $child == '.' || $child == '..' ) continue;
  209. if( $details ) {
  210. $children[] = $this->stat($directory . $this->_directorySeparator . $child);
  211. } else {
  212. $children[] = $this->path($directory . $this->_directorySeparator . $child);
  213. }
  214. }
  215. return $children;
  216. }
  217. public function makeDirectory($directory, $recursive = false)
  218. {
  219. $directory = $this->path($directory);
  220. // Already a directory
  221. if( $this->isDirectory($directory) ) {
  222. return true;
  223. }
  224. $return = @mkdir($directory, $this->getUmask(0777), $recursive);
  225. if( false === $return ) {
  226. throw new Engine_Vfs_Adapter_Exception(sprintf('Unable to make directory "%s"', $directory));
  227. }
  228. return $return;
  229. }
  230. public function printDirectory()
  231. {
  232. if( null === $this->_path ) {
  233. $this->_path = getcwd();
  234. }
  235. return $this->_path;
  236. }
  237. public function removeDirectory($directory, $recursive = false)
  238. {
  239. $directory = $this->path($directory);
  240. // Recursive
  241. if( $recursive ) {
  242. $return = true;
  243. // Iterate over contents
  244. $it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory, RecursiveDirectoryIterator::KEY_AS_PATHNAME), RecursiveIteratorIterator::CHILD_FIRST);
  245. foreach( $it as $key => $child ) {
  246. if( $child->getFilename() == '..' || $child->getFilename() == '.' ) continue;
  247. if( $child->isDir() ) {
  248. $return &= $this->removeDirectory($child->getPathname(), false);
  249. } else if( $it->isFile() ) {
  250. $return &= $this->unlink($child->getPathname(), false);
  251. }
  252. }
  253. $return &= $this->removeDirectory($directory, false);
  254. }
  255. // Normal
  256. else {
  257. $return = @rmdir($directory);
  258. }
  259. if( false === $return ) {
  260. throw new Engine_Vfs_Adapter_Exception(sprintf('Unable to remove directory "%s"', $directory));
  261. }
  262. return $return;
  263. }
  264. // User
  265. public function getUid()
  266. {
  267. if( null === $this->_uid ) {
  268. if( function_exists('posix_getuid') ) {
  269. $this->_uid = posix_getuid();
  270. } else {
  271. // Find another way to do it?
  272. $this->_uid = false;
  273. }
  274. }
  275. return $this->_uid;
  276. }
  277. public function getGid()
  278. {
  279. if( null === $this->_gid ) {
  280. if( function_exists('posix_getgid') ) {
  281. $this->_gid = posix_getgid();
  282. } else {
  283. // Find another way to do it?
  284. $this->_gid = false;
  285. }
  286. }
  287. return $this->_gid;
  288. }
  289. }