aboutsummaryrefslogtreecommitdiff
path: root/test/components/engine.php
blob: bc89d8e36d7a121097de48c68b75ab11e3f9705e (plain) (blame)
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
<?php
function ShowRecord($id) {
  for ($i=0; $i < $id; $i++) {
    $cookie = json_decode($_COOKIE['TALK'.$i], true);
    echo("<tr>");
    echo("<td>".$cookie['type']."</td>");
    echo("<td>".$cookie['textarea']."</td>");
    echo("<td>".date("H:i:s", $cookie['time'])."</td>");
    echo("</tr>");
  }
  return 0;
}

  if (isset($_COOKIE['BOT_TOKEN'])) {
    $BOT_TOKEN = $_COOKIE['BOT_TOKEN'];
  }

  if (isset($_POST['BOT_TOKEN'])) {
    unset($_COOKIE); // Clear the session
    SetCookie("BOT_TOKEN", htmlspecialchars($_POST['BOT_TOKEN']));
    $BOT_TOKEN = htmlspecialchars($_POST['BOT_TOKEN']);
  }

  if (isset($BOT_TOKEN)) {
    define('BOT_TOKEN', $BOT_TOKEN);
    $bot = new Bot(BOT_TOKEN);
    if (isset($_COOKIE['BOT_SESSION'])) {
      $session = $bot->session($_COOKIE['BOT_SESSION']);
    } else {
      $session = $bot->session();
      SetCookie("BOT_SESSION", $session);
    }
    if (!isset($session)) { ?>
      <div class="container">
        <div class="alert alert-danger" role="alert">Session is not initialized, check the settings!</div>
      </div>
    <?php }
  }

  if (isset($_POST['textarea'])) {
    $textarea = htmlspecialchars($_POST['textarea']);
    if (isset($_COOKIE['CURRENT_ID'])) {
      $current_id = (int)$_COOKIE['CURRENT_ID'];
      SetCookie('CURRENT_ID', $current_id+1);
    } else {
      $current_id = 0;
      SetCookie('CURRENT_ID', $current_id);
    }

    $cookie = array(
      'type' => 'user',
      'textarea' => $textarea,
      'time' => time()
    );
    SetCookie("TALK".$current_id, json_encode($cookie));

    $current_id = $current_id+1;
    SetCookie('CURRENT_ID', $current_id+1);
    $cookie = array(
      'type' => 'bot',
      'textarea' => $bot->say($textarea),
      'time' => time()
    );
    SetCookie("TALK".$current_id, json_encode($cookie));
  }
?>