forked from BlogoText/blogotext
/
index.php
398 lines (344 loc) · 12.8 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
<?php
# *** LICENSE ***
# This file is part of BlogoText.
# http://lehollandaisvolant.net/blogotext/
#
# 2006 Frederic Nassar.
# 2010-2015 Timo Van Neerden <timo@neerden.eu>
#
# BlogoText is free software.
# You can redistribute it under the terms of the MIT / X11 Licence.
#
# *** LICENSE ***
/*****************************************************************************
some misc routines
******************************************************************************/
// gzip compression
if (extension_loaded('zlib') and ob_get_length() > 0) {
ob_end_clean();
ob_start("ob_gzhandler");
}
else {
ob_start("ob_gzhandler");
}
if (strpos($_SERVER["SERVER_NAME"], "phd.kevin.pouget.me") !== false
&& strpos($_SERVER["REQUEST_URI"], $_SERVER["SCRIPT_NAME"]) === false
&& strpos($_SERVER["REQUEST_URI"], "?") === false )
{
header("Location: index.php?tag=work");
die();
}
if ((strpos($_SERVER["SERVER_NAME"], "autour.de.grenoble.0x972.info") !== false
|| strpos($_SERVER["SERVER_NAME"], "polynesie.0x972.info") !== false
|| strpos($_SERVER["SERVER_NAME"], "sohann.pouget.me") !== false
|| strpos($_SERVER["SERVER_NAME"], "jessica.pouget.me") !== false
|| strpos($_SERVER["SERVER_NAME"], "martinique") !== false)
&& strpos($_SERVER["REQUEST_URI"], $_SERVER["SCRIPT_NAME"]) === false
&& strpos($_SERVER["REQUEST_URI"], "?") === false )
{
header("Location: wall.php");
die();
}
if ((strpos($_SERVER["SERVER_NAME"], "martinique.0x972.info") !== false)
&& strpos($_SERVER["REQUEST_URI"], "/index.php?tag=") !== false)
{
$tagname = explode("tag=", $_SERVER["REQUEST_URI"])[1];
header("Location: wall.php#$tagname");
die();
}
$CAPOEIRA_WEBSITE = strpos($_SERVER["SERVER_NAME"], "capoeira.0x972.info") !== false;
if ($CAPOEIRA_WEBSITE
&& strpos($_SERVER["REQUEST_URI"], $_SERVER["SCRIPT_NAME"]) === false
&& strpos($_SERVER["REQUEST_URI"], "?") === false )
{
header("Location: ?alpha");
die();
}
header('Content-Type: text/html; charset=UTF-8');
$begin = microtime(TRUE);
error_reporting(-1);
$GLOBALS['BT_ROOT_PATH'] = '';
session_start();
require_once 'inc/conf.php';
if (isset($_POST['allowcookie'])) { // si cookies autorisés, conserve les champs remplis
if (isset($_POST['auteur'])) { setcookie('auteur_c', $_POST['auteur'], time() + 365*24*3600, null, null, false, true); }
if (isset($_POST['email'])) { setcookie('email_c', $_POST['email'], time() + 365*24*3600, null, null, false, true); }
if (isset($_POST['webpage'])) { setcookie('webpage_c', $_POST['webpage'], time() + 365*24*3600, null, null, false, true); }
setcookie('subscribe_c', (isset($_POST['subscribe']) and $_POST['subscribe'] == 'on' ) ? 1 : 0, time() + 365*24*3600, null, null, false, true);
setcookie('cookie_c', 1, time() + 365*24*3600, null, null, false, true);
}
if ( !file_exists($GLOBALS['dossier_config'].'/user.php') or !file_exists($GLOBALS['dossier_config'].'/prefs.php') ) {
header('Location: '.$GLOBALS['dossier_admin'].'/install.php');
}
require_once $GLOBALS['dossier_config'].'/user.php';
require_once $GLOBALS['dossier_config'].'/prefs.php';
require_once 'inc/lang.php';
require_once 'inc/them.php';
require_once 'inc/fich.php';
require_once 'inc/html.php';
require_once 'inc/form.php';
require_once 'inc/comm.php';
require_once 'inc/conv.php';
require_once 'inc/util.php';
require_once 'inc/veri.php';
require_once 'inc/jasc.php';
require_once 'inc/sqli.php';
$GLOBALS['db_handle'] = open_base($GLOBALS['db_location']);
/*****************************************************************************
some misc requests
******************************************************************************/
// anti XSS : /index.php/%22onmouseover=prompt(971741)%3E or /index.php/ redirects all on index.php
// if there is a slash after the "index.php", the file is considered as a folder, but the code inside it still executed…
if ($_SERVER['PHP_SELF'] !== $_SERVER['SCRIPT_NAME']) {
header('Location: '.$_SERVER['SCRIPT_NAME']);
}
// Random article :-)
if (isset($_GET['random'])) {
try {
// getting nb articles, gen random num, then select one article is much faster than "sql(order by rand limit 1)"
$result = $GLOBALS['db_handle']->query("SELECT count(ID) FROM articles WHERE bt_statut=1")->fetch();
if ($result[0] == 0) {
header('Location: '.$_SERVER['SCRIPT_NAME']);
}
$rand = mt_rand(0, $result[0] - 1);
$tableau = liste_elements("SELECT * FROM articles WHERE bt_statut=1 LIMIT $rand, 1", array(), 'articles');
} catch (Exception $e) {
die('Erreur rand: '.$e->getMessage());
}
header('Location: '.$tableau[0]['bt_link']);
exit;
}
// unsubscribe from comments-newsletter and redirect on main page
if ((isset($_GET['unsub']) and $_GET['unsub'] == 1) and (isset($_GET['comment']) and preg_match('#\d{14}#',($_GET['comment']))) and isset($_GET['mail']) ) {
if (isset($_GET['all'])) {
$res = unsubscribe(htmlspecialchars($_GET['comment']), $_GET['mail'], 1);
} else {
$res = unsubscribe(htmlspecialchars($_GET['comment']), $_GET['mail'], 0);
}
if ($res == TRUE) {
header('Location: '.basename($_SERVER['PHP_SELF']).'?unsubsribe=yes');
} else {
header('Location: '.basename($_SERVER['PHP_SELF']).'?unsubsribe=no');
}
}
/*****************************************************************************
Show one post : 1 blogpost (with comments)
******************************************************************************/
// Single Blog Post
if ( isset($_GET['d']) and preg_match('#^\d{4}/\d{2}/\d{2}/\d{2}/\d{2}/\d{2}#', $_GET['d']) ) {
$tab = explode('/', $_GET['d']);
$id = substr($tab['0'].$tab['1'].$tab['2'].$tab['3'].$tab['4'].$tab['5'], '0', '14');
// 'admin' connected is allowed to see draft articles, but not 'public'. Same for article posted with a date in the future.
// let 'public' see the hidden article if they point right to it
{
$query = "SELECT * FROM articles WHERE bt_id=? LIMIT 1";
$billets = liste_elements($query, array($id), 'articles');
}
if ( !empty($billets[0]) ) {
// TRAITEMENT new commentaire
$erreurs_form = array();
if (isset($_POST['_verif_envoi'], $_POST['commentaire'], $_POST['captcha'], $_POST['_token'], $_POST['auteur'], $_POST['email'], $_POST['webpage']) and ($billets[0]['bt_allow_comments'] == '1' )) {
// COMMENT POST INIT
$comment = init_post_comment($id, 'public');
if (isset($_POST['enregistrer'])) {
$erreurs_form = valider_form_commentaire($comment, 'public');
}
} else { unset($_POST['enregistrer']); }
afficher_form_commentaire($id, 'public', $erreurs_form);
if (empty($erreurs_form) and isset($_POST['enregistrer'])) {
traiter_form_commentaire($comment, 'public');
}
afficher_index($billets[0], 'post');
}
else { afficher_index(NULL, 'list'); }
}
// single link post
elseif ( isset($_GET['id']) and preg_match('#\d{14}#', $_GET['id']) ) {
$tableau = liste_elements("SELECT * FROM links WHERE bt_id=? AND bt_statut=1", array($_GET['id']), 'links');
afficher_index($tableau, 'list');
}
// List of all articles ordered by tags
elseif (isset($_GET['tags'])) {
$query = "SELECT * FROM articles WHERE bt_categories LIKE ? OR bt_categories LIKE ? OR bt_categories LIKE ? OR bt_categories LIKE ? ORDER BY bt_date DESC";
$multi_tableau = array();
foreach(list_all_tags("articles", "1") as $tag => $count) {
$search = $tag;
$tableau = liste_elements($query, array($search, $search.',%', '%, '.$search, '%, '.$search.', %'), 'articles');
$multi_tableau[$tag] = $tableau;
}
afficher_liste($multi_tableau, $alpha=0, $multi_tab=1);
}
// List of all articles
elseif (isset($_GET['liste']) or isset($_GET['alpha'])) {
$array = array();
// paramètre de tag "tag"
if (isset($_GET['tag'])) {
$sql_tag = "AND ( bt_categories LIKE ? OR bt_categories LIKE ? OR bt_categories LIKE ? OR bt_categories LIKE ? ) ";
$array[] = $_GET['tag'];
$array[] = $_GET['tag'].', %';
$array[] = '%, '.$_GET['tag'].', %';
$array[] = '%, '.$_GET['tag'];
} else {
$sql_tag = "";
}
$what = "bt_date,bt_id,bt_title,bt_type,bt_content,bt_categories,bt_abstract,bt_notes,bt_nb_comments,bt_link,bt_statut";
$query = "SELECT $what FROM articles WHERE bt_date <= ".date('YmdHis')." ";
if (empty($_SESSION['user_id'])) {
$query .= " AND bt_statut=1";
}
$query .= $sql_tag;
$query .= " ORDER BY bt_date DESC";
$tableau = liste_elements($query, $array, 'articles');
afficher_liste($tableau, isset($_GET['alpha']));
}
/*****************************************************************************
show by lists of more than one post
******************************************************************************/
else {
$annee = date('Y'); $mois = date('m'); $jour = '';
$array = array();
$query = "SELECT * FROM ";
// paramètre mode : quelle table "mode" ?
if (isset($_GET['mode'])) {
switch($_GET['mode']) {
case 'blog':
$where = 'articles';
break;
case 'comments':
$where = 'commentaires';
break;
case 'links':
$where = 'links';
break;
default:
$where = 'articles';
break;
}
} else {
$where = 'articles';
}
$query .= $where.' ';
// paramètre de recherche uniquement dans les articles publiés :
$query .= 'WHERE bt_statut=1 ';
// paramètre de date "d"
if (isset($_GET['d']) and preg_match('#^\d{4}(/\d{2})?(/\d{2})?#', $_GET['d'])) {
$date = '';
$dates = array();
$tab = explode('/', $_GET['d']);
if ( isset($tab['0']) and preg_match('#\d{4}#', ($tab['0'])) ) { $date .= $tab['0']; $annee = $tab['0']; }
if ( isset($tab['1']) and preg_match('#\d{2}#', ($tab['1'])) ) { $date .= $tab['1']; $mois = $tab['1']; }
if ( isset($tab['2']) and preg_match('#\d{2}#', ($tab['2'])) ) { $date .= $tab['2']; $jour = $tab['2']; }
if (!empty($date)) {
switch ($where) {
case 'articles':
$sql_date = "bt_date LIKE ? ";
break;
default:
$sql_date = "bt_id LIKE ? ";
break;
}
$array[] = $date.'%';
} else {
$sql_date = "";
}
}
// paramètre de recherche "q"
if (isset($_GET['q'])) {
$arr = parse_search($_GET['q']);
$array = array_merge($array, $arr);
switch ($where) {
case 'articles' :
$sql_q = implode(array_fill(0, count($arr), '( bt_content || bt_title ) LIKE ? '), 'AND ');
break;
case 'links' :
$sql_q = implode(array_fill(0, count($arr), '( bt_content || bt_title || bt_link ) LIKE ? '), 'AND ');
break;
case 'commentaires' :
$sql_q = implode(array_fill(0, count($arr), 'bt_content LIKE ? '), 'AND ');
break;
default:
$sql_q = "";
break;
}
}
// paramètre de tag "tag"
if (isset($_GET['tag'])) {
switch ($where) {
case 'articles' :
$sql_tag = "( bt_categories LIKE ? OR bt_categories LIKE ? OR bt_categories LIKE ? OR bt_categories LIKE ? ) ";
$array[] = $_GET['tag'];
$array[] = $_GET['tag'].', %';
$array[] = '%, '.$_GET['tag'].', %';
$array[] = '%, '.$_GET['tag'];
break;
case 'links' :
$sql_tag = "( bt_tags LIKE ? OR bt_tags LIKE ? OR bt_tags LIKE ? OR bt_tags LIKE ? )";
$array[] = $_GET['tag'];
$array[] = $_GET['tag'].', %';
$array[] = '%, '.$_GET['tag'].', %';
$array[] = '%, '.$_GET['tag'];
break;
default:
$sql_tag = "";
break;
}
}
// paramètre d’auteur "author" FIXME !
// paramètre ORDER BY (pas un paramètre, mais ajouté à la $query quand même)
switch ($where) {
case 'articles' :
$sql_order = "ORDER BY bt_date ";
break;
default:
$sql_order = "ORDER BY bt_id ";
break;
}
// ordre chronologique ?
if ($GLOBALS['old_first'] || isset($_GET['old_first']) && $_GET['old_first'] !== 'n' ) {
$sql_order .= "ASC ";
} else {
$sql_order .= "DESC ";
}
// paramètre de filtrage admin/public (pas un paramètre, mais ajouté quand même)
switch ($where) {
case 'articles' :
$sql_a_p = "bt_date <= ".date('YmdHis')." AND bt_statut=1 ";
break;
default:
$sql_a_p = "bt_id <= ".date('YmdHis')." AND bt_statut=1 ";
break;
}
// paramètre de page "p"
if (isset($_GET['p']) and is_numeric($_GET['p']) and $_GET['p'] >= 1) {
$sql_p = 'LIMIT '.$GLOBALS['max_bill_acceuil'] * $_GET['p'].', '.$GLOBALS['max_bill_acceuil'];
} elseif (!isset($_GET['d']) ) {
if (!$CAPOEIRA_WEBSITE) {
$sql_p = 'LIMIT '.$GLOBALS['max_bill_acceuil'];
}
} else {
$sql_p = '';
}
// Concaténation de tout ça.
$glue = 'AND ';
if (!empty($sql_date)) {
$query .= $glue.$sql_date;
}
if (!empty($sql_q)) {
$query .= $glue.$sql_q;
}
if (!empty($sql_tag)) {
$query .= $glue.$sql_tag;
}
$query .= $glue.$sql_a_p.$sql_order.$sql_p;
$tableau = liste_elements($query, $array, $where);
$GLOBALS['param_pagination'] = array('nb' => count($tableau), 'nb_par_page' => $GLOBALS['max_bill_acceuil']);
if (isset($_GET['q'])) {
$want_alpha = $CAPOEIRA_WEBSITE;
afficher_liste($tableau, $want_alpha);
} else {
afficher_index($tableau, 'list');
}
}
$end = microtime(TRUE);
echo ' <!-- Rendered in '.round(($end - $begin),6).' seconds -->';
?>