PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/phpexcel/PHPExcel/Shared/File.php

http://github.com/moodle/moodle
PHP | 186 lines | 91 code | 18 blank | 77 comment | 32 complexity | 6ca43a1506777635e2ef7c6d90fce1f2 MD5 | raw file
Possible License(s): MIT, AGPL-3.0, MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, Apache-2.0, LGPL-2.1, BSD-3-Clause
  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. // Moodle hack!
  121. if (function_exists('make_temp_directory')) {
  122. $temp = make_temp_directory('phpexcel');
  123. return realpath(dirname($temp));
  124. }
  125. if (self::$useUploadTempDirectory) {
  126. // use upload-directory when defined to allow running on environments having very restricted
  127. // open_basedir configs
  128. if (ini_get('upload_tmp_dir') !== false) {
  129. if ($temp = ini_get('upload_tmp_dir')) {
  130. if (file_exists($temp)) {
  131. return realpath($temp);
  132. }
  133. }
  134. }
  135. }
  136. // sys_get_temp_dir is only available since PHP 5.2.1
  137. // http://php.net/manual/en/function.sys-get-temp-dir.php#94119
  138. if (!function_exists('sys_get_temp_dir')) {
  139. if ($temp = getenv('TMP')) {
  140. if ((!empty($temp)) && (file_exists($temp))) {
  141. return realpath($temp);
  142. }
  143. }
  144. if ($temp = getenv('TEMP')) {
  145. if ((!empty($temp)) && (file_exists($temp))) {
  146. return realpath($temp);
  147. }
  148. }
  149. if ($temp = getenv('TMPDIR')) {
  150. if ((!empty($temp)) && (file_exists($temp))) {
  151. return realpath($temp);
  152. }
  153. }
  154. // trick for creating a file in system's temporary dir
  155. // without knowing the path of the system's temporary dir
  156. $temp = tempnam(__FILE__, '');
  157. if (file_exists($temp)) {
  158. unlink($temp);
  159. return realpath(dirname($temp));
  160. }
  161. return null;
  162. }
  163. // use ordinary built-in PHP function
  164. // There should be no problem with the 5.2.4 Suhosin realpath() bug, because this line should only
  165. // be called if we're running 5.2.1 or earlier
  166. return realpath(sys_get_temp_dir());
  167. }
  168. }