| 1 | <?php |
| 2 | /*************************************************************************** |
| 3 | * Copyright (C) 2003-2010 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 | /** A feed article. |
| 23 | */ |
| 24 | class PlFeedArticle |
| 25 | { |
| 26 | public $id; |
| 27 | |
| 28 | public $author; |
| 29 | public $author_email; |
| 30 | |
| 31 | public $publication; |
| 32 | public $last_modification; |
| 33 | |
| 34 | public $title; |
| 35 | public $link; |
| 36 | |
| 37 | public $template; |
| 38 | |
| 39 | public function __construct($tpl, array& $data) |
| 40 | { |
| 41 | $this->template = $tpl; |
| 42 | foreach ($data as $key => $value) { |
| 43 | $this->$key = $value; |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | /** An abstract feed. A feed is nothing more than a set of article. |
| 49 | */ |
| 50 | abstract class PlFeed implements PlIterator |
| 51 | { |
| 52 | public $title; |
| 53 | public $link; |
| 54 | public $description; |
| 55 | |
| 56 | public $img_link; |
| 57 | |
| 58 | private $article_tpl; |
| 59 | private $iterator; |
| 60 | |
| 61 | public function __construct($title, $link, $desc, $img_link, |
| 62 | $article_tpl) { |
| 63 | $this->title = $title; |
| 64 | $this->link = $link; |
| 65 | $this->description = $desc; |
| 66 | $this->img_link = $img_link; |
| 67 | $this->article_tpl = $article_tpl; |
| 68 | } |
| 69 | |
| 70 | /** Fetch the feed for the given user. |
| 71 | */ |
| 72 | abstract protected function fetch(PlUser &$user); |
| 73 | |
| 74 | public function next() |
| 75 | { |
| 76 | $data = $this->iterator->next(); |
| 77 | if (!empty($data)) { |
| 78 | return new PlFeedArticle($this->article_tpl, $data); |
| 79 | } |
| 80 | return null; |
| 81 | } |
| 82 | |
| 83 | public function total() |
| 84 | { |
| 85 | return $this->iterator->total(); |
| 86 | } |
| 87 | |
| 88 | public function first() |
| 89 | { |
| 90 | return $this->iterator->first(); |
| 91 | } |
| 92 | |
| 93 | public function last() |
| 94 | { |
| 95 | return $this->iterator->last(); |
| 96 | } |
| 97 | |
| 98 | public function run(PlPage& $page, $login, $token, $require_auth = true, $type = 'rss2') |
| 99 | { |
| 100 | $user = Platal::session()->tokenAuth($login, $token); |
| 101 | if (empty($user)) { |
| 102 | if ($require_auth) { |
| 103 | return PL_FORBIDDEN; |
| 104 | } else { |
| 105 | $user = null; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | $page->assign('rss_hash', $token); |
| 110 | pl_content_headers("application/rss+xml"); |
| 111 | $this->iterator = $this->fetch($user); |
| 112 | $page->coreTpl('feed.' . $type . '.tpl', NO_SKIN); |
| 113 | $page->assign_by_ref('feed', $this); |
| 114 | $page->run(); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
| 119 | ?> |