Php instant chat program (php chat room)
Put it bluntly and directly add the code
Define ('root _ path', dirname (_ file __).'/');
Date_default_timezone_set ('prc ');
$ Lang ['title'] = 'php chatroom ';
$ Lang ['description'] = 'simple chatroom ';
$ Lang ['keyword'] = 'php chat room, simple chatroom ';
$ Config ['datalistnum'] = 30;
$ Config ['datadir '] = './';
$ Config ['datafile'] = 'data ';
$ Action = isset ($ _ POST ['action'])? Trim ($ _ POST ['action']): '';
Function access ($ method, $ data = '', $ type = 'array ')
{
Global $ config;
$ File = ROOT_PATH. $ config ['datadir '].'/'. $ config ['datafile'];
If ($ method = 'r '){
If (file_exists ($ file )){
If ($ type = 'array '){
$ Data = file_get_contents ($ file );
$ Data = json_decode ($ data, true );
$ Data = is_array ($ data )? $ Data: array ();
}
If ($ type = 'json') $ data = file_get_contents ($ file );
Return $ data;
}
If ($ type = 'array') $ data = array ();
If ($ type = 'json') $ data = '';
Return $ data;
}
If ($ method = 'w '){
$ Data = json_encode ($ data );
Return file_put_contents ($ file, $ data );
}
}
Function makeDir ($ dir, $ mode = 0777)
{
If (! Is_dir ($ dir )){
MakeDir (dirname ($ dir), $ mode );
Return mkdir ($ dir, $ mode );
}
Return true;
}
Function showIp ($ ip)
{
$ IpArr = explode ('.', $ ip );
Return $ IpArr ['0']. '. '. $ IpArr ['1']. '. '. preg_replace ("/[0-9]/", "*", $ IpArr ['2']). '. '. preg_replace ("/[0-9]/", "*", $ IpArr ['3']);
}
Function ubb ($ ubb ){
Global $ lang;
$ Ubb = strip_tags ($ ubb );
$ Ubb = preg_replace ('/\ n /','
', Preg_replace (' // ', '', $ ubb ));
$ Ubb = preg_replace ("/(\ [url = )(. *) \] (. *) (\ [\/url \])/"," \ 3 ", $ ubb );
$ Ubb = preg_replace ("/(\ [color = )(. *) \] (. *) (\ [\/color \])/"," \ 3 ", $ ubb );
$ Ubb = preg_replace ("/(\ [size =) ([0-9] *) \] (. *) (\ [\/size \])/"," \ 3 ", $ ubb );
$ Ubb = preg_replace ("/(\ [B \]) (. *) (\ [\/B \])/","\ 2", $ Ubb );
$ Ubb = preg_replace ("/(\ [img \]) (. *) (\ [\/img \])/", "", $ ubb );
$ Ubb = preg_replace ("/\ [qq \] ([0-9] *) \ [\/qq \]/", "", $ ubb );
Return $ ubb;
}
Function cutstr ($ string, $ length, $ dot = '...')
{
If (strlen ($ string) <= $ length) return $ string;
$ String = str_replace (array ('&', '"', '<', '>'), array ('&', '"', '<', '>'), $ string );
$ Strcut = '';
$ N = $ tn = $ noc = 0;
While ($ n <strlen ($ string )){
$ T = ord ($ string [$ n]);
If ($ t = 9 | $ t = 10 | (32 <= $ t & $ t <= 126 )){
$ Tn = 1; $ n ++; $ noc ++;
} Else if (194 <=$ t & $ t <= 223 ){
$ Tn = 2; $ n + = 2; $ noc + = 2;
} Else if (224 <=$ t & $ t <= 239 ){
$ Tn = 3; $ n + = 3; $ noc + = 2;
} Else if (240 <=$ t & $ t <= 247 ){
$ Tn = 4; $ n + = 4; $ noc + = 2;
} Else if (248 <=$ t & $ t <= 251 ){
$ Tn = 5; $ n + = 5; $ noc + = 2;
} Else if ($ t = 252 | $ t = 253 ){
$ Tn = 6; $ n + = 6; $ noc + = 2;
} Else {
$ N ++;
}
If ($ noc >=$ length ){
Break;
}
}
If ($ noc> $ length ){
$ N-= $ tn;
}
$ Strcut = substr ($ string, 0, $ n );
$ Strcut = str_replace (array ('&', '"', '<', '>'), array ('&', '"', '<', '>'), $ strcut );
Return $ strcut. $ dot;
}
Function checkLen ($ str, $ minLen, $ maxLen = '')
{
$ StrLen = strlen ($ str );
If ($ maxLen ){
If ($ strLen <$ minLen | $ strLen> $ maxLen) return false;
} Else {
If ($ strLen <$ minLen) return false;
}
Return true;
}
If ($ action = 'write '){
$ Name = $ _ POST ['name']? $ _ POST ['name']: showIp ($ _ SERVER ['remote _ ADDR ']);
$ Content = ubb ($ _ POST ['content']);
If (trim ($ _ POST ['content']) {
SetCookie ('chatname', $ name, time () + 1*30*24*60*60*1000); // one month
MakeDir (ROOT_PATH. $ config ['datadir ']);
! CheckLen ($ name, 0, 20) & $ name = cutStr ($ name, 20 );
! CheckLen ($ content, 0, 1000) & $ content = cutStr ($ content, 1000 );
If (! File_exists (ROOT_PATH. $ config ['datadir '].'/'. $ config ['datafile']) {
Access ('W', array ('0' => array ('name' => $ name, 'content' => $ content, 'Time' => date ('H: I: s '))));
} Else {
$ DataArr = access ('r ');
$ Count = count ($ dataArr );
If ($ count> = $ config ['datalistnum']) {
Foreach ($ dataArr as $ key => $ value ){
If ($ key >=( $ count-($ config ['datalistnum']-1 ))){
$ Data [] = $ value;
}
}
} Else {
$ Data = $ dataArr;
}
Array_push ($ data, array ('name' => $ name, 'content' => $ content, 'Time' => date ('H: I: s ')));
Access ('W', $ data );
}
}
}
If ($ action = 'read '){
Exit (access ('R', '', 'json '));
}
?>
<? Php echo $ lang ['title']?>