-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathClient.php
209 lines (160 loc) · 4.37 KB
/
Client.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<?php
/**
* @fileoverview Client - File Manager
* @author Vincent Thibault (alias KeyWorld - Twitter: @robrowser)
* @version 1.5.1
*/
final class Client
{
/**
* @var {string} client path
*/
static public $path = '';
/**
* @var {string} data.ini file
*/
static public $data_ini = '';
/**
* @var {Array} grf list
*/
static private $grfs = array();
/**
* @var {boolean} auto extract mode
*/
static public $AutoExtract = false;
/**
* Initialize client file
*/
static public function init()
{
if (empty(self::$data_ini)) {
Debug::write('No DATA.INI file defined in configs ?');
return;
}
$path = self::$path . self::$data_ini;
if (!file_exists($path)) {
Debug::write('File not found: ' . $path, 'error');
return;
}
if (!is_readable($path)) {
Debug::write('Can\'t read file: ' . $path, 'error');
return;
}
// Setup GRF context
$data_ini = parse_ini_file($path, true );
$grfs = array();
$info = pathinfo($path);
$keys = array_keys($data_ini);
$index = array_search('data', array_map('strtolower', $keys));
if ($index === false) {
Debug::write('Can\'t find token "[Data]" in "' . $path . '".', 'error');
return;
}
$grfs = $data_ini[ $keys[$index] ];
ksort($grfs);
Debug::write('File ' . $path . ' loaded.', 'success');
Debug::write('GRFs to use :', 'info');
// Open GRFs files
foreach ($grfs as $index => $grf_filename) {
Debug::write($index . ') ' . $info['dirname'] . '/' . $grf_filename);
self::$grfs[$index] = new Grf($info['dirname'] . '/' . $grf_filename);
self::$grfs[$index]->filename = $grf_filename;
}
}
/**
* Get a file from client, search it on data folder first and then on grf
*
* @param {string} file path
* @return {boolean} success
*/
static public function getFile($path)
{
$local_path = self::$path;
$local_path .= str_replace('\\', '/', $path );
$local_pathEncoded = utf8_encode($local_path);
$grf_path = str_replace('/', '\\', $path );
Debug::write('Searching file ' . $path . '...', 'title');
// Read data first
if (file_exists($local_pathEncoded) && !is_dir($local_pathEncoded) && is_readable($local_pathEncoded)) {
Debug::write('File found at ' . $local_path, 'success');
// Store file
if(self::$AutoExtract) {
return self::store( $path, file_get_contents($local_pathEncoded) );
}
return file_get_contents($local_pathEncoded);
}
else {
Debug::write('File not found at ' . $local_path);
}
foreach (self::$grfs as $grf) {
// Load GRF just if needed
if (!$grf->loaded) {
Debug::write('Loading GRF: ' . $grf->filename, 'info');
$grf->load();
}
// If file is found
if ($grf->getFile($grf_path, $content)) {
if (self::$AutoExtract) {
return self::store( $path, $content );
}
return $content;
}
}
return false;
}
/**
* Storing file in data folder (convert it if needed)
*
* @param {string} save to path
* @param {string} file content
* @return {string} content
*/
static public function store( $path, $content )
{
$path = utf8_encode($path);
$current_path = self::$path;
$local_path = $current_path . str_replace('\\', '/', $path );
$parent_path = preg_replace("/[^\/]+$/", '', $local_path);
if (!file_exists($parent_path)) {
if (!@mkdir( $parent_path, 0777, true)) {
Debug::write("Can't build path '{$parent_path}', need write permission ?", 'error');
return $content;
}
}
if (!is_writable($parent_path)) {
Debug::write("Can't write file to '{$parent_path}', need write permission.", 'error');
return $content;
}
// storing bmp images as png
if (strtolower(pathinfo($path, PATHINFO_EXTENSION)) === 'bmp') {
$img = imagecreatefrombmpstring( $content );
$path = str_ireplace('.bmp', '.png', $local_path);
imagepng($img, $path );
return file_get_contents( $path );
}
// Saving file
file_put_contents( $local_path, $content);
return $content;
}
/**
* Search files (only work in GRF)
*
* @param {string} regex
* @return {Array} file list
*/
static public function search($filter) {
$out = array();
foreach (self::$grfs as $grf) {
// Load GRF only if needed
if (!$grf->loaded) {
$grf->load();
}
// Search
$list = $grf->search($filter);
// Merge
$out = array_unique( array_merge($out, $list) );
}
//sort($out);
return $out;
}
}