pkgfuncs.inc.php 39.4 KB
Newer Older
1
<?php
2
include_once("config.inc.php");
eric's avatar
eric committed
3

4
5
6
7
8
9
10
11
12
13
14
15
/**
 * Determine if the user can delete a specific package comment
 *
 * Only the comment submitter, Trusted Users, and Developers can delete
 * comments. This function is used for the backend side of comment deletion.
 *
 * @param string $comment_id The comment ID in the database
 * @param string $atype The account type of the user trying to delete a comment
 * @param string|int $uid The user ID of the individual trying to delete a comment
 *
 * @return bool True if the user can delete the comment, otherwise false
 */
16
function canDeleteComment($comment_id=0, $atype="", $uid=0) {
17
18
19
20
	if (!$uid) {
		/* Unauthenticated users cannot delete anything. */
		return false;
	}
21
22
	if ($atype == "Trusted User" || $atype == "Developer") {
		# A TU/Dev can delete any comment
23
		return true;
24
	}
25
	$dbh = DB::connect();
26
27
28
29
	$q = "SELECT COUNT(ID) AS CNT ";
	$q.= "FROM PackageComments ";
	$q.= "WHERE ID = " . intval($comment_id);
	$q.= " AND UsersID = " . $uid;
canyonknight's avatar
canyonknight committed
30
	$result = $dbh->query($q);
31
	if ($result != NULL) {
canyonknight's avatar
canyonknight committed
32
		$row = $result->fetch(PDO::FETCH_ASSOC);
33
		if ($row['CNT'] > 0) {
34
			return true;
35
36
		}
	}
37
	return false;
38
}
eric's avatar
eric committed
39

40
41
42
43
44
45
46
47
48
49
50
51
/**
 * Determine if the user can delete a specific package comment using an array
 *
 * Only the comment submitter, Trusted Users, and Developers can delete
 * comments. This function is used for the frontend side of comment deletion.
 *
 * @param array $comment All database information relating a specific comment
 * @param string $atype The account type of the user trying to delete a comment
 * @param string|int $uid The user ID of the individual trying to delete a comment
 *
 * @return bool True if the user can delete the comment, otherwise false
 */
52
function canDeleteCommentArray($comment, $atype="", $uid=0) {
53
54
55
56
	if (!$uid) {
		/* Unauthenticated users cannot delete anything. */
		return false;
	} elseif ($atype == "Trusted User" || $atype == "Developer") {
57
		# A TU/Dev can delete any comment
58
		return true;
59
60
	} else if ($comment['UsersID'] == $uid) {
		# User's own comment
61
		return true;
62
	}
63
	return false;
64
65
}

66
67
68
69
70
71
72
73
74
75
/**
 * Determine if the visitor can submit blacklisted packages.
 *
 * Only Trusted Users and Developers can delete blacklisted packages. Packages
 * are blacklisted if they are include in the official repositories.
 *
 * @param string $atype The account type of the user
 *
 * @return bool True if the user can submit blacklisted packages, otherwise false
 */
76
77
78
function canSubmitBlacklisted($atype = "") {
	if ($atype == "Trusted User" || $atype == "Developer") {
		# Only TUs/Devs can submit blacklisted packages.
79
		return true;
80
81
	}
	else {
82
		return false;
83
84
85
	}
}

86
87
88
89
90
91
92
/**
 * Get all package categories stored in the database
 *
 * @param \PDO An already established database connection
 *
 * @return array All package categories
 */
93
function pkgCategories() {
eric's avatar
eric committed
94
	$cats = array();
95
	$dbh = DB::connect();
96
	$q = "SELECT * FROM PackageCategories WHERE ID != 1 ";
eric's avatar
eric committed
97
	$q.= "ORDER BY Category ASC";
canyonknight's avatar
canyonknight committed
98
	$result = $dbh->query($q);
eric's avatar
eric committed
99
	if ($result) {
canyonknight's avatar
canyonknight committed
100
		while ($row = $result->fetch(PDO::FETCH_NUM)) {
eric's avatar
eric committed
101
102
103
104
105
106
			$cats[$row[0]] = $row[1];
		}
	}
	return $cats;
}

107
108
109
110
111
112
113
/**
 * Check to see if the package name already exists in the database
 *
 * @param string $name The package name to check
 *
 * @return string|void Package name if it already exists
 */
114
function pkgid_from_name($name="") {
115
	if (!$name) {return NULL;}
116
	$dbh = DB::connect();
117
	$q = "SELECT ID FROM Packages ";
canyonknight's avatar
canyonknight committed
118
119
120
121
122
123
	$q.= "WHERE Name = " . $dbh->quote($name);
	$result = $dbh->query($q);
	if (!$result) {
		return;
	}
	$row = $result->fetch(PDO::FETCH_NUM);
124
125
126
	return $row[0];
}

127
128
129
130
131
132
133
/**
 * Get package dependencies for a specific package
 *
 * @param int $pkgid The package to get dependencies for
 *
 * @return array All package dependencies for the package
 */
