1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/minchat_ynh.git synced 2024-09-03 19:36:29 +02:00
minchat_ynh/sources/post.php

93 lines
2 KiB
PHP
Raw Normal View History

2015-04-06 17:46:57 +02:00
<?php
function v($v, $czyscHtmlIExit = false) {
if ($czyscHtmlIExit) ob_end_clean();
echo '<pre>' . print_r($v, true) . '</pre>';
if ($czyscHtmlIExit) exit;
}
function vv($v, $czyscHtmlIExit = false) {
if ($czyscHtmlIExit) ob_end_clean();
echo '<pre>';
var_dump($v);
echo '</pre>';
if ($czyscHtmlIExit) exit;
}
function vvv($var, & $result = null, $is_view = true)
{
if (is_array($var) || is_object($var)) foreach ($var as $key=> $value) vvv($value, $result[$key], false);
else $result = $var;
if ($is_view) v($result);
}
$text = isset($_POST['text']) ? $_POST['text'] : '';
2015-04-10 23:54:09 +02:00
$name = isset($_POST['name']) ? $_POST['name'] : '';
$room = isset($_POST['room']) ? $_POST['room'] : '';
$delay = isset($_POST['delay']) ? $_POST['delay'] : '';
if ($text === '' || $name === '' || $room === '' || $delay === '') return;
2015-04-06 17:46:57 +02:00
$isApc = extension_loaded('apc');
$time = time();
$date = date('Y-m-d', $time);
$uniqid = uniqid();
$id = $time.'-'.$uniqid;
$historyDir = './history/';
2015-04-10 23:54:09 +02:00
$tmpFile = $historyDir.$room.'cache2100-01-01';
$historyFile = $historyDir.$room.$date;
2015-04-06 17:46:57 +02:00
$fh = @fopen($historyFile, 'a');
if ($fh === false) {
mkdir($historyDir);
$fh = @fopen($historyFile, 'a');
}
/* start semafore */
flock($fh, LOCK_EX);
// data
2015-04-10 23:54:09 +02:00
$data = array($id, $name, stripslashes(htmlspecialchars($text)));
2015-04-06 17:46:57 +02:00
// write history
2015-04-10 23:54:09 +02:00
fwrite($fh, implode('>', $data)."\n");
2015-04-06 17:46:57 +02:00
// 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
2015-04-10 23:54:09 +02:00
$expireTime = floor($time - $delay);
2015-04-06 17:46:57 +02:00
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);