Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

DomainHandler.php 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. <?php
  2. # $Id: DomainHandler.php 1783 2015-04-06 23:30:44Z christian_boltz $
  3. /**
  4. * Handlers User level alias actions - e.g. add alias, get aliases, update etc.
  5. */
  6. class DomainHandler extends PFAHandler {
  7. protected $db_table = 'domain';
  8. protected $id_field = 'domain';
  9. protected $domain_field = 'domain';
  10. protected function validate_new_id() {
  11. $domain_check = check_domain($this->id);
  12. if ($domain_check != '') {
  13. $this->errormsg[$this->id_field] = $domain_check;
  14. return false;
  15. }
  16. if (Config::read('vacation_domain') == $this->id) {
  17. $this->errormsg[$this->id_field] = Config::Lang('domain_conflict_vacation_domain');
  18. return false;
  19. }
  20. # still here? good.
  21. return true;
  22. }
  23. protected function initStruct() {
  24. # TODO: shorter PALANG labels ;-)
  25. $super = $this->is_superadmin;
  26. $transp = min($super, Config::intbool('transport'));
  27. $editquota = min($super, Config::intbool('quota'));
  28. $quota = Config::intbool('quota');
  29. $edit_dom_q = min($super, Config::intbool('domain_quota'), $quota);
  30. $dom_q = min(Config::intbool('domain_quota'), $quota);
  31. $query_used_domainquota = 'round(coalesce(__total_quota/' . intval(Config::read('quota_multiplier')) . ',0))';
  32. # NOTE: There are dependencies between alias_count, mailbox_count and total_quota.
  33. # NOTE: If you disable "display in list" for one of them, the SQL query for the others might break.
  34. # NOTE: (Disabling all of them shouldn't be a problem.)
  35. $this->struct=array(
  36. # field name allow display in... type $PALANG label $PALANG description default / options / ...
  37. # editing? form list
  38. 'domain' => pacol( $this->new, 1, 1, 'text', 'domain' , '' , '', '',
  39. array('linkto' => 'list-virtual.php?domain=%s') ),
  40. 'description' => pacol( $super, $super, $super, 'text', 'description' , '' ),
  41. # Aliases
  42. 'aliases' => pacol( $super, $super, 0, 'num' , 'aliases' , 'pAdminEdit_domain_aliases_text' , Config::read('aliases') ),
  43. 'alias_count' => pacol( 0, 0, 1, 'vnum', '' , '' , '', '',
  44. /*not_in_db*/ 0,
  45. /*dont_write_to_db*/ 1,
  46. /*select*/ 'coalesce(__alias_count,0) - coalesce(__mailbox_count,0) as alias_count',
  47. /*extrafrom*/ 'left join ( select count(*) as __alias_count, domain as __alias_domain from ' . table_by_key('alias') .
  48. ' group by domain) as __alias on domain = __alias_domain'),
  49. 'aliases_quot' => pacol( 0, 0, 1, 'quot', 'aliases' , '' , 0, '',
  50. array('select' => db_quota_text( '__alias_count - coalesce(__mailbox_count,0)', 'aliases', 'aliases_quot')) ),
  51. '_aliases_quot_percent' => pacol( 0, 0, 1, 'vnum', '' ,'' , 0, '',
  52. array('select' => db_quota_percent('__alias_count - coalesce(__mailbox_count,0)', 'aliases', '_aliases_quot_percent')) ),
  53. # Mailboxes
  54. 'mailboxes' => pacol( $super, $super, 0, 'num' , 'mailboxes' , 'pAdminEdit_domain_aliases_text' , Config::read('mailboxes') ),
  55. 'mailbox_count' => pacol( 0, 0, 1, 'vnum', '' , '' , '', '',
  56. /*not_in_db*/ 0,
  57. /*dont_write_to_db*/ 1,
  58. /*select*/ 'coalesce(__mailbox_count,0) as mailbox_count',
  59. /*extrafrom*/ 'left join ( select count(*) as __mailbox_count, sum(quota) as __total_quota, domain as __mailbox_domain from ' . table_by_key('mailbox') .
  60. ' group by domain) as __mailbox on domain = __mailbox_domain'),
  61. 'mailboxes_quot' => pacol( 0, 0, 1, 'quot', 'mailboxes' , '' , 0, '',
  62. array('select' => db_quota_text( '__mailbox_count', 'mailboxes', 'mailboxes_quot')) ),
  63. '_mailboxes_quot_percent' => pacol( 0, 0, 1, 'vnum', '' , '' , 0, '',
  64. array('select' => db_quota_percent('__mailbox_count', 'mailboxes', '_mailboxes_quot_percent')) ),
  65. 'maxquota' => pacol($editquota,$editquota,$quota, 'num' , 'pOverview_get_quota' , 'pAdminEdit_domain_maxquota_text' , Config::read('maxquota') ),
  66. # Domain quota
  67. 'quota' => pacol($edit_dom_q,$edit_dom_q, 0, 'num', 'pAdminEdit_domain_quota' , 'pAdminEdit_domain_maxquota_text' , Config::read('domain_quota_default') ),
  68. 'total_quota' => pacol( 0, 0, 1, 'vnum', '' , '' , '', '',
  69. array('select' => "$query_used_domainquota AS total_quota") /*extrafrom*//* already in mailbox_count */ ),
  70. 'total_quot' => pacol( 0, 0, $dom_q, 'quot', 'pAdminEdit_domain_quota' , '' , 0, '',
  71. array('select' => db_quota_text( $query_used_domainquota, 'quota', 'total_quot')) ),
  72. '_total_quot_percent'=> pacol( 0, 0, $dom_q, 'vnum', '' , '' , 0, '',
  73. array('select' => db_quota_percent($query_used_domainquota, 'quota', '_total_quot_percent')) ),
  74. 'transport' => pacol( $transp, $transp,$transp,'enum', 'transport' , 'pAdminEdit_domain_transport_text' , Config::read('transport_default') ,
  75. /*options*/ Config::read('transport_options') ),
  76. 'backupmx' => pacol( $super, $super, 1, 'bool', 'pAdminEdit_domain_backupmx' , '' , 0),
  77. 'active' => pacol( $super, $super, 1, 'bool', 'active' , '' , 1 ),
  78. 'default_aliases' => pacol( $this->new, $this->new, 0, 'bool', 'pAdminCreate_domain_defaultaliases', '' , 1,'', /*not in db*/ 1 ),
  79. 'created' => pacol( 0, 0, 0, 'ts', 'created' , '' ),
  80. 'modified' => pacol( 0, 0, $super, 'ts', 'last_modified' , '' ),
  81. '_can_edit' => pacol( 0, 0, 1, 'int', '' , '' , 0 ,
  82. /*options*/ '',
  83. /*not_in_db*/ 0,
  84. /*dont_write_to_db*/ 1,
  85. /*select*/ $this->is_superadmin . ' as _can_edit' ),
  86. '_can_delete' => pacol( 0, 0, 1, 'int', '' , '' , 0 ,
  87. /*options*/ '',
  88. /*not_in_db*/ 0,
  89. /*dont_write_to_db*/ 1,
  90. /*select*/ $this->is_superadmin . ' as _can_delete' ),
  91. );
  92. }
  93. protected function initMsg() {
  94. $this->msg['error_already_exists'] = 'pAdminCreate_domain_domain_text_error';
  95. $this->msg['error_does_not_exist'] = 'domain_does_not_exist';
  96. $this->msg['confirm_delete'] = 'confirm_delete_domain';
  97. if ($this->new) {
  98. $this->msg['logname'] = 'create_domain';
  99. $this->msg['store_error'] = 'pAdminCreate_domain_result_error';
  100. $this->msg['successmessage'] = 'pAdminCreate_domain_result_success';
  101. } else {
  102. $this->msg['logname'] = 'edit_domain';
  103. $this->msg['store_error'] = 'pAdminEdit_domain_result_error';
  104. $this->msg['successmessage'] = 'domain_updated';
  105. }
  106. $this->msg['can_create'] = $this->is_superadmin;
  107. }
  108. public function webformConfig() {
  109. return array(
  110. # $PALANG labels
  111. 'formtitle_create' => 'pAdminCreate_domain_welcome',
  112. 'formtitle_edit' => 'pAdminEdit_domain_welcome',
  113. 'create_button' => 'pAdminCreate_domain_button',
  114. # various settings
  115. 'required_role' => 'admin',
  116. 'listview' => 'list.php?table=domain',
  117. 'early_init' => 0,
  118. );
  119. }
  120. protected function beforestore() {
  121. # TODO: is this function superfluous? _can_edit should already cover this
  122. if ($this->is_superadmin) {
  123. return true;
  124. }
  125. $this->errormsg[] = Config::Lang('edit_not_allowed', $this->id);
  126. return false;
  127. }
  128. /**
  129. * called by $this->store() after storing $this->values in the database
  130. * can be used to update additional tables, call scripts etc.
  131. */
  132. protected function storemore() {
  133. if ($this->new && $this->values['default_aliases']) {
  134. foreach (Config::read('default_aliases') as $address=>$goto) {
  135. $address = $address . "@" . $this->id;
  136. # if $goto doesn't contain @, let the alias point to the same domain
  137. if(!strstr($goto, '@')) {
  138. $goto = $goto . "@" . $this->id;
  139. }
  140. # TODO: use AliasHandler->add instead of writing directly to the alias table
  141. $arr = array(
  142. 'address' => $address,
  143. 'goto' => $goto,
  144. 'domain' => $this->id,
  145. );
  146. $result = db_insert ('alias', $arr);
  147. # TODO: error checking
  148. }
  149. }
  150. if ($this->new) {
  151. if (!$this->domain_postcreation()) {
  152. $this->errormsg[] = Config::lang('domain_postcreate_failed');
  153. }
  154. } else {
  155. # we don't have domain_postedit()
  156. }
  157. return true; # TODO: don't hardcode
  158. }
  159. /**
  160. * @return true on success false on failure
  161. */
  162. public function delete() {
  163. # TODO: check for _can_delete instead
  164. if (! $this->is_superadmin) {
  165. $this->errormsg[] = Config::Lang_f('no_delete_permissions', $this->id);
  166. return false;
  167. }
  168. if ( ! $this->view() ) {
  169. $this->errormsg[] = Config::Lang('domain_does_not_exist'); # TODO: can users hit this message at all? init() should already fail...
  170. return false;
  171. }
  172. if (Config::bool('alias_domain')) {
  173. # check if this domain is an alias domain target - if yes, do not allow to delete it
  174. $handler = new AliasdomainHandler(0, $this->admin_username);
  175. $handler->getList("target_domain = '" . escape_string($this->id) . "'");
  176. $aliasdomains = $handler->result();
  177. if (count($aliasdomains) > 0) {
  178. $this->errormsg[] = Config::Lang_f('delete_domain_aliasdomain_target', $this->id);
  179. return false;
  180. }
  181. }
  182. # the correct way would be to recursively delete mailboxes, aliases, alias_domains, fetchmail entries
  183. # with *Handler before deleting the domain, but this would be terribly slow on domains with many aliases etc.,
  184. # so we do it the fast way on the database level
  185. # cleaning up all tables doesn't hurt, even if vacation or displaying the quota is disabled
  186. # some tables don't have a domain field, so we need a workaround
  187. $like_domain = "LIKE '" . escape_string('%@' . $this->id) . "'";
  188. db_delete('domain_admins', 'domain', $this->id);
  189. db_delete('alias', 'domain', $this->id);
  190. db_delete('mailbox', 'domain', $this->id);
  191. db_delete('alias_domain', 'alias_domain', $this->id);
  192. db_delete('vacation', 'domain', $this->id);
  193. db_delete('vacation_notification', 'on_vacation', $this->id, "OR on_vacation $like_domain");
  194. db_delete('quota', 'username', $this->id, "OR username $like_domain");
  195. db_delete('quota2', 'username', $this->id, "OR username $like_domain");
  196. db_delete('fetchmail', 'mailbox', $this->id, "OR mailbox $like_domain");
  197. db_delete('log', 'domain', $this->id); # TODO: should we really delete the log?
  198. # finally delete the domain
  199. db_delete($this->db_table, $this->id_field, $this->id);
  200. if ( !$this->domain_postdeletion() ) {
  201. $this->error_msg[] = $PALANG['domain_postdel_failed'];
  202. }
  203. db_log ($this->id, 'delete_domain', $this->id); # TODO delete_domain is not a valid db_log keyword yet
  204. $this->infomsg[] = Config::Lang_f('pDelete_delete_success', $this->id);
  205. return true;
  206. }
  207. /**
  208. * get formatted version of fields
  209. *
  210. * @param array values of current item
  211. */
  212. public function _formatted_aliases ($item) { return $item['alias_count'] . ' / ' . $item['aliases'] ; }
  213. public function _formatted_mailboxes($item) { return $item['mailbox_count'] . ' / ' . $item['mailboxes']; }
  214. public function _formatted_quota ($item) { return $item['total_quota'] . ' / ' . $item['quota'] ; }
  215. /**
  216. * Called after a domain has been added
  217. *
  218. * @return boolean
  219. */
  220. protected function domain_postcreation() {
  221. $script=Config::read('domain_postcreation_script');
  222. if (empty($script)) {
  223. return true;
  224. }
  225. if (empty($this->id)) {
  226. $this->errormsg[] = 'Empty domain parameter in domain_postcreation';
  227. return false;
  228. }
  229. $cmdarg1=escapeshellarg($this->id);
  230. $command= "$script $cmdarg1";
  231. $retval=0;
  232. $output=array();
  233. $firstline='';
  234. $firstline=exec($command,$output,$retval);
  235. if (0!=$retval) {
  236. error_log("Running $command yielded return value=$retval, first line of output=$firstline");
  237. $this->errormsg[] = 'Problems running domain postcreation script!';
  238. return FALSE;
  239. }
  240. return TRUE;
  241. }
  242. /**
  243. * Called after a domain has been deleted
  244. *
  245. * @return boolean
  246. */
  247. protected function domain_postdeletion() {
  248. $script=Config::read('domain_postdeletion_script');
  249. if (empty($script)) {
  250. return true;
  251. }
  252. if (empty($this->id)) {
  253. $this->errormsg[] = 'Empty domain parameter in domain_postdeletion';
  254. return false;
  255. }
  256. $cmdarg1=escapeshellarg($this->id);
  257. $command= "$script $cmdarg1";
  258. $retval=0;
  259. $output=array();
  260. $firstline='';
  261. $firstline=exec($command,$output,$retval);
  262. if (0!=$retval) {
  263. error_log("Running $command yielded return value=$retval, first line of output=$firstline");
  264. $this->errormsg[] = 'Problems running domain postdeletion script!';
  265. return FALSE;
  266. }
  267. return TRUE;
  268. }
  269. }
  270. /* vim: set expandtab softtabstop=4 tabstop=4 shiftwidth=4: */