Passed
Push — release-2.1 ( 0c2197...207d2d )
by Jeremy
05:47
created

PackageGet.php ➔ PackageServers()   F

Complexity

Conditions 23
Paths > 20000

Size

Total Lines 110

Duplication

Lines 22
Ratio 20 %

Importance

Changes 0
Metric Value
cc 23
nc 32788
nop 0
dl 22
loc 110
rs 0
c 0
b 0
f 0

How to fix   Long Method    Complexity   

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 http://www.simplemachines.org
10
 * @copyright 2018 Simple Machines and individual contributors
11
 * @license http://www.simplemachines.org/about/smf/license.php BSD
12
 *
13
 * @version 2.1 Beta 4
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, $forum_version, $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
		// Clear any "absolute" URL.  Since "server" is present, "absolute" is garbage.
232
		unset($_GET['absolute']);
233
	}
234
	elseif (isset($_GET['absolute']) && $_GET['absolute'] != '')
235
	{
236
		// Initialize the requried variables.
237
		$server = '';
238
		$url = $_GET['absolute'];
239
		$name = '';
240
		$_GET['package'] = $url . '/packages.xml?language=' . $context['user']['language'];
241
242
		// Clear any "relative" URL.  Since "server" is not present, "relative" is garbage.
243
		unset($_GET['relative']);
244
245
		$token = checkConfirm('get_absolute_url');
246
		if ($token !== true)
247
		{
248
			$context['sub_template'] = 'package_confirm';
249
250
			$context['page_title'] = $txt['package_servers'];
251
			$context['confirm_message'] = sprintf($txt['package_confirm_view_package_content'], $smcFunc['htmlspecialchars']($_GET['absolute']));
252
			$context['proceed_href'] = $scripturl . '?action=admin;area=packages;get;sa=browse;absolute=' . urlencode($_GET['absolute']) . ';confirm=' . $token;
253
254
			return;
255
		}
256
	}
257
	// Minimum required parameter did not exist so dump out.
258
	else
259
		fatal_lang_error('couldnt_connect', false);
260
261
	// Attempt to connect.  If unsuccessful... try the URL.
262
	if (!isset($_GET['package']) || file_exists($_GET['package']))
263
		$_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...
264
265
	// Check to be sure the packages.xml file actually exists where it is should be... or dump out.
266
	if ((isset($_GET['absolute']) || isset($_GET['relative'])) && !url_exists($_GET['package']))
267
		fatal_lang_error('packageget_unable', false, array($url . '/index.php'));
268
269
	// Might take some time.
