[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/lib/phpexcel/PHPExcel/Shared/ -> File.php (source)

   1  <?php
   2  
   3  /**
   4   * PHPExcel_Shared_File
   5   *
   6   * Copyright (c) 2006 - 2015 PHPExcel
   7   *
   8   * This library is free software; you can redistribute it and/or
   9   * modify it under the terms of the GNU Lesser General Public
  10   * License as published by the Free Software Foundation; either
  11   * version 2.1 of the License, or (at your option) any later version.
  12   *
  13   * This library is distributed in the hope that it will be useful,
  14   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16   * Lesser General Public License for more details.
  17   *
  18   * You should have received a copy of the GNU Lesser General Public
  19   * License along with this library; if not, write to the Free Software
  20   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  21   *
  22   * @category   PHPExcel
  23   * @package    PHPExcel_Shared
  24   * @copyright  Copyright (c) 2006 - 2015 PHPExcel (http://www.codeplex.com/PHPExcel)
  25   * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
  26   * @version    ##VERSION##, ##DATE##
  27   */
  28  class PHPExcel_Shared_File
  29  {
  30      /*
  31       * Use Temp or File Upload Temp for temporary files
  32       *
  33       * @protected
  34       * @var    boolean
  35       */
  36      protected static $useUploadTempDirectory = false;
  37  
  38  
  39      /**
  40       * Set the flag indicating whether the File Upload Temp directory should be used for temporary files
  41       *
  42       * @param     boolean    $useUploadTempDir        Use File Upload Temporary directory (true or false)
  43       */
  44      public static function setUseUploadTempDirectory($useUploadTempDir = false)
  45      {
  46          self::$useUploadTempDirectory = (boolean) $useUploadTempDir;
  47      }
  48  
  49  
  50      /**
  51       * Get the flag indicating whether the File Upload Temp directory should be used for temporary files
  52       *
  53       * @return     boolean    Use File Upload Temporary directory (true or false)
  54       */
  55      public static function getUseUploadTempDirectory()
  56      {
  57          return self::$useUploadTempDirectory;
  58      }
  59  
  60  
  61      /**
  62        * Verify if a file exists
  63        *
  64        * @param     string    $pFilename    Filename
  65        * @return bool
  66        */
  67      public static function file_exists($pFilename)
  68      {
  69          // Sick construction, but it seems that
  70          // file_exists returns strange values when
  71          // doing the original file_exists on ZIP archives...
  72          if (strtolower(substr($pFilename, 0, 3)) == 'zip') {
  73              // Open ZIP file and verify if the file exists
  74              $zipFile     = substr($pFilename, 6, strpos($pFilename, '#') - 6);
  75              $archiveFile = substr($pFilename, strpos($pFilename, '#') + 1);
  76  
  77              $zip = new ZipArchive();
  78              if ($zip->open($zipFile) === true) {
  79                  $returnValue = ($zip->getFromName($archiveFile) !== false);
  80                  $zip->close();
  81                  return $returnValue;
  82              } else {
  83                  return false;
  84              }
  85          } else {
  86              // Regular file_exists
  87              return file_exists($pFilename);
  88          }
  89      }
  90  
  91      /**
  92       * Returns canonicalized absolute pathname, also for ZIP archives
  93       *
  94       * @param string $pFilename
  95       * @return string
  96       */
  97      public static function realpath($pFilename)
  98      {
  99          // Returnvalue
 100          $returnValue = '';
 101  
 102          // Try using realpath()
 103          if (file_exists($pFilename)) {
 104              $returnValue = realpath($pFilename);
 105          }
 106  
 107          // Found something?
 108          if ($returnValue == '' || ($returnValue === null)) {
 109              $pathArray = explode('/', $pFilename);
 110              while (in_array('..', $pathArray) && $pathArray[0] != '..') {
 111                  for ($i = 0; $i < count($pathArray); ++$i) {
 112                      if ($pathArray[$i] == '..' && $i > 0) {
 113                          unset($pathArray[$i]);
 114                          unset($pathArray[$i - 1]);
 115                          break;
 116                      }
 117                  }
 118              }
 119              $returnValue = implode('/', $pathArray);
 120          }
 121  
 122          // Return
 123          return $returnValue;
 124      }
 125  
 126      /**
 127       * Get the systems temporary directory.
 128       *
 129       * @return string
 130       */
 131      public static function sys_get_temp_dir()
 132      {
 133      // Moodle hack!
 134      if (function_exists('make_temp_directory')) {
 135          $temp = make_temp_directory('phpexcel');
 136          return realpath(dirname($temp));
 137      }
 138  
 139          if (self::$useUploadTempDirectory) {
 140              //  use upload-directory when defined to allow running on environments having very restricted
 141              //      open_basedir configs
 142              if (ini_get('upload_tmp_dir') !== false) {
 143                  if ($temp = ini_get('upload_tmp_dir')) {
 144                      if (file_exists($temp)) {
 145                          return realpath($temp);
 146                      }
 147                  }
 148              }
 149          }
 150  
 151          // sys_get_temp_dir is only available since PHP 5.2.1
 152          // http://php.net/manual/en/function.sys-get-temp-dir.php#94119
 153          if (!function_exists('sys_get_temp_dir')) {
 154              if ($temp = getenv('TMP')) {
 155                  if ((!empty($temp)) && (file_exists($temp))) {
 156                      return realpath($temp);
 157                  }
 158              }
 159              if ($temp = getenv('TEMP')) {
 160                  if ((!empty($temp)) && (file_exists($temp))) {
 161                      return realpath($temp);
 162                  }
 163              }
 164              if ($temp = getenv('TMPDIR')) {
 165                  if ((!empty($temp)) && (file_exists($temp))) {
 166                      return realpath($temp);
 167                  }
 168              }
 169  
 170              // trick for creating a file in system's temporary dir
 171              // without knowing the path of the system's temporary dir
 172              $temp = tempnam(__FILE__, '');
 173              if (file_exists($temp)) {
 174                  unlink($temp);
 175                  return realpath(dirname($temp));
 176              }
 177  
 178              return null;
 179          }
 180  
 181          // use ordinary built-in PHP function
 182          //    There should be no problem with the 5.2.4 Suhosin realpath() bug, because this line should only
 183          //        be called if we're running 5.2.1 or earlier
 184          return realpath(sys_get_temp_dir());
 185      }
 186  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1