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

15
16
$cwd = getcwd();

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

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

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

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

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

eric's avatar
eric committed
31
		if (!$error) {
32
			$tar = new Archive_Tar($_FILES['pfile']['tmp_name']);
33

34
35
36
37
38
39
			# Extract PKGBUILD into a string
			$pkgbuild_raw = '';
			foreach ($tar->listContent() as $tar_file) {
				if (preg_match('/^[^\/]+\/PKGBUILD$/', $tar_file['filename'])) {
					$pkgbuild_raw = $tar->extractInString($tar_file['filename']);
					break;
40
41
				}
			}
42

43
			if (empty($pkgbuild_raw)) {
44
45
				$error = __("Error trying to unpack upload - PKGBUILD does not exist.");
			}
46
		}
jchu's avatar
jchu committed
47

48
		# if no error, get list of directory contents and process PKGBUILD
Callan Barrett's avatar
Callan Barrett committed
49
50
		# TODO: This needs to be completely rewritten to support stuff like arrays
		# and variable substitution among other things.
51
		if (!$error) {
52
			# process PKGBUILD - remove line concatenation
eric's avatar
eric committed
53
			#
54
			$pkgbuild = array();
55
56
57
58
			$line_no = 0;
			$lines = array();
			$continuation_line = 0;
			$current_line = "";
59
			$paren_depth = 0;
60
61
			foreach (split("\n", $pkgbuild_raw) as $line) {
				$line = trim($line);
62
63
64
				# Remove comments
				$line = preg_replace('/\s*#.*/', '', $line);

65
				$char_counts = count_chars($line, 0);
66
				$paren_depth += $char_counts[ord('(')] - $char_counts[ord(')')];
67
68
69
70
71
				if (substr($line, strlen($line)-1) == "\\") {
					# continue appending onto existing line_no
					#
					$current_line .= substr($line, 0, strlen($line)-1);
					$continuation_line = 1;
72
				} elseif ($paren_depth > 0) {
73
					# assumed continuation
74
75
76
77
					# continue appending onto existing line_no
					#
					$current_line .= $line . " ";
					$continuation_line = 1;
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
				} 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++;
				}
			}

97
			# Now process the lines and put any var=val lines into the
98
			# 'pkgbuild' array.
99
			while (list($k, $line) = each($lines)) {
100
101
102
				# Neutralize parameter substitution
				$line = preg_replace('/\${(\w+)#(\w*)}?/', '$1$2', $line);

103
104
105
106
107
108
109
				$lparts = Array();
				# Match variable assignment only.
				if (preg_match('/^\s*[_\w]+=[^=].*/', $line, $matches)) {
					$lparts = explode("=", $matches[0], 2);
				}

				if (!empty($lparts)) {
110
					# this is a variable/value pair, strip out
111
					# array parens and any quoting, except in pkgdesc
112
					# for pkgdesc, only remove start/end pairs of " or '
113
					if ($lparts[0]=="pkgdesc") {
Loui Chang's avatar
Loui Chang committed
114
						if ($lparts[1]{0} == '"' &&
115
116
117
								$lparts[1]{strlen($lparts[1])-1} == '"') {
							$pkgbuild[$lparts[0]] = substr($lparts[1], 1, -1);
						}
Loui Chang's avatar
Loui Chang committed
118
119
						elseif
							($lparts[1]{0} == "'" &&
120
121
							 $lparts[1]{strlen($lparts[1])-1} == "'") {
							$pkgbuild[$lparts[0]] = substr($lparts[1], 1, -1);
Loui Chang's avatar
Loui Chang committed
122
						} else {
123
							$pkgbuild[$lparts[0]] = $lparts[1];
124
						}
125
126
127
128
					} else {
						$pkgbuild[$lparts[0]] = str_replace(array("(",")","\"","'"), "",
								$lparts[1]);
					}
129
				}
130
			}
eric's avatar
eric committed
131

132
			# some error checking on PKGBUILD contents - just make sure each
Loui Chang's avatar
Loui Chang committed
133
			# variable has a value. This does not do any validity checking
134
			# on the values, or attempts to fix line continuation/wrapping.
135
			$req_vars = array("url", "pkgdesc", "license", "pkgrel", "pkgver", "arch", "pkgname");
Callan Barrett's avatar
Callan Barrett committed
136
			foreach ($req_vars as $var) {
137
				if (!array_key_exists($var, $pkgbuild)) {
138
139
					$error = __('Missing %s variable in PKGBUILD.', $var);
					break;
140
141
				}
			}
142
143
144
		}

		# TODO This is where other additional error checking can be
145
		# performed. Examples: #md5sums == #sources?, md5sums of any
146
147
		# included files match?, install scriptlet file exists?
		#
