8da0d3c1 |
1 | <?php |
2 | /*************************************************************************** |
3 | * Copyright (C) 2003-2007 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 | class AXLetterModule extends PLModule |
23 | { |
24 | function handlers() |
25 | { |
26 | return array( |
27 | 'ax' => $this->make_hook('index', AUTH_COOKIE), |
a0f05027 |
28 | 'ax/out' => $this->make_hook('out', AUTH_PUBLIC), |
8da0d3c1 |
29 | 'ax/show' => $this->make_hook('show', AUTH_COOKIE), |
30 | 'ax/edit' => $this->make_hook('submit', AUTH_MDP), |
31 | 'ax/edit/cancel' => $this->make_hook('cancel', AUTH_MDP), |
32 | 'ax/edit/valid' => $this->make_hook('valid', AUTH_MDP), |
33 | 'admin/axletter/rights' => $this->make_hook('admin_rights', AUTH_MDP, 'admin'), |
34 | ); |
35 | } |
36 | |
a0f05027 |
37 | function handler_out(&$page, $hash) |
38 | { |
39 | if (!$hash) { |
40 | if (!S::logged()) { |
41 | return PL_DO_AUTH; |
42 | } else { |
43 | return $this->handler_index($page, 'out'); |
44 | } |
45 | } |
46 | require_once dirname(__FILE__) . '/axletter/axletter.inc.php'; |
47 | $page->changeTpl('axletter/unsubscribe.tpl'); |
48 | $page->assign('success', AXLetter::unsubscribe($hash, true)); |
49 | } |
50 | |
51 | function handler_index(&$page, $action = null) |
8da0d3c1 |
52 | { |
53 | require_once dirname(__FILE__) . '/axletter/axletter.inc.php'; |
54 | |
55 | $page->changeTpl('axletter/index.tpl'); |
56 | $page->assign('xorg_title','Polytechnique.org - Envois de l\'AX'); |
57 | |
58 | switch ($action) { |
8da0d3c1 |
59 | case 'in': AXLetter::subscribe(); break; |
a0f05027 |
60 | case 'out': AXLetter::unsubscribe(); break; |
8da0d3c1 |
61 | } |
62 | |
63 | $perm = AXLetter::hasPerms(); |
64 | if ($perm) { |
a0f05027 |
65 | $page->assign('new', AXLetter::awaiting()); |
8da0d3c1 |
66 | } |
67 | $page->assign('axs', AXLetter::subscriptionState()); |
68 | $page->assign('ax_list', AXLetter::listSent()); |
69 | $page->assign('ax_rights', $perm); |
70 | } |
71 | |
72 | function handler_submit(&$page, $action = null) |
73 | { |
74 | require_once dirname(__FILE__) . '/axletter/axletter.inc.php'; |
75 | if (!AXLetter::hasPerms()) { |
76 | return PL_FORBIDDEN; |
77 | } |
78 | |
79 | $page->changeTpl('axletter/edit.tpl'); |
80 | |
81 | $saved = Post::i('saved'); |
82 | $new = false; |
83 | $id = Post::i('id'); |
84 | $shortname = trim(Post::v('shortname')); |
85 | $subject = trim(Post::v('subject')); |
86 | $title = trim(Post::v('title')); |
87 | $body = rtrim(Post::v('body')); |
88 | $signature = trim(Post::v('signature')); |
89 | $promo_min = Post::i('promo_min'); |
90 | $promo_max = Post::i('promo_max'); |
91 | $echeance = Post::has('echeance_date') ? Post::v('echeance_date') . ' ' . Post::v('echeance_time') |
92 | : Post::v('echeance'); |
93 | $echeance_date = Post::v('echeance_date'); |
94 | $echeance_time = Post::v('echeance_time'); |
95 | |
96 | if (!$id) { |
97 | $res = XDB::query("SELECT * FROM axletter WHERE FIND_IN_SET('new', bits)"); |
98 | if ($res->numRows()) { |
99 | extract($res->fetchOneAssoc(), EXTR_OVERWRITE); |
100 | $saved = true; |
101 | } else { |
102 | XDB::execute("INSERT INTO axletter SET id = NULL"); |
103 | $id = XDB::insertId(); |
104 | } |
105 | if (!$echeance || $echeance == '0000-00-00 00:00:00') { |
106 | $saved = false; |
107 | $new = true; |
108 | } |
109 | } elseif (Post::has('valid')) { |
110 | if (!$subject && $title) { |
111 | $subject = $title; |
112 | } |
113 | if (!$title && $subject) { |
114 | $title = $subject; |
115 | } |
116 | if (!$subject || !$title || !$body) { |
117 | $page->trig("L'article doit avoir un sujet et un contenu"); |
118 | Post::kill('valid'); |
119 | } |
120 | if (($promo_min > $promo_max && $promo_max != 0)|| |
121 | ($promo_min != 0 && ($promo_min <= 1900 || $promo_min >= 2020)) || |
122 | ($promo_max != 0 && ($promo_max <= 1900 || $promo_max >= 2020))) |
123 | { |
124 | $page->trig("L'intervalle de promotions n'est pas valide"); |
125 | Post::kill('valid'); |
126 | } |
127 | if (empty($shortname)) { |
128 | $page->trig("L'annonce doit avoir un nom raccourci pour simplifier la navigation dans les archives"); |
129 | Post::kill('valid'); |
130 | } elseif (!preg_match('/^[a-z][-a-z0-9]*[a-z0-9]$/', $shortname)) { |
131 | $page->trig("Le nom raccourci n'est pas valide, il doit comporter au moins 2 caractères et n'être composé " |
132 | . "que de chiffres, lettres et tirets"); |
133 | Post::kill('valid'); |
134 | } elseif ($shortname != Post::v('old_shortname')) { |
135 | $res = XDB::query("SELECT id FROM axletter WHERE shortname = {?}", $shortname); |
136 | if ($res->numRows() && $res->fetchOneCell() != $id) { |
137 | $page->trig("Le nom $shortname est déjà utilisé, merci d'en choisir un autre"); |
138 | $shortname = Post::v('old_shortname'); |
139 | if (empty($shortname)) { |
140 | Post::kill('valid'); |
141 | } |
142 | } |
143 | } |
144 | |
145 | switch (@Post::v('valid')) { |
146 | case 'Aperçu': |
147 | require_once dirname(__FILE__) . '/axletter/axletter.inc.php'; |
148 | $al = new AXLetter(array($id, $shortname, $subject, $title, $body, $signature, |
149 | $promo_min, $promo_max, $echeance, 0, 'new')); |
150 | $al->toHtml($page, S::v('prenom'), S::v('nom'), S::v('femme')); |
151 | break; |
152 | |
153 | case 'Confirmer': |
154 | XDB::execute("REPLACE INTO axletter |
155 | SET id = {?}, shortname = {?}, subject = {?}, title = {?}, body = {?}, |
156 | signature = {?}, promo_min = {?}, promo_max = {?}, echeance = {?}", |
157 | $id, $shortname, $subject, $title, $body, $signature, $promo_min, $promo_max, $echeance); |
158 | $saved = true; |
159 | $echeance_date = null; |
160 | $echeance_time = null; |
161 | pl_redirect('ax'); |
162 | break; |
163 | } |
164 | } |
165 | $page->assign('id', $id); |
166 | $page->assign('shortname', $shortname); |
167 | $page->assign('subject', $subject); |
168 | $page->assign('title', $title); |
169 | $page->assign('body', $body); |
170 | $page->assign('signature', $signature); |
171 | $page->assign('promo_min', $promo_min); |
172 | $page->assign('promo_max', $promo_max); |
173 | $page->assign('echeance', $echeance); |
174 | $page->assign('echeance_date', $echeance_date); |
175 | $page->assign('echeance_time', $echeance_time); |
176 | $page->assign('saved', $saved); |
177 | $page->assign('new', $new); |
178 | $page->assign('is_xorg', S::has_perms()); |
179 | |
180 | if (!$saved) { |
181 | $select = ''; |
182 | $time = time() + 3600 * 24 * 2; |
183 | for ($i = 0 ; $i < 15 ; $i++) { |
184 | $time += 3600 * 24; |
185 | $p_stamp = date('Ymd', $time); |
186 | $year = date('Y', $time); |
187 | $month = date('m', $time); |
188 | $day = date('d', $time); |
189 | |
190 | if ($p_stamp == $echeance_date) { |
191 | $sel = ' selected="selected"'; |
192 | } else { |
193 | $sel = ''; |
194 | } |
195 | $select .= "<option value=\"$p_stamp\"$sel> $day / $month / $year</option>\n"; |
196 | } |
197 | $page->assign('echeance_date', $select); |
198 | $select = ''; |
199 | for ($i = 0 ; $i < 24 ; $i++) { |
200 | $stamp = sprintf('%02d:00:00', $i); |
201 | if ($stamp == $echeance_time) { |
202 | $sel = ' selected="selected"'; |
203 | } else { |
204 | $sel = ''; |
205 | } |
206 | $select .= "<option value=\"$stamp\"$sel>{$i}h</option>\n"; |
207 | } |
208 | $page->assign('echeance_time', $select); |
209 | } |
210 | } |
211 | |
212 | function handler_cancel(&$page, $force = null) |
213 | { |
214 | require_once dirname(__FILE__) . '/axletter/axletter.inc.php'; |
215 | if (!AXLetter::hasPerms()) { |
216 | return PL_FORBIDDEN; |
217 | } |
218 | |
219 | $url = parse_url($_SERVER['HTTP_REFERER']); |
220 | if ($force != 'force' && trim($url['path'], '/') != 'ax/edit') { |
221 | return PL_FORBIDDEN; |
222 | } |
223 | |
a0f05027 |
224 | $al = AXLetter::awaiting(); |
225 | if (!$alg) { |
8da0d3c1 |
226 | $page->kill("Aucune lettre en attente"); |
227 | return; |
228 | } |
8da0d3c1 |
229 | if (!$al->invalid()) { |
230 | $page->kill("Une erreur est survenue lors de l'annulation de l'envoi"); |
231 | return; |
232 | } |
233 | |
234 | $page->kill("L'envoi de l'annonce {$al->title()} est annulé"); |
235 | } |
236 | |
237 | function handler_valid(&$page, $force = null) |
238 | { |
239 | require_once dirname(__FILE__) . '/axletter/axletter.inc.php'; |
240 | if (!AXLetter::hasPerms()) { |
241 | return PL_FORBIDDEN; |
242 | } |
243 | |
244 | $url = parse_url($_SERVER['HTTP_REFERER']); |
245 | if ($force != 'force' && trim($url['path'], '/') != 'ax/edit') { |
246 | return PL_FORBIDDEN; |
247 | } |
248 | |
a0f05027 |
249 | $al = AXLetter::awaiting(); |
250 | if (!$al) { |
8da0d3c1 |
251 | $page->kill("Aucune lettre en attente"); |
252 | return; |
253 | } |
8da0d3c1 |
254 | if (!$al->valid()) { |
255 | $page->kill("Une erreur est survenue lors de la validation de l'envoi"); |
256 | return; |
257 | } |
258 | |
259 | $page->kill("L'envoi de l'annonce aura lieu dans l'heure qui vient."); |
260 | } |
261 | |
262 | function handler_show(&$page, $nid = 'last') |
263 | { |
264 | require_once dirname(__FILE__) . '/axletter/axletter.inc.php'; |
265 | $page->changeTpl('axletter/show.tpl'); |
266 | |
267 | $nl = new AXLetter($nid); |
268 | if (Get::has('text')) { |
269 | $nl->toText($page, S::v('prenom'), S::v('nom'), S::v('femme')); |
270 | } else { |
271 | $nl->toHtml($page, S::v('prenom'), S::v('nom'), S::v('femme')); |
272 | } |
273 | if (Post::has('send')) { |
274 | $nl->sendTo(S::v('prenom'), S::v('nom'), |
275 | S::v('bestalias'), S::v('femme'), |
276 | S::v('mail_fmt') != 'texte'); |
277 | } |
278 | } |
279 | } |
280 | |
281 | ?> |