pkgsubmit.php 15 KB
Newer Older
1
<?php
pjmattal's avatar
pjmattal committed
2

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

Dan McGee's avatar
Dan McGee committed
6
require_once('Archive/Tar.php');
pjmattal's avatar
pjmattal committed
7

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

eric's avatar
eric committed
11
12
set_lang();                 # this sets up the visitor's language
check_sid();                # see if they're still logged in
eric's avatar
eric committed
13

14
15
$cwd = getcwd();

16
17
18
19
20
21
22
23
if ($_COOKIE["AURSID"]) {
	$uid = uid_from_sid($_COOKIE['AURSID']);
}
else {
	$uid = NULL;
}

if ($uid):
24

25
	# Track upload errors
eric's avatar
eric committed
26
27
	$error = "";

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

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

35
		# Before processing, make sure we even have a file
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
		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;
dsa's avatar
dsa committed
53
		}
54

55
56
57
58
		# Check whether the file is gzip'ed
		if (!$error) {
			$fh = fopen($_FILES['pfile']['tmp_name'], 'rb');
			fseek($fh, 0, SEEK_SET);
59
			list(, $magic) = unpack('v', fread($fh, 2));
60
61
62
63
64
65

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

66
67
68
		# Check uncompressed file size (ZIP bomb protection)
		if (!$error && $MAX_FILESIZE_UNCOMPRESSED) {
			fseek($fh, -4, SEEK_END);
69
			list(, $filesize_uncompressed) = unpack('V', fread($fh, 4));
70
71
72
73
74
75

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

76
		# Close file handle before extracting stuff
77
		if (isset($fh) && is_resource($fh)) {
78
79
80
			fclose($fh);
		}

eric's avatar
eric committed
81
		if (!$error) {
82
			$tar = new Archive_Tar($_FILES['pfile']['tmp_name']);
83

84
85
			# Extract PKGBUILD into a string
			$pkgbuild_raw = '';
86
			$dircount = 0;
87
			foreach ($tar->listContent() as $tar_file) {
88
89
90
91
92
93
94
95
				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']);
					}
96
				}
97
98
99
100
101
102
				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) {
103
104
105
106
						$error = __("Error - source tarball may not contain more than one directory.");
						break;
					}
				}
107
			}
108

109
			if (!$error && empty($pkgbuild_raw)) {
110
111
				$error = __("Error trying to unpack upload - PKGBUILD does not exist.");
			}
112
		}
jchu's avatar
jchu committed
113

114
		# if no error, get list of directory contents and process PKGBUILD
Callan Barrett's avatar
Callan Barrett committed
115
116
		# TODO: This needs to be completely rewritten to support stuff like arrays
		# and variable substitution among other things.
117
		if (!$error) {
118
			# process PKGBUILD - remove line concatenation
eric's avatar
eric committed
119
			#
120
			$pkgbuild = array();
121
122
123
124
			$line_no = 0;
			$lines = array();
			$continuation_line = 0;
			$current_line = "";
125
			$paren_depth = 0;
126
			foreach (explode("\n", $pkgbuild_raw) as $line) {
127
				$line = trim($line);
128
129
130
				# Remove comments
				$line = preg_replace('/\s*#.*/', '', $line);

131
				$char_counts = count_chars($line, 0);
132
				$paren_depth += $char_counts[ord('(')] - $char_counts[ord(')')];
133
134
135
136
137
				if (substr($line, strlen($line)-1) == "\\") {
					# continue appending onto existing line_no
					#
					$current_line .= substr($line, 0, strlen($line)-1);
					$continuation_line = 1;
138
				} elseif ($paren_depth > 0) {
139
					# assumed continuation
140
141
142
143
					# continue appending onto existing line_no
					#
					$current_line .= $line . " ";
					$continuation_line = 1;
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
				} 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++;
				}
			}

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

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

				if (!empty($lparts)) {
176
					# this is a variable/value pair, strip out
177
					# array parens and any quoting, except in pkgdesc
178
					# for pkgdesc, only remove start/end pairs of " or '
179
					if ($lparts[0]=="pkgdesc") {
Loui Chang's avatar
Loui Chang committed
180
						if ($lparts[1]{0} == '"' &&
181
182
183
								$lparts[1]{strlen($lparts[1])-1} == '"') {
							$pkgbuild[$lparts[0]] = substr($lparts[1], 1, -1);
						}
Loui Chang's avatar
Loui Chang committed
184
185
						elseif
							($lparts[1]{0} == "'" &&
186
187
							 $lparts[1]{strlen($lparts[1])-1} == "'") {
							$pkgbuild[$lparts[0]] = substr($lparts[1], 1, -1);
Loui Chang's avatar
Loui Chang committed
188
						} else {
189
							$pkgbuild[$lparts[0]] = $lparts[1];
190
						}
191
192
193
194
					} else {
						$pkgbuild[$lparts[0]] = str_replace(array("(",")","\"","'"), "",
								$lparts[1]);
					}
195
				}
