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

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

require('Archive/Tar.php');
Simo Leone's avatar
Simo Leone committed
7
require('Find.php');
pjmattal's avatar
pjmattal committed
8

eric's avatar
eric committed
9
include("aur.inc");         # access AUR common functions
10
include("pkgfuncs.inc");    # package functions
11

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

Callan Barrett's avatar
Callan Barrett committed
15
if ($_COOKIE["AURSID"]):
16

17
	# Track upload errors
eric's avatar
eric committed
18
19
	$error = "";

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

22
		# Before processing, make sure we even have a file
23
24
		if ($_FILES['pfile']['size'] == 0){
			$error = __("Error - No file uploaded");
dsa's avatar
dsa committed
25
		}
26

Dan McGee's avatar
Dan McGee committed
27
28
		$uid = uid_from_sid($_COOKIE['AURSID']);

29
		# Temporary dir to put the tarball contents
Dan McGee's avatar
Dan McGee committed
30
		$tempdir = UPLOAD_DIR . $uid . time();
31

eric's avatar
eric committed
32
		if (!$error) {
33
			if (!@mkdir($tempdir)) {
34
				$error = __("Could not create incoming directory: %s.", $tempdir);
35
			} else {
36
				if (!@chdir($tempdir)) {
37
					$error = __("Could not change directory to %s.", $tempdir);
38
				} else {
39
40
41
42
43
44
45
46
47
48
					if ($_FILES['pfile']['name'] == "PKGBUILD") {
						move_uploaded_file($_FILES['pfile']['tmp_name'], $tempdir . "/PKGBUILD");
					} else {
						$tar = new Archive_Tar($_FILES['pfile']['tmp_name']);
						$extract = $tar->extract();

						if (!$extract) {
							$error = __("Unknown file format for uploaded file.");
						}
					}
49
50
51
				}
			}
		}
eric's avatar
eric committed
52

53
		# Find the PKGBUILD
54
		if (!$error) {
55
56
57
58
59
60
61
62
			$pkgbuild = File_Find::search('PKGBUILD', $tempdir);

			if (count($pkgbuild)) {
				$pkgbuild = $pkgbuild[0];
				$pkg_dir = dirname($pkgbuild);
			} else {
				$error = __("Error trying to unpack upload - PKGBUILD does not exist.");
			}
63
		}
jchu's avatar
jchu committed
64

65
		# if no error, get list of directory contents and process PKGBUILD
Callan Barrett's avatar
Callan Barrett committed
66
67
		# TODO: This needs to be completely rewritten to support stuff like arrays
		# and variable substitution among other things.
68
		if (!$error) {
69
			# process PKGBUILD - remove line concatenation
eric's avatar
eric committed
70
			#
71
72
			$pkgbuild = array();
			$fp = fopen($pkg_dir."/PKGBUILD", "r");
73
74
75
76
			$line_no = 0;
			$lines = array();
			$continuation_line = 0;
			$current_line = "";
77
			$paren_depth = 0;
78
			while (!feof($fp)) {
79
				$line = trim(fgets($fp));
80
81
82
				# Remove comments
				$line = preg_replace('/\s*#.*/', '', $line);

83
				$char_counts = count_chars($line, 0);
84
				$paren_depth += $char_counts[ord('(')] - $char_counts[ord(')')];
85
86
87
88
89
				if (substr($line, strlen($line)-1) == "\\") {
					# continue appending onto existing line_no
					#
					$current_line .= substr($line, 0, strlen($line)-1);
					$continuation_line = 1;
90
				} elseif ($paren_depth > 0) {
91
					# assumed continuation
92
93
94
95
					# continue appending onto existing line_no
					#
					$current_line .= $line . " ";
					$continuation_line = 1;
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
				} 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++;
				}
			}
			fclose($fp);

116
			# Now process the lines and put any var=val lines into the
117
			# 'pkgbuild' array. Also check to make sure it has the build()
118
119
			# function.
			#
