Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

AliasHandler.php 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. <?php
  2. # $Id$
  3. /**
  4. * Handlers User level alias actions - e.g. add alias, get aliases, update etc.
  5. * @property $username name of alias
  6. * @property $return return of methods
  7. */
  8. class AliasHandler extends PFAHandler {
  9. protected $db_table = 'alias';
  10. protected $id_field = 'address';
  11. protected $domain_field = 'domain';
  12. protected $searchfields = array('address', 'goto');
  13. /**
  14. *
  15. * @public
  16. */
  17. public $return = null;
  18. protected function initStruct() {
  19. # hide 'goto_mailbox' if $this->new
  20. # (for existing aliases, init() hides it for non-mailbox aliases)
  21. $mbgoto = 1 - $this->new;
  22. $this->struct=array(
  23. # field name allow display in... type $PALANG label $PALANG description default / ...
  24. # editing? form list
  25. 'status' => pacol(0, 0, 0, 'html', '' , '' , '', '',
  26. array('not_in_db' => 1) ),
  27. 'address' => pacol($this->new, 1, 1, 'mail', 'alias' , 'pCreate_alias_catchall_text' ),
  28. 'localpart' => pacol($this->new, 0, 0, 'text', 'alias' , 'pCreate_alias_catchall_text' , '',
  29. /*options*/ '',
  30. /*not_in_db*/ 1 ),
  31. 'domain' => pacol($this->new, 0, 1, 'enum', '' , '' , '',
  32. /*options*/ $this->allowed_domains ),
  33. 'goto' => pacol(1, 1, 1, 'txtl', 'to' , 'pEdit_alias_help' , array() ),
  34. 'is_mailbox' => pacol(0, 0, 1, 'int', '' , '' , 0 ,
  35. # technically 'is_mailbox' is bool, but the automatic bool conversion breaks the query. Flagging it as int avoids this problem.
  36. # Maybe having a vbool type (without the automatic conversion) would be cleaner - we'll see if we need it.
  37. /*options*/ '',
  38. /*not_in_db*/ 0,
  39. /*dont_write_to_db*/ 1,
  40. /*select*/ 'coalesce(__is_mailbox,0) as is_mailbox' ),
  41. /*extrafrom set via set_is_mailbox_extrafrom() */
  42. '__mailbox_username' => pacol( 0, 0, 1, 'vtxt', '' , '' , 0), # filled via is_mailbox
  43. 'goto_mailbox' => pacol($mbgoto, $mbgoto,$mbgoto,'bool', 'pEdit_alias_forward_and_store' , '' , 0,
  44. /*options*/ '',
  45. /*not_in_db*/ 1 ), # read_from_db_postprocess() sets the value
  46. 'on_vacation' => pacol(1, 0, 1, 'bool', 'pUsersMenu_vacation' , '' , 0 ,
  47. /*options*/ '',
  48. /*not_in_db*/ 1 ), # read_from_db_postprocess() sets the value - TODO: read active flag from vacation table instead?
  49. 'created' => pacol(0, 0, 0, 'ts', 'created' , '' ),
  50. 'modified' => pacol(0, 0, 1, 'ts', 'last_modified' , '' ),
  51. 'active' => pacol(1, 1, 1, 'bool', 'active' , '' , 1 ),
  52. '_can_edit' => pacol(0, 0, 1, 'vnum', '' , '' , 0 , '',
  53. array('select' => '1 as _can_edit') ),
  54. '_can_delete' => pacol(0, 0, 1, 'vnum', '' , '' , 0 , '',
  55. array('select' => '1 as _can_delete') ), # read_from_db_postprocess() updates the value
  56. # aliases listed in $CONF[default_aliases] are read-only for domain admins if $CONF[special_alias_control] is NO.
  57. );
  58. $this->set_is_mailbox_extrafrom();
  59. }
  60. /*
  61. * set $this->struct['is_mailbox']['extrafrom'] based on the search conditions.
  62. * If a listing for a specific domain is requested, optimize the subquery to only return mailboxes from that domain.
  63. * This doesn't change the result of the main query, but improves the performance a lot on setups with lots of mailboxes.
  64. * When using this function to optimize the is_mailbox extrafrom, don't forget to reset it to the default value
  65. * (all domains for this admin) afterwards.
  66. */
  67. private function set_is_mailbox_extrafrom($condition=array(), $searchmode=array()) {
  68. $extrafrom = 'LEFT JOIN ( ' .
  69. ' SELECT 1 as __is_mailbox, username as __mailbox_username ' .
  70. ' FROM ' . table_by_key('mailbox') .
  71. ' WHERE username IS NOT NULL ';
  72. if (isset($condition['domain']) && !isset($searchmode['domain']) && in_array($condition['domain'], $this->allowed_domains)) {
  73. # listing for a specific domain, so restrict subquery to that domain
  74. $extrafrom .= ' AND ' . db_in_clause($this->domain_field, array($condition['domain']));
  75. } else {
  76. # restrict subquery to all domains accessible to this admin
  77. $extrafrom .= ' AND ' . db_in_clause($this->domain_field, $this->allowed_domains);
  78. }
  79. $extrafrom .= ' ) AS __mailbox ON __mailbox_username = address';
  80. $this->struct['is_mailbox']['extrafrom'] = $extrafrom;
  81. }
  82. protected function initMsg() {
  83. $this->msg['error_already_exists'] = 'email_address_already_exists';
  84. $this->msg['error_does_not_exist'] = 'alias_does_not_exist';
  85. $this->msg['confirm_delete'] = 'confirm_delete_alias';
  86. $this->msg['list_header'] = 'pOverview_alias_title';
  87. if ($this->new) {
  88. $this->msg['logname'] = 'create_alias';
  89. $this->msg['store_error'] = 'pCreate_alias_result_error';
  90. $this->msg['successmessage'] = 'pCreate_alias_result_success';
  91. } else {
  92. $this->msg['logname'] = 'edit_alias';
  93. $this->msg['store_error'] = 'pEdit_alias_result_error';
  94. $this->msg['successmessage'] = 'alias_updated';
  95. }
  96. }
  97. public function webformConfig() {
  98. if ($this->new) { # the webform will display a localpart field + domain dropdown on $new
  99. $this->struct['address']['display_in_form'] = 0;
  100. $this->struct['localpart']['display_in_form'] = 1;
  101. $this->struct['domain']['display_in_form'] = 1;
  102. }
  103. if (Config::bool('show_status')) {
  104. $this->struct['status']['display_in_list'] = 1;
  105. $this->struct['status']['label'] = ' ';
  106. }
  107. return array(
  108. # $PALANG labels
  109. 'formtitle_create' => 'pMain_create_alias',
  110. 'formtitle_edit' => 'pEdit_alias_welcome',
  111. 'create_button' => 'add_alias',
  112. # various settings
  113. 'required_role' => 'admin',
  114. 'listview' => 'list-virtual.php',
  115. 'early_init' => 0,
  116. 'prefill' => array('domain'),
  117. );
  118. }
  119. /**
  120. * AliasHandler needs some special handling in init() and therefore overloads the function.
  121. * It also calls parent::init()
  122. */
  123. public function init($id) {
  124. @list($local_part, $domain) = explode('@', $id); # supress error message if $id doesn't contain '@'
  125. if ($local_part == '*') { # catchall - postfix expects '@domain', not '*@domain'
  126. $id = '@' . $domain;
  127. }
  128. $retval = parent::init($id);
  129. if (!$retval) {
  130. return false;
  131. } # parent::init() failed, no need to continue
  132. # hide 'goto_mailbox' for non-mailbox aliases
  133. # parent::init called view() before, so we can rely on having $this->result filled
  134. # (only validate_new_id() is called from parent::init and could in theory change $this->result)
  135. if ($this->new || $this->result['is_mailbox'] == 0) {
  136. $this->struct['goto_mailbox']['editable'] = 0;
  137. $this->struct['goto_mailbox']['display_in_form'] = 0;
  138. $this->struct['goto_mailbox']['display_in_list'] = 0;
  139. }
  140. if (!$this->new && $this->result['is_mailbox'] && $this->admin_username != ''&& !authentication_has_role('global-admin')) {
  141. # domain admins are not allowed to change mailbox alias $CONF['alias_control_admin'] = NO
  142. # TODO: apply the same restriction to superadmins?
  143. if (!Config::bool('alias_control_admin')) {
  144. # TODO: make translateable
  145. $this->errormsg[] = "Domain administrators do not have the ability to edit user's aliases (check config.inc.php - alias_control_admin)";
  146. return false;
  147. }
  148. }
  149. return $retval;
  150. }
  151. protected function domain_from_id() {
  152. list(/*NULL*/, $domain) = explode('@', $this->id);
  153. return $domain;
  154. }
  155. protected function validate_new_id() {
  156. if ($this->id == '') {
  157. $this->errormsg[$this->id_field] = Config::lang('pCreate_alias_address_text_error1');
  158. return false;
  159. }
  160. list($local_part, $domain) = explode('@', $this->id);
  161. if (!$this->create_allowed($domain)) {
  162. $this->errormsg[$this->id_field] = Config::lang('pCreate_alias_address_text_error3');
  163. return false;
  164. }
  165. # TODO: already checked in set() - does it make sense to check it here also? Only advantage: it's an early check
  166. # if (!in_array($domain, $this->allowed_domains)) {
  167. # $this->errormsg[] = Config::lang('pCreate_alias_address_text_error1');
  168. # return false;
  169. # }
  170. if ($local_part == '') { # catchall
  171. $valid = true;
  172. } else {
  173. $email_check = check_email($this->id);
  174. if ($email_check == '') {
  175. $valid = true;
  176. } else {
  177. $this->errormsg[$this->id_field] = $email_check;
  178. $valid = false;
  179. }
  180. }
  181. return $valid;
  182. }
  183. /**
  184. * check number of existing aliases for this domain - is one more allowed?
  185. */
  186. private function create_allowed($domain) {
  187. if ($this->called_by == 'MailboxHandler') {
  188. return true;
  189. } # always allow creating an alias for a mailbox
  190. $limit = get_domain_properties($domain);
  191. if ($limit['aliases'] == 0) {
  192. return true;
  193. } # unlimited
  194. if ($limit['aliases'] < 0) {
  195. return false;
  196. } # disabled
  197. if ($limit['alias_count'] >= $limit['aliases']) {
  198. return false;
  199. }
  200. return true;
  201. }
  202. /**
  203. * merge localpart and domain to address
  204. * called by edit.php (if id_field is editable and hidden in editform) _before_ ->init
  205. */
  206. public function mergeId($values) {
  207. if ($this->struct['localpart']['display_in_form'] == 1 && $this->struct['domain']['display_in_form']) { # webform mode - combine to 'address' field
  208. if (empty($values['localpart']) || empty($values['domain'])) { # localpart or domain not set
  209. return "";
  210. }
  211. if ($values['localpart'] == '*') {
  212. $values['localpart'] = '';
  213. } # catchall
  214. return $values['localpart'] . '@' . $values['domain'];
  215. } else {
  216. return $values[$this->id_field];
  217. }
  218. }
  219. protected function setmore($values) {
  220. if ($this->new) {
  221. if ($this->struct['address']['display_in_form'] == 1) { # default mode - split off 'domain' field from 'address' # TODO: do this unconditional?
  222. list(/*NULL*/, $domain) = explode('@', $values['address']);
  223. $this->values['domain'] = $domain;
  224. }
  225. }
  226. if (! $this->new) { # edit mode - preserve vacation and mailbox alias if they were included before
  227. $old_ah = new AliasHandler();
  228. if (!$old_ah->init($this->id)) {
  229. $this->errormsg[] = $old_ah->errormsg[0];
  230. } elseif (!$old_ah->view()) {
  231. $this->errormsg[] = $old_ah->errormsg[0];
  232. } else {
  233. $oldvalues = $old_ah->result();
  234. if (!isset($values['goto'])) { # no new value given?
  235. $values['goto'] = $oldvalues['goto'];
  236. }
  237. if (!isset($values['on_vacation'])) { # no new value given?
  238. $values['on_vacation'] = $oldvalues['on_vacation'];
  239. }
  240. if ($values['on_vacation']) {
  241. $values['goto'][] = $this->getVacationAlias();
  242. }
  243. if ($oldvalues['is_mailbox']) { # alias belongs to a mailbox - add/keep mailbox to/in goto
  244. if (!isset($values['goto_mailbox'])) { # no new value given?
  245. $values['goto_mailbox'] = $oldvalues['goto_mailbox'];
  246. }
  247. if ($values['goto_mailbox']) {
  248. $values['goto'][] = $this->id;
  249. # if the alias points to the mailbox, don't display the "empty goto" error message
  250. if (isset($this->errormsg['goto']) && $this->errormsg['goto'] == Config::lang('pEdit_alias_goto_text_error1')) {
  251. unset($this->errormsg['goto']);
  252. }
  253. }
  254. }
  255. }
  256. }
  257. $this->values['goto'] = join(',', $values['goto']);
  258. }
  259. protected function storemore() {
  260. # TODO: if alias belongs to a mailbox, update mailbox active status
  261. return true;
  262. }
  263. protected function read_from_db_postprocess($db_result) {
  264. foreach ($db_result as $key => $value) {
  265. # split comma-separated 'goto' into an array
  266. $db_result[$key]['goto'] = explode(',', $db_result[$key]['goto']);
  267. # Vacation enabled?
  268. list($db_result[$key]['on_vacation'], $db_result[$key]['goto']) = remove_from_array($db_result[$key]['goto'], $this->getVacationAlias());
  269. # if it is a mailbox, does the alias point to the mailbox?
  270. if ($db_result[$key]['is_mailbox']) {
  271. # this intentionally does not match mailbox targets with recipient delimiter.
  272. # if it would, we would have to make goto_mailbox a text instead of a bool (which would annoy 99% of the users)
  273. list($db_result[$key]['goto_mailbox'], $db_result[$key]['goto']) = remove_from_array($db_result[$key]['goto'], $key);
  274. } else { # not a mailbox
  275. $db_result[$key]['goto_mailbox'] = 0;
  276. }
  277. # editing a default alias (postmaster@ etc.) is only allowed if special_alias_control is allowed or if the user is a superadmin
  278. $tmp = preg_split('/\@/', $db_result[$key]['address']);
  279. if (!$this->is_superadmin && !Config::bool('special_alias_control') && array_key_exists($tmp[0], Config::Read('default_aliases'))) {
  280. $db_result[$key]['_can_edit'] = 0;
  281. $db_result[$key]['_can_delete'] = 0;
  282. }
  283. if ($this->struct['status']['display_in_list'] && Config::Bool('show_status')) {
  284. $db_result[$key]['status'] = gen_show_status($db_result[$key]['address']);
  285. }
  286. }
  287. return $db_result;
  288. }
  289. private function condition_ignore_mailboxes($condition, $searchmode) {
  290. # only list aliases that do not belong to mailboxes
  291. if (is_array($condition)) {
  292. $condition['__mailbox_username'] = 1;
  293. $searchmode['__mailbox_username'] = 'NULL';
  294. } else {
  295. if ($condition != '') {
  296. $condition = " ( $condition ) AND ";
  297. }
  298. $condition = " $condition __mailbox_username IS NULL ";
  299. }
  300. return array($condition, $searchmode);
  301. }
  302. public function getList($condition, $searchmode = array(), $limit=-1, $offset=-1) {
  303. list($condition, $searchmode) = $this->condition_ignore_mailboxes($condition, $searchmode);
  304. $this->set_is_mailbox_extrafrom($condition, $searchmode);
  305. $result = parent::getList($condition, $searchmode, $limit, $offset);
  306. $this->set_is_mailbox_extrafrom(); # reset to default
  307. return $result;
  308. }
  309. public function getPagebrowser($condition, $searchmode = array()) {
  310. list($condition, $searchmode) = $this->condition_ignore_mailboxes($condition, $searchmode);
  311. $this->set_is_mailbox_extrafrom($condition, $searchmode);
  312. $result = parent::getPagebrowser($condition, $searchmode);
  313. $this->set_is_mailbox_extrafrom(); # reset to default
  314. return $result;
  315. }
  316. protected function _validate_goto($field, $val) {
  317. if (count($val) == 0) {
  318. # empty is ok for mailboxes - this is checked in setmore() which can clear the error message
  319. $this->errormsg[$field] = Config::lang('pEdit_alias_goto_text_error1');
  320. return false;
  321. }
  322. $errors = array();
  323. foreach ($val as $singlegoto) {
  324. if (substr($this->id, 0, 1) == '@' && substr($singlegoto, 0, 1) == '@') { # domain-wide forward - check only the domain part
  325. # only allowed if $this->id is a catchall
  326. # Note: alias domains are better, but we should keep this way supported for backward compatibility
  327. # and because alias domains can't forward to external domains
  328. list(/*NULL*/, $domain) = explode('@', $singlegoto);
  329. $domain_check = check_domain($domain);
  330. if ($domain_check != '') {
  331. $errors[] = "$singlegoto: $domain_check";
  332. }
  333. } else {
  334. $email_check = check_email($singlegoto);
  335. // preg_match -> allows for redirect to a local system account.
  336. if ($email_check != '' && !preg_match('/^[a-z0-9]+$/', $singlegoto)) {
  337. $errors[] = "$singlegoto: $email_check";
  338. }
  339. }
  340. }
  341. if (count($errors)) {
  342. $this->errormsg[$field] = join(" ", $errors); # TODO: find a way to display multiple error messages per field
  343. return false;
  344. } else {
  345. return true;
  346. }
  347. }
  348. /**
  349. * on $this->new, set localpart based on address
  350. */
  351. protected function _missing_localpart($field) {
  352. if (isset($this->RAWvalues['address'])) {
  353. $parts = explode('@', $this->RAWvalues['address']);
  354. if (count($parts) == 2) {
  355. $this->RAWvalues['localpart'] = $parts[0];
  356. }
  357. }
  358. }
  359. /**
  360. * on $this->new, set domain based on address
  361. */
  362. protected function _missing_domain($field) {
  363. if (isset($this->RAWvalues['address'])) {
  364. $parts = explode('@', $this->RAWvalues['address']);
  365. if (count($parts) == 2) {
  366. $this->RAWvalues['domain'] = $parts[1];
  367. }
  368. }
  369. }
  370. /**
  371. * Returns the vacation alias for this user.
  372. * i.e. if this user's username was roger@example.com, and the autoreply domain was set to
  373. * autoreply.fish.net in config.inc.php we'd return roger#example.com@autoreply.fish.net
  374. * @return string an email alias.
  375. */
  376. protected function getVacationAlias() {
  377. $vacation_goto = str_replace('@', '#', $this->id);
  378. return $vacation_goto . '@' . Config::read('vacation_domain');
  379. }
  380. /**
  381. * @return true on success false on failure
  382. */
  383. public function delete() {
  384. if (! $this->view()) {
  385. $this->errormsg[] = Config::Lang('alias_does_not_exist');
  386. return false;
  387. }
  388. if ($this->result['is_mailbox']) {
  389. $this->errormsg[] = Config::Lang('mailbox_alias_cant_be_deleted');
  390. return false;
  391. }
  392. if (!$this->can_delete) {
  393. $this->errormsg[] = Config::Lang_f('protected_alias_cant_be_deleted', $this->id);
  394. return false;
  395. }
  396. db_delete('alias', 'address', $this->id);
  397. list(/*NULL*/, $domain) = explode('@', $this->id);
  398. db_log($domain, 'delete_alias', $this->id);
  399. $this->infomsg[] = Config::Lang_f('pDelete_delete_success', $this->id);
  400. return true;
  401. }
  402. }
  403. /* vim: set expandtab softtabstop=4 tabstop=4 shiftwidth=4: */