134
function package_dependencies($pkgid) {
135
	$deps = array();
136
137
	$pkgid = intval($pkgid);
	if ($pkgid > 0) {
138
		$dbh = DB::connect();
Dan McGee's avatar
Dan McGee committed
139
140
141
142
		$q = "SELECT pd.DepName, pd.DepCondition, p.ID FROM PackageDepends pd ";
		$q.= "LEFT JOIN Packages p ON pd.DepName = p.Name ";
		$q.= "WHERE pd.PackageID = ". $pkgid . " ";
		$q.= "ORDER BY pd.DepName";
canyonknight's avatar
canyonknight committed
143
144
145
146
147
		$result = $dbh->query($q);
		if (!$result) {
			return array();
		}
		while ($row = $result->fetch(PDO::FETCH_NUM)) {
148
149
150
151
152
153
			$deps[] = $row;
		}
	}
	return $deps;
}

154
155
156
157
158
159
160
/**
 * Determine packages that depend on a package
 *
 * @param string $name The package name for the dependency search
 *
 * @return array All packages that depend on the specified package name
 */
161
function package_required($name="") {
162
	$deps = array();
Dan McGee's avatar
Dan McGee committed
163
	if ($name != "") {
164
		$dbh = DB::connect();
165
		$q = "SELECT DISTINCT p.Name, PackageID FROM PackageDepends pd ";
Dan McGee's avatar
Dan McGee committed
166
		$q.= "JOIN Packages p ON pd.PackageID = p.ID ";
canyonknight's avatar
canyonknight committed
167
		$q.= "WHERE DepName = " . $dbh->quote($name) . " ";
Dan McGee's avatar
Dan McGee committed
168
		$q.= "ORDER BY p.Name";
canyonknight's avatar
canyonknight committed
169
		$result = $dbh->query($q);
170
		if (!$result) {return array();}
canyonknight's avatar
canyonknight committed
171
		while ($row = $result->fetch(PDO::FETCH_NUM)) {
172
173
174
175
176
177
			$deps[] = $row;
		}
	}
	return $deps;
}

178
179
180
181
182
183
184
/**
 * Get the number of non-deleted comments for a specific package
 *
 * @param string $pkgid The package ID to get comment count for
 *
 * @return string The number of comments left for a specific package
 */
185
function package_comments_count($pkgid) {
186
	$dbh = DB::connect();
canyonknight's avatar
canyonknight committed
187

188
189
	$pkgid = intval($pkgid);
	if ($pkgid > 0) {
190
		$dbh = DB::connect();
191
		$q = "SELECT COUNT(*) FROM PackageComments ";
192
		$q.= "WHERE PackageID = " . $pkgid;
193
		$q.= " AND DelUsersID IS NULL";
194
	}
canyonknight's avatar
canyonknight committed
195
	$result = $dbh->query($q);
196
197
198
199
200

	if (!$result) {
		return;
	}

canyonknight's avatar
canyonknight committed
201
202
	$row = $result->fetch(PDO::FETCH_NUM);
	return $row[0];
203
204
}

205
206
207
208
209
210
211
/**
 * Get all package comment information for a specific package
 *
 * @param int $pkgid The package ID to get comments for
 *
 * @return array All package comment information for a specific package
 */
212
function package_comments($pkgid) {
eric's avatar
eric committed
213
	$comments = array();
214
215
	$pkgid = intval($pkgid);
	if ($pkgid > 0) {
216
		$dbh = DB::connect();
217
		$q = "SELECT PackageComments.ID, UserName, UsersID, Comments, CommentTS ";
218
219
220
221
222
		$q.= "FROM PackageComments LEFT JOIN Users ";
		$q.= "ON PackageComments.UsersID = Users.ID ";
		$q.= "WHERE PackageID = " . $pkgid . " ";
		$q.= "AND DelUsersID IS NULL "; # only display non-deleted comments
		$q.= "ORDER BY CommentTS DESC";
223
224
225
226
227

		if (!isset($_GET['comments'])) {
			$q.= " LIMIT 10";
		}

canyonknight's avatar
canyonknight committed
228
		$result = $dbh->query($q);
Loui Chang's avatar
Loui Chang committed
229
230
231
232
233

		if (!$result) {
			return;
		}

canyonknight's avatar
canyonknight committed
234
		while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
eric's avatar
eric committed
235
236
237
238
239
240
			$comments[] = $row;
		}
	}
	return $comments;
}

241
242
243
244
245
246
247
248
249
250
/**
 * Add a comment to a package page and send out appropriate notifications
 *
 * @global string $AUR_LOCATION The AUR's URL used for notification e-mails
 * @param string $pkgid The package ID to add the comment on
 * @param string $uid The user ID of the individual who left the comment
 * @param string $comment The comment left on a package page
 *
 * @return void
 */
