示例#1
0
 /**
  * checks if the parameter passed in first argument exists in global arrays. if the second argument
  * is set will check only in global array with scope passed in this argument. e.g. "POST" will only
  * check in post array if parameter exists. if second parameter is not set will bubble through all
  * global variables until found in one of them or returns false if not found. the third parameter
  * activates the strict mode to check if value is a value
  *
  * @error 14412
  * @param string $name expects the parameter name
  * @param null|string $from expects optional scope value like "POST"
  * @param bool $strict expects boolean value whether to activate strict value check
  * @return bool
  */
 public function has($name, $from = null, $strict = false)
 {
     if ($from === null) {
         $from = true;
     }
     switch ($from) {
         case self::PARAMS:
             $return = xapp_array_isset($this->_params, $name, $strict);
             if ($return) {
                 return true;
             }
             if ($from !== true) {
                 break;
             }
         case self::RPC:
             if (isset($GLOBALS['_RPC'])) {
                 $return = (bool) $strict ? (bool) (array_key_exists($name, $GLOBALS['_RPC']) && xapp_is_value($GLOBALS['_RPC'][$name])) : array_key_exists($name, $GLOBALS['_RPC']);
             } else {
                 $return = false;
             }
             if ($return) {
                 return true;
             }
             if ($from !== true) {
                 break;
             }
         case self::POST:
             $return = (bool) $strict ? (bool) (array_key_exists($name, $_POST) && xapp_is_value($_POST[$name])) : array_key_exists($name, $_POST);
             if ($return) {
                 return true;
             }
             if ($from !== true) {
                 break;
             }
         case self::GET:
             $return = (bool) $strict ? (bool) (array_key_exists($name, $_GET) && xapp_is_value($_GET[$name])) : array_key_exists($name, $_GET);
             if ($return) {
                 return true;
             }
             if ($from !== true) {
                 break;
             }
         case self::COOKIE:
             $return = (bool) $strict ? (bool) (array_key_exists($name, $_COOKIE) && xapp_is_value($_COOKIE[$name])) : array_key_exists($name, $_COOKIE);
             if ($return) {
                 return true;
             }
             if ($from !== true) {
                 break;
             }
         case self::SERVER:
             $return = (bool) $strict ? (bool) (array_key_exists($name, $_SERVER) && xapp_is_value($_SERVER[$name])) : array_key_exists($name, $_SERVER);
             if ($return) {
                 return true;
             }
             if ($from !== true) {
                 break;
             }
         default:
             return false;
     }
     return false;
 }
示例#2
0
 /**
  * validate jsonp request testing for request parameter to be valid and checking all
  * additional parameters
  *
  * @error 14705
  * @param array $call expects the call to validate
  * @return void
  * @throws Xapp_Rpc_Fault
  */
 protected function validate($call)
 {
     $get = $this->request()->getGet();
     $service = null;
     if ($this->smd()->has($call[2], $call[1])) {
         if ($call[2] !== null) {
             $service = $this->smd()->get($call[2] . '.' . $call[1]);
         } else {
             $service = $this->smd()->get($call[1]);
         }
     } else {
         Xapp_Rpc_Fault::t("method or function is not registered as service", array(1470501, -32601));
     }
     if (!empty($service->parameters)) {
         foreach ($service->parameters as $k => $v) {
             if (!$v->optional && (!array_key_exists($v->name, $get) || !xapp_is_value($get[$v->name]))) {
                 Xapp_Rpc_Fault::t(xapp_sprintf("param: %s must be set", array($v->name)), array(1470503, -32602));
             }
             if (isset($v->type) && array_key_exists($v->name, $get) && !in_array('mixed', (array) $v->type) && !in_array(xapp_type($get[$v->name], true), (array) $v->type)) {
                 Xapp_Rpc_Fault::t(xapp_sprintf("param: %s must be of the following types: %s", array($v->name, implode('|', (array) $v->type))), array(1470504, -32602));
             }
         }
     }
     if (xapp_is_option(self::ADDITIONAL_PARAMETERS, $this)) {
         foreach (xapp_get_option(self::ADDITIONAL_PARAMETERS, $this) as $k => $v) {
             $type = isset($v[0]) ? (array) $v[0] : false;
             $optional = isset($v[1]) ? (bool) $v[1] : true;
             if (!$optional && !array_key_exists($k, $get)) {
                 Xapp_Rpc_Fault::t(xapp_sprintf("additional param: %s must be set", array($k)), array(1470505, -32602));
             }
             if ($type && !in_array('mixed', $type) && !in_array(xapp_type($get[$k], true), $type)) {
                 Xapp_Rpc_Fault::t(xapp_sprintf("additional param: %s must be of the following types: %s", array($k, implode('|', $type))), array(1470506, -32602));
             }
         }
     }
 }
示例#3
0
/**
 * check if key is set in array in strict mode or not. when in strict mode will
 * also check if the array keys value is not null, false, empty array or empty value.
 * if second parameter key is null will just check if first parameter array is an empty array or not.
 * key can be in dot notation e.g. config.database.user. in the case the first parameter
 * array will be iterate to find right dimension to check if key exits or not
 *
 * @param array $array expects array to check for key
 * @param null|mixed $key expects key
 * @param bool $strict
 * @return bool
 */
