Subversion Repositories SvarDOS

Rev

Rev 2000 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1223 mateusz.vi 1
<?php
2
//
1242 mateusz.vi 3
// mateuszbb - minimalist bulletin board forum. MIT license.
4
//
2003 mateusz.vi 5
// VERSION 20240909
1242 mateusz.vi 6
//
1645 mateusz.vi 7
// Copyright (C) 2021-2024 Mateusz Viste
1223 mateusz.vi 8
//
1242 mateusz.vi 9
// Permission is hereby granted, free of charge, to any person obtaining a copy
10
// of this software and associated documentation files (the “Software”), to
11
// deal in the Software without restriction, including without limitation the
12
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
13
// sell copies of the Software, and to permit persons to whom the Software is
14
// furnished to do so, subject to the following conditions:
15
//
1651 mateusz.vi 16
// The above copyright notice and this permission notice shall be included in
1242 mateusz.vi 17
// all copies or substantial portions of the Software.
18
//
19
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
25
// IN THE SOFTWARE.
1223 mateusz.vi 26
 
27
global $TRIP_SALT;
28
global $INITYEAR;
29
global $DATADIR;
30
global $NICE_URLS;
31
global $STR;
32
global $LANG;
1242 mateusz.vi 33
global $TZ;
34
global $DATE_FORMAT;
35
global $MAINPAGE_MAXTHREADS;
36
global $MAINPAGE_MAXINACT;
37
global $LOCK_DELAY;
38
global $EDIT_ALLOWED_MINUTES;
39
global $MAXDAILYPOSTS;
1223 mateusz.vi 40
 
41
include 'mateuszbb-config.php';
42
 
43
 
44
// *** TRANSLATION STRINGS *****************************************
45
 
46
$STR = array();
47
$STR['en']['opnewthread'] = 'open a new thread';
48
$STR['en']['newthread']   = 'New thread';
49
$STR['en']['latestentry'] = 'latest entry:';
50
$STR['en']['searchterm']  = 'searched term:';
51
$STR['en']['noresults']   = 'No results';
52
$STR['en']['reply']       = 'reply';
53
$STR['en']['jumptoend']   = 'jump to end';
54
$STR['en']['listthreads'] = 'list of threads';
55
$STR['en']['author']      = 'author:';
56
$STR['en']['address']     = 'address:';
57
$STR['en']['date']        = 'date:';
58
$STR['en']['nameornick']  = 'your name or nick';
59
$STR['en']['threadsubj']  = 'subject';
60
$STR['en']['yourmsg']     = 'Your message';
61
$STR['en']['cancel']      = 'cancel';
62
$STR['en']['send']        = 'send';
63
$STR['en']['archives']    = 'archives';
64
$STR['en']['backtocur']   = 'go back to current threads';
65
$STR['en']['search']      = 'search';
66
$STR['en']['password']    = 'password';
67
$STR['en']['optional']    = 'optional';
68
$STR['en']['passhelp']    = 'Providing a password here will generate a unique digital signature on your message.';
1242 mateusz.vi 69
$STR['en']['locked']      = "Thread locked due to inactivity since over {$LOCK_DELAY} days.";
1223 mateusz.vi 70
$STR['en']['captcha'][1]  = 'check the FIRST box';
71
$STR['en']['captcha'][2]  = 'check the MIDDLE box';
72
$STR['en']['captcha'][3]  = 'check the LAST box';
73
$STR['en']['captcha'][4]  = 'check the FIRST and LAST boxes';
74
$STR['en']['captcha'][5]  = 'check the TWO LAST boxes';
2003 mateusz.vi 75
$STR['en']['msglimit'] = "At most {$MAXDAILYPOSTS} messages are allowed from any single IP address for a period of 24h. This limit has been reached for your IP address. Please try again later.";
1223 mateusz.vi 76
 
1651 mateusz.vi 77
// DE translations by Robert Riebisch
78
$STR['de']['opnewthread'] = 'Neues Thema eröffnen';
79
$STR['de']['newthread']   = 'Neues Thema';
80
$STR['de']['latestentry'] = 'Neuester Eintrag:';
81
$STR['de']['searchterm']  = 'Gesuchter Begriff:';
82
$STR['de']['noresults']   = 'Keine Ergebnisse';
83
$STR['de']['reply']       = 'Antworten';
84
$STR['de']['jumptoend']   = 'Zum Ende springen';
85
$STR['de']['listthreads'] = 'Liste der Themen';
86
$STR['de']['author']      = 'Autor:';
87
$STR['de']['address']     = 'Adresse:';
88
$STR['de']['date']        = 'Zeitpunkt:';
89
$STR['de']['nameornick']  = 'Dein Name oder Spitzname';
90
$STR['de']['threadsubj']  = 'Thema';
91
$STR['de']['yourmsg']     = 'Deine Nachricht';
92
$STR['de']['cancel']      = 'Abbrechen';
93
$STR['de']['send']        = 'Senden';
94
$STR['de']['archives']    = 'Archiv';
95
$STR['de']['backtocur']   = 'Zurück zu den aktuellen Themen';
96
$STR['de']['search']      = 'Suchbegriff';
97
$STR['de']['password']    = 'Kennwort';
98
$STR['de']['optional']    = 'optional';
99
$STR['de']['passhelp']    = 'Wenn du hier ein Kennwort eingibst, wird deine Nachricht mit einer eindeutigen digitalen Signatur versehen.';
100
$STR['de']['locked']      = "Thema wegen Inaktivität seit über {$LOCK_DELAY} Tagen gesperrt.";
101
$STR['de']['captcha'][1]  = 'Kreuze das ERSTE Kästchen an';
102
$STR['de']['captcha'][2]  = 'Kreuze das MITTLERE Kästchen an';
103
$STR['de']['captcha'][3]  = 'Kreuze das LETZTE Kästchen an';
104
$STR['de']['captcha'][4]  = 'Kreuze das ERSTE und LETZTE Kästchen an';
105
$STR['de']['captcha'][5]  = 'Kreuze die beiden LETZTEN Kästchen an';
106
 