251
function add_package_comment($pkgid, $uid, $comment) {
252
253
	global $AUR_LOCATION;

254
	$dbh = DB::connect();
255

canyonknight's avatar
canyonknight committed
256
257
258
259
260
	$q = "INSERT INTO PackageComments ";
	$q.= "(PackageID, UsersID, Comments, CommentTS) VALUES (";
	$q.= intval($pkgid) . ", " . $uid . ", ";
	$q.= $dbh->quote($comment) . ", UNIX_TIMESTAMP())";
	$dbh->exec($q);
261

262
        # TODO: Move notification logic to separate function where it belongs
263
	# Send email notifications
canyonknight's avatar
canyonknight committed
264
265
266
267
268
269
	$q = "SELECT CommentNotify.*, Users.Email ";
	$q.= "FROM CommentNotify, Users ";
	$q.= "WHERE Users.ID = CommentNotify.UserID ";
	$q.= "AND CommentNotify.UserID != " . $uid . " ";
	$q.= "AND CommentNotify.PkgID = " . intval($pkgid);
	$result = $dbh->query($q);
270
271
	$bcc = array();

canyonknight's avatar
canyonknight committed
272
273
	if ($result) {
		while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
274
275
276
			array_push($bcc, $row['Email']);
		}

canyonknight's avatar
canyonknight committed
277
278
279
280
281
		$q = "SELECT Packages.* ";
		$q.= "FROM Packages ";
		$q.= "WHERE Packages.ID = " . intval($pkgid);
		$result = $dbh->query($q);
		$row = $result->fetch(PDO::FETCH_ASSOC);
282
283
284
285
286

		# TODO: native language emails for users, based on their prefs
		# Simply making these strings translatable won't work, users would be
		# getting emails in the language that the user who posted the comment was in
		$body =
287
		'from ' . $AUR_LOCATION . get_pkg_uri($row['Name']) . "\n"
288
		. username_from_sid($_COOKIE['AURSID']) . " wrote:\n\n"
289
290
291
292
293
294
295
296
297
		. $comment
		. "\n\n---\nIf you no longer wish to receive notifications about this package, please go the the above package page and click the UnNotify button.";
		$body = wordwrap($body, 70);
		$bcc = implode(', ', $bcc);
		$headers = "Bcc: $bcc\nReply-to: nobody@archlinux.org\nFrom: aur-notify@archlinux.org\nX-Mailer: AUR\n";
		@mail('undisclosed-recipients: ;', "AUR Comment for " . $row['Name'], $body, $headers);
	}
}

298
299
300
301
302
303
304
/**
 * Get all package sources for a specific package
 *
 * @param string $pkgid The package ID to get the sources for
 *
 * @return array All sources associated with a specific package
 */
305
function package_sources($pkgid) {
306
	$sources = array();
307
308
	$pkgid = intval($pkgid);
	if ($pkgid > 0) {
309
		$dbh = DB::connect();
310
		$q = "SELECT Source FROM PackageSources ";
311
		$q.= "WHERE PackageID = " . $pkgid;
312
		$q.= " ORDER BY Source";
canyonknight's avatar
canyonknight committed
313
314
315
316
317
		$result = $dbh->query($q);
		if (!$result) {
			return array();
		}
		while ($row = $result->fetch(PDO::FETCH_NUM)) {
318
319
320
321
322
323
			$sources[] = $row[0];
		}
	}
	return $sources;
}

324
325
326
327
328
329
330
/**
 * Get a list of all packages a logged-in user has voted for
 *
 * @param string $sid The session ID of the visitor
 *
 * @return array All packages the visitor has voted for
 */
331
function pkgvotes_from_sid($sid="") {
332
333
	$pkgs = array();
	if (!$sid) {return $pkgs;}
334
	$dbh = DB::connect();
335
336
337
338
	$q = "SELECT PackageID ";
	$q.= "FROM PackageVotes, Users, Sessions ";
	$q.= "WHERE Users.ID = Sessions.UsersID ";
	$q.= "AND Users.ID = PackageVotes.UsersID ";
canyonknight's avatar
canyonknight committed
339
340
	$q.= "AND Sessions.SessionID = " . $dbh->quote($sid);
	$result = $dbh->query($q);
341
	if ($result) {
canyonknight's avatar
canyonknight committed
342
		while ($row = $result->fetch(PDO::FETCH_NUM)) {
343
344
345
346
347
348
			$pkgs[$row[0]] = 1;
		}
	}
	return $pkgs;
}

349
350
351
352
353
354
355
/**
 * Determine package names from package IDs
 *
 * @param string|array $pkgids The package IDs to get names for
 *
 * @return array|string All names if multiple package IDs, otherwise package name
 */
356
function pkgname_from_id($pkgids) {
357
358
359
	if (is_array($pkgids)) {
		$pkgids = sanitize_ids($pkgids);
		$names = array();
360
		$dbh = DB::connect();
canyonknight's avatar
canyonknight committed
361
362
363
364
365
		$q = "SELECT Name FROM Packages WHERE ID IN (";
		$q.= implode(",", $pkgids) . ")";
		$result = $dbh->query($q);
		if ($result) {
			while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
366
367
368
369
370
371
				$names[] = $row['Name'];
			}
		}
		return $names;
	}
	elseif ($pkgids > 0) {
372
		$dbh = DB::connect();
373
		$q = "SELECT Name FROM Packages WHERE ID = " . $pkgids;
canyonknight's avatar
canyonknight committed
374
375
376
		$result = $dbh->query($q);
		if ($result) {
			$name = $result->fetch(PDO::FETCH_NUM);
377
		}
canyonknight's avatar
canyonknight committed
378
		return $name[0];
379
380
381
	}
	else {
		return NULL;
382
383
384
	}
}