function xapp_array_isset(array $array, $key = null, $strict = false)
{
    if ($key === null) {
        return !empty($array) ? true : false;
    }
    if (array_key_exists($key, $array)) {
        if ((bool) $strict) {
            return xapp_is_value($array[$key]) ? true : false;
        } else {
            return true;
        }
    }
    foreach (explode('.', trim($key, '.')) as $k => $v) {
        if (!is_array($array) || !array_key_exists($v, $array)) {
            return false;
        }
        $array = $array[$v];
    }
    if ((bool) $strict) {
        return xapp_is_value($array) ? true : false;
    } else {
        return true;
    }
}
示例#4
0
 /**
  * validate json request object testing all request object parameters for validity. also checking all additional
  * parameters for validity and throwing fault if necessary
  *
  * @error 14604
  * @param array $call expects the call to validate
  * @return void
  * @throws Xapp_Rpc_Fault
  */
 protected function validate($call)
 {
     if (!xapp_get_option(self::VALIDATE, $this)) {
         return;
     }
     if ($this->request()->isPost()) {
         if ($this->request()->getRaw() === "") {
             Xapp_Rpc_Fault::t("empty or invalid request object", array(1460401, -32600));
         }
         if ($this->request()->getVersion($call[3]) != xapp_get_option(self::VERSION, $this)) {
             Xapp_Rpc_Fault::t("rpc version not set or version miss match", array(1460402, -32013));
         }
         if (!xapp_get_option(self::ALLOW_NOTIFICATIONS, $this) || array_key_exists('id', $call[3])) {
             if (!array_key_exists('id', $call[3])) {
                 Xapp_Rpc_Fault::t("rpc transaction id must be set", array(1460405, -32015));
             }
             if (!is_numeric($call[3]['id']) && !is_string($call[3]['id'])) {
                 Xapp_Rpc_Fault::t("rpc transaction id must be string or integer", array(1460406, -32016));
             }
             if (xapp_is_option(self::TRANSACTION_ID_REGEX, $this) && !preg_match(trim((string) xapp_get_option(self::TRANSACTION_ID_REGEX, $this)), $call[3]['id'])) {
                 Xapp_Rpc_Fault::t("rpc transaction id does not match transaction id regex pattern", array(1460411, -32017));
             }
         }
         if (!xapp_get_option(self::ALLOW_FUNCTIONS, $this) && empty($call[2])) {
             Xapp_Rpc_Fault::t("php functions as service are not supported by this rpc service", 1460412, -32018);
         }
         if (!array_key_exists('method', $call[3])) {
             Xapp_Rpc_Fault::t("rpc method must be set", array(1460403, -32014));
         }
         if (!$this->smd()->has($call[2], $call[1])) {
             Xapp_Rpc_Fault::t("method or function is not registered as service", array(1460404, -32601));
         }
         if (!is_null($call[2])) {
             $service = $this->smd()->get($call[2] . '.' . $call[1]);
         } else {
             $service = $this->smd()->get($call[1]);
         }
         if (!empty($service->parameters)) {
             $params = array_key_exists('params', $call[3]) ? $call[3]['params'] : null;
             $p = (array) $params;
             $k = is_null($params) || array_values($p) !== $p ? 'n' : 'i';
             $i = 0;
             foreach ($service->parameters as $v) {
                 $n = $v->name;
                 if (!$v->optional && (!array_key_exists(${$k}, $p) || !xapp_is_value($p[${$k}]))) {
                     Xapp_Rpc_Fault::t(xapp_sprintf("param: %s must be set", array(${$k})), array(1460407, -32602));
                 }
                 if (isset($v->type) && array_key_exists(${$k}, $p) && !in_array('mixed', (array) $v->type) && !in_array(xapp_type($p[${$k}]), (array) $v->type)) {
                     Xapp_Rpc_Fault::t(xapp_sprintf("param: %s must be of the following types: %s", array(${$k}, implode('|', (array) $v->type))), array(1460408, -32602));
                 }
                 $i++;
             }
         }
         if (xapp_is_option(self::ADDITIONAL_PARAMETERS, $this)) {
             foreach (xapp_get_option(self::ADDITIONAL_PARAMETERS, $this) as $k => $v) {
                 $type = isset($v[0]) ? (array) $v[0] : false;
                 $optional = isset($v[1]) ? (bool) $v[1] : true;
                 if (!$optional && !$this->request()->hasParam($k)) {
                     Xapp_Rpc_Fault::t(xapp_sprintf("additional param: %s must be set", array($k)), array(1460409, -32602));
                 }
                 if ($type && !in_array('mixed', $type) && !in_array(xapp_type($this->request()->getParam($k)), $type)) {
                     Xapp_Rpc_Fault::t(xapp_sprintf("additional param: %s must be of the following types: %s", array($k, implode('|', $type))), array(1460410, -32602));
                 }
             }
         }
     }
 }