1230 mateusz.vi 107
// PL translations by Mateusz Viste
1223 mateusz.vi 108
$STR['pl']['opnewthread'] = 'stwórz nowy wątek';
109
$STR['pl']['newthread']   = 'Nowy wątek';
110
$STR['pl']['latestentry'] = 'ostatni wpis:';
111
$STR['pl']['searchterm']  = 'szukane wyrażenie:';
112
$STR['pl']['noresults']   = 'Brak wyników';
113
$STR['pl']['reply']       = 'odpowiedz';
114
$STR['pl']['jumptoend']   = 'skocz do końca';
115
$STR['pl']['listthreads'] = 'lista wątków';
116
$STR['pl']['author']      = 'autor:';
117
$STR['pl']['address']     = 'adres:';
118
$STR['pl']['date']        = 'data:';
119
$STR['pl']['nameornick']  = 'imię, nazwisko lub pseudonim';
120
$STR['pl']['threadsubj']  = 'tytuł wątku';
121
$STR['pl']['yourmsg']     = 'Twoja wiadomość';
122
$STR['pl']['cancel']      = 'anuluj';
123
$STR['pl']['send']        = 'wyślij';
124
$STR['pl']['archives']    = 'archiwum';
125
$STR['pl']['backtocur']   = 'powrót do bieżących wątków';
126
$STR['pl']['search']      = 'szukaj';
127
$STR['pl']['password']    = 'hasło';
1242 mateusz.vi 128
$STR['pl']['optional']    = 'opcjonalne';
1223 mateusz.vi 129
$STR['pl']['passhelp']    = 'Podanie hasła pozwoli wygenerować unikalny podpis elektroniczny przy twojej wiadomości.';
1242 mateusz.vi 130
$STR['pl']['locked']      = "Wątek zamknięty z powodu braku aktywności od ponad {$LOCK_DELAY} dni.";
1223 mateusz.vi 131
$STR['pl']['captcha'][1]  = 'zaznacz PIERWSZE pole';
132
$STR['pl']['captcha'][2]  = 'zaznacz ŚRODKOWE pole';
133
$STR['pl']['captcha'][3]  = 'zaznacz OSTATNIE pole';
134
$STR['pl']['captcha'][4]  = 'zaznacz PIERWSZE i OSTATNIE pole';
135
$STR['pl']['captcha'][5]  = 'zaznacz DWA OSTATNIE pola';
2003 mateusz.vi 136
$STR['pl']['msglimit'] = "Dopuszcza się maksymalnie {$MAXDAILYPOSTS} postów w ciągu 24h. Ten limit został już osiągnięty dla twojego adresu IP. Spróbuj ponownie za jakiś czas.";
1223 mateusz.vi 137
 
1230 mateusz.vi 138
// pt-BR translations courtesty of Luzemário Dantas
139
$STR['pt']['opnewthread'] = 'abrir novo tópico';
140
$STR['pt']['newthread']   = 'Novo tópico';
141
$STR['pt']['latestentry'] = 'entrada mais recente:';
142
$STR['pt']['searchterm']  = 'termo pesquisado:';
143
$STR['pt']['noresults']   = 'Sem resultados';
144
$STR['pt']['reply']       = 'responder';
145
$STR['pt']['jumptoend']   = 'ir para o final';
146
$STR['pt']['listthreads'] = 'lista de tópicos';
147
$STR['pt']['author']      = 'autor:';
148
$STR['pt']['address']     = 'endereço:';
149
$STR['pt']['date']        = 'data:';
150
$STR['pt']['nameornick']  = 'seu nome ou apelido';
151
$STR['pt']['threadsubj']  = 'assunto';
152
$STR['pt']['yourmsg']     = 'Sua mensagem';
153
$STR['pt']['cancel']      = 'cancelar';
154
$STR['pt']['send']        = 'enviar';
155
$STR['pt']['archives']    = 'arquivos';
156
$STR['pt']['backtocur']   = 'voltar ao tópico atuai';
157
$STR['pt']['search']      = 'pesquisar';
158
$STR['pt']['password']    = 'senha';
159
$STR['pt']['optional']    = 'opcional';
160
$STR['pt']['passhelp']    = 'Fornecer uma senha aqui vai gerar uma assinatura digital única na sua mensagem.';
1242 mateusz.vi 161
$STR['pt']['locked']      = "Este tópico está bloqueado porque está inativo há mais de {$LOCK_DELAY} dias."; // translated by google translate, wording might be poor
1230 mateusz.vi 162
$STR['pt']['captcha'][1]  = 'marque a PRIMEIRA caixa';
163
$STR['pt']['captcha'][2]  = 'marque a caixa do MEIO';
164
$STR['pt']['captcha'][3]  = 'marque a ÚLTIMA caixa';
165
$STR['pt']['captcha'][4]  = 'marque a PRIMEIRA e ÚLTIMA caixas';
166
$STR['pt']['captcha'][5]  = 'marque as DUAS ÚLTIMAS caixas';
167
 
1223 mateusz.vi 168
// *****************************************************************
169
 
170
 
171
function data_dluga($timestamp) {
1242 mateusz.vi 172
  global $DATE_FORMAT;
173
  return(date($DATE_FORMAT, $timestamp));
1223 mateusz.vi 174
}
175
 
176
 
1231 mateusz.vi 177
function selfurl($params = '') {
178
  global $SELFURL;
179
  $r = $SELFURL;
180
  if (!empty($params)) {
181
    if (strrchr($SELFURL, '?')) {
182
      $r .= '&';
183
    } else {
184
      $r .= '?';
185
    }
186
    $r .= $params;
187
  }
188
  return($r);
189
}
190
 
191
 
1223 mateusz.vi 192
// returns an array with the list of languages requested by the browser, in
193
// the order of preference
194
function getpreflang() {
195
  $res = array();
196
  if (! isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) return($res);
197
  $langlist = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
198
  foreach ($langlist as $lang) {
199
    $res[] = strtolower(substr($lang, 0, 2));
200
  }
201
  return(array_unique($res));
202
}
203
 