385
386
387
388
389
390
391
/**
 * Determine if a package name is on the database blacklist
 *
 * @param string $name The package name to check
 *
 * @return bool True if the name is blacklisted, otherwise false
 */
392
function pkgname_is_blacklisted($name) {
393
	$dbh = DB::connect();
canyonknight's avatar
canyonknight committed
394
395
396
	$q = "SELECT COUNT(*) FROM PackageBlacklist ";
	$q.= "WHERE Name = " . $dbh->quote($name);
	$result = $dbh->query($q);
397
398

	if (!$result) return false;
399
	return ($result->fetchColumn() > 0);
400
401
}

402
/**
403
 * Get the package details
404
 *
405
 * @param string $id The package ID to get description for
406
 *
407
408
 * @return array The package's details OR error message
 **/
409
function get_package_details($id=0) {
410
	$dbh = DB::connect();
411

412
413
414
	$q = "SELECT Packages.*,Category ";
	$q.= "FROM Packages,PackageCategories ";
	$q.= "WHERE Packages.CategoryID = PackageCategories.ID ";
Loui Chang's avatar
Loui Chang committed
415
	$q.= "AND Packages.ID = " . intval($id);
canyonknight's avatar
canyonknight committed
416
	$result = $dbh->query($q);
eric's avatar
eric committed
417

418
419
	$row = array();

canyonknight's avatar
canyonknight committed
420
	if (!$result) {
421
		$row['error'] = __("Error retrieving package details.");
422
423
	}
	else {
canyonknight's avatar
canyonknight committed
424
		$row = $result->fetch(PDO::FETCH_ASSOC);
eric's avatar
eric committed
425
		if (empty($row)) {
426
			$row['error'] = __("Package details could not be found.");
427
		}
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
	}

	return $row;
}

/**
 * Display the package details page
 *
 * @global string $AUR_LOCATION The AUR's URL used for notification e-mails
 * @global bool $USE_VIRTUAL_URLS True if using URL rewriting, otherwise false
 * @param string $id The package ID to get details page for
 * @param array $row Package details retrieved by get_package_details
 * @param string $SID The session ID of the visitor
 *
 * @return void
 */
444
function display_package_details($id=0, $row, $SID="") {
445
446
447
	global $AUR_LOCATION;
	global $USE_VIRTUAL_URLS;

448
	$dbh = DB::connect();
449

450
451
452
453
454
455
456
457
458
459
460
461
462
	if (isset($row['error'])) {
		print "<p>" . $row['error'] . "</p>\n";
	}
	else {
		include('pkg_details.php');

		# Actions Bar
		if ($SID) {
			include('actions_form.php');
			include('pkg_comment_form.php');
		}

		# Print Comments
463
		$comments = package_comments($id);
464
465
		if (!empty($comments)) {
			include('pkg_comments.php');
eric's avatar
eric committed
466
467
468
469
470
		}
	}
}


Simo Leone's avatar
Simo Leone committed
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
/* pkg_search_page(SID)
 * outputs the body of search/search results page
 *
 * parameters:
 *  SID - current Session ID
 * preconditions:
 *  package search page has been accessed
 *  request variables have not been sanitized
 *
 *  request vars:
 *    O  - starting result number
 *    PP - number of search hits per page
 *    C  - package category ID number
 *    K  - package search string
 *    SO - search hit sort order:
 *          values: a - ascending
 *                  d - descending
 *    SB - sort search hits by:
489
 *          values: c - package category
Simo Leone's avatar
Simo Leone committed
490
491
492
493
 *                  n - package name
 *                  v - number of votes
 *                  m - maintainer username
 *    SeB- property that search string (K) represents
Andrea Scarpino's avatar
Andrea Scarpino committed
494
495
 *          values: n  - package name
 *                  nd - package name & description
496
 *                  x  - package name (exact match)
Simo Leone's avatar
Simo Leone committed
497
498
499
500
501
502
503
504
505
506
507
508
509
510
 *                  m  - package maintainer's username
 *                  s  - package submitter's username
 *    do_Orphans    - boolean. whether to search packages
 *                     without a maintainer
 *
 *
 *    These two are actually handled in packages.php.
 *
 *    IDs- integer array of ticked packages' IDs
 *    action - action to be taken on ticked packages
 *             values: do_Flag   - Flag out-of-date
 *                     do_UnFlag - Remove out-of-date flag
 *                     do_Adopt  - Adopt
 *                     do_Disown - Disown
511
 *                     do_Delete - Delete (requires confirm_Delete to be set)
512
513
 *                     do_Notify - Enable notification
 *                     do_UnNotify - Disable notification
Simo Leone's avatar
Simo Leone committed
514
 */
