summaryrefslogtreecommitdiff
path: root/web/html/pkgsubmit.php
blob: 373af35112b769470ae2fb491a2bdf9ec804d060 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
<?php

set_include_path(get_include_path() . PATH_SEPARATOR . '../lib');
include_once("config.inc.php");

require_once('Archive/Tar.php');

include_once("aur.inc.php");         # access AUR common functions
include_once("pkgfuncs.inc.php");    # package functions

set_lang();                 # this sets up the visitor's language
check_sid();                # see if they're still logged in

$cwd = getcwd();

if ($_COOKIE["AURSID"]) {
	$uid = uid_from_sid($_COOKIE['AURSID']);
}
else {
	$uid = NULL;
}

if ($uid):

	# Track upload errors
	$error = "";
	$ignore_missing_aurinfo = 0;

	if (isset($_REQUEST['pkgsubmit'])) {

		# Make sure authenticated user submitted the package themselves
		if (!check_token()) {
			$error = __("Invalid token for user action.");
		}

		# Before processing, make sure we even have a file
		switch($_FILES['pfile']['error']) {
			case UPLOAD_ERR_INI_SIZE:
				$maxsize =  ini_get('upload_max_filesize');
				$error = __("Error - Uploaded file larger than maximum allowed size (%s)", $maxsize);
				break;
			case UPLOAD_ERR_PARTIAL:
				$error = __("Error - File partially uploaded");
				break;
			case UPLOAD_ERR_NO_FILE:
				$error = __("Error - No file uploaded");
				break;
			case UPLOAD_ERR_NO_TMP_DIR:
				$error = __("Error - Could not locate temporary upload folder");
				break;
			case UPLOAD_ERR_CANT_WRITE:
				$error = __("Error - File could not be written");
				break;
		}

		# Check whether the file is gzip'ed
		if (!$error) {
			$fh = fopen($_FILES['pfile']['tmp_name'], 'rb');
			fseek($fh, 0, SEEK_SET);
			list(, $magic) = unpack('v', fread($fh, 2));

			if ($magic != 0x8b1f) {
				$error = __("Error - unsupported file format (please submit gzip'ed tarballs generated by makepkg(8) only).");
			}
		}

		# Check uncompressed file size (ZIP bomb protection)
		if (!$error && $MAX_FILESIZE_UNCOMPRESSED) {
			fseek($fh, -4, SEEK_END);
			list(, $filesize_uncompressed) = unpack('V', fread($fh, 4));

			if ($filesize_uncompressed > $MAX_FILESIZE_UNCOMPRESSED) {
				$error = __("Error - uncompressed file size too large.");
			}
		}

		# Close file handle before extracting stuff
		if (isset($fh) && is_resource($fh)) {
			fclose($fh);
		}

		if (!$error) {
			$tar = new Archive_Tar($_FILES['pfile']['tmp_name']);

			# Extract PKGBUILD and .AURINFO into a string
			$pkgbuild_raw = $srcinfo_raw = '';
			$dircount = 0;
			foreach ($tar->listContent() as $tar_file) {
				if ($tar_file['typeflag'] == 0) {
					if (strchr($tar_file['filename'], '/') === false) {
						$error = __("Error - source tarball may not contain files outside a directory.");
						break;
					}
					elseif (substr($tar_file['filename'], -9) == '/PKGBUILD') {
						$pkgbuild_raw = $tar->extractInString($tar_file['filename']);
					}
					elseif (substr($tar_file['filename'], -9) == '/.AURINFO') {
						$srcinfo_raw = $tar->extractInString($tar_file['filename']);
					}
				}
				elseif ($tar_file['typeflag'] == 5) {
					if (substr_count($tar_file['filename'], "/") > 1) {
						$error = __("Error - source tarball may not contain nested subdirectories.");
						break;
					}
					elseif (++$dircount > 1) {
						$error = __("Error - source tarball may not contain more than one directory.");
						break;
					}
				}
			}

			if (!$error && $dircount !== 1) {
				$error = __("Error - source tarball may not contain files outside a directory.");
			}

			if (!$error && empty($pkgbuild_raw)) {
				$error = __("Error trying to unpack upload - PKGBUILD does not exist.");
			}

			if (!$error && empty($srcinfo_raw) && (!isset($_POST['ignore_missing_aurinfo']) || $_POST['ignore_missing_aurinfo'] != 1)) {
				$ignore_missing_aurinfo = 1;
				$error = __("The source package does not contain any meta data. Please use `mkaurball` to create AUR source packages. Support for source packages without .AURINFO entries will be removed in an upcoming release! You can resubmit the package if you want to proceed anyway.");
			}
		}

		# if no error, get list of directory contents and process PKGBUILD
		# TODO: This needs to be completely rewritten to support stuff like arrays
		# and variable substitution among other things.
		if (!$error) {
			# process PKGBUILD - remove line concatenation
			#
			$pkgbuild = array();
			$line_no = 0;
			$lines = array();
			$continuation_line = 0;
			$current_line = "";
			$paren_depth = 0;
			foreach (explode("\n", $pkgbuild_raw) as $line) {
				$line = trim($line);
				# Remove comments
				$line = preg_replace('/\s*#.*/', '', $line);

				$char_counts = count_chars($line, 0);
				$paren_depth += $char_counts[ord('(')] - $char_counts[ord(')')];
				if (substr($line, strlen($line)-1) == "\\") {
					# continue appending onto existing line_no
					#
					$current_line .= substr($line, 0, strlen($line)-1);
					$continuation_line = 1;
				} elseif ($paren_depth > 0) {
					# assumed continuation
					# continue appending onto existing line_no
					#
					$current_line .= $line . " ";
					$continuation_line = 1;
				} else {
					# maybe the last line in a continuation, or a standalone line?
					#
					if ($continuation_line) {
						# append onto existing line_no
						#
						$current_line .= $line;
						$lines[$line_no] = $current_line;
						$current_line = "";
					} else {
						# it's own line_no
						#
						$lines[$line_no] = $line;
					}
					$continuation_line = 0;
					$line_no++;
				}
			}

			# Now process the lines and put any var=val lines into the
			# 'pkgbuild' array.
			while (list($k, $line) = each($lines)) {
				# Neutralize parameter substitution
				$line = preg_replace('/\${(\w+)#(\w*)}?/', '$1$2', $line);

				$lparts = Array();
				# Match variable assignment only.
				if (preg_match('/^\s*[_\w]+=[^=].*/', $line, $matches)) {
					$lparts = explode("=", $matches[0], 2);
				}

				if (!empty($lparts)) {
					# this is a variable/value pair, strip
					# out array parens and any quoting,
					# except in pkgdesc for pkgname or
					# pkgdesc, only remove start/end pairs
					# of " or '
					if ($lparts[0] == "pkgname" || $lparts[0] == "pkgdesc") {
						if ($lparts[1]{0} == '"' &&
								$lparts[1]{strlen($lparts[1])-1} == '"') {
							$pkgbuild[$lparts[0]] = substr($lparts[1], 1, -1);
						}
						elseif
							($lparts[1]{0} == "'" &&
							 $lparts[1]{strlen($lparts[1])-1} == "'") {
							$pkgbuild[$lparts[0]] = substr($lparts[1], 1, -1);
						} else {
							$pkgbuild[$lparts[0]] = $lparts[1];
						}
					} else {
						$pkgbuild[$lparts[0]] = str_replace(array("(",")","\"","'"), "",
								$lparts[1]);
					}
				}
			}

			# some error checking on PKGBUILD contents - just make sure each
			# variable has a value. This does not do any validity checking
			# on the values, or attempts to fix line continuation/wrapping.
			$req_vars = array("url", "pkgdesc", "license", "pkgrel", "pkgver", "arch", "pkgname");
			foreach ($req_vars as $var) {
				if (!array_key_exists($var, $pkgbuild)) {
					$error = __('Missing %s variable in PKGBUILD.', $var);
					break;
				}
			}
		}

		# Now, run through the pkgbuild array, and do "eval" and simple substituions.
		if (!$error) {
			while (list($k, $v) = each($pkgbuild)) {
				if (strpos($k,'eval ') !== false) {
					$k = preg_replace('/^eval[\s]*/', "", $k);
					##"eval" replacements
					$pattern_eval = '/{\$({?)([\w]+)(}?)}/';
					while (preg_match($pattern_eval,$v,$regs)) {
						$pieces = explode(",",$pkgbuild["$regs[2]"]);
						## nongreedy matching! - preserving the order of "eval"
						$pattern = '/([\S]*?){\$'.$regs[1].$regs[2].$regs[3].'}([\S]*)/';
						while (preg_match($pattern,$v,$regs_replace)) {
							$replacement = "";
							for ($i = 0; $i < sizeof($pieces); $i++) {
								$replacement .= $regs_replace[1].$pieces[$i].$regs_replace[2]." ";
							}
							$v=preg_replace($pattern, $replacement, $v, 1);
						}
					}
				}

				# Simple variable replacement
				$pattern_var = '/\$({?)([_\w]+)(}?)/';
				$offset = 0;
				while (preg_match($pattern_var, $v, $regs, PREG_OFFSET_CAPTURE, $offset)) {
					$var = $regs[2][0];
					$pos = $regs[0][1];
					$len = strlen($regs[0][0]);

					if (isset($new_pkgbuild[$var])) {
						$replacement = substr($new_pkgbuild[$var], strpos($new_pkgbuild[$var], " "));
					}
					else {
						$replacement = '';
					}

					$v = substr_replace($v, $replacement, $pos, $len);
					$offset = $pos + strlen($replacement);
				}
				$new_pkgbuild[$k] = $v;
			}
		}

		# Parse .AURINFO and overwrite PKGBUILD fields accordingly
		unset($pkg_version);
		$depends = array();
		$srcinfo_pkgname_count = 0;
		foreach (explode("\n", $srcinfo_raw) as $line) {
			$line = trim($line);
			if (empty($line) || $line[0] == '#') {
				continue;
			}
			list($key, $value) = explode(' = ', $line, 2);
			switch ($key) {
			case 'pkgname':
				$srcinfo_pkgname_count++;
				if ($srcinfo_pkgname_count > 1) {
					$error = __("Error - The AUR does not support split packages!");
				}
				/* Fall-through case. */
			case 'pkgdesc':
			case 'url':
			case 'license':
				$new_pkgbuild[$key] = $value;
				break;
			case 'pkgver':
				$pkg_version = $value;
				break;
			case 'depends':
				$depends[] = $value;
				break;
			}
		}

		# Validate package name
		if (!$error) {
			$pkg_name = $new_pkgbuild['pkgname'];
			if ($pkg_name[0] == '(') {
				$error = __("Error - The AUR does not support split packages!");
			} elseif (!preg_match("/^[a-z0-9][a-z0-9\.+_-]*$/", $pkg_name)) {
				$error = __("Invalid name: only lowercase letters are allowed.");
			}
		}

		# Determine the full package version with epoch
		if (!$error && !isset($pkg_version)) {
			if (isset($new_pkgbuild['epoch']) && (int)$new_pkgbuild['epoch'] > 0) {
				$pkg_version = sprintf('%d:%s-%s', $new_pkgbuild['epoch'], $new_pkgbuild['pkgver'], $new_pkgbuild['pkgrel']);
			} else {
				$pkg_version = sprintf('%s-%s', $new_pkgbuild['pkgver'], $new_pkgbuild['pkgrel']);
			}
		}

		# Check for http:// or other protocol in url
		if (!$error) {
			$parsed_url = parse_url($new_pkgbuild['url']);
			if (!$parsed_url['scheme']) {
				$error = __("Package URL is missing a protocol (ie. http:// ,ftp://)");
			}
		}

		# TODO: This is where other additional error checking can be
		# performed. Examples: #md5sums == #sources?, md5sums of any
		# included files match?, install scriptlet file exists?

		# The DB schema imposes limitations on number of allowed characters
		# Print error message when these limitations are exceeded
		if (!$error) {
			if (strlen($pkg_name) > 64) {
				$error = __("Error - Package name cannot be greater than %d characters", 64);
			}
			if (strlen($new_pkgbuild['url']) > 255) {
				$error = __("Error - Package URL cannot be greater than %d characters", 255);
			}
			if (strlen($new_pkgbuild['pkgdesc']) > 255) {
				$error = __("Error - Package description cannot be greater than %d characters", 255);
			}
			if (strlen($new_pkgbuild['license']) > 40) {
				$error = __("Error - Package license cannot be greater than %d characters", 40);
			}
			if (strlen($pkg_version) > 32) {
				$error = __("Error - Package version cannot be greater than %d characters", 32);
			}
		}

		if (isset($pkg_name)) {
			$incoming_pkgdir = INCOMING_DIR . substr($pkg_name, 0, 2) . "/" . $pkg_name;
		}

		if (!$error) {
			# First, see if this package already exists, and if it can be overwritten
			$pkg_id = pkgid_from_name($pkg_name);
			if (can_submit_pkg($pkg_name, $_COOKIE["AURSID"])) {
				if (file_exists($incoming_pkgdir)) {
					# Blow away the existing file/dir and contents
					rm_tree($incoming_pkgdir);
				}

				# The mode is masked by the current umask, so not as scary as it looks
				if (!mkdir($incoming_pkgdir, 0777, true)) {
					$error = __( "Could not create directory %s.", $incoming_pkgdir);
				}
			} else {
				$error = __( "You are not allowed to overwrite the %s%s%s package.", "<strong>", $pkg_name, "</strong>");
			}

		if (!$error) {
			# Check if package name is blacklisted.
			if (!$pkg_id && pkgname_is_blacklisted($pkg_name)) {
				if (!canSubmitBlacklisted(account_from_sid($_COOKIE["AURSID"]))) {
					$error = __( "%s is on the package blacklist, please check if it's available in the official repos.", $pkg_name);
				}
			}
		}
		}

		if (!$error) {
			if (!chdir($incoming_pkgdir)) {
				$error = __("Could not change directory to %s.", $incoming_pkgdir);
			}

			file_put_contents('PKGBUILD', $pkgbuild_raw);
			move_uploaded_file($_FILES['pfile']['tmp_name'], $pkg_name . '.tar.gz');
		}

		# Update the backend database
		if (!$error) {
			begin_atomic_commit();

			$pdata = pkgdetails_by_pkgname($new_pkgbuild['pkgname']);

			# Check the category to use, "1" meaning "none" (or "keep category" for
			# existing packages).
			if (isset($_POST['category'])) {
				$category_id = intval($_POST['category']);
				if ($category_id <= 0) {
					$category_id = 1;
				}
			}
			else {
				$category_id = 1;
			}

			if ($pdata) {
				# This is an overwrite of an existing package, the database ID
				# needs to be preserved so that any votes are retained. However,
				# PackageDepends and PackageSources can be purged.
				$packageID = $pdata["ID"];

				# Flush out old data that will be replaced with new data
				remove_pkg_deps($packageID);
				remove_pkg_sources($packageID);

				# If a new category was chosen, change it to that
				if ($category_id > 1) {
					update_pkg_category($packageID, $category_id);
				}

				# Update package data
				update_pkgdetails($new_pkgbuild['pkgname'], $new_pkgbuild['license'], $pkg_version, $new_pkgbuild['pkgdesc'], $new_pkgbuild['url'], $uid, $packageID);
			} else {
				# This is a brand new package
				new_pkgdetails($new_pkgbuild['pkgname'], $new_pkgbuild['license'], $pkg_version, $category_id, $new_pkgbuild['pkgdesc'], $new_pkgbuild['url'], $uid);
				$packageID = last_insert_id();

			}

			# Update package depends
			if (empty($depends) && !empty($new_pkgbuild['depends'])) {
				$depends = explode(" ", $new_pkgbuild['depends']);
			}
			if (!empty($depends)) {
				foreach ($depends as $dep) {
					$deppkgname = preg_replace("/(<|<=|=|>=|>).*/", "", $dep);
					$depcondition = str_replace($deppkgname, "", $dep);

					if ($deppkgname == "") {
						continue;
					}
					else if ($deppkgname == "#") {
						break;
					}
					add_pkg_dep($packageID, $deppkgname, $depcondition);
				}
			}

			# Insert sources
			if (!empty($new_pkgbuild['source'])) {
				$sources = explode(" ", $new_pkgbuild['source']);
				foreach ($sources as $src) {
					add_pkg_src($packageID, $src);
				}
			}

			# If we just created this package, or it was an orphan and we
			# auto-adopted, add submitting user to the notification list.
			if (!$pdata || $pdata["MaintainerUID"] === NULL) {
				pkg_notify(account_from_sid($_COOKIE["AURSID"]), array($packageID), true);
			}

			# Entire package creation process is atomic
			end_atomic_commit();

			header('Location: ' . get_pkg_uri($pkg_name));
		}

		chdir($cwd);
	}

