| 1 | <?php |
| 2 | /*************************************************************************** |
| 3 | * Copyright (C) 2003-2013 Polytechnique.org * |
| 4 | * http://opensource.polytechnique.org/ * |
| 5 | * * |
| 6 | * This program is free software; you can redistribute it and/or modify * |
| 7 | * it under the terms of the GNU General Public License as published by * |
| 8 | * the Free Software Foundation; either version 2 of the License, or * |
| 9 | * (at your option) any later version. * |
| 10 | * * |
| 11 | * This program is distributed in the hope that it will be useful, * |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 14 | * GNU General Public License for more details. * |
| 15 | * * |
| 16 | * You should have received a copy of the GNU General Public License * |
| 17 | * along with this program; if not, write to the Free Software * |
| 18 | * Foundation, Inc., * |
| 19 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * |
| 20 | ***************************************************************************/ |
| 21 | |
| 22 | // {{{ class MailingList |
| 23 | |
| 24 | class MailingList |
| 25 | { |
| 26 | public $address; // Fully qualified address of the list |
| 27 | public $mbox; // mailbox for the list |
| 28 | public $domain; // domain for the list |
| 29 | protected $mmclient; // The XML-RPC client for Mailman requests |
| 30 | |
| 31 | public function __construct($mbox, $domain, $user=null) |
| 32 | { |
| 33 | $this->mbox = $mbox; |
| 34 | $this->domain = $domain; |
| 35 | $this->address = "$mbox@$domain"; |
| 36 | |
| 37 | if (is_null($user)) { |
| 38 | $user = S::user(); |
| 39 | } |
| 40 | $this->mmclient = new MMList($user, $this->domain); |
| 41 | } |
| 42 | |
| 43 | /** Instantiate a MailingList from its address. |
| 44 | */ |
| 45 | public static function fromAddress($address, $user=null) |
| 46 | { |
| 47 | if (strstr($address, '@') !== false) { |
| 48 | list($mbox, $domain) = explode('@', $address); |
| 49 | } else { |
| 50 | global $globals; |
| 51 | $mbox = $address; |
| 52 | $domain = $globals->mail->domain; |
| 53 | } |
| 54 | return new MailingList($mbox, $domain, $user); |
| 55 | } |
| 56 | |
| 57 | /** Retrieve the MailingList associated with a given promo. |
| 58 | */ |
| 59 | public static function promo($promo, $user=null) |
| 60 | { |
| 61 | global $globals; |
| 62 | $mail_domain = $globals->mail->domain; |
| 63 | return new MailingList('promo', "$promo.$mail_domain", $user); |
| 64 | } |
| 65 | |
| 66 | const KIND_BOUNCE = 'bounces'; |
| 67 | const KIND_OWNER = 'owner'; |
| 68 | public function getAddress($kind) |
| 69 | { |
| 70 | return $this->mbox . '-' . $kind . '@' . $this->domain; |
| 71 | } |
| 72 | |
| 73 | /** Subscribe the current user to the list |
| 74 | */ |
| 75 | public function subscribe() |
| 76 | { |
| 77 | return $this->mmclient->subscribe($this->mbox); |
| 78 | } |
| 79 | |
| 80 | public static function subscribeTo($mbox, $domain, $user=null) |
| 81 | { |
| 82 | $mlist = new MailingList($mbox, $domain, $user); |
| 83 | return $mlist->subscribe(); |
| 84 | } |
| 85 | |
| 86 | public static function subscribePromo($promo, $user=null) |
| 87 | { |
| 88 | $mlist = MailingList::promo($promo, $user); |
| 89 | return $mlist->subscribe(); |
| 90 | } |
| 91 | |
| 92 | /** Subscribe a batch of users to the list |
| 93 | */ |
| 94 | public function subscribeBulk($members) |
| 95 | { |
| 96 | return $this->mmclient->mass_subscribe($this->mbox, $members); |
| 97 | } |
| 98 | |
| 99 | /** Unsubscribe the current user from the list |
| 100 | */ |
| 101 | public function unsubscribe() |
| 102 | { |
| 103 | return $this->mmclient->unsubscribe($this->mbox); |
| 104 | } |
| 105 | |
| 106 | /** Unsubscribe a batch of users from the list |
| 107 | */ |
| 108 | public function unsubscribeBulk($members) |
| 109 | { |
| 110 | return $this->mmclient->mass_unsubscribe($this->mbox, $members); |
| 111 | } |
| 112 | |
| 113 | /** Retrieve owners for the list. |
| 114 | * |
| 115 | * TODO: document the return type |
| 116 | */ |
| 117 | public function getOwners() |
| 118 | { |
| 119 | return $this->mmclient->get_owners($this->mbox); |
| 120 | } |
| 121 | |
| 122 | /** Add an owner to the list |
| 123 | */ |
| 124 | public function addOwner($email) |
| 125 | { |
| 126 | return $this->mmclient->add_owner($this->mbox, $email); |
| 127 | } |
| 128 | |
| 129 | /** Remove an owner from the list |
| 130 | */ |
| 131 | public function removeOwner($email) |
| 132 | { |
| 133 | return $this->mmclient->del_owner($this->mbox, $email); |
| 134 | } |
| 135 | |
| 136 | /** Retrieve members of the list. |
| 137 | * |
| 138 | * TODO: document the return type |
| 139 | */ |
| 140 | public function getMembers() |
| 141 | { |
| 142 | return $this->mmclient->get_members($this->mbox); |
| 143 | } |
| 144 | |
| 145 | /** Retrieve a subset of list members. |
| 146 | * |
| 147 | * TODO: document the return type |
| 148 | */ |
| 149 | public function getMembersLimit($page, $number_per_page) |
| 150 | { |
| 151 | return $this->mmclient->get_members_limit($this->mbox, $page, $number_per_page); |
| 152 | } |
| 153 | |
| 154 | /** Fetch pending list operations. |
| 155 | * |
| 156 | * TODO: document the return type |
| 157 | */ |
| 158 | public function getPendingOps() |
| 159 | { |
| 160 | return $this->mmclient->get_pending_ops($this->mbox); |
| 161 | } |
| 162 | |
| 163 | const REQ_ACCEPT = 1; |
| 164 | const REQ_REJECT = 2; |
| 165 | const REQ_DISCARD = 3; |
| 166 | const REQ_SUBSCRIBE = 4; |
| 167 | |
| 168 | /** Handle a mailing list request |
| 169 | */ |
| 170 | public function handleRequest($kind, $value, $comment='') |
| 171 | { |
| 172 | return $this->mmclient->handle_request($this->mbox, $value, $kind, |
| 173 | utf8_decode($comment)); |
| 174 | } |
| 175 | |
| 176 | /** Retrieve the current status of a pending subscription request |
| 177 | */ |
| 178 | public function getPendingSubscription($email) |
| 179 | { |
| 180 | return $this->mmclient->get_pending_sub($this->mbox, $email); |
| 181 | } |
| 182 | |
| 183 | /** Retrieve pending mails |
| 184 | */ |
| 185 | public function getPendingMail($mid) |
| 186 | { |
| 187 | return $this->mmclient->get_pending_mail($this->mbox, $mid); |
| 188 | } |
| 189 | |
| 190 | /** Create a list |
| 191 | */ |
| 192 | public static function create($mbox, $domain, $user, $description, |
| 193 | $advertise, $moderation_level, $subscription_level, |
| 194 | $owners, $members) |
| 195 | { |
| 196 | $mlist = new MailingList($mbox, $domain, $user); |
| 197 | return $mlist->mmclient->create_list($mlist->mbox, utf8_decode($description), |
| 198 | $advertise, $moderation_level, $subscription_level, |
| 199 | $owners, $members); |
| 200 | } |
| 201 | |
| 202 | /** Delete a list |
| 203 | */ |
| 204 | public function delete($remove_archives=false) |
| 205 | { |
| 206 | return $this->mmclient->delete_list($this->mbox, $remove_archives); |
| 207 | } |
| 208 | |
| 209 | /** Set antispam level. |
| 210 | */ |
| 211 | public function setBogoLevel($level) |
| 212 | { |
| 213 | return $this->mmclient->set_bogo_level($this->mbox, $level); |
| 214 | } |
| 215 | |
| 216 | /** Get antispam level. |
| 217 | * |
| 218 | * @return int |
| 219 | */ |
| 220 | public function getBogoLevel() |
| 221 | { |
| 222 | $bogo = $this->mmclient->get_bogo_level($this->mbox); |
| 223 | return $bogo; |
| 224 | } |
| 225 | |
| 226 | /** Set public options. |
| 227 | * |
| 228 | * @param $options array |
| 229 | */ |
| 230 | public function setOwnerOptions($options) |
| 231 | { |
| 232 | return $this->mmclient->set_owner_options($this->mbox, $options); |
| 233 | } |
| 234 | |
| 235 | /** Retrieve owner options |
| 236 | * |
| 237 | * @return array |
| 238 | */ |
| 239 | public function getOwnerOptions() |
| 240 | { |
| 241 | return $this->mmclient->get_owner_options($this->mbox); |
| 242 | } |
| 243 | |
| 244 | /** Set admin options. |
| 245 | * |
| 246 | * @param $options array |
| 247 | */ |
| 248 | public function setAdminOptions($options) |
| 249 | { |
| 250 | return $this->mmclient->set_admin_options($this->mbox, $options); |
| 251 | } |
| 252 | |
| 253 | /** Retrieve admin options |
| 254 | * |
| 255 | * @return array |
| 256 | */ |
| 257 | public function getAdminOptions() |
| 258 | { |
| 259 | return $this->mmclient->get_admin_options($this->mbox); |
| 260 | } |
| 261 | |
| 262 | /** Check options, optionnally fixing them. |
| 263 | */ |
| 264 | public function checkOptions($fix=false) |
| 265 | { |
| 266 | return $this->mmclient->check_options($this->mbox, $fix); |
| 267 | } |
| 268 | |
| 269 | /** Add an email to the list of whitelisted senders |
| 270 | */ |
| 271 | public function whitelistAdd($email) |
| 272 | { |
| 273 | return $this->mmclient->add_to_wl($this->mbox, $email); |
| 274 | } |
| 275 | |
| 276 | /** Remove an email from the list of whitelisted senders |
| 277 | */ |
| 278 | public function whitelistRemove($email) |
| 279 | { |
| 280 | return $this->mmclient->del_from_wl($this->mbox, $email); |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | // }}} |
| 285 | |
| 286 | // vim:set et sw=4 sts=4 sws=4 enc=utf-8: |
| 287 | ?> |