forked from pceres/GVExport
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.php
69 lines (60 loc) · 1.85 KB
/
utils.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
/**
* Returns the temporary dir
*
* Based on http://www.phpit.net/
* article/creating-zip-tar-archives-dynamically-php/2/
*
* changed to prevent SAFE_MODE restrictions
*
* @return string System temp dir
*/
function sys_get_temp_dir_my() {
// Try to get from environment variable
if ( !empty( $_ENV['TMP']) && is__writable($_ENV,'TMP') ) {
return realpath( $_ENV['TMP']);
} elseif ( !empty( $_ENV['TMPDIR']) && is__writable($_ENV,'TMPDIR') ) {
return realpath( $_ENV['TMPDIR']);
} elseif ( !empty( $_ENV['TEMP']) && is__writable($_ENV,'TEMP') ) {
return realpath( $_ENV['TEMP'] );
}
// Detect by creating a temporary file
else {
// Try to use system's temporary directory
// as random name shouldn't exist
$temp_file = tempnam( sys_get_temp_dir(), md5( uniqid( rand(), TRUE)));
if ( $temp_file ) {
if (!is__writable(dirname( $temp_file))) {
unlink( $temp_file );
// Last resort: try index folder
// as random name shouldn't exist
$temp_file = tempnam(realpath("index/"), md5( uniqid( rand(), TRUE)));
}
$temp_dir = realpath( dirname( $temp_file));
unlink( $temp_file );
return $temp_dir;
} else {
return FALSE;
}
}
}
function is__writable($path) {
//will work in despite of Windows ACLs bug
//NOTE: use a trailing slash for folders!!!
//see http://bugs.php.net/bug.php?id=27609
//see http://bugs.php.net/bug.php?id=30931
if ($path{strlen($path)-1}=='/') // recursively return a temporary file path
return is__writable($path.uniqid(mt_rand()).'.tmp');
else if (is_dir($path))
return is__writable($path.'/'.uniqid(mt_rand()).'.tmp');
// check tmp file for read/write capabilities
$rm = file_exists($path);
$f = @fopen($path, 'a');
if ($f===false)
return false;
fclose($f);
if (!$rm)
unlink($path);
return true;
}
?>