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

Packages.php ➔ ViewOperations()   F

Complexity

Conditions 23
Paths 2560

Size

Total Lines 113

Duplication

Lines 27
Ratio 23.89 %

Importance

Changes 0
Metric Value
cc 23
nc 2560
nop 0
dl 27
loc 113
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 is the main 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
 * This is the notoriously defunct package manager..... :/.
21
 */
22
function Packages()
23
{
24
	global $txt, $sourcedir, $context;
25
26
	// @todo Remove this!
27
	if (isset($_GET['get']) || isset($_GET['pgdownload']))
28
	{
29
		require_once($sourcedir . '/PackageGet.php');
30
		return PackageGet();
31
	}
32
33
	isAllowedTo('admin_forum');
34
35
	// Load all the basic stuff.
36
	require_once($sourcedir . '/Subs-Package.php');
37
	loadLanguage('Packages');
38
	loadTemplate('Packages', 'admin');
39
40
	$context['page_title'] = $txt['package'];
41
42
	// Delegation makes the world... that is, the package manager go 'round.
43
	$subActions = array(
44
		'browse' => 'PackageBrowse',
45
		'remove' => 'PackageRemove',
46
		'list' => 'PackageList',
47
		'ftptest' => 'PackageFTPTest',
48
		'install' => 'PackageInstallTest',
49
		'install2' => 'PackageInstall',
50
		'uninstall' => 'PackageInstallTest',
51
		'uninstall2' => 'PackageInstall',
52
		'options' => 'PackageOptions',
53
		'perms' => 'PackagePermissions',
54
		'flush' => 'FlushInstall',
55
		'examine' => 'ExamineFile',
56
		'showoperations' => 'ViewOperations',
57
	);
58
59
	// Work out exactly who it is we are calling.
60
	if (isset($_REQUEST['sa']) && isset($subActions[$_REQUEST['sa']]))
61
		$context['sub_action'] = $_REQUEST['sa'];
62
	else
63
		$context['sub_action'] = 'browse';
64
65
	// Set up some tabs...
66
	$context[$context['admin_menu_name']]['tab_data'] = array(
67
		'title' => $txt['package_manager'],
68
		// @todo 'help' => 'registrations',
69
		'description' => $txt['package_manager_desc'],
70
		'tabs' => array(
71
			'browse' => array(
72
			),
73
			'packageget' => array(
74
				'description' => $txt['download_packages_desc'],
75
			),
76
			'perms' => array(
77
				'description' => $txt['package_file_perms_desc'],
78
			),
79
			'options' => array(
80
				'description' => $txt['package_install_options_desc'],
81
			),
82
		),
83
	);
84
85
	if ($context['sub_action'] == 'browse')
86
		loadJavaScriptFile('suggest.js', array('defer' => false, 'minimize' => true), 'smf_suggest');
87
88
	call_integration_hook('integrate_manage_packages', array(&$subActions));
89
90
	// Call the function we're handing control to.
91
	call_helper($subActions[$context['sub_action']]);
92
}
93
94
/**
95
 * Test install a package.
96
 */