196
			}
eric's avatar
eric committed
197

198
			# some error checking on PKGBUILD contents - just make sure each
Loui Chang's avatar
Loui Chang committed
199
			# variable has a value. This does not do any validity checking
200
			# on the values, or attempts to fix line continuation/wrapping.
201
			$req_vars = array("url", "pkgdesc", "license", "pkgrel", "pkgver", "arch", "pkgname");
Callan Barrett's avatar
Callan Barrett committed
202
			foreach ($req_vars as $var) {
203
				if (!array_key_exists($var, $pkgbuild)) {
204
205
					$error = __('Missing %s variable in PKGBUILD.', $var);
					break;
206
207
				}
			}
208
209
210
		}

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

215
		# Check for http:// or other protocol in url
Loui Chang's avatar
Loui Chang committed
216
		#
217
		if (!$error) {
218
219
220
221
			$parsed_url = parse_url($pkgbuild['url']);
			if (!$parsed_url['scheme']) {
				$error = __("Package URL is missing a protocol (ie. http:// ,ftp://)");
			}
222
		}
223

224
		# Now, run through the pkgbuild array, and do "eval" and simple substituions.
225
226
		if (!$error) {
			while (list($k, $v) = each($pkgbuild)) {
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
				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);
						}
					}
				}
244
245
246

				# Simple variable replacement
				$pattern_var = '/\$({?)([_\w]+)(}?)/';
247
248
249
250
251
252
253
				$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])) {
254
						$replacement = substr($new_pkgbuild[$var], strpos($new_pkgbuild[$var], " "));
255
256
257
258
259
260
					}
					else {
						$replacement = '';
					}

					$v = substr_replace($v, $replacement, $pos, $len);
261
					$offset = $pos + strlen($replacement);
262
				}
263
264
265
				$new_pkgbuild[$k] = $v;
			}
		}
266

Callan Barrett's avatar
Callan Barrett committed
267
		# Now we've parsed the pkgbuild, let's move it to where it belongs
268
		if (!$error) {
269
			$pkg_name = str_replace("'", "", $new_pkgbuild['pkgname']);
270
			$pkg_name = escapeshellarg($pkg_name);
Callan Barrett's avatar
Callan Barrett committed
271
			$pkg_name = str_replace("'", "", $pkg_name);
272

273
			$presult = preg_match("/^[a-z0-9][a-z0-9\.+_-]*$/", $pkg_name);
274

Callan Barrett's avatar
Callan Barrett committed
275
			if (!$presult) {
276
277
278
279
				$error = __("Invalid name: only lowercase letters are allowed.");
			}
		}

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
		# Determine the full package version with epoch
		if (!$error) {
			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']);
			}
		}

		# 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);
			}
		}

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

313
		if (!$error) {
Callan Barrett's avatar
Callan Barrett committed
314
			# First, see if this package already exists, and if it can be overwritten
315
			$pkg_id = pkgid_from_name($pkg_name);
316
			if (can_submit_pkg($pkg_name, $_COOKIE["AURSID"])) {
317
				if (file_exists($incoming_pkgdir)) {
Callan Barrett's avatar
Callan Barrett committed
318
					# Blow away the existing file/dir and contents
319
					rm_tree($incoming_pkgdir);
320
321
				}

322
323
				# The mode is masked by the current umask, so not as scary as it looks
				if (!mkdir($incoming_pkgdir, 0777, true)) {
324
					$error = __( "Could not create directory %s.", $incoming_pkgdir);
325
326
				}
			} else {
Lukas Fleischer's avatar
Lukas Fleischer committed
327
				$error = __( "You are not allowed to overwrite the %s%s%s package.", "<strong>", $pkg_name, "</strong>");
328
			}
329
330
331

		if (!$error) {
			# Check if package name is blacklisted.
332
			if (!$pkg_id && pkgname_is_blacklisted($pkg_name)) {
333
334
335
336
337
				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);
				}
			}
		}
