Esempio n. 1
0
 public function callActionMethod($method)
 {
     if ($this->request->getRequestType() == Scalr_UI_Request::REQUEST_TYPE_API) {
         $apiMethodCheck = false;
         if (method_exists($this, 'getApiDefinitions')) {
             $api = $this::getApiDefinitions();
             $m = str_replace('Action', '', $method);
             if (in_array($m, $api)) {
                 $apiMethodCheck = true;
             }
         }
         if (!$apiMethodCheck) {
             throw new Scalr_UI_Exception_NotFound();
         }
     }
     /*
      * Debug action section
      * Controller::Action => array of filter's params (accountId, userId) or true
      */
     $debug = false;
     $debugMode = false;
     $key = get_class($this) . '::' . $method;
     if ($debug && array_key_exists($key, $debug)) {
         $value = $debug[$key];
         if (is_array($value) && $this->user) {
             if (isset($value['accountId'])) {
                 if (is_array($value['accountId']) && in_array($this->user->getAccountId(), $value['accountId'])) {
                     $debugMode = true;
                 }
                 if (is_numeric($value['accountId']) && $value['accountId'] == $this->user->getAccountId()) {
                     $debugMode = true;
                 }
             }
             if (isset($value['userId'])) {
                 if (is_array($value['userId']) && in_array($this->user->getId(), $value['userId'])) {
                     $debugMode = true;
                 }
                 if (is_numeric($value['userId']) && $value['userId'] == $this->user->getId()) {
                     $debugMode = true;
                 }
             }
         } else {
             $debugMode = true;
         }
     }
     if ($debugMode) {
         $this->response->debugLog('Server', $_SERVER);
         $this->response->debugLog('Request', $_REQUEST);
         $this->response->debugLog('Session', Scalr_Session::getInstance());
     }
     $this->{$method}();
     if ($debugMode) {
         if ($this->response->jsResponseFlag) {
             $this->response->debugLog('JS Response', $this->response->jsResponse);
         }
         try {
             $message = '';
             foreach ($this->response->serverDebugLog as $value) {
                 $message .= $value['key'] . ":\n" . $value['value'] . "\n\n";
             }
             $this->db->Execute('INSERT INTO ui_debug_log (ipaddress, url, report, env_id, account_id, user_id) VALUES(?, ?, ?, ?, ?, ?)', array($this->request->getClientIp(), $key, $message, $this->getEnvironment() ? $this->getEnvironmentId() : 0, $this->user ? $this->user->getAccountId() : 0, $this->user ? $this->user->getId() : 0));
         } catch (Exception $e) {
         }
     }
 }
