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

80 lines
2.4 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);
}
2015-04-10 23:54:09 +02:00
$id = isset($_POST['id']) ? $_POST['id'] : '';
$room = isset($_POST['room']) ? $_POST['room'] : '';
if ($id === '' || $room === '') return;
2015-04-06 17:46:57 +02:00
$isApc = extension_loaded('apc');
2015-04-10 23:54:09 +02:00
$cache = $isApc ? apc_fetch('chat') : @unserialize(file_get_contents('./history/'.$room.'cache2100-01-01'));
2015-04-06 17:46:57 +02:00
$data = array();
if ($id === 'undefined') {
$id = empty($cache) ? 0 : $cache[0][0];
// first refresh : loads 50 last msg from history
2015-04-10 23:54:09 +02:00
$file = './history/'.$room.date('Y-m-d');
2015-04-06 17:46:57 +02:00
if (file_exists($file)) {
$history = file($file, FILE_IGNORE_NEW_LINES);
$history = array_reverse($history);
foreach ($history as & $ref) {
2015-04-10 23:54:09 +02:00
$ref = explode('>', $ref);
2015-04-06 17:46:57 +02:00
}
$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();
2015-04-10 23:54:09 +02:00
while (($history = array_merge(file('./history/'.$room.$date, FILE_IGNORE_NEW_LINES), $history)) && $history[0] > $id) {
2015-04-06 17:46:57 +02:00
$date = date('Y-m-d', strtotime('-1 day', strtotime($date)));
2015-04-10 23:54:09 +02:00
if (!file_exists('./history/'.$room.$date)) break;
2015-04-06 17:46:57 +02:00
}
// prepare history
$history = array_reverse($history);
foreach ($history as & $ref) {
2015-04-10 23:54:09 +02:00
$ref = explode('>', $ref);
2015-04-06 17:46:57 +02:00
}
// 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));