File.php 6.0 KB

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