corrade-http-templates

Subversion Repositories:
Compare Path: Rev
With Path: Rev
?path1? @ 1  →  ?path2? @ 2
/groupChat/storeGroupMessage.php
@@ -0,0 +1,111 @@
<?php
 
###########################################################################
## Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3 ##
###########################################################################
## This is a script that stores group messages to a local file that can ##
## then be read-in by other scripts and displayed. The script will only ##
## write to a file "chat.log" in the current directory and the name of ##
## the chat log ("chat.log") is hard-coded because it depends on this ##
## script's HTML counter-part that has the name hard-coded too. ##
###########################################################################
 
###########################################################################
## CONFIGURATION ##
###########################################################################
 
# Set this to the name of the group.
$GROUP = 'My Group';
# How many chat lines to store?
$CHAT_LINES = 10;
# Set this to the group password.
$PASSWORD = 'mypassword';
# Set this to Corrade's HTTP Server URL.
$URL = 'http://corrade.hostname:8080';
 
###########################################################################
## INTERNALS ##
###########################################################################
 
# Check if this is the group chat notification.
if(!isset($_POST['type']) || $_POST['type'] != "group") return;
# Check that the notification is for the configured group.
if(!isset($_POST['group']) || $_POST['group'] != $GROUP) return;
# Bail if "firstname", "lastname" or the "message" variables are blank.
if($_POST['firstname'] == ''
|| $_POST['lastname'] == ''
|| $_POST['message'] == '') return;
 
###########################################################################
## Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3 ##
###########################################################################
function atomized_put_contents($file, $data) {
$fp = fopen($file, "w+");
if (flock($fp, LOCK_EX)) {
fwrite($fp, $data);
fflush($fp);
flock($fp, LOCK_UN);
}
fclose($fp);
}
###########################################################################
## Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3 ##
###########################################################################
function atomized_get_contents($file) {
$fp = fopen($file, "r+");
$ct = '';
if (flock($fp, LOCK_SH)) {
if (filesize($file)) {
$ct = fread($fp, filesize($file));
}
flock($fp, LOCK_UN);
}
fclose($fp);
return $ct;
}
 
####
# I. Initialize a new blank array.
$data = array();
 
####
# II. If the file exists, trim it to the number of chat lines.
switch(file_exists("chat.log")) {
case TRUE:
# Open the chat log and trim the lines to the configured line size.
$data = explode(
PHP_EOL,
atomized_get_contents(
"chat.log"
)
);
while(sizeof($data) > $CHAT_LINES)
array_shift($data);
break;
}
 
####
# III. Add the line at the end including the date.
array_push(
$data,
sprintf(
"[%s:%s] %s %s : %s",
date("H"),
date("i"),
$_POST['firstname'],
$_POST['lastname'],
$_POST['message']
)
);
 
####
# IV. Now dump the array to the file.
atomized_put_contents(
"chat.log",
implode(
PHP_EOL,
$data
)
);
 
?>