MailboxHandler.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  1. <?php
  2. # $Id$
  3. /**
  4. * Simple class to represent a user.
  5. */
  6. class MailboxHandler extends PFAHandler {
  7. protected $db_table = 'mailbox';
  8. protected $id_field = 'username';
  9. protected $domain_field = 'domain';
  10. protected $searchfields = array('username');
  11. # init $this->struct, $this->db_table and $this->id_field
  12. protected function initStruct() {
  13. $passwordReset = (int) Config::bool('forgotten_user_password_reset');
  14. $reset_by_sms = 0;
  15. if ($passwordReset && Config::read_string('sms_send_function')) {
  16. $reset_by_sms = 1;
  17. }
  18. $this->struct = array(
  19. # field name allow display in... type $PALANG label $PALANG description default / options / ...
  20. # editing? form list
  21. 'username' => pacol($this->new, 1, 1, 'mail', 'pEdit_mailbox_username' , '' , '' ),
  22. 'local_part' => pacol($this->new, 0, 0, 'text', 'pEdit_mailbox_username' , '' , '' ),
  23. 'domain' => pacol($this->new, 0, 1, 'enum', '' , '' , '',
  24. /*options*/ $this->allowed_domains ),
  25. # TODO: maildir: display in list is needed to include maildir in SQL result (for post_edit hook)
  26. # TODO: (not a perfect solution, but works for now - maybe we need a separate "include in SELECT query" field?)
  27. 'maildir' => pacol($this->new, 0, 1, 'text', '' , '' , '' ),
  28. 'password' => pacol(1, 1, 0, 'pass', 'password' , 'pCreate_mailbox_password_text' , '' ),
  29. 'password2' => pacol(1, 1, 0, 'pass', 'password_again' , '' , '',
  30. /*options*/ array(),
  31. /*not_in_db*/ 0,
  32. /*dont_write_to_db*/ 1,
  33. /*select*/ 'password as password2'
  34. ),
  35. 'name' => pacol(1, 1, 1, 'text', 'name' , 'pCreate_mailbox_name_text' , '' ),
  36. 'quota' => pacol(1, 1, 1, 'int' , 'pEdit_mailbox_quota' , 'pEdit_mailbox_quota_text' , '' ), # in MB
  37. # read_from_db_postprocess() also sets 'quotabytes' for use in init()
  38. # TODO: read used quota from quota/quota2 table
  39. 'active' => pacol(1, 1, 1, 'bool', 'active' , '' , 1 ),
  40. 'welcome_mail' => pacol($this->new, $this->new, 0, 'bool', 'pCreate_mailbox_mail' , '' , 1,
  41. /*options*/ array(),
  42. /*not_in_db*/ 1 ),
  43. 'phone' => pacol(1, $reset_by_sms, 0, 'text', 'pCreate_mailbox_phone' , 'pCreate_mailbox_phone_desc' , ''),
  44. 'email_other' => pacol(1, $passwordReset, 0, 'mail', 'pCreate_mailbox_email' , 'pCreate_mailbox_email_desc' , ''),
  45. 'token' => pacol(1, 0, 0, 'text', '' , '' ),
  46. 'token_validity' => pacol(1, 0, 0, 'ts', '' , '', date("Y-m-d H:i:s",time())),
  47. 'created' => pacol(0, 0, 1, 'ts', 'created' , '' ),
  48. 'modified' => pacol(0, 0, 1, 'ts', 'last_modified' , '' ),
  49. 'password_expiry' => pacol(0, 0, 1, 'ts', 'password_expiration' , '' ),
  50. # TODO: add virtual 'notified' column and allow to display who received a vacation response?
  51. );
  52. # update allowed quota
  53. if (count($this->struct['domain']['options']) > 0) {
  54. $this->prefill('domain', $this->struct['domain']['options'][0]);
  55. }
  56. }
  57. public function init($id) : bool {
  58. if (!parent::init($id)) {
  59. return false;
  60. }
  61. if ($this->new) {
  62. $currentquota = 0;
  63. } else {
  64. $currentquota = $this->result['quotabytes']; # parent::init called ->view()
  65. }
  66. $this->updateMaxquota($this->domain, $currentquota);
  67. return true; # still here? good.
  68. }
  69. protected function domain_from_id() {
  70. list(/*NULL*/, $domain) = explode('@', $this->id);
  71. return $domain;
  72. }
  73. /**
  74. * show max allowed quota in quota field description
  75. * @param string - domain
  76. * @param int - current quota
  77. */
  78. protected function updateMaxquota($domain, $currentquota) {
  79. if ($domain == '') {
  80. return false;
  81. }
  82. $maxquota = $this->allowed_quota($domain, $currentquota);
  83. if ($maxquota == 0) {
  84. # TODO: show 'unlimited'
  85. # } elseif ($maxquota < 0) {
  86. # TODO: show 'disabled' - at the moment, just shows '-1'
  87. } else {
  88. $this->struct['quota']['desc'] = Config::lang_f('mb_max', "" . $maxquota);
  89. }
  90. }
  91. protected function initMsg() {
  92. $this->msg['error_already_exists'] = 'email_address_already_exists';
  93. $this->msg['error_does_not_exist'] = 'pCreate_mailbox_username_text_error1';
  94. $this->msg['confirm_delete'] = 'confirm_delete_mailbox';
  95. if ($this->new) {
  96. $this->msg['logname'] = 'create_mailbox';
  97. $this->msg['store_error'] = 'pCreate_mailbox_result_error';
  98. $this->msg['successmessage'] = 'pCreate_mailbox_result_success';
  99. } else {
  100. $this->msg['logname'] = 'edit_mailbox';
  101. $this->msg['store_error'] = 'mailbox_update_failed';
  102. $this->msg['successmessage'] = 'mailbox_updated';
  103. }
  104. }
  105. public function webformConfig() {
  106. if ($this->new) { # the webform will display a local_part field + domain dropdown on $new
  107. $this->struct['username']['display_in_form'] = 0;
  108. $this->struct['local_part']['display_in_form'] = 1;
  109. $this->struct['domain']['display_in_form'] = 1;
  110. }
  111. return array(
  112. # $PALANG labels
  113. 'formtitle_create' => 'pCreate_mailbox_welcome',
  114. 'formtitle_edit' => 'pEdit_mailbox_welcome',
  115. 'create_button' => 'add_mailbox',
  116. # various settings
  117. 'required_role' => 'admin',
  118. 'listview' => 'list-virtual.php',
  119. 'early_init' => 0,
  120. 'prefill' => array('domain'),
  121. );
  122. }
  123. protected function validate_new_id() {
  124. if ($this->id == '') {
  125. $this->errormsg[$this->id_field] = Config::lang('pCreate_mailbox_username_text_error1');
  126. return false;
  127. }
  128. $email_check = check_email($this->id);
  129. if ($email_check != '') {
  130. $this->errormsg[$this->id_field] = $email_check;
  131. return false;
  132. }
  133. list(/*NULL*/, $domain) = explode('@', $this->id);
  134. if (!$this->create_allowed($domain)) {
  135. $this->errormsg[] = Config::lang('pCreate_mailbox_username_text_error3');
  136. return false;
  137. }
  138. # check if an alias with this name already exists - if yes, don't allow to create the mailbox
  139. $handler = new AliasHandler(1);
  140. $handler->calledBy('MailboxHandler'); # make sure mailbox creation still works if the alias limit for the domain is hit
  141. if (!$handler->init($this->id)) {
  142. # TODO: keep original error message from AliasHandler
  143. $this->errormsg[] = Config::lang('email_address_already_exists');
  144. return false;
  145. }
  146. return true; # still here? good!
  147. }
  148. /**
  149. * check number of existing mailboxes for this domain - is one more allowed?
  150. */
  151. private function create_allowed($domain) {
  152. $limit = get_domain_properties($domain);
  153. if ($limit['mailboxes'] == 0) {
  154. return true;
  155. } # unlimited
  156. if ($limit['mailboxes'] < 0) {
  157. return false;
  158. } # disabled
  159. if ($limit['mailbox_count'] >= $limit['mailboxes']) {
  160. return false;
  161. }
  162. return true;
  163. }
  164. /**
  165. * merge local_part and domain to address
  166. * called by edit.php (if id_field is editable and hidden in editform) _before_ ->init
  167. */
  168. public function mergeId($values) {
  169. if ($this->struct['local_part']['display_in_form'] == 1 && $this->struct['domain']['display_in_form']) { # webform mode - combine to 'address' field
  170. return $values['local_part'] . '@' . $values['domain'];
  171. } else {
  172. return $values[$this->id_field];
  173. }
  174. }
  175. protected function read_from_db_postprocess($db_result) {
  176. foreach ($db_result as $key => $row) {
  177. if (isset($row['quota']) && is_numeric($row['quota']) && $row['quota'] > -1) { # quota could be disabled in $struct
  178. $db_result[$key]['quotabytes'] = $row['quota'];
  179. $db_result[$key]['quota'] = divide_quota( (int) $row['quota']); # convert quota to MB
  180. } else {
  181. $db_result[$key]['quotabytes'] = -1;
  182. $db_result[$key]['quota'] = -1;
  183. }
  184. }
  185. return $db_result;
  186. }
  187. protected function preSave() : bool {
  188. if (isset($this->values['quota']) && $this->values['quota'] != -1 && is_numeric($this->values['quota'])) {
  189. $multiplier = Config::read_string('quota_multiplier');
  190. if ($multiplier == 0 || !is_numeric($multiplier)) { // or empty string, or null, or false...
  191. $multiplier = 1;
  192. }
  193. $this->values['quota'] = $this->values['quota'] * $multiplier; # convert quota from MB to bytes
  194. }
  195. // Avoid trying to store '' in an integer field
  196. if ($this->values['quota'] === '') {
  197. $this->values['quota'] = 0;
  198. }
  199. $ah = new AliasHandler($this->new, $this->admin_username);
  200. $ah->calledBy('MailboxHandler');
  201. if (!$ah->init($this->id)) {
  202. $arraykeys = array_keys($ah->errormsg);
  203. $this->errormsg[] = $ah->errormsg[$arraykeys[0]]; # TODO: implement this as PFAHandler->firstErrormsg()
  204. return false;
  205. }
  206. $alias_data = array();
  207. if (isset($this->values['active'])) { # might not be set in edit mode
  208. $alias_data['active'] = $this->values['active'];
  209. }
  210. if ($this->new) {
  211. $alias_data['goto'] = array($this->id); # 'goto_mailbox' = 1; # would be technically correct, but setting 'goto' is easier
  212. }
  213. if (!$ah->set($alias_data)) {
  214. $this->errormsg[] = $ah->errormsg[0];
  215. return false;
  216. }
  217. if (!$ah->save()) {
  218. $this->errormsg[] = $ah->errormsg[0];
  219. return false;
  220. }
  221. if (!empty($this->values['password'])) {
  222. // provide some default value to keep MySQL etc happy.
  223. $this->values['password_expiry'] = date('Y-m-d H:i', strtotime("+365 days"));
  224. if (Config::bool('password_expiration')) {
  225. $domain_dirty = $this->domain_from_id();
  226. $domain = trim($domain_dirty, "`'"); // naive assumption it is ' escaping.
  227. $password_expiration_value = (int)get_password_expiration_value($domain);
  228. $this->values['password_expiry'] = date('Y-m-d H:i', strtotime("+$password_expiration_value day"));
  229. }
  230. }
  231. return true;
  232. }
  233. protected function setmore(array $values) {
  234. if (array_key_exists('quota', $this->values)) {
  235. $this->values['quota'] = (int)$this->values['quota'];
  236. }
  237. }
  238. // Could perhaps also use _validate_local_part($new_value) { .... }
  239. public function set(array $values) {
  240. // See: https://github.com/postfixadmin/postfixadmin/issues/282 - ensure the 'local_part' does not contain an @ sign.
  241. $ok = true;
  242. if (isset($values['local_part']) && strpos($values['local_part'], '@')) {
  243. $this->errormsg['local_part'] = Config::lang('pCreate_mailbox_local_part_error');
  244. $ok = false;
  245. }
  246. return $ok && parent::set($values);
  247. }
  248. protected function postSave() : bool {
  249. if ($this->new) {
  250. if (!$this->mailbox_post_script()) {
  251. # return false; # TODO: should this be fatal?
  252. }
  253. if ($this->values['welcome_mail'] == true) {
  254. if (!$this->send_welcome_mail()) {
  255. # return false; # TODO: should this be fatal?
  256. }
  257. }
  258. if (!$this->create_mailbox_subfolders()) {
  259. $this->infomsg[] = Config::lang_f('pCreate_mailbox_result_succes_nosubfolders', $this->id);
  260. }
  261. } else { # edit mode
  262. # alias active status is updated in before_store()
  263. # postedit hook
  264. # TODO: implement a poststore() function? - would make handling of old and new values much easier...
  265. $old_mh = new MailboxHandler();
  266. if (!$old_mh->init($this->id)) {
  267. $this->errormsg[] = $old_mh->errormsg[0];
  268. } elseif (!$old_mh->view()) {
  269. $this->errormsg[] = $old_mh->errormsg[0];
  270. } else {
  271. $oldvalues = $old_mh->result();
  272. $this->values['maildir'] = $oldvalues['maildir'];
  273. if (isset($this->values['quota'])) {
  274. $quota = $this->values['quota'];
  275. } else {
  276. $quota = $oldvalues['quota'];
  277. }
  278. if (!$this->mailbox_post_script()) {
  279. # TODO: should this be fatal?
  280. }
  281. }
  282. }
  283. return true; # even if a hook failed, mark the overall operation as OK
  284. }
  285. public function delete() {
  286. if (! $this->view()) {
  287. $this->errormsg[] = Config::Lang('pFetchmail_invalid_mailbox'); # TODO: can users hit this message at all? init() should already fail...
  288. return false;
  289. }
  290. # the correct way would be to delete the alias and fetchmail entries with *Handler before
  291. # deleting the mailbox, but it's easier and a bit faster to do it on the database level.
  292. # cleaning up all tables doesn't hurt, even if vacation or displaying the quota is disabled
  293. db_delete('fetchmail', 'mailbox', $this->id);
  294. db_delete('vacation', 'email', $this->id);
  295. db_delete('vacation_notification', 'on_vacation', $this->id); # should be caught by cascade, if PgSQL
  296. db_delete('quota', 'username', $this->id);
  297. db_delete('quota2', 'username', $this->id);
  298. db_delete('alias', 'address', $this->id);
  299. db_delete($this->db_table, $this->id_field, $this->id); # finally delete the mailbox
  300. if (!$this->mailbox_postdeletion()) {
  301. $this->errormsg[] = Config::Lang('mailbox_postdel_failed');
  302. }
  303. list(/*NULL*/, $domain) = explode('@', $this->id);
  304. db_log($domain, 'delete_mailbox', $this->id);
  305. $this->infomsg[] = Config::Lang_f('pDelete_delete_success', $this->id);
  306. return true;
  307. }
  308. protected function _prefill_domain($field, $val) {
  309. if (in_array($val, $this->struct[$field]['options'])) {
  310. $this->struct[$field]['default'] = $val;
  311. $this->updateMaxquota($val, 0);
  312. }
  313. }
  314. /**
  315. * check if quota is allowed
  316. */
  317. protected function _validate_quota($field, $val) {
  318. if (!$this->check_quota($val)) {
  319. $this->errormsg[$field] = Config::lang('pEdit_mailbox_quota_text_error');
  320. return false;
  321. }
  322. return true;
  323. }
  324. /**
  325. * - compare password / password2 field (error message will be displayed at password2 field)
  326. * - autogenerate password if enabled in config and $new
  327. * - display password on $new if enabled in config or autogenerated
  328. */
  329. protected function _validate_password($field, $val) {
  330. if (!$this->_validate_password2($field, $val)) {
  331. return false;
  332. }
  333. if ($this->new && Config::read('generate_password') == 'YES' && $val == '') {
  334. # auto-generate new password
  335. unset($this->errormsg[$field]); # remove "password too short" error message
  336. $val = generate_password();
  337. $this->values[$field] = $val; # we are doing this "behind the back" of set()
  338. $this->infomsg[] = Config::Lang('password') . ": $val";
  339. return false; # to avoid that set() overwrites $this->values[$field]
  340. } elseif ($this->new && Config::read('show_password') == 'YES') {
  341. $this->infomsg[] = Config::Lang('password') . ": $val";
  342. }
  343. return true; # still here? good.
  344. }
  345. /**
  346. * compare password / password2 field
  347. * error message will be displayed at the password2 field
  348. */
  349. protected function _validate_password2($field, $val) {
  350. return $this->compare_password_fields('password', 'password2');
  351. }
  352. /**
  353. * on $this->new, set localpart based on address
  354. */
  355. protected function _missing_local_part($field) {
  356. list($local_part, $domain) = explode('@', $this->id);
  357. $this->RAWvalues['local_part'] = $local_part;
  358. }
  359. /**
  360. * on $this->new, set domain based on address
  361. */
  362. protected function _missing_domain($field) {
  363. list($local_part, $domain) = explode('@', $this->id);
  364. $this->RAWvalues['domain'] = $domain;
  365. }
  366. # TODO: read used quota from quota/quota2 table, then enable _formatted_quota()
  367. # public function _formatted_quota ($item) { return $item['used_quota'] . ' / ' . $item['quota'] ; }
  368. /**
  369. * calculate maildir path for the mailbox
  370. */
  371. protected function _missing_maildir($field) {
  372. list($local_part, $domain) = explode('@', $this->id);
  373. $maildir_name_hook = Config::read('maildir_name_hook');
  374. if (is_string($maildir_name_hook) && $maildir_name_hook != 'NO' && function_exists($maildir_name_hook)) {
  375. $maildir = $maildir_name_hook($domain, $this->id);
  376. } elseif (Config::bool('domain_path')) {
  377. if (Config::bool('domain_in_mailbox')) {
  378. $maildir = $domain . "/" . $this->id . "/";
  379. } else {
  380. $maildir = $domain . "/" . $local_part . "/";
  381. }
  382. } else {
  383. # If $CONF['domain_path'] is set to NO, $CONF['domain_in_mailbox] is forced to YES.
  384. # Otherwise user@example.com and user@foo.bar would be mixed up in the same maildir "user/".
  385. $maildir = $this->id . "/";
  386. }
  387. $this->RAWvalues['maildir'] = $maildir;
  388. }
  389. private function send_welcome_mail() {
  390. $fTo = $this->id;
  391. $fFrom = smtp_get_admin_email();
  392. if (empty($fFrom) || $fFrom == 'CLI') {
  393. $fFrom = $this->id;
  394. }
  395. $fSubject = Config::lang('pSendmail_subject_text');
  396. $fBody = Config::read('welcome_text');
  397. if (!smtp_mail($fTo, $fFrom, $fSubject, smtp_get_admin_password(), $fBody)) {
  398. $this->errormsg[] = Config::lang_f('pSendmail_result_error', $this->id);
  399. return false;
  400. }
  401. return true;
  402. }
  403. /**
  404. * Check if the user is creating a mailbox within the quota limits of the domain
  405. *
  406. * @param int $quota - quota wanted for the mailbox
  407. * @return boolean - true if requested quota is OK, otherwise false
  408. * @todo merge with allowed_quota?
  409. */
  410. protected function check_quota($quota) {
  411. if (!Config::bool('quota')) {
  412. return true; # enforcing quotas is disabled - just allow it
  413. }
  414. $quota = (int) $quota;
  415. list(/*NULL*/, $domain) = explode('@', $this->id);
  416. $limit = get_domain_properties($domain);
  417. if (($limit['maxquota'] < 0) and ($quota < 0)) {
  418. return true; # maxquota and $quota are both disabled -> OK, no need for more checks
  419. }
  420. if (($limit['maxquota'] > 0) and ($quota == 0)) {
  421. return false; # mailbox with unlimited quota on a domain with maxquota restriction -> not allowed, no more checks needed
  422. }
  423. if ($limit['maxquota'] != 0 && $quota > $limit['maxquota']) {
  424. return false; # mailbox bigger than maxquota restriction (and maxquota != unlimited) -> not allowed, no more checks needed
  425. }
  426. # TODO: detailed error message ("domain quota exceeded", "mailbox quota too big" etc.) via flash_error? Or "available quota: xxx MB"?
  427. if (!Config::bool('domain_quota')) {
  428. return true; # enforcing domain_quota is disabled - just allow it
  429. } elseif ($limit['quota'] <= 0) { # TODO: CHECK - 0 (unlimited) is fine, not sure about <= -1 (disabled)...
  430. $rval = true;
  431. } elseif ($quota == 0) { # trying to create an unlimited mailbox, but domain quota is set
  432. return false;
  433. } else {
  434. $table_mailbox = table_by_key('mailbox');
  435. $query = "SELECT SUM(quota) as sum FROM $table_mailbox WHERE domain = ? AND username != ?";
  436. $rows = db_query_all($query, array($domain, $this->id));
  437. $cur_quota_total = divide_quota($rows[0]['sum']); # convert to MB
  438. if (($quota + $cur_quota_total) > $limit['quota']) {
  439. $rval = false;
  440. } else {
  441. $rval = true;
  442. }
  443. }
  444. return $rval;
  445. }
  446. /**
  447. * Get allowed maximum quota for a mailbox
  448. *
  449. * @param string $domain
  450. * @param int $current_user_quota (in bytes)
  451. * @return int allowed maximum quota (in MB)
  452. */
  453. protected function allowed_quota($domain, $current_user_quota) {
  454. if (!Config::bool('quota')) {
  455. return 0; # quota disabled means no limits - no need for more checks
  456. }
  457. $domain_properties = get_domain_properties($domain);
  458. $tMaxquota = $domain_properties['maxquota'];
  459. if (Config::bool('domain_quota') && $domain_properties['quota']) {
  460. $dquota = $domain_properties['quota'] - $domain_properties['total_quota'] + divide_quota($current_user_quota);
  461. if ($dquota < $tMaxquota) {
  462. $tMaxquota = $dquota;
  463. }
  464. if ($tMaxquota == 0) {
  465. $tMaxquota = $dquota;
  466. }
  467. }
  468. return $tMaxquota;
  469. }
  470. /**
  471. * Called after a mailbox has been created or edited in the DBMS.
  472. *
  473. * @return boolean success/failure status
  474. */
  475. protected function mailbox_post_script() {
  476. if ($this->new) {
  477. $cmd = Config::read_string('mailbox_postcreation_script');
  478. $warnmsg = Config::Lang('mailbox_postcreate_failed');
  479. } else {
  480. $cmd = Config::read_string('mailbox_postedit_script');
  481. $warnmsg = Config::Lang('mailbox_postedit_failed');
  482. }
  483. if (empty($cmd)) {
  484. return true;
  485. } # nothing to do
  486. list(/*NULL*/, $domain) = explode('@', $this->id);
  487. $quota = $this->values['quota'];
  488. if (empty($this->id) || empty($domain) || empty($this->values['maildir'])) {
  489. trigger_error('In '.__FUNCTION__.': empty username, domain and/or maildir parameter', E_USER_ERROR);
  490. return false;
  491. }
  492. $cmdarg1=escapeshellarg($this->id);
  493. $cmdarg2=escapeshellarg($domain);
  494. $cmdarg3=escapeshellarg($this->values['maildir']);
  495. if ($quota <= 0) {
  496. $quota = 0;
  497. } # TODO: check if this is correct behaviour
  498. $cmdarg4 = escapeshellarg("" . $quota);
  499. $command= "$cmd $cmdarg1 $cmdarg2 $cmdarg3 $cmdarg4";
  500. $retval=0;
  501. $output=array();
  502. $firstline='';
  503. $firstline=exec($command, $output, $retval);
  504. if (0!=$retval) {
  505. error_log("Running $command yielded return value=$retval, first line of output=$firstline");
  506. $this->errormsg[] = $warnmsg;
  507. return false;
  508. }
  509. return true;
  510. }
  511. /**
  512. * Called after a mailbox has been deleted
  513. *
  514. * @return boolean true on success, false on failure
  515. * also adds a detailed error message to $this->errormsg[]
  516. */
  517. protected function mailbox_postdeletion() {
  518. $cmd = Config::read_string('mailbox_postdeletion_script');
  519. if (empty($cmd)) {
  520. return true;
  521. }
  522. list(/*NULL*/, $domain) = explode('@', $this->id);
  523. if (empty($this->id) || empty($domain)) {
  524. $this->errormsg[] = 'Empty username and/or domain parameter in mailbox_postdeletion';
  525. return false;
  526. }
  527. $cmdarg1=escapeshellarg($this->id);
  528. $cmdarg2=escapeshellarg($domain);
  529. $command = "$cmd $cmdarg1 $cmdarg2";
  530. $retval=0;
  531. $output=array();
  532. $firstline='';
  533. $firstline=exec($command, $output, $retval);
  534. if (0!=$retval) {
  535. error_log("Running $command yielded return value=$retval, first line of output=$firstline");
  536. $this->errormsg[] = 'Problems running mailbox postdeletion script!';
  537. return false;
  538. }
  539. return true;
  540. }
  541. /**
  542. * Called by postSave() after a mailbox has been created.
  543. * Immediately returns, unless configuration indicates
  544. * that one or more sub-folders should be created.
  545. *
  546. * Triggers E_USER_ERROR if configuration error is detected.
  547. *
  548. * If IMAP login fails, the problem is logged to the system log
  549. * (such as /var/log/httpd/error_log), and the function returns
  550. * FALSE.
  551. *
  552. * Doesn't clean up, if only some of the folders could be
  553. * created.
  554. *
  555. * @return boolean TRUE if everything succeeds, FALSE on all errors
  556. */
  557. protected function create_mailbox_subfolders() {
  558. $create_mailbox_subdirs = Config::read('create_mailbox_subdirs');
  559. if (empty($create_mailbox_subdirs)) {
  560. return true;
  561. }
  562. if (!function_exists('imap_open')) {
  563. trigger_error('imap_open function not present; cannot create_mailbox_subdirs');
  564. return false;
  565. }
  566. if (!is_array($create_mailbox_subdirs)) {
  567. trigger_error('create_mailbox_subdirs must be an array', E_USER_ERROR);
  568. return false;
  569. }
  570. $s_host = Config::read_string('create_mailbox_subdirs_host');
  571. if (empty($s_host)) {
  572. trigger_error('An IMAP/POP server host ($CONF["create_mailbox_subdirs_host"]) must be configured, if sub-folders are to be created', E_USER_ERROR);
  573. return false;
  574. }
  575. $s_options='';
  576. $create_mailbox_subdirs_hostoptions = Config::read('create_mailbox_subdirs_hostoptions');
  577. if (!empty($create_mailbox_subdirs_hostoptions)) {
  578. if (!is_array($create_mailbox_subdirs_hostoptions)) {
  579. trigger_error('The $CONF["create_mailbox_subdirs_hostoptions"] parameter must be an array', E_USER_ERROR);
  580. return false;
  581. }
  582. foreach ($create_mailbox_subdirs_hostoptions as $o) {
  583. $s_options.='/'.$o;
  584. }
  585. }
  586. $s_port='';
  587. if (Config::has('create_mailbox_subdirs_hostport')) {
  588. $create_mailbox_subdirs_hostport = Config::read('create_mailbox_subdirs_hostport');
  589. if (!empty($create_mailbox_subdirs_hostport)) {
  590. $s_port = $create_mailbox_subdirs_hostport;
  591. if (intval($s_port)!=$s_port) {
  592. trigger_error('The $CONF["create_mailbox_subdirs_hostport"] parameter must be an integer', E_USER_ERROR);
  593. return false;
  594. }
  595. $s_port=':'.$s_port;
  596. }
  597. }
  598. $s='{'.$s_host.$s_port.$s_options.'}';
  599. sleep(1); # give the mail triggering the mailbox creation a chance to do its job
  600. $i=@imap_open($s, $this->id, $this->values['password']);
  601. if (false==$i) {
  602. error_log('Could not log into IMAP/POP server: ' . $this->id . ': ' . imap_last_error());
  603. return false;
  604. }
  605. $s_prefix = Config::read_string('create_mailbox_subdirs_prefix');
  606. foreach ($create_mailbox_subdirs as $f) {
  607. $f='{'.$s_host.'}'.$s_prefix.$f;
  608. $res=imap_createmailbox($i, $f);
  609. if (!$res) {
  610. error_log('Could not create IMAP folder $f: ' . $this->id . ': ' . imap_last_error());
  611. @imap_close($i);
  612. return false;
  613. }
  614. @imap_subscribe($i, $f);
  615. }
  616. @imap_close($i);
  617. return true;
  618. }
  619. #TODO: more self explaining language strings!
  620. }
  621. /* vim: set expandtab softtabstop=4 tabstop=4 shiftwidth=4: */