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 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. <?php
  2. # $Id: AliasHandler.php 1777 2015-04-06 22:09:18Z christian_boltz $
  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, __mailbox_username',
  41. # __mailbox_username is unused, but needed as workaround for a MariaDB bug
  42. /*extrafrom*/ 'LEFT JOIN ( ' .
  43. ' SELECT 1 as __is_mailbox, username as __mailbox_username ' .
  44. ' FROM ' . table_by_key('mailbox') .
  45. ' WHERE username IS NOT NULL ' .
  46. ' ) AS __mailbox ON __mailbox_username = address' ),
  47. 'goto_mailbox' => pacol( $mbgoto, $mbgoto,$mbgoto,'bool', 'pEdit_alias_forward_and_store' , '' , 0,
  48. /*options*/ '',
  49. /*not_in_db*/ 1 ), # read_from_db_postprocess() sets the value
  50. 'on_vacation' => pacol( 1, 0, 1, 'bool', 'pUsersMenu_vacation' , '' , 0 ,
  51. /*options*/ '',
  52. /*not_in_db*/ 1 ), # read_from_db_postprocess() sets the value - TODO: read active flag from vacation table instead?
  53. 'created' => pacol( 0, 0, 0, 'ts', 'created' , '' ),
  54. 'modified' => pacol( 0, 0, 1, 'ts', 'last_modified' , '' ),
  55. 'active' => pacol( 1, 1, 1, 'bool', 'active' , '' , 1 ),
  56. '_can_edit' => pacol( 0, 0, 1, 'vnum', '' , '' , 0 , '',
  57. array('select' => '1 as _can_edit') ),
  58. '_can_delete' => pacol( 0, 0, 1, 'vnum', '' , '' , 0 , '',
  59. array('select' => '1 as _can_delete') ), # read_from_db_postprocess() updates the value
  60. # aliases listed in $CONF[default_aliases] are read-only for domain admins if $CONF[special_alias_control] is NO.
  61. );
  62. }
  63. protected function initMsg() {
  64. $this->msg['error_already_exists'] = 'email_address_already_exists';
  65. $this->msg['error_does_not_exist'] = 'alias_does_not_exist';
  66. $this->msg['confirm_delete'] = 'confirm_delete_alias';
  67. $this->msg['list_header'] = 'pOverview_alias_title';
  68. if ($this->new) {
  69. $this->msg['logname'] = 'create_alias';
  70. $this->msg['store_error'] = 'pCreate_alias_result_error';
  71. $this->msg['successmessage'] = 'pCreate_alias_result_success';
  72. } else {
  73. $this->msg['logname'] = 'edit_alias';
  74. $this->msg['store_error'] = 'pEdit_alias_result_error';
  75. $this->msg['successmessage'] = 'alias_updated';
  76. }
  77. }
  78. public function webformConfig() {
  79. if ($this->new) { # the webform will display a localpart field + domain dropdown on $new
  80. $this->struct['address']['display_in_form'] = 0;
  81. $this->struct['localpart']['display_in_form'] = 1;
  82. $this->struct['domain']['display_in_form'] = 1;
  83. }
  84. if (Config::bool('show_status')) {
  85. $this->struct['status']['display_in_list'] = 1;
  86. $this->struct['status']['label'] = ' ';
  87. }
  88. return array(
  89. # $PALANG labels
  90. 'formtitle_create' => 'pMain_create_alias',
  91. 'formtitle_edit' => 'pEdit_alias_welcome',
  92. 'create_button' => 'add_alias',
  93. # various settings
  94. 'required_role' => 'admin',
  95. 'listview' => 'list-virtual.php',
  96. 'early_init' => 0,
  97. 'prefill' => array('domain'),
  98. );
  99. }
  100. /**
  101. * AliasHandler needs some special handling in init() and therefore overloads the function.
  102. * It also calls parent::init()
  103. */
  104. public function init($id) {
  105. @list($local_part,$domain) = explode ('@', $id); # supress error message if $id doesn't contain '@'
  106. if ($local_part == '*') { # catchall - postfix expects '@domain', not '*@domain'
  107. $id = '@' . $domain;
  108. }
  109. $retval = parent::init($id);
  110. if (!$retval) return false; # parent::init() failed, no need to continue
  111. # hide 'goto_mailbox' for non-mailbox aliases
  112. # parent::init called view() before, so we can rely on having $this->result filled
  113. # (only validate_new_id() is called from parent::init and could in theory change $this->result)
  114. if ($this->new || $this->result['is_mailbox'] == 0) {
  115. $this->struct['goto_mailbox']['editable'] = 0;
  116. $this->struct['goto_mailbox']['display_in_form'] = 0;
  117. $this->struct['goto_mailbox']['display_in_list'] = 0;
  118. }
  119. if ( !$this->new && $this->result['is_mailbox'] && $this->admin_username != ''&& !authentication_has_role('global-admin') ) {
  120. # domain admins are not allowed to change mailbox alias $CONF['alias_control_admin'] = NO
  121. # TODO: apply the same restriction to superadmins?
  122. if (!Config::bool('alias_control_admin')) {
  123. # TODO: make translateable
  124. $this->errormsg[] = "Domain administrators do not have the ability to edit user's aliases (check config.inc.php - alias_control_admin)";
  125. return false;
  126. }
  127. }
  128. return $retval;
  129. }
  130. protected function domain_from_id() {
  131. list(/*NULL*/,$domain) = explode('@', $this->id);
  132. return $domain;
  133. }
  134. protected function validate_new_id() {
  135. if ($this->id == '') {
  136. $this->errormsg[$this->id_field] = Config::lang('pCreate_alias_address_text_error1');
  137. return false;
  138. }
  139. list($local_part,$domain) = explode ('@', $this->id);
  140. if(!$this->create_allowed($domain)) {
  141. $this->errormsg[$this->id_field] = Config::lang('pCreate_alias_address_text_error3');
  142. return false;
  143. }
  144. # TODO: already checked in set() - does it make sense to check it here also? Only advantage: it's an early check
  145. # if (!in_array($domain, $this->allowed_domains)) {
  146. # $this->errormsg[] = Config::lang('pCreate_alias_address_text_error1');
  147. # return false;
  148. # }
  149. if ($local_part == '') { # catchall
  150. $valid = true;
  151. } else {
  152. $email_check = check_email($this->id);
  153. if ($email_check == '') {
  154. $valid = true;
  155. } else {
  156. $this->errormsg[$this->id_field] = $email_check;
  157. $valid = false;
  158. }
  159. }
  160. return $valid;
  161. }
  162. /**
  163. * check number of existing aliases for this domain - is one more allowed?
  164. */
  165. private function create_allowed($domain) {
  166. if ($this->called_by == 'MailboxHandler') return true; # always allow creating an alias for a mailbox
  167. $limit = get_domain_properties ($domain);
  168. if ($limit['aliases'] == 0) return true; # unlimited
  169. if ($limit['aliases'] < 0) return false; # disabled
  170. if ($limit['alias_count'] >= $limit['aliases']) return false;
  171. return true;
  172. }
  173. /**
  174. * merge localpart and domain to address
  175. * called by edit.php (if id_field is editable and hidden in editform) _before_ ->init
  176. */
  177. public function mergeId($values) {
  178. if ($this->struct['localpart']['display_in_form'] == 1 && $this->struct['domain']['display_in_form']) { # webform mode - combine to 'address' field
  179. if (empty($values['localpart']) || empty($values['domain']) ) { # localpart or domain not set
  180. return "";
  181. }
  182. if ($values['localpart'] == '*') $values['localpart'] = ''; # catchall
  183. return $values['localpart'] . '@' . $values['domain'];
  184. } else {
  185. return $values[$this->id_field];
  186. }
  187. }
  188. protected function setmore($values) {
  189. if ($this->new) {
  190. if ($this->struct['address']['display_in_form'] == 1) { # default mode - split off 'domain' field from 'address' # TODO: do this unconditional?
  191. list(/*NULL*/,$domain) = explode('@', $values['address']);
  192. $this->values['domain'] = $domain;
  193. }
  194. }
  195. if (! $this->new) { # edit mode - preserve vacation and mailbox alias if they were included before
  196. $old_ah = new AliasHandler();
  197. if (!$old_ah->init($this->id)) {
  198. $this->errormsg[] = $old_ah->errormsg[0];
  199. } elseif (!$old_ah->view()) {
  200. $this->errormsg[] = $old_ah->errormsg[0];
  201. } else {
  202. $oldvalues = $old_ah->result();
  203. if (!isset($values['goto'])) { # no new value given?
  204. $values['goto'] = $oldvalues['goto'];
  205. }
  206. if (!isset($values['on_vacation'])) { # no new value given?
  207. $values['on_vacation'] = $oldvalues['on_vacation'];
  208. }
  209. if ($values['on_vacation']) {
  210. $values['goto'][] = $this->getVacationAlias();
  211. }
  212. if ($oldvalues['is_mailbox']) { # alias belongs to a mailbox - add/keep mailbox to/in goto
  213. if (!isset($values['goto_mailbox'])) { # no new value given?
  214. $values['goto_mailbox'] = $oldvalues['goto_mailbox'];
  215. }
  216. if ($values['goto_mailbox']) {
  217. $values['goto'][] = $this->id;
  218. # if the alias points to the mailbox, don't display the "empty goto" error message
  219. if (isset($this->errormsg['goto']) && $this->errormsg['goto'] == Config::lang('pEdit_alias_goto_text_error1') ) {
  220. unset($this->errormsg['goto']);
  221. }
  222. }
  223. }
  224. }
  225. }
  226. $this->values['goto'] = join(',', $values['goto']);
  227. }
  228. protected function storemore() {
  229. # TODO: if alias belongs to a mailbox, update mailbox active status
  230. return true;
  231. }
  232. protected function read_from_db_postprocess($db_result) {
  233. foreach ($db_result as $key => $value) {
  234. # split comma-separated 'goto' into an array
  235. $db_result[$key]['goto'] = explode(',', $db_result[$key]['goto']);
  236. # Vacation enabled?
  237. list($db_result[$key]['on_vacation'], $db_result[$key]['goto']) = remove_from_array($db_result[$key]['goto'], $this->getVacationAlias() );
  238. # if it is a mailbox, does the alias point to the mailbox?
  239. if ($db_result[$key]['is_mailbox']) {
  240. # this intentionally does not match mailbox targets with recipient delimiter.
  241. # if it would, we would have to make goto_mailbox a text instead of a bool (which would annoy 99% of the users)
  242. list($db_result[$key]['goto_mailbox'], $db_result[$key]['goto']) = remove_from_array($db_result[$key]['goto'], $key);
  243. } else { # not a mailbox
  244. $db_result[$key]['goto_mailbox'] = 0;
  245. }
  246. # editing a default alias (postmaster@ etc.) is only allowed if special_alias_control is allowed or if the user is a superadmin
  247. $tmp = preg_split('/\@/', $db_result[$key]['address']);
  248. if (!$this->is_superadmin && !Config::bool('special_alias_control') && array_key_exists($tmp[0], Config::Read('default_aliases'))) {
  249. $db_result[$key]['_can_edit'] = 0;
  250. $db_result[$key]['_can_delete'] = 0;
  251. }
  252. if ($this->struct['status']['display_in_list'] && Config::Bool('show_status')) {
  253. $db_result[$key]['status'] = gen_show_status($db_result[$key]['address']);
  254. }
  255. }
  256. return $db_result;
  257. }
  258. public function getList($condition, $searchmode = array(), $limit=-1, $offset=-1) {
  259. # only list aliases that do not belong to mailboxes
  260. # TODO: breaks if $condition is an array
  261. if ($condition != '') {
  262. $condition = " AND ( $condition ) ";
  263. }
  264. return parent::getList( "__mailbox_username IS NULL $condition", $searchmode, $limit, $offset);
  265. }
  266. public function getPagebrowser($condition, $searchmode = array()) {
  267. # only list aliases that do not belong to mailboxes
  268. # TODO: breaks if $condition is an array
  269. if ($condition != '') {
  270. $condition = " AND ( $condition ) ";
  271. }
  272. return parent::getPagebrowser( "__mailbox_username IS NULL $condition", $searchmode);
  273. }
  274. protected function _validate_goto($field, $val) {
  275. if (count($val) == 0) {
  276. # empty is ok for mailboxes - this is checked in setmore() which can clear the error message
  277. $this->errormsg[$field] = Config::lang('pEdit_alias_goto_text_error1');
  278. return false;
  279. }
  280. $errors = array();
  281. foreach ($val as $singlegoto) {
  282. if (substr($this->id, 0, 1) == '@' && substr($singlegoto, 0, 1) == '@') { # domain-wide forward - check only the domain part
  283. # only allowed if $this->id is a catchall
  284. # Note: alias domains are better, but we should keep this way supported for backward compatibility
  285. # and because alias domains can't forward to external domains
  286. list (/*NULL*/, $domain) = explode('@', $singlegoto);
  287. $domain_check = check_domain($domain);
  288. if ($domain_check != '') {
  289. $errors[] = "$singlegoto: $domain_check";
  290. }
  291. } else {
  292. $email_check = check_email($singlegoto);
  293. if ($email_check != '') {
  294. $errors[] = "$singlegoto: $email_check";
  295. }
  296. }
  297. }
  298. if (count($errors)) {
  299. $this->errormsg[$field] = join(" ", $errors); # TODO: find a way to display multiple error messages per field
  300. return false;
  301. } else {
  302. return true;
  303. }
  304. }
  305. /**
  306. * on $this->new, set localpart based on address
  307. */
  308. protected function _missing_localpart ($field) {
  309. if (isset($this->RAWvalues['address'])) {
  310. $parts = explode('@', $this->RAWvalues['address']);
  311. if (count($parts) == 2) $this->RAWvalues['localpart'] = $parts[0];
  312. }
  313. }
  314. /**
  315. * on $this->new, set domain based on address
  316. */
  317. protected function _missing_domain ($field) {
  318. if (isset($this->RAWvalues['address'])) {
  319. $parts = explode('@', $this->RAWvalues['address']);
  320. if (count($parts) == 2) $this->RAWvalues['domain'] = $parts[1];
  321. }
  322. }
  323. /**
  324. * Returns the vacation alias for this user.
  325. * i.e. if this user's username was roger@example.com, and the autoreply domain was set to
  326. * autoreply.fish.net in config.inc.php we'd return roger#example.com@autoreply.fish.net
  327. * @return string an email alias.
  328. */
  329. protected function getVacationAlias() {
  330. $vacation_goto = str_replace('@', '#', $this->id);
  331. return $vacation_goto . '@' . Config::read('vacation_domain');
  332. }
  333. /**
  334. * @return true on success false on failure
  335. */
  336. public function delete() {
  337. if( ! $this->view() ) {
  338. $this->errormsg[] = Config::Lang('alias_does_not_exist');
  339. return false;
  340. }
  341. if ($this->result['is_mailbox']) {
  342. $this->errormsg[] = Config::Lang('mailbox_alias_cant_be_deleted');
  343. return false;
  344. }
  345. db_delete('alias', 'address', $this->id);
  346. list(/*NULL*/,$domain) = explode('@', $this->id);
  347. db_log ($domain, 'delete_alias', $this->id);
  348. $this->infomsg[] = Config::Lang_f('pDelete_delete_success', $this->id);
  349. return true;
  350. }
  351. }
  352. /* vim: set expandtab softtabstop=4 tabstop=4 shiftwidth=4: */