No Description

File.php 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2014 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 - 2014 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. /**
  28. * PHPExcel_Shared_File
  29. *
  30. * @category PHPExcel
  31. * @package PHPExcel_Shared
  32. * @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
  33. */
  34. class PHPExcel_Shared_File
  35. {
  36. /*
  37. * Use Temp or File Upload Temp for temporary files
  38. *
  39. * @protected
  40. * @var boolean
  41. */
  42. protected static $_useUploadTempDirectory = FALSE;
  43. /**
  44. * Set the flag indicating whether the File Upload Temp directory should be used for temporary files
  45. *
  46. * @param boolean $useUploadTempDir Use File Upload Temporary directory (true or false)
  47. */
  48. public static function setUseUploadTempDirectory($useUploadTempDir = FALSE) {
  49. self::$_useUploadTempDirectory = (boolean) $useUploadTempDir;
  50. } // function setUseUploadTempDirectory()
  51. /**
  52. * Get the flag indicating whether the File Upload Temp directory should be used for temporary files
  53. *
  54. * @return boolean Use File Upload Temporary directory (true or false)
  55. */
  56. public static function getUseUploadTempDirectory() {
  57. return self::$_useUploadTempDirectory;
  58. } // function getUseUploadTempDirectory()
  59. /**
  60. * Verify if a file exists
  61. *
  62. * @param string $pFilename Filename
  63. * @return bool
  64. */
  65. public static function file_exists($pFilename) {
  66. // Sick construction, but it seems that
  67. // file_exists returns strange values when
  68. // doing the original file_exists on ZIP archives...
  69. if ( strtolower(substr($pFilename, 0, 3)) == 'zip' ) {
  70. // Open ZIP file and verify if the file exists
  71. $zipFile = substr($pFilename, 6, strpos($pFilename, '#') - 6);
  72. $archiveFile = substr($pFilename, strpos($pFilename, '#') + 1);
  73. $zip = new ZipArchive();
  74. if ($zip->open($zipFile) === true) {
  75. $returnValue = ($zip->getFromName($archiveFile) !== false);
  76. $zip->close();
  77. return $returnValue;
  78. } else {
  79. return false;
  80. }
  81. } else {
  82. // Regular file_exists
  83. return file_exists($pFilename);
  84. }
  85. }
  86. /**
  87. * Returns canonicalized absolute pathname, also for ZIP archives
  88. *
  89. * @param string $pFilename
  90. * @return string
  91. */
  92. public static function realpath($pFilename) {
  93. // Returnvalue
  94. $returnValue = '';
  95. // Try using realpath()
  96. if (file_exists($pFilename)) {
  97. $returnValue = realpath($pFilename);
  98. }
  99. // Found something?
  100. if ($returnValue == '' || ($returnValue === NULL)) {
  101. $pathArray = explode('/' , $pFilename);
  102. while(in_array('..', $pathArray) && $pathArray[0] != '..') {
  103. for ($i = 0; $i < count($pathArray); ++$i) {
  104. if ($pathArray[$i] == '..' && $i > 0) {
  105. unset($pathArray[$i]);
  106. unset($pathArray[$i - 1]);
  107. break;
  108. }
  109. }
  110. }
  111. $returnValue = implode('/', $pathArray);
  112. }
  113. // Return
  114. return $returnValue;
  115. }
  116. /**
  117. * Get the systems temporary directory.
  118. *
  119. * @return string
  120. */
  121. public static function sys_get_temp_dir()
  122. {
  123. if (self::$_useUploadTempDirectory) {
  124. // use upload-directory when defined to allow running on environments having very restricted
  125. // open_basedir configs
  126. if (ini_get('upload_tmp_dir') !== FALSE) {
  127. if ($temp = ini_get('upload_tmp_dir')) {
  128. if (file_exists($temp))
  129. return realpath($temp);
  130. }
  131. }
  132. }
  133. // sys_get_temp_dir is only available since PHP 5.2.1
  134. // http://php.net/manual/en/function.sys-get-temp-dir.php#94119
  135. if ( !function_exists('sys_get_temp_dir')) {
  136. if ($temp = getenv('TMP') ) {
  137. if ((!empty($temp)) && (file_exists($temp))) { return realpath($temp); }
  138. }
  139. if ($temp = getenv('TEMP') ) {
  140. if ((!empty($temp)) && (file_exists($temp))) { return realpath($temp); }
  141. }
  142. if ($temp = getenv('TMPDIR') ) {
  143. if ((!empty($temp)) && (file_exists($temp))) { return realpath($temp); }
  144. }
  145. // trick for creating a file in system's temporary dir
  146. // without knowing the path of the system's temporary dir
  147. $temp = tempnam(__FILE__, '');
  148. if (file_exists($temp)) {
  149. unlink($temp);
  150. return realpath(dirname($temp));
  151. }
  152. return null;
  153. }
  154. // use ordinary built-in PHP function
  155. // There should be no problem with the 5.2.4 Suhosin realpath() bug, because this line should only
  156. // be called if we're running 5.2.1 or earlier
  157. return realpath(sys_get_temp_dir());
  158. }
  159. }