aboutsummaryrefslogtreecommitdiff
path: root/guestbook.php
blob: 33ed46e8a54f0401dcf193b49eaed5671db4cf4e (plain)
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
<?php

// Guestbook for Gopher
session_start();
$db = new PDO("sqlite:db/guestbook.db");
$db -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stm="CREATE TABLE IF NOT EXISTS log (
    id integer primary key autoincrement,
	sessionid TEXT,
   	msg TEXT,
	date TEXT
);";
$db -> exec($stm);

$i="";

if (getenv("SEARCHREQUEST") !== false) {
    $msg=htmlentities(getenv("SEARCHREQUEST")); 

    $sessionid = $_SERVER["SESSION_ID"];
    date_default_timezone_set('Europe/Paris');
    $d = new DateTime();
    $date = $d->format('r');

    if ((isset($msg)) && (!empty($msg))) {
    	$i="i";
        try {
            $stm = $db->prepare("INSERT INTO log (sessionid,msg,date) VALUES (:sessionid,:msg,:date)");
            $stm -> execute([ 'sessionid' => $sessionid, 'msg' => $msg, 'date' => $date ]);
        } catch(Exception $e) {
            $db->rollback();
            throw $e;
        }
    }
}

// print out guestbook comments

$stm = $db->query("SELECT `sessionid`, `msg`,`date` FROM `log` ORDER BY id DESC");

while ($row = $stm->fetch()) {
  $sessionid=$row['sessionid'];
  $date=$row['date'];
  $msg=$row['msg'];
  
  if (!empty($msg)) {

  	// prepend "i" to each line if gophermap
  	echo $i."+".str_repeat("-", 65)."+\n";
  	echo $i."[".$sessionid."]  ".date("H:i, l jS F Y", strtotime($date))."\n";
  	echo $i."+".str_repeat("-", 65)."+\n";
  
  	// get a clean string for passing to the shell 
  	$msg = escapeshellarg(html_entity_decode($msg));
  	// format string newline on words
  	$msg = shell_exec("echo $msg | par w67");

  	// split lines to array
  	$ma = explode(PHP_EOL, $msg);

  	foreach ($ma as $msgline) {
    	$msgline = stripslashes(trim($msgline,'\'"'));
    	echo $i.$msgline."\n";
  	}
  }
}

?>