515
function pkg_search_page($SID="") {
516
	$dbh = DB::connect();
Loui Chang's avatar
Loui Chang committed
517
518
519
520
521

	// get commonly used variables...
	// TODO: REDUCE DB HITS.
	// grab info for user if they're logged in
	if ($SID)
522
		$myuid = uid_from_sid($SID);
Loui Chang's avatar
Loui Chang committed
523
	// get a list of package categories
524
	$cats = pkgCategories($dbh); //meow
Loui Chang's avatar
Loui Chang committed
525
526
527
528
529
530
531
532
533
534
535
536
537
538

	// sanitize paging variables
	//
	if (isset($_GET['O'])) {
		$_GET['O'] = intval($_GET['O']);
		if ($_GET['O'] < 0)
			$_GET['O'] = 0;
	}
	else {
		$_GET['O'] = 0;
	}

	if (isset($_GET["PP"])) {
		$_GET["PP"] = intval($_GET["PP"]);
539
540
541
542
		if ($_GET["PP"] < 50)
			$_GET["PP"] = 50;
		else if ($_GET["PP"] > 250)
			$_GET["PP"] = 250;
Loui Chang's avatar
Loui Chang committed
543
544
	}
	else {
545
		$_GET["PP"] = 50;
Loui Chang's avatar
Loui Chang committed
546
547
548
549
550
551
552
553
	}

	// FIXME: pull out DB-related code. all of it.
	//        this one's worth a choco-chip cookie,
	//        one of those nice big soft ones

	// build the package search query
	//
554
	$q_select = "SELECT ";
Loui Chang's avatar
Loui Chang committed
555
	if ($SID) {
556
		$q_select .= "CommentNotify.UserID AS Notify,
557
			   PackageVotes.UsersID AS Voted, ";
Loui Chang's avatar
Loui Chang committed
558
	}
559
	$q_select .= "Users.Username AS Maintainer,
Loui Chang's avatar
Loui Chang committed
560
561
	PackageCategories.Category,
	Packages.Name, Packages.Version, Packages.Description, Packages.NumVotes,
562
	Packages.ID, Packages.OutOfDateTS ";
Loui Chang's avatar
Loui Chang committed
563

564
	$q_from = "FROM Packages
565
566
567
	LEFT JOIN Users ON (Packages.MaintainerUID = Users.ID)
	LEFT JOIN PackageCategories
	ON (Packages.CategoryID = PackageCategories.ID) ";
Loui Chang's avatar
Loui Chang committed
568
	if ($SID) {
569
570
		# this portion is not needed for the total row count query
		$q_from_extra = "LEFT JOIN PackageVotes
Loui Chang's avatar
Loui Chang committed
571
572
573
		ON (Packages.ID = PackageVotes.PackageID AND PackageVotes.UsersID = $myuid)
		LEFT JOIN CommentNotify
		ON (Packages.ID = CommentNotify.PkgID AND CommentNotify.UserID = $myuid) ";
574
575
	} else {
		$q_from_extra = "";
Loui Chang's avatar
Loui Chang committed
576
577
	}

Dan McGee's avatar
Dan McGee committed
578
	$q_where = "WHERE 1 = 1 ";
579
580
	// TODO: possibly do string matching on category
	//       to make request variable values more sensible
581
	if (isset($_GET["C"]) && intval($_GET["C"])) {
582
		$q_where .= "AND Packages.CategoryID = ".intval($_GET["C"])." ";
Loui Chang's avatar
Loui Chang committed
583
584
	}

585
	if (isset($_GET['K'])) {
Loui Chang's avatar
Loui Chang committed
586
		# Search by maintainer
587
		if (isset($_GET["SeB"]) && $_GET["SeB"] == "m") {
canyonknight's avatar
canyonknight committed
588
			$q_where .= "AND Users.Username = " . $dbh->quote($_GET['K']) . " ";
Loui Chang's avatar
Loui Chang committed
589
		}
Andrea Scarpino's avatar
Andrea Scarpino committed
590
		# Search by submitter
591
		elseif (isset($_GET["SeB"]) && $_GET["SeB"] == "s") {
592
			$q_where .= "AND SubmitterUID = ".uid_from_username($_GET['K'])." ";
Loui Chang's avatar
Loui Chang committed
593
		}
594
		# Search by name
595
		elseif (isset($_GET["SeB"]) && $_GET["SeB"] == "n") {
canyonknight's avatar
canyonknight committed
596
597
			$K = "%" . addcslashes($_GET['K'], '%_') . "%";
			$q_where .= "AND (Name LIKE " . $dbh->quote($K) . ") ";
Andrea Scarpino's avatar
Andrea Scarpino committed
598
		}
599
600
		# Search by name (exact match)
		elseif (isset($_GET["SeB"]) && $_GET["SeB"] == "x") {
canyonknight's avatar
canyonknight committed
601
			$q_where .= "AND (Name = " . $dbh->quote($_GET['K']) . ") ";
602
		}
Andrea Scarpino's avatar
Andrea Scarpino committed
603
		# Search by name and description (Default)
Loui Chang's avatar
Loui Chang committed
604
		else {
canyonknight's avatar
canyonknight committed
605
606
607
			$K = "%" . addcslashes($_GET['K'], '%_') . "%";
			$q_where .= "AND (Name LIKE " . $dbh->quote($K) . " OR ";
			$q_where .= "Description LIKE " . $dbh->quote($K) . ") ";
Loui Chang's avatar
Loui Chang committed
608
609
610
		}
	}

611
	if (isset($_GET["do_Orphans"])) {
612
		$q_where .= "AND MaintainerUID IS NULL ";
Loui Chang's avatar
Loui Chang committed
613
	}
614

615
	if (isset($_GET['outdated'])) {
616
		if ($_GET['outdated'] == 'on') {
617
			$q_where .= "AND OutOfDateTS IS NOT NULL ";
618
619
		}
		elseif ($_GET['outdated'] == 'off') {
620
			$q_where .= "AND OutOfDateTS IS NULL ";
621
		}
622
623
	}

624
	$order = (isset($_GET["SO"]) && $_GET["SO"] == 'd') ? 'DESC' : 'ASC';
Loui Chang's avatar
Loui Chang committed
625

626
	$q_sort = "ORDER BY Name ".$order." ";
627
628
	$sort_by = isset($_GET["SB"]) ? $_GET["SB"] : '';
	switch ($sort_by) {
Loui Chang's avatar
Loui Chang committed
629
	case 'c':
630
		$q_sort = "ORDER BY CategoryID ".$order.", Name ASC ";
Loui Chang's avatar
Loui Chang committed
631
632
		break;
	case 'v':
633
		$q_sort = "ORDER BY NumVotes ".$order.", Name ASC ";
Loui Chang's avatar
Loui Chang committed
634
		break;
635
636
	case 'w':
		if ($SID) {
637
			$q_sort = "ORDER BY Voted ".$order.", Name ASC ";
638
639
640
641
		}
		break;
	case 'o':
		if ($SID) {
642
			$q_sort = "ORDER BY Notify ".$order.", Name ASC ";
643
644
		}
		break;
Loui Chang's avatar
Loui Chang committed
645
	case 'm':
646
		$q_sort = "ORDER BY Maintainer ".$order.", Name ASC ";
Loui Chang's avatar
Loui Chang committed
647
648
		break;
	case 'a':
649
		$q_sort = "ORDER BY ModifiedTS ".$order.", Name ASC ";
Loui Chang's avatar
Loui Chang committed
650
651
652
653
654
		break;
	default:
		break;
	}

655
	$q_limit = "LIMIT ".$_GET["PP"]." OFFSET ".$_GET["O"];
656

657
658
	$q = $q_select . $q_from . $q_from_extra . $q_where . $q_sort . $q_limit;
	$q_total = "SELECT COUNT(*) " . $q_from . $q_where;
Loui Chang's avatar
Loui Chang committed
659

canyonknight's avatar
canyonknight committed
660
661
	$result = $dbh->query($q);
	$result_t = $dbh->query($q_total);
662
	if ($result_t) {
canyonknight's avatar
canyonknight committed
663
664
		$row = $result_t->fetch(PDO::FETCH_NUM);
		$total = $row[0];
665
666
667
668
	}
	else {
		$total = 0;
	}
Simo Leone's avatar
Simo Leone committed
669

670
	if ($result && $total > 0) {
671
672
		if (isset($_GET["SO"]) && $_GET["SO"] == "d"){
			$SO_next = "a";
Loui Chang's avatar
Loui Chang committed
673
674
		}
		else {
675
			$SO_next = "d";
Loui Chang's avatar
Loui Chang committed
676
		}
677
	}
Simo Leone's avatar
Simo Leone committed
678

679
	// figure out the results to use
Loui Chang's avatar
Loui Chang committed
680
	$first = $_GET['O'] + 1;
Simo Leone's avatar
Simo Leone committed
681

682
683
684
685
686
	# calculation of pagination links
	$per_page = ($_GET['PP'] > 0) ? $_GET['PP'] : 50;
	$current = ceil($first / $per_page);
	$pages = ceil($total / $per_page);
	$templ_pages = array();
687

688
	if ($current > 1) {
689
690
		$templ_pages['&laquo; ' . __('First')] = 0;
		$templ_pages['&lsaquo; ' . __('Previous')] = ($current - 2) * $per_page;
691
	}
692

693
694
	if ($current - 5 > 1)
		$templ_pages["..."] = false;
695

696
697
698
	for ($i = max($current - 5, 1); $i <= min($pages, $current + 5); $i++) {
		$templ_pages[$i] = ($i - 1) * $per_page;
	}
699

700
701
	if ($current + 5 < $pages)
		$templ_pages["... "] = false;
702

703
	if ($current < $pages) {
704
705
		$templ_pages[__('Next') . ' &rsaquo;'] = $current * $per_page;
		$templ_pages[__('Last') . ' &raquo;'] = ($pages - 1) * $per_page;
706
	}
Simo Leone's avatar
Simo Leone committed
707

708
	include('pkg_search_form.php');
709

canyonknight's avatar
canyonknight committed
710
711
712
713
	if ($result) {
		while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
			$searchresults[] = $row;
		}
714
715
	}

