diff --git a/sources/conf/setup.ini b/sources/conf/setup.ini new file mode 100644 index 0000000..a27ae8a --- /dev/null +++ b/sources/conf/setup.ini @@ -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 + diff --git a/sources/index.php b/sources/index.php index 49749de..ee38b6c 100644 --- a/sources/index.php +++ b/sources/index.php @@ -1,5 +1,4 @@ ' . print_r($v, true) . ''; @@ -20,79 +19,80 @@ function vvv($var, & $result = null, $is_view = true) if ($is_view) v($result); } - - -function loginForm() { - echo' -
-
-

Please enter your name to continue:

- - - -
-
- '; -} - -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 'Please type in a name'; - } + $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 = "User name missing.";} + else if ($room=="") {$prompt="Room missing.";} + else if (in_array($name.":".$room,$auth)) {$prompt="";} + else if (in_array(":".$room,$auth)) {$prompt="";} + else if (in_array($name.":",$auth)) {$prompt="";} + else {$prompt="User not authorized to this room.";} +} + ?> - + - Chat - Customer Module + Minchat - <?php echo $room; ?> room

'; + echo $err; + echo $prompt; + echo '

'; + echo '

'; + echo '

'; } else { + deleteOldHistory(); +// Enter the room ?>
-
- +
@@ -111,7 +111,7 @@ if (isset($_REQUEST['room'])) { $.ajax({ type: 'POST', url: 'post.php', - data: {text: clientmsg}, + data: {text: clientmsg,name:'',room:'',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:''}, dataType: 'json', //cache: false, async: false, @@ -155,19 +155,10 @@ if (isset($_REQUEST['room'])) { }); } loadLog(); - setInterval(loadLog, ); //Reload file every 2.5 seconds + setInterval(loadLog, ); //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'; - } - }); }); - + diff --git a/sources/post.php b/sources/post.php index ca5bd6f..3a397c9 100644 --- a/sources/post.php +++ b/sources/post.php @@ -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]); diff --git a/sources/server.php b/sources/server.php index 794522d..1855d58 100644 --- a/sources/server.php +++ b/sources/server.php @@ -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) { diff --git a/sources/style.css b/sources/style.css index 79a8cd3..5790d10 100644 --- a/sources/style.css +++ b/sources/style.css @@ -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;} \ No newline at end of file