pkgsubmit.php 14.2 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

12
include_lang('submit_po.inc');   # use some form of this for i18n support
eric's avatar
eric committed
13
14
set_lang();                 # this sets up the visitor's language
check_sid();                # see if they're still logged in
eric's avatar
eric committed
15

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

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

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

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

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

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

eric's avatar
eric committed
33
		if (!$error) {
34
			if (!@mkdir($tempdir)) {
35
				$error = __("Could not create incoming directory: %s.", $tempdir);
36
			} else {
37
				if (!@chdir($tempdir)) {
38
					$error = __("Could not change directory to %s.", $tempdir);
39
				} else {
40
41
42
43
44
45
46
47
48
49
					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.");
						}
					}
50
51
52
				}
			}
		}
eric's avatar
eric committed
53

54
		# Find the PKGBUILD
55
		if (!$error) {
56
57
58
59
60
61
62
63
			$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.");
			}
64
		}
jchu's avatar
jchu committed
65

66
		# if no error, get list of directory contents and process PKGBUILD
Callan Barrett's avatar
Callan Barrett committed
67
68
		# TODO: This needs to be completely rewritten to support stuff like arrays
		# and variable substitution among other things.
69
		if (!$error) {
70
			# process PKGBIULD - remove line concatenation
eric's avatar
eric committed
71
			#
72
73
			$pkgbuild = array();
			$fp = fopen($pkg_dir."/PKGBUILD", "r");
74
75
			$line_no = 0;
			$lines = array();
76
			$decomment = array();
77
78
			$continuation_line = 0;
			$current_line = "";
79
			$paren_depth = 0;
80
			while (!feof($fp)) {
81
				$line = trim(fgets($fp));
82
				$char_counts = count_chars($line, 0);
83
				$paren_depth += $char_counts[ord('(')] - $char_counts[ord(')')];
84
85
86
87
88
				if (substr($line, strlen($line)-1) == "\\") {
					# continue appending onto existing line_no
					#
					$current_line .= substr($line, 0, strlen($line)-1);
					$continuation_line = 1;
89
				} elseif ($paren_depth > 0) {
90
					# assumed continuation
91
92
					# continue appending onto existing line_no
					#
93
94
					$decomment = explode("#",$line,2);
					$line = $decomment[0];
95
96
					$current_line .= $line . " ";
					$continuation_line = 1;
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
				} 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);

117
			# Now process the lines and put any var=val lines into the
118
			# 'pkgbuild' array. Also check to make sure it has the build()
119
120
			# function.
			#
121
122
			$seen_build_function = 0;
			while (list($k, $line) = each($lines)) {
jchu's avatar
jchu committed
123
				$lparts = explode("=", $line, 2);
124
				if (count($lparts) == 2) {
125
					# this is a variable/value pair, strip out
126
					# array parens and any quoting, except in pkgdesc
127
					# for pkgdesc, only remove start/end pairs of " or '
128
					if ($lparts[0]=="pkgdesc") {
129
130
131
132
						if ($lparts[1]{0} == '"' && 
								$lparts[1]{strlen($lparts[1])-1} == '"') {
							$pkgbuild[$lparts[0]] = substr($lparts[1], 1, -1);
						}
133
						elseif 
134
135
136
137
138
							($lparts[1]{0} == "'" && 
							 $lparts[1]{strlen($lparts[1])-1} == "'") {
							$pkgbuild[$lparts[0]] = substr($lparts[1], 1, -1);
						} else { 
							$pkgbuild[$lparts[0]] = $lparts[1];
139
						}
140
141
142
143
					} else {
						$pkgbuild[$lparts[0]] = str_replace(array("(",")","\"","'"), "",
								$lparts[1]);
					}
144
				} else {
145
					# either a comment, blank line, continued line, or build function
146
147
					#
					if (substr($lparts[0], 0, 5) == "build") {
148
						$seen_build_function = 1;
149
150
151
					}
				}
			}
eric's avatar
eric committed
152

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

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

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

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

184
185
186
		# Now, run through the pkgbuild array and do any $pkgname/$pkgver
		# substituions.
		#
Callan Barrett's avatar
Callan Barrett committed
187
		# TODO: run through and do ALL substitutions, to cover custom vars
188
189
190
191
192
		if (!$error) {
			$pkgname_var = $pkgbuild["pkgname"];
			$pkgver_var = $pkgbuild["pkgver"];
			$new_pkgbuild = array();
			while (list($k, $v) = each($pkgbuild)) {
193
194
195
196
				$v = str_replace('$pkgname', $pkgname_var, $v);
				$v = str_replace('${pkgname}', $pkgname_var, $v);
				$v = str_replace('$pkgver', $pkgver_var, $v);
				$v = str_replace('${pkgver}', $pkgver_var, $v);
197
198
199
				$new_pkgbuild[$k] = $v;
			}
		}
200

Callan Barrett's avatar
Callan Barrett committed
201
		# Now we've parsed the pkgbuild, let's move it to where it belongs
202
203
204
		if (!$error) {
			$pkg_name = str_replace("'", "", $pkgbuild['pkgname']);
			$pkg_name = escapeshellarg($pkg_name);
Callan Barrett's avatar
Callan Barrett committed
205
			$pkg_name = str_replace("'", "", $pkg_name);
206

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

Callan Barrett's avatar
Callan Barrett committed
209
			if (!$presult) {
210
211
212
213
				$error = __("Invalid name: only lowercase letters are allowed.");
			}
		}

214
215
216
		if (isset($pkg_name)) {
			$incoming_pkgdir = INCOMING_DIR . $pkg_name;
		}
217

218
		if (!$error) {
Callan Barrett's avatar
Callan Barrett committed
219
			# First, see if this package already exists, and if it can be overwritten
220
221
			$pkg_exists = package_exists($pkg_name);
			if (can_submit_pkg($pkg_name, $_COOKIE["AURSID"])) {
222
				if (file_exists($incoming_pkgdir)) {
Callan Barrett's avatar
Callan Barrett committed
223
					# Blow away the existing file/dir and contents
224
					rm_rf($incoming_pkgdir);
225
226
				}

227
228
				if (!@mkdir($incoming_pkgdir)) {
					$error = __( "Could not create directory %s.", $incoming_pkgdir);
229
230
				}

231
				rename($pkg_dir, $incoming_pkgdir . "/" . $pkg_name);
232
			} else {
233
				$error = __( "You are not allowed to overwrite the %h%s%h package.", "<b>", $pkg_name, "</b>");
234
235
236
			}
		}

237
238
		# Re-tar the package for consistency's sake
		if (!$error) {
239
240
			if (!@chdir($incoming_pkgdir)) {
				$error = __("Could not change directory to %s.", $incoming_pkgdir);
241
242
			}
		}
243

244
		if (!$error) {
245
246
247
			$tar = new Archive_Tar($pkg_name . '.tar.gz');
			$create = $tar->create(array($pkg_name));

248
			if (!$create) {
249
250
251
				$error = __("Could not re-tar");
			}
		}
252

253
		# Chmod files after everything has been done.
254
		if (!$error && !chmod_group($incoming_pkgdir)) {
255
256
257
			$error = __("Could not chmod directory %s.", $incoming_pkgdir);
		}

Callan Barrett's avatar
Callan Barrett committed
258
		# Whether it failed or not we can clean this out
259
260
		if (file_exists($tempdir)) {
			rm_rf($tempdir);
261
262
		}

Callan Barrett's avatar
Callan Barrett committed
263
		# Update the backend database
264
		if (!$error) {
265

266
			$dbh = db_connect();
267

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

Callan Barrett's avatar
Callan Barrett committed
272
			$q = "SELECT * FROM Packages WHERE Name = '" . mysql_real_escape_string($new_pkgbuild['pkgname']) . "'";
273
274
275
276
277
			$result = db_query($q, $dbh);
			$pdata = mysql_fetch_assoc($result);

			if ($pdata) {

Callan Barrett's avatar
Callan Barrett committed
278
279
				# Flush out old data that will be replaced with new data
				$q = "DELETE FROM PackageDepends WHERE PackageID = " . $pdata["ID"];
280
				db_query($q, $dbh);
Callan Barrett's avatar
Callan Barrett committed
281
				$q = "DELETE FROM PackageSources WHERE PackageID = " . $pdata["ID"];
282
				db_query($q, $dbh);
283

Callan Barrett's avatar
Callan Barrett committed
284
285
				# If the package was a dummy, undummy it
				if ($pdata['DummyPkg']) {
286
					$q = sprintf( "UPDATE Packages SET DummyPkg = 0, SubmitterUID = %d, MaintainerUID = %d, SubmittedTS = UNIX_TIMESTAMP() WHERE ID = %d",
Dan McGee's avatar
Dan McGee committed
287
288
						$uid,
						$uid,
289
						$pdata["ID"]);
Callan Barrett's avatar
Callan Barrett committed
290

291
					db_query($q, $dbh);
292
				}
293

Callan Barrett's avatar
Callan Barrett committed
294
				# If a new category was chosen, change it to that
295
				if ($_POST['category'] > 1) {
296
297
298
299
300
301
302
					$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
303
				# Update package data
304
305
306
307
308
309
310
				$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']),
311
					mysql_real_escape_string($incoming_pkgdir . "/" . $pkg_name . ".tar.gz"),
312
313
314
					mysql_real_escape_string(URL_DIR . $pkg_name . "/" . $pkg_name . ".tar.gz"),
					$pdata["ID"]);

Callan Barrett's avatar
Callan Barrett committed
315
				db_query($q, $dbh);
316

Callan Barrett's avatar
Callan Barrett committed
317
				# Update package depends
318
				$depends = explode(" ", $new_pkgbuild['depends']);
319
				foreach ($depends as $dep) {
320
					$q = "INSERT INTO PackageDepends (PackageID, DepPkgID, DepCondition) VALUES (";
Callan Barrett's avatar
Callan Barrett committed
321
					$deppkgname = preg_replace("/[<>]?=.*/", "", $dep);
322
323
324
325
326
327
					$depcondition = str_replace($deppkgname, "", $dep);

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

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

331
					db_query($q, $dbh);
332
				}
Callan Barrett's avatar
Callan Barrett committed
333
334
335
336

				# Insert sources
				$sources = explode(" ", $new_pkgbuild['source']);
				foreach ($sources as $src) {
337
338
339
340
341
					if ($src != "" ) {
						$q = "INSERT INTO PackageSources (PackageID, Source) VALUES (";
						$q .= $pdata["ID"] . ", '" . mysql_real_escape_string($src) . "')";
						db_query($q, $dbh);
					}
342
				}
343
344

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

346
			} else {
347

Callan Barrett's avatar
Callan Barrett committed
348
				# This is a brand new package
349
350
351
352
353
354
355
356
				$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
357
358
					$uid,
					$uid,
359
					mysql_real_escape_string($incoming_pkgdir . "/" . $pkg_name . ".tar.gz"),
360
					mysql_real_escape_string(URL_DIR . $pkg_name . "/" . $pkg_name . ".tar.gz"));
jchu's avatar
jchu committed
361

Callan Barrett's avatar
Callan Barrett committed
362
				$result = db_query($q, $dbh);
jchu's avatar
jchu committed
363
364
				$packageID = mysql_insert_id($dbh);

Callan Barrett's avatar
Callan Barrett committed
365
				# Update package depends
366
				$depends = explode(" ", $new_pkgbuild['depends']);
Callan Barrett's avatar
Callan Barrett committed
367
				foreach ($depends as $dep) {
Callan Barrett's avatar
Callan Barrett committed
368
					$q = "INSERT INTO PackageDepends (PackageID, DepPkgID, DepCondition) VALUES (";
Callan Barrett's avatar
Callan Barrett committed
369
370
					$deppkgname = preg_replace("/[<>]?=.*/", "", $dep);
					$depcondition = str_replace($deppkgname, "", $dep);
371
372
373
374
375
376
377
378

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

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

jchu's avatar
jchu committed
379
					db_query($q, $dbh);
380
				}
jchu's avatar
jchu committed
381

Callan Barrett's avatar
Callan Barrett committed
382
383
384
				# Insert sources
				$sources = explode(" ", $new_pkgbuild['source']);
				foreach ($sources as $src) {
385
386
387
388
389
					if ($src != "" ) {
						$q = "INSERT INTO PackageSources (PackageID, Source) VALUES (";
						$q .= $packageID . ", '" . mysql_real_escape_string($src) . "')";
						db_query($q, $dbh);
					}
390
				}
391
392

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

394
395
			}
		}
