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

Simo Leone's avatar
Simo Leone committed
3
set_include_path(get_include_path() . PATH_SEPARATOR . '../lib' . PATH_SEPARATOR . '../lang');
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
10
include("aur.inc");         # access AUR common functions
include("submit_po.inc");   # use some form of this for i18n support
11
include("pkgfuncs.inc");    # package functions
12

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
21
	$error = "";

	if ($_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

28
29
		# Temporary dir to put the tarball contents
		$tempdir = UPLOAD_DIR . uid_from_sid($_COOKIE['AURSID']) . time();
30

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

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

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

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

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

Callan Barrett's avatar
Callan Barrett committed
154
155
			$req_vars = array("md5sums", "source", "url", "pkgdesc", "license", "pkgrel", "pkgver", "arch", "pkgname");
			foreach ($req_vars as $var) {
156
157
158
159
				if (!array_key_exists($var, $pkgbuild)) {
					$error = __("Missing " . $var . " variable in PKGBUILD.");
				}
			}
160
161
162
		}

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

167
168
		# Check for http:// or other protocol in url
		# 
169
		if (!$error) {
170
171
172
173
			$parsed_url = parse_url($pkgbuild['url']);
			if (!$parsed_url['scheme']) {
				$error = __("Package URL is missing a protocol (ie. http:// ,ftp://)");
			}
174
		}
175

176
177
178
		# Now, run through the pkgbuild array and do any $pkgname/$pkgver
		# substituions.
		#
Callan Barrett's avatar
Callan Barrett committed
179
		# TODO: run through and do ALL substitutions, to cover custom vars
180
181
182
183
184
		if (!$error) {
			$pkgname_var = $pkgbuild["pkgname"];
			$pkgver_var = $pkgbuild["pkgver"];
			$new_pkgbuild = array();
			while (list($k, $v) = each($pkgbuild)) {
185
186
187
188
				$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);
189
190
191
				$new_pkgbuild[$k] = $v;
			}
		}
192

Callan Barrett's avatar
Callan Barrett committed
193
		# Now we've parsed the pkgbuild, let's move it to where it belongs
194
195
196
		if (!$error) {
			$pkg_name = str_replace("'", "", $pkgbuild['pkgname']);
			$pkg_name = escapeshellarg($pkg_name);
Callan Barrett's avatar
Callan Barrett committed
197
			$pkg_name = str_replace("'", "", $pkg_name);
198

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

Callan Barrett's avatar
Callan Barrett committed
201
			if (!$presult) {
202
203
204
205
				$error = __("Invalid name: only lowercase letters are allowed.");
			}
		}

206
207
		$incoming_pkgdir = INCOMING_DIR . $pkg_name;

208
		if (!$error) {
Callan Barrett's avatar
Callan Barrett committed
209
			# First, see if this package already exists, and if it can be overwritten
210
211
			$pkg_exists = package_exists($pkg_name);
			if (can_submit_pkg($pkg_name, $_COOKIE["AURSID"])) {
212
				if (file_exists($incoming_pkgdir)) {
Callan Barrett's avatar
Callan Barrett committed
213
					# Blow away the existing file/dir and contents
214
					rm_rf($incoming_pkgdir);
215
216
				}

217
218
				if (!@mkdir($incoming_pkgdir)) {
					$error = __( "Could not create directory %s.", $incoming_pkgdir);
219
220
				}

221
				rename($pkg_dir, $incoming_pkgdir . "/" . $pkg_name);
222
			} else {
223
				$error = __( "You are not allowed to overwrite the %h%s%h package.", "<b>", $pkg_name, "</b>");
224
225
226
			}
		}

227
228
		# Re-tar the package for consistency's sake
		if (!$error) {
229
230
			if (!@chdir($incoming_pkgdir)) {
				$error = __("Could not change directory to %s.", $incoming_pkgdir);
231
232
			}
		}
233