270
	@set_time_limit(600);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for set_time_limit(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

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

270
	/** @scrutinizer ignore-unhandled */ @set_time_limit(600);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
271
272
	// Read packages.xml and parse into xmlArray. (the true tells it to trim things ;).)
273
	require_once($sourcedir . '/Class-Package.php');
274
	$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

274
	$listing = new xmlArray(/** @scrutinizer ignore-type */ fetch_web_data($_GET['package']), true);
Loading history...
275
276
	// Errm.... empty file?  Try the URL....
277
	if (!$listing->exists('package-list'))
278
		fatal_lang_error('packageget_unable', false, array($url . '/index.php'));
279
280
	// List out the packages...
281
	$context['package_list'] = array();
282
283
	$listing = $listing->path('package-list[0]');
284
285
	// Use the package list's name if it exists.
286
	if ($listing->exists('list-title'))
287
		$name = $smcFunc['htmlspecialchars']($listing->fetch('list-title'));
288
289
	// Pick the correct template.
290
	$context['sub_template'] = 'package_list';
291
292
	$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...
293
	$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...
294
295
	// By default we use an unordered list, unless there are no lists with more than one package.
296
	$context['list_type'] = 'ul';
297
298
	$instmods = loadInstalledPackages();
299
300
	$installed_mods = array();
301
	// Look through the list of installed mods...
302
	foreach ($instmods as $installed_mod)
303
		$installed_mods[$installed_mod['package_id']] = $installed_mod['version'];
304
305
	// Get default author and email if they exist.
306
	if ($listing->exists('default-author'))
307
	{
308
		$default_author = $smcFunc['htmlspecialchars']($listing->fetch('default-author'));
309
		if ($listing->exists('default-author/@email') && filter_var($listing->fetch('default-author/@email'), FILTER_VALIDATE_EMAIL))
310
			$default_email = $smcFunc['htmlspecialchars']($listing->fetch('default-author/@email'));
311
	}
312
313
	// Get default web site if it exists.
314
	if ($listing->exists('default-website'))
315
	{
316
		$default_website = $smcFunc['htmlspecialchars']($listing->fetch('default-website'));
317
		if ($listing->exists('default-website/@title'))
318
			$default_title = $smcFunc['htmlspecialchars']($listing->fetch('default-website/@title'));
319
	}
320
321
	$the_version = strtr($forum_version, array('SMF ' => ''));
322
	if (!empty($_SESSION['version_emulate']))
323
		$the_version = $_SESSION['version_emulate'];
324
325
	$packageNum = 0;
326
	$packageSection = 0;
327
328
	$sections = $listing->set('section');
329
	foreach ($sections as $i => $section)
330
	{
331
		$context['package_list'][$packageSection] = array(
332
			'title' => '',
333
			'text' => '',
334
			'items' => array(),
335
		);
336
337
		$packages = $section->set('title|heading|text|remote|rule|modification|language|avatar-pack|theme|smiley-set');
338
		foreach ($packages as $thisPackage)
339
		{
340
			$package = array(
341
				'type' => $thisPackage->name(),
342
			);
343
344
			if (in_array($package['type'], array('title', 'text')))
345
				$context['package_list'][$packageSection][$package['type']] = $smcFunc['htmlspecialchars']($thisPackage->fetch('.'));
346
			// It's a Title, Heading, Rule or Text.
347
			elseif (in_array($package['type'], array('heading', 'rule')))
348
				$package['name'] = $smcFunc['htmlspecialchars']($thisPackage->fetch('.'));
349
			// It's a Remote link.
350
			elseif ($package['type'] == 'remote')
351
			{
352
				$remote_type = $thisPackage->exists('@type') ? $thisPackage->fetch('@type') : 'relative';
353
354
				if ($remote_type == 'relative' && substr($thisPackage->fetch('@href'), 0, 7) != 'http://' && substr($thisPackage->fetch('@href'), 0, 8) != 'https://')
355
				{
356
					if (isset($_GET['absolute']))
357
						$current_url = $_GET['absolute'] . '/';
358
					elseif (isset($_GET['relative']))
359
						$current_url = $_GET['relative'] . '/';
360
					else
361
						$current_url = '';
362
363
					$current_url .= $thisPackage->fetch('@href');
364
					if (isset($_GET['absolute']))
365
						$package['href'] = $scripturl . '?action=admin;area=packages;get;sa=browse;absolute=' . $current_url;
366
					else
367
						$package['href'] = $scripturl . '?action=admin;area=packages;get;sa=browse;server=' . $context['package_server'] . ';relative=' . $current_url;
368
				}
369
				else
370
				{
371
					$current_url = $thisPackage->fetch('@href');
372
					$package['href'] = $scripturl . '?action=admin;area=packages;get;sa=browse;absolute=' . $current_url;
373
				}
374
375
				$package['name'] = $smcFunc['htmlspecialchars']($thisPackage->fetch('.'));
376
				$package['link'] = '<a href="' . $package['href'] . '">' . $package['name'] . '</a>';
377
			}
378
			// It's a package...
379
			else
380
			{
381
				if (isset($_GET['absolute']))
382
					$current_url = $_GET['absolute'] . '/';
383
				elseif (isset($_GET['relative']))
384
					$current_url = $_GET['relative'] . '/';
385
				else
386
					$current_url = '';
387
388
				$server_att = $server != '' ? ';server=' . $server : '';
389
390
				$package += $thisPackage->to_array();
391
392
				if (isset($package['website']))
393
					unset($package['website']);
394
				$package['author'] = array();
395
396
				if ($package['description'] == '')
397
					$package['description'] = $txt['package_no_description'];
398
				else
399
					$package['description'] = parse_bbc(preg_replace('~\[[/]?html\]~i', '', $smcFunc['htmlspecialchars']($package['description'])));
400
401
				$package['is_installed'] = isset($installed_mods[$package['id']]);
402
				$package['is_current'] = $package['is_installed'] && ($installed_mods[$package['id']] == $package['version']);
403
				$package['is_newer'] = $package['is_installed'] && ($installed_mods[$package['id']] > $package['version']);
404
405
				// This package is either not installed, or installed but old.  Is it supported on this version of SMF?
406
				if (!$package['is_installed'] || (!$package['is_current'] && !$package['is_newer']))
407
				{
408
					if ($thisPackage->exists('version/@for'))
409
						$package['can_install'] = matchPackageVersion($the_version, $thisPackage->fetch('version/@for'));
410
				}
411
				// Okay, it's already installed AND up to date.
412
				else
413
					$package['can_install'] = false;
414
415
				$already_exists = getPackageInfo(basename($package['filename']));
416
				$package['download_conflict'] = is_array($already_exists) && $already_exists['id'] == $package['id'] && $already_exists['version'] != $package['version'];
417
418
				$package['href'] = $url . '/' . $package['filename'];
419
				$package['link'] = '<a href="' . $package['href'] . '">' . $package['name'] . '</a>';
420
				$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'];
421
				$package['download']['link'] = '<a href="' . $package['download']['href'] . '">' . $package['name'] . '</a>';
422
423
				if ($thisPackage->exists('author') || isset($default_author))
424
				{
425
					if ($thisPackage->exists('author/@email') && filter_var($thisPackage->fetch('author/@email'), FILTER_VALIDATE_EMAIL))
426
						$package['author']['email'] = $thisPackage->fetch('author/@email');
427
					elseif (isset($default_email))
428
						$package['author']['email'] = $default_email;
429
430
					if ($thisPackage->exists('author') && $thisPackage->fetch('author') != '')
431
						$package['author']['name'] = $smcFunc['htmlspecialchars']($thisPackage->fetch('author'));
432
					else
433
						$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...
434
435
					if (!empty($package['author']['email']))
436
						$package['author']['link'] = '<a href="mailto:' . $package['author']['email'] . '">' . $package['author']['name'] . '</a>';
437
				}
438
439
				if ($thisPackage->exists('website') || isset($default_website))
440
				{
441
					if ($thisPackage->exists('website') && $thisPackage->exists('website/@title'))
442
						$package['author']['website']['name'] = $smcFunc['htmlspecialchars']($thisPackage->fetch('website/@title'));
443
					elseif (isset($default_title))
444
						$package['author']['website']['name'] = $default_title;
445
					elseif ($thisPackage->exists('website'))
446
						$package['author']['website']['name'] = $smcFunc['htmlspecialchars']($thisPackage->fetch('website'));
447
					else
448
						$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...
449
450
					if ($thisPackage->exists('website') && $thisPackage->fetch('website') != '')
451
						$authorhompage = $smcFunc['htmlspecialchars']($thisPackage->fetch('website'));
452
					else
453
						$authorhompage = $default_website;
454
455
					$package['author']['website']['href'] = $authorhompage;
456
					$package['author']['website']['link'] = '<a href="' . $authorhompage . '">' . $package['author']['website']['name'] . '</a>';
457
				}
458
				else
459
				{
460
					$package['author']['website']['href'] = '';
461
					$package['author']['website']['link'] = '';
462
				}
463
			}
464
465
			$package['is_remote'] = $package['type'] == 'remote';
466
			$package['is_title'] = $package['type'] == 'title';
467
			$package['is_heading'] = $package['type'] == 'heading';
468
			$package['is_text'] = $package['type'] == 'text';
469
			$package['is_line'] = $package['type'] == 'rule';
470
471
			$packageNum = in_array($package['type'], array('title', 'heading', 'text', 'remote', 'rule')) ? 0 : $packageNum + 1;
472
			$package['count'] = $packageNum;
473
474
			if (!in_array($package['type'], array('title', 'text')))
475
				$context['package_list'][$packageSection]['items'][] = $package;
476
477
			if ($package['count'] > 1)
478
				$context['list_type'] = 'ol';
479
		}
480
481
		$packageSection++;
482
	}
483
484
	// Lets make sure we get a nice new spiffy clean $package to work with.  Otherwise we get PAIN!
485
	unset($package);
486
487
	foreach ($context['package_list'] as $ps_id => $packageSection)
488
	{
489
		foreach ($packageSection['items'] as $i => $package)
490
		{
491
			if ($package['count'] == 0 || isset($package['can_install']))
492
				continue;
493
494
			$context['package_list'][$ps_id]['items'][$i]['can_install'] = false;
495
496
			$packageInfo = getPackageInfo($url . '/' . $package['filename']);
497
			if (is_array($packageInfo) && $packageInfo['xml']->exists('install'))
498
			{
499
				$installs = $packageInfo['xml']->set('install');
500
				foreach ($installs as $install)
501
				{
502
					if (!$install->exists('@for') || matchPackageVersion($the_version, $install->fetch('@for')))
503
					{
504
						// Okay, this one is good to go.
505
						$context['package_list'][$ps_id]['items'][$i]['can_install'] = true;
506
						break;
507
					}
508
509
					// no install found for this version, lets see if one exists for another
510
					if ($context['package_list'][$ps_id]['items'][$i]['can_install'] === false && $install->exists('@for'))
511
					{
512
						$reset = true;
513
514
						// Get the highest install version that is available from the package
515
						foreach ($installs as $install)
0 ignored issues
show
Comprehensibility Bug introduced by
$install is overwriting a variable from outer foreach loop.
Loading history...
516
						{
517
							$context['package_list'][$ps_id]['items'][$i]['can_emulate_install'] = matchHighestPackageVersion($install->fetch('@for'), $reset, $the_version);
518
							$reset = false;
519
						}
520
					}
521
				}
522
			}
523
		}
524
	}
525
}
526
527
/**
528
 * Download a package.
529
 */
