Ejemplo n.º 1
0
 public static function getChats($lastID)
 {
     $lastID = (int) $lastID;
     $result = DB::query('SELECT * FROM webchat_lines WHERE id > ' . $lastID . ' ORDER BY id ASC');
     $chats = array();
     while ($chat = $result->fetch_object()) {
         $chat->time = array('hours' => gmdate('H', strtotime($chat->ts)), 'minutes' => gmdate('i', strtotime($chat->ts)));
         $chat->gravatar = Chat::gravatarFromHash($chat->gravatar);
         $chats[] = $chat;
     }
     return array('chats' => $chats);
 }
 public function actionChat()
 {
     $dbOptions = array('db_host' => 'localhost', 'db_user' => 'root', 'db_pass' => '', 'db_name' => 'ot');
     session_name('webchat');
     session_id(rand(1, 5));
     if (get_magic_quotes_gpc()) {
         // If magic quotes is enabled, strip the extra slashes
         array_walk_recursive($_GET, create_function('&$v,$k', '$v = stripslashes($v);'));
         array_walk_recursive($_POST, create_function('&$v,$k', '$v = stripslashes($v);'));
     }
     try {
         // Connecting to the database
         //$db = new DB;
         $chat = new Chat();
         //DB::init($dbOptions);
         $response = array();
         // Handling the supported actions:
         switch ($_GET['action']) {
             case 'login':
                 $response = array('status' => 1, 'name' => Yii::app()->user->name, 'gravatar' => Chat::gravatarFromHash('123'));
                 break;
             case 'checkLogged':
                 $response = $chat::checkLogged();
                 break;
             case 'logout':
                 $response = $chat::logout();
                 break;
             case 'submitChat':
                 $response = $chat::submitChat($_POST['chatText']);
                 break;
             case 'getUsers':
                 $response = $chat::getUsers();
                 break;
             case 'getChats':
                 $response = $chat::getChats($_GET['lastID']);
                 break;
             default:
                 throw new Exception('Wrong action');
         }
         echo json_encode($response);
     } catch (Exception $e) {
         die(json_encode(array('error' => $e->getMessage())));
     }
 }