|
eZ Publish
[4.0]
|
00001 <?php 00002 // 00003 // Definition of eZFile class 00004 // 00005 // Created on: <03-Jun-2002 17:19:12 amos> 00006 // 00007 // ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ## 00008 // SOFTWARE NAME: eZ Publish 00009 // SOFTWARE RELEASE: 4.0.x 00010 // COPYRIGHT NOTICE: Copyright (C) 1999-2008 eZ Systems AS 00011 // SOFTWARE LICENSE: GNU General Public License v2.0 00012 // NOTICE: > 00013 // This program is free software; you can redistribute it and/or 00014 // modify it under the terms of version 2.0 of the GNU General 00015 // Public License as published by the Free Software Foundation. 00016 // 00017 // This program is distributed in the hope that it will be useful, 00018 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00019 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00020 // GNU General Public License for more details. 00021 // 00022 // You should have received a copy of version 2.0 of the GNU General 00023 // Public License along with this program; if not, write to the Free 00024 // Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 00025 // MA 02110-1301, USA. 00026 // 00027 // 00028 // ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ## 00029 // 00030 00031 /*! \file ezfile.php 00032 */ 00033 00034 /*! 00035 \class eZFile ezfile.php 00036 \ingroup eZUtils 00037 \brief Tool class which has convencience functions for files and directories 00038 00039 */ 00040 00041 require_once( "lib/ezutils/classes/ezdebug.php" ); 00042 //include_once( 'lib/ezfile/classes/ezdir.php' ); 00043 00044 class eZFile 00045 { 00046 /*! 00047 \static 00048 Reads the whole contents of the file \a $file and 00049 splits it into lines which is collected into an array and returned. 00050 It will handle Unix (\n), Windows (\r\n) and Mac (\r) style newlines. 00051 \note The newline character(s) are not present in the line string. 00052 */ 00053 static function splitLines( $file ) 00054 { 00055 $fp = @fopen( $file, "rb" ); 00056 if ( !$fp ) 00057 return false; 00058 $size = filesize( $file ); 00059 $contents = fread( $fp, $size ); 00060 fclose( $fp ); 00061 $lines = preg_split( "#\r\n|\r|\n#", $contents ); 00062 unset( $contents ); 00063 return $lines; 00064 } 00065 00066 /*! 00067 Creates a file called \a $filename. 00068 If \a $directory is specified the file is placed there, the directory will also be created if missing. 00069 if \a $data is specified the file will created with the content of this variable. 00070 00071 \param $atomic If true the file contents will be written to a temporary file and renamed to the correct file. 00072 */ 00073 static function create( $filename, $directory = false, $data = false, $atomic = false ) 00074 { 00075 $filepath = $filename; 00076 if ( $directory ) 00077 { 00078 if ( !file_exists( $directory ) ) 00079 { 00080 eZDir::mkdir( $directory, false, true ); 00081 // eZDebugSetting::writeNotice( 'ezfile-create', "Created directory $directory", 'eZFile::create' ); 00082 } 00083 $filepath = $directory . '/' . $filename; 00084 } 00085 // If atomic creation is needed we will use a temporary 00086 // file when writing the data, then rename it to the correct path. 00087 if ( $atomic ) 00088 { 00089 $realpath = $filepath; 00090 $dirname = dirname( $filepath ); 00091 if ( strlen( $dirname ) != 0 ) 00092 $dirname .= "/"; 00093 $filepath = $dirname . "ezfile-tmp." . md5( $filepath . getmypid() . mt_rand() ); 00094 } 00095 00096 $file = fopen( $filepath, 'wb' ); 00097 if ( $file ) 00098 { 00099 // eZDebugSetting::writeNotice( 'ezfile-create', "Created file $filepath", 'eZFile::create' ); 00100 if ( $data ) 00101 fwrite( $file, $data ); 00102 fclose( $file ); 00103 00104 if ( $atomic ) 00105 { 00106 eZFile::rename( $filepath, $realpath ); 00107 } 00108 return true; 00109 } 00110 // eZDebugSetting::writeNotice( 'ezfile-create', "Failed creating file $filepath", 'eZFile::create' ); 00111 return false; 00112 } 00113 00114 /*! 00115 \static 00116 Read all content of file. 00117 00118 \param filename 00119 00120 \return file contents, false if error 00121 */ 00122 static function getContents( $filename ) 00123 { 00124 if ( function_exists( 'file_get_contents' ) ) 00125 { 00126 return file_get_contents( $filename ); 00127 } 00128 else 00129 { 00130 $fp = fopen( $filename, 'r' ); 00131 if ( !$fp ) 00132 { 00133 eZDebug::writeError( 'Could not read contents of ' . $filename, 'eZFile::getContents()' ); 00134 return false; 00135 } 00136 00137 return fread( $fp, filesize( $filename ) ); 00138 } 00139 } 00140 00141 /*! 00142 \static 00143 Get suffix from filename 00144 00145 \param filename 00146 \return suffix, extends: file/to/readme.txt return txt 00147 */ 00148 static function suffix( $filename ) 00149 { 00150 $parts = explode( '.', $filename); 00151 return array_pop( $parts ); 00152 } 00153 00154 /*! 00155 \static 00156 Check if a given file is writeable 00157 00158 \return TRUE/FALSE 00159 */ 00160 static function isWriteable( $filename ) 00161 { 00162 //include_once( 'lib/ezutils/classes/ezsys.php' ); 00163 00164 if ( eZSys::osType() != 'win32' ) 00165 return is_writable( $filename ); 00166 00167 /* PHP function is_writable() doesn't work correctly on Windows NT descendants. 00168 * So we have to use the following hack on those OSes. 00169 * FIXME: maybe on win9x we shouldn't do this? 00170 */ 00171 if ( !( $fd = @fopen( $filename, 'a' ) ) ) 00172 return FALSE; 00173 00174 fclose( $fd ); 00175 00176 return TRUE; 00177 } 00178 00179 /*! 00180 \static 00181 Renames a file atomically on Unix, and provides a workaround for Windows 00182 00183 \param from filename 00184 \param to filename 00185 00186 \return rename status. ( true if successful, false if not ) 00187 */ 00188 static function rename( $srcFile, $destFile ) 00189 { 00190 /* On windows we need to unlink the destination file first */ 00191 if ( strtolower( substr( PHP_OS, 0, 3 ) ) == 'win' ) 00192 { 00193 @unlink( $destFile ); 00194 } 00195 return rename( $srcFile, $destFile ); 00196 } 00197 00198 /*! 00199 \static 00200 Prepares a file for Download and terminates the execution. 00201 00202 \param $file Filename 00203 \param $isAttached Download Determines weather to download the file as an attachment ( download popup box ) or not. 00204 00205 \return false if error 00206 */ 00207 static function download( $file, $isAttachedDownload = true, $overrideFilename = false ) 00208 { 00209 if ( file_exists( $file ) ) 00210 { 00211 //include_once( 'lib/ezutils/classes/ezmimetype.php' ); 00212 $mimeinfo = eZMimeType::findByURL( $file ); 00213 00214 ob_clean(); 00215 00216 header( 'X-Powered-By: eZ Publish' ); 00217 header( 'Content-Length: ' . filesize( $file ) ); 00218 header( 'Content-Type: ' . $mimeinfo['name'] ); 00219 00220 // Fixes problems with IE when opening a file directly 00221 header( "Pragma: " ); 00222 header( "Cache-Control: " ); 00223 /* Set cache time out to 10 minutes, this should be good enough to work 00224 around an IE bug */ 00225 header( "Expires: ". gmdate('D, d M Y H:i:s', time() + 600) . ' GMT' ); 00226 if( $overrideFilename ) 00227 { 00228 $mimeinfo['filename'] = $overrideFilename; 00229 } 00230 if ( $isAttachedDownload ) 00231 { 00232 header( 'Content-Disposition: attachment; filename='.$mimeinfo['filename'] ); 00233 } 00234 else 00235 { 00236 header( 'Content-Disposition: inline; filename='.$mimeinfo['filename'] ); 00237 } 00238 header( 'Content-Transfer-Encoding: binary' ); 00239 header( 'Accept-Ranges: bytes' ); 00240 00241 ob_end_clean(); 00242 00243 @readfile( $file ); 00244 00245 require_once( 'lib/ezutils/classes/ezexecution.php' ); 00246 eZExecution::cleanExit(); 00247 } 00248 else 00249 { 00250 return false; 00251 } 00252 } 00253 } 00254 00255 ?>