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/server.php

78 lines
2.3 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);
}
//$_POST['id'] = '1305177620-53c14f147c456';
if (!isset($_POST['id'])) return;
$isApc = extension_loaded('apc');
$id = $_POST['id'];
$cache = $isApc ? apc_fetch('chat') : @unserialize(file_get_contents('./tmp/cache'));
$data = array();
if ($id === 'undefined') {
$id = empty($cache) ? 0 : $cache[0][0];
// first refresh : loads 50 last msg from history
$file = './history/'.date('Y-m-d');
if (file_exists($file)) {
$history = file($file, FILE_IGNORE_NEW_LINES);
$history = array_reverse($history);
foreach ($history as & $ref) {
$ref = explode('&', $ref);
}
$data = array_slice($history, 0, 50);
}
} elseif ($id === '0') {
if (!empty($cache)) {
$id = $cache[0][0];
$data = $cache;
}
} else {
$end = end($cache);
// read data from cache
if ($id >= $end[0]) {
foreach ($cache as $k => $c) {
if ($c[0] === $id) break;
}
$data = array_slice($cache, 0, $k);
}
// read data from history (any problem witch Internet and are delays)
else {
$date = date('Y-m-d');
$history = array();
while (($history = array_merge(file('./history/'.$date, FILE_IGNORE_NEW_LINES), $history)) && $history[0] > $id) {
$date = date('Y-m-d', strtotime('-1 day', strtotime($date)));
if (!file_exists('./history/'.$date)) break;
}
// prepare history
$history = array_reverse($history);
foreach ($history as & $ref) {
$ref = explode('&', $ref);
}
// get data
foreach ($history as $k => $h) {
if ($h[0] === $id) break;
}
$data = array_slice($history, 0, $k);
}
$id = $cache[0][0];
}
echo json_encode(array('id' => $id, 'data' => $data));