Chris@76
|
1 <?php
|
Chris@76
|
2
|
Chris@76
|
3 /**
|
Chris@76
|
4 * Simple Machines Forum (SMF)
|
Chris@76
|
5 *
|
Chris@76
|
6 * @package SMF
|
Chris@76
|
7 * @author Simple Machines http://www.simplemachines.org
|
Chris@76
|
8 * @copyright 2011 Simple Machines
|
Chris@76
|
9 * @license http://www.simplemachines.org/about/smf/license.php BSD
|
Chris@76
|
10 *
|
Chris@76
|
11 * @version 2.0.1
|
Chris@76
|
12 */
|
Chris@76
|
13
|
Chris@76
|
14 if (!defined('SMF'))
|
Chris@76
|
15 die('Hacking attempt...');
|
Chris@76
|
16
|
Chris@76
|
17 /* This file's central purpose of existence is that of making the package
|
Chris@76
|
18 manager work nicely. It contains functions for handling tar.gz and zip
|
Chris@76
|
19 files, as well as a simple xml parser to handle the xml package stuff.
|
Chris@76
|
20 Not to mention a few functions to make file handling easier.
|
Chris@76
|
21
|
Chris@76
|
22 array read_tgz_file(string filename, string destination,
|
Chris@76
|
23 bool single_file = false, bool overwrite = false, array files_to_extract = null)
|
Chris@76
|
24 - reads a .tar.gz file, filename, in and extracts file(s) from it.
|
Chris@76
|
25 - essentially just a shortcut for read_tgz_data().
|
Chris@76
|
26
|
Chris@76
|
27 array read_tgz_data(string data, string destination,
|
Chris@76
|
28 bool single_file = false, bool overwrite = false, array files_to_extract = null)
|
Chris@76
|
29 - extracts a file or files from the .tar.gz contained in data.
|
Chris@76
|
30 - detects if the file is really a .zip file, and if so returns the
|
Chris@76
|
31 result of read_zip_data
|
Chris@76
|
32 - if destination is null, returns a list of files in the archive.
|
Chris@76
|
33 - if single_file is true, returns the contents of the file specified
|
Chris@76
|
34 by destination, if it exists, or false.
|
Chris@76
|
35 - if single_file is true, destination can start with * and / to
|
Chris@76
|
36 signify that the file may come from any directory.
|
Chris@76
|
37 - destination should not begin with a / if single_file is true.
|
Chris@76
|
38 - overwrites existing files with newer modification times if and
|
Chris@76
|
39 only if overwrite is true.
|
Chris@76
|
40 - creates the destination directory if it doesn't exist, and is
|
Chris@76
|
41 is specified.
|
Chris@76
|
42 - requires zlib support be built into PHP.
|
Chris@76
|
43 - returns an array of the files extracted.
|
Chris@76
|
44 - if files_to_extract is not equal to null only extracts file within this array.
|
Chris@76
|
45
|
Chris@76
|
46 array read_zip_data(string data, string destination,
|
Chris@76
|
47 bool single_file = false, bool overwrite = false, array files_to_extract = null)
|
Chris@76
|
48 - extracts a file or files from the .zip contained in data.
|
Chris@76
|
49 - if destination is null, returns a list of files in the archive.
|
Chris@76
|
50 - if single_file is true, returns the contents of the file specified
|
Chris@76
|
51 by destination, if it exists, or false.
|
Chris@76
|
52 - if single_file is true, destination can start with * and / to
|
Chris@76
|
53 signify that the file may come from any directory.
|
Chris@76
|
54 - destination should not begin with a / if single_file is true.
|
Chris@76
|
55 - overwrites existing files with newer modification times if and
|
Chris@76
|
56 only if overwrite is true.
|
Chris@76
|
57 - creates the destination directory if it doesn't exist, and is
|
Chris@76
|
58 is specified.
|
Chris@76
|
59 - requires zlib support be built into PHP.
|
Chris@76
|
60 - returns an array of the files extracted.
|
Chris@76
|
61 - if files_to_extract is not equal to null only extracts file within this array.
|
Chris@76
|
62
|
Chris@76
|
63 bool url_exists(string url)
|
Chris@76
|
64 - checks to see if url is valid, and returns a 200 status code.
|
Chris@76
|
65 - will return false if the file is "moved permanently" or similar.
|
Chris@76
|
66 - returns true if the remote url exists.
|
Chris@76
|
67
|
Chris@76
|
68 array loadInstalledPackages()
|
Chris@76
|
69 - loads and returns an array of installed packages.
|
Chris@76
|
70 - gets this information from Packages/installed.list.
|
Chris@76
|
71 - returns the array of data.
|
Chris@76
|
72
|
Chris@76
|
73 array getPackageInfo(string filename)
|
Chris@76
|
74 - loads a package's information and returns a representative array.
|
Chris@76
|
75 - expects the file to be a package in Packages/.
|
Chris@76
|
76 - returns a error string if the package-info is invalid.
|
Chris@76
|
77 - returns a basic array of id, version, filename, and similar
|
Chris@76
|
78 information.
|
Chris@76
|
79 - in the array returned, an xmlArray is available in 'xml'.
|
Chris@76
|
80
|
Chris@76
|
81 void packageRequireFTP(string destination_url, array files = none, bool return = false)
|
Chris@76
|
82 // !!!
|
Chris@76
|
83
|
Chris@76
|
84 array parsePackageInfo(xmlArray &package, bool testing_only = true,
|
Chris@76
|
85 string method = 'install', string previous_version = '')
|
Chris@76
|
86 - parses the actions in package-info.xml files from packages.
|
Chris@76
|
87 - package should be an xmlArray with package-info as its base.
|
Chris@76
|
88 - testing_only should be true if the package should not actually be
|
Chris@76
|
89 applied.
|
Chris@76
|
90 - method is upgrade, install, or uninstall. Its default is install.
|
Chris@76
|
91 - previous_version should be set to the previous installed version
|
Chris@76
|
92 of this package, if any.
|
Chris@76
|
93 - does not handle failure terribly well; testing first is always
|
Chris@76
|
94 better.
|
Chris@76
|
95 - returns an array of those changes made.
|
Chris@76
|
96
|
Chris@76
|
97 bool matchPackageVersion(string version, string versions)
|
Chris@76
|
98 - checks if version matches any of the versions in versions.
|
Chris@76
|
99 - supports comma separated version numbers, with or without
|
Chris@76
|
100 whitespace.
|
Chris@76
|
101 - supports lower and upper bounds. (1.0-1.2)
|
Chris@76
|
102 - returns true if the version matched.
|
Chris@76
|
103
|
Chris@76
|
104 int compareVersions(string version1, string version2)
|
Chris@76
|
105 - compares two versions.
|
Chris@76
|
106 - returns 0 if version1 is equal to version2.
|
Chris@76
|
107 - returns -1 if version1 is lower than version2.
|
Chris@76
|
108 - returns 1 if version1 is higher than version2.
|
Chris@76
|
109
|
Chris@76
|
110 string parse_path(string path)
|
Chris@76
|
111 - parses special identifiers out of the specified path.
|
Chris@76
|
112 - returns the parsed path.
|
Chris@76
|
113
|
Chris@76
|
114 void deltree(string path, bool delete_directory = true)
|
Chris@76
|
115 - deletes a directory, and all the files and direcories inside it.
|
Chris@76
|
116 - requires access to delete these files.
|
Chris@76
|
117
|
Chris@76
|
118 bool mktree(string path, int mode)
|
Chris@76
|
119 - creates the specified tree structure with the mode specified.
|
Chris@76
|
120 - creates every directory in path until it finds one that already
|
Chris@76
|
121 exists.
|
Chris@76
|
122 - returns true if successful, false otherwise.
|
Chris@76
|
123
|
Chris@76
|
124 void copytree(string source, string destination)
|
Chris@76
|
125 - copies one directory structure over to another.
|
Chris@76
|
126 - requires the destination to be writable.
|
Chris@76
|
127
|
Chris@76
|
128 void listtree(string path, string sub_path = none)
|
Chris@76
|
129 // !!!
|
Chris@76
|
130
|
Chris@76
|
131 array parseModification(string file, bool testing = true, bool undo = false, array theme_paths = array())
|
Chris@76
|
132 - parses a xml-style modification file (file).
|
Chris@76
|
133 - testing tells it the modifications shouldn't actually be saved.
|
Chris@76
|
134 - undo specifies that the modifications the file requests should be
|
Chris@76
|
135 undone; this doesn't work with everything (regular expressions.)
|
Chris@76
|
136 - returns an array of those changes made.
|
Chris@76
|
137
|
Chris@76
|
138 array parseBoardMod(string file, bool testing = true, bool undo = false, array theme_paths = array())
|
Chris@76
|
139 - parses a boardmod-style modification file (file).
|
Chris@76
|
140 - testing tells it the modifications shouldn't actually be saved.
|
Chris@76
|
141 - undo specifies that the modifications the file requests should be
|
Chris@76
|
142 undone.
|
Chris@76
|
143 - returns an array of those changes made.
|
Chris@76
|
144
|
Chris@76
|
145 // !!!
|
Chris@76
|
146
|
Chris@76
|
147 int package_put_contents(string filename, string data)
|
Chris@76
|
148 - writes data to a file, almost exactly like the file_put_contents()
|
Chris@76
|
149 function.
|
Chris@76
|
150 - uses FTP to create/chmod the file when necessary and available.
|
Chris@76
|
151 - uses text mode for text mode file extensions.
|
Chris@76
|
152 - returns the number of bytes written.
|
Chris@76
|
153
|
Chris@76
|
154 void package_chmod(string filename)
|
Chris@76
|
155 // !!!
|
Chris@76
|
156
|
Chris@76
|
157 string package_crypt(string password)
|
Chris@76
|
158 // !!!
|
Chris@76
|
159
|
Chris@76
|
160 string fetch_web_data(string url, string post_data = '',
|
Chris@76
|
161 bool keep_alive = false)
|
Chris@76
|
162 // !!!
|
Chris@76
|
163
|
Chris@76
|
164 Creating your own package server:
|
Chris@76
|
165 ---------------------------------------------------------------------------
|
Chris@76
|
166 // !!!
|
Chris@76
|
167
|
Chris@76
|
168 Creating your own package:
|
Chris@76
|
169 ---------------------------------------------------------------------------
|
Chris@76
|
170 // !!!
|
Chris@76
|
171 */
|
Chris@76
|
172
|
Chris@76
|
173 // Get the data from the file and extract it.
|
Chris@76
|
174 function read_tgz_file($gzfilename, $destination, $single_file = false, $overwrite = false, $files_to_extract = null)
|
Chris@76
|
175 {
|
Chris@76
|
176 if (substr($gzfilename, 0, 7) == 'http://')
|
Chris@76
|
177 {
|
Chris@76
|
178 $data = fetch_web_data($gzfilename);
|
Chris@76
|
179
|
Chris@76
|
180 if ($data === false)
|
Chris@76
|
181 return false;
|
Chris@76
|
182 }
|
Chris@76
|
183 else
|
Chris@76
|
184 {
|
Chris@76
|
185 $data = @file_get_contents($gzfilename);
|
Chris@76
|
186
|
Chris@76
|
187 if ($data === false)
|
Chris@76
|
188 return false;
|
Chris@76
|
189 }
|
Chris@76
|
190
|
Chris@76
|
191 return read_tgz_data($data, $destination, $single_file, $overwrite, $files_to_extract);
|
Chris@76
|
192 }
|
Chris@76
|
193
|
Chris@76
|
194 // Extract tar.gz data. If destination is null, return a listing.
|
Chris@76
|
195 function read_tgz_data($data, $destination, $single_file = false, $overwrite = false, $files_to_extract = null)
|
Chris@76
|
196 {
|
Chris@76
|
197 // Make sure we have this loaded.
|
Chris@76
|
198 loadLanguage('Packages');
|
Chris@76
|
199
|
Chris@76
|
200 // This function sorta needs gzinflate!
|
Chris@76
|
201 if (!function_exists('gzinflate'))
|
Chris@76
|
202 fatal_lang_error('package_no_zlib', 'critical');
|
Chris@76
|
203
|
Chris@76
|
204 umask(0);
|
Chris@76
|
205 if (!$single_file && $destination !== null && !file_exists($destination))
|
Chris@76
|
206 mktree($destination, 0777);
|
Chris@76
|
207
|
Chris@76
|
208 // No signature?
|
Chris@76
|
209 if (strlen($data) < 2)
|
Chris@76
|
210 return false;
|
Chris@76
|
211
|
Chris@76
|
212 $id = unpack('H2a/H2b', substr($data, 0, 2));
|
Chris@76
|
213 if (strtolower($id['a'] . $id['b']) != '1f8b')
|
Chris@76
|
214 {
|
Chris@76
|
215 // Okay, this ain't no tar.gz, but maybe it's a zip file.
|
Chris@76
|
216 if (substr($data, 0, 2) == 'PK')
|
Chris@76
|
217 return read_zip_data($data, $destination, $single_file, $overwrite, $files_to_extract);
|
Chris@76
|
218 else
|
Chris@76
|
219 return false;
|
Chris@76
|
220 }
|
Chris@76
|
221
|
Chris@76
|
222 $flags = unpack('Ct/Cf', substr($data, 2, 2));
|
Chris@76
|
223
|
Chris@76
|
224 // Not deflate!
|
Chris@76
|
225 if ($flags['t'] != 8)
|
Chris@76
|
226 return false;
|
Chris@76
|
227 $flags = $flags['f'];
|
Chris@76
|
228
|
Chris@76
|
229 $offset = 10;
|
Chris@76
|
230 $octdec = array('mode', 'uid', 'gid', 'size', 'mtime', 'checksum', 'type');
|
Chris@76
|
231
|
Chris@76
|
232 // "Read" the filename and comment. // !!! Might be mussed.
|
Chris@76
|
233 if ($flags & 12)
|
Chris@76
|
234 {
|
Chris@76
|
235 while ($flags & 8 && $data{$offset++} != "\0")
|
Chris@76
|
236 continue;
|
Chris@76
|
237 while ($flags & 4 && $data{$offset++} != "\0")
|
Chris@76
|
238 continue;
|
Chris@76
|
239 }
|
Chris@76
|
240
|
Chris@76
|
241 $crc = unpack('Vcrc32/Visize', substr($data, strlen($data) - 8, 8));
|
Chris@76
|
242 $data = @gzinflate(substr($data, $offset, strlen($data) - 8 - $offset));
|
Chris@76
|
243
|
Chris@76
|
244 // smf_crc32 and crc32 may not return the same results, so we accept either.
|
Chris@76
|
245 if ($crc['crc32'] != smf_crc32($data) && $crc['crc32'] != crc32($data))
|
Chris@76
|
246 return false;
|
Chris@76
|
247
|
Chris@76
|
248 $blocks = strlen($data) / 512 - 1;
|
Chris@76
|
249 $offset = 0;
|
Chris@76
|
250
|
Chris@76
|
251 $return = array();
|
Chris@76
|
252
|
Chris@76
|
253 while ($offset < $blocks)
|
Chris@76
|
254 {
|
Chris@76
|
255 $header = substr($data, $offset << 9, 512);
|
Chris@76
|
256 $current = unpack('a100filename/a8mode/a8uid/a8gid/a12size/a12mtime/a8checksum/a1type/a100linkname/a6magic/a2version/a32uname/a32gname/a8devmajor/a8devminor/a155path', $header);
|
Chris@76
|
257
|
Chris@76
|
258 // Blank record? This is probably at the end of the file.
|
Chris@76
|
259 if (empty($current['filename']))
|
Chris@76
|
260 {
|
Chris@76
|
261 $offset += 512;
|
Chris@76
|
262 continue;
|
Chris@76
|
263 }
|
Chris@76
|
264
|
Chris@76
|
265 if ($current['type'] == 5 && substr($current['filename'], -1) != '/')
|
Chris@76
|
266 $current['filename'] .= '/';
|
Chris@76
|
267
|
Chris@76
|
268 foreach ($current as $k => $v)
|
Chris@76
|
269 {
|
Chris@76
|
270 if (in_array($k, $octdec))
|
Chris@76
|
271 $current[$k] = octdec(trim($v));
|
Chris@76
|
272 else
|
Chris@76
|
273 $current[$k] = trim($v);
|
Chris@76
|
274 }
|
Chris@76
|
275
|
Chris@76
|
276 $checksum = 256;
|
Chris@76
|
277 for ($i = 0; $i < 148; $i++)
|
Chris@76
|
278 $checksum += ord($header{$i});
|
Chris@76
|
279 for ($i = 156; $i < 512; $i++)
|
Chris@76
|
280 $checksum += ord($header{$i});
|
Chris@76
|
281
|
Chris@76
|
282 if ($current['checksum'] != $checksum)
|
Chris@76
|
283 break;
|
Chris@76
|
284
|
Chris@76
|
285 $size = ceil($current['size'] / 512);
|
Chris@76
|
286 $current['data'] = substr($data, ++$offset << 9, $current['size']);
|
Chris@76
|
287 $offset += $size;
|
Chris@76
|
288
|
Chris@76
|
289 // Not a directory and doesn't exist already...
|
Chris@76
|
290 if (substr($current['filename'], -1, 1) != '/' && !file_exists($destination . '/' . $current['filename']))
|
Chris@76
|
291 $write_this = true;
|
Chris@76
|
292 // File exists... check if it is newer.
|
Chris@76
|
293 elseif (substr($current['filename'], -1, 1) != '/')
|
Chris@76
|
294 $write_this = $overwrite || filemtime($destination . '/' . $current['filename']) < $current['mtime'];
|
Chris@76
|
295 // Folder... create.
|
Chris@76
|
296 elseif ($destination !== null && !$single_file)
|
Chris@76
|
297 {
|
Chris@76
|
298 // Protect from accidental parent directory writing...
|
Chris@76
|
299 $current['filename'] = strtr($current['filename'], array('../' => '', '/..' => ''));
|
Chris@76
|
300
|
Chris@76
|
301 if (!file_exists($destination . '/' . $current['filename']))
|
Chris@76
|
302 mktree($destination . '/' . $current['filename'], 0777);
|
Chris@76
|
303 $write_this = false;
|
Chris@76
|
304 }
|
Chris@76
|
305 else
|
Chris@76
|
306 $write_this = false;
|
Chris@76
|
307
|
Chris@76
|
308 if ($write_this && $destination !== null)
|
Chris@76
|
309 {
|
Chris@76
|
310 if (strpos($current['filename'], '/') !== false && !$single_file)
|
Chris@76
|
311 mktree($destination . '/' . dirname($current['filename']), 0777);
|
Chris@76
|
312
|
Chris@76
|
313 // Is this the file we're looking for?
|
Chris@76
|
314 if ($single_file && ($destination == $current['filename'] || $destination == '*/' . basename($current['filename'])))
|
Chris@76
|
315 return $current['data'];
|
Chris@76
|
316 // If we're looking for another file, keep going.
|
Chris@76
|
317 elseif ($single_file)
|
Chris@76
|
318 continue;
|
Chris@76
|
319 // Looking for restricted files?
|
Chris@76
|
320 elseif ($files_to_extract !== null && !in_array($current['filename'], $files_to_extract))
|
Chris@76
|
321 continue;
|
Chris@76
|
322
|
Chris@76
|
323 package_put_contents($destination . '/' . $current['filename'], $current['data']);
|
Chris@76
|
324 }
|
Chris@76
|
325
|
Chris@76
|
326 if (substr($current['filename'], -1, 1) != '/')
|
Chris@76
|
327 $return[] = array(
|
Chris@76
|
328 'filename' => $current['filename'],
|
Chris@76
|
329 'md5' => md5($current['data']),
|
Chris@76
|
330 'preview' => substr($current['data'], 0, 100),
|
Chris@76
|
331 'size' => $current['size'],
|
Chris@76
|
332 'skipped' => false
|
Chris@76
|
333 );
|
Chris@76
|
334 }
|
Chris@76
|
335
|
Chris@76
|
336 if ($destination !== null && !$single_file)
|
Chris@76
|
337 package_flush_cache();
|
Chris@76
|
338
|
Chris@76
|
339 if ($single_file)
|
Chris@76
|
340 return false;
|
Chris@76
|
341 else
|
Chris@76
|
342 return $return;
|
Chris@76
|
343 }
|
Chris@76
|
344
|
Chris@76
|
345 // Extract zip data. If destination is null, return a listing.
|
Chris@76
|
346 function read_zip_data($data, $destination, $single_file = false, $overwrite = false, $files_to_extract = null)
|
Chris@76
|
347 {
|
Chris@76
|
348 umask(0);
|
Chris@76
|
349 if ($destination !== null && !file_exists($destination) && !$single_file)
|
Chris@76
|
350 mktree($destination, 0777);
|
Chris@76
|
351
|
Chris@76
|
352 // Look for the PK header...
|
Chris@76
|
353 if (substr($data, 0, 2) != 'PK')
|
Chris@76
|
354 return false;
|
Chris@76
|
355
|
Chris@76
|
356 // Find the central whosamawhatsit at the end; if there's a comment it's a pain.
|
Chris@76
|
357 if (substr($data, -22, 4) == 'PK' . chr(5) . chr(6))
|
Chris@76
|
358 $p = -22;
|
Chris@76
|
359 else
|
Chris@76
|
360 {
|
Chris@76
|
361 // Have to find where the comment begins, ugh.
|
Chris@76
|
362 for ($p = -22; $p > -strlen($data); $p--)
|
Chris@76
|
363 {
|
Chris@76
|
364 if (substr($data, $p, 4) == 'PK' . chr(5) . chr(6))
|
Chris@76
|
365 break;
|
Chris@76
|
366 }
|
Chris@76
|
367 }
|
Chris@76
|
368
|
Chris@76
|
369 $return = array();
|
Chris@76
|
370
|
Chris@76
|
371 // Get the basic zip file info.
|
Chris@76
|
372 $zip_info = unpack('vfiles/Vsize/Voffset', substr($data, $p + 10, 10));
|
Chris@76
|
373
|
Chris@76
|
374 $p = $zip_info['offset'];
|
Chris@76
|
375 for ($i = 0; $i < $zip_info['files']; $i++)
|
Chris@76
|
376 {
|
Chris@76
|
377 // Make sure this is a file entry...
|
Chris@76
|
378 if (substr($data, $p, 4) != 'PK' . chr(1) . chr(2))
|
Chris@76
|
379 return false;
|
Chris@76
|
380
|
Chris@76
|
381 // Get all the important file information.
|
Chris@76
|
382 $file_info = unpack('Vcrc/Vcompressed_size/Vsize/vfilename_len/vextra_len/vcomment_len/vdisk/vinternal/Vexternal/Voffset', substr($data, $p + 16, 30));
|
Chris@76
|
383 $file_info['filename'] = substr($data, $p + 46, $file_info['filename_len']);
|
Chris@76
|
384
|
Chris@76
|
385 // Skip all the information we don't care about anyway.
|
Chris@76
|
386 $p += 46 + $file_info['filename_len'] + $file_info['extra_len'] + $file_info['comment_len'];
|
Chris@76
|
387
|
Chris@76
|
388 // If this is a file, and it doesn't exist.... happy days!
|
Chris@76
|
389 if (substr($file_info['filename'], -1, 1) != '/' && !file_exists($destination . '/' . $file_info['filename']))
|
Chris@76
|
390 $write_this = true;
|
Chris@76
|
391 // If the file exists, we may not want to overwrite it.
|
Chris@76
|
392 elseif (substr($file_info['filename'], -1, 1) != '/')
|
Chris@76
|
393 $write_this = $overwrite;
|
Chris@76
|
394 // This is a directory, so we're gonna want to create it. (probably...)
|
Chris@76
|
395 elseif ($destination !== null && !$single_file)
|
Chris@76
|
396 {
|
Chris@76
|
397 // Just a little accident prevention, don't mind me.
|
Chris@76
|
398 $file_info['filename'] = strtr($file_info['filename'], array('../' => '', '/..' => ''));
|
Chris@76
|
399
|
Chris@76
|
400 if (!file_exists($destination . '/' . $file_info['filename']))
|
Chris@76
|
401 mktree($destination . '/' . $file_info['filename'], 0777);
|
Chris@76
|
402 $write_this = false;
|
Chris@76
|
403 }
|
Chris@76
|
404 else
|
Chris@76
|
405 $write_this = false;
|
Chris@76
|
406
|
Chris@76
|
407 // Check that the data is there and does exist.
|
Chris@76
|
408 if (substr($data, $file_info['offset'], 4) != 'PK' . chr(3) . chr(4))
|
Chris@76
|
409 return false;
|
Chris@76
|
410
|
Chris@76
|
411 // Get the actual compressed data.
|
Chris@76
|
412 $file_info['data'] = substr($data, $file_info['offset'] + 30 + $file_info['filename_len'], $file_info['compressed_size']);
|
Chris@76
|
413
|
Chris@76
|
414 // Only inflate it if we need to ;).
|
Chris@76
|
415 if ($file_info['compressed_size'] != $file_info['size'])
|
Chris@76
|
416 $file_info['data'] = @gzinflate($file_info['data']);
|
Chris@76
|
417
|
Chris@76
|
418 // Okay! We can write this file, looks good from here...
|
Chris@76
|
419 if ($write_this && $destination !== null)
|
Chris@76
|
420 {
|
Chris@76
|
421 if (strpos($file_info['filename'], '/') !== false && !$single_file)
|
Chris@76
|
422 mktree($destination . '/' . dirname($file_info['filename']), 0777);
|
Chris@76
|
423
|
Chris@76
|
424 // If we're looking for a specific file, and this is it... ka-bam, baby.
|
Chris@76
|
425 if ($single_file && ($destination == $file_info['filename'] || $destination == '*/' . basename($file_info['filename'])))
|
Chris@76
|
426 return $file_info['data'];
|
Chris@76
|
427 // Oh? Another file. Fine. You don't like this file, do you? I know how it is. Yeah... just go away. No, don't apologize. I know this file's just not *good enough* for you.
|
Chris@76
|
428 elseif ($single_file)
|
Chris@76
|
429 continue;
|
Chris@76
|
430 // Don't really want this?
|
Chris@76
|
431 elseif ($files_to_extract !== null && !in_array($file_info['filename'], $files_to_extract))
|
Chris@76
|
432 continue;
|
Chris@76
|
433
|
Chris@76
|
434 package_put_contents($destination . '/' . $file_info['filename'], $file_info['data']);
|
Chris@76
|
435 }
|
Chris@76
|
436
|
Chris@76
|
437 if (substr($file_info['filename'], -1, 1) != '/')
|
Chris@76
|
438 $return[] = array(
|
Chris@76
|
439 'filename' => $file_info['filename'],
|
Chris@76
|
440 'md5' => md5($file_info['data']),
|
Chris@76
|
441 'preview' => substr($file_info['data'], 0, 100),
|
Chris@76
|
442 'size' => $file_info['size'],
|
Chris@76
|
443 'skipped' => false
|
Chris@76
|
444 );
|
Chris@76
|
445 }
|
Chris@76
|
446
|
Chris@76
|
447 if ($destination !== null && !$single_file)
|
Chris@76
|
448 package_flush_cache();
|
Chris@76
|
449
|
Chris@76
|
450 if ($single_file)
|
Chris@76
|
451 return false;
|
Chris@76
|
452 else
|
Chris@76
|
453 return $return;
|
Chris@76
|
454 }
|
Chris@76
|
455
|
Chris@76
|
456 // Checks the existence of a remote file since file_exists() does not do remote.
|
Chris@76
|
457 function url_exists($url)
|
Chris@76
|
458 {
|
Chris@76
|
459 $a_url = parse_url($url);
|
Chris@76
|
460
|
Chris@76
|
461 if (!isset($a_url['scheme']))
|
Chris@76
|
462 return false;
|
Chris@76
|
463
|
Chris@76
|
464 // Attempt to connect...
|
Chris@76
|
465 $temp = '';
|
Chris@76
|
466 $fid = fsockopen($a_url['host'], !isset($a_url['port']) ? 80 : $a_url['port'], $temp, $temp, 8);
|
Chris@76
|
467 if (!$fid)
|
Chris@76
|
468 return false;
|
Chris@76
|
469
|
Chris@76
|
470 fputs($fid, 'HEAD ' . $a_url['path'] . ' HTTP/1.0' . "\r\n" . 'Host: ' . $a_url['host'] . "\r\n\r\n");
|
Chris@76
|
471 $head = fread($fid, 1024);
|
Chris@76
|
472 fclose($fid);
|
Chris@76
|
473
|
Chris@76
|
474 return preg_match('~^HTTP/.+\s+(20[01]|30[127])~i', $head) == 1;
|
Chris@76
|
475 }
|
Chris@76
|
476
|
Chris@76
|
477 // Load the installed packages.
|
Chris@76
|
478 function loadInstalledPackages()
|
Chris@76
|
479 {
|
Chris@76
|
480 global $boarddir, $smcFunc;
|
Chris@76
|
481
|
Chris@76
|
482 // First, check that the database is valid, installed.list is still king.
|
Chris@76
|
483 $install_file = implode('', file($boarddir . '/Packages/installed.list'));
|
Chris@76
|
484 if (trim($install_file) == '')
|
Chris@76
|
485 {
|
Chris@76
|
486 $smcFunc['db_query']('', '
|
Chris@76
|
487 UPDATE {db_prefix}log_packages
|
Chris@76
|
488 SET install_state = {int:not_installed}',
|
Chris@76
|
489 array(
|
Chris@76
|
490 'not_installed' => 0,
|
Chris@76
|
491 )
|
Chris@76
|
492 );
|
Chris@76
|
493
|
Chris@76
|
494 // Don't have anything left, so send an empty array.
|
Chris@76
|
495 return array();
|
Chris@76
|
496 }
|
Chris@76
|
497
|
Chris@76
|
498 // Load the packages from the database - note this is ordered by install time to ensure latest package uninstalled first.
|
Chris@76
|
499 $request = $smcFunc['db_query']('', '
|
Chris@76
|
500 SELECT id_install, package_id, filename, name, version
|
Chris@76
|
501 FROM {db_prefix}log_packages
|
Chris@76
|
502 WHERE install_state != {int:not_installed}
|
Chris@76
|
503 ORDER BY time_installed DESC',
|
Chris@76
|
504 array(
|
Chris@76
|
505 'not_installed' => 0,
|
Chris@76
|
506 )
|
Chris@76
|
507 );
|
Chris@76
|
508 $installed = array();
|
Chris@76
|
509 $found = array();
|
Chris@76
|
510 while ($row = $smcFunc['db_fetch_assoc']($request))
|
Chris@76
|
511 {
|
Chris@76
|
512 // Already found this? If so don't add it twice!
|
Chris@76
|
513 if (in_array($row['package_id'], $found))
|
Chris@76
|
514 continue;
|
Chris@76
|
515
|
Chris@76
|
516 $found[] = $row['package_id'];
|
Chris@76
|
517
|
Chris@76
|
518 $installed[] = array(
|
Chris@76
|
519 'id' => $row['id_install'],
|
Chris@76
|
520 'name' => $row['name'],
|
Chris@76
|
521 'filename' => $row['filename'],
|
Chris@76
|
522 'package_id' => $row['package_id'],
|
Chris@76
|
523 'version' => $row['version'],
|
Chris@76
|
524 );
|
Chris@76
|
525 }
|
Chris@76
|
526 $smcFunc['db_free_result']($request);
|
Chris@76
|
527
|
Chris@76
|
528 return $installed;
|
Chris@76
|
529 }
|
Chris@76
|
530
|
Chris@76
|
531 function getPackageInfo($gzfilename)
|
Chris@76
|
532 {
|
Chris@76
|
533 global $boarddir;
|
Chris@76
|
534
|
Chris@76
|
535 // Extract package-info.xml from downloaded file. (*/ is used because it could be in any directory.)
|
Chris@76
|
536 if (strpos($gzfilename, 'http://') !== false)
|
Chris@76
|
537 $packageInfo = read_tgz_data(fetch_web_data($gzfilename, '', true), '*/package-info.xml', true);
|
Chris@76
|
538 else
|
Chris@76
|
539 {
|
Chris@76
|
540 if (!file_exists($boarddir . '/Packages/' . $gzfilename))
|
Chris@76
|
541 return 'package_get_error_not_found';
|
Chris@76
|
542
|
Chris@76
|
543 if (is_file($boarddir . '/Packages/' . $gzfilename))
|
Chris@76
|
544 $packageInfo = read_tgz_file($boarddir . '/Packages/' . $gzfilename, '*/package-info.xml', true);
|
Chris@76
|
545 elseif (file_exists($boarddir . '/Packages/' . $gzfilename . '/package-info.xml'))
|
Chris@76
|
546 $packageInfo = file_get_contents($boarddir . '/Packages/' . $gzfilename . '/package-info.xml');
|
Chris@76
|
547 else
|
Chris@76
|
548 return 'package_get_error_missing_xml';
|
Chris@76
|
549 }
|
Chris@76
|
550
|
Chris@76
|
551 // Nothing?
|
Chris@76
|
552 if (empty($packageInfo))
|
Chris@76
|
553 return 'package_get_error_is_zero';
|
Chris@76
|
554
|
Chris@76
|
555 // Parse package-info.xml into an xmlArray.
|
Chris@76
|
556 loadClassFile('Class-Package.php');
|
Chris@76
|
557 $packageInfo = new xmlArray($packageInfo);
|
Chris@76
|
558
|
Chris@76
|
559 // !!! Error message of some sort?
|
Chris@76
|
560 if (!$packageInfo->exists('package-info[0]'))
|
Chris@76
|
561 return 'package_get_error_packageinfo_corrupt';
|
Chris@76
|
562
|
Chris@76
|
563 $packageInfo = $packageInfo->path('package-info[0]');
|
Chris@76
|
564
|
Chris@76
|
565 $package = $packageInfo->to_array();
|
Chris@76
|
566 $package['xml'] = $packageInfo;
|
Chris@76
|
567 $package['filename'] = $gzfilename;
|
Chris@76
|
568
|
Chris@76
|
569 if (!isset($package['type']))
|
Chris@76
|
570 $package['type'] = 'modification';
|
Chris@76
|
571
|
Chris@76
|
572 return $package;
|
Chris@76
|
573 }
|
Chris@76
|
574
|
Chris@76
|
575 // Create a chmod control for chmoding files.
|
Chris@76
|
576 function create_chmod_control($chmodFiles = array(), $chmodOptions = array(), $restore_write_status = false)
|
Chris@76
|
577 {
|
Chris@76
|
578 global $context, $modSettings, $package_ftp, $boarddir, $txt, $sourcedir, $scripturl;
|
Chris@76
|
579
|
Chris@76
|
580 // If we're restoring the status of existing files prepare the data.
|
Chris@76
|
581 if ($restore_write_status && isset($_SESSION['pack_ftp']) && !empty($_SESSION['pack_ftp']['original_perms']))
|
Chris@76
|
582 {
|
Chris@76
|
583 function list_restoreFiles($dummy1, $dummy2, $dummy3, $do_change)
|
Chris@76
|
584 {
|
Chris@76
|
585 global $txt;
|
Chris@76
|
586
|
Chris@76
|
587 $restore_files = array();
|
Chris@76
|
588 foreach ($_SESSION['pack_ftp']['original_perms'] as $file => $perms)
|
Chris@76
|
589 {
|
Chris@76
|
590 // Check the file still exists, and the permissions were indeed different than now.
|
Chris@76
|
591 $file_permissions = @fileperms($file);
|
Chris@76
|
592 if (!file_exists($file) || $file_permissions == $perms)
|
Chris@76
|
593 {
|
Chris@76
|
594 unset($_SESSION['pack_ftp']['original_perms'][$file]);
|
Chris@76
|
595 continue;
|
Chris@76
|
596 }
|
Chris@76
|
597
|
Chris@76
|
598 // Are we wanting to change the permission?
|
Chris@76
|
599 if ($do_change && isset($_POST['restore_files']) && in_array($file, $_POST['restore_files']))
|
Chris@76
|
600 {
|
Chris@76
|
601 // Use FTP if we have it.
|
Chris@76
|
602 if (!empty($package_ftp))
|
Chris@76
|
603 {
|
Chris@76
|
604 $ftp_file = strtr($file, array($_SESSION['pack_ftp']['root'] => ''));
|
Chris@76
|
605 $package_ftp->chmod($ftp_file, $perms);
|
Chris@76
|
606 }
|
Chris@76
|
607 else
|
Chris@76
|
608 @chmod($file, $perms);
|
Chris@76
|
609
|
Chris@76
|
610 $new_permissions = @fileperms($file);
|
Chris@76
|
611 $result = $new_permissions == $perms ? 'success' : 'failure';
|
Chris@76
|
612 unset($_SESSION['pack_ftp']['original_perms'][$file]);
|
Chris@76
|
613 }
|
Chris@76
|
614 elseif ($do_change)
|
Chris@76
|
615 {
|
Chris@76
|
616 $new_permissions = '';
|
Chris@76
|
617 $result = 'skipped';
|
Chris@76
|
618 unset($_SESSION['pack_ftp']['original_perms'][$file]);
|
Chris@76
|
619 }
|
Chris@76
|
620
|
Chris@76
|
621 // Record the results!
|
Chris@76
|
622 $restore_files[] = array(
|
Chris@76
|
623 'path' => $file,
|
Chris@76
|
624 'old_perms_raw' => $perms,
|
Chris@76
|
625 'old_perms' => substr(sprintf('%o', $perms), -4),
|
Chris@76
|
626 'cur_perms' => substr(sprintf('%o', $file_permissions), -4),
|
Chris@76
|
627 'new_perms' => isset($new_permissions) ? substr(sprintf('%o', $new_permissions), -4) : '',
|
Chris@76
|
628 'result' => isset($result) ? $result : '',
|
Chris@76
|
629 'writable_message' => '<span style="color: ' . (@is_writable($file) ? 'green' : 'red') . '">' . (@is_writable($file) ? $txt['package_file_perms_writable'] : $txt['package_file_perms_not_writable']) . '</span>',
|
Chris@76
|
630 );
|
Chris@76
|
631 }
|
Chris@76
|
632
|
Chris@76
|
633 return $restore_files;
|
Chris@76
|
634 }
|
Chris@76
|
635
|
Chris@76
|
636 $listOptions = array(
|
Chris@76
|
637 'id' => 'restore_file_permissions',
|
Chris@76
|
638 'title' => $txt['package_restore_permissions'],
|
Chris@76
|
639 'get_items' => array(
|
Chris@76
|
640 'function' => 'list_restoreFiles',
|
Chris@76
|
641 'params' => array(
|
Chris@76
|
642 !empty($_POST['restore_perms']),
|
Chris@76
|
643 ),
|
Chris@76
|
644 ),
|
Chris@76
|
645 'columns' => array(
|
Chris@76
|
646 'path' => array(
|
Chris@76
|
647 'header' => array(
|
Chris@76
|
648 'value' => $txt['package_restore_permissions_filename'],
|
Chris@76
|
649 ),
|
Chris@76
|
650 'data' => array(
|
Chris@76
|
651 'db' => 'path',
|
Chris@76
|
652 'class' => 'smalltext',
|
Chris@76
|
653 ),
|
Chris@76
|
654 ),
|
Chris@76
|
655 'old_perms' => array(
|
Chris@76
|
656 'header' => array(
|
Chris@76
|
657 'value' => $txt['package_restore_permissions_orig_status'],
|
Chris@76
|
658 ),
|
Chris@76
|
659 'data' => array(
|
Chris@76
|
660 'db' => 'old_perms',
|
Chris@76
|
661 'class' => 'smalltext',
|
Chris@76
|
662 ),
|
Chris@76
|
663 ),
|
Chris@76
|
664 'cur_perms' => array(
|
Chris@76
|
665 'header' => array(
|
Chris@76
|
666 'value' => $txt['package_restore_permissions_cur_status'],
|
Chris@76
|
667 ),
|
Chris@76
|
668 'data' => array(
|
Chris@76
|
669 'function' => create_function('$rowData', '
|
Chris@76
|
670 global $txt;
|
Chris@76
|
671
|
Chris@76
|
672 $formatTxt = $rowData[\'result\'] == \'\' || $rowData[\'result\'] == \'skipped\' ? $txt[\'package_restore_permissions_pre_change\'] : $txt[\'package_restore_permissions_post_change\'];
|
Chris@76
|
673 return sprintf($formatTxt, $rowData[\'cur_perms\'], $rowData[\'new_perms\'], $rowData[\'writable_message\']);
|
Chris@76
|
674 '),
|
Chris@76
|
675 'class' => 'smalltext',
|
Chris@76
|
676 ),
|
Chris@76
|
677 ),
|
Chris@76
|
678 'check' => array(
|
Chris@76
|
679 'header' => array(
|
Chris@76
|
680 'value' => '<input type="checkbox" onclick="invertAll(this, this.form);" class="input_check" />',
|
Chris@76
|
681 ),
|
Chris@76
|
682 'data' => array(
|
Chris@76
|
683 'sprintf' => array(
|
Chris@76
|
684 'format' => '<input type="checkbox" name="restore_files[]" value="%1$s" class="input_check" />',
|
Chris@76
|
685 'params' => array(
|
Chris@76
|
686 'path' => false,
|
Chris@76
|
687 ),
|
Chris@76
|
688 ),
|
Chris@76
|
689 'style' => 'text-align: center',
|
Chris@76
|
690 ),
|
Chris@76
|
691 ),
|
Chris@76
|
692 'result' => array(
|
Chris@76
|
693 'header' => array(
|
Chris@76
|
694 'value' => $txt['package_restore_permissions_result'],
|
Chris@76
|
695 ),
|
Chris@76
|
696 'data' => array(
|
Chris@76
|
697 'function' => create_function('$rowData', '
|
Chris@76
|
698 global $txt;
|
Chris@76
|
699
|
Chris@76
|
700 return $txt[\'package_restore_permissions_action_\' . $rowData[\'result\']];
|
Chris@76
|
701 '),
|
Chris@76
|
702 'class' => 'smalltext',
|
Chris@76
|
703 ),
|
Chris@76
|
704 ),
|
Chris@76
|
705 ),
|
Chris@76
|
706 'form' => array(
|
Chris@76
|
707 'href' => !empty($chmodOptions['destination_url']) ? $chmodOptions['destination_url'] : $scripturl . '?action=admin;area=packages;sa=perms;restore;' . $context['session_var'] . '=' . $context['session_id'],
|
Chris@76
|
708 ),
|
Chris@76
|
709 'additional_rows' => array(
|
Chris@76
|
710 array(
|
Chris@76
|
711 'position' => 'below_table_data',
|
Chris@76
|
712 'value' => '<input type="submit" name="restore_perms" value="' . $txt['package_restore_permissions_restore'] . '" class="button_submit" />',
|
Chris@76
|
713 'class' => 'titlebg',
|
Chris@76
|
714 'style' => 'text-align: right;',
|
Chris@76
|
715 ),
|
Chris@76
|
716 array(
|
Chris@76
|
717 'position' => 'after_title',
|
Chris@76
|
718 'value' => '<span class="smalltext">' . $txt['package_restore_permissions_desc'] . '</span>',
|
Chris@76
|
719 'class' => 'windowbg2',
|
Chris@76
|
720 ),
|
Chris@76
|
721 ),
|
Chris@76
|
722 );
|
Chris@76
|
723
|
Chris@76
|
724 // Work out what columns and the like to show.
|
Chris@76
|
725 if (!empty($_POST['restore_perms']))
|
Chris@76
|
726 {
|
Chris@76
|
727 $listOptions['additional_rows'][1]['value'] = sprintf($txt['package_restore_permissions_action_done'], $scripturl . '?action=admin;area=packages;sa=perms;' . $context['session_var'] . '=' . $context['session_id']);
|
Chris@76
|
728 unset($listOptions['columns']['check'], $listOptions['form'], $listOptions['additional_rows'][0]);
|
Chris@76
|
729
|
Chris@76
|
730 $context['sub_template'] = 'show_list';
|
Chris@76
|
731 $context['default_list'] = 'restore_file_permissions';
|
Chris@76
|
732 }
|
Chris@76
|
733 else
|
Chris@76
|
734 {
|
Chris@76
|
735 unset($listOptions['columns']['result']);
|
Chris@76
|
736 }
|
Chris@76
|
737
|
Chris@76
|
738 // Create the list for display.
|
Chris@76
|
739 require_once($sourcedir . '/Subs-List.php');
|
Chris@76
|
740 createList($listOptions);
|
Chris@76
|
741
|
Chris@76
|
742 // If we just restored permissions then whereever we are, we are now done and dusted.
|
Chris@76
|
743 if (!empty($_POST['restore_perms']))
|
Chris@76
|
744 obExit();
|
Chris@76
|
745 }
|
Chris@76
|
746 // Otherwise, it's entirely irrelevant?
|
Chris@76
|
747 elseif ($restore_write_status)
|
Chris@76
|
748 return true;
|
Chris@76
|
749
|
Chris@76
|
750 // This is where we report what we got up to.
|
Chris@76
|
751 $return_data = array(
|
Chris@76
|
752 'files' => array(
|
Chris@76
|
753 'writable' => array(),
|
Chris@76
|
754 'notwritable' => array(),
|
Chris@76
|
755 ),
|
Chris@76
|
756 );
|
Chris@76
|
757
|
Chris@76
|
758 // If we have some FTP information already, then let's assume it was required and try to get ourselves connected.
|
Chris@76
|
759 if (!empty($_SESSION['pack_ftp']['connected']))
|
Chris@76
|
760 {
|
Chris@76
|
761 // Load the file containing the ftp_connection class.
|
Chris@76
|
762 loadClassFile('Class-Package.php');
|
Chris@76
|
763
|
Chris@76
|
764 $package_ftp = new ftp_connection($_SESSION['pack_ftp']['server'], $_SESSION['pack_ftp']['port'], $_SESSION['pack_ftp']['username'], package_crypt($_SESSION['pack_ftp']['password']));
|
Chris@76
|
765 }
|
Chris@76
|
766
|
Chris@76
|
767 // Just got a submission did we?
|
Chris@76
|
768 if (empty($package_ftp) && isset($_POST['ftp_username']))
|
Chris@76
|
769 {
|
Chris@76
|
770 loadClassFile('Class-Package.php');
|
Chris@76
|
771 $ftp = new ftp_connection($_POST['ftp_server'], $_POST['ftp_port'], $_POST['ftp_username'], $_POST['ftp_password']);
|
Chris@76
|
772
|
Chris@76
|
773 // We're connected, jolly good!
|
Chris@76
|
774 if ($ftp->error === false)
|
Chris@76
|
775 {
|
Chris@76
|
776 // Common mistake, so let's try to remedy it...
|
Chris@76
|
777 if (!$ftp->chdir($_POST['ftp_path']))
|
Chris@76
|
778 {
|
Chris@76
|
779 $ftp_error = $ftp->last_message;
|
Chris@76
|
780 $ftp->chdir(preg_replace('~^/home[2]?/[^/]+?~', '', $_POST['ftp_path']));
|
Chris@76
|
781 }
|
Chris@76
|
782
|
Chris@76
|
783 if (!in_array($_POST['ftp_path'], array('', '/')))
|
Chris@76
|
784 {
|
Chris@76
|
785 $ftp_root = strtr($boarddir, array($_POST['ftp_path'] => ''));
|
Chris@76
|
786 if (substr($ftp_root, -1) == '/' && ($_POST['ftp_path'] == '' || substr($_POST['ftp_path'], 0, 1) == '/'))
|
Chris@76
|
787 $ftp_root = substr($ftp_root, 0, -1);
|
Chris@76
|
788 }
|
Chris@76
|
789 else
|
Chris@76
|
790 $ftp_root = $boarddir;
|
Chris@76
|
791
|
Chris@76
|
792 $_SESSION['pack_ftp'] = array(
|
Chris@76
|
793 'server' => $_POST['ftp_server'],
|
Chris@76
|
794 'port' => $_POST['ftp_port'],
|
Chris@76
|
795 'username' => $_POST['ftp_username'],
|
Chris@76
|
796 'password' => package_crypt($_POST['ftp_password']),
|
Chris@76
|
797 'path' => $_POST['ftp_path'],
|
Chris@76
|
798 'root' => $ftp_root,
|
Chris@76
|
799 'connected' => true,
|
Chris@76
|
800 );
|
Chris@76
|
801
|
Chris@76
|
802 if (!isset($modSettings['package_path']) || $modSettings['package_path'] != $_POST['ftp_path'])
|
Chris@76
|
803 updateSettings(array('package_path' => $_POST['ftp_path']));
|
Chris@76
|
804
|
Chris@76
|
805 // This is now the primary connection.
|
Chris@76
|
806 $package_ftp = $ftp;
|
Chris@76
|
807 }
|
Chris@76
|
808 }
|
Chris@76
|
809
|
Chris@76
|
810 // Now try to simply make the files writable, with whatever we might have.
|
Chris@76
|
811 if (!empty($chmodFiles))
|
Chris@76
|
812 {
|
Chris@76
|
813 foreach ($chmodFiles as $k => $file)
|
Chris@76
|
814 {
|
Chris@76
|
815 // Sometimes this can somehow happen maybe?
|
Chris@76
|
816 if (empty($file))
|
Chris@76
|
817 unset($chmodFiles[$k]);
|
Chris@76
|
818 // Already writable?
|
Chris@76
|
819 elseif (@is_writable($file))
|
Chris@76
|
820 $return_data['files']['writable'][] = $file;
|
Chris@76
|
821 else
|
Chris@76
|
822 {
|
Chris@76
|
823 // Now try to change that.
|
Chris@76
|
824 $return_data['files'][package_chmod($file, 'writable', true) ? 'writable' : 'notwritable'][] = $file;
|
Chris@76
|
825 }
|
Chris@76
|
826 }
|
Chris@76
|
827 }
|
Chris@76
|
828
|
Chris@76
|
829 // Have we still got nasty files which ain't writable? Dear me we need more FTP good sir.
|
Chris@76
|
830 if (empty($package_ftp) && (!empty($return_data['files']['notwritable']) || !empty($chmodOptions['force_find_error'])))
|
Chris@76
|
831 {
|
Chris@76
|
832 if (!isset($ftp) || $ftp->error !== false)
|
Chris@76
|
833 {
|
Chris@76
|
834 if (!isset($ftp))
|
Chris@76
|
835 {
|
Chris@76
|
836 loadClassFile('Class-Package.php');
|
Chris@76
|
837 $ftp = new ftp_connection(null);
|
Chris@76
|
838 }
|
Chris@76
|
839 elseif ($ftp->error !== false && !isset($ftp_error))
|
Chris@76
|
840 $ftp_error = $ftp->last_message === null ? '' : $ftp->last_message;
|
Chris@76
|
841
|
Chris@76
|
842 list ($username, $detect_path, $found_path) = $ftp->detect_path($boarddir);
|
Chris@76
|
843
|
Chris@76
|
844 if ($found_path)
|
Chris@76
|
845 $_POST['ftp_path'] = $detect_path;
|
Chris@76
|
846 elseif (!isset($_POST['ftp_path']))
|
Chris@76
|
847 $_POST['ftp_path'] = isset($modSettings['package_path']) ? $modSettings['package_path'] : $detect_path;
|
Chris@76
|
848
|
Chris@76
|
849 if (!isset($_POST['ftp_username']))
|
Chris@76
|
850 $_POST['ftp_username'] = $username;
|
Chris@76
|
851 }
|
Chris@76
|
852
|
Chris@76
|
853 $context['package_ftp'] = array(
|
Chris@76
|
854 'server' => isset($_POST['ftp_server']) ? $_POST['ftp_server'] : (isset($modSettings['package_server']) ? $modSettings['package_server'] : 'localhost'),
|
Chris@76
|
855 'port' => isset($_POST['ftp_port']) ? $_POST['ftp_port'] : (isset($modSettings['package_port']) ? $modSettings['package_port'] : '21'),
|
Chris@76
|
856 'username' => isset($_POST['ftp_username']) ? $_POST['ftp_username'] : (isset($modSettings['package_username']) ? $modSettings['package_username'] : ''),
|
Chris@76
|
857 'path' => $_POST['ftp_path'],
|
Chris@76
|
858 'error' => empty($ftp_error) ? null : $ftp_error,
|
Chris@76
|
859 'destination' => !empty($chmodOptions['destination_url']) ? $chmodOptions['destination_url'] : '',
|
Chris@76
|
860 );
|
Chris@76
|
861
|
Chris@76
|
862 // Which files failed?
|
Chris@76
|
863 if (!isset($context['notwritable_files']))
|
Chris@76
|
864 $context['notwritable_files'] = array();
|
Chris@76
|
865 $context['notwritable_files'] = array_merge($context['notwritable_files'], $return_data['files']['notwritable']);
|
Chris@76
|
866
|
Chris@76
|
867 // Sent here to die?
|
Chris@76
|
868 if (!empty($chmodOptions['crash_on_error']))
|
Chris@76
|
869 {
|
Chris@76
|
870 $context['page_title'] = $txt['package_ftp_necessary'];
|
Chris@76
|
871 $context['sub_template'] = 'ftp_required';
|
Chris@76
|
872 obExit();
|
Chris@76
|
873 }
|
Chris@76
|
874 }
|
Chris@76
|
875
|
Chris@76
|
876 return $return_data;
|
Chris@76
|
877 }
|
Chris@76
|
878
|
Chris@76
|
879 function packageRequireFTP($destination_url, $files = null, $return = false)
|
Chris@76
|
880 {
|
Chris@76
|
881 global $context, $modSettings, $package_ftp, $boarddir, $txt;
|
Chris@76
|
882
|
Chris@76
|
883 // Try to make them writable the manual way.
|
Chris@76
|
884 if ($files !== null)
|
Chris@76
|
885 {
|
Chris@76
|
886 foreach ($files as $k => $file)
|
Chris@76
|
887 {
|
Chris@76
|
888 // If this file doesn't exist, then we actually want to look at the directory, no?
|
Chris@76
|
889 if (!file_exists($file))
|
Chris@76
|
890 $file = dirname($file);
|
Chris@76
|
891
|
Chris@76
|
892 // This looks odd, but it's an attempt to work around PHP suExec.
|
Chris@76
|
893 if (!@is_writable($file))
|
Chris@76
|
894 @chmod($file, 0755);
|
Chris@76
|
895 if (!@is_writable($file))
|
Chris@76
|
896 @chmod($file, 0777);
|
Chris@76
|
897 if (!@is_writable(dirname($file)))
|
Chris@76
|
898 @chmod($file, 0755);
|
Chris@76
|
899 if (!@is_writable(dirname($file)))
|
Chris@76
|
900 @chmod($file, 0777);
|
Chris@76
|
901
|
Chris@76
|
902 $fp = is_dir($file) ? @opendir($file) : @fopen($file, 'rb');
|
Chris@76
|
903 if (@is_writable($file) && $fp)
|
Chris@76
|
904 {
|
Chris@76
|
905 unset($files[$k]);
|
Chris@76
|
906 if (!is_dir($file))
|
Chris@76
|
907 fclose($fp);
|
Chris@76
|
908 else
|
Chris@76
|
909 closedir($fp);
|
Chris@76
|
910 }
|
Chris@76
|
911 }
|
Chris@76
|
912
|
Chris@76
|
913 // No FTP required!
|
Chris@76
|
914 if (empty($files))
|
Chris@76
|
915 return array();
|
Chris@76
|
916 }
|
Chris@76
|
917
|
Chris@76
|
918 // They've opted to not use FTP, and try anyway.
|
Chris@76
|
919 if (isset($_SESSION['pack_ftp']) && $_SESSION['pack_ftp'] == false)
|
Chris@76
|
920 {
|
Chris@76
|
921 if ($files === null)
|
Chris@76
|
922 return array();
|
Chris@76
|
923
|
Chris@76
|
924 foreach ($files as $k => $file)
|
Chris@76
|
925 {
|
Chris@76
|
926 // This looks odd, but it's an attempt to work around PHP suExec.
|
Chris@76
|
927 if (!file_exists($file))
|
Chris@76
|
928 {
|
Chris@76
|
929 mktree(dirname($file), 0755);
|
Chris@76
|
930 @touch($file);
|
Chris@76
|
931 @chmod($file, 0755);
|
Chris@76
|
932 }
|
Chris@76
|
933
|
Chris@76
|
934 if (!@is_writable($file))
|
Chris@76
|
935 @chmod($file, 0777);
|
Chris@76
|
936 if (!@is_writable(dirname($file)))
|
Chris@76
|
937 @chmod(dirname($file), 0777);
|
Chris@76
|
938
|
Chris@76
|
939 if (@is_writable($file))
|
Chris@76
|
940 unset($files[$k]);
|
Chris@76
|
941 }
|
Chris@76
|
942
|
Chris@76
|
943 return $files;
|
Chris@76
|
944 }
|
Chris@76
|
945 elseif (isset($_SESSION['pack_ftp']))
|
Chris@76
|
946 {
|
Chris@76
|
947 // Load the file containing the ftp_connection class.
|
Chris@76
|
948 loadClassFile('Class-Package.php');
|
Chris@76
|
949
|
Chris@76
|
950 $package_ftp = new ftp_connection($_SESSION['pack_ftp']['server'], $_SESSION['pack_ftp']['port'], $_SESSION['pack_ftp']['username'], package_crypt($_SESSION['pack_ftp']['password']));
|
Chris@76
|
951
|
Chris@76
|
952 if ($files === null)
|
Chris@76
|
953 return array();
|
Chris@76
|
954
|
Chris@76
|
955 foreach ($files as $k => $file)
|
Chris@76
|
956 {
|
Chris@76
|
957 $ftp_file = strtr($file, array($_SESSION['pack_ftp']['root'] => ''));
|
Chris@76
|
958
|
Chris@76
|
959 // This looks odd, but it's an attempt to work around PHP suExec.
|
Chris@76
|
960 if (!file_exists($file))
|
Chris@76
|
961 {
|
Chris@76
|
962 mktree(dirname($file), 0755);
|
Chris@76
|
963 $package_ftp->create_file($ftp_file);
|
Chris@76
|
964 $package_ftp->chmod($ftp_file, 0755);
|
Chris@76
|
965 }
|
Chris@76
|
966
|
Chris@76
|
967 if (!@is_writable($file))
|
Chris@76
|
968 $package_ftp->chmod($ftp_file, 0777);
|
Chris@76
|
969 if (!@is_writable(dirname($file)))
|
Chris@76
|
970 $package_ftp->chmod(dirname($ftp_file), 0777);
|
Chris@76
|
971
|
Chris@76
|
972 if (@is_writable($file))
|
Chris@76
|
973 unset($files[$k]);
|
Chris@76
|
974 }
|
Chris@76
|
975
|
Chris@76
|
976 return $files;
|
Chris@76
|
977 }
|
Chris@76
|
978
|
Chris@76
|
979 if (isset($_POST['ftp_none']))
|
Chris@76
|
980 {
|
Chris@76
|
981 $_SESSION['pack_ftp'] = false;
|
Chris@76
|
982
|
Chris@76
|
983 $files = packageRequireFTP($destination_url, $files, $return);
|
Chris@76
|
984 return $files;
|
Chris@76
|
985 }
|
Chris@76
|
986 elseif (isset($_POST['ftp_username']))
|
Chris@76
|
987 {
|
Chris@76
|
988 loadClassFile('Class-Package.php');
|
Chris@76
|
989 $ftp = new ftp_connection($_POST['ftp_server'], $_POST['ftp_port'], $_POST['ftp_username'], $_POST['ftp_password']);
|
Chris@76
|
990
|
Chris@76
|
991 if ($ftp->error === false)
|
Chris@76
|
992 {
|
Chris@76
|
993 // Common mistake, so let's try to remedy it...
|
Chris@76
|
994 if (!$ftp->chdir($_POST['ftp_path']))
|
Chris@76
|
995 {
|
Chris@76
|
996 $ftp_error = $ftp->last_message;
|
Chris@76
|
997 $ftp->chdir(preg_replace('~^/home[2]?/[^/]+?~', '', $_POST['ftp_path']));
|
Chris@76
|
998 }
|
Chris@76
|
999 }
|
Chris@76
|
1000 }
|
Chris@76
|
1001
|
Chris@76
|
1002 if (!isset($ftp) || $ftp->error !== false)
|
Chris@76
|
1003 {
|
Chris@76
|
1004 if (!isset($ftp))
|
Chris@76
|
1005 {
|
Chris@76
|
1006 loadClassFile('Class-Package.php');
|
Chris@76
|
1007 $ftp = new ftp_connection(null);
|
Chris@76
|
1008 }
|
Chris@76
|
1009 elseif ($ftp->error !== false && !isset($ftp_error))
|
Chris@76
|
1010 $ftp_error = $ftp->last_message === null ? '' : $ftp->last_message;
|
Chris@76
|
1011
|
Chris@76
|
1012 list ($username, $detect_path, $found_path) = $ftp->detect_path($boarddir);
|
Chris@76
|
1013
|
Chris@76
|
1014 if ($found_path)
|
Chris@76
|
1015 $_POST['ftp_path'] = $detect_path;
|
Chris@76
|
1016 elseif (!isset($_POST['ftp_path']))
|
Chris@76
|
1017 $_POST['ftp_path'] = isset($modSettings['package_path']) ? $modSettings['package_path'] : $detect_path;
|
Chris@76
|
1018
|
Chris@76
|
1019 if (!isset($_POST['ftp_username']))
|
Chris@76
|
1020 $_POST['ftp_username'] = $username;
|
Chris@76
|
1021
|
Chris@76
|
1022 $context['package_ftp'] = array(
|
Chris@76
|
1023 'server' => isset($_POST['ftp_server']) ? $_POST['ftp_server'] : (isset($modSettings['package_server']) ? $modSettings['package_server'] : 'localhost'),
|
Chris@76
|
1024 'port' => isset($_POST['ftp_port']) ? $_POST['ftp_port'] : (isset($modSettings['package_port']) ? $modSettings['package_port'] : '21'),
|
Chris@76
|
1025 'username' => isset($_POST['ftp_username']) ? $_POST['ftp_username'] : (isset($modSettings['package_username']) ? $modSettings['package_username'] : ''),
|
Chris@76
|
1026 'path' => $_POST['ftp_path'],
|
Chris@76
|
1027 'error' => empty($ftp_error) ? null : $ftp_error,
|
Chris@76
|
1028 'destination' => $destination_url,
|
Chris@76
|
1029 );
|
Chris@76
|
1030
|
Chris@76
|
1031 // If we're returning dump out here.
|
Chris@76
|
1032 if ($return)
|
Chris@76
|
1033 return $files;
|
Chris@76
|
1034
|
Chris@76
|
1035 $context['page_title'] = $txt['package_ftp_necessary'];
|
Chris@76
|
1036 $context['sub_template'] = 'ftp_required';
|
Chris@76
|
1037 obExit();
|
Chris@76
|
1038 }
|
Chris@76
|
1039 else
|
Chris@76
|
1040 {
|
Chris@76
|
1041 if (!in_array($_POST['ftp_path'], array('', '/')))
|
Chris@76
|
1042 {
|
Chris@76
|
1043 $ftp_root = strtr($boarddir, array($_POST['ftp_path'] => ''));
|
Chris@76
|
1044 if (substr($ftp_root, -1) == '/' && ($_POST['ftp_path'] == '' || substr($_POST['ftp_path'], 0, 1) == '/'))
|
Chris@76
|
1045 $ftp_root = substr($ftp_root, 0, -1);
|
Chris@76
|
1046 }
|
Chris@76
|
1047 else
|
Chris@76
|
1048 $ftp_root = $boarddir;
|
Chris@76
|
1049
|
Chris@76
|
1050 $_SESSION['pack_ftp'] = array(
|
Chris@76
|
1051 'server' => $_POST['ftp_server'],
|
Chris@76
|
1052 'port' => $_POST['ftp_port'],
|
Chris@76
|
1053 'username' => $_POST['ftp_username'],
|
Chris@76
|
1054 'password' => package_crypt($_POST['ftp_password']),
|
Chris@76
|
1055 'path' => $_POST['ftp_path'],
|
Chris@76
|
1056 'root' => $ftp_root,
|
Chris@76
|
1057 );
|
Chris@76
|
1058
|
Chris@76
|
1059 if (!isset($modSettings['package_path']) || $modSettings['package_path'] != $_POST['ftp_path'])
|
Chris@76
|
1060 updateSettings(array('package_path' => $_POST['ftp_path']));
|
Chris@76
|
1061
|
Chris@76
|
1062 $files = packageRequireFTP($destination_url, $files, $return);
|
Chris@76
|
1063 }
|
Chris@76
|
1064
|
Chris@76
|
1065 return $files;
|
Chris@76
|
1066 }
|
Chris@76
|
1067
|
Chris@76
|
1068 // Parses a package-info.xml file - method can be 'install', 'upgrade', or 'uninstall'.
|
Chris@76
|
1069 function parsePackageInfo(&$packageXML, $testing_only = true, $method = 'install', $previous_version = '')
|
Chris@76
|
1070 {
|
Chris@76
|
1071 global $boarddir, $forum_version, $context, $temp_path, $language;
|
Chris@76
|
1072
|
Chris@76
|
1073 // Mayday! That action doesn't exist!!
|
Chris@76
|
1074 if (empty($packageXML) || !$packageXML->exists($method))
|
Chris@76
|
1075 return array();
|
Chris@76
|
1076
|
Chris@76
|
1077 // We haven't found the package script yet...
|
Chris@76
|
1078 $script = false;
|
Chris@76
|
1079 $the_version = strtr($forum_version, array('SMF ' => ''));
|
Chris@76
|
1080
|
Chris@76
|
1081 // Emulation support...
|
Chris@76
|
1082 if (!empty($_SESSION['version_emulate']))
|
Chris@76
|
1083 $the_version = $_SESSION['version_emulate'];
|
Chris@76
|
1084
|
Chris@76
|
1085 // Get all the versions of this method and find the right one.
|
Chris@76
|
1086 $these_methods = $packageXML->set($method);
|
Chris@76
|
1087 foreach ($these_methods as $this_method)
|
Chris@76
|
1088 {
|
Chris@76
|
1089 // They specified certain versions this part is for.
|
Chris@76
|
1090 if ($this_method->exists('@for'))
|
Chris@76
|
1091 {
|
Chris@76
|
1092 // Don't keep going if this won't work for this version of SMF.
|
Chris@76
|
1093 if (!matchPackageVersion($the_version, $this_method->fetch('@for')))
|
Chris@76
|
1094 continue;
|
Chris@76
|
1095 }
|
Chris@76
|
1096
|
Chris@76
|
1097 // Upgrades may go from a certain old version of the mod.
|
Chris@76
|
1098 if ($method == 'upgrade' && $this_method->exists('@from'))
|
Chris@76
|
1099 {
|
Chris@76
|
1100 // Well, this is for the wrong old version...
|
Chris@76
|
1101 if (!matchPackageVersion($previous_version, $this_method->fetch('@from')))
|
Chris@76
|
1102 continue;
|
Chris@76
|
1103 }
|
Chris@76
|
1104
|
Chris@76
|
1105 // We've found it!
|
Chris@76
|
1106 $script = $this_method;
|
Chris@76
|
1107 break;
|
Chris@76
|
1108 }
|
Chris@76
|
1109
|
Chris@76
|
1110 // Bad news, a matching script wasn't found!
|
Chris@76
|
1111 if ($script === false)
|
Chris@76
|
1112 return array();
|
Chris@76
|
1113
|
Chris@76
|
1114 // Find all the actions in this method - in theory, these should only be allowed actions. (* means all.)
|
Chris@76
|
1115 $actions = $script->set('*');
|
Chris@76
|
1116 $return = array();
|
Chris@76
|
1117
|
Chris@76
|
1118 $temp_auto = 0;
|
Chris@76
|
1119 $temp_path = $boarddir . '/Packages/temp/' . (isset($context['base_path']) ? $context['base_path'] : '');
|
Chris@76
|
1120
|
Chris@76
|
1121 $context['readmes'] = array();
|
Chris@76
|
1122 // This is the testing phase... nothing shall be done yet.
|
Chris@76
|
1123 foreach ($actions as $action)
|
Chris@76
|
1124 {
|
Chris@76
|
1125 $actionType = $action->name();
|
Chris@76
|
1126
|
Chris@76
|
1127 if ($actionType == 'readme' || $actionType == 'code' || $actionType == 'database' || $actionType == 'modification' || $actionType == 'redirect')
|
Chris@76
|
1128 {
|
Chris@76
|
1129 // Allow for translated readme files.
|
Chris@76
|
1130 if ($actionType == 'readme')
|
Chris@76
|
1131 {
|
Chris@76
|
1132 if ($action->exists('@lang'))
|
Chris@76
|
1133 {
|
Chris@76
|
1134 // Auto-select a readme language based on either request variable or current language.
|
Chris@76
|
1135 if ((isset($_REQUEST['readme']) && $action->fetch('@lang') == $_REQUEST['readme']) || (!isset($_REQUEST['readme']) && $action->fetch('@lang') == $language))
|
Chris@76
|
1136 {
|
Chris@76
|
1137 // In case the user put the readme blocks in the wrong order.
|
Chris@76
|
1138 if (isset($context['readmes']['selected']) && $context['readmes']['selected'] == 'default')
|
Chris@76
|
1139 $context['readmes'][] = 'default';
|
Chris@76
|
1140
|
Chris@76
|
1141 $context['readmes']['selected'] = htmlspecialchars($action->fetch('@lang'));
|
Chris@76
|
1142 }
|
Chris@76
|
1143 else
|
Chris@76
|
1144 {
|
Chris@76
|
1145 // We don't want this readme now, but we'll allow the user to select to read it.
|
Chris@76
|
1146 $context['readmes'][] = htmlspecialchars($action->fetch('@lang'));
|
Chris@76
|
1147 continue;
|
Chris@76
|
1148 }
|
Chris@76
|
1149 }
|
Chris@76
|
1150 // Fallback readme. Without lang parameter.
|
Chris@76
|
1151 else
|
Chris@76
|
1152 {
|
Chris@76
|
1153
|
Chris@76
|
1154 // Already selected a readme.
|
Chris@76
|
1155 if (isset($context['readmes']['selected']))
|
Chris@76
|
1156 {
|
Chris@76
|
1157 $context['readmes'][] = 'default';
|
Chris@76
|
1158 continue;
|
Chris@76
|
1159 }
|
Chris@76
|
1160 else
|
Chris@76
|
1161 $context['readmes']['selected'] = 'default';
|
Chris@76
|
1162 }
|
Chris@76
|
1163 }
|
Chris@76
|
1164
|
Chris@76
|
1165 // !!! TODO: Make sure the file actually exists? Might not work when testing?
|
Chris@76
|
1166 if ($action->exists('@type') && $action->fetch('@type') == 'inline')
|
Chris@76
|
1167 {
|
Chris@76
|
1168 $filename = $temp_path . '$auto_' . $temp_auto++ . ($actionType == 'readme' || $actionType == 'redirect' ? '.txt' : ($actionType == 'code' || $actionType == 'database' ? '.php' : '.mod'));
|
Chris@76
|
1169 package_put_contents($filename, $action->fetch('.'));
|
Chris@76
|
1170 $filename = strtr($filename, array($temp_path => ''));
|
Chris@76
|
1171 }
|
Chris@76
|
1172 else
|
Chris@76
|
1173 $filename = $action->fetch('.');
|
Chris@76
|
1174
|
Chris@76
|
1175 $return[] = array(
|
Chris@76
|
1176 'type' => $actionType,
|
Chris@76
|
1177 'filename' => $filename,
|
Chris@76
|
1178 'description' => '',
|
Chris@76
|
1179 'reverse' => $action->exists('@reverse') && $action->fetch('@reverse') == 'true',
|
Chris@76
|
1180 'boardmod' => $action->exists('@format') && $action->fetch('@format') == 'boardmod',
|
Chris@76
|
1181 'redirect_url' => $action->exists('@url') ? $action->fetch('@url') : '',
|
Chris@76
|
1182 'redirect_timeout' => $action->exists('@timeout') ? (int) $action->fetch('@timeout') : '',
|
Chris@76
|
1183 'parse_bbc' => $action->exists('@parsebbc') && $action->fetch('@parsebbc') == 'true',
|
Chris@76
|
1184 'language' => ($actionType == 'readme' && $action->exists('@lang') && $action->fetch('@lang') == $language) ? $language : '',
|
Chris@76
|
1185 );
|
Chris@76
|
1186
|
Chris@76
|
1187 continue;
|
Chris@76
|
1188 }
|
Chris@76
|
1189 elseif ($actionType == 'error')
|
Chris@76
|
1190 {
|
Chris@76
|
1191 $return[] = array(
|
Chris@76
|
1192 'type' => 'error',
|
Chris@76
|
1193 );
|
Chris@76
|
1194 }
|
Chris@76
|
1195
|
Chris@76
|
1196 $this_action = &$return[];
|
Chris@76
|
1197 $this_action = array(
|
Chris@76
|
1198 'type' => $actionType,
|
Chris@76
|
1199 'filename' => $action->fetch('@name'),
|
Chris@76
|
1200 'description' => $action->fetch('.')
|
Chris@76
|
1201 );
|
Chris@76
|
1202
|
Chris@76
|
1203 // If there is a destination, make sure it makes sense.
|
Chris@76
|
1204 if (substr($actionType, 0, 6) != 'remove')
|
Chris@76
|
1205 {
|
Chris@76
|
1206 $this_action['unparsed_destination'] = $action->fetch('@destination');
|
Chris@76
|
1207 $this_action['destination'] = parse_path($action->fetch('@destination')) . '/' . basename($this_action['filename']);
|
Chris@76
|
1208 }
|
Chris@76
|
1209 else
|
Chris@76
|
1210 {
|
Chris@76
|
1211 $this_action['unparsed_filename'] = $this_action['filename'];
|
Chris@76
|
1212 $this_action['filename'] = parse_path($this_action['filename']);
|
Chris@76
|
1213 }
|
Chris@76
|
1214
|
Chris@76
|
1215 // If we're moving or requiring (copying) a file.
|
Chris@76
|
1216 if (substr($actionType, 0, 4) == 'move' || substr($actionType, 0, 7) == 'require')
|
Chris@76
|
1217 {
|
Chris@76
|
1218 if ($action->exists('@from'))
|
Chris@76
|
1219 $this_action['source'] = parse_path($action->fetch('@from'));
|
Chris@76
|
1220 else
|
Chris@76
|
1221 $this_action['source'] = $temp_path . $this_action['filename'];
|
Chris@76
|
1222 }
|
Chris@76
|
1223
|
Chris@76
|
1224 // Check if these things can be done. (chmod's etc.)
|
Chris@76
|
1225 if ($actionType == 'create-dir')
|
Chris@76
|
1226 {
|
Chris@76
|
1227 if (!mktree($this_action['destination'], false))
|
Chris@76
|
1228 {
|
Chris@76
|
1229 $temp = $this_action['destination'];
|
Chris@76
|
1230 while (!file_exists($temp) && strlen($temp) > 1)
|
Chris@76
|
1231 $temp = dirname($temp);
|
Chris@76
|
1232
|
Chris@76
|
1233 $return[] = array(
|
Chris@76
|
1234 'type' => 'chmod',
|
Chris@76
|
1235 'filename' => $temp
|
Chris@76
|
1236 );
|
Chris@76
|
1237 }
|
Chris@76
|
1238 }
|
Chris@76
|
1239 elseif ($actionType == 'create-file')
|
Chris@76
|
1240 {
|
Chris@76
|
1241 if (!mktree(dirname($this_action['destination']), false))
|
Chris@76
|
1242 {
|
Chris@76
|
1243 $temp = dirname($this_action['destination']);
|
Chris@76
|
1244 while (!file_exists($temp) && strlen($temp) > 1)
|
Chris@76
|
1245 $temp = dirname($temp);
|
Chris@76
|
1246
|
Chris@76
|
1247 $return[] = array(
|
Chris@76
|
1248 'type' => 'chmod',
|
Chris@76
|
1249 'filename' => $temp
|
Chris@76
|
1250 );
|
Chris@76
|
1251 }
|
Chris@76
|
1252
|
Chris@76
|
1253 if (!is_writable($this_action['destination']) && (file_exists($this_action['destination']) || !is_writable(dirname($this_action['destination']))))
|
Chris@76
|
1254 $return[] = array(
|
Chris@76
|
1255 'type' => 'chmod',
|
Chris@76
|
1256 'filename' => $this_action['destination']
|
Chris@76
|
1257 );
|
Chris@76
|
1258 }
|
Chris@76
|
1259 elseif ($actionType == 'require-dir')
|
Chris@76
|
1260 {
|
Chris@76
|
1261 if (!mktree($this_action['destination'], false))
|
Chris@76
|
1262 {
|
Chris@76
|
1263 $temp = $this_action['destination'];
|
Chris@76
|
1264 while (!file_exists($temp) && strlen($temp) > 1)
|
Chris@76
|
1265 $temp = dirname($temp);
|
Chris@76
|
1266
|
Chris@76
|
1267 $return[] = array(
|
Chris@76
|
1268 'type' => 'chmod',
|
Chris@76
|
1269 'filename' => $temp
|
Chris@76
|
1270 );
|
Chris@76
|
1271 }
|
Chris@76
|
1272 }
|
Chris@76
|
1273 elseif ($actionType == 'require-file')
|
Chris@76
|
1274 {
|
Chris@76
|
1275 if ($action->exists('@theme'))
|
Chris@76
|
1276 $this_action['theme_action'] = $action->fetch('@theme');
|
Chris@76
|
1277
|
Chris@76
|
1278 if (!mktree(dirname($this_action['destination']), false))
|
Chris@76
|
1279 {
|
Chris@76
|
1280 $temp = dirname($this_action['destination']);
|
Chris@76
|
1281 while (!file_exists($temp) && strlen($temp) > 1)
|
Chris@76
|
1282 $temp = dirname($temp);
|
Chris@76
|
1283
|
Chris@76
|
1284 $return[] = array(
|
Chris@76
|
1285 'type' => 'chmod',
|
Chris@76
|
1286 'filename' => $temp
|
Chris@76
|
1287 );
|
Chris@76
|
1288 }
|
Chris@76
|
1289
|
Chris@76
|
1290 if (!is_writable($this_action['destination']) && (file_exists($this_action['destination']) || !is_writable(dirname($this_action['destination']))))
|
Chris@76
|
1291 $return[] = array(
|
Chris@76
|
1292 'type' => 'chmod',
|
Chris@76
|
1293 'filename' => $this_action['destination']
|
Chris@76
|
1294 );
|
Chris@76
|
1295 }
|
Chris@76
|
1296 elseif ($actionType == 'move-dir' || $actionType == 'move-file')
|
Chris@76
|
1297 {
|
Chris@76
|
1298 if (!mktree(dirname($this_action['destination']), false))
|
Chris@76
|
1299 {
|
Chris@76
|
1300 $temp = dirname($this_action['destination']);
|
Chris@76
|
1301 while (!file_exists($temp) && strlen($temp) > 1)
|
Chris@76
|
1302 $temp = dirname($temp);
|
Chris@76
|
1303
|
Chris@76
|
1304 $return[] = array(
|
Chris@76
|
1305 'type' => 'chmod',
|
Chris@76
|
1306 'filename' => $temp
|
Chris@76
|
1307 );
|
Chris@76
|
1308 }
|
Chris@76
|
1309
|
Chris@76
|
1310 if (!is_writable($this_action['destination']) && (file_exists($this_action['destination']) || !is_writable(dirname($this_action['destination']))))
|
Chris@76
|
1311 $return[] = array(
|
Chris@76
|
1312 'type' => 'chmod',
|
Chris@76
|
1313 'filename' => $this_action['destination']
|
Chris@76
|
1314 );
|
Chris@76
|
1315 }
|
Chris@76
|
1316 elseif ($actionType == 'remove-dir')
|
Chris@76
|
1317 {
|
Chris@76
|
1318 if (!is_writable($this_action['filename']) && file_exists($this_action['destination']))
|
Chris@76
|
1319 $return[] = array(
|
Chris@76
|
1320 'type' => 'chmod',
|
Chris@76
|
1321 'filename' => $this_action['filename']
|
Chris@76
|
1322 );
|
Chris@76
|
1323 }
|
Chris@76
|
1324 elseif ($actionType == 'remove-file')
|
Chris@76
|
1325 {
|
Chris@76
|
1326 if (!is_writable($this_action['filename']) && file_exists($this_action['filename']))
|
Chris@76
|
1327 $return[] = array(
|
Chris@76
|
1328 'type' => 'chmod',
|
Chris@76
|
1329 'filename' => $this_action['filename']
|
Chris@76
|
1330 );
|
Chris@76
|
1331 }
|
Chris@76
|
1332 }
|
Chris@76
|
1333
|
Chris@76
|
1334 // Only testing - just return a list of things to be done.
|
Chris@76
|
1335 if ($testing_only)
|
Chris@76
|
1336 return $return;
|
Chris@76
|
1337
|
Chris@76
|
1338 umask(0);
|
Chris@76
|
1339
|
Chris@76
|
1340 $failure = false;
|
Chris@76
|
1341 $not_done = array(array('type' => '!'));
|
Chris@76
|
1342 foreach ($return as $action)
|
Chris@76
|
1343 {
|
Chris@76
|
1344 if ($action['type'] == 'modification' || $action['type'] == 'code' || $action['type'] == 'database' || $action['type'] == 'redirect')
|
Chris@76
|
1345 $not_done[] = $action;
|
Chris@76
|
1346
|
Chris@76
|
1347 if ($action['type'] == 'create-dir')
|
Chris@76
|
1348 {
|
Chris@76
|
1349 if (!mktree($action['destination'], 0755) || !is_writable($action['destination']))
|
Chris@76
|
1350 $failure |= !mktree($action['destination'], 0777);
|
Chris@76
|
1351 }
|
Chris@76
|
1352 elseif ($action['type'] == 'create-file')
|
Chris@76
|
1353 {
|
Chris@76
|
1354 if (!mktree(dirname($action['destination']), 0755) || !is_writable(dirname($action['destination'])))
|
Chris@76
|
1355 $failure |= !mktree(dirname($action['destination']), 0777);
|
Chris@76
|
1356
|
Chris@76
|
1357 // Create an empty file.
|
Chris@76
|
1358 package_put_contents($action['destination'], package_get_contents($action['source']), $testing_only);
|
Chris@76
|
1359
|
Chris@76
|
1360 if (!file_exists($action['destination']))
|
Chris@76
|
1361 $failure = true;
|
Chris@76
|
1362 }
|
Chris@76
|
1363 elseif ($action['type'] == 'require-dir')
|
Chris@76
|
1364 {
|
Chris@76
|
1365 copytree($action['source'], $action['destination']);
|
Chris@76
|
1366 // Any other theme folders?
|
Chris@76
|
1367 if (!empty($context['theme_copies']) && !empty($context['theme_copies'][$action['type']][$action['destination']]))
|
Chris@76
|
1368 foreach ($context['theme_copies'][$action['type']][$action['destination']] as $theme_destination)
|
Chris@76
|
1369 copytree($action['source'], $theme_destination);
|
Chris@76
|
1370 }
|
Chris@76
|
1371 elseif ($action['type'] == 'require-file')
|
Chris@76
|
1372 {
|
Chris@76
|
1373 if (!mktree(dirname($action['destination']), 0755) || !is_writable(dirname($action['destination'])))
|
Chris@76
|
1374 $failure |= !mktree(dirname($action['destination']), 0777);
|
Chris@76
|
1375
|
Chris@76
|
1376 package_put_contents($action['destination'], package_get_contents($action['source']), $testing_only);
|
Chris@76
|
1377
|
Chris@76
|
1378 $failure |= !copy($action['source'], $action['destination']);
|
Chris@76
|
1379
|
Chris@76
|
1380 // Any other theme files?
|
Chris@76
|
1381 if (!empty($context['theme_copies']) && !empty($context['theme_copies'][$action['type']][$action['destination']]))
|
Chris@76
|
1382 foreach ($context['theme_copies'][$action['type']][$action['destination']] as $theme_destination)
|
Chris@76
|
1383 {
|
Chris@76
|
1384 if (!mktree(dirname($theme_destination), 0755) || !is_writable(dirname($theme_destination)))
|
Chris@76
|
1385 $failure |= !mktree(dirname($theme_destination), 0777);
|
Chris@76
|
1386
|
Chris@76
|
1387 package_put_contents($theme_destination, package_get_contents($action['source']), $testing_only);
|
Chris@76
|
1388
|
Chris@76
|
1389 $failure |= !copy($action['source'], $theme_destination);
|
Chris@76
|
1390 }
|
Chris@76
|
1391 }
|
Chris@76
|
1392 elseif ($action['type'] == 'move-file')
|
Chris@76
|
1393 {
|
Chris@76
|
1394 if (!mktree(dirname($action['destination']), 0755) || !is_writable(dirname($action['destination'])))
|
Chris@76
|
1395 $failure |= !mktree(dirname($action['destination']), 0777);
|
Chris@76
|
1396
|
Chris@76
|
1397 $failure |= !rename($action['source'], $action['destination']);
|
Chris@76
|
1398 }
|
Chris@76
|
1399 elseif ($action['type'] == 'move-dir')
|
Chris@76
|
1400 {
|
Chris@76
|
1401 if (!mktree($action['destination'], 0755) || !is_writable($action['destination']))
|
Chris@76
|
1402 $failure |= !mktree($action['destination'], 0777);
|
Chris@76
|
1403
|
Chris@76
|
1404 $failure |= !rename($action['source'], $action['destination']);
|
Chris@76
|
1405 }
|
Chris@76
|
1406 elseif ($action['type'] == 'remove-dir')
|
Chris@76
|
1407 {
|
Chris@76
|
1408 deltree($action['filename']);
|
Chris@76
|
1409
|
Chris@76
|
1410 // Any other theme folders?
|
Chris@76
|
1411 if (!empty($context['theme_copies']) && !empty($context['theme_copies'][$action['type']][$action['filename']]))
|
Chris@76
|
1412 foreach ($context['theme_copies'][$action['type']][$action['filename']] as $theme_destination)
|
Chris@76
|
1413 deltree($theme_destination);
|
Chris@76
|
1414 }
|
Chris@76
|
1415 elseif ($action['type'] == 'remove-file')
|
Chris@76
|
1416 {
|
Chris@76
|
1417 // Make sure the file exists before deleting it.
|
Chris@76
|
1418 if (file_exists($action['filename']))
|
Chris@76
|
1419 {
|
Chris@76
|
1420 package_chmod($action['filename']);
|
Chris@76
|
1421 $failure |= !unlink($action['filename']);
|
Chris@76
|
1422 }
|
Chris@76
|
1423 // The file that was supposed to be deleted couldn't be found.
|
Chris@76
|
1424 else
|
Chris@76
|
1425 $failure = true;
|
Chris@76
|
1426
|
Chris@76
|
1427 // Any other theme folders?
|
Chris@76
|
1428 if (!empty($context['theme_copies']) && !empty($context['theme_copies'][$action['type']][$action['filename']]))
|
Chris@76
|
1429 foreach ($context['theme_copies'][$action['type']][$action['filename']] as $theme_destination)
|
Chris@76
|
1430 if (file_exists($theme_destination))
|
Chris@76
|
1431 $failure |= !unlink($theme_destination);
|
Chris@76
|
1432 else
|
Chris@76
|
1433 $failure = true;
|
Chris@76
|
1434 }
|
Chris@76
|
1435 }
|
Chris@76
|
1436
|
Chris@76
|
1437 return $not_done;
|
Chris@76
|
1438 }
|
Chris@76
|
1439
|
Chris@76
|
1440 // This function tries to match $version into any of the ranges given in $versions
|
Chris@76
|
1441 function matchPackageVersion($version, $versions)
|
Chris@76
|
1442 {
|
Chris@76
|
1443 // Make sure everything is lowercase and clean of spaces and unpleasant history.
|
Chris@76
|
1444 $version = str_replace(array(' ', '2.0rc1-1'), array('', '2.0rc1.1'), strtolower($version));
|
Chris@76
|
1445 $versions = explode(',', str_replace(array(' ', '2.0rc1-1'), array('', '2.0rc1.1'), strtolower($versions)));
|
Chris@76
|
1446
|
Chris@76
|
1447 // Perhaps we do accept anything?
|
Chris@76
|
1448 if (in_array('all', $versions))
|
Chris@76
|
1449 return true;
|
Chris@76
|
1450
|
Chris@76
|
1451 // Loop through each version.
|
Chris@76
|
1452 foreach ($versions as $for)
|
Chris@76
|
1453 {
|
Chris@76
|
1454 // Wild card spotted?
|
Chris@76
|
1455 if (strpos($for, '*') !== false)
|
Chris@76
|
1456 $for = str_replace('*', '0dev0', $for) . '-' . str_replace('*', '999', $for);
|
Chris@76
|
1457
|
Chris@76
|
1458 // Do we have a range?
|
Chris@76
|
1459 if (strpos($for, '-') !== false)
|
Chris@76
|
1460 {
|
Chris@76
|
1461 list ($lower, $upper) = explode('-', $for);
|
Chris@76
|
1462
|
Chris@76
|
1463 // Compare the version against lower and upper bounds.
|
Chris@76
|
1464 if (compareVersions($version, $lower) > -1 && compareVersions($version, $upper) < 1)
|
Chris@76
|
1465 return true;
|
Chris@76
|
1466 }
|
Chris@76
|
1467 // Otherwise check if they are equal...
|
Chris@76
|
1468 elseif (compareVersions($version, $for) === 0)
|
Chris@76
|
1469 return true;
|
Chris@76
|
1470 }
|
Chris@76
|
1471
|
Chris@76
|
1472 return false;
|
Chris@76
|
1473 }
|
Chris@76
|
1474
|
Chris@76
|
1475 // The geek version of versioning checks for dummies, which basically compares two versions.
|
Chris@76
|
1476 function compareVersions($version1, $version2)
|
Chris@76
|
1477 {
|
Chris@76
|
1478 static $categories;
|
Chris@76
|
1479
|
Chris@76
|
1480 $versions = array();
|
Chris@76
|
1481 foreach (array(1 => $version1, $version2) as $id => $version)
|
Chris@76
|
1482 {
|
Chris@76
|
1483 // Clean the version and extract the version parts.
|
Chris@76
|
1484 $clean = str_replace(array(' ', '2.0rc1-1'), array('', '2.0rc1.1'), strtolower($version));
|
Chris@76
|
1485 preg_match('~(\d+)(?:\.(\d+|))?(?:\.)?(\d+|)(?:(alpha|beta|rc)(\d+|)(?:\.)?(\d+|))?(?:(dev))?(\d+|)~', $clean, $parts);
|
Chris@76
|
1486
|
Chris@76
|
1487 // Build an array of parts.
|
Chris@76
|
1488 $versions[$id] = array(
|
Chris@76
|
1489 'major' => (int) $parts[1],
|
Chris@76
|
1490 'minor' => !empty($parts[2]) ? (int) $parts[2] : 0,
|
Chris@76
|
1491 'patch' => !empty($parts[3]) ? (int) $parts[3] : 0,
|
Chris@76
|
1492 'type' => empty($parts[4]) ? 'stable' : $parts[4],
|
Chris@76
|
1493 'type_major' => !empty($parts[6]) ? (int) $parts[5] : 0,
|
Chris@76
|
1494 'type_minor' => !empty($parts[6]) ? (int) $parts[6] : 0,
|
Chris@76
|
1495 'dev' => !empty($parts[7]),
|
Chris@76
|
1496 );
|
Chris@76
|
1497 }
|
Chris@76
|
1498
|
Chris@76
|
1499 // Are they the same, perhaps?
|
Chris@76
|
1500 if ($versions[1] === $versions[2])
|
Chris@76
|
1501 return 0;
|
Chris@76
|
1502
|
Chris@76
|
1503 // Get version numbering categories...
|
Chris@76
|
1504 if (!isset($categories))
|
Chris@76
|
1505 $categories = array_keys($versions[1]);
|
Chris@76
|
1506
|
Chris@76
|
1507 // Loop through each category.
|
Chris@76
|
1508 foreach ($categories as $category)
|
Chris@76
|
1509 {
|
Chris@76
|
1510 // Is there something for us to calculate?
|
Chris@76
|
1511 if ($versions[1][$category] !== $versions[2][$category])
|
Chris@76
|
1512 {
|
Chris@76
|
1513 // Dev builds are a problematic exception.
|
Chris@76
|
1514 // (stable) dev < (stable) but (unstable) dev = (unstable)
|
Chris@76
|
1515 if ($category == 'type')
|
Chris@76
|
1516 return $versions[1][$category] > $versions[2][$category] ? ($versions[1]['dev'] ? -1 : 1) : ($versions[2]['dev'] ? 1 : -1);
|
Chris@76
|
1517 elseif ($category == 'dev')
|
Chris@76
|
1518 return $versions[1]['dev'] ? ($versions[2]['type'] == 'stable' ? -1 : 0) : ($versions[1]['type'] == 'stable' ? 1 : 0);
|
Chris@76
|
1519 // Otherwise a simple comparison.
|
Chris@76
|
1520 else
|
Chris@76
|
1521 return $versions[1][$category] > $versions[2][$category] ? 1 : -1;
|
Chris@76
|
1522 }
|
Chris@76
|
1523 }
|
Chris@76
|
1524
|
Chris@76
|
1525 // They are the same!
|
Chris@76
|
1526 return 0;
|
Chris@76
|
1527 }
|
Chris@76
|
1528
|
Chris@76
|
1529 function parse_path($path)
|
Chris@76
|
1530 {
|
Chris@76
|
1531 global $modSettings, $boarddir, $sourcedir, $settings, $temp_path;
|
Chris@76
|
1532
|
Chris@76
|
1533 $dirs = array(
|
Chris@76
|
1534 '\\' => '/',
|
Chris@76
|
1535 '$boarddir' => $boarddir,
|
Chris@76
|
1536 '$sourcedir' => $sourcedir,
|
Chris@76
|
1537 '$avatardir' => $modSettings['avatar_directory'],
|
Chris@76
|
1538 '$avatars_dir' => $modSettings['avatar_directory'],
|
Chris@76
|
1539 '$themedir' => $settings['default_theme_dir'],
|
Chris@76
|
1540 '$imagesdir' => $settings['default_theme_dir'] . '/' . basename($settings['default_images_url']),
|
Chris@76
|
1541 '$themes_dir' => $boarddir . '/Themes',
|
Chris@76
|
1542 '$languagedir' => $settings['default_theme_dir'] . '/languages',
|
Chris@76
|
1543 '$languages_dir' => $settings['default_theme_dir'] . '/languages',
|
Chris@76
|
1544 '$smileysdir' => $modSettings['smileys_dir'],
|
Chris@76
|
1545 '$smileys_dir' => $modSettings['smileys_dir'],
|
Chris@76
|
1546 );
|
Chris@76
|
1547
|
Chris@76
|
1548 // do we parse in a package directory?
|
Chris@76
|
1549 if (!empty($temp_path))
|
Chris@76
|
1550 $dirs['$package'] = $temp_path;
|
Chris@76
|
1551
|
Chris@76
|
1552 if (strlen($path) == 0)
|
Chris@76
|
1553 trigger_error('parse_path(): There should never be an empty filename', E_USER_ERROR);
|
Chris@76
|
1554
|
Chris@76
|
1555 return strtr($path, $dirs);
|
Chris@76
|
1556 }
|
Chris@76
|
1557
|
Chris@76
|
1558 function deltree($dir, $delete_dir = true)
|
Chris@76
|
1559 {
|
Chris@76
|
1560 global $package_ftp;
|
Chris@76
|
1561
|
Chris@76
|
1562 if (!file_exists($dir))
|
Chris@76
|
1563 return;
|
Chris@76
|
1564
|
Chris@76
|
1565 $current_dir = @opendir($dir);
|
Chris@76
|
1566 if ($current_dir == false)
|
Chris@76
|
1567 {
|
Chris@76
|
1568 if ($delete_dir && isset($package_ftp))
|
Chris@76
|
1569 {
|
Chris@76
|
1570 $ftp_file = strtr($dir, array($_SESSION['pack_ftp']['root'] => ''));
|
Chris@76
|
1571 if (!is_writable($dir . '/' . $entryname))
|
Chris@76
|
1572 $package_ftp->chmod($ftp_file, 0777);
|
Chris@76
|
1573 $package_ftp->unlink($ftp_file);
|
Chris@76
|
1574 }
|
Chris@76
|
1575
|
Chris@76
|
1576 return;
|
Chris@76
|
1577 }
|
Chris@76
|
1578
|
Chris@76
|
1579 while ($entryname = readdir($current_dir))
|
Chris@76
|
1580 {
|
Chris@76
|
1581 if (in_array($entryname, array('.', '..')))
|
Chris@76
|
1582 continue;
|
Chris@76
|
1583
|
Chris@76
|
1584 if (is_dir($dir . '/' . $entryname))
|
Chris@76
|
1585 deltree($dir . '/' . $entryname);
|
Chris@76
|
1586 else
|
Chris@76
|
1587 {
|
Chris@76
|
1588 // Here, 755 doesn't really matter since we're deleting it anyway.
|
Chris@76
|
1589 if (isset($package_ftp))
|
Chris@76
|
1590 {
|
Chris@76
|
1591 $ftp_file = strtr($dir . '/' . $entryname, array($_SESSION['pack_ftp']['root'] => ''));
|
Chris@76
|
1592
|
Chris@76
|
1593 if (!is_writable($dir . '/' . $entryname))
|
Chris@76
|
1594 $package_ftp->chmod($ftp_file, 0777);
|
Chris@76
|
1595 $package_ftp->unlink($ftp_file);
|
Chris@76
|
1596 }
|
Chris@76
|
1597 else
|
Chris@76
|
1598 {
|
Chris@76
|
1599 if (!is_writable($dir . '/' . $entryname))
|
Chris@76
|
1600 @chmod($dir . '/' . $entryname, 0777);
|
Chris@76
|
1601 unlink($dir . '/' . $entryname);
|
Chris@76
|
1602 }
|
Chris@76
|
1603 }
|
Chris@76
|
1604 }
|
Chris@76
|
1605
|
Chris@76
|
1606 closedir($current_dir);
|
Chris@76
|
1607
|
Chris@76
|
1608 if ($delete_dir)
|
Chris@76
|
1609 {
|
Chris@76
|
1610 if (isset($package_ftp))
|
Chris@76
|
1611 {
|
Chris@76
|
1612 $ftp_file = strtr($dir, array($_SESSION['pack_ftp']['root'] => ''));
|
Chris@76
|
1613 if (!is_writable($dir . '/' . $entryname))
|
Chris@76
|
1614 $package_ftp->chmod($ftp_file, 0777);
|
Chris@76
|
1615 $package_ftp->unlink($ftp_file);
|
Chris@76
|
1616 }
|
Chris@76
|
1617 else
|
Chris@76
|
1618 {
|
Chris@76
|
1619 if (!is_writable($dir))
|
Chris@76
|
1620 @chmod($dir, 0777);
|
Chris@76
|
1621 @rmdir($dir);
|
Chris@76
|
1622 }
|
Chris@76
|
1623 }
|
Chris@76
|
1624 }
|
Chris@76
|
1625
|
Chris@76
|
1626 function mktree($strPath, $mode)
|
Chris@76
|
1627 {
|
Chris@76
|
1628 global $package_ftp;
|
Chris@76
|
1629
|
Chris@76
|
1630 if (is_dir($strPath))
|
Chris@76
|
1631 {
|
Chris@76
|
1632 if (!is_writable($strPath) && $mode !== false)
|
Chris@76
|
1633 {
|
Chris@76
|
1634 if (isset($package_ftp))
|
Chris@76
|
1635 $package_ftp->chmod(strtr($strPath, array($_SESSION['pack_ftp']['root'] => '')), $mode);
|
Chris@76
|
1636 else
|
Chris@76
|
1637 @chmod($strPath, $mode);
|
Chris@76
|
1638 }
|
Chris@76
|
1639
|
Chris@76
|
1640 $test = @opendir($strPath);
|
Chris@76
|
1641 if ($test)
|
Chris@76
|
1642 {
|
Chris@76
|
1643 closedir($test);
|
Chris@76
|
1644 return is_writable($strPath);
|
Chris@76
|
1645 }
|
Chris@76
|
1646 else
|
Chris@76
|
1647 return false;
|
Chris@76
|
1648 }
|
Chris@76
|
1649 // Is this an invalid path and/or we can't make the directory?
|
Chris@76
|
1650 if ($strPath == dirname($strPath) || !mktree(dirname($strPath), $mode))
|
Chris@76
|
1651 return false;
|
Chris@76
|
1652
|
Chris@76
|
1653 if (!is_writable(dirname($strPath)) && $mode !== false)
|
Chris@76
|
1654 {
|
Chris@76
|
1655 if (isset($package_ftp))
|
Chris@76
|
1656 $package_ftp->chmod(dirname(strtr($strPath, array($_SESSION['pack_ftp']['root'] => ''))), $mode);
|
Chris@76
|
1657 else
|
Chris@76
|
1658 @chmod(dirname($strPath), $mode);
|
Chris@76
|
1659 }
|
Chris@76
|
1660
|
Chris@76
|
1661 if ($mode !== false && isset($package_ftp))
|
Chris@76
|
1662 return $package_ftp->create_dir(strtr($strPath, array($_SESSION['pack_ftp']['root'] => '')));
|
Chris@76
|
1663 elseif ($mode === false)
|
Chris@76
|
1664 {
|
Chris@76
|
1665 $test = @opendir(dirname($strPath));
|
Chris@76
|
1666 if ($test)
|
Chris@76
|
1667 {
|
Chris@76
|
1668 closedir($test);
|
Chris@76
|
1669 return true;
|
Chris@76
|
1670 }
|
Chris@76
|
1671 else
|
Chris@76
|
1672 return false;
|
Chris@76
|
1673 }
|
Chris@76
|
1674 else
|
Chris@76
|
1675 {
|
Chris@76
|
1676 @mkdir($strPath, $mode);
|
Chris@76
|
1677 $test = @opendir($strPath);
|
Chris@76
|
1678 if ($test)
|
Chris@76
|
1679 {
|
Chris@76
|
1680 closedir($test);
|
Chris@76
|
1681 return true;
|
Chris@76
|
1682 }
|
Chris@76
|
1683 else
|
Chris@76
|
1684 return false;
|
Chris@76
|
1685 }
|
Chris@76
|
1686 }
|
Chris@76
|
1687
|
Chris@76
|
1688 function copytree($source, $destination)
|
Chris@76
|
1689 {
|
Chris@76
|
1690 global $package_ftp;
|
Chris@76
|
1691
|
Chris@76
|
1692 if (!file_exists($destination) || !is_writable($destination))
|
Chris@76
|
1693 mktree($destination, 0755);
|
Chris@76
|
1694 if (!is_writable($destination))
|
Chris@76
|
1695 mktree($destination, 0777);
|
Chris@76
|
1696
|
Chris@76
|
1697 $current_dir = opendir($source);
|
Chris@76
|
1698 if ($current_dir == false)
|
Chris@76
|
1699 return;
|
Chris@76
|
1700
|
Chris@76
|
1701 while ($entryname = readdir($current_dir))
|
Chris@76
|
1702 {
|
Chris@76
|
1703 if (in_array($entryname, array('.', '..')))
|
Chris@76
|
1704 continue;
|
Chris@76
|
1705
|
Chris@76
|
1706 if (isset($package_ftp))
|
Chris@76
|
1707 $ftp_file = strtr($destination . '/' . $entryname, array($_SESSION['pack_ftp']['root'] => ''));
|
Chris@76
|
1708
|
Chris@76
|
1709 if (is_file($source . '/' . $entryname))
|
Chris@76
|
1710 {
|
Chris@76
|
1711 if (isset($package_ftp) && !file_exists($destination . '/' . $entryname))
|
Chris@76
|
1712 $package_ftp->create_file($ftp_file);
|
Chris@76
|
1713 elseif (!file_exists($destination . '/' . $entryname))
|
Chris@76
|
1714 @touch($destination . '/' . $entryname);
|
Chris@76
|
1715 }
|
Chris@76
|
1716
|
Chris@76
|
1717 package_chmod($destination . '/' . $entryname);
|
Chris@76
|
1718
|
Chris@76
|
1719 if (is_dir($source . '/' . $entryname))
|
Chris@76
|
1720 copytree($source . '/' . $entryname, $destination . '/' . $entryname);
|
Chris@76
|
1721 elseif (file_exists($destination . '/' . $entryname))
|
Chris@76
|
1722 package_put_contents($destination . '/' . $entryname, package_get_contents($source . '/' . $entryname));
|
Chris@76
|
1723 else
|
Chris@76
|
1724 copy($source . '/' . $entryname, $destination . '/' . $entryname);
|
Chris@76
|
1725 }
|
Chris@76
|
1726
|
Chris@76
|
1727 closedir($current_dir);
|
Chris@76
|
1728 }
|
Chris@76
|
1729
|
Chris@76
|
1730 function listtree($path, $sub_path = '')
|
Chris@76
|
1731 {
|
Chris@76
|
1732 $data = array();
|
Chris@76
|
1733
|
Chris@76
|
1734 $dir = @dir($path . $sub_path);
|
Chris@76
|
1735 if (!$dir)
|
Chris@76
|
1736 return array();
|
Chris@76
|
1737 while ($entry = $dir->read())
|
Chris@76
|
1738 {
|
Chris@76
|
1739 if ($entry == '.' || $entry == '..')
|
Chris@76
|
1740 continue;
|
Chris@76
|
1741
|
Chris@76
|
1742 if (is_dir($path . $sub_path . '/' . $entry))
|
Chris@76
|
1743 $data = array_merge($data, listtree($path, $sub_path . '/' . $entry));
|
Chris@76
|
1744 else
|
Chris@76
|
1745 $data[] = array(
|
Chris@76
|
1746 'filename' => $sub_path == '' ? $entry : $sub_path . '/' . $entry,
|
Chris@76
|
1747 'size' => filesize($path . $sub_path . '/' . $entry),
|
Chris@76
|
1748 'skipped' => false,
|
Chris@76
|
1749 );
|
Chris@76
|
1750 }
|
Chris@76
|
1751 $dir->close();
|
Chris@76
|
1752
|
Chris@76
|
1753 return $data;
|
Chris@76
|
1754 }
|
Chris@76
|
1755
|
Chris@76
|
1756 // Parse an xml based modification file.
|
Chris@76
|
1757 function parseModification($file, $testing = true, $undo = false, $theme_paths = array())
|
Chris@76
|
1758 {
|
Chris@76
|
1759 global $boarddir, $sourcedir, $settings, $txt, $modSettings, $package_ftp;
|
Chris@76
|
1760
|
Chris@76
|
1761 @set_time_limit(600);
|
Chris@76
|
1762 loadClassFile('Class-Package.php');
|
Chris@76
|
1763 $xml = new xmlArray(strtr($file, array("\r" => '')));
|
Chris@76
|
1764 $actions = array();
|
Chris@76
|
1765 $everything_found = true;
|
Chris@76
|
1766
|
Chris@76
|
1767 if (!$xml->exists('modification') || !$xml->exists('modification/file'))
|
Chris@76
|
1768 {
|
Chris@76
|
1769 $actions[] = array(
|
Chris@76
|
1770 'type' => 'error',
|
Chris@76
|
1771 'filename' => '-',
|
Chris@76
|
1772 'debug' => $txt['package_modification_malformed']
|
Chris@76
|
1773 );
|
Chris@76
|
1774 return $actions;
|
Chris@76
|
1775 }
|
Chris@76
|
1776
|
Chris@76
|
1777 // Get the XML data.
|
Chris@76
|
1778 $files = $xml->set('modification/file');
|
Chris@76
|
1779
|
Chris@76
|
1780 // Use this for holding all the template changes in this mod.
|
Chris@76
|
1781 $template_changes = array();
|
Chris@76
|
1782 // This is needed to hold the long paths, as they can vary...
|
Chris@76
|
1783 $long_changes = array();
|
Chris@76
|
1784
|
Chris@76
|
1785 // First, we need to build the list of all the files likely to get changed.
|
Chris@76
|
1786 foreach ($files as $file)
|
Chris@76
|
1787 {
|
Chris@76
|
1788 // What is the filename we're currently on?
|
Chris@76
|
1789 $filename = parse_path(trim($file->fetch('@name')));
|
Chris@76
|
1790
|
Chris@76
|
1791 // Now, we need to work out whether this is even a template file...
|
Chris@76
|
1792 foreach ($theme_paths as $id => $theme)
|
Chris@76
|
1793 {
|
Chris@76
|
1794 // If this filename is relative, if so take a guess at what it should be.
|
Chris@76
|
1795 $real_filename = $filename;
|
Chris@76
|
1796 if (strpos($filename, 'Themes') === 0)
|
Chris@76
|
1797 $real_filename = $boarddir . '/' . $filename;
|
Chris@76
|
1798
|
Chris@76
|
1799 if (strpos($real_filename, $theme['theme_dir']) === 0)
|
Chris@76
|
1800 {
|
Chris@76
|
1801 $template_changes[$id][] = substr($real_filename, strlen($theme['theme_dir']) + 1);
|
Chris@76
|
1802 $long_changes[$id][] = $filename;
|
Chris@76
|
1803 }
|
Chris@76
|
1804 }
|
Chris@76
|
1805 }
|
Chris@76
|
1806
|
Chris@76
|
1807 // Custom themes to add.
|
Chris@76
|
1808 $custom_themes_add = array();
|
Chris@76
|
1809
|
Chris@76
|
1810 // If we have some template changes, we need to build a master link of what new ones are required for the custom themes.
|
Chris@76
|
1811 if (!empty($template_changes[1]))
|
Chris@76
|
1812 {
|
Chris@76
|
1813 foreach ($theme_paths as $id => $theme)
|
Chris@76
|
1814 {
|
Chris@76
|
1815 // Default is getting done anyway, so no need for involvement here.
|
Chris@76
|
1816 if ($id == 1)
|
Chris@76
|
1817 continue;
|
Chris@76
|
1818
|
Chris@76
|
1819 // For every template, do we want it? Yea, no, maybe?
|
Chris@76
|
1820 foreach ($template_changes[1] as $index => $template_file)
|
Chris@76
|
1821 {
|
Chris@76
|
1822 // What, it exists and we haven't already got it?! Lordy, get it in!
|
Chris@76
|
1823 if (file_exists($theme['theme_dir'] . '/' . $template_file) && (!isset($template_changes[$id]) || !in_array($template_file, $template_changes[$id])))
|
Chris@76
|
1824 {
|
Chris@76
|
1825 // Now let's add it to the "todo" list.
|
Chris@76
|
1826 $custom_themes_add[$long_changes[1][$index]][$id] = $theme['theme_dir'] . '/' . $template_file;
|
Chris@76
|
1827 }
|
Chris@76
|
1828 }
|
Chris@76
|
1829 }
|
Chris@76
|
1830 }
|
Chris@76
|
1831
|
Chris@76
|
1832 foreach ($files as $file)
|
Chris@76
|
1833 {
|
Chris@76
|
1834 // This is the actual file referred to in the XML document...
|
Chris@76
|
1835 $files_to_change = array(
|
Chris@76
|
1836 1 => parse_path(trim($file->fetch('@name'))),
|
Chris@76
|
1837 );
|
Chris@76
|
1838
|
Chris@76
|
1839 // Sometimes though, we have some additional files for other themes, if we have add them to the mix.
|
Chris@76
|
1840 if (isset($custom_themes_add[$files_to_change[1]]))
|
Chris@76
|
1841 $files_to_change += $custom_themes_add[$files_to_change[1]];
|
Chris@76
|
1842
|
Chris@76
|
1843 // Now, loop through all the files we're changing, and, well, change them ;)
|
Chris@76
|
1844 foreach ($files_to_change as $theme => $working_file)
|
Chris@76
|
1845 {
|
Chris@76
|
1846 if ($working_file[0] != '/' && $working_file[1] != ':')
|
Chris@76
|
1847 {
|
Chris@76
|
1848 trigger_error('parseModification(): The filename \'' . $working_file . '\' is not a full path!', E_USER_WARNING);
|
Chris@76
|
1849
|
Chris@76
|
1850 $working_file = $boarddir . '/' . $working_file;
|
Chris@76
|
1851 }
|
Chris@76
|
1852
|
Chris@76
|
1853 // Doesn't exist - give an error or what?
|
Chris@76
|
1854 if (!file_exists($working_file) && (!$file->exists('@error') || !in_array(trim($file->fetch('@error')), array('ignore', 'skip'))))
|
Chris@76
|
1855 {
|
Chris@76
|
1856 $actions[] = array(
|
Chris@76
|
1857 'type' => 'missing',
|
Chris@76
|
1858 'filename' => $working_file,
|
Chris@76
|
1859 'debug' => $txt['package_modification_missing']
|
Chris@76
|
1860 );
|
Chris@76
|
1861
|
Chris@76
|
1862 $everything_found = false;
|
Chris@76
|
1863 continue;
|
Chris@76
|
1864 }
|
Chris@76
|
1865 // Skip the file if it doesn't exist.
|
Chris@76
|
1866 elseif (!file_exists($working_file) && $file->exists('@error') && trim($file->fetch('@error')) == 'skip')
|
Chris@76
|
1867 {
|
Chris@76
|
1868 $actions[] = array(
|
Chris@76
|
1869 'type' => 'skipping',
|
Chris@76
|
1870 'filename' => $working_file,
|
Chris@76
|
1871 );
|
Chris@76
|
1872 continue;
|
Chris@76
|
1873 }
|
Chris@76
|
1874 // Okay, we're creating this file then...?
|
Chris@76
|
1875 elseif (!file_exists($working_file))
|
Chris@76
|
1876 $working_data = '';
|
Chris@76
|
1877 // Phew, it exists! Load 'er up!
|
Chris@76
|
1878 else
|
Chris@76
|
1879 $working_data = str_replace("\r", '', package_get_contents($working_file));
|
Chris@76
|
1880
|
Chris@76
|
1881 $actions[] = array(
|
Chris@76
|
1882 'type' => 'opened',
|
Chris@76
|
1883 'filename' => $working_file
|
Chris@76
|
1884 );
|
Chris@76
|
1885
|
Chris@76
|
1886 $operations = $file->exists('operation') ? $file->set('operation') : array();
|
Chris@76
|
1887 foreach ($operations as $operation)
|
Chris@76
|
1888 {
|
Chris@76
|
1889 // Convert operation to an array.
|
Chris@76
|
1890 $actual_operation = array(
|
Chris@76
|
1891 'searches' => array(),
|
Chris@76
|
1892 'error' => $operation->exists('@error') && in_array(trim($operation->fetch('@error')), array('ignore', 'fatal', 'required')) ? trim($operation->fetch('@error')) : 'fatal',
|
Chris@76
|
1893 );
|
Chris@76
|
1894
|
Chris@76
|
1895 // The 'add' parameter is used for all searches in this operation.
|
Chris@76
|
1896 $add = $operation->exists('add') ? $operation->fetch('add') : '';
|
Chris@76
|
1897
|
Chris@76
|
1898 // Grab all search items of this operation (in most cases just 1).
|
Chris@76
|
1899 $searches = $operation->set('search');
|
Chris@76
|
1900 foreach ($searches as $i => $search)
|
Chris@76
|
1901 $actual_operation['searches'][] = array(
|
Chris@76
|
1902 'position' => $search->exists('@position') && in_array(trim($search->fetch('@position')), array('before', 'after', 'replace', 'end')) ? trim($search->fetch('@position')) : 'replace',
|
Chris@76
|
1903 'is_reg_exp' => $search->exists('@regexp') && trim($search->fetch('@regexp')) === 'true',
|
Chris@76
|
1904 'loose_whitespace' => $search->exists('@whitespace') && trim($search->fetch('@whitespace')) === 'loose',
|
Chris@76
|
1905 'search' => $search->fetch('.'),
|
Chris@76
|
1906 'add' => $add,
|
Chris@76
|
1907 'preg_search' => '',
|
Chris@76
|
1908 'preg_replace' => '',
|
Chris@76
|
1909 );
|
Chris@76
|
1910
|
Chris@76
|
1911 // At least one search should be defined.
|
Chris@76
|
1912 if (empty($actual_operation['searches']))
|
Chris@76
|
1913 {
|
Chris@76
|
1914 $actions[] = array(
|
Chris@76
|
1915 'type' => 'failure',
|
Chris@76
|
1916 'filename' => $working_file,
|
Chris@76
|
1917 'search' => $search['search'],
|
Chris@76
|
1918 'is_custom' => $theme > 1 ? $theme : 0,
|
Chris@76
|
1919 );
|
Chris@76
|
1920
|
Chris@76
|
1921 // Skip to the next operation.
|
Chris@76
|
1922 continue;
|
Chris@76
|
1923 }
|
Chris@76
|
1924
|
Chris@76
|
1925 // Reverse the operations in case of undoing stuff.
|
Chris@76
|
1926 if ($undo)
|
Chris@76
|
1927 {
|
Chris@76
|
1928 foreach ($actual_operation['searches'] as $i => $search)
|
Chris@76
|
1929 {
|
Chris@76
|
1930
|
Chris@76
|
1931 // Reverse modification of regular expressions are not allowed.
|
Chris@76
|
1932 if ($search['is_reg_exp'])
|
Chris@76
|
1933 {
|
Chris@76
|
1934 if ($actual_operation['error'] === 'fatal')
|
Chris@76
|
1935 $actions[] = array(
|
Chris@76
|
1936 'type' => 'failure',
|
Chris@76
|
1937 'filename' => $working_file,
|
Chris@76
|
1938 'search' => $search['search'],
|
Chris@76
|
1939 'is_custom' => $theme > 1 ? $theme : 0,
|
Chris@76
|
1940 );
|
Chris@76
|
1941
|
Chris@76
|
1942 // Continue to the next operation.
|
Chris@76
|
1943 continue 2;
|
Chris@76
|
1944 }
|
Chris@76
|
1945
|
Chris@76
|
1946 // The replacement is now the search subject...
|
Chris@76
|
1947 if ($search['position'] === 'replace' || $search['position'] === 'end')
|
Chris@76
|
1948 $actual_operation['searches'][$i]['search'] = $search['add'];
|
Chris@76
|
1949 else
|
Chris@76
|
1950 {
|
Chris@76
|
1951 // Reversing a before/after modification becomes a replacement.
|
Chris@76
|
1952 $actual_operation['searches'][$i]['position'] = 'replace';
|
Chris@76
|
1953
|
Chris@76
|
1954 if ($search['position'] === 'before')
|
Chris@76
|
1955 $actual_operation['searches'][$i]['search'] .= $search['add'];
|
Chris@76
|
1956 elseif ($search['position'] === 'after')
|
Chris@76
|
1957 $actual_operation['searches'][$i]['search'] = $search['add'] . $search['search'];
|
Chris@76
|
1958 }
|
Chris@76
|
1959
|
Chris@76
|
1960 // ...and the search subject is now the replacement.
|
Chris@76
|
1961 $actual_operation['searches'][$i]['add'] = $search['search'];
|
Chris@76
|
1962 }
|
Chris@76
|
1963 }
|
Chris@76
|
1964
|
Chris@76
|
1965 // Sort the search list so the replaces come before the add before/after's.
|
Chris@76
|
1966 if (count($actual_operation['searches']) !== 1)
|
Chris@76
|
1967 {
|
Chris@76
|
1968 $replacements = array();
|
Chris@76
|
1969
|
Chris@76
|
1970 foreach ($actual_operation['searches'] as $i => $search)
|
Chris@76
|
1971 {
|
Chris@76
|
1972 if ($search['position'] === 'replace')
|
Chris@76
|
1973 {
|
Chris@76
|
1974 $replacements[] = $search;
|
Chris@76
|
1975 unset($actual_operation['searches'][$i]);
|
Chris@76
|
1976 }
|
Chris@76
|
1977 }
|
Chris@76
|
1978 $actual_operation['searches'] = array_merge($replacements, $actual_operation['searches']);
|
Chris@76
|
1979 }
|
Chris@76
|
1980
|
Chris@76
|
1981 // Create regular expression replacements from each search.
|
Chris@76
|
1982 foreach ($actual_operation['searches'] as $i => $search)
|
Chris@76
|
1983 {
|
Chris@76
|
1984 // Not much needed if the search subject is already a regexp.
|
Chris@76
|
1985 if ($search['is_reg_exp'])
|
Chris@76
|
1986 $actual_operation['searches'][$i]['preg_search'] = $search['search'];
|
Chris@76
|
1987 else
|
Chris@76
|
1988 {
|
Chris@76
|
1989 // Make the search subject fit into a regular expression.
|
Chris@76
|
1990 $actual_operation['searches'][$i]['preg_search'] = preg_quote($search['search'], '~');
|
Chris@76
|
1991
|
Chris@76
|
1992 // Using 'loose', a random amount of tabs and spaces may be used.
|
Chris@76
|
1993 if ($search['loose_whitespace'])
|
Chris@76
|
1994 $actual_operation['searches'][$i]['preg_search'] = preg_replace('~[ \t]+~', '[ \t]+', $actual_operation['searches'][$i]['preg_search']);
|
Chris@76
|
1995 }
|
Chris@76
|
1996
|
Chris@76
|
1997 // Shuzzup. This is done so we can safely use a regular expression. ($0 is bad!!)
|
Chris@76
|
1998 $actual_operation['searches'][$i]['preg_replace'] = strtr($search['add'], array('$' => '[$PACK' . 'AGE1$]', '\\' => '[$PACK' . 'AGE2$]'));
|
Chris@76
|
1999
|
Chris@76
|
2000 // Before, so the replacement comes after the search subject :P
|
Chris@76
|
2001 if ($search['position'] === 'before')
|
Chris@76
|
2002 {
|
Chris@76
|
2003 $actual_operation['searches'][$i]['preg_search'] = '(' . $actual_operation['searches'][$i]['preg_search'] . ')';
|
Chris@76
|
2004 $actual_operation['searches'][$i]['preg_replace'] = '$1' . $actual_operation['searches'][$i]['preg_replace'];
|
Chris@76
|
2005 }
|
Chris@76
|
2006
|
Chris@76
|
2007 // After, after what?
|
Chris@76
|
2008 elseif ($search['position'] === 'after')
|
Chris@76
|
2009 {
|
Chris@76
|
2010 $actual_operation['searches'][$i]['preg_search'] = '(' . $actual_operation['searches'][$i]['preg_search'] . ')';
|
Chris@76
|
2011 $actual_operation['searches'][$i]['preg_replace'] .= '$1';
|
Chris@76
|
2012 }
|
Chris@76
|
2013
|
Chris@76
|
2014 // Position the replacement at the end of the file (or just before the closing PHP tags).
|
Chris@76
|
2015 elseif ($search['position'] === 'end')
|
Chris@76
|
2016 {
|
Chris@76
|
2017 if ($undo)
|
Chris@76
|
2018 {
|
Chris@76
|
2019 $actual_operation['searches'][$i]['preg_replace'] = '';
|
Chris@76
|
2020 }
|
Chris@76
|
2021 else
|
Chris@76
|
2022 {
|
Chris@76
|
2023 $actual_operation['searches'][$i]['preg_search'] = '(\\n\\?\\>)?$';
|
Chris@76
|
2024 $actual_operation['searches'][$i]['preg_replace'] .= '$1';
|
Chris@76
|
2025 }
|
Chris@76
|
2026 }
|
Chris@76
|
2027
|
Chris@76
|
2028 // Testing 1, 2, 3...
|
Chris@76
|
2029 $failed = preg_match('~' . $actual_operation['searches'][$i]['preg_search'] . '~s', $working_data) === 0;
|
Chris@76
|
2030
|
Chris@76
|
2031 // Nope, search pattern not found.
|
Chris@76
|
2032 if ($failed && $actual_operation['error'] === 'fatal')
|
Chris@76
|
2033 {
|
Chris@76
|
2034 $actions[] = array(
|
Chris@76
|
2035 'type' => 'failure',
|
Chris@76
|
2036 'filename' => $working_file,
|
Chris@76
|
2037 'search' => $actual_operation['searches'][$i]['preg_search'],
|
Chris@76
|
2038 'search_original' => $actual_operation['searches'][$i]['search'],
|
Chris@76
|
2039 'replace_original' => $actual_operation['searches'][$i]['add'],
|
Chris@76
|
2040 'position' => $search['position'],
|
Chris@76
|
2041 'is_custom' => $theme > 1 ? $theme : 0,
|
Chris@76
|
2042 'failed' => $failed,
|
Chris@76
|
2043 );
|
Chris@76
|
2044
|
Chris@76
|
2045 $everything_found = false;
|
Chris@76
|
2046 continue;
|
Chris@76
|
2047 }
|
Chris@76
|
2048
|
Chris@76
|
2049 // Found, but in this case, that means failure!
|
Chris@76
|
2050 elseif (!$failed && $actual_operation['error'] === 'required')
|
Chris@76
|
2051 {
|
Chris@76
|
2052 $actions[] = array(
|
Chris@76
|
2053 'type' => 'failure',
|
Chris@76
|
2054 'filename' => $working_file,
|
Chris@76
|
2055 'search' => $actual_operation['searches'][$i]['preg_search'],
|
Chris@76
|
2056 'search_original' => $actual_operation['searches'][$i]['search'],
|
Chris@76
|
2057 'replace_original' => $actual_operation['searches'][$i]['add'],
|
Chris@76
|
2058 'position' => $search['position'],
|
Chris@76
|
2059 'is_custom' => $theme > 1 ? $theme : 0,
|
Chris@76
|
2060 'failed' => $failed,
|
Chris@76
|
2061 );
|
Chris@76
|
2062
|
Chris@76
|
2063 $everything_found = false;
|
Chris@76
|
2064 continue;
|
Chris@76
|
2065 }
|
Chris@76
|
2066
|
Chris@76
|
2067 // Replace it into nothing? That's not an option...unless it's an undoing end.
|
Chris@76
|
2068 if ($search['add'] === '' && ($search['position'] !== 'end' || !$undo))
|
Chris@76
|
2069 continue;
|
Chris@76
|
2070
|
Chris@76
|
2071 // Finally, we're doing some replacements.
|
Chris@76
|
2072 $working_data = preg_replace('~' . $actual_operation['searches'][$i]['preg_search'] . '~s', $actual_operation['searches'][$i]['preg_replace'], $working_data, 1);
|
Chris@76
|
2073
|
Chris@76
|
2074 $actions[] = array(
|
Chris@76
|
2075 'type' => 'replace',
|
Chris@76
|
2076 'filename' => $working_file,
|
Chris@76
|
2077 'search' => $actual_operation['searches'][$i]['preg_search'],
|
Chris@76
|
2078 'replace' => $actual_operation['searches'][$i]['preg_replace'],
|
Chris@76
|
2079 'search_original' => $actual_operation['searches'][$i]['search'],
|
Chris@76
|
2080 'replace_original' => $actual_operation['searches'][$i]['add'],
|
Chris@76
|
2081 'position' => $search['position'],
|
Chris@76
|
2082 'failed' => $failed,
|
Chris@76
|
2083 'ignore_failure' => $failed && $actual_operation['error'] === 'ignore',
|
Chris@76
|
2084 'is_custom' => $theme > 1 ? $theme : 0,
|
Chris@76
|
2085 );
|
Chris@76
|
2086 }
|
Chris@76
|
2087 }
|
Chris@76
|
2088
|
Chris@76
|
2089 // Fix any little helper symbols ;).
|
Chris@76
|
2090 $working_data = strtr($working_data, array('[$PACK' . 'AGE1$]' => '$', '[$PACK' . 'AGE2$]' => '\\'));
|
Chris@76
|
2091
|
Chris@76
|
2092 package_chmod($working_file);
|
Chris@76
|
2093
|
Chris@76
|
2094 if ((file_exists($working_file) && !is_writable($working_file)) || (!file_exists($working_file) && !is_writable(dirname($working_file))))
|
Chris@76
|
2095 $actions[] = array(
|
Chris@76
|
2096 'type' => 'chmod',
|
Chris@76
|
2097 'filename' => $working_file
|
Chris@76
|
2098 );
|
Chris@76
|
2099
|
Chris@76
|
2100 if (basename($working_file) == 'Settings_bak.php')
|
Chris@76
|
2101 continue;
|
Chris@76
|
2102
|
Chris@76
|
2103 if (!$testing && !empty($modSettings['package_make_backups']) && file_exists($working_file))
|
Chris@76
|
2104 {
|
Chris@76
|
2105 // No, no, not Settings.php!
|
Chris@76
|
2106 if (basename($working_file) == 'Settings.php')
|
Chris@76
|
2107 @copy($working_file, dirname($working_file) . '/Settings_bak.php');
|
Chris@76
|
2108 else
|
Chris@76
|
2109 @copy($working_file, $working_file . '~');
|
Chris@76
|
2110 }
|
Chris@76
|
2111
|
Chris@76
|
2112 // Always call this, even if in testing, because it won't really be written in testing mode.
|
Chris@76
|
2113 package_put_contents($working_file, $working_data, $testing);
|
Chris@76
|
2114
|
Chris@76
|
2115 $actions[] = array(
|
Chris@76
|
2116 'type' => 'saved',
|
Chris@76
|
2117 'filename' => $working_file,
|
Chris@76
|
2118 'is_custom' => $theme > 1 ? $theme : 0,
|
Chris@76
|
2119 );
|
Chris@76
|
2120 }
|
Chris@76
|
2121 }
|
Chris@76
|
2122
|
Chris@76
|
2123 $actions[] = array(
|
Chris@76
|
2124 'type' => 'result',
|
Chris@76
|
2125 'status' => $everything_found
|
Chris@76
|
2126 );
|
Chris@76
|
2127
|
Chris@76
|
2128 return $actions;
|
Chris@76
|
2129 }
|
Chris@76
|
2130
|
Chris@76
|
2131 // Parses a BoardMod format mod file...
|
Chris@76
|
2132 function parseBoardMod($file, $testing = true, $undo = false, $theme_paths = array())
|
Chris@76
|
2133 {
|
Chris@76
|
2134 global $boarddir, $sourcedir, $settings, $txt, $modSettings;
|
Chris@76
|
2135
|
Chris@76
|
2136 @set_time_limit(600);
|
Chris@76
|
2137 $file = strtr($file, array("\r" => ''));
|
Chris@76
|
2138
|
Chris@76
|
2139 $working_file = null;
|
Chris@76
|
2140 $working_search = null;
|
Chris@76
|
2141 $working_data = '';
|
Chris@76
|
2142 $replace_with = null;
|
Chris@76
|
2143
|
Chris@76
|
2144 $actions = array();
|
Chris@76
|
2145 $everything_found = true;
|
Chris@76
|
2146
|
Chris@76
|
2147 // This holds all the template changes in the standard mod file.
|
Chris@76
|
2148 $template_changes = array();
|
Chris@76
|
2149 // This is just the temporary file.
|
Chris@76
|
2150 $temp_file = $file;
|
Chris@76
|
2151 // This holds the actual changes on a step counter basis.
|
Chris@76
|
2152 $temp_changes = array();
|
Chris@76
|
2153 $counter = 0;
|
Chris@76
|
2154 $step_counter = 0;
|
Chris@76
|
2155
|
Chris@76
|
2156 // Before we do *anything*, let's build a list of what we're editing, as it's going to be used for other theme edits.
|
Chris@76
|
2157 while (preg_match('~<(edit file|file|search|search for|add|add after|replace|add before|add above|above|before)>\n(.*?)\n</\\1>~is', $temp_file, $code_match) != 0)
|
Chris@76
|
2158 {
|
Chris@76
|
2159 $counter++;
|
Chris@76
|
2160
|
Chris@76
|
2161 // Get rid of the old stuff.
|
Chris@76
|
2162 $temp_file = substr_replace($temp_file, '', strpos($temp_file, $code_match[0]), strlen($code_match[0]));
|
Chris@76
|
2163
|
Chris@76
|
2164 // No interest to us?
|
Chris@76
|
2165 if ($code_match[1] != 'edit file' && $code_match[1] != 'file')
|
Chris@76
|
2166 {
|
Chris@76
|
2167 // It's a step, let's add that to the current steps.
|
Chris@76
|
2168 if (isset($temp_changes[$step_counter]))
|
Chris@76
|
2169 $temp_changes[$step_counter]['changes'][] = $code_match[0];
|
Chris@76
|
2170 continue;
|
Chris@76
|
2171 }
|
Chris@76
|
2172
|
Chris@76
|
2173 // We've found a new edit - let's make ourself heard, kind of.
|
Chris@76
|
2174 $step_counter = $counter;
|
Chris@76
|
2175 $temp_changes[$step_counter] = array(
|
Chris@76
|
2176 'title' => $code_match[0],
|
Chris@76
|
2177 'changes' => array(),
|
Chris@76
|
2178 );
|
Chris@76
|
2179
|
Chris@76
|
2180 $filename = parse_path($code_match[2]);
|
Chris@76
|
2181
|
Chris@76
|
2182 // Now, is this a template file, and if so, which?
|
Chris@76
|
2183 foreach ($theme_paths as $id => $theme)
|
Chris@76
|
2184 {
|
Chris@76
|
2185 // If this filename is relative, if so take a guess at what it should be.
|
Chris@76
|
2186 if (strpos($filename, 'Themes') === 0)
|
Chris@76
|
2187 $filename = $boarddir . '/' . $filename;
|
Chris@76
|
2188
|
Chris@76
|
2189 if (strpos($filename, $theme['theme_dir']) === 0)
|
Chris@76
|
2190 $template_changes[$id][$counter] = substr($filename, strlen($theme['theme_dir']) + 1);
|
Chris@76
|
2191 }
|
Chris@76
|
2192 }
|
Chris@76
|
2193
|
Chris@76
|
2194 // Anything above $counter must be for custom themes.
|
Chris@76
|
2195 $custom_template_begin = $counter;
|
Chris@76
|
2196 // Reference for what theme ID this action belongs to.
|
Chris@76
|
2197 $theme_id_ref = array();
|
Chris@76
|
2198
|
Chris@76
|
2199 // Now we know what templates we need to touch, cycle through each theme and work out what we need to edit.
|
Chris@76
|
2200 if (!empty($template_changes[1]))
|
Chris@76
|
2201 {
|
Chris@76
|
2202 foreach ($theme_paths as $id => $theme)
|
Chris@76
|
2203 {
|
Chris@76
|
2204 // Don't do default, it means nothing to me.
|
Chris@76
|
2205 if ($id == 1)
|
Chris@76
|
2206 continue;
|
Chris@76
|
2207
|
Chris@76
|
2208 // Now, for each file do we need to edit it?
|
Chris@76
|
2209 foreach ($template_changes[1] as $pos => $template_file)
|
Chris@76
|
2210 {
|
Chris@76
|
2211 // It does? Add it to the list darlin'.
|
Chris@76
|
2212 if (file_exists($theme['theme_dir'] . '/' . $template_file) && (!isset($template_changes[$id][$pos]) || !in_array($template_file, $template_changes[$id][$pos])))
|
Chris@76
|
2213 {
|
Chris@76
|
2214 // Actually add it to the mod file too, so we can see that it will work ;)
|
Chris@76
|
2215 if (!empty($temp_changes[$pos]['changes']))
|
Chris@76
|
2216 {
|
Chris@76
|
2217 $file .= "\n\n" . '<edit file>' . "\n" . $theme['theme_dir'] . '/' . $template_file . "\n" . '</edit file>' . "\n\n" . implode("\n\n", $temp_changes[$pos]['changes']);
|
Chris@76
|
2218 $theme_id_ref[$counter] = $id;
|
Chris@76
|
2219 $counter += 1 + count($temp_changes[$pos]['changes']);
|
Chris@76
|
2220 }
|
Chris@76
|
2221 }
|
Chris@76
|
2222 }
|
Chris@76
|
2223 }
|
Chris@76
|
2224 }
|
Chris@76
|
2225
|
Chris@76
|
2226 $counter = 0;
|
Chris@76
|
2227 $is_custom = 0;
|
Chris@76
|
2228 while (preg_match('~<(edit file|file|search|search for|add|add after|replace|add before|add above|above|before)>\n(.*?)\n</\\1>~is', $file, $code_match) != 0)
|
Chris@76
|
2229 {
|
Chris@76
|
2230 // This is for working out what we should be editing.
|
Chris@76
|
2231 $counter++;
|
Chris@76
|
2232
|
Chris@76
|
2233 // Edit a specific file.
|
Chris@76
|
2234 if ($code_match[1] == 'file' || $code_match[1] == 'edit file')
|
Chris@76
|
2235 {
|
Chris@76
|
2236 // Backup the old file.
|
Chris@76
|
2237 if ($working_file !== null)
|
Chris@76
|
2238 {
|
Chris@76
|
2239 package_chmod($working_file);
|
Chris@76
|
2240
|
Chris@76
|
2241 // Don't even dare.
|
Chris@76
|
2242 if (basename($working_file) == 'Settings_bak.php')
|
Chris@76
|
2243 continue;
|
Chris@76
|
2244
|
Chris@76
|
2245 if (!is_writable($working_file))
|
Chris@76
|
2246 $actions[] = array(
|
Chris@76
|
2247 'type' => 'chmod',
|
Chris@76
|
2248 'filename' => $working_file
|
Chris@76
|
2249 );
|
Chris@76
|
2250
|
Chris@76
|
2251 if (!$testing && !empty($modSettings['package_make_backups']) && file_exists($working_file))
|
Chris@76
|
2252 {
|
Chris@76
|
2253 if (basename($working_file) == 'Settings.php')
|
Chris@76
|
2254 @copy($working_file, dirname($working_file) . '/Settings_bak.php');
|
Chris@76
|
2255 else
|
Chris@76
|
2256 @copy($working_file, $working_file . '~');
|
Chris@76
|
2257 }
|
Chris@76
|
2258
|
Chris@76
|
2259 package_put_contents($working_file, $working_data, $testing);
|
Chris@76
|
2260 }
|
Chris@76
|
2261
|
Chris@76
|
2262 if ($working_file !== null)
|
Chris@76
|
2263 $actions[] = array(
|
Chris@76
|
2264 'type' => 'saved',
|
Chris@76
|
2265 'filename' => $working_file,
|
Chris@76
|
2266 'is_custom' => $is_custom,
|
Chris@76
|
2267 );
|
Chris@76
|
2268
|
Chris@76
|
2269 // Is this "now working on" file a theme specific one?
|
Chris@76
|
2270 $is_custom = isset($theme_id_ref[$counter - 1]) ? $theme_id_ref[$counter - 1] : 0;
|
Chris@76
|
2271
|
Chris@76
|
2272 // Make sure the file exists!
|
Chris@76
|
2273 $working_file = parse_path($code_match[2]);
|
Chris@76
|
2274
|
Chris@76
|
2275 if ($working_file[0] != '/' && $working_file[1] != ':')
|
Chris@76
|
2276 {
|
Chris@76
|
2277 trigger_error('parseBoardMod(): The filename \'' . $working_file . '\' is not a full path!', E_USER_WARNING);
|
Chris@76
|
2278
|
Chris@76
|
2279 $working_file = $boarddir . '/' . $working_file;
|
Chris@76
|
2280 }
|
Chris@76
|
2281
|
Chris@76
|
2282 if (!file_exists($working_file))
|
Chris@76
|
2283 {
|
Chris@76
|
2284 $places_to_check = array($boarddir, $sourcedir, $settings['default_theme_dir'], $settings['default_theme_dir'] . '/languages');
|
Chris@76
|
2285
|
Chris@76
|
2286 foreach ($places_to_check as $place)
|
Chris@76
|
2287 if (file_exists($place . '/' . $working_file))
|
Chris@76
|
2288 {
|
Chris@76
|
2289 $working_file = $place . '/' . $working_file;
|
Chris@76
|
2290 break;
|
Chris@76
|
2291 }
|
Chris@76
|
2292 }
|
Chris@76
|
2293
|
Chris@76
|
2294 if (file_exists($working_file))
|
Chris@76
|
2295 {
|
Chris@76
|
2296 // Load the new file.
|
Chris@76
|
2297 $working_data = str_replace("\r", '', package_get_contents($working_file));
|
Chris@76
|
2298
|
Chris@76
|
2299 $actions[] = array(
|
Chris@76
|
2300 'type' => 'opened',
|
Chris@76
|
2301 'filename' => $working_file
|
Chris@76
|
2302 );
|
Chris@76
|
2303 }
|
Chris@76
|
2304 else
|
Chris@76
|
2305 {
|
Chris@76
|
2306 $actions[] = array(
|
Chris@76
|
2307 'type' => 'missing',
|
Chris@76
|
2308 'filename' => $working_file
|
Chris@76
|
2309 );
|
Chris@76
|
2310
|
Chris@76
|
2311 $working_file = null;
|
Chris@76
|
2312 $everything_found = false;
|
Chris@76
|
2313 }
|
Chris@76
|
2314
|
Chris@76
|
2315 // Can't be searching for something...
|
Chris@76
|
2316 $working_search = null;
|
Chris@76
|
2317 }
|
Chris@76
|
2318 // Search for a specific string.
|
Chris@76
|
2319 elseif (($code_match[1] == 'search' || $code_match[1] == 'search for') && $working_file !== null)
|
Chris@76
|
2320 {
|
Chris@76
|
2321 if ($working_search !== null)
|
Chris@76
|
2322 {
|
Chris@76
|
2323 $actions[] = array(
|
Chris@76
|
2324 'type' => 'error',
|
Chris@76
|
2325 'filename' => $working_file
|
Chris@76
|
2326 );
|
Chris@76
|
2327
|
Chris@76
|
2328 $everything_found = false;
|
Chris@76
|
2329 }
|
Chris@76
|
2330
|
Chris@76
|
2331 $working_search = $code_match[2];
|
Chris@76
|
2332 }
|
Chris@76
|
2333 // Must've already loaded a search string.
|
Chris@76
|
2334 elseif ($working_search !== null)
|
Chris@76
|
2335 {
|
Chris@76
|
2336 // This is the base string....
|
Chris@76
|
2337 $replace_with = $code_match[2];
|
Chris@76
|
2338
|
Chris@76
|
2339 // Add this afterward...
|
Chris@76
|
2340 if ($code_match[1] == 'add' || $code_match[1] == 'add after')
|
Chris@76
|
2341 $replace_with = $working_search . "\n" . $replace_with;
|
Chris@76
|
2342 // Add this beforehand.
|
Chris@76
|
2343 elseif ($code_match[1] == 'before' || $code_match[1] == 'add before' || $code_match[1] == 'above' || $code_match[1] == 'add above')
|
Chris@76
|
2344 $replace_with .= "\n" . $working_search;
|
Chris@76
|
2345 // Otherwise.. replace with $replace_with ;).
|
Chris@76
|
2346 }
|
Chris@76
|
2347
|
Chris@76
|
2348 // If we have a search string, replace string, and open file..
|
Chris@76
|
2349 if ($working_search !== null && $replace_with !== null && $working_file !== null)
|
Chris@76
|
2350 {
|
Chris@76
|
2351 // Make sure it's somewhere in the string.
|
Chris@76
|
2352 if ($undo)
|
Chris@76
|
2353 {
|
Chris@76
|
2354 $temp = $replace_with;
|
Chris@76
|
2355 $replace_with = $working_search;
|
Chris@76
|
2356 $working_search = $temp;
|
Chris@76
|
2357 }
|
Chris@76
|
2358
|
Chris@76
|
2359 if (strpos($working_data, $working_search) !== false)
|
Chris@76
|
2360 {
|
Chris@76
|
2361 $working_data = str_replace($working_search, $replace_with, $working_data);
|
Chris@76
|
2362
|
Chris@76
|
2363 $actions[] = array(
|
Chris@76
|
2364 'type' => 'replace',
|
Chris@76
|
2365 'filename' => $working_file,
|
Chris@76
|
2366 'search' => $working_search,
|
Chris@76
|
2367 'replace' => $replace_with,
|
Chris@76
|
2368 'search_original' => $working_search,
|
Chris@76
|
2369 'replace_original' => $replace_with,
|
Chris@76
|
2370 'position' => $code_match[1] == 'replace' ? 'replace' : ($code_match[1] == 'add' || $code_match[1] == 'add after' ? 'before' : 'after'),
|
Chris@76
|
2371 'is_custom' => $is_custom,
|
Chris@76
|
2372 'failed' => false,
|
Chris@76
|
2373 );
|
Chris@76
|
2374 }
|
Chris@76
|
2375 // It wasn't found!
|
Chris@76
|
2376 else
|
Chris@76
|
2377 {
|
Chris@76
|
2378 $actions[] = array(
|
Chris@76
|
2379 'type' => 'failure',
|
Chris@76
|
2380 'filename' => $working_file,
|
Chris@76
|
2381 'search' => $working_search,
|
Chris@76
|
2382 'is_custom' => $is_custom,
|
Chris@76
|
2383 'search_original' => $working_search,
|
Chris@76
|
2384 'replace_original' => $replace_with,
|
Chris@76
|
2385 'position' => $code_match[1] == 'replace' ? 'replace' : ($code_match[1] == 'add' || $code_match[1] == 'add after' ? 'before' : 'after'),
|
Chris@76
|
2386 'is_custom' => $is_custom,
|
Chris@76
|
2387 'failed' => true,
|
Chris@76
|
2388 );
|
Chris@76
|
2389
|
Chris@76
|
2390 $everything_found = false;
|
Chris@76
|
2391 }
|
Chris@76
|
2392
|
Chris@76
|
2393 // These don't hold any meaning now.
|
Chris@76
|
2394 $working_search = null;
|
Chris@76
|
2395 $replace_with = null;
|
Chris@76
|
2396 }
|
Chris@76
|
2397
|
Chris@76
|
2398 // Get rid of the old tag.
|
Chris@76
|
2399 $file = substr_replace($file, '', strpos($file, $code_match[0]), strlen($code_match[0]));
|
Chris@76
|
2400 }
|
Chris@76
|
2401
|
Chris@76
|
2402 // Backup the old file.
|
Chris@76
|
2403 if ($working_file !== null)
|
Chris@76
|
2404 {
|
Chris@76
|
2405 package_chmod($working_file);
|
Chris@76
|
2406
|
Chris@76
|
2407 if (!is_writable($working_file))
|
Chris@76
|
2408 $actions[] = array(
|
Chris@76
|
2409 'type' => 'chmod',
|
Chris@76
|
2410 'filename' => $working_file
|
Chris@76
|
2411 );
|
Chris@76
|
2412
|
Chris@76
|
2413 if (!$testing && !empty($modSettings['package_make_backups']) && file_exists($working_file))
|
Chris@76
|
2414 {
|
Chris@76
|
2415 if (basename($working_file) == 'Settings.php')
|
Chris@76
|
2416 @copy($working_file, dirname($working_file) . '/Settings_bak.php');
|
Chris@76
|
2417 else
|
Chris@76
|
2418 @copy($working_file, $working_file . '~');
|
Chris@76
|
2419 }
|
Chris@76
|
2420
|
Chris@76
|
2421 package_put_contents($working_file, $working_data, $testing);
|
Chris@76
|
2422 }
|
Chris@76
|
2423
|
Chris@76
|
2424 if ($working_file !== null)
|
Chris@76
|
2425 $actions[] = array(
|
Chris@76
|
2426 'type' => 'saved',
|
Chris@76
|
2427 'filename' => $working_file,
|
Chris@76
|
2428 'is_custom' => $is_custom,
|
Chris@76
|
2429 );
|
Chris@76
|
2430
|
Chris@76
|
2431 $actions[] = array(
|
Chris@76
|
2432 'type' => 'result',
|
Chris@76
|
2433 'status' => $everything_found
|
Chris@76
|
2434 );
|
Chris@76
|
2435
|
Chris@76
|
2436 return $actions;
|
Chris@76
|
2437 }
|
Chris@76
|
2438
|
Chris@76
|
2439 function package_get_contents($filename)
|
Chris@76
|
2440 {
|
Chris@76
|
2441 global $package_cache, $modSettings;
|
Chris@76
|
2442
|
Chris@76
|
2443 if (!isset($package_cache))
|
Chris@76
|
2444 {
|
Chris@76
|
2445 // Windows doesn't seem to care about the memory_limit.
|
Chris@76
|
2446 if (!empty($modSettings['package_disable_cache']) || ini_set('memory_limit', '128M') !== false || strpos(strtolower(PHP_OS), 'win') !== false)
|
Chris@76
|
2447 $package_cache = array();
|
Chris@76
|
2448 else
|
Chris@76
|
2449 $package_cache = false;
|
Chris@76
|
2450 }
|
Chris@76
|
2451
|
Chris@76
|
2452 if (strpos($filename, 'Packages/') !== false || $package_cache === false || !isset($package_cache[$filename]))
|
Chris@76
|
2453 return file_get_contents($filename);
|
Chris@76
|
2454 else
|
Chris@76
|
2455 return $package_cache[$filename];
|
Chris@76
|
2456 }
|
Chris@76
|
2457
|
Chris@76
|
2458 function package_put_contents($filename, $data, $testing = false)
|
Chris@76
|
2459 {
|
Chris@76
|
2460 global $package_ftp, $package_cache, $modSettings;
|
Chris@76
|
2461 static $text_filetypes = array('php', 'txt', '.js', 'css', 'vbs', 'tml', 'htm');
|
Chris@76
|
2462
|
Chris@76
|
2463 if (!isset($package_cache))
|
Chris@76
|
2464 {
|
Chris@76
|
2465 // Try to increase the memory limit - we don't want to run out of ram!
|
Chris@76
|
2466 if (!empty($modSettings['package_disable_cache']) || ini_set('memory_limit', '128M') !== false || strpos(strtolower(PHP_OS), 'win') !== false)
|
Chris@76
|
2467 $package_cache = array();
|
Chris@76
|
2468 else
|
Chris@76
|
2469 $package_cache = false;
|
Chris@76
|
2470 }
|
Chris@76
|
2471
|
Chris@76
|
2472 if (isset($package_ftp))
|
Chris@76
|
2473 $ftp_file = strtr($filename, array($_SESSION['pack_ftp']['root'] => ''));
|
Chris@76
|
2474
|
Chris@76
|
2475 if (!file_exists($filename) && isset($package_ftp))
|
Chris@76
|
2476 $package_ftp->create_file($ftp_file);
|
Chris@76
|
2477 elseif (!file_exists($filename))
|
Chris@76
|
2478 @touch($filename);
|
Chris@76
|
2479
|
Chris@76
|
2480 package_chmod($filename);
|
Chris@76
|
2481
|
Chris@76
|
2482 if (!$testing && (strpos($filename, 'Packages/') !== false || $package_cache === false))
|
Chris@76
|
2483 {
|
Chris@76
|
2484 $fp = @fopen($filename, in_array(substr($filename, -3), $text_filetypes) ? 'w' : 'wb');
|
Chris@76
|
2485
|
Chris@76
|
2486 // We should show an error message or attempt a rollback, no?
|
Chris@76
|
2487 if (!$fp)
|
Chris@76
|
2488 return false;
|
Chris@76
|
2489
|
Chris@76
|
2490 fwrite($fp, $data);
|
Chris@76
|
2491 fclose($fp);
|
Chris@76
|
2492 }
|
Chris@76
|
2493 elseif (strpos($filename, 'Packages/') !== false || $package_cache === false)
|
Chris@76
|
2494 return strlen($data);
|
Chris@76
|
2495 else
|
Chris@76
|
2496 {
|
Chris@76
|
2497 $package_cache[$filename] = $data;
|
Chris@76
|
2498
|
Chris@76
|
2499 // Permission denied, eh?
|
Chris@76
|
2500 $fp = @fopen($filename, 'r+');
|
Chris@76
|
2501 if (!$fp)
|
Chris@76
|
2502 return false;
|
Chris@76
|
2503 fclose($fp);
|
Chris@76
|
2504 }
|
Chris@76
|
2505
|
Chris@76
|
2506 return strlen($data);
|
Chris@76
|
2507 }
|
Chris@76
|
2508
|
Chris@76
|
2509 function package_flush_cache($trash = false)
|
Chris@76
|
2510 {
|
Chris@76
|
2511 global $package_ftp, $package_cache;
|
Chris@76
|
2512 static $text_filetypes = array('php', 'txt', '.js', 'css', 'vbs', 'tml', 'htm');
|
Chris@76
|
2513
|
Chris@76
|
2514 if (empty($package_cache))
|
Chris@76
|
2515 return;
|
Chris@76
|
2516
|
Chris@76
|
2517 // First, let's check permissions!
|
Chris@76
|
2518 foreach ($package_cache as $filename => $data)
|
Chris@76
|
2519 {
|
Chris@76
|
2520 if (isset($package_ftp))
|
Chris@76
|
2521 $ftp_file = strtr($filename, array($_SESSION['pack_ftp']['root'] => ''));
|
Chris@76
|
2522
|
Chris@76
|
2523 if (!file_exists($filename) && isset($package_ftp))
|
Chris@76
|
2524 $package_ftp->create_file($ftp_file);
|
Chris@76
|
2525 elseif (!file_exists($filename))
|
Chris@76
|
2526 @touch($filename);
|
Chris@76
|
2527
|
Chris@76
|
2528 package_chmod($filename);
|
Chris@76
|
2529
|
Chris@76
|
2530 $fp = fopen($filename, 'r+');
|
Chris@76
|
2531 if (!$fp && !$trash)
|
Chris@76
|
2532 {
|
Chris@76
|
2533 // We should have package_chmod()'d them before, no?!
|
Chris@76
|
2534 trigger_error('package_flush_cache(): some files are still not writable', E_USER_WARNING);
|
Chris@76
|
2535 return;
|
Chris@76
|
2536 }
|
Chris@76
|
2537 fclose($fp);
|
Chris@76
|
2538 }
|
Chris@76
|
2539
|
Chris@76
|
2540 if ($trash)
|
Chris@76
|
2541 {
|
Chris@76
|
2542 $package_cache = array();
|
Chris@76
|
2543 return;
|
Chris@76
|
2544 }
|
Chris@76
|
2545
|
Chris@76
|
2546 foreach ($package_cache as $filename => $data)
|
Chris@76
|
2547 {
|
Chris@76
|
2548 $fp = fopen($filename, in_array(substr($filename, -3), $text_filetypes) ? 'w' : 'wb');
|
Chris@76
|
2549 fwrite($fp, $data);
|
Chris@76
|
2550 fclose($fp);
|
Chris@76
|
2551 }
|
Chris@76
|
2552
|
Chris@76
|
2553 $package_cache = array();
|
Chris@76
|
2554 }
|
Chris@76
|
2555
|
Chris@76
|
2556 // Try to make a file writable. Return true if it worked, false if it didn't.
|
Chris@76
|
2557 function package_chmod($filename, $perm_state = 'writable', $track_change = false)
|
Chris@76
|
2558 {
|
Chris@76
|
2559 global $package_ftp;
|
Chris@76
|
2560
|
Chris@76
|
2561 if (file_exists($filename) && is_writable($filename) && $perm_state == 'writable')
|
Chris@76
|
2562 return true;
|
Chris@76
|
2563
|
Chris@76
|
2564 // Start off checking without FTP.
|
Chris@76
|
2565 if (!isset($package_ftp) || $package_ftp === false)
|
Chris@76
|
2566 {
|
Chris@76
|
2567 for ($i = 0; $i < 2; $i++)
|
Chris@76
|
2568 {
|
Chris@76
|
2569 $chmod_file = $filename;
|
Chris@76
|
2570
|
Chris@76
|
2571 // Start off with a less agressive test.
|
Chris@76
|
2572 if ($i == 0)
|
Chris@76
|
2573 {
|
Chris@76
|
2574 // If this file doesn't exist, then we actually want to look at whatever parent directory does.
|
Chris@76
|
2575 $subTraverseLimit = 2;
|
Chris@76
|
2576 while (!file_exists($chmod_file) && $subTraverseLimit)
|
Chris@76
|
2577 {
|
Chris@76
|
2578 $chmod_file = dirname($chmod_file);
|
Chris@76
|
2579 $subTraverseLimit--;
|
Chris@76
|
2580 }
|
Chris@76
|
2581
|
Chris@76
|
2582 // Keep track of the writable status here.
|
Chris@76
|
2583 $file_permissions = @fileperms($chmod_file);
|
Chris@76
|
2584 }
|
Chris@76
|
2585 else
|
Chris@76
|
2586 {
|
Chris@76
|
2587 // This looks odd, but it's an attempt to work around PHP suExec.
|
Chris@76
|
2588 if (!file_exists($chmod_file) && $perm_state == 'writable')
|
Chris@76
|
2589 {
|
Chris@76
|
2590 $file_permissions = @fileperms(dirname($chmod_file));
|
Chris@76
|
2591
|
Chris@76
|
2592 mktree(dirname($chmod_file), 0755);
|
Chris@76
|
2593 @touch($chmod_file);
|
Chris@76
|
2594 @chmod($chmod_file, 0755);
|
Chris@76
|
2595 }
|
Chris@76
|
2596 else
|
Chris@76
|
2597 $file_permissions = @fileperms($chmod_file);
|
Chris@76
|
2598 }
|
Chris@76
|
2599
|
Chris@76
|
2600 // This looks odd, but it's another attempt to work around PHP suExec.
|
Chris@76
|
2601 if ($perm_state != 'writable')
|
Chris@76
|
2602 @chmod($chmod_file, $perm_state == 'execute' ? 0755 : 0644);
|
Chris@76
|
2603 else
|
Chris@76
|
2604 {
|
Chris@76
|
2605 if (!@is_writable($chmod_file))
|
Chris@76
|
2606 @chmod($chmod_file, 0755);
|
Chris@76
|
2607 if (!@is_writable($chmod_file))
|
Chris@76
|
2608 @chmod($chmod_file, 0777);
|
Chris@76
|
2609 if (!@is_writable(dirname($chmod_file)))
|
Chris@76
|
2610 @chmod($chmod_file, 0755);
|
Chris@76
|
2611 if (!@is_writable(dirname($chmod_file)))
|
Chris@76
|
2612 @chmod($chmod_file, 0777);
|
Chris@76
|
2613 }
|
Chris@76
|
2614
|
Chris@76
|
2615 // The ultimate writable test.
|
Chris@76
|
2616 if ($perm_state == 'writable')
|
Chris@76
|
2617 {
|
Chris@76
|
2618 $fp = is_dir($chmod_file) ? @opendir($chmod_file) : @fopen($chmod_file, 'rb');
|
Chris@76
|
2619 if (@is_writable($chmod_file) && $fp)
|
Chris@76
|
2620 {
|
Chris@76
|
2621 if (!is_dir($chmod_file))
|
Chris@76
|
2622 fclose($fp);
|
Chris@76
|
2623 else
|
Chris@76
|
2624 closedir($fp);
|
Chris@76
|
2625
|
Chris@76
|
2626 // It worked!
|
Chris@76
|
2627 if ($track_change)
|
Chris@76
|
2628 $_SESSION['pack_ftp']['original_perms'][$chmod_file] = $file_permissions;
|
Chris@76
|
2629
|
Chris@76
|
2630 return true;
|
Chris@76
|
2631 }
|
Chris@76
|
2632 }
|
Chris@76
|
2633 elseif ($perm_state != 'writable' && isset($_SESSION['pack_ftp']['original_perms'][$chmod_file]))
|
Chris@76
|
2634 unset($_SESSION['pack_ftp']['original_perms'][$chmod_file]);
|
Chris@76
|
2635 }
|
Chris@76
|
2636
|
Chris@76
|
2637 // If we're here we're a failure.
|
Chris@76
|
2638 return false;
|
Chris@76
|
2639 }
|
Chris@76
|
2640 // Otherwise we do have FTP?
|
Chris@76
|
2641 elseif ($package_ftp !== false && !empty($_SESSION['pack_ftp']))
|
Chris@76
|
2642 {
|
Chris@76
|
2643 $ftp_file = strtr($filename, array($_SESSION['pack_ftp']['root'] => ''));
|
Chris@76
|
2644
|
Chris@76
|
2645 // This looks odd, but it's an attempt to work around PHP suExec.
|
Chris@76
|
2646 if (!file_exists($filename) && $perm_state == 'writable')
|
Chris@76
|
2647 {
|
Chris@76
|
2648 $file_permissions = @fileperms(dirname($filename));
|
Chris@76
|
2649
|
Chris@76
|
2650 mktree(dirname($filename), 0755);
|
Chris@76
|
2651 $package_ftp->create_file($ftp_file);
|
Chris@76
|
2652 $package_ftp->chmod($ftp_file, 0755);
|
Chris@76
|
2653 }
|
Chris@76
|
2654 else
|
Chris@76
|
2655 $file_permissions = @fileperms($filename);
|
Chris@76
|
2656
|
Chris@76
|
2657 if ($perm_state != 'writable')
|
Chris@76
|
2658 {
|
Chris@76
|
2659 $package_ftp->chmod($ftp_file, $perm_state == 'execute' ? 0755 : 0644);
|
Chris@76
|
2660 }
|
Chris@76
|
2661 else
|
Chris@76
|
2662 {
|
Chris@76
|
2663 if (!@is_writable($filename))
|
Chris@76
|
2664 $package_ftp->chmod($ftp_file, 0777);
|
Chris@76
|
2665 if (!@is_writable(dirname($filename)))
|
Chris@76
|
2666 $package_ftp->chmod(dirname($ftp_file), 0777);
|
Chris@76
|
2667 }
|
Chris@76
|
2668
|
Chris@76
|
2669 if (@is_writable($filename))
|
Chris@76
|
2670 {
|
Chris@76
|
2671 if ($track_change)
|
Chris@76
|
2672 $_SESSION['pack_ftp']['original_perms'][$filename] = $file_permissions;
|
Chris@76
|
2673
|
Chris@76
|
2674 return true;
|
Chris@76
|
2675 }
|
Chris@76
|
2676 elseif ($perm_state != 'writable' && isset($_SESSION['pack_ftp']['original_perms'][$filename]))
|
Chris@76
|
2677 unset($_SESSION['pack_ftp']['original_perms'][$filename]);
|
Chris@76
|
2678 }
|
Chris@76
|
2679
|
Chris@76
|
2680 // Oh dear, we failed if we get here.
|
Chris@76
|
2681 return false;
|
Chris@76
|
2682 }
|
Chris@76
|
2683
|
Chris@76
|
2684 function package_crypt($pass)
|
Chris@76
|
2685 {
|
Chris@76
|
2686 $n = strlen($pass);
|
Chris@76
|
2687
|
Chris@76
|
2688 $salt = session_id();
|
Chris@76
|
2689 while (strlen($salt) < $n)
|
Chris@76
|
2690 $salt .= session_id();
|
Chris@76
|
2691
|
Chris@76
|
2692 for ($i = 0; $i < $n; $i++)
|
Chris@76
|
2693 $pass{$i} = chr(ord($pass{$i}) ^ (ord($salt{$i}) - 32));
|
Chris@76
|
2694
|
Chris@76
|
2695 return $pass;
|
Chris@76
|
2696 }
|
Chris@76
|
2697
|
Chris@76
|
2698 function package_create_backup($id = 'backup')
|
Chris@76
|
2699 {
|
Chris@76
|
2700 global $sourcedir, $boarddir, $smcFunc;
|
Chris@76
|
2701
|
Chris@76
|
2702 $files = array();
|
Chris@76
|
2703
|
Chris@76
|
2704 $base_files = array('index.php', 'SSI.php', 'agreement.txt', 'ssi_examples.php', 'ssi_examples.shtml');
|
Chris@76
|
2705 foreach ($base_files as $file)
|
Chris@76
|
2706 {
|
Chris@76
|
2707 if (file_exists($boarddir . '/' . $file))
|
Chris@76
|
2708 $files[realpath($boarddir . '/' . $file)] = array(
|
Chris@76
|
2709 empty($_REQUEST['use_full_paths']) ? $file : $boarddir . '/' . $file,
|
Chris@76
|
2710 stat($boarddir . '/' . $file)
|
Chris@76
|
2711 );
|
Chris@76
|
2712 }
|
Chris@76
|
2713
|
Chris@76
|
2714 $dirs = array(
|
Chris@76
|
2715 $sourcedir => empty($_REQUEST['use_full_paths']) ? 'Sources/' : strtr($sourcedir . '/', '\\', '/')
|
Chris@76
|
2716 );
|
Chris@76
|
2717
|
Chris@76
|
2718 $request = $smcFunc['db_query']('', '
|
Chris@76
|
2719 SELECT value
|
Chris@76
|
2720 FROM {db_prefix}themes
|
Chris@76
|
2721 WHERE id_member = {int:no_member}
|
Chris@76
|
2722 AND variable = {string:theme_dir}',
|
Chris@76
|
2723 array(
|
Chris@76
|
2724 'no_member' => 0,
|
Chris@76
|
2725 'theme_dir' => 'theme_dir',
|
Chris@76
|
2726 )
|
Chris@76
|
2727 );
|
Chris@76
|
2728 while ($row = $smcFunc['db_fetch_assoc']($request))
|
Chris@76
|
2729 $dirs[$row['value']] = empty($_REQUEST['use_full_paths']) ? 'Themes/' . basename($row['value']) . '/' : strtr($row['value'] . '/', '\\', '/');
|
Chris@76
|
2730 $smcFunc['db_free_result']($request);
|
Chris@76
|
2731
|
Chris@76
|
2732 while (!empty($dirs))
|
Chris@76
|
2733 {
|
Chris@76
|
2734 list ($dir, $dest) = each($dirs);
|
Chris@76
|
2735 unset($dirs[$dir]);
|
Chris@76
|
2736
|
Chris@76
|
2737 $listing = @dir($dir);
|
Chris@76
|
2738 if (!$listing)
|
Chris@76
|
2739 continue;
|
Chris@76
|
2740 while ($entry = $listing->read())
|
Chris@76
|
2741 {
|
Chris@76
|
2742 if (preg_match('~^(\.{1,2}|CVS|backup.*|help|images|.*\~)$~', $entry) != 0)
|
Chris@76
|
2743 continue;
|
Chris@76
|
2744
|
Chris@76
|
2745 $filepath = realpath($dir . '/' . $entry);
|
Chris@76
|
2746 if (isset($files[$filepath]))
|
Chris@76
|
2747 continue;
|
Chris@76
|
2748
|
Chris@76
|
2749 $stat = stat($dir . '/' . $entry);
|
Chris@76
|
2750 if ($stat['mode'] & 040000)
|
Chris@76
|
2751 {
|
Chris@76
|
2752 $files[$filepath] = array($dest . $entry . '/', $stat);
|
Chris@76
|
2753 $dirs[$dir . '/' . $entry] = $dest . $entry . '/';
|
Chris@76
|
2754 }
|
Chris@76
|
2755 else
|
Chris@76
|
2756 $files[$filepath] = array($dest . $entry, $stat);
|
Chris@76
|
2757 }
|
Chris@76
|
2758 $listing->close();
|
Chris@76
|
2759 }
|
Chris@76
|
2760
|
Chris@76
|
2761 if (!file_exists($boarddir . '/Packages/backups'))
|
Chris@76
|
2762 mktree($boarddir . '/Packages/backups', 0777);
|
Chris@76
|
2763 if (!is_writable($boarddir . '/Packages/backups'))
|
Chris@76
|
2764 package_chmod($boarddir . '/Packages/backups');
|
Chris@76
|
2765 $output_file = $boarddir . '/Packages/backups/' . strftime('%Y-%m-%d_') . preg_replace('~[$\\\\/:<>|?*"\']~', '', $id);
|
Chris@76
|
2766 $output_ext = '.tar' . (function_exists('gzopen') ? '.gz' : '');
|
Chris@76
|
2767
|
Chris@76
|
2768 if (file_exists($output_file . $output_ext))
|
Chris@76
|
2769 {
|
Chris@76
|
2770 $i = 2;
|
Chris@76
|
2771 while (file_exists($output_file . '_' . $i . $output_ext))
|
Chris@76
|
2772 $i++;
|
Chris@76
|
2773 $output_file = $output_file . '_' . $i . $output_ext;
|
Chris@76
|
2774 }
|
Chris@76
|
2775 else
|
Chris@76
|
2776 $output_file .= $output_ext;
|
Chris@76
|
2777
|
Chris@76
|
2778 @set_time_limit(300);
|
Chris@76
|
2779 if (function_exists('apache_reset_timeout'))
|
Chris@76
|
2780 @apache_reset_timeout();
|
Chris@76
|
2781
|
Chris@76
|
2782 if (function_exists('gzopen'))
|
Chris@76
|
2783 {
|
Chris@76
|
2784 $fwrite = 'gzwrite';
|
Chris@76
|
2785 $fclose = 'gzclose';
|
Chris@76
|
2786 $output = gzopen($output_file, 'wb');
|
Chris@76
|
2787 }
|
Chris@76
|
2788 else
|
Chris@76
|
2789 {
|
Chris@76
|
2790 $fwrite = 'fwrite';
|
Chris@76
|
2791 $fclose = 'fclose';
|
Chris@76
|
2792 $output = fopen($output_file, 'wb');
|
Chris@76
|
2793 }
|
Chris@76
|
2794
|
Chris@76
|
2795 foreach ($files as $real_file => $file)
|
Chris@76
|
2796 {
|
Chris@76
|
2797 if (!file_exists($real_file))
|
Chris@76
|
2798 continue;
|
Chris@76
|
2799
|
Chris@76
|
2800 $stat = $file[1];
|
Chris@76
|
2801 if (substr($file[0], -1) == '/')
|
Chris@76
|
2802 $stat['size'] = 0;
|
Chris@76
|
2803
|
Chris@76
|
2804 $current = pack('a100a8a8a8a12a12a8a1a100a6a2a32a32a8a8a155a12', $file[0], decoct($stat['mode']), sprintf('%06d', decoct($stat['uid'])), sprintf('%06d', decoct($stat['gid'])), decoct($stat['size']), decoct($stat['mtime']), '', 0, '', '', '', '', '', '', '', '', '');
|
Chris@76
|
2805
|
Chris@76
|
2806 $checksum = 256;
|
Chris@76
|
2807 for ($i = 0; $i < 512; $i++)
|
Chris@76
|
2808 $checksum += ord($current{$i});
|
Chris@76
|
2809
|
Chris@76
|
2810 $fwrite($output, substr($current, 0, 148) . pack('a8', decoct($checksum)) . substr($current, 156, 511));
|
Chris@76
|
2811
|
Chris@76
|
2812 if ($stat['size'] == 0)
|
Chris@76
|
2813 continue;
|
Chris@76
|
2814
|
Chris@76
|
2815 $fp = fopen($real_file, 'rb');
|
Chris@76
|
2816 while (!feof($fp))
|
Chris@76
|
2817 $fwrite($output, fread($fp, 16384));
|
Chris@76
|
2818 fclose($fp);
|
Chris@76
|
2819
|
Chris@76
|
2820 $fwrite($output, pack('a' . (512 - $stat['size'] % 512), ''));
|
Chris@76
|
2821 }
|
Chris@76
|
2822
|
Chris@76
|
2823 $fwrite($output, pack('a1024', ''));
|
Chris@76
|
2824 $fclose($output);
|
Chris@76
|
2825 }
|
Chris@76
|
2826
|
Chris@76
|
2827 // Get the contents of a URL, irrespective of allow_url_fopen.
|
Chris@76
|
2828 function fetch_web_data($url, $post_data = '', $keep_alive = false, $redirection_level = 0)
|
Chris@76
|
2829 {
|
Chris@76
|
2830 global $webmaster_email;
|
Chris@76
|
2831 static $keep_alive_dom = null, $keep_alive_fp = null;
|
Chris@76
|
2832
|
Chris@76
|
2833 preg_match('~^(http|ftp)(s)?://([^/:]+)(:(\d+))?(.+)$~', $url, $match);
|
Chris@76
|
2834
|
Chris@76
|
2835 // An FTP url. We should try connecting and RETRieving it...
|
Chris@76
|
2836 if (empty($match[1]))
|
Chris@76
|
2837 return false;
|
Chris@76
|
2838 elseif ($match[1] == 'ftp')
|
Chris@76
|
2839 {
|
Chris@76
|
2840 // Include the file containing the ftp_connection class.
|
Chris@76
|
2841 loadClassFile('Class-Package.php');
|
Chris@76
|
2842
|
Chris@76
|
2843 // Establish a connection and attempt to enable passive mode.
|
Chris@76
|
2844 $ftp = new ftp_connection(($match[2] ? 'ssl://' : '') . $match[3], empty($match[5]) ? 21 : $match[5], 'anonymous', $webmaster_email);
|
Chris@76
|
2845 if ($ftp->error !== false || !$ftp->passive())
|
Chris@76
|
2846 return false;
|
Chris@76
|
2847
|
Chris@76
|
2848 // I want that one *points*!
|
Chris@76
|
2849 fwrite($ftp->connection, 'RETR ' . $match[6] . "\r\n");
|
Chris@76
|
2850
|
Chris@76
|
2851 // Since passive mode worked (or we would have returned already!) open the connection.
|
Chris@76
|
2852 $fp = @fsockopen($ftp->pasv['ip'], $ftp->pasv['port'], $err, $err, 5);
|
Chris@76
|
2853 if (!$fp)
|
Chris@76
|
2854 return false;
|
Chris@76
|
2855
|
Chris@76
|
2856 // The server should now say something in acknowledgement.
|
Chris@76
|
2857 $ftp->check_response(150);
|
Chris@76
|
2858
|
Chris@76
|
2859 $data = '';
|
Chris@76
|
2860 while (!feof($fp))
|
Chris@76
|
2861 $data .= fread($fp, 4096);
|
Chris@76
|
2862 fclose($fp);
|
Chris@76
|
2863
|
Chris@76
|
2864 // All done, right? Good.
|
Chris@76
|
2865 $ftp->check_response(226);
|
Chris@76
|
2866 $ftp->close();
|
Chris@76
|
2867 }
|
Chris@76
|
2868 // This is more likely; a standard HTTP URL.
|
Chris@76
|
2869 elseif (isset($match[1]) && $match[1] == 'http')
|
Chris@76
|
2870 {
|
Chris@76
|
2871 if ($keep_alive && $match[3] == $keep_alive_dom)
|
Chris@76
|
2872 $fp = $keep_alive_fp;
|
Chris@76
|
2873 if (empty($fp))
|
Chris@76
|
2874 {
|
Chris@76
|
2875 // Open the socket on the port we want...
|
Chris@76
|
2876 $fp = @fsockopen(($match[2] ? 'ssl://' : '') . $match[3], empty($match[5]) ? ($match[2] ? 443 : 80) : $match[5], $err, $err, 5);
|
Chris@76
|
2877 if (!$fp)
|
Chris@76
|
2878 return false;
|
Chris@76
|
2879 }
|
Chris@76
|
2880
|
Chris@76
|
2881 if ($keep_alive)
|
Chris@76
|
2882 {
|
Chris@76
|
2883 $keep_alive_dom = $match[3];
|
Chris@76
|
2884 $keep_alive_fp = $fp;
|
Chris@76
|
2885 }
|
Chris@76
|
2886
|
Chris@76
|
2887 // I want this, from there, and I'm not going to be bothering you for more (probably.)
|
Chris@76
|
2888 if (empty($post_data))
|
Chris@76
|
2889 {
|
Chris@76
|
2890 fwrite($fp, 'GET ' . $match[6] . ' HTTP/1.0' . "\r\n");
|
Chris@76
|
2891 fwrite($fp, 'Host: ' . $match[3] . (empty($match[5]) ? ($match[2] ? ':443' : '') : ':' . $match[5]) . "\r\n");
|
Chris@76
|
2892 fwrite($fp, 'User-Agent: PHP/SMF' . "\r\n");
|
Chris@76
|
2893 if ($keep_alive)
|
Chris@76
|
2894 fwrite($fp, 'Connection: Keep-Alive' . "\r\n\r\n");
|
Chris@76
|
2895 else
|
Chris@76
|
2896 fwrite($fp, 'Connection: close' . "\r\n\r\n");
|
Chris@76
|
2897 }
|
Chris@76
|
2898 else
|
Chris@76
|
2899 {
|
Chris@76
|
2900 fwrite($fp, 'POST ' . $match[6] . ' HTTP/1.0' . "\r\n");
|
Chris@76
|
2901 fwrite($fp, 'Host: ' . $match[3] . (empty($match[5]) ? ($match[2] ? ':443' : '') : ':' . $match[5]) . "\r\n");
|
Chris@76
|
2902 fwrite($fp, 'User-Agent: PHP/SMF' . "\r\n");
|
Chris@76
|
2903 if ($keep_alive)
|
Chris@76
|
2904 fwrite($fp, 'Connection: Keep-Alive' . "\r\n");
|
Chris@76
|
2905 else
|
Chris@76
|
2906 fwrite($fp, 'Connection: close' . "\r\n");
|
Chris@76
|
2907 fwrite($fp, 'Content-Type: application/x-www-form-urlencoded' . "\r\n");
|
Chris@76
|
2908 fwrite($fp, 'Content-Length: ' . strlen($post_data) . "\r\n\r\n");
|
Chris@76
|
2909 fwrite($fp, $post_data);
|
Chris@76
|
2910 }
|
Chris@76
|
2911
|
Chris@76
|
2912 $response = fgets($fp, 768);
|
Chris@76
|
2913
|
Chris@76
|
2914 // Redirect in case this location is permanently or temporarily moved.
|
Chris@76
|
2915 if ($redirection_level < 3 && preg_match('~^HTTP/\S+\s+30[127]~i', $response) === 1)
|
Chris@76
|
2916 {
|
Chris@76
|
2917 $header = '';
|
Chris@76
|
2918 $location = '';
|
Chris@76
|
2919 while (!feof($fp) && trim($header = fgets($fp, 4096)) != '')
|
Chris@76
|
2920 if (strpos($header, 'Location:') !== false)
|
Chris@76
|
2921 $location = trim(substr($header, strpos($header, ':') + 1));
|
Chris@76
|
2922
|
Chris@76
|
2923 if (empty($location))
|
Chris@76
|
2924 return false;
|
Chris@76
|
2925 else
|
Chris@76
|
2926 {
|
Chris@76
|
2927 if (!$keep_alive)
|
Chris@76
|
2928 fclose($fp);
|
Chris@76
|
2929 return fetch_web_data($location, $post_data, $keep_alive, $redirection_level + 1);
|
Chris@76
|
2930 }
|
Chris@76
|
2931 }
|
Chris@76
|
2932
|
Chris@76
|
2933 // Make sure we get a 200 OK.
|
Chris@76
|
2934 elseif (preg_match('~^HTTP/\S+\s+20[01]~i', $response) === 0)
|
Chris@76
|
2935 return false;
|
Chris@76
|
2936
|
Chris@76
|
2937 // Skip the headers...
|
Chris@76
|
2938 while (!feof($fp) && trim($header = fgets($fp, 4096)) != '')
|
Chris@76
|
2939 {
|
Chris@76
|
2940 if (preg_match('~content-length:\s*(\d+)~i', $header, $match) != 0)
|
Chris@76
|
2941 $content_length = $match[1];
|
Chris@76
|
2942 elseif (preg_match('~connection:\s*close~i', $header) != 0)
|
Chris@76
|
2943 {
|
Chris@76
|
2944 $keep_alive_dom = null;
|
Chris@76
|
2945 $keep_alive = false;
|
Chris@76
|
2946 }
|
Chris@76
|
2947
|
Chris@76
|
2948 continue;
|
Chris@76
|
2949 }
|
Chris@76
|
2950
|
Chris@76
|
2951 $data = '';
|
Chris@76
|
2952 if (isset($content_length))
|
Chris@76
|
2953 {
|
Chris@76
|
2954 while (!feof($fp) && strlen($data) < $content_length)
|
Chris@76
|
2955 $data .= fread($fp, $content_length - strlen($data));
|
Chris@76
|
2956 }
|
Chris@76
|
2957 else
|
Chris@76
|
2958 {
|
Chris@76
|
2959 while (!feof($fp))
|
Chris@76
|
2960 $data .= fread($fp, 4096);
|
Chris@76
|
2961 }
|
Chris@76
|
2962
|
Chris@76
|
2963 if (!$keep_alive)
|
Chris@76
|
2964 fclose($fp);
|
Chris@76
|
2965 }
|
Chris@76
|
2966 else
|
Chris@76
|
2967 {
|
Chris@76
|
2968 // Umm, this shouldn't happen?
|
Chris@76
|
2969 trigger_error('fetch_web_data(): Bad URL', E_USER_NOTICE);
|
Chris@76
|
2970 $data = false;
|
Chris@76
|
2971 }
|
Chris@76
|
2972
|
Chris@76
|
2973 return $data;
|
Chris@76
|
2974 }
|
Chris@76
|
2975
|
Chris@76
|
2976 // crc32 doesn't work as expected on 64-bit functions - make our own.
|
Chris@76
|
2977 // http://www.php.net/crc32#79567
|
Chris@76
|
2978 if (!function_exists('smf_crc32'))
|
Chris@76
|
2979 {
|
Chris@76
|
2980 function smf_crc32($number)
|
Chris@76
|
2981 {
|
Chris@76
|
2982 $crc = crc32($number);
|
Chris@76
|
2983
|
Chris@76
|
2984 if ($crc & 0x80000000)
|
Chris@76
|
2985 {
|
Chris@76
|
2986 $crc ^= 0xffffffff;
|
Chris@76
|
2987 $crc += 1;
|
Chris@76
|
2988 $crc = -$crc;
|
Chris@76
|
2989 }
|
Chris@76
|
2990
|
Chris@76
|
2991 return $crc;
|
Chris@76
|
2992 }
|
Chris@76
|
2993 }
|
Chris@76
|
2994
|
Chris@76
|
2995 ?> |