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

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