Ejemplo n.º 1
0
 public function __call($name, $params)
 {
     try {
         if (strpos($name, '_call_') === 0) {
             $methodName = substr($name, strlen('_call_'));
             list($args, $argsuments) = $params;
             if (is_object($argsuments)) {
                 $argsuments = (array) $argsuments;
             }
             //                var_dump($argsuments);
             if (empty($argsuments) || empty($argsuments['token'])) {
                 // todo disconnect session
                 return ['error' => 'token is null'];
             }
             $token = $argsuments['token'];
             $session = Session::getSession($token);
             //                var_dump($token);
             //                var_dump($session);
             if ($session == null) {
                 // todo disconect client
                 return ['error' => 'session is null'];
             }
             $user = $session->getUser();
             //                var_dump($user->toArray());
             if ($user == null) {
                 return ['error' => 'user is null'];
             }
             if ($user->wampGenerateToken($session->id) != $token) {
                 return ['error' => 'token!'];
             }
             unset($args['token']);
             if ($this->hasMethod($methodName)) {
                 //                    var_dump('line:'.__LINE__);
                 $params = ['session' => $session];
                 if (!empty($argsuments)) {
                     foreach ($argsuments as $name => $value) {
                         $name = str_replace(' ', '', ucwords(implode(' ', explode('-', $name))));
                         $name = mb_strtolower(substr($name, 0, 1)) . substr($name, 1);
                         $params[$name] = $value;
                     }
                     //                        $params = array_merge($params, $argsuments);
                     //                        VarDumper::dump($params, 3, 0);
                 }
                 $params['args'] = $args;
                 $params['userId'] = (int) $session->userId;
                 $params['currentUserId'] = (int) $session->userId;
                 try {
                     if ($this->beforeCall($methodName, $params) !== false) {
                         $method = new \ReflectionMethod($this, $methodName);
                         //                    var_dump('line:'.__LINE__);
                         $argsMethod = [];
                         $missing = [];
                         foreach ($method->getParameters() as $param) {
                             //                        var_dump('line:'.__LINE__);
                             $name = $param->getName();
                             if (array_key_exists($name, $params)) {
                                 if ($param->isArray()) {
                                     $argsMethod[] = is_array($params[$name]) ? $params[$name] : [$params[$name]];
                                 } else {
                                     $argsMethod[] = $params[$name];
                                 }
                                 unset($params[$name]);
                             } elseif ($param->isDefaultValueAvailable()) {
                                 $argsMethod[] = $param->getDefaultValue();
                             } else {
                                 if ($name == 'currentUser' || $name == 'user') {
                                     if ($user == null) {
                                         $missing[] = $name;
                                     } else {
                                         $argsMethod[] = $user;
                                     }
                                 } else {
                                     $missing[] = $name;
                                 }
                             }
                         }
                         if (Yii::$app->requestedParams === null) {
                             Yii::$app->requestedParams =& $params;
                         }
                         /** @var WampResult $result */
                         $result = call_user_func_array([$this, $methodName], $argsMethod);
                         $this->afterCall($methodName, $params, $result);
                         if ($result instanceof WampResult == false) {
                             return WampResult::failure(new WampException('Result not instance of WampResult class', 500, isset($methodName) ? $methodName : $name));
                         } else {
                             return $result;
                         }
                     }
                 } catch (WampException $ex) {
                     return WampResult::failure($ex);
                 } catch (\Exception $ex) {
                     //todo
                     return WampResult::failure(new WampException($ex->getMessage(), $ex->getCode(), isset($methodName) ? $methodName : $name, $ex));
                 }
                 return null;
             }
         }
         $this->ensureBehaviors();
         foreach ($this->_behaviors as $object) {
             if ($object->hasMethod($name)) {
                 return call_user_func_array([$object, $name], $params);
             }
         }
         throw new UnknownMethodException('Calling unknown method: ' . get_class($this) . "::{$name}()");
     } catch (\Exception $ex) {
         echo PHP_EOL . $ex->getFile() . ':' . $ex->getLine() . ' - ' . $ex->getMessage() . PHP_EOL;
         \Yii::getLogger()->log($ex, Logger::LEVEL_ERROR, 'wamp-server');
     }
     return null;
 }
Ejemplo n.º 2
0
 public function onAuth($args, $kwArgs, $options)
 {
     $sessionId = $kwArgs->sessionId;
     $authToken = $kwArgs->authToken;
     $uid = (int) $kwArgs->uid;
     /** @var ActiveRecord $userClass */
     $userClass = \Yii::$app->user->identityClass;
     /** @var $user ActiveRecord|IdentityInterface|WampUserTrait */
     $user = $userClass::findOne($uid);
     if ($user == null) {
         return ['error' => 'user is null'];
     }
     if ($user->wampGetAuthToken() != $authToken) {
         //            VarDumper::dump([$user->wampGetAuthToken(), $authToken, $sessionId, $user->toArray()]);
         return ['error' => 'authToken'];
     }
     $token = $user->wampGenerateToken($sessionId);
     if ($token) {
         $session = new Session($sessionId, $token, $user->id);
         $session->saveSession();
     }
     //        VarDumper::dump([$kwArgs, $token, $user->toArray()]);
     return ['token' => $token];
 }