Esempio n. 2
0
 public function callActionMethod($method)
 {
     if ($this->request->getRequestType() == Scalr_UI_Request::REQUEST_TYPE_API) {
         $apiMethodCheck = false;
         if (method_exists($this, 'getApiDefinitions')) {
             $api = $this::getApiDefinitions();
             $m = str_replace('Action', '', $method);
             if (in_array($m, $api)) {
                 $apiMethodCheck = true;
             }
         }
         if (!$apiMethodCheck) {
             throw new Scalr_UI_Exception_NotFound();
         }
     }
     /*
      * Debug action section
      * Controller::Action => array of filter's params (accountId, userId) or true
      */
     $debug = false;
     $debugMode = false;
     $key = get_class($this) . '::' . $method;
     if ($debug && array_key_exists($key, $debug)) {
         $value = $debug[$key];
         if (is_array($value) && $this->user) {
             if (isset($value['accountId'])) {
                 if (is_array($value['accountId']) && in_array($this->user->getAccountId(), $value['accountId'])) {
                     $debugMode = true;
                 }
                 if (is_numeric($value['accountId']) && $value['accountId'] == $this->user->getAccountId()) {
                     $debugMode = true;
                 }
             }
             if (isset($value['userId'])) {
                 if (is_array($value['userId']) && in_array($this->user->getId(), $value['userId'])) {
                     $debugMode = true;
                 }
                 if (is_numeric($value['userId']) && $value['userId'] == $this->user->getId()) {
                     $debugMode = true;
                 }
             }
         } else {
             $debugMode = true;
         }
     }
     if ($debugMode) {
         $this->response->debugLog('Server', $_SERVER);
         $this->response->debugLog('Request', $_REQUEST);
         $this->response->debugLog('Session', Scalr_Session::getInstance());
     }
     $reflection = new ReflectionMethod($this, $method);
     if ($reflection->getNumberOfParameters()) {
         $params = array();
         $comment = $reflection->getDocComment();
         $matches = array();
         $types = array();
         if (preg_match_all('/^\\s+\\*\\s+@param\\s+(.*)\\s+\\$([A-Za-z0-9_]+)*.*$/m', $comment, $matches)) {
             for ($i = 0; $i < count($matches[0]); $i++) {
                 $matches[1][$i] = strtolower(trim($matches[1][$i]));
                 if (in_array($matches[1][$i], array('bool', 'boolean', 'int', 'integer', 'float', 'string', 'array'))) {
                     $types[trim($matches[2][$i])] = $matches[1][$i];
                 }
             }
         }
         // TODO: else: make some warning to log, otherwise we don't know when type-casting is not working
         foreach ($reflection->getParameters() as $parameter) {
             $className = $parameter->getClass() ? $parameter->getClass()->name : NULL;
             $value = $this->request->getRequestParam($parameter->name);
             $hasValue = $this->request->hasParam($parameter->name);
             if ($className) {
                 if (is_subclass_of($className, 'Scalr\\UI\\Request\\ObjectInitializingInterface')) {
                     /* @var ObjectInitializingInterface $className */
                     $params[] = $className::initFromRequest($className == 'Scalr\\UI\\Request\\FileUploadData' ? $this->request->getFileName($parameter->name) : $value);
                 } else {
                     throw new Scalr\Exception\Http\BadRequestException(sprintf('%s is invalid class in argument', $className));
                 }
             } else {
                 $type = $types[$parameter->name] ? $types[$parameter->name] : 'string';
                 if ($hasValue) {
                     if (in_array($type, ['bool', 'boolean'])) {
                         if (is_numeric($value)) {
                             $value = !empty($value);
                         } else {
                             if (is_string($value)) {
                                 $value = $value !== '' && strtolower($value) !== 'false';
                             } else {
                                 $value = (bool) $value;
                             }
                         }
                     } else {
                         if ($type == 'array') {
                             // do not strip value
                             settype($value, $type);
                         } else {
                             $value = $this->request->stripValue($value);
                             settype($value, $type);
                         }
                     }
                 } else {
                     if ($parameter->isDefaultValueAvailable()) {
                         $value = $parameter->getDefaultValue();
                     } else {
                         throw new Exception(sprintf('Missing required argument: %s', $parameter->name));
                     }
                 }
                 $params[] = $value;
             }
         }
         call_user_func_array(array($this, $method), $params);
     } else {
         $this->{$method}();
     }
     if ($debugMode) {
         if ($this->response->jsResponseFlag) {
             $this->response->debugLog('JS Response', $this->response->jsResponse);
         }
         try {
             $message = '';
             foreach ($this->response->serverDebugLog as $value) {
                 $message .= $value['key'] . ":\n" . $value['value'] . "\n\n";
             }
             $this->db->Execute('INSERT INTO ui_debug_log (ipaddress, url, report, env_id, account_id, user_id) VALUES(?, ?, ?, ?, ?, ?)', array($this->request->getClientIp(), $key, $message, $this->getEnvironment() ? $this->getEnvironmentId() : 0, $this->user ? $this->user->getAccountId() : 0, $this->user ? $this->user->getId() : 0));
         } catch (Exception $e) {
         }
     }
 }