148

149
		# Check for http:// or other protocol in url
Loui Chang's avatar
Loui Chang committed
150
		#
151
		if (!$error) {
152
153
154
155
			$parsed_url = parse_url($pkgbuild['url']);
			if (!$parsed_url['scheme']) {
				$error = __("Package URL is missing a protocol (ie. http:// ,ftp://)");
			}
156
		}
157

158
		# Now, run through the pkgbuild array, and do "eval" and simple substituions.
159
160
		if (!$error) {
			while (list($k, $v) = each($pkgbuild)) {
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
				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);
						}
					}
				}
178
179
180

				# Simple variable replacement
				$pattern_var = '/\$({?)([_\w]+)(}?)/';
181
182
183
184
185
186
187
188
189
190
191
192
193
194
				$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])) {
						$replacement = explode(" ", $new_pkgbuild[$var], 2);
					}
					else {
						$replacement = '';
					}

					$v = substr_replace($v, $replacement, $pos, $len);
195
					$offset = $pos + strlen($replacement);
196
				}
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
		if (!$error) {
203
			$pkg_name = str_replace("'", "", $new_pkgbuild['pkgname']);
204
			$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_tree($incoming_pkgdir);
225
226
				}

227
228
				if (!@mkdir($incoming_pkgdir)) {
					$error = __( "Could not create directory %s.", $incoming_pkgdir);
229
230
				}
			} else {
231
				$error = __( "You are not allowed to overwrite the %h%s%h package.", "<b>", $pkg_name, "</b>");
232
233
234
			}
		}

235
		if (!$error) {
236
237
			if (!@chdir($incoming_pkgdir)) {
				$error = __("Could not change directory to %s.", $incoming_pkgdir);
238
			}
239

240
241
			file_put_contents('PKGBUILD', $pkgbuild_raw);
			rename($_FILES['pfile']['tmp_name'], $pkg_name . '.tar.gz');
242
243
		}

Callan Barrett's avatar
Callan Barrett committed
244
		# Update the backend database
245
		if (!$error) {
246

247
			$dbh = db_connect();
248

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

Callan Barrett's avatar
Callan Barrett committed
253
			$q = "SELECT * FROM Packages WHERE Name = '" . mysql_real_escape_string($new_pkgbuild['pkgname']) . "'";
254
255
256
257
258
			$result = db_query($q, $dbh);
			$pdata = mysql_fetch_assoc($result);

			if ($pdata) {

Callan Barrett's avatar
Callan Barrett committed
259
260
				# Flush out old data that will be replaced with new data
				$q = "DELETE FROM PackageDepends WHERE PackageID = " . $pdata["ID"];
261
				db_query($q, $dbh);
Callan Barrett's avatar
Callan Barrett committed
262
				$q = "DELETE FROM PackageSources WHERE PackageID = " . $pdata["ID"];
263
				db_query($q, $dbh);
264

Callan Barrett's avatar
Callan Barrett committed
265
266
				# If the package was a dummy, undummy it
				if ($pdata['DummyPkg']) {
267
					$q = sprintf( "UPDATE Packages SET DummyPkg = 0, SubmitterUID = %d, MaintainerUID = %d, SubmittedTS = UNIX_TIMESTAMP() WHERE ID = %d",
Dan McGee's avatar
Dan McGee committed
268
269
						$uid,
						$uid,
270
						$pdata["ID"]);
Callan Barrett's avatar
Callan Barrett committed
271

272
					db_query($q, $dbh);
273
				}
274

Callan Barrett's avatar
Callan Barrett committed
275
				# If a new category was chosen, change it to that
276
				if ($_POST['category'] > 1) {
277
278
279
280
281
282
283
					$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
284
				# Update package data
285
				$q = sprintf("UPDATE Packages SET ModifiedTS = UNIX_TIMESTAMP(), Name = '%s', Version = '%s-%s', License = '%s', Description = '%s', URL = '%s', OutOfDateTS = NULL WHERE ID = %d",
286
287
288
289
290
291
292
293
					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']),
					$pdata["ID"]);

Callan Barrett's avatar
Callan Barrett committed
294
				db_query($q, $dbh);
295

Callan Barrett's avatar
Callan Barrett committed
296
				# Update package depends
297
				$depends = explode(" ", $new_pkgbuild['depends']);
298
				foreach ($depends as $dep) {
299
					$q = "INSERT INTO PackageDepends (PackageID, DepPkgID, DepCondition) VALUES (";
Callan Barrett's avatar
Callan Barrett committed
300
					$deppkgname = preg_replace("/[<>]?=.*/", "", $dep);
301
302
303
304
305
306
					$depcondition = str_replace($deppkgname, "", $dep);

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

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

310
					db_query($q, $dbh);
311
				}
Callan Barrett's avatar
Callan Barrett committed
312
313
314
315

				# Insert sources
				$sources = explode(" ", $new_pkgbuild['source']);
				foreach ($sources as $src) {
316
317
318
319
320
					if ($src != "" ) {
						$q = "INSERT INTO PackageSources (PackageID, Source) VALUES (";
						$q .= $pdata["ID"] . ", '" . mysql_real_escape_string($src) . "')";
						db_query($q, $dbh);
					}
321
				}
322
323

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

325
			} else {
326
				$uid = uid_from_sid($_COOKIE["AURSID"]);
327

Callan Barrett's avatar
Callan Barrett committed
328
				# This is a brand new package
329
				$q = sprintf("INSERT INTO Packages (Name, License, Version, CategoryID, Description, URL, SubmittedTS, SubmitterUID, MaintainerUID) VALUES ('%s', '%s', '%s-%s', %d, '%s', '%s', UNIX_TIMESTAMP(), %d, %d)",
330
331
332
333
334
335
336
					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
337
					$uid,
338
					$uid);
jchu's avatar
jchu committed
339

Callan Barrett's avatar
Callan Barrett committed
340
				$result = db_query($q, $dbh);
jchu's avatar
jchu committed
341
342
				$packageID = mysql_insert_id($dbh);

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

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

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

jchu's avatar
jchu committed
357
					db_query($q, $dbh);
358
				}
jchu's avatar
jchu committed
359

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

370
				pkg_notify(account_from_sid($_COOKIE["AURSID"]), array($packageID));
371
372

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

374
375
			}
		}