97
function PackageInstallTest()
98
{
99
	global $boarddir, $txt, $context, $scripturl, $sourcedir, $packagesdir, $modSettings, $smcFunc, $settings;
100
101
	// You have to specify a file!!
102
	if (!isset($_REQUEST['package']) || $_REQUEST['package'] == '')
103
		redirectexit('action=admin;area=packages');
104
	$context['filename'] = preg_replace('~[\.]+~', '.', $_REQUEST['package']);
105
106
	// Do we have an existing id, for uninstalls and the like.
107
	$context['install_id'] = isset($_REQUEST['pid']) ? (int) $_REQUEST['pid'] : 0;
108
109
	require_once($sourcedir . '/Subs-Package.php');
110
111
	// Load up the package FTP information?
112
	create_chmod_control();
113
114
	// Make sure temp directory exists and is empty.
115
	if (file_exists($packagesdir . '/temp'))
116
		deltree($packagesdir . '/temp', false);
117
118
	if (!mktree($packagesdir . '/temp', 0755))
119
	{
120
		deltree($packagesdir . '/temp', false);
121
		if (!mktree($packagesdir . '/temp', 0777))
122
		{
123
			deltree($packagesdir . '/temp', false);
124
			create_chmod_control(array($packagesdir . '/temp/delme.tmp'), array('destination_url' => $scripturl . '?action=admin;area=packages;sa=' . $_REQUEST['sa'] . ';package=' . $_REQUEST['package'], 'crash_on_error' => true));
125
126
			deltree($packagesdir . '/temp', false);
127
			if (!mktree($packagesdir . '/temp', 0777))
128
				fatal_lang_error('package_cant_download', false);
129
		}
130
	}
131
132
	$context['uninstalling'] = $_REQUEST['sa'] == 'uninstall';
133
134
	// Change our last link tree item for more information on this Packages area.
135
	$context['linktree'][count($context['linktree']) - 1] = array(
136
		'url' => $scripturl . '?action=admin;area=packages;sa=browse',
137
		'name' => $context['uninstalling'] ? $txt['package_uninstall_actions'] : $txt['install_actions']
138
	);
139
	$context['page_title'] .= ' - ' . ($context['uninstalling'] ? $txt['package_uninstall_actions'] : $txt['install_actions']);
140
141
	$context['sub_template'] = 'view_package';
142
143
	if (!file_exists($packagesdir . '/' . $context['filename']))
144
	{
145
		deltree($packagesdir . '/temp');
146
		fatal_lang_error('package_no_file', false);
147
	}
148
149
	// Extract the files so we can get things like the readme, etc.
150
	if (is_file($packagesdir . '/' . $context['filename']))
151
	{
152
		$context['extracted_files'] = read_tgz_file($packagesdir . '/' . $context['filename'], $packagesdir . '/temp');
153
154
		if ($context['extracted_files'] && !file_exists($packagesdir . '/temp/package-info.xml'))
155
			foreach ($context['extracted_files'] as $file)
156
				if (basename($file['filename']) == 'package-info.xml')
157
				{
158
					$context['base_path'] = dirname($file['filename']) . '/';
159
					break;
160
				}
161
162
		if (!isset($context['base_path']))
163
			$context['base_path'] = '';
164
	}
165
	elseif (is_dir($packagesdir . '/' . $context['filename']))
166
	{
167
		copytree($packagesdir . '/' . $context['filename'], $packagesdir . '/temp');
168
		$context['extracted_files'] = listtree($packagesdir . '/temp');
169
		$context['base_path'] = '';
170
	}
171
	else
172
		fatal_lang_error('no_access', false);
173
174
	// Load up any custom themes we may want to install into...
175
	$request = $smcFunc['db_query']('', '
176
		SELECT id_theme, variable, value
177
		FROM {db_prefix}themes
178
		WHERE (id_theme = {int:default_theme} OR id_theme IN ({array_int:known_theme_list}))
179
			AND variable IN ({string:name}, {string:theme_dir})',
180
		array(
181
			'known_theme_list' => explode(',', $modSettings['knownThemes']),
182
			'default_theme' => 1,
183
			'name' => 'name',
184
			'theme_dir' => 'theme_dir',
185
		)
186
	);
187
	$theme_paths = array();
188
	while ($row = $smcFunc['db_fetch_assoc']($request))
189
		$theme_paths[$row['id_theme']][$row['variable']] = $row['value'];
190
	$smcFunc['db_free_result']($request);
191
192
	// Get the package info...
193
	$packageInfo = getPackageInfo($context['filename']);
194
195
	if (!is_array($packageInfo))
196
		fatal_lang_error($packageInfo);
197
198
	$packageInfo['filename'] = $context['filename'];
199
	$context['package_name'] = isset($packageInfo['name']) ? $packageInfo['name'] : $context['filename'];
200
201
	// Set the type of extraction...
202
	$context['extract_type'] = isset($packageInfo['type']) ? $packageInfo['type'] : 'modification';
203
204
	// The mod isn't installed.... unless proven otherwise.
205
	$context['is_installed'] = false;
206
207
	// See if it is installed?
208
	$request = $smcFunc['db_query']('', '
209
		SELECT version, themes_installed, db_changes
210
		FROM {db_prefix}log_packages
211
		WHERE package_id = {string:current_package}
212
			AND install_state != {int:not_installed}
213
		ORDER BY time_installed DESC
214
		LIMIT 1',
215
		array(
216
			'not_installed'	=> 0,
217
			'current_package' => $packageInfo['id'],
218
		)
219
	);
220
221
	while ($row = $smcFunc['db_fetch_assoc']($request))
222
	{
223
		$old_themes = explode(',', $row['themes_installed']);
224
		$old_version = $row['version'];
225
		$db_changes = empty($row['db_changes']) ? array() : $smcFunc['json_decode']($row['db_changes'], true);
226
	}
227
	$smcFunc['db_free_result']($request);
228
229
	$context['database_changes'] = array();
230
	if (isset($packageInfo['uninstall']['database']))
231
		$context['database_changes'][] = $txt['execute_database_changes'] . ' - ' . $packageInfo['uninstall']['database'];
232
	elseif (!empty($db_changes))
233
	{
234
		foreach ($db_changes as $change)
235
		{
236
			if (isset($change[2]) && isset($txt['package_db_' . $change[0]]))
237
				$context['database_changes'][] = sprintf($txt['package_db_' . $change[0]], $change[1], $change[2]);
238
			elseif (isset($txt['package_db_' . $change[0]]))
239
				$context['database_changes'][] = sprintf($txt['package_db_' . $change[0]], $change[1]);
240
			else
241
				$context['database_changes'][] = $change[0] . '-' . $change[1] . (isset($change[2]) ? '-' . $change[2] : '');
242
		}
243
	}
244
245
	// Uninstalling?
246
	if ($context['uninstalling'])
247
	{
248
		// Wait, it's not installed yet!
249
		if (!isset($old_version) && $context['uninstalling'])
250
		{
251
			deltree($packagesdir . '/temp');
252
			fatal_lang_error('package_cant_uninstall', false);
253
		}
254
255
		$actions = parsePackageInfo($packageInfo['xml'], true, 'uninstall');
256
257
		// Gadzooks!  There's no uninstaller at all!?
258
		if (empty($actions))
259
		{
260
			deltree($packagesdir . '/temp');
261
			fatal_lang_error('package_uninstall_cannot', false);
262
		}
263
264
		// Can't edit the custom themes it's edited if you're uninstalling, they must be removed.
265
		$context['themes_locked'] = true;
266
267
		// Only let them uninstall themes it was installed into.
268
		foreach ($theme_paths as $id => $data)
269
			if ($id != 1 && !in_array($id, $old_themes))
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $old_themes does not seem to be defined for all execution paths leading up to this point.
Loading history...
270
				unset($theme_paths[$id]);
271
	}
272
	elseif (isset($old_version) && $old_version != $packageInfo['version'])
273
	{
274
		// Look for an upgrade...
275
		$actions = parsePackageInfo($packageInfo['xml'], true, 'upgrade', $old_version);
276
277
		// There was no upgrade....
278
		if (empty($actions))
279
			$context['is_installed'] = true;
280
		else
281
		{
282
			// Otherwise they can only upgrade themes from the first time around.
283
			foreach ($theme_paths as $id => $data)
284
				if ($id != 1 && !in_array($id, $old_themes))
285
					unset($theme_paths[$id]);
286
		}
287
	}
288
	elseif (isset($old_version) && $old_version == $packageInfo['version'])
289
		$context['is_installed'] = true;
290
291
	if (!isset($old_version) || $context['is_installed'])
292
		$actions = parsePackageInfo($packageInfo['xml'], true, 'install');
293
294
	$context['actions'] = array();
295
	$context['ftp_needed'] = false;
296
	$context['has_failure'] = false;
297
	$chmod_files = array();
298
299
	// no actions found, return so we can display an error
300
	if (empty($actions))
301
		return;
302
303
	// This will hold data about anything that can be installed in other themes.
304
	$themeFinds = array(
305
		'candidates' => array(),
306
		'other_themes' => array(),
307
	);
308
309
	// Now prepare things for the template.
310
	foreach ($actions as $action)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $actions does not seem to be defined for all execution paths leading up to this point.
Loading history...
311
	{
312
		// Not failed until proven otherwise.
313
		$failed = false;
314
		$thisAction = array();
315
316
		if ($action['type'] == 'chmod')
317
		{
318
			$chmod_files[] = $action['filename'];
319
			continue;
320
		}
321
		elseif ($action['type'] == 'readme' || $action['type'] == 'license')
322
		{
323
			$type = 'package_' . $action['type'];
324
			if (file_exists($packagesdir . '/temp/' . $context['base_path'] . $action['filename']))
325
				$context[$type] = $smcFunc['htmlspecialchars'](trim(file_get_contents($packagesdir . '/temp/' . $context['base_path'] . $action['filename']), "\n\r"));
326
			elseif (file_exists($action['filename']))
327
				$context[$type] = $smcFunc['htmlspecialchars'](trim(file_get_contents($action['filename']), "\n\r"));
328
329
			if (!empty($action['parse_bbc']))
330
			{
331
				require_once($sourcedir . '/Subs-Post.php');
332
				$context[$type] = preg_replace('~\[[/]?html\]~i', '', $context[$type]);
333
				preparsecode($context[$type]);
334
				$context[$type] = parse_bbc($context[$type]);
335
			}
336
			else
337
				$context[$type] = nl2br($context[$type]);
338
339
			continue;
340
		}
341
		// Don't show redirects.
342
		elseif ($action['type'] == 'redirect')
343
			continue;
344
		elseif ($action['type'] == 'error')
345
		{
346
			$context['has_failure'] = true;
347
			if (isset($action['error_msg']) && isset($action['error_var']))
348
				$context['failure_details'] = sprintf($txt['package_will_fail_' . $action['error_msg']], $action['error_var']);
349
			elseif (isset($action['error_msg']))
350
				$context['failure_details'] = isset($txt['package_will_fail_' . $action['error_msg']]) ? $txt['package_will_fail_' . $action['error_msg']] : $action['error_msg'];
351
		}
352
		elseif ($action['type'] == 'modification')
353
		{
354
			if (!file_exists($packagesdir . '/temp/' . $context['base_path'] . $action['filename']))
355
			{
356
				$context['has_failure'] = true;
357
358
				$context['actions'][] = array(
359
					'type' => $txt['execute_modification'],
360
					'action' => $smcFunc['htmlspecialchars'](strtr($action['filename'], array($boarddir => '.'))),
361
					'description' => $txt['package_action_missing'],
362
					'failed' => true,
363
				);
364
			}
365
			else
366
			{
367
				if ($action['boardmod'])
368
					$mod_actions = parseBoardMod(@file_get_contents($packagesdir . '/temp/' . $context['base_path'] . $action['filename']), true, $action['reverse'], $theme_paths);
369
				else
370
					$mod_actions = parseModification(@file_get_contents($packagesdir . '/temp/' . $context['base_path'] . $action['filename']), true, $action['reverse'], $theme_paths);
371
372
				if (count($mod_actions) == 1 && isset($mod_actions[0]) && $mod_actions[0]['type'] == 'error' && $mod_actions[0]['filename'] == '-')
373
					$mod_actions[0]['filename'] = $action['filename'];
374
375
				foreach ($mod_actions as $key => $mod_action)
376
				{
377
					// Lets get the last section of the file name.
378
					if (isset($mod_action['filename']) && substr($mod_action['filename'], -13) != '.template.php')
379
						$actual_filename = strtolower(substr(strrchr($mod_action['filename'], '/'), 1) . '||' . $action['filename']);
380
					elseif (isset($mod_action['filename']) && preg_match('~([\w]*)/([\w]*)\.template\.php$~', $mod_action['filename'], $matches))
381
						$actual_filename = strtolower($matches[1] . '/' . $matches[2] . '.template.php' . '||' . $action['filename']);
382
					else
383
						$actual_filename = $key;
384
385
					if ($mod_action['type'] == 'opened')
386
						$failed = false;
387
					elseif ($mod_action['type'] == 'failure')
388
					{
389
						if (empty($mod_action['is_custom']))
390
							$context['has_failure'] = true;
391
						$failed = true;
392
					}
393
					elseif ($mod_action['type'] == 'chmod')
394
					{
395
						$chmod_files[] = $mod_action['filename'];
396
					}
397
					elseif ($mod_action['type'] == 'saved')
398
					{
399
						if (!empty($mod_action['is_custom']))
400
						{
401
							if (!isset($context['theme_actions'][$mod_action['is_custom']]))
402
								$context['theme_actions'][$mod_action['is_custom']] = array(
403
									'name' => $theme_paths[$mod_action['is_custom']]['name'],
404
									'actions' => array(),
405
									'has_failure' => $failed,
406
								);
407
							else
408
								$context['theme_actions'][$mod_action['is_custom']]['has_failure'] |= $failed;
409
410
							$context['theme_actions'][$mod_action['is_custom']]['actions'][$actual_filename] = array(
411
								'type' => $txt['execute_modification'],
412
								'action' => $smcFunc['htmlspecialchars'](strtr($mod_action['filename'], array($boarddir => '.'))),
413
								'description' => $failed ? $txt['package_action_failure'] : $txt['package_action_success'],
414
								'failed' => $failed,
415
							);
416
						}
417
						elseif (!isset($context['actions'][$actual_filename]))
418
						{
419
							$context['actions'][$actual_filename] = array(
420
								'type' => $txt['execute_modification'],
421
								'action' => $smcFunc['htmlspecialchars'](strtr($mod_action['filename'], array($boarddir => '.'))),
422
								'description' => $failed ? $txt['package_action_failure'] : $txt['package_action_success'],
423
								'failed' => $failed,
424
							);
425
						}
426
						else
427
						{
428
								$context['actions'][$actual_filename]['failed'] |= $failed;
429
								$context['actions'][$actual_filename]['description'] = $context['actions'][$actual_filename]['failed'] ? $txt['package_action_failure'] : $txt['package_action_success'];
430
						}
431
					}
432
					elseif ($mod_action['type'] == 'skipping')
433
					{
434
						$context['actions'][$actual_filename] = array(
435
							'type' => $txt['execute_modification'],
436
							'action' => $smcFunc['htmlspecialchars'](strtr($mod_action['filename'], array($boarddir => '.'))),
437
							'description' => $txt['package_action_skipping']
438
						);
439
					}
440
					elseif ($mod_action['type'] == 'missing' && empty($mod_action['is_custom']))
441
					{
442
						$context['has_failure'] = true;
443
						$context['actions'][$actual_filename] = array(
444
							'type' => $txt['execute_modification'],
445
							'action' => $smcFunc['htmlspecialchars'](strtr($mod_action['filename'], array($boarddir => '.'))),
446
							'description' => $txt['package_action_missing'],
447
							'failed' => true,
448
						);
449
					}
450
					elseif ($mod_action['type'] == 'error')
451
						$context['actions'][$actual_filename] = array(
452
							'type' => $txt['execute_modification'],
453
							'action' => $smcFunc['htmlspecialchars'](strtr($mod_action['filename'], array($boarddir => '.'))),
454
							'description' => $txt['package_action_error'],
455
							'failed' => true,
456
						);
457
				}
458
459
				// We need to loop again just to get the operations down correctly.
460
				foreach ($mod_actions as $operation_key => $mod_action)
461
				{
462
					// Lets get the last section of the file name.
463
					if (isset($mod_action['filename']) && substr($mod_action['filename'], -13) != '.template.php')
464
						$actual_filename = strtolower(substr(strrchr($mod_action['filename'], '/'), 1) . '||' . $action['filename']);
465
					elseif (isset($mod_action['filename']) && preg_match('~([\w]*)/([\w]*)\.template\.php$~', $mod_action['filename'], $matches))
466
						$actual_filename = strtolower($matches[1] . '/' . $matches[2] . '.template.php' . '||' . $action['filename']);
467
					else
468
						$actual_filename = $key;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $key does not seem to be defined for all execution paths leading up to this point.
Loading history...
469
470
					// We just need it for actual parse changes.
471
					if (!in_array($mod_action['type'], array('error', 'result', 'opened', 'saved', 'end', 'missing', 'skipping', 'chmod')))
472
					{
473
						if (empty($mod_action['is_custom']))
474
							$context['actions'][$actual_filename]['operations'][] = array(
475
								'type' => $txt['execute_modification'],
476
								'action' => $smcFunc['htmlspecialchars'](strtr($mod_action['filename'], array($boarddir => '.'))),
477
								'description' => $mod_action['failed'] ? $txt['package_action_failure'] : $txt['package_action_success'],
478
								'position' => $mod_action['position'],
479
								'operation_key' => $operation_key,
480
								'filename' => $action['filename'],
481
								'is_boardmod' => $action['boardmod'],
482
								'failed' => $mod_action['failed'],
483
								'ignore_failure' => !empty($mod_action['ignore_failure']),
484
							);
485
486
						// Themes are under the saved type.
487
						if (isset($mod_action['is_custom']) && isset($context['theme_actions'][$mod_action['is_custom']]))
488
							$context['theme_actions'][$mod_action['is_custom']]['actions'][$actual_filename]['operations'][] = array(
489
								'type' => $txt['execute_modification'],
490
								'action' => $smcFunc['htmlspecialchars'](strtr($mod_action['filename'], array($boarddir => '.'))),
491
								'description' => $mod_action['failed'] ? $txt['package_action_failure'] : $txt['package_action_success'],
492
								'position' => $mod_action['position'],
493
								'operation_key' => $operation_key,
494
								'filename' => $action['filename'],
495
								'is_boardmod' => $action['boardmod'],
496
								'failed' => $mod_action['failed'],
497
								'ignore_failure' => !empty($mod_action['ignore_failure']),
498
							);
499
					}
500
				}
501
			}
502
		}
503
		elseif ($action['type'] == 'code')
504
		{
505
			$thisAction = array(
506
				'type' => $txt['execute_code'],
507
				'action' => $smcFunc['htmlspecialchars']($action['filename']),
508
			);
509
		}
510
		elseif ($action['type'] == 'database')
511
		{
512
			$thisAction = array(
513
				'type' => $txt['execute_database_changes'],
514
				'action' => $smcFunc['htmlspecialchars']($action['filename']),
515
			);
516
		}
517
		elseif (in_array($action['type'], array('create-dir', 'create-file')))
518
		{
519
			$thisAction = array(
520
				'type' => $txt['package_create'] . ' ' . ($action['type'] == 'create-dir' ? $txt['package_tree'] : $txt['package_file']),
521
				'action' => $smcFunc['htmlspecialchars'](strtr($action['destination'], array($boarddir => '.')))
522
			);
523
		}
524
		elseif ($action['type'] == 'hook')
525
		{
526
			$action['description'] = !isset($action['hook'], $action['function']) ? $txt['package_action_failure'] : $txt['package_action_success'];
527
528
			if (!isset($action['hook'], $action['function']))
529
				$context['has_failure'] = true;
530
531
			$thisAction = array(
532
				'type' => $action['reverse'] ? $txt['execute_hook_remove'] : $txt['execute_hook_add'],
533
				'action' => sprintf($txt['execute_hook_action' . ($action['reverse'] ? '_inverse' : '')], $smcFunc['htmlspecialchars']($action['hook'])),
534
			);
535
		}
536
		elseif ($action['type'] == 'credits')
537
		{
538
			$thisAction = array(
539
				'type' => $txt['execute_credits_add'],
540
				'action' => sprintf($txt['execute_credits_action'], $smcFunc['htmlspecialchars']($action['title'])),
541
			);
542
		}
543
		elseif ($action['type'] == 'requires')
544
		{
545
			$installed = false;
546
			$version = true;
547
548
			// package missing required values?
549
			if (!isset($action['id']))
550
				$context['has_failure'] = true;
551
			else
552
			{
553
				// See if this dependancy is installed
554
				$request = $smcFunc['db_query']('', '
555
					SELECT version
556
					FROM {db_prefix}log_packages
557
					WHERE package_id = {string:current_package}
558
						AND install_state != {int:not_installed}
559
					ORDER BY time_installed DESC
560
					LIMIT 1',
561
					array(
562
						'not_installed'	=> 0,
563
						'current_package' => $action['id'],
564
					)
565
				);
566
				$installed = ($smcFunc['db_num_rows']($request) !== 0);
567
				if ($installed)
568
					list ($version) = $smcFunc['db_fetch_row']($request);
569
				$smcFunc['db_free_result']($request);
570
571
				// do a version level check (if requested) in the most basic way
572
				$version = (isset($action['version']) ? $version == $action['version'] : true);
573
			}
574
575
			// Set success or failure information
576
			$action['description'] = ($installed && $version) ? $txt['package_action_success'] : $txt['package_action_failure'];
577
			$context['has_failure'] = !($installed && $version);
578
579
			$thisAction = array(
580
				'type' => $txt['package_requires'],
581
				'action' => $txt['package_check_for'] . ' ' . $action['id'] . (isset($action['version']) ? (' / ' . ($version ? $action['version'] : '<span class="error">' . $action['version'] . '</span>')) : ''),
582
			);
583
		}
584
		elseif (in_array($action['type'], array('require-dir', 'require-file')))
585
		{
586
			// Do this one...
587
			$thisAction = array(
588
				'type' => $txt['package_extract'] . ' ' . ($action['type'] == 'require-dir' ? $txt['package_tree'] : $txt['package_file']),
589
				'action' => $smcFunc['htmlspecialchars'](strtr($action['destination'], array($boarddir => '.')))
590
			);
591
592
			// Could this be theme related?
593
			if (!empty($action['unparsed_destination']) && preg_match('~^\$(languagedir|languages_dir|imagesdir|themedir|themes_dir)~i', $action['unparsed_destination'], $matches))
594
			{
595
				// Is the action already stated?
596
				$theme_action = !empty($action['theme_action']) && in_array($action['theme_action'], array('no', 'yes', 'auto')) ? $action['theme_action'] : 'auto';
597
				// If it's not auto do we think we have something we can act upon?
598
				if ($theme_action != 'auto' && !in_array($matches[1], array('languagedir', 'languages_dir', 'imagesdir', 'themedir')))
599
					$theme_action = '';
600
				// ... or if it's auto do we even want to do anything?
601
				elseif ($theme_action == 'auto' && $matches[1] != 'imagesdir')
602
					$theme_action = '';
603
604
				// So, we still want to do something?
605
				if ($theme_action != '')
606
					$themeFinds['candidates'][] = $action;
607
				// Otherwise is this is going into another theme record it.
608
				elseif ($matches[1] == 'themes_dir')
609
					$themeFinds['other_themes'][] = strtolower(strtr(parse_path($action['unparsed_destination']), array('\\' => '/')) . '/' . basename($action['filename']));
610
			}
611
		}
612
		elseif (in_array($action['type'], array('move-dir', 'move-file')))
613
			$thisAction = array(
614
				'type' => $txt['package_move'] . ' ' . ($action['type'] == 'move-dir' ? $txt['package_tree'] : $txt['package_file']),
615
				'action' => $smcFunc['htmlspecialchars'](strtr($action['source'], array($boarddir => '.'))) . ' => ' . $smcFunc['htmlspecialchars'](strtr($action['destination'], array($boarddir => '.')))
616
			);
617
		elseif (in_array($action['type'], array('remove-dir', 'remove-file')))
618
		{
619
			$thisAction = array(
620
				'type' => $txt['package_delete'] . ' ' . ($action['type'] == 'remove-dir' ? $txt['package_tree'] : $txt['package_file']),
621
				'action' => $smcFunc['htmlspecialchars'](strtr($action['filename'], array($boarddir => '.')))
622
			);
623
624
			// Could this be theme related?
625
			if (!empty($action['unparsed_filename']) && preg_match('~^\$(languagedir|languages_dir|imagesdir|themedir|themes_dir)~i', $action['unparsed_filename'], $matches))
626
			{
627
				// Is the action already stated?
628
				$theme_action = !empty($action['theme_action']) && in_array($action['theme_action'], array('no', 'yes', 'auto')) ? $action['theme_action'] : 'auto';
629
				$action['unparsed_destination'] = $action['unparsed_filename'];
630
631
				// If it's not auto do we think we have something we can act upon?
632
				if ($theme_action != 'auto' && !in_array($matches[1], array('languagedir', 'languages_dir', 'imagesdir', 'themedir')))
633
					$theme_action = '';
634
				// ... or if it's auto do we even want to do anything?
635
				elseif ($theme_action == 'auto' && $matches[1] != 'imagesdir')
636
					$theme_action = '';
637
638
				// So, we still want to do something?
639
				if ($theme_action != '')
640
					$themeFinds['candidates'][] = $action;
641
				// Otherwise is this is going into another theme record it.
642
				elseif ($matches[1] == 'themes_dir')
643
					$themeFinds['other_themes'][] = strtolower(strtr(parse_path($action['unparsed_filename']), array('\\' => '/')) . '/' . basename($action['filename']));
644
			}
645
		}
646
647
		if (empty($thisAction))
648
			continue;
649
650
		if (!in_array($action['type'], array('hook', 'credits')))
651
		{
652
			if ($context['uninstalling'])
653
				$file = in_array($action['type'], array('remove-dir', 'remove-file')) ? $action['filename'] : $packagesdir . '/temp/' . $context['base_path'] . $action['filename'];
654
			else
655
				$file = $packagesdir . '/temp/' . $context['base_path'] . $action['filename'];
656
		}
657
658
		// Don't fail if a file/directory we're trying to create doesn't exist...
659
		if (isset($action['filename']) && !file_exists($file) && !in_array($action['type'], array('create-dir', 'create-file')))
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $file does not seem to be defined for all execution paths leading up to this point.
Loading history...
660
		{
661
			$context['has_failure'] = true;
662
663
			$thisAction += array(
664
				'description' => $txt['package_action_missing'],
665
				'failed' => true,
666
			);
667
		}
668
669
		// @todo None given?
670
		if (empty($thisAction['description']))
671
			$thisAction['description'] = isset($action['description']) ? $action['description'] : '';
672
673
		$context['actions'][] = $thisAction;
674
	}
675
676
	// Have we got some things which we might want to do "multi-theme"?
677
	if (!empty($themeFinds['candidates']))
678
	{
679
		foreach ($themeFinds['candidates'] as $action_data)
680
		{
681
			// Get the part of the file we'll be dealing with.
682
			preg_match('~^\$(languagedir|languages_dir|imagesdir|themedir)(\\|/)*(.+)*~i', $action_data['unparsed_destination'], $matches);
683
684
			if ($matches[1] == 'imagesdir')
685
				$path = '/' . basename($settings['default_images_url']);
686
			elseif ($matches[1] == 'languagedir' || $matches[1] == 'languages_dir')
687
				$path = '/languages';
688
			else
689
				$path = '';
690
691
			if (!empty($matches[3]))
692
				$path .= $matches[3];
693
694
			if (!$context['uninstalling'])
695
				$path .= '/' . basename($action_data['filename']);
696
697
			// Loop through each custom theme to note it's candidacy!
698
			foreach ($theme_paths as $id => $theme_data)
699
			{
700
				if (isset($theme_data['theme_dir']) && $id != 1)
701
				{
702
					$real_path = $theme_data['theme_dir'] . $path;
703
704
					// Confirm that we don't already have this dealt with by another entry.
705
					if (!in_array(strtolower(strtr($real_path, array('\\' => '/'))), $themeFinds['other_themes']))
706
					{
707
						// Check if we will need to chmod this.
708
						if (!mktree(dirname($real_path), false))
0 ignored issues
show
Bug introduced by
false of type false is incompatible with the type integer expected by parameter $mode of mktree(). ( Ignorable by Annotation )

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

708
						if (!mktree(dirname($real_path), /** @scrutinizer ignore-type */ false))
Loading history...
709
						{
710
							$temp = dirname($real_path);
711
							while (!file_exists($temp) && strlen($temp) > 1)
712
								$temp = dirname($temp);
713
							$chmod_files[] = $temp;
714
						}
715
716
						if ($action_data['type'] == 'require-dir' && !is_writable($real_path) && (file_exists($real_path) || !is_writable(dirname($real_path))))
717
							$chmod_files[] = $real_path;
718
719
						if (!isset($context['theme_actions'][$id]))
720
							$context['theme_actions'][$id] = array(
721
								'name' => $theme_data['name'],
722
								'actions' => array(),
723
							);
724
725
						if ($context['uninstalling'])
726
							$context['theme_actions'][$id]['actions'][] = array(
727
								'type' => $txt['package_delete'] . ' ' . ($action_data['type'] == 'require-dir' ? $txt['package_tree'] : $txt['package_file']),
728
								'action' => strtr($real_path, array('\\' => '/', $boarddir => '.')),
729
								'description' => '',
730
								'value' => base64_encode($smcFunc['json_encode'](array('type' => $action_data['type'], 'orig' => $action_data['filename'], 'future' => $real_path, 'id' => $id))),
731
								'not_mod' => true,
732
							);
733
						else
734
							$context['theme_actions'][$id]['actions'][] = array(
735
								'type' => $txt['package_extract'] . ' ' . ($action_data['type'] == 'require-dir' ? $txt['package_tree'] : $txt['package_file']),
736
								'action' => strtr($real_path, array('\\' => '/', $boarddir => '.')),
737
								'description' => '',
738
								'value' => base64_encode($smcFunc['json_encode'](array('type' => $action_data['type'], 'orig' => $action_data['destination'], 'future' => $real_path, 'id' => $id))),
739
								'not_mod' => true,
740
							);
741
					}
742
				}
743
			}
744
		}
745
	}
746
747
	// Trash the cache... which will also check permissions for us!
748
	package_flush_cache(true);
749
750
	if (file_exists($packagesdir . '/temp'))
751
		deltree($packagesdir . '/temp');
752
753
	if (!empty($chmod_files))
754
	{
755
		$ftp_status = create_chmod_control($chmod_files);
756
		$context['ftp_needed'] = !empty($ftp_status['files']['notwritable']) && !empty($context['package_ftp']);
757
	}
758
759
	$context['post_url'] = $scripturl . '?action=admin;area=packages;sa=' . ($context['uninstalling'] ? 'uninstall' : 'install') . ($context['ftp_needed'] ? '' : '2') . ';package=' . $context['filename'] . ';pid=' . $context['install_id'];
760
	checkSubmitOnce('register');
761
}
762
763
/**
764
 * Apply another type of (avatar, language, etc.) package.
765
 */
