/extensions/pogostick/components/CPSFile.php

https://github.com/Pogostick/ps-yii-extensions · PHP · 286 lines · 132 code · 31 blank · 123 comment · 26 complexity · 3a5e73c10463322111fe4257bb3f66ff MD5 · raw file

  1. <?php
  2. /**
  3. * CPSFile.php
  4. *
  5. * Copyright (c) 2010 Jerry Ablan <jablan@pogostick.com>.
  6. * @link http://www.pogostick.com Pogostick, LLC.
  7. * @license http://www.pogostick.com/licensing
  8. *
  9. * This file is part of the Pogostick Yii Extension Library.
  10. *
  11. * We share the same open source ideals as does the jQuery team, and
  12. * we love them so much we like to quote their license statement:
  13. *
  14. * You may use our open source libraries under the terms of either the MIT
  15. * License or the Gnu General Public License (GPL) Version 2.
  16. *
  17. * The MIT License is recommended for most projects. It is simple and easy to
  18. * understand, and it places almost no restrictions on what you can do with
  19. * our code.
  20. *
  21. * If the GPL suits your project better, you are also free to use our code
  22. * under that license.
  23. *
  24. * You don’t have to do anything special to choose one license or the other,
  25. * and you don’t have to notify anyone which license you are using.
  26. */
  27. //*************************************************************************
  28. //* File Constants
  29. //*************************************************************************
  30. /**#@+
  31. * Extra GLOB constant for safe_glob()
  32. */
  33. define( 'GLOB_NODIR', 0x0100 );
  34. define( 'GLOB_PATH', 0x0200 );
  35. define( 'GLOB_NODOTS', 0x0400 );
  36. define( 'GLOB_RECURSE', 0x0800 );
  37. /**#@-*/
  38. /**
  39. * A quicky down and dirty file object with a sprinkle of awesomeness
  40. *
  41. * @package psYiiExtensions
  42. * @subpackage components
  43. *
  44. * @author Jerry Ablan <jablan@pogostick.com>
  45. * @version SVN $Id: CPSFile.php 389 2010-06-20 14:18:58Z jerryablan@gmail.com $
  46. * @since v1.0.0
  47. *
  48. * @filesource
  49. *
  50. * @property-read $fileHandle The handle of the current file
  51. * @property-read $fileName The name of the current file
  52. */
  53. class CPSFile extends CPSComponent
  54. {
  55. //********************************************************************************
  56. //* Member Variables
  57. //********************************************************************************
  58. /**
  59. * The name of the current file
  60. * @var string
  61. */
  62. protected $_fileName = false;
  63. /**
  64. * The handle of the current file
  65. * @var integer
  66. */
  67. protected $_fileHandle = false;
  68. //********************************************************************************
  69. //* Public Methods
  70. //********************************************************************************
  71. /**
  72. * Constructo
  73. */
  74. public function __construct( $fileName )
  75. {
  76. $this->_fileName = $fileName;
  77. $this->open();
  78. }
  79. /**
  80. * @return bool
  81. */
  82. public function validHandle()
  83. {
  84. return ( false !== $this->_fileHandle );
  85. }
  86. /**
  87. * @return bool
  88. */
  89. public function open()
  90. {
  91. if ( file_exists( $this->_fileName ) )
  92. {
  93. if ( false !== ( $this->_fileHandle = @fopen( $this->_fileName, 'a+' ) ) )
  94. $this->_fileHandle = @fopen( $this->_fileName, 'r' );
  95. }
  96. return $this->validHandle();
  97. }
  98. /**
  99. */
  100. public function close()
  101. {
  102. @fclose( $this->_fileHandle );
  103. $this->_fileHandle = false;
  104. }
  105. /**
  106. * @return bool
  107. */
  108. public function filesize()
  109. {
  110. return $this->validHandle() && filesize( $this->_fileName );
  111. }
  112. /**
  113. * @return bool
  114. */
  115. public function atime()
  116. {
  117. return $this->validHandle() && fileatime( $this->_fileName );
  118. }
  119. /**
  120. * @return bool
  121. */
  122. public function fileowner()
  123. {
  124. return $this->validHandle() && fileowner( $this->_fileName );
  125. }
  126. /**
  127. * @return bool
  128. */
  129. public function filegroup()
  130. {
  131. return $this->validHandle() && filegroup( $this->_fileName );
  132. }
  133. /**
  134. * @param int $iOffset
  135. * @return bool
  136. */
  137. public function fseek( $iOffset = 0 )
  138. {
  139. return $this->validHandle() && fseek( $this->_fileHandle, $iOffset );
  140. }
  141. /**
  142. * @return bool
  143. */
  144. public function ftell()
  145. {
  146. return $this->validHandle() && ftell( $this->_fileHandle );
  147. }
  148. /**
  149. * Retrieves a string from the current file
  150. */
  151. public function fgets( $iOffset = 0 )
  152. {
  153. if ( false !== $this->ftell() )
  154. rewind( $this->_fileHandle );
  155. return fgets( $this->_fileHandle );
  156. }
  157. /**
  158. * As found on php.net posted by: BigueNique at yahoo dot ca 20-Apr-2010 07:15
  159. * A safe empowered glob().
  160. *
  161. * Supported flags: GLOB_MARK, GLOB_NOSORT, GLOB_ONLYDIR
  162. * Additional flags: GLOB_NODIR, GLOB_PATH, GLOB_NODOTS, GLOB_RECURSE (not original glob() flags, defined here)
  163. * @author BigueNique AT yahoo DOT ca
  164. * @param string $pattern
  165. * @param int flags
  166. * @return array|false
  167. */
  168. public static function glob( $pattern, $flags = 0 )
  169. {
  170. $_split = explode( '/', str_replace( '\\', '/', $pattern ) );
  171. $_mask = array_pop( $_split );
  172. $_path = implode( '/', $_split );
  173. $_glob = false;
  174. if ( false !== ( $_directory = opendir( $_path ) ) )
  175. {
  176. $_glob = array();
  177. while ( false !== ( $_file = readdir( $_directory ) ) )
  178. {
  179. // Recurse directories
  180. if ( ( $flags & GLOB_RECURSE ) && is_dir( $_file ) && ( ! in_array( $_file, array( '.', '..' ) ) ) )
  181. {
  182. $_glob = array_merge(
  183. $_glob,
  184. self::array_prepend(
  185. self::glob(
  186. $_path . '/' . $_file . '/' . $_mask,
  187. $flags
  188. ),
  189. ( $flags & GLOB_PATH ? '' : $_file . '/' )
  190. )
  191. );
  192. }
  193. // Match file mask
  194. if ( fnmatch( $_mask, $_file ) )
  195. {
  196. if ( ( ( !( $flags & GLOB_ONLYDIR ) ) || is_dir( "$_path/$_file" ) )
  197. && ( ( !( $flags & GLOB_NODIR ) ) || ( ! is_dir( $_path . '/' . $_file ) ) )
  198. && ( ( !( $flags & GLOB_NODOTS ) ) || ( ! in_array( $_file, array( '.', '..' ) ) ) )
  199. )
  200. {
  201. $_glob[] = ( $flags & GLOB_PATH ? $_path . '/' : '' ) . $_file . ( $flags & GLOB_MARK ? '/' : '' );
  202. }
  203. }
  204. }
  205. closedir( $_directory );
  206. if ( !( $flags & GLOB_NOSORT ) )
  207. {
  208. sort( $_glob );
  209. }
  210. }
  211. return $_glob;
  212. }
  213. /**
  214. * @static
  215. * @param array $array
  216. * @param string $string
  217. * @param bool $deep
  218. * @return array
  219. */
  220. public static function array_prepend( $array, $string, $deep = false )
  221. {
  222. if ( empty( $array ) || empty( $string ) )
  223. return $array;
  224. foreach ( $array as $key => $element )
  225. {
  226. if ( is_array( $element ) )
  227. {
  228. if ( $deep )
  229. $array[$key] = array_prepend( $element, $string, $deep );
  230. else
  231. trigger_error( 'array_prepend: array element', E_USER_WARNING );
  232. }
  233. else
  234. $array[$key] = $string . $element;
  235. }
  236. return $array;
  237. }
  238. //*************************************************************************
  239. //* Properties
  240. //*************************************************************************
  241. /**
  242. * @return string
  243. */
  244. public function getFileName( )
  245. {
  246. return $this->_fileName;
  247. }
  248. /**
  249. * @return int
  250. */
  251. public function getFileHandle( )
  252. {
  253. return $this->_fileHandle;
  254. }
  255. }