pkgfuncs.inc.php: Squelch PHP warning
[aur.git] / web / lib / pkgfuncs.inc.php
blob66bc249ddadee87aaadf8d1962939a561ddfb69a
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 * Get the package details
485 * @param string $id The package ID to get description for
487 * @return array The package's details OR error message
489 function pkg_get_details($id=0) {
490 $dbh = DB::connect();
492 $q = "SELECT Packages.*, PackageBases.ID AS BaseID, ";
493 $q.= "PackageBases.Name AS BaseName, PackageBases.NumVotes, ";
494 $q.= "PackageBases.Popularity, PackageBases.OutOfDateTS, ";
495 $q.= "PackageBases.SubmittedTS, PackageBases.ModifiedTS, ";
496 $q.= "PackageBases.SubmitterUID, PackageBases.MaintainerUID, ";
497 $q.= "PackageBases.PackagerUID, PackageBases.FlaggerUID, ";
498 $q.= "(SELECT COUNT(*) FROM PackageRequests ";
499 $q.= " WHERE PackageRequests.PackageBaseID = Packages.PackageBaseID ";
500 $q.= " AND PackageRequests.Status = 0) AS RequestCount ";
501 $q.= "FROM Packages, PackageBases ";
502 $q.= "WHERE PackageBases.ID = Packages.PackageBaseID ";
503 $q.= "AND Packages.ID = " . intval($id);
504 $result = $dbh->query($q);
506 $row = array();
508 if (!$result) {
509 $row['error'] = __("Error retrieving package details.");
511 else {
512 $row = $result->fetch(PDO::FETCH_ASSOC);
513 if (empty($row)) {
514 $row['error'] = __("Package details could not be found.");
518 return $row;
522 * Display the package details page
524 * @param string $id The package ID to get details page for
525 * @param array $row Package details retrieved by pkg_get_details()
526 * @param string $SID The session ID of the visitor
528 * @return void
530 function pkg_display_details($id=0, $row, $SID="") {
531 $dbh = DB::connect();
533 if (isset($row['error'])) {
534 print "<p>" . $row['error'] . "</p>\n";
536 else {
537 $base_id = pkgbase_from_pkgid($id);
538 $pkgbase_name = pkgbase_name_from_id($base_id);
540 include('pkg_details.php');
542 if ($SID) {
543 include('pkg_comment_box.php');
546 $limit = isset($_GET['comments']) ? 0 : 10;
547 $include_deleted = has_credential(CRED_COMMENT_VIEW_DELETED);
548 $comments = pkgbase_comments($base_id, $limit, $include_deleted);
549 if (!empty($comments)) {
550 include('pkg_comments.php');
555 /* pkg_search_page(SID)
556 * outputs the body of search/search results page
558 * parameters:
559 * SID - current Session ID
560 * preconditions:
561 * package search page has been accessed
562 * request variables have not been sanitized
564 * request vars:
565 * O - starting result number
566 * PP - number of search hits per page
567 * K - package search string
568 * SO - search hit sort order:
569 * values: a - ascending
570 * d - descending
571 * SB - sort search hits by:
572 * values: n - package name
573 * v - number of votes
574 * m - maintainer username
575 * SeB- property that search string (K) represents
576 * values: n - package name
577 * nd - package name & description
578 * b - package base name
579 * N - package name (exact match)
580 * B - package base name (exact match)
581 * k - package keyword(s)
582 * m - package maintainer's username
583 * s - package submitter's username
584 * do_Orphans - boolean. whether to search packages
585 * without a maintainer
588 * These two are actually handled in packages.php.
590 * IDs- integer array of ticked packages' IDs
591 * action - action to be taken on ticked packages
592 * values: do_Flag - Flag out-of-date
593 * do_UnFlag - Remove out-of-date flag
594 * do_Adopt - Adopt
595 * do_Disown - Disown
596 * do_Delete - Delete
597 * do_Notify - Enable notification
598 * do_UnNotify - Disable notification
600 function pkg_search_page($SID="") {
601 $dbh = DB::connect();
604 * Get commonly used variables.
605 * TODO: Reduce the number of database queries!
607 if ($SID)
608 $myuid = uid_from_sid($SID);
610 /* Sanitize paging variables. */
611 if (isset($_GET['O'])) {
612 $_GET['O'] = max(intval($_GET['O']), 0);
613 } else {
614 $_GET['O'] = 0;
617 if (isset($_GET["PP"])) {
618 $_GET["PP"] = bound(intval($_GET["PP"]), 50, 250);
619 } else {
620 $_GET["PP"] = 50;
624 * FIXME: Pull out DB-related code. All of it! This one's worth a
625 * choco-chip cookie, one of those nice big soft ones.
628 /* Build the package search query. */
629 $q_select = "SELECT ";
630 if ($SID) {
631 $q_select .= "CommentNotify.UserID AS Notify,
632 PackageVotes.UsersID AS Voted, ";
634 $q_select .= "Users.Username AS Maintainer,
635 Packages.Name, Packages.Version, Packages.Description,
636 PackageBases.NumVotes, PackageBases.Popularity, Packages.ID,
637 Packages.PackageBaseID, PackageBases.OutOfDateTS ";
639 $q_from = "FROM Packages
640 LEFT JOIN PackageBases ON (PackageBases.ID = Packages.PackageBaseID)
641 LEFT JOIN Users ON (PackageBases.MaintainerUID = Users.ID) ";
642 if ($SID) {
643 /* This is not needed for the total row count query. */
644 $q_from_extra = "LEFT JOIN PackageVotes
645 ON (PackageBases.ID = PackageVotes.PackageBaseID AND PackageVotes.UsersID = $myuid)
646 LEFT JOIN CommentNotify
647 ON (PackageBases.ID = CommentNotify.PackageBaseID AND CommentNotify.UserID = $myuid) ";
648 } else {
649 $q_from_extra = "";
652 $q_where = 'WHERE PackageBases.PackagerUID IS NOT NULL ';
654 if (isset($_GET['K'])) {
655 if (isset($_GET["SeB"]) && $_GET["SeB"] == "m") {
656 /* Search by maintainer. */
657 $q_where .= "AND Users.Username = " . $dbh->quote($_GET['K']) . " ";
659 elseif (isset($_GET["SeB"]) && $_GET["SeB"] == "s") {
660 /* Search by submitter. */
661 $q_where .= "AND SubmitterUID = " . intval(uid_from_username($_GET['K'])) . " ";
663 elseif (isset($_GET["SeB"]) && $_GET["SeB"] == "n") {
664 /* Search by name. */
665 $K = "%" . addcslashes($_GET['K'], '%_') . "%";
666 $q_where .= "AND (Packages.Name LIKE " . $dbh->quote($K) . ") ";
668 elseif (isset($_GET["SeB"]) && $_GET["SeB"] == "b") {
669 /* Search by package base name. */
670 $K = "%" . addcslashes($_GET['K'], '%_') . "%";
671 $q_where .= "AND (PackageBases.Name LIKE " . $dbh->quote($K) . ") ";
673 elseif (isset($_GET["SeB"]) && $_GET["SeB"] == "k") {
674 /* Search by keywords. */
675 $q_where .= construct_keyword_search($dbh, false);
677 elseif (isset($_GET["SeB"]) && $_GET["SeB"] == "N") {
678 /* Search by name (exact match). */
679 $q_where .= "AND (Packages.Name = " . $dbh->quote($_GET['K']) . ") ";
681 elseif (isset($_GET["SeB"]) && $_GET["SeB"] == "B") {
682 /* Search by package base name (exact match). */
683 $q_where .= "AND (PackageBases.Name = " . $dbh->quote($_GET['K']) . ") ";
685 else {
686 /* Keyword search (default). */
687 $q_where .= construct_keyword_search($dbh, true);
691 if (isset($_GET["do_Orphans"])) {
692 $q_where .= "AND MaintainerUID IS NULL ";
695 if (isset($_GET['outdated'])) {
696 if ($_GET['outdated'] == 'on') {
697 $q_where .= "AND OutOfDateTS IS NOT NULL ";
699 elseif ($_GET['outdated'] == 'off') {
700 $q_where .= "AND OutOfDateTS IS NULL ";
704 $order = (isset($_GET["SO"]) && $_GET["SO"] == 'd') ? 'DESC' : 'ASC';
706 $q_sort = "ORDER BY ";
707 $sort_by = isset($_GET["SB"]) ? $_GET["SB"] : '';
708 switch ($sort_by) {
709 case 'v':
710 $q_sort .= "NumVotes " . $order . ", ";
711 break;
712 case 'p':
713 $q_sort .= "Popularity " . $order . ", ";
714 break;
715 case 'w':
716 if ($SID) {
717 $q_sort .= "Voted " . $order . ", ";
719 break;
720 case 'o':
721 if ($SID) {
722 $q_sort .= "Notify " . $order . ", ";
724 break;
725 case 'm':
726 $q_sort .= "Maintainer " . $order . ", ";
727 break;
728 case 'l':
729 $q_sort .= "ModifiedTS " . $order . ", ";
730 break;
731 case 'a':
732 /* For compatibility with old search links. */
733 $q_sort .= "-ModifiedTS " . $order . ", ";
734 break;
735 default:
736 break;
738 $q_sort .= " Packages.Name " . $order . " ";
740 $q_limit = "LIMIT ".$_GET["PP"]." OFFSET ".$_GET["O"];
742 $q = $q_select . $q_from . $q_from_extra . $q_where . $q_sort . $q_limit;
743 $q_total = "SELECT COUNT(*) " . $q_from . $q_where;
745 $result = $dbh->query($q);
746 $result_t = $dbh->query($q_total);
747 if ($result_t) {
748 $row = $result_t->fetch(PDO::FETCH_NUM);
749 $total = $row[0];
751 else {
752 $total = 0;
755 if ($result && $total > 0) {
756 if (isset($_GET["SO"]) && $_GET["SO"] == "d"){
757 $SO_next = "a";
759 else {
760 $SO_next = "d";
764 /* Calculate the results to use. */
765 $first = $_GET['O'] + 1;
767 /* Calculation of pagination links. */
768 $per_page = ($_GET['PP'] > 0) ? $_GET['PP'] : 50;
769 $current = ceil($first / $per_page);
770 $pages = ceil($total / $per_page);
771 $templ_pages = array();
773 if ($current > 1) {
774 $templ_pages['&laquo; ' . __('First')] = 0;
775 $templ_pages['&lsaquo; ' . __('Previous')] = ($current - 2) * $per_page;
778 if ($current - 5 > 1)
779 $templ_pages["..."] = false;
781 for ($i = max($current - 5, 1); $i <= min($pages, $current + 5); $i++) {
782 $templ_pages[$i] = ($i - 1) * $per_page;
785 if ($current + 5 < $pages)
786 $templ_pages["... "] = false;
788 if ($current < $pages) {
789 $templ_pages[__('Next') . ' &rsaquo;'] = $current * $per_page;
790 $templ_pages[__('Last') . ' &raquo;'] = ($pages - 1) * $per_page;
793 include('pkg_search_form.php');
795 $searchresults = array();
796 if ($result) {
797 while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
798 $searchresults[] = $row;
802 include('pkg_search_results.php');
804 return;
808 * Construct the WHERE part of the sophisticated keyword search
810 * @param handle $dbh Database handle
811 * @param boolean $namedesc Search name and description fields
813 * @return string WHERE part of the SQL clause
815 function construct_keyword_search($dbh, $namedesc) {
816 $count = 0;
817 $where_part = "";
818 $q_keywords = "";
819 $op = "";
821 foreach (str_getcsv($_GET['K'], ' ') as $term) {
822 if ($term == "") {
823 continue;
825 if ($count > 0 && strtolower($term) == "and") {
826 $op = "AND ";
827 continue;
829 if ($count > 0 && strtolower($term) == "or") {
830 $op = "OR ";
831 continue;
833 if ($count > 0 && strtolower($term) == "not") {
834 $op .= "NOT ";
835 continue;
838 $term = "%" . addcslashes($term, '%_') . "%";
839 $q_keywords .= $op . " (";
840 if ($namedesc) {
841 $q_keywords .= "Packages.Name LIKE " . $dbh->quote($term) . " OR ";
842 $q_keywords .= "Description LIKE " . $dbh->quote($term) . " OR ";
844 $q_keywords .= "EXISTS (SELECT * FROM PackageKeywords WHERE ";
845 $q_keywords .= "PackageKeywords.PackageBaseID = Packages.PackageBaseID AND ";
846 $q_keywords .= "PackageKeywords.Keyword LIKE " . $dbh->quote($term) . ")) ";
848 $count++;
849 if ($count >= 20) {
850 break;
852 $op = "AND ";
855 if (!empty($q_keywords)) {
856 $where_part = "AND (" . $q_keywords . ") ";
859 return $where_part;
863 * Determine if a POST string has been sent by a visitor
865 * @param string $action String to check has been sent via POST
867 * @return bool True if the POST string was used, otherwise false
869 function current_action($action) {
870 return (isset($_POST['action']) && $_POST['action'] == $action) ||
871 isset($_POST[$action]);
875 * Determine if sent IDs are valid integers
877 * @param array $ids IDs to validate
879 * @return array All sent IDs that are valid integers
881 function sanitize_ids($ids) {
882 $new_ids = array();
883 foreach ($ids as $id) {
884 $id = intval($id);
885 if ($id > 0) {
886 $new_ids[] = $id;
889 return $new_ids;
893 * Determine package information for latest package
895 * @param int $numpkgs Number of packages to get information on
897 * @return array $packages Package info for the specified number of recent packages
899 function latest_pkgs($numpkgs) {
900 $dbh = DB::connect();
902 $q = "SELECT Packages.*, MaintainerUID, SubmittedTS ";
903 $q.= "FROM Packages LEFT JOIN PackageBases ON ";
904 $q.= "PackageBases.ID = Packages.PackageBaseID ";
905 $q.= "ORDER BY SubmittedTS DESC ";
906 $q.= "LIMIT " . intval($numpkgs);
907 $result = $dbh->query($q);
909 $packages = array();
910 if ($result) {
911 while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
912 $packages[] = $row;
916 return $packages;