PostfixAdmin - web based virtual user administration interface for Postfix mail servers https://postfixadmin.github.io/postfixadmin/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

278 lines
8.8 KiB

  1. <?php
  2. /**
  3. * Postfix Admin
  4. *
  5. * LICENSE
  6. * This source file is subject to the GPL license that is bundled with
  7. * this package in the file LICENSE.TXT.
  8. *
  9. * Further details on the project are available at :
  10. * http://www.postfixadmin.com or http://postfixadmin.sf.net
  11. *
  12. * File: create-mailbox.php
  13. * Responsible for allowing for the creation of mail boxes
  14. *
  15. * @version $Id$
  16. * @license GNU GPL v2 or later.
  17. *
  18. * Template Variables:
  19. *
  20. * tMessage
  21. * tUsername
  22. * tName
  23. * tQuota
  24. * tDomain
  25. *
  26. * Form POST \ GET Variables:
  27. *
  28. * fUsername
  29. * fPassword
  30. * fPassword2
  31. * fName
  32. * fQuota
  33. * fDomain
  34. * fActive
  35. * fMail
  36. */
  37. require_once('common.php');
  38. authentication_require_role('admin');
  39. $SESSID_USERNAME = authentication_get_username();
  40. if(authentication_has_role('global-admin')) {
  41. $list_domains = list_domains ();
  42. }
  43. else {
  44. $list_domains = list_domains_for_admin($SESSID_USERNAME);
  45. }
  46. $pCreate_mailbox_password_text = $PALANG['pCreate_mailbox_password_text'];
  47. $pCreate_mailbox_name_text = $PALANG['pCreate_mailbox_name_text'];
  48. $pCreate_mailbox_quota_text = $PALANG['pCreate_mailbox_quota_text'];
  49. if ($_SERVER['REQUEST_METHOD'] == "GET")
  50. {
  51. $fDomain = $list_domains[0];
  52. if (isset ($_GET['domain'])) $fDomain = escape_string ($_GET['domain']);
  53. if(!in_array($fDomain, $list_domains)) {
  54. die("Invalid domain name selected, or you tried to select a domain you are not an admin for");
  55. }
  56. $tDomain = $fDomain;
  57. $result = db_query ("SELECT * FROM $table_domain WHERE domain='$fDomain'");
  58. if ($result['rows'] == 1)
  59. {
  60. $row = db_array ($result['result']);
  61. $tQuota = $row['maxquota'];
  62. }
  63. }
  64. if ($_SERVER['REQUEST_METHOD'] == "POST")
  65. {
  66. if (isset ($_POST['fUsername']) && isset ($_POST['fDomain'])) $fUsername = escape_string ($_POST['fUsername']) . "@" . escape_string ($_POST['fDomain']);
  67. $fUsername = strtolower ($fUsername);
  68. if (isset ($_POST['fPassword'])) $fPassword = escape_string ($_POST['fPassword']);
  69. if (isset ($_POST['fPassword2'])) $fPassword2 = escape_string ($_POST['fPassword2']);
  70. isset ($_POST['fName']) ? $fName = escape_string ($_POST['fName']) : $fName = "";
  71. if (isset ($_POST['fDomain'])) $fDomain = escape_string ($_POST['fDomain']);
  72. isset ($_POST['fQuota']) ? $fQuota = intval($_POST['fQuota']) : $fQuota = 0;
  73. isset ($_POST['fActive']) ? $fActive = escape_string ($_POST['fActive']) : $fActive = "1";
  74. if (isset ($_POST['fMail'])) $fMail = escape_string ($_POST['fMail']);
  75. if ( (!check_owner ($SESSID_USERNAME, $fDomain)) && (!authentication_has_role('global-admin')) )
  76. {
  77. $error = 1;
  78. $tUsername = escape_string ($_POST['fUsername']);
  79. $tName = $fName;
  80. $tQuota = $fQuota;
  81. $tDomain = $fDomain;
  82. $pCreate_mailbox_username_text = $PALANG['pCreate_mailbox_username_text_error1'];
  83. }
  84. if (!check_mailbox ($fDomain))
  85. {
  86. $error = 1;
  87. $tUsername = escape_string ($_POST['fUsername']);
  88. $tName = $fName;
  89. $tQuota = $fQuota;
  90. $tDomain = $fDomain;
  91. $pCreate_mailbox_username_text = $PALANG['pCreate_mailbox_username_text_error3'];
  92. }
  93. if (empty ($fUsername) or !check_email ($fUsername))
  94. {
  95. $error = 1;
  96. $tUsername = escape_string ($_POST['fUsername']);
  97. $tName = $fName;
  98. $tQuota = $fQuota;
  99. $tDomain = $fDomain;
  100. $pCreate_mailbox_username_text = $PALANG['pCreate_mailbox_username_text_error1'];
  101. }
  102. $tPassGenerated = 0;
  103. if (empty ($fPassword) or empty ($fPassword2) or ($fPassword != $fPassword2))
  104. {
  105. if (empty ($fPassword) and empty ($fPassword2) and $CONF['generate_password'] == "YES")
  106. {
  107. $fPassword = generate_password ();
  108. $tPassGenerated = 1;
  109. }
  110. else
  111. {
  112. $error = 1;
  113. $tUsername = escape_string ($_POST['fUsername']);
  114. $tName = $fName;
  115. $tQuota = $fQuota;
  116. $tDomain = $fDomain;
  117. $pCreate_mailbox_password_text = $PALANG['pCreate_mailbox_password_text_error'];
  118. }
  119. }
  120. if ($CONF['quota'] == "YES")
  121. {
  122. if (!check_quota ($fQuota, $fDomain))
  123. {
  124. $error = 1;
  125. $tUsername = escape_string ($_POST['fUsername']);
  126. $tName = $fName;
  127. $tQuota = $fQuota;
  128. $tDomain = $fDomain;
  129. $pCreate_mailbox_quota_text = $PALANG['pCreate_mailbox_quota_text_error'];
  130. }
  131. }
  132. $result = db_query ("SELECT * FROM $table_alias WHERE address='$fUsername'");
  133. if ($result['rows'] == 1)
  134. {
  135. $error = 1;
  136. $tUsername = escape_string ($_POST['fUsername']);
  137. $tName = $fName;
  138. $tQuota = $fQuota;
  139. $tDomain = $fDomain;
  140. $pCreate_mailbox_username_text = $PALANG['pCreate_mailbox_username_text_error2'];
  141. }
  142. if ($error != 1)
  143. {
  144. $password = pacrypt ($fPassword);
  145. if($CONF['maildir_name_hook'] != 'NO' && function_exists($CONF['maildir_name_hook'])) {
  146. $hook_func = $CONF['maildir_name_hook'];
  147. $maildir = $hook_func ($fDomain, $fUsername);
  148. }
  149. else if ($CONF['domain_path'] == "YES")
  150. {
  151. if ($CONF['domain_in_mailbox'] == "YES")
  152. {
  153. $maildir = $fDomain . "/" . $fUsername . "/";
  154. }
  155. else
  156. {
  157. $maildir = $fDomain . "/" . escape_string (strtolower($_POST['fUsername'])) . "/";
  158. }
  159. }
  160. else
  161. {
  162. $maildir = $fUsername . "/";
  163. }
  164. if (!empty ($fQuota))
  165. {
  166. $quota = multiply_quota ($fQuota);
  167. }
  168. else
  169. {
  170. $quota = 0;
  171. }
  172. if ($fActive == "on")
  173. {
  174. $sqlActive = db_get_boolean(True);
  175. }
  176. else
  177. {
  178. $sqlActive = db_get_boolean(False);
  179. }
  180. if ('pgsql'==$CONF['database_type'])
  181. {
  182. db_query('BEGIN');
  183. }
  184. $result = db_query ("INSERT INTO $table_alias (address,goto,domain,created,modified,active) VALUES ('$fUsername','$fUsername','$fDomain',NOW(),NOW(),'$sqlActive')");
  185. if ($result['rows'] != 1)
  186. {
  187. $tDomain = $fDomain;
  188. $tMessage = $PALANG['pAlias_result_error'] . "<br />($fUsername -> $fUsername)</br />";
  189. }
  190. // apparently uppercase usernames really confuse some IMAP clients.
  191. $fUsername = strtolower($fUsername);
  192. $local_part = '';
  193. if(preg_match('/^(.*)@/', $fUsername, $matches)) {
  194. $local_part = $matches[1];
  195. }
  196. $result = db_query ("INSERT INTO $table_mailbox (username,password,name,maildir,local_part,quota,domain,created,modified,active) VALUES ('$fUsername','$password','$fName','$maildir','$local_part','$quota','$fDomain',NOW(),NOW(),'$sqlActive')");
  197. if ($result['rows'] != 1 || !mailbox_postcreation($fUsername,$fDomain,$maildir, $quota))
  198. {
  199. $tDomain = $fDomain;
  200. $tMessage .= $PALANG['pCreate_mailbox_result_error'] . "<br />($fUsername)<br />";
  201. db_query('ROLLBACK');
  202. }
  203. else
  204. {
  205. db_query('COMMIT');
  206. db_log ($SESSID_USERNAME, $fDomain, 'create_mailbox', "$fUsername");
  207. $tDomain = $fDomain;
  208. $tQuota = $CONF['maxquota'];
  209. if ($fMail == "on")
  210. {
  211. $fTo = $fUsername;
  212. $fFrom = $SESSID_USERNAME;
  213. $fHeaders = "To: " . $fTo . "\n";
  214. $fHeaders .= "From: " . $fFrom . "\n";
  215. $fHeaders .= "Subject: " . encode_header ($PALANG['pSendmail_subject_text']) . "\n";
  216. $fHeaders .= "MIME-Version: 1.0\n";
  217. $fHeaders .= "Content-Type: text/plain; charset=utf-8\n";
  218. $fHeaders .= "Content-Transfer-Encoding: 8bit\n";
  219. $fHeaders .= $CONF['welcome_text'];
  220. if (!smtp_mail ($fTo, $fFrom, $fHeaders))
  221. {
  222. $tMessage .= "<br />" . $PALANG['pSendmail_result_error'] . "<br />";
  223. }
  224. else
  225. {
  226. $tMessage .= "<br />" . $PALANG['pSendmail_result_success'] . "<br />";
  227. }
  228. }
  229. $tShowpass = "";
  230. if ( $tPassGenerated == 1 || $CONF['show_password'] == "YES") $tShowpass = " / $fPassword";
  231. if (create_mailbox_subfolders($fUsername,$fPassword))
  232. {
  233. $tMessage .= $PALANG['pCreate_mailbox_result_success'] . "<br />($fUsername$tShowpass)";
  234. } else {
  235. $tMessage .= $PALANG['pCreate_mailbox_result_succes_nosubfolders'] . "<br />($fUsername$tShowpass)";
  236. }
  237. }
  238. }
  239. }
  240. include ("templates/header.php");
  241. include ("templates/menu.php");
  242. include ("templates/create-mailbox.php");
  243. include ("templates/footer.php");
  244. /* vim: set expandtab softtabstop=3 tabstop=3 shiftwidth=3: */
  245. ?>