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