338
339
		}

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

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

Callan Barrett's avatar
Callan Barrett committed
349
		# Update the backend database
350
351
		if (!$error) {
			$dbh = db_connect();
canyonknight's avatar
canyonknight committed
352
			begin_atomic_commit($dbh);
353

canyonknight's avatar
canyonknight committed
354
			$pdata = pkgdetails_by_pkgname($new_pkgbuild['pkgname'], $dbh);
355

356
357
358
359
360
361
362
363
364
365
366
367
			# 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;
			}

368
			if ($pdata) {
369
370
371
				# 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.
Dan McGee's avatar
Dan McGee committed
372
				$packageID = $pdata["ID"];
373

Callan Barrett's avatar
Callan Barrett committed
374
				# Flush out old data that will be replaced with new data
canyonknight's avatar
canyonknight committed
375
376
				remove_pkg_deps($packageID, $dbh);
				remove_pkg_sources($packageID, $dbh);
377

Callan Barrett's avatar
Callan Barrett committed
378
				# If a new category was chosen, change it to that
379
				if ($category_id > 1) {
canyonknight's avatar
canyonknight committed
380
					update_pkg_category($packageID, $category_id);
381
382
				}

Callan Barrett's avatar
Callan Barrett committed
383
				# Update package data
canyonknight's avatar
canyonknight committed
384
				update_pkgdetails($packageID, $new_pkgbuild['pkgname'], $new_pkgbuild['license'], $pkg_version, "", $new_pkgbuild['pkgdesc'], $new_pkgbuild['url'], "", $uid, $dbh);
385
			} else {
Callan Barrett's avatar
Callan Barrett committed
386
				# This is a brand new package
canyonknight's avatar
canyonknight committed
387
388
				new_pkgdetails($new_pkgbuild['pkgname'], $new_pkgbuild['license'], $pkg_version, $category_id, $new_pkgbuild['pkgdesc'], $new_pkgbuild['url'], $uid, $dbh);
				$packageID = last_insert_id($dbh);
jchu's avatar
jchu committed
389

Dan McGee's avatar
Dan McGee committed
390
			}
391

Dan McGee's avatar
Dan McGee committed
392
			# Update package depends
393
394
			if (!empty($new_pkgbuild['depends'])) {
				$depends = explode(" ", $new_pkgbuild['depends']);
395
396
397
				foreach ($depends as $dep) {
					$deppkgname = preg_replace("/(<|<=|=|>=|>).*/", "", $dep);
					$depcondition = str_replace($deppkgname, "", $dep);
398

399
400
401
402
403
404
					if ($deppkgname == "") {
						continue;
					}
					else if ($deppkgname == "#") {
						break;
					}
canyonknight's avatar
canyonknight committed
405
					add_pkg_dep($packageID, $deppkgname, $depcondition, $dbh);
406
				}
Dan McGee's avatar
Dan McGee committed
407
			}
408

Dan McGee's avatar
Dan McGee committed
409
			# Insert sources
410
411
412
			if (!empty($new_pkgbuild['source'])) {
				$sources = explode(" ", $new_pkgbuild['source']);
				foreach ($sources as $src) {
canyonknight's avatar
canyonknight committed
413
					add_pkg_src($packageID, $src, $dbh);
Dan McGee's avatar
Dan McGee committed
414
415
				}
			}
416

Dan McGee's avatar
Dan McGee committed
417
418
419
			# 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) {
420
				pkg_notify(account_from_sid($_COOKIE["AURSID"], $dbh), array($packageID), true, $dbh);
421
			}