766
function PackageInstall()
767
{
768
	global $txt, $context, $boardurl, $scripturl, $sourcedir, $packagesdir, $modSettings;
769
	global $user_info, $smcFunc;
770
771
	// Make sure we don't install this mod twice.
772
	checkSubmitOnce('check');
773
	checkSession();
774
775
	// If there's no file, what are we installing?
776
	if (!isset($_REQUEST['package']) || $_REQUEST['package'] == '')
777
		redirectexit('action=admin;area=packages');
778
	$context['filename'] = $_REQUEST['package'];
779
780
	// If this is an uninstall, we'll have an id.
781
	$context['install_id'] = isset($_REQUEST['pid']) ? (int) $_REQUEST['pid'] : 0;
782
783
	require_once($sourcedir . '/Subs-Package.php');
784
785
	// @todo Perhaps do it in steps, if necessary?
786
787
	$context['uninstalling'] = $_REQUEST['sa'] == 'uninstall2';
788
789
	// Set up the linktree for other.
790
	$context['linktree'][count($context['linktree']) - 1] = array(
791
		'url' => $scripturl . '?action=admin;area=packages;sa=browse',
792
		'name' => $context['uninstalling'] ? $txt['uninstall'] : $txt['extracting']
793
	);
794
	$context['page_title'] .= ' - ' . ($context['uninstalling'] ? $txt['uninstall'] : $txt['extracting']);
795
796
	$context['sub_template'] = 'extract_package';
797
798
	if (!file_exists($packagesdir . '/' . $context['filename']))
799
		fatal_lang_error('package_no_file', false);
800
801
	// Load up the package FTP information?
802
	create_chmod_control(array(), array('destination_url' => $scripturl . '?action=admin;area=packages;sa=' . $_REQUEST['sa'] . ';package=' . $_REQUEST['package']));
803
804
	// Make sure temp directory exists and is empty!
805
	if (file_exists($packagesdir . '/temp'))
806
		deltree($packagesdir . '/temp', false);
807
	else
808
		mktree($packagesdir . '/temp', 0777);
809
810
	// Let the unpacker do the work.
811
	if (is_file($packagesdir . '/' . $context['filename']))
812
	{
813
		$context['extracted_files'] = read_tgz_file($packagesdir . '/' . $context['filename'], $packagesdir . '/temp');
814
815
		if (!file_exists($packagesdir . '/temp/package-info.xml'))
816
			foreach ($context['extracted_files'] as $file)
817
				if (basename($file['filename']) == 'package-info.xml')
818
				{
819
					$context['base_path'] = dirname($file['filename']) . '/';
820
					break;
821
				}
822
823
		if (!isset($context['base_path']))
824
			$context['base_path'] = '';
825
	}
826
	elseif (is_dir($packagesdir . '/' . $context['filename']))
827
	{
828
		copytree($packagesdir . '/' . $context['filename'], $packagesdir . '/temp');
829
		$context['extracted_files'] = listtree($packagesdir . '/temp');
830
		$context['base_path'] = '';
831
	}
832
	else
833
		fatal_lang_error('no_access', false);
834
835
	// Are we installing this into any custom themes?
836
	$custom_themes = array(1);
837
	$known_themes = explode(',', $modSettings['knownThemes']);
838
	if (!empty($_POST['custom_theme']))
839
	{
840
		foreach ($_POST['custom_theme'] as $tid)
841
			if (in_array($tid, $known_themes))
842
				$custom_themes[] = (int) $tid;
843
	}
844
845
	// Now load up the paths of the themes that we need to know about.
846
	$request = $smcFunc['db_query']('', '
847
		SELECT id_theme, variable, value
848
		FROM {db_prefix}themes
849
		WHERE id_theme IN ({array_int:custom_themes})
850
			AND variable IN ({string:name}, {string:theme_dir})',
851
		array(
852
			'custom_themes' => $custom_themes,
853
			'name' => 'name',
854
			'theme_dir' => 'theme_dir',
855
		)
856
	);
857
	$theme_paths = array();
858
	$themes_installed = array(1);
859
	while ($row = $smcFunc['db_fetch_assoc']($request))
860
		$theme_paths[$row['id_theme']][$row['variable']] = $row['value'];
861
	$smcFunc['db_free_result']($request);
862
863
	// Are there any theme copying that we want to take place?
864
	$context['theme_copies'] = array(
865
		'require-file' => array(),
866
		'require-dir' => array(),
867
	);
868
	if (!empty($_POST['theme_changes']))
869
	{
870
		foreach ($_POST['theme_changes'] as $change)
871
		{
872
			if (empty($change))
873
				continue;
874
			$theme_data = $smcFunc['json_decode'](base64_decode($change), true);
875
			if (empty($theme_data['type']))
876
				continue;
877
878
			$themes_installed[] = $theme_data['id'];
879
			$context['theme_copies'][$theme_data['type']][$theme_data['orig']][] = $theme_data['future'];
880
		}
881
	}
882
883
	// Get the package info...
884
	$packageInfo = getPackageInfo($context['filename']);
885
	if (!is_array($packageInfo))
886
		fatal_lang_error($packageInfo);
887
888
	$packageInfo['filename'] = $context['filename'];
889
890
	// Set the type of extraction...
891
	$context['extract_type'] = isset($packageInfo['type']) ? $packageInfo['type'] : 'modification';
892
893
	// Create a backup file to roll back to! (but if they do this more than once, don't run it a zillion times.)
894
	if (!empty($modSettings['package_make_full_backups']) && (!isset($_SESSION['last_backup_for']) || $_SESSION['last_backup_for'] != $context['filename'] . ($context['uninstalling'] ? '$$' : '$')))
895
	{
896
		$_SESSION['last_backup_for'] = $context['filename'] . ($context['uninstalling'] ? '$$' : '$');
897
		$result = package_create_backup(($context['uninstalling'] ? 'backup_' : 'before_') . strtok($context['filename'], '.'));
898
		if (!$result)
899
			fatal_lang_error('could_not_package_backup', false);
900
	}
901
902
	// The mod isn't installed.... unless proven otherwise.
903
	$context['is_installed'] = false;
904
905
	// Is it actually installed?
906
	$request = $smcFunc['db_query']('', '
907
		SELECT version, themes_installed, db_changes
908
		FROM {db_prefix}log_packages
909
		WHERE package_id = {string:current_package}
910
			AND install_state != {int:not_installed}
911
		ORDER BY time_installed DESC
912
		LIMIT 1',
913
		array(
914
			'not_installed'	=> 0,
915
			'current_package' => $packageInfo['id'],
916
		)
917
	);
918
	while ($row = $smcFunc['db_fetch_assoc']($request))
919
	{
920
		$old_themes = explode(',', $row['themes_installed']);
921
		$old_version = $row['version'];
922
		$db_changes = empty($row['db_changes']) ? array() : $smcFunc['json_decode']($row['db_changes'], true);
923
	}
924
	$smcFunc['db_free_result']($request);
925
926
	// Wait, it's not installed yet!
927
	// @todo Replace with a better error message!
928
	if (!isset($old_version) && $context['uninstalling'])
929
	{
930
		deltree($packagesdir . '/temp');
931
		fatal_error('Hacker?', false);
932
	}
933
	// Uninstalling?
934
	elseif ($context['uninstalling'])
935
	{
936
		$install_log = parsePackageInfo($packageInfo['xml'], false, 'uninstall');
937
938
		// Gadzooks!  There's no uninstaller at all!?
939
		if (empty($install_log))
940
			fatal_lang_error('package_uninstall_cannot', false);
941
942
		// They can only uninstall from what it was originally installed into.
943
		foreach ($theme_paths as $id => $data)
944
			if ($id != 1 && !in_array($id, $old_themes))
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $old_themes does not seem to be defined for all execution paths leading up to this point.
Loading history...
945
				unset($theme_paths[$id]);
946
	}
947
	elseif (isset($old_version) && $old_version != $packageInfo['version'])
948
	{
949
		// Look for an upgrade...
950
		$install_log = parsePackageInfo($packageInfo['xml'], false, 'upgrade', $old_version);
951
952
		// There was no upgrade....
953
		if (empty($install_log))
954
			$context['is_installed'] = true;
955
		else
956
		{
957
			// Upgrade previous themes only!
958
			foreach ($theme_paths as $id => $data)
959
				if ($id != 1 && !in_array($id, $old_themes))
960
					unset($theme_paths[$id]);
961
		}
962
	}
963
	elseif (isset($old_version) && $old_version == $packageInfo['version'])
964
		$context['is_installed'] = true;
965
966
	if (!isset($old_version) || $context['is_installed'])
967
		$install_log = parsePackageInfo($packageInfo['xml'], false, 'install');
968
969
	$context['install_finished'] = false;
970
971
	// @todo Make a log of any errors that occurred and output them?
972
973
	if (!empty($install_log))
974
	{
975
		$failed_steps = array();
976
		$failed_count = 0;
977
978
		foreach ($install_log as $action)
979
		{
980
			$failed_count++;
981
982
			if ($action['type'] == 'modification' && !empty($action['filename']))
983
			{
984
				if ($action['boardmod'])
985
					$mod_actions = parseBoardMod(file_get_contents($packagesdir . '/temp/' . $context['base_path'] . $action['filename']), false, $action['reverse'], $theme_paths);
986
				else
987
					$mod_actions = parseModification(file_get_contents($packagesdir . '/temp/' . $context['base_path'] . $action['filename']), false, $action['reverse'], $theme_paths);
988
989
				// Any errors worth noting?
990
				foreach ($mod_actions as $key => $modAction)
991
				{
992
					if ($modAction['type'] == 'failure')
993
						$failed_steps[] = array(
994
							'file' => $modAction['filename'],
995
							'large_step' => $failed_count,
996
							'sub_step' => $key,
997
							'theme' => 1,
998
						);
999
1000
					// Gather the themes we installed into.
1001
					if (!empty($modAction['is_custom']))
1002
						$themes_installed[] = $modAction['is_custom'];
1003
				}
1004
			}
1005
			elseif ($action['type'] == 'code' && !empty($action['filename']))
1006
			{
1007
				// This is just here as reference for what is available.
1008
				global $txt, $boarddir, $sourcedir, $modSettings, $context, $settings, $forum_version, $smcFunc;
1009
1010
				// Now include the file and be done with it ;).
1011
				if (file_exists($packagesdir . '/temp/' . $context['base_path'] . $action['filename']))
1012
					require($packagesdir . '/temp/' . $context['base_path'] . $action['filename']);
1013
			}
1014
			elseif ($action['type'] == 'credits')
1015
			{
1016
				// Time to build the billboard
1017
				$credits_tag = array(
1018
					'url' => $action['url'],
1019
					'license' => $action['license'],
1020
					'licenseurl' => $action['licenseurl'],
1021
					'copyright' => $action['copyright'],
1022
					'title' => $action['title'],
1023
				);
1024
			}
1025
			elseif ($action['type'] == 'hook' && isset($action['hook'], $action['function']))
1026
			{
1027
				if ($action['reverse'])
1028
					remove_integration_function($action['hook'], $action['function'], true, $action['include_file'], $action['object']);
1029
				else
1030
					add_integration_function($action['hook'], $action['function'], true, $action['include_file'], $action['object']);
1031
			}
1032
			// Only do the database changes on uninstall if requested.
1033
			elseif ($action['type'] == 'database' && !empty($action['filename']) && (!$context['uninstalling'] || !empty($_POST['do_db_changes'])))
1034
			{
1035
				// These can also be there for database changes.
1036
				global $txt, $boarddir, $sourcedir, $modSettings, $context, $settings, $forum_version, $smcFunc;
1037
				global $db_package_log;
1038
1039
				// We'll likely want the package specific database functionality!
1040
				db_extend('packages');
1041
1042
				// Let the file work its magic ;)
1043
				if (file_exists($packagesdir . '/temp/' . $context['base_path'] . $action['filename']))
1044
					require($packagesdir . '/temp/' . $context['base_path'] . $action['filename']);
1045
			}
1046
			// Handle a redirect...
1047
			elseif ($action['type'] == 'redirect' && !empty($action['redirect_url']))
1048
			{
1049
				$context['redirect_url'] = $action['redirect_url'];
1050
				$context['redirect_text'] = !empty($action['filename']) && file_exists($packagesdir . '/temp/' . $context['base_path'] . $action['filename']) ? $smcFunc['htmlspecialchars'](file_get_contents($packagesdir . '/temp/' . $context['base_path'] . $action['filename'])) : ($context['uninstalling'] ? $txt['package_uninstall_done'] : $txt['package_installed_done']);
1051
				$context['redirect_timeout'] = $action['redirect_timeout'];
1052
				if (!empty($action['parse_bbc']))
1053
				{
1054
					require_once($sourcedir . '/Subs-Post.php');
1055
					$context['redirect_text'] = preg_replace('~\[[/]?html\]~i', '', $context['redirect_text']);
1056
					preparsecode($context['redirect_text']);
1057
					$context['redirect_text'] = parse_bbc($context['redirect_text']);
1058
				}
1059
1060
				// Parse out a couple of common urls.
1061
				$urls = array(
1062
					'$boardurl' => $boardurl,
1063
					'$scripturl' => $scripturl,
1064
					'$session_var' => $context['session_var'],
1065
					'$session_id' => $context['session_id'],
1066
				);
1067
1068
				$context['redirect_url'] = strtr($context['redirect_url'], $urls);
1069
			}
1070
		}
1071
1072
		package_flush_cache();
1073
1074
		// See if this is already installed, and change it's state as required.
1075
		$request = $smcFunc['db_query']('', '
1076
			SELECT package_id, install_state, db_changes
1077
			FROM {db_prefix}log_packages
1078
			WHERE install_state != {int:not_installed}
1079
				AND package_id = {string:current_package}
1080
				' . ($context['install_id'] ? ' AND id_install = {int:install_id} ' : '') . '
1081
			ORDER BY time_installed DESC
1082
			LIMIT 1',
1083
			array(
1084
				'not_installed' => 0,
1085
				'install_id' => $context['install_id'],
1086
				'current_package' => $packageInfo['id'],
1087
			)
1088
		);
1089
		$is_upgrade = false;
1090
		while ($row = $smcFunc['db_fetch_assoc']($request))
1091
		{
1092
			// Uninstalling?
1093
			if ($context['uninstalling'])
1094
			{
1095
				$smcFunc['db_query']('', '
1096
					UPDATE {db_prefix}log_packages
1097
					SET install_state = {int:not_installed}, member_removed = {string:member_name}, id_member_removed = {int:current_member},
1098
						time_removed = {int:current_time}
1099
					WHERE package_id = {string:package_id}
1100
						AND id_install = {int:install_id}',
1101
					array(
1102
						'current_member' => $user_info['id'],
1103
						'not_installed' => 0,
1104
						'current_time' => time(),
1105
						'package_id' => $row['package_id'],
1106
						'member_name' => $user_info['name'],
1107
						'install_id' => $context['install_id'],
1108
					)
1109
				);
1110
			}
1111
			// Otherwise must be an upgrade.
1112
			else
1113
			{
1114
				$is_upgrade = true;
1115
				$old_db_changes = empty($row['db_changes']) ? array() : $smcFunc['json_decode']($row['db_changes'], true);
1116
			}
1117
		}
1118
1119
		// Assuming we're not uninstalling, add the entry.
1120
		if (!$context['uninstalling'])
1121
		{
1122
			// Reload the settings table for mods that have altered them upon installation
1123
			reloadSettings();
1124
1125
			// Any db changes from older version?
1126
			if (!empty($old_db_changes))
1127
				$db_package_log = empty($db_package_log) ? $old_db_changes : array_merge($old_db_changes, $db_package_log);
1128
1129
			// If there are some database changes we might want to remove then filter them out.
1130
			if (!empty($db_package_log))
1131
			{
1132
				// We're really just checking for entries which are create table AND add columns (etc).
1133
				$tables = array();
1134
				/**
1135
				 * Table sorting function used in usort
1136
				 *
1137
				 * @param array $a
1138
				 * @param array $b
1139
				 * @return int
1140
				 */
1141
				function sort_table_first($a, $b)
1142
				{
1143
					if ($a[0] == $b[0])
1144
						return 0;
1145
					return $a[0] == 'remove_table' ? -1 : 1;
1146
				}
1147
				usort($db_package_log, 'sort_table_first');
1148
				foreach ($db_package_log as $k => $log)
1149
				{
1150
					if ($log[0] == 'remove_table')
1151
						$tables[] = $log[1];
1152
					elseif (in_array($log[1], $tables))
1153
						unset($db_package_log[$k]);
1154
				}
1155
				$db_changes = $smcFunc['json_encode']($db_package_log);
1156
			}
1157
			else
1158
				$db_changes = '';
1159
1160
			// What themes did we actually install?
1161
			$themes_installed = array_unique($themes_installed);
1162
			$themes_installed = implode(',', $themes_installed);
1163
1164
			// What failed steps?
1165
			$failed_step_insert = $smcFunc['json_encode']($failed_steps);
1166
1167
			// Un-sanitize things before we insert them...
1168
			$keys = array('filename', 'name', 'id', 'version');
1169
			foreach ($keys as $key)
1170
			{
1171
				// Yay for variable variables...
1172
				${"package_$key"} = un_htmlspecialchars($packageInfo[$key]);
1173
			}
1174
1175
			// Credits tag?
1176
			$credits_tag = (empty($credits_tag)) ? '' : $smcFunc['json_encode']($credits_tag);
1177
			$smcFunc['db_insert']('',
1178
				'{db_prefix}log_packages',
1179
				array(
1180
					'filename' => 'string', 'name' => 'string', 'package_id' => 'string', 'version' => 'string',
1181
					'id_member_installed' => 'int', 'member_installed' => 'string', 'time_installed' => 'int',
1182
					'install_state' => 'int', 'failed_steps' => 'string', 'themes_installed' => 'string',
1183
					'member_removed' => 'int', 'db_changes' => 'string', 'credits' => 'string',
1184
				),
1185
				array(
1186
					$package_filename, $package_name, $package_id, $package_version,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $package_name does not exist. Did you maybe mean $packageInfo?
Loading history...
Comprehensibility Best Practice introduced by
The variable $package_id does not exist. Did you maybe mean $packageInfo?
Loading history...
Comprehensibility Best Practice introduced by
The variable $package_version seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $package_filename seems to be never defined.
Loading history...
1187
					$user_info['id'], $user_info['name'], time(),
1188
					$is_upgrade ? 2 : 1, $failed_step_insert, $themes_installed,
1189
					0, $db_changes, $credits_tag,
1190
				),
1191
				array('id_install')
1192
			);
1193
		}
1194
		$smcFunc['db_free_result']($request);
1195
1196
		$context['install_finished'] = true;
1197
	}
1198
1199
	// If there's database changes - and they want them removed - let's do it last!
1200
	if (!empty($db_changes) && !empty($_POST['do_db_changes']))
1201
	{
1202
		// We're gonna be needing the package db functions!
1203
		db_extend('packages');
1204
1205
		foreach ($db_changes as $change)
1206
		{
1207
			if ($change[0] == 'remove_table' && isset($change[1]))
1208
				$smcFunc['db_drop_table']($change[1]);
1209
			elseif ($change[0] == 'remove_column' && isset($change[2]))
1210
				$smcFunc['db_remove_column']($change[1], $change[2]);
1211
			elseif ($change[0] == 'remove_index' && isset($change[2]))
1212
				$smcFunc['db_remove_index']($change[1], $change[2]);
1213
		}
1214
	}
1215
1216
	// Clean house... get rid of the evidence ;).
1217
	if (file_exists($packagesdir . '/temp'))
1218
		deltree($packagesdir . '/temp');
1219
1220
	// Log what we just did.
1221
	logAction($context['uninstalling'] ? 'uninstall_package' : (!empty($is_upgrade) ? 'upgrade_package' : 'install_package'), array('package' => $smcFunc['htmlspecialchars']($packageInfo['name']), 'version' => $smcFunc['htmlspecialchars']($packageInfo['version'])), 'admin');
1222
1223
	// Just in case, let's clear the whole cache and any minimized CSS and JS to avoid anything going up the swanny.
1224
	clean_cache();
1225
	deleteAllMinified();
1226
1227
	// Restore file permissions?
1228
	create_chmod_control(array(), array(), true);
1229
}
1230
1231
/**
1232
 * List the files in a package.
1233
 */
