PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/ezfile/classes/ezfile.php

https://github.com/lserwatka/ezpublish
PHP | 249 lines | 122 code | 22 blank | 105 comment | 19 complexity | e04b0e526e8ddc7c07db050a5949fc9b MD5 | raw file
  1. <?php
  2. //
  3. // Definition of eZFile class
  4. //
  5. // Created on: <03-Jun-2002 17:19:12 amos>
  6. //
  7. // ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
  8. // SOFTWARE NAME: eZ Publish
  9. // SOFTWARE RELEASE: 4.1.x
  10. // COPYRIGHT NOTICE: Copyright (C) 1999-2010 eZ Systems AS
  11. // SOFTWARE LICENSE: GNU General Public License v2.0
  12. // NOTICE: >
  13. // This program is free software; you can redistribute it and/or
  14. // modify it under the terms of version 2.0 of the GNU General
  15. // Public License as published by the Free Software Foundation.
  16. //
  17. // This program is distributed in the hope that it will be useful,
  18. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. // GNU General Public License for more details.
  21. //
  22. // You should have received a copy of version 2.0 of the GNU General
  23. // Public License along with this program; if not, write to the Free
  24. // Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  25. // MA 02110-1301, USA.
  26. //
  27. //
  28. // ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
  29. //
  30. /*! \file
  31. */
  32. /*!
  33. \class eZFile ezfile.php
  34. \ingroup eZUtils
  35. \brief Tool class which has convencience functions for files and directories
  36. */
  37. class eZFile
  38. {
  39. /**
  40. * Reads the whole contents of the file \a $file and
  41. * splits it into lines which is collected into an array and returned.
  42. * It will handle Unix (\n), Windows (\r\n) and Mac (\r) style newlines.
  43. * \note The newline character(s) are not present in the line string.
  44. *
  45. * @deprecated Since 4.4, use file( $file, FILE_IGNORE_NEW_LINES ) instead.
  46. * @return array|false
  47. */
  48. static function splitLines( $file )
  49. {
  50. $contents = file_get_contents( $file );
  51. if ( $contents === false )
  52. return false;
  53. $lines = preg_split( "#\r\n|\r|\n#", $contents );
  54. unset( $contents );
  55. return $lines;
  56. }
  57. /*!
  58. Creates a file called \a $filename.
  59. If \a $directory is specified the file is placed there, the directory will also be created if missing.
  60. if \a $data is specified the file will created with the content of this variable.
  61. \param $atomic If true the file contents will be written to a temporary file and renamed to the correct file.
  62. */
  63. static function create( $filename, $directory = false, $data = false, $atomic = false )
  64. {
  65. $filepath = $filename;
  66. if ( $directory )
  67. {
  68. if ( !file_exists( $directory ) )
  69. {
  70. eZDir::mkdir( $directory, false, true );
  71. // eZDebugSetting::writeNotice( 'ezfile-create', "Created directory $directory", 'eZFile::create' );
  72. }
  73. $filepath = $directory . '/' . $filename;
  74. }
  75. // If atomic creation is needed we will use a temporary
  76. // file when writing the data, then rename it to the correct path.
  77. if ( $atomic )
  78. {
  79. $realpath = $filepath;
  80. $dirname = dirname( $filepath );
  81. if ( strlen( $dirname ) != 0 )
  82. $dirname .= "/";
  83. $filepath = $dirname . "ezfile-tmp." . md5( $filepath . getmypid() . mt_rand() );
  84. }
  85. $file = fopen( $filepath, 'wb' );
  86. if ( $file )
  87. {
  88. // eZDebugSetting::writeNotice( 'ezfile-create', "Created file $filepath", 'eZFile::create' );
  89. if ( $data )
  90. fwrite( $file, $data );
  91. fclose( $file );
  92. if ( $atomic )
  93. {
  94. eZFile::rename( $filepath, $realpath );
  95. }
  96. return true;
  97. }
  98. // eZDebugSetting::writeNotice( 'ezfile-create', "Failed creating file $filepath", 'eZFile::create' );
  99. return false;
  100. }
  101. /*!
  102. \static
  103. Read all content of file.
  104. \param filename
  105. \return file contents, false if error
  106. \deprecated since eZ Publish 4.1, use file_get_contents() instead
  107. */
  108. static function getContents( $filename )
  109. {
  110. eZDebug::writeWarning( __METHOD__ . ' is deprecated, use file_get_contents() instead' );
  111. if ( function_exists( 'file_get_contents' ) )
  112. {
  113. return file_get_contents( $filename );
  114. }
  115. else
  116. {
  117. $fp = fopen( $filename, 'r' );
  118. if ( !$fp )
  119. {
  120. eZDebug::writeError( 'Could not read contents of ' . $filename, 'eZFile::getContents()' );
  121. return false;
  122. }
  123. return fread( $fp, filesize( $filename ) );
  124. }
  125. }
  126. /*!
  127. \static
  128. Get suffix from filename
  129. \param filename
  130. \return suffix, extends: file/to/readme.txt return txt
  131. */
  132. static function suffix( $filename )
  133. {
  134. $parts = explode( '.', $filename);
  135. return array_pop( $parts );
  136. }
  137. /*!
  138. \static
  139. Check if a given file is writeable
  140. \return TRUE/FALSE
  141. */
  142. static function isWriteable( $filename )
  143. {
  144. if ( eZSys::osType() != 'win32' )
  145. return is_writable( $filename );
  146. /* PHP function is_writable() doesn't work correctly on Windows NT descendants.
  147. * So we have to use the following hack on those OSes.
  148. */
  149. if ( !( $fd = @fopen( $filename, 'a' ) ) )
  150. return FALSE;
  151. fclose( $fd );
  152. return TRUE;
  153. }
  154. /*!
  155. \static
  156. Renames a file atomically on Unix, and provides a workaround for Windows
  157. \param $srcFile from filename
  158. \param $destFile to filename
  159. \return rename status. ( true if successful, false if not )
  160. */
  161. static function rename( $srcFile, $destFile )
  162. {
  163. /* On windows we need to unlink the destination file first */
  164. if ( strtolower( substr( PHP_OS, 0, 3 ) ) == 'win' )
  165. {
  166. @unlink( $destFile );
  167. }
  168. return rename( $srcFile, $destFile );
  169. }
  170. /*!
  171. \static
  172. Prepares a file for Download and terminates the execution.
  173. \param $file Filename
  174. \param $isAttachedDownload Determines weather to download the file as an attachment ( download popup box ) or not.
  175. \return false if error
  176. */
  177. static function download( $file, $isAttachedDownload = true, $overrideFilename = false )
  178. {
  179. if ( file_exists( $file ) )
  180. {
  181. $mimeinfo = eZMimeType::findByURL( $file );
  182. ob_clean();
  183. header( 'X-Powered-By: eZ Publish' );
  184. header( 'Content-Length: ' . filesize( $file ) );
  185. header( 'Content-Type: ' . $mimeinfo['name'] );
  186. // Fixes problems with IE when opening a file directly
  187. header( "Pragma: " );
  188. header( "Cache-Control: " );
  189. /* Set cache time out to 10 minutes, this should be good enough to work
  190. around an IE bug */
  191. header( "Expires: ". gmdate('D, d M Y H:i:s', time() + 600) . ' GMT' );
  192. if( $overrideFilename )
  193. {
  194. $mimeinfo['filename'] = $overrideFilename;
  195. }
  196. if ( $isAttachedDownload )
  197. {
  198. header( 'Content-Disposition: attachment; filename='.$mimeinfo['filename'] );
  199. }
  200. else
  201. {
  202. header( 'Content-Disposition: inline; filename='.$mimeinfo['filename'] );
  203. }
  204. header( 'Content-Transfer-Encoding: binary' );
  205. header( 'Accept-Ranges: bytes' );
  206. ob_end_clean();
  207. @readfile( $file );
  208. eZExecution::cleanExit();
  209. }
  210. else
  211. {
  212. return false;
  213. }
  214. }
  215. }
  216. ?>