notify: Do not pass notification texts via pipes
[aur.git] / web / lib / pkgreqfuncs.inc.php
blobcf56663bbca493287e49fe38f30a41a27481b3e0
1 <?php
3 include_once("confparser.inc.php");
4 include_once("pkgbasefuncs.inc.php");
6 /**
7 * Get the number of package requests
9 * @return int The total number of package requests
11 function pkgreq_count() {
12 $dbh = DB::connect();
13 $q = "SELECT COUNT(*) FROM PackageRequests";
14 return $dbh->query($q)->fetchColumn();
17 /**
18 * Get a list of all package requests
20 * @param int $offset The index of the first request to return
21 * @param int $limit The maximum number of requests to return
23 * @return array List of pacakge requests with details
25 function pkgreq_list($offset, $limit) {
26 $dbh = DB::connect();
28 $q = "SELECT PackageRequests.ID, ";
29 $q.= "PackageRequests.PackageBaseID AS BaseID, ";
30 $q.= "PackageRequests.PackageBaseName AS Name, ";
31 $q.= "PackageRequests.MergeBaseName AS MergeInto, ";
32 $q.= "RequestTypes.Name AS Type, PackageRequests.Comments, ";
33 $q.= "Users.Username AS User, PackageRequests.RequestTS, ";
34 $q.= "PackageRequests.Status, PackageRequests.Status = 0 AS Open ";
35 $q.= "FROM PackageRequests INNER JOIN RequestTypes ON ";
36 $q.= "RequestTypes.ID = PackageRequests.ReqTypeID ";
37 $q.= "INNER JOIN Users ON Users.ID = PackageRequests.UsersID ";
38 $q.= "ORDER BY Open DESC, RequestTS DESC ";
39 $q.= "LIMIT " . $limit . " OFFSET " . $offset;
41 return $dbh->query($q)->fetchAll();
44 /**
45 * Get a list of all open package requests belonging to a certain package base
47 * @param int $baseid The package base ID to retrieve requests for
48 * @param int $type The type of requests to obtain
50 * @return array List of package request IDs
52 function pkgreq_by_pkgbase($baseid, $type=false) {
53 $dbh = DB::connect();
55 $q = "SELECT PackageRequests.ID ";
56 $q.= "FROM PackageRequests INNER JOIN RequestTypes ON ";
57 $q.= "RequestTypes.ID = PackageRequests.ReqTypeID ";
58 $q.= "WHERE PackageRequests.Status = 0 ";
59 $q.= "AND PackageRequests.PackageBaseID = " . intval($baseid);
61 if ($type) {
62 $q .= " AND RequestTypes.Name = " . $dbh->quote($type);
65 return $dbh->query($q)->fetchAll(PDO::FETCH_COLUMN, 0);
68 /**
69 * Obtain the package base that belongs to a package request.
71 * @param int $id Package request ID to retrieve the package base for
73 * @return int The name of the corresponding package base
75 function pkgreq_get_pkgbase_name($id) {
76 $dbh = DB::connect();
78 $q = "SELECT PackageBaseName FROM PackageRequests ";
79 $q.= "WHERE ID = " . intval($id);
80 $result = $dbh->query($q);
81 return $result->fetch(PDO::FETCH_COLUMN, 0);
84 /**
85 * Obtain the email address of the creator of a package request
87 * @param int $id Package request ID to retrieve the creator for
89 * @return int The email address of the creator
91 function pkgreq_get_creator_email($id) {
92 $dbh = DB::connect();
94 $q = "SELECT Email FROM Users INNER JOIN PackageRequests ";
95 $q.= "ON Users.ID = PackageRequests.UsersID ";
96 $q.= "WHERE PackageRequests.ID = " . intval($id);
97 $result = $dbh->query($q);
98 return $result->fetch(PDO::FETCH_COLUMN, 0);
102 * File a deletion/orphan request against a package base
104 * @param string $ids The package base IDs to file the request against
105 * @param string $type The type of the request
106 * @param string $merge_into The target of a merge operation
107 * @param string $comments The comments to be added to the request
109 * @return array Tuple of success/failure indicator and error message
111 function pkgreq_file($ids, $type, $merge_into, $comments) {
112 if (!has_credential(CRED_PKGREQ_FILE)) {
113 return array(false, __("You must be logged in to file package requests."));
116 if (!empty($merge_into) && !preg_match("/^[a-z0-9][a-z0-9\.+_-]*$/D", $merge_into)) {
117 return array(false, __("Invalid name: only lowercase letters are allowed."));
120 if (!empty($merge_into) && !pkgbase_from_name($merge_into)) {
121 return array(false, __("Cannot find package to merge votes and comments into."));
124 if (empty($comments)) {
125 return array(false, __("The comment field must not be empty."));
128 $dbh = DB::connect();
129 $uid = uid_from_sid($_COOKIE["AURSID"]);
131 /* TODO: Allow for filing multiple requests at once. */
132 $base_id = intval($ids[0]);
133 $pkgbase_name = pkgbase_name_from_id($base_id);
135 if ($merge_into == $pkgbase_name) {
136 return array(false, __("Cannot merge a package base with itself."));
139 $q = "SELECT ID FROM RequestTypes WHERE Name = " . $dbh->quote($type);
140 $result = $dbh->query($q);
141 if ($row = $result->fetch(PDO::FETCH_ASSOC)) {
142 $type_id = $row['ID'];
143 } else {
144 return array(false, __("Invalid request type."));
147 $q = "INSERT INTO PackageRequests ";
148 $q.= "(ReqTypeID, PackageBaseID, PackageBaseName, MergeBaseName, ";
149 $q.= "UsersID, Comments, RequestTS) VALUES (" . $type_id . ", ";
150 $q.= $base_id . ", " . $dbh->quote($pkgbase_name) . ", ";
151 $q.= $dbh->quote($merge_into) . ", " . $uid . ", ";
152 $q.= $dbh->quote($comments) . ", UNIX_TIMESTAMP())";
153 $dbh->exec($q);
154 $request_id = $dbh->lastInsertId();
156 /* Send e-mail notifications. */
157 $params = array('request-open', $uid, $request_id, $type, $base_id);
158 if ($type === 'merge') {
159 $params[] = $merge_into;
161 notify($params);
163 $auto_orphan_age = config_get('options', 'auto_orphan_age');
164 $auto_delete_age = config_get('options', 'auto_delete_age');
165 $details = pkgbase_get_details($base_id);
166 if ($type == 'orphan' && $details['OutOfDateTS'] > 0 &&
167 time() - $details['OutOfDateTS'] >= $auto_orphan_age &&
168 $auto_orphan_age > 0) {
170 * Close package request. NOTE: This needs to happen *before*
171 * the actual disown operation. Otherwise, the former
172 * maintainer will not be included in the Cc list of the
173 * request notification email.
175 $out_of_date_time = gmdate("Y-m-d", intval($details["OutOfDateTS"]));
176 pkgreq_close($request_id, "accepted",
177 "The package base has been flagged out-of-date " .
178 "since " . $out_of_date_time . ".", true);
179 $q = "UPDATE PackageBases SET MaintainerUID = NULL ";
180 $q.= "WHERE ID = " . $base_id;
181 $dbh->exec($q);
182 } else if ($type == 'deletion' && $details['MaintainerUID'] == $uid &&
183 $details['SubmittedTS'] > 0 && $auto_delete_age > 0 &&
184 time() - $details['SubmittedTS'] <= $auto_delete_age) {
186 * Close package request. NOTE: This needs to happen *before*
187 * the actual deletion operation. Otherwise, the former
188 * maintainer will not be included in the Cc list of the
189 * request notification email.
191 pkgreq_close($request_id, "accepted",
192 "Deletion of a fresh package requested by its " .
193 "current maintainer.", true);
194 pkgbase_delete(array($base_id), NULL, NULL, true);
197 return array(true, __("Added request successfully."));
201 * Close a deletion/orphan request
203 * @param int $id The package request to close
204 * @param string $reason Whether the request was accepted or rejected
205 * @param string $comments Comments to be added to the notification email
206 * @param boolean $auto_close (optional) Whether the request is auto-closed
208 * @return array Tuple of success/failure indicator and error message
210 function pkgreq_close($id, $reason, $comments, $auto_close=false) {
211 switch ($reason) {
212 case 'accepted':
213 $status = 2;
214 break;
215 case 'rejected':
216 $status = 3;
217 break;
218 default:
219 return array(false, __("Invalid reason."));
222 $dbh = DB::connect();
223 $id = intval($id);
224 $uid = uid_from_sid($_COOKIE["AURSID"]);
226 if (!$auto_close && !has_credential(CRED_PKGREQ_CLOSE)) {
227 return array(false, __("Only TUs and developers can close requests."));
230 $q = "UPDATE PackageRequests SET Status = " . intval($status) . ", ";
231 $q.= "ClosureComment = " . $dbh->quote($comments) . " ";
232 $q.= "WHERE ID = " . intval($id);
233 $dbh->exec($q);
235 /* Send e-mail notifications. */
236 notify(array('request-close', $uid, $id, $reason));
238 return array(true, __("Request closed successfully."));