530
function PackageDownload()
531
{
532
	global $txt, $scripturl, $context, $packagesdir, $smcFunc;
533
534
	// Use the downloaded sub template.
535
	$context['sub_template'] = 'downloaded';
536
537
	// Security is good...
538
	checkSession('get');
539
540
	// To download something, we need a valid server or url.
541
	if (empty($_GET['server']) && (!empty($_GET['get']) && !empty($_REQUEST['package'])))
542
		fatal_lang_error('package_get_error_is_zero', false);
543
544
	if (isset($_GET['server']))
545
	{
546
		$server = (int) $_GET['server'];
547
548
		// Query the server table to find the requested server.
549
		$request = $smcFunc['db_query']('', '
550
			SELECT name, url
551
			FROM {db_prefix}package_servers
552
			WHERE id_server = {int:current_server}
553
			LIMIT 1',
554
			array(
555
				'current_server' => $server,
556
			)
557
		);
558
		list ($name, $url) = $smcFunc['db_fetch_row']($request);
559
		$smcFunc['db_free_result']($request);
560
561
		// If server does not exist then dump out.
562
		if (empty($url))
563
			fatal_lang_error('couldnt_connect', false);
564
565
		$url = $url . '/';
566
	}
567
	else
568
	{
569
		// Initialize the requried variables.
570
		$server = '';
571
		$url = '';
572
	}
573
574
	if (isset($_REQUEST['byurl']) && !empty($_POST['filename']))
575
		$package_name = basename($_REQUEST['filename']);
576
	else
577
		$package_name = basename($_REQUEST['package']);
578
579
	if (isset($_REQUEST['conflict']) || (isset($_REQUEST['auto']) && file_exists($packagesdir . '/' . $package_name)))
580
	{
581
		// Find the extension, change abc.tar.gz to abc_1.tar.gz...
582
		if (strrpos(substr($package_name, 0, -3), '.') !== false)
583
		{
584
			$ext = substr($package_name, strrpos(substr($package_name, 0, -3), '.'));
585
			$package_name = substr($package_name, 0, strrpos(substr($package_name, 0, -3), '.')) . '_';
586
		}
587
		else
588
			$ext = '';
589
590
		// Find the first available.
591
		$i = 1;
592
		while (file_exists($packagesdir . '/' . $package_name . $i . $ext))
593
			$i++;
594
595
		$package_name = $package_name . $i . $ext;
596
	}
597
598
	// Use FTP if necessary.
599
	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));
