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

207 lines
7.8 KiB
PHP
Raw Normal View History

2015-04-06 17:46:57 +02:00
<?php
2015-04-10 23:54:09 +02:00
function getarr($arr,$key,$default) {
return isset($arr[$key]) ? $arr[$key] : $default;
2015-04-06 17:46:57 +02:00
}
function deleteOldHistory() {
2015-04-10 23:54:09 +02:00
$expireDate = date('Y-m-d', strtotime("-1 day"));
2015-04-06 17:46:57 +02:00
foreach (glob('./history/*') as $f) {
2015-04-10 23:54:09 +02:00
if (substr(basename($f),-10) < $expireDate) {
2015-04-06 17:46:57 +02:00
unlink($f);
}
}
}
2015-04-10 23:54:09 +02:00
// init setup.ini parms
$ini = parse_ini_file('conf/setup.ini');
$interval= getarr($ini,'interval',2500);
2015-04-12 15:35:06 +02:00
$auth= explode(',',getarr($ini,'auth','*:'));
2015-04-06 17:46:57 +02:00
2015-04-10 23:54:09 +02:00
// read args
$name="";
2015-04-06 17:46:57 +02:00
if (isset($_REQUEST['name'])) {
2015-04-10 23:54:09 +02:00
$name = stripslashes(htmlspecialchars($_REQUEST['name']));
2015-04-06 17:46:57 +02:00
}
2015-04-10 23:54:09 +02:00
$room="";
2015-04-06 17:46:57 +02:00
if (isset($_REQUEST['room'])) {
2015-04-10 23:54:09 +02:00
$room = stripslashes(htmlspecialchars($_REQUEST['room']));
}
2015-04-11 00:42:20 +02:00
// no auth = single room = no room specified
if ($auth[0]==""){$room="";}
2015-04-10 23:54:09 +02:00
// check args
if ($name.$room=="") {
// no args
$prompt = "Please fill in the form to continue:";
2015-04-06 17:46:57 +02:00
} else {
2015-04-12 15:35:06 +02:00
// user name mandatory in any case
2015-04-10 23:54:09 +02:00
if ($name=="") {$prompt = "<em>User name missing.</em>";}
2015-04-11 00:42:20 +02:00
else if ($room=="") {
2015-04-12 15:35:06 +02:00
// room not mandatory depending on setup
if (in_array("*:",$auth)||in_array($name.":",$auth)){$prompt="";}
2015-04-11 00:42:20 +02:00
else {$prompt="<em>Room missing.</em>";}
2015-04-12 15:35:06 +02:00
}
// here we have both room and user
else if (in_array($name.":".$room,$auth)||in_array("*:".$room,$auth)||in_array($name.":*",$auth)||in_array("*:*",$auth)) {$prompt="";}
2015-04-10 23:54:09 +02:00
else {$prompt="<em>User not authorized to this room.</em>";}
2015-04-06 17:46:57 +02:00
}
2015-04-10 23:54:09 +02:00
2015-04-06 17:46:57 +02:00
?>
2015-04-10 23:54:09 +02:00
<!DOCTYPE html>
2015-04-06 17:46:57 +02:00
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
2015-04-10 23:54:09 +02:00
<title>Minchat - <?php echo $room; ?> room</title>
2015-04-06 17:46:57 +02:00
<link type="text/css" rel="stylesheet" href="style.css" />
</head>
<?php
2015-04-10 23:54:09 +02:00
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>';
2015-04-11 00:42:20 +02:00
if ($auth[0]!==""){
echo '<p><label for="room">Room:</label><input type="text" name="room" id="room" value="';
echo $room;
echo '"/></p>';
}
2015-04-10 23:54:09 +02:00
echo '<br/><input type="submit" value="Enter" /></p></form></div>';
2015-04-06 17:46:57 +02:00
} else {
2015-04-10 23:54:09 +02:00
deleteOldHistory();
// Enter the room
2015-04-06 17:46:57 +02:00
?>
<div id="wrapper">
<div id="menu">
2015-04-20 23:19:57 +02:00
<div style="float:right">Alert on new message:
<select id="whenal">
<option value="alno">never</option>
<option value="alwnf">when not in focus</option>
<option value="alyes">always</option>
</select>
</div>
<p class="welcome">Welcome to the <b><?php echo $room; ?></b> room, <b><?php echo $name; ?></b></p>
2015-04-06 17:46:57 +02:00
</div>
2015-04-17 09:09:51 +02:00
<div id="content">
2015-04-10 23:54:09 +02:00
<div id="chatbox"></div>
2015-04-17 09:09:51 +02:00
</div>
2015-04-06 17:46:57 +02:00
<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" />
</form>
</div>
<script type="text/javascript" src="lib/jquery-2.1.3.min.js"></script>
<script type="text/javascript">
2015-04-12 10:06:30 +02:00
// add link or img
2015-04-11 22:22:02 +02:00
function replacer(match, p1,p2,p3, offset, string){
2015-04-12 10:06:30 +02:00
if (p1.charAt(0)=='!') {
return(' <img src="' + p1.substr(1) + '" />');
2015-04-11 22:22:02 +02:00
} else {
return ' <a href="' + p1 + '">' + p1 + '</a>';
}
}
2015-04-12 10:06:30 +02:00
// scans URLS
2015-04-11 22:22:02 +02:00
function lienurl(s){
2015-04-12 10:06:30 +02:00
return s.replace(/\s(!?https?:\/\/([-\w\.]+[-\w]+(:\d+)?(\/([\w/_\.#-]*(\?\S+)?[^\.\s])?)?))/g,replacer);
2015-04-11 22:22:02 +02:00
}
2015-04-06 17:46:57 +02:00
$(document).ready(function() {
2015-04-20 23:19:57 +02:00
var doalert=false; // pas d'alerte au chargement init
2015-04-11 19:32:50 +02:00
var pos = 0;
var lastdate = 0;
2015-04-17 09:09:51 +02:00
var lastday='';
2015-04-11 22:22:02 +02:00
var oldscrollHeight = $("#chatbox")[0].scrollHeight;
2015-04-20 23:19:57 +02:00
var whenal=localStorage.getItem('whenal');
if (whenal===null) whenal="alno";
$("#whenal").val(whenal);
$("#whenal").change(function(){
whenal=$("#whenal").val();
localStorage.setItem('whenal',whenal);
});
2015-04-11 19:32:50 +02:00
2015-04-06 17:46:57 +02:00
$("#submitmsg").click(function() {
var clientmsg = $("#usermsg").val();
$("#usermsg").val('');
$("#usermsg").focus();
$.ajax({
type: 'POST',
url: 'post.php',
2015-04-11 19:32:50 +02:00
data: {text: clientmsg,name:'<?php echo $name; ?>',room:'<?php echo $room; ?>'},
2015-04-06 17:46:57 +02:00
async: false,
success: function(data) {
2015-04-20 23:19:57 +02:00
doalert=false;
loadLog(); // pas d'alerte si propre message
2015-04-06 17:46:57 +02:00
},
error: function(request, status, error) {
$("#usermsg").val(clientmsg);
},
});
return false;
});
function loadLog() {
$.ajax({
type: 'POST',
url: 'server.php',
2015-04-11 19:32:50 +02:00
data: {room:'<?php echo $room; ?>', pos: pos, lastdate: lastdate},
2015-04-06 17:46:57 +02:00
dataType: 'json',
async: false,
success: function(data) {
2015-04-11 21:34:49 +02:00
var html='';
2015-04-06 17:46:57 +02:00
var date;
2015-04-11 21:34:49 +02:00
var heure='';
2015-04-17 09:09:51 +02:00
var day='';
2015-04-11 19:32:50 +02:00
for (var k in data.data) {
lastdate = data.data[k][0];
date = new Date(parseInt(lastdate)*1000);
2015-04-11 21:34:49 +02:00
heure = date.toLocaleTimeString().substr(0,5);
2015-04-06 17:46:57 +02:00
html = html
2015-04-11 21:34:49 +02:00
+"("+heure+") <b>"
2015-04-11 19:32:50 +02:00
+data.data[k][1]+"</b>: "+data.data[k][2]+"<br>";
2015-04-06 17:46:57 +02:00
}
2015-04-11 22:22:02 +02:00
html=lienurl(html);
2015-04-17 09:09:51 +02:00
if (html!=""){
if (pos==0){
day = date.toLocaleDateString();
if (day!=lastday){
$("#chatbox").append('<b>----- '+day+' -----</b><br>');
if (data.pos==0) {
$("#chatbox").append('<span id="nomsg">No message yet. Enter yours!<br></span>');
}
lastday=day;
}
if (data.pos>0) {
$("#nomsg").remove();
} else {
html="";
}
}
2015-04-20 23:19:57 +02:00
$("#chatbox").append(html);
if (doalert &&(whenal=="alyes" || ((whenal!=="alno") && !document.hasFocus()))){
alert('New message!');
}
doalert=true; // par défaut, alerte pour les autres cas
2015-04-17 09:09:51 +02:00
}
2015-04-06 17:46:57 +02:00
var newscrollHeight = $("#chatbox")[0].scrollHeight;
if (newscrollHeight > oldscrollHeight) {
2015-04-17 09:09:51 +02:00
$("#chatbox").scrollTop(newscrollHeight);
2015-04-06 17:46:57 +02:00
}
2015-04-17 09:09:51 +02:00
pos = data.pos;
2015-04-06 17:46:57 +02:00
},
});
}
loadLog();
2015-04-10 23:54:09 +02:00
setInterval(loadLog, <?php echo $interval ?>); //Reload file every $interval ms
2015-04-06 17:46:57 +02:00
});
</script>
2015-04-10 23:54:09 +02:00
<?php } ?>
2015-04-06 17:46:57 +02:00
</body>
</html>