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.

332 lines
9.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['domain_path'] == "YES")
  146. {
  147. if ($CONF['domain_in_mailbox'] == "YES")
  148. {
  149. $maildir = $fDomain . "/" . $fUsername . "/";
  150. }
  151. else
  152. {
  153. $maildir = $fDomain . "/" . escape_string (strtolower($_POST['fUsername'])) . "/";
  154. }
  155. }
  156. else
  157. {
  158. $maildir = $fUsername . "/";
  159. }
  160. if (!empty ($fQuota))
  161. {
  162. $quota = multiply_quota ($fQuota);
  163. }
  164. else
  165. {
  166. $quota = 0;
  167. }
  168. if ($fActive == "on")
  169. {
  170. $sqlActive = db_get_boolean(True);
  171. }
  172. else
  173. {
  174. $sqlActive = db_get_boolean(False);
  175. }
  176. if ('pgsql'==$CONF['database_type'])
  177. {
  178. db_query('BEGIN');
  179. }
  180. $result = db_query ("INSERT INTO $table_alias (address,goto,domain,created,modified,active) VALUES ('$fUsername','$fUsername','$fDomain',NOW(),NOW(),'$sqlActive')");
  181. if ($result['rows'] != 1)
  182. {
  183. $tDomain = $fDomain;
  184. $tMessage = $PALANG['pAlias_result_error'] . "<br />($fUsername -> $fUsername)</br />";
  185. }
  186. /*
  187. # TODO: The following code segment is from admin/create-mailbox.php. To be compared/merged with the code from /create-mailbox.php.
  188. Lines starting with /* were inserted to keep this section in commented mode.
  189. if ($result['rows'] != 1)
  190. {
  191. $tDomain = $fDomain;
  192. $tMessage .= $PALANG['pCreate_mailbox_result_error'] . "<br />($fUsername)<br />";
  193. }
  194. else
  195. {
  196. $error=TRUE; // Being pessimistic
  197. if (mailbox_postcreation($fUsername,$fDomain,$maildir))
  198. {
  199. if ('pgsql'==$CONF['database_type'])
  200. {
  201. $result=db_query("COMMIT");
  202. /* should really not be possible: */
  203. /*
  204. if (!$result) die('COMMIT-query failed.');
  205. }
  206. $error=FALSE;
  207. } else {
  208. $tMessage .= $PALANG['pCreate_mailbox_result_error'] . "<br />($fUsername)<br />";
  209. if ('pgsql'==$CONF['database_type'])
  210. {
  211. $result=db_query("ROLLBACK");
  212. /* should really not be possible: */
  213. /*
  214. if (!$result) die('ROLLBACK-query failed.');
  215. } else {
  216. /*
  217. When we cannot count on transactions, we need to move forward, despite
  218. the problems.
  219. */
  220. /*
  221. $error=FALSE;
  222. }
  223. }
  224. if (!$error)
  225. {
  226. db_log ($CONF['admin_email'], $fDomain, 'create_mailbox', $fUsername);
  227. */
  228. /*
  229. TODO: this is the start of /create-mailbox code segment that was originally used in /create-mailbox.php instead
  230. of the above from admin/create-mailbox.php.
  231. To be compared / merged.
  232. */
  233. // apparently uppercase usernames really confuse some IMAP clients.
  234. $fUsername = strtolower($fUsername);
  235. $local_part = '';
  236. if(preg_match('/^(.*)@/', $fUsername, $matches)) {
  237. $local_part = $matches[1];
  238. }
  239. $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')");
  240. if ($result['rows'] != 1 || !mailbox_postcreation($fUsername,$fDomain,$maildir, $quota))
  241. {
  242. $tDomain = $fDomain;
  243. $tMessage .= $PALANG['pCreate_mailbox_result_error'] . "<br />($fUsername)<br />";
  244. db_query('ROLLBACK');
  245. }
  246. else
  247. {
  248. db_query('COMMIT');
  249. db_log ($SESSID_USERNAME, $fDomain, 'create_mailbox', "$fUsername");
  250. /*
  251. TODO: this is the end of /create-mailbox.php code segment
  252. */
  253. $tDomain = $fDomain;
  254. $tQuota = $CONF['maxquota'];
  255. if ($fMail == "on")
  256. {
  257. $fTo = $fUsername;
  258. $fFrom = $SESSID_USERNAME;
  259. $fHeaders = "To: " . $fTo . "\n";
  260. $fHeaders .= "From: " . $fFrom . "\n";
  261. $fHeaders .= "Subject: " . encode_header ($PALANG['pSendmail_subject_text']) . "\n";
  262. $fHeaders .= "MIME-Version: 1.0\n";
  263. $fHeaders .= "Content-Type: text/plain; charset=utf-8\n";
  264. $fHeaders .= "Content-Transfer-Encoding: 8bit\n";
  265. $fHeaders .= $CONF['welcome_text'];
  266. if (!smtp_mail ($fTo, $fFrom, $fHeaders))
  267. {
  268. $tMessage .= "<br />" . $PALANG['pSendmail_result_error'] . "<br />";
  269. }
  270. else
  271. {
  272. $tMessage .= "<br />" . $PALANG['pSendmail_result_success'] . "<br />";
  273. }
  274. }
  275. $tShowpass = "";
  276. if ( $tPassGenerated == 1 || $CONF['show_password'] == "YES") $tShowpass = " / $fPassword";
  277. if (create_mailbox_subfolders($fUsername,$fPassword))
  278. {
  279. $tMessage .= $PALANG['pCreate_mailbox_result_success'] . "<br />($fUsername$tShowpass)";
  280. } else {
  281. $tMessage .= $PALANG['pCreate_mailbox_result_succes_nosubfolders'] . "<br />($fUsername$tShowpass)";
  282. }
  283. }
  284. }
  285. }
  286. include ("templates/header.php");
  287. include ("templates/menu.php");
  288. include ("templates/create-mailbox.php");
  289. include ("templates/footer.php");
  290. /* vim: set expandtab softtabstop=3 tabstop=3 shiftwidth=3: */
  291. ?>