716
717
	include('pkg_search_results.php');

Loui Chang's avatar
Loui Chang committed
718
	return;
eric's avatar
eric committed
719
720
}

721
722
723
724
725
726
727
/**
 * Determine if a POST string has been sent by a visitor
 *
 * @param string $action String to check has been sent via POST
 *
 * @return bool True if the POST string was used, otherwise false
 */
Dan McGee's avatar
Dan McGee committed
728
729
730
731
732
function current_action($action) {
	return (isset($_POST['action']) && $_POST['action'] == $action) ||
		isset($_POST[$action]);
}

733
/**
734
735
736
737
738
 * Determine if sent IDs are valid integers
 *
 * @param array $ids IDs to validate
 *
 * @return array All sent IDs that are valid integers
739
740
741
742
743
744
745
746
747
748
749
750
 */
function sanitize_ids($ids) {
	$new_ids = array();
	foreach ($ids as $id) {
		$id = intval($id);
		if ($id > 0) {
			$new_ids[] = $id;
		}
	}
	return $new_ids;
}

751
/**
752
 * Flag package(s) as out-of-date
753
 *
754
 * @global string $AUR_LOCATION The AUR's URL used for notification e-mails
755
 * @param string $atype Account type, output of account_from_sid
756
 * @param array $ids Array of package IDs to flag/unflag
757
 *
758
 * @return array Tuple of success/failure indicator and error message
759
 */