234
		if (!$error) {
235
236
237
			$tar = new Archive_Tar($pkg_name . '.tar.gz');
			$create = $tar->create(array($pkg_name));

238
			if (!$create) {
239
240
241
				$error = __("Could not re-tar");
			}
		}
242

243
		# Chmod files after everything has been done.
244
		if (!error && !chmod_group($incoming_pkgdir)) {
245
246
247
			$error = __("Could not chmod directory %s.", $incoming_pkgdir);
		}

Callan Barrett's avatar
Callan Barrett committed
248
		# Whether it failed or not we can clean this out
249
250
		if (file_exists($tempdir)) {
			rm_rf($tempdir);
251
252
		}

Callan Barrett's avatar
Callan Barrett committed
253
		# Update the backend database
254
		if (!$error) {
255

256
			$dbh = db_connect();
257

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

Callan Barrett's avatar
Callan Barrett committed
262
			$q = "SELECT * FROM Packages WHERE Name = '" . mysql_real_escape_string($new_pkgbuild['pkgname']) . "'";
263
264
265
266
267
			$result = db_query($q, $dbh);
			$pdata = mysql_fetch_assoc($result);

			if ($pdata) {

Callan Barrett's avatar
Callan Barrett committed
268
269
				# Flush out old data that will be replaced with new data
				$q = "DELETE FROM PackageDepends WHERE PackageID = " . $pdata["ID"];
270
				db_query($q, $dbh);
Callan Barrett's avatar
Callan Barrett committed
271
				$q = "DELETE FROM PackageSources WHERE PackageID = " . $pdata["ID"];
272
				db_query($q, $dbh);
273

Callan Barrett's avatar
Callan Barrett committed
274
275
				# If the package was a dummy, undummy it
				if ($pdata['DummyPkg']) {
276
277
278
279
					$q = sprintf( "UPDATE Packages SET DummyPkg = 0, SubmitterUID = %d, MaintainerUID = %d, SubmittedTS = UNIX_TIMESTAMP() WHERE ID = %d",
						uid_from_sid($_COOKIE["AURSID"]),
						uid_from_sid($_COOKIE["AURSID"]),
						$pdata["ID"]);
Callan Barrett's avatar
Callan Barrett committed
280

281
					db_query($q, $dbh);
282
				}
283

Callan Barrett's avatar
Callan Barrett committed
284
				# If a new category was chosen, change it to that
285
				if ($_POST['category'] > 1) {
286
287
288
289
290
291
292
					$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
293
				# Update package data
294
295
296
297
298
299
300
				$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']),
301
					mysql_real_escape_string($incoming_pkgdir . "/" . $pkg_name . ".tar.gz"),
302
303
304
					mysql_real_escape_string(URL_DIR . $pkg_name . "/" . $pkg_name . ".tar.gz"),
					$pdata["ID"]);

Callan Barrett's avatar
Callan Barrett committed
305
				db_query($q, $dbh);
306

Callan Barrett's avatar
Callan Barrett committed
307
				# Update package depends
308
				$depends = explode(" ", $new_pkgbuild['depends']);
309
				foreach ($depends as $dep) {
310
					$q = "INSERT INTO PackageDepends (PackageID, DepPkgID, DepCondition) VALUES (";
Callan Barrett's avatar
Callan Barrett committed
311
					$deppkgname = preg_replace("/[<>]?=.*/", "", $dep);
312
313
314
315
316
317
					$depcondition = str_replace($deppkgname, "", $dep);

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

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

321
					db_query($q, $dbh);
322
				}
Callan Barrett's avatar
Callan Barrett committed
323
324
325
326
327
328
329

				# Insert sources
				$sources = explode(" ", $new_pkgbuild['source']);
				foreach ($sources as $src) {
					$q = "INSERT INTO PackageSources (PackageID, Source) VALUES (";
					$q .= $pdata["ID"] . ", '" . mysql_real_escape_string($src) . "')";
					db_query($q, $dbh);
330
				}
331
332

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

334
			} else {
335

Callan Barrett's avatar
Callan Barrett committed
336
				# This is a brand new package
337
338
339
340
341
342
343
344
345
346
				$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']),
					uid_from_sid($_COOKIE["AURSID"]),
					uid_from_sid($_COOKIE["AURSID"]),
347
					mysql_real_escape_string($incoming_pkgdir . "/" . $pkg_name . ".tar.gz"),
348
					mysql_real_escape_string(URL_DIR . $pkg_name . "/" . $pkg_name . ".tar.gz"));
jchu's avatar
jchu committed
349

Callan Barrett's avatar
Callan Barrett committed
350
				$result = db_query($q, $dbh);
jchu's avatar
jchu committed
351
352
				$packageID = mysql_insert_id($dbh);

Callan Barrett's avatar
Callan Barrett committed
353
				# Update package depends
354
				$depends = explode(" ", $new_pkgbuild['depends']);
Callan Barrett's avatar
Callan Barrett committed
355
				foreach ($depends as $dep) {
Callan Barrett's avatar
Callan Barrett committed
356
					$q = "INSERT INTO PackageDepends (PackageID, DepPkgID, DepCondition) VALUES (";
Callan Barrett's avatar
Callan Barrett committed
357
358
					$deppkgname = preg_replace("/[<>]?=.*/", "", $dep);
					$depcondition = str_replace($deppkgname, "", $dep);
359
360
361
362
363
364
365
366

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

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

jchu's avatar
jchu committed
367
					db_query($q, $dbh);
368
				}
jchu's avatar
jchu committed
369

Callan Barrett's avatar
Callan Barrett committed
370
371
372
373
374
375
				# Insert sources
				$sources = explode(" ", $new_pkgbuild['source']);
				foreach ($sources as $src) {
					$q = "INSERT INTO PackageSources (PackageID, Source) VALUES (";
					$q .= $packageID . ", '" . mysql_real_escape_string($src) . "')";
					db_query($q, $dbh);
376
				}
377
378

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

380
381
			}
		}