1234
function PackageList()
1235
{
1236
	global $txt, $scripturl, $context, $sourcedir, $packagesdir;
1237
1238
	require_once($sourcedir . '/Subs-Package.php');
1239
1240
	// No package?  Show him or her the door.
1241
	if (!isset($_REQUEST['package']) || $_REQUEST['package'] == '')
1242
		redirectexit('action=admin;area=packages');
1243
1244
	$context['linktree'][] = array(
1245
		'url' => $scripturl . '?action=admin;area=packages;sa=list;package=' . $_REQUEST['package'],
1246
		'name' => $txt['list_file']
1247
	);
1248
	$context['page_title'] .= ' - ' . $txt['list_file'];
1249
	$context['sub_template'] = 'list';
1250
1251
	// The filename...
1252
	$context['filename'] = $_REQUEST['package'];
1253
1254
	// Let the unpacker do the work.
1255
	if (is_file($packagesdir . '/' . $context['filename']))
1256
		$context['files'] = read_tgz_file($packagesdir . '/' . $context['filename'], null);
1257
	elseif (is_dir($packagesdir . '/' . $context['filename']))
1258
		$context['files'] = listtree($packagesdir . '/' . $context['filename']);
1259
}
1260
1261
/**
1262
 * Display one of the files in a package.
1263
 */
1264
function ExamineFile()
1265
{
1266
	global $txt, $scripturl, $context, $sourcedir, $packagesdir, $smcFunc;
1267
1268
	require_once($sourcedir . '/Subs-Package.php');
1269
1270
	// No package?  Show him or her the door.
1271
	if (!isset($_REQUEST['package']) || $_REQUEST['package'] == '')
1272
		redirectexit('action=admin;area=packages');
1273
1274
	// No file?  Show him or her the door.
1275
	if (!isset($_REQUEST['file']) || $_REQUEST['file'] == '')
1276
		redirectexit('action=admin;area=packages');
1277
1278
	$_REQUEST['package'] = preg_replace('~[\.]+~', '.', strtr($_REQUEST['package'], array('/' => '_', '\\' => '_')));
1279
	$_REQUEST['file'] = preg_replace('~[\.]+~', '.', $_REQUEST['file']);
1280
1281
	if (isset($_REQUEST['raw']))
1282
	{
1283
		if (is_file($packagesdir . '/' . $_REQUEST['package']))
1284
			echo read_tgz_file($packagesdir . '/' . $_REQUEST['package'], $_REQUEST['file'], true);
0 ignored issues
show
Bug introduced by
Are you sure read_tgz_file($packagesd..._REQUEST['file'], true) of type array|false can be used in echo? ( Ignorable by Annotation )

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

1284
			echo /** @scrutinizer ignore-type */ read_tgz_file($packagesdir . '/' . $_REQUEST['package'], $_REQUEST['file'], true);
Loading history...
1285
		elseif (is_dir($packagesdir . '/' . $_REQUEST['package']))
1286
			echo file_get_contents($packagesdir . '/' . $_REQUEST['package'] . '/' . $_REQUEST['file']);
1287
1288
		obExit(false);
1289
	}
1290
1291
	$context['linktree'][count($context['linktree']) - 1] = array(
1292
		'url' => $scripturl . '?action=admin;area=packages;sa=list;package=' . $_REQUEST['package'],
1293
		'name' => $txt['package_examine_file']
1294
	);
1295
	$context['page_title'] .= ' - ' . $txt['package_examine_file'];
1296
	$context['sub_template'] = 'examine';
1297
1298
	// The filename...
1299
	$context['package'] = $_REQUEST['package'];
1300
	$context['filename'] = $_REQUEST['file'];
1301
1302
	// Let the unpacker do the work.... but make sure we handle images properly.
1303
	if (in_array(strtolower(strrchr($_REQUEST['file'], '.')), array('.bmp', '.gif', '.jpeg', '.jpg', '.png')))
1304
		$context['filedata'] = '<img src="' . $scripturl . '?action=admin;area=packages;sa=examine;package=' . $_REQUEST['package'] . ';file=' . $_REQUEST['file'] . ';raw" alt="' . $_REQUEST['file'] . '">';
1305
	else
1306
	{
1307
		if (is_file($packagesdir . '/' . $_REQUEST['package']))
1308
			$context['filedata'] = $smcFunc['htmlspecialchars'](read_tgz_file($packagesdir . '/' . $_REQUEST['package'], $_REQUEST['file'], true));
1309
		elseif (is_dir($packagesdir . '/' . $_REQUEST['package']))
1310
			$context['filedata'] = $smcFunc['htmlspecialchars'](file_get_contents($packagesdir . '/' . $_REQUEST['package'] . '/' . $_REQUEST['file']));
1311
1312
		if (strtolower(strrchr($_REQUEST['file'], '.')) == '.php')
1313
			$context['filedata'] = highlight_php_code($context['filedata']);
1314
	}
1315
}
1316
1317
/**
1318
 * Delete a package.
1319
 */
1320
function PackageRemove()
1321
{
1322
	global $scripturl, $packagesdir;
1323
1324
	// Check it.
1325
	checkSession('get');
1326
1327
	// Ack, don't allow deletion of arbitrary files here, could become a security hole somehow!
1328
	if (!isset($_GET['package']) || $_GET['package'] == 'index.php' || $_GET['package'] == 'backups')
1329
		redirectexit('action=admin;area=packages;sa=browse');
1330
	$_GET['package'] = preg_replace('~[\.]+~', '.', strtr($_GET['package'], array('/' => '_', '\\' => '_')));
1331
1332
	// Can't delete what's not there.
1333
	if (file_exists($packagesdir . '/' . $_GET['package']) && (substr($_GET['package'], -4) == '.zip' || substr($_GET['package'], -4) == '.tgz' || substr($_GET['package'], -7) == '.tar.gz' || is_dir($packagesdir . '/' . $_GET['package'])) && $_GET['package'] != 'backups' && substr($_GET['package'], 0, 1) != '.')
1334
	{
1335
		create_chmod_control(array($packagesdir . '/' . $_GET['package']), array('destination_url' => $scripturl . '?action=admin;area=packages;sa=remove;package=' . $_GET['package'], 'crash_on_error' => true));
1336
1337
		if (is_dir($packagesdir . '/' . $_GET['package']))
1338
			deltree($packagesdir . '/' . $_GET['package']);
1339
		else
1340
		{
1341
			smf_chmod($packagesdir . '/' . $_GET['package'], 0777);
1342
			unlink($packagesdir . '/' . $_GET['package']);
1343
		}
1344
	}
1345
1346
	redirectexit('action=admin;area=packages;sa=browse');
1347
}
1348
1349
/**
1350
 * Browse a list of installed packages.
1351
 */
