Commit | Line | Data |
---|---|---|
0337d704 | 1 | <?php |
2 | /*************************************************************************** | |
12262f13 | 3 | * Copyright (C) 2003-2011 Polytechnique.org * |
0337d704 | 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 | ||
84163d58 RB |
22 | // {{{ class MailNotFound |
23 | ||
24 | class MailNotFound extends Exception { | |
25 | } | |
26 | ||
27 | // }}} | |
0337d704 | 28 | |
0337d704 | 29 | // {{{ class NewsLetter |
30 | ||
84163d58 | 31 | class NewsLetter |
0337d704 | 32 | { |
84163d58 RB |
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; | |
0337d704 | 41 | |
84163d58 RB |
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 | ||
5e9a29ed SJ |
50 | // Searches on mutiple fields |
51 | const SEARCH_ALL = 'all'; | |
52 | const SEARCH_TITLE = 'title'; | |
53 | ||
54 | ||
84163d58 RB |
55 | // {{{ Constructor, NewsLetter retrieval (forGroup, getAll) |
56 | ||
57 | public function __construct($id) | |
0337d704 | 58 | { |
84163d58 RB |
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); | |
ed76a506 | 163 | } |
ed76a506 | 164 | if (!$res->numRows()) { |
84163d58 RB |
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(); | |
ed76a506 | 179 | } |
ad6ca77d | 180 | } |
84163d58 | 181 | |
26ba053e | 182 | return new NLIssue($id, $this); |
84163d58 RB |
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. | |
b8bcbe02 | 239 | * @p $create Whether to create an empty issue if no pending issue exist. |
84163d58 RB |
240 | * @return Either null, or a NL object. |
241 | */ | |
b8bcbe02 | 242 | public function getPendingIssue($create = false) |
84163d58 RB |
243 | { |
244 | $res = XDB::query('SELECT MAX(id) | |
245 | FROM newsletter_issues | |
246 | WHERE nlid = {?} AND state = \'new\'', | |
247 | $this->id); | |
8dc39e6a RB |
248 | $id = $res->fetchOneCell(); |
249 | if ($id != null) { | |
84163d58 | 250 | return new NLIssue($id, $this); |
b8bcbe02 RB |
251 | } else if ($create) { |
252 | $id = $this->createPending(); | |
253 | return new NLIssue($id, $this); | |
84163d58 RB |
254 | } else { |
255 | return null; | |
256 | } | |
257 | } | |
258 | ||
5e9a29ed SJ |
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 | ||
84163d58 RB |
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 | */ | |
602119a2 | 324 | public function unsubscribe($issue_id = null, $uid = null, $hash = false) |
84163d58 RB |
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); | |
602119a2 SJ |
345 | if (!is_null($issue_id)) { |
346 | XDB::execute('UPDATE newsletter_issues | |
347 | SET unsubscribe = unsubscribe + 1 | |
348 | WHERE id = {?}', | |
349 | $id); | |
350 | } | |
84163d58 RB |
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 | */ | |
b6d52898 | 504 | public function prefix($enforce_xnet=true, $with_group=true) |
84163d58 RB |
505 | { |
506 | if (!empty($GLOBALS['IS_XNET_SITE'])) { | |
b6d52898 RB |
507 | if ($with_group) { |
508 | return $this->group . '/nl'; | |
509 | } else { | |
510 | return 'nl'; | |
511 | } | |
84163d58 RB |
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 | |
c8fc2813 | 522 | assert(!$enforce_xnet); |
84163d58 RB |
523 | } |
524 | } | |
525 | ||
526 | /** Get the prefix to use for all 'admin' pages of this NL. | |
527 | */ | |
b6d52898 | 528 | public function adminPrefix($enforce_xnet=true, $with_group=true) |
84163d58 RB |
529 | { |
530 | if (!empty($GLOBALS['IS_XNET_SITE'])) { | |
b6d52898 RB |
531 | if ($with_group) { |
532 | return $this->group . '/admin/nl'; | |
533 | } else { | |
534 | return 'admin/nl'; | |
535 | } | |
84163d58 RB |
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 | |
e9700d3e | 546 | assert(!$enforce_xnet); |
84163d58 RB |
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 | // }}} | |
571 | } | |
572 | ||
573 | // }}} | |
574 | ||
575 | // {{{ class NLIssue | |
576 | ||
577 | // A NLIssue is an issue of a given NewsLetter | |
578 | class NLIssue | |
579 | { | |
580 | protected $nlid; // Id of the newsletter | |
581 | ||
582 | const STATE_NEW = 'new'; // New, currently being edited | |
583 | const STATE_PENDING = 'pending'; // Ready for mailing | |
584 | const STATE_SENT = 'sent'; // Sent | |
585 | ||
586 | public $nl; // Related NL | |
587 | ||
588 | public $id; // Id of this issue of the newsletter | |
589 | public $shortname; // Shortname for this issue | |
590 | public $title; // Title of this issue | |
591 | public $title_mail; // Title of the email | |
592 | public $state; // State of the issue (one of the STATE_ values) | |
593 | public $sufb; // Environment to use to generate the UFC through an UserFilterBuilder | |
594 | ||
595 | public $date; // Date at which this issue was sent | |
596 | public $send_before; // Date at which issue should be sent | |
597 | public $head; // Foreword of the issue (or body for letters with no articles) | |
598 | public $signature; // Signature of the letter | |
599 | public $arts = array(); // Articles of the issue | |
600 | ||
601 | const BATCH_SIZE = 60; // Number of emails to send every minute. | |
602 | ||
603 | // {{{ Constructor, id-related functions | |
604 | ||
605 | /** Build a NewsLetter. | |
606 | * @p $id: ID of the issue (unique among all newsletters) | |
607 | * @p $nl: Optional argument containing an already built NewsLetter object. | |
608 | */ | |
609 | function __construct($id, $nl = null, $fetch_articles = true) | |
610 | { | |
611 | return $this->fetch($id, $nl, $fetch_articles); | |
612 | } | |
613 | ||
614 | protected function refresh() | |
615 | { | |
616 | return $this->fetch($this->id, $this->nl, false); | |
617 | } | |
618 | ||
619 | protected function fetch($id, $nl = null, $fetch_articles = true) | |
620 | { | |
621 | // Load this issue | |
622 | $res = XDB::query('SELECT nlid, short_name, date, send_before, state, sufb_json, | |
623 | title, mail_title, head, signature | |
624 | FROM newsletter_issues | |
625 | WHERE id = {?}', | |
626 | $id); | |
627 | if (!$res->numRows()) { | |
9da70671 SJ |
628 | throw new MailNotFound(); |
629 | } | |
84163d58 RB |
630 | $issue = $res->fetchOneAssoc(); |
631 | if ($nl && $nl->id == $issue['nlid']) { | |
632 | $this->nl = $nl; | |
633 | } else { | |
634 | $this->nl = new NewsLetter($issue['nlid']); | |
635 | } | |
636 | $this->id = $id; | |
b9518dc7 | 637 | $this->nlid = $issue['nlid']; |
84163d58 RB |
638 | $this->shortname = $issue['short_name']; |
639 | $this->date = $issue['date']; | |
640 | $this->send_before = $issue['send_before']; | |
641 | $this->state = $issue['state']; | |
642 | $this->title = $issue['title']; | |
643 | $this->title_mail = $issue['mail_title']; | |
644 | $this->head = $issue['head']; | |
645 | $this->signature = $issue['signature']; | |
646 | $this->sufb = $this->importJSonStoredUFB($issue['sufb_json']); | |
0337d704 | 647 | |
84163d58 RB |
648 | if ($fetch_articles) { |
649 | $this->fetchArticles(); | |
650 | } | |
651 | } | |
0337d704 | 652 | |
84163d58 RB |
653 | protected function fetchArticles($force = false) |
654 | { | |
655 | if (count($this->arts) && !$force) { | |
656 | return; | |
ed76a506 | 657 | } |
eaf30d86 | 658 | |
84163d58 | 659 | // Load the articles |
ed76a506 | 660 | $res = XDB::iterRow( |
84163d58 RB |
661 | 'SELECT a.title, a.body, a.append, a.aid, a.cid, a.pos |
662 | FROM newsletter_art AS a | |
663 | INNER JOIN newsletter_issues AS ni USING(id) | |
664 | LEFT JOIN newsletter_cat AS c ON (a.cid = c.cid) | |
665 | WHERE a.id = {?} | |
666 | ORDER BY c.pos, a.pos', | |
667 | $this->id); | |
ed76a506 | 668 | while (list($title, $body, $append, $aid, $cid, $pos) = $res->next()) { |
84163d58 RB |
669 | $this->arts[$cid][$aid] = new NLArticle($title, $body, $append, $aid, $cid, $pos); |
670 | } | |
671 | } | |
672 | ||
673 | protected function importJSonStoredUFB($json = null) | |
674 | { | |
675 | require_once 'ufbuilder.inc.php'; | |
676 | $ufb = $this->nl->getSubscribersUFB(); | |
677 | if (is_null($json)) { | |
678 | return new StoredUserFilterBuilder($ufb, new PFC_True()); | |
679 | } | |
680 | $export = json_decode($json, true); | |
681 | if (is_null($export)) { | |
682 | PlErrorReport::report("Invalid json while reading NL {$this->nlid}, issue {$this->id}: failed to import '''{$json}'''."); | |
683 | return new StoredUserFilterBuilder($ufb, new PFC_True()); | |
684 | } | |
685 | $sufb = new StoredUserFilterBuilder($ufb); | |
686 | $sufb->fillFromExport($export); | |
687 | return $sufb; | |
688 | } | |
689 | ||
690 | protected function exportStoredUFBAsJSon() | |
691 | { | |
692 | return json_encode($this->sufb->export()); | |
693 | } | |
694 | ||
695 | public function id() | |
696 | { | |
697 | return is_null($this->shortname) ? $this->id : $this->shortname; | |
698 | } | |
699 | ||
700 | protected function selectId($where) | |
701 | { | |
702 | $res = XDB::query("SELECT IFNULL(ni.short_name, ni.id) | |
703 | FROM newsletter_issues AS ni | |
704 | WHERE ni.state != 'new' AND ni.nlid = {?} AND ${where} | |
705 | LIMIT 1", $this->nl->id); | |
706 | if ($res->numRows() != 1) { | |
707 | return null; | |
708 | } | |
709 | return $res->fetchOneCell(); | |
710 | } | |
711 | ||
712 | /** Delete this issue | |
713 | * @return True if the issue could be deleted, false otherwise. | |
714 | * Related articles will be deleted through cascading FKs. | |
715 | * If this issue was the last issue for at least one subscriber, the deletion will be aborted. | |
716 | */ | |
717 | public function delete() | |
718 | { | |
719 | if ($this->state == self::STATE_NEW) { | |
720 | $res = XDB::query('SELECT COUNT(*) | |
721 | FROM newsletter_ins | |
722 | WHERE last = {?}', $this->id); | |
723 | if ($res->fetchOneCell() > 0) { | |
724 | return false; | |
725 | } | |
726 | ||
727 | return XDB::execute('DELETE FROM newsletter_issues | |
728 | WHERE id = {?}', $this->id); | |
729 | } else { | |
730 | return false; | |
731 | } | |
732 | } | |
733 | ||
734 | /** Schedule a mailing of this NL | |
735 | * If the 'send_before' field was NULL, it is set to the current time. | |
736 | * @return Boolean Whether the date could be set (false if trying to schedule an already sent NL) | |
737 | */ | |
738 | public function scheduleMailing() | |
739 | { | |
740 | if ($this->state == self::STATE_NEW) { | |
741 | $success = XDB::execute('UPDATE newsletter_issues | |
742 | SET state = \'pending\', send_before = IFNULL(send_before, NOW()) | |
743 | WHERE id = {?}', | |
744 | $this->id); | |
745 | if ($success) { | |
d5524e7b RB |
746 | global $globals; |
747 | $mailer = new PlMailer('newsletter/notify_scheduled.mail.tpl'); | |
e9700d3e | 748 | $mailer->assign('issue', $this); |
d5524e7b | 749 | $mailer->assign('base', $globals->baseurl); |
d5524e7b | 750 | $mailer->send(); |
84163d58 RB |
751 | $this->refresh(); |
752 | } | |
753 | return $success; | |
754 | } else { | |
755 | return false; | |
756 | } | |
757 | } | |
758 | ||
759 | /** Cancel the scheduled mailing of this NL | |
760 | * @return Boolean: whether the mailing could be cancelled. | |
761 | */ | |
762 | public function cancelMailing() | |
763 | { | |
764 | if ($this->state == self::STATE_PENDING) { | |
765 | $success = XDB::execute('UPDATE newsletter_issues | |
a386dd97 | 766 | SET state = \'new\' |
84163d58 RB |
767 | WHERE id = {?}', $this->id); |
768 | if ($success) { | |
769 | $this->refresh(); | |
770 | } | |
771 | return $success; | |
772 | } else { | |
773 | return false; | |
774 | } | |
775 | } | |
776 | ||
777 | /** Helper function for smarty templates: is this issue editable ? | |
778 | */ | |
779 | public function isEditable() | |
780 | { | |
781 | return $this->state == self::STATE_NEW; | |
782 | } | |
783 | ||
784 | /** Helper function for smarty templates: is the mailing of this issue scheduled ? | |
785 | */ | |
786 | public function isPending() | |
787 | { | |
788 | return $this->state == self::STATE_PENDING; | |
789 | } | |
790 | ||
791 | /** Helper function for smarty templates: has this issue been sent ? | |
792 | */ | |
793 | public function isSent() | |
794 | { | |
795 | return $this->state == self::STATE_SENT; | |
796 | } | |
797 | ||
798 | // }}} | |
799 | // {{{ Navigation | |
800 | ||
801 | private $id_prev = null; | |
802 | private $id_next = null; | |
803 | private $id_last = null; | |
804 | ||
805 | /** Retrieve ID of the previous issue | |
806 | * That value, once fetched, is cached in the private $id_prev variable. | |
807 | * @return ID of the previous issue. | |
808 | */ | |
809 | public function prev() | |
810 | { | |
811 | if (is_null($this->id_prev)) { | |
812 | $this->id_prev = $this->selectId(XDB::format("ni.id < {?} ORDER BY ni.id DESC", $this->id)); | |
813 | } | |
814 | return $this->id_prev; | |
815 | } | |
816 | ||
817 | /** Retrieve ID of the following issue | |
818 | * That value, once fetched, is cached in the private $id_next variable. | |
819 | * @return ID of the following issue. | |
820 | */ | |
821 | public function next() | |
822 | { | |
823 | if (is_null($this->id_next)) { | |
824 | $this->id_next = $this->selectId(XDB::format("ni.id > {?} ORDER BY ni.id", $this->id)); | |
825 | } | |
826 | return $this->id_next; | |
827 | } | |
828 | ||
829 | /** Retrieve ID of the last issue | |
830 | * That value, once fetched, is cached in the private $id_last variable. | |
831 | * @return ID of the last issue. | |
832 | */ | |
833 | public function last() | |
834 | { | |
835 | if (is_null($this->id_last)) { | |
b6d52898 RB |
836 | try { |
837 | $this->id_last = $this->nl->getIssue('last')->id; | |
838 | } catch (MailNotFound $e) { | |
839 | $this->id_last = null; | |
840 | } | |
ed76a506 | 841 | } |
84163d58 | 842 | return $this->id_last; |
0337d704 | 843 | } |
844 | ||
84163d58 RB |
845 | // }}} |
846 | // {{{ Edition, articles | |
847 | ||
848 | const ERROR_INVALID_SHORTNAME = 'invalid_shortname'; | |
849 | const ERROR_INVALID_UFC = 'invalid_ufc'; | |
9ce149f3 | 850 | const ERROR_TOO_LONG_UFC = 'too_long_ufc'; |
84163d58 RB |
851 | const ERROR_SQL_SAVE = 'sql_error'; |
852 | ||
853 | /** Save the global properties of this NL issue (title&co). | |
854 | */ | |
4882f4a5 | 855 | public function save() |
0337d704 | 856 | { |
84163d58 RB |
857 | $errors = array(); |
858 | ||
859 | // Fill the list of fields to update | |
860 | $fields = array( | |
861 | 'title' => $this->title, | |
862 | 'mail_title' => $this->title_mail, | |
863 | 'head' => $this->head, | |
864 | 'signature' => $this->signature, | |
865 | ); | |
866 | ||
867 | if ($this->isEditable()) { | |
868 | $fields['date'] = $this->date; | |
869 | if (!preg_match('/^[-a-z0-9]+$/i', $this->shortname) || is_numeric($this->shortname)) { | |
870 | $errors[] = self::ERROR_INVALID_SHORTNAME; | |
871 | } else { | |
872 | $fields['short_name'] = $this->shortname; | |
873 | } | |
874 | if ($this->sufb->isValid() || $this->sufb->isEmpty()) { | |
875 | $fields['sufb_json'] = json_encode($this->sufb->export()->dict()); | |
9ce149f3 SJ |
876 | // If sufb_json is too long to be store, we do not store a truncated json and notify the user. |
877 | // The limit is LONGTEXT's one, ie 2^32 = 4294967296. | |
878 | if (strlen($fields['sufb_json']) > 4294967295) { | |
879 | $errors[] = self::ERROR_TOO_LONG_UFC; | |
880 | } | |
84163d58 RB |
881 | } else { |
882 | $errors[] = self::ERROR_INVALID_UFC; | |
883 | } | |
884 | ||
885 | if ($this->nl->automaticMailingEnabled()) { | |
886 | $fields['send_before'] = ($this->send_before ? $this->send_before : null); | |
887 | } | |
888 | } | |
889 | ||
890 | if (count($errors)) { | |
891 | return $errors; | |
892 | } | |
893 | $field_sets = array(); | |
894 | foreach ($fields as $key => $value) { | |
895 | $field_sets[] = XDB::format($key . ' = {?}', $value); | |
896 | } | |
897 | XDB::execute('UPDATE newsletter_issues | |
898 | SET ' . implode(', ', $field_sets) . ' | |
899 | WHERE id={?}', | |
900 | $this->id); | |
901 | if (XDB::affectedRows()) { | |
902 | $this->refresh(); | |
903 | } else { | |
904 | $errors[] = self::ERROR_SQL_SAVE; | |
905 | } | |
906 | return $errors; | |
ed76a506 | 907 | } |
eaf30d86 | 908 | |
84163d58 RB |
909 | /** Get an article by number |
910 | * @p $aid Article ID (among articles of the issue) | |
911 | * @return A NLArticle object, or null if there is no article by that number | |
912 | */ | |
4882f4a5 | 913 | public function getArt($aid) |
0337d704 | 914 | { |
84163d58 RB |
915 | $this->fetchArticles(); |
916 | ||
917 | foreach ($this->arts as $category => $artlist) { | |
918 | if (isset($artlist[$aid])) { | |
919 | return $artlist[$aid]; | |
0337d704 | 920 | } |
11f44323 | 921 | } |
922 | return null; | |
0337d704 | 923 | } |
924 | ||
84163d58 | 925 | /** Save an article |
26ba053e | 926 | * @p $a A reference to a NLArticle object (will be modified once saved) |
84163d58 | 927 | */ |
26ba053e | 928 | public function saveArticle($a) |
0337d704 | 929 | { |
84163d58 RB |
930 | $this->fetchArticles(); |
931 | ||
932 | // Prevent cid to be 0 (use NULL instead) | |
933 | $a->cid = ($a->cid == 0) ? null : $a->cid; | |
934 | if ($a->aid >= 0) { | |
935 | // Article already exists in DB | |
00ba8a74 SJ |
936 | XDB::execute('UPDATE newsletter_art |
937 | SET cid = {?}, pos = {?}, title = {?}, body = {?}, append = {?} | |
938 | WHERE id = {?} AND aid = {?}', | |
84163d58 | 939 | $a->cid, $a->pos, $a->title, $a->body, $a->append, $this->id, $a->aid); |
11f44323 | 940 | } else { |
84163d58 | 941 | // New article |
00ba8a74 SJ |
942 | XDB::startTransaction(); |
943 | list($aid, $pos) = XDB::fetchOneRow('SELECT MAX(aid) AS aid, MAX(pos) AS pos | |
944 | FROM newsletter_art AS a | |
945 | WHERE a.id = {?}', | |
84163d58 RB |
946 | $this->id); |
947 | $a->aid = ++$aid; | |
948 | $a->pos = ($a->pos ? $a->pos : ++$pos); | |
00ba8a74 SJ |
949 | XDB::execute('INSERT INTO newsletter_art (id, aid, cid, pos, title, body, append) |
950 | VALUES ({?}, {?}, {?}, {?}, {?}, {?}, {?})', | |
84163d58 RB |
951 | $this->id, $a->aid, $a->cid, $a->pos, |
952 | $a->title, $a->body, $a->append); | |
00ba8a74 | 953 | XDB::commit(); |
11f44323 | 954 | } |
84163d58 RB |
955 | // Update local ID of article |
956 | $this->arts[$a->aid] = $a; | |
0337d704 | 957 | } |
eaf30d86 | 958 | |
84163d58 RB |
959 | /** Delete an article by its ID |
960 | * @p $aid ID of the article to delete | |
961 | */ | |
4882f4a5 | 962 | public function delArticle($aid) |
0337d704 | 963 | { |
84163d58 RB |
964 | $this->fetchArticles(); |
965 | ||
966 | XDB::execute('DELETE FROM newsletter_art WHERE id={?} AND aid={?}', $this->id, $aid); | |
967 | foreach ($this->arts as $key=>$art) { | |
968 | unset($this->arts[$key][$aid]); | |
11f44323 | 969 | } |
0337d704 | 970 | } |
971 | ||
84163d58 RB |
972 | // }}} |
973 | // {{{ Display | |
974 | ||
975 | /** Retrieve the title of this issue | |
976 | * @p $mail Whether we want the normal title or the email subject | |
977 | * @return Title of the issue | |
978 | */ | |
979 | public function title($mail = false) | |
4882f4a5 | 980 | { |
84163d58 | 981 | return $mail ? $this->title_mail : $this->title; |
4882f4a5 | 982 | } |
0337d704 | 983 | |
84163d58 RB |
984 | /** Retrieve the head of this issue |
985 | * @p $user User for <dear> customization (may be null: no customization) | |
986 | * @p $type Either 'text' or 'html' | |
987 | * @return Formatted head of the issue. | |
988 | */ | |
989 | public function head($user = null, $type = 'text') | |
0337d704 | 990 | { |
84163d58 RB |
991 | if (is_null($user)) { |
992 | return $this->head; | |
993 | } else { | |
994 | $head = $this->head; | |
995 | $head = str_replace(array('<cher>', '<prenom>', '<nom>'), | |
996 | array(($user->isFemale() ? 'Chère' : 'Cher'), $user->displayName(), ''), | |
997 | $head); | |
998 | return format_text($head, $type, 2, 64); | |
999 | } | |
eaf30d86 | 1000 | } |
0337d704 | 1001 | |
84163d58 RB |
1002 | /** Retrieve the formatted signature of this issue. |
1003 | */ | |
1004 | public function signature($type = 'text') | |
0337d704 | 1005 | { |
84163d58 RB |
1006 | return format_text($this->signature, $type, 2, 64); |
1007 | } | |
1008 | ||
1009 | /** Get the title of a given category | |
1010 | * @p $cid ID of the category to retrieve | |
1011 | * @return Name of the category | |
1012 | */ | |
1013 | public function category($cid) | |
1014 | { | |
1015 | return $this->nl->cats[$cid]; | |
1016 | } | |
1017 | ||
1018 | /** Add required data to the given $page for proper CSS display | |
1019 | * @p $page Smarty object | |
1020 | * @return Either 'true' (if CSS was added to a page) or the raw CSS to add (when $page is null) | |
1021 | */ | |
26ba053e | 1022 | public function css($page = null) |
84163d58 RB |
1023 | { |
1024 | if (!is_null($page)) { | |
1025 | $page->addCssLink($this->nl->cssFile()); | |
1026 | return true; | |
1027 | } else { | |
1028 | $css = file_get_contents(dirname(__FILE__) . '/../htdocs/css/' . $this->nl->cssFile()); | |
1029 | return preg_replace('@/\*.*?\*/@us', '', $css); | |
1030 | } | |
1031 | } | |
1032 | ||
1033 | /** Set up a smarty page for a 'text' mode render of the issue | |
1034 | * @p $page Smarty object (using the $this->nl->tplFile() template) | |
1035 | * @p $user User to use when rendering the template | |
1036 | */ | |
26ba053e | 1037 | public function toText($page, $user) |
84163d58 RB |
1038 | { |
1039 | $this->fetchArticles(); | |
1040 | ||
1041 | $this->css($page); | |
1042 | $page->assign('prefix', null); | |
1043 | $page->assign('is_mail', false); | |
1044 | $page->assign('mail_part', 'text'); | |
1045 | $page->assign('user', $user); | |
1046 | $page->assign('hash', null); | |
1047 | $this->assignData($page); | |
eaf30d86 PH |
1048 | } |
1049 | ||
84163d58 RB |
1050 | /** Set up a smarty page for a 'html' mode render of the issue |
1051 | * @p $page Smarty object (using the $this->nl->tplFile() template) | |
1052 | * @p $user User to use when rendering the template | |
1053 | */ | |
26ba053e | 1054 | public function toHtml($page, $user) |
4882f4a5 | 1055 | { |
84163d58 RB |
1056 | $this->fetchArticles(); |
1057 | ||
1058 | $this->css($page); | |
1059 | $page->assign('prefix', $this->nl->prefix() . '/show/' . $this->id()); | |
1060 | $page->assign('is_mail', false); | |
1061 | $page->assign('mail_part', 'html'); | |
1062 | $page->assign('user', $user); | |
1063 | $page->assign('hash', null); | |
1064 | $this->assignData($page); | |
0337d704 | 1065 | } |
1066 | ||
84163d58 RB |
1067 | /** Set all 'common' data for the page (those which are required for both web and email rendering) |
1068 | * @p $smarty Smarty object (e.g page) which should be filled | |
1069 | */ | |
26ba053e | 1070 | protected function assignData($smarty) |
4882f4a5 | 1071 | { |
84163d58 RB |
1072 | $this->fetchArticles(); |
1073 | ||
1074 | $smarty->assign_by_ref('issue', $this); | |
1075 | $smarty->assign_by_ref('nl', $this->nl); | |
4882f4a5 | 1076 | } |
1077 | ||
84163d58 RB |
1078 | // }}} |
1079 | // {{{ Mailing | |
d5524e7b RB |
1080 | |
1081 | /** Check whether this issue is empty | |
1082 | * An issue is empty if the email has no title (or the default one), or no articles and an empty head. | |
1083 | */ | |
1084 | public function isEmpty() | |
1085 | { | |
a386dd97 | 1086 | return $this->title_mail == '' || $this->title_mail == 'to be continued' || (count($this->arts) == 0 && strlen($this->head) == 0); |
d5524e7b RB |
1087 | } |
1088 | ||
84163d58 RB |
1089 | /** Retrieve the 'Send before' date, in a clean format. |
1090 | */ | |
1091 | public function getSendBeforeDate() | |
4882f4a5 | 1092 | { |
84163d58 | 1093 | return strftime('%Y-%m-%d', strtotime($this->send_before)); |
4882f4a5 | 1094 | } |
1095 | ||
84163d58 RB |
1096 | /** Retrieve the 'Send before' time (i.e hour), in a clean format. |
1097 | */ | |
1098 | public function getSendBeforeTime() | |
4882f4a5 | 1099 | { |
84163d58 | 1100 | return strtotime($this->send_before); |
4882f4a5 | 1101 | } |
1102 | ||
84163d58 RB |
1103 | /** Create a hash based on some additional data |
1104 | * $line Line-specific data (to prevent two hashes generated at the same time to be the same) | |
1105 | */ | |
1106 | protected static function createHash($line) | |
4882f4a5 | 1107 | { |
84163d58 RB |
1108 | $hash = implode(time(), $line) . rand(); |
1109 | $hash = md5($hash); | |
1110 | return $hash; | |
4882f4a5 | 1111 | } |
1112 | ||
84163d58 RB |
1113 | /** Send this issue to the given user, reusing an existing hash if provided. |
1114 | * @p $user User to whom the issue should be mailed | |
1115 | * @p $hash Optional hash to use in the 'unsubscribe' link; if null, another one will be generated. | |
1116 | */ | |
1117 | public function sendTo($user, $hash = null) | |
4882f4a5 | 1118 | { |
84163d58 RB |
1119 | $this->fetchArticles(); |
1120 | ||
1121 | if (is_null($hash)) { | |
1122 | $hash = XDB::fetchOneCell("SELECT hash | |
1123 | FROM newsletter_ins | |
1124 | WHERE uid = {?} AND nlid = {?}", | |
1125 | $user->id(), $this->nl->id); | |
1126 | } | |
1127 | if (is_null($hash)) { | |
1128 | $hash = self::createHash(array($user->displayName(), $user->fullName(), | |
1129 | $user->isFemale(), $user->isEmailFormatHtml(), | |
1130 | rand(), "X.org rulez")); | |
1131 | XDB::execute("UPDATE newsletter_ins as ni | |
1132 | SET ni.hash = {?} | |
1133 | WHERE ni.uid = {?} AND ni.nlid = {?}", | |
1134 | $hash, $user->id(), $this->nl->id); | |
1135 | } | |
1136 | ||
1137 | $mailer = new PlMailer($this->nl->tplFile()); | |
1138 | $this->assignData($mailer); | |
1139 | $mailer->assign('is_mail', true); | |
1140 | $mailer->assign('user', $user); | |
1141 | $mailer->assign('prefix', null); | |
1142 | $mailer->assign('hash', $hash); | |
1143 | $mailer->sendTo($user); | |
4882f4a5 | 1144 | } |
84163d58 RB |
1145 | |
1146 | /** Select a subset of subscribers which should receive the newsletter. | |
1147 | * NL-Specific selections (not yet received, is subscribed) are done when sending. | |
1148 | * @return A PlFilterCondition. | |
1149 | */ | |
1150 | protected function getRecipientsUFC() | |
1151 | { | |
1152 | return $this->sufb->getUFC(); | |
1153 | } | |
1154 | ||
1155 | /** Check whether a given user may see this issue. | |
1156 | * @p $user User whose access should be checked | |
1157 | * @return Whether he may access the issue | |
1158 | */ | |
1159 | public function checkUser($user = null) | |
1160 | { | |
1161 | if ($user == null) { | |
1162 | $user = S::user(); | |
1163 | } | |
1164 | $uf = new UserFilter($this->getRecipientsUFC()); | |
1165 | return $uf->checkUser($user); | |
1166 | } | |
1167 | ||
1168 | /** Sent this issue to all valid recipients | |
1169 | * @return Number of issues sent | |
1170 | */ | |
1171 | public function sendToAll() | |
1172 | { | |
1173 | $this->fetchArticles(); | |
1174 | ||
1175 | XDB::execute('UPDATE newsletter_issues | |
1176 | SET state = \'sent\', date=CURDATE() | |
1177 | WHERE id = {?}', | |
1178 | $this->id); | |
1179 | ||
df6d9034 | 1180 | $ufc = new PFC_And($this->getRecipientsUFC(), new UFC_NLSubscribed($this->nl->id, $this->id), new UFC_HasValidEmail()); |
84163d58 | 1181 | $emailsCount = 0; |
d5524e7b | 1182 | $uf = new UserFilter($ufc, array(new UFO_IsAdmin(), new UFO_Uid())); |
00278954 | 1183 | $limit = new PlLimit(self::BATCH_SIZE); |
84163d58 RB |
1184 | |
1185 | while (true) { | |
1186 | $sent = array(); | |
00278954 | 1187 | $users = $uf->getUsers($limit); |
84163d58 RB |
1188 | if (count($users) == 0) { |
1189 | return $emailsCount; | |
1190 | } | |
1191 | foreach ($users as $user) { | |
1192 | $sent[] = $user->id(); | |
1193 | $this->sendTo($user, $hash); | |
1194 | ++$emailsCount; | |
1195 | } | |
1196 | XDB::execute("UPDATE newsletter_ins | |
1197 | SET last = {?} | |
1198 | WHERE nlid = {?} AND uid IN {?}", $this->id, $this->nl->id, $sent); | |
1199 | ||
1200 | sleep(60); | |
1201 | } | |
1202 | return $emailsCount; | |
1203 | } | |
1204 | ||
1205 | // }}} | |
0337d704 | 1206 | } |
1207 | ||
1208 | // }}} | |
1209 | // {{{ class NLArticle | |
1210 | ||
1211 | class NLArticle | |
1212 | { | |
84163d58 | 1213 | // Maximum number of lines per article |
0fd5be99 SJ |
1214 | const MAX_LINES_PER_ARTICLE = 8; |
1215 | const MAX_CHARACTERS_PER_LINE = 68; | |
84163d58 | 1216 | |
0337d704 | 1217 | // {{{ properties |
eaf30d86 | 1218 | |
84163d58 RB |
1219 | public $aid; |
1220 | public $cid; | |
1221 | public $pos; | |
1222 | public $title; | |
1223 | public $body; | |
1224 | public $append; | |
0337d704 | 1225 | |
1226 | // }}} | |
1227 | // {{{ constructor | |
eaf30d86 | 1228 | |
4882f4a5 | 1229 | function __construct($title='', $body='', $append='', $aid=-1, $cid=0, $pos=0) |
0337d704 | 1230 | { |
84163d58 RB |
1231 | $this->body = $body; |
1232 | $this->title = $title; | |
1233 | $this->append = $append; | |
1234 | $this->aid = $aid; | |
1235 | $this->cid = $cid; | |
1236 | $this->pos = $pos; | |
0337d704 | 1237 | } |
1238 | ||
1239 | // }}} | |
1240 | // {{{ function title() | |
1241 | ||
4882f4a5 | 1242 | public function title() |
84163d58 | 1243 | { return trim($this->title); } |
0337d704 | 1244 | |
1245 | // }}} | |
1246 | // {{{ function body() | |
eaf30d86 | 1247 | |
4882f4a5 | 1248 | public function body() |
84163d58 | 1249 | { return trim($this->body); } |
eaf30d86 | 1250 | |
0337d704 | 1251 | // }}} |
1252 | // {{{ function append() | |
eaf30d86 | 1253 | |
4882f4a5 | 1254 | public function append() |
84163d58 | 1255 | { return trim($this->append); } |
0337d704 | 1256 | |
1257 | // }}} | |
1258 | // {{{ function toText() | |
1259 | ||
e0e77c5a | 1260 | public function toText($hash = null, $login = null) |
0337d704 | 1261 | { |
4882f4a5 | 1262 | $title = '*'.$this->title().'*'; |
84163d58 RB |
1263 | $body = MiniWiki::WikiToText($this->body, true); |
1264 | $app = MiniWiki::WikiToText($this->append, false, 4); | |
e0e77c5a FB |
1265 | $text = trim("$title\n\n$body\n\n$app")."\n"; |
1266 | if (!is_null($hash) && !is_null($login)) { | |
1267 | $text = str_replace('%HASH%', "$hash/$login", $text); | |
1268 | } else { | |
1269 | $text = str_replace('%HASH%', '', $text); | |
1270 | } | |
1271 | return $text; | |
0337d704 | 1272 | } |
1273 | ||
1274 | // }}} | |
1275 | // {{{ function toHtml() | |
1276 | ||
e0e77c5a | 1277 | public function toHtml($hash = null, $login = null) |
0337d704 | 1278 | { |
84163d58 RB |
1279 | $title = "<h2 class='xorg_nl'><a id='art{$this->aid}'></a>".pl_entities($this->title()).'</h2>'; |
1280 | $body = MiniWiki::WikiToHTML($this->body); | |
1281 | $app = MiniWiki::WikiToHTML($this->append); | |
eaf30d86 | 1282 | |
4882f4a5 | 1283 | $art = "$title\n"; |
1284 | $art .= "<div class='art'>\n$body\n"; | |
1285 | if ($app) { | |
0337d704 | 1286 | $art .= "<div class='app'>$app</div>"; |
1287 | } | |
4882f4a5 | 1288 | $art .= "</div>\n"; |
e0e77c5a FB |
1289 | if (!is_null($hash) && !is_null($login)) { |
1290 | $art = str_replace('%HASH%', "$hash/$login", $art); | |
1291 | } else { | |
1292 | $art = str_replace('%HASH%', '', $art); | |
1293 | } | |
eaf30d86 | 1294 | |
4882f4a5 | 1295 | return $art; |
0337d704 | 1296 | } |
1297 | ||
1298 | // }}} | |
1299 | // {{{ function check() | |
1300 | ||
4882f4a5 | 1301 | public function check() |
0337d704 | 1302 | { |
0fd5be99 SJ |
1303 | $rest = $this->remain(); |
1304 | ||
1305 | return $rest['remaining_lines'] >= 0; | |
1306 | } | |
1307 | ||
1308 | // }}} | |
1309 | // {{{ function remain() | |
1310 | ||
1311 | public function remain() | |
1312 | { | |
1313 | $text = MiniWiki::WikiToText($this->body); | |
1314 | $array = explode("\n", wordwrap($text, self::MAX_CHARACTERS_PER_LINE)); | |
1315 | $lines_count = 0; | |
1316 | foreach ($array as $line) { | |
1317 | if (trim($line) != '') { | |
1318 | ++$lines_count; | |
0337d704 | 1319 | } |
1320 | } | |
0337d704 | 1321 | |
0fd5be99 SJ |
1322 | return array( |
1323 | 'remaining_lines' => self::MAX_LINES_PER_ARTICLE - $lines_count, | |
1324 | 'remaining_characters_for_last_line' => self::MAX_CHARACTERS_PER_LINE - strlen($array[count($array) - 1]) | |
1325 | ); | |
1326 | } | |
0337d704 | 1327 | // }}} |
9e2a6a32 SJ |
1328 | // {{{ function parseUrlsFromArticle() |
1329 | ||
84163d58 | 1330 | protected function parseUrlsFromArticle() |
9e2a6a32 SJ |
1331 | { |
1332 | $email_regex = '([a-z0-9.\-+_\$]+@([\-.+_]?[a-z0-9])+)'; | |
1333 | $url_regex = '((https?|ftp)://[a-zA-Z0-9._%#+/?=&~-]+)'; | |
1334 | $regex = '{' . $email_regex . '|' . $url_regex . '}i'; | |
1335 | ||
1336 | $matches = array(); | |
1337 | $body_matches = array(); | |
1338 | if (preg_match_all($regex, $this->body(), $body_matches)) { | |
1339 | $matches = array_merge($matches, $body_matches[0]); | |
1340 | } | |
1341 | ||
1342 | $append_matches = array(); | |
1343 | if (preg_match_all($regex, $this->append(), $append_matches)) { | |
1344 | $matches = array_merge($matches, $append_matches[0]); | |
1345 | } | |
1346 | ||
1347 | return $matches; | |
1348 | } | |
1349 | ||
1350 | // }}} | |
1351 | // {{{ function getLinkIps() | |
1352 | ||
955109ba | 1353 | public function getLinkIps(&$blacklist_host_resolution_count) |
9e2a6a32 SJ |
1354 | { |
1355 | $matches = $this->parseUrlsFromArticle(); | |
1356 | $article_ips = array(); | |
1357 | ||
1358 | if (!empty($matches)) { | |
1359 | global $globals; | |
1360 | ||
1361 | foreach ($matches as $match) { | |
1362 | $host = parse_url($match, PHP_URL_HOST); | |
1363 | if ($host == '') { | |
1364 | list(, $host) = explode('@', $match); | |
1365 | } | |
1366 | ||
955109ba | 1367 | if ($blacklist_host_resolution_count >= $globals->mail->blacklist_host_resolution_limit) { |
9e2a6a32 SJ |
1368 | break; |
1369 | } | |
1370 | ||
955109ba | 1371 | if (!preg_match('/^(' . str_replace(' ', '|', $globals->mail->domain_whitelist) . ')$/i', $host)) { |
9e2a6a32 | 1372 | $article_ips = array_merge($article_ips, array(gethostbyname($host) => $host)); |
955109ba | 1373 | ++$blacklist_host_resolution_count; |
9e2a6a32 SJ |
1374 | } |
1375 | } | |
1376 | } | |
1377 | ||
1378 | return $article_ips; | |
1379 | } | |
1380 | ||
1381 | // }}} | |
0337d704 | 1382 | } |
1383 | ||
1384 | // }}} | |
0337d704 | 1385 | |
84163d58 RB |
1386 | // {{{ Functions |
1387 | ||
1388 | function format_text($input, $format, $indent = 0, $width = 68) | |
1389 | { | |
1390 | if ($format == 'text') { | |
1391 | return MiniWiki::WikiToText($input, true, $indent, $width, "title"); | |
1392 | } | |
1393 | return MiniWiki::WikiToHTML($input, "title"); | |
1394 | } | |
1395 | ||
1396 | // function enriched_to_text($input,$html=false,$just=false,$indent=0,$width=68) | |
1397 | ||
1398 | // }}} | |
1399 | ||
a7de4ef7 | 1400 | // vim:set et sw=4 sts=4 sws=4 enc=utf-8: |
0337d704 | 1401 | ?> |