Add basic support for a json output.
[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 if (@strtolower($_GET['output']) === 'json') {
130 header('Content-Type: text/javascript');
131 echo $res;
132 exit;
133 }
134
135 // Genererate the page
136 ?>
137 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
138 <html xmlns="http://www.w3.org/1999/xhtml">
139 <head>
140 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
141 <meta name="description" content="WebForum2/Banana" />
142 <link href="css/style.css" type="text/css" rel="stylesheet" media="screen" />
143 <link href="css/banana.css" type="text/css" rel="stylesheet" media="screen" />
144 <?php if ($feed) { ?>
145 <link rel="alternate" type="application/rss+xml" title="Banana :: Abonnements" href="<?php echo htmlentities($feed); ?>" />
146 <?php } ?>
147 <?php if ($css) { ?>
148 <style type="text/css">
149 <?php echo $css; ?>
150 </style>
151 <?php } ?>
152 <title>
153 Banana, a NNTP<->Web Gateway
154 </title>
155 </head>
156 <body>
157 <div class="bloc">
158 <h1>Les Forums de Banana</h1>
159 <?php echo $res; ?>
160 <div>
161 <div style="padding-top: 1ex; float: right; text-align: right; font-size: small">
162 <form action="" method="post">
163 Vous êtes :<br />
164 <?php echo $_SESSION['banana_name'] . ' &lt;' . $_SESSION['banana_email'] . '&gt;'; ?><br />
165 <input type="submit" name="change_login" value="Changer" />
166 </form>
167 </div>
168 <div class="foot">
169 <em>Banana</em>, a Web interface for a NNTP Server<br />
170 Developed under GPL License for <a href="http://www.polytechnique.org">Polytechnique.org</a><br />
171 Use <em>silk</em> icons from <a href="http://www.famfamfam.com/lab/icons/silk/">www.famfamfam.com</a>
172 </div>
173 </div>
174 <?php
175 // Generate the protocole Backtrace at the bottom of the page
176 if ($bt) {
177 echo "<div class=\"backtrace\">";
178 foreach ($bt as &$entry) {
179 echo "<div><pre>" . $entry['action'] . "</pre>";
180 echo "<p style=\"padding-left: 4em;\">"
181 . "Exécution en " . sprintf("%.3fs", $entry['time']) . "<br />"
182 . "Retour : " . $entry['code'] . "<br />"
183 . "Lignes : " . $entry['response'] . "</p></div>";
184 }
185 echo "</div>";
186 }
187 ?>
188 </div>
189 </body>
190 </html>
191 <?php
192
193 // vim:set et sw=4 sts=4 ts=4
194 ?>