PackageGet()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 58
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 33
c 0
b 0
f 0
nop 0
dl 0
loc 58
rs 9.392
nc 3

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * This file handles the package servers and packages download from Package Manager.
5
 *
6
 * Simple Machines Forum (SMF)
7
 *
8
 * @package SMF
9
 * @author Simple Machines https://www.simplemachines.org
10
 * @copyright 2022 Simple Machines and individual contributors
11
 * @license https://www.simplemachines.org/about/smf/license.php BSD
12
 *
13
 * @version 2.1.1
14
 */
15
16
if (!defined('SMF'))
17
	die('No direct access...');
18
19
/**
20
 * Browse the list of package servers, add servers...
21
 */
22
function PackageGet()
23
{
24
	global $txt, $context, $sourcedir;
25
26
	isAllowedTo('admin_forum');
27
	require_once($sourcedir . '/Subs-Package.php');
28
29
	// Use the Packages template... no reason to separate.
30
	loadLanguage('Packages');
31
	loadTemplate('Packages', 'admin');
32
33
	$context['page_title'] = $txt['package'];
34
35
	// Here is a list of all the potentially valid actions.
36
	$subActions = array(
37
		'servers' => 'PackageServers',
38
		'add' => 'PackageServerAdd',
39
		'browse' => 'PackageGBrowse',
40
		'download' => 'PackageDownload',
41
		'remove' => 'PackageServerRemove',
42
		'upload' => 'PackageUpload',
43
	);
44
45
	// Now let's decide where we are taking this...
46
	if (isset($_REQUEST['sa']) && isset($subActions[$_REQUEST['sa']]))
47
		$context['sub_action'] = $_REQUEST['sa'];
48
	// We need to support possible old javascript links...
49
	elseif (isset($_GET['pgdownload']))
50
		$context['sub_action'] = 'download';
51
	else
52
		$context['sub_action'] = 'servers';
53
54
	// We need to force the "Download" tab as selected.
55
	$context['menu_data_' . $context['admin_menu_id']]['current_subsection'] = 'packageget';
56
57
	// Now create the tabs for the template.
58
	$context[$context['admin_menu_name']]['tab_data'] = array(
59
		'title' => $txt['package_manager'],
60
		//'help' => 'registrations',
61
		'description' => $txt['package_manager_desc'],
62
		'tabs' => array(
63
			'browse' => array(
64
			),
65
			'packageget' => array(
66
				'description' => $txt['download_packages_desc'],
67
			),
68
			'perms' => array(
69
				'description' => $txt['package_file_perms_desc'],
70
			),
71
			'options' => array(
72
				'description' => $txt['package_install_options_ftp_why'],
73
			),
74
		),
75
	);
76
77
	call_integration_hook('integrate_package_get', array(&$subActions));
78
79
	call_helper($subActions[$context['sub_action']]);
80
}
81
82
/**
83
 * Load a list of package servers.
84
 */
