-
Notifications
You must be signed in to change notification settings - Fork 0
/
chat.class.php
106 lines (104 loc) · 2.82 KB
/
chat.class.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
<?php
require_once('error_handler.php');//Include this to handle any error at server
class Chat extends SQLite3
{
function __construct()
{
$this->open('chatting.db');
}
//Create table in database if not exist
public function CreateTable()
{
$CT =<<<EOF
CREATE TABLE IF NOT EXISTS CHAT
(chat_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
posted_on DATETIME NOT NULL,
user_name VARCHAR(50) NOT NULL,
message TEXT NOT NULL);
EOF;
$this->exec($CT);
}
//Insert chat in DB
public function postMessage($name, $message)
{
$name = $name;
$message = $message;
$query =<<<EOF
INSERT INTO CHAT(user_name, message, posted_on)
VALUES ('$name', '$message',DateTime('now'));
EOF;
$result = $this->exec($query);
}
//Fetches new messages from server
public function retrieveNewMessages($id=0)
{
$id = $id;
if($id>0)
{
$sql =<<<EOF
SELECT chat_id, user_name, message FROM chat WHERE chat_id > '$id' ORDER BY chat_id ASC;
EOF;
$result = $this->query($sql);
}
else
{
$sql =<<<EOF
SELECT chat_id, user_name, message FROM (SELECT chat_id, user_name, message FROM CHAT ORDER BY chat_id DESC) AS Last50 ORDER BY chat_id ASC;
EOF;
}
$result = $this->query($sql);
$response = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>';
$response .= '<response>';
$response .= $this->isDatabaseCleared($id);
if($result)
{
while ($row = $result->fetchArray(SQLITE3_ASSOC))
{
$id = $row['chat_id'];
$userName =$row['user_name'];
$message = $row['message'];
$response .= '<id>' . $id . '</id>' .
'<name><![CDATA[' . $userName . ']]></name>' .
'<message><![CDATA[' . $message . ']]></message>';
}
}
$response = $response . '</response>';
return $response;
}
//fetches the list of users participaed in chat
public function getUsers(){
$sql =<<<EOF
SELECT DISTINCT user_name FROM CHAT ORDER BY chat_id DESC;
EOF;
$result = $this->query($sql);
//$response .= $this->isDatabaseCleared($id);
$userName = array();
if($result)
{
$outp = "[";
while ($row = $result->fetchArray(SQLITE3_ASSOC))
{
if ($outp != "[") {$outp .= ",";}
$outp .= '{"Name":"'.$row["user_name"].'"}';
}
$outp .="]";
}
return $outp; //Returning output as array
}
//Check for database is cleared or not
private function isDatabaseCleared($id)
{
if($id>0)
{
$check_clear =<<<EOF
SELECT count(*) old FROM chat where chat_id<=' . $id';
EOF;
$result = $this->query($check_clear);
$row = $result->fetchArray(SQLITE3_ASSOC);
if($row['old']==0)
return '<clear>true</clear>';
}
return '<clear>false</clear>';
}
}
?>