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