3 include_once("pkgbasefuncs.inc.php");
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) {
18 $q = "SELECT UsersID FROM PackageComments ";
19 $q.= "WHERE ID = " . intval($comment_id);
20 $result = $dbh->query($q);
26 $uid = $result->fetch(PDO
::FETCH_COLUMN
, 0);
28 return has_credential(CRED_COMMENT_DELETE
, array($uid));
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']));
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) {
58 $q = "SELECT UsersID FROM PackageComments ";
59 $q.= "WHERE ID = " . intval($comment_id);
60 $result = $dbh->query($q);
66 $uid = $result->fetch(PDO
::FETCH_COLUMN
, 0);
68 return has_credential(CRED_COMMENT_EDIT
, array($uid));
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']));
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) {
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);
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);
142 $row = $result->fetch(PDO
::FETCH_NUM
);
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) {
155 $pkgid = intval($pkgid);
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);
165 while ($row = $result->fetch(PDO
::FETCH_COLUMN
, 0)) {
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) {
181 $pkgid = intval($pkgid);
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);
191 while ($row = $result->fetch(PDO
::FETCH_COLUMN
, 0)) {
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) . ")";
214 $q.= "SELECT 0, Name FROM OfficialProviders ";
215 $q.= "WHERE Provides = " . $dbh->quote($name);
216 $result = $dbh->query($q);
222 $providers = array();
223 while ($row = $result->fetch(PDO
::FETCH_NUM
)) {
230 * Get package dependencies for a specific package
232 * @param int $pkgid The package to get dependencies for
233 * @param int $limit An upper bound for the number of packages to retrieve
235 * @return array All package dependencies for the package
237 function pkg_dependencies($pkgid, $limit) {
239 $pkgid = intval($pkgid);
241 $dbh = DB
::connect();
242 $q = "SELECT pd.DepName, dt.Name, pd.DepCondition, pd.DepArch, p.ID FROM PackageDepends pd ";
243 $q.= "LEFT JOIN Packages p ON pd.DepName = p.Name ";
244 $q.= "OR SUBSTRING(pd.DepName FROM 1 FOR POSITION(': ' IN pd.DepName) - 1) = p.Name ";
245 $q.= "LEFT JOIN DependencyTypes dt ON dt.ID = pd.DepTypeID ";
246 $q.= "WHERE pd.PackageID = ". $pkgid . " ";
247 $q.= "ORDER BY pd.DepName LIMIT " . intval($limit);
248 $result = $dbh->query($q);
252 while ($row = $result->fetch(PDO
::FETCH_NUM
)) {
260 * Get package relations for a specific package
262 * @param int $pkgid The package to get relations for
264 * @return array All package relations for the package
266 function pkg_relations($pkgid) {
268 $pkgid = intval($pkgid);
270 $dbh = DB
::connect();
271 $q = "SELECT pr.RelName, rt.Name, pr.RelCondition, pr.RelArch, p.ID FROM PackageRelations pr ";
272 $q.= "LEFT JOIN Packages p ON pr.RelName = p.Name ";
273 $q.= "LEFT JOIN RelationTypes rt ON rt.ID = pr.RelTypeID ";
274 $q.= "WHERE pr.PackageID = ". $pkgid . " ";
275 $q.= "ORDER BY pr.RelName";
276 $result = $dbh->query($q);
280 while ($row = $result->fetch(PDO
::FETCH_NUM
)) {
288 * Get the HTML code to display a package dependency link annotation
289 * (dependency type, architecture, ...)
291 * @param string $type The name of the dependency type
292 * @param string $arch The package dependency architecture
293 * @param string $desc An optdepends description
295 * @return string The HTML code of the label to display
297 function pkg_deplink_annotation($type, $arch, $desc=false) {
298 if ($type == 'depends' && !$arch) {
304 if ($type == 'makedepends') {
306 } elseif ($type == 'checkdepends') {
308 } elseif ($type == 'optdepends') {
312 if ($type != 'depends' && $arch) {
317 $link .= htmlspecialchars($arch);
321 if ($type == 'optdepends' && $desc) {
322 $link .= ' – ' . htmlspecialchars($desc) . ' </em>';
330 * Get the HTML code to display a package provider link
332 * @param string $name The name of the provider
333 * @param bool $official True if the package is in the official repositories
335 * @return string The HTML code of the link to display
337 function pkg_provider_link($name, $official) {
340 $link .= 'https://www.archlinux.org/packages/?q=' .
343 $link .= htmlspecialchars(get_pkg_uri($name), ENT_QUOTES
);
345 $link .= '" title="' . __('View packages details for') . ' ';
346 $link .= htmlspecialchars($name) . '">';
347 $link .= htmlspecialchars($name) . '</a>';
353 * Get the HTML code to display a package dependency link
355 * @param string $name The name of the dependency
356 * @param string $type The name of the dependency type
357 * @param string $cond The package dependency condition string
358 * @param string $arch The package dependency architecture
359 * @param int $pkg_id The package of the package to display the dependency for
361 * @return string The HTML code of the label to display
363 function pkg_depend_link($name, $type, $cond, $arch, $pkg_id) {
364 if ($type == 'optdepends' && strpos($name, ':') !== false) {
365 $tokens = explode(':', $name, 2);
373 * TODO: We currently perform one SQL query per nonexistent package
374 * dependency. It would be much better if we could annotate dependency
375 * data with providers so that we already know whether a dependency is
376 * a "provision name" or a package from the official repositories at
379 $providers = pkg_providers($name);
381 if (count($providers) == 0) {
382 $link = '<span class="broken">';
383 $link .= htmlspecialchars($name);
385 $link .= htmlspecialchars($cond) . ' ';
386 $link .= pkg_deplink_annotation($type, $arch, $desc);
390 $link = htmlspecialchars($name);
391 foreach ($providers as $provider) {
392 if ($provider[1] == $name) {
393 $is_official = ($provider[0] == 0);
394 $name = $provider[1];
395 $link = pkg_provider_link($name, $is_official);
399 $link .= htmlspecialchars($cond) . ' ';
401 foreach ($providers as $key => $provider) {
402 if ($provider[1] == $name) {
403 unset($providers[$key]);
407 if (count($providers) > 0) {
408 $link .= '<span class="virtual-dep">(';
409 foreach ($providers as $provider) {
410 $is_official = ($provider[0] == 0);
411 $name = $provider[1];
412 $link .= pkg_provider_link($name, $is_official) . ', ';
414 $link = substr($link, 0, -2);
418 $link .= pkg_deplink_annotation($type, $arch, $desc);
424 * Get the HTML code to display a package requirement link
426 * @param string $name The name of the requirement
427 * @param string $depends The (literal) name of the dependency of $name
428 * @param string $type The name of the dependency type
429 * @param string $arch The package dependency architecture
430 * @param string $pkgname The name of dependant package
432 * @return string The HTML code of the link to display
434 function pkg_requiredby_link($name, $depends, $type, $arch, $pkgname) {
435 if ($type == 'optdepends' && strpos($name, ':') !== false) {
436 $tokens = explode(':', $name, 2);
441 $link .= htmlspecialchars(get_pkg_uri($name), ENT_QUOTES
);
442 $link .= '" title="' . __('View packages details for') .' ' . htmlspecialchars($name) . '">';
443 $link .= htmlspecialchars($name) . '</a>';
445 if ($depends != $pkgname) {
447 if (strpos($depends, ':') !== false) {
448 $tokens = explode(':', $depname, 2);
449 $depname = $tokens[0];
452 $link .= ' <span class="virtual-dep">(';
453 $link .= __('requires %s', htmlspecialchars($depname));
457 return $link . pkg_deplink_annotation($type, $arch);
461 * Get the HTML code to display a package relation
463 * @param string $name The name of the relation
464 * @param string $cond The package relation condition string
465 * @param string $arch The package relation architecture
467 * @return string The HTML code of the label to display
469 function pkg_rel_html($name, $cond, $arch) {
470 $html = htmlspecialchars($name) . htmlspecialchars($cond);
473 $html .= ' <em>(' . htmlspecialchars($arch) . ')</em>';
480 * Get the HTML code to display a source link
482 * @param string $url The URL of the source
483 * @param string $arch The source architecture
485 * @return string The HTML code of the label to display
487 function pkg_source_link($url, $arch) {
488 $url = explode('::', $url);
489 $parsed_url = parse_url($url[0]);
491 if (isset($parsed_url['scheme']) ||
isset($url[1])) {
492 $link = '<a href="' . htmlspecialchars((isset($url[1]) ?
$url[1] : $url[0]), ENT_QUOTES
) . '">' . htmlspecialchars($url[0]) . '</a>';
494 $link = htmlspecialchars($url[0]);
498 $link .= ' <em>(' . htmlspecialchars($arch) . ')</em>';
505 * Determine packages that depend on a package
507 * @param string $name The package name for the dependency search
508 * @param array $provides A list of virtual provisions of the package
509 * @param int $limit An upper bound for the number of packages to retrieve
511 * @return array All packages that depend on the specified package name
513 function pkg_required($name="", $provides, $limit) {
516 $dbh = DB
::connect();
518 $name_list = $dbh->quote($name);
519 foreach ($provides as $p) {
520 $name_list .= ',' . $dbh->quote($p[0]);
523 $q = "SELECT p.Name, pd.DepName, dt.Name, pd.DepArch FROM PackageDepends pd ";
524 $q.= "LEFT JOIN Packages p ON p.ID = pd.PackageID ";
525 $q.= "LEFT JOIN DependencyTypes dt ON dt.ID = pd.DepTypeID ";
526 $q.= "WHERE pd.DepName IN (" . $name_list . ") ";
527 $q.= "OR SUBSTRING(pd.DepName FROM 1 FOR POSITION(': ' IN pd.DepName) - 1) IN (" . $name_list . ") ";
528 $q.= "ORDER BY p.Name LIMIT " . intval($limit);
529 $result = $dbh->query($q);
530 if (!$result) {return array();}
531 while ($row = $result->fetch(PDO
::FETCH_NUM
)) {
539 * Get all package sources for a specific package
541 * @param string $pkgid The package ID to get the sources for
543 * @return array All sources associated with a specific package
545 function pkg_sources($pkgid) {
547 $pkgid = intval($pkgid);
549 $dbh = DB
::connect();
550 $q = "SELECT Source, SourceArch FROM PackageSources ";
551 $q.= "WHERE PackageID = " . $pkgid;
552 $q.= " ORDER BY Source";
553 $result = $dbh->query($q);
557 while ($row = $result->fetch(PDO
::FETCH_NUM
)) {
565 * Get the package details
567 * @param string $id The package ID to get description for
569 * @return array The package's details OR error message
571 function pkg_get_details($id=0) {
572 $dbh = DB
::connect();
574 $q = "SELECT Packages.*, PackageBases.ID AS BaseID, ";
575 $q.= "PackageBases.Name AS BaseName, PackageBases.NumVotes, ";
576 $q.= "PackageBases.Popularity, PackageBases.OutOfDateTS, ";
577 $q.= "PackageBases.SubmittedTS, PackageBases.ModifiedTS, ";
578 $q.= "PackageBases.SubmitterUID, PackageBases.MaintainerUID, ";
579 $q.= "PackageBases.PackagerUID, PackageBases.FlaggerUID, ";
580 $q.= "(SELECT COUNT(*) FROM PackageRequests ";
581 $q.= " WHERE PackageRequests.PackageBaseID = Packages.PackageBaseID ";
582 $q.= " AND PackageRequests.Status = 0) AS RequestCount ";
583 $q.= "FROM Packages, PackageBases ";
584 $q.= "WHERE PackageBases.ID = Packages.PackageBaseID ";
585 $q.= "AND Packages.ID = " . intval($id);
586 $result = $dbh->query($q);
591 $row['error'] = __("Error retrieving package details.");
594 $row = $result->fetch(PDO
::FETCH_ASSOC
);
596 $row['error'] = __("Package details could not be found.");
604 * Display the package details page
606 * @param string $id The package ID to get details page for
607 * @param array $row Package details retrieved by pkg_get_details()
608 * @param string $SID The session ID of the visitor
612 function pkg_display_details($id=0, $row, $SID="") {
613 $dbh = DB
::connect();
615 if (isset($row['error'])) {
616 print "<p>" . $row['error'] . "</p>\n";
619 $base_id = pkgbase_from_pkgid($id);
620 $pkgbase_name = pkgbase_name_from_id($base_id);
622 include('pkg_details.php');
625 include('pkg_comment_box.php');
628 $include_deleted = has_credential(CRED_COMMENT_VIEW_DELETED
);
630 $limit_pinned = isset($_GET['pinned']) ?
0 : 5;
631 $pinned = pkgbase_comments($base_id, $limit_pinned, false, true);
632 if (!empty($pinned)) {
633 include('pkg_comments.php');
637 $limit = isset($_GET['comments']) ?
0 : 10;
638 $comments = pkgbase_comments($base_id, $limit, $include_deleted);
639 if (!empty($comments)) {
640 include('pkg_comments.php');
645 /* pkg_search_page(SID)
646 * outputs the body of search/search results page
649 * SID - current Session ID
651 * package search page has been accessed
652 * request variables have not been sanitized
655 * O - starting result number
656 * PP - number of search hits per page
657 * K - package search string
658 * SO - search hit sort order:
659 * values: a - ascending
661 * SB - sort search hits by:
662 * values: n - package name
663 * v - number of votes
664 * m - maintainer username
665 * SeB- property that search string (K) represents
666 * values: n - package name
667 * nd - package name & description
668 * b - package base name
669 * N - package name (exact match)
670 * B - package base name (exact match)
671 * k - package keyword(s)
672 * m - package maintainer's username
673 * s - package submitter's username
674 * do_Orphans - boolean. whether to search packages
675 * without a maintainer
678 * These two are actually handled in packages.php.
680 * IDs- integer array of ticked packages' IDs
681 * action - action to be taken on ticked packages
682 * values: do_Flag - Flag out-of-date
683 * do_UnFlag - Remove out-of-date flag
687 * do_Notify - Enable notification
688 * do_UnNotify - Disable notification
690 function pkg_search_page($SID="") {
691 $dbh = DB
::connect();
694 * Get commonly used variables.
695 * TODO: Reduce the number of database queries!
698 $myuid = uid_from_sid($SID);
700 /* Sanitize paging variables. */
701 if (isset($_GET['O'])) {
702 $_GET['O'] = max(intval($_GET['O']), 0);
707 if (isset($_GET["PP"])) {
708 $_GET["PP"] = bound(intval($_GET["PP"]), 50, 250);
714 * FIXME: Pull out DB-related code. All of it! This one's worth a
715 * choco-chip cookie, one of those nice big soft ones.
718 /* Build the package search query. */
719 $q_select = "SELECT ";
721 $q_select .= "PackageNotifications.UserID AS Notify,
722 PackageVotes.UsersID AS Voted, ";
724 $q_select .= "Users.Username AS Maintainer,
725 Packages.Name, Packages.Version, Packages.Description,
726 PackageBases.NumVotes, PackageBases.Popularity, Packages.ID,
727 Packages.PackageBaseID, PackageBases.OutOfDateTS ";
729 $q_from = "FROM Packages
730 LEFT JOIN PackageBases ON (PackageBases.ID = Packages.PackageBaseID)
731 LEFT JOIN Users ON (PackageBases.MaintainerUID = Users.ID) ";
733 /* This is not needed for the total row count query. */
734 $q_from_extra = "LEFT JOIN PackageVotes
735 ON (PackageBases.ID = PackageVotes.PackageBaseID AND PackageVotes.UsersID = $myuid)
736 LEFT JOIN PackageNotifications
737 ON (PackageBases.ID = PackageNotifications.PackageBaseID AND PackageNotifications.UserID = $myuid) ";
742 $q_where = 'WHERE PackageBases.PackagerUID IS NOT NULL ';
744 if (isset($_GET['K'])) {
745 if (isset($_GET["SeB"]) && $_GET["SeB"] == "m") {
746 /* Search by maintainer. */
747 $q_where .= "AND Users.Username = " . $dbh->quote($_GET['K']) . " ";
749 elseif (isset($_GET["SeB"]) && $_GET["SeB"] == "s") {
750 /* Search by submitter. */
751 $q_where .= "AND SubmitterUID = " . intval(uid_from_username($_GET['K'])) . " ";
753 elseif (isset($_GET["SeB"]) && $_GET["SeB"] == "n") {
754 /* Search by name. */
755 $K = "%" . addcslashes($_GET['K'], '%_') . "%";
756 $q_where .= "AND (Packages.Name LIKE " . $dbh->quote($K) . ") ";
758 elseif (isset($_GET["SeB"]) && $_GET["SeB"] == "b") {
759 /* Search by package base name. */
760 $K = "%" . addcslashes($_GET['K'], '%_') . "%";
761 $q_where .= "AND (PackageBases.Name LIKE " . $dbh->quote($K) . ") ";
763 elseif (isset($_GET["SeB"]) && $_GET["SeB"] == "k") {
764 /* Search by keywords. */
765 $q_where .= construct_keyword_search($dbh, false);
767 elseif (isset($_GET["SeB"]) && $_GET["SeB"] == "N") {
768 /* Search by name (exact match). */
769 $q_where .= "AND (Packages.Name = " . $dbh->quote($_GET['K']) . ") ";
771 elseif (isset($_GET["SeB"]) && $_GET["SeB"] == "B") {
772 /* Search by package base name (exact match). */
773 $q_where .= "AND (PackageBases.Name = " . $dbh->quote($_GET['K']) . ") ";
776 /* Keyword search (default). */
777 $q_where .= construct_keyword_search($dbh, true);
781 if (isset($_GET["do_Orphans"])) {
782 $q_where .= "AND MaintainerUID IS NULL ";
785 if (isset($_GET['outdated'])) {
786 if ($_GET['outdated'] == 'on') {
787 $q_where .= "AND OutOfDateTS IS NOT NULL ";
789 elseif ($_GET['outdated'] == 'off') {
790 $q_where .= "AND OutOfDateTS IS NULL ";
794 $order = (isset($_GET["SO"]) && $_GET["SO"] == 'd') ?
'DESC' : 'ASC';
796 $q_sort = "ORDER BY ";
797 $sort_by = isset($_GET["SB"]) ?
$_GET["SB"] : '';
800 $q_sort .= "NumVotes " . $order . ", ";
803 $q_sort .= "Popularity " . $order . ", ";
807 $q_sort .= "Voted " . $order . ", ";
812 $q_sort .= "Notify " . $order . ", ";
816 $q_sort .= "Maintainer " . $order . ", ";
819 $q_sort .= "ModifiedTS " . $order . ", ";
822 /* For compatibility with old search links. */
823 $q_sort .= "-ModifiedTS " . $order . ", ";
828 $q_sort .= " Packages.Name " . $order . " ";
830 $q_limit = "LIMIT ".$_GET["PP"]." OFFSET ".$_GET["O"];
832 $q = $q_select . $q_from . $q_from_extra . $q_where . $q_sort . $q_limit;
833 $q_total = "SELECT COUNT(*) " . $q_from . $q_where;
835 $result = $dbh->query($q);
836 $result_t = $dbh->query($q_total);
838 $row = $result_t->fetch(PDO
::FETCH_NUM
);
845 if ($result && $total > 0) {
846 if (isset($_GET["SO"]) && $_GET["SO"] == "d"){
854 /* Calculate the results to use. */
855 $first = $_GET['O'] +
1;
857 /* Calculation of pagination links. */
858 $per_page = ($_GET['PP'] > 0) ?
$_GET['PP'] : 50;
859 $current = ceil($first / $per_page);
860 $pages = ceil($total / $per_page);
861 $templ_pages = array();
864 $templ_pages['« ' . __('First')] = 0;
865 $templ_pages['‹ ' . __('Previous')] = ($current - 2) * $per_page;
868 if ($current - 5 > 1)
869 $templ_pages["..."] = false;
871 for ($i = max($current - 5, 1); $i <= min($pages, $current +
5); $i++
) {
872 $templ_pages[$i] = ($i - 1) * $per_page;
875 if ($current +
5 < $pages)
876 $templ_pages["... "] = false;
878 if ($current < $pages) {
879 $templ_pages[__('Next') . ' ›'] = $current * $per_page;
880 $templ_pages[__('Last') . ' »'] = ($pages - 1) * $per_page;
883 include('pkg_search_form.php');
885 $searchresults = array();
887 while ($row = $result->fetch(PDO
::FETCH_ASSOC
)) {
888 $searchresults[] = $row;
892 include('pkg_search_results.php');
898 * Construct the WHERE part of the sophisticated keyword search
900 * @param handle $dbh Database handle
901 * @param boolean $namedesc Search name and description fields
903 * @return string WHERE part of the SQL clause
905 function construct_keyword_search($dbh, $namedesc) {
911 foreach (str_getcsv($_GET['K'], ' ') as $term) {
915 if ($count > 0 && strtolower($term) == "and") {
919 if ($count > 0 && strtolower($term) == "or") {
923 if ($count > 0 && strtolower($term) == "not") {
928 $term = "%" . addcslashes($term, '%_') . "%";
929 $q_keywords .= $op . " (";
931 $q_keywords .= "Packages.Name LIKE " . $dbh->quote($term) . " OR ";
932 $q_keywords .= "Description LIKE " . $dbh->quote($term) . " OR ";
934 $q_keywords .= "EXISTS (SELECT * FROM PackageKeywords WHERE ";
935 $q_keywords .= "PackageKeywords.PackageBaseID = Packages.PackageBaseID AND ";
936 $q_keywords .= "PackageKeywords.Keyword LIKE " . $dbh->quote($term) . ")) ";
945 if (!empty($q_keywords)) {
946 $where_part = "AND (" . $q_keywords . ") ";
953 * Determine if a POST string has been sent by a visitor
955 * @param string $action String to check has been sent via POST
957 * @return bool True if the POST string was used, otherwise false
959 function current_action($action) {
960 return (isset($_POST['action']) && $_POST['action'] == $action) ||
961 isset($_POST[$action]);
965 * Determine if sent IDs are valid integers
967 * @param array $ids IDs to validate
969 * @return array All sent IDs that are valid integers
971 function sanitize_ids($ids) {
973 foreach ($ids as $id) {
983 * Determine package information for latest package
985 * @param int $numpkgs Number of packages to get information on
987 * @return array $packages Package info for the specified number of recent packages
989 function latest_pkgs($numpkgs) {
990 $dbh = DB
::connect();
992 $q = "SELECT Packages.*, MaintainerUID, SubmittedTS ";
993 $q.= "FROM Packages LEFT JOIN PackageBases ON ";
994 $q.= "PackageBases.ID = Packages.PackageBaseID ";
995 $q.= "ORDER BY SubmittedTS DESC ";
996 $q.= "LIMIT " . intval($numpkgs);
997 $result = $dbh->query($q);
1001 while ($row = $result->fetch(PDO
::FETCH_ASSOC
)) {