-
Notifications
You must be signed in to change notification settings - Fork 2
/
TPCServerLocal.php
100 lines (85 loc) · 2.18 KB
/
TPCServerLocal.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
/**
* Local file system storage back-end.
*
* @file
* @author Nmlgc
*/
class TPCServerLocal extends TPCServer {
// Local root path.
protected $rootPath = null;
/// ===================
/// Recursive functions
/// ===================
// Recursively create a long directory path
protected static function createPath( $path ) {
if ( is_dir( $path ) ) {
return true;
}
$len = strrpos( $path, '/', -2 );
if ( $len ) {
$prevPath = substr( $path, 0, $len + 1 );
self::createPath( $prevPath );
}
// Why PHP decided that an empty path is worthy of a warning is beyond me
return $path ? mkdir( $path ) : true;
}
// ------------ lixlpixel recursive PHP functions -------------
// removePath( directory to delete, empty )
// ------------------------------------------------------------
public static function removePath( $path, $empty = FALSE ) {
if ( substr( $path, -1 ) == '/' ) {
$path = substr( $path, 0, -1 );
}
if ( !file_exists ( $path ) || !is_dir ( $path ) ) {
return FALSE;
} elseif ( is_readable( $path ) ) {
$handle = opendir( $path );
while ( FALSE !== ( $item = readdir( $handle ) ) ) {
if ( $item != '.' && $item != '..' ) {
$curPath = $path . '/' . $item;
if ( is_dir ( $curPath ) ) {
self::removePath( $curPath );
} else {
unlink( $curPath );
}
}
}
closedir( $handle );
if ( $empty == FALSE ) {
if ( !rmdir( $path ) ) {
return FALSE;
}
}
}
return TRUE;
}
// ------------------------------------------------------------
/// ===================
/// TPCServer functions
/// ===================
function __construct( array &$serverInfo ) {
$this->rootPath = $serverInfo['local_path'];
}
function wipe() {
self::removePath( $this->rootPath, true );
}
function mkdir( &$dir ) {
return self::createPath( $this->rootPath . $dir );
}
function chdir( &$dir ) {
chdir( $this->rootPath . $dir );
}
function put( &$fn, &$data ) {
file_put_contents( $fn, $data, LOCK_EX );
}
function get( &$fn ) {
return file_get_contents( $fn );
}
function copy( &$target, &$source ) {
copy( $source, $target );
}
function delete( &$target ) {
unlink( $target );
}
};