Always use virtual URLs
[aur.git] / web / lib / pkgbasefuncs.inc.php
blob9cdd8a3a9663f47168ff88581fdfbe0db343282c
1 <?php
3 include_once("pkgreqfuncs.inc.php");
5 /**
6 * Get all package categories stored in the database
8 * @param \PDO An already established database connection
10 * @return array All package categories
12 function pkgbase_categories() {
13 $dbh = DB::connect();
14 $q = "SELECT * FROM PackageCategories WHERE ID != 1 ";
15 $q.= "ORDER BY Category ASC";
16 $result = $dbh->query($q);
17 if (!$result) {
18 return null;
21 return $result->fetchAll(PDO::FETCH_KEY_PAIR);
24 /**
25 * Get the number of non-deleted comments for a specific package base
27 * @param string $base_id The package base ID to get comment count for
28 * @param bool $include_deleted True if deleted comments should be included
30 * @return string The number of comments left for a specific package
32 function pkgbase_comments_count($base_id, $include_deleted) {
33 $base_id = intval($base_id);
34 if (!$base_id) {
35 return null;
38 $dbh = DB::connect();
39 $q = "SELECT COUNT(*) FROM PackageComments ";
40 $q.= "WHERE PackageBaseID = " . $base_id . " ";
41 if (!$include_deleted) {
42 $q.= "AND DelUsersID IS NULL";
44 $result = $dbh->query($q);
45 if (!$result) {
46 return null;
49 return $result->fetchColumn(0);
52 /**
53 * Get all package comment information for a specific package base
55 * @param int $base_id The package base ID to get comments for
56 * @param int $limit Maximum number of comments to return (0 means unlimited)
57 * @param bool $include_deleted True if deleted comments should be included
59 * @return array All package comment information for a specific package base
61 function pkgbase_comments($base_id, $limit, $include_deleted) {
62 $base_id = intval($base_id);
63 $limit = intval($limit);
64 if (!$base_id) {
65 return null;
68 $dbh = DB::connect();
69 $q = "SELECT PackageComments.ID, UserName, UsersID, Comments, ";
70 $q.= "CommentTS, DelUsersID FROM PackageComments LEFT JOIN Users ";
71 $q.= "ON PackageComments.UsersID = Users.ID ";
72 $q.= "WHERE PackageBaseID = " . $base_id . " ";
73 if (!$include_deleted) {
74 $q.= "AND DelUsersID IS NULL ";
76 $q.= "ORDER BY CommentTS DESC";
77 if ($limit > 0) {
78 $q.=" LIMIT " . $limit;
80 $result = $dbh->query($q);
81 if (!$result) {
82 return null;
85 return $result->fetchAll();
88 /**
89 * Add a comment to a package page and send out appropriate notifications
91 * @param string $base_id The package base ID to add the comment on
92 * @param string $uid The user ID of the individual who left the comment
93 * @param string $comment The comment left on a package page
95 * @return void
97 function pkgbase_add_comment($base_id, $uid, $comment) {
98 $dbh = DB::connect();
100 $q = "INSERT INTO PackageComments ";
101 $q.= "(PackageBaseID, UsersID, Comments, CommentTS) VALUES (";
102 $q.= intval($base_id) . ", " . $uid . ", ";
103 $q.= $dbh->quote($comment) . ", UNIX_TIMESTAMP())";
104 $dbh->exec($q);
107 * Send e-mail notifications.
108 * TODO: Move notification logic to separate function where it belongs.
110 $q = "SELECT CommentNotify.*, Users.Email ";
111 $q.= "FROM CommentNotify, Users ";
112 $q.= "WHERE Users.ID = CommentNotify.UserID ";
113 $q.= "AND CommentNotify.UserID != " . $uid . " ";
114 $q.= "AND CommentNotify.PackageBaseID = " . intval($base_id);
115 $result = $dbh->query($q);
116 $bcc = array();
118 if ($result) {
119 while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
120 array_push($bcc, $row['Email']);
123 $q = "SELECT Name FROM PackageBases WHERE ID = ";
124 $q.= intval($base_id);
125 $result = $dbh->query($q);
126 $row = $result->fetch(PDO::FETCH_ASSOC);
129 * TODO: Add native language emails for users, based on their
130 * preferences. Simply making these strings translatable won't
131 * work, users would be getting emails in the language that the
132 * user who posted the comment was in.
134 $body =
135 'from ' . aur_location() . get_pkgbase_uri($row['Name']) . "\n"
136 . username_from_sid($_COOKIE['AURSID']) . " wrote:\n\n"
137 . $comment
138 . "\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.";
139 $body = wordwrap($body, 70);
140 $bcc = implode(', ', $bcc);
141 $thread_id = "<pkg-notifications-" . $row['Name'] . "@aur.archlinux.org>";
142 $headers = "MIME-Version: 1.0\r\n" .
143 "Content-type: text/plain; charset=UTF-8\r\n" .
144 "Bcc: $bcc\r\n" .
145 "Reply-to: noreply@aur.archlinux.org\r\n" .
146 "From: notify@aur.archlinux.org\r\n" .
147 "In-Reply-To: $thread_id\r\n" .
148 "References: $thread_id\r\n" .
149 "X-Mailer: AUR";
150 @mail('undisclosed-recipients: ;', "AUR Comment for " . $row['Name'], $body, $headers);
155 * Get a list of all packages a logged-in user has voted for
157 * @param string $sid The session ID of the visitor
159 * @return array All packages the visitor has voted for
161 function pkgbase_votes_from_sid($sid="") {
162 $pkgs = array();
163 if (!$sid) {return $pkgs;}
164 $dbh = DB::connect();
165 $q = "SELECT PackageBaseID ";
166 $q.= "FROM PackageVotes, Users, Sessions ";
167 $q.= "WHERE Users.ID = Sessions.UsersID ";
168 $q.= "AND Users.ID = PackageVotes.UsersID ";
169 $q.= "AND Sessions.SessionID = " . $dbh->quote($sid);
170 $result = $dbh->query($q);
171 if ($result) {
172 while ($row = $result->fetch(PDO::FETCH_NUM)) {
173 $pkgs[$row[0]] = 1;
176 return $pkgs;
180 * Get the package base details
182 * @param string $id The package base ID to get description for
184 * @return array The package base's details OR error message
186 function pkgbase_get_details($base_id) {
187 $dbh = DB::connect();
189 $q = "SELECT PackageBases.ID, PackageBases.Name, ";
190 $q.= "PackageBases.CategoryID, PackageBases.NumVotes, ";
191 $q.= "PackageBases.OutOfDateTS, PackageBases.SubmittedTS, ";
192 $q.= "PackageBases.ModifiedTS, PackageBases.SubmitterUID, ";
193 $q.= "PackageBases.MaintainerUID, PackageBases.PackagerUID, ";
194 $q.= "PackageCategories.Category, ";
195 $q.= "(SELECT COUNT(*) FROM PackageRequests ";
196 $q.= " WHERE PackageRequests.PackageBaseID = PackageBases.ID ";
197 $q.= " AND PackageRequests.Status = 0) AS RequestCount ";
198 $q.= "FROM PackageBases, PackageCategories ";
199 $q.= "WHERE PackageBases.CategoryID = PackageCategories.ID ";
200 $q.= "AND PackageBases.ID = " . intval($base_id);
201 $result = $dbh->query($q);
203 $row = array();
205 if (!$result) {
206 $row['error'] = __("Error retrieving package details.");
208 else {
209 $row = $result->fetch(PDO::FETCH_ASSOC);
210 if (empty($row)) {
211 $row['error'] = __("Package details could not be found.");
215 return $row;
219 * Display the package base details page
221 * @param string $id The package base ID to get details page for
222 * @param array $row Package base details retrieved by pkgbase_get_details()
223 * @param string $SID The session ID of the visitor
225 * @return void
227 function pkgbase_display_details($base_id, $row, $SID="") {
228 $dbh = DB::connect();
230 if (isset($row['error'])) {
231 print "<p>" . $row['error'] . "</p>\n";
233 else {
234 $pkgbase_name = pkgbase_name_from_id($base_id);
236 include('pkgbase_details.php');
238 if ($SID) {
239 include('pkg_comment_form.php');
242 $limit = isset($_GET['comments']) ? 0 : 10;
243 $include_deleted = has_credential(CRED_COMMENT_VIEW_DELETED);
244 $comments = pkgbase_comments($base_id, $limit, $include_deleted);
245 if (!empty($comments)) {
246 include('pkg_comments.php');
252 * Convert a list of package IDs into a list of corresponding package bases.
254 * @param array|int $ids Array of package IDs to convert
256 * @return array|int List of package base IDs
258 function pkgbase_from_pkgid($ids) {
259 $dbh = DB::connect();
261 if (is_array($ids)) {
262 $q = "SELECT PackageBaseID FROM Packages ";
263 $q.= "WHERE ID IN (" . implode(",", $ids) . ")";
264 $result = $dbh->query($q);
265 return $result->fetchAll(PDO::FETCH_COLUMN, 0);
266 } else {
267 $q = "SELECT PackageBaseID FROM Packages ";
268 $q.= "WHERE ID = " . $ids;
269 $result = $dbh->query($q);
270 return $result->fetch(PDO::FETCH_COLUMN, 0);
275 * Retrieve ID of a package base by name
277 * @param string $name The package base name to retrieve the ID for
279 * @return int The ID of the package base
281 function pkgbase_from_name($name) {
282 $dbh = DB::connect();
283 $q = "SELECT ID FROM PackageBases WHERE Name = " . $dbh->quote($name);
284 $result = $dbh->query($q);
285 return $result->fetch(PDO::FETCH_COLUMN, 0);
289 * Retrieve the name of a package base given its ID
291 * @param int $base_id The ID of the package base to query
293 * @return string The name of the package base
295 function pkgbase_name_from_id($base_id) {
296 $dbh = DB::connect();
297 $q = "SELECT Name FROM PackageBases WHERE ID = " . intval($base_id);
298 $result = $dbh->query($q);
299 return $result->fetch(PDO::FETCH_COLUMN, 0);
303 * Get the names of all packages belonging to a package base
305 * @param int $base_id The ID of the package base
307 * @return array The names of all packages belonging to the package base
309 function pkgbase_get_pkgnames($base_id) {
310 $dbh = DB::connect();
311 $q = "SELECT Name FROM Packages WHERE PackageBaseID = " . intval($base_id);
312 $result = $dbh->query($q);
313 return $result->fetchAll(PDO::FETCH_COLUMN, 0);
317 * Delete all packages belonging to a package base
319 * @param int $base_id The ID of the package base
321 * @return void
323 function pkgbase_delete_packages($base_id) {
324 $dbh = DB::connect();
325 $q = "DELETE FROM Packages WHERE PackageBaseID = " . intval($base_id);
326 $dbh->exec($q);
330 * Retrieve the maintainer of a package base given its ID
332 * @param int $base_id The ID of the package base to query
334 * @return int The user ID of the current package maintainer
336 function pkgbase_maintainer_uid($base_id) {
337 $dbh = DB::connect();
338 $q = "SELECT MaintainerUID FROM PackageBases WHERE ID = " . intval($base_id);
339 $result = $dbh->query($q);
340 return $result->fetch(PDO::FETCH_COLUMN, 0);
345 * Flag package(s) as out-of-date
347 * @param array $base_ids Array of package base IDs to flag/unflag
349 * @return array Tuple of success/failure indicator and error message
351 function pkgbase_flag($base_ids) {
352 if (!has_credential(CRED_PKGBASE_FLAG)) {
353 return array(false, __("You must be logged in before you can flag packages."));
356 $base_ids = sanitize_ids($base_ids);
357 if (empty($base_ids)) {
358 return array(false, __("You did not select any packages to flag."));
361 $dbh = DB::connect();
363 $q = "UPDATE PackageBases SET";
364 $q.= " OutOfDateTS = UNIX_TIMESTAMP()";
365 $q.= " WHERE ID IN (" . implode(",", $base_ids) . ")";
366 $q.= " AND OutOfDateTS IS NULL";
368 $affected_pkgs = $dbh->exec($q);
370 if ($affected_pkgs > 0) {
371 /* Notify of flagging by e-mail. */
372 $f_name = username_from_sid($_COOKIE['AURSID']);
373 $f_email = email_from_sid($_COOKIE['AURSID']);
374 $f_uid = uid_from_sid($_COOKIE['AURSID']);
375 $q = "SELECT PackageBases.Name, Users.Email ";
376 $q.= "FROM PackageBases, Users ";
377 $q.= "WHERE PackageBases.ID IN (" . implode(",", $base_ids) .") ";
378 $q.= "AND Users.ID = PackageBases.MaintainerUID ";
379 $q.= "AND Users.ID != " . $f_uid;
380 $result = $dbh->query($q);
381 if ($result) {
382 while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
383 $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_pkgbase_uri($row['Name']) . "\n\n[1] - " . aur_location() . get_user_uri($f_name);
384 $body = wordwrap($body, 70);
385 $headers = "MIME-Version: 1.0\r\n" .
386 "Content-type: text/plain; charset=UTF-8\r\n" .
387 "Reply-to: noreply@aur.archlinux.org\r\n" .
388 "From: notify@aur.archlinux.org\r\n" .
389 "X-Mailer: PHP\r\n" .
390 "X-MimeOLE: Produced By AUR";
391 @mail($row['Email'], "AUR Out-of-date Notification for ".$row['Name'], $body, $headers);
396 return array(true, __("The selected packages have been flagged out-of-date."));
400 * Unflag package(s) as out-of-date
402 * @param array $base_ids Array of package base IDs to flag/unflag
404 * @return array Tuple of success/failure indicator and error message
406 function pkgbase_unflag($base_ids) {
407 $uid = uid_from_sid($_COOKIE["AURSID"]);
408 if (!$uid) {
409 return array(false, __("You must be logged in before you can unflag packages."));
412 $base_ids = sanitize_ids($base_ids);
413 if (empty($base_ids)) {
414 return array(false, __("You did not select any packages to unflag."));
417 $dbh = DB::connect();
419 $q = "UPDATE PackageBases SET ";
420 $q.= "OutOfDateTS = NULL ";
421 $q.= "WHERE ID IN (" . implode(",", $base_ids) . ") ";
423 if (!has_credential(CRED_PKGBASE_UNFLAG)) {
424 $q.= "AND MaintainerUID = " . $uid;
427 $result = $dbh->exec($q);
429 if ($result) {
430 return array(true, __("The selected packages have been unflagged."));
435 * Delete package bases
437 * @param array $base_ids Array of package base IDs to delete
438 * @param int $merge_base_id Package base to merge the deleted ones into
439 * @param int $via Package request to close upon deletion
441 * @return array Tuple of success/failure indicator and error message
443 function pkgbase_delete ($base_ids, $merge_base_id, $via) {
444 if (!has_credential(CRED_PKGBASE_DELETE)) {
445 return array(false, __("You do not have permission to delete packages."));
448 $base_ids = sanitize_ids($base_ids);
449 if (empty($base_ids)) {
450 return array(false, __("You did not select any packages to delete."));
453 $dbh = DB::connect();
455 if ($merge_base_id) {
456 $merge_base_name = pkgbase_name_from_id($merge_base_id);
459 /* Send e-mail notifications. */
460 foreach ($base_ids as $base_id) {
461 $q = "SELECT CommentNotify.*, Users.Email ";
462 $q.= "FROM CommentNotify, Users ";
463 $q.= "WHERE Users.ID = CommentNotify.UserID ";
464 $q.= "AND CommentNotify.UserID != " . uid_from_sid($_COOKIE['AURSID']) . " ";
465 $q.= "AND CommentNotify.PackageBaseID = " . $base_id;
466 $result = $dbh->query($q);
467 $bcc = array();
469 while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
470 array_push($bcc, $row['Email']);
472 if (!empty($bcc)) {
473 $pkgbase_name = pkgbase_name_from_id($base_id);
476 * TODO: Add native language emails for users, based on
477 * their preferences. Simply making these strings
478 * translatable won't work, users would be getting
479 * emails in the language that the user who posted the
480 * comment was in.
482 $body = "";
483 if ($merge_base_id) {
484 $body .= username_from_sid($_COOKIE['AURSID']) . " merged \"".$pkgbase_name."\" into \"$merge_base_name\".\n\n";
485 $body .= "You will no longer receive notifications about this package, please go to https://aur.archlinux.org" . get_pkgbase_uri($merge_base_name) . " and click the Notify button if you wish to recieve them again.";
486 } else {
487 $body .= username_from_sid($_COOKIE['AURSID']) . " deleted \"".$pkgbase_name."\".\n\n";
488 $body .= "You will no longer receive notifications about this package.";
490 $body = wordwrap($body, 70);
491 $bcc = implode(', ', $bcc);
492 $headers = "MIME-Version: 1.0\r\n" .
493 "Content-type: text/plain; charset=UTF-8\r\n" .
494 "Bcc: $bcc\r\n" .
495 "Reply-to: noreply@aur.archlinux.org\r\n" .
496 "From: notify@aur.archlinux.org\r\n" .
497 "X-Mailer: AUR";
498 @mail('undisclosed-recipients: ;', "AUR Package deleted: " . $pkgbase_name, $body, $headers);
503 * Close package request if the deletion was initiated through the
504 * request interface. NOTE: This needs to happen *before* the actual
505 * deletion. Otherwise, the former maintainer will not be included in
506 * the Cc list of the request notification email.
508 if ($via) {
509 pkgreq_close(intval($via), 'accepted', '');
512 if ($merge_base_id) {
513 /* Merge comments */
514 $q = "UPDATE PackageComments ";
515 $q.= "SET PackageBaseID = " . intval($merge_base_id) . " ";
516 $q.= "WHERE PackageBaseID IN (" . implode(",", $base_ids) . ")";
517 $dbh->exec($q);
519 /* Merge votes */
520 foreach ($base_ids as $base_id) {
521 $q = "UPDATE PackageVotes ";
522 $q.= "SET PackageBaseID = " . intval($merge_base_id) . " ";
523 $q.= "WHERE PackageBaseID = " . $base_id . " ";
524 $q.= "AND UsersID NOT IN (";
525 $q.= "SELECT * FROM (SELECT UsersID ";
526 $q.= "FROM PackageVotes ";
527 $q.= "WHERE PackageBaseID = " . intval($merge_base_id);
528 $q.= ") temp)";
529 $dbh->exec($q);
532 $q = "UPDATE PackageBases ";
533 $q.= "SET NumVotes = (SELECT COUNT(*) FROM PackageVotes ";
534 $q.= "WHERE PackageBaseID = " . intval($merge_base_id) . ") ";
535 $q.= "WHERE ID = " . intval($merge_base_id);
536 $dbh->exec($q);
539 $q = "DELETE FROM Packages WHERE PackageBaseID IN (" . implode(",", $base_ids) . ")";
540 $dbh->exec($q);
542 $q = "DELETE FROM PackageBases WHERE ID IN (" . implode(",", $base_ids) . ")";
543 $dbh->exec($q);
545 return array(true, __("The selected packages have been deleted."));
549 * Adopt or disown packages
551 * @param array $base_ids Array of package base IDs to adopt/disown
552 * @param bool $action Adopts if true, disowns if false. Adopts by default
553 * @param int $via Package request to close upon adoption
555 * @return array Tuple of success/failure indicator and error message
557 function pkgbase_adopt ($base_ids, $action=true, $via) {
558 $uid = uid_from_sid($_COOKIE["AURSID"]);
559 if (!$uid) {
560 if ($action) {
561 return array(false, __("You must be logged in before you can adopt packages."));
562 } else {
563 return array(false, __("You must be logged in before you can disown packages."));
567 $base_ids = sanitize_ids($base_ids);
568 if (empty($base_ids)) {
569 if ($action) {
570 return array(false, __("You did not select any packages to adopt."));
571 } else {
572 return array(false, __("You did not select any packages to disown."));
577 * Close package request if the disownment was initiated through the
578 * request interface. NOTE: This needs to happen *before* the actual
579 * disown operation. Otherwise, the former maintainer will not be
580 * included in the Cc list of the request notification email.
582 if ($via) {
583 pkgreq_close(intval($via), 'accepted', '');
586 $dbh = DB::connect();
588 $q = "UPDATE PackageBases ";
589 if ($action) {
590 $q.= "SET MaintainerUID = $uid ";
591 } else {
592 $q.= "SET MaintainerUID = NULL ";
594 $q.= "WHERE ID IN (" . implode(",", $base_ids) . ") ";
596 if ($action && !has_credential(CRED_PKGBASE_ADOPT)) {
597 /* Regular users may only adopt orphan packages. */
598 $q.= "AND MaintainerUID IS NULL";
600 if (!$action && !has_credential(CRED_PKGBASE_DISOWN)) {
601 /* Regular users may only disown their own packages. */
602 $q.= "AND MaintainerUID = " . $uid;
605 $dbh->exec($q);
607 if ($action) {
608 pkgbase_notify($base_ids);
609 return array(true, __("The selected packages have been adopted."));
610 } else {
611 return array(true, __("The selected packages have been disowned."));
616 * Vote and un-vote for packages
618 * @param array $base_ids Array of package base IDs to vote/un-vote
619 * @param bool $action Votes if true, un-votes if false. Votes by default
621 * @return array Tuple of success/failure indicator and error message
623 function pkgbase_vote ($base_ids, $action=true) {
624 if (!has_credential(CRED_PKGBASE_VOTE)) {
625 if ($action) {
626 return array(false, __("You must be logged in before you can vote for packages."));
627 } else {
628 return array(false, __("You must be logged in before you can un-vote for packages."));
632 $base_ids = sanitize_ids($base_ids);
633 if (empty($base_ids)) {
634 if ($action) {
635 return array(false, __("You did not select any packages to vote for."));
636 } else {
637 return array(false, __("Your votes have been removed from the selected packages."));
641 $dbh = DB::connect();
642 $my_votes = pkgbase_votes_from_sid($_COOKIE["AURSID"]);
643 $uid = uid_from_sid($_COOKIE["AURSID"]);
645 $first = 1;
646 foreach ($base_ids as $pid) {
647 if ($action) {
648 $check = !isset($my_votes[$pid]);
649 } else {
650 $check = isset($my_votes[$pid]);
653 if ($check) {
654 if ($first) {
655 $first = 0;
656 $vote_ids = $pid;
657 if ($action) {
658 $vote_clauses = "($uid, $pid, UNIX_TIMESTAMP())";
660 } else {
661 $vote_ids .= ", $pid";
662 if ($action) {
663 $vote_clauses .= ", ($uid, $pid, UNIX_TIMESTAMP())";
669 /* Only add votes for packages the user hasn't already voted for. */
670 $op = $action ? "+" : "-";
671 $q = "UPDATE PackageBases SET NumVotes = NumVotes $op 1 ";
672 $q.= "WHERE ID IN ($vote_ids)";
674 $dbh->exec($q);
676 if ($action) {
677 $q = "INSERT INTO PackageVotes (UsersID, PackageBaseID, VoteTS) VALUES ";
678 $q.= $vote_clauses;
679 } else {
680 $q = "DELETE FROM PackageVotes WHERE UsersID = $uid ";
681 $q.= "AND PackageBaseID IN ($vote_ids)";
684 $dbh->exec($q);
686 if ($action) {
687 return array(true, __("Your votes have been cast for the selected packages."));
688 } else {
689 return array(true, __("Your votes have been removed from the selected packages."));
694 * Get all usernames and IDs that voted for a specific package base
696 * @param string $pkgbase_name The package base to retrieve votes for
698 * @return array User IDs and usernames that voted for a specific package base
700 function pkgbase_votes_from_name($pkgbase_name) {
701 $dbh = DB::connect();
703 $q = "SELECT UsersID, Username, Name, VoteTS FROM PackageVotes ";
704 $q.= "LEFT JOIN Users ON UsersID = Users.ID ";
705 $q.= "LEFT JOIN PackageBases ";
706 $q.= "ON PackageVotes.PackageBaseID = PackageBases.ID ";
707 $q.= "WHERE PackageBases.Name = ". $dbh->quote($pkgbase_name) . " ";
708 $q.= "ORDER BY Username";
709 $result = $dbh->query($q);
711 if (!$result) {
712 return;
715 $votes = array();
716 while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
717 $votes[] = $row;
720 return $votes;
724 * Determine if a user has already voted for a specific package base
726 * @param string $uid The user ID to check for an existing vote
727 * @param string $base_id The package base ID to check for an existing vote
729 * @return bool True if the user has already voted, otherwise false
731 function pkgbase_user_voted($uid, $base_id) {
732 $dbh = DB::connect();
733 $q = "SELECT COUNT(*) FROM PackageVotes WHERE ";
734 $q.= "UsersID = ". $dbh->quote($uid) . " AND ";
735 $q.= "PackageBaseID = " . $dbh->quote($base_id);
736 $result = $dbh->query($q);
737 if (!$result) {
738 return null;
741 return ($result->fetch(PDO::FETCH_COLUMN, 0) > 0);
745 * Determine if a user wants notifications for a specific package base
747 * @param string $uid User ID to check in the database
748 * @param string $base_id Package base ID to check notifications for
750 * @return bool True if the user wants notifications, otherwise false
752 function pkgbase_user_notify($uid, $base_id) {
753 $dbh = DB::connect();
755 $q = "SELECT * FROM CommentNotify WHERE UserID = " . $dbh->quote($uid);
756 $q.= " AND PackageBaseID = " . $dbh->quote($base_id);
757 $result = $dbh->query($q);
759 if ($result->fetch(PDO::FETCH_NUM)) {
760 return true;
762 else {
763 return false;
768 * Toggle notification of packages
770 * @param array $base_ids Array of package base IDs to toggle
772 * @return array Tuple of success/failure indicator and error message
774 function pkgbase_notify ($base_ids, $action=true) {
775 if (!has_credential(CRED_PKGBASE_NOTIFY)) {
776 return;
779 $base_ids = sanitize_ids($base_ids);
780 if (empty($base_ids)) {
781 return array(false, __("Couldn't add to notification list."));
784 $dbh = DB::connect();
785 $uid = uid_from_sid($_COOKIE["AURSID"]);
787 $output = "";
789 $first = true;
792 * There currently shouldn't be multiple requests here, but the format
793 * in which it's sent requires this.
795 foreach ($base_ids as $bid) {
796 $q = "SELECT Name FROM PackageBases WHERE ID = $bid";
797 $result = $dbh->query($q);
798 if ($result) {
799 $row = $result->fetch(PDO::FETCH_NUM);
800 $basename = $row[0];
802 else {
803 $basename = '';
806 if ($first)
807 $first = false;
808 else
809 $output .= ", ";
812 if ($action) {
813 $q = "SELECT COUNT(*) FROM CommentNotify WHERE ";
814 $q .= "UserID = $uid AND PackageBaseID = $bid";
816 /* Notification already added. Don't add again. */
817 $result = $dbh->query($q);
818 if ($result->fetchColumn() == 0) {
819 $q = "INSERT INTO CommentNotify (PackageBaseID, UserID) VALUES ($bid, $uid)";
820 $dbh->exec($q);
823 $output .= $basename;
825 else {
826 $q = "DELETE FROM CommentNotify WHERE PackageBaseID = $bid ";
827 $q .= "AND UserID = $uid";
828 $dbh->exec($q);
830 $output .= $basename;
834 if ($action) {
835 $output = __("You have been added to the comment notification list for %s.", $output);
837 else {
838 $output = __("You have been removed from the comment notification list for %s.", $output);
841 return array(true, $output);
845 * Delete a package comment
847 * @return array Tuple of success/failure indicator and error message
849 function pkgbase_delete_comment() {
850 $uid = uid_from_sid($_COOKIE["AURSID"]);
851 if (!$uid) {
852 return array(false, __("You must be logged in before you can edit package information."));
855 if (isset($_POST["comment_id"])) {
856 $comment_id = $_POST["comment_id"];
857 } else {
858 return array(false, __("Missing comment ID."));
861 $dbh = DB::connect();
862 if (can_delete_comment($comment_id)) {
863 $q = "UPDATE PackageComments ";
864 $q.= "SET DelUsersID = ".$uid." ";
865 $q.= "WHERE ID = ".intval($comment_id);
866 $dbh->exec($q);
867 return array(true, __("Comment has been deleted."));
868 } else {
869 return array(false, __("You are not allowed to delete this comment."));
874 * Change package base category
876 * @param int Package base ID of the package base to modify
878 * @return array Tuple of success/failure indicator and error message
880 function pkgbase_change_category($base_id) {
881 $uid = uid_from_sid($_COOKIE["AURSID"]);
882 if (!$uid) {
883 return array(false, __("You must be logged in before you can edit package information."));
886 if (isset($_POST["category_id"])) {
887 $category_id = $_POST["category_id"];
888 } else {
889 return array(false, __("Missing category ID."));
892 $dbh = DB::connect();
893 $catArray = pkgbase_categories($dbh);
894 if (!array_key_exists($category_id, $catArray)) {
895 return array(false, __("Invalid category ID."));
898 $base_id = intval($base_id);
900 /* Verify package ownership. */
901 $q = "SELECT MaintainerUID FROM PackageBases WHERE ID = " . $base_id;
902 $result = $dbh->query($q);
903 if ($result) {
904 $row = $result->fetch(PDO::FETCH_ASSOC);
907 if (!$result || !has_credential(CRED_PKGBASE_CHANGE_CATEGORY, array($row["MaintainerUID"]))) {
908 return array(false, __("You are not allowed to change this package category."));
911 $q = "UPDATE PackageBases ";
912 $q.= "SET CategoryID = ".intval($category_id)." ";
913 $q.= "WHERE ID = ".intval($base_id);
914 $dbh->exec($q);
915 return array(true, __("Package category changed."));
919 * Add package base information to the database
921 * @param string $name Name of the new package base
922 * @param int $category_id Category for the new package base
923 * @param int $uid User ID of the package uploader
925 * @return int ID of the new package base
927 function pkgbase_create($name, $category_id, $uid) {
928 $dbh = DB::connect();
929 $q = sprintf("INSERT INTO PackageBases (Name, CategoryID, " .
930 "SubmittedTS, ModifiedTS, SubmitterUID, MaintainerUID, " .
931 "PackagerUID) VALUES (%s, %d, UNIX_TIMESTAMP(), " .
932 "UNIX_TIMESTAMP(), %d, %d, %d)",
933 $dbh->quote($name), $category_id, $uid, $uid, $uid);
934 $dbh->exec($q);
935 return $dbh->lastInsertId();
939 * Update package base information for a specific package base
941 * @param string $name Name of the updated package base
942 * @param int $base_id The package base ID of the affected package
943 * @param int $uid User ID of the package uploader
945 * @return void
947 function pkgbase_update($base_id, $name, $uid) {
948 $dbh = DB::connect();
949 $q = sprintf("UPDATE PackageBases SET " .
950 "Name = %s, ModifiedTS = UNIX_TIMESTAMP(), " .
951 "MaintainerUID = %d, PackagerUID = %d, OutOfDateTS = NULL " .
952 "WHERE ID = %d",
953 $dbh->quote($name), $uid, $uid, $base_id);
954 $dbh->exec($q);
958 * Change the category a package base belongs to
960 * @param int $base_id The package base ID to change the category for
961 * @param int $category_id The new category ID for the package
963 * @return void
965 function pkgbase_update_category($base_id, $category_id) {
966 $dbh = DB::connect();
967 $q = sprintf("UPDATE PackageBases SET CategoryID = %d WHERE ID = %d",
968 $category_id, $base_id);
969 $dbh->exec($q);