1352
function PackageBrowse()
1353
{
1354
	global $txt, $scripturl, $context, $forum_version, $sourcedir, $smcFunc;
1355
1356
	$context['page_title'] .= ' - ' . $txt['browse_packages'];
1357
1358
	$context['forum_version'] = $forum_version;
1359
	$context['modification_types'] = array('modification', 'avatar', 'language', 'unknown');
1360
1361
	call_integration_hook('integrate_modification_types');
1362
1363
	require_once($sourcedir . '/Subs-List.php');
1364
1365
	foreach ($context['modification_types'] as $type)
1366
	{
1367
		// Use the standard templates for showing this.
1368
		$listOptions = array(
1369
			'id' => 'packages_lists_' . $type,
1370
			'title' => $txt[$type . '_package'],
1371
			'no_items_label' => $txt['no_packages'],
1372
			'get_items' => array(
1373
				'function' => 'list_getPackages',
1374
				'params' => array('type' => $type),
1375
			),
1376
			'base_href' => $scripturl . '?action=admin;area=packages;sa=browse;type=' . $type,
1377
			'default_sort_col' => 'id' . $type,
1378
			'columns' => array(
1379
				'id' . $type => array(
1380
					'header' => array(
1381
						'value' => $txt['package_id'],
1382
						'style' => 'width: 40px;',
1383
					),
1384
					'data' => array(
1385
						'function' => function($package_md5) use ($type, &$context)
1386
						{
1387
							if (isset($context['available_' . $type . ''][$package_md5]))
1388
								return $context['available_' . $type . ''][$package_md5]['sort_id'];
1389
						},
1390
					),
1391
					'sort' => array(
1392
						'default' => 'sort_id',
1393
						'reverse' => 'sort_id'
1394
					),
1395
				),
1396
				'mod_name' . $type => array(
1397
					'header' => array(
1398
						'value' => $txt['mod_name'],
1399
						'style' => 'width: 25%;',
1400
					),
1401
					'data' => array(
1402
						'function' => function($package_md5) use ($type, &$context)
1403
						{
1404
							if (isset($context['available_' . $type . ''][$package_md5]))
1405
								return $context['available_' . $type . ''][$package_md5]['name'];
1406
						},
1407
					),
1408
					'sort' => array(
1409
						'default' => 'name',
1410
						'reverse' => 'name',
1411
					),
1412
				),
1413
				'version' . $type => array(
1414
					'header' => array(
1415
						'value' => $txt['mod_version'],
1416
					),
1417
					'data' => array(
1418
						'function' => function($package_md5) use ($type, &$context)
1419
						{
1420
							if (isset($context['available_' . $type . ''][$package_md5]))
1421
								return $context['available_' . $type . ''][$package_md5]['version'];
1422
						},
1423
					),
1424
					'sort' => array(
1425
						'default' => 'version',
1426
						'reverse' => 'version',
1427
					),
1428
				),
1429
				'time_installed' . $type => array(
1430
					'header' => array(
1431
						'value' => $txt['mod_installed_time'],
1432
					),
1433
					'data' => array(
1434
						'function' => function($package_md5) use ($type, $txt, &$context)
1435
						{
1436
							if (isset($context['available_' . $type . ''][$package_md5]))
1437
								return !empty($context['available_' . $type . ''][$package_md5]['time_installed']) ? timeformat($context['available_' . $type . ''][$package_md5]['time_installed']) : $txt['not_applicable'];
1438
						},
1439
						'class' => 'smalltext',
1440
					),
1441
					'sort' => array(
1442
						'default' => 'time_installed',
1443
						'reverse' => 'time_installed',
1444
					),
1445
				),
1446
				'operations' . $type => array(
1447
					'header' => array(
1448
						'value' => '',
1449
					),
1450
					'data' => array(
1451
						'function' => function($package_md5) use ($type, &$context, $scripturl, $txt)
1452
						{
1453
							if (!isset($context['available_' . $type . ''][$package_md5]))
1454
								return '';
1455
1456
							// Rewrite shortcut
1457
							$package = $context['available_' . $type . ''][$package_md5];
1458
							$return = '';
1459
1460
							if ($package['can_uninstall'])
1461
								$return = '
1462
									<a href="' . $scripturl . '?action=admin;area=packages;sa=uninstall;package=' . $package['filename'] . ';pid=' . $package['installed_id'] . '" class="button">' . $txt['uninstall'] . '</a>';
1463
							elseif ($package['can_emulate_uninstall'])
1464
								$return = '
1465
									<a href="' . $scripturl . '?action=admin;area=packages;sa=uninstall;ve=' . $package['can_emulate_uninstall'] . ';package=' . $package['filename'] . ';pid=' . $package['installed_id'] . '" class="button">' . $txt['package_emulate_uninstall'] . ' ' . $package['can_emulate_uninstall'] . '</a>';
1466
							elseif ($package['can_upgrade'])
1467
								$return = '
1468
									<a href="' . $scripturl . '?action=admin;area=packages;sa=install;package=' . $package['filename'] . '" class="button">' . $txt['package_upgrade'] . '</a>';
1469
							elseif ($package['can_install'])
1470
								$return = '
1471
									<a href="' . $scripturl . '?action=admin;area=packages;sa=install;package=' . $package['filename'] . '" class="button">' . $txt['install_mod'] . '</a>';
1472
							elseif ($package['can_emulate_install'])
1473
								$return = '
1474
									<a href="' . $scripturl . '?action=admin;area=packages;sa=install;ve=' . $package['can_emulate_install'] . ';package=' . $package['filename'] . '" class="button">' . $txt['package_emulate_install'] . ' ' . $package['can_emulate_install'] . '</a>';
1475
1476
							return $return . '
1477
									<a href="' . $scripturl . '?action=admin;area=packages;sa=list;package=' . $package['filename'] . '" class="button">' . $txt['list_files'] . '</a>
1478
									<a href="' . $scripturl . '?action=admin;area=packages;sa=remove;package=' . $package['filename'] . ';' . $context['session_var'] . '=' . $context['session_id'] . '"' . ($package['is_installed'] && $package['is_current'] ? ' data-confirm="' . $txt['package_delete_bad'] . '"' : '') . ' class="button' . ($package['is_installed'] && $package['is_current'] ? ' you_sure' : '') . '">' . $txt['package_delete'] . '</a>';
1479
						},
1480
						'class' => 'righttext',
1481
					),
1482
				),
1483
			),
1484
		);
1485
1486
		createList($listOptions);
1487
	}
1488
1489
	$context['sub_template'] = 'browse';
1490
	$context['default_list'] = 'packages_lists';
1491
1492
	// Empty lists for now.
1493
	$context['available_mods'] = array();
1494
	$context['available_avatars'] = array();
1495
	$context['available_languages'] = array();
1496
	$context['available_other'] = array();
1497
	$context['available_all'] = array();
1498
1499
	$get_versions = $smcFunc['db_query']('', '
1500
		SELECT data FROM {db_prefix}admin_info_files WHERE filename={string:versionsfile} AND path={string:smf}',
1501
		array(
1502
			'versionsfile' => 'latest-versions.txt',
1503
			'smf' => '/smf/',
1504
		)
1505
	);
1506
1507
	$data = $smcFunc['db_fetch_assoc']($get_versions);
1508
	$smcFunc['db_free_result']($get_versions);
1509
1510
	// Decode the data.
1511
	$items = $smcFunc['json_decode']($data['data'], true);
1512
1513
	$context['emulation_versions'] = preg_replace('~^SMF ~', '', $items);
1514
1515
	// Current SMF version, which is selected by default
1516
	$context['default_version'] = preg_replace('~^SMF ~', '', $forum_version);
1517
1518
	// Version we're currently emulating, if any
1519
	$context['selected_version'] = preg_replace('~^SMF ~', '', $context['forum_version']);
1520
}
1521
1522
/**
1523
 * Get a listing of all the packages
1524
 * Determines if the package is a mod, avatar, language package
1525
 * Determines if the package has been installed or not
1526
 *
1527
 * @param int $start The item to start with (not used here)
1528
 * @param int $items_per_page The number of items to show per page (not used here)
1529
 * @param string $sort A string indicating how to sort the results
1530
 * @param string? $params A key for the $packages array
0 ignored issues
show
Documentation Bug introduced by
The doc comment string? at position 0 could not be parsed: Unknown type name 'string?' at position 0 in string?.
Loading history...
1531
 * @return array An array of information about the packages
1532
 */
