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