600
	package_put_contents($packagesdir . '/' . $package_name, fetch_web_data($url . $_REQUEST['package']));
601
602
	// Done!  Did we get this package automatically?
603
	// @ TODO: These are usually update packages.  Allowing both for now until more testing has been done.
604
	if (preg_match('~^https?://[\w_\-]+\.simplemachines\.org/~', $_REQUEST['package']) == 1 && strpos($_REQUEST['package'], 'dlattach') === false && isset($_REQUEST['auto']))
605
		redirectexit('action=admin;area=packages;sa=install;package=' . $package_name);
606
607
	// You just downloaded a mod from SERVER_NAME_GOES_HERE.
608
	$context['package_server'] = $server;
609
610
	$context['package'] = getPackageInfo($package_name);
611
612
	if (!is_array($context['package']))
613
		fatal_lang_error('package_cant_download', false);
614
615
	if ($context['package']['type'] == 'modification')
616
		$context['package']['install']['link'] = '<a href="' . $scripturl . '?action=admin;area=packages;sa=install;package=' . $context['package']['filename'] . '">[ ' . $txt['install_mod'] . ' ]</a>';
617
	elseif ($context['package']['type'] == 'avatar')
618
		$context['package']['install']['link'] = '<a href="' . $scripturl . '?action=admin;area=packages;sa=install;package=' . $context['package']['filename'] . '">[ ' . $txt['use_avatars'] . ' ]</a>';
