Add direct links to each source file
[aur.git] / web / lib / pkgfuncs.inc.php
blobadb21f66447cd189bee79d66e73336fe3c70dc43
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
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) {
238 $deps = array();
239 $pkgid = intval($pkgid);
240 if ($pkgid > 0) {
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);
249 if (!$result) {
250 return array();
252 while ($row = $result->fetch(PDO::FETCH_NUM)) {
253 $deps[] = $row;
256 return $deps;
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) {
267 $rels = array();
268 $pkgid = intval($pkgid);
269 if ($pkgid > 0) {
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);
277 if (!$result) {
278 return array();
280 while ($row = $result->fetch(PDO::FETCH_NUM)) {
281 $rels[] = $row;
284 return $rels;
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) {
299 return '';
302 $link = ' <em>(';
304 if ($type == 'makedepends') {
305 $link .= 'make';
306 } elseif ($type == 'checkdepends') {
307 $link .= 'check';
308 } elseif ($type == 'optdepends') {
309 $link .= 'optional';
312 if ($type != 'depends' && $arch) {
313 $link .= ', ';
316 if ($arch) {
317 $link .= htmlspecialchars($arch);
320 $link .= ')';
321 if ($type == 'optdepends' && $desc) {
322 $link .= ' &ndash; ' . htmlspecialchars($desc) . ' </em>';
324 $link .= '</em>';
326 return $link;
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) {
338 $link = '<a href="';
339 if ($official) {
340 $link .= 'https://www.archlinux.org/packages/?q=' .
341 urlencode($name);
342 } else {
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>';
349 return $link;
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);
366 $name = $tokens[0];
367 $desc = $tokens[1];
368 } else {
369 $desc = '';
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
377 * this point.
379 $providers = pkg_providers($name);
381 if (count($providers) == 0) {
382 $link = '<span class="broken">';
383 $link .= htmlspecialchars($name);
384 $link .= '</span>';
385 $link .= htmlspecialchars($cond) . ' ';
386 $link .= pkg_deplink_annotation($type, $arch, $desc);
387 return $link;
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);
396 break;
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);
415 $link .= ')</span>';
418 $link .= pkg_deplink_annotation($type, $arch, $desc);
420 return $link;
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);
437 $name = $tokens[0];
440 $link = '<a href="';
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) {
446 $depname = $depends;
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));
454 $link .= ')</span>';
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);
472 if ($arch) {
473 $html .= ' <em>(' . htmlspecialchars($arch) . ')</em>';
476 return $html;
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
484 * @param string $package The name of the package
486 * @return string The HTML code of the label to display
488 function pkg_source_link($url, $arch, $package) {
489 $url = explode('::', $url);
490 $parsed_url = parse_url($url[0]);
492 if (isset($parsed_url['scheme']) || isset($url[1])) {
493 $link = '<a href="' . htmlspecialchars((isset($url[1]) ? $url[1] : $url[0]), ENT_QUOTES) . '">' . htmlspecialchars($url[0]) . '</a>';
494 } else {
495 $file_url = sprintf(config_get('options', 'source_file_uri'), htmlspecialchars($url[0]), $package);
496 $link = '<a href="' . $file_url . '">' . htmlspecialchars($url[0]) . '</a>';
499 if ($arch) {
500 $link .= ' <em>(' . htmlspecialchars($arch) . ')</em>';
503 return $link;
507 * Determine packages that depend on a package
509 * @param string $name The package name for the dependency search
510 * @param array $provides A list of virtual provisions of the package
511 * @param int $limit An upper bound for the number of packages to retrieve
513 * @return array All packages that depend on the specified package name
515 function pkg_required($name="", $provides, $limit) {
516 $deps = array();
517 if ($name != "") {
518 $dbh = DB::connect();
520 $name_list = $dbh->quote($name);
521 foreach ($provides as $p) {
522 $name_list .= ',' . $dbh->quote($p[0]);
525 $q = "SELECT p.Name, pd.DepName, dt.Name, pd.DepArch FROM PackageDepends pd ";
526 $q.= "LEFT JOIN Packages p ON p.ID = pd.PackageID ";
527 $q.= "LEFT JOIN DependencyTypes dt ON dt.ID = pd.DepTypeID ";
528 $q.= "WHERE pd.DepName IN (" . $name_list . ") ";
529 $q.= "OR SUBSTRING(pd.DepName FROM 1 FOR POSITION(': ' IN pd.DepName) - 1) IN (" . $name_list . ") ";
530 $q.= "ORDER BY p.Name LIMIT " . intval($limit);
531 $result = $dbh->query($q);
532 if (!$result) {return array();}
533 while ($row = $result->fetch(PDO::FETCH_NUM)) {
534 $deps[] = $row;
537 return $deps;
541 * Get all package sources for a specific package
543 * @param string $pkgid The package ID to get the sources for
545 * @return array All sources associated with a specific package
547 function pkg_sources($pkgid) {
548 $sources = array();
549 $pkgid = intval($pkgid);
550 if ($pkgid > 0) {
551 $dbh = DB::connect();
552 $q = "SELECT Source, SourceArch FROM PackageSources ";
553 $q.= "WHERE PackageID = " . $pkgid;
554 $q.= " ORDER BY Source";
555 $result = $dbh->query($q);
556 if (!$result) {
557 return array();
559 while ($row = $result->fetch(PDO::FETCH_NUM)) {
560 $sources[] = $row;
563 return $sources;
567 * Get the package details
569 * @param string $id The package ID to get description for
571 * @return array The package's details OR error message
573 function pkg_get_details($id=0) {
574 $dbh = DB::connect();
576 $q = "SELECT Packages.*, PackageBases.ID AS BaseID, ";
577 $q.= "PackageBases.Name AS BaseName, PackageBases.NumVotes, ";
578 $q.= "PackageBases.Popularity, PackageBases.OutOfDateTS, ";
579 $q.= "PackageBases.SubmittedTS, PackageBases.ModifiedTS, ";
580 $q.= "PackageBases.SubmitterUID, PackageBases.MaintainerUID, ";
581 $q.= "PackageBases.PackagerUID, PackageBases.FlaggerUID, ";
582 $q.= "(SELECT COUNT(*) FROM PackageRequests ";
583 $q.= " WHERE PackageRequests.PackageBaseID = Packages.PackageBaseID ";
584 $q.= " AND PackageRequests.Status = 0) AS RequestCount ";
585 $q.= "FROM Packages, PackageBases ";
586 $q.= "WHERE PackageBases.ID = Packages.PackageBaseID ";
587 $q.= "AND Packages.ID = " . intval($id);
588 $result = $dbh->query($q);
590 $row = array();
592 if (!$result) {
593 $row['error'] = __("Error retrieving package details.");
595 else {
596 $row = $result->fetch(PDO::FETCH_ASSOC);
597 if (empty($row)) {
598 $row['error'] = __("Package details could not be found.");
602 return $row;
606 * Display the package details page
608 * @param string $id The package ID to get details page for
609 * @param array $row Package details retrieved by pkg_get_details()
610 * @param string $SID The session ID of the visitor
612 * @return void
614 function pkg_display_details($id=0, $row, $SID="") {
615 $dbh = DB::connect();
617 if (isset($row['error'])) {
618 print "<p>" . $row['error'] . "</p>\n";
620 else {
621 $base_id = pkgbase_from_pkgid($id);
622 $pkgbase_name = pkgbase_name_from_id($base_id);
624 include('pkg_details.php');
626 if ($SID) {
627 include('pkg_comment_box.php');
630 $include_deleted = has_credential(CRED_COMMENT_VIEW_DELETED);
632 $limit_pinned = isset($_GET['pinned']) ? 0 : 5;
633 $pinned = pkgbase_comments($base_id, $limit_pinned, false, true);
634 if (!empty($pinned)) {
635 include('pkg_comments.php');
637 unset($pinned);
639 $limit = isset($_GET['comments']) ? 0 : 10;
640 $comments = pkgbase_comments($base_id, $limit, $include_deleted);
641 if (!empty($comments)) {
642 include('pkg_comments.php');
648 * Output the body of the search results page
650 * @param array $params Search parameters
651 * @param bool $show_headers True if statistics should be included
652 * @param string $SID The session ID of the visitor
654 * @return int The total number of packages matching the query
656 function pkg_search_page($params, $show_headers=true, $SID="") {
657 $dbh = DB::connect();
660 * Get commonly used variables.
661 * TODO: Reduce the number of database queries!
663 if ($SID)
664 $myuid = uid_from_sid($SID);
666 /* Sanitize paging variables. */
667 if (isset($params['O'])) {
668 $params['O'] = max(intval($params['O']), 0);
669 } else {
670 $params['O'] = 0;
673 if (isset($params["PP"])) {
674 $params["PP"] = bound(intval($params["PP"]), 50, 250);
675 } else {
676 $params["PP"] = 50;
680 * FIXME: Pull out DB-related code. All of it! This one's worth a
681 * choco-chip cookie, one of those nice big soft ones.
684 /* Build the package search query. */
685 $q_select = "SELECT ";
686 if ($SID) {
687 $q_select .= "PackageNotifications.UserID AS Notify,
688 PackageVotes.UsersID AS Voted, ";
690 $q_select .= "Users.Username AS Maintainer,
691 Packages.Name, Packages.Version, Packages.Description,
692 PackageBases.NumVotes, PackageBases.Popularity, Packages.ID,
693 Packages.PackageBaseID, PackageBases.OutOfDateTS ";
695 $q_from = "FROM Packages
696 LEFT JOIN PackageBases ON (PackageBases.ID = Packages.PackageBaseID)
697 LEFT JOIN Users ON (PackageBases.MaintainerUID = Users.ID) ";
698 if ($SID) {
699 /* This is not needed for the total row count query. */
700 $q_from_extra = "LEFT JOIN PackageVotes
701 ON (PackageBases.ID = PackageVotes.PackageBaseID AND PackageVotes.UsersID = $myuid)
702 LEFT JOIN PackageNotifications
703 ON (PackageBases.ID = PackageNotifications.PackageBaseID AND PackageNotifications.UserID = $myuid) ";
704 } else {
705 $q_from_extra = "";
708 $q_where = 'WHERE PackageBases.PackagerUID IS NOT NULL ';
710 if (isset($params['K'])) {
711 if (isset($params["SeB"]) && $params["SeB"] == "m") {
712 /* Search by maintainer. */
713 $q_where .= "AND Users.Username = " . $dbh->quote($params['K']) . " ";
715 elseif (isset($params["SeB"]) && $params["SeB"] == "c") {
716 /* Search by co-maintainer. */
717 $q_where .= "AND EXISTS (SELECT * FROM PackageComaintainers ";
718 $q_where .= "INNER JOIN Users ON Users.ID = PackageComaintainers.UsersID ";
719 $q_where .= "WHERE PackageComaintainers.PackageBaseID = PackageBases.ID ";
720 $q_where .= "AND Users.Username = " . $dbh->quote($params['K']) . ")";
722 elseif (isset($params["SeB"]) && $params["SeB"] == "M") {
723 /* Search by maintainer and co-maintainer. */
724 $q_where .= "AND (Users.Username = " . $dbh->quote($params['K']) . " ";
725 $q_where .= "OR EXISTS (SELECT * FROM PackageComaintainers ";
726 $q_where .= "INNER JOIN Users ON Users.ID = PackageComaintainers.UsersID ";
727 $q_where .= "WHERE PackageComaintainers.PackageBaseID = PackageBases.ID ";
728 $q_where .= "AND Users.Username = " . $dbh->quote($params['K']) . "))";
730 elseif (isset($params["SeB"]) && $params["SeB"] == "s") {
731 /* Search by submitter. */
732 $q_where .= "AND SubmitterUID = " . intval(uid_from_username($params['K'])) . " ";
734 elseif (isset($params["SeB"]) && $params["SeB"] == "n") {
735 /* Search by name. */
736 $K = "%" . addcslashes($params['K'], '%_') . "%";
737 $q_where .= "AND (Packages.Name LIKE " . $dbh->quote($K) . ") ";
739 elseif (isset($params["SeB"]) && $params["SeB"] == "b") {
740 /* Search by package base name. */
741 $K = "%" . addcslashes($params['K'], '%_') . "%";
742 $q_where .= "AND (PackageBases.Name LIKE " . $dbh->quote($K) . ") ";
744 elseif (isset($params["SeB"]) && $params["SeB"] == "k") {
745 /* Search by keywords. */
746 $q_where .= construct_keyword_search($dbh, $params['K'], false);
748 elseif (isset($params["SeB"]) && $params["SeB"] == "N") {
749 /* Search by name (exact match). */
750 $q_where .= "AND (Packages.Name = " . $dbh->quote($params['K']) . ") ";
752 elseif (isset($params["SeB"]) && $params["SeB"] == "B") {
753 /* Search by package base name (exact match). */
754 $q_where .= "AND (PackageBases.Name = " . $dbh->quote($params['K']) . ") ";
756 else {
757 /* Keyword search (default). */
758 $q_where .= construct_keyword_search($dbh, $params['K'], true);
762 if (isset($params["do_Orphans"])) {
763 $q_where .= "AND MaintainerUID IS NULL ";
766 if (isset($params['outdated'])) {
767 if ($params['outdated'] == 'on') {
768 $q_where .= "AND OutOfDateTS IS NOT NULL ";
770 elseif ($params['outdated'] == 'off') {
771 $q_where .= "AND OutOfDateTS IS NULL ";
775 $order = (isset($params["SO"]) && $params["SO"] == 'd') ? 'DESC' : 'ASC';
777 $q_sort = "ORDER BY ";
778 $sort_by = isset($params["SB"]) ? $params["SB"] : '';
779 switch ($sort_by) {
780 case 'v':
781 $q_sort .= "NumVotes " . $order . ", ";
782 break;
783 case 'p':
784 $q_sort .= "Popularity " . $order . ", ";
785 break;
786 case 'w':
787 if ($SID) {
788 $q_sort .= "Voted " . $order . ", ";
790 break;
791 case 'o':
792 if ($SID) {
793 $q_sort .= "Notify " . $order . ", ";
795 break;
796 case 'm':
797 $q_sort .= "Maintainer " . $order . ", ";
798 break;
799 case 'l':
800 $q_sort .= "ModifiedTS " . $order . ", ";
801 break;
802 case 'a':
803 /* For compatibility with old search links. */
804 $q_sort .= "-ModifiedTS " . $order . ", ";
805 break;
806 default:
807 break;
809 $q_sort .= " Packages.Name " . $order . " ";
811 $q_limit = "LIMIT ".$params["PP"]." OFFSET ".$params["O"];
813 $q = $q_select . $q_from . $q_from_extra . $q_where . $q_sort . $q_limit;
814 $q_total = "SELECT COUNT(*) " . $q_from . $q_where;
816 $result = $dbh->query($q);
817 $result_t = $dbh->query($q_total);
818 if ($result_t) {
819 $row = $result_t->fetch(PDO::FETCH_NUM);
820 $total = $row[0];
822 else {
823 $total = 0;
826 if ($result && $total > 0) {
827 if (isset($params["SO"]) && $params["SO"] == "d"){
828 $SO_next = "a";
830 else {
831 $SO_next = "d";
835 /* Calculate the results to use. */
836 $first = $params['O'] + 1;
838 /* Calculation of pagination links. */
839 $per_page = ($params['PP'] > 0) ? $params['PP'] : 50;
840 $current = ceil($first / $per_page);
841 $pages = ceil($total / $per_page);
842 $templ_pages = array();
844 if ($current > 1) {
845 $templ_pages['&laquo; ' . __('First')] = 0;
846 $templ_pages['&lsaquo; ' . __('Previous')] = ($current - 2) * $per_page;
849 if ($current - 5 > 1)
850 $templ_pages["..."] = false;
852 for ($i = max($current - 5, 1); $i <= min($pages, $current + 5); $i++) {
853 $templ_pages[$i] = ($i - 1) * $per_page;
856 if ($current + 5 < $pages)
857 $templ_pages["... "] = false;
859 if ($current < $pages) {
860 $templ_pages[__('Next') . ' &rsaquo;'] = $current * $per_page;
861 $templ_pages[__('Last') . ' &raquo;'] = ($pages - 1) * $per_page;
864 $searchresults = array();
865 if ($result) {
866 while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
867 $searchresults[] = $row;
871 include('pkg_search_results.php');
873 return $total;
877 * Construct the WHERE part of the sophisticated keyword search
879 * @param handle $dbh Database handle
880 * @param string $keywords The search term
881 * @param bool $namedesc Search name and description fields
883 * @return string WHERE part of the SQL clause
885 function construct_keyword_search($dbh, $keywords, $namedesc) {
886 $count = 0;
887 $where_part = "";
888 $q_keywords = "";
889 $op = "";
891 foreach (str_getcsv($keywords, ' ') as $term) {
892 if ($term == "") {
893 continue;
895 if ($count > 0 && strtolower($term) == "and") {
896 $op = "AND ";
897 continue;
899 if ($count > 0 && strtolower($term) == "or") {
900 $op = "OR ";
901 continue;
903 if ($count > 0 && strtolower($term) == "not") {
904 $op .= "NOT ";
905 continue;
908 $term = "%" . addcslashes($term, '%_') . "%";
909 $q_keywords .= $op . " (";
910 if ($namedesc) {
911 $q_keywords .= "Packages.Name LIKE " . $dbh->quote($term) . " OR ";
912 $q_keywords .= "Description LIKE " . $dbh->quote($term) . " OR ";
914 $q_keywords .= "EXISTS (SELECT * FROM PackageKeywords WHERE ";
915 $q_keywords .= "PackageKeywords.PackageBaseID = Packages.PackageBaseID AND ";
916 $q_keywords .= "PackageKeywords.Keyword LIKE " . $dbh->quote($term) . ")) ";
918 $count++;
919 if ($count >= 20) {
920 break;
922 $op = "AND ";
925 if (!empty($q_keywords)) {
926 $where_part = "AND (" . $q_keywords . ") ";
929 return $where_part;
933 * Determine if a POST string has been sent by a visitor
935 * @param string $action String to check has been sent via POST
937 * @return bool True if the POST string was used, otherwise false
939 function current_action($action) {
940 return (isset($_POST['action']) && $_POST['action'] == $action) ||
941 isset($_POST[$action]);
945 * Determine if sent IDs are valid integers
947 * @param array $ids IDs to validate
949 * @return array All sent IDs that are valid integers
951 function sanitize_ids($ids) {
952 $new_ids = array();
953 foreach ($ids as $id) {
954 $id = intval($id);
955 if ($id > 0) {
956 $new_ids[] = $id;
959 return $new_ids;
963 * Determine package information for latest package
965 * @param int $numpkgs Number of packages to get information on
967 * @return array $packages Package info for the specified number of recent packages
969 function latest_pkgs($numpkgs) {
970 $dbh = DB::connect();
972 $q = "SELECT Packages.*, MaintainerUID, SubmittedTS ";
973 $q.= "FROM Packages LEFT JOIN PackageBases ON ";
974 $q.= "PackageBases.ID = Packages.PackageBaseID ";
975 $q.= "ORDER BY SubmittedTS DESC ";
976 $q.= "LIMIT " . intval($numpkgs);
977 $result = $dbh->query($q);
979 $packages = array();
980 if ($result) {
981 while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
982 $packages[] = $row;
986 return $packages;