1
<?php
2
/**
3
 *   File functions:
4
 *   Show text in chat, send messages, color player names, ban/unban players in chat
5
 *
6
 *   @name                 : chatmsg.php
7
 *   @copyright            : (C) 2004,2005,2006 Vallheru Team based on Gamers-Fusion ver 2.5
8
 *   @author               : thindil <thindil@users.sourceforge.net>
9
 *   @author               : eyescream <> 
10
 *   @author               : Klaus Korner <albitos.snape@gmail.com>
11
 *   @version              : 1.0
12
 *   @since                : 03.02.2006
13
 *
14
 */
15
16
//
17
//
18
//       This program is free software; you can redistribute it and/or modify
19
//   it under the terms of the GNU General Public License as published by
20
//   the Free Software Foundation; either version 2 of the License, or
21
//   (at your option) any later version.
22
//
23
//   This program is distributed in the hope that it will be useful,
24
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
25
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26
//   GNU General Public License for more details.
27
//
28
//   You should have received a copy of the GNU General Public License
29
//   along with this program; if not, write to the Free Software
30
//   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
31
//
32
// $Id: chatmsgs.php 566 2006-09-13 09:31:08Z thindil $
33
/**
34
* Initializing mian classes needed in AJAX response/request
35
*/
36
require_once('includes/ajaxinit.php');
37
38
/**
39
* Get the localization for game
40
*/
41
require_once('languages/'.$player['lang'].'/chatmsgs.php');
42
$aRights = array(
43
    'ban' => array('Admin', 'Staff'),
44
    'unban' => array('Admin', 'Staff'),
45
    'bot' => array('Admin', 'Staff'),
46
    'clear' => array('Admin', 'Staff')
47
);
48
49
switch($_GET['room'])
50
{
51
	case 'izba':
52
		$room = "'izba'";
53
		break;
54
	case 'piwnica':
55
		$room = "'piwnica'";
56
		break;
57
	default:
58
		$room = "'izba'";
59
		break;
60
}
61
62
/**
63
* Function to send chat message
64
*/
65
function send($sMsg, $sSender, $iSender, $sLang, $iOwner = 0)
66
{
67
    global $db, $room;
68
    require_once('includes/bbcode.php');
69
70
    $sSender = $db -> qstr($sSender);
71
    $sLang = $db -> qstr($sLang, get_magic_quotes_gpc());
72
    $iSender = (int)$iSender;
73
    $iOwner = (int)$iOwner;
74
75
    $sMsg = $db -> qstr(breakLongWords(censorship(bbcodetohtml($sMsg)), 30, "<br />\n"));
76
    
77
    $time = array_sum(explode(' ', microtime()));
78
    $db -> Execute('INSERT INTO chat(sender, text, senderid, ownerid, lang, time, room) VALUES('.$sSender.','.$sMsg.','.$iSender.','.$iOwner.','.$sLang.','.$time.', '.$room.')') or die($db -> ErrorMsg());
79
}
80
81
/*
82
* Function to send system message to one player
83
*/
84
function systemMsg($msg)
85
{
86
    global $player;
87
    send($msg, '', 0, $player['lang'], $player['id']);
88
    exit;
89
}
90
91
/*
92
* Function to check right for command (from config array)
93
*/
94
function checkRights($rank, $action)
95
{
96
    global $aRights;
97
    if(!in_array($rank, $aRights[$action]))
98
    {
99
        systemMsg(NO_PERM);
100
    }
101
}
102
103
/*
104
* Function to ban player
105
*/
106
function ban($id, $duration, $reason)
107
{
108
    global $db, $player;
109
    $user = $db -> Execute('SELECT user FROM players WHERE id='.(int)$id);
110
    $db -> Execute('INSERT INTO chat_config(gracz, resets) VALUES('.(int)$id.', '.(int)$duration.')');
111
    send(PLAYER.$user->fields['user'].KICKED.REASON.$reason, getColoredName(INNKEEPER3, 'bot'), $player['id'], $player['lang']);
112
}
113
114
function unban($id)
115
{   
116
    global $db;
117
    $db -> Execute('DELETE FROM chat_config WHERE gracz='.(int)$id);
118
    systemMsg("Gracz ID: $id zostaƂ odbanowany");
119
}
120
121
122
/*
123
* Function to get collored player name
124
*/
125
function getColoredName($sName, $sRank)
126
{
127
    switch($sRank)
128
    {
129
      case 'Admin':
130
        $starter = '<span style="color: #0066cc;">'.$sName.'</span>';
131
        break;
132
      case 'Staff':
133
      case 'Karczmarka':
134
      case 'bot':
135
        $starter = '<span style="color: #108310;">'.$sName.'</span>';
136
        break;
137
      default:
138
        $starter = $sName;
139
    }
140
    return $starter;
141
}
142
143
/*
144
* Function to wrap too long words (like wordwrap)
145
*/
146
function breakLongWords($str, $maxLength, $char){
147
    $wordEndChars = array(" ", "\n", "\r", "\f", "\v", "\0");
148
    $count = 0;
149
    $newStr = "";
150
    $openTag = false;
151
    for($i=0; $i<strlen($str); $i++){
152
        $newStr .= $str{$i};   
153
       
154
        if($str{$i} == "<"){
155
            $openTag = true;
156
            continue;
157
        }
158
        if(($openTag) && ($str{$i} == ">")){
159
            $openTag = false;
160
            continue;
161
        }
162
       
163
        if(!$openTag){
164
            if(!in_array($str{$i}, $wordEndChars)){//If not word ending char
165
                $count++;
166
                if($count==$maxLength){//if current word max length is reached
167
                    $newStr .= $char;//insert word break char
168
                    $count = 0;
169
                }
170
            }else{//Else char is word ending, reset word char count
171
                    $count = 0;
172
            }
173
        }
174
       
175
    }//End for   
176
    return $newStr;
177
}
178
/*
179
* Sending messages
180
*/
181
if(isset($_POST['msg']))
182
{
183
    $starter = getColoredName($player['user'], $player['rank']);
184
    if($_POST['msg'][0] == '!')
185
    {
186
        if(preg_match('#^!ban ([1-90-9]+) ([1-90-9]+) (.*)$#', $_POST['msg'], $x))
187
        {
188
            checkRights($player['rank'], 'ban');
189
            ban($x[1], $x[2], $x[3]);
190
        }
191
        elseif(preg_match('#^!unban ([1-90-9]+)$#', $_POST['msg'], $x))
192
        {
193
            checkRights($player['rank'], 'unban');
194
            unban($x[1]);
195
        }
196
        elseif(preg_match('#^!bot ([^ ]+) (.*)$#', $_POST['msg'], $x))
197
        {
198
            checkRights($player['rank'], 'bot');
199
            send($x[2], getColoredName($x[1], 'bot'), $player['id'], $player['lang']);
200
        }
201
        elseif(preg_match('#^!clear$#', $_POST['msg']))
202
        {
203
            checkRights($player['rank'], 'clear');
204
            $db -> Execute("DELETE FROM chat WHERE lang = '".$player['lang']."' AND room=".$room);
205
            send(CLEAR_TEXT, getColoredName(INNKEEPER4, 'bot'), $player['id'], $player['lang']);
206
        }
207
        elseif(preg_match("#^!w ([1-90-9]+) (.*)$#", $_POST['msg'], $x))
208
        {
209
            $user = $db -> getRow('SELECT user FROM players WHERE id='.(int)$x[1]);
210
            if(!isset($user['user'])) systemMsg(NO_USER);
211
            send($x[2], $starter, $player['id'], $player['lang'], $x[1]);
212
        }
213
        else
214
        {
215
            systemMsg(NO_COMMAND);
216
        }
217
    }
218
    else
219
    {
220
        $czat = $db -> Execute('SELECT `gracz` FROM `chat_config` WHERE `gracz`='.$player['id']) or die($db -> ErrorMsg()); //check for chat ban
221
        if ($czat -> fields['gracz'])
222
        {
223
            systemMsg (NO_PERM);
224
        }
225
        $czat -> Close();
226
227
        /**
228
        * Start innkeeper bot
229
        */
230
        require_once('class/bot_class.php');
231
        $objBot = new Bot($_POST['msg'], 'Karczmarzu');
232
        $blnOrder = false;
233
        $message = $_POST['msg'];
234
        if ($blnCheckbot = $objBot -> Checkbot())
235
        {
236
            $strAnswer = $objBot -> Botanswer();
237
            // player gives something to other player: Karczmarzu X Y Z for 1234
238
            //Search for numbers at the end of message. If numeric is found, it will be stored in $intReceiverId[0]
239
            if (preg_match("#\d+#",$_POST['msg'], $intReceiverId))
240
            {
241
				$user = $db -> Execute('SELECT `user` FROM `players` WHERE `id`='.$intReceiverId[0]);
242
                $message = preg_replace("#dla \d+$#", ' '.FOR_A.' '.str_replace("&#39;","'",$user -> fields['user']), $_POST['msg']);
243
                $user -> Close();
244
                $blnOrder = true;
245
            }
246
        }        
247
        $owner = 0;
248
249
        send($message, $starter, $player['id'], $player['lang']);
250
        $objInnkeeper = $db -> Execute('SELECT `user` FROM `players` WHERE `rank`=\'Karczmarka\' AND `page`=\'Chat\' AND `lpv`>='.(time() - 60));
251
        if (!$objInnkeeper -> fields['user'] && isset($strAnswer))
252
        {
253
            send($strAnswer, getColoredName(INNKEEPER2, 'bot'), 0, $player['lang']);
254
        }
255
        elseif ($blnCheckbot)
256
        {
257
            send(INNKEEPER_GONE.$objInnkeeper -> fields['user'].RULES,  getColoredName(INNKEEPER3, 'bot'), 0, $player['lang']);
258
        }
259
        $objInnkeeper -> Close();
260
    }
261
}
262
else
263
{
264
  $time = array_sum(explode(' ', microtime()));
265
  $oUserAction = $db -> Execute('SELECT time FROM chat_users WHERE userid='.$player['id'].' AND room='.$room) or die($db -> ErrorMsg());
266
  
267
  if(isset($_GET['visited']) && $_GET['visited'] == 1)
268
  {
269
    $iPlayerTime = $oUserAction -> fields['time'];
270
  }
271
  else
272
  {
273
    $iPlayerTime = 0;
274
  }
275
  $db -> Execute('INSERT INTO `chat_users`(`userid`, `time`, `room`) VALUES(?,?,?) ON DUPLICATE KEY UPDATE `time`='.$time.', `room`='.$room.'', array($player['id'],$time,$room));
276
	
277
278
279
/*
280
* Sending non-read messages
281
*/
282
283
$arrMsg = array();
284
$messages = $db -> SelectLimit('SELECT * FROM chat WHERE time >= '.$iPlayerTime.' AND (ownerid=0 OR ownerid='.$player['id'].' OR senderid='.$player['id'].') AND (lang=\''.$player['lang'].'\' OR lang=\'\') AND room = '.$room.' ORDER BY chat.id DESC', 20);
285
286
$i = 0;
287
while(!$messages -> EOF)
288
{
289
    $arrMsg[$i] = $messages -> fields;
290
    $arrMsg[$i]['date'] = date('d m Y\r. G:i:s', substr($messages -> fields['time'], 0, 10));
291
    if($messages -> fields['ownerid'] != 0)
292
    {
293
        $username = $db -> GetRow('SELECT user FROM players WHERE id='.$messages -> fields['ownerid']);
294
        $arrMsg[$i]['owner'] = $username['user'];
295
        $username = $db -> GetRow('SELECT gender FROM players WHERE id='.$messages -> fields['senderid']);
296
        $arrMsg[$i]['gender'] = $username['gender'];
297
    }
298
    $messages -> MoveNext();
299
    $i++;
300
}
301
302
$arrUser = array();
303
$users = $db -> Execute('SELECT cu.userid, p.user, p.rank FROM chat_users cu, players p WHERE cu.room='.$room.' AND cu.time > '.($time - 30).' AND cu.userid = p.id ORDER BY user') or die($db -> ErrorMsg());
304
while(!$users -> EOF)
305
{
306
    $arrUser[] = array(
307
        'userid' => $users -> fields['userid'],
308
        'user' => getColoredName($users -> fields['user'], $users -> fields['rank'])
309
    );
310
    $users -> MoveNext();
311
}
312
$users -> Close();
313
314
$num = $db -> Execute('SELECT COUNT(*) AS c FROM chat WHERE room='.$room.' AND (lang=\''.$player['lang'].'\' OR lang=\'\')') or die($db -> ErrorMsg());
315
$arrReply = array(
316
	'msg'   => array_reverse($arrMsg),
317
    'users' => $arrUser,
318
    'number' => $num -> fields['c']
319
);
320
}
321
require_once('includes/json.php');
322
$json =  new Services_JSON();
323
$smarty -> assign('Reply', $json -> encode($arrReply));
324
325
$smarty -> display('chatmsgs.tpl');
326
327
328
?>