619
	elseif ($context['package']['type'] == 'language')
620
		$context['package']['install']['link'] = '<a href="' . $scripturl . '?action=admin;area=packages;sa=install;package=' . $context['package']['filename'] . '">[ ' . $txt['add_languages'] . ' ]</a>';
621
	else
622
		$context['package']['install']['link'] = '';
623
624
	// Does a 3rd party hook want to do some additional changes?
625
	call_integration_hook('integrate_package_download');
626
627
	$context['package']['list_files']['link'] = '<a href="' . $scripturl . '?action=admin;area=packages;sa=list;package=' . $context['package']['filename'] . '">[ ' . $txt['list_files'] . ' ]</a>';
628
629
	// Free a little bit of memory...
630
	unset($context['package']['xml']);
631
632
	$context['page_title'] = $txt['download_success'];
633
}
634
635
/**
636
 * Upload a new package to the directory.
637
 */
638
function PackageUpload()
639
{
640
	global $txt, $scripturl, $context, $packagesdir;
641
642
	// Setup the correct template, even though I'll admit we ain't downloading ;)
643
	$context['sub_template'] = 'downloaded';
644
	$allowext = array('.zip','.tgz','.gz');
645
	// @todo Use FTP if the Packages directory is not writable.
646
647
	// Check the file was even sent!
648
	if (!isset($_FILES['package']['name']) || $_FILES['package']['name'] == '')
649
		fatal_lang_error('package_upload_error_nofile');
650
	elseif (!is_uploaded_file($_FILES['package']['tmp_name']) || (ini_get('open_basedir') == '' && !file_exists($_FILES['package']['tmp_name'])))
651
		fatal_lang_error('package_upload_error_failed');
652
653
	// Make sure it has a sane filename.
654
	$_FILES['package']['name'] = preg_replace(array('/\s/', '/\.[\.]+/', '/[^\w_\.\-]/'), array('_', '.', ''), $_FILES['package']['name']);
655
	$extension = substr(strrchr(strtolower($_FILES['package']['name']), '.'), 0);
656
	if(!in_array($extension, $allowext))
657
	{
658
		fatal_lang_error('package_upload_error_supports', false, array('zip, tgz, tar.gz'));
659
	}
660
661
	// We only need the filename...
662
	$extension = ($extension == '.gz') ? '.tar.gz' : $extension ;
663
	$packageName = time() . $extension;
664
665
	// Setup the destination and throw an error if the file is already there!
666
	$destination = $packagesdir . '/' . $packageName;
667
	// @todo Maybe just roll it like we do for downloads?
668
	if (file_exists($destination))
669
		fatal_lang_error('package_upload_error_exists');
670
671
	// Now move the file.
672
	move_uploaded_file($_FILES['package']['tmp_name'], $destination);
673
	smf_chmod($destination, 0777);
674
675
	// If we got this far that should mean it's available.
676
	$context['package'] = getPackageInfo($packageName);
677
	$context['package_server'] = '';
678
679
	// Not really a package, you lazy bum!
680
	if (!is_array($context['package']))
681
	{
682
		@unlink($destination);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for unlink(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

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

682
		/** @scrutinizer ignore-unhandled */ @unlink($destination);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
683
		loadLanguage('Errors');
684
		$txt[$context['package']] = str_replace('{MANAGETHEMEURL}', $scripturl . '?action=admin;area=theme;sa=admin;' . $context['session_var'] . '=' . $context['session_id'] . '#theme_install', $txt[$context['package']]);
685
		fatal_lang_error('package_upload_error_broken', false, $txt[$context['package']]);
686
	}
687
	// Is it already uploaded, maybe?
688
	elseif ($dir = @opendir($packagesdir))
689
	{
690
		while ($package = readdir($dir))
691
		{
692
			if ($package == '.' || $package == '..' || $package == 'temp' || $package == $packageName || (!(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'))
693
				continue;
694
695
			$packageInfo = getPackageInfo($package);
696
			if (!is_array($packageInfo))
697
				continue;
698
699
			if ($packageInfo['id'] == $context['package']['id'] && $packageInfo['version'] == $context['package']['version'])
700
			{
701
				@unlink($destination);
702
				loadLanguage('Errors');
703
				fatal_lang_error('package_upload_error_exists');
704
			}
705
		}
706
		closedir($dir);
707
	}
708
709
	if ($context['package']['type'] == 'modification')
710
		$context['package']['install']['link'] = '<a href="' . $scripturl . '?action=admin;area=packages;sa=install;package=' . $context['package']['filename'] . '">[ ' . $txt['install_mod'] . ' ]</a>';
711
	elseif ($context['package']['type'] == 'avatar')
712
		$context['package']['install']['link'] = '<a href="' . $scripturl . '?action=admin;area=packages;sa=install;package=' . $context['package']['filename'] . '">[ ' . $txt['use_avatars'] . ' ]</a>';
713
	elseif ($context['package']['type'] == 'language')
714
		$context['package']['install']['link'] = '<a href="' . $scripturl . '?action=admin;area=packages;sa=install;package=' . $context['package']['filename'] . '">[ ' . $txt['add_languages'] . ' ]</a>';
715
	else
716
		$context['package']['install']['link'] = '';
717
718
	// Does a 3rd party hook want to do some additional changes?
719
	call_integration_hook('integrate_package_upload');
720
721
	$context['package']['list_files']['link'] = '<a href="' . $scripturl . '?action=admin;area=packages;sa=list;package=' . $context['package']['filename'] . '">[ ' . $txt['list_files'] . ' ]</a>';
722
723
	unset($context['package']['xml']);
724
725
	$context['page_title'] = $txt['package_uploaded_success'];
726
}
727
728
/**
729
 * Add a package server to the list.
730
 */
731
function PackageServerAdd()
732
{
733
	global $smcFunc;
734
735
	// Validate the user.
736
	checkSession();
737
738
	// If they put a slash on the end, get rid of it.
739
	if (substr($_POST['serverurl'], -1) == '/')
740
		$_POST['serverurl'] = substr($_POST['serverurl'], 0, -1);
741
742
	// Are they both nice and clean?
743
	$servername = trim($smcFunc['htmlspecialchars']($_POST['servername']));
744
	$serverurl = trim($smcFunc['htmlspecialchars']($_POST['serverurl']));
745
746
	// Make sure the URL has the correct prefix.
747
	if (strpos($serverurl, 'http://') !== 0 && strpos($serverurl, 'https://') !== 0)
748
		$serverurl = 'http://' . $serverurl;
749
750
	$smcFunc['db_insert']('',
751
		'{db_prefix}package_servers',
752
		array(
753
			'name' => 'string-255', 'url' => 'string-255',
754
		),
755
		array(
756
			$servername, $serverurl,
757
		),
758
		array('id_server')
759
	);
760
761
	redirectexit('action=admin;area=packages;get');
762
}
763
764
/**
765
 * Remove a server from the list.
766
 */
767
function PackageServerRemove()
768
{
769
	global $smcFunc;
770
771
	checkSession('get');
772
773
	$smcFunc['db_query']('', '
774
		DELETE FROM {db_prefix}package_servers
775
		WHERE id_server = {int:current_server}',
776
		array(
777
			'current_server' => (int) $_GET['server'],
778
		)
779
	);
780
781
	redirectexit('action=admin;area=packages;get');
782
}
783
784
?>