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');
Dan McGee's avatar
Dan McGee committed
4
include_once("config.inc");
5

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

Dan McGee's avatar
Dan McGee committed
9
10
include_once("aur.inc");         # access AUR common functions
include_once("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
						$tar = new Archive_Tar($_FILES['pfile']['tmp_name']);
						$extract = $tar->extract();

						if (!$extract) {
							$error = __("Unknown file format for uploaded file.");
						}
45
46
47
				}
			}
		}
eric's avatar
eric committed
48

49
		# Find the PKGBUILD
50
		if (!$error) {
51
52
53
54
55
56
57
58
			$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.");
			}
59
		}
jchu's avatar
jchu committed
60

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Callan Barrett's avatar
Callan Barrett committed
233
			if (!$presult) {
234
235
236
237
				$error = __("Invalid name: only lowercase letters are allowed.");
			}
		}

238
239
240
		if (isset($pkg_name)) {
			$incoming_pkgdir = INCOMING_DIR . $pkg_name;
		}
241

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

251
252
				if (!@mkdir($incoming_pkgdir)) {
					$error = __( "Could not create directory %s.", $incoming_pkgdir);
253
254
				}

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

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

268
		if (!$error) {
269
270
271
			$tar = new Archive_Tar($pkg_name . '.tar.gz');
			$create = $tar->create(array($pkg_name));

272
			if (!$create) {
273
274
275
				$error = __("Could not re-tar");
			}
		}
276

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

Callan Barrett's avatar
Callan Barrett committed
282
		# Whether it failed or not we can clean this out
283
284
		if (file_exists($tempdir)) {
			rm_rf($tempdir);
285
286
		}

Callan Barrett's avatar
Callan Barrett committed
287
		# Update the backend database
288
		if (!$error) {
289

290
			$dbh = db_connect();
291

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

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

			if ($pdata) {

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

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

315
					db_query($q, $dbh);
316
				}
317

Callan Barrett's avatar
Callan Barrett committed
318
				# If a new category was chosen, change it to that
319
				if ($_POST['category'] > 1) {
320
321
322
323
324
325
326
					$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
327
				# Update package data
328
329
330
331
332
333
334
				$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']),
335
					mysql_real_escape_string($incoming_pkgdir . "/" . $pkg_name . ".tar.gz"),
336
337
338
					mysql_real_escape_string(URL_DIR . $pkg_name . "/" . $pkg_name . ".tar.gz"),
					$pdata["ID"]);

Callan Barrett's avatar
Callan Barrett committed
339
				db_query($q, $dbh);
340

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

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

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

355
					db_query($q, $dbh);
356
				}
Callan Barrett's avatar
Callan Barrett committed
357
358
359
360

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

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

370
			} else {
371
				$uid = uid_from_sid($_COOKIE["AURSID"]);
372

Callan Barrett's avatar
Callan Barrett committed
373
				# This is a brand new package
374
375
376
377
378
379
380
381
				$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
382
383
					$uid,
					$uid,
384
					mysql_real_escape_string($incoming_pkgdir . "/" . $pkg_name . ".tar.gz"),
385
					mysql_real_escape_string(URL_DIR . $pkg_name . "/" . $pkg_name . ".tar.gz"));
jchu's avatar
jchu committed
386

Callan Barrett's avatar
Callan Barrett committed
387
				$result = db_query($q, $dbh);
jchu's avatar
jchu committed
388
389
				$packageID = mysql_insert_id($dbh);

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

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

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

jchu's avatar
jchu committed
404
					db_query($q, $dbh);
405
				}
jchu's avatar
jchu committed
406

Callan Barrett's avatar
Callan Barrett committed
407
408
409
				# Insert sources
				$sources = explode(" ", $new_pkgbuild['source']);
				foreach ($sources as $src) {
410
411
412
413
414
					if ($src != "" ) {
						$q = "INSERT INTO PackageSources (PackageID, Source) VALUES (";
						$q .= $packageID . ", '" . mysql_real_escape_string($src) . "')";
						db_query($q, $dbh);
					}
415
				}
416

417
				pkg_notify(account_from_sid($_COOKIE["AURSID"]), array($packageID));
418
419

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

421
422
			}
		}
423
424

		chdir($_SERVER['DOCUMENT_ROOT']);
eric's avatar
eric committed
425
426
	}

427
428
429
# Logic over, let's do some output

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

431
432
433
?>

<div class="pgbox">
434
435
436
437
	<div class="pgboxtitle">
		<span class="f3"><?php print __("Submit"); ?></span>
	</div>
	<div class="pgboxbody">
438
	<p><?php echo __("Upload your source packages here. Create source packages with `makepkg --source`.") ?></p>
eric's avatar
eric committed
439

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

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

<?php
			endif;

454
455
			$pkg_categories = pkgCategories();
			$pkg_locations = pkgLocations();
456
?>
457

458
<form action='pkgsubmit.php' method='post' enctype='multipart/form-data'>
459
	<div> <input type='hidden' name='pkgsubmit' value='1' /> </div>
460
461
	<table border='0' cellspacing='5'>
		<tr>
462
463
			<td class='f4' align='right'><?php print __("Package Category"); ?>:</td>
			<td class='f4' align='left'>
464
465
466
			<select name='category'>
				<option value='1'><?php print __("Select Category"); ?></option>
				<?php
Callan Barrett's avatar
Callan Barrett committed
467
468
469
					foreach ($pkg_categories as $num => $cat):
						print "<option value='" . $num . "'";
						if (isset($_POST['category']) && $_POST['category'] == $cat):
470
							print " selected='selected'";
Callan Barrett's avatar
Callan Barrett committed
471
472
473
						endif;
						print ">" . $cat . "</option>";
					endforeach;
474
475
476
477
478
				?>
			</select>
			</td>
		</tr>
		<tr>
479
480
			<td class='f4' align='right'><?php print __("Upload package file"); ?>:</td>
			<td class='f4' align='left'>
481
482
483
484
485
486
487
488
489
490
491
492
				<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
493
		else:
eric's avatar
eric committed
494
			print __("Sorry, uploads are not permitted by this server.");
Callan Barrett's avatar
Callan Barrett committed
495
496
497
498
499
500
501
502
503
?>

<br />

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

Callan Barrett's avatar
Callan Barrett committed
507
<br />
508

Callan Barrett's avatar
Callan Barrett committed
509
510
<?php
endif;
511
512
?>

513
	</div>
514
515
516
</div>

<?php
tardo's avatar
tardo committed
517
html_footer(AUR_VERSION);
518