Marius Burkard
2016-04-20 4569cae57f127afd093794310ccd290d2d9fdf36
commit | author | age
532ae5 1 <?php
L 2
3 /*
4 Copyright (c) 2007, Till Brehm, projektfarm Gmbh
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without modification,
8 are permitted provided that the following conditions are met:
9
10     * Redistributions of source code must retain the above copyright notice,
11       this list of conditions and the following disclaimer.
12     * Redistributions in binary form must reproduce the above copyright notice,
13       this list of conditions and the following disclaimer in the documentation
14       and/or other materials provided with the distribution.
15     * Neither the name of ISPConfig nor the names of its contributors
16       may be used to endorse or promote products derived from this software without
17       specific prior written permission.
18
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
26 OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 class mailman_plugin {
7fe908 32
532ae5 33     var $plugin_name = 'mailman_plugin';
L 34     var $class_name = 'mailman_plugin';
7fe908 35
MC 36
532ae5 37     var $mailman_config_dir = '/etc/mailman/';
7fe908 38
532ae5 39     //* This function is called during ispconfig installation to determine
L 40     //  if a symlink shall be created for this plugin.
41     function onInstall() {
42         global $conf;
7fe908 43
532ae5 44         if($conf['services']['mail'] == true) {
L 45             return true;
46         } else {
47             return false;
48         }
7fe908 49
532ae5 50     }
7fe908 51
532ae5 52     /*
L 53          This function is called when the plugin is loaded
54     */
7fe908 55
532ae5 56     function onLoad() {
L 57         global $app;
7fe908 58
532ae5 59         /*
L 60         Register for the events
61         */
7fe908 62
MC 63         $app->plugins->registerEvent('mail_mailinglist_insert', 'mailman_plugin', 'insert');
64         $app->plugins->registerEvent('mail_mailinglist_update', 'mailman_plugin', 'update');
65         $app->plugins->registerEvent('mail_mailinglist_delete', 'mailman_plugin', 'delete');
66
67
68
532ae5 69     }
7fe908 70
MC 71     function insert($event_name, $data) {
532ae5 72         global $app, $conf;
7fe908 73
532ae5 74         $this->update_config();
7fe908 75
f8bc88 76         $pid = exec("nohup /usr/lib/mailman/bin/newlist -u ".escapeshellcmd($data["new"]["domain"])." -e ".escapeshellcmd($data["new"]["domain"])." ".escapeshellcmd($data["new"]["listname"])." ".escapeshellcmd($data["new"]["email"])." ".escapeshellcmd($data["new"]["password"])." >/dev/null 2>&1 & echo $!;");
FS 77         // wait for /usr/lib/mailman/bin/newlist-call
78         $running = true;
79         do {
80             exec('ps -p '.intval($pid), $out);
81             if (count($out) ==1) $running=false; else sleep(1);
82             unset($out);
83         } while ($running);
84         unset($out);
3f478f 85         if(is_file('/var/lib/mailman/data/virtual-mailman')) exec('postmap /var/lib/mailman/data/virtual-mailman');
d22542 86         if(is_file('/var/lib/mailman/data/transport-mailman')) exec('postmap /var/lib/mailman/data/transport-mailman');
26c0fc 87         exec('nohup '.$conf['init_scripts'] . '/' . 'mailman reload >/dev/null 2>&1 &');
7fe908 88
cc7a82 89         $app->db->query("UPDATE mail_mailinglist SET password = '' WHERE mailinglist_id = ?", $data["new"]['mailinglist_id']);
7fe908 90
532ae5 91     }
7fe908 92
532ae5 93     // The purpose of this plugin is to rewrite the main.cf file
7fe908 94     function update($event_name, $data) {
532ae5 95         global $app, $conf;
d22542 96         
TB 97         $this->update_config();
7fe908 98
5378e9 99         if($data["new"]["password"] != $data["old"]["password"] && $data["new"]["password"] != '') {
26c0fc 100             exec("nohup /usr/lib/mailman/bin/change_pw -l ".escapeshellcmd($data["new"]["listname"])." -p ".escapeshellcmd($data["new"]["password"])." >/dev/null 2>&1 &");
T 101             exec('nohup '.$conf['init_scripts'] . '/' . 'mailman reload >/dev/null 2>&1 &');
cc7a82 102             $app->db->query("UPDATE mail_mailinglist SET password = '' WHERE mailinglist_id = ?", $data["new"]['mailinglist_id']);
5378e9 103         }
d22542 104         
TB 105         if(is_file('/var/lib/mailman/data/virtual-mailman')) exec('postmap /var/lib/mailman/data/virtual-mailman');
106         if(is_file('/var/lib/mailman/data/transport-mailman')) exec('postmap /var/lib/mailman/data/transport-mailman');
532ae5 107     }
7fe908 108
MC 109     function delete($event_name, $data) {
532ae5 110         global $app, $conf;
7fe908 111
532ae5 112         $this->update_config();
7fe908 113
26c0fc 114         exec("nohup /usr/lib/mailman/bin/rmlist -a ".escapeshellcmd($data["old"]["listname"])." >/dev/null 2>&1 &");
7fe908 115
26c0fc 116         exec('nohup '.$conf['init_scripts'] . '/' . 'mailman reload >/dev/null 2>&1 &');
d22542 117         
TB 118         if(is_file('/var/lib/mailman/data/virtual-mailman')) exec('postmap /var/lib/mailman/data/virtual-mailman');
119         if(is_file('/var/lib/mailman/data/transport-mailman')) exec('postmap /var/lib/mailman/data/transport-mailman');
7fe908 120
532ae5 121     }
7fe908 122
532ae5 123     function update_config() {
L 124         global $app, $conf;
7fe908 125
86e699 126         copy($this->mailman_config_dir.'mm_cfg.py', $this->mailman_config_dir.'mm_cfg.py~');
7fe908 127
532ae5 128         // load the server configuration options
L 129         $app->uses('getconf');
130         $server_config = $app->getconf->get_server_config($conf['server_id'], 'server');
7fe908 131
532ae5 132         // load files
02bf99 133         if(file_exists($conf["rootpath"]."/conf-custom/mm_cfg.py.master")) {
355efb 134             $content = file_get_contents($conf["rootpath"]."/conf-custom/mm_cfg.py.master");
T 135         } else {
136             $content = file_get_contents($conf["rootpath"]."/conf/mm_cfg.py.master");
137         }
532ae5 138         $old_file = file_get_contents($this->mailman_config_dir."/mm_cfg.py");
7fe908 139
532ae5 140         $old_options = array();
7fe908 141         $lines = explode("\n", $old_file);
532ae5 142         foreach ($lines as $line)
L 143         {
144             if (strlen($line) && substr($line, 0, 1) != '#')
145             {
146                 list($key, $value) = explode("=", $line);
86e699 147                 if ($value && $value !== '')
532ae5 148                 {
L 149                     $key = rtrim($key);
150                     $old_options[$key] = trim($value);
151                 }
152             }
153         }
7fe908 154
532ae5 155         // create virtual_domains list
L 156         $domainAll = $app->db->queryAllRecords("SELECT domain FROM mail_mailinglist GROUP BY domain");
157         $virtual_domains = '';
158         foreach($domainAll as $domain)
159         {
160             if ($domainAll[0]['domain'] == $domain['domain'])
161                 $virtual_domains .= "'".$domain['domain']."'";
162             else
163                 $virtual_domains .= ", '".$domain['domain']."'";
164         }
7fe908 165
532ae5 166         $content = str_replace('{hostname}', $server_config['hostname'], $content);
L 167         $content = str_replace('{default_language}', $old_options['DEFAULT_SERVER_LANGUAGE'], $content);
168         $content = str_replace('{virtual_domains}', $virtual_domains, $content);
7fe908 169
532ae5 170         file_put_contents($this->mailman_config_dir."/mm_cfg.py", $content);
L 171     }
172
173 } // end class
174
175 ?>