Add basic support for a json output.
[banana.git] / examples / index.php
CommitLineData
90962c86 1<?php
2/********************************************************************************
dfb752b1 3* index.php : Banana NNTP client example
90962c86 4* -----------
5*
6* This file is part of the banana distribution
7* Copyright: See COPYING files that comes with this distribution
8********************************************************************************/
9
4cc7f778 10require_once("banana/banana.inc.php");
fe44f824 11
12session_start();
13
dfb752b1 14// Some configuration
15Banana::$nntp_host = 'news://user:password@host:119/'; // where is the news server
16Banana::$spool_root = dirname(__FILE__) . '/spool'; // where to store cache files
17Banana::$debug_nntp = false; // if true, show the NNTP backtrace
18Banana::$debug_smarty = false; // if true, shos php-error in page generation
19Banana::$feed_active = true; // Activate RSS feed
068c6301 20Banana::$feed_updateOnDemand = true; // Update the feed cache when it is acceeded
dfb752b1 21
22// Implement a Banana which stores subscription list in a cookie
fe44f824 23class MyBanana extends Banana
24{
25 protected function action_saveSubs($groups)
26 {
27 parent::action_saveSubs($groups);
dfb752b1 28 setcookie('banana_subs', serialize(Banana::$profile['subscribe']), time() + 25920000);
fe44f824 29 return true;
30 }
31}
32
6f5d05ae
FB
33// Implements storage of a list of read messages
34// (this is only an example of what is possible)
35function 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
44function 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
068c6301 58// Minimalist login
5a2b2063 59if ((@$_GET['action'] == 'rss2') ||
068c6301 60 (!isset($_SESSION['banana_email']) || isset($_POST['change_login']) || isset($_POST['valid_change']))) {
67f43f06 61 if (isset($_COOKIE['banana_email']) && !isset($_POST['change_login']) && !isset($_POST['valid_change'])) {
e83d5aa0 62 $_SESSION['banana_email'] = $_COOKIE['banana_email'];
67f43f06 63 $_SESSION['banana_name'] = $_COOKIE['banana_name'];
e83d5aa0 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
dfb752b1 105// Restore subscription list
fe44f824 106if (isset($_COOKIE['banana_subs'])) {
107 Banana::$profile['subscribe'] = unserialize($_COOKIE['banana_subs']);
108}
dfb752b1 109
110// Compute and set last visit time
fe44f824 111if (!isset($_SESSION['banana_lastnews']) && isset($_COOKIE['banana_lastnews'])) {
112 $_SESSION['banana_lastnews'] = $_COOKIE['banana_lastnews'];
dfb752b1 113} else if (!isset($_SESSION['banana_lastnews'])) {
114 $_SESSION['banana_lastnews'] = 0;
fe44f824 115}
e83d5aa0 116Banana::$profile['signature'] = $_SESSION['banana_name'];
117Banana::$profile['headers']['From'] = '"' . $_SESSION['banana_name'] . '" <' . $_SESSION['banana_email'] . '>';
dfb752b1 118Banana::$profile['lastnews'] = $_SESSION['banana_lastnews'];
119setcookie('banana_lastnews', time(), time() + 25920000);
fe44f824 120
dfb752b1 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
90962c86 127
fe44f824 128session_write_close();
8f4ed520
FB
129if (@strtolower($_GET['output']) === 'json') {
130 header('Content-Type: text/javascript');
131 echo $res;
132 exit;
133}
fe44f824 134
dfb752b1 135// Genererate the page
90962c86 136?>
0e25d15d 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">
8d99c683 139 <head>
62add405 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" />
dfb752b1 144<?php if ($feed) { ?>
145 <link rel="alternate" type="application/rss+xml" title="Banana :: Abonnements" href="<?php echo htmlentities($feed); ?>" />
146<?php } ?>
1ee29bd5 147<?php if ($css) { ?>
148 <style type="text/css">
149 <?php echo $css; ?>
150 </style>
151<?php } ?>
8d99c683 152 <title>
153 Banana, a NNTP<->Web Gateway
154 </title>
155 </head>
156 <body>
157 <div class="bloc">
fe44f824 158 <h1>Les Forums de Banana</h1>
159 <?php echo $res; ?>
e83d5aa0 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>
8d99c683 168 <div class="foot">
169 <em>Banana</em>, a Web interface for a NNTP Server<br />
e83d5aa0 170 Developed under GPL License for <a href="http://www.polytechnique.org">Polytechnique.org</a><br />
35bf53d0 171 Use <em>silk</em> icons from <a href="http://www.famfamfam.com/lab/icons/silk/">www.famfamfam.com</a>
8d99c683 172 </div>
e83d5aa0 173 </div>
1ee29bd5 174<?php
dfb752b1 175 // Generate the protocole Backtrace at the bottom of the page
1ee29bd5 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?>
8d99c683 188 </div>
189 </body>
190</html>
608f5b71 191<?php
d5588318 192
193// vim:set et sw=4 sts=4 ts=4
608f5b71 194?>