120
121
			$seen_build_function = 0;
			while (list($k, $line) = each($lines)) {
122
123
124
				# Neutralize parameter substitution
				$line = preg_replace('/\${(\w+)#(\w*)}?/', '$1$2', $line);

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

				if (!empty($lparts)) {
132
					# this is a variable/value pair, strip out
133
					# array parens and any quoting, except in pkgdesc
134
					# for pkgdesc, only remove start/end pairs of " or '
135
					if ($lparts[0]=="pkgdesc") {
Loui Chang's avatar
Loui Chang committed
136
						if ($lparts[1]{0} == '"' &&
137
138
139
								$lparts[1]{strlen($lparts[1])-1} == '"') {
							$pkgbuild[$lparts[0]] = substr($lparts[1], 1, -1);
						}
Loui Chang's avatar
Loui Chang committed
140
141
						elseif
							($lparts[1]{0} == "'" &&
142
143
							 $lparts[1]{strlen($lparts[1])-1} == "'") {
							$pkgbuild[$lparts[0]] = substr($lparts[1], 1, -1);
Loui Chang's avatar
Loui Chang committed
144
						} else {
145
							$pkgbuild[$lparts[0]] = $lparts[1];
146
						}
147
148
149
150
					} else {
						$pkgbuild[$lparts[0]] = str_replace(array("(",")","\"","'"), "",
								$lparts[1]);
					}
151
152
153
154
				}
				else {
					# Non variable assignment line. (comment, blank, command, etc)
					if (preg_match('/\s*build\s*\(\)/', $line)) {
155
						$seen_build_function = 1;
156
157
158
					}
				}
			}
eric's avatar
eric committed
159

160
			# some error checking on PKGBUILD contents - just make sure each
Loui Chang's avatar
Loui Chang committed
161
			# variable has a value. This does not do any validity checking
162
			# on the values, or attempts to fix line continuation/wrapping.
eric's avatar
eric committed
163
			#
164
			if (!$seen_build_function) {
165
166
				$error = __("Missing build function in PKGBUILD.");
			}
167

168
			$req_vars = array("url", "pkgdesc", "license", "pkgrel", "pkgver", "arch", "pkgname");
Callan Barrett's avatar
Callan Barrett committed
169
			foreach ($req_vars as $var) {
170
				if (!array_key_exists($var, $pkgbuild)) {
171
172
					$error = __('Missing %s variable in PKGBUILD.', $var);
					break;
173
174
				}
			}
175
176
177
		}

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

182
		# Check for http:// or other protocol in url
Loui Chang's avatar
Loui Chang committed
183
		#
184
		if (!$error) {
185
186
187
188
			$parsed_url = parse_url($pkgbuild['url']);
			if (!$parsed_url['scheme']) {
				$error = __("Package URL is missing a protocol (ie. http:// ,ftp://)");
			}
189
		}
190

191
		# Now, run through the pkgbuild array, and do "eval" and simple substituions.
192
193
		if (!$error) {
			while (list($k, $v) = each($pkgbuild)) {
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
				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);
						}
					}
				}
211
212
213

				# Simple variable replacement
				$pattern_var = '/\$({?)([_\w]+)(}?)/';
214
215
				while (preg_match($pattern_var,$v,$regs)) {
					$pieces = explode(" ",$pkgbuild["$regs[2]"],2);
216

217
					$pattern = '/\$'.$regs[1].$regs[2].$regs[3].'/';
218
219
220
221
222
					if ($regs[2] != $k) {
						$replacement = $pieces[0];
					} else {
						$replacement = "";
					}
223
224
					$v=preg_replace($pattern, $replacement, $v);
				}
225
226
227
				$new_pkgbuild[$k] = $v;
			}
		}
228

Callan Barrett's avatar
Callan Barrett committed
229
		# Now we've parsed the pkgbuild, let's move it to where it belongs
230
		if (!$error) {
231
			$pkg_name = str_replace("'", "", $new_pkgbuild['pkgname']);
232
			$pkg_name = escapeshellarg($pkg_name);
Callan Barrett's avatar
Callan Barrett committed
233
			$pkg_name = str_replace("'", "", $pkg_name);
234

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

Callan Barrett's avatar
Callan Barrett committed
237
			if (!$presult) {
238
239
240
241
				$error = __("Invalid name: only lowercase letters are allowed.");
			}
		}

242
243
244
		if (isset($pkg_name)) {
			$incoming_pkgdir = INCOMING_DIR . $pkg_name;
		}
245

