| 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 MailNotFound |
| 23 | |
| 24 | class MailNotFound extends Exception { |
| 25 | } |
| 26 | |
| 27 | // }}} |
| 28 | |
| 29 | // {{{ class NewsLetter |
| 30 | |
| 31 | class NewsLetter |
| 32 | { |
| 33 | public $id; // ID of the NL (in table newsletters) |
| 34 | public $group; // Short name of the group corresponding to the NL |
| 35 | public $group_id; // ID of that group |
| 36 | public $name; // Name of the NL (e.g "Lettre de Polytechnique.org", ...) |
| 37 | public $cats; // List of all categories for this NL |
| 38 | public $criteria; // PlFlagSet of allowed filters for recipient selection |
| 39 | |
| 40 | protected $custom_css = false; |
| 41 | |
| 42 | // Base name to use instead of the group short name for NLs without a custom CSS |
| 43 | const FORMAT_DEFAULT_GROUP = 'default'; |
| 44 | |
| 45 | // Diminutif of X.net groups with a specific NL view |
| 46 | const GROUP_XORG = 'Polytechnique.org'; |
| 47 | const GROUP_AX = 'AX'; |
| 48 | const GROUP_EP = 'Ecole'; |
| 49 | |
| 50 | // Searches on mutiple fields |
| 51 | const SEARCH_ALL = 'all'; |
| 52 | const SEARCH_TITLE = 'title'; |
| 53 | |
| 54 | |
| 55 | // {{{ Constructor, NewsLetter retrieval (forGroup, getAll) |
| 56 | |
| 57 | public function __construct($id) |
| 58 | { |
| 59 | // Load NL data |
| 60 | $res = XDB::query('SELECT nls.group_id, g.diminutif AS group_name, |
| 61 | nls.name AS nl_name, nls.custom_css, nls.criteria |
| 62 | FROM newsletters AS nls |
| 63 | LEFT JOIN groups AS g ON (nls.group_id = g.id) |
| 64 | WHERE nls.id = {?}', |
| 65 | $id); |
| 66 | if (!$res->numRows()) { |
| 67 | throw new MailNotFound(); |
| 68 | } |
| 69 | |
| 70 | $data = $res->fetchOneAssoc(); |
| 71 | $this->id = $id; |
| 72 | $this->group_id = $data['group_id']; |
| 73 | $this->group = $data['group_name']; |
| 74 | $this->name = $data['nl_name']; |
| 75 | $this->custom_css = $data['custom_css']; |
| 76 | $this->criteria = new PlFlagSet($data['criteria']); |
| 77 | |
| 78 | // Load the categories |
| 79 | $res = XDB::iterRow( |
| 80 | 'SELECT cid, title |
| 81 | FROM newsletter_cat |
| 82 | WHERE nlid = {?} |
| 83 | ORDER BY pos', $id); |
| 84 | while (list($cid, $title) = $res->next()) { |
| 85 | $this->cats[$cid] = $title; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | /** Retrieve the NL associated with a given group. |
| 90 | * @p $group Short name of the group |
| 91 | * @return A NewsLetter object, or null if the group doesn't have a NL. |
| 92 | */ |
| 93 | public static function forGroup($group) |
| 94 | { |
| 95 | $res = XDB::query('SELECT nls.id |
| 96 | FROM newsletters AS nls |
| 97 | LEFT JOIN groups AS g ON (nls.group_id = g.id) |
| 98 | WHERE g.diminutif = {?}', $group); |
| 99 | if (!$res->numRows()) { |
| 100 | return null; |
| 101 | } |
| 102 | return new NewsLetter($res->fetchOneCell()); |
| 103 | } |
| 104 | |
| 105 | /** Retrieve all newsletters |
| 106 | * @return An array of $id => NewsLetter objects |
| 107 | */ |
| 108 | public static function getAll() |
| 109 | { |
| 110 | $res = XDB::query('SELECT id |
| 111 | FROM newsletters'); |
| 112 | $nls = array(); |
| 113 | foreach ($res->fetchColumn() as $id) { |
| 114 | $nls[$id] = new NewsLetter($id); |
| 115 | } |
| 116 | return $nls; |
| 117 | } |
| 118 | |
| 119 | // }}} |
| 120 | // {{{ Issue retrieval |
| 121 | |
| 122 | /** Retrieve all issues which should be sent |
| 123 | * @return An array of NLIssue objects to send (i.e state = 'new' and send_before <= today) |
| 124 | */ |
| 125 | public static function getIssuesToSend() |
| 126 | { |
| 127 | $res = XDB::query('SELECT id |
| 128 | FROM newsletter_issues |
| 129 | WHERE state = \'pending\' AND send_before <= NOW()'); |
| 130 | $issues = array(); |
| 131 | foreach ($res->fetchColumn() as $id) { |
| 132 | $issues[$id] = new NLIssue($id); |
| 133 | } |
| 134 | return $issues; |
| 135 | } |
| 136 | |
| 137 | /** Retrieve a given issue of this NewsLetter |
| 138 | * @p $name Name or ID of the issue to retrieve. |
| 139 | * @return A NLIssue object. |
| 140 | * |
| 141 | * $name may be either a short_name, an ID or the special value 'last' which |
| 142 | * selects the latest sent NL. |
| 143 | * If $name is null, this will retrieve the current pending NL. |
| 144 | */ |
| 145 | public function getIssue($name = null, $only_sent = true) |
| 146 | { |
| 147 | if ($name) { |
| 148 | if ($name == 'last') { |
| 149 | if ($only_sent) { |
| 150 | $where = 'state = \'sent\' AND '; |
| 151 | } else { |
| 152 | $where = ''; |
| 153 | } |
| 154 | $res = XDB::query('SELECT MAX(id) |
| 155 | FROM newsletter_issues |
| 156 | WHERE ' . $where . ' nlid = {?}', |
| 157 | $this->id); |
| 158 | } else { |
| 159 | $res = XDB::query('SELECT id |
| 160 | FROM newsletter_issues |
| 161 | WHERE nlid = {?} AND (id = {?} OR short_name = {?})', |
| 162 | $this->id, $name, $name); |
| 163 | } |
| 164 | if (!$res->numRows()) { |
| 165 | throw new MailNotFound(); |
| 166 | } |
| 167 | $id = $res->fetchOneCell(); |
| 168 | } else { |
| 169 | $query = XDB::format('SELECT id |
| 170 | FROM newsletter_issues |
| 171 | WHERE nlid = {?} AND state = \'new\' |
| 172 | ORDER BY id DESC', $this->id); |
| 173 | $res = XDB::query($query); |
| 174 | if ($res->numRows()) { |
| 175 | $id = $res->fetchOneCell(); |
| 176 | } else { |
| 177 | // Create a new, empty issue, and return it |
| 178 | $id = $this->createPending(); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | return new NLIssue($id, $this); |
| 183 | } |
| 184 | |
| 185 | /** Create a new, empty, pending newsletter issue |
| 186 | * @p $nlid The id of the NL for which a new pending issue should be created. |
| 187 | * @return Id of the newly created issue. |
| 188 | */ |
| 189 | public function createPending() |
| 190 | { |
| 191 | XDB::execute('INSERT INTO newsletter_issues |
| 192 | SET nlid = {?}, state=\'new\', date=NOW(), |
| 193 | title=\'to be continued\', |
| 194 | mail_title=\'to be continued\'', |
| 195 | $this->id); |
| 196 | return XDB::insertId(); |
| 197 | } |
| 198 | |
| 199 | /** Return all sent issues of this newsletter. |
| 200 | * @return An array of (id => NLIssue) |
| 201 | */ |
| 202 | public function listSentIssues($check_user = false, $user = null) |
| 203 | { |
| 204 | if ($check_user && $user == null) { |
| 205 | $user = S::user(); |
| 206 | } |
| 207 | |
| 208 | $res = XDB::query('SELECT id |
| 209 | FROM newsletter_issues |
| 210 | WHERE nlid = {?} AND state = \'sent\' |
| 211 | ORDER BY date DESC', $this->id); |
| 212 | $issues = array(); |
| 213 | foreach ($res->fetchColumn() as $id) { |
| 214 | $issue = new NLIssue($id, $this, false); |
| 215 | if (!$check_user || $issue->checkUser($user)) { |
| 216 | $issues[$id] = $issue; |
| 217 | } |
| 218 | } |
| 219 | return $issues; |
| 220 | } |
| 221 | |
| 222 | /** Return all issues of this newsletter, including invalid and sent. |
| 223 | * @return An array of (id => NLIssue) |
| 224 | */ |
| 225 | public function listAllIssues() |
| 226 | { |
| 227 | $res = XDB::query('SELECT id |
| 228 | FROM newsletter_issues |
| 229 | WHERE nlid = {?} |
| 230 | ORDER BY FIELD(state, \'pending\', \'new\') DESC, date DESC', $this->id); |
| 231 | $issues = array(); |
| 232 | foreach ($res->fetchColumn() as $id) { |
| 233 | $issues[$id] = new NLIssue($id, $this, false); |
| 234 | } |
| 235 | return $issues; |
| 236 | } |
| 237 | |
| 238 | /** Return the latest pending issue of the newsletter. |
| 239 | * @p $create Whether to create an empty issue if no pending issue exist. |
| 240 | * @return Either null, or a NL object. |
| 241 | */ |
| 242 | public function getPendingIssue($create = false) |
| 243 | { |
| 244 | $res = XDB::query('SELECT MAX(id) |
| 245 | FROM newsletter_issues |
| 246 | WHERE nlid = {?} AND state = \'new\'', |
| 247 | $this->id); |
| 248 | $id = $res->fetchOneCell(); |
| 249 | if ($id != null) { |
| 250 | return new NLIssue($id, $this); |
| 251 | } else if ($create) { |
| 252 | $id = $this->createPending(); |
| 253 | return new NLIssue($id, $this); |
| 254 | } else { |
| 255 | return null; |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | /** Returns a list of either issues or articles corresponding to the search. |
| 260 | * @p $search The searched pattern. |
| 261 | * @p $field The fields where to search, if none given, search in all possible fields. |
| 262 | * @return The list of object found. |
| 263 | */ |
| 264 | public function issueSearch($search, $field, $user) |
| 265 | { |
| 266 | $search = XDB::formatWildcards(XDB::WILDCARD_CONTAINS, $search); |
| 267 | if ($field == self::SEARCH_ALL) { |
| 268 | $where = '(title ' . $search . ' OR mail_title ' . $search . ' OR head ' . $search . ' OR signature ' . $search . ')'; |
| 269 | } elseif ($field == self::SEARCH_TITLE) { |
| 270 | $where = '(title ' . $search . ' OR mail_title ' . $search . ')'; |
| 271 | } else { |
| 272 | $where = $field . $search; |
| 273 | } |
| 274 | $list = XDB::fetchColumn('SELECT DISTINCT(id) |
| 275 | FROM newsletter_issues |
| 276 | WHERE nlid = {?} AND state = \'sent\' AND ' . $where . ' |
| 277 | ORDER BY date DESC', |
| 278 | $this->id); |
| 279 | |
| 280 | $issues = array(); |
| 281 | foreach ($list as $id) { |
| 282 | $issue = new NLIssue($id, $this, false); |
| 283 | if ($issue->checkUser($user)) { |
| 284 | $issues[] = $issue; |
| 285 | } |
| 286 | } |
| 287 | return $issues; |
| 288 | } |
| 289 | |
| 290 | public function articleSearch($search, $field, $user) |
| 291 | { |
| 292 | $search = XDB::formatWildcards(XDB::WILDCARD_CONTAINS, $search); |
| 293 | if ($field == self::SEARCH_ALL) { |
| 294 | $where = '(a.title ' . $search . ' OR a.body ' . $search . ' OR a.append ' . $search . ')'; |
| 295 | } else { |
| 296 | $where = 'a.' . $field . $search; |
| 297 | } |
| 298 | $list = XDB::fetchAllAssoc('SELECT i.short_name, a.aid, i.id, a.title |
| 299 | FROM newsletter_art AS a |
| 300 | INNER JOIN newsletter_issues AS i ON (a.id = i.id) |
| 301 | WHERE i.nlid = {?} AND i.state = \'sent\' AND ' . $where . ' |
| 302 | GROUP BY a.id, a.aid |
| 303 | ORDER BY i.date DESC, a.aid', |
| 304 | $this->id); |
| 305 | |
| 306 | $articles = array(); |
| 307 | foreach ($list as $item) { |
| 308 | $issue = new NLIssue($item['id'], $this, false); |
| 309 | if ($issue->checkUser($user)) { |
| 310 | $articles[] = $item; |
| 311 | } |
| 312 | } |
| 313 | return $articles; |
| 314 | } |
| 315 | |
| 316 | // }}} |
| 317 | // {{{ Subscription related function |
| 318 | |
| 319 | /** Unsubscribe a user from this newsletter |
| 320 | * @p $uid UID to unsubscribe from the newsletter; if null, use current user. |
| 321 | * @p $hash True if the uid is actually a hash. |
| 322 | * @return True if the user was successfully unsubscribed. |
| 323 | */ |
| 324 | public function unsubscribe($issue_id = null, $uid = null, $hash = false) |
| 325 | { |
| 326 | if (is_null($uid) && $hash) { |
| 327 | // Unable to unsubscribe from an empty hash |
| 328 | return false; |
| 329 | } |
| 330 | $user = is_null($uid) ? S::user()->id() : $uid; |
| 331 | $field = $hash ? 'hash' : 'uid'; |
| 332 | $res = XDB::query('SELECT uid |
| 333 | FROM newsletter_ins |
| 334 | WHERE nlid = {?} AND ' . $field . ' = {?}', |
| 335 | $this->id, $user); |
| 336 | if (!$res->numRows()) { |
| 337 | // No subscribed user with that UID/hash |
| 338 | return false; |
| 339 | } |
| 340 | $user = $res->fetchOneCell(); |
| 341 | |
| 342 | XDB::execute('DELETE FROM newsletter_ins |
| 343 | WHERE nlid = {?} AND uid = {?}', |
| 344 | $this->id, $user); |
| 345 | if (!is_null($issue_id)) { |
| 346 | XDB::execute('UPDATE newsletter_issues |
| 347 | SET unsubscribe = unsubscribe + 1 |
| 348 | WHERE id = {?}', |
| 349 | $id); |
| 350 | } |
| 351 | return true; |
| 352 | } |
| 353 | |
| 354 | /** Subscribe a user to a newsletter |
| 355 | * @p $user User to subscribe to the newsletter; if null, use current user. |
| 356 | */ |
| 357 | public function subscribe($user = null) |
| 358 | { |
| 359 | if (is_null($user)) { |
| 360 | $user = S::user(); |
| 361 | } |
| 362 | if (self::maySubscribe($user)) { |
| 363 | XDB::execute('INSERT IGNORE INTO newsletter_ins (nlid, uid, last, hash) |
| 364 | VALUES ({?}, {?}, NULL, hash)', |
| 365 | $this->id, $user->id()); |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | /** Retrieve subscription state of a user |
| 370 | * @p $user Target user; if null, use current user. |
| 371 | * @return Boolean: true if the user has subscribed to the NL. |
| 372 | */ |
| 373 | public function subscriptionState($user = null) |
| 374 | { |
| 375 | if (is_null($user)) { |
| 376 | $user = S::user(); |
| 377 | } |
| 378 | $res = XDB::query('SELECT 1 |
| 379 | FROM newsletter_ins |
| 380 | WHERE nlid = {?} AND uid = {?}', |
| 381 | $this->id, $user->id()); |
| 382 | return ($res->numRows() == 1); |
| 383 | } |
| 384 | |
| 385 | /** Get the count of subscribers to the NL. |
| 386 | * @return Number of subscribers. |
| 387 | */ |
| 388 | public function subscriberCount() |
| 389 | { |
| 390 | return XDB::fetchOneCell('SELECT COUNT(uid) |
| 391 | FROM newsletter_ins |
| 392 | WHERE nlid = {?}', $this->id); |
| 393 | } |
| 394 | |
| 395 | /** Get the number of subscribers to the NL whose last received mailing was $last. |
| 396 | * @p $last ID of the issue for which subscribers should be counted. |
| 397 | * @return Number of subscribers |
| 398 | */ |
| 399 | public function subscriberCountForLast($last) |
| 400 | { |
| 401 | return XDB::fetchOneCell('SELECT COUNT(uid) |
| 402 | FROM newsletter_ins |
| 403 | WHERE nlid = {?} AND last = {?}', $this->id, $last); |
| 404 | } |
| 405 | |
| 406 | /** Retrieve the list of newsletters a user has subscribed to |
| 407 | * @p $user User whose subscriptions should be retrieved (if null, use session user). |
| 408 | * @return Array of newsletter IDs |
| 409 | */ |
| 410 | public static function getUserSubscriptions($user = null) |
| 411 | { |
| 412 | if (is_null($user)) { |
| 413 | $user = S::user(); |
| 414 | } |
| 415 | $res = XDB::query('SELECT nlid |
| 416 | FROM newsletter_ins |
| 417 | WHERE uid = {?}', |
| 418 | $user->id()); |
| 419 | return $res->fetchColumn(); |
| 420 | } |
| 421 | |
| 422 | /** Retrieve the UserFilterBuilder for subscribers to this NL. |
| 423 | * This is the place where NL-specific filters may be allowed or prevented. |
| 424 | * @p $envprefix Prefix to use for env fields (cf. UserFilterBuilder) |
| 425 | * @return A UserFilterBuilder object using the given env prefix |
| 426 | */ |
| 427 | public function getSubscribersUFB($envprefix = '') |
| 428 | { |
| 429 | require_once 'ufbuilder.inc.php'; |
| 430 | return new UFB_NewsLetter($this->criteria, $envprefix); |
| 431 | } |
| 432 | |
| 433 | // }}} |
| 434 | // {{{ Permissions related functions |
| 435 | |
| 436 | /** For later use: check whether a given user may subscribe to this newsletter. |
| 437 | * @p $user User whose access should be checked |
| 438 | * @return Boolean: whether the user may subscribe to the NL. |
| 439 | */ |
| 440 | public function maySubscribe($user = null) |
| 441 | { |
| 442 | return true; |
| 443 | } |
| 444 | |
| 445 | /** Whether a given user may edit this newsletter |
| 446 | * @p $uid UID of the user whose perms should be checked (if null, use current user) |
| 447 | * @return Boolean: whether the user may edit the NL |
| 448 | */ |
| 449 | public function mayEdit($user = null) |
| 450 | { |
| 451 | if (is_null($user)) { |
| 452 | $user = S::user(); |
| 453 | } |
| 454 | if ($user->checkPerms('admin')) { |
| 455 | return true; |
| 456 | } |
| 457 | $res = XDB::query('SELECT perms |
| 458 | FROM group_members |
| 459 | WHERE asso_id = {?} AND uid = {?}', |
| 460 | $this->group_id, $user->id()); |
| 461 | return ($res->numRows() && $res->fetchOneCell() == 'admin'); |
| 462 | } |
| 463 | |
| 464 | /** Whether a given user may submit articles to this newsletter using X.org validation system |
| 465 | * @p $user User whose access should be checked (if null, use current user) |
| 466 | * @return Boolean: whether the user may submit articles |
| 467 | */ |
| 468 | public function maySubmit($user = null) |
| 469 | { |
| 470 | // Submission of new articles is only enabled for the X.org NL (and forbidden when viewing issues on X.net) |
| 471 | return ($this->group == self::GROUP_XORG && !isset($GLOBALS['IS_XNET_SITE'])); |
| 472 | } |
| 473 | |
| 474 | // }}} |
| 475 | // {{{ Display-related functions: cssFile, tplFile, prefix, admin_prefix, admin_links_enabled, automatic_mailings_enabled |
| 476 | |
| 477 | /** Get the name of the css file used to display this newsletter. |
| 478 | */ |
| 479 | public function cssFile() |
| 480 | { |
| 481 | if ($this->custom_css) { |
| 482 | $base = $this->group; |
| 483 | } else { |
| 484 | $base = self::FORMAT_DEFAULT_GROUP; |
| 485 | } |
| 486 | return 'nl.' . $base . '.css'; |
| 487 | } |
| 488 | |
| 489 | /** Get the name of the template file used to display this newsletter. |
| 490 | */ |
| 491 | public function tplFile() |
| 492 | { |
| 493 | if ($this->custom_css) { |
| 494 | $base = $this->group; |
| 495 | } else { |
| 496 | $base = self::FORMAT_DEFAULT_GROUP; |
| 497 | } |
| 498 | return 'newsletter/nl.' . $base . '.mail.tpl'; |
| 499 | } |
| 500 | |
| 501 | /** Get the prefix leading to the page for this NL |
| 502 | * Only X.org / AX / X groups may be seen on X.org. |
| 503 | */ |
| 504 | public function prefix($enforce_xnet=true, $with_group=true) |
| 505 | { |
| 506 | if (!empty($GLOBALS['IS_XNET_SITE'])) { |
| 507 | if ($with_group) { |
| 508 | return $this->group . '/nl'; |
| 509 | } else { |
| 510 | return 'nl'; |
| 511 | } |
| 512 | } |
| 513 | switch ($this->group) { |
| 514 | case self::GROUP_XORG: |
| 515 | return 'nl'; |
| 516 | case self::GROUP_AX: |
| 517 | return 'ax'; |
| 518 | case self::GROUP_EP: |
| 519 | return 'epletter'; |
| 520 | default: |
| 521 | // Don't display groups NLs on X.org |
| 522 | assert(!$enforce_xnet); |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | /** Get the prefix to use for all 'admin' pages of this NL. |
| 527 | */ |
| 528 | public function adminPrefix($enforce_xnet=true, $with_group=true) |
| 529 | { |
| 530 | if (!empty($GLOBALS['IS_XNET_SITE'])) { |
| 531 | if ($with_group) { |
| 532 | return $this->group . '/admin/nl'; |
| 533 | } else { |
| 534 | return 'admin/nl'; |
| 535 | } |
| 536 | } |
| 537 | switch ($this->group) { |
| 538 | case self::GROUP_XORG: |
| 539 | return 'admin/newsletter'; |
| 540 | case self::GROUP_AX: |
| 541 | return 'ax/admin'; |
| 542 | case self::GROUP_EP: |
| 543 | return 'epletter/admin'; |
| 544 | default: |
| 545 | // Don't display groups NLs on X.org |
| 546 | assert(!$enforce_xnet); |
| 547 | } |
| 548 | } |
| 549 | |
| 550 | /** Hack used to remove "admin" links on X.org page on X.net |
| 551 | * The 'admin' links are enabled for all pages, except for X.org when accessing NL through X.net |
| 552 | */ |
| 553 | public function adminLinksEnabled() |
| 554 | { |
| 555 | return ($this->group != self::GROUP_XORG || !isset($GLOBALS['IS_XNET_SITE'])); |
| 556 | } |
| 557 | |
| 558 | /** Automatic mailings are disabled for X.org NL. |
| 559 | */ |
| 560 | public function automaticMailingEnabled() |
| 561 | { |
| 562 | return $this->group != self::GROUP_XORG; |
| 563 | } |
| 564 | |
| 565 | public function hasCustomCss() |
| 566 | { |
| 567 | return $this->custom_css; |
| 568 | } |
| 569 | |
| 570 | public function canSyncWithGroup() |
| 571 | { |
| 572 | switch ($this->group) { |
| 573 | case self::GROUP_XORG: |
| 574 | case self::GROUP_AX: |
| 575 | case self::GROUP_EP: |
| 576 | return false; |
| 577 | default: |
| 578 | return true; |
| 579 | } |
| 580 | } |
| 581 | |
| 582 | // }}} |
| 583 | } |
| 584 | |
| 585 | // }}} |
| 586 | |
| 587 | // {{{ class NLIssue |
| 588 | |
| 589 | // A NLIssue is an issue of a given NewsLetter |
| 590 | class NLIssue |
| 591 | { |
| 592 | protected $nlid; // Id of the newsletter |
| 593 | |
| 594 | const STATE_NEW = 'new'; // New, currently being edited |
| 595 | const STATE_PENDING = 'pending'; // Ready for mailing |
| 596 | const STATE_SENT = 'sent'; // Sent |
| 597 | |
| 598 | public $nl; // Related NL |
| 599 | |
| 600 | public $id; // Id of this issue of the newsletter |
| 601 | public $shortname; // Shortname for this issue |
| 602 | public $title; // Title of this issue |
| 603 | public $title_mail; // Title of the email |
| 604 | public $state; // State of the issue (one of the STATE_ values) |
| 605 | public $sufb; // Environment to use to generate the UFC through an UserFilterBuilder |
| 606 | |
| 607 | public $date; // Date at which this issue was sent |
| 608 | public $send_before; // Date at which issue should be sent |
| 609 | public $head; // Foreword of the issue (or body for letters with no articles) |
| 610 | public $signature; // Signature of the letter |
| 611 | public $arts = array(); // Articles of the issue |
| 612 | |
| 613 | const BATCH_SIZE = 60; // Number of emails to send every minute. |
| 614 | |
| 615 | // {{{ Constructor, id-related functions |
| 616 | |
| 617 | /** Build a NewsLetter. |
| 618 | * @p $id: ID of the issue (unique among all newsletters) |
| 619 | * @p $nl: Optional argument containing an already built NewsLetter object. |
| 620 | */ |
| 621 | function __construct($id, $nl = null, $fetch_articles = true) |
| 622 | { |
| 623 | return $this->fetch($id, $nl, $fetch_articles); |
| 624 | } |
| 625 | |
| 626 | protected function refresh() |
| 627 | { |
| 628 | return $this->fetch($this->id, $this->nl, false); |
| 629 | } |
| 630 | |
| 631 | protected function fetch($id, $nl = null, $fetch_articles = true) |
| 632 | { |
| 633 | // Load this issue |
| 634 | $res = XDB::query('SELECT nlid, short_name, date, send_before, state, sufb_json, |
| 635 | title, mail_title, head, signature |
| 636 | FROM newsletter_issues |
| 637 | WHERE id = {?}', |
| 638 | $id); |
| 639 | if (!$res->numRows()) { |
| 640 | throw new MailNotFound(); |
| 641 | } |
| 642 | $issue = $res->fetchOneAssoc(); |
| 643 | if ($nl && $nl->id == $issue['nlid']) { |
| 644 | $this->nl = $nl; |
| 645 | } else { |
| 646 | $this->nl = new NewsLetter($issue['nlid']); |
| 647 | } |
| 648 | $this->id = $id; |
| 649 | $this->nlid = $issue['nlid']; |
| 650 | $this->shortname = $issue['short_name']; |
| 651 | $this->date = $issue['date']; |
| 652 | $this->send_before = $issue['send_before']; |
| 653 | $this->state = $issue['state']; |
| 654 | $this->title = $issue['title']; |
| 655 | $this->title_mail = $issue['mail_title']; |
| 656 | $this->head = $issue['head']; |
| 657 | $this->signature = $issue['signature']; |
| 658 | $this->sufb = $this->importJSonStoredUFB($issue['sufb_json']); |
| 659 | |
| 660 | if ($fetch_articles) { |
| 661 | $this->fetchArticles(); |
| 662 | } |
| 663 | } |
| 664 | |
| 665 | protected function fetchArticles($force = false) |
| 666 | { |
| 667 | if (count($this->arts) && !$force) { |
| 668 | return; |
| 669 | } |
| 670 | |
| 671 | // Load the articles |
| 672 | $res = XDB::iterRow( |
| 673 | 'SELECT a.title, a.body, a.append, a.aid, a.cid, a.pos |
| 674 | FROM newsletter_art AS a |
| 675 | INNER JOIN newsletter_issues AS ni USING(id) |
| 676 | LEFT JOIN newsletter_cat AS c ON (a.cid = c.cid) |
| 677 | WHERE a.id = {?} |
| 678 | ORDER BY c.pos, a.pos', |
| 679 | $this->id); |
| 680 | while (list($title, $body, $append, $aid, $cid, $pos) = $res->next()) { |
| 681 | $this->arts[$cid][$aid] = new NLArticle($title, $body, $append, $aid, $cid, $pos); |
| 682 | } |
| 683 | } |
| 684 | |
| 685 | protected function importJSonStoredUFB($json = null) |
| 686 | { |
| 687 | require_once 'ufbuilder.inc.php'; |
| 688 | $ufb = $this->nl->getSubscribersUFB(); |
| 689 | if (is_null($json)) { |
| 690 | return new StoredUserFilterBuilder($ufb, new PFC_True()); |
| 691 | } |
| 692 | $export = json_decode($json, true); |
| 693 | if (is_null($export)) { |
| 694 | PlErrorReport::report("Invalid json while reading NL {$this->nlid}, issue {$this->id}: failed to import '''{$json}'''."); |
| 695 | return new StoredUserFilterBuilder($ufb, new PFC_True()); |
| 696 | } |
| 697 | $sufb = new StoredUserFilterBuilder($ufb); |
| 698 | $sufb->fillFromExport($export); |
| 699 | return $sufb; |
| 700 | } |
| 701 | |
| 702 | protected function exportStoredUFBAsJSon() |
| 703 | { |
| 704 | return json_encode($this->sufb->export()); |
| 705 | } |
| 706 | |
| 707 | public function id() |
| 708 | { |
| 709 | return is_null($this->shortname) ? $this->id : $this->shortname; |
| 710 | } |
| 711 | |
| 712 | protected function selectId($where) |
| 713 | { |
| 714 | $res = XDB::query("SELECT IFNULL(ni.short_name, ni.id) |
| 715 | FROM newsletter_issues AS ni |
| 716 | WHERE ni.state != 'new' AND ni.nlid = {?} AND ${where} |
| 717 | LIMIT 1", $this->nl->id); |
| 718 | if ($res->numRows() != 1) { |
| 719 | return null; |
| 720 | } |
| 721 | return $res->fetchOneCell(); |
| 722 | } |
| 723 | |
| 724 | /** Delete this issue |
| 725 | * @return True if the issue could be deleted, false otherwise. |
| 726 | * Related articles will be deleted through cascading FKs. |
| 727 | * If this issue was the last issue for at least one subscriber, the deletion will be aborted. |
| 728 | */ |
| 729 | public function delete() |
| 730 | { |
| 731 | if ($this->state == self::STATE_NEW) { |
| 732 | $res = XDB::query('SELECT COUNT(*) |
| 733 | FROM newsletter_ins |
| 734 | WHERE last = {?}', $this->id); |
| 735 | if ($res->fetchOneCell() > 0) { |
| 736 | return false; |
| 737 | } |
| 738 | |
| 739 | return XDB::execute('DELETE FROM newsletter_issues |
| 740 | WHERE id = {?}', $this->id); |
| 741 | } else { |
| 742 | return false; |
| 743 | } |
| 744 | } |
| 745 | |
| 746 | /** Schedule a mailing of this NL |
| 747 | * If the 'send_before' field was NULL, it is set to the current time. |
| 748 | * @return Boolean Whether the date could be set (false if trying to schedule an already sent NL) |
| 749 | */ |
| 750 | public function scheduleMailing() |
| 751 | { |
| 752 | if ($this->state == self::STATE_NEW) { |
| 753 | $success = XDB::execute('UPDATE newsletter_issues |
| 754 | SET state = \'pending\', send_before = IFNULL(send_before, NOW()) |
| 755 | WHERE id = {?}', |
| 756 | $this->id); |
| 757 | if ($success) { |
| 758 | global $globals; |
| 759 | $mailer = new PlMailer('newsletter/notify_scheduled.mail.tpl'); |
| 760 | $mailer->assign('issue', $this); |
| 761 | $mailer->assign('base', $globals->baseurl); |
| 762 | $mailer->send(); |
| 763 | $this->refresh(); |
| 764 | } |
| 765 | return $success; |
| 766 | } else { |
| 767 | return false; |
| 768 | } |
| 769 | } |
| 770 | |
| 771 | /** Cancel the scheduled mailing of this NL |
| 772 | * @return Boolean: whether the mailing could be cancelled. |
| 773 | */ |
| 774 | public function cancelMailing() |
| 775 | { |
| 776 | if ($this->state == self::STATE_PENDING) { |
| 777 | $success = XDB::execute('UPDATE newsletter_issues |
| 778 | SET state = \'new\' |
| 779 | WHERE id = {?}', $this->id); |
| 780 | if ($success) { |
| 781 | $this->refresh(); |
| 782 | } |
| 783 | return $success; |
| 784 | } else { |
| 785 | return false; |
| 786 | } |
| 787 | } |
| 788 | |
| 789 | /** Helper function for smarty templates: is this issue editable ? |
| 790 | */ |
| 791 | public function isEditable() |
| 792 | { |
| 793 | return $this->state == self::STATE_NEW; |
| 794 | } |
| 795 | |
| 796 | /** Helper function for smarty templates: is the mailing of this issue scheduled ? |
| 797 | */ |
| 798 | public function isPending() |
| 799 | { |
| 800 | return $this->state == self::STATE_PENDING; |
| 801 | } |
| 802 | |
| 803 | /** Helper function for smarty templates: has this issue been sent ? |
| 804 | */ |
| 805 | public function isSent() |
| 806 | { |
| 807 | return $this->state == self::STATE_SENT; |
| 808 | } |
| 809 | |
| 810 | // }}} |
| 811 | // {{{ Navigation |
| 812 | |
| 813 | private $id_prev = null; |
| 814 | private $id_next = null; |
| 815 | private $id_last = null; |
| 816 | |
| 817 | /** Retrieve ID of the previous issue |
| 818 | * That value, once fetched, is cached in the private $id_prev variable. |
| 819 | * @return ID of the previous issue. |
| 820 | */ |
| 821 | public function prev() |
| 822 | { |
| 823 | if (is_null($this->id_prev)) { |
| 824 | $this->id_prev = $this->selectId(XDB::format("ni.id < {?} ORDER BY ni.id DESC", $this->id)); |
| 825 | } |
| 826 | return $this->id_prev; |
| 827 | } |
| 828 | |
| 829 | /** Retrieve ID of the following issue |
| 830 | * That value, once fetched, is cached in the private $id_next variable. |
| 831 | * @return ID of the following issue. |
| 832 | */ |
| 833 | public function next() |
| 834 | { |
| 835 | if (is_null($this->id_next)) { |
| 836 | $this->id_next = $this->selectId(XDB::format("ni.id > {?} ORDER BY ni.id", $this->id)); |
| 837 | } |
| 838 | return $this->id_next; |
| 839 | } |
| 840 | |
| 841 | /** Retrieve ID of the last issue |
| 842 | * That value, once fetched, is cached in the private $id_last variable. |
| 843 | * @return ID of the last issue. |
| 844 | */ |
| 845 | public function last() |
| 846 | { |
| 847 | if (is_null($this->id_last)) { |
| 848 | try { |
| 849 | $this->id_last = $this->nl->getIssue('last')->id; |
| 850 | } catch (MailNotFound $e) { |
| 851 | $this->id_last = null; |
| 852 | } |
| 853 | } |
| 854 | return $this->id_last; |
| 855 | } |
| 856 | |
| 857 | // }}} |
| 858 | // {{{ Edition, articles |
| 859 | |
| 860 | const ERROR_INVALID_SHORTNAME = 'invalid_shortname'; |
| 861 | const ERROR_INVALID_UFC = 'invalid_ufc'; |
| 862 | const ERROR_TOO_LONG_UFC = 'too_long_ufc'; |
| 863 | const ERROR_SQL_SAVE = 'sql_error'; |
| 864 | |
| 865 | /** Save the global properties of this NL issue (title&co). |
| 866 | */ |
| 867 | public function save() |
| 868 | { |
| 869 | $errors = array(); |
| 870 | |
| 871 | // Fill the list of fields to update |
| 872 | $fields = array( |
| 873 | 'title' => $this->title, |
| 874 | 'mail_title' => $this->title_mail, |
| 875 | 'head' => $this->head, |
| 876 | 'signature' => $this->signature, |
| 877 | ); |
| 878 | |
| 879 | if ($this->isEditable()) { |
| 880 | $fields['date'] = $this->date; |
| 881 | if (!preg_match('/^[-a-z0-9]+$/i', $this->shortname) || is_numeric($this->shortname)) { |
| 882 | $errors[] = self::ERROR_INVALID_SHORTNAME; |
| 883 | } else { |
| 884 | $fields['short_name'] = $this->shortname; |
| 885 | } |
| 886 | if ($this->sufb->isValid() || $this->sufb->isEmpty()) { |
| 887 | $fields['sufb_json'] = json_encode($this->sufb->export()->dict()); |
| 888 | // If sufb_json is too long to be store, we do not store a truncated json and notify the user. |
| 889 | // The limit is LONGTEXT's one, ie 2^32 = 4294967296. |
| 890 | if (strlen($fields['sufb_json']) > 4294967295) { |
| 891 | $errors[] = self::ERROR_TOO_LONG_UFC; |
| 892 | } |
| 893 | } else { |
| 894 | $errors[] = self::ERROR_INVALID_UFC; |
| 895 | } |
| 896 | |
| 897 | if ($this->nl->automaticMailingEnabled()) { |
| 898 | $fields['send_before'] = ($this->send_before ? $this->send_before : null); |
| 899 | } |
| 900 | } |
| 901 | |
| 902 | if (count($errors)) { |
| 903 | return $errors; |
| 904 | } |
| 905 | $field_sets = array(); |
| 906 | foreach ($fields as $key => $value) { |
| 907 | $field_sets[] = XDB::format($key . ' = {?}', $value); |
| 908 | } |
| 909 | XDB::execute('UPDATE newsletter_issues |
| 910 | SET ' . implode(', ', $field_sets) . ' |
| 911 | WHERE id={?}', |
| 912 | $this->id); |
| 913 | if (XDB::affectedRows()) { |
| 914 | $this->refresh(); |
| 915 | } else { |
| 916 | $errors[] = self::ERROR_SQL_SAVE; |
| 917 | } |
| 918 | return $errors; |
| 919 | } |
| 920 | |
| 921 | /** Get an article by number |
| 922 | * @p $aid Article ID (among articles of the issue) |
| 923 | * @return A NLArticle object, or null if there is no article by that number |
| 924 | */ |
| 925 | public function getArt($aid) |
| 926 | { |
| 927 | $this->fetchArticles(); |
| 928 | |
| 929 | foreach ($this->arts as $category => $artlist) { |
| 930 | if (isset($artlist[$aid])) { |
| 931 | return $artlist[$aid]; |
| 932 | } |
| 933 | } |
| 934 | return null; |
| 935 | } |
| 936 | |
| 937 | /** Save an article |
| 938 | * @p $a A reference to a NLArticle object (will be modified once saved) |
| 939 | */ |
| 940 | public function saveArticle($a) |
| 941 | { |
| 942 | $this->fetchArticles(); |
| 943 | |
| 944 | // Prevent cid to be 0 (use NULL instead) |
| 945 | $a->cid = ($a->cid == 0) ? null : $a->cid; |
| 946 | if ($a->aid >= 0) { |
| 947 | // Article already exists in DB |
| 948 | XDB::execute('UPDATE newsletter_art |
| 949 | SET cid = {?}, pos = {?}, title = {?}, body = {?}, append = {?} |
| 950 | WHERE id = {?} AND aid = {?}', |
| 951 | $a->cid, $a->pos, $a->title, $a->body, $a->append, $this->id, $a->aid); |
| 952 | } else { |
| 953 | // New article |
| 954 | XDB::startTransaction(); |
| 955 | list($aid, $pos) = XDB::fetchOneRow('SELECT MAX(aid) AS aid, MAX(pos) AS pos |
| 956 | FROM newsletter_art AS a |
| 957 | WHERE a.id = {?}', |
| 958 | $this->id); |
| 959 | $a->aid = ++$aid; |
| 960 | $a->pos = ($a->pos ? $a->pos : ++$pos); |
| 961 | XDB::execute('INSERT INTO newsletter_art (id, aid, cid, pos, title, body, append) |
| 962 | VALUES ({?}, {?}, {?}, {?}, {?}, {?}, {?})', |
| 963 | $this->id, $a->aid, $a->cid, $a->pos, |
| 964 | $a->title, $a->body, $a->append); |
| 965 | XDB::commit(); |
| 966 | } |
| 967 | // Update local ID of article |
| 968 | $this->arts[$a->aid] = $a; |
| 969 | } |
| 970 | |
| 971 | /** Delete an article by its ID |
| 972 | * @p $aid ID of the article to delete |
| 973 | */ |
| 974 | public function delArticle($aid) |
| 975 | { |
| 976 | $this->fetchArticles(); |
| 977 | |
| 978 | XDB::execute('DELETE FROM newsletter_art WHERE id={?} AND aid={?}', $this->id, $aid); |
| 979 | foreach ($this->arts as $key=>$art) { |
| 980 | unset($this->arts[$key][$aid]); |
| 981 | } |
| 982 | } |
| 983 | |
| 984 | // }}} |
| 985 | // {{{ Display |
| 986 | |
| 987 | /** Retrieve the title of this issue |
| 988 | * @p $mail Whether we want the normal title or the email subject |
| 989 | * @return Title of the issue |
| 990 | */ |
| 991 | public function title($mail = false) |
| 992 | { |
| 993 | return $mail ? $this->title_mail : $this->title; |
| 994 | } |
| 995 | |
| 996 | /** Retrieve the head of this issue |
| 997 | * @p $user User for <dear> customization (may be null: no customization) |
| 998 | * @p $type Either 'text' or 'html' |
| 999 | * @return Formatted head of the issue. |
| 1000 | */ |
| 1001 | public function head($user = null, $type = 'text') |
| 1002 | { |
| 1003 | if (is_null($user)) { |
| 1004 | return $this->head; |
| 1005 | } else { |
| 1006 | $head = $this->head; |
| 1007 | $head = str_replace(array('<cher>', '<prenom>', '<nom>'), |
| 1008 | array(($user->isFemale() ? 'Chère' : 'Cher'), $user->displayName(), ''), |
| 1009 | $head); |
| 1010 | return format_text($head, $type, 2, 64); |
| 1011 | } |
| 1012 | } |
| 1013 | |
| 1014 | /** Retrieve the formatted signature of this issue. |
| 1015 | */ |
| 1016 | public function signature($type = 'text') |
| 1017 | { |
| 1018 | return format_text($this->signature, $type, 2, 64); |
| 1019 | } |
| 1020 | |
| 1021 | /** Get the title of a given category |
| 1022 | * @p $cid ID of the category to retrieve |
| 1023 | * @return Name of the category |
| 1024 | */ |
| 1025 | public function category($cid) |
| 1026 | { |
| 1027 | return $this->nl->cats[$cid]; |
| 1028 | } |
| 1029 | |
| 1030 | /** Add required data to the given $page for proper CSS display |
| 1031 | * @p $page Smarty object |
| 1032 | * @return Either 'true' (if CSS was added to a page) or the raw CSS to add (when $page is null) |
| 1033 | */ |
| 1034 | public function css($page = null) |
| 1035 | { |
| 1036 | if (!is_null($page)) { |
| 1037 | $page->addCssLink($this->nl->cssFile()); |
| 1038 | return true; |
| 1039 | } else { |
| 1040 | $css = file_get_contents(dirname(__FILE__) . '/../htdocs/css/' . $this->nl->cssFile()); |
| 1041 | return preg_replace('@/\*.*?\*/@us', '', $css); |
| 1042 | } |
| 1043 | } |
| 1044 | |
| 1045 | /** Set up a smarty page for a 'text' mode render of the issue |
| 1046 | * @p $page Smarty object (using the $this->nl->tplFile() template) |
| 1047 | * @p $user User to use when rendering the template |
| 1048 | */ |
| 1049 | public function toText($page, $user) |
| 1050 | { |
| 1051 | $this->fetchArticles(); |
| 1052 | |
| 1053 | $this->css($page); |
| 1054 | $page->assign('prefix', null); |
| 1055 | $page->assign('is_mail', false); |
| 1056 | $page->assign('mail_part', 'text'); |
| 1057 | $page->assign('user', $user); |
| 1058 | $page->assign('hash', null); |
| 1059 | $this->assignData($page); |
| 1060 | } |
| 1061 | |
| 1062 | /** Set up a smarty page for a 'html' mode render of the issue |
| 1063 | * @p $page Smarty object (using the $this->nl->tplFile() template) |
| 1064 | * @p $user User to use when rendering the template |
| 1065 | */ |
| 1066 | public function toHtml($page, $user) |
| 1067 | { |
| 1068 | $this->fetchArticles(); |
| 1069 | |
| 1070 | $this->css($page); |
| 1071 | $page->assign('prefix', $this->nl->prefix() . '/show/' . $this->id()); |
| 1072 | $page->assign('is_mail', false); |
| 1073 | $page->assign('mail_part', 'html'); |
| 1074 | $page->assign('user', $user); |
| 1075 | $page->assign('hash', null); |
| 1076 | $this->assignData($page); |
| 1077 | } |
| 1078 | |
| 1079 | /** Set all 'common' data for the page (those which are required for both web and email rendering) |
| 1080 | * @p $smarty Smarty object (e.g page) which should be filled |
| 1081 | */ |
| 1082 | protected function assignData($smarty) |
| 1083 | { |
| 1084 | $this->fetchArticles(); |
| 1085 | |
| 1086 | $smarty->assign_by_ref('issue', $this); |
| 1087 | $smarty->assign_by_ref('nl', $this->nl); |
| 1088 | } |
| 1089 | |
| 1090 | // }}} |
| 1091 | // {{{ Mailing |
| 1092 | |
| 1093 | /** Check whether this issue is empty |
| 1094 | * An issue is empty if the email has no title (or the default one), or no articles and an empty head. |
| 1095 | */ |
| 1096 | public function isEmpty() |
| 1097 | { |
| 1098 | return $this->title_mail == '' || $this->title_mail == 'to be continued' || (count($this->arts) == 0 && strlen($this->head) == 0); |
| 1099 | } |
| 1100 | |
| 1101 | /** Retrieve the 'Send before' date, in a clean format. |
| 1102 | */ |
| 1103 | public function getSendBeforeDate() |
| 1104 | { |
| 1105 | return strftime('%Y-%m-%d', strtotime($this->send_before)); |
| 1106 | } |
| 1107 | |
| 1108 | /** Retrieve the 'Send before' time (i.e hour), in a clean format. |
| 1109 | */ |
| 1110 | public function getSendBeforeTime() |
| 1111 | { |
| 1112 | return strtotime($this->send_before); |
| 1113 | } |
| 1114 | |
| 1115 | /** Create a hash based on some additional data |
| 1116 | * $line Line-specific data (to prevent two hashes generated at the same time to be the same) |
| 1117 | */ |
| 1118 | protected static function createHash($line) |
| 1119 | { |
| 1120 | $hash = implode(time(), $line) . rand(); |
| 1121 | $hash = md5($hash); |
| 1122 | return $hash; |
| 1123 | } |
| 1124 | |
| 1125 | /** Send this issue to the given user, reusing an existing hash if provided. |
| 1126 | * @p $user User to whom the issue should be mailed |
| 1127 | * @p $hash Optional hash to use in the 'unsubscribe' link; if null, another one will be generated. |
| 1128 | */ |
| 1129 | public function sendTo($user, $hash = null) |
| 1130 | { |
| 1131 | $this->fetchArticles(); |
| 1132 | |
| 1133 | if (is_null($hash)) { |
| 1134 | $hash = XDB::fetchOneCell("SELECT hash |
| 1135 | FROM newsletter_ins |
| 1136 | WHERE uid = {?} AND nlid = {?}", |
| 1137 | $user->id(), $this->nl->id); |
| 1138 | } |
| 1139 | if (is_null($hash)) { |
| 1140 | $hash = self::createHash(array($user->displayName(), $user->fullName(), |
| 1141 | $user->isFemale(), $user->isEmailFormatHtml(), |
| 1142 | rand(), "X.org rulez")); |
| 1143 | XDB::execute("UPDATE newsletter_ins as ni |
| 1144 | SET ni.hash = {?} |
| 1145 | WHERE ni.uid = {?} AND ni.nlid = {?}", |
| 1146 | $hash, $user->id(), $this->nl->id); |
| 1147 | } |
| 1148 | |
| 1149 | $mailer = new PlMailer($this->nl->tplFile()); |
| 1150 | $this->assignData($mailer); |
| 1151 | $mailer->assign('is_mail', true); |
| 1152 | $mailer->assign('user', $user); |
| 1153 | $mailer->assign('prefix', null); |
| 1154 | $mailer->assign('hash', $hash); |
| 1155 | $mailer->sendTo($user); |
| 1156 | } |
| 1157 | |
| 1158 | /** Select a subset of subscribers which should receive the newsletter. |
| 1159 | * NL-Specific selections (not yet received, is subscribed) are done when sending. |
| 1160 | * @return A PlFilterCondition. |
| 1161 | */ |
| 1162 | protected function getRecipientsUFC() |
| 1163 | { |
| 1164 | return $this->sufb->getUFC(); |
| 1165 | } |
| 1166 | |
| 1167 | /** Check whether a given user may see this issue. |
| 1168 | * @p $user User whose access should be checked |
| 1169 | * @return Whether he may access the issue |
| 1170 | */ |
| 1171 | public function checkUser($user = null) |
| 1172 | { |
| 1173 | if ($user == null) { |
| 1174 | $user = S::user(); |
| 1175 | } |
| 1176 | $uf = new UserFilter($this->getRecipientsUFC()); |
| 1177 | return $uf->checkUser($user); |
| 1178 | } |
| 1179 | |
| 1180 | /** Sent this issue to all valid recipients |
| 1181 | * @return Number of issues sent |
| 1182 | */ |
| 1183 | public function sendToAll() |
| 1184 | { |
| 1185 | $this->fetchArticles(); |
| 1186 | |
| 1187 | XDB::execute('UPDATE newsletter_issues |
| 1188 | SET state = \'sent\', date=CURDATE() |
| 1189 | WHERE id = {?}', |
| 1190 | $this->id); |
| 1191 | |
| 1192 | $ufc = new PFC_And($this->getRecipientsUFC(), new UFC_NLSubscribed($this->nl->id, $this->id), new UFC_HasValidEmail()); |
| 1193 | $emailsCount = 0; |
| 1194 | $uf = new UserFilter($ufc, array(new UFO_IsAdmin(), new UFO_Uid())); |
| 1195 | $limit = new PlLimit(self::BATCH_SIZE); |
| 1196 | |
| 1197 | while (true) { |
| 1198 | $sent = array(); |
| 1199 | $users = $uf->getUsers($limit); |
| 1200 | if (count($users) == 0) { |
| 1201 | return $emailsCount; |
| 1202 | } |
| 1203 | foreach ($users as $user) { |
| 1204 | $sent[] = $user->id(); |
| 1205 | $this->sendTo($user, $hash); |
| 1206 | ++$emailsCount; |
| 1207 | } |
| 1208 | XDB::execute("UPDATE newsletter_ins |
| 1209 | SET last = {?} |
| 1210 | WHERE nlid = {?} AND uid IN {?}", $this->id, $this->nl->id, $sent); |
| 1211 | |
| 1212 | sleep(60); |
| 1213 | } |
| 1214 | return $emailsCount; |
| 1215 | } |
| 1216 | |
| 1217 | // }}} |
| 1218 | } |
| 1219 | |
| 1220 | // }}} |
| 1221 | // {{{ class NLArticle |
| 1222 | |
| 1223 | class NLArticle |
| 1224 | { |
| 1225 | // Maximum number of lines per article |
| 1226 | const MAX_LINES_PER_ARTICLE = 8; |
| 1227 | const MAX_CHARACTERS_PER_LINE = 68; |
| 1228 | |
| 1229 | // {{{ properties |
| 1230 | |
| 1231 | public $aid; |
| 1232 | public $cid; |
| 1233 | public $pos; |
| 1234 | public $title; |
| 1235 | public $body; |
| 1236 | public $append; |
| 1237 | |
| 1238 | // }}} |
| 1239 | // {{{ constructor |
| 1240 | |
| 1241 | function __construct($title='', $body='', $append='', $aid=-1, $cid=0, $pos=0) |
| 1242 | { |
| 1243 | $this->body = $body; |
| 1244 | $this->title = $title; |
| 1245 | $this->append = $append; |
| 1246 | $this->aid = $aid; |
| 1247 | $this->cid = $cid; |
| 1248 | $this->pos = $pos; |
| 1249 | } |
| 1250 | |
| 1251 | // }}} |
| 1252 | // {{{ function title() |
| 1253 | |
| 1254 | public function title() |
| 1255 | { return trim($this->title); } |
| 1256 | |
| 1257 | // }}} |
| 1258 | // {{{ function body() |
| 1259 | |
| 1260 | public function body() |
| 1261 | { return trim($this->body); } |
| 1262 | |
| 1263 | // }}} |
| 1264 | // {{{ function append() |
| 1265 | |
| 1266 | public function append() |
| 1267 | { return trim($this->append); } |
| 1268 | |
| 1269 | // }}} |
| 1270 | // {{{ function toText() |
| 1271 | |
| 1272 | public function toText($hash = null, $login = null) |
| 1273 | { |
| 1274 | $title = '*'.$this->title().'*'; |
| 1275 | $body = MiniWiki::WikiToText($this->body, true); |
| 1276 | $app = MiniWiki::WikiToText($this->append, false, 4); |
| 1277 | $text = trim("$title\n\n$body\n\n$app")."\n"; |
| 1278 | if (!is_null($hash) && !is_null($login)) { |
| 1279 | $text = str_replace('%HASH%', "$hash/$login", $text); |
| 1280 | } else { |
| 1281 | $text = str_replace('%HASH%', '', $text); |
| 1282 | } |
| 1283 | return $text; |
| 1284 | } |
| 1285 | |
| 1286 | // }}} |
| 1287 | // {{{ function toHtml() |
| 1288 | |
| 1289 | public function toHtml($hash = null, $login = null) |
| 1290 | { |
| 1291 | $title = "<h2 class='xorg_nl'><a id='art{$this->aid}'></a>".pl_entities($this->title()).'</h2>'; |
| 1292 | $body = MiniWiki::WikiToHTML($this->body); |
| 1293 | $app = MiniWiki::WikiToHTML($this->append); |
| 1294 | |
| 1295 | $art = "$title\n"; |
| 1296 | $art .= "<div class='art'>\n$body\n"; |
| 1297 | if ($app) { |
| 1298 | $art .= "<div class='app'>$app</div>"; |
| 1299 | } |
| 1300 | $art .= "</div>\n"; |
| 1301 | if (!is_null($hash) && !is_null($login)) { |
| 1302 | $art = str_replace('%HASH%', "$hash/$login", $art); |
| 1303 | } else { |
| 1304 | $art = str_replace('%HASH%', '', $art); |
| 1305 | } |
| 1306 | |
| 1307 | return $art; |
| 1308 | } |
| 1309 | |
| 1310 | // }}} |
| 1311 | // {{{ function check() |
| 1312 | |
| 1313 | public function check() |
| 1314 | { |
| 1315 | $rest = $this->remain(); |
| 1316 | |
| 1317 | return $rest['remaining_lines'] >= 0; |
| 1318 | } |
| 1319 | |
| 1320 | // }}} |
| 1321 | // {{{ function remain() |
| 1322 | |
| 1323 | public function remain() |
| 1324 | { |
| 1325 | $text = MiniWiki::WikiToText($this->body); |
| 1326 | $array = explode("\n", wordwrap($text, self::MAX_CHARACTERS_PER_LINE)); |
| 1327 | $lines_count = 0; |
| 1328 | foreach ($array as $line) { |
| 1329 | if (trim($line) != '') { |
| 1330 | ++$lines_count; |
| 1331 | } |
| 1332 | } |
| 1333 | |
| 1334 | return array( |
| 1335 | 'remaining_lines' => self::MAX_LINES_PER_ARTICLE - $lines_count, |
| 1336 | 'remaining_characters_for_last_line' => self::MAX_CHARACTERS_PER_LINE - strlen($array[count($array) - 1]) |
| 1337 | ); |
| 1338 | } |
| 1339 | // }}} |
| 1340 | // {{{ function parseUrlsFromArticle() |
| 1341 | |
| 1342 | protected function parseUrlsFromArticle() |
| 1343 | { |
| 1344 | $email_regex = '([a-z0-9.\-+_\$]+@([\-.+_]?[a-z0-9])+)'; |
| 1345 | $url_regex = '((https?|ftp)://[a-zA-Z0-9._%#+/?=&~-]+)'; |
| 1346 | $regex = '{' . $email_regex . '|' . $url_regex . '}i'; |
| 1347 | |
| 1348 | $matches = array(); |
| 1349 | $body_matches = array(); |
| 1350 | if (preg_match_all($regex, $this->body(), $body_matches)) { |
| 1351 | $matches = array_merge($matches, $body_matches[0]); |
| 1352 | } |
| 1353 | |
| 1354 | $append_matches = array(); |
| 1355 | if (preg_match_all($regex, $this->append(), $append_matches)) { |
| 1356 | $matches = array_merge($matches, $append_matches[0]); |
| 1357 | } |
| 1358 | |
| 1359 | return $matches; |
| 1360 | } |
| 1361 | |
| 1362 | // }}} |
| 1363 | // {{{ function getLinkIps() |
| 1364 | |
| 1365 | public function getLinkIps(&$blacklist_host_resolution_count) |
| 1366 | { |
| 1367 | $matches = $this->parseUrlsFromArticle(); |
| 1368 | $article_ips = array(); |
| 1369 | |
| 1370 | if (!empty($matches)) { |
| 1371 | global $globals; |
| 1372 | |
| 1373 | foreach ($matches as $match) { |
| 1374 | $host = parse_url($match, PHP_URL_HOST); |
| 1375 | if ($host == '') { |
| 1376 | list(, $host) = explode('@', $match); |
| 1377 | } |
| 1378 | |
| 1379 | if ($blacklist_host_resolution_count >= $globals->mail->blacklist_host_resolution_limit) { |
| 1380 | break; |
| 1381 | } |
| 1382 | |
| 1383 | if (!preg_match('/^(' . str_replace(' ', '|', $globals->mail->domain_whitelist) . ')$/i', $host)) { |
| 1384 | $article_ips = array_merge($article_ips, array(gethostbyname($host) => $host)); |
| 1385 | ++$blacklist_host_resolution_count; |
| 1386 | } |
| 1387 | } |
| 1388 | } |
| 1389 | |
| 1390 | return $article_ips; |
| 1391 | } |
| 1392 | |
| 1393 | // }}} |
| 1394 | } |
| 1395 | |
| 1396 | // }}} |
| 1397 | |
| 1398 | // {{{ Functions |
| 1399 | |
| 1400 | function format_text($input, $format, $indent = 0, $width = 68) |
| 1401 | { |
| 1402 | if ($format == 'text') { |
| 1403 | return MiniWiki::WikiToText($input, true, $indent, $width, "title"); |
| 1404 | } |
| 1405 | return MiniWiki::WikiToHTML($input, "title"); |
| 1406 | } |
| 1407 | |
| 1408 | // function enriched_to_text($input,$html=false,$just=false,$indent=0,$width=68) |
| 1409 | |
| 1410 | // }}} |
| 1411 | |
| 1412 | // vim:set et sw=4 sts=4 sws=4 enc=utf-8: |
| 1413 | ?> |