/**
  * Process the incoming command, default commands have a system of twist
  * @param $resUserSocket
  * @param $strMessageData
  */
 protected function process($resUserSocket, $strMessageData)
 {
     $arrData = $this->parseString($strMessageData);
     //Check that we have a valid response
     if (is_array($arrData) && count($arrData)) {
         //Check that a valid system call has been passed in
         if (array_key_exists('instance', $arrData) && array_key_exists('system', $arrData) && array_key_exists('action', $arrData) && array_key_exists('data', $arrData)) {
             switch ($arrData['system']) {
                 case 'twist':
                     switch ($arrData['action']) {
                         case 'login':
                             Users::login($arrData, $resUserSocket);
                             break;
                         case 'logout':
                             Users::logout($arrData, $resUserSocket);
                             break;
                         case 'debug':
                             //Toggle the admin log for this connection, only allowing toggle of connections that belong to admins
                             $blCurrentStatus = Sockets::getData($resUserSocket, 'system_admin_log');
                             Sockets::setData($resUserSocket, 'system_admin_log', $blCurrentStatus ? false : true);
                             System::stats();
                             break;
                         case 'restart':
                             //Shutdown the server and it will restart in 1 minute
                             $this->shutdownServer();
                             break;
                         case 'active':
                         case 'inactive':
                             //Set the connections current Active status (clicked in the last 30 seconds or not)
                             Sockets::setData($resUserSocket, 'activeStatus', array('status' => $arrData['action'], 'updated' => time()));
                             break;
                         case 'blur':
                         case 'focus':
                             //Set the connections current View status (focused on browser tab or not)
                             Sockets::setData($resUserSocket, 'viewStatus', array('status' => $arrData['action'], 'updated' => time()));
                             break;
                         case 'uri':
                             //Set the connections current URI (The page of the website that the connection is coming from)
                             Sockets::setData($resUserSocket, 'currentURI', $arrData['data']);
                             break;
                         case 'users':
                             Users::sendUserList($resUserSocket, $arrData);
                             break;
                     }
                     break;
                     //This is where the magic happens, any module can be called here using the correct command
                 //This is where the magic happens, any module can be called here using the correct command
                 default:
                     AppHandler::process($arrData['system'], $resUserSocket, $arrData);
                     break;
             }
         } else {
             //Error invalid params
             System::sendErrorResponse($resUserSocket, $arrData, 'Invalid request parameters');
         }
     }
 }
Example #2
0
 /**
  * Validate the users login credentials, create their connection and user record or reject them from the system
  * @param $arrData
  * @param $resSocket
  */
 public static function login($arrData, $resSocket)
 {
     $arrResponse = array('instance' => array_key_exists('instance', $arrData) ? $arrData['instance'] : null, 'system' => 'twist', 'action' => 'login', 'message' => '', 'data' => array());
     //Detect the correct configuration of parameters provided
     $strUID = array_key_exists('uid', $arrData['data']) ? $arrData['data']['uid'] : 'guest';
     $strPassword = array_key_exists('password', $arrData['data']) ? $arrData['data']['password'] : null;
     //Get the users identification data
     $arrUserData = self::requestUserData($strUID, $strPassword);
     //If the user is valid then connect the user
     if ($arrUserData['status']) {
         if (Sockets::create($resSocket)) {
             Sockets::setData($resSocket, 'user_id', $arrUserData['data']['id']);
             $intUserID = self::createUser($arrUserData['data']);
             //Now send through the valid login details to the user when they login
             $arrResponse['message'] = $arrUserData['message'];
             $arrResponse['data'] = array('user_id' => $intUserID, 'valid' => !is_null($intUserID) ? true : false);
         } else {
             $arrResponse['action'] = 'login-failed';
             $arrResponse['message'] = 'Failed to login, unknown connection issue';
         }
         Sockets::writeJSON($resSocket, $arrResponse);
     } else {
         //Failed to login, tell the user and destroy the connection
         $arrResponse['action'] = 'login-failed';
         $arrResponse['message'] = $arrUserData['message'];
         Sockets::writeJSON($resSocket, $arrResponse);
         Sockets::removeTemporary($resSocket);
     }
     //Log user status to the admin users
     System::stats();
 }