246
		if (!$error) {
Callan Barrett's avatar
Callan Barrett committed
247
			# First, see if this package already exists, and if it can be overwritten
248
249
			$pkg_exists = package_exists($pkg_name);
			if (can_submit_pkg($pkg_name, $_COOKIE["AURSID"])) {
250
				if (file_exists($incoming_pkgdir)) {
Callan Barrett's avatar
Callan Barrett committed
251
					# Blow away the existing file/dir and contents
252
					rm_rf($incoming_pkgdir);
253
254
				}

255
256
				if (!@mkdir($incoming_pkgdir)) {
					$error = __( "Could not create directory %s.", $incoming_pkgdir);
257
258
				}

259
				rename($pkg_dir, $incoming_pkgdir . "/" . $pkg_name);
260
			} else {
261
				$error = __( "You are not allowed to overwrite the %h%s%h package.", "<b>", $pkg_name, "</b>");
262
263
264
			}
		}

265
266
		# Re-tar the package for consistency's sake
		if (!$error) {
267
268
			if (!@chdir($incoming_pkgdir)) {
				$error = __("Could not change directory to %s.", $incoming_pkgdir);
269
270
			}
		}
271

272
		if (!$error) {
273
274
275
			$tar = new Archive_Tar($pkg_name . '.tar.gz');
			$create = $tar->create(array($pkg_name));

276
			if (!$create) {
277
278
279
				$error = __("Could not re-tar");
			}
		}
280

281
		# Chmod files after everything has been done.
282
		if (!$error && !chmod_group($incoming_pkgdir)) {
283
284
285
			$error = __("Could not chmod directory %s.", $incoming_pkgdir);
		}

Callan Barrett's avatar
Callan Barrett committed
286
		# Whether it failed or not we can clean this out
287
288
		if (file_exists($tempdir)) {
			rm_rf($tempdir);
289
290
		}

Callan Barrett's avatar
Callan Barrett committed
291
		# Update the backend database
