Exemple #1
0
 public function addMessage($message, $by)
 {
     if (!$this->getId()) {
         $this->commit();
     }
     $m = new ChatMessage();
     $m->setChat($this);
     $m->setMessage($message);
     $m->setByUser($by);
     $m->commit();
 }
Exemple #2
0
 public function say()
 {
     if ($this->isPost()) {
         $message = new ChatMessage();
         $message->set_model($this->vars());
         $message->save();
         C($message)->commit();
         self::$static_flag = true;
     }
     exit;
 }
 public function retrieveChatMessages()
 {
     $username = Input::get('username');
     $message = ChatMessage::where('sender_username', '!=', $username)->where('read', '=', false)->first();
     if (count($message) > 0) {
         $message->read = true;
         $message->save();
         return $message->message;
     }
 }
Exemple #4
0
 public function getConversation()
 {
     $id = Input::get('id');
     $messages = ChatMessage::where(function ($query) use($id) {
         $query->where('send_to', '=', Auth::id())->where('sender', '=', $id);
     })->orWhere(function ($query) use($id) {
         $query->where('send_to', '=', $id)->where('sender', '=', Auth::id());
     })->with('sender', 'send_to')->get();
     ChatMessage::where('send_to', '=', Auth::user()->id)->where('sender', '=', $id)->update(['read' => 1]);
     return View::make('chat.conversation', compact('messages'));
 }
<?php

/* PARAMETRES DE LA BASE */
const DATABASE_HOST = 'localhost';
const DATABASE_DBNAME = 'minichat';
const DATABASE_USER = '******';
const DATABASE_PASS = '******';
/* CONNEXION A LA BASE */
require '../../config/database.php';
/* INSERTION DES CLASSES */
require '../../model/classes/User.php';
require '../../model/classes/ChatMessage.php';
/* SESSION */
session_start();
if (isset($_SESSION['userId']) && $_SESSION['userId'] !== 0) {
    $user = new User($database, $_SESSION['userId']);
}
// Variables pour tester en GET
// ?message=TEST&userId=1
// Si un message est envoyé
if (!empty($_POST)) {
    // Filtre les données entrantes contre les attaques XSS
    $inputs = filter_input_array(INPUT_POST, ['message' => FILTER_SANITIZE_STRING, 'userId' => FILTER_SANITIZE_STRING]);
    $message = new ChatMessage($inputs['message'], $user->getId(), $database);
    $message->insertMessageInDatabase();
}
Exemple #6
0
     $x->setHandler('editHandler');
     echo $x->render();
     echo '<br/>';
     echo '&raquo; ' . ahref('a/chatroom/empty/' . $this->child, 'Empty chatroom of messages') . '<br/>';
     echo '<br/>';
     echo '&raquo; ' . ahref('a/chatroom/remove/' . $this->child, 'Remove chatroom') . '<br/>';
     break;
 case 'remove':
     if (confirmed('Are you sure you want to remove this chatroom?')) {
         ChatRoom::remove($this->child);
         js_redirect('a/chatroom/list');
     }
     break;
 case 'empty':
     if (confirmed('Are you sure you want to remove all messages from this chatroom?')) {
         ChatMessage::deleteByRoom($this->child);
         js_redirect('a/chatroom/list');
     }
     break;
 case 'new':
     function createHandler($p)
     {
         $o = new ChatRoom();
         $o->name = trim($p['name']);
         $o->id = $o->store();
         js_redirect('a/chatroom/list');
     }
     echo '<h2>Create new chatroom</h2>';
     $x = new XhtmlForm();
     $x->addInput('name', 'Name');
     $x->addSubmit('Create');
 static function setDatabase($database)
 {
     self::$database = $database;
 }
 public function actionCheckMessage()
 {
     $data = array();
     $date = date('Y-m-d H:i:s', strtotime('-5 seconds'));
     $receiverId = Yii::app()->user->getId();
     $criteria = new CDbCriteria();
     $criteria->condition = "ReceiverId = {$receiverId} AND Date > '" . $date . "'";
     $models = ChatMessage::model()->findAll($criteria);
     if (!empty($models)) {
         foreach ($models as $model) {
             if (MembershipUsers::model()->isSubscribe($model->ReceiverId)) {
                 $data[] = array('UserId' => $model->UserId, 'UserName' => Profile::model()->findByPk($model->UserId)->UserName, 'ReceiverId' => $model->ReceiverId);
             }
         }
         echo json_encode($data);
     }
 }