204
 
205
function mateuszbb_rss() {
206
  global $DATADIR;
1229 mateusz.vi 207
  global $RSS_TITLE;
1242 mateusz.vi 208
  global $NICE_URLS;
1229 mateusz.vi 209
 
1223 mateusz.vi 210
  $db = new SQLite3($DATADIR . 'mateuszbb.sqlite3', SQLITE3_OPEN_READONLY);
211
  if (! $db) {
212
    echo "SQL ERROR: ACCESS DENIED\n";
213
    return false;
214
  }
215
  $sqlres = $db->query('SELECT thread, msgid, author FROM rss ORDER BY msgid DESC, thread DESC LIMIT 100;');
216
  if (! $sqlres) {
217
    echo "SQL ERROR: QUERY FAILED\n";
218
    return false;
219
  }
220
 
2003 mateusz.vi 221
  header('content-type: text/xml');
1223 mateusz.vi 222
 
223
  echo '<?xml version="1.0" encoding="utf-8" ?>' . "\n";
224
  echo '<rss version="2.0">' . "\n";
225
  echo "<channel>\n";
1229 mateusz.vi 226
  echo "<title>" . htmlspecialchars($RSS_TITLE, ENT_XML1) . "</title>\n";
227
  echo "<link>" . selfurl(). "</link>\n";
1645 mateusz.vi 228
  echo "<description>" . htmlspecialchars($RSS_TITLE, ENT_XML1) . "</description>\n";
1223 mateusz.vi 229
 
230
  while ($row = $sqlres->fetchArray()) {
1999 mateusz.vi 231
    // TITLE
1223 mateusz.vi 232
    $rawtitle = file_get_contents($DATADIR . 'threads/' . $row['thread'] . '/title.txt');
233
    if (empty($rawtitle)) continue;
234
    $title = htmlspecialchars($rawtitle, ENT_XML1, 'UTF-8');
1999 mateusz.vi 235
 
236
    // AUTHOR
1223 mateusz.vi 237
    $author = htmlspecialchars($row['author'], ENT_XML1, 'UTF-8');
1999 mateusz.vi 238
 
239
    // CONTENT
240
    $rawcontent = loadmsg($row['thread'], $row['msgid'])['msg'];
2003 mateusz.vi 241
    // remove all quoted lines
242
    $rawcontent = preg_replace('/^>.*/m', '(...)', $rawcontent);
243
    // trim out whitespaces
244
    $rawcontent = trim($rawcontent);
245
    // shorten the message to 256 chars
1999 mateusz.vi 246
    $rawcontent_shorter =  mb_substr($rawcontent, 0, 256);
247
    if (strlen($rawcontent) > strlen($rawcontent_shorter)) $rawcontent_shorter .= " (...)";
2003 mateusz.vi 248
    // escape xml chars
1999 mateusz.vi 249
    $content = htmlspecialchars($rawcontent_shorter, ENT_XHTML, 'UTF-8');
2003 mateusz.vi 250
    // remove all newlines (replace by a space)
251
    $content = str_replace(array("\r\n", "\r"), "\n", $content);
252
    $content = str_replace(array("\n\n\n\n", "\n\n\n", "\n\n"), ' ', $content);
1999 mateusz.vi 253
 
254
    // LINK
1229 mateusz.vi 255
    if ($NICE_URLS) {
256
      $link = selfurl();
257
      if (substr($link, -1) !== '/') $link .= '/';
258
      $link .= "{$row['thread']}";
259
    } else {
260
      $link = selfurl('thread=' . $row['thread']);
261
    }
262
    $link .= '#' . $row['msgid'];
1999 mateusz.vi 263
 
264
    // RSS
1223 mateusz.vi 265
    echo "<item>\n";
1229 mateusz.vi 266
    echo "<title>{$author} @ '{$title}'</title>\n";
267
    echo "<link>" . htmlspecialchars($link, ENT_XML1) . "</link>\n";
1999 mateusz.vi 268
    echo "<description>{$content}</description>\n";
1223 mateusz.vi 269
    echo "<pubDate>" . date('r', $row['msgid']) . "</pubDate>\n";
1229 mateusz.vi 270
    echo "<guid>" . htmlspecialchars($link, ENT_XML1) . "</guid>\n";
1223 mateusz.vi 271
    echo "</item>\n";
272
  }
273
  $db->close();
274
 
275
  echo "</channel>\n";
276
  echo "</rss>\n";
277
  return true;
278
}
279
 
280
 
