1<?php
2    /*
3     * get_domain_id(): Looks up a domain name (in @domain format)
4     *                  and returns its id if found, 0 otherwise.
5     */
6    function get_domain_id($domain_name)
7    {
8        global $dbh;
9
10        $domain_id = 0;
11        $sth = $dbh->prepare("SELECT id FROM maia_domains WHERE domain = ?");
12        $res = $sth->execute(array(strtolower($domain_name)));
13        if (PEAR::isError($sth)) {
14            die($sth->getMessage());
15        }
16        if ($row = $res->fetchrow()) {
17            $domain_id = $row["id"];
18        }
19        $sth->free();
20
21        return $domain_id;
22    }
23
24
25    /*
26     * is_admin_for_domain(): Returns true if the specified user
27     *                        has administrator privileges for
28     *                        the specified domain, false otherwise.
29     */
30    function is_admin_for_domain($uid, $domain_id)
31    {
32        global $dbh;
33
34        $user_level = get_user_level($uid);
35
36        if ($user_level == "S") {
37            return true;
38        } else {
39            $sth = $dbh->prepare("SELECT maia_users.user_level " .
40                      "FROM maia_users, maia_domain_admins " .
41                      "WHERE maia_users.id = ? " .
42                      "AND maia_users.id = maia_domain_admins.admin_id " .
43                      "AND maia_domain_admins.domain_id = ?");
44
45            $res = $sth->execute(array($uid, $domain_id));
46            if (PEAR::isError($sth)) {
47                die($sth->getMessage());
48            }
49            if ($row = $res->fetchrow()) {
50                $result = ($row["user_level"] == "A");
51            } else {
52                $result = false;
53            }
54            $sth->free();
55
56            return $result;
57        }
58    }
59
60    /*
61     *
62     * get_default_domain_policies():  Gets default policies for autocreation and domain transports from @.
63     *                Returns a named array: ['autocreation'=>boolean, 'transport'=>string]
64     */
65    function get_default_domain_policies() {
66        global $dbh;
67
68        $sth = $dbh->prepare("SELECT enable_user_autocreation, transport FROM maia_domains WHERE domain = '@.'");
69        $res = $sth->execute();
70        if (PEAR::isError($sth)) {
71            die($sth->getMessage());
72        }
73        if ($row = $res->fetchrow()) {
74            $policy = $row["enable_user_autocreation"];
75            $transport = $row['transport'];
76        } else {
77            $policy = false;
78            $transport = ':';
79        }
80        $sth->free();
81        return array('autocreation' => $policy, 'transport' => $transport);
82    }
83
84    /*
85     *
86     * get_autocreation_policy():  Gets autocreation policy for given domain
87     *                Returns 'Y' or 'N', or false on error.
88     */
89    function get_autocreation_policy($domain) {
90        global $dbh;
91
92        $sth = $dbh->prepare("SELECT enable_user_autocreation FROM maia_domains WHERE domain = ?");
93        $res = $sth->execute(array($domain));
94        if (PEAR::isError($sth)) {
95            die($sth->getMessage());
96        }
97        if ($row = $res->fetchrow()) {
98            $policy = $row["enable_user_autocreation"];
99        } else {
100            $policy = false;
101        }
102        $sth->free();
103        return $policy;
104    }
105
106     /*
107     *
108     * get_autocreation_policy():  Sets autocreation policy for given domain
109     *
110     */
111    function set_autocreation_policy($domain, $policy) {
112        global $dbh;
113
114        $sth= $dbh->prepare("UPDATE maia_domains SET enable_user_autocreation=? FROM maia_domains WHERE domain = ?");
115        $sth = $sth->execute(array($policy, $domain));
116        if (PEAR::isError($sth)) {
117            die($sth->getMessage());
118        }
119        $sth->free();
120    }
121
122    /*
123     *
124     * add_domain():  adds a domain default account.
125     *                Copies defaults in from system default.
126     */
127    function add_domain($domain_name) {
128        global $dbh;
129        global $logger;
130
131        if (get_domain_id($domain_name) != 0) {
132            $logger->warning("Attempt to add duplicate domain:" . $domain_name);
133            return 0;
134        }
135
136        // get default autocreation policy
137        $default_policies            = get_default_domain_policies();
138        $default_autocreation_policy = $default_policies['autocreation'];
139        $default_transport           = $default_policies['transport'];
140        $routing_domain              = substr($domain_name,1);
141
142        // Add the domain to the maia_domains table
143        $sth = $dbh->prepare("INSERT INTO maia_domains (domain, enable_user_autocreation, routing_domain, transport) VALUES (?,?,?,?)");
144        $sth->execute(array($domain_name, $default_autocreation_policy,$routing_domain,$default_transport));
145        if (PEAR::isError($sth)) {
146            die($sth->getMessage());
147        }
148        $sth->free();
149        $sth = $dbh->prepare("SELECT id FROM maia_domains WHERE domain = ?");
150        $res = $sth->execute(array($domain_name));
151        if (PEAR::isError($sth)) {
152            die($sth->getMessage());
153        }
154        if ($row = $res->fetchrow()) {
155            $domain_id = $row["id"];
156        }
157        $sth->free();
158
159        // Add a new policy for the domain, based on the
160        // system defaults.
161        $policy_id = add_policy($domain_name);
162        // Add the domain address to the users table
163        $primary_email_id = add_address_to_user($policy_id, $domain_name, 0, $domain_id);
164
165        $default_user_config = get_maia_user_row(get_user_id("@.", "@."));
166
167        // Add a domain default user to the maia_users table
168        $sth = $dbh->prepare("INSERT INTO maia_users (user_name, primary_email_id, reminders, discard_ham, theme_id) VALUES (?, ?, 'N', ?, ?)");
169        $sth->execute(array($domain_name,
170                                   $primary_email_id,
171                                   $default_user_config["discard_ham"],
172                                   $default_user_config["theme_id"]
173                                   ));
174        if (PEAR::isError($sth)) {
175            die($sth->getMessage());
176        }
177        $sth->free();
178
179        $sth = $dbh->prepare("SELECT id FROM maia_users WHERE user_name = ?");
180        $res = $sth->execute(array($domain_name));
181        if (PEAR::isError($sth)) {
182            die($sth->getMessage());
183        }
184        if ($row = $res->fetchrow()) {
185            $maia_user_id = $row["id"];
186        }
187        $sth->free();
188
189        // Update the users table to link the e-mail address back to the domain
190        $sth = $dbh->prepare("UPDATE users SET maia_user_id = ? WHERE id = ?");
191        $sth->execute(array($maia_user_id, $primary_email_id));
192        if (PEAR::isError($sth)) {
193            die($sth->getMessage());
194        }
195
196        return $domain_id;
197    }
198
199    /*
200     * delete_domain_admin_references(): Deletes all administrator references
201     *                                   to the specified domain.
202     */
203    function delete_domain_admin_references($domain_id)
204    {
205        global $dbh;
206
207        // List all the administrators that reference the target domain.
208        $sth = $dbh->prepare("SELECT admin_id FROM maia_domain_admins WHERE domain_id = ?");
209        $res = $sth->execute(array($domain_id));
210        if (PEAR::isError($sth)) {
211            die($sth->getMessage());
212        }
213        while ($row = $res->fetchrow()) {
214            $admin_id = $row["admin_id"];
215
216            // Delete this admin's reference to the target domain.
217            $sth2 = $dbh->prepare("DELETE FROM maia_domain_admins WHERE domain_id = ? AND admin_id = ?");
218            $res2 = $sth2->execute(array($domain_id, $admin_id));
219            if (PEAR::isError($sth2)) {
220                die($sth2->getMessage());
221            }
222            $sth2->free();
223
224            // Does this admin administer any other domains?
225            $sth2 = $dbh->prepare("SELECT domain_id FROM maia_domain_admins WHERE admin_id = ?");
226            $res2 = $sth2->execute(array($admin_id));
227            if (PEAR::isError($sth2)) {
228                die($sth2->getMessage());
229            }
230            if (!$res2->fetchrow()) {
231
232                // This admin no longer administers any domains, so
233                // return his privilege level to that of a normal user.
234                $sth3 = $dbh->prepare("UPDATE maia_users SET user_level = 'U' WHERE id = ?");
235                $res3 = $sth->execute(array($admin_id));
236                if (PEAR::isError($sth3)) {
237                    die($sth3->getMessage());
238                }
239            }
240            $sth2->free();
241        }
242        $sth->free();
243    }
244
245
246    /*
247     * delete_domain(): Deletes the specified domain and all administrator
248     *                  references to it.
249     */
250    function delete_domain($domain_id)
251    {
252        global $dbh;
253
254        // Delete all admin references to this domain.
255        delete_domain_admin_references($domain_id);
256
257        // Delete the domain record itself.
258        $sth = $dbh->prepare("DELETE FROM maia_domains WHERE id = ?");
259        $res = $sth->execute(array($domain_id));
260        if (PEAR::isError($sth)) {
261            die($sth->getMessage());
262        }
263        $sth->free();
264
265        // Find and delete the default user records associated with this domain
266        $sth = $dbh->prepare("SELECT maia_user_id FROM users WHERE maia_domain_id = ?");
267        $res = $sth->execute(array($domain_id));
268        if (PEAR::isError($sth)) {
269            die($sth->getMessage());
270        }
271        if ($row = $res->fetchrow()) {
272            $maia_user_id = $row["maia_user_id"];
273            delete_user($maia_user_id);
274        }
275        $sth->free();
276    }
277