PageRenderTime 53ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/create-mailbox.php

https://github.com/albanpeignier/postfixadmin
PHP | 337 lines | 210 code | 33 blank | 94 comment | 53 complexity | 6b041e50ce7aaf4aeecb3c0aa4ffec57 MD5 | raw file
  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: create-mailbox.php 414 2008-07-23 11:41:45Z GingerDog $
  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. if (empty ($fPassword) or empty ($fPassword2) or ($fPassword != $fPassword2))
  103. {
  104. if (empty ($fPassword) and empty ($fPassword2) and $CONF['generate_password'] == "YES")
  105. {
  106. $fPassword = generate_password ();
  107. }
  108. else
  109. {
  110. $error = 1;
  111. $tUsername = escape_string ($_POST['fUsername']);
  112. $tName = $fName;
  113. $tQuota = $fQuota;
  114. $tDomain = $fDomain;
  115. $pCreate_mailbox_password_text = $PALANG['pCreate_mailbox_password_text_error'];
  116. }
  117. }
  118. if ($CONF['quota'] == "YES")
  119. {
  120. if (!check_quota ($fQuota, $fDomain))
  121. {
  122. $error = 1;
  123. $tUsername = escape_string ($_POST['fUsername']);
  124. $tName = $fName;
  125. $tQuota = $fQuota;
  126. $tDomain = $fDomain;
  127. $pCreate_mailbox_quota_text = $PALANG['pCreate_mailbox_quota_text_error'];
  128. }
  129. }
  130. $result = db_query ("SELECT * FROM $table_alias WHERE address='$fUsername'");
  131. if ($result['rows'] == 1)
  132. {
  133. $error = 1;
  134. $tUsername = escape_string ($_POST['fUsername']);
  135. $tName = $fName;
  136. $tQuota = $fQuota;
  137. $tDomain = $fDomain;
  138. $pCreate_mailbox_username_text = $PALANG['pCreate_mailbox_username_text_error2'];
  139. }
  140. if ($error != 1)
  141. {
  142. $password = pacrypt ($fPassword);
  143. if ($CONF['domain_path'] == "YES")
  144. {
  145. if ($CONF['domain_in_mailbox'] == "YES")
  146. {
  147. $maildir = $fDomain . "/" . $fUsername . "/";
  148. }
  149. else
  150. {
  151. $maildir = $fDomain . "/" . escape_string (strtolower($_POST['fUsername'])) . "/";
  152. }
  153. }
  154. else
  155. {
  156. $maildir = $fUsername . "/";
  157. }
  158. if (!empty ($fQuota))
  159. {
  160. $quota = multiply_quota ($fQuota);
  161. }
  162. else
  163. {
  164. $quota = 0;
  165. }
  166. if ($fActive == "on")
  167. {
  168. $sqlActive = db_get_boolean(True);
  169. }
  170. else
  171. {
  172. $sqlActive = db_get_boolean(False);
  173. }
  174. if ('pgsql'==$CONF['database_type'])
  175. {
  176. db_query('BEGIN');
  177. }
  178. $result = db_query ("INSERT INTO $table_alias (address,goto,domain,created,modified,active) VALUES ('$fUsername','$fUsername','$fDomain',NOW(),NOW(),'$sqlActive')");
  179. if ($result['rows'] != 1)
  180. {
  181. $tDomain = $fDomain;
  182. $tMessage = $PALANG['pAlias_result_error'] . "<br />($fUsername -> $fUsername)</br />";
  183. }
  184. /*
  185. # TODO: The following code segment is from admin/create-mailbox.php. To be compared/merged with the code from /create-mailbox.php.
  186. Lines starting with /* were inserted to keep this section in commented mode.
  187. if ($result['rows'] != 1)
  188. {
  189. $tDomain = $fDomain;
  190. $tMessage .= $PALANG['pCreate_mailbox_result_error'] . "<br />($fUsername)<br />";
  191. }
  192. else
  193. {
  194. $error=TRUE; // Being pessimistic
  195. if (mailbox_postcreation($fUsername,$fDomain,$maildir))
  196. {
  197. if ('pgsql'==$CONF['database_type'])
  198. {
  199. $result=db_query("COMMIT");
  200. /* should really not be possible: */
  201. /*
  202. if (!$result) die('COMMIT-query failed.');
  203. }
  204. $error=FALSE;
  205. } else {
  206. $tMessage .= $PALANG['pCreate_mailbox_result_error'] . "<br />($fUsername)<br />";
  207. if ('pgsql'==$CONF['database_type'])
  208. {
  209. $result=db_query("ROLLBACK");
  210. /* should really not be possible: */
  211. /*
  212. if (!$result) die('ROLLBACK-query failed.');
  213. } else {
  214. /*
  215. When we cannot count on transactions, we need to move forward, despite
  216. the problems.
  217. */
  218. /*
  219. $error=FALSE;
  220. }
  221. }
  222. if (!$error)
  223. {
  224. db_log ($CONF['admin_email'], $fDomain, 'create_mailbox', $fUsername);
  225. */
  226. /*
  227. TODO: this is the start of /create-mailbox code segment that was originally used in /create-mailbox.php instead
  228. of the above from admin/create-mailbox.php.
  229. To be compared / merged.
  230. */
  231. $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')");
  232. if ($result['rows'] != 1 || !mailbox_postcreation($fUsername,$fDomain,$maildir, $quota))
  233. {
  234. $tDomain = $fDomain;
  235. $tMessage .= $PALANG['pCreate_mailbox_result_error'] . "<br />($fUsername)<br />";
  236. db_query('ROLLBACK');
  237. }
  238. else
  239. {
  240. db_query('COMMIT');
  241. db_log ($SESSID_USERNAME, $fDomain, 'create_mailbox', "$fUsername");
  242. /*
  243. TODO: this is the end of /create-mailbox.php code segment
  244. */
  245. $tDomain = $fDomain;
  246. if ($CONF['generate_password'] == "YES")
  247. {
  248. $tMessage .= " / $fPassword)</br />";
  249. }
  250. else
  251. {
  252. if ($CONF['show_password'] == "YES")
  253. {
  254. $tMessage .= " / $fPassword)</br />";
  255. }
  256. else
  257. {
  258. $tMessage .= ")</br />";
  259. }
  260. }
  261. $tQuota = $CONF['maxquota'];
  262. if ($fMail == "on")
  263. {
  264. $fTo = $fUsername;
  265. $fFrom = $SESSID_USERNAME;
  266. $fHeaders = "To: " . $fTo . "\n";
  267. $fHeaders .= "From: " . $fFrom . "\n";
  268. $fHeaders .= "Subject: " . encode_header ($PALANG['pSendmail_subject_text']) . "\n";
  269. $fHeaders .= "MIME-Version: 1.0\n";
  270. $fHeaders .= "Content-Type: text/plain; charset=utf-8\n";
  271. $fHeaders .= "Content-Transfer-Encoding: 8bit\n";
  272. $fHeaders .= $CONF['welcome_text'];
  273. if (!smtp_mail ($fTo, $fFrom, $fHeaders))
  274. {
  275. $tMessage .= "<br />" . $PALANG['pSendmail_result_error'] . "<br />";
  276. }
  277. else
  278. {
  279. $tMessage .= "<br />" . $PALANG['pSendmail_result_success'] . "<br />";
  280. }
  281. }
  282. if (create_mailbox_subfolders($fUsername,$fPassword))
  283. {
  284. $tMessage = $PALANG['pCreate_mailbox_result_success'] . "<br />($fUsername";
  285. } else {
  286. $tMessage = $PALANG['pCreate_mailbox_result_succes_nosubfolders'] . "<br />($fUsername";
  287. }
  288. }
  289. }
  290. }
  291. include ("templates/header.php");
  292. include ("templates/menu.php");
  293. include ("templates/create-mailbox.php");
  294. include ("templates/footer.php");
  295. /* vim: set expandtab softtabstop=3 tabstop=3 shiftwidth=3: */
  296. ?>