Moderators: OldWolf, re*s.t.a.r.s.*2
require_once dirname(__FILE__)."/src/phpfreechat.class.php";
$params = array();
$params["title"] = "Live Chat";
$params["nick"] = $_COOKIE['u'];
$params["serverid"] = md5(__FILE__); // calculate a unique id for this chat
$params["data_private_path"] = "/dev/shm/conquerchat";
$params["theme"] = "phpbb2";
$params['channels'] = array('Callouts','Social');
$params["frozen_nick"] = true;
$params["max_nick_len"] = 16;
$params["refresh_delay"] = 3000; // 2000ms = 2s
$params["height"] = "350px";
$params['admins'] = array('lackattack'=>'','AndyDufresne'=>'','wicked'=>'','moz976'=>'','AK_iceman'=>'','mrdexter'=>'','Fireside Poet'=>'','king achilles'=>'');
$params["time_offset"] = (int)$_COOKIE['o'];
$chat = new phpFreeChat( $params );
ch_Callouts 20/07/2007 07:02:50 qeee1 hey kidtwist
ch_Social 20/07/2007 07:02:53 lackattack testing
ch_Callouts 20/07/2007 07:03:07 KidTwist thought you were quitting?
/**
* Returns the last message id
* Notice: the default file container just returns the messages.index file content
* @param $chan if NULL then read if from the server, otherwise read if from the given channel
* @return int is the last posted message id
*/
function getLastId($chan)
{
if ($chan == NULL) $chan = 'SERVER';
$lastmsgid = $this->getMeta("channelid-to-msgid", $this->encode($chan), 'lastmsgid', true);
if (count($lastmsgid["value"]) == 0)
$lastmsgid = 0;
else
$lastmsgid = $lastmsgid["value"][0];
return $lastmsgid;
}
/**
* Return a unique id. Each time this function is called, the last id is incremented.
* used internaly
* @private
*/
function _requestMsgId($chan)
{
if ($chan == NULL) $chan = 'SERVER';
$lastmsgid = $this->getLastId($chan);
$lastmsgid++;
$this->setMeta("channelid-to-msgid", $this->encode($chan), 'lastmsgid', $lastmsgid);
return $lastmsgid;
}
function setMeta($group, $subgroup, $leaf, $leafvalue = NULL)
{
$c =& pfcGlobalConfig::Instance();
if ($c->debug)
file_put_contents("/tmp/debug", "nsetMeta(".$group.",".$subgroup.",".$leaf.",".$leafvalue.")", FILE_APPEND);
// create directories
$dir_base = $c->container_cfg_server_dir;
$dir = $dir_base.'/'.$group.'/'.$subgroup;
if (!is_dir($dir)) mkdir_r($dir);
// create or replace metadata file
$leaffilename = $dir."/".$leaf;
$leafexists = file_exists($leaffilename);
if ($leafvalue == NULL)
{
if (file_exists($leaffilename) &&
filesize($leaffilename)>0) unlink($leaffilename);
touch($leaffilename);
}
else
{
flock_put_contents($leaffilename, $leafvalue);
}
// store the value in the memory cache
//@todo
// $this->_meta[$enc_type][$enc_subtype][$enc_key] = $value;
if ($leafexists)
return 1; // value overwritten
else
return 0; // value created
}
function getMeta($group, $subgroup = null, $leaf = null, $withleafvalue = false)
{
$c =& pfcGlobalConfig::Instance();
if ($c->debug)
file_put_contents("/tmp/debug", "ngetMeta(".$group.",".$subgroup.",".$leaf.",".$withleafvalue.")", FILE_APPEND);
// read data from metadata file
$ret = array();
$ret["timestamp"] = array();
$ret["value"] = array();
$dir_base = $c->container_cfg_server_dir;
$dir = $dir_base.'/'.$group;
if ($subgroup == NULL)
{
if (is_dir($dir))
{
$dh = opendir($dir);
while (false !== ($file = readdir($dh)))
{
if ($file == "." || $file == "..") continue; // skip . and .. generic files
$ret["timestamp"][] = filemtime($dir.'/'.$file);
$ret["value"][] = $file;
}
closedir($dh);
}
return $ret;
}
$dir .= '/'.$subgroup;
if ($leaf == NULL)
{
if (is_dir($dir))
{
$dh = opendir($dir);
while (false !== ($file = readdir($dh)))
{
if ($file == "." || $file == "..") continue; // skip . and .. generic files
$ret["timestamp"][] = filemtime($dir.'/'.$file);
$ret["value"][] = $file;
}
closedir($dh);
}
return $ret;
}
$leaffilename = $dir."/".$leaf;
if (!file_exists($leaffilename)) return $ret;
if ($withleafvalue)
$ret["value"][] = flock_get_contents($leaffilename);
else
$ret["value"][] = NULL;
$ret["timestamp"][] = filemtime($leaffilename);
return $ret;
}
function _requestMsgId($chan)
{
if ($chan == NULL) $chan = 'SERVER';
$lastmsgid = time();
$this->setMeta("channelid-to-msgid", $this->encode($chan), 'lastmsgid', $lastmsgid);
return $lastmsgid;
}
/**
* Almost the Same as file_get_contents except that it uses flock() to lock the file
*/
function flock_get_contents($filename, $mode = "rb")
{
$data = '';
if (!file_exists($filename)) return $data;
$size = filesize($filename);
if ($size == 0) return $data;
$fp = fopen( $filename, $mode );
if( $fp && flock( $fp, LOCK_SH ) )
{
$data = fread( $fp, $size );
// flock($fp, LOCK_UN); // will be done by fclose
}
fclose( $fp );
return $data;
}
/**
* Almost the Same as file_put_contents except that it uses flock() to lock the file
*/
function flock_put_contents($filename, $data, $mode = "wb")
{
$fp = fopen( $filename, $mode );
if( $fp )
{
if ( flock( $fp, LOCK_EX ) )
{
fwrite( $fp, $data );
// flock($fp, LOCK_UN); // will be done by fclose
}
fclose( $fp );
}
}
kerphi wrote:Anyway, I think the cleaner idea is to use file locking to avoid possible file corruptions.
Here is the code I use to read and write in files ... it should have bug but where ?
/**
* Almost the Same as file_get_contents except that it uses flock() to lock the file
*/
function flock_get_contents($filename, $mode = "rb")
{
$data = '';
// if (!file_exists($filename)) return $data;
$fp = fopen( $filename, $mode );
if( $fp && flock( $fp, LOCK_SH ) )
{
clearstatcache();
$size = filesize($filename);
if ($size > 0)
{
$data = fread( $fp, $size );
}
// flock($fp, LOCK_UN); // will be done by fclose
}
fclose( $fp );
return $data;
}
I think the "filesize" may return zero if another thread is writing to the file with a lock on.
<?php
$starttime = time();
$date = date('h:i:s',$starttime);
echo 'start:'.$date.'<br />';
$handle = fopen('test.txt','wb');
if($handle){
$flock = flock($handle,LOCK_EX);
echo '$flock '; var_dump($flock);echo '<br />';
if(!$flock){
echo 'no lock for me<br />';
}else{
echo 'got a lock<br />';
fwrite( $handle, ' start '.$date.' ' );
sleep(5); //assume the whole writing process takes 5 seconds
fwrite( $handle, ' end '.$date."n" );
}
fclose($handle);
}
echo 'end: this took '.(time()-$starttime).' seconds';
?>
<?php
$starttime = time();
$date = date('h:i:s',$starttime);
echo 'start:'.$date.'<br />';
for ($i = 0; $i < 20; $i++)
{
echo 'File size: '; var_dump(filesize('test.txt'));echo '<br />';
}
echo 'end: this took '.(time()-$starttime).' seconds';
?>
<?php
$starttime = time();
$date = date('h:i:s',$starttime);
echo 'start:'.$date.'<br />';
$handle = fopen('test.txt','rb');
if($handle)
{
$flock = flock($handle,LOCK_SH);
echo '$flock '; var_dump($flock);echo '<br />';
if(!$flock){
echo 'no lock for me<br />';
}
else
{
echo 'got a lock<br />';
for ($i = 0; $i < 20; $i++)
{
echo 'File size: '; var_dump(filesize('test.txt'));echo '<br />';
}
}
fclose($handle);
}
echo 'end: this took '.(time()-$starttime).' secconds';
?>
function flock_get_contents($filename, $mode = "rb")
{
$data = '';
$fp = fopen( $filename, $mode );
if( $fp && flock( $fp, LOCK_SH ) )
{
clearstatcache();
$size = filesize($filename);
if ($size > 0)
$data = fread( $fp, $size );
// flock($fp, LOCK_UN); // will be done by fclose
}
fclose( $fp );
return $data;
}
if (!defined('LOCK_SH')) {
define('LOCK_SH', 1);
}
/**
* Replace file_get_contents()
*
* @category PHP
* @package PHP_Compat
* @link http://php.net/function.file_get_contents
* @author Aidan Lister <aidan@php.net>
* @version $Revision: 1.21 $
* @internal resource_context is not supported
* @since PHP 5
* @require PHP 4.0.0 (user_error)
*/
function file_get_contents_flock($filename, $incpath = false, $resource_context = null)
{
if (false === $fh = fopen($filename, 'rb', $incpath)) {
user_error('file_get_contents() failed to open stream: No such file or directory',
E_USER_WARNING);
return false;
}
// Attempt to get a shared (read) lock
if (!flock($fh, LOCK_SH)) {
return false;
}
clearstatcache();
if ($fsize = @filesize($filename)) {
$data = fread($fh, $fsize);
} else {
$data = '';
while (!feof($fh)) {
$data .= fread($fh, 8192);
}
}
fclose($fh);
return $data;
}
Return to General Support (v1.x)
Users browsing this forum: No registered users and 2 guests