Dan McGee's avatar
Dan McGee committed
422

423
			# Entire package creation process is atomic
canyonknight's avatar
canyonknight committed
424
			end_atomic_commit($dbh);
425

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

429
		chdir($cwd);
eric's avatar
eric committed
430
431
	}

432
433
434
# Logic over, let's do some output

html_header("Submit");
eric's avatar
eric committed
435

436
437
?>

Loui Chang's avatar
Loui Chang committed
438
<?php if ($error): ?>
Lukas Fleischer's avatar
Lukas Fleischer committed
439
	<p class="pkgoutput"><?= $error ?></p>
Loui Chang's avatar
Loui Chang committed
440
441
<?php endif; ?>

442
<div class="box">
Lukas Fleischer's avatar
Lukas Fleischer committed
443
444
	<h2><?= __("Submit"); ?></h2>
	<p><?= __("Upload your source packages here. Create source packages with `makepkg --source`.") ?></p>
eric's avatar
eric committed
445

446
<?php
447
	if (empty($_REQUEST['pkgsubmit']) || $error):
448
		# User is not uploading, or there were errors uploading - then
eric's avatar
eric committed
449
		# give the visitor the default upload form
Callan Barrett's avatar
Callan Barrett committed
450
451
		if (ini_get("file_uploads")):

452
			$pkg_categories = pkgCategories();
453
?>
454

Lukas Fleischer's avatar
Lukas Fleischer committed
455
<form action="<?= get_uri('/submit/'); ?>" method="post" enctype="multipart/form-data">
456
457
458
	<fieldset>
		<div>
			<input type="hidden" name="pkgsubmit" value="1" />
Lukas Fleischer's avatar
Lukas Fleischer committed
459
			<input type="hidden" name="token" value="<?= htmlspecialchars($_COOKIE['AURSID']) ?>" />
460
461
		</div>
		<p>
Lukas Fleischer's avatar
Lukas Fleischer committed
462
			<label for="id_category"><?= __("Package Category"); ?>:</label>
463
			<select id="id_category" name="category">
Lukas Fleischer's avatar
Lukas Fleischer committed
464
				<option value="1"><?= __("Select Category"); ?></option>
465
				<?php
Callan Barrett's avatar
Callan Barrett committed
466
					foreach ($pkg_categories as $num => $cat):
467
						print '<option value="' . $num . '"';
Callan Barrett's avatar
Callan Barrett committed
468
						if (isset($_POST['category']) && $_POST['category'] == $cat):
469
							print ' selected="selected"';
Callan Barrett's avatar
Callan Barrett committed
470
						endif;
471
						print '>' . $cat . '</option>';
Callan Barrett's avatar
Callan Barrett committed
472
					endforeach;
473
474
				?>
			</select>
475
476
		</p>
		<p>
Lukas Fleischer's avatar
Lukas Fleischer committed
477
			<label for="id_file"><?= __("Upload package file"); ?>:</label>
478
479
480
481
			<input id="id_file" type="file" name="pfile" size='30' />
		</p>
		<p>
			<label></label>
Lukas Fleischer's avatar
Lukas Fleischer committed
482
			<input class="button" type="submit" value="<?= __("Upload"); ?>" />
483
484
		</p>
	</fieldset>
485
</form>
486
</div>
487
<?php
Callan Barrett's avatar
Callan Barrett committed
488
		else:
eric's avatar
eric committed
489
			print __("Sorry, uploads are not permitted by this server.");
Callan Barrett's avatar
Callan Barrett committed
490
491
492
?>

<br />
493
</div>
Callan Barrett's avatar
Callan Barrett committed
494
495
496
497
498
<?php
		endif;
	endif;
else:
	# Visitor is not logged in
499
	html_header("Submit");
eric's avatar
eric committed
500
	print __("You must create an account before you can upload packages.");
Callan Barrett's avatar
Callan Barrett committed
501
?>
502

Callan Barrett's avatar
Callan Barrett committed
503
<br />
504

Callan Barrett's avatar
Callan Barrett committed
505
506
<?php
endif;
507
508
?>

509

510
511

<?php
tardo's avatar
tardo committed
512
html_footer(AUR_VERSION);
513