Exemple #9
0
         $out[] = array('name' => User::get($r->from)->name, 'from' => $r->from, 'msg' => $r->msg, 'ts' => $r->microtime);
     }
     echo json_encode($out);
     break;
 case 'send':
     // XHR - writes a message to chatroom
     // child = room id
     // $_GET['m'] = message
     if (!$this->child || !is_numeric($this->child) || empty($_GET['m'])) {
         die('hey');
     }
     $cr = ChatRoom::get($this->child);
     if ($cr->locked_by) {
         die('hey3');
     }
     $m = new ChatMessage();
     $m->room = $this->child;
     $m->from = $session->id;
     $m->msg = $_GET['m'];
     $m->microtime = microtime(true);
     $m->store();
     $page->setMimeType('text/plain');
     echo 'OK';
     break;
 case 'show':
     // child = room id
     $cr = ChatRoom::get($this->child);
     echo '<h2>Chat in ' . $cr->name . '</h2>';
     if ($cr->locked_by) {
         echo 'The chatroom is locked!';
         return;
Exemple #10
0
@if(Auth::check())
    @if(isset($groups_with_users))
        @foreach ($groups_with_users as $group)
            @if (count($group->users))
                <h3 class="list-heading">{{ $group->name }}</h3>
                <ul class="media-list list-items">
                    @foreach ($group->users as $user)
                        <li class="media" data-id="{{ $user->id }}">
                            <?php 
if (ChatMessage::where("sender", '=', $user->id)->where('send_to', '=', Auth::user()->id)->where('read', 0)->count()) {
    ?>
                                <div class="media-status user-chat" >
                                    <span class="badge badge-success">new</span>
                            <?php 
} else {
    ?>
                                <div class="media-status user-chat" style="display: none;">
                                    <span class="badge badge-success"></span>
                            <?php 
}
?>
                            </div>
                            <img class="media-object" src="{{ URL::to('images/avatar.png') }}" alt="...">
                            <div class="media-body">
                                <h4 class="media-heading">{{ $user->full_name }}</h4>
                                <div class="media-heading-sub">
                                    @if ($user->last_activity + 5 < time())
                                        OFFLINE
                                    @else
                                        <span style="color: greenyellow;">ONLINE</span>
                                    @endif
 /**
  * Analyzes body stream.
  *
  * @return bool true if the stream was successfully analyzed, false otherwise
  */
 protected function analyzeBodyStreamF()
 {
     $time_cnt = $this->gameSettings->gameSpeed;
     $age_flag = array(0, 0, 0, 0, 0, 0, 0, 0);
     $bodyStream = $this->bodyStream->getDataString();
     $gameInfo = $this->gameInfo;
     $size = $this->bodyStream->getSize();
     $pos = 0;
     while ($pos < $size - 3) {
         if ($pos == 0 && !$this->isMgx) {
             $od_type = 0x4;
         } else {
             $packed_data = substr($bodyStream, $pos, 4);
             $pos += 4;
             $unpacked_data = unpack('l', $packed_data);
             $od_type = $unpacked_data[1];
         }
         // ope_data types: 4(Game_start or Chat), 2(Sync), or 1(Command)
         switch ($od_type) {
             // Game_start or Chat command
             case 0x4:
             case 0x3:
                 $packed_data = substr($bodyStream, $pos, 4);
                 $pos += 4;
                 $unpacked_data = unpack('l', $packed_data);
                 $command = $unpacked_data[1];
                 if ($command == 0x1f4) {
                     // Game_start
                     if ($this->isMgl) {
                         $pos += 28;
                         $packed_data = substr($bodyStream, $pos, 1);
                         $pos++;
                         $unpacked_data = unpack('C', $packed_data);
                         $ver = $unpacked_data[1];
                         switch ($ver) {
                             case 0:
                                 if ($gameInfo->gameVersion != GameInfo::VERSION_AOKTRIAL) {
                                     $gameInfo->gameVersion = GameInfo::VERSION_AOK20;
                                 }
                                 break;
                             case 1:
                                 $gameInfo->gameVersion = GameInfo::VERSION_AOK20A;
                                 break;
                         }
                         $pos += 3;
                     } else {
                         switch ($od_type) {
                             case 0x3:
                                 if ($gameInfo->gameVersion != GameInfo::VERSION_AOCTRIAL) {
                                     $gameInfo->gameVersion = GameInfo::VERSION_AOC10;
                                 }
                                 break;
                             case 0x4:
                                 if ($gameInfo->gameVersion == GameInfo::VERSION_AOC) {
                                     $gameInfo->gameVersion = GameInfo::VERSION_AOC10C;
                                 }
                                 break;
                         }
                         $pos += 20;
                     }
                 } elseif ($command == -1) {
                     // Chat
                     foreach ($this->players as $i => $player) {
                         if ($player->feudalTime != 0 && $player->feudalTime < $time_cnt && $age_flag[$i] < 1) {
                             $this->ingameChat[] = new ChatMessage($player->feudalTime, null, $player->name . ' advanced to Feudal Age');
                             $age_flag[$i] = 1;
                         }
                         if ($player->castleTime != 0 && $player->castleTime < $time_cnt && $age_flag[$i] < 2) {
                             $this->ingameChat[] = new ChatMessage($player->castleTime, null, $player->name . ' advanced to Castle Age');
                             $age_flag[$i] = 2;
                         }
                         if ($player->imperialTime != 0 && $player->imperialTime < $time_cnt && $age_flag[$i] < 3) {
                             $this->ingameChat[] = new ChatMessage($player->imperialTime, null, $player->name . ' advanced to Imperial Age');
                             $age_flag[$i] = 3;
                         }
                     }
                     $packed_data = substr($bodyStream, $pos, 4);
                     $pos += 4;
                     $unpacked_data = unpack('l', $packed_data);
                     $chat_len = $unpacked_data[1];
                     $chat = substr($bodyStream, $pos, $chat_len);
                     $pos += $chat_len;
                     if ($chat[0] == '@' && $chat[1] == '#' && $chat[2] >= '1' && $chat[2] <= '8') {
                         $chat = rtrim($chat);
                         // throw null-termination character
                         if (substr($chat, 3, 2) == '--' && substr($chat, -2) == '--') {
                             // skip messages like "--Warning: You are being under attack... --"
                         } else {
                             $this->ingameChat[] = ChatMessage::create($time_cnt, $this->players[$chat[2] - 1], substr($chat, 3));
                         }
                     }
                 }
                 break;
                 // Sync
             // Sync
             case 0x2:
                 $packed_data = substr($bodyStream, $pos, 4);
                 $pos += 4;
                 $unpacked_data = unpack('l', $packed_data);
                 $time_cnt += $unpacked_data[1];
                 // time_cnt is in miliseconds
                 $packed_data = substr($bodyStream, $pos, 4);
                 $pos += 4;
                 $unpacked_data = unpack('l', $packed_data);
                 $unknown = $unpacked_data[1];
                 if ($unknown == 0) {
                     $pos += 28;
                 }
                 $pos += 12;
                 break;
                 // Command
             // Command
             case 0x1:
                 $packed_data = substr($bodyStream, $pos, 4);
                 $pos += 4;
                 $unpacked_data = unpack('l', $packed_data);
                 $length = $unpacked_data[1];
                 $packed_data = substr($bodyStream, $pos, 1);
                 $pos++;
                 $unpacked_data = unpack('C', $packed_data);
                 $command = $unpacked_data[1];
                 $pos--;
                 switch ($command) {
                     case 0xb:
                         // player resign
                         $pos++;
                         $packed_data = substr($bodyStream, $pos, 1);
                         $pos++;
                         $unpacked_data = unpack('C', $packed_data);
                         $player_index = $unpacked_data[1];
                         if (($player = $this->playersByIndex[$player_index]) && $player->resignTime == 0) {
                             $player->resignTime = $time_cnt;
                             $this->ingameChat[] = new ChatMessage($time_cnt, null, $player->name . ' resigned');
                         }
                         $pos += $length - 2;
                         break;
                     case 0x65:
                         // researches
                         $pos += 8;
                         $packed_data = substr($bodyStream, $pos, 2);
                         $pos += 2;
                         $unpacked_data = unpack('v', $packed_data);
                         $player_id = $unpacked_data[1];
                         $packed_data = substr($bodyStream, $pos, 2);
                         $pos += 2;
                         $unpacked_data = unpack('v', $packed_data);
                         $research_id = $unpacked_data[1];
                         if (!($player = $this->playersByIndex[$player_id])) {
                             $pos += $length - 12;
                             break;
                         }
                         switch ($research_id) {
                             case 101:
                                 $player->feudalTime = $time_cnt + 130000;
                                 break;
                             case 102:
                                 $player->castleTime = $player->civId == Civilization::PERSIANS ? $time_cnt + round(160000 / 1.1) : $time_cnt + 160000;
                                 break;
                             case 103:
                                 $player->imperialTime = $player->civId == Civilization::PERSIANS ? $time_cnt + round(190000 / 1.15) : $time_cnt + 190000;
                                 break;
                         }
                         $player->researches[$research_id] = $time_cnt;
                         $pos += $length - 12;
                         break;
                     case 0x77:
                         // training unit
                         $pos += 8;
                         $packed_data = substr($bodyStream, $pos, 2);
                         $pos += 2;
                         $unpacked_data = unpack('v', $packed_data);
                         $unit_type_id = $unpacked_data[1];
                         $packed_data = substr($bodyStream, $pos, 2);
                         $pos += 2;
                         $unpacked_data = unpack('v', $packed_data);
                         $unit_num = $unpacked_data[1];
                         if (!isset($this->units[$unit_type_id])) {
                             $this->units[$unit_type_id] = $unit_num;
                         } else {
                             $this->units[$unit_type_id] += $unit_num;
                         }
                         $pos += $length - 12;
                         break;
                     case 0x64:
                         // pc trains unit
                         $pos += 10;
                         $packed_data = substr($bodyStream, $pos, 2);
                         $pos += 2;
                         $unpacked_data = unpack('v', $packed_data);
                         $unit_type_id = $unpacked_data[1];
                         $unit_num = 1;
                         // always for pc?
                         if (!isset($this->units[$unit_type_id])) {
                             $this->units[$unit_type_id] = $unit_num;
                         } else {
                             $this->units[$unit_type_id] += $unit_num;
                         }
                         $pos += $length - 12;
                         break;
                     case 0x66:
                         // building
                         $pos += 2;
                         $packed_data = substr($bodyStream, $pos, 2);
                         $pos += 2;
                         $unpacked_data = unpack('v', $packed_data);
                         $player_id = $unpacked_data[1];
                         $pos += 8;
                         $packed_data = substr($bodyStream, $pos, 2);
                         $pos += 2;
                         $unpacked_data = unpack('v', $packed_data);
                         $building_type_id = $unpacked_data[1];
                         if (in_array($building_type_id, RecAnalystConst::$GATE_UNITS)) {
                             $building_type_id = Unit::GATE;
                         } elseif (in_array($building_type_id, RecAnalystConst::$PALISADE_GATE_UNITS)) {
                             $building_type_id = Unit::PALISADE_GATE;
                         }
                         if (!isset($this->buildings[$player_id][$building_type_id])) {
                             $this->buildings[$player_id][$building_type_id] = 1;
                         } else {
                             $this->buildings[$player_id][$building_type_id]++;
                         }
                         $pos += $length - 14;
                         break;
                     case 0x6c:
                         // tributing
                         $pos++;
                         // player_id_from
                         $packed_data = substr($bodyStream, $pos, 1);
                         $pos++;
                         $unpacked_data = unpack('C', $packed_data);
                         $player_id_from = $unpacked_data[1];
                         // player_id_to
                         $packed_data = substr($bodyStream, $pos, 1);
                         $pos++;
                         $unpacked_data = unpack('C', $packed_data);
                         $player_id_to = $unpacked_data[1];
                         // resource_id
                         $packed_data = substr($bodyStream, $pos, 1);
                         $pos++;
                         $unpacked_data = unpack('C', $packed_data);
                         $resource_id = $unpacked_data[1];
                         // amount_tributed
                         $packed_data = substr($bodyStream, $pos, 4);
                         $pos += 4;
                         $unpacked_data = unpack('f', $packed_data);
                         $amount_tributed = $unpacked_data[1];
                         // market_fee
                         $packed_data = substr($bodyStream, $pos, 4);
                         $pos += 4;
                         $unpacked_data = unpack('f', $packed_data);
                         $market_fee = $unpacked_data[1];
                         $playerFrom = $this->playersByIndex[$player_id_from];
                         $playerTo = $this->playersByIndex[$player_id_to];
                         if ($playerFrom && $playerTo) {
                             $tribute = new Tribute();
                             $tribute->time = $time_cnt;
                             $tribute->playerFrom = $playerFrom;
                             $tribute->playerTo = $playerTo;
                             $tribute->resourceId = $resource_id;
                             $tribute->amount = floor($amount_tributed);
                             $tribute->fee = $market_fee;
                             $this->tributes[] = $tribute;
                         }
                         $pos += $length - 12;
                         break;
                     default:
                         $pos += $length;
                         break;
                 }
                 $pos += 4;
                 break;
             default:
                 /* detect if this is a header of saved chapter */
                 /* sometimes header of the saved chapter is in $03 command, instead of $20 as it should be,
                    when this happens the length of $20 command is $0E, otherwise it is $02 (always?, rule?),
                    we do not rely on it, that's why we are skipping saved chapter data here and not in $20 command */
                 if ($pos == $this->_nextPos - $this->_headerLen - 4) {
                     /* this is a header of saved chapter data, we have already read next_command_block
                        that's why -4 in the if-statement */
                     /* next_pos - header_len = offset of compressed chapter data */
                     $next_command_block = $od_type;
                     $packed_data = substr($bodyStream, $pos, 4);
                     $pos += 4;
                     $unpacked_data = unpack('l', $packed_data);
                     $this->_nextPos = $unpacked_data[1];
                     // next_chapter_pos
                     $pos = $next_command_block - $this->_headerLen - 8;
                 } else {
                     // shouldn't occure, just to prevent unexpected endless cycling
                     $pos++;
                 }
                 break;
         }
     }
     unset($bodyStream);
     $gameInfo->playTime = $time_cnt;
     return true;
 }