376

377
		chdir($cwd);
eric's avatar
eric committed
378
379
	}

380
381
382
# Logic over, let's do some output

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

384
385
?>

Loui Chang's avatar
Loui Chang committed
386
387
388
389
<?php if ($error): ?>
	<p class="pkgoutput"><?php print $error ?></p>
<?php endif; ?>

390
<div class="pgbox">
391
392
393
394
	<div class="pgboxtitle">
		<span class="f3"><?php print __("Submit"); ?></span>
	</div>
	<div class="pgboxbody">
395
	<p><?php echo __("Upload your source packages here. Create source packages with `makepkg --source`.") ?></p>
eric's avatar
eric committed
396

397
<?php
398
	if (empty($_REQUEST['pkgsubmit']) || $error):
399
		# User is not uploading, or there were errors uploading - then
eric's avatar
eric committed
400
		# give the visitor the default upload form
Callan Barrett's avatar
Callan Barrett committed
401
402
		if (ini_get("file_uploads")):

403
			$pkg_categories = pkgCategories();
404
?>
405

406
<form action='pkgsubmit.php' method='post' enctype='multipart/form-data'>
407
	<div> <input type='hidden' name='pkgsubmit' value='1' /> </div>
408
409
	<table border='0' cellspacing='5'>
		<tr>
410
411
			<td class='f4' align='right'><?php print __("Package Category"); ?>:</td>
			<td class='f4' align='left'>
412
413
414
			<select name='category'>
				<option value='1'><?php print __("Select Category"); ?></option>
				<?php
Callan Barrett's avatar
Callan Barrett committed
415
416
417
					foreach ($pkg_categories as $num => $cat):
						print "<option value='" . $num . "'";
						if (isset($_POST['category']) && $_POST['category'] == $cat):
418
							print " selected='selected'";
Callan Barrett's avatar
Callan Barrett committed
419
420
421
						endif;
						print ">" . $cat . "</option>";
					endforeach;
422
423
424
425
426
				?>
			</select>
			</td>
		</tr>
		<tr>
427
428
			<td class='f4' align='right'><?php print __("Upload package file"); ?>:</td>
			<td class='f4' align='left'>
429
430
431
432
433
434
435
436
437
438
439
440
				<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
441
		else:
eric's avatar
eric committed
442
			print __("Sorry, uploads are not permitted by this server.");
Callan Barrett's avatar
Callan Barrett committed
443
444
445
446
447
448
449
450
451
?>

<br />

<?php
		endif;
	endif;
else:
	# Visitor is not logged in
eric's avatar
eric committed
452
	print __("You must create an account before you can upload packages.");
Loui Chang's avatar
Loui Chang committed
453
	exit();
Callan Barrett's avatar
Callan Barrett committed
454
?>
455

Callan Barrett's avatar
Callan Barrett committed
456
<br />
457

Callan Barrett's avatar
Callan Barrett committed
458
459
<?php
endif;
460
461
?>

462
	</div>
463
464
465
</div>

<?php
tardo's avatar
tardo committed
466
html_footer(AUR_VERSION);
467