1242 mateusz.vi 281
function formularz($thread = 0, $postid = 0, $msg = '') {
1223 mateusz.vi 282
  global $STR;
283
  global $LANG;
1242 mateusz.vi 284
  global $NICE_URLS;
1223 mateusz.vi 285
 
1242 mateusz.vi 286
  if ($thread == 0) {
1223 mateusz.vi 287
    echo '<form class="minibb" method="POST" action="' . selfurl() . '#title" id="formularz">' . "\n";
288
    echo '<input type="hidden" name="action" value="createthread">' . "\n";
289
  } else {
290
    echo '<form class="minibb" method="POST" action="' . selfurl() . '" id="formularz">' . "\n";
291
    echo '<input type="hidden" name="thread" value="' . $thread . '">' . "\n";
1242 mateusz.vi 292
    if ($postid > 0) {
293
      echo '<input type="hidden" name="action" value="editpost">' . "\n";
294
      echo '<input type="hidden" name="postid" value="' . $postid . '">' . "\n";
295
    } else {
296
      echo '<input type="hidden" name="action" value="newpost">' . "\n";
297
    }
1223 mateusz.vi 298
  }
299
 
300
  echo '<div class="minibb-formfields">' . "\n";
1242 mateusz.vi 301
  echo '<div class="minibb-formlabelgroup"><p>' . $STR[$LANG]['nameornick'] . '</p><input type="text" name="username" pattern=".*[^\s].*" minlength="1" maxlength="40" autofill="username" title="' . $STR[$LANG]['nameornick'];
302
  if (!empty($msg)) echo '" value="' . htmlspecialchars($msg['author']) . '"';
303
  echo '" required></div><div class="minibb-formlabelgroup"><p>' . $STR[$LANG]['password'] . ' (<span title="' . $STR[$LANG]['passhelp'] . '" style="text-decoration-line: underline; text-decoration-style: dotted;">' . $STR[$LANG]['optional'] . '</span>)</p><input type="password" name="password" maxlength="40" autofill="current-password"></div>' . "\n";
304
  if ($thread == 0) {
1223 mateusz.vi 305
    echo '<div class="minibb-formlabelgroup" style="width: 100%;">' . "\n";
306
    echo "<p>" . $STR[$LANG]['threadsubj'] . "</p>\n";
307
    echo '<input type="text" name="title" title="' . $STR[$LANG]['threadsubj'] . '" maxlength="64" pattern=".*[^\s].*" required>' . "\n";
308
    echo "</div>\n";
309
  }
310
  echo '<textarea name="msg" placeholder="' . $STR[$LANG]['yourmsg'] . '">' . "\n";
1242 mateusz.vi 311
  if (!empty($msg)) echo htmlspecialchars($msg['msg']);
312
  echo "</textarea><br>\n";
313
  echo "</div>\n";
1223 mateusz.vi 314
  // --- CAPTCHA ---
315
  $capid = rand(1, 5);
1242 mateusz.vi 316
  echo '<div class="minibb-formcaptcha">' . $STR[$LANG]['captcha'][$capid] . ': <span class="minibb-cboxgroup"><input type="checkbox" name=c1><input type="checkbox" name=c2><input type="checkbox" name=c3></span>' . "\n";
1223 mateusz.vi 317
  echo '<input type="hidden" name="capid" value="' . $capid . '">';
318
  // ---------------
1242 mateusz.vi 319
  echo '<div class="minibb-formbtns">' . "\n";
320
  $link = selfurl();
321
  if ($postid > 0) {
322
    $link = selfurl("thread=" . $thread);
323
    if ($NICE_URLS) $link = $thread;
324
    $link .= '#' . $postid;
325
  }
326
  echo '<a href="' . $link . '">' . $STR[$LANG]['cancel'] . '</a> <input type="submit" value="' . $STR[$LANG]['send'] . '">' . "\n";
327
  echo "</div>\n";
328
  echo "</div>\n";
1223 mateusz.vi 329
  echo '</form>';
330
}
331
 
332
function wyswietl_watek_w_liscie($threadid, $tytul, $lastauthor, $lastupdate) {
333
  global $NICE_URLS;
334
  global $STR;
335
  global $LANG;
336
 
337
  echo '<a href="';
338
  if (!$NICE_URLS) {
339
    echo selfurl("thread=$threadid");
340
  } else {
341
    echo $threadid;
342
  }
343
  echo '" class="minibb-threaditem">' . "\n";
344
  echo '<h2>' . htmlspecialchars($tytul) . "</h2>\n";
345
  echo '<p>' . $STR[$LANG]['latestentry'] . ' ' . htmlspecialchars($lastauthor) . ', ' . htmlspecialchars(data_dluga($lastupdate)) . "</p>\n";
346
  echo "</a>\n";
347
}
348
 
349
function sprawdz_captcha($CAPARR) {
350
  //echo "<!-- capid={$CAPARR['capid']} c1={$CAPARR['c1']} c2={$CAPARR['c2']} c3={$CAPARR['c3']}-->\n";
351
  switch ($CAPARR['capid']) {
352
    case 1:
353
      if (($CAPARR['c1']) && (!$CAPARR['c2']) && (!$CAPARR['c3'])) return(true);
354
      break;
355
    case 2:
356
      if ((!$CAPARR['c1']) && ($CAPARR['c2']) && (!$CAPARR['c3'])) return(true);
357
      break;
358
    case 3:
359
      if ((!$CAPARR['c1']) && (!$CAPARR['c2']) && ($CAPARR['c3'])) return(true);
360
      break;
361
    case 4:
362
      if (($CAPARR['c1']) && (!$CAPARR['c2']) && ($CAPARR['c3'])) return(true);
363
      break;
364
    case 5:
365
      if ((!$CAPARR['c1']) && ($CAPARR['c2']) && ($CAPARR['c3'])) return(true);
366
      break;
367
  }
368
  return(false);
369
}
370
 
371
 
372
// zwraca akcję na podstawie globalnych POST lub GET
373
function getvar_action() {
374
  if (!empty($_POST['action'])) return $_POST['action'];
375
  if (!empty($_GET['action'])) return $_GET['action'];
376
  return('');
377
}
378
 
379
function getvar_thread() {
380
  if (!empty($_POST['thread'])) return intval($_POST['thread']);
381
  if (!empty($_GET['thread'])) return intval($_GET['thread']);
382
  return(-1);
383
}
384
 
385
function getvar_archiveyear() {
386
  if (!empty($_POST['arch'])) return intval($_POST['arch']);
387
  if (!empty($_GET['arch'])) return intval($_GET['arch']);
388
  return(-1);
389
}
390
 
391
 
