[ Index ]
 

Code source de eZ Publish 3.9.0

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/lib/ezfile/classes/ -> ezfile.php (source)

   1  <?php
   2  //
   3  // Definition of eZFile class
   4  //
   5  // Created on: <03-Jun-2002 17:19:12 amos>
   6  //
   7  // SOFTWARE NAME: eZ publish
   8  // SOFTWARE RELEASE: 3.9.0
   9  // BUILD VERSION: 17785
  10  // COPYRIGHT NOTICE: Copyright (C) 1999-2006 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  
  29  /*! \file ezfile.php
  30  */
  31  
  32  /*!
  33   \class eZFile ezfile.php
  34   \ingroup eZUtils
  35   \brief Tool class which has convencience functions for files and directories
  36  
  37  */
  38  
  39  include_once ( "lib/ezutils/classes/ezdebug.php" );
  40  include_once ( 'lib/ezfile/classes/ezdir.php' );
  41  
  42  class eZFile
  43  {
  44      /*!
  45       Constructor
  46      */
  47      function eZFile()
  48      {
  49      }
  50  
  51      /*!
  52       \static
  53       Reads the whole contents of the file \a $file and
  54       splits it into lines which is collected into an array and returned.
  55       It will handle Unix (\n), Windows (\r\n) and Mac (\r) style newlines.
  56       \note The newline character(s) are not present in the line string.
  57      */
  58      function splitLines( $file )
  59      {
  60          $fp = @fopen( $file, "rb" );
  61          if ( !$fp )
  62              return false;
  63          $size = filesize( $file );
  64          $contents = fread( $fp, $size );
  65          fclose( $fp );
  66          $lines = preg_split( "#\r\n|\r|\n#", $contents );
  67          unset( $contents );
  68          return $lines;
  69      }
  70  
  71      /*!
  72       Creates a file called \a $filename.
  73       If \a $directory is specified the file is placed there, the directory will also be created if missing.
  74       if \a $data is specified the file will created with the content of this variable.
  75      */
  76      function create( $filename, $directory = false, $data = false )
  77      {
  78          $filepath = $filename;
  79          if ( $directory )
  80          {
  81              if ( !file_exists( $directory ) )
  82              {
  83                  eZDir::mkdir( $directory, eZDir::directoryPermission(), true );
  84  //                 eZDebugSetting::writeNotice( 'ezfile-create', "Created directory $directory", 'eZFile::create' );
  85              }
  86              $filepath = $directory . '/' . $filename;
  87          }
  88          $file = fopen( $filepath, 'w' );
  89          if ( $file )
  90          {
  91  //             eZDebugSetting::writeNotice( 'ezfile-create', "Created file $filepath", 'eZFile::create' );
  92              if ( $data )
  93                  fwrite( $file, $data );
  94              fclose( $file );
  95              return true;
  96          }
  97  //         eZDebugSetting::writeNotice( 'ezfile-create', "Failed creating file $filepath", 'eZFile::create' );
  98          return false;
  99      }
 100  
 101      /*!
 102       \static
 103       Read all content of file.
 104  
 105       \param filename
 106  
 107       \return file contents, false if error
 108      */
 109      function getContents( $filename )
 110      {
 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  
 124              return fread( $fp, filesize( $filename ) );
 125          }
 126      }
 127  
 128      /*!
 129       \static
 130       Get suffix from filename
 131  
 132       \param filename
 133       \return suffix, extends: file/to/readme.txt return txt
 134      */
 135      function suffix( $filename )
 136      {
 137          return array_pop( explode( '.', $filename) );
 138      }
 139  
 140      /*!
 141      \static
 142      Check if a given file is writeable
 143  
 144      \return TRUE/FALSE
 145      */
 146      function isWriteable( $filename )
 147      {
 148          include_once ( 'lib/ezutils/classes/ezsys.php' );
 149  
 150          if ( eZSys::osType() != 'win32' )
 151              return is_writable( $filename );
 152  
 153          /* PHP function is_writable() doesn't work correctly on Windows NT descendants.
 154           * So we have to use the following hack on those OSes.
 155           * FIXME: maybe on win9x we shouldn't do this?
 156           */
 157          if ( !( $fd = @fopen( $filename, 'a' ) ) )
 158              return FALSE;
 159  
 160          fclose( $fd );
 161  
 162          return TRUE;
 163      }
 164  
 165      /*!
 166      \static
 167      Renames a file atomically on Unix, and provides a workaround for Windows
 168      */
 169      function rename( $srcFile, $destFile )
 170      {
 171          /* On windows we need to unlink the destination file first */
 172          if ( strtolower( substr( PHP_OS, 0, 3 ) ) == 'win' )
 173          {
 174              @unlink( $destFile );
 175          }
 176          rename( $srcFile, $destFile );
 177      }
 178  
 179      /*!
 180       \static
 181       Prepares a file for Download and terminates the execution.
 182  
 183       \param $file Filename
 184       \param $isAttached Download Determines weather to download the file as an attachment ( download popup box ) or not.
 185  
 186       \return false if error
 187      */
 188      function download( $file, $isAttachedDownload = true, $overrideFilename = false )
 189      {
 190          if ( file_exists( $file ) )
 191          {
 192              include_once ( 'lib/ezutils/classes/ezmimetype.php' );
 193              $mimeinfo = eZMimeType::findByURL( $file );
 194  
 195              ob_clean();
 196  
 197              header( 'X-Powered-By: eZ publish' );
 198              header( 'Content-Length: ' . filesize( $file ) );
 199              header( 'Content-Type: ' . $mimeinfo['name'] );
 200  
 201              // Fixes problems with IE when opening a file directly
 202              header( 'Cache-Control: no-store, no-cache, must-revalidate' ); // HTTP/1.1
 203              header( 'Cache-Control: pre-check=0, post-check=0, max-age=0' ); // HTTP/1.1
 204              if( $overrideFilename )
 205              {
 206                  $mimeinfo['filename'] = $overrideFilename;
 207              }
 208              if ( $isAttachedDownload )
 209              {
 210                  header( 'Content-Disposition: attachment; filename='.$mimeinfo['filename'] );
 211              }
 212              else
 213              {
 214                  header( 'Content-Disposition: inline; filename='.$mimeinfo['filename'] );
 215              }
 216              header( 'Content-Transfer-Encoding: binary' );
 217              header( 'Accept-Ranges: bytes' );
 218  
 219              ob_end_clean();
 220  
 221              @readfile( $file );
 222  
 223              include_once ( 'lib/ezutils/classes/ezexecution.php' );
 224              eZExecution::cleanExit();
 225          }
 226          else
 227          {
 228              return false;
 229          }
 230      }
 231  }
 232  
 233  ?>


Généré le : Sat Feb 24 10:30:04 2007 par Balluche grâce à PHPXref 0.7