Rename the CommentNotify table to PackageNotifications
[aur.git] / web / lib / pkgfuncs.inc.php
blob0e152ddc83c6c75d5635074501504175105a3b2a
1 <?php
3 include_once("pkgbasefuncs.inc.php");
5 /**
6 * Determine if the user can delete a specific package comment
8 * Only the comment submitter, Trusted Users, and Developers can delete
9 * comments. This function is used for the backend side of comment deletion.
11 * @param string $comment_id The comment ID in the database
13 * @return bool True if the user can delete the comment, otherwise false
15 function can_delete_comment($comment_id=0) {
16 $dbh = DB::connect();
18 $q = "SELECT UsersID FROM PackageComments ";
19 $q.= "WHERE ID = " . intval($comment_id);
20 $result = $dbh->query($q);
22 if (!$result) {
23 return false;
26 $uid = $result->fetch(PDO::FETCH_COLUMN, 0);
28 return has_credential(CRED_COMMENT_DELETE, array($uid));
31 /**
32 * Determine if the user can delete a specific package comment using an array
34 * Only the comment submitter, Trusted Users, and Developers can delete
35 * comments. This function is used for the frontend side of comment deletion.
37 * @param array $comment All database information relating a specific comment
39 * @return bool True if the user can delete the comment, otherwise false
41 function can_delete_comment_array($comment) {
42 return has_credential(CRED_COMMENT_DELETE, array($comment['UsersID']));
45 /**
46 * Determine if the user can edit a specific package comment
48 * Only the comment submitter, Trusted Users, and Developers can edit
49 * comments. This function is used for the backend side of comment editing.
51 * @param string $comment_id The comment ID in the database
53 * @return bool True if the user can edit the comment, otherwise false
55 function can_edit_comment($comment_id=0) {
56 $dbh = DB::connect();
58 $q = "SELECT UsersID FROM PackageComments ";
59 $q.= "WHERE ID = " . intval($comment_id);
60 $result = $dbh->query($q);
62 if (!$result) {
63 return false;
66 $uid = $result->fetch(PDO::FETCH_COLUMN, 0);
68 return has_credential(CRED_COMMENT_EDIT, array($uid));
71 /**
72 * Determine if the user can edit a specific package comment using an array
74 * Only the comment submitter, Trusted Users, and Developers can edit
75 * comments. This function is used for the frontend side of comment editing.
77 * @param array $comment All database information relating a specific comment
79 * @return bool True if the user can edit the comment, otherwise false
81 function can_edit_comment_array($comment) {
82 return has_credential(CRED_COMMENT_EDIT, array($comment['UsersID']));
85 /**
86 * Determine if the user can pin a specific package comment
88 * Only the Package Maintainer, Trusted Users, and Developers can pin
89 * comments. This function is used for the backend side of comment pinning.
91 * @param string $comment_id The comment ID in the database
93 * @return bool True if the user can pin the comment, otherwise false
95 function can_pin_comment($comment_id=0) {
96 $dbh = DB::connect();
98 $q = "SELECT MaintainerUID FROM PackageBases AS pb ";
99 $q.= "LEFT JOIN PackageComments AS pc ON pb.ID = pc.PackageBaseID ";
100 $q.= "WHERE pc.ID = " . intval($comment_id);
101 $result = $dbh->query($q);
103 if (!$result) {
104 return false;
107 $uid = $result->fetch(PDO::FETCH_COLUMN, 0);
109 return has_credential(CRED_COMMENT_PIN, array($uid));
113 * Determine if the user can edit a specific package comment using an array
115 * Only the Package Maintainer, Trusted Users, and Developers can pin
116 * comments. This function is used for the frontend side of comment pinning.
118 * @param array $comment All database information relating a specific comment
120 * @return bool True if the user can edit the comment, otherwise false
122 function can_pin_comment_array($comment) {
123 return can_pin_comment($comment['ID']);
127 * Check to see if the package name already exists in the database
129 * @param string $name The package name to check
131 * @return string|void Package name if it already exists
133 function pkg_from_name($name="") {
134 if (!$name) {return NULL;}
135 $dbh = DB::connect();
136 $q = "SELECT ID FROM Packages ";
137 $q.= "WHERE Name = " . $dbh->quote($name);
138 $result = $dbh->query($q);
139 if (!$result) {
140 return;
142 $row = $result->fetch(PDO::FETCH_NUM);
143 return $row[0];
147 * Get licenses for a specific package
149 * @param int $pkgid The package to get licenses for
151 * @return array All licenses for the package
153 function pkg_licenses($pkgid) {
154 $lics = array();
155 $pkgid = intval($pkgid);
156 if ($pkgid > 0) {
157 $dbh = DB::connect();
158 $q = "SELECT l.Name FROM Licenses l ";
159 $q.= "INNER JOIN PackageLicenses pl ON pl.LicenseID = l.ID ";
160 $q.= "WHERE pl.PackageID = ". $pkgid;
161 $result = $dbh->query($q);
162 if (!$result) {
163 return array();
165 while ($row = $result->fetch(PDO::FETCH_COLUMN, 0)) {
166 $lics[] = $row;
169 return $lics;
173 * Get package groups for a specific package
175 * @param int $pkgid The package to get groups for
177 * @return array All package groups for the package
179 function pkg_groups($pkgid) {
180 $grps = array();
181 $pkgid = intval($pkgid);
182 if ($pkgid > 0) {
183 $dbh = DB::connect();
184 $q = "SELECT g.Name FROM Groups g ";
185 $q.= "INNER JOIN PackageGroups pg ON pg.GroupID = g.ID ";
186 $q.= "WHERE pg.PackageID = ". $pkgid;
187 $result = $dbh->query($q);
188 if (!$result) {
189 return array();
191 while ($row = $result->fetch(PDO::FETCH_COLUMN, 0)) {
192 $grps[] = $row;
195 return $grps;
199 * Get providers for a specific package
201 * @param string $name The name of the "package" to get providers for
203 * @return array The IDs and names of all providers of the package
205 function pkg_providers($name) {
206 $dbh = DB::connect();
207 $q = "SELECT p.ID, p.Name FROM Packages p ";
208 $q.= "LEFT JOIN PackageRelations pr ON pr.PackageID = p.ID ";
209 $q.= "LEFT JOIN RelationTypes rt ON rt.ID = pr.RelTypeID ";
210 $q.= "WHERE p.Name = " . $dbh->quote($name) . " ";
211 $q.= "OR (rt.Name = 'provides' ";
212 $q.= "AND pr.RelName = " . $dbh->quote($name) . ")";
213 $q.= "UNION ";
214 $q.= "SELECT 0, Name FROM OfficialProviders ";
215 $q.= "WHERE Provides = " . $dbh->quote($name);
216 $result = $dbh->query($q);
218 if (!$result) {
219 return array();
222 $providers = array();
223 while ($row = $result->fetch(PDO::FETCH_NUM)) {
224 $providers[] = $row;
226 return $providers;
230 * Get package dependencies for a specific package
232 * @param int $pkgid The package to get dependencies for
234 * @return array All package dependencies for the package
236 function pkg_dependencies($pkgid) {
237 $deps = array();
238 $pkgid = intval($pkgid);
239 if ($pkgid > 0) {
240 $dbh = DB::connect();
241 $q = "SELECT pd.DepName, dt.Name, pd.DepCondition, pd.DepArch, p.ID FROM PackageDepends pd ";
242 $q.= "LEFT JOIN Packages p ON pd.DepName = p.Name ";
243 $q.= "OR SUBSTRING(pd.DepName FROM 1 FOR POSITION(': ' IN pd.DepName) - 1) = p.Name ";
244 $q.= "LEFT JOIN DependencyTypes dt ON dt.ID = pd.DepTypeID ";
245 $q.= "WHERE pd.PackageID = ". $pkgid . " ";
246 $q.= "ORDER BY pd.DepName";
247 $result = $dbh->query($q);
248 if (!$result) {
249 return array();
251 while ($row = $result->fetch(PDO::FETCH_NUM)) {
252 $deps[] = $row;
255 return $deps;
259 * Get package relations for a specific package
261 * @param int $pkgid The package to get relations for
263 * @return array All package relations for the package
265 function pkg_relations($pkgid) {
266 $rels = array();
267 $pkgid = intval($pkgid);
268 if ($pkgid > 0) {
269 $dbh = DB::connect();
270 $q = "SELECT pr.RelName, rt.Name, pr.RelCondition, pr.RelArch, p.ID FROM PackageRelations pr ";
271 $q.= "LEFT JOIN Packages p ON pr.RelName = p.Name ";
272 $q.= "LEFT JOIN RelationTypes rt ON rt.ID = pr.RelTypeID ";
273 $q.= "WHERE pr.PackageID = ". $pkgid . " ";
274 $q.= "ORDER BY pr.RelName";
275 $result = $dbh->query($q);
276 if (!$result) {
277 return array();
279 while ($row = $result->fetch(PDO::FETCH_NUM)) {
280 $rels[] = $row;
283 return $rels;
287 * Get the HTML code to display a package dependency link annotation
288 * (dependency type, architecture, ...)
290 * @param string $type The name of the dependency type
291 * @param string $arch The package dependency architecture
292 * @param string $desc An optdepends description
294 * @return string The HTML code of the label to display
296 function pkg_deplink_annotation($type, $arch, $desc=false) {
297 if ($type == 'depends' && !$arch) {
298 return '';
301 $link = ' <em>(';
303 if ($type == 'makedepends') {
304 $link .= 'make';
305 } elseif ($type == 'checkdepends') {
306 $link .= 'check';
307 } elseif ($type == 'optdepends') {
308 $link .= 'optional';
311 if ($type != 'depends' && $arch) {
312 $link .= ', ';
315 if ($arch) {
316 $link .= htmlspecialchars($arch);
319 $link .= ')';
320 if ($type == 'optdepends' && $desc) {
321 $link .= ' &ndash; ' . htmlspecialchars($desc) . ' </em>';
323 $link .= '</em>';
325 return $link;
329 * Get the HTML code to display a package provider link
331 * @param string $name The name of the provider
332 * @param bool $official True if the package is in the official repositories
334 * @return string The HTML code of the link to display
336 function pkg_provider_link($name, $official) {
337 $link = '<a href="';
338 if ($official) {
339 $link .= 'https://www.archlinux.org/packages/?q=' .
340 urlencode($name);
341 } else {
342 $link .= htmlspecialchars(get_pkg_uri($name), ENT_QUOTES);
344 $link .= '" title="' . __('View packages details for') . ' ';
345 $link .= htmlspecialchars($name) . '">';
346 $link .= htmlspecialchars($name) . '</a>';
348 return $link;
352 * Get the HTML code to display a package dependency link
354 * @param string $name The name of the dependency
355 * @param string $type The name of the dependency type
356 * @param string $cond The package dependency condition string
357 * @param string $arch The package dependency architecture
358 * @param int $pkg_id The package of the package to display the dependency for
360 * @return string The HTML code of the label to display
362 function pkg_depend_link($name, $type, $cond, $arch, $pkg_id) {
363 if ($type == 'optdepends' && strpos($name, ':') !== false) {
364 $tokens = explode(':', $name, 2);
365 $name = $tokens[0];
366 $desc = $tokens[1];
367 } else {
368 $desc = '';
372 * TODO: We currently perform one SQL query per nonexistent package
373 * dependency. It would be much better if we could annotate dependency
374 * data with providers so that we already know whether a dependency is
375 * a "provision name" or a package from the official repositories at
376 * this point.
378 $providers = pkg_providers($name);
380 if (count($providers) == 0) {
381 $link = '<span class="broken">';
382 $link .= htmlspecialchars($name);
383 $link .= '</span>';
384 $link .= htmlspecialchars($cond) . ' ';
385 $link .= pkg_deplink_annotation($type, $arch, $desc);
386 return $link;
389 $link = htmlspecialchars($name);
390 foreach ($providers as $provider) {
391 if ($provider[1] == $name) {
392 $is_official = ($provider[0] == 0);
393 $name = $provider[1];
394 $link = pkg_provider_link($name, $is_official);
395 break;
398 $link .= htmlspecialchars($cond) . ' ';
400 foreach ($providers as $key => $provider) {
401 if ($provider[1] == $name) {
402 unset($providers[$key]);
406 if (count($providers) > 0) {
407 $link .= '<span class="virtual-dep">(';
408 foreach ($providers as $provider) {
409 $is_official = ($provider[0] == 0);
410 $name = $provider[1];
411 $link .= pkg_provider_link($name, $is_official) . ', ';
413 $link = substr($link, 0, -2);
414 $link .= ')</span>';
417 $link .= pkg_deplink_annotation($type, $arch, $desc);
419 return $link;
423 * Get the HTML code to display a package requirement link
425 * @param string $name The name of the requirement
426 * @param string $depends The (literal) name of the dependency of $name
427 * @param string $type The name of the dependency type
428 * @param string $arch The package dependency architecture
429 * @param string $pkgname The name of dependant package
431 * @return string The HTML code of the link to display
433 function pkg_requiredby_link($name, $depends, $type, $arch, $pkgname) {
434 if ($type == 'optdepends' && strpos($name, ':') !== false) {
435 $tokens = explode(':', $name, 2);
436 $name = $tokens[0];
439 $link = '<a href="';
440 $link .= htmlspecialchars(get_pkg_uri($name), ENT_QUOTES);
441 $link .= '" title="' . __('View packages details for') .' ' . htmlspecialchars($name) . '">';
442 $link .= htmlspecialchars($name) . '</a>';
444 if ($depends != $pkgname) {
445 $depname = $depends;
446 if (strpos($depends, ':') !== false) {
447 $tokens = explode(':', $depname, 2);
448 $depname = $tokens[0];
451 $link .= ' <span class="virtual-dep">(';
452 $link .= __('requires %s', htmlspecialchars($depname));
453 $link .= ')</span>';
456 return $link . pkg_deplink_annotation($type, $arch);
460 * Get the HTML code to display a package relation
462 * @param string $name The name of the relation
463 * @param string $cond The package relation condition string
464 * @param string $arch The package relation architecture
466 * @return string The HTML code of the label to display
468 function pkg_rel_html($name, $cond, $arch) {
469 $html = htmlspecialchars($name) . htmlspecialchars($cond);
471 if ($arch) {
472 $html .= ' <em>(' . htmlspecialchars($arch) . ')</em>';
475 return $html;
479 * Get the HTML code to display a source link
481 * @param string $url The URL of the source
482 * @param string $arch The source architecture
484 * @return string The HTML code of the label to display
486 function pkg_source_link($url, $arch) {
487 $url = explode('::', $url);
488 $parsed_url = parse_url($url[0]);
490 if (isset($parsed_url['scheme']) || isset($url[1])) {
491 $link = '<a href="' . htmlspecialchars((isset($url[1]) ? $url[1] : $url[0]), ENT_QUOTES) . '">' . htmlspecialchars($url[0]) . '</a>';
492 } else {
493 $link = htmlspecialchars($url[0]);
496 if ($arch) {
497 $link .= ' <em>(' . htmlspecialchars($arch) . ')</em>';
500 return $link;
504 * Determine packages that depend on a package
506 * @param string $name The package name for the dependency search
507 * @param array $provides A list of virtual provisions of the package
509 * @return array All packages that depend on the specified package name
511 function pkg_required($name="", $provides) {
512 $deps = array();
513 if ($name != "") {
514 $dbh = DB::connect();
516 $name_list = $dbh->quote($name);
517 foreach ($provides as $p) {
518 $name_list .= ',' . $dbh->quote($p[0]);
521 $q = "SELECT p.Name, pd.DepName, dt.Name, pd.DepArch FROM PackageDepends pd ";
522 $q.= "LEFT JOIN Packages p ON p.ID = pd.PackageID ";
523 $q.= "LEFT JOIN DependencyTypes dt ON dt.ID = pd.DepTypeID ";
524 $q.= "WHERE pd.DepName IN (" . $name_list . ") ";
525 $q.= "OR SUBSTRING(pd.DepName FROM 1 FOR POSITION(': ' IN pd.DepName) - 1) IN (" . $name_list . ") ";
526 $q.= "ORDER BY p.Name";
527 $result = $dbh->query($q);
528 if (!$result) {return array();}
529 while ($row = $result->fetch(PDO::FETCH_NUM)) {
530 $deps[] = $row;
533 return $deps;
537 * Get all package sources for a specific package
539 * @param string $pkgid The package ID to get the sources for
541 * @return array All sources associated with a specific package
543 function pkg_sources($pkgid) {
544 $sources = array();
545 $pkgid = intval($pkgid);
546 if ($pkgid > 0) {
547 $dbh = DB::connect();
548 $q = "SELECT Source, SourceArch FROM PackageSources ";
549 $q.= "WHERE PackageID = " . $pkgid;
550 $q.= " ORDER BY Source";
551 $result = $dbh->query($q);
552 if (!$result) {
553 return array();
555 while ($row = $result->fetch(PDO::FETCH_NUM)) {
556 $sources[] = $row;
559 return $sources;
563 * Get the package details
565 * @param string $id The package ID to get description for
567 * @return array The package's details OR error message
569 function pkg_get_details($id=0) {
570 $dbh = DB::connect();
572 $q = "SELECT Packages.*, PackageBases.ID AS BaseID, ";
573 $q.= "PackageBases.Name AS BaseName, PackageBases.NumVotes, ";
574 $q.= "PackageBases.Popularity, PackageBases.OutOfDateTS, ";
575 $q.= "PackageBases.SubmittedTS, PackageBases.ModifiedTS, ";
576 $q.= "PackageBases.SubmitterUID, PackageBases.MaintainerUID, ";
577 $q.= "PackageBases.PackagerUID, PackageBases.FlaggerUID, ";
578 $q.= "(SELECT COUNT(*) FROM PackageRequests ";
579 $q.= " WHERE PackageRequests.PackageBaseID = Packages.PackageBaseID ";
580 $q.= " AND PackageRequests.Status = 0) AS RequestCount ";
581 $q.= "FROM Packages, PackageBases ";
582 $q.= "WHERE PackageBases.ID = Packages.PackageBaseID ";
583 $q.= "AND Packages.ID = " . intval($id);
584 $result = $dbh->query($q);
586 $row = array();
588 if (!$result) {
589 $row['error'] = __("Error retrieving package details.");
591 else {
592 $row = $result->fetch(PDO::FETCH_ASSOC);
593 if (empty($row)) {
594 $row['error'] = __("Package details could not be found.");
598 return $row;
602 * Display the package details page
604 * @param string $id The package ID to get details page for
605 * @param array $row Package details retrieved by pkg_get_details()
606 * @param string $SID The session ID of the visitor
608 * @return void
610 function pkg_display_details($id=0, $row, $SID="") {
611 $dbh = DB::connect();
613 if (isset($row['error'])) {
614 print "<p>" . $row['error'] . "</p>\n";
616 else {
617 $base_id = pkgbase_from_pkgid($id);
618 $pkgbase_name = pkgbase_name_from_id($base_id);
620 include('pkg_details.php');
622 if ($SID) {
623 include('pkg_comment_box.php');
626 $include_deleted = has_credential(CRED_COMMENT_VIEW_DELETED);
628 $limit_pinned = isset($_GET['pinned']) ? 0 : 5;
629 $pinned = pkgbase_comments($base_id, $limit_pinned, false, true);
630 if (!empty($pinned)) {
631 include('pkg_comments.php');
632 unset($pinned);
635 $limit = isset($_GET['comments']) ? 0 : 10;
636 $comments = pkgbase_comments($base_id, $limit, $include_deleted);
637 if (!empty($comments)) {
638 include('pkg_comments.php');
643 /* pkg_search_page(SID)
644 * outputs the body of search/search results page
646 * parameters:
647 * SID - current Session ID
648 * preconditions:
649 * package search page has been accessed
650 * request variables have not been sanitized
652 * request vars:
653 * O - starting result number
654 * PP - number of search hits per page
655 * K - package search string
656 * SO - search hit sort order:
657 * values: a - ascending
658 * d - descending
659 * SB - sort search hits by:
660 * values: n - package name
661 * v - number of votes
662 * m - maintainer username
663 * SeB- property that search string (K) represents
664 * values: n - package name
665 * nd - package name & description
666 * b - package base name
667 * N - package name (exact match)
668 * B - package base name (exact match)
669 * k - package keyword(s)
670 * m - package maintainer's username
671 * s - package submitter's username
672 * do_Orphans - boolean. whether to search packages
673 * without a maintainer
676 * These two are actually handled in packages.php.
678 * IDs- integer array of ticked packages' IDs
679 * action - action to be taken on ticked packages
680 * values: do_Flag - Flag out-of-date
681 * do_UnFlag - Remove out-of-date flag
682 * do_Adopt - Adopt
683 * do_Disown - Disown
684 * do_Delete - Delete
685 * do_Notify - Enable notification
686 * do_UnNotify - Disable notification
688 function pkg_search_page($SID="") {
689 $dbh = DB::connect();
692 * Get commonly used variables.
693 * TODO: Reduce the number of database queries!
695 if ($SID)
696 $myuid = uid_from_sid($SID);
698 /* Sanitize paging variables. */
699 if (isset($_GET['O'])) {
700 $_GET['O'] = max(intval($_GET['O']), 0);
701 } else {
702 $_GET['O'] = 0;
705 if (isset($_GET["PP"])) {
706 $_GET["PP"] = bound(intval($_GET["PP"]), 50, 250);
707 } else {
708 $_GET["PP"] = 50;
712 * FIXME: Pull out DB-related code. All of it! This one's worth a
713 * choco-chip cookie, one of those nice big soft ones.
716 /* Build the package search query. */
717 $q_select = "SELECT ";
718 if ($SID) {
719 $q_select .= "PackageNotifications.UserID AS Notify,
720 PackageVotes.UsersID AS Voted, ";
722 $q_select .= "Users.Username AS Maintainer,
723 Packages.Name, Packages.Version, Packages.Description,
724 PackageBases.NumVotes, PackageBases.Popularity, Packages.ID,
725 Packages.PackageBaseID, PackageBases.OutOfDateTS ";
727 $q_from = "FROM Packages
728 LEFT JOIN PackageBases ON (PackageBases.ID = Packages.PackageBaseID)
729 LEFT JOIN Users ON (PackageBases.MaintainerUID = Users.ID) ";
730 if ($SID) {
731 /* This is not needed for the total row count query. */
732 $q_from_extra = "LEFT JOIN PackageVotes
733 ON (PackageBases.ID = PackageVotes.PackageBaseID AND PackageVotes.UsersID = $myuid)
734 LEFT JOIN PackageNotifications
735 ON (PackageBases.ID = PackageNotifications.PackageBaseID AND PackageNotifications.UserID = $myuid) ";
736 } else {
737 $q_from_extra = "";
740 $q_where = 'WHERE PackageBases.PackagerUID IS NOT NULL ';
742 if (isset($_GET['K'])) {
743 if (isset($_GET["SeB"]) && $_GET["SeB"] == "m") {
744 /* Search by maintainer. */
745 $q_where .= "AND Users.Username = " . $dbh->quote($_GET['K']) . " ";
747 elseif (isset($_GET["SeB"]) && $_GET["SeB"] == "s") {
748 /* Search by submitter. */
749 $q_where .= "AND SubmitterUID = " . intval(uid_from_username($_GET['K'])) . " ";
751 elseif (isset($_GET["SeB"]) && $_GET["SeB"] == "n") {
752 /* Search by name. */
753 $K = "%" . addcslashes($_GET['K'], '%_') . "%";
754 $q_where .= "AND (Packages.Name LIKE " . $dbh->quote($K) . ") ";
756 elseif (isset($_GET["SeB"]) && $_GET["SeB"] == "b") {
757 /* Search by package base name. */
758 $K = "%" . addcslashes($_GET['K'], '%_') . "%";
759 $q_where .= "AND (PackageBases.Name LIKE " . $dbh->quote($K) . ") ";
761 elseif (isset($_GET["SeB"]) && $_GET["SeB"] == "k") {
762 /* Search by keywords. */
763 $q_where .= construct_keyword_search($dbh, false);
765 elseif (isset($_GET["SeB"]) && $_GET["SeB"] == "N") {
766 /* Search by name (exact match). */
767 $q_where .= "AND (Packages.Name = " . $dbh->quote($_GET['K']) . ") ";
769 elseif (isset($_GET["SeB"]) && $_GET["SeB"] == "B") {
770 /* Search by package base name (exact match). */
771 $q_where .= "AND (PackageBases.Name = " . $dbh->quote($_GET['K']) . ") ";
773 else {
774 /* Keyword search (default). */
775 $q_where .= construct_keyword_search($dbh, true);
779 if (isset($_GET["do_Orphans"])) {
780 $q_where .= "AND MaintainerUID IS NULL ";
783 if (isset($_GET['outdated'])) {
784 if ($_GET['outdated'] == 'on') {
785 $q_where .= "AND OutOfDateTS IS NOT NULL ";
787 elseif ($_GET['outdated'] == 'off') {
788 $q_where .= "AND OutOfDateTS IS NULL ";
792 $order = (isset($_GET["SO"]) && $_GET["SO"] == 'd') ? 'DESC' : 'ASC';
794 $q_sort = "ORDER BY ";
795 $sort_by = isset($_GET["SB"]) ? $_GET["SB"] : '';
796 switch ($sort_by) {
797 case 'v':
798 $q_sort .= "NumVotes " . $order . ", ";
799 break;
800 case 'p':
801 $q_sort .= "Popularity " . $order . ", ";
802 break;
803 case 'w':
804 if ($SID) {
805 $q_sort .= "Voted " . $order . ", ";
807 break;
808 case 'o':
809 if ($SID) {
810 $q_sort .= "Notify " . $order . ", ";
812 break;
813 case 'm':
814 $q_sort .= "Maintainer " . $order . ", ";
815 break;
816 case 'l':
817 $q_sort .= "ModifiedTS " . $order . ", ";
818 break;
819 case 'a':
820 /* For compatibility with old search links. */
821 $q_sort .= "-ModifiedTS " . $order . ", ";
822 break;
823 default:
824 break;
826 $q_sort .= " Packages.Name " . $order . " ";
828 $q_limit = "LIMIT ".$_GET["PP"]." OFFSET ".$_GET["O"];
830 $q = $q_select . $q_from . $q_from_extra . $q_where . $q_sort . $q_limit;
831 $q_total = "SELECT COUNT(*) " . $q_from . $q_where;
833 $result = $dbh->query($q);
834 $result_t = $dbh->query($q_total);
835 if ($result_t) {
836 $row = $result_t->fetch(PDO::FETCH_NUM);
837 $total = $row[0];
839 else {
840 $total = 0;
843 if ($result && $total > 0) {
844 if (isset($_GET["SO"]) && $_GET["SO"] == "d"){
845 $SO_next = "a";
847 else {
848 $SO_next = "d";
852 /* Calculate the results to use. */
853 $first = $_GET['O'] + 1;
855 /* Calculation of pagination links. */
856 $per_page = ($_GET['PP'] > 0) ? $_GET['PP'] : 50;
857 $current = ceil($first / $per_page);
858 $pages = ceil($total / $per_page);
859 $templ_pages = array();
861 if ($current > 1) {
862 $templ_pages['&laquo; ' . __('First')] = 0;
863 $templ_pages['&lsaquo; ' . __('Previous')] = ($current - 2) * $per_page;
866 if ($current - 5 > 1)
867 $templ_pages["..."] = false;
869 for ($i = max($current - 5, 1); $i <= min($pages, $current + 5); $i++) {
870 $templ_pages[$i] = ($i - 1) * $per_page;
873 if ($current + 5 < $pages)
874 $templ_pages["... "] = false;
876 if ($current < $pages) {
877 $templ_pages[__('Next') . ' &rsaquo;'] = $current * $per_page;
878 $templ_pages[__('Last') . ' &raquo;'] = ($pages - 1) * $per_page;
881 include('pkg_search_form.php');
883 $searchresults = array();
884 if ($result) {
885 while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
886 $searchresults[] = $row;
890 include('pkg_search_results.php');
892 return;
896 * Construct the WHERE part of the sophisticated keyword search
898 * @param handle $dbh Database handle
899 * @param boolean $namedesc Search name and description fields
901 * @return string WHERE part of the SQL clause
903 function construct_keyword_search($dbh, $namedesc) {
904 $count = 0;
905 $where_part = "";
906 $q_keywords = "";
907 $op = "";
909 foreach (str_getcsv($_GET['K'], ' ') as $term) {
910 if ($term == "") {
911 continue;
913 if ($count > 0 && strtolower($term) == "and") {
914 $op = "AND ";
915 continue;
917 if ($count > 0 && strtolower($term) == "or") {
918 $op = "OR ";
919 continue;
921 if ($count > 0 && strtolower($term) == "not") {
922 $op .= "NOT ";
923 continue;
926 $term = "%" . addcslashes($term, '%_') . "%";
927 $q_keywords .= $op . " (";
928 if ($namedesc) {
929 $q_keywords .= "Packages.Name LIKE " . $dbh->quote($term) . " OR ";
930 $q_keywords .= "Description LIKE " . $dbh->quote($term) . " OR ";
932 $q_keywords .= "EXISTS (SELECT * FROM PackageKeywords WHERE ";
933 $q_keywords .= "PackageKeywords.PackageBaseID = Packages.PackageBaseID AND ";
934 $q_keywords .= "PackageKeywords.Keyword LIKE " . $dbh->quote($term) . ")) ";
936 $count++;
937 if ($count >= 20) {
938 break;
940 $op = "AND ";
943 if (!empty($q_keywords)) {
944 $where_part = "AND (" . $q_keywords . ") ";
947 return $where_part;
951 * Determine if a POST string has been sent by a visitor
953 * @param string $action String to check has been sent via POST
955 * @return bool True if the POST string was used, otherwise false
957 function current_action($action) {
958 return (isset($_POST['action']) && $_POST['action'] == $action) ||
959 isset($_POST[$action]);
963 * Determine if sent IDs are valid integers
965 * @param array $ids IDs to validate
967 * @return array All sent IDs that are valid integers
969 function sanitize_ids($ids) {
970 $new_ids = array();
971 foreach ($ids as $id) {
972 $id = intval($id);
973 if ($id > 0) {
974 $new_ids[] = $id;
977 return $new_ids;
981 * Determine package information for latest package
983 * @param int $numpkgs Number of packages to get information on
985 * @return array $packages Package info for the specified number of recent packages
987 function latest_pkgs($numpkgs) {
988 $dbh = DB::connect();
990 $q = "SELECT Packages.*, MaintainerUID, SubmittedTS ";
991 $q.= "FROM Packages LEFT JOIN PackageBases ON ";
992 $q.= "PackageBases.ID = Packages.PackageBaseID ";
993 $q.= "ORDER BY SubmittedTS DESC ";
994 $q.= "LIMIT " . intval($numpkgs);
995 $result = $dbh->query($q);
997 $packages = array();
998 if ($result) {
999 while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
1000 $packages[] = $row;
1004 return $packages;