1
0
Fork 0
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:
Chtixof 2015-04-10 23:54:09 +02:00
parent f9958148d1
commit 504c4d18f9
5 changed files with 93 additions and 90 deletions

14
sources/conf/setup.ini Normal file
View 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

View file

@ -1,5 +1,4 @@
<?php
function v($v, $czyscHtmlIExit = false) {
if ($czyscHtmlIExit) ob_end_clean();
echo '<pre>' . print_r($v, true) . '</pre>';
@ -20,79 +19,80 @@ function vvv($var, & $result = null, $is_view = true)
if ($is_view) v($result);
}
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 getarr($arr,$key,$default) {
return isset($arr[$key]) ? $arr[$key] : $default;
}
function deleteOldHistory() {
$expireHistory = getSetup('expire_history');
$expireDate = date('Y-m-d', strtotime("-$expireHistory day"));
$expireDate = date('Y-m-d', strtotime("-1 day"));
foreach (glob('./history/*') as $f) {
if (basename($f) < $expireDate) {
if (substr(basename($f),-10) < $expireDate) {
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();
if (isset($_GET['logout'])) {
session_destroy();
header("Location: ./"); //Redirect the user
}
// read args
$name="";
if (isset($_REQUEST['name'])) {
if ($_REQUEST['name'] != "") {
$_SESSION['name'] = stripslashes(htmlspecialchars($_REQUEST['name']));
} else {
echo '<span class="error">Please type in a name</span>';
}
$name = stripslashes(htmlspecialchars($_REQUEST['name']));
}
$room="";
if (isset($_REQUEST['room'])) {
$room = $_REQUEST['room'];
} else {
$room = "";
$room = stripslashes(htmlspecialchars($_REQUEST['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">
<head>
<title>Chat - Customer Module</title>
<title>Minchat - <?php echo $room; ?> room</title>
<link type="text/css" rel="stylesheet" href="style.css" />
</head>
<?php
if (!isset($_SESSION['name'])) {
loginForm();
deleteOldHistory();
if ($prompt!="") {
// Form to get args
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 {
deleteOldHistory();
// Enter the room
?>
<div id="wrapper">
<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>
<div id="chatbox"><?php
?></div>
<div id="chatbox"></div>
<form name="message" action="">
<input name="usermsg" type="text" id="usermsg" size="63" autocomplete="off" autofocus/>
<input name="submitmsg" type="submit" id="submitmsg" value="Send" />
@ -111,7 +111,7 @@ if (isset($_REQUEST['room'])) {
$.ajax({
type: 'POST',
url: 'post.php',
data: {text: clientmsg},
data: {text: clientmsg,name:'<?php echo $name; ?>',room:'<?php echo $room; ?>',delay:'<?php echo $delay; ?>'},
//cache: false,
async: false,
success: function(data) {
@ -130,7 +130,7 @@ if (isset($_REQUEST['room'])) {
$.ajax({
type: 'POST',
url: 'server.php',
data: {id: id},
data: {id: id,room:'<?php echo $room; ?>'},
dataType: 'json',
//cache: false,
async: false,
@ -155,19 +155,10 @@ if (isset($_REQUEST['room'])) {
});
}
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>
<?php
}
?>
<?php } ?>
</body>
</html>

View file

@ -20,39 +20,28 @@ function vvv($var, & $result = null, $is_view = true)
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'] : '';
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');
$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;
$tmpFile = $historyDir.$room.'cache2100-01-01';
$historyFile = $historyDir.$room.$date;
$fh = @fopen($historyFile, 'a');
if ($fh === false) {
mkdir($historyDir);
if (!is_dir($tmpDir)) mkdir($tmpDir);
$fh = @fopen($historyFile, 'a');
}
@ -60,10 +49,10 @@ if ($fh === false) {
flock($fh, LOCK_EX);
// data
$data = array($id, $_SESSION['name'], stripslashes(htmlspecialchars($text)));
$data = array($id, $name, stripslashes(htmlspecialchars($text)));
// write history
fwrite($fh, implode('&', $data)."\n");
fwrite($fh, implode('>', $data)."\n");
// cache
if ($isApc) {
@ -83,7 +72,7 @@ if ($isApc) {
array_unshift($cache, $data);
// delete expired cache
$expireTime = floor($time - $setup['interval']/1000 - $setup['expire_cache']);
$expireTime = floor($time - $delay);
foreach (array_reverse($cache,true) as $k => $e) {
if ($e[0] < $expireTime) {
unset($cache[$k]);

View file

@ -20,21 +20,23 @@ function vvv($var, & $result = null, $is_view = true)
if ($is_view) v($result);
}
//$_POST['id'] = '1305177620-53c14f147c456';
if (!isset($_POST['id'])) return;
$id = isset($_POST['id']) ? $_POST['id'] : '';
$room = isset($_POST['room']) ? $_POST['room'] : '';
if ($id === '' || $room === '') return;
$isApc = extension_loaded('apc');
$id = $_POST['id'];
$cache = $isApc ? apc_fetch('chat') : @unserialize(file_get_contents('./tmp/cache'));
$cache = $isApc ? apc_fetch('chat') : @unserialize(file_get_contents('./history/'.$room.'cache2100-01-01'));
$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');
$file = './history/'.$room.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);
$ref = explode('>', $ref);
}
$data = array_slice($history, 0, 50);
}
@ -56,14 +58,14 @@ if ($id === 'undefined') {
else {
$date = date('Y-m-d');
$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)));
if (!file_exists('./history/'.$date)) break;
if (!file_exists('./history/'.$room.$date)) break;
}
// prepare history
$history = array_reverse($history);
foreach ($history as & $ref) {
$ref = explode('&', $ref);
$ref = explode('>', $ref);
}
// get data
foreach ($history as $k => $h) {

View file

@ -9,7 +9,8 @@ body {
height: 100%;
font: 100% arial;
margin: 0;
padding: 0; }
padding: 0;
background: #d8d8d8;}
p, span {
margin: 0;
@ -69,3 +70,9 @@ a:hover { text-decoration: underline; }
.logout { float: right; }
.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;}