PageRenderTime 25ms CodeModel.GetById 7ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 1ms

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