382
383

		chdir($_SERVER['DOCUMENT_ROOT']);
eric's avatar
eric committed
384
385
	}

386
387
388
# Logic over, let's do some output

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

390
391
392
?>

<div class="pgbox">
393
394
395
396
	<div class="pgboxtitle">
		<span class="f3"><?php print __("Submit"); ?></span>
	</div>
	<div class="pgboxbody">
eric's avatar
eric committed
397

398
<?php
Callan Barrett's avatar
Callan Barrett committed
399
	if (!$_REQUEST["pkgsubmit"] || $error):
400
		# User is not uploading, or there were errors uploading - then
eric's avatar
eric committed
401
		# give the visitor the default upload form
Callan Barrett's avatar
Callan Barrett committed
402
403
404
405
406
407
408
409
410
411
		if (ini_get("file_uploads")):
			if ($error):
?>

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

<?php
			endif;

412
413
			$pkg_categories = pkgCategories();
			$pkg_locations = pkgLocations();
414
?>
415

416
417
418
419
420
421
422
423
424
<form action='/pkgsubmit.php' method='post' enctype='multipart/form-data'>
	<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
425
426
427
					foreach ($pkg_categories as $num => $cat):
						print "<option value='" . $num . "'";
						if (isset($_POST['category']) && $_POST['category'] == $cat):
428
							print " selected='selected'";
Callan Barrett's avatar
Callan Barrett committed
429
430
431
						endif;
						print ">" . $cat . "</option>";
					endforeach;
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
				?>
			</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
451
		else:
eric's avatar
eric committed
452
			print __("Sorry, uploads are not permitted by this server.");
Callan Barrett's avatar
Callan Barrett committed
453
454
455
456
457
458
459
460
461
?>

<br />

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

Callan Barrett's avatar
Callan Barrett committed
465
<br />
466

Callan Barrett's avatar
Callan Barrett committed
467
468
<?php
endif;
469
470
?>

471
	</div>
472
473
474
</div>

<?php
tardo's avatar
tardo committed
475
html_footer(AUR_VERSION);
476