292
		if (!$error) {
293

294
			$dbh = db_connect();
295

Callan Barrett's avatar
Callan Barrett committed
296
			# This is an overwrite of an existing package, the database ID
297
			# needs to be preserved so that any votes are retained. However,
Callan Barrett's avatar
Callan Barrett committed
298
			# PackageDepends and PackageSources can be purged.
299

Callan Barrett's avatar
Callan Barrett committed
300
			$q = "SELECT * FROM Packages WHERE Name = '" . mysql_real_escape_string($new_pkgbuild['pkgname']) . "'";
301
302
303
304
305
			$result = db_query($q, $dbh);
			$pdata = mysql_fetch_assoc($result);

			if ($pdata) {

Callan Barrett's avatar
Callan Barrett committed
306
307
				# Flush out old data that will be replaced with new data
				$q = "DELETE FROM PackageDepends WHERE PackageID = " . $pdata["ID"];
308
				db_query($q, $dbh);
Callan Barrett's avatar
Callan Barrett committed
309
				$q = "DELETE FROM PackageSources WHERE PackageID = " . $pdata["ID"];
310
				db_query($q, $dbh);
311

Callan Barrett's avatar
Callan Barrett committed
312
313
				# If the package was a dummy, undummy it
				if ($pdata['DummyPkg']) {
314
					$q = sprintf( "UPDATE Packages SET DummyPkg = 0, SubmitterUID = %d, MaintainerUID = %d, SubmittedTS = UNIX_TIMESTAMP() WHERE ID = %d",
Dan McGee's avatar
Dan McGee committed
315
316
						$uid,
						$uid,
317
						$pdata["ID"]);
Callan Barrett's avatar
Callan Barrett committed
318

319
					db_query($q, $dbh);
320
				}
321

Callan Barrett's avatar
Callan Barrett committed
322
				# If a new category was chosen, change it to that
323
				if ($_POST['category'] > 1) {
324
325
326
327
328
329
330
					$q = sprintf( "UPDATE Packages SET CategoryID = %d WHERE ID = %d",
						mysql_real_escape_string($_REQUEST['category']),
						$pdata["ID"]);

					db_query($q, $dbh);
				}

Callan Barrett's avatar
Callan Barrett committed
331
				# Update package data
332
333
334
335
336
337
338
				$q = sprintf("UPDATE Packages SET ModifiedTS = UNIX_TIMESTAMP(), Name = '%s', Version = '%s-%s', License = '%s', Description = '%s', URL = '%s', LocationID = 2, FSPath = '%s', URLPath = '%s', OutOfDate = 0 WHERE ID = %d",
					mysql_real_escape_string($new_pkgbuild['pkgname']),
					mysql_real_escape_string($new_pkgbuild['pkgver']),
					mysql_real_escape_string($new_pkgbuild['pkgrel']),
					mysql_real_escape_string($new_pkgbuild['license']),
					mysql_real_escape_string($new_pkgbuild['pkgdesc']),
					mysql_real_escape_string($new_pkgbuild['url']),
339
					mysql_real_escape_string($incoming_pkgdir . "/" . $pkg_name . ".tar.gz"),
340
341
342
					mysql_real_escape_string(URL_DIR . $pkg_name . "/" . $pkg_name . ".tar.gz"),
					$pdata["ID"]);

Callan Barrett's avatar
Callan Barrett committed
343
				db_query($q, $dbh);
344

Callan Barrett's avatar
Callan Barrett committed
345
				# Update package depends
346
				$depends = explode(" ", $new_pkgbuild['depends']);
347
				foreach ($depends as $dep) {
348
					$q = "INSERT INTO PackageDepends (PackageID, DepPkgID, DepCondition) VALUES (";
Callan Barrett's avatar
Callan Barrett committed
349
					$deppkgname = preg_replace("/[<>]?=.*/", "", $dep);
350
351
352
353
354
355
					$depcondition = str_replace($deppkgname, "", $dep);

					if ($deppkgname == "#") {
						break;
					}

jchu's avatar
jchu committed
356
					$deppkgid = create_dummy($deppkgname, $_COOKIE['AURSID']);
357
					$q .= $pdata["ID"] . ", " . $deppkgid . ", '" . mysql_real_escape_string($depcondition) . "')";
jchu's avatar
jchu committed
358

359
					db_query($q, $dbh);
360
				}
Callan Barrett's avatar
Callan Barrett committed
361
362
363
364

				# Insert sources
				$sources = explode(" ", $new_pkgbuild['source']);
				foreach ($sources as $src) {
365
366
367
368
369
					if ($src != "" ) {
						$q = "INSERT INTO PackageSources (PackageID, Source) VALUES (";
						$q .= $pdata["ID"] . ", '" . mysql_real_escape_string($src) . "')";
						db_query($q, $dbh);
					}
370
				}
371
372

				header('Location: packages.php?ID=' . $pdata['ID']);
373

374
			} else {
375
				$uid = uid_from_sid($_COOKIE["AURSID"]);
376

Callan Barrett's avatar
Callan Barrett committed
377
				# This is a brand new package
378
379
380
381
382
383
384
385
				$q = sprintf("INSERT INTO Packages (Name, License, Version, CategoryID, Description, URL, LocationID, SubmittedTS, SubmitterUID, MaintainerUID, FSPath, URLPath) VALUES ('%s', '%s', '%s-%s', %d, '%s', '%s', 2, UNIX_TIMESTAMP(), %d, %d, '%s', '%s')",
					mysql_real_escape_string($new_pkgbuild['pkgname']),
					mysql_real_escape_string($new_pkgbuild['license']),
					mysql_real_escape_string($new_pkgbuild['pkgver']),
					mysql_real_escape_string($new_pkgbuild['pkgrel']),
					mysql_real_escape_string($_REQUEST['category']),
					mysql_real_escape_string($new_pkgbuild['pkgdesc']),
					mysql_real_escape_string($new_pkgbuild['url']),
Dan McGee's avatar
Dan McGee committed
386
387
					$uid,
					$uid,
388
					mysql_real_escape_string($incoming_pkgdir . "/" . $pkg_name . ".tar.gz"),
389
					mysql_real_escape_string(URL_DIR . $pkg_name . "/" . $pkg_name . ".tar.gz"));
jchu's avatar
jchu committed
390

Callan Barrett's avatar
Callan Barrett committed
391
				$result = db_query($q, $dbh);
jchu's avatar
jchu committed
392
393
				$packageID = mysql_insert_id($dbh);

Callan Barrett's avatar
Callan Barrett committed
394
				# Update package depends
395
				$depends = explode(" ", $new_pkgbuild['depends']);
Callan Barrett's avatar
Callan Barrett committed
396
				foreach ($depends as $dep) {
Callan Barrett's avatar
Callan Barrett committed
397
					$q = "INSERT INTO PackageDepends (PackageID, DepPkgID, DepCondition) VALUES (";
Callan Barrett's avatar
Callan Barrett committed
398
399
					$deppkgname = preg_replace("/[<>]?=.*/", "", $dep);
					$depcondition = str_replace($deppkgname, "", $dep);
400
401
402
403
404
405
406
407

					if ($deppkgname == "#") {
						break;
					}

					$deppkgid = create_dummy($deppkgname, $_COOKIE['AURSID']);
					$q .= $packageID . ", " . $deppkgid . ", '" . mysql_real_escape_string($depcondition) . "')";

jchu's avatar
jchu committed
408
					db_query($q, $dbh);
409
				}
jchu's avatar
jchu committed
410

Callan Barrett's avatar
Callan Barrett committed
411
412
413
				# Insert sources
				$sources = explode(" ", $new_pkgbuild['source']);
				foreach ($sources as $src) {
414
415
416
417
418
					if ($src != "" ) {
						$q = "INSERT INTO PackageSources (PackageID, Source) VALUES (";
						$q .= $packageID . ", '" . mysql_real_escape_string($src) . "')";
						db_query($q, $dbh);
					}
419
				}
420
421
				
				pkg_notify(account_from_sid($_COOKIE["AURSID"]), array($packageID));
422
423

				header('Location: packages.php?ID=' . $packageID);
424

425
426
			}
		}
