| 1 | <?php |
| 2 | /*************************************************************************** |
| 3 | * Copyright (C) 2003-2011 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 | /** Subscribe a batch of users to the list |
| 81 | */ |
| 82 | public function subscribeBulk($members) |
| 83 | { |
| 84 | return $this->mmclient->mass_subscribe($this->mbox, $members); |
| 85 | } |
| 86 | |
| 87 | /** Unsubscribe the current user from the list |
| 88 | */ |
| 89 | public function unsubscribe() |
| 90 | { |
| 91 | return $this->mmclient->unsubscribe($this->mbox); |
| 92 | } |
| 93 | |
| 94 | /** Unsubscribe a batch of users from the list |
| 95 | */ |
| 96 | public function unsubscribeBulk($members) |
| 97 | { |
| 98 | return $this->mmclient->mass_unsubscribe($this->mbox, $members); |
| 99 | } |
| 100 | |
| 101 | /** Retrieve owners for the list. |
| 102 | * |
| 103 | * TODO: document the return type |
| 104 | */ |
| 105 | public function getOwners() |
| 106 | { |
| 107 | return $this->mmclient->get_owners($this->mbox); |
| 108 | } |
| 109 | |
| 110 | /** Add an owner to the list |
| 111 | */ |
| 112 | public function addOwner($email) |
| 113 | { |
| 114 | return $this->mmclient->add_owner($this->mbox, $email); |
| 115 | } |
| 116 | |
| 117 | /** Remove an owner from the list |
| 118 | */ |
| 119 | public function removeOwner($email) |
| 120 | { |
| 121 | return $this->mmclient->del_owner($this->mbox, $email); |
| 122 | } |
| 123 | |
| 124 | /** Retrieve members of the list. |
| 125 | * |
| 126 | * TODO: document the return type |
| 127 | */ |
| 128 | public function getMembers() |
| 129 | { |
| 130 | return $this->mmclient->get_members($this->mbox); |
| 131 | } |
| 132 | |
| 133 | /** Retrieve a subset of list members. |
| 134 | * |
| 135 | * TODO: document the return type |
| 136 | */ |
| 137 | public function getMembersLimit($page, $number_per_page) |
| 138 | { |
| 139 | return $this->mmclient->get_members_limit($this->mbox, $page, $number_per_page); |
| 140 | } |
| 141 | |
| 142 | /** Fetch pending list operations. |
| 143 | * |
| 144 | * TODO: document the return type |
| 145 | */ |
| 146 | public function getPendingOps() |
| 147 | { |
| 148 | return $this->mmclient->get_pending_ops($this->mbox); |
| 149 | } |
| 150 | |
| 151 | const REQ_ACCEPT = 1; |
| 152 | const REQ_REJECT = 2; |
| 153 | const REQ_DISCARD = 3; |
| 154 | const REQ_SUBSCRIBE = 4; |
| 155 | |
| 156 | /** Handle a mailing list request |
| 157 | */ |
| 158 | public function handleRequest($kind, $value, $comment='') |
| 159 | { |
| 160 | return $this->mmclient->handle_request($this->mbox, $value, $kind, |
| 161 | utf8_decode($comment)); |
| 162 | } |
| 163 | |
| 164 | /** Retrieve the current status of a pending subscription request |
| 165 | */ |
| 166 | public function getPendingSubscription($email) |
| 167 | { |
| 168 | return $this->mmclient->get_pending_sub($this->mbox, $email); |
| 169 | } |
| 170 | |
| 171 | /** Retrieve pending mails |
| 172 | */ |
| 173 | public function getPendingMail($mid) |
| 174 | { |
| 175 | return $this->mmclient->get_pending_mail($this->mbox, $mid); |
| 176 | } |
| 177 | |
| 178 | /** Create a list |
| 179 | */ |
| 180 | public static function create($mbox, $domain, $user, $description, |
| 181 | $advertise, $moderation_level, $subscription_level, |
| 182 | $owners, $members) |
| 183 | { |
| 184 | $mlist = new MailingList($mbox, $domain, $user); |
| 185 | return $mlist->mmclient->create_list($mlist->mbox, utf8_decode($description), |
| 186 | $advertise, $moderation_level, $subscription_level, |
| 187 | $owners, $members); |
| 188 | } |
| 189 | |
| 190 | /** Delete a list |
| 191 | */ |
| 192 | public function delete($remove_archives=false) |
| 193 | { |
| 194 | return $this->mmclient->delete_list($this->mbox, $remove_archives); |
| 195 | } |
| 196 | |
| 197 | /** Set antispam level. |
| 198 | */ |
| 199 | public function setBogoLevel($level) |
| 200 | { |
| 201 | return $this->mmclient->set_bogo_level($this->mbox); |
| 202 | } |
| 203 | |
| 204 | /** Get antispam level. |
| 205 | * |
| 206 | * @return int |
| 207 | */ |
| 208 | public function getBogoLevel() |
| 209 | { |
| 210 | $bogo = $this->mmclient->get_bogo_level($this->mbox); |
| 211 | return $bogo; |
| 212 | } |
| 213 | |
| 214 | /** Set public options. |
| 215 | * |
| 216 | * @param $options array |
| 217 | */ |
| 218 | public function setOwnerOptions($options) |
| 219 | { |
| 220 | return $this->mmclient->set_owner_options($this->mbox, $options); |
| 221 | } |
| 222 | |
| 223 | /** Retrieve owner options |
| 224 | * |
| 225 | * @return array |
| 226 | */ |
| 227 | public function getOwnerOptions() |
| 228 | { |
| 229 | return $this->mmclient->get_owner_options($this->mbox); |
| 230 | } |
| 231 | |
| 232 | /** Set admin options. |
| 233 | * |
| 234 | * @param $options array |
| 235 | */ |
| 236 | public function setAdminOptions($options) |
| 237 | { |
| 238 | return $this->mmclient->set_admin_options($this->mbox, $options); |
| 239 | } |
| 240 | |
| 241 | /** Retrieve admin options |
| 242 | * |
| 243 | * @return array |
| 244 | */ |
| 245 | public function getAdminOptions() |
| 246 | { |
| 247 | return $this->mmclient->get_admin_options($this->mbox); |
| 248 | } |
| 249 | |
| 250 | /** Check options, optionnally fixing them. |
| 251 | */ |
| 252 | public function checkOptions($fix=false) |
| 253 | { |
| 254 | return $this->mmclient->check_options($this->mbox, $fix); |
| 255 | } |
| 256 | |
| 257 | /** Add an email to the list of whitelisted senders |
| 258 | */ |
| 259 | public function whitelistAdd($email) |
| 260 | { |
| 261 | return $this->mmclient->add_to_wl($this->mbox, $email); |
| 262 | } |
| 263 | |
| 264 | /** Remove an email from the list of whitelisted senders |
| 265 | */ |
| 266 | public function whitelistRemove($email) |
| 267 | { |
| 268 | return $this->mmclient->del_from_wl($this->mbox, $email); |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | // }}} |
| 273 | |
| 274 | // vim:set et sw=4 sts=4 sws=4 enc=utf-8: |
| 275 | ?> |