-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathpost.php
More file actions
81 lines (64 loc) · 1.59 KB
/
post.php
File metadata and controls
81 lines (64 loc) · 1.59 KB
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
<?php
function getSetup($key = null) {
$arr = parse_ini_file('setup.ini');
return isset($key) ? $arr[$key] : $arr;
}
//$_POST['text'] = 'abc';
session_start();
if (!isset($_SESSION['name'])) return;
$text = isset($_POST['text']) ? $_POST['text'] : '';
if ($text === '') return;
$isApc = extension_loaded('apc');
$setup = getSetup();
$time = time();
$date = date('Y-m-d', $time);
$uniqid = uniqid();
$id = $time.'-'.$uniqid;
$tmpDir = './tmp/';
$historyDir = './history/';
$tmpFile = $tmpDir.'cache';
$historyFile = $historyDir.$date;
$fh = @fopen($historyFile, 'a');
if ($fh === false) {
mkdir($historyDir);
if (!is_dir($tmpDir)) mkdir($tmpDir);
$fh = @fopen($historyFile, 'a');
}
/* start semafore */
flock($fh, LOCK_EX);
// data
$data = array($id, $_SESSION['name'], stripslashes(htmlspecialchars($text)));
// write history
fwrite($fh, implode('&', $data)."\n");
// cache
if ($isApc) {
$cache = apc_fetch('chat');
if ($cache === false) {
$cache = array();
}
} else {
$cache = @file_get_contents($tmpFile);
if ($cache === false) {
$cache = array();
} else {
$cache = unserialize($cache);
}
}
array_unshift($cache, $data);
// delete expired cache
$expireTime = floor($time - $setup['interval']/1000 - $setup['expire_cache']);
foreach (array_reverse($cache,true) as $k => $e) {
if ($e[0] < $expireTime) {
unset($cache[$k]);
} else {
break;
}
}
if ($isApc) {
apc_store('chat', $cache);
} else {
file_put_contents($tmpFile, serialize($cache));
}
/* end semafore */
flock($fh, LOCK_UN);
fclose($fh);