427
428

		chdir($_SERVER['DOCUMENT_ROOT']);
eric's avatar
eric committed
429
430
	}

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

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

435
436
437
?>

<div class="pgbox">
438
439
440
441
	<div class="pgboxtitle">
		<span class="f3"><?php print __("Submit"); ?></span>
	</div>
	<div class="pgboxbody">
eric's avatar
eric committed
442

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

<span class='error'><?php print $error; ?></span><br />
<br />

<?php
			endif;

457
458
			$pkg_categories = pkgCategories();
			$pkg_locations = pkgLocations();
459
?>
460

461
<form action='pkgsubmit.php' method='post' enctype='multipart/form-data'>
462
463
464
465
466
467
468
469
	<input type='hidden' name='pkgsubmit' value='1' />
	<table border='0' cellspacing='5'>
		<tr>
			<td span='f4' align='right'><?php print __("Package Category"); ?>:</td>
			<td span='f4' align='left'>
			<select name='category'>
				<option value='1'><?php print __("Select Category"); ?></option>
				<?php
Callan Barrett's avatar
Callan Barrett committed
470
471
472
					foreach ($pkg_categories as $num => $cat):
						print "<option value='" . $num . "'";
						if (isset($_POST['category']) && $_POST['category'] == $cat):
473
							print " selected='selected'";
Callan Barrett's avatar
Callan Barrett committed
474
475
476
						endif;
						print ">" . $cat . "</option>";
					endforeach;
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
				?>
			</select>
			</td>
		</tr>
		<tr>
			<td span='f4' align='right'><?php print __("Upload package file"); ?>:</td>
			<td span='f4' align='left'>
				<input type='file' name='pfile' size='30' />
			</td>
		</tr>
		<tr>
			<td align='left'>
				<input class='button' type='submit' value='<?php print __("Upload"); ?>' />
			</td>
		</tr>
	</table>
</form>

<?php
Callan Barrett's avatar
Callan Barrett committed
496
		else:
eric's avatar
eric committed
497
			print __("Sorry, uploads are not permitted by this server.");
Callan Barrett's avatar
Callan Barrett committed
498
499
500
501
502
503
504
505
506
?>

<br />

<?php
		endif;
	endif;
else:
	# Visitor is not logged in
eric's avatar
eric committed
507
	print __("You must create an account before you can upload packages.");
Callan Barrett's avatar
Callan Barrett committed
508
?>
509

Callan Barrett's avatar
Callan Barrett committed
510
<br />
511

Callan Barrett's avatar
Callan Barrett committed
512
513
<?php
endif;
514
515
?>

516
	</div>
517
518
519
</div>

<?php
tardo's avatar
tardo committed
520
html_footer(AUR_VERSION);
521