Mercurial > hg > vamp-website
comparison forum/Sources/PackageGet.php @ 76:e3e11437ecea website
Add forum code
author | Chris Cannam |
---|---|
date | Sun, 07 Jul 2013 11:25:48 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
75:72f59aa7e503 | 76:e3e11437ecea |
---|---|
1 <?php | |
2 | |
3 /** | |
4 * Simple Machines Forum (SMF) | |
5 * | |
6 * @package SMF | |
7 * @author Simple Machines http://www.simplemachines.org | |
8 * @copyright 2011 Simple Machines | |
9 * @license http://www.simplemachines.org/about/smf/license.php BSD | |
10 * | |
11 * @version 2.0 | |
12 */ | |
13 | |
14 if (!defined('SMF')) | |
15 die('Hacking attempt...'); | |
16 | |
17 /* // !!! | |
18 | |
19 void PackageGet() | |
20 // !!! | |
21 | |
22 void PackageGBrowse() | |
23 // !!! | |
24 | |
25 void PackageDownload() | |
26 // !!! | |
27 | |
28 void PackageUpload() | |
29 // !!! | |
30 | |
31 void PackageServerAdd() | |
32 // !!! | |
33 | |
34 void PackageServerRemove() | |
35 // !!! | |
36 */ | |
37 | |
38 // Browse the list of package servers, add servers... | |
39 function PackageGet() | |
40 { | |
41 global $txt, $scripturl, $context, $boarddir, $sourcedir, $modSettings; | |
42 | |
43 isAllowedTo('admin_forum'); | |
44 require_once($sourcedir . '/Subs-Package.php'); | |
45 | |
46 // Use the Packages template... no reason to separate. | |
47 loadLanguage('Packages'); | |
48 loadTemplate('Packages', 'admin'); | |
49 | |
50 $context['page_title'] = $txt['package']; | |
51 | |
52 // Here is a list of all the potentially valid actions. | |
53 $subActions = array( | |
54 'servers' => 'PackageServers', | |
55 'add' => 'PackageServerAdd', | |
56 'browse' => 'PackageGBrowse', | |
57 'download' => 'PackageDownload', | |
58 'remove' => 'PackageServerRemove', | |
59 'upload' => 'PackageUpload', | |
60 ); | |
61 | |
62 // Now let's decide where we are taking this... | |
63 if (isset($_REQUEST['sa']) && isset($subActions[$_REQUEST['sa']])) | |
64 $context['sub_action'] = $_REQUEST['sa']; | |
65 // We need to support possible old javascript links... | |
66 elseif (isset($_GET['pgdownload'])) | |
67 $context['sub_action'] = 'download'; | |
68 else | |
69 $context['sub_action'] = 'servers'; | |
70 | |
71 // We need to force the "Download" tab as selected. | |
72 $context['menu_data_' . $context['admin_menu_id']]['current_subsection'] = 'packageget'; | |
73 | |
74 // Now create the tabs for the template. | |
75 $context[$context['admin_menu_name']]['tab_data'] = array( | |
76 'title' => $txt['package_manager'], | |
77 //'help' => 'registrations', | |
78 'description' => $txt['package_manager_desc'], | |
79 'tabs' => array( | |
80 'browse' => array( | |
81 ), | |
82 'packageget' => array( | |
83 'description' => $txt['download_packages_desc'], | |
84 ), | |
85 'installed' => array( | |
86 'description' => $txt['installed_packages_desc'], | |
87 ), | |
88 'perms' => array( | |
89 'description' => $txt['package_file_perms_desc'], | |
90 ), | |
91 'options' => array( | |
92 'description' => $txt['package_install_options_ftp_why'], | |
93 ), | |
94 ), | |
95 ); | |
96 | |
97 $subActions[$context['sub_action']](); | |
98 } | |
99 | |
100 function PackageServers() | |
101 { | |
102 global $txt, $scripturl, $context, $boarddir, $sourcedir, $modSettings, $smcFunc; | |
103 | |
104 // Ensure we use the correct template, and page title. | |
105 $context['sub_template'] = 'servers'; | |
106 $context['page_title'] .= ' - ' . $txt['download_packages']; | |
107 | |
108 // Load the list of servers. | |
109 $request = $smcFunc['db_query']('', ' | |
110 SELECT id_server, name, url | |
111 FROM {db_prefix}package_servers', | |
112 array( | |
113 ) | |
114 ); | |
115 $context['servers'] = array(); | |
116 while ($row = $smcFunc['db_fetch_assoc']($request)) | |
117 { | |
118 $context['servers'][] = array( | |
119 'name' => $row['name'], | |
120 'url' => $row['url'], | |
121 'id' => $row['id_server'], | |
122 ); | |
123 } | |
124 $smcFunc['db_free_result']($request); | |
125 | |
126 $context['package_download_broken'] = !is_writable($boarddir . '/Packages') || !is_writable($boarddir . '/Packages/installed.list'); | |
127 | |
128 if ($context['package_download_broken']) | |
129 { | |
130 @chmod($boarddir . '/Packages', 0777); | |
131 @chmod($boarddir . '/Packages/installed.list', 0777); | |
132 } | |
133 | |
134 $context['package_download_broken'] = !is_writable($boarddir . '/Packages') || !is_writable($boarddir . '/Packages/installed.list'); | |
135 | |
136 if ($context['package_download_broken']) | |
137 { | |
138 if (isset($_POST['ftp_username'])) | |
139 { | |
140 loadClassFile('Class-Package.php'); | |
141 $ftp = new ftp_connection($_POST['ftp_server'], $_POST['ftp_port'], $_POST['ftp_username'], $_POST['ftp_password']); | |
142 | |
143 if ($ftp->error === false) | |
144 { | |
145 // I know, I know... but a lot of people want to type /home/xyz/... which is wrong, but logical. | |
146 if (!$ftp->chdir($_POST['ftp_path'])) | |
147 { | |
148 $ftp_error = $ftp->error; | |
149 $ftp->chdir(preg_replace('~^/home[2]?/[^/]+?~', '', $_POST['ftp_path'])); | |
150 } | |
151 } | |
152 } | |
153 | |
154 if (!isset($ftp) || $ftp->error !== false) | |
155 { | |
156 if (!isset($ftp)) | |
157 { | |
158 loadClassFile('Class-Package.php'); | |
159 $ftp = new ftp_connection(null); | |
160 } | |
161 elseif ($ftp->error !== false && !isset($ftp_error)) | |
162 $ftp_error = $ftp->last_message === null ? '' : $ftp->last_message; | |
163 | |
164 list ($username, $detect_path, $found_path) = $ftp->detect_path($boarddir); | |
165 | |
166 if ($found_path || !isset($_POST['ftp_path'])) | |
167 $_POST['ftp_path'] = $detect_path; | |
168 | |
169 if (!isset($_POST['ftp_username'])) | |
170 $_POST['ftp_username'] = $username; | |
171 | |
172 $context['package_ftp'] = array( | |
173 'server' => isset($_POST['ftp_server']) ? $_POST['ftp_server'] : (isset($modSettings['package_server']) ? $modSettings['package_server'] : 'localhost'), | |
174 'port' => isset($_POST['ftp_port']) ? $_POST['ftp_port'] : (isset($modSettings['package_port']) ? $modSettings['package_port'] : '21'), | |
175 'username' => isset($_POST['ftp_username']) ? $_POST['ftp_username'] : (isset($modSettings['package_username']) ? $modSettings['package_username'] : ''), | |
176 'path' => $_POST['ftp_path'], | |
177 'error' => empty($ftp_error) ? null : $ftp_error, | |
178 ); | |
179 } | |
180 else | |
181 { | |
182 $context['package_download_broken'] = false; | |
183 | |
184 $ftp->chmod('Packages', 0777); | |
185 $ftp->chmod('Packages/installed.list', 0777); | |
186 | |
187 $ftp->close(); | |
188 } | |
189 } | |
190 } | |
191 | |
192 // Browse a server's list of packages. | |
193 function PackageGBrowse() | |
194 { | |
195 global $txt, $boardurl, $context, $scripturl, $boarddir, $sourcedir, $forum_version, $context, $smcFunc; | |
196 | |
197 if (isset($_GET['server'])) | |
198 { | |
199 if ($_GET['server'] == '') | |
200 redirectexit('action=admin;area=packages;get'); | |
201 | |
202 $server = (int) $_GET['server']; | |
203 | |
204 // Query the server list to find the current server. | |
205 $request = $smcFunc['db_query']('', ' | |
206 SELECT name, url | |
207 FROM {db_prefix}package_servers | |
208 WHERE id_server = {int:current_server} | |
209 LIMIT 1', | |
210 array( | |
211 'current_server' => $server, | |
212 ) | |
213 ); | |
214 list ($name, $url) = $smcFunc['db_fetch_row']($request); | |
215 $smcFunc['db_free_result']($request); | |
216 | |
217 // If the server does not exist, dump out. | |
218 if (empty($url)) | |
219 fatal_lang_error('couldnt_connect', false); | |
220 | |
221 // If there is a relative link, append to the stored server url. | |
222 if (isset($_GET['relative'])) | |
223 $url = $url . (substr($url, -1) == '/' ? '' : '/') . $_GET['relative']; | |
224 | |
225 // Clear any "absolute" URL. Since "server" is present, "absolute" is garbage. | |
226 unset($_GET['absolute']); | |
227 } | |
228 elseif (isset($_GET['absolute']) && $_GET['absolute'] != '') | |
229 { | |
230 // Initialize the requried variables. | |
231 $server = ''; | |
232 $url = $_GET['absolute']; | |
233 $name = ''; | |
234 $_GET['package'] = $url . '/packages.xml?language=' . $context['user']['language']; | |
235 | |
236 // Clear any "relative" URL. Since "server" is not present, "relative" is garbage. | |
237 unset($_GET['relative']); | |
238 | |
239 $token = checkConfirm('get_absolute_url'); | |
240 if ($token !== true) | |
241 { | |
242 $context['sub_template'] = 'package_confirm'; | |
243 | |
244 $context['page_title'] = $txt['package_servers']; | |
245 $context['confirm_message'] = sprintf($txt['package_confirm_view_package_content'], htmlspecialchars($_GET['absolute'])); | |
246 $context['proceed_href'] = $scripturl . '?action=admin;area=packages;get;sa=browse;absolute=' . urlencode($_GET['absolute']) . ';confirm=' . $token; | |
247 | |
248 return; | |
249 } | |
250 } | |
251 // Minimum required parameter did not exist so dump out. | |
252 else | |
253 fatal_lang_error('couldnt_connect', false); | |
254 | |
255 // Attempt to connect. If unsuccessful... try the URL. | |
256 if (!isset($_GET['package']) || file_exists($_GET['package'])) | |
257 $_GET['package'] = $url . '/packages.xml?language=' . $context['user']['language']; | |
258 | |
259 // Check to be sure the packages.xml file actually exists where it is should be... or dump out. | |
260 if ((isset($_GET['absolute']) || isset($_GET['relative'])) && !url_exists($_GET['package'])) | |
261 fatal_lang_error('packageget_unable', false, array($url . '/index.php')); | |
262 | |
263 // Might take some time. | |
264 @set_time_limit(600); | |
265 | |
266 // Read packages.xml and parse into xmlArray. (the true tells it to trim things ;).) | |
267 loadClassFile('Class-Package.php'); | |
268 $listing = new xmlArray(fetch_web_data($_GET['package']), true); | |
269 | |
270 // Errm.... empty file? Try the URL.... | |
271 if (!$listing->exists('package-list')) | |
272 fatal_lang_error('packageget_unable', false, array($url . '/index.php')); | |
273 | |
274 // List out the packages... | |
275 $context['package_list'] = array(); | |
276 | |
277 $listing = $listing->path('package-list[0]'); | |
278 | |
279 // Use the package list's name if it exists. | |
280 if ($listing->exists('list-title')) | |
281 $name = $listing->fetch('list-title'); | |
282 | |
283 // Pick the correct template. | |
284 $context['sub_template'] = 'package_list'; | |
285 | |
286 $context['page_title'] = $txt['package_servers'] . ($name != '' ? ' - ' . $name : ''); | |
287 $context['package_server'] = $server; | |
288 | |
289 // By default we use an unordered list, unless there are no lists with more than one package. | |
290 $context['list_type'] = 'ul'; | |
291 | |
292 $instmods = loadInstalledPackages(); | |
293 | |
294 $installed_mods = array(); | |
295 // Look through the list of installed mods... | |
296 foreach ($instmods as $installed_mod) | |
297 $installed_mods[$installed_mod['package_id']] = $installed_mod['version']; | |
298 | |
299 // Get default author and email if they exist. | |
300 if ($listing->exists('default-author')) | |
301 { | |
302 $default_author = $smcFunc['htmlspecialchars']($listing->fetch('default-author')); | |
303 if ($listing->exists('default-author/@email')) | |
304 $default_email = $smcFunc['htmlspecialchars']($listing->fetch('default-author/@email')); | |
305 } | |
306 | |
307 // Get default web site if it exists. | |
308 if ($listing->exists('default-website')) | |
309 { | |
310 $default_website = $smcFunc['htmlspecialchars']($listing->fetch('default-website')); | |
311 if ($listing->exists('default-website/@title')) | |
312 $default_title = $smcFunc['htmlspecialchars']($listing->fetch('default-website/@title')); | |
313 } | |
314 | |
315 $the_version = strtr($forum_version, array('SMF ' => '')); | |
316 if (!empty($_SESSION['version_emulate'])) | |
317 $the_version = $_SESSION['version_emulate']; | |
318 | |
319 $packageNum = 0; | |
320 $packageSection = 0; | |
321 | |
322 $sections = $listing->set('section'); | |
323 foreach ($sections as $i => $section) | |
324 { | |
325 $context['package_list'][$packageSection] = array( | |
326 'title' => '', | |
327 'text' => '', | |
328 'items' => array(), | |
329 ); | |
330 | |
331 $packages = $section->set('title|heading|text|remote|rule|modification|language|avatar-pack|theme|smiley-set'); | |
332 foreach ($packages as $thisPackage) | |
333 { | |
334 $package = array( | |
335 'type' => $thisPackage->name(), | |
336 ); | |
337 | |
338 if (in_array($package['type'], array('title', 'text'))) | |
339 $context['package_list'][$packageSection][$package['type']] = $smcFunc['htmlspecialchars']($thisPackage->fetch('.')); | |
340 // It's a Title, Heading, Rule or Text. | |
341 elseif (in_array($package['type'], array('heading', 'rule'))) | |
342 $package['name'] = $smcFunc['htmlspecialchars']($thisPackage->fetch('.')); | |
343 // It's a Remote link. | |
344 elseif ($package['type'] == 'remote') | |
345 { | |
346 $remote_type = $thisPackage->exists('@type') ? $thisPackage->fetch('@type') : 'relative'; | |
347 | |
348 if ($remote_type == 'relative' && substr($thisPackage->fetch('@href'), 0, 7) != 'http://') | |
349 { | |
350 if (isset($_GET['absolute'])) | |
351 $current_url = $_GET['absolute'] . '/'; | |
352 elseif (isset($_GET['relative'])) | |
353 $current_url = $_GET['relative'] . '/'; | |
354 else | |
355 $current_url = ''; | |
356 | |
357 $current_url .= $thisPackage->fetch('@href'); | |
358 if (isset($_GET['absolute'])) | |
359 $package['href'] = $scripturl . '?action=admin;area=packages;get;sa=browse;absolute=' . $current_url; | |
360 else | |
361 $package['href'] = $scripturl . '?action=admin;area=packages;get;sa=browse;server=' . $context['package_server'] . ';relative=' . $current_url; | |
362 } | |
363 else | |
364 { | |
365 $current_url = $thisPackage->fetch('@href'); | |
366 $package['href'] = $scripturl . '?action=admin;area=packages;get;sa=browse;absolute=' . $current_url; | |
367 } | |
368 | |
369 $package['name'] = $smcFunc['htmlspecialchars']($thisPackage->fetch('.')); | |
370 $package['link'] = '<a href="' . $package['href'] . '">' . $package['name'] . '</a>'; | |
371 } | |
372 // It's a package... | |
373 else | |
374 { | |
375 if (isset($_GET['absolute'])) | |
376 $current_url = $_GET['absolute'] . '/'; | |
377 elseif (isset($_GET['relative'])) | |
378 $current_url = $_GET['relative'] . '/'; | |
379 else | |
380 $current_url = ''; | |
381 | |
382 $server_att = $server != '' ? ';server=' . $server : ''; | |
383 | |
384 $package += $thisPackage->to_array(); | |
385 | |
386 if (isset($package['website'])) | |
387 unset($package['website']); | |
388 $package['author'] = array(); | |
389 | |
390 if ($package['description'] == '') | |
391 $package['description'] = $txt['package_no_description']; | |
392 else | |
393 $package['description'] = parse_bbc(preg_replace('~\[[/]?html\]~i', '', $smcFunc['htmlspecialchars']($package['description']))); | |
394 | |
395 $package['is_installed'] = isset($installed_mods[$package['id']]); | |
396 $package['is_current'] = $package['is_installed'] && ($installed_mods[$package['id']] == $package['version']); | |
397 $package['is_newer'] = $package['is_installed'] && ($installed_mods[$package['id']] > $package['version']); | |
398 | |
399 // This package is either not installed, or installed but old. Is it supported on this version of SMF? | |
400 if (!$package['is_installed'] || (!$package['is_current'] && !$package['is_newer'])) | |
401 { | |
402 if ($thisPackage->exists('version/@for')) | |
403 $package['can_install'] = matchPackageVersion($the_version, $thisPackage->fetch('version/@for')); | |
404 } | |
405 // Okay, it's already installed AND up to date. | |
406 else | |
407 $package['can_install'] = false; | |
408 | |
409 $already_exists = getPackageInfo(basename($package['filename'])); | |
410 $package['download_conflict'] = is_array($already_exists) && $already_exists['id'] == $package['id'] && $already_exists['version'] != $package['version']; | |
411 | |
412 $package['href'] = $url . '/' . $package['filename']; | |
413 $package['name'] = $smcFunc['htmlspecialchars']($package['name']); | |
414 $package['link'] = '<a href="' . $package['href'] . '">' . $package['name'] . '</a>'; | |
415 $package['download']['href'] = $scripturl . '?action=admin;area=packages;get;sa=download' . $server_att . ';package=' . $current_url . $package['filename'] . ($package['download_conflict'] ? ';conflict' : '') . ';' . $context['session_var'] . '=' . $context['session_id']; | |
416 $package['download']['link'] = '<a href="' . $package['download']['href'] . '">' . $package['name'] . '</a>'; | |
417 | |
418 if ($thisPackage->exists('author') || isset($default_author)) | |
419 { | |
420 if ($thisPackage->exists('author/@email')) | |
421 $package['author']['email'] = $thisPackage->fetch('author/@email'); | |
422 elseif (isset($default_email)) | |
423 $package['author']['email'] = $default_email; | |
424 | |
425 if ($thisPackage->exists('author') && $thisPackage->fetch('author') != '') | |
426 $package['author']['name'] = $smcFunc['htmlspecialchars']($thisPackage->fetch('author')); | |
427 else | |
428 $package['author']['name'] = $default_author; | |
429 | |
430 if (!empty($package['author']['email'])) | |
431 { | |
432 // Only put the "mailto:" if it looks like a valid email address. Some may wish to put a link to an SMF IM Form or other web mail form. | |
433 $package['author']['href'] = preg_match('~^[\w\.\-]+@[\w][\w\-\.]+[\w]$~', $package['author']['email']) != 0 ? 'mailto:' . $package['author']['email'] : $package['author']['email']; | |
434 $package['author']['link'] = '<a href="' . $package['author']['href'] . '">' . $package['author']['name'] . '</a>'; | |
435 } | |
436 } | |
437 | |
438 if ($thisPackage->exists('website') || isset($default_website)) | |
439 { | |
440 if ($thisPackage->exists('website') && $thisPackage->exists('website/@title')) | |
441 $package['author']['website']['name'] = $smcFunc['htmlspecialchars']($thisPackage->fetch('website/@title')); | |
442 elseif (isset($default_title)) | |
443 $package['author']['website']['name'] = $default_title; | |
444 elseif ($thisPackage->exists('website')) | |
445 $package['author']['website']['name'] = $smcFunc['htmlspecialchars']($thisPackage->fetch('website')); | |
446 else | |
447 $package['author']['website']['name'] = $default_website; | |
448 | |
449 if ($thisPackage->exists('website') && $thisPackage->fetch('website') != '') | |
450 $authorhompage = $thisPackage->fetch('website'); | |
451 else | |
452 $authorhompage = $default_website; | |
453 | |
454 if (strpos(strtolower($authorhompage), 'a href') === false) | |
455 { | |
456 $package['author']['website']['href'] = $authorhompage; | |
457 $package['author']['website']['link'] = '<a href="' . $authorhompage . '">' . $package['author']['website']['name'] . '</a>'; | |
458 } | |
459 else | |
460 { | |
461 if (preg_match('/a href="(.+?)"/', $authorhompage, $match) == 1) | |
462 $package['author']['website']['href'] = $match[1]; | |
463 else | |
464 $package['author']['website']['href'] = ''; | |
465 $package['author']['website']['link'] = $authorhompage; | |
466 } | |
467 } | |
468 else | |
469 { | |
470 $package['author']['website']['href'] = ''; | |
471 $package['author']['website']['link'] = ''; | |
472 } | |
473 } | |
474 | |
475 $package['is_remote'] = $package['type'] == 'remote'; | |
476 $package['is_title'] = $package['type'] == 'title'; | |
477 $package['is_heading'] = $package['type'] == 'heading'; | |
478 $package['is_text'] = $package['type'] == 'text'; | |
479 $package['is_line'] = $package['type'] == 'rule'; | |
480 | |
481 $packageNum = in_array($package['type'], array('title', 'heading', 'text', 'remote', 'rule')) ? 0 : $packageNum + 1; | |
482 $package['count'] = $packageNum; | |
483 | |
484 if (!in_array($package['type'], array('title', 'text'))) | |
485 $context['package_list'][$packageSection]['items'][] = $package; | |
486 | |
487 if ($package['count'] > 1) | |
488 $context['list_type'] = 'ol'; | |
489 } | |
490 | |
491 $packageSection++; | |
492 } | |
493 | |
494 // Lets make sure we get a nice new spiffy clean $package to work with. Otherwise we get PAIN! | |
495 unset($package); | |
496 | |
497 foreach ($context['package_list'] as $ps_id => $packageSection) | |
498 { | |
499 foreach ($packageSection['items'] as $i => $package) | |
500 { | |
501 if ($package['count'] == 0 || isset($package['can_install'])) | |
502 continue; | |
503 | |
504 $context['package_list'][$ps_id]['items'][$i]['can_install'] = false; | |
505 | |
506 $packageInfo = getPackageInfo($url . '/' . $package['filename']); | |
507 if (is_array($packageInfo) && $packageInfo['xml']->exists('install')) | |
508 { | |
509 $installs = $packageInfo['xml']->set('install'); | |
510 foreach ($installs as $install) | |
511 if (!$install->exists('@for') || matchPackageVersion($the_version, $install->fetch('@for'))) | |
512 { | |
513 // Okay, this one is good to go. | |
514 $context['package_list'][$ps_id]['items'][$i]['can_install'] = true; | |
515 break; | |
516 } | |
517 } | |
518 } | |
519 } | |
520 } | |
521 | |
522 // Download a package. | |
523 function PackageDownload() | |
524 { | |
525 global $txt, $scripturl, $boarddir, $context, $sourcedir, $smcFunc; | |
526 | |
527 // Use the downloaded sub template. | |
528 $context['sub_template'] = 'downloaded'; | |
529 | |
530 // Security is good... | |
531 checkSession('get'); | |
532 | |
533 // To download something, we need a valid server or url. | |
534 if (empty($_GET['server']) && (!empty($_GET['get']) && !empty($_REQUEST['package']))) | |
535 fatal_lang_error('package_get_error_is_zero', false); | |
536 | |
537 if (isset($_GET['server'])) | |
538 { | |
539 $server = (int) $_GET['server']; | |
540 | |
541 // Query the server table to find the requested server. | |
542 $request = $smcFunc['db_query']('', ' | |
543 SELECT name, url | |
544 FROM {db_prefix}package_servers | |
545 WHERE id_server = {int:current_server} | |
546 LIMIT 1', | |
547 array( | |
548 'current_server' => $server, | |
549 ) | |
550 ); | |
551 list ($name, $url) = $smcFunc['db_fetch_row']($request); | |
552 $smcFunc['db_free_result']($request); | |
553 | |
554 // If server does not exist then dump out. | |
555 if (empty($url)) | |
556 fatal_lang_error('couldnt_connect', false); | |
557 | |
558 $url = $url . '/'; | |
559 } | |
560 else | |
561 { | |
562 // Initialize the requried variables. | |
563 $server = ''; | |
564 $url = ''; | |
565 } | |
566 | |
567 if (isset($_REQUEST['byurl']) && !empty($_POST['filename'])) | |
568 $package_name = basename($_REQUEST['filename']); | |
569 else | |
570 $package_name = basename($_REQUEST['package']); | |
571 | |
572 if (isset($_REQUEST['conflict']) || (isset($_REQUEST['auto']) && file_exists($boarddir . '/Packages/' . $package_name))) | |
573 { | |
574 // Find the extension, change abc.tar.gz to abc_1.tar.gz... | |
575 if (strrpos(substr($package_name, 0, -3), '.') !== false) | |
576 { | |
577 $ext = substr($package_name, strrpos(substr($package_name, 0, -3), '.')); | |
578 $package_name = substr($package_name, 0, strrpos(substr($package_name, 0, -3), '.')) . '_'; | |
579 } | |
580 else | |
581 $ext = ''; | |
582 | |
583 // Find the first available. | |
584 $i = 1; | |
585 while (file_exists($boarddir . '/Packages/' . $package_name . $i . $ext)) | |
586 $i++; | |
587 | |
588 $package_name = $package_name . $i . $ext; | |
589 } | |
590 | |
591 // First make sure it's a package. | |
592 $packageInfo = getPackageInfo($url . $_REQUEST['package']); | |
593 if (!is_array($packageInfo)) | |
594 fatal_lang_error($packageInfo); | |
595 | |
596 // Use FTP if necessary. | |
597 create_chmod_control(array($boarddir . '/Packages/' . $package_name), array('destination_url' => $scripturl . '?action=admin;area=packages;get;sa=download' . (isset($_GET['server']) ? ';server=' . $_GET['server'] : '') . (isset($_REQUEST['auto']) ? ';auto' : '') . ';package=' . $_REQUEST['package'] . (isset($_REQUEST['conflict']) ? ';conflict' : '') . ';' . $context['session_var'] . '=' . $context['session_id'], 'crash_on_error' => true)); | |
598 package_put_contents($boarddir . '/Packages/' . $package_name, fetch_web_data($url . $_REQUEST['package'])); | |
599 | |
600 // Done! Did we get this package automatically? | |
601 if (preg_match('~^http://[\w_\-]+\.simplemachines\.org/~', $_REQUEST['package']) == 1 && strpos($_REQUEST['package'], 'dlattach') === false && isset($_REQUEST['auto'])) | |
602 redirectexit('action=admin;area=packages;sa=install;package=' . $package_name); | |
603 | |
604 // You just downloaded a mod from SERVER_NAME_GOES_HERE. | |
605 $context['package_server'] = $server; | |
606 | |
607 $context['package'] = getPackageInfo($package_name); | |
608 | |
609 if (!is_array($context['package'])) | |
610 fatal_lang_error('package_cant_download', false); | |
611 | |
612 if ($context['package']['type'] == 'modification') | |
613 $context['package']['install']['link'] = '<a href="' . $scripturl . '?action=admin;area=packages;sa=install;package=' . $context['package']['filename'] . '">[ ' . $txt['install_mod'] . ' ]</a>'; | |
614 elseif ($context['package']['type'] == 'avatar') | |
615 $context['package']['install']['link'] = '<a href="' . $scripturl . '?action=admin;area=packages;sa=install;package=' . $context['package']['filename'] . '">[ ' . $txt['use_avatars'] . ' ]</a>'; | |
616 elseif ($context['package']['type'] == 'language') | |
617 $context['package']['install']['link'] = '<a href="' . $scripturl . '?action=admin;area=packages;sa=install;package=' . $context['package']['filename'] . '">[ ' . $txt['add_languages'] . ' ]</a>'; | |
618 else | |
619 $context['package']['install']['link'] = ''; | |
620 | |
621 $context['package']['list_files']['link'] = '<a href="' . $scripturl . '?action=admin;area=packages;sa=list;package=' . $context['package']['filename'] . '">[ ' . $txt['list_files'] . ' ]</a>'; | |
622 | |
623 // Free a little bit of memory... | |
624 unset($context['package']['xml']); | |
625 | |
626 $context['page_title'] = $txt['download_success']; | |
627 } | |
628 | |
629 // Upload a new package to the directory. | |
630 function PackageUpload() | |
631 { | |
632 global $txt, $scripturl, $boarddir, $context, $sourcedir; | |
633 | |
634 // Setup the correct template, even though I'll admit we ain't downloading ;) | |
635 $context['sub_template'] = 'downloaded'; | |
636 | |
637 // !!! TODO: Use FTP if the Packages directory is not writable. | |
638 | |
639 // Check the file was even sent! | |
640 if (!isset($_FILES['package']['name']) || $_FILES['package']['name'] == '') | |
641 fatal_lang_error('package_upload_error_nofile'); | |
642 elseif (!is_uploaded_file($_FILES['package']['tmp_name']) || (@ini_get('open_basedir') == '' && !file_exists($_FILES['package']['tmp_name']))) | |
643 fatal_lang_error('package_upload_error_failure'); | |
644 | |
645 // Make sure it has a sane filename. | |
646 $_FILES['package']['name'] = preg_replace(array('/\s/', '/\.[\.]+/', '/[^\w_\.\-]/'), array('_', '.', ''), $_FILES['package']['name']); | |
647 | |
648 if (strtolower(substr($_FILES['package']['name'], -4)) != '.zip' && strtolower(substr($_FILES['package']['name'], -4)) != '.tgz' && strtolower(substr($_FILES['package']['name'], -7)) != '.tar.gz') | |
649 fatal_lang_error('package_upload_error_supports', false, array('zip, tgz, tar.gz')); | |
650 | |
651 // We only need the filename... | |
652 $packageName = basename($_FILES['package']['name']); | |
653 | |
654 // Setup the destination and throw an error if the file is already there! | |
655 $destination = $boarddir . '/Packages/' . $packageName; | |
656 // !!! Maybe just roll it like we do for downloads? | |
657 if (file_exists($destination)) | |
658 fatal_lang_error('package_upload_error_exists'); | |
659 | |
660 // Now move the file. | |
661 move_uploaded_file($_FILES['package']['tmp_name'], $destination); | |
662 @chmod($destination, 0777); | |
663 | |
664 // If we got this far that should mean it's available. | |
665 $context['package'] = getPackageInfo($packageName); | |
666 $context['package_server'] = ''; | |
667 | |
668 // Not really a package, you lazy bum! | |
669 if (!is_array($context['package'])) | |
670 { | |
671 @unlink($destination); | |
672 loadLanguage('Errors'); | |
673 fatal_lang_error('package_upload_error_broken', false, $txt[$context['package']]); | |
674 } | |
675 // Is it already uploaded, maybe? | |
676 elseif ($dir = @opendir($boarddir . '/Packages')) | |
677 { | |
678 while ($package = readdir($dir)) | |
679 { | |
680 if ($package == '.' || $package == '..' || $package == 'temp' || $package == $packageName || (!(is_dir($boarddir . '/Packages/' . $package) && file_exists($boarddir . '/Packages/' . $package . '/package-info.xml')) && substr(strtolower($package), -7) != '.tar.gz' && substr(strtolower($package), -4) != '.tgz' && substr(strtolower($package), -4) != '.zip')) | |
681 continue; | |
682 | |
683 $packageInfo = getPackageInfo($package); | |
684 if (!is_array($packageInfo)) | |
685 continue; | |
686 | |
687 if ($packageInfo['id'] == $context['package']['id'] && $packageInfo['version'] == $context['package']['version']) | |
688 { | |
689 @unlink($destination); | |
690 loadLanguage('Errors'); | |
691 fatal_lang_error('package_upload_error_exists'); | |
692 } | |
693 } | |
694 closedir($dir); | |
695 } | |
696 | |
697 if ($context['package']['type'] == 'modification') | |
698 $context['package']['install']['link'] = '<a href="' . $scripturl . '?action=admin;area=packages;sa=install;package=' . $context['package']['filename'] . '">[ ' . $txt['install_mod'] . ' ]</a>'; | |
699 elseif ($context['package']['type'] == 'avatar') | |
700 $context['package']['install']['link'] = '<a href="' . $scripturl . '?action=admin;area=packages;sa=install;package=' . $context['package']['filename'] . '">[ ' . $txt['use_avatars'] . ' ]</a>'; | |
701 elseif ($context['package']['type'] == 'language') | |
702 $context['package']['install']['link'] = '<a href="' . $scripturl . '?action=admin;area=packages;sa=install;package=' . $context['package']['filename'] . '">[ ' . $txt['add_languages'] . ' ]</a>'; | |
703 else | |
704 $context['package']['install']['link'] = ''; | |
705 | |
706 $context['package']['list_files']['link'] = '<a href="' . $scripturl . '?action=admin;area=packages;sa=list;package=' . $context['package']['filename'] . '">[ ' . $txt['list_files'] . ' ]</a>'; | |
707 | |
708 unset($context['package']['xml']); | |
709 | |
710 $context['page_title'] = $txt['package_uploaded_success']; | |
711 } | |
712 | |
713 // Add a package server to the list. | |
714 function PackageServerAdd() | |
715 { | |
716 global $smcFunc; | |
717 | |
718 // Validate the user. | |
719 checkSession(); | |
720 | |
721 // If they put a slash on the end, get rid of it. | |
722 if (substr($_POST['serverurl'], -1) == '/') | |
723 $_POST['serverurl'] = substr($_POST['serverurl'], 0, -1); | |
724 | |
725 // Are they both nice and clean? | |
726 $servername = trim($smcFunc['htmlspecialchars']($_POST['servername'])); | |
727 $serverurl = trim($smcFunc['htmlspecialchars']($_POST['serverurl'])); | |
728 | |
729 // Make sure the URL has the correct prefix. | |
730 if (strpos($serverurl, 'http://') !== 0 && strpos($serverurl, 'https://') !== 0) | |
731 $serverurl = 'http://' . $serverurl; | |
732 | |
733 $smcFunc['db_insert']('', | |
734 '{db_prefix}package_servers', | |
735 array( | |
736 'name' => 'string-255', 'url' => 'string-255', | |
737 ), | |
738 array( | |
739 $servername, $serverurl, | |
740 ), | |
741 array('id_server') | |
742 ); | |
743 | |
744 redirectexit('action=admin;area=packages;get'); | |
745 } | |
746 | |
747 // Remove a server from the list. | |
748 function PackageServerRemove() | |
749 { | |
750 global $smcFunc; | |
751 | |
752 checkSession('get'); | |
753 | |
754 $smcFunc['db_query']('', ' | |
755 DELETE FROM {db_prefix}package_servers | |
756 WHERE id_server = {int:current_server}', | |
757 array( | |
758 'current_server' => (int) $_GET['server'], | |
759 ) | |
760 ); | |
761 | |
762 redirectexit('action=admin;area=packages;get'); | |
763 } | |
764 | |
765 ?> |