1533
function list_getPackages($start, $items_per_page, $sort, $params)
1534
{
1535
	global $scripturl, $packagesdir, $context, $forum_version;
1536
	static $packages, $installed_mods;
1537
1538
	// Start things up
1539
	if (!isset($packages[$params]))
1540
		$packages[$params] = array();
1541
1542
	// We need the packages directory to be writable for this.
1543
	if (!@is_writable($packagesdir))
1544
		create_chmod_control(array($packagesdir), array('destination_url' => $scripturl . '?action=admin;area=packages', 'crash_on_error' => true));
1545
1546
	$the_version = strtr($forum_version, array('SMF ' => ''));
1547
1548
	// Here we have a little code to help those who class themselves as something of gods, version emulation ;)
1549
	if (isset($_GET['version_emulate']) && strtr($_GET['version_emulate'], array('SMF ' => '')) == $the_version)
1550
	{
1551
		unset($_SESSION['version_emulate']);
1552
	}
1553
	elseif (isset($_GET['version_emulate']))
1554
	{
1555
		if (($_GET['version_emulate'] === 0 || $_GET['version_emulate'] === $forum_version) && isset($_SESSION['version_emulate']))
1556
			unset($_SESSION['version_emulate']);
1557
		elseif ($_GET['version_emulate'] !== 0)
1558
			$_SESSION['version_emulate'] = strtr($_GET['version_emulate'], array('-' => ' ', '+' => ' ', 'SMF ' => ''));
1559
	}
1560
	if (!empty($_SESSION['version_emulate']))
1561
	{
1562
		$context['forum_version'] = 'SMF ' . $_SESSION['version_emulate'];
1563
		$the_version = $_SESSION['version_emulate'];
1564
	}
1565
	if (isset($_SESSION['single_version_emulate']))
1566
		unset($_SESSION['single_version_emulate']);
1567
1568
	if (empty($installed_mods))
1569
	{
1570
		$instmods = loadInstalledPackages();
1571
		$installed_mods = array();
1572
		// Look through the list of installed mods...
1573
		foreach ($instmods as $installed_mod)
1574
			$installed_mods[$installed_mod['package_id']] = array(
1575
				'id' => $installed_mod['id'],
1576
				'version' => $installed_mod['version'],
1577
				'time_installed' => $installed_mod['time_installed'],
1578
			);
1579
1580
		// Get a list of all the ids installed, so the latest packages won't include already installed ones.
1581
		$context['installed_mods'] = array_keys($installed_mods);
1582
	}
1583
1584
	if (empty($packages))
1585
		foreach ($context['modification_types'] as $type)
1586
			$packages[$type] = array();
1587
1588
	if ($dir = @opendir($packagesdir))
1589
	{
1590
		$dirs = array();
1591
		$sort_id = array(
1592
			'mod' => 1,
1593
			'modification' => 1,
1594
			'avatar' => 1,
1595
			'language' => 1,
1596
			'unknown' => 1,
1597
		);
1598
		call_integration_hook('integrate_packages_sort_id', array(&$sort_id, &$packages));
1599
1600
		while ($package = readdir($dir))
1601
		{
1602
			if ($package == '.' || $package == '..' || $package == 'temp' || (!(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'))
1603
				continue;
1604
1605
			$skip = false;
1606
			foreach ($context['modification_types'] as $type)
1607
				if (isset($context['available_' . $type][md5($package)]))
1608
					$skip = true;
1609
1610
			if ($skip)
1611
				continue;
1612
1613
			// Skip directories or files that are named the same.
1614
			if (is_dir($packagesdir . '/' . $package))
1615
			{
1616
				if (in_array($package, $dirs))
1617
					continue;
1618
				$dirs[] = $package;
1619
			}
1620
			elseif (substr(strtolower($package), -7) == '.tar.gz')
1621
			{
1622
				if (in_array(substr($package, 0, -7), $dirs))
1623
					continue;
1624
				$dirs[] = substr($package, 0, -7);
1625
			}
1626
			elseif (substr(strtolower($package), -4) == '.zip' || substr(strtolower($package), -4) == '.tgz')
1627
			{
1628
				if (in_array(substr($package, 0, -4), $dirs))
1629
					continue;
1630
				$dirs[] = substr($package, 0, -4);
1631
			}
1632
1633
			$packageInfo = getPackageInfo($package);
1634
			if (!is_array($packageInfo))
1635
				continue;
1636
1637
			if (!empty($packageInfo))
1638
			{
1639
				$packageInfo['installed_id'] = isset($installed_mods[$packageInfo['id']]) ? $installed_mods[$packageInfo['id']]['id'] : 0;
1640
				$packageInfo['time_installed'] = isset($installed_mods[$packageInfo['id']]) ? $installed_mods[$packageInfo['id']]['time_installed'] : 0;
1641
1642
				if (!isset($sort_id[$packageInfo['type']]))
1643
					$packageInfo['sort_id'] = $sort_id['unknown'];
1644
				else
1645
					$packageInfo['sort_id'] = $sort_id[$packageInfo['type']];
1646
1647
				$packageInfo['is_installed'] = isset($installed_mods[$packageInfo['id']]);
1648
				$packageInfo['is_current'] = $packageInfo['is_installed'] && ($installed_mods[$packageInfo['id']]['version'] == $packageInfo['version']);
1649
				$packageInfo['is_newer'] = $packageInfo['is_installed'] && ($installed_mods[$packageInfo['id']]['version'] > $packageInfo['version']);
1650
1651
				$packageInfo['can_install'] = false;
1652
				$packageInfo['can_uninstall'] = false;
1653
				$packageInfo['can_upgrade'] = false;
1654
				$packageInfo['can_emulate_install'] = false;
1655
				$packageInfo['can_emulate_uninstall'] = false;
1656
1657
				// This package is currently NOT installed.  Check if it can be.
1658
				if (!$packageInfo['is_installed'] && $packageInfo['xml']->exists('install'))
1659
				{
1660
					// Check if there's an install for *THIS* version of SMF.
1661
					$installs = $packageInfo['xml']->set('install');
1662
					foreach ($installs as $install)
1663
					{
1664
						if (!$install->exists('@for') || matchPackageVersion($the_version, $install->fetch('@for')))
1665
						{
1666
							// Okay, this one is good to go.
1667
							$packageInfo['can_install'] = true;
1668
							break;
1669
						}
1670
					}
1671
1672
					// no install found for this version, lets see if one exists for another
1673
					if ($packageInfo['can_install'] === false && $install->exists('@for') && empty($_SESSION['version_emulate']))
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $install does not seem to be defined for all execution paths leading up to this point.
Loading history...
1674
					{
1675
						$reset = true;
1676
1677
						// Get the highest install version that is available from the package
1678
						foreach ($installs as $install)
1679
						{
1680
							$packageInfo['can_emulate_install'] = matchHighestPackageVersion($install->fetch('@for'), $reset, $the_version);
1681
							$reset = false;
1682
						}
1683
					}
1684
				}
1685
				// An already installed, but old, package.  Can we upgrade it?
1686
				elseif ($packageInfo['is_installed'] && !$packageInfo['is_current'] && $packageInfo['xml']->exists('upgrade'))
1687
				{
1688
					$upgrades = $packageInfo['xml']->set('upgrade');
1689
1690
					// First go through, and check against the current version of SMF.
1691
					foreach ($upgrades as $upgrade)
1692
					{
1693
						// Even if it is for this SMF, is it for the installed version of the mod?
1694
						if (!$upgrade->exists('@for') || matchPackageVersion($the_version, $upgrade->fetch('@for')))
1695
							if (!$upgrade->exists('@from') || matchPackageVersion($installed_mods[$packageInfo['id']]['version'], $upgrade->fetch('@from')))
1696
							{
1697
								$packageInfo['can_upgrade'] = true;
1698
								break;
1699
							}
1700
					}
1701
				}
1702
				// Note that it has to be the current version to be uninstallable.  Shucks.
1703
				elseif ($packageInfo['is_installed'] && $packageInfo['is_current'] && $packageInfo['xml']->exists('uninstall'))
1704
				{
1705
					$uninstalls = $packageInfo['xml']->set('uninstall');
1706
1707
					// Can we find any uninstallation methods that work for this SMF version?
1708
					foreach ($uninstalls as $uninstall)
1709
					{
1710
						if (!$uninstall->exists('@for') || matchPackageVersion($the_version, $uninstall->fetch('@for')))
1711
						{
1712
							$packageInfo['can_uninstall'] = true;
1713
							break;
1714
						}
1715
					}
1716
1717
					// no uninstall found for this version, lets see if one exists for another
1718
					if ($packageInfo['can_uninstall'] === false && $uninstall->exists('@for') && empty($_SESSION['version_emulate']))
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $uninstall does not seem to be defined for all execution paths leading up to this point.
Loading history...
1719
					{
1720
						$reset = true;
1721
1722
						// Get the highest install version that is available from the package
1723
						foreach ($uninstalls as $uninstall)
1724
						{
1725
							$packageInfo['can_emulate_uninstall'] = matchHighestPackageVersion($uninstall->fetch('@for'), $reset, $the_version);
1726
							$reset = false;
1727
						}
1728
					}
1729
				}
1730
1731
				// Modification.
1732
				if ($packageInfo['type'] == 'modification' || $packageInfo['type'] == 'mod')
1733
				{
1734
					$sort_id['modification']++;
1735
					$sort_id['mod']++;
1736
					$packages['modification'][strtolower($packageInfo[$sort]) . '_' . $sort_id['mod']] = md5($package);
1737
					$context['available_modification'][md5($package)] = $packageInfo;
1738
				}
1739
				// Avatar package.
1740
				elseif ($packageInfo['type'] == 'avatar')
1741
				{
1742
					$sort_id[$packageInfo['type']]++;
1743
					$packages['avatar'][strtolower($packageInfo[$sort])] = md5($package);
1744
					$context['available_avatar'][md5($package)] = $packageInfo;
1745
				}
1746
				// Language package.
1747
				elseif ($packageInfo['type'] == 'language')
1748
				{
1749
					$sort_id[$packageInfo['type']]++;
1750
					$packages['language'][strtolower($packageInfo[$sort])] = md5($package);
1751
					$context['available_language'][md5($package)] = $packageInfo;
1752
				}
1753
				// This might be a 3rd party section.
1754
				elseif (isset($sort_id[$packageInfo['type']], $packages[$packageInfo['type']], $context['available_' . $packageInfo['type']]))
1755
				{
1756
					$sort_id[$packageInfo['type']]++;
1757
					$packages[$packageInfo['type']][strtolower($packageInfo[$sort])] = md5($package);
1758
					$context['available_' . $packageInfo['type']][md5($package)] = $packageInfo;
1759
				}
1760
				// Other stuff.
1761
				else
1762
				{
1763
					$sort_id['unknown']++;
1764
					$packages['unknown'][strtolower($packageInfo[$sort])] = md5($package);
1765
					$context['available_unknown'][md5($package)] = $packageInfo;
1766
				}
1767
			}
1768
		}
1769
		closedir($dir);
1770
	}
1771
1772
	if (isset($_GET['type']) && $_GET['type'] == $params)
1773
	{
1774
		if (isset($_GET['desc']))
1775
			krsort($packages[$params]);
1776
		else
1777
			ksort($packages[$params]);
1778
	}
1779
1780
	return $packages[$params];
1781
}
1782
1783
/**
1784
 * Used when a temp FTP access is needed to package functions
1785
 */
1786
function PackageOptions()
1787
{
1788
	global $txt, $context, $modSettings, $smcFunc;
1789
1790
	if (isset($_POST['save']))
1791
	{
1792
		checkSession();
1793
1794
		updateSettings(array(
1795
			'package_server' => trim($smcFunc['htmlspecialchars']($_POST['pack_server'])),
1796
			'package_port' => trim($smcFunc['htmlspecialchars']($_POST['pack_port'])),
1797
			'package_username' => trim($smcFunc['htmlspecialchars']($_POST['pack_user'])),
1798
			'package_make_backups' => !empty($_POST['package_make_backups']),
1799
			'package_make_full_backups' => !empty($_POST['package_make_full_backups'])
1800
		));
1801
		$_SESSION['adm-save'] = true;
1802
1803
		redirectexit('action=admin;area=packages;sa=options');
1804
	}
1805
1806
	if (preg_match('~^/home\d*/([^/]+?)/public_html~', $_SERVER['DOCUMENT_ROOT'], $match))
1807
		$default_username = $match[1];
1808
	else
1809
		$default_username = '';
1810
1811
	$context['page_title'] = $txt['package_settings'];
1812
	$context['sub_template'] = 'install_options';
1813
1814
	$context['package_ftp_server'] = isset($modSettings['package_server']) ? $modSettings['package_server'] : 'localhost';
1815
	$context['package_ftp_port'] = isset($modSettings['package_port']) ? $modSettings['package_port'] : '21';
1816
	$context['package_ftp_username'] = isset($modSettings['package_username']) ? $modSettings['package_username'] : $default_username;
1817
	$context['package_make_backups'] = !empty($modSettings['package_make_backups']);
1818
	$context['package_make_full_backups'] = !empty($modSettings['package_make_full_backups']);
1819
1820
	if (!empty($_SESSION['adm-save']))
1821
	{
1822
		$context['saved_successful'] = true;
1823
		unset ($_SESSION['adm-save']);
1824
	}
1825
}
1826
1827
/**
1828
 * List operations
1829
 */
1830
function ViewOperations()
1831
{
1832
	global $context, $txt, $sourcedir, $packagesdir, $smcFunc, $modSettings;
1833
1834
	// Can't be in here buddy.
1835
	isAllowedTo('admin_forum');
1836
1837
	// We need to know the operation key for the search and replace, mod file looking at, is it a board mod?
1838
	if (!isset($_REQUEST['operation_key'], $_REQUEST['filename']) && !is_numeric($_REQUEST['operation_key']))
1839
		fatal_lang_error('operation_invalid', 'general');
1840
1841
	// Load the required file.
1842
	require_once($sourcedir . '/Subs-Package.php');
1843
1844
	// Uninstalling the mod?
1845
	$reverse = isset($_REQUEST['reverse']) ? true : false;
1846
1847
	// Get the base name.
1848
	$context['filename'] = preg_replace('~[\.]+~', '.', $_REQUEST['package']);
1849
1850
	// We need to extract this again.
1851
	if (is_file($packagesdir . '/' . $context['filename']))
1852
	{
1853
		$context['extracted_files'] = read_tgz_file($packagesdir . '/' . $context['filename'], $packagesdir . '/temp');
1854
1855
		if ($context['extracted_files'] && !file_exists($packagesdir . '/temp/package-info.xml'))
1856
			foreach ($context['extracted_files'] as $file)
1857
				if (basename($file['filename']) == 'package-info.xml')
1858
				{
1859
					$context['base_path'] = dirname($file['filename']) . '/';
1860
					break;
1861
				}
1862
1863
		if (!isset($context['base_path']))
1864
			$context['base_path'] = '';
1865
	}
1866
	elseif (is_dir($packagesdir . '/' . $context['filename']))
1867
	{
1868
		copytree($packagesdir . '/' . $context['filename'], $packagesdir . '/temp');
1869
		$context['extracted_files'] = listtree($packagesdir . '/temp');
1870
		$context['base_path'] = '';
1871
	}
1872
1873
	// Load up any custom themes we may want to install into...
1874
	$request = $smcFunc['db_query']('', '
1875
		SELECT id_theme, variable, value
1876
		FROM {db_prefix}themes
1877
		WHERE (id_theme = {int:default_theme} OR id_theme IN ({array_int:known_theme_list}))
1878
			AND variable IN ({string:name}, {string:theme_dir})',
1879
		array(
1880
			'known_theme_list' => explode(',', $modSettings['knownThemes']),
1881
			'default_theme' => 1,
1882
			'name' => 'name',
1883
			'theme_dir' => 'theme_dir',
1884
		)
1885
	);
1886
	$theme_paths = array();
1887
	while ($row = $smcFunc['db_fetch_assoc']($request))
1888
		$theme_paths[$row['id_theme']][$row['variable']] = $row['value'];
1889
	$smcFunc['db_free_result']($request);
1890
1891
	// If we're viewing uninstall operations, only consider themes that
1892
	// the package is actually installed into.
1893
	if (isset($_REQUEST['reverse']) && !empty($_REQUEST['install_id']))
1894
	{
1895
		$install_id = (int) $_REQUEST['install_id'];
1896
		if ($install_id > 0)
1897
		{
1898
			$old_themes = array();
0 ignored issues
show
Unused Code introduced by
The assignment to $old_themes is dead and can be removed.
Loading history...
1899
			$request = $smcFunc['db_query']('', '
1900
				SELECT themes_installed
1901
				FROM {db_prefix}log_packages
1902
				WHERE id_install = {int:install_id}',
1903
				array(
1904
					'install_id' => $install_id,
1905
				)
1906
			);
1907
1908
			if ($smcFunc['db_num_rows']($request) == 1)
1909
			{
1910
				list ($old_themes) = $smcFunc['db_fetch_row']($request);
1911
				$old_themes = explode(',', $old_themes);
1912
1913
				foreach ($theme_paths as $id => $data)
1914
					if ($id != 1 && !in_array($id, $old_themes))
1915
						unset($theme_paths[$id]);
1916
			}
1917
			$smcFunc['db_free_result']($request);
1918
		}
1919
	}
1920
1921
	// Boardmod?
1922
	if (isset($_REQUEST['boardmod']))
1923
		$mod_actions = parseBoardMod(@file_get_contents($packagesdir . '/temp/' . $context['base_path'] . $_REQUEST['filename']), true, $reverse, $theme_paths);
1924
	else
1925
		$mod_actions = parseModification(@file_get_contents($packagesdir . '/temp/' . $context['base_path'] . $_REQUEST['filename']), true, $reverse, $theme_paths);
1926
1927
	// Ok lets get the content of the file.
1928
	$context['operations'] = array(
1929
		'search' => strtr($smcFunc['htmlspecialchars']($mod_actions[$_REQUEST['operation_key']]['search_original']), array('[' => '&#91;', ']' => '&#93;')),
1930
		'replace' => strtr($smcFunc['htmlspecialchars']($mod_actions[$_REQUEST['operation_key']]['replace_original']), array('[' => '&#91;', ']' => '&#93;')),
1931
		'position' => $mod_actions[$_REQUEST['operation_key']]['position'],
1932
	);
1933
1934
	// Let's do some formatting...
1935
	$operation_text = $context['operations']['position'] == 'replace' ? 'operation_replace' : ($context['operations']['position'] == 'before' ? 'operation_after' : 'operation_before');
1936
	$context['operations']['search'] = parse_bbc('[code=' . $txt['operation_find'] . ']' . ($context['operations']['position'] == 'end' ? '?&gt;' : $context['operations']['search']) . '[/code]');
1937
	$context['operations']['replace'] = parse_bbc('[code=' . $txt[$operation_text] . ']' . $context['operations']['replace'] . '[/code]');
1938
1939
	// No layers
1940
	$context['template_layers'] = array();
1941
	$context['sub_template'] = 'view_operations';
1942
}
1943
1944
/**
1945
 * Allow the admin to reset permissions on files.
1946
 */
1947
function PackagePermissions()
1948
{
1949
	global $context, $txt, $modSettings, $boarddir, $sourcedir, $cachedir, $smcFunc, $package_ftp;
1950
1951
	// Let's try and be good, yes?
1952
	checkSession('get');
1953
1954
	// If we're restoring permissions this is just a pass through really.
1955
	if (isset($_GET['restore']))
1956
	{
1957
		create_chmod_control(array(), array(), true);
1958
		fatal_lang_error('no_access', false);
1959
	}
1960
1961
	// This is a memory eat.
1962
	setMemoryLimit('128M');
1963
	@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

1963
	/** @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...
1964
1965
	// Load up some FTP stuff.
1966
	create_chmod_control();
1967
1968
	if (empty($package_ftp) && !isset($_POST['skip_ftp']))
1969
	{
1970
		require_once($sourcedir . '/Class-Package.php');
1971
		$ftp = new ftp_connection(null);
1972
		list ($username, $detect_path, $found_path) = $ftp->detect_path($boarddir);
1973
1974
		$context['package_ftp'] = array(
1975
			'server' => isset($modSettings['package_server']) ? $modSettings['package_server'] : 'localhost',
1976
			'port' => isset($modSettings['package_port']) ? $modSettings['package_port'] : '21',
1977
			'username' => empty($username) ? (isset($modSettings['package_username']) ? $modSettings['package_username'] : '') : $username,
1978
			'path' => $detect_path,
1979
			'form_elements_only' => true,
1980
		);
1981
	}
1982
	else
1983
		$context['ftp_connected'] = true;
1984
1985
	// Define the template.
1986
	$context['page_title'] = $txt['package_file_perms'];
1987
	$context['sub_template'] = 'file_permissions';
1988
1989
	// Define what files we're interested in, as a tree.
1990
	$context['file_tree'] = array(
1991
		strtr($boarddir, array('\\' => '/')) => array(
1992
			'type' => 'dir',
1993
			'contents' => array(
1994
				'agreement.txt' => array(
1995
					'type' => 'file',
1996
					'writable_on' => 'standard',
1997
				),
1998
				'Settings.php' => array(
1999
					'type' => 'file',
2000
					'writable_on' => 'restrictive',
2001
				),
2002
				'Settings_bak.php' => array(
2003
					'type' => 'file',
2004
					'writable_on' => 'restrictive',
2005
				),
2006
				'attachments' => array(
2007
					'type' => 'dir',
2008
					'writable_on' => 'restrictive',
2009
				),
2010
				'avatars' => array(
2011
					'type' => 'dir',
2012
					'writable_on' => 'standard',
2013
				),
2014
				'cache' => array(
2015
					'type' => 'dir',
2016
					'writable_on' => 'restrictive',
2017
				),
2018
				'custom_avatar_dir' => array(
2019
					'type' => 'dir',
2020
					'writable_on' => 'restrictive',
2021
				),
2022
				'Smileys' => array(
2023
					'type' => 'dir_recursive',
2024
					'writable_on' => 'standard',
2025
				),
2026
				'Sources' => array(
2027
					'type' => 'dir_recursive',
2028
					'list_contents' => true,
2029
					'writable_on' => 'standard',
2030
					'contents' => array(
2031
						'tasks' => array(
2032
							'type' => 'dir',
2033
							'list_contents' => true,
2034
						),
2035
					),
2036
				),
2037
				'Themes' => array(
2038
					'type' => 'dir_recursive',
2039
					'writable_on' => 'standard',
2040
					'contents' => array(
2041
						'default' => array(
2042
							'type' => 'dir_recursive',
2043
							'list_contents' => true,
2044
							'contents' => array(
2045
								'languages' => array(
2046
									'type' => 'dir',
2047
									'list_contents' => true,
2048
								),
2049
							),
2050
						),
2051
					),
2052
				),
2053
				'Packages' => array(
2054
					'type' => 'dir',
2055
					'writable_on' => 'standard',
2056
					'contents' => array(
2057
						'temp' => array(
2058
							'type' => 'dir',
2059
						),
2060
						'backup' => array(
2061
							'type' => 'dir',
2062
						),
2063
					),
2064
				),
2065
			),
2066
		),
2067
	);
2068
2069
	// Directories that can move.
2070
	if (substr($sourcedir, 0, strlen($boarddir)) != $boarddir)
2071
	{
2072
		unset($context['file_tree'][strtr($boarddir, array('\\' => '/'))]['contents']['Sources']);
2073
		$context['file_tree'][strtr($sourcedir, array('\\' => '/'))] = array(
2074
			'type' => 'dir',
2075
			'list_contents' => true,
2076
			'writable_on' => 'standard',
2077
		);
2078
	}
2079
2080
	// Moved the cache?
2081
	if (substr($cachedir, 0, strlen($boarddir)) != $boarddir)
2082
	{
2083
		unset($context['file_tree'][strtr($boarddir, array('\\' => '/'))]['contents']['cache']);
2084
		$context['file_tree'][strtr($cachedir, array('\\' => '/'))] = array(
2085
			'type' => 'dir',
2086
			'list_contents' => false,
2087
			'writable_on' => 'restrictive',
2088
		);
2089
	}
2090
2091
	// Are we using multiple attachment directories?
2092
	if (!empty($modSettings['currentAttachmentUploadDir']))
2093
	{
2094
		unset($context['file_tree'][strtr($boarddir, array('\\' => '/'))]['contents']['attachments']);
2095
2096
		if (!is_array($modSettings['attachmentUploadDir']))
2097
			$modSettings['attachmentUploadDir'] = $smcFunc['json_decode']($modSettings['attachmentUploadDir'], true);
2098
2099
		// @todo Should we suggest non-current directories be read only?
2100
		foreach ($modSettings['attachmentUploadDir'] as $dir)
2101
			$context['file_tree'][strtr($dir, array('\\' => '/'))] = array(
2102
			'type' => 'dir',
2103
			'writable_on' => 'restrictive',
2104
		);
2105
	}
2106
	elseif (substr($modSettings['attachmentUploadDir'], 0, strlen($boarddir)) != $boarddir)
2107
	{
2108
		unset($context['file_tree'][strtr($boarddir, array('\\' => '/'))]['contents']['attachments']);
2109
		$context['file_tree'][strtr($modSettings['attachmentUploadDir'], array('\\' => '/'))] = array(
2110
			'type' => 'dir',
2111
			'writable_on' => 'restrictive',
2112
		);
2113
	}
2114
2115
	if (substr($modSettings['smileys_dir'], 0, strlen($boarddir)) != $boarddir)
2116
	{
2117
		unset($context['file_tree'][strtr($boarddir, array('\\' => '/'))]['contents']['Smileys']);
2118
		$context['file_tree'][strtr($modSettings['smileys_dir'], array('\\' => '/'))] = array(
2119
			'type' => 'dir_recursive',
2120
			'writable_on' => 'standard',
2121
		);
2122
	}
2123
	if (substr($modSettings['avatar_directory'], 0, strlen($boarddir)) != $boarddir)
2124
	{
2125
		unset($context['file_tree'][strtr($boarddir, array('\\' => '/'))]['contents']['avatars']);
2126
		$context['file_tree'][strtr($modSettings['avatar_directory'], array('\\' => '/'))] = array(
2127
			'type' => 'dir',
2128
			'writable_on' => 'standard',
2129
		);
2130
	}
2131
	if (isset($modSettings['custom_avatar_dir']) && substr($modSettings['custom_avatar_dir'], 0, strlen($boarddir)) != $boarddir)
2132
	{
2133
		unset($context['file_tree'][strtr($boarddir, array('\\' => '/'))]['contents']['custom_avatar_dir']);
2134
		$context['file_tree'][strtr($modSettings['custom_avatar_dir'], array('\\' => '/'))] = array(
2135
			'type' => 'dir',
2136
			'writable_on' => 'restrictive',
2137
		);
2138
	}
2139
2140
	// Load up any custom themes.
2141
	$request = $smcFunc['db_query']('', '
2142
		SELECT value
2143
		FROM {db_prefix}themes
2144
		WHERE id_theme > {int:default_theme_id}
2145
			AND id_member = {int:guest_id}
2146
			AND variable = {string:theme_dir}
2147
		ORDER BY value ASC',
2148
		array(
2149
			'default_theme_id' => 1,
2150
			'guest_id' => 0,
2151
			'theme_dir' => 'theme_dir',
2152
		)
2153
	);
2154
	while ($row = $smcFunc['db_fetch_assoc']($request))
2155
	{
2156
		if (substr(strtolower(strtr($row['value'], array('\\' => '/'))), 0, strlen($boarddir) + 7) == strtolower(strtr($boarddir, array('\\' => '/')) . '/Themes'))
2157
			$context['file_tree'][strtr($boarddir, array('\\' => '/'))]['contents']['Themes']['contents'][substr($row['value'], strlen($boarddir) + 8)] = array(
2158
				'type' => 'dir_recursive',
2159
				'list_contents' => true,
2160
				'contents' => array(
2161
					'languages' => array(
2162
						'type' => 'dir',
2163
						'list_contents' => true,
2164
					),
2165
				),
2166
			);
2167
		else
2168
		{
2169
			$context['file_tree'][strtr($row['value'], array('\\' => '/'))] = array(
2170
				'type' => 'dir_recursive',
2171
				'list_contents' => true,
2172
				'contents' => array(
2173
					'languages' => array(
2174
						'type' => 'dir',
2175
						'list_contents' => true,
2176
					),
2177
				),
2178
			);
2179
		}
2180
	}
2181
	$smcFunc['db_free_result']($request);
2182
2183
	// If we're submitting then let's move on to another function to keep things cleaner..
2184
	if (isset($_POST['action_changes']))
2185
		return PackagePermissionsAction();
2186
2187
	$context['look_for'] = array();
2188
	// Are we looking for a particular tree - normally an expansion?
2189
	if (!empty($_REQUEST['find']))
2190
		$context['look_for'][] = base64_decode($_REQUEST['find']);
2191
	// Only that tree?
2192
	$context['only_find'] = isset($_GET['xml']) && !empty($_REQUEST['onlyfind']) ? $_REQUEST['onlyfind'] : '';
2193
	if ($context['only_find'])
2194
		$context['look_for'][] = $context['only_find'];
2195
2196
	// Have we got a load of back-catalogue trees to expand from a submit etc?
2197
	if (!empty($_GET['back_look']))
2198
	{
2199
		$potententialTrees = $smcFunc['json_decode'](base64_decode($_GET['back_look']), true);
2200
		foreach ($potententialTrees as $tree)
2201
			$context['look_for'][] = $tree;
2202
	}
2203
	// ... maybe posted?
2204
	if (!empty($_POST['back_look']))
2205
		$context['only_find'] = array_merge($context['only_find'], $_POST['back_look']);
2206
2207
	$context['back_look_data'] = base64_encode($smcFunc['json_encode'](array_slice($context['look_for'], 0, 15)));
2208
2209
	// Are we finding more files than first thought?
2210
	$context['file_offset'] = !empty($_REQUEST['fileoffset']) ? (int) $_REQUEST['fileoffset'] : 0;
2211
	// Don't list more than this many files in a directory.
2212
	$context['file_limit'] = 150;
2213
2214
	// How many levels shall we show?
2215
	$context['default_level'] = empty($context['only_find']) ? 2 : 25;
2216
2217
	// This will be used if we end up catching XML data.
2218
	$context['xml_data'] = array(
2219
		'roots' => array(
2220
			'identifier' => 'root',
2221
			'children' => array(
2222
				array(
2223
					'value' => preg_replace('~[^A-Za-z0-9_\-=:]~', ':-:', $context['only_find']),
2224
				),
2225
			),
2226
		),
2227
		'folders' => array(
2228
			'identifier' => 'folder',
2229
			'children' => array(),
2230
		),
2231
	);
2232
2233
	foreach ($context['file_tree'] as $path => $data)
2234
	{
2235
		// Run this directory.
2236
		if (file_exists($path) && (empty($context['only_find']) || substr($context['only_find'], 0, strlen($path)) == $path))
2237
		{
2238
			// Get the first level down only.
2239
			fetchPerms__recursive($path, $context['file_tree'][$path], 1);
2240
			$context['file_tree'][$path]['perms'] = array(
2241
				'chmod' => @is_writable($path),
2242
				'perms' => @fileperms($path),
2243
			);
2244
		}
2245
		else
2246
			unset($context['file_tree'][$path]);
2247
	}
2248
2249
	// Is this actually xml?
2250
	if (isset($_GET['xml']))
2251
	{
2252
		loadTemplate('Xml');
2253
		$context['sub_template'] = 'generic_xml';
2254
		$context['template_layers'] = array();
2255
	}
2256
}
2257
2258
/**
2259
 * Checkes the permissions of all the areas that will be affected by the package
2260
 *
2261
 * @param string $path The path to the directiory to check permissions for
2262
 * @param array $data An array of data about the directory
2263
 * @param int $level How far deep to go
2264
 */
2265
function fetchPerms__recursive($path, &$data, $level)
2266
{
2267
	global $context;
2268
2269
	$isLikelyPath = false;
2270
	foreach ($context['look_for'] as $possiblePath)
2271
		if (substr($possiblePath, 0, strlen($path)) == $path)
2272
			$isLikelyPath = true;
2273
2274
	// Is this where we stop?
2275
	if (isset($_GET['xml']) && !empty($context['look_for']) && !$isLikelyPath)
2276
		return;
2277
	elseif ($level > $context['default_level'] && !$isLikelyPath)
2278
		return;
2279
2280
	// Are we actually interested in saving this data?
2281
	$save_data = empty($context['only_find']) || $context['only_find'] == $path;
2282
2283
	// @todo Shouldn't happen - but better error message?
2284
	if (!is_dir($path))
2285
		fatal_lang_error('no_access', false);
2286
2287
	// This is where we put stuff we've found for sorting.
2288
	$foundData = array(
2289
		'files' => array(),
2290
		'folders' => array(),
2291
	);
2292
2293
	$dh = opendir($path);
2294
	while ($entry = readdir($dh))
0 ignored issues
show
Bug introduced by
It seems like $dh can also be of type false; however, parameter $dir_handle of readdir() does only seem to accept resource, 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

2294
	while ($entry = readdir(/** @scrutinizer ignore-type */ $dh))
Loading history...
2295
	{
2296
		// Some kind of file?
2297
		if (is_file($path . '/' . $entry))
2298
		{
2299
			// Are we listing PHP files in this directory?
2300
			if ($save_data && !empty($data['list_contents']) && substr($entry, -4) == '.php')
2301
				$foundData['files'][$entry] = true;
2302
			// A file we were looking for.
2303
			elseif ($save_data && isset($data['contents'][$entry]))
2304
				$foundData['files'][$entry] = true;
2305
		}
2306
		// It's a directory - we're interested one way or another, probably...
2307
		elseif ($entry != '.' && $entry != '..')
2308
		{
2309
			// Going further?
2310
			if ((!empty($data['type']) && $data['type'] == 'dir_recursive') || (isset($data['contents'][$entry]) && (!empty($data['contents'][$entry]['list_contents']) || (!empty($data['contents'][$entry]['type']) && $data['contents'][$entry]['type'] == 'dir_recursive'))))
2311
			{
2312
				if (!isset($data['contents'][$entry]))
2313
					$foundData['folders'][$entry] = 'dir_recursive';
2314
				else
2315
					$foundData['folders'][$entry] = true;
2316
2317
				// If this wasn't expected inherit the recusiveness...
2318
				if (!isset($data['contents'][$entry]))
2319
					// We need to do this as we will be going all recursive.
2320
					$data['contents'][$entry] = array(
2321
						'type' => 'dir_recursive',
2322
					);
2323
2324
				// Actually do the recursive stuff...
2325
				fetchPerms__recursive($path . '/' . $entry, $data['contents'][$entry], $level + 1);
2326
			}
2327
			// Maybe it is a folder we are not descending into.
2328
			elseif (isset($data['contents'][$entry]))
2329
				$foundData['folders'][$entry] = true;
2330
			// Otherwise we stop here.
2331
		}
2332
	}
2333
	closedir($dh);
0 ignored issues
show
Bug introduced by
It seems like $dh can also be of type false; however, parameter $dir_handle of closedir() does only seem to accept resource, 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

2333
	closedir(/** @scrutinizer ignore-type */ $dh);
Loading history...
2334
2335
	// Nothing to see here?
2336
	if (!$save_data)
2337
		return;
2338
2339
	// Now actually add the data, starting with the folders.
2340
	ksort($foundData['folders']);
2341
	foreach ($foundData['folders'] as $folder => $type)
2342
	{
2343
		$additional_data = array(
2344
			'perms' => array(
2345
				'chmod' => @is_writable($path . '/' . $folder),
2346
				'perms' => @fileperms($path . '/' . $folder),
2347
			),
2348
		);
2349
		if ($type !== true)
2350
			$additional_data['type'] = $type;
2351
2352
		// If there's an offset ignore any folders in XML mode.
2353
		if (isset($_GET['xml']) && $context['file_offset'] == 0)
2354
		{
2355
			$context['xml_data']['folders']['children'][] = array(
2356
				'attributes' => array(
2357
					'writable' => $additional_data['perms']['chmod'] ? 1 : 0,
2358
					'permissions' => substr(sprintf('%o', $additional_data['perms']['perms']), -4),
2359
					'folder' => 1,
2360
					'path' => $context['only_find'],
2361
					'level' => $level,
2362
					'more' => 0,
2363
					'offset' => $context['file_offset'],
2364
					'my_ident' => preg_replace('~[^A-Za-z0-9_\-=:]~', ':-:', $context['only_find'] . '/' . $folder),
2365
					'ident' => preg_replace('~[^A-Za-z0-9_\-=:]~', ':-:', $context['only_find']),
2366
				),
2367
				'value' => $folder,
2368
			);
2369
		}
2370
		elseif (!isset($_GET['xml']))
2371
		{
2372
			if (isset($data['contents'][$folder]))
2373
				$data['contents'][$folder] = array_merge($data['contents'][$folder], $additional_data);
2374
			else
2375
				$data['contents'][$folder] = $additional_data;
2376
		}
2377
	}
2378
2379
	// Now we want to do a similar thing with files.
2380
	ksort($foundData['files']);
2381
	$counter = -1;
2382
	foreach ($foundData['files'] as $file => $dummy)
2383
	{
2384
		$counter++;
2385
2386
		// Have we reached our offset?
2387
		if ($context['file_offset'] > $counter)
2388
			continue;
2389
		// Gone too far?
2390
		if ($counter > ($context['file_offset'] + $context['file_limit']))
2391
			continue;
2392
2393
		$additional_data = array(
2394
			'perms' => array(
2395
				'chmod' => @is_writable($path . '/' . $file),
2396
				'perms' => @fileperms($path . '/' . $file),
2397
			),
2398
		);
2399
2400
		// XML?
2401
		if (isset($_GET['xml']))
2402
		{
2403
			$context['xml_data']['folders']['children'][] = array(
2404
				'attributes' => array(
2405
					'writable' => $additional_data['perms']['chmod'] ? 1 : 0,
2406
					'permissions' => substr(sprintf('%o', $additional_data['perms']['perms']), -4),
2407
					'folder' => 0,
2408
					'path' => $context['only_find'],
2409
					'level' => $level,
2410
					'more' => $counter == ($context['file_offset'] + $context['file_limit']) ? 1 : 0,
2411
					'offset' => $context['file_offset'],
2412
					'my_ident' => preg_replace('~[^A-Za-z0-9_\-=:]~', ':-:', $context['only_find'] . '/' . $file),
2413
					'ident' => preg_replace('~[^A-Za-z0-9_\-=:]~', ':-:', $context['only_find']),
2414
				),
2415
				'value' => $file,
2416
			);
2417
		}
2418
		elseif ($counter != ($context['file_offset'] + $context['file_limit']))
2419
		{
2420
			if (isset($data['contents'][$file]))
2421
				$data['contents'][$file] = array_merge($data['contents'][$file], $additional_data);
2422
			else
2423
				$data['contents'][$file] = $additional_data;
2424
		}
2425
	}
2426
}
2427
2428
/**
2429
 * Actually action the permission changes they want.
2430
 */
2431
function PackagePermissionsAction()
2432
{
2433
	global $smcFunc, $context, $txt, $time_start, $package_ftp;
2434
2435
	umask(0);
2436
2437
	$timeout_limit = 5;
2438
2439
	$context['method'] = $_POST['method'] == 'individual' ? 'individual' : 'predefined';
2440
	$context['sub_template'] = 'action_permissions';
2441
	$context['page_title'] = $txt['package_file_perms_applying'];
2442
	$context['back_look_data'] = isset($_POST['back_look']) ? $_POST['back_look'] : array();
2443
2444
	// Skipping use of FTP?
2445
	if (empty($package_ftp))
2446
		$context['skip_ftp'] = true;
2447
2448
	// We'll start off in a good place, security. Make sure that if we're dealing with individual files that they seem in the right place.
2449
	if ($context['method'] == 'individual')
2450
	{
2451
		// Only these path roots are legal.
2452
		$legal_roots = array_keys($context['file_tree']);
2453
		$context['custom_value'] = (int) $_POST['custom_value'];
2454
2455
		// Continuing?
2456
		if (isset($_POST['toProcess']))
2457
			$_POST['permStatus'] = $smcFunc['json_decode'](base64_decode($_POST['toProcess']), true);
2458
2459
		if (isset($_POST['permStatus']))
2460
		{
2461
			$context['to_process'] = array();
2462
			$validate_custom = false;
2463
			foreach ($_POST['permStatus'] as $path => $status)
2464
			{
2465
				// Nothing to see here?
2466
				if ($status == 'no_change')
2467
					continue;
2468
				$legal = false;
2469
				foreach ($legal_roots as $root)
2470
					if (substr($path, 0, strlen($root)) == $root)
2471
						$legal = true;
2472
2473
				if (!$legal)
2474
					continue;
2475
2476
				// Check it exists.
2477
				if (!file_exists($path))
2478
					continue;
2479
2480
				if ($status == 'custom')
2481
					$validate_custom = true;
2482
2483
				// Now add it.
2484
				$context['to_process'][$path] = $status;
2485
			}
2486
			$context['total_items'] = isset($_POST['totalItems']) ? (int) $_POST['totalItems'] : count($context['to_process']);
2487
2488
			// Make sure the chmod status is valid?
2489
			if ($validate_custom)
2490
			{
2491
				if (preg_match('~^[4567][4567][4567]$~', $context['custom_value']) == false)
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing preg_match('~^[4567][456...ontext['custom_value']) of type integer to the boolean false. If you are specifically checking for 0, consider using something more explicit like === 0 instead.
Loading history...
2492
					fatal_error($txt['chmod_value_invalid']);
2493
			}
2494
2495
			// Nothing to do?
2496
			if (empty($context['to_process']))
2497
				redirectexit('action=admin;area=packages;sa=perms' . (!empty($context['back_look_data']) ? ';back_look=' . base64_encode($smcFunc['json_encode']($context['back_look_data'])) : '') . ';' . $context['session_var'] . '=' . $context['session_id']);
2498
		}
2499
		// Should never get here,
2500
		else
2501
			fatal_lang_error('no_access', false);
2502
2503
		// Setup the custom value.
2504
		$custom_value = octdec('0' . $context['custom_value']);
2505
2506
		// Start processing items.
2507
		foreach ($context['to_process'] as $path => $status)
2508
		{
2509
			if (in_array($status, array('execute', 'writable', 'read')))
2510
				package_chmod($path, $status);
2511
			elseif ($status == 'custom' && !empty($custom_value))
2512
			{
2513
				// Use FTP if we have it.
2514
				if (!empty($package_ftp) && !empty($_SESSION['pack_ftp']))
2515
				{
2516
					$ftp_file = strtr($path, array($_SESSION['pack_ftp']['root'] => ''));
2517
					$package_ftp->chmod($ftp_file, $custom_value);
2518
				}
2519
				else
2520
					smf_chmod($path, $custom_value);
2521
			}
2522
2523
			// This fish is fried...
2524
			unset($context['to_process'][$path]);
2525
2526
			// See if we're out of time?
2527
			if ((time() - $time_start) > $timeout_limit)
2528
			{
2529
				// Prepare template usage for to_process.
2530
				$context['to_process_encode'] = base64_encode($smcFunc['json_encode']($context['to_process']));
2531
2532
				return false;
2533
			}
2534
		}
2535
2536
		// Prepare template usage for to_process.
2537
		$context['to_process_encode'] = base64_encode($smcFunc['json_encode']($context['to_process']));
2538
	}
2539
	// If predefined this is a little different.
2540
	else
2541
	{
2542
		$context['predefined_type'] = isset($_POST['predefined']) ? $_POST['predefined'] : 'restricted';
2543
2544
		$context['total_items'] = isset($_POST['totalItems']) ? (int) $_POST['totalItems'] : 0;
2545
		$context['directory_list'] = isset($_POST['dirList']) ? $smcFunc['json_decode'](base64_decode($_POST['dirList']), true) : array();
2546
2547
		$context['file_offset'] = isset($_POST['fileOffset']) ? (int) $_POST['fileOffset'] : 0;
2548
2549
		// Haven't counted the items yet?
2550
		if (empty($context['total_items']))
2551
		{
2552
			/**
2553
			 * Counts all the directories under a given path
2554
			 *
2555
			 * @param string $dir
2556
			 * @return integer
2557
			 */
2558
			function count_directories__recursive($dir)
2559
			{
2560
				global $context;
2561
2562
				$count = 0;
2563
				$dh = @opendir($dir);
2564
				while ($entry = readdir($dh))
0 ignored issues
show
Bug introduced by
It seems like $dh can also be of type false; however, parameter $dir_handle of readdir() does only seem to accept resource, 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

2564
				while ($entry = readdir(/** @scrutinizer ignore-type */ $dh))
Loading history...
2565
				{
2566
					if ($entry != '.' && $entry != '..' && is_dir($dir . '/' . $entry))
2567
					{
2568
						$context['directory_list'][$dir . '/' . $entry] = 1;
2569
						$count++;
2570
						$count += count_directories__recursive($dir . '/' . $entry);
2571
					}
2572
				}
2573
				closedir($dh);
0 ignored issues
show
Bug introduced by
It seems like $dh can also be of type false; however, parameter $dir_handle of closedir() does only seem to accept resource, 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

2573
				closedir(/** @scrutinizer ignore-type */ $dh);
Loading history...
2574
2575
				return $count;
2576
			}
2577
2578
			foreach ($context['file_tree'] as $path => $data)
2579
			{
2580
				if (is_dir($path))
2581
				{
2582
					$context['directory_list'][$path] = 1;
2583
					$context['total_items'] += count_directories__recursive($path);
2584
					$context['total_items']++;
2585
				}
2586
			}
2587
		}
2588
2589
		// Have we built up our list of special files?
2590
		if (!isset($_POST['specialFiles']) && $context['predefined_type'] != 'free')
2591
		{
2592
			$context['special_files'] = array();
2593
2594
			/**
2595
			 * Builds a list of special files recursively for a given path
2596
			 *
2597
			 * @param string $path
2598
			 * @param array $data
2599
			 */
2600
			function build_special_files__recursive($path, &$data)
2601
			{
2602
				global $context;
2603
2604
				if (!empty($data['writable_on']))
2605
					if ($context['predefined_type'] == 'standard' || $data['writable_on'] == 'restrictive')
2606
						$context['special_files'][$path] = 1;
2607
2608
				if (!empty($data['contents']))
2609
					foreach ($data['contents'] as $name => $contents)
2610
						build_special_files__recursive($path . '/' . $name, $contents);
2611
			}
2612
2613
			foreach ($context['file_tree'] as $path => $data)
2614
				build_special_files__recursive($path, $data);
2615
		}
2616
		// Free doesn't need special files.
2617
		elseif ($context['predefined_type'] == 'free')
2618
			$context['special_files'] = array();
2619
		else
2620
			$context['special_files'] = $smcFunc['json_decode'](base64_decode($_POST['specialFiles']), true);
2621
2622
		// Now we definitely know where we are, we need to go through again doing the chmod!
2623
		foreach ($context['directory_list'] as $path => $dummy)
2624
		{
2625
			// Do the contents of the directory first.
2626
			$dh = @opendir($path);
2627
			$file_count = 0;
2628
			$dont_chmod = false;
2629
			while ($entry = readdir($dh))
0 ignored issues
show
Bug introduced by
It seems like $dh can also be of type false; however, parameter $dir_handle of readdir() does only seem to accept resource, 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

2629
			while ($entry = readdir(/** @scrutinizer ignore-type */ $dh))
Loading history...
2630
			{
2631
				$file_count++;
2632
				// Actually process this file?
2633
				if (!$dont_chmod && !is_dir($path . '/' . $entry) && (empty($context['file_offset']) || $context['file_offset'] < $file_count))
2634
				{
2635
					$status = $context['predefined_type'] == 'free' || isset($context['special_files'][$path . '/' . $entry]) ? 'writable' : 'execute';
2636
					package_chmod($path . '/' . $entry, $status);
2637
				}
2638
2639
				// See if we're out of time?
2640
				if (!$dont_chmod && (time() - $time_start) > $timeout_limit)
2641
				{
2642
					$dont_chmod = true;
2643
					// Don't do this again.
2644
					$context['file_offset'] = $file_count;
2645
				}
2646
			}
2647
			closedir($dh);
0 ignored issues
show
Bug introduced by
It seems like $dh can also be of type false; however, parameter $dir_handle of closedir() does only seem to accept resource, 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

2647
			closedir(/** @scrutinizer ignore-type */ $dh);
Loading history...
2648
2649
			// If this is set it means we timed out half way through.
2650
			if ($dont_chmod)
2651
			{
2652
				$context['total_files'] = $file_count;
2653
				$context['directory_list_encode'] = base64_encode($smcFunc['json_encode']($context['directory_list']));
2654
				$context['special_files_encode'] = base64_encode($smcFunc['json_encode']($context['special_files']));
2655
				return false;
2656
			}
2657
2658
			// Do the actual directory.
2659
			$status = $context['predefined_type'] == 'free' || isset($context['special_files'][$path]) ? 'writable' : 'execute';
2660
			package_chmod($path, $status);
2661
2662
			// We've finished the directory so no file offset, and no record.
2663
			$context['file_offset'] = 0;
2664
			unset($context['directory_list'][$path]);
2665
2666
			// See if we're out of time?
2667
			if ((time() - $time_start) > $timeout_limit)
2668
			{
2669
				// Prepare this for usage on templates.
2670
				$context['directory_list_encode'] = base64_encode($smcFunc['json_encode']($context['directory_list']));
2671
				$context['special_files_encode'] = base64_encode($smcFunc['json_encode']($context['special_files']));
2672
2673
				return false;
2674
			}
2675
		}
2676
2677
		// Prepare this for usage on templates.
2678
		$context['directory_list_encode'] = base64_encode($smcFunc['json_encode']($context['directory_list']));
2679
		$context['special_files_encode'] = base64_encode($smcFunc['json_encode']($context['special_files']));
2680
	}
2681
2682
	// If we're here we are done!
2683
	redirectexit('action=admin;area=packages;sa=perms' . (!empty($context['back_look_data']) ? ';back_look=' . base64_encode($smcFunc['json_encode']($context['back_look_data'])) : '') . ';' . $context['session_var'] . '=' . $context['session_id']);
2684
}
2685
2686
/**
2687
 * Test an FTP connection.
2688
 */
2689
function PackageFTPTest()
2690
{
2691
	global $context, $txt, $package_ftp;
2692
2693
	checkSession('get');
2694
2695
	// Try to make the FTP connection.
2696
	create_chmod_control(array(), array('force_find_error' => true));
2697
2698
	// Deal with the template stuff.
2699
	loadTemplate('Xml');
2700
	$context['sub_template'] = 'generic_xml';
2701
	$context['template_layers'] = array();
2702
2703
	// Define the return data, this is simple.
2704
	$context['xml_data'] = array(
2705
		'results' => array(
2706
			'identifier' => 'result',
2707
			'children' => array(
2708
				array(
2709
					'attributes' => array(
2710
						'success' => !empty($package_ftp) ? 1 : 0,
2711
					),
2712
					'value' => !empty($package_ftp) ? $txt['package_ftp_test_success'] : (isset($context['package_ftp'], $context['package_ftp']['error']) ? $context['package_ftp']['error'] : $txt['package_ftp_test_failed']),
2713
				),
2714
			),
2715
		),
2716
	);
2717
}
2718
2719
?>