760
function pkg_flag($atype, $ids) {
Dan McGee's avatar
Dan McGee committed
761
762
	global $AUR_LOCATION;

763
	if (!$atype) {
764
		return array(false, __("You must be logged in before you can flag packages."));
765
766
	}

767
	$ids = sanitize_ids($ids);
768
	if (empty($ids)) {
769
		return array(false, __("You did not select any packages to flag."));
770
771
	}

772
	$dbh = DB::connect();
773

774
	$q = "UPDATE Packages SET";
775
	$q.= " OutOfDateTS = UNIX_TIMESTAMP()";
776
	$q.= " WHERE ID IN (" . implode(",", $ids) . ")";
777
	$q.= " AND OutOfDateTS IS NULL";
778
779
780

	$affected_pkgs = $dbh->exec($q);

781
	if ($affected_pkgs > 0) {
782
		# Notify of flagging by email
783
784
785
		$f_name = username_from_sid($_COOKIE['AURSID']);
		$f_email = email_from_sid($_COOKIE['AURSID']);
		$f_uid = uid_from_sid($_COOKIE['AURSID']);
786
787
		$q = "SELECT Packages.Name, Users.Email, Packages.ID ";
		$q.= "FROM Packages, Users ";
788
		$q.= "WHERE Packages.ID IN (" . implode(",", $ids) .") ";
789
790
		$q.= "AND Users.ID = Packages.MaintainerUID ";
		$q.= "AND Users.ID != " . $f_uid;
canyonknight's avatar
canyonknight committed
791
792
793
		$result = $dbh->query($q);
		if ($result) {
			while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
794
				# construct email
795
				$body = "Your package " . $row['Name'] . " has been flagged out of date by " . $f_name . " [1]. You may view your package at:\n" . $AUR_LOCATION . get_pkg_uri($row['Name']) . "\n\n[1] - " . $AUR_LOCATION . get_user_uri($f_name);
796
				$body = wordwrap($body, 70);
797
798
				$headers = "Reply-to: nobody@archlinux.org\nFrom:aur-notify@archlinux.org\nX-Mailer: PHP\nX-MimeOLE: Produced By AUR\n";
				@mail($row['Email'], "AUR Out-of-date Notification for ".$row['Name'], $body, $headers);
799
800
801
802
			}
		}
	}

803
	return array(true, __("The selected packages have been flagged out-of-date."));
804
805
806
807
808
809
810
811
}

/**
 * Unflag package(s) as out-of-date
 *
 * @param string $atype Account type, output of account_from_sid
 * @param array $ids Array of package IDs to flag/unflag
 *
812
 * @return array Tuple of success/failure indicator and error message
813
 */
814
function pkg_unflag($atype, $ids) {
815
	if (!$atype) {
816
		return array(false, __("You must be logged in before you can unflag packages."));
817
818
819
820
	}

	$ids = sanitize_ids($ids);
	if (empty($ids)) {
821
		return array(false, __("You did not select any packages to unflag."));
822
823
	}

824
	$dbh = DB::connect();
825
826
827
828
829
830

	$q = "UPDATE Packages SET ";
	$q.= "OutOfDateTS = NULL ";
	$q.= "WHERE ID IN (" . implode(",", $ids) . ") ";

	if ($atype != "Trusted User" && $atype != "Developer") {
831
		$q.= "AND MaintainerUID = " . uid_from_sid($_COOKIE["AURSID"]);
832
833
834
835
836
	}

	$result = $dbh->exec($q);

	if ($result) {
837
		return array(true, __("The selected packages have been unflagged."));
838
839
	}
}
840

841
842
843
844
/**
 * Delete packages
 *
 * @param string $atype Account type, output of account_from_sid
845
 * @param array $ids Array of package IDs to delete
846
 * @param int $mergepkgid Package to merge the deleted ones into
847
 *
848
 * @return array Tuple of success/failure indicator and error message
849
 */
