PageRenderTime 37ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/www/emails.php

https://bitbucket.org/wayfarer/verse
PHP | 202 lines | 161 code | 15 blank | 26 comment | 22 complexity | 85d543b660b4b012e325503d725a72d0 MD5 | raw file
Possible License(s): ISC, AGPL-3.0, LGPL-2.1, BSD-3-Clause, LGPL-3.0
  1. <?php
  2. // backend email management, LDAP
  3. include("inc/verse.inc.php"); //main header - initializes Verse environment
  4. require_once("Net/LDAP2.php");
  5. if ($user->have_role(ROLE_EMAIL_MANAGEMENT)) {
  6. $action = @$_POST["action"];
  7. $myCacheConfig = array(
  8. // 'path' => 'c:/temp/ldap/ttt.cache',
  9. 'path' => '/tmp/ldap.cache',
  10. 'max_age' => 1200
  11. );
  12. $myCacheObject = new Net_LDAP2_SimpleFileSchemaCache($myCacheConfig);
  13. // Connect using the configuration:
  14. $ldap = Net_LDAP2::connect($ldap_config);
  15. if (PEAR::isError($ldap)) {
  16. die('Could not connect to LDAP-server: ' . $ldap->getMessage());
  17. }
  18. $ldap->registerSchemaCache($myCacheObject);
  19. // $domain_name = "christianlearningcenter.com";
  20. // $domain_name = "twintierstech.com";
  21. $baseDN = "domainName=$domain_name,o=domains,dc=twintierstech,dc=net";
  22. switch ($action) {
  23. case "load_email":
  24. $email = @$_POST["id"];
  25. $entry = $ldap->getEntry("mail=$email,ou=Users,$baseDN", array("mail", "cn"));
  26. if (PEAR::isError($entry)) {
  27. echo 'Error: ' . $entry->getMessage();
  28. exit;
  29. }
  30. $ret = $entry->getValues();
  31. $ret["username"] = substr($ret["mail"], 0, strpos($ret["mail"], "@"));
  32. unset($ret["mail"]);
  33. header("X-JSON:" . make_json_response($ret));
  34. break;
  35. case "load_alias":
  36. $email = @$_POST["id"];
  37. $entry = $ldap->getEntry("mail=$email,ou=Aliases,$baseDN", array("mail", "mailForwardingAddress"));
  38. if (PEAR::isError($entry)) {
  39. echo 'Error: ' . $entry->getMessage();
  40. exit;
  41. }
  42. $ret = $entry->getValues();
  43. if (is_array($ret["mailForwardingAddress"])) {
  44. $ret["aliases"] = implode("\n", $ret["mailForwardingAddress"]);
  45. }
  46. else {
  47. $ret["aliases"] = $ret["mailForwardingAddress"];
  48. }
  49. $ret["username"] = substr($ret["mail"], 0, strpos($ret["mail"], "@"));
  50. unset($ret["mailForwardingAddress"], $ret["mail"]);
  51. header("X-JSON:" . make_json_response($ret));
  52. break;
  53. case "save_email":
  54. $email = $_POST["id"];
  55. if ($email !== "0") {
  56. if (check_email($email)) {
  57. // 1st way, directly update entry by its DN, 1 LDAP request
  58. $replace = array("cn" => $_POST["cn"]);
  59. $email_pass = $_POST["email_pass"];
  60. if ($email_pass) {
  61. $replace["userPassword"] = make_ssha_password($email_pass);
  62. }
  63. $ret = $ldap->modify("mail=$email,ou=Users,$baseDN", array("replace" => $replace));
  64. echo "mail=$email,ou=Users,$baseDN";
  65. var_dump($replace);
  66. var_dump($ret);
  67. // 2nd way: fetch DN for modification, 2 LDAP requests
  68. /*$entry = $ldap->getEntry("mail=$email,ou=Users,$baseDN", array("cn", "userPassword"));
  69. if(!PEAR::isError($entry)) {
  70. // modify entry
  71. $replace = array("cn"=>@$_POST["cn"]);
  72. $email_pass = @$_POST["email_pass"];
  73. if($email_pass) {
  74. $replace["userPassword"] = make_ssha_password($email_pass);
  75. }
  76. $entry->replace($replace);
  77. }
  78. */
  79. }
  80. }
  81. else {
  82. $username = $_POST["username"];
  83. $email = "$username@$domain_name";
  84. $email_pass = $_POST["email_pass"];
  85. $cn = $_POST["cn"];
  86. if (!$cn) $cn = $username;
  87. if (check_email($email)) {
  88. $storagebasedir = "/home/vmail/vmail01";
  89. // generate mail message store
  90. $mailmessagestore = sprintf("%s/%s/%s/%s/%s-%s/", $domain_name, $username[0], substr($username, 0, 2), substr($username, 0, 3), $username, date("Y.m.d.H.i.s"));
  91. $homedir = $storagebasedir . "/" . $mailmessagestore;
  92. // create new entry
  93. $attributes = array(
  94. 'objectClass' => array("inetOrgPerson", "mailUser", "shadowAccount"),
  95. 'cn' => $cn,
  96. 'mail' => $email,
  97. 'sn' => $username,
  98. 'uid' => $username,
  99. 'accountStatus' => "active",
  100. 'enabledService' => array('mail', 'smtp', 'pop3', 'imap', 'deliver', 'forward',
  101. 'senderbcc', 'recipientbcc', 'managesieve',
  102. 'displayedInGlobalAddressBook'),
  103. 'homeDirectory' => $homedir,
  104. 'mailMessageStore' => $mailmessagestore,
  105. 'mailQuota' => 104857600,
  106. 'memberOfGroup' => "",
  107. 'mtaTransport' => "dovecot",
  108. 'storageBaseDirectory' => $storagebasedir,
  109. 'userPassword' => make_ssha_password($email_pass)
  110. );
  111. $entry = Net_LDAP2_Entry::createFresh("mail=$email,ou=Users,$baseDN", $attributes);
  112. // Add the entry to the directory:
  113. $ldap->add($entry);
  114. }
  115. }
  116. break;
  117. case "save_alias":
  118. $email = $_POST["id"];
  119. $aliases = explode("\n", trim($_POST["aliases"]));
  120. foreach ($aliases as $i => $alias) {
  121. if (!check_email($alias)) {
  122. unset($aliases[$i]);
  123. }
  124. }
  125. if ($email !== "0") {
  126. if (check_email($email)) {
  127. // 1st way, directly update entry by its DN, 1 LDAP request
  128. $replace = array("mailForwardingAddress" => $aliases);
  129. $ret = $ldap->modify("mail=$email,ou=Aliases,$baseDN", array("replace" => $replace));
  130. }
  131. }
  132. else {
  133. $username = $_POST["username"];
  134. $email = "$username@$domain_name";
  135. if (check_email($email)) {
  136. // create new entry
  137. $attributes = array(
  138. "objectClass" => array("mailAlias", "top"),
  139. "mail" => $email,
  140. "mailForwardingAddress" => $aliases,
  141. "accountStatus" => "active",
  142. "enabledService" => array("mail", "deliver")
  143. );
  144. $entry = Net_LDAP2_Entry::createFresh("mail=$email,ou=Aliases,$baseDN", $attributes);
  145. // Add the entry to the directory:
  146. $ret = $ldap->add($entry);
  147. echo $ret;
  148. }
  149. }
  150. break;
  151. case "delete_email":
  152. $email = $_POST["id"];
  153. if (check_email($email)) {
  154. $ldap->delete("mail=$email,ou=Users,$baseDN");
  155. }
  156. break;
  157. case "delete_alias":
  158. $email = $_POST["id"];
  159. if (check_email($email)) {
  160. $ldap->delete("mail=$email,ou=Aliases,$baseDN");
  161. }
  162. break;
  163. default:
  164. $query = "SELECT postfix FROM sms_domain WHERE domain_id='$domain_id'";
  165. $postfix = $db->getOne($query);
  166. // list action
  167. $result = $ldap->search("domainName=$domain_name,o=domains,dc=twintierstech,dc=net", '(&(mail=*)(|(objectClass=mailAlias)(objectClass=mailUser)))', array("attributes" => array("mail", "objectClass", "mailForwardingAddress", "mailQuota")));
  168. if (PEAR::isError($result)) {
  169. die($result->getMessage());
  170. }
  171. // var_dump($result->sorted_as_struct());
  172. $smarty->assign("data", $result->sorted_as_struct(array('mail')));
  173. $smarty->assign("entries_count", $result->count());
  174. $smarty->assign("domain_name", $domain_name);
  175. $smarty->assign("postfix", $domain_name);
  176. $smarty->display("emails.tpl");
  177. }
  178. }
  179. else {
  180. header("Location: login.php");
  181. }
  182. function make_ssha_password($password) {
  183. mt_srand((double)microtime() * 1000000);
  184. $salt = pack("CCCC", mt_rand(), mt_rand(), mt_rand(), mt_rand());
  185. $hash = "{SSHA}" . base64_encode(pack("H*", sha1($password . $salt)) . $salt);
  186. return $hash;
  187. }