Alternc  latest
Alternc logiel libre pour l'hébergement
Mail.php
Go to the documentation of this file.
1 <?php
2 
3 /**
4  * Domain Api of AlternC, used by alternc-api package
5  */
7 
8  protected $mail; // m_mail instance
9 
10  function __construct($service) {
11  global $mail;
12  parent::__construct($service);
13  $this->mail = $mail;
14  }
15 
16  /** API Method from legacy class method mail->enum_domains()
17  * @param $options a hash with parameters transmitted to legacy call
18  * @return Alternc_Api_Response whose content is the list of hosted domains
19  * for mails on this server
20  * (no more details as of now)
21  */
22  function listDomains($options) {
23  global $cuid;
24  $sql = "";
25  $uid = $cuid;
26  if ($this->isAdmin && isset($options["uid"])) {
27  $uid = intval($options["uid"]);
28  }
29 
30  $did = $this->mail->enum_domains($uid);
31  if (!$did) {
32  return $this->alterncLegacyErrorManager();
33  } else {
34  return new Alternc_Api_Response(array("content" => $did));
35  }
36  }
37 
38  /** API Method from legacy class method mail->available()
39  * @param $options a hash with parameters transmitted to legacy call
40  * mandatory parameters: mail(str)
41  * @return Alternc_Api_Response telling TRUE or FALSE
42  */
43  function isAvailable($options) {
44  if (!isset($options["mail"])) {
45  return new Alternc_Api_Response(array("code" => self::ERR_INVALID_ARGUMENT, "message" => "Missing or invalid argument: " . "mail"));
46  }
47  $did = $this->mail->available($options["mail"]);
48  if (!$did) {
49  return $this->alterncLegacyErrorManager();
50  } else {
51  return new Alternc_Api_Response(array("content" => $did));
52  }
53  }
54 
55  /** API Method from legacy class method $mail->enum_domain_mails
56  * ($dom_id = null, $search="", $offset=0, $count=30, $show_systemmails=false)
57  * @param $options a hash with parameters transmitted to legacy call
58  * mandatory parameters:
59  * non-mandatory:
60  * @return Alternc_Api_Response whose content is
61  */
62  function getAll($options) {
63  $defaults = array("dom_id" => null, "search" => "", "offset" => 0, "count" => 30, "show_systemmails" => false);
64  foreach ($defaults as $key => $value) {
65  if (!isset($options[$key])) {
66  $options[$key] = $value;
67  }
68  }
69  $did = $this->mail->enum_domain_mails($options["dom_id"], $options["search"], $options["offset"], $options["count"], $options["show_systemmails"]);
70  if (!$did) {
71  return $this->alterncLegacyErrorManager();
72  } else {
73  return new Alternc_Api_Response(array("content" => $did));
74  }
75  }
76 
77  /** API Method from legacy class method $mail->create
78  * ($dom_id, $mail,$type="",$dontcheck=false){
79  * @param $options a hash with parameters transmitted to legacy call
80  * mandatory parameters:
81  * non-mandatory:
82  * @return Alternc_Api_Response whose content is
83  */
84  function create($options) {
85  $defaults = array("type" => "");
86  $mandatory = array("dom_id", "mail");
87  $missing = "";
88  foreach ($mandatory as $key) {
89  if (!isset($options[$key])) {
90  $missing.=$key . " ";
91  }
92  }
93  if ($missing) {
94  return new Alternc_Api_Response(array("code" => self::ERR_INVALID_ARGUMENT, "message" => "Missing or invalid argument: " . $missing));
95  }
96  foreach ($defaults as $key => $value) {
97  if (!isset($options[$key])) {
98  $options[$key] = $value;
99  }
100  }
101  $did = $this->mail->create($options["dom_id"], $options["mail"], $options["type"]);
102  if (!$did) {
103  return $this->alterncLegacyErrorManager();
104  } else {
105  return new Alternc_Api_Response(array("content" => $did));
106  }
107  }
108 
109  /** API Method from legacy class method $mail->get_details($mail_id)
110  * @param $options a hash with parameters transmitted to legacy call
111  * mandatory parameters: mail_id
112  * @return Alternc_Api_Response whose content is
113  */
114  function get($options) {
115  $mandatory = array("mail_id");
116  $missing = "";
117  foreach ($mandatory as $key) {
118  if (!isset($options[$key])) {
119  $missing.=$key . " ";
120  }
121  }
122  if ($missing) {
123  return new Alternc_Api_Response(array("code" => self::ERR_INVALID_ARGUMENT, "message" => "Missing or invalid argument: " . $missing));
124  }
125  $did = $this->mail->get_details($options["mail_id"]);
126  if (!$did) {
127  return $this->alterncLegacyErrorManager();
128  } else {
129  return new Alternc_Api_Response(array("content" => $did));
130  }
131  }
132 
133  /** API Method from legacy class method $mail->get_account_by_mail_id($mail_id)
134  * @param $options a hash with parameters transmitted to legacy call
135  * mandatory parameters: mail_id
136  * @return Alternc_Api_Response whose content is
137  */
138  function account($options) {
139  $mandatory = array("mail_id");
140  $missing = "";
141  foreach ($mandatory as $key) {
142  if (!isset($options[$key])) {
143  $missing.=$key . " ";
144  }
145  }
146  if ($missing) {
147  return new Alternc_Api_Response(array("code" => self::ERR_INVALID_ARGUMENT, "message" => "Missing or invalid argument: " . $missing));
148  }
149  $did = $this->mail->get_account_by_mail_id($options["mail_id"]);
150  if (!$did) {
151  return $this->alterncLegacyErrorManager();
152  } else {
153  return new Alternc_Api_Response(array("content" => $did));
154  }
155  }
156 
157  /** API Method from legacy class method $mail->delete($mail_id)
158  * @param $options a hash with parameters transmitted to legacy call
159  * mandatory parameters: mail_id
160  * @return Alternc_Api_Response whose content is
161  */
162  function delete($options) {
163  $mandatory = array("mail_id");
164  $missing = "";
165  foreach ($mandatory as $key) {
166  if (!isset($options[$key])) {
167  $missing.=$key . " ";
168  }
169  }
170  if ($missing) {
171  return new Alternc_Api_Response(array("code" => self::ERR_INVALID_ARGUMENT, "message" => "Missing or invalid argument: " . $missing));
172  }
173  $did = $this->mail->delete($options["mail_id"]);
174  if (!$did) {
175  return $this->alterncLegacyErrorManager();
176  } else {
177  return new Alternc_Api_Response(array("content" => $did));
178  }
179  }
180 
181  /** API Method from legacy class method $mail->undelete($mail_id)
182  * @param $options a hash with parameters transmitted to legacy call
183  * mandatory parameters: mail_id
184  * @return Alternc_Api_Response whose content is
185  */
186  function undelete($options) {
187  $mandatory = array("mail_id");
188  $missing = "";
189  foreach ($mandatory as $key) {
190  if (!isset($options[$key])) {
191  $missing.=$key . " ";
192  }
193  }
194  if ($missing) {
195  return new Alternc_Api_Response(array("code" => self::ERR_INVALID_ARGUMENT, "message" => "Missing or invalid argument: " . $missing));
196  }
197  $did = $this->mail->undelete($options["mail_id"]);
198  if (!$did) {
199  return $this->alterncLegacyErrorManager();
200  } else {
201  return new Alternc_Api_Response(array("content" => $did));
202  }
203  }
204 
205  /** API Method from legacy class method $mail->delete($mail_id)
206  * @param $options a hash with parameters transmitted to legacy call
207  * mandatory parameters: mail_id, password
208  * @return Alternc_Api_Response whose content is
209  */
210  function passwd($options) {
211  $mandatory = array("mail_id", "password");
212  $missing = "";
213  foreach ($mandatory as $key) {
214  if (!isset($options[$key])) {
215  $missing.=$key . " ";
216  }
217  }
218  if ($missing) {
219  return new Alternc_Api_Response(array("code" => self::ERR_INVALID_ARGUMENT, "message" => "Missing or invalid argument: " . $missing));
220  }
221  $did = $this->mail->set_passwd($options["mail_id"], $options["password"]);
222  if (!$did) {
223  return $this->alterncLegacyErrorManager();
224  } else {
225  return new Alternc_Api_Response(array("content" => $did));
226  }
227  }
228 
229  /** API Method from legacy class method $mail->enable($mail_id)
230  * @param $options a hash with parameters transmitted to legacy call
231  * mandatory parameters: mail_id
232  * @return Alternc_Api_Response whose content is
233  */
234  function enable($options) {
235  $mandatory = array("mail_id");
236  $missing = "";
237  foreach ($mandatory as $key) {
238  if (!isset($options[$key])) {
239  $missing.=$key . " ";
240  }
241  }
242  if ($missing) {
243  return new Alternc_Api_Response(array("code" => self::ERR_INVALID_ARGUMENT, "message" => "Missing or invalid argument: " . $missing));
244  }
245  $did = $this->mail->enable($options["mail_id"]);
246  if (!$did) {
247  return $this->alterncLegacyErrorManager();
248  } else {
249  return new Alternc_Api_Response(array("content" => $did));
250  }
251  }
252 
253  /** API Method from legacy class method $mail->disable($mail_id)
254  * @param $options a hash with parameters transmitted to legacy call
255  * mandatory parameters: mail_id
256  * @return Alternc_Api_Response whose content is
257  */
258  function disable($options) {
259  $mandatory = array("mail_id");
260  $missing = "";
261  foreach ($mandatory as $key) {
262  if (!isset($options[$key])) {
263  $missing.=$key . " ";
264  }
265  }
266  if ($missing) {
267  return new Alternc_Api_Response(array("code" => self::ERR_INVALID_ARGUMENT, "message" => "Missing or invalid argument: " . $missing));
268  }
269  $did = $this->mail->disable($options["mail_id"]);
270  if (!$did) {
271  return $this->alterncLegacyErrorManager();
272  } else {
273  return new Alternc_Api_Response(array("content" => $did));
274  }
275  }
276 
277  /** API Method from legacy class method $mail-> set_details
278  * ($mail_id, $islocal, $quotamb, $recipients,$delivery="dovecot",$dontcheck=false)
279  * @param $options a hash with parameters transmitted to legacy call
280  * mandatory parameters:
281  * non-mandatory:
282  * @return Alternc_Api_Response whose content is
283  */
284  function update($options) {
285  $defaults = array("delivery" => "dovecot");
286  $mandatory = array("mail_id", "islocal", "quotamb", "recipients");
287  $missing = "";
288  foreach ($mandatory as $key) {
289  if (!isset($options[$key])) {
290  $missing.=$key . " ";
291  }
292  }
293  if ($missing) {
294  return new Alternc_Api_Response(array("code" => self::ERR_INVALID_ARGUMENT, "message" => "Missing or invalid argument: " . $missing));
295  }
296  foreach ($defaults as $key => $value) {
297  if (!isset($options[$key])) {
298  $options[$key] = $value;
299  }
300  }
301  $did = $this->mail->set_details($options["mail_id"], $options["islocal"], $options["quotamb"], $options["recipients"], $options["delivery"]);
302  if (!$did) {
303  return $this->alterncLegacyErrorManager();
304  } else {
305  return new Alternc_Api_Response(array("content" => $did));
306  }
307  }
308 
309 
310  /** API Method for email authentication
311  * @param $options a hash with email and password parameters.
312  * @return Alternc_Api_Response whose content is true / false if the auth
313  * to this email was successfull.
314  */
315  function login($options) {
316  global $cuid;
317  $uid = $cuid;
318  if ($this->isAdmin && isset($options["uid"])) {
319  $uid = intval($options["uid"]);
320  }
321  if (!isset($options["email"]) || !isset($options["password"])) {
322  return new Alternc_Api_Response(array("code" => self::ERR_INVALID_ARGUMENT, "message" => "Missing email or password argument"));
323  }
324  list($address,$domain)=explode("@",$options["email"],2);
325  $stmt = $this->db->prepare("SELECT enabled,password FROM domaines d,address a WHERE a.domain_id=d.id AND address=? AND domaine=?;");
326  $stmt->execute(array($address,$domain));
327  $me = $stmt->fetch(PDO::FETCH_OBJ);
328  if ($me && $me->enabled) {
329  // Check password :
330  return new Alternc_Api_Response(array("content" => $this->check_password($options["password"],$me->password) ));
331  } else {
332  return new Alternc_Api_Response(array("content" => false));
333  }
334  }
335 
336 
337  function check_password($password, $hash) {
338  if ($hash == '') { // no password
339  return FALSE;
340  }
341 
342  if ($hash{0} != '{') { // plaintext or crypt() password ?
343  if ($hash{0} == '$' ) {
344  if (crypt($password,$hash)==$hash)
345  return TRUE;
346  } else { // plaintext ? (NOT RECOMMENDED !!!)
347  if ($password == $hash)
348  return TRUE;
349  }
350  return FALSE;
351  }
352 
353  if (substr($hash,0,7) == '{crypt}') {
354  if (crypt($password, substr($hash,7)) == substr($hash,7))
355  return TRUE;
356  return FALSE;
357  }
358  elseif (substr($hash,0,5) == '{MD5}') {
359  $encrypted_password = '{MD5}' . base64_encode(md5( $password,TRUE));
360  }
361  elseif (substr($hash,0,6) == '{SHA1}') {
362  $encrypted_password = '{SHA}' . base64_encode(sha1( $password, TRUE ));
363  }
364  elseif (substr($hash,0,6) == '{SSHA}') {
365  $salt = substr(base64_decode(substr($hash,6)),20);
366  $encrypted_password = '{SSHA}' . base64_encode(sha1( $password.$salt, TRUE ). $salt);
367  } else {
368  echo "Unsupported password hash format";
369  return FALSE;
370  }
371 
372  if ($hash == $encrypted_password)
373  return TRUE;
374 
375  return FALSE;
376  }
377 
378 }
379 
380 // class Alternc_Api_Object_Mail
Any Legacy AlternC Api should use this class as a parent to be able to handle properly the access rig...
Definition: Legacyobject.php:9
alterncLegacyErrorManager()
return a proper Alternc_Api_Response from an error class and error string from AlternC legacy class
Domain Api of AlternC, used by alternc-api package.
Definition: Mail.php:6
check_password($password, $hash)
Definition: Mail.php:337
update($options)
API Method from legacy class method $mail-> set_details ($mail_id, $islocal, $quotamb,...
Definition: Mail.php:284
create($options)
API Method from legacy class method $mail->create ($dom_id, $mail,$type="",$dontcheck=false){.
Definition: Mail.php:84
isAvailable($options)
API Method from legacy class method mail->available()
Definition: Mail.php:43
__construct($service)
Definition: Mail.php:10
account($options)
API Method from legacy class method $mail->get_account_by_mail_id($mail_id)
Definition: Mail.php:138
disable($options)
API Method from legacy class method $mail->disable($mail_id)
Definition: Mail.php:258
listDomains($options)
API Method from legacy class method mail->enum_domains()
Definition: Mail.php:22
login($options)
API Method for email authentication.
Definition: Mail.php:315
enable($options)
API Method from legacy class method $mail->enable($mail_id)
Definition: Mail.php:234
getAll($options)
API Method from legacy class method $mail->enum_domain_mails ($dom_id = null, $search="",...
Definition: Mail.php:62
passwd($options)
API Method from legacy class method $mail->delete($mail_id)
Definition: Mail.php:210
undelete($options)
API Method from legacy class method $mail->undelete($mail_id)
Definition: Mail.php:186
Standard Response object for the AlternC API.
Definition: Response.php:7
$domain
Definition: dom_import.php:36
$value
$uid
$password
Definition: bootstrap.php:85
if(empty($site_name)) elseif($piwik->site_add( $site_name, $site_urls))
if(empty($_POST['key'])||empty($_POST['val'])) $key
Definition: tempovars.php:14