85
function PackageServers()
86
{
87
	global $txt, $context, $sourcedir, $packagesdir, $modSettings, $smcFunc;
88
89
	// Ensure we use the correct template, and page title.
90
	$context['sub_template'] = 'servers';
91
	$context['page_title'] .= ' - ' . $txt['download_packages'];
92
93
	// Load the list of servers.
94
	$request = $smcFunc['db_query']('', '
95
		SELECT id_server, name, url
96
		FROM {db_prefix}package_servers',
97
		array(
98
		)
99
	);
100
	$context['servers'] = array();
101
	while ($row = $smcFunc['db_fetch_assoc']($request))
102
	{
103
		$context['servers'][] = array(
104
			'name' => $row['name'],
105
			'url' => $row['url'],
106
			'id' => $row['id_server'],
107
		);
108
	}
109
	$smcFunc['db_free_result']($request);
110
111
	$context['package_download_broken'] = !is_writable($packagesdir);
112
113
	if ($context['package_download_broken'])
114
	{
115
		smf_chmod($packagesdir, 0777);
116
	}
117
118
	$context['package_download_broken'] = !is_writable($packagesdir);
119
120
	if ($context['package_download_broken'])
121
	{
122
		if (isset($_POST['ftp_username']))
123
		{
124
			require_once($sourcedir . '/Class-Package.php');
125
			$ftp = new ftp_connection($_POST['ftp_server'], $_POST['ftp_port'], $_POST['ftp_username'], $_POST['ftp_password']);
126
127
			if ($ftp->error === false)
0 ignored issues
show
introduced by
The condition $ftp->error === false is always false.
Loading history...
128
			{
129
				// I know, I know... but a lot of people want to type /home/xyz/... which is wrong, but logical.
130
				if (!$ftp->chdir($_POST['ftp_path']))
131
				{
132
					$ftp_error = $ftp->error;
133
					$ftp->chdir(preg_replace('~^/home[2]?/[^/]+?~', '', $_POST['ftp_path']));
134
				}
135
			}
136
		}
137
138
		if (!isset($ftp) || $ftp->error !== false)
139
		{
140
			if (!isset($ftp))
141
			{
142
				require_once($sourcedir . '/Class-Package.php');
143
				$ftp = new ftp_connection(null);
144
			}
145
			elseif ($ftp->error !== false && !isset($ftp_error))
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $ftp does not seem to be defined for all execution paths leading up to this point.
Loading history...
146
				$ftp_error = $ftp->last_message === null ? '' : $ftp->last_message;
147
148
			list ($username, $detect_path, $found_path) = $ftp->detect_path($packagesdir);
149
150
			if ($found_path || !isset($_POST['ftp_path']))
151
				$_POST['ftp_path'] = $detect_path;
152
153
			if (!isset($_POST['ftp_username']))
154
				$_POST['ftp_username'] = $username;
155
156
			$context['package_ftp'] = array(
157
				'server' => isset($_POST['ftp_server']) ? $_POST['ftp_server'] : (isset($modSettings['package_server']) ? $modSettings['package_server'] : 'localhost'),
158
				'port' => isset($_POST['ftp_port']) ? $_POST['ftp_port'] : (isset($modSettings['package_port']) ? $modSettings['package_port'] : '21'),
159
				'username' => isset($_POST['ftp_username']) ? $_POST['ftp_username'] : (isset($modSettings['package_username']) ? $modSettings['package_username'] : ''),
160
				'path' => $_POST['ftp_path'],
161
				'error' => empty($ftp_error) ? null : $ftp_error,
162
			);
163
		}
164
		else
165
		{
166
			$context['package_download_broken'] = false;
167
168
			$ftp->chmod('.', 0777);
169
			$ftp->close();
170
		}
171
	}
172
173
	addInlineJavaScript('
174
	$(\'.new_package_content\').hide();
175
	$(\'.download_new_package\').on(\'click\', function() {
176
		var collapseState = $(\'.new_package_content\').css(\'display\');
177
		var icon = $(\'.download_new_package\').children(\'span\');
178
		var collapsedDiv = $(\'.new_package_content\');
179
180
		if (collapseState == \'none\')
181
		{
182
			collapsedDiv.show(\'slow\');
183
			icon.removeClass(\'toggle_down\').addClass(\'toggle_up\');
184
			icon.prop(\'title\', ' . JavaScriptEscape($txt['hide']) . ');
185
		}
186
187
		else
188
		{
189
			collapsedDiv.hide(\'slow\');
190
			icon.removeClass(\'toggle_up\').addClass(\'toggle_down\');
191
			icon.prop(\'title\', ' . JavaScriptEscape($txt['show']) . ');
192
		}
193
	});', true);
194
}
195
196
/**
197
 * Browse a server's list of packages.
198
 */
199
function PackageGBrowse()
200
{
201
	global $txt, $context, $scripturl, $sourcedir, $smcFunc;
202
203
	if (isset($_GET['server']))
204
	{
205
		if ($_GET['server'] == '')
206
			redirectexit('action=admin;area=packages;get');
207
208
		$server = (int) $_GET['server'];
209
210
		// Query the server list to find the current server.
211
		$request = $smcFunc['db_query']('', '
212
			SELECT name, url
213
			FROM {db_prefix}package_servers
214
			WHERE id_server = {int:current_server}
215
			LIMIT 1',
216
			array(
217
				'current_server' => $server,
218
			)
219
		);
220
		list ($name, $url) = $smcFunc['db_fetch_row']($request);
221
		$smcFunc['db_free_result']($request);
222
223
		// If the server does not exist, dump out.
224
		if (empty($url))
225
			fatal_lang_error('couldnt_connect', false);
226
227
		// If there is a relative link, append to the stored server url.
228
		if (isset($_GET['relative']))
229
			$url = $url . (substr($url, -1) == '/' ? '' : '/') . $_GET['relative'];
230
231
		$the_version = SMF_VERSION;
232
		if (!empty($_SESSION['version_emulate']))
233
			$the_version = $_SESSION['version_emulate'];
234
235
		// Sub out any variables we support in the url.
236
		$url = strtr($url, array(
237
			'{SMF_VERSION}' => urlencode($the_version)
238
		));
239
240
		// Clear any "absolute" URL.  Since "server" is present, "absolute" is garbage.
241
		unset($_GET['absolute']);
242
	}
243
	elseif (isset($_GET['absolute']) && $_GET['absolute'] != '')
244
	{
245
		// Initialize the requried variables.
246
		$server = '';
247
		$url = $_GET['absolute'];
248
		$name = '';
249
		$_GET['package'] = $url . '/packages.xml?language=' . $context['user']['language'];
250
251
		// Clear any "relative" URL.  Since "server" is not present, "relative" is garbage.
252
		unset($_GET['relative']);
253
254
		$token = checkConfirm('get_absolute_url');
255
		if ($token !== true)
256
		{
257
			$context['sub_template'] = 'package_confirm';
258
259
			$context['page_title'] = $txt['package_servers'];
260
			$context['confirm_message'] = sprintf($txt['package_confirm_view_package_content'], $smcFunc['htmlspecialchars']($_GET['absolute']));
261
			$context['proceed_href'] = $scripturl . '?action=admin;area=packages;get;sa=browse;absolute=' . urlencode($_GET['absolute']) . ';confirm=' . $token;
262
263
			return;
264
		}
265
	}
266
	// Minimum required parameter did not exist so dump out.
267
	else
268
		fatal_lang_error('couldnt_connect', false);
269
270
	// Attempt to connect.  If unsuccessful... try the URL.
271
	if (!isset($_GET['package']) || file_exists($_GET['package']))
272
		$_GET['package'] = $url . '/packages.xml?language=' . $context['user']['language'];
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $url does not seem to be defined for all execution paths leading up to this point.
Loading history...
273
274
	// Check to be sure the packages.xml file actually exists where it is should be... or dump out.
275
	if ((isset($_GET['absolute']) || isset($_GET['relative'])) && !url_exists($_GET['package']))
276
		fatal_lang_error('packageget_unable', false, array($url . '/index.php'));
277
278
	// Might take some time.
279
	@set_time_limit(600);
280
281
	// Read packages.xml and parse into xmlArray. (the true tells it to trim things ;).)
282
	require_once($sourcedir . '/Class-Package.php');
283
	$listing = new xmlArray(fetch_web_data($_GET['package']), true);
0 ignored issues
show
Bug introduced by
It seems like fetch_web_data($_GET['package']) can also be of type false; however, parameter $data of xmlArray::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

283
	$listing = new xmlArray(/** @scrutinizer ignore-type */ fetch_web_data($_GET['package']), true);
Loading history...
284
285
	// Errm.... empty file?  Try the URL....
286
	if (!$listing->exists('package-list'))
287
		fatal_lang_error('packageget_unable', false, array($url . '/index.php'));
288
289
	// List out the packages...
290
	$context['package_list'] = array();
291
292
	$listing = $listing->path('package-list[0]');
293
294
	// Use the package list's name if it exists.
295
	if ($listing->exists('list-title'))
296
		$name = $smcFunc['htmlspecialchars']($listing->fetch('list-title'));
297
298
	// Pick the correct template.
299
	$context['sub_template'] = 'package_list';
300
301
	$context['page_title'] = $txt['package_servers'] . ($name != '' ? ' - ' . $name : '');
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $name does not seem to be defined for all execution paths leading up to this point.
Loading history...
302
	$context['package_server'] = $server;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $server does not seem to be defined for all execution paths leading up to this point.
Loading history...
303
304
	// By default we use an unordered list, unless there are no lists with more than one package.
305
	$context['list_type'] = 'ul';
306
307
	$instmods = loadInstalledPackages();
308
309
	$installed_mods = array();
310
	// Look through the list of installed mods...
311
	foreach ($instmods as $installed_mod)
312
		$installed_mods[$installed_mod['package_id']] = $installed_mod['version'];
313
314
	// Get default author and email if they exist.
315
	if ($listing->exists('default-author'))
316
	{
317
		$default_author = $smcFunc['htmlspecialchars']($listing->fetch('default-author'));
318
		if ($listing->exists('default-author/@email') && filter_var($listing->fetch('default-author/@email'), FILTER_VALIDATE_EMAIL))
319
			$default_email = $smcFunc['htmlspecialchars']($listing->fetch('default-author/@email'));
320
	}
321
322
	// Get default web site if it exists.
323
	if ($listing->exists('default-website'))
324
	{
325
		$default_website = $smcFunc['htmlspecialchars']($listing->fetch('default-website'));
326
		if ($listing->exists('default-website/@title'))
327
			$default_title = $smcFunc['htmlspecialchars']($listing->fetch('default-website/@title'));
328
	}
329
330
	$the_version = SMF_VERSION;
331
	if (!empty($_SESSION['version_emulate']))
332
		$the_version = $_SESSION['version_emulate'];
333
334
	$packageNum = 0;
335
	$packageSection = 0;
336
337
	$sections = $listing->set('section');
338
	foreach ($sections as $i => $section)
339
	{
340
		$context['package_list'][$packageSection] = array(
341
			'title' => '',
342
			'text' => '',
343
			'items' => array(),
344
		);
345
346
		$packages = $section->set('title|heading|text|remote|rule|modification|language|avatar-pack|theme|smiley-set');
347
		foreach ($packages as $thisPackage)
348
		{
349
			$package = array(
350
				'type' => $thisPackage->name(),
351
			);
352
353
			if (in_array($package['type'], array('title', 'text')))
354
				$context['package_list'][$packageSection][$package['type']] = $smcFunc['htmlspecialchars']($thisPackage->fetch('.'));
355
			// It's a Title, Heading, Rule or Text.
356
			elseif (in_array($package['type'], array('heading', 'rule')))
357
				$package['name'] = $smcFunc['htmlspecialchars']($thisPackage->fetch('.'));
358
			// It's a Remote link.
359
			elseif ($package['type'] == 'remote')
360
			{
361
				$remote_type = $thisPackage->exists('@type') ? $thisPackage->fetch('@type') : 'relative';
362
363
				if ($remote_type == 'relative' && substr($thisPackage->fetch('@href'), 0, 7) != 'http://' && substr($thisPackage->fetch('@href'), 0, 8) != 'https://')
364
				{
365
					if (isset($_GET['absolute']))
366
						$current_url = $_GET['absolute'] . '/';
367
					elseif (isset($_GET['relative']))
368
						$current_url = $_GET['relative'] . '/';
369
					else
370
						$current_url = '';
371
372
					$current_url .= $thisPackage->fetch('@href');
373
					if (isset($_GET['absolute']))
374
						$package['href'] = $scripturl . '?action=admin;area=packages;get;sa=browse;absolute=' . $current_url;
375
					else
376
						$package['href'] = $scripturl . '?action=admin;area=packages;get;sa=browse;server=' . $context['package_server'] . ';relative=' . $current_url;
377
				}
378
				else
379
				{
380
					$current_url = $thisPackage->fetch('@href');
381
					$package['href'] = $scripturl . '?action=admin;area=packages;get;sa=browse;absolute=' . $current_url;
382
				}
383
384
				$package['name'] = $smcFunc['htmlspecialchars']($thisPackage->fetch('.'));
385
				$package['link'] = '<a href="' . $package['href'] . '">' . $package['name'] . '</a>';
386
			}
387
			// It's a package...
388
			else
389
			{
390
				if (isset($_GET['absolute']))
391
					$current_url = $_GET['absolute'] . '/';
392
				elseif (isset($_GET['relative']))
393
					$current_url = $_GET['relative'] . '/';
394
				else
395
					$current_url = '';
396
397
				$server_att = $server != '' ? ';server=' . $server : '';
398
399
				$package += $thisPackage->to_array();
400
401
				if (isset($package['website']))
402
					unset($package['website']);
403
				$package['author'] = array();
404
405
				if ($package['description'] == '')
406
					$package['description'] = $txt['package_no_description'];
407
				else
408
					$package['description'] = parse_bbc(preg_replace('~\[[/]?html\]~i', '', $smcFunc['htmlspecialchars']($package['description'])));
409
410
				$package['is_installed'] = isset($installed_mods[$package['id']]);
411
				$package['is_current'] = $package['is_installed'] && ($installed_mods[$package['id']] == $package['version']);
412
				$package['is_newer'] = $package['is_installed'] && ($installed_mods[$package['id']] > $package['version']);
413
414
				// This package is either not installed, or installed but old.  Is it supported on this version of SMF?
415
				if (!$package['is_installed'] || (!$package['is_current'] && !$package['is_newer']))
416
				{
417
					if ($thisPackage->exists('version/@for'))
418
						$package['can_install'] = matchPackageVersion($the_version, $thisPackage->fetch('version/@for'));
419
				}
420
				// Okay, it's already installed AND up to date.
421
				else
422
					$package['can_install'] = false;
423
424
				$already_exists = getPackageInfo(basename($package['filename']));
425
				$package['download_conflict'] = is_array($already_exists) && $already_exists['id'] == $package['id'] && $already_exists['version'] != $package['version'];
426
427
				$package['href'] = $url . '/' . $package['filename'];
428
				$package['link'] = '<a href="' . $package['href'] . '">' . $package['name'] . '</a>';
429
				$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'];
430
				$package['download']['link'] = '<a href="' . $package['download']['href'] . '">' . $package['name'] . '</a>';
431
432
				if ($thisPackage->exists('author') || isset($default_author))
433
				{
434
					if ($thisPackage->exists('author/@email') && filter_var($thisPackage->fetch('author/@email'), FILTER_VALIDATE_EMAIL))
435
						$package['author']['email'] = $thisPackage->fetch('author/@email');
436
					elseif (isset($default_email))
437
						$package['author']['email'] = $default_email;
438
439
					if ($thisPackage->exists('author') && $thisPackage->fetch('author') != '')
440
						$package['author']['name'] = $smcFunc['htmlspecialchars']($thisPackage->fetch('author'));
441
					else
442
						$package['author']['name'] = $default_author;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $default_author does not seem to be defined for all execution paths leading up to this point.
Loading history...
443
444
					if (!empty($package['author']['email']))
445
						$package['author']['link'] = '<a href="mailto:' . $package['author']['email'] . '">' . $package['author']['name'] . '</a>';
446
				}
447
448
				if ($thisPackage->exists('website') || isset($default_website))
449
				{
450
					if ($thisPackage->exists('website') && $thisPackage->exists('website/@title'))
451
						$package['author']['website']['name'] = $smcFunc['htmlspecialchars']($thisPackage->fetch('website/@title'));
452
					elseif (isset($default_title))
453
						$package['author']['website']['name'] = $default_title;
454
					elseif ($thisPackage->exists('website'))
455
						$package['author']['website']['name'] = $smcFunc['htmlspecialchars']($thisPackage->fetch('website'));
456
					else
457
						$package['author']['website']['name'] = $default_website;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $default_website does not seem to be defined for all execution paths leading up to this point.
Loading history...
458
459
					if ($thisPackage->exists('website') && $thisPackage->fetch('website') != '')
460
						$authorhompage = $smcFunc['htmlspecialchars']($thisPackage->fetch('website'));
461
					else
462
						$authorhompage = $default_website;
463
464
					$package['author']['website']['href'] = $authorhompage;
465
					$package['author']['website']['link'] = '<a href="' . $authorhompage . '">' . $package['author']['website']['name'] . '</a>';
466
				}
467
				else
468
				{
469
					$package['author']['website']['href'] = '';
470
					$package['author']['website']['link'] = '';
471
				}
472
			}
473
474
			$package['is_remote'] = $package['type'] == 'remote';
475
			$package['is_title'] = $package['type'] == 'title';
476
			$package['is_heading'] = $package['type'] == 'heading';
477
			$package['is_text'] = $package['type'] == 'text';
478
			$package['is_line'] = $package['type'] == 'rule';
479
480
			$packageNum = in_array($package['type'], array('title', 'heading', 'text', 'remote', 'rule')) ? 0 : $packageNum + 1;
481
			$package['count'] = $packageNum;
482
483
			if (!in_array($package['type'], array('title', 'text')))
484
				$context['package_list'][$packageSection]['items'][] = $package;
485
486
			if ($package['count'] > 1)
487
				$context['list_type'] = 'ol';
488
		}
489
490
		$packageSection++;
491
	}
492
493
	// Lets make sure we get a nice new spiffy clean $package to work with.  Otherwise we get PAIN!
494
	unset($package);
495
496
	foreach ($context['package_list'] as $ps_id => $packageSection)
497
	{
498
		foreach ($packageSection['items'] as $i => $package)
499
		{
500
			if ($package['count'] == 0 || isset($package['can_install']))
501
				continue;
502
503
			$context['package_list'][$ps_id]['items'][$i]['can_install'] = false;
504
505
			$packageInfo = getPackageInfo($url . '/' . $package['filename']);
506
			if (is_array($packageInfo) && $packageInfo['xml']->exists('install'))
507
			{
508
				$installs = $packageInfo['xml']->set('install');
509
				foreach ($installs as $install)
510
				{
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
					// no install found for this version, lets see if one exists for another
519
					if ($context['package_list'][$ps_id]['items'][$i]['can_install'] === false && $install->exists('@for'))
520
					{
521
						$reset = true;
522
523
						// Get the highest install version that is available from the package
524
						foreach ($installs as $install)
0 ignored issues
show
Comprehensibility Bug introduced by
$install is overwriting a variable from outer foreach loop.
Loading history...
525
						{
526
							$context['package_list'][$ps_id]['items'][$i]['can_emulate_install'] = matchHighestPackageVersion($install->fetch('@for'), $reset, $the_version);
527
							$reset = false;
528
						}
529
					}
530
				}
531
			}
532
		}
533
	}
534
}
535
536
/**
537
 * Download a package.
538
 */
539
function PackageDownload()
540
{
541
	global $txt, $scripturl, $context, $packagesdir, $smcFunc;
542
543
	// Use the downloaded sub template.
544
	$context['sub_template'] = 'downloaded';
545
546
	// Security is good...
547
	checkSession('get');
548
549
	// To download something, we need a valid server or url.
550
	if (empty($_GET['server']) && (!empty($_GET['get']) && !empty($_REQUEST['package'])))
551
		fatal_lang_error('package_get_error_is_zero', false);
552
553
	if (isset($_GET['server']))
554
	{
555
		$server = (int) $_GET['server'];
556
557
		// Query the server table to find the requested server.
558
		$request = $smcFunc['db_query']('', '
559
			SELECT name, url
560
			FROM {db_prefix}package_servers
561
			WHERE id_server = {int:current_server}
562
			LIMIT 1',
563
			array(
564
				'current_server' => $server,
565
			)
566
		);
567
		list ($name, $url) = $smcFunc['db_fetch_row']($request);
568
		$smcFunc['db_free_result']($request);
569
570
		// If server does not exist then dump out.
571
		if (empty($url))
572
			fatal_lang_error('couldnt_connect', false);
573
574
		$the_version = SMF_VERSION;
575
		if (!empty($_SESSION['version_emulate']))
576
			$the_version = $_SESSION['version_emulate'];
577
578
		// Sub out any variables we support in the url.
579
		$url = strtr($url, array(
580
			'{SMF_VERSION}' => urlencode($the_version)
581
		));
582
583
		$url = $url . '/';
584
	}
585
	else
586
	{
587
		// Initialize the requried variables.
588
		$server = '';
589
		$url = '';
590
	}
591
592
	if (isset($_REQUEST['byurl']) && !empty($_POST['filename']))
593
		$package_name = basename($_REQUEST['filename']);
594
	else
595
		$package_name = basename($_REQUEST['package']);
596
597
	if (isset($_REQUEST['conflict']) || (isset($_REQUEST['auto']) && file_exists($packagesdir . '/' . $package_name)))
598
	{
599
		// Find the extension, change abc.tar.gz to abc_1.tar.gz...
600
		if (strrpos(substr($package_name, 0, -3), '.') !== false)
601
		{
602
			$ext = substr($package_name, strrpos(substr($package_name, 0, -3), '.'));
603
			$package_name = substr($package_name, 0, strrpos(substr($package_name, 0, -3), '.')) . '_';
604
		}
605
		else
606
			$ext = '';
607
608
		// Find the first available.
609
		$i = 1;
610
		while (file_exists($packagesdir . '/' . $package_name . $i . $ext))
611
			$i++;
612
613
		$package_name = $package_name . $i . $ext;
614
	}
615
616
	// Use FTP if necessary.
617
	create_chmod_control(array($packagesdir . '/' . $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));
618
	package_put_contents($packagesdir . '/' . $package_name, fetch_web_data($url . $_REQUEST['package']));
619
620
	// Done!  Did we get this package automatically?
621
	// @ TODO: These are usually update packages.  Allowing both for now until more testing has been done.
622
	if (preg_match('~^https?://[\w_\-]+\.simplemachines\.org/~', $_REQUEST['package']) == 1 && strpos($_REQUEST['package'], 'dlattach') === false && isset($_REQUEST['auto']))
623
		redirectexit('action=admin;area=packages;sa=install;package=' . $package_name);
624
625
	// You just downloaded a mod from SERVER_NAME_GOES_HERE.
626
	$context['package_server'] = $server;
627
628
	$context['package'] = getPackageInfo($package_name);
629
630
	if (!is_array($context['package']))
631
		fatal_lang_error('package_cant_download', false);
632
633
	if (!isset($context['package']['type']))
634
		$context['package']['install']['link'] = '';
635
	else
636
		$context['package']['install']['link'] = '<a href="' . $scripturl . '?action=admin;area=packages;sa=install;package=' . $context['package']['filename'] . '">[ ' . (isset($txt['install_' . $context['package']['type']]) ? $txt['install_' . $context['package']['type']] : $txt['install_unknown']) . ' ]</a>';
637
638
	// Does a 3rd party hook want to do some additional changes?
639
	call_integration_hook('integrate_package_download');
640
641
	$context['package']['list_files']['link'] = '<a href="' . $scripturl . '?action=admin;area=packages;sa=list;package=' . $context['package']['filename'] . '">[ ' . $txt['list_files'] . ' ]</a>';
642
643
	// Free a little bit of memory...
644
	unset($context['package']['xml']);
645
646
	$context['page_title'] = $txt['download_success'];
647
}
648
649
/**
650
 * Upload a new package to the directory.
651
 */
652
function PackageUpload()
653
{
654
	global $txt, $scripturl, $context, $packagesdir;
655
656
	// Setup the correct template, even though I'll admit we ain't downloading ;)
657
	$context['sub_template'] = 'downloaded';
658
659
	// @todo Use FTP if the Packages directory is not writable.
660
661
	// Check the file was even sent!
662
	if (!isset($_FILES['package']['name']) || $_FILES['package']['name'] == '')
663
		fatal_lang_error('package_upload_error_nofile', false);
664
	elseif (!is_uploaded_file($_FILES['package']['tmp_name']) || (ini_get('open_basedir') == '' && !file_exists($_FILES['package']['tmp_name'])))
665
		fatal_lang_error('package_upload_error_failed', false);
666
667
	// Make sure it has a sane filename.
668
	$_FILES['package']['name'] = preg_replace(array('/\s/', '/\.[\.]+/', '/[^\w_\.\-]/'), array('_', '.', ''), $_FILES['package']['name']);
669
670
	$found_ext = preg_match('/\.(zip|tgz|tar\.gz)$/i', $_FILES['package']['name'], $match);
671
	if ($found_ext === 0)
672
		fatal_lang_error('package_upload_error_supports', false, array('zip, tgz, tar.gz'));
673
674
	// We only need the filename...
675
	$packageName = substr($_FILES['package']['name'], 0, -strlen($match[0]));
676
	$packageFileName = package_unique_filename($packagesdir, $packageName, $match[1]) . $match[0];
677
678
	// Setup the destination and throw an error if the file is already there!
679
	$destination = $packagesdir . '/' . $packageFileName;
680
	if (file_exists($destination))
681
		fatal_lang_error('package_upload_error_exists', false);
682
683
	// Now move the file.
684
	move_uploaded_file($_FILES['package']['tmp_name'], $destination);
685
	smf_chmod($destination, 0777);
686
687
	// If we got this far that should mean it's available.
688
	$context['package'] = getPackageInfo($packageFileName);
689
	$context['package_server'] = '';
690
691
	// Not really a package, you lazy bum!
692
	if (!is_array($context['package']))
693
	{
694
		@unlink($destination);
695
		loadLanguage('Errors');
696
		$txt[$context['package']] = str_replace('{MANAGETHEMEURL}', $scripturl . '?action=admin;area=theme;sa=admin;' . $context['session_var'] . '=' . $context['session_id'] . '#theme_install', $txt[$context['package']]);
697
		fatal_lang_error('package_upload_error_broken', false, array($txt[$context['package']]));
698
	}
699
	// Is it already uploaded, maybe?
700
	elseif ($dir = @opendir($packagesdir))
701
	{
702
		while ($package = readdir($dir))
703
		{
704
			if ($package == '.' || $package == '..' || $package == 'temp' || $package == $packageFileName || (!(is_dir($packagesdir . '/' . $package) && file_exists($packagesdir . '/' . $package . '/package-info.xml')) && substr(strtolower($package), -7) != '.tar.gz' && substr(strtolower($package), -4) != '.tgz' && substr(strtolower($package), -4) != '.zip'))
705
				continue;
706
707
			$packageInfo = getPackageInfo($package);
708
			if (!is_array($packageInfo))
709
				continue;
710
711
			if ($packageInfo['id'] == $context['package']['id'] && $packageInfo['version'] == $context['package']['version'])
712
			{
713
				@unlink($destination);
714
				loadLanguage('Errors');
715
				fatal_lang_error('package_upload_error_exists', false);
716
			}
717
		}
718
		closedir($dir);
719
	}
720
721
	if (!isset($context['package']['type']))
722
		$context['package']['install']['link'] = '';
723
	else
724
		$context['package']['install']['link'] = '<a href="' . $scripturl . '?action=admin;area=packages;sa=install;package=' . $context['package']['filename'] . '">[ ' . (isset($txt['install_' . $context['package']['type']]) ? $txt['install_' . $context['package']['type']] : $txt['install_unknown']) . ' ]</a>';
725
726
	// Does a 3rd party hook want to do some additional changes?
727
	call_integration_hook('integrate_package_upload');
728
729
	$context['package']['list_files']['link'] = '<a href="' . $scripturl . '?action=admin;area=packages;sa=list;package=' . $context['package']['filename'] . '">[ ' . $txt['list_files'] . ' ]</a>';
730
731
	unset($context['package']['xml']);
732
733
	$context['page_title'] = $txt['package_uploaded_success'];
734
}
735
736
/**
737
 * Add a package server to the list.
738
 */
739
function PackageServerAdd()
740
{
741
	global $smcFunc;
742
743
	// Validate the user.
744
	checkSession();
745
746
	// If they put a slash on the end, get rid of it.
747
	if (substr($_POST['serverurl'], -1) == '/')
748
		$_POST['serverurl'] = substr($_POST['serverurl'], 0, -1);
749
750
	// Are they both nice and clean?
751
	$servername = trim($smcFunc['htmlspecialchars']($_POST['servername']));
752
	$serverurl = trim($smcFunc['htmlspecialchars']($_POST['serverurl']));
753
754
	// Make sure the URL has the correct prefix.
755
	if (strpos($serverurl, 'http://') !== 0 && strpos($serverurl, 'https://') !== 0)
756
		$serverurl = 'http://' . $serverurl;
757
758
	$smcFunc['db_insert']('',
759
		'{db_prefix}package_servers',
760
		array(
761
			'name' => 'string-255', 'url' => 'string-255',
762
		),
763
		array(
764
			$servername, $serverurl,
765
		),
766
		array('id_server')
767
	);
768
769
	redirectexit('action=admin;area=packages;get');
770
}
771
772
/**
773
 * Remove a server from the list.
774
 */
775
function PackageServerRemove()
776
{
777
	global $smcFunc;
778
779
	checkSession('get');
780
781
	$smcFunc['db_query']('', '
782
		DELETE FROM {db_prefix}package_servers
783
		WHERE id_server = {int:current_server}',
784
		array(
785
			'current_server' => (int) $_GET['server'],
786
		)
787
	);
788
789
	redirectexit('action=admin;area=packages;get');
790
}
791
792
?>