392
// funkcja która zapisuje nowe wiadomości
393
function mateuszbb_preprocess() {
394
global $TRIP_SALT;
395
global $DATADIR;
396
global $NICE_URLS;
397
global $ERRSTR; // zmienna zawierająca komunikat błędu (jeśli jakiś wystąpił)
398
global $STR;
399
global $LANG;
1242 mateusz.vi 400
global $MAXDAILYPOSTS;
401
global $EDIT_ALLOWED_MINUTES;
1223 mateusz.vi 402
 
403
$action = getvar_action();
404
$thread = getvar_thread();
405
$archiveyear = getvar_archiveyear();
406
 
1242 mateusz.vi 407
// negotiate language, unless forced by configuration
408
if (empty($LANG)) {
409
  $LANG = 'en'; // preselect english as default language
410
  foreach (getpreflang() as $l) {
411
    if (!empty($STR[$l])) {
412
      $LANG = $l;
413
      break;
414
    }
1223 mateusz.vi 415
  }
1242 mateusz.vi 416
} else { // if language forced by configuration then make sure it is supported
417
  if (empty($STR[$LANG])) $LANG = 'en'; // fall back to 'en' on error
1223 mateusz.vi 418
}
419
 
420
// write access: check how many messages the user posted during last 24h
421
if (($action === 'createthread') || ($action === 'newpost')) {
1242 mateusz.vi 422
  $db = new SQLite3($DATADIR . 'mateuszbb.sqlite3');
423
  if ($db) {
424
    $db->exec('DELETE FROM ip_msg_counters24h WHERE msgid < strftime(\'%s\', \'now\') - 24*3600;');
425
    $count24h = intval($db->querySingle("SELECT count(*) FROM ip_msg_counters24h WHERE ipaddr = '{$_SERVER['REMOTE_ADDR']}'"));
426
    $db->close();
427
    if ($count24h >= $MAXDAILYPOSTS) {
2003 mateusz.vi 428
      $ERRSTR = $STR[$LANG]['msglimit'];
1242 mateusz.vi 429
      $action = '';
430
    }
431
  }
1223 mateusz.vi 432
}
433
 
1242 mateusz.vi 434
// edit post becomes newpost, it was different just to avoid 24h counters
435
if ($action === 'editpost') $action = 'newpost';
436
 
1223 mateusz.vi 437
// new thread creation (+switch to read thread)
438
if ($action === 'createthread') {
439
  // captcha check
440
  if (!sprawdz_captcha($_POST)) {
441
    echo "<p>BŁĄD: NIEPRAWIDŁOWE CAPTCHA</p>\n";
442
    goto DONE;
443
  }
444
  //
445
  $thread = time();
1242 mateusz.vi 446
  if (empty($_POST['username']) || (empty($_POST['msg'])) || (empty($_POST['title']))) {
1223 mateusz.vi 447
    echo '<p>BŁĄD: pusty nick, wiadomość lub tytuł</p>' . "\n";
448
    goto DONE;
449
  }
450
  if (!mkdir($DATADIR . 'threads/' . $thread, 0755, true)) {
451
    echo '<p>BŁĄD: nie zdołano utworzyć wątku nr ' . $thread . "</p>\n";
452
    goto DONE;
453
  }
454
  // zapisz tytuł
455
  file_put_contents($DATADIR . 'threads/' . $thread . '/title.txt', trim($_POST['title']));
456
  // ustaw co trzeba żeby zapisać wiadomość
457
  $action = 'newpost';
458
}
459
 
460
// nowy post do istniejącego wątku
1242 mateusz.vi 461
if (($action === 'newpost') && ($thread >= 0) && (!empty($_POST['msg'])) && (!empty($_POST['username']))) {
462
  // is it really about a NEW post or about EDITING an existing one?
463
  if (empty($_POST['postid'])) {
464
    $postid = time();
465
  } else { // editing an existing post
466
    $msg = loadmsg($_POST['thread'], $_POST['postid']);
467
    if (!is_art_edition_allowed($_POST['postid'], $msg)) {
468
      $action = '';
469
      $ERRSTR = "NOT ALLOWED";
470
      goto DONE;
471
    }
472
    $postid = $_POST['postid'];
473
  }
474
 
1223 mateusz.vi 475
  if (!sprawdz_captcha($_POST)) {
476
    $ERRSTR = "BŁĄD: NIEPRAWIDŁOWE CAPTCHA";
477
    goto DONE;
478
  }
479
  // nadpisz lastauthor i lastupdate
1242 mateusz.vi 480
  $lastupdate = array('lastupdate' => $postid, 'lastauthor' => trim($_POST['username']));
1223 mateusz.vi 481
  file_put_contents($DATADIR . 'threads/' . $thread . '/lastupdate', serialize($lastupdate));
482
  // oblicz tripkod, jeśli hasło zostało ustawione
483
  $tripsig = '';
1242 mateusz.vi 484
  if (!empty(trim($_POST['password']))) {
485
    $tripsig = hash('whirlpool', trim($_POST['username']) . '#' . trim($_POST['password']) . $TRIP_SALT);
1223 mateusz.vi 486
  }
1242 mateusz.vi 487
  // wygeneruj klucz do edycji postu i prześlij go przeglądarce przez ciasteczko (chyba że przeglądarka już ma klucz)
488
  if (!empty($EDIT_ALLOWED_MINUTES)) {
489
    if (!empty($_COOKIE['mateuszbbkey'])) {
490
      $artkey = $_COOKIE['mateuszbbkey'];
491
    } else {
492
      $artkey = bin2hex(random_bytes(128));
1703 mateusz.vi 493
      setcookie('mateuszbbkey', $artkey, array('secure' => false, 'httponly' => true, 'samesite' => 'Lax'));
1242 mateusz.vi 494
    }
495
  }
1223 mateusz.vi 496
  // zapisz wiadomość
1242 mateusz.vi 497
  $msg = array('author' => trim($_POST['username']), 'ip' => $_SERVER['REMOTE_ADDR'], 'trip' => $tripsig, 'msg' => trim($_POST['msg']), 'key' => password_hash($artkey, PASSWORD_DEFAULT));
1223 mateusz.vi 498
  file_put_contents($DATADIR . 'threads/' . $thread . '/' . $postid, serialize($msg));
1242 mateusz.vi 499
  // zaktualizuj metadane dot. ostatniego wpisu, ostatniego autora i ilości wpisów dla tego IP w ciągu ostatniej godziny, ale tylko dla nowych wpisów (nie dla edycji)
500
  if (empty($_POST['postid'])) {
501
    $db = new SQLite3($DATADIR . 'mateuszbb.sqlite3');
502
    if ($db) {
503
      $db->exec('CREATE TABLE IF NOT EXISTS newest (thread INTEGER PRIMARY KEY, lastupdate INTEGER NOT NULL, lastauthor TEXT NOT NULL);');
504
      $db->exec('CREATE INDEX IF NOT EXISTS lastupdated ON newest (lastupdate);');
505
      $db->exec('CREATE TABLE IF NOT EXISTS ip_msg_counters24h (threadid INTEGER NOT NULL, msgid INTEGER NOT NULL, ipaddr TEXT NOT NULL);');
506
      $db->exec('CREATE TABLE IF NOT EXISTS rss (thread INTEGER NOT NULL, msgid INTEGER NOT NULL, author TEXT NOT NULL);');
507
      $db->exec('CREATE INDEX IF NOT EXISTS rss_msgid ON rss (msgid);');
508
      $login_escaped = $db->escapeString(trim($_POST['username']));
509
      $db->exec("INSERT OR REPLACE INTO newest (thread, lastupdate, lastauthor) VALUES ({$thread}, {$postid}, '{$login_escaped}');");
510
      $db->exec("INSERT INTO rss (thread, msgid, author) VALUES ({$thread}, {$postid}, '{$login_escaped}');");
511
      $db->exec("INSERT INTO ip_msg_counters24h (threadid, msgid, ipaddr) VALUES ({$thread}, {$postid}, '{$_SERVER['REMOTE_ADDR']}');");
512
      $db->close();
513
    } else {
514
      echo "SQL ERROR WHILE WRITING STATS\n";
515
    }
1223 mateusz.vi 516
  }
517
  // przekieruj
518
  if ($NICE_URLS) {
519
    $newurl = "{$thread}#{$postid}";
520
  } else {
521
    $newurl = selfurl("thread={$thread}") . "#{$postid}";
522
  }
523
  header("Location: {$newurl}");
524
  echo "<html><head></head><body><a href=\"{$newurl}\">KLIKNIJ TUTAJ</a></body></html>\n";
525
  exit();
526
}
527
 
528
DONE:
529
 
530
}
531
 
