Honor virtual provisions in package requirements
[aur.git] / web / lib / pkgfuncs.inc.php
blobb00b22d28018862987c8a90e80306c1a05397619
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 * Check to see if the package name already exists in the database
88 * @param string $name The package name to check
90 * @return string|void Package name if it already exists
92 function pkg_from_name($name="") {
93 if (!$name) {return NULL;}
94 $dbh = DB::connect();
95 $q = "SELECT ID FROM Packages ";
96 $q.= "WHERE Name = " . $dbh->quote($name);
97 $result = $dbh->query($q);
98 if (!$result) {
99 return;
101 $row = $result->fetch(PDO::FETCH_NUM);
102 return $row[0];
106 * Get licenses for a specific package
108 * @param int $pkgid The package to get licenses for
110 * @return array All licenses for the package
112 function pkg_licenses($pkgid) {
113 $lics = array();
114 $pkgid = intval($pkgid);
115 if ($pkgid > 0) {
116 $dbh = DB::connect();
117 $q = "SELECT l.Name FROM Licenses l ";
118 $q.= "INNER JOIN PackageLicenses pl ON pl.LicenseID = l.ID ";
119 $q.= "WHERE pl.PackageID = ". $pkgid;
120 $result = $dbh->query($q);
121 if (!$result) {
122 return array();
124 while ($row = $result->fetch(PDO::FETCH_COLUMN, 0)) {
125 $lics[] = $row;
128 return $lics;
132 * Get package groups for a specific package
134 * @param int $pkgid The package to get groups for
136 * @return array All package groups for the package
138 function pkg_groups($pkgid) {
139 $grps = array();
140 $pkgid = intval($pkgid);
141 if ($pkgid > 0) {
142 $dbh = DB::connect();
143 $q = "SELECT g.Name FROM Groups g ";
144 $q.= "INNER JOIN PackageGroups pg ON pg.GroupID = g.ID ";
145 $q.= "WHERE pg.PackageID = ". $pkgid;
146 $result = $dbh->query($q);
147 if (!$result) {
148 return array();
150 while ($row = $result->fetch(PDO::FETCH_COLUMN, 0)) {
151 $grps[] = $row;
154 return $grps;
158 * Get providers for a specific package
160 * @param string $name The name of the "package" to get providers for
162 * @return array The IDs and names of all providers of the package
164 function pkg_providers($name) {
165 $dbh = DB::connect();
166 $q = "SELECT p.ID, p.Name FROM Packages p ";
167 $q.= "INNER JOIN PackageRelations pr ON pr.PackageID = p.ID ";
168 $q.= "INNER JOIN RelationTypes rt ON rt.ID = pr.RelTypeID ";
169 $q.= "WHERE rt.Name = 'provides' ";
170 $q.= "AND pr.RelName = " . $dbh->quote($name);
171 $result = $dbh->query($q);
173 if (!$result) {
174 return array();
177 $providers = array();
178 while ($row = $result->fetch(PDO::FETCH_NUM)) {
179 $providers[] = $row;
181 return $providers;
185 * Get package dependencies for a specific package
187 * @param int $pkgid The package to get dependencies for
189 * @return array All package dependencies for the package
191 function pkg_dependencies($pkgid) {
192 $deps = array();
193 $pkgid = intval($pkgid);
194 if ($pkgid > 0) {
195 $dbh = DB::connect();
196 $q = "SELECT pd.DepName, dt.Name, pd.DepCondition, pd.DepArch, p.ID FROM PackageDepends pd ";
197 $q.= "LEFT JOIN Packages p ON pd.DepName = p.Name ";
198 $q.= "OR SUBSTRING(pd.DepName FROM 1 FOR POSITION(': ' IN pd.DepName) - 1) = p.Name ";
199 $q.= "LEFT JOIN DependencyTypes dt ON dt.ID = pd.DepTypeID ";
200 $q.= "WHERE pd.PackageID = ". $pkgid . " ";
201 $q.= "ORDER BY pd.DepName";
202 $result = $dbh->query($q);
203 if (!$result) {
204 return array();
206 while ($row = $result->fetch(PDO::FETCH_NUM)) {
207 $deps[] = $row;
210 return $deps;
214 * Get package relations for a specific package
216 * @param int $pkgid The package to get relations for
218 * @return array All package relations for the package
220 function pkg_relations($pkgid) {
221 $rels = array();
222 $pkgid = intval($pkgid);
223 if ($pkgid > 0) {
224 $dbh = DB::connect();
225 $q = "SELECT pr.RelName, rt.Name, pr.RelCondition, pr.RelArch, p.ID FROM PackageRelations pr ";
226 $q.= "LEFT JOIN Packages p ON pr.RelName = p.Name ";
227 $q.= "LEFT JOIN RelationTypes rt ON rt.ID = pr.RelTypeID ";
228 $q.= "WHERE pr.PackageID = ". $pkgid . " ";
229 $q.= "ORDER BY pr.RelName";
230 $result = $dbh->query($q);
231 if (!$result) {
232 return array();
234 while ($row = $result->fetch(PDO::FETCH_NUM)) {
235 $rels[] = $row;
238 return $rels;
242 * Get the HTML code to display a package dependency link annotation
243 * (dependency type, architecture, ...)
245 * @param string $type The name of the dependency type
246 * @param string $arch The package dependency architecture
247 * @param string $desc An optdepends description
249 * @return string The HTML code of the label to display
251 function pkg_deplink_annotation($type, $arch, $desc=false) {
252 if ($type == 'depends' && !$arch) {
253 return '';
256 $link = ' <em>(';
258 if ($type == 'makedepends') {
259 $link .= 'make';
260 } elseif ($type == 'checkdepends') {
261 $link .= 'check';
262 } elseif ($type == 'optdepends') {
263 $link .= 'optional';
266 if ($type != 'depends' && $arch) {
267 $link .= ', ';
270 if ($arch) {
271 $link .= htmlspecialchars($arch);
274 $link .= ')';
275 if ($type == 'optdepends' && $desc) {
276 $link .= ' &ndash; ' . htmlspecialchars($desc) . ' </em>';
278 $link .= '</em>';
280 return $link;
284 * Get the HTML code to display a package dependency link
286 * @param string $name The name of the dependency
287 * @param string $type The name of the dependency type
288 * @param string $cond The package dependency condition string
289 * @param string $arch The package dependency architecture
290 * @param int $pkg_id The package of the package to display the dependency for
292 * @return string The HTML code of the label to display
294 function pkg_depend_link($name, $type, $cond, $arch, $pkg_id) {
295 if ($type == 'optdepends' && strpos($name, ':') !== false) {
296 $tokens = explode(':', $name, 2);
297 $name = $tokens[0];
298 $desc = $tokens[1];
299 } else {
300 $desc = '(unknown)';
303 $providers = array();
304 if (is_null($pkg_id)) {
306 * TODO: We currently perform one SQL query per nonexistent
307 * package dependency. It would be much better if we could
308 * annotate dependency data with providers so that we already
309 * know whether a dependency is a "provision name" or a package
310 * from the official repositories at this point.
312 $providers = pkg_providers($name);
315 if (count($providers) > 0) {
316 $link = htmlspecialchars($name) . ' ';
317 $link .= '<span class="virtual-dep">(';
318 foreach ($providers as $provider) {
319 $name = $provider[1];
320 $link .= '<a href="';
321 $link .= htmlspecialchars(get_pkg_uri($name), ENT_QUOTES);
322 $link .= '" title="' . __('View packages details for') .' ' . htmlspecialchars($name) . '">';
323 $link .= htmlspecialchars($name) . '</a>, ';
325 $link = substr($link, 0, -2);
326 $link .= ')</span>';
327 } else {
328 $link = '<a href="';
329 if (is_null($pkg_id)) {
330 $link .= 'https://www.archlinux.org/packages/?q=' . urlencode($name);
331 } else {
332 $link .= htmlspecialchars(get_pkg_uri($name), ENT_QUOTES);
334 $link .= '" title="' . __('View packages details for') .' ' . htmlspecialchars($name) . '">';
335 $link .= htmlspecialchars($name) . '</a>';
336 $link .= htmlspecialchars($cond);
339 return $link . pkg_deplink_annotation($type, $arch, $desc);
343 * Get the HTML code to display a package requirement link
345 * @param string $name The name of the requirement
346 * @param string $depends The (literal) name of the dependency of $name
347 * @param string $type The name of the dependency type
348 * @param string $arch The package dependency architecture
349 * @param string $pkgname The name of dependant package
351 * @return string The HTML code of the link to display
353 function pkg_requiredby_link($name, $depends, $type, $arch, $pkgname) {
354 if ($type == 'optdepends' && strpos($name, ':') !== false) {
355 $tokens = explode(':', $name, 2);
356 $name = $tokens[0];
359 $link = '<a href="';
360 $link .= htmlspecialchars(get_pkg_uri($name), ENT_QUOTES);
361 $link .= '" title="' . __('View packages details for') .' ' . htmlspecialchars($name) . '">';
362 $link .= htmlspecialchars($name) . '</a>';
364 if ($depends != $pkgname) {
365 $depname = $depends;
366 if (strpos($depends, ':') !== false) {
367 $tokens = explode(':', $depname, 2);
368 $depname = $tokens[0];
371 $link .= ' <span class="virtual-dep">(';
372 $link .= __('requires %s', htmlspecialchars($depname));
373 $link .= ')</span>';
376 return $link . pkg_deplink_annotation($type, $arch);
380 * Get the HTML code to display a package relation
382 * @param string $name The name of the relation
383 * @param string $cond The package relation condition string
384 * @param string $arch The package relation architecture
386 * @return string The HTML code of the label to display
388 function pkg_rel_html($name, $cond, $arch) {
389 $html = htmlspecialchars($name) . htmlspecialchars($cond);
391 if ($arch) {
392 $html .= ' <em>(' . htmlspecialchars($arch) . ')</em>';
395 return $html;
399 * Get the HTML code to display a source link
401 * @param string $url The URL of the source
402 * @param string $arch The source architecture
404 * @return string The HTML code of the label to display
406 function pkg_source_link($url, $arch) {
407 $url = explode('::', $url);
408 $parsed_url = parse_url($url[0]);
410 if (isset($parsed_url['scheme']) || isset($url[1])) {
411 $link = '<a href="' . htmlspecialchars((isset($url[1]) ? $url[1] : $url[0]), ENT_QUOTES) . '">' . htmlspecialchars($url[0]) . '</a>';
412 } else {
413 $link = htmlspecialchars($url[0]);
416 if ($arch) {
417 $link .= ' <em>(' . htmlspecialchars($arch) . ')</em>';
420 return $link;
424 * Determine packages that depend on a package
426 * @param string $name The package name for the dependency search
427 * @param array $provides A list of virtual provisions of the package
429 * @return array All packages that depend on the specified package name
431 function pkg_required($name="", $provides) {
432 $deps = array();
433 if ($name != "") {
434 $dbh = DB::connect();
436 $name_list = $dbh->quote($name);
437 foreach ($provides as $p) {
438 $name_list .= ',' . $dbh->quote($p[0]);
441 $q = "SELECT p.Name, pd.DepName, dt.Name, pd.DepArch FROM PackageDepends pd ";
442 $q.= "LEFT JOIN Packages p ON p.ID = pd.PackageID ";
443 $q.= "LEFT JOIN DependencyTypes dt ON dt.ID = pd.DepTypeID ";
444 $q.= "WHERE pd.DepName IN (" . $name_list . ") ";
445 $q.= "OR SUBSTRING(pd.DepName FROM 1 FOR POSITION(': ' IN pd.DepName) - 1) IN (" . $name_list . ") ";
446 $q.= "ORDER BY p.Name";
447 $result = $dbh->query($q);
448 if (!$result) {return array();}
449 while ($row = $result->fetch(PDO::FETCH_NUM)) {
450 $deps[] = $row;
453 return $deps;
457 * Get all package sources for a specific package
459 * @param string $pkgid The package ID to get the sources for
461 * @return array All sources associated with a specific package
463 function pkg_sources($pkgid) {
464 $sources = array();
465 $pkgid = intval($pkgid);
466 if ($pkgid > 0) {
467 $dbh = DB::connect();
468 $q = "SELECT Source, SourceArch FROM PackageSources ";
469 $q.= "WHERE PackageID = " . $pkgid;
470 $q.= " ORDER BY Source";
471 $result = $dbh->query($q);
472 if (!$result) {
473 return array();
475 while ($row = $result->fetch(PDO::FETCH_NUM)) {
476 $sources[] = $row;
479 return $sources;
483 * Determine package names from package IDs
485 * @param string|array $pkgids The package IDs to get names for
487 * @return array|string All names if multiple package IDs, otherwise package name
489 function pkg_name_from_id($pkgids) {
490 if (is_array($pkgids)) {
491 $pkgids = sanitize_ids($pkgids);
492 $names = array();
493 $dbh = DB::connect();
494 $q = "SELECT Name FROM Packages WHERE ID IN (";
495 $q.= implode(",", $pkgids) . ")";
496 $result = $dbh->query($q);
497 if ($result) {
498 while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
499 $names[] = $row['Name'];
502 return $names;
504 elseif ($pkgids > 0) {
505 $dbh = DB::connect();
506 $q = "SELECT Name FROM Packages WHERE ID = " . $pkgids;
507 $result = $dbh->query($q);
508 if ($result) {
509 $name = $result->fetch(PDO::FETCH_NUM);
511 return $name[0];
513 else {
514 return NULL;
519 * Determine if a package name is on the database blacklist
521 * @param string $name The package name to check
523 * @return bool True if the name is blacklisted, otherwise false
525 function pkg_name_is_blacklisted($name) {
526 $dbh = DB::connect();
527 $q = "SELECT COUNT(*) FROM PackageBlacklist ";
528 $q.= "WHERE Name = " . $dbh->quote($name);
529 $result = $dbh->query($q);
531 if (!$result) return false;
532 return ($result->fetchColumn() > 0);
536 * Get the package details
538 * @param string $id The package ID to get description for
540 * @return array The package's details OR error message
542 function pkg_get_details($id=0) {
543 $dbh = DB::connect();
545 $q = "SELECT Packages.*, PackageBases.ID AS BaseID, ";
546 $q.= "PackageBases.Name AS BaseName, PackageBases.NumVotes, ";
547 $q.= "PackageBases.Popularity, PackageBases.OutOfDateTS, ";
548 $q.= "PackageBases.SubmittedTS, PackageBases.ModifiedTS, ";
549 $q.= "PackageBases.SubmitterUID, PackageBases.MaintainerUID, ";
550 $q.= "PackageBases.PackagerUID, PackageBases.FlaggerUID, ";
551 $q.= "(SELECT COUNT(*) FROM PackageRequests ";
552 $q.= " WHERE PackageRequests.PackageBaseID = Packages.PackageBaseID ";
553 $q.= " AND PackageRequests.Status = 0) AS RequestCount ";
554 $q.= "FROM Packages, PackageBases ";
555 $q.= "WHERE PackageBases.ID = Packages.PackageBaseID ";
556 $q.= "AND Packages.ID = " . intval($id);
557 $result = $dbh->query($q);
559 $row = array();
561 if (!$result) {
562 $row['error'] = __("Error retrieving package details.");
564 else {
565 $row = $result->fetch(PDO::FETCH_ASSOC);
566 if (empty($row)) {
567 $row['error'] = __("Package details could not be found.");
571 return $row;
575 * Display the package details page
577 * @param string $id The package ID to get details page for
578 * @param array $row Package details retrieved by pkg_get_details()
579 * @param string $SID The session ID of the visitor
581 * @return void
583 function pkg_display_details($id=0, $row, $SID="") {
584 $dbh = DB::connect();
586 if (isset($row['error'])) {
587 print "<p>" . $row['error'] . "</p>\n";
589 else {
590 $base_id = pkgbase_from_pkgid($id);
591 $pkgbase_name = pkgbase_name_from_id($base_id);
593 include('pkg_details.php');
595 if ($SID) {
596 include('pkg_comment_box.php');
599 $limit = isset($_GET['comments']) ? 0 : 10;
600 $include_deleted = has_credential(CRED_COMMENT_VIEW_DELETED);
601 $comments = pkgbase_comments($base_id, $limit, $include_deleted);
602 if (!empty($comments)) {
603 include('pkg_comments.php');
608 /* pkg_search_page(SID)
609 * outputs the body of search/search results page
611 * parameters:
612 * SID - current Session ID
613 * preconditions:
614 * package search page has been accessed
615 * request variables have not been sanitized
617 * request vars:
618 * O - starting result number
619 * PP - number of search hits per page
620 * K - package search string
621 * SO - search hit sort order:
622 * values: a - ascending
623 * d - descending
624 * SB - sort search hits by:
625 * values: n - package name
626 * v - number of votes
627 * m - maintainer username
628 * SeB- property that search string (K) represents
629 * values: n - package name
630 * nd - package name & description
631 * x - package name (exact match)
632 * m - package maintainer's username
633 * s - package submitter's username
634 * do_Orphans - boolean. whether to search packages
635 * without a maintainer
638 * These two are actually handled in packages.php.
640 * IDs- integer array of ticked packages' IDs
641 * action - action to be taken on ticked packages
642 * values: do_Flag - Flag out-of-date
643 * do_UnFlag - Remove out-of-date flag
644 * do_Adopt - Adopt
645 * do_Disown - Disown
646 * do_Delete - Delete
647 * do_Notify - Enable notification
648 * do_UnNotify - Disable notification
650 function pkg_search_page($SID="") {
651 $dbh = DB::connect();
654 * Get commonly used variables.
655 * TODO: Reduce the number of database queries!
657 if ($SID)
658 $myuid = uid_from_sid($SID);
660 /* Sanitize paging variables. */
661 if (isset($_GET['O'])) {
662 $_GET['O'] = max(intval($_GET['O']), 0);
663 } else {
664 $_GET['O'] = 0;
667 if (isset($_GET["PP"])) {
668 $_GET["PP"] = bound(intval($_GET["PP"]), 50, 250);
669 } else {
670 $_GET["PP"] = 50;
674 * FIXME: Pull out DB-related code. All of it! This one's worth a
675 * choco-chip cookie, one of those nice big soft ones.
678 /* Build the package search query. */
679 $q_select = "SELECT ";
680 if ($SID) {
681 $q_select .= "CommentNotify.UserID AS Notify,
682 PackageVotes.UsersID AS Voted, ";
684 $q_select .= "Users.Username AS Maintainer,
685 Packages.Name, Packages.Version, Packages.Description,
686 PackageBases.NumVotes, PackageBases.Popularity, Packages.ID,
687 Packages.PackageBaseID, PackageBases.OutOfDateTS ";
689 $q_from = "FROM Packages
690 LEFT JOIN PackageBases ON (PackageBases.ID = Packages.PackageBaseID)
691 LEFT JOIN Users ON (PackageBases.MaintainerUID = Users.ID) ";
692 if ($SID) {
693 /* This is not needed for the total row count query. */
694 $q_from_extra = "LEFT JOIN PackageVotes
695 ON (PackageBases.ID = PackageVotes.PackageBaseID AND PackageVotes.UsersID = $myuid)
696 LEFT JOIN CommentNotify
697 ON (PackageBases.ID = CommentNotify.PackageBaseID AND CommentNotify.UserID = $myuid) ";
698 } else {
699 $q_from_extra = "";
702 $q_where = 'WHERE PackageBases.PackagerUID IS NOT NULL ';
704 if (isset($_GET['K'])) {
705 if (isset($_GET["SeB"]) && $_GET["SeB"] == "m") {
706 /* Search by maintainer. */
707 $q_where .= "AND Users.Username = " . $dbh->quote($_GET['K']) . " ";
709 elseif (isset($_GET["SeB"]) && $_GET["SeB"] == "s") {
710 /* Search by submitter. */
711 $q_where .= "AND SubmitterUID = " . intval(uid_from_username($_GET['K'])) . " ";
713 elseif (isset($_GET["SeB"]) && $_GET["SeB"] == "n") {
714 /* Search by name. */
715 $K = "%" . addcslashes($_GET['K'], '%_') . "%";
716 $q_where .= "AND (Packages.Name LIKE " . $dbh->quote($K) . ") ";
718 elseif (isset($_GET["SeB"]) && $_GET["SeB"] == "b") {
719 /* Search by package base name. */
720 $K = "%" . addcslashes($_GET['K'], '%_') . "%";
721 $q_where .= "AND (PackageBases.Name LIKE " . $dbh->quote($K) . ") ";
723 elseif (isset($_GET["SeB"]) && $_GET["SeB"] == "N") {
724 /* Search by name (exact match). */
725 $q_where .= "AND (Packages.Name = " . $dbh->quote($_GET['K']) . ") ";
727 elseif (isset($_GET["SeB"]) && $_GET["SeB"] == "B") {
728 /* Search by package base name (exact match). */
729 $q_where .= "AND (PackageBases.Name = " . $dbh->quote($_GET['K']) . ") ";
731 else {
732 /* Keyword search (default). */
733 $count = 0;
734 $q_keywords = "";
735 $op = "";
737 foreach (str_getcsv($_GET['K'], ' ') as $term) {
738 if ($term == "") {
739 continue;
741 if ($count > 0 && strtolower($term) == "and") {
742 $op = "AND ";
743 continue;
745 if ($count > 0 && strtolower($term) == "or") {
746 $op = "OR ";
747 continue;
749 if ($count > 0 && strtolower($term) == "not") {
750 $op .= "NOT ";
751 continue;
754 $term = "%" . addcslashes($term, '%_') . "%";
755 $q_keywords .= $op . " (Packages.Name LIKE " . $dbh->quote($term) . " OR ";
756 $q_keywords .= "Description LIKE " . $dbh->quote($term) . " OR ";
757 $q_keywords .= "EXISTS (SELECT * FROM PackageKeywords WHERE ";
758 $q_keywords .= "PackageKeywords.PackageBaseID = Packages.PackageBaseID AND ";
759 $q_keywords .= "PackageKeywords.Keyword LIKE " . $dbh->quote($term) . ")) ";
761 $count++;
762 if ($count >= 20) {
763 break;
765 $op = "AND ";
768 if (!empty($q_keywords)) {
769 $q_where .= "AND (" . $q_keywords . ") ";
774 if (isset($_GET["do_Orphans"])) {
775 $q_where .= "AND MaintainerUID IS NULL ";
778 if (isset($_GET['outdated'])) {
779 if ($_GET['outdated'] == 'on') {
780 $q_where .= "AND OutOfDateTS IS NOT NULL ";
782 elseif ($_GET['outdated'] == 'off') {
783 $q_where .= "AND OutOfDateTS IS NULL ";
787 $order = (isset($_GET["SO"]) && $_GET["SO"] == 'd') ? 'DESC' : 'ASC';
789 $q_sort = "ORDER BY ";
790 $sort_by = isset($_GET["SB"]) ? $_GET["SB"] : '';
791 switch ($sort_by) {
792 case 'v':
793 $q_sort .= "NumVotes " . $order . ", ";
794 break;
795 case 'p':
796 $q_sort .= "Popularity " . $order . ", ";
797 break;
798 case 'w':
799 if ($SID) {
800 $q_sort .= "Voted " . $order . ", ";
802 break;
803 case 'o':
804 if ($SID) {
805 $q_sort .= "Notify " . $order . ", ";
807 break;
808 case 'm':
809 $q_sort .= "Maintainer " . $order . ", ";
810 break;
811 case 'a':
812 $q_sort .= "-ModifiedTS " . $order . ", ";
813 break;
814 default:
815 break;
817 $q_sort .= " Packages.Name " . $order . " ";
819 $q_limit = "LIMIT ".$_GET["PP"]." OFFSET ".$_GET["O"];
821 $q = $q_select . $q_from . $q_from_extra . $q_where . $q_sort . $q_limit;
822 $q_total = "SELECT COUNT(*) " . $q_from . $q_where;
824 $result = $dbh->query($q);
825 $result_t = $dbh->query($q_total);
826 if ($result_t) {
827 $row = $result_t->fetch(PDO::FETCH_NUM);
828 $total = $row[0];
830 else {
831 $total = 0;
834 if ($result && $total > 0) {
835 if (isset($_GET["SO"]) && $_GET["SO"] == "d"){
836 $SO_next = "a";
838 else {
839 $SO_next = "d";
843 /* Calculate the results to use. */
844 $first = $_GET['O'] + 1;
846 /* Calculation of pagination links. */
847 $per_page = ($_GET['PP'] > 0) ? $_GET['PP'] : 50;
848 $current = ceil($first / $per_page);
849 $pages = ceil($total / $per_page);
850 $templ_pages = array();
852 if ($current > 1) {
853 $templ_pages['&laquo; ' . __('First')] = 0;
854 $templ_pages['&lsaquo; ' . __('Previous')] = ($current - 2) * $per_page;
857 if ($current - 5 > 1)
858 $templ_pages["..."] = false;
860 for ($i = max($current - 5, 1); $i <= min($pages, $current + 5); $i++) {
861 $templ_pages[$i] = ($i - 1) * $per_page;
864 if ($current + 5 < $pages)
865 $templ_pages["... "] = false;
867 if ($current < $pages) {
868 $templ_pages[__('Next') . ' &rsaquo;'] = $current * $per_page;
869 $templ_pages[__('Last') . ' &raquo;'] = ($pages - 1) * $per_page;
872 include('pkg_search_form.php');
874 if ($result) {
875 while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
876 $searchresults[] = $row;
880 include('pkg_search_results.php');
882 return;
886 * Determine if a POST string has been sent by a visitor
888 * @param string $action String to check has been sent via POST
890 * @return bool True if the POST string was used, otherwise false
892 function current_action($action) {
893 return (isset($_POST['action']) && $_POST['action'] == $action) ||
894 isset($_POST[$action]);
898 * Determine if sent IDs are valid integers
900 * @param array $ids IDs to validate
902 * @return array All sent IDs that are valid integers
904 function sanitize_ids($ids) {
905 $new_ids = array();
906 foreach ($ids as $id) {
907 $id = intval($id);
908 if ($id > 0) {
909 $new_ids[] = $id;
912 return $new_ids;
916 * Add package information to the database for a specific package
918 * @param int $base_id ID of the package base
919 * @param string $pkgname Name of the new package
920 * @param string $pkgver Version of the new package
921 * @param string $pkgdesc Description of the new package
922 * @param string $pkgurl Upstream URL for the new package
924 * @return int ID of the new package
926 function pkg_create($base_id, $pkgname, $pkgver, $pkgdesc, $pkgurl) {
927 $dbh = DB::connect();
928 $q = sprintf("INSERT INTO Packages (PackageBaseID, Name, Version, " .
929 "Description, URL) VALUES (%d, %s, %s, %s, %s)",
930 $base_id, $dbh->quote($pkgname), $dbh->quote($pkgver),
931 $dbh->quote($pkgdesc), $dbh->quote($pkgurl));
932 $dbh->exec($q);
933 return $dbh->lastInsertId();
937 * Add a dependency for a specific package to the database
939 * @param int $pkgid The package ID to add the dependency for
940 * @param string $type The type of dependency to add
941 * @param string $depname The name of the dependency to add
942 * @param string $depcondition The type of dependency for the package
943 * @param string $deparch The architecture of the dependency to add
945 * @return void
947 function pkg_add_dep($pkgid, $type, $depname, $depcondition, $deparch) {
948 $dbh = DB::connect();
949 $q = sprintf("INSERT INTO PackageDepends (PackageID, DepTypeID, DepName, DepCondition, DepArch) VALUES (%d, %d, %s, %s, %s)",
950 $pkgid,
951 pkg_dependency_type_id_from_name($type),
952 $dbh->quote($depname),
953 $dbh->quote($depcondition),
954 $deparch ? $dbh->quote($deparch) : 'NULL'
956 $dbh->exec($q);
960 * Add a relation for a specific package to the database
962 * @param int $pkgid The package ID to add the relation for
963 * @param string $type The type of relation to add
964 * @param string $relname The name of the relation to add
965 * @param string $relcondition The version requirement of the relation
966 * @param string $relarch The architecture of the relation to add
968 * @return void
970 function pkg_add_rel($pkgid, $type, $relname, $relcondition, $relarch) {
971 $dbh = DB::connect();
972 $q = sprintf("INSERT INTO PackageRelations (PackageID, RelTypeID, RelName, RelCondition, RelArch) VALUES (%d, %d, %s, %s, %s)",
973 $pkgid,
974 pkg_relation_type_id_from_name($type),
975 $dbh->quote($relname),
976 $dbh->quote($relcondition),
977 $relarch ? $dbh->quote($relarch) : 'NULL'
979 $dbh->exec($q);
983 * Add a source for a specific package to the database
985 * @param int $pkgid The package ID to add the source for
986 * @param string $pkgsrc The package source to add to the database
987 * @param string $srcarch The architecture of the source to add
989 * @return void
991 function pkg_add_src($pkgid, $pkgsrc, $srcarch) {
992 $dbh = DB::connect();
993 $q = sprintf("INSERT INTO PackageSources (PackageID, Source, SourceArch) VALUES (%d, %s, %s)",
994 $pkgid,
995 $dbh->quote($pkgsrc),
996 $srcarch ? $dbh->quote($srcarch) : 'NULL'
998 $dbh->exec($q);
1002 * Creates a new group and returns its ID
1004 * If the groups already exists, the ID of the already existing group is
1005 * returned.
1007 * @param string $name The name of the group to create
1009 * @return int The ID of the group
1011 function pkg_create_group($name) {
1012 $dbh = DB::connect();
1013 $q = sprintf("SELECT ID FROM Groups WHERE Name = %s", $dbh->quote($name));
1014 $result = $dbh->query($q);
1015 if ($result) {
1016 $grpid = $result->fetch(PDO::FETCH_COLUMN, 0);
1017 if ($grpid > 0) {
1018 return $grpid;
1022 $q = sprintf("INSERT INTO Groups (Name) VALUES (%s)", $dbh->quote($name));
1023 $dbh->exec($q);
1024 return $dbh->lastInsertId();
1028 * Add a package to a group
1030 * @param int $pkgid The package ID of the package to add
1031 * @param int $grpid The group ID of the group to add the package to
1033 * @return void
1035 function pkg_add_grp($pkgid, $grpid) {
1036 $dbh = DB::connect();
1037 $q = sprintf("INSERT INTO PackageGroups (PackageID, GroupID) VALUES (%d, %d)",
1038 $pkgid,
1039 $grpid
1041 $dbh->exec($q);
1045 * Creates a new license and returns its ID
1047 * If the license already exists, the ID of the already existing license is
1048 * returned.
1050 * @param string $name The name of the license to create
1052 * @return int The ID of the license
1054 function pkg_create_license($name) {
1055 $dbh = DB::connect();
1056 $q = sprintf("SELECT ID FROM Licenses WHERE Name = %s", $dbh->quote($name));
1057 $result = $dbh->query($q);
1058 if ($result) {
1059 $licid = $result->fetch(PDO::FETCH_COLUMN, 0);
1060 if ($licid > 0) {
1061 return $licid;
1065 $q = sprintf("INSERT INTO Licenses (Name) VALUES (%s)", $dbh->quote($name));
1066 $dbh->exec($q);
1067 return $dbh->lastInsertId();
1071 * Add a license to a package
1073 * @param int $pkgid The package ID of the package
1074 * @param int $grpid The ID of the license to add
1076 * @return void
1078 function pkg_add_lic($pkgid, $licid) {
1079 $dbh = DB::connect();
1080 $q = sprintf("INSERT INTO PackageLicenses (PackageID, LicenseID) VALUES (%d, %d)",
1081 $pkgid,
1082 $licid
1084 $dbh->exec($q);
1088 * Determine package information for latest package
1090 * @param int $numpkgs Number of packages to get information on
1092 * @return array $packages Package info for the specified number of recent packages
1094 function latest_pkgs($numpkgs) {
1095 $dbh = DB::connect();
1097 $q = "SELECT Packages.*, MaintainerUID, SubmittedTS ";
1098 $q.= "FROM Packages LEFT JOIN PackageBases ON ";
1099 $q.= "PackageBases.ID = Packages.PackageBaseID ";
1100 $q.= "ORDER BY SubmittedTS DESC ";
1101 $q.= "LIMIT " . intval($numpkgs);
1102 $result = $dbh->query($q);
1104 $packages = array();
1105 if ($result) {
1106 while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
1107 $packages[] = $row;
1111 return $packages;