4419ee3d9eb632994c725b27d9ac3c0192914b2a
[banana.git] / examples / index.php
1 <?php
2 /********************************************************************************
3 * index.php : Banana NNTP client example
4 * -----------
5 *
6 * This file is part of the banana distribution
7 * Copyright: See COPYING files that comes with this distribution
8 ********************************************************************************/
9
10 require_once("banana/banana.inc.php");
11
12 session_start();
13
14 // Some configuration
15 Banana::$nntp_host = 'news://user:password@host:119/'; // where is the news server
16 Banana::$spool_root = dirname(__FILE__) . '/spool'; // where to store cache files
17 Banana::$debug_nntp = false; // if true, show the NNTP backtrace
18 Banana::$debug_smarty = false; // if true, shos php-error in page generation
19 Banana::$feed_active = true; // Activate RSS feed
20 Banana::$feed_updateOnDemand = true; // Update the feed cache when it is acceeded
21
22 // Implement a Banana which stores subscription list in a cookie
23 class MyBanana extends Banana
24 {
25 protected function action_saveSubs($groups)
26 {
27 parent::action_saveSubs($groups);
28 setcookie('banana_subs', serialize(Banana::$profile['subscribe']), time() + 25920000);
29 return true;
30 }
31 }
32
33 // Implements storage of a list of read messages
34 // (this is only an example of what is possible)
35 function hook_listReadMessages($group)
36 {
37 if (!isset($_COOKIE['banana_read'])) {
38 return null;
39 }
40 $msgs = unserialize(gzinflate(base64_decode($_COOKIE['banana_read'])));
41 return array_keys($msgs);
42 }
43
44 function hook_markAsRead($group, $artid, $msg)
45 {
46 $msgs = array();
47 if (isset($_COOKIE['banana_read'])) {
48 $msgs = unserialize(gzinflate(base64_decode($_COOKIE['banana_read'])));
49 }
50 $id = $msg->getHeader('message-id');
51 $msgs[$id] = true;
52 $msgs = base64_encode(gzdeflate(serialize($msgs)));
53 setcookie('banana_read', $msgs, 0);
54 $_COOKIE['banana_read'] = $msgs;
55 }
56
57
58 // Minimalist login
59 if ((@$_GET['action'] == 'rss2') ||
60 (!isset($_SESSION['banana_email']) || isset($_POST['change_login']) || isset($_POST['valid_change']))) {
61 if (isset($_COOKIE['banana_email']) && !isset($_POST['change_login']) && !isset($_POST['valid_change'])) {
62 $_SESSION['banana_email'] = $_COOKIE['banana_email'];
63 $_SESSION['banana_name'] = $_COOKIE['banana_name'];
64 } elseif (isset($_POST['valid_change'])) {
65 $_SESSION['banana_name'] = $_POST['name'];
66 $_SESSION['banana_email'] = $_POST['email'];
67 setcookie('banana_name', $_POST['name'], time() + 25920000);
68 setcookie('banana_email', $_POST['email'], time() + 25920000);
69 } else {
70 ?>
71 <html xmlns="http://www.w3.org/1999/xhtml">
72 <head>
73 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
74 <meta name="description" content="WebForum2/Banana" />
75 <link href="css/style.css" type="text/css" rel="stylesheet" media="screen" />
76 <link href="css/banana.css" type="text/css" rel="stylesheet" media="screen">
77 <title>Banana, a NNTP<->Web Gateway</title>
78 </head>
79 <body>
80 <div class="bloc">
81 <h1>Les Forums de Banana</h1>
82 Merci d'entrer vos identifiants pour accéder à Banana :
83 <form action="" method="post">
84 <div class="banana" style="margin: auto; width: 50%">
85 Nom : <input type="text" name="name" size="40" /><br />
86 Email : <input type="text" name="email" size="40" />
87 <div class="center">
88 <input type="submit" name="valid_change" value="Valider" />
89 </div>
90 </div>
91 </form>
92 <div class="foot">
93 <em>Banana</em>, a Web interface for a NNTP Server<br />
94 Developed under GPL License for <a href="http://www.polytechnique.org">Polytechnique.org</a><br />
95 Use <em>silk</em> icons from <a href="http://www.famfamfam.com/lab/icons/silk/">www.famfamfam.com</a>
96 </div>
97 </div>
98 </body>
99 </html>
100 <?php
101 exit;
102 }
103 }
104
105 // Restore subscription list
106 if (isset($_COOKIE['banana_subs'])) {
107 Banana::$profile['subscribe'] = unserialize($_COOKIE['banana_subs']);
108 }
109
110 // Compute and set last visit time
111 if (!isset($_SESSION['banana_lastnews']) && isset($_COOKIE['banana_lastnews'])) {
112 $_SESSION['banana_lastnews'] = $_COOKIE['banana_lastnews'];
113 } else if (!isset($_SESSION['banana_lastnews'])) {
114 $_SESSION['banana_lastnews'] = 0;
115 }
116 Banana::$profile['signature'] = $_SESSION['banana_name'];
117 Banana::$profile['headers']['From'] = '"' . $_SESSION['banana_name'] . '" <' . $_SESSION['banana_email'] . '>';
118 Banana::$profile['lastnews'] = $_SESSION['banana_lastnews'];
119 setcookie('banana_lastnews', time(), time() + 25920000);
120
121 // Run Bananan
122 $banana = new MyBanana(); // Create the instance of Banana
123 $res = $banana->run(); // Run banana, and generate the XHTML output
124 $css = $banana->css(); // Get the CSS code to add in my page headers
125 $feed = $banana->feed(); // Get a link to banana's feed. You need to use Banana::refreshAllFeeds in a cron or enable Banana::$feed_updateOnDemand in order to keep up-to-date feeds
126 $bt = $banana->backtrace(); // Get protocole execution backtrace
127
128 session_write_close();
129
130 // Genererate the page
131 ?>
132 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
133 <html xmlns="http://www.w3.org/1999/xhtml">
134 <head>
135 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
136 <meta name="description" content="WebForum2/Banana" />
137 <link href="css/style.css" type="text/css" rel="stylesheet" media="screen" />
138 <link href="css/banana.css" type="text/css" rel="stylesheet" media="screen" />
139 <?php if ($feed) { ?>
140 <link rel="alternate" type="application/rss+xml" title="Banana :: Abonnements" href="<?php echo htmlentities($feed); ?>" />
141 <?php } ?>
142 <?php if ($css) { ?>
143 <style type="text/css">
144 <?php echo $css; ?>
145 </style>
146 <?php } ?>
147 <title>
148 Banana, a NNTP<->Web Gateway
149 </title>
150 </head>
151 <body>
152 <div class="bloc">
153 <h1>Les Forums de Banana</h1>
154 <?php echo $res; ?>
155 <div>
156 <div style="padding-top: 1ex; float: right; text-align: right; font-size: small">
157 <form action="" method="post">
158 Vous êtes :<br />
159 <?php echo $_SESSION['banana_name'] . ' &lt;' . $_SESSION['banana_email'] . '&gt;'; ?><br />
160 <input type="submit" name="change_login" value="Changer" />
161 </form>
162 </div>
163 <div class="foot">
164 <em>Banana</em>, a Web interface for a NNTP Server<br />
165 Developed under GPL License for <a href="http://www.polytechnique.org">Polytechnique.org</a><br />
166 Use <em>silk</em> icons from <a href="http://www.famfamfam.com/lab/icons/silk/">www.famfamfam.com</a>
167 </div>
168 </div>
169 <?php
170 // Generate the protocole Backtrace at the bottom of the page
171 if ($bt) {
172 echo "<div class=\"backtrace\">";
173 foreach ($bt as &$entry) {
174 echo "<div><pre>" . $entry['action'] . "</pre>";
175 echo "<p style=\"padding-left: 4em;\">"
176 . "Exécution en " . sprintf("%.3fs", $entry['time']) . "<br />"
177 . "Retour : " . $entry['code'] . "<br />"
178 . "Lignes : " . $entry['response'] . "</p></div>";
179 }
180 echo "</div>";
181 }
182 ?>
183 </div>
184 </body>
185 </html>
186 <?php
187
188 // vim:set et sw=4 sts=4 ts=4
189 ?>