New remote API function: mail_domain_get_users [PATCH INCLUDED]
There is a real lack of functions in the remote api that allows the retrieval of information of which then subsequent commands can be utilized. This is illustrated by the latest functions that have been added such as: client_get_by_username, mail_domain_get_by_domain, dns_zone_get_by_user, etc. These are all functions that allow someone to perform a logical action with a logical piece of data, not internal ids that mean nothing. Along those lines, I had the need to retrieve the mail accounts for a domain. I do not wish to cast judgment on the design decision to not have a foreign key for the mail_domain that a mail_user is in, but I did have need to acquire that information. The below new api function takes a stab at solving the need without getting into a debate over FKs or making core changes. If those core changes occur, great, this function could change, but for now I think other might find it useful as-is. Let me know if there is anything I can do to improve the function or bring it more inline with ISPConfig coding style, etc.
Also, please email me for the original, un-formatted patch file if needed.
BEGIN PATCH
--- /usr/local/ispconfig/interface/lib/classes/remoting.inc.php 2011-04-20 10:38:28.574075332 -0500
+++ ispconfig3_install/interface/lib/classes/remoting.inc.php 2011-04-20 11:14:08.730841387 -0500
@@ -2397,6 +2397,43 @@
}
/**
+ * Retrieve all mail accounts for the specified domain or domain id.
+ * @param int session_id
+ * @param string [null] the fully qualified domain (or subdomain), null to ignore
+ * @param int [null] the domain id, null to ignore, takes precedence if both are provided
+ * @return array array of arrays representing mail_user records or an empty array if no results
+ * @author Ben Lake <dev@benlake.org>
+ */
+ public function mail_domain_get_users($session_id, $domain = null, $domain_id = null)
+ {
+ global $app;
+
+ if (empty($domain) && empty($domain_id)) {
+ $this->server->fault('invalid_arguments', 'You must specify either a domain name or domain id, neither provided');
+ }
+
+ if(!$this->checkPerm($session_id, 'mail_domain_get_users')) {
+ $this->server->fault('permission_denied', 'You do not have the permissions to access this function.');
+ return false;
+ }
+
+ if (!empty($domain_id)) {
+ $domain_id = intval($domain_id);
+ $sql = "SELECT * FROM mail_user WHERE email LIKE concat('%@', (SELECT domain FROM mail_domain WHERE domain_id = ".$domain_id."))";
+ }
+ elseif (!empty($domain)) {
+ $domain = $app->db->quote($domain);
+ $sql = "SELECT * FROM mail_user WHERE email LIKE '%@".$domain."'";
+ }
+
+ $result = $app->db->queryAllRecords($sql);
+ if (!is_array($result))
+ $result = array();
+
+ return $result;
+ }
+
+ /**
* Fetch the mail_domain record for the provided domain.
* @param int session_id
* @param string the fully qualified domain (or subdomain)