850
function pkg_delete ($atype, $ids, $mergepkgid) {
851
	if (!$atype) {
852
		return array(false, __("You must be logged in before you can delete packages."));
853
854
855
	}

	# If they're a TU or dev, can delete
856
	if ($atype != "Trusted User" && $atype != "Developer") {
857
		return array(false, __("You do not have permission to delete packages."));
858
859
	}

860
	$ids = sanitize_ids($ids);
861
	if (empty($ids)) {
862
		return array(false, __("You did not select any packages to delete."));
863
864
	}

865
	$dbh = DB::connect();
866

867
	if ($mergepkgid) {
868
		$mergepkgname = pkgname_from_id($mergepkgid);
869
870
871
872
	}

	# Send email notifications
	foreach ($ids as $pkgid) {
canyonknight's avatar
canyonknight committed
873
874
875
876
877
878
		$q = "SELECT CommentNotify.*, Users.Email ";
		$q.= "FROM CommentNotify, Users ";
		$q.= "WHERE Users.ID = CommentNotify.UserID ";
		$q.= "AND CommentNotify.UserID != " . uid_from_sid($_COOKIE['AURSID']) . " ";
		$q.= "AND CommentNotify.PkgID = " . $pkgid;
		$result = $dbh->query($q);
879
880
		$bcc = array();

canyonknight's avatar
canyonknight committed
881
		while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
882
883
884
885
886
887
888
889
890
891
892
			array_push($bcc, $row['Email']);
		}
		if (!empty($bcc)) {
			$pkgname = pkgname_from_id($pkgid);

			# TODO: native language emails for users, based on their prefs
			# Simply making these strings translatable won't work, users would be
			# getting emails in the language that the user who posted the comment was in
			$body = "";
			if ($mergepkgid) {
				$body .= username_from_sid($_COOKIE['AURSID']) . " merged \"".$pkgname."\" into \"$mergepkgname\".\n\n";
893
				$body .= "You will no longer receive notifications about this package, please go to https://aur.archlinux.org" . get_pkg_uri($mergepkgname) . " and click the Notify button if you wish to recieve them again.";
894
895
896
897
898
899
900
			} else {
				$body .= username_from_sid($_COOKIE['AURSID']) . " deleted \"".$pkgname."\".\n\n";
				$body .= "You will no longer receive notifications about this package.";
			}
			$body = wordwrap($body, 70);
			$bcc = implode(', ', $bcc);
			$headers = "Bcc: $bcc\nReply-to: nobody@archlinux.org\nFrom: aur-notify@archlinux.org\nX-Mailer: AUR\n";
901
			@mail('undisclosed-recipients: ;', "AUR Package deleted: " . $pkgname, $body, $headers);
902
903
904
		}
	}

905
906
907
908
909
	if ($mergepkgid) {
		/* Merge comments */
		$q = "UPDATE PackageComments ";
		$q.= "SET PackageID = " . intval($mergepkgid) . " ";
		$q.= "WHERE PackageID IN (" . implode(",", $ids) . ")";
canyonknight's avatar
canyonknight committed
910
		$dbh->exec($q);
911
912
913
914
915
916
917
918
919
920
921

		/* Merge votes */
		foreach ($ids as $pkgid) {
			$q = "UPDATE PackageVotes ";
			$q.= "SET PackageID = " . intval($mergepkgid) . " ";
			$q.= "WHERE PackageID = " . $pkgid . " ";
			$q.= "AND UsersID NOT IN (";
			$q.= "SELECT * FROM (SELECT UsersID ";
			$q.= "FROM PackageVotes ";
			$q.= "WHERE PackageID = " . intval($mergepkgid);
			$q.= ") temp)";
canyonknight's avatar
canyonknight committed
922
			$dbh->exec($q);
923
924
925
926
927
928
		}

		$q = "UPDATE Packages ";
		$q.= "SET NumVotes = (SELECT COUNT(*) FROM PackageVotes ";
		$q.= "WHERE PackageID = " . intval($mergepkgid) . ") ";
		$q.= "WHERE ID = " . intval($mergepkgid);
canyonknight's avatar
canyonknight committed
929
		$dbh->exec($q);
930
931
	}

932
	$q = "DELETE FROM Packages WHERE ID IN (" . implode(",", $ids) . ")";
canyonknight's avatar
canyonknight committed
933
	$result = $dbh->exec($q);
934

935
	return array(true, __("The selected packages have been deleted."));
936
}
937

938
939
940
941
/**
 * Adopt or disown packages
 *
 * @param string $atype Account type, output of account_from_sid
942
 * @param array $ids Array of package IDs to adopt/disown
943
 * @param bool $action Adopts if true, disowns if false. Adopts by default
944
 *
945
 * @return array Tuple of success/failure indicator and error message
946
 */
947
function pkg_adopt ($atype, $ids, $action=true) {
948
949
	if (!$atype) {
		if ($action) {
950
			return array(false, __("You must be logged in before you can adopt packages."));
951
		} else {
952
			return array(false, __("You must be logged in before you can disown packages."));
953
954
955
		}
	}

956
	$ids = sanitize_ids($ids);
957
958
	if (empty($ids)) {
		if ($action) {
959
			return array(false, __("You did not select any packages to adopt."));
960
		} else {
961
			return array(false, __("You did not select any packages to disown."));
962
963
964
		}
	}