forked from zilveer/Multiplayer-PHP-Ajax-Games
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sb_chat.php
106 lines (80 loc) · 2.37 KB
/
sb_chat.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
$pre = preg_replace("/[^a-z]/", "", $_GET['pre']);
$pre = substr($pre, 0, 4);
if (file_exists($pre."_config.php"))
{
include $pre."_config.php";
} else
{
die("Error.");
}
// Date in the past
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Cache-Control: no-cache");
$gamepage=$gpres;
session_start();
require_once "config.php";
require_once "global_func.php";
global $_CONFIG;
define("MONO_ON", 1);
require_once "class/class_db_mysql.php";
$db=new database;
$db->configure($_CONFIG['hostname'],
$_CONFIG['username'],
$_CONFIG['password'],
$_CONFIG['database'],
$_CONFIG['persistent']);
$db->connect();
$c=$db->connection_id;
$_POST['chattxt']=mysql_escape($_POST['chattxt']);
$id=abs((int)$_POST['id']);
if(!$id)
{
$id=abs((int)$_GET['id']);
}
$is=$db->query("SELECT * FROM users WHERE userid={$_SESSION['userid']}");
$ir=$db->fetch_row($is);
$userid=$ir['userid'];
$goodroom = $db->num_rows($db->query("SELECT id FROM {$gpre}room WHERE (p1=$userid OR p2=$userid) AND id=$id"));
if(!$goodroom){die('Error. Bad room ID.');}
print"<table class='table' width=100%>";
$pcha=$db->query("SELECT txt,timestamp FROM {$gpre}chat WHERE {$gpre}room=$id ORDER BY timestamp DESC LIMIT 5");
while($pchat=$db->fetch_row($pcha))
{
$pchat['txt']=stripslashes($pchat['txt']);
$result = nicetime($pchat['timestamp']); // 2 days ago
print "<tr><td>$result</td><td>{$pchat['txt']}</td></tr>
";
}
print"</table>";
function nicetime($date)
{
if(empty($date)) {
return "No date provided";
}
$periods = array("second", "minute", "hour", "day", "week", "month", "year", "decade");
$lengths = array("60","60","24","7","4.35","12","10");
$now = time();
$unix_date = $date;
// check validity of date
if(empty($unix_date)) {
return "Bad date";
}
// is it future date or past date
if($now >= $unix_date) {
$difference = $now - $unix_date;
$tense = "ago";
} else {
$difference = $unix_date - $now;
$tense = "from now";
}
for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) {
$difference /= $lengths[$j];
}
$difference = round($difference);
if($difference != 1) {
$periods[$j].= "s";
}
return "$difference $periods[$j] {$tense}";
}
?>