532
 
533
function mateuszbb_tytulwatku($id) {
534
  global $DATADIR;
2000 mateusz.vi 535
  return(trim(file_get_contents($DATADIR . 'threads/' . $id . '/title.txt')));
1223 mateusz.vi 536
}
537
 
538
 
1242 mateusz.vi 539
// returns an array of last n threads with most recent activity that had activity
540
// in last maxinact days. returns false on error or empty set.
541
// the returned result, when not false, is an array of arrays, where each
542
// leaf array represents one thread
543
function mateuszbb_getactivethreads($n, $maxinact = -1) {
544
  global $DATADIR;
545
  $result = array();
546
 
2003 mateusz.vi 547
  try { /* some versions of sqlite throw an exception if db does not exist, while other versions simply return false */
548
    $db = new SQLite3($DATADIR . 'mateuszbb.sqlite3', SQLITE3_OPEN_READONLY);
549
  } catch (Exception $e) {
550
    return(false);
551
  }
1242 mateusz.vi 552
  if (! $db) return(false);
553
 
554
  $minupdatedate = 0;
555
  if ($maxinact >= 0) $minupdatedate = time() - (intval($maxinact) * 86400);
556
 
557
  $sqlquery = 'SELECT thread, lastupdate, lastauthor FROM newest WHERE lastupdate > ' . $minupdatedate . ' ORDER BY lastupdate DESC LIMIT ' . intval($n) . ';';
558
 
559
  $sqlres = $db->query($sqlquery);
560
  if (! $sqlres) {
561
    $db->close();
562
    return(false);
563
  }
564
 
565
  // kopiuj wpisy do nowej tablicy
566
  while ($row = $sqlres->fetchArray()) {
567
    $result[] = $row;
568
  }
569
 
570
  $db->close();
571
  return($result);
572
}
573
 
574
 
575
// returns true if post can be edited by current user
576
function is_art_edition_allowed($timestamp, $msg) {
577
  global $EDIT_ALLOWED_MINUTES;
578
  if ($EDIT_ALLOWED_MINUTES >= 0) {
579
    if (((time() - $timestamp) / 60) >= $EDIT_ALLOWED_MINUTES) return(false); // only posts from last x minutes can be edited
580
  }
581
  if (empty($_COOKIE['mateuszbbkey'])) return(false);
582
  if (empty($msg['key'])) return(false);
583
  return(password_verify($_COOKIE['mateuszbbkey'], $msg['key']));
584
}
585
 
586
 
587
function loadmsg($threadid, $postid) {
588
  global $DATADIR;
589
  $fname = $DATADIR . 'threads/' . $threadid . '/' . $postid;
590
  if (!file_exists($fname)) return(false);
591
  return(unserialize(file_get_contents($fname)));
592
}
593
 
594
 
