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