mirror of
https://github.com/YunoHost-Apps/minchat_ynh.git
synced 2024-09-03 19:36:29 +02:00
Multi room
This commit is contained in:
parent
f9958148d1
commit
504c4d18f9
5 changed files with 93 additions and 90 deletions
14
sources/conf/setup.ini
Normal file
14
sources/conf/setup.ini
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
; how often get data from serwer
|
||||||
|
interval = 2500 ; milliseconds
|
||||||
|
|
||||||
|
; extra time for cache to secure transmission delay
|
||||||
|
cache = 60000 ; milliseconds
|
||||||
|
|
||||||
|
; authorized user:room (comma separated)
|
||||||
|
; caution: names must be valid for file names !
|
||||||
|
; :room with no user name is a room opened to any user name
|
||||||
|
; user: with no room name is a user allowed to every room
|
||||||
|
; default = single unnamed room and free user names
|
||||||
|
; Example:
|
||||||
|
;auth = John:Game,Mary:Game,John:Family,Tim:Family,admin:,:Public
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
function v($v, $czyscHtmlIExit = false) {
|
function v($v, $czyscHtmlIExit = false) {
|
||||||
if ($czyscHtmlIExit) ob_end_clean();
|
if ($czyscHtmlIExit) ob_end_clean();
|
||||||
echo '<pre>' . print_r($v, true) . '</pre>';
|
echo '<pre>' . print_r($v, true) . '</pre>';
|
||||||
|
@ -20,79 +19,80 @@ function vvv($var, & $result = null, $is_view = true)
|
||||||
if ($is_view) v($result);
|
if ($is_view) v($result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getarr($arr,$key,$default) {
|
||||||
|
return isset($arr[$key]) ? $arr[$key] : $default;
|
||||||
function loginForm() {
|
|
||||||
echo'
|
|
||||||
<div id="loginform">
|
|
||||||
<form action="" method="post">
|
|
||||||
<p>Please enter your name to continue:</p>
|
|
||||||
<label for="name">Name:</label>
|
|
||||||
<input type="text" name="name" id="name"/>
|
|
||||||
<input type="submit" name="enter" id="enter" value="Enter" />
|
|
||||||
</form>
|
|
||||||
</div>
|
|
||||||
';
|
|
||||||
}
|
|
||||||
|
|
||||||
function getSetup($key = null) {
|
|
||||||
$arr = parse_ini_file('setup.ini');
|
|
||||||
return isset($key) ? $arr[$key] : $arr;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function deleteOldHistory() {
|
function deleteOldHistory() {
|
||||||
$expireHistory = getSetup('expire_history');
|
$expireDate = date('Y-m-d', strtotime("-1 day"));
|
||||||
$expireDate = date('Y-m-d', strtotime("-$expireHistory day"));
|
|
||||||
foreach (glob('./history/*') as $f) {
|
foreach (glob('./history/*') as $f) {
|
||||||
if (basename($f) < $expireDate) {
|
if (substr(basename($f),-10) < $expireDate) {
|
||||||
unlink($f);
|
unlink($f);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//-------------------------
|
//-------------------------
|
||||||
|
// init setup.ini parms
|
||||||
|
$ini = parse_ini_file('conf/setup.ini');
|
||||||
|
$interval= getarr($ini,'interval',2500);
|
||||||
|
$delay= $interval+getarr($ini,'cache',60000);
|
||||||
|
$auth= explode(',',getarr($ini,'auth',''));
|
||||||
|
|
||||||
session_start();
|
// read args
|
||||||
|
$name="";
|
||||||
if (isset($_GET['logout'])) {
|
|
||||||
session_destroy();
|
|
||||||
header("Location: ./"); //Redirect the user
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isset($_REQUEST['name'])) {
|
if (isset($_REQUEST['name'])) {
|
||||||
if ($_REQUEST['name'] != "") {
|
$name = stripslashes(htmlspecialchars($_REQUEST['name']));
|
||||||
$_SESSION['name'] = stripslashes(htmlspecialchars($_REQUEST['name']));
|
|
||||||
} else {
|
|
||||||
echo '<span class="error">Please type in a name</span>';
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
$room="";
|
||||||
if (isset($_REQUEST['room'])) {
|
if (isset($_REQUEST['room'])) {
|
||||||
$room = $_REQUEST['room'];
|
$room = stripslashes(htmlspecialchars($_REQUEST['room']));
|
||||||
} else {
|
|
||||||
$room = "";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// check args
|
||||||
|
if ($name.$room=="") {
|
||||||
|
// no args
|
||||||
|
$prompt = "Please fill in the form to continue:";
|
||||||
|
} else {
|
||||||
|
if ($name=="") {$prompt = "<em>User name missing.</em>";}
|
||||||
|
else if ($room=="") {$prompt="<em>Room missing.</em>";}
|
||||||
|
else if (in_array($name.":".$room,$auth)) {$prompt="";}
|
||||||
|
else if (in_array(":".$room,$auth)) {$prompt="";}
|
||||||
|
else if (in_array($name.":",$auth)) {$prompt="";}
|
||||||
|
else {$prompt="<em>User not authorized to this room.</em>";}
|
||||||
|
}
|
||||||
|
|
||||||
?>
|
?>
|
||||||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
<!DOCTYPE html>
|
||||||
<html xmlns="http://www.w3.org/1999/xhtml">
|
<html xmlns="http://www.w3.org/1999/xhtml">
|
||||||
<head>
|
<head>
|
||||||
<title>Chat - Customer Module</title>
|
<title>Minchat - <?php echo $room; ?> room</title>
|
||||||
<link type="text/css" rel="stylesheet" href="style.css" />
|
<link type="text/css" rel="stylesheet" href="style.css" />
|
||||||
</head>
|
</head>
|
||||||
|
|
||||||
<?php
|
<?php
|
||||||
if (!isset($_SESSION['name'])) {
|
if ($prompt!="") {
|
||||||
loginForm();
|
// Form to get args
|
||||||
deleteOldHistory();
|
echo'<div id="loginform"><p>';
|
||||||
|
echo $err;
|
||||||
|
echo $prompt;
|
||||||
|
echo '</p><form action="" method="get" class="tform"><p><label for="name">Name:</label><input type="text" name="name" id="name" value="';
|
||||||
|
echo $name;
|
||||||
|
echo '"/></p>';
|
||||||
|
echo '<p><label for="room">Room:</label><input type="text" name="room" id="room" value="';
|
||||||
|
echo $room;
|
||||||
|
echo '"/></p>';
|
||||||
|
echo '<br/><input type="submit" value="Enter" /></p></form></div>';
|
||||||
} else {
|
} else {
|
||||||
|
deleteOldHistory();
|
||||||
|
// Enter the room
|
||||||
?>
|
?>
|
||||||
<div id="wrapper">
|
<div id="wrapper">
|
||||||
<div id="menu">
|
<div id="menu">
|
||||||
<p class="welcome">Welcome to the <b><?php echo $room; ?></b> room, <b><?php echo $_SESSION['name']; ?></b></p>
|
<p class="welcome">Welcome to the <b><?php echo $room; ?></b> room, <b><?php echo $name; ?></b></p>
|
||||||
<div style="clear:both"></div>
|
<div style="clear:both"></div>
|
||||||
</div>
|
</div>
|
||||||
<div id="chatbox"><?php
|
<div id="chatbox"></div>
|
||||||
?></div>
|
|
||||||
|
|
||||||
<form name="message" action="">
|
<form name="message" action="">
|
||||||
<input name="usermsg" type="text" id="usermsg" size="63" autocomplete="off" autofocus/>
|
<input name="usermsg" type="text" id="usermsg" size="63" autocomplete="off" autofocus/>
|
||||||
<input name="submitmsg" type="submit" id="submitmsg" value="Send" />
|
<input name="submitmsg" type="submit" id="submitmsg" value="Send" />
|
||||||
|
@ -111,7 +111,7 @@ if (isset($_REQUEST['room'])) {
|
||||||
$.ajax({
|
$.ajax({
|
||||||
type: 'POST',
|
type: 'POST',
|
||||||
url: 'post.php',
|
url: 'post.php',
|
||||||
data: {text: clientmsg},
|
data: {text: clientmsg,name:'<?php echo $name; ?>',room:'<?php echo $room; ?>',delay:'<?php echo $delay; ?>'},
|
||||||
//cache: false,
|
//cache: false,
|
||||||
async: false,
|
async: false,
|
||||||
success: function(data) {
|
success: function(data) {
|
||||||
|
@ -130,7 +130,7 @@ if (isset($_REQUEST['room'])) {
|
||||||
$.ajax({
|
$.ajax({
|
||||||
type: 'POST',
|
type: 'POST',
|
||||||
url: 'server.php',
|
url: 'server.php',
|
||||||
data: {id: id},
|
data: {id: id,room:'<?php echo $room; ?>'},
|
||||||
dataType: 'json',
|
dataType: 'json',
|
||||||
//cache: false,
|
//cache: false,
|
||||||
async: false,
|
async: false,
|
||||||
|
@ -155,19 +155,10 @@ if (isset($_REQUEST['room'])) {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
loadLog();
|
loadLog();
|
||||||
setInterval(loadLog, <?php echo getSetup('interval') ?>); //Reload file every 2.5 seconds
|
setInterval(loadLog, <?php echo $interval ?>); //Reload file every $interval ms
|
||||||
|
|
||||||
//If user wants to end session
|
|
||||||
$("#exit").click(function() {
|
|
||||||
var exit = confirm("Are you sure you want to end the session?");
|
|
||||||
if (exit == true) {
|
|
||||||
window.location = 'index.php?logout=true';
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
<?php
|
<?php } ?>
|
||||||
}
|
|
||||||
?>
|
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
|
@ -20,39 +20,28 @@ function vvv($var, & $result = null, $is_view = true)
|
||||||
if ($is_view) v($result);
|
if ($is_view) v($result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
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'] : '';
|
$text = isset($_POST['text']) ? $_POST['text'] : '';
|
||||||
if ($text === '') return;
|
$name = isset($_POST['name']) ? $_POST['name'] : '';
|
||||||
|
$room = isset($_POST['room']) ? $_POST['room'] : '';
|
||||||
|
$delay = isset($_POST['delay']) ? $_POST['delay'] : '';
|
||||||
|
|
||||||
|
if ($text === '' || $name === '' || $room === '' || $delay === '') return;
|
||||||
|
|
||||||
$isApc = extension_loaded('apc');
|
$isApc = extension_loaded('apc');
|
||||||
|
|
||||||
$setup = getSetup();
|
|
||||||
$time = time();
|
$time = time();
|
||||||
$date = date('Y-m-d', $time);
|
$date = date('Y-m-d', $time);
|
||||||
$uniqid = uniqid();
|
$uniqid = uniqid();
|
||||||
$id = $time.'-'.$uniqid;
|
$id = $time.'-'.$uniqid;
|
||||||
|
|
||||||
$tmpDir = './tmp/';
|
|
||||||
$historyDir = './history/';
|
$historyDir = './history/';
|
||||||
|
|
||||||
$tmpFile = $tmpDir.'cache';
|
$tmpFile = $historyDir.$room.'cache2100-01-01';
|
||||||
$historyFile = $historyDir.$date;
|
$historyFile = $historyDir.$room.$date;
|
||||||
|
|
||||||
$fh = @fopen($historyFile, 'a');
|
$fh = @fopen($historyFile, 'a');
|
||||||
if ($fh === false) {
|
if ($fh === false) {
|
||||||
mkdir($historyDir);
|
mkdir($historyDir);
|
||||||
if (!is_dir($tmpDir)) mkdir($tmpDir);
|
|
||||||
$fh = @fopen($historyFile, 'a');
|
$fh = @fopen($historyFile, 'a');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -60,10 +49,10 @@ if ($fh === false) {
|
||||||
flock($fh, LOCK_EX);
|
flock($fh, LOCK_EX);
|
||||||
|
|
||||||
// data
|
// data
|
||||||
$data = array($id, $_SESSION['name'], stripslashes(htmlspecialchars($text)));
|
$data = array($id, $name, stripslashes(htmlspecialchars($text)));
|
||||||
|
|
||||||
// write history
|
// write history
|
||||||
fwrite($fh, implode('&', $data)."\n");
|
fwrite($fh, implode('>', $data)."\n");
|
||||||
|
|
||||||
// cache
|
// cache
|
||||||
if ($isApc) {
|
if ($isApc) {
|
||||||
|
@ -83,7 +72,7 @@ if ($isApc) {
|
||||||
array_unshift($cache, $data);
|
array_unshift($cache, $data);
|
||||||
|
|
||||||
// delete expired cache
|
// delete expired cache
|
||||||
$expireTime = floor($time - $setup['interval']/1000 - $setup['expire_cache']);
|
$expireTime = floor($time - $delay);
|
||||||
foreach (array_reverse($cache,true) as $k => $e) {
|
foreach (array_reverse($cache,true) as $k => $e) {
|
||||||
if ($e[0] < $expireTime) {
|
if ($e[0] < $expireTime) {
|
||||||
unset($cache[$k]);
|
unset($cache[$k]);
|
||||||
|
|
|
@ -20,21 +20,23 @@ function vvv($var, & $result = null, $is_view = true)
|
||||||
if ($is_view) v($result);
|
if ($is_view) v($result);
|
||||||
}
|
}
|
||||||
|
|
||||||
//$_POST['id'] = '1305177620-53c14f147c456';
|
$id = isset($_POST['id']) ? $_POST['id'] : '';
|
||||||
if (!isset($_POST['id'])) return;
|
$room = isset($_POST['room']) ? $_POST['room'] : '';
|
||||||
|
|
||||||
|
if ($id === '' || $room === '') return;
|
||||||
|
|
||||||
$isApc = extension_loaded('apc');
|
$isApc = extension_loaded('apc');
|
||||||
$id = $_POST['id'];
|
$cache = $isApc ? apc_fetch('chat') : @unserialize(file_get_contents('./history/'.$room.'cache2100-01-01'));
|
||||||
$cache = $isApc ? apc_fetch('chat') : @unserialize(file_get_contents('./tmp/cache'));
|
|
||||||
$data = array();
|
$data = array();
|
||||||
if ($id === 'undefined') {
|
if ($id === 'undefined') {
|
||||||
$id = empty($cache) ? 0 : $cache[0][0];
|
$id = empty($cache) ? 0 : $cache[0][0];
|
||||||
// first refresh : loads 50 last msg from history
|
// first refresh : loads 50 last msg from history
|
||||||
$file = './history/'.date('Y-m-d');
|
$file = './history/'.$room.date('Y-m-d');
|
||||||
if (file_exists($file)) {
|
if (file_exists($file)) {
|
||||||
$history = file($file, FILE_IGNORE_NEW_LINES);
|
$history = file($file, FILE_IGNORE_NEW_LINES);
|
||||||
$history = array_reverse($history);
|
$history = array_reverse($history);
|
||||||
foreach ($history as & $ref) {
|
foreach ($history as & $ref) {
|
||||||
$ref = explode('&', $ref);
|
$ref = explode('>', $ref);
|
||||||
}
|
}
|
||||||
$data = array_slice($history, 0, 50);
|
$data = array_slice($history, 0, 50);
|
||||||
}
|
}
|
||||||
|
@ -56,14 +58,14 @@ if ($id === 'undefined') {
|
||||||
else {
|
else {
|
||||||
$date = date('Y-m-d');
|
$date = date('Y-m-d');
|
||||||
$history = array();
|
$history = array();
|
||||||
while (($history = array_merge(file('./history/'.$date, FILE_IGNORE_NEW_LINES), $history)) && $history[0] > $id) {
|
while (($history = array_merge(file('./history/'.$room.$date, FILE_IGNORE_NEW_LINES), $history)) && $history[0] > $id) {
|
||||||
$date = date('Y-m-d', strtotime('-1 day', strtotime($date)));
|
$date = date('Y-m-d', strtotime('-1 day', strtotime($date)));
|
||||||
if (!file_exists('./history/'.$date)) break;
|
if (!file_exists('./history/'.$room.$date)) break;
|
||||||
}
|
}
|
||||||
// prepare history
|
// prepare history
|
||||||
$history = array_reverse($history);
|
$history = array_reverse($history);
|
||||||
foreach ($history as & $ref) {
|
foreach ($history as & $ref) {
|
||||||
$ref = explode('&', $ref);
|
$ref = explode('>', $ref);
|
||||||
}
|
}
|
||||||
// get data
|
// get data
|
||||||
foreach ($history as $k => $h) {
|
foreach ($history as $k => $h) {
|
||||||
|
|
|
@ -9,7 +9,8 @@ body {
|
||||||
height: 100%;
|
height: 100%;
|
||||||
font: 100% arial;
|
font: 100% arial;
|
||||||
margin: 0;
|
margin: 0;
|
||||||
padding: 0; }
|
padding: 0;
|
||||||
|
background: #d8d8d8;}
|
||||||
|
|
||||||
p, span {
|
p, span {
|
||||||
margin: 0;
|
margin: 0;
|
||||||
|
@ -69,3 +70,9 @@ a:hover { text-decoration: underline; }
|
||||||
.logout { float: right; }
|
.logout { float: right; }
|
||||||
|
|
||||||
.msgln { margin:0 0 2px 0; }
|
.msgln { margin:0 0 2px 0; }
|
||||||
|
|
||||||
|
.tform { display: table; }
|
||||||
|
.tform p { display: table-row; }
|
||||||
|
.tform label { display: table-cell; }
|
||||||
|
.tform input { display: table-cell; }
|
||||||
|
em {color:red;}
|
Loading…
Reference in a new issue