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.

340 lines
9.7 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. * @version $Id$
  13. * @license GNU GPL v2 or later.
  14. *
  15. * File: create-mailbox.php
  16. * Responsible for allowing for the creation of mail boxes
  17. *
  18. * @version $Id$
  19. * @license GNU GPL v2 or later.
  20. *
  21. * Template Variables:
  22. *
  23. * tMessage
  24. * tUsername
  25. * tName
  26. * tQuota
  27. * tDomain
  28. *
  29. * Form POST \ GET Variables:
  30. *
  31. * fUsername
  32. * fPassword
  33. * fPassword2
  34. * fName
  35. * fQuota
  36. * fDomain
  37. * fActive
  38. * fMail
  39. */
  40. require_once('common.php');
  41. authentication_require_role('admin');
  42. $SESSID_USERNAME = authentication_get_username();
  43. if(authentication_has_role('global-admin')) {
  44. $list_domains = list_domains ();
  45. }
  46. else {
  47. $list_domains = list_domains_for_admin($SESSID_USERNAME);
  48. }
  49. $pCreate_mailbox_password_text = $PALANG['pCreate_mailbox_password_text'];
  50. $pCreate_mailbox_name_text = $PALANG['pCreate_mailbox_name_text'];
  51. $pCreate_mailbox_quota_text = $PALANG['pCreate_mailbox_quota_text'];
  52. if ($_SERVER['REQUEST_METHOD'] == "GET")
  53. {
  54. $fDomain = $list_domains[0];
  55. if (isset ($_GET['domain'])) $fDomain = escape_string ($_GET['domain']);
  56. if(!in_array($fDomain, $list_domains)) {
  57. die("Invalid domain name selected, or you tried to select a domain you are not an admin for");
  58. }
  59. $tDomain = $fDomain;
  60. $result = db_query ("SELECT * FROM $table_domain WHERE domain='$fDomain'");
  61. if ($result['rows'] == 1)
  62. {
  63. $row = db_array ($result['result']);
  64. $tQuota = $row['maxquota'];
  65. }
  66. }
  67. if ($_SERVER['REQUEST_METHOD'] == "POST")
  68. {
  69. if (isset ($_POST['fUsername']) && isset ($_POST['fDomain'])) $fUsername = escape_string ($_POST['fUsername']) . "@" . escape_string ($_POST['fDomain']);
  70. $fUsername = strtolower ($fUsername);
  71. if (isset ($_POST['fPassword'])) $fPassword = escape_string ($_POST['fPassword']);
  72. if (isset ($_POST['fPassword2'])) $fPassword2 = escape_string ($_POST['fPassword2']);
  73. isset ($_POST['fName']) ? $fName = escape_string ($_POST['fName']) : $fName = "";
  74. if (isset ($_POST['fDomain'])) $fDomain = escape_string ($_POST['fDomain']);
  75. isset ($_POST['fQuota']) ? $fQuota = intval($_POST['fQuota']) : $fQuota = 0;
  76. isset ($_POST['fActive']) ? $fActive = escape_string ($_POST['fActive']) : $fActive = "1";
  77. if (isset ($_POST['fMail'])) $fMail = escape_string ($_POST['fMail']);
  78. if ( (!check_owner ($SESSID_USERNAME, $fDomain)) && (!authentication_has_role('global-admin')) )
  79. {
  80. $error = 1;
  81. $tUsername = escape_string ($_POST['fUsername']);
  82. $tName = $fName;
  83. $tQuota = $fQuota;
  84. $tDomain = $fDomain;
  85. $pCreate_mailbox_username_text = $PALANG['pCreate_mailbox_username_text_error1'];
  86. }
  87. if (!check_mailbox ($fDomain))
  88. {
  89. $error = 1;
  90. $tUsername = escape_string ($_POST['fUsername']);
  91. $tName = $fName;
  92. $tQuota = $fQuota;
  93. $tDomain = $fDomain;
  94. $pCreate_mailbox_username_text = $PALANG['pCreate_mailbox_username_text_error3'];
  95. }
  96. if (empty ($fUsername) or !check_email ($fUsername))
  97. {
  98. $error = 1;
  99. $tUsername = escape_string ($_POST['fUsername']);
  100. $tName = $fName;
  101. $tQuota = $fQuota;
  102. $tDomain = $fDomain;
  103. $pCreate_mailbox_username_text = $PALANG['pCreate_mailbox_username_text_error1'];
  104. }
  105. if (empty ($fPassword) or empty ($fPassword2) or ($fPassword != $fPassword2))
  106. {
  107. if (empty ($fPassword) and empty ($fPassword2) and $CONF['generate_password'] == "YES")
  108. {
  109. $fPassword = generate_password ();
  110. }
  111. else
  112. {
  113. $error = 1;
  114. $tUsername = escape_string ($_POST['fUsername']);
  115. $tName = $fName;
  116. $tQuota = $fQuota;
  117. $tDomain = $fDomain;
  118. $pCreate_mailbox_password_text = $PALANG['pCreate_mailbox_password_text_error'];
  119. }
  120. }
  121. if ($CONF['quota'] == "YES")
  122. {
  123. if (!check_quota ($fQuota, $fDomain))
  124. {
  125. $error = 1;
  126. $tUsername = escape_string ($_POST['fUsername']);
  127. $tName = $fName;
  128. $tQuota = $fQuota;
  129. $tDomain = $fDomain;
  130. $pCreate_mailbox_quota_text = $PALANG['pCreate_mailbox_quota_text_error'];
  131. }
  132. }
  133. $result = db_query ("SELECT * FROM $table_alias WHERE address='$fUsername'");
  134. if ($result['rows'] == 1)
  135. {
  136. $error = 1;
  137. $tUsername = escape_string ($_POST['fUsername']);
  138. $tName = $fName;
  139. $tQuota = $fQuota;
  140. $tDomain = $fDomain;
  141. $pCreate_mailbox_username_text = $PALANG['pCreate_mailbox_username_text_error2'];
  142. }
  143. if ($error != 1)
  144. {
  145. $password = pacrypt ($fPassword);
  146. if ($CONF['domain_path'] == "YES")
  147. {
  148. if ($CONF['domain_in_mailbox'] == "YES")
  149. {
  150. $maildir = $fDomain . "/" . $fUsername . "/";
  151. }
  152. else
  153. {
  154. $maildir = $fDomain . "/" . escape_string (strtolower($_POST['fUsername'])) . "/";
  155. }
  156. }
  157. else
  158. {
  159. $maildir = $fUsername . "/";
  160. }
  161. if (!empty ($fQuota))
  162. {
  163. $quota = multiply_quota ($fQuota);
  164. }
  165. else
  166. {
  167. $quota = 0;
  168. }
  169. if ($fActive == "on")
  170. {
  171. $sqlActive = db_get_boolean(True);
  172. }
  173. else
  174. {
  175. $sqlActive = db_get_boolean(False);
  176. }
  177. if ('pgsql'==$CONF['database_type'])
  178. {
  179. db_query('BEGIN');
  180. }
  181. $result = db_query ("INSERT INTO $table_alias (address,goto,domain,created,modified,active) VALUES ('$fUsername','$fUsername','$fDomain',NOW(),NOW(),'$sqlActive')");
  182. if ($result['rows'] != 1)
  183. {
  184. $tDomain = $fDomain;
  185. $tMessage = $PALANG['pAlias_result_error'] . "<br />($fUsername -> $fUsername)</br />";
  186. }
  187. /*
  188. # TODO: The following code segment is from admin/create-mailbox.php. To be compared/merged with the code from /create-mailbox.php.
  189. Lines starting with /* were inserted to keep this section in commented mode.
  190. if ($result['rows'] != 1)
  191. {
  192. $tDomain = $fDomain;
  193. $tMessage .= $PALANG['pCreate_mailbox_result_error'] . "<br />($fUsername)<br />";
  194. }
  195. else
  196. {
  197. $error=TRUE; // Being pessimistic
  198. if (mailbox_postcreation($fUsername,$fDomain,$maildir))
  199. {
  200. if ('pgsql'==$CONF['database_type'])
  201. {
  202. $result=db_query("COMMIT");
  203. /* should really not be possible: */
  204. /*
  205. if (!$result) die('COMMIT-query failed.');
  206. }
  207. $error=FALSE;
  208. } else {
  209. $tMessage .= $PALANG['pCreate_mailbox_result_error'] . "<br />($fUsername)<br />";
  210. if ('pgsql'==$CONF['database_type'])
  211. {
  212. $result=db_query("ROLLBACK");
  213. /* should really not be possible: */
  214. /*
  215. if (!$result) die('ROLLBACK-query failed.');
  216. } else {
  217. /*
  218. When we cannot count on transactions, we need to move forward, despite
  219. the problems.
  220. */
  221. /*
  222. $error=FALSE;
  223. }
  224. }
  225. if (!$error)
  226. {
  227. db_log ($CONF['admin_email'], $fDomain, 'create_mailbox', $fUsername);
  228. */
  229. /*
  230. TODO: this is the start of /create-mailbox code segment that was originally used in /create-mailbox.php instead
  231. of the above from admin/create-mailbox.php.
  232. To be compared / merged.
  233. */
  234. $result = db_query ("INSERT INTO $table_mailbox (username,password,name,maildir,quota,domain,created,modified,active) VALUES ('$fUsername','$password','$fName','$maildir','$quota','$fDomain',NOW(),NOW(),'$sqlActive')");
  235. if ($result['rows'] != 1 || !mailbox_postcreation($fUsername,$fDomain,$maildir, $quota))
  236. {
  237. $tDomain = $fDomain;
  238. $tMessage .= $PALANG['pCreate_mailbox_result_error'] . "<br />($fUsername)<br />";
  239. db_query('ROLLBACK');
  240. }
  241. else
  242. {
  243. db_query('COMMIT');
  244. db_log ($SESSID_USERNAME, $fDomain, 'create_mailbox', "$fUsername");
  245. /*
  246. TODO: this is the end of /create-mailbox.php code segment
  247. */
  248. $tDomain = $fDomain;
  249. if ($CONF['generate_password'] == "YES")
  250. {
  251. $tMessage .= " / $fPassword)</br />";
  252. }
  253. else
  254. {
  255. if ($CONF['show_password'] == "YES")
  256. {
  257. $tMessage .= " / $fPassword)</br />";
  258. }
  259. else
  260. {
  261. $tMessage .= ")</br />";
  262. }
  263. }
  264. $tQuota = $CONF['maxquota'];
  265. if ($fMail == "on")
  266. {
  267. $fTo = $fUsername;
  268. $fFrom = $SESSID_USERNAME;
  269. $fHeaders = "To: " . $fTo . "\n";
  270. $fHeaders .= "From: " . $fFrom . "\n";
  271. $fHeaders .= "Subject: " . encode_header ($PALANG['pSendmail_subject_text']) . "\n";
  272. $fHeaders .= "MIME-Version: 1.0\n";
  273. $fHeaders .= "Content-Type: text/plain; charset=utf-8\n";
  274. $fHeaders .= "Content-Transfer-Encoding: 8bit\n";
  275. $fHeaders .= $CONF['welcome_text'];
  276. if (!smtp_mail ($fTo, $fFrom, $fHeaders))
  277. {
  278. $tMessage .= "<br />" . $PALANG['pSendmail_result_error'] . "<br />";
  279. }
  280. else
  281. {
  282. $tMessage .= "<br />" . $PALANG['pSendmail_result_success'] . "<br />";
  283. }
  284. }
  285. if (create_mailbox_subfolders($fUsername,$fPassword))
  286. {
  287. $tMessage = $PALANG['pCreate_mailbox_result_success'] . "<br />($fUsername";
  288. } else {
  289. $tMessage = $PALANG['pCreate_mailbox_result_succes_nosubfolders'] . "<br />($fUsername";
  290. }
  291. }
  292. }
  293. }
  294. include ("templates/header.php");
  295. include ("templates/menu.php");
  296. include ("templates/create-mailbox.php");
  297. include ("templates/footer.php");
  298. /* vim: set expandtab softtabstop=3 tabstop=3 shiftwidth=3: */
  299. ?>