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.

MailboxHandler.php 28KB

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