In order to use last svn of banana
[platal.git] / include / banana.inc.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2006 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 require_once('banana/banana.inc.php');
23
24 function hook_formatDisplayHeader($_header, $_text) {
25 global $globals, $banana;
26 if ($_header == 'from') {
27 $id = $banana->post->headers['x-org-id'];
28 $_text = formatFrom($_text);
29 return $_text . ' <a href="' . $globals->baseurl . '/profile/'
30 . $id . '" class="popup2" title="' . $id . '">'
31 . '<img src="' . $globals->baseurl . '/images/loupe.gif" alt="fiche" title="fiche" />'
32 . '</a>';
33 }
34 }
35
36 function hook_checkcancel($_headers) {
37 return ($_headers['x-org-id'] == Session::get('forlife') or has_perms());
38 }
39
40 function hook_shortcuts()
41 {
42 global $globals;
43 return '<a href="' . $globals->baseurl . '/banana/profile">Préférences</a>';
44 }
45
46 function hook_makeLink($params) {
47 global $globals;
48 $base = $globals->baseurl . '/banana';
49 if ($params['subscribe'] == 1) {
50 return $base . '/subscription';
51 }
52 if (isset($params['xface'])) {
53 return $base . '/xface/' . strtr(base64_encode($params['xface']), '+/', '.:');
54 }
55
56 if (!isset($params['group'])) {
57 return $base;
58 }
59 $base .= '/' . $params['group'];
60
61 if (isset($params['first'])) {
62 return $base . '/from/' . $params['first'];
63 }
64 if (isset($params['artid'])) {
65 if ($params['action'] == 'new') {
66 $base .= '/reply';
67 } elseif ($params['action'] == 'cancel') {
68 $base .= '/cancel';
69 } else {
70 $base .= '/read';
71 }
72 return $base . '/' . $params['artid'];
73 }
74
75 if ($params['action'] == 'new') {
76 return $base . '/new';
77 }
78 return $base;
79 }
80
81 function hook_makeImg($img, $alt, $height, $width)
82 {
83 global $globals;
84 $url = $globals->baseurl . '/images/banana/' . $img;
85
86 if (!is_null($width)) {
87 $width = ' width="' . $width . '"';
88 }
89 if (!is_null($height)) {
90 $height = ' height="' . $height . '"';
91 }
92
93 return '<img src="' . $url . '"' . $height . $width . ' alt="' . $alt . '" />';
94 }
95
96 function hook_getSubject(&$subject)
97 {
98 if (preg_match('!(.*\S)\s*\[=> ([^\]\s]+)\]!', $subject, $matches)) {
99 $subject = $matches[1];
100 global $banana;
101 if ($banana->state['group'] == $matches[2]) {
102 return ' [=> ' . $matches[2] . ']';
103 } else {
104 return ' [=> ' . makeHREF(Array('group' => $matches[2]), $matches[2]) . ']';
105 }
106 }
107 return null;
108 }
109
110 class PlatalBanana extends Banana
111 {
112 var $profile = Array( 'name' => '', 'sig' => '', 'org' => 'Utilisateur de Polytechnique.org',
113 'customhdr' =>'', 'display' => 0, 'lastnews' => 0, 'locale' => 'fr_FR', 'subscribe' => array());
114 var $can_attach = false;
115
116 function PlatalBanana()
117 {
118 global $globals;
119
120 $uid = Session::getInt('uid');
121 $req = $globals->xdb->query(
122 "SELECT nom, mail, sig, FIND_IN_SET('threads',flags), FIND_IN_SET('automaj',flags)
123 FROM {$globals->banana->table_prefix}profils
124 WHERE uid={?}", $uid);
125
126 if (!(list($nom,$mail,$sig,$disp,$maj) = $req->fetchOneRow())) {
127 $nom = Session::get('prenom')." ".Session::get('nom');
128 $mail = Session::get('forlife')."@polytechnique.org";
129 $sig = $nom." (".Session::getInt('promo').")";
130 $disp = 0;
131 $maj = 1;
132 }
133 $this->profile['name'] = "$nom <$mail>";
134 $this->profile['sig'] = $sig;
135 $this->profile['display'] = $disp;
136 $this->profile['autoup'] = $maj;
137 $this->profile['lastnews'] = Session::get('banana_last');
138
139 if ($maj) {
140 $globals->xdb->execute("UPDATE auth_user_quick SET banana_last={?} WHERE user_id={?}", gmdate("YmdHis"), $uid);
141 }
142
143 $req = $globals->xdb->query("
144 SELECT nom
145 FROM {$globals->banana->table_prefix}abos
146 LEFT JOIN {$globals->banana->table_prefix}list ON list.fid=abos.fid
147 WHERE uid={?}", $uid);
148 $this->profile['subscribe'] = $req->fetchColumn();
149
150 array_splice($this->show_hdr, count($this->show_hdr) - 2, 0);
151 array_splice($this->parse_hdr, count($this->parse_hdr) - 2, 0, 'x-org-id');
152
153 $this->host = 'news://web_'.Session::get('forlife')
154 .":{$globals->banana->password}@{$globals->banana->server}:{$globals->banana->port}/";
155
156 parent::Banana();
157 }
158
159 function run($params = null)
160 {
161 global $banana, $globals;
162
163 if (Get::get('banana') == 'updateall'
164 || (!is_null($params) && isset($params['banana']) && $params['banana'] == 'updateall')) {
165 $globals->xdb->execute('UPDATE auth_user_quick SET banana_last={?} WHERE user_id={?}', gmdate('YmdHis'), Session::getInt('uid'));
166 $_SESSION['banana_last'] = time();
167 }
168 return Banana::run('PlatalBanana', $params);
169 }
170
171 function action_saveSubs()
172 {
173 global $globals;
174 $uid = Session::getInt('uid');
175
176 $this->profile['subscribe'] = Array();
177 $globals->xdb->execute("DELETE FROM {$globals->banana->table_prefix}abos WHERE uid={?}", $uid);
178 if (!count($_POST['subscribe'])) {
179 return true;
180 }
181
182 $req = $globals->xdb->iterRow("SELECT fid,nom FROM {$globals->banana->table_prefix}list");
183 $fids = array();
184 while (list($fid,$fnom) = $req->next()) {
185 $fids[$fnom] = $fid;
186 }
187
188 $diff = array_diff($_POST['subscribe'], array_keys($fids));
189 foreach ($diff as $g) {
190 $globals->xdb->execute("INSERT INTO {$globals->banana->table_prefix}list (nom) VALUES ({?})", $g);
191 $fids[$g] = mysql_insert_id();
192 }
193
194 foreach ($_POST['subscribe'] as $g) {
195 $globals->xdb->execute("INSERT INTO {$globals->banana->table_prefix}abos (fid,uid) VALUES ({?},{?})", $fids[$g], $uid);
196 $this->profile['subscribe'][] = $g;
197 }
198 }
199 }
200
201 ?>