# Logic over, let's do some output

html_header("Submit");

?>

<div class="box">
	<h2><?= __("Submit"); ?></h2>
	<p><?= __("Upload your source packages here. Create source packages with `mkaurball`.") ?></p>

<?php
	if (empty($_REQUEST['pkgsubmit']) || $error):
		# User is not uploading, or there were errors uploading - then
		# give the visitor the default upload form
		if (ini_get("file_uploads")):

			$pkg_categories = pkgCategories();
?>

<?php if ($error): ?>
	<ul class="errorlist"><li><?= $error ?></li></ul>
<?php endif; ?>

<form action="<?= get_uri('/submit/'); ?>" method="post" enctype="multipart/form-data">
	<fieldset>
		<div>
			<input type="hidden" name="pkgsubmit" value="1" />
			<input type="hidden" name="ignore_missing_aurinfo" value="<?= $ignore_missing_aurinfo ?>" />
			<input type="hidden" name="token" value="<?= htmlspecialchars($_COOKIE['AURSID']) ?>" />
		</div>
		<p>
			<label for="id_category"><?= __("Package Category"); ?>:</label>
			<select id="id_category" name="category">
				<option value="1"><?= __("Select Category"); ?></option>
				<?php
					foreach ($pkg_categories as $num => $cat):
						print '<option value="' . $num . '"';
						if (isset($_POST['category']) && $_POST['category'] == $cat):
							print ' selected="selected"';
						endif;
						print '>' . $cat . '</option>';
					endforeach;
				?>
			</select>
		</p>
		<p>
			<label for="id_file"><?= __("Upload package file"); ?>:</label>
			<input id="id_file" type="file" name="pfile" size='30' />
		</p>
		<p>
			<label></label>
			<input class="button" type="submit" value="<?= __("Upload"); ?>" />
		</p>
	</fieldset>
</form>
</div>
<?php
		else:
			print __("Sorry, uploads are not permitted by this server.");
?>

<br />
</div>
<?php
		endif;
	endif;
else:
	# Visitor is not logged in
	html_header("Submit");
	print __("You must create an account before you can upload packages.");
?>

<br />

<?php
endif;
?>



<?php
html_footer(AUR_VERSION);