1223 mateusz.vi 595
// wyświetlanie UI itd
596
function mateuszbb_start() {
597
global $TRIP_SALT;
598
global $ERRSTR;
599
global $DATADIR;
600
global $INITYEAR;
601
global $LANG;
602
global $STR;
603
global $NICE_URLS;
604
global $SEARCH_API_URL;
1242 mateusz.vi 605
global $TZ;
606
global $LOCK_DELAY;
607
global $MAINPAGE_MAXTHREADS;
608
global $MAINPAGE_MAXINACT;
1223 mateusz.vi 609
 
610
// read global variables
611
$action = getvar_action();
612
$thread = getvar_thread();
613
$archiveyear = getvar_archiveyear();
614
 
1242 mateusz.vi 615
// ustaw strefę czasową, jeśli jakaś jest skonfigurowana
616
if (!empty($TZ)) date_default_timezone_set($TZ);
617
 
1223 mateusz.vi 618
// wyświetl błąd, jeśli jakiś wystąpił w mateuszbb_preprocess()
619
if (!empty($ERRSTR)) {
620
  echo "<p class=\"minibb-errstr\">{$ERRSTR}</p>\n";
621
  $action = '';
622
  goto DONE;
623
}
624
 
2003 mateusz.vi 625
// upewnij się że sqlite3 jest dostępny
626
if (!extension_loaded('sqlite3')) {
627
  echo "<p class=\"minibb-errstr\">SQLITE3 extension not found</p>\n";
628
  $action = '';
629
  goto DONE;
630
}
631
 
1223 mateusz.vi 632
// szukanie
633
if (isset($_POST['szukaj']) && (!empty(trim($_POST['szukaj'])))) {
634
  $q = trim($_POST['szukaj']);
635
  $query = $SEARCH_API_URL . urlencode($q);
636
  echo '<p>' . $STR[$LANG]['searchterm'] . ' ' . htmlentities($q) . '</p>';
637
  $results = file_get_contents($query);
638
  $resarr = json_decode($results, true)['items'];
639
 
640
  $licznik = 0;
641
  foreach ($resarr as $r) {
642
    if (mb_substr($r['link'], -1) === '/') continue;
1702 mateusz.vi 643
    if (strlen($r['link']) <= strlen(selfurl())) continue;
1223 mateusz.vi 644
    echo '<a href=' . $r['link'] . ' class="minibb-searchresult">';
645
    echo "<div><h1>{$r['title']}</h1><p>{$r['htmlSnippet']}</p></div></a>\n";
646
    $licznik++;
647
  }
648
 
649
  if ($licznik == 0) echo "<p>" . $STR[$LANG]['noresults'] . "</p>\n";
650
 
651
  goto DONE;
652
}
653
 
1242 mateusz.vi 654
// edit post
655
if ($action === 'editpostform') {
656
  $msg = loadmsg($_POST['thread'], $_POST['post']);
657
  if (is_art_edition_allowed($_POST['post'], $msg)) {
658
    formularz(intval($_POST['thread']), intval($_POST['post']), $msg);
659
  } else {
660
    echo "<p>Link expired</p>\n";
661
  }
662
  GOTO DONE;
663
}
664
 
1223 mateusz.vi 665
// new thread form
666
if ($action === 'newthread') {
667
  echo '<h2 class="minibb-threadtitle">' . $STR[$LANG]['newthread'] . '</h2>' . "\n";
668
  formularz();
669
  goto DONE;
670
}
671
 
1242 mateusz.vi 672
// zobacz listę wątków (main page)
1223 mateusz.vi 673
if ((empty($action)) && ($thread < 0) && ($archiveyear <= 0)) {
1242 mateusz.vi 674
  // display the main page header if any is defined
675
  if (file_exists($DATADIR . 'mateuszbb-main-head.html')) {
676
    readfile($DATADIR . 'mateuszbb-main-head.html');
677
  }
678
  // list wątków
1223 mateusz.vi 679
  echo '<div class="minibb-toolbar" style="justify-content: space-between;">';
680
  echo '<form action="' . selfurl() . '" method="POST"><input type="text" name="szukaj" placeholder="' . $STR[$LANG]['search'] . '"></form>';
681
  echo '<a href="' . selfurl('action=newthread') . '#formularz">' . $STR[$LANG]['opnewthread'] . '</a>';
682
  echo "</div>\n";
1242 mateusz.vi 683
 
684
  $lista_watkow = mateuszbb_getactivethreads($MAINPAGE_MAXTHREADS, $MAINPAGE_MAXINACT);
685
  if ($lista_watkow === false) {
686
    echo "<p>NO ENTRIES FOUND</p>";
687
  } else {
688
    foreach ($lista_watkow as $row) {
689
      $title = mateuszbb_tytulwatku($row['thread']);
690
      if (empty($title)) {
691
        echo "<!-- BŁĄD: nie zdołano załadować wątku nr {$row['thread']} -->\n";
692
        continue;
1223 mateusz.vi 693
      }
1242 mateusz.vi 694
      wyswietl_watek_w_liscie($row['thread'], $title, $row['lastauthor'], $row['lastupdate']);
1223 mateusz.vi 695
    }
696
  }
697
 
698
  echo '<div style="display: flex; justify-content: space-between; font-size: 0.9em; opacity: 0.8; margin: 0.6em 0.5em 0 0.5em;">' . "\n";
699
  echo '<div>' . $STR[$LANG]['archives'] . ':';
700
  for ($y = $INITYEAR; $y <= intval(gmdate('Y')); $y++) {
701
    if ($NICE_URLS) {
702
      echo " <a href=\"{$y}\">{$y}</a>";
703
    } else {
704
      echo ' <a href="' . selfurl("arch={$y}") . '">' . $y . '</a>';
705
    }
706
  }
707
  echo "</div>\n";
1242 mateusz.vi 708
  echo '<a href="rss.php"><img style="height: 1em;" src="mateuszbb_rss.svg" alt="RSS"></a>' . "\n";
1223 mateusz.vi 709
  echo "</div>\n";
710
 
711
  // display the main page footer if any is defined
1242 mateusz.vi 712
  if (file_exists($DATADIR . 'mateuszbb-main-foot.html')) {
713
    readfile($DATADIR . 'mateuszbb-main-foot.html');
1223 mateusz.vi 714
  }
715
 
716
  goto DONE;
717
}
718
 
719
// wyświetl archiwum
720
if ($archiveyear > 0) {
721
  echo '<div class="minibb-toolbar" id="title"><a href="' . selfurl() . '">' . $STR[$LANG]['backtocur'] . '</a></div>' . "\n";
1233 mateusz.vi 722
  echo '<h2 class="minibb-threadtitle">' . $STR[$LANG]['archives'] . " {$archiveyear}</h2>\n";
1223 mateusz.vi 723
  $threads = scandir($DATADIR . 'threads/', SCANDIR_SORT_ASCENDING);
724
  foreach ($threads as $t) {
725
    if (!preg_match('/^[0-9][0-9]*$/', $t)) continue; // skip anything that is not a thread id
726
    if (intval(gmdate('Y', $t)) != $archiveyear) continue; // skip threads out of the targeted year
2000 mateusz.vi 727
    $title = mateuszbb_tytulwatku($t);
1223 mateusz.vi 728
    $link = $t;
729
    if (! $NICE_URLS) $link = selfurl("thread={$t}");
2000 mateusz.vi 730
    echo '<span style="font-family: monospace;">[' . gmdate("Y-m-d", $t) . "]</span> <a href=\"{$link}\">". htmlspecialchars($title) . "</a><br>\n";
1223 mateusz.vi 731
  }
732
  goto DONE;
733
}
734
 
735
// zobacz wątek
736
if ((empty($action)) && ($thread >= 0)) {
737
  // załaduj listę postów (i zapamiętaj ostatnią pozycję)
738
  $listapostow = scandir($DATADIR . 'threads/' . $thread . '/');
739
  // usuń pozycje które nie są żadnym msgid (np. title.txt) i zapamiętaj ostatni msgid
740
  $posty = array();
741
  foreach ($listapostow as $p) {
742
    if (!preg_match('/^[0-9][0-9]*$/', $p)) continue; // skip anything that is not a messageid
743
    $posty[] = $p;
744
    $ostatnipost = $p;
745
  }
1242 mateusz.vi 746
  // is this thread locked?
747
  $islocked = false;
748
  if (($LOCK_DELAY >= 0) && ((time() - intval($ostatnipost)) / 86400 >= $LOCK_DELAY)) $islocked = true;
1223 mateusz.vi 749
  // toolbar (ostatni wątek / odpowiedz / powrót do forum)
750
  echo '<div class="minibb-toolbar" id="title">';
1242 mateusz.vi 751
  echo '<a href="#' . $ostatnipost . '">' . $STR[$LANG]['jumptoend'] . '</a>';
752
  if (! $islocked) echo ' <a href="#formularz">' . $STR[$LANG]['reply'] . '</a>';
753
  echo ' <a href="' . selfurl() . '">' . $STR[$LANG]['listthreads'] . '</a></div>' . "\n";
1223 mateusz.vi 754
  // wyświetl tytuł wątku
755
  echo '<h2 class="minibb-threadtitle">' . htmlspecialchars(file_get_contents($DATADIR . 'threads/' . $thread . '/title.txt')) . "</h2>\n";
1242 mateusz.vi 756
  // "thread is locked"
757
  if ($islocked) echo '<p class="minibb-islockedmsg">' . $STR[$LANG]['locked'] . "</p>\n";
758
  // wyświetl listę postów
1223 mateusz.vi 759
  foreach ($posty as $p) {
1242 mateusz.vi 760
    $msg = loadmsg($thread, $p);
1223 mateusz.vi 761
    echo '<div class="minibb-post" id="' . $p . '">' . "\n";
762
    echo '<div class="minibb-postheader"><a href="#' . $p . '" style="text-decoration: inherit; color: inherit;"><div class="minibb-postauthor">' . "\n";
763
    echo $STR[$LANG]['author'] . ' ' . htmlspecialchars($msg['author']) . "<br>\n";
764
    echo $STR[$LANG]['address'] . ' ' . htmlspecialchars($msg['ip']) . "<br>\n";
765
    echo $STR[$LANG]['date'] . ' ' . htmlspecialchars(data_dluga($p)) . "</div></a>\n";
766
    if (!empty($msg['trip'])) {
767
      echo '<div class="minibb-trip">';
768
      echo chunk_split($msg['trip'], 16, "\n");
769
      echo "</div>\n";
770
    }
771
    echo "</div>\n";
772
 
773
    // symbole html
774
    $bodyprocessed = htmlspecialchars($msg['msg']);
775
 
1242 mateusz.vi 776
    // ludzie czasem dodają znaczniki [img] do obrazków, usuń je (ale tylko jeśli są na początku linii)
777
    $bodyprocessed = preg_replace('~^(\[img\])(.*)(\[/img\])~m', '$2', $bodyprocessed);
778
 
1223 mateusz.vi 779
    // dodaj podgląd pod linki do obrazków, ale tylko jeśli link jest sam w linijce
1998 mateusz.vi 780
    $bodyprocessed = preg_replace('~^(http[s]?://[^<>[:space:]]+[[:alnum:]/]\.(jpg|jpeg|png))($|[\r\n]{1,2})~m', "$1\n<img src=\"$1\">\n", $bodyprocessed);
1223 mateusz.vi 781
 
782
    // olinkuj linki
1242 mateusz.vi 783
    $bodyprocessed = preg_replace("~([^\"]|^)(http[s]?://[^<>[:space:]]+[[:alnum:]/=])~", "$1<a href=\"$2\">$2</a>", $bodyprocessed);
1223 mateusz.vi 784
 
785
    // oflaguj cytaty (linijki zaczynające się od ">")
1242 mateusz.vi 786
    $bodyprocessed = preg_replace('/^(&gt;.*)[\r]?\n/m', '<blockquote>$1</blockquote>', $bodyprocessed);
1223 mateusz.vi 787
 
1242 mateusz.vi 788
    echo '<div class="minibb-postbody">';
789
    // czy mogę edytować?
790
    if (is_art_edition_allowed($p, $msg)) {
791
      echo '<form class="editbtn" method="POST" action="' . selfurl() . '"><input type="hidden" name="action" value="editpostform"><input type="hidden" name="post" value="' . $p . '"><input type="hidden" name="thread" value="' . $thread . '"><input type="submit" value="EDIT"></form>';
792
    }
793
    echo $bodyprocessed . "</div>\n";
1223 mateusz.vi 794
    echo "</div>\n";
795
  }
1242 mateusz.vi 796
  // formularz odpowiedzi albo komunikat o zamknięciu
797
  if ($islocked) {
798
    echo '<p class="minibb-islockedmsg">' . $STR[$LANG]['locked'] . "</p>\n";
799
  } else {
800
    formularz($thread);
801
  }
1223 mateusz.vi 802
  goto DONE;
803
}
804
 
805
DONE:
806
}
807
?>