/PHPExcel/Shared/File.php

https://bitbucket.org/nfredricks/wp-employee-time · PHP · 139 lines · 61 code · 13 blank · 65 comment · 26 complexity · bbe57005ef060067bf71ce2fdfc0b277 MD5 · raw file

  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2012 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 - 2012 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version 1.7.8, 2012-10-12
  26. */
  27. /**
  28. * PHPExcel_Shared_File
  29. *
  30. * @category PHPExcel
  31. * @package PHPExcel_Shared
  32. * @copyright Copyright (c) 2006 - 2012 PHPExcel (http://www.codeplex.com/PHPExcel)
  33. */
  34. class PHPExcel_Shared_File
  35. {
  36. /**
  37. * Verify if a file exists
  38. *
  39. * @param string $pFilename Filename
  40. * @return bool
  41. */
  42. public static function file_exists($pFilename) {
  43. // Sick construction, but it seems that
  44. // file_exists returns strange values when
  45. // doing the original file_exists on ZIP archives...
  46. if ( strtolower(substr($pFilename, 0, 3)) == 'zip' ) {
  47. // Open ZIP file and verify if the file exists
  48. $zipFile = substr($pFilename, 6, strpos($pFilename, '#') - 6);
  49. $archiveFile = substr($pFilename, strpos($pFilename, '#') + 1);
  50. $zip = new ZipArchive();
  51. if ($zip->open($zipFile) === true) {
  52. $returnValue = ($zip->getFromName($archiveFile) !== false);
  53. $zip->close();
  54. return $returnValue;
  55. } else {
  56. return false;
  57. }
  58. } else {
  59. // Regular file_exists
  60. return file_exists($pFilename);
  61. }
  62. }
  63. /**
  64. * Returns canonicalized absolute pathname, also for ZIP archives
  65. *
  66. * @param string $pFilename
  67. * @return string
  68. */
  69. public static function realpath($pFilename) {
  70. // Returnvalue
  71. $returnValue = '';
  72. // Try using realpath()
  73. if (file_exists($pFilename)) {
  74. $returnValue = realpath($pFilename);
  75. }
  76. // Found something?
  77. if ($returnValue == '' || ($returnValue === NULL)) {
  78. $pathArray = explode('/' , $pFilename);
  79. while(in_array('..', $pathArray) && $pathArray[0] != '..') {
  80. for ($i = 0; $i < count($pathArray); ++$i) {
  81. if ($pathArray[$i] == '..' && $i > 0) {
  82. unset($pathArray[$i]);
  83. unset($pathArray[$i - 1]);
  84. break;
  85. }
  86. }
  87. }
  88. $returnValue = implode('/', $pathArray);
  89. }
  90. // Return
  91. return $returnValue;
  92. }
  93. /**
  94. * Get the systems temporary directory.
  95. *
  96. * @return string
  97. */
  98. public static function sys_get_temp_dir()
  99. {
  100. // sys_get_temp_dir is only available since PHP 5.2.1
  101. // http://php.net/manual/en/function.sys-get-temp-dir.php#94119
  102. if ( !function_exists('sys_get_temp_dir')) {
  103. if ($temp = getenv('TMP') ) {
  104. if ((!empty($temp)) && (file_exists($temp))) { return realpath($temp); }
  105. }
  106. if ($temp = getenv('TEMP') ) {
  107. if ((!empty($temp)) && (file_exists($temp))) { return realpath($temp); }
  108. }
  109. if ($temp = getenv('TMPDIR') ) {
  110. if ((!empty($temp)) && (file_exists($temp))) { return realpath($temp); }
  111. }
  112. // trick for creating a file in system's temporary dir
  113. // without knowing the path of the system's temporary dir
  114. $temp = tempnam(__FILE__, '');
  115. if (file_exists($temp)) {
  116. unlink($temp);
  117. return realpath(dirname($temp));
  118. }
  119. return null;
  120. }
  121. // use ordinary built-in PHP function
  122. // There should be no problem with the 5.2.4 Suhosin realpath() bug, because this line should only
  123. // be called if we're running 5.2.1 or earlier
  124. return realpath(sys_get_temp_dir());
  125. }
  126. }