396
397

		chdir($_SERVER['DOCUMENT_ROOT']);
eric's avatar
eric committed
398
399
	}

400
401
402
# Logic over, let's do some output

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

404
405
406
?>

<div class="pgbox">
407
408
409
410
	<div class="pgboxtitle">
		<span class="f3"><?php print __("Submit"); ?></span>
	</div>
	<div class="pgboxbody">
eric's avatar
eric committed
411

412
<?php
413
	if (empty($_REQUEST['pkgsubmit']) || $error):
414
		# User is not uploading, or there were errors uploading - then
eric's avatar
eric committed
415
		# give the visitor the default upload form
Callan Barrett's avatar
Callan Barrett committed
416
417
418
419
420
421
422
423
424
425
		if (ini_get("file_uploads")):
			if ($error):
?>

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

<?php
			endif;

426
427
			$pkg_categories = pkgCategories();
			$pkg_locations = pkgLocations();
428
?>
429

430
<form action='pkgsubmit.php' method='post' enctype='multipart/form-data'>
431
432
433
434
435
436
437
438
	<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
439
440
441
					foreach ($pkg_categories as $num => $cat):
						print "<option value='" . $num . "'";
						if (isset($_POST['category']) && $_POST['category'] == $cat):
442
							print " selected='selected'";
Callan Barrett's avatar
Callan Barrett committed
443
444
445
						endif;
						print ">" . $cat . "</option>";
					endforeach;
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
				?>
			</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
465
		else:
eric's avatar
eric committed
466
			print __("Sorry, uploads are not permitted by this server.");
Callan Barrett's avatar
Callan Barrett committed
467
468
469
470
471
472
473
474
475
?>

<br />

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

Callan Barrett's avatar
Callan Barrett committed
479
<br />
480

Callan Barrett's avatar
Callan Barrett committed
481
482
<?php
endif;
483
484
?>

485
	</div>
486
487
488
</div>

<?php
tardo's avatar
tardo committed
489
html_footer(AUR_VERSION);
490