Example #1
0
 public function put()
 {
     if (parent::put() != false) {
         try {
             $xml = new SimpleXMLElement($this->getData());
         } catch (Exception $e) {
             $this->_error = RestErrorEnum::RE_INVALID_REPRESENTATION;
             return false;
         }
         $xmli = $xml->xpath('//appdb:request');
         if (count($xmli) === 0) {
             $this->_error = RestErrorEnum::RE_INVALID_REPRESENTATION;
             $this->_extError = "No request element provided";
             return false;
         }
         $apiroutes = new SimpleXMLElement(APPLICATION_PATH . "/apiroutes.xml", 0, true);
         $ret = array();
         foreach ($xmli as $x) {
             $routeXslt = null;
             $username = null;
             $userid = null;
             $passwd = null;
             $apikey = null;
             $sessionid = null;
             $src = null;
             $srv = null;
             $cid = null;
             if (trim($apikey) == '') {
                 $apikey = $this->getParam("apikey");
             }
             $method = strval($x->attributes()->method);
             switch (strtolower($method)) {
                 case "get":
                     $method = RestMethodEnum::RM_GET;
                     break;
                 case "put":
                     $method = RestMethodEnum::RM_PUT;
                     break;
                 case "post":
                     $method = RestMethodEnum::RM_POST;
                     break;
                 case "delete":
                     $method = RestMethodEnum::RM_DELETE;
                     break;
                 case "options":
                     $method = RestMethodEnum::RM_OPTIONS;
                     break;
                 default:
                     $method = false;
                     break;
             }
             if ($method === false) {
                 $this->_error = RestErrorEnum::RE_INVALID_REPRESENTATION;
                 $this->_extError = "Invalid value in request `method' attribute";
                 return false;
             }
             $reqID = strval($x->attributes()->id);
             if ($reqID == "") {
                 $this->_error = RestErrorEnum::RE_INVALID_REPRESENTATION;
                 $this->_extError = "Missing request `id' attribute";
                 return false;
             }
             $username = strval($x->attributes()->username);
             if (trim($username) == '') {
                 $username = $this->getParam("username");
             }
             $userid = strval($x->attributes()->userid);
             if (trim($userid) == '') {
                 $userid = $this->getParam("userid");
             }
             $passwd = strval($x->attributes()->passwd);
             if (trim($passwd) == '') {
                 $passwd = $this->getParam("passwd");
             }
             $apikey = strval($x->attributes()->apikey);
             if (trim($apikey) == '') {
                 $apikey = $this->getParam("apikey");
             }
             $sessionid = $this->getParam("sessionid");
             $src = $this->getParam("src");
             $cid = $this->getParam("cid");
             $srv = $this->getParam("remoteaddr");
             $res = strval($x->attributes()->resource);
             if ($res != '') {
                 if (substr($res, 0, 1) == "/") {
                     $res = substr($res, 1);
                 }
                 $pars = array();
                 $rx = RestBroker::matchResource($res, $apiroutes, $pars);
                 if (!is_null($rx)) {
                     try {
                         $resclass = strval($rx->resource);
                         if ($username != '') {
                             $pars["username"] = $username;
                         }
                         if ($userid != '') {
                             $pars["userid"] = $userid;
                         }
                         if ($passwd != '') {
                             $pars["passwd"] = $passwd;
                         }
                         if ($apikey != '') {
                             $pars["apikey"] = $apikey;
                         }
                         if ($sessionid != '') {
                             $pars["sessionid"] = $sessionid;
                         }
                         if ($src != '') {
                             $pars["src"] = $src;
                         }
                         if ($cid != '') {
                             $pars["cid"] = $cid;
                         }
                         if ($srv != '') {
                             $pars["remoteaddr"] = $srv;
                         }
                         $xparams = $x->xpath("appdb:param");
                         foreach ($xparams as $xparam) {
                             $pname = strval($xparam->attributes()->name);
                             if ($pname != '') {
                                 $pars[$pname] = strval($xparam);
                             } else {
                                 $this->_error = RestErrorEnum::RE_INVALID_REPRESENTATION;
                                 $this->_extError = "Missing `name' attribute in `param' element for request `" . $reqID . "'";
                                 return false;
                             }
                         }
                         $res = new $resclass($pars);
                         $fmt = $rx->xpath("format");
                         if (count($fmt) > 0) {
                             foreach ($fmt as $f) {
                                 if (strval($f) === "xml") {
                                     if (strval($f->attributes()->xslt) != '') {
                                         $routeXslt = strval($f->attributes()->xslt);
                                     }
                                     break;
                                 }
                             }
                         }
                     } catch (Exception $e) {
                         $this->_error = RestErrorEnum::RE_INVALID_REPRESENTATION;
                         $this->_extError = "Error initializing resource specified for request `" . $reqID . "'";
                         return false;
                     }
                 } else {
                     $this->_error = RestErrorEnum::RE_INVALID_REPRESENTATION;
                     $this->_extError = "Invalid resource specified for request `" . $reqID . "'";
                     return false;
                 }
             } else {
                 $this->_error = RestErrorEnum::RE_INVALID_REPRESENTATION;
                 $this->_extError = "No resource of empty resource specified for request `" . $reqID . "'";
                 return false;
             }
             $s_method = strtolower(RestMethodEnum::toString($method));
             $_res = $res->{$s_method}();
             if ($_res !== false) {
                 if ($_res->isFragment()) {
                     $res = $_res->finalize();
                 } else {
                     $res = $_res;
                 }
             } else {
                 $this->_error = $res->_error;
                 $this->_extError = $res->_extError;
                 return false;
             }
             if (!is_null($routeXslt)) {
                 $res = $res->transform(RestAPIHelper::getFolder(RestFolderEnum::FE_XSL_FOLDER) . $routeXslt);
             }
             $ret[] = '<appdb:reply id="' . $reqID . '">' . "\n" . $res . "\n" . '</appdb:reply>';
         }
         $ret = new XMLRestResponse($ret, $this);
         $ret = '<appdb:broker ' . implode(" ", RestAPIHelper::namespaces()) . ' ' . '>' . "\n" . $ret . "\n" . '</appdb:broker>';
         return new XMLRestResponse($ret, $this);
     } else {
         return false;
     }
 }
Example #2
0
 public static function responseHead($datatype, $type = null, $count = null, $pageLength = null, $pageOffset = null, $error = null, $exterror = null, $reqTime = null)
 {
     $resTime = microtime(true);
     if (is_null($type)) {
         $type = "entry";
     }
     db()->setFetchMode(Zend_Db::FETCH_NUM);
     try {
         $cacheState = db()->query("SELECT data FROM config WHERE var = 'cache_build_count';")->fetchAll();
         $cacheState = $cacheState[0];
         $cacheState = $cacheState[0];
     } catch (Exception $e) {
         /*do nothing*/
     }
     try {
         $permsState = db()->query("SELECT data FROM config WHERE var = 'permissions_cache_dirty';")->fetchAll();
         $permsState = $permsState[0];
         $permsState = $permsState[0];
     } catch (Exception $e) {
         /*do nothing*/
     }
     return '<appdb:appdb ' . implode(" ", RestAPIHelper::namespaces()) . ' ' . 'datatype="' . $datatype . '" ' . 'type="' . $type . '" ' . ($type === "list" ? 'count="' . ($count === null ? 0 : $count) . '" ' : '') . ($type === "list" && !is_null($pageLength) ? 'pagelength="' . $pageLength . '" ' : '') . ($type === "list" && !is_null($pageOffset) ? 'pageoffset="' . $pageOffset . '" ' : '') . ($error != "" ? 'error="' . RestErrorEnum::toString($error) . '" ' : '') . ($error != "" ? 'errornum="' . $error . '" ' : '') . ($exterror != "" ? 'errordesc="' . htmlentities($exterror, ENT_QUOTES | ENT_XML1 | ENT_DISALLOWED, "UTF-8") . '" ' : '') . 'host="' . $_SERVER['APPLICATION_UI_HOSTNAME'] . '" ' . 'apihost="' . $_SERVER['APPLICATION_API_HOSTNAME'] . '" ' . 'cacheState="' . $cacheState . '" ' . 'permsState="' . $permsState . '" ' . 'requestedOn="' . sprintf("%.3f", $reqTime) . '" ' . 'deliveredOn="' . sprintf("%.3f", $resTime) . '" ' . 'processingTime="' . sprintf("%.3f", $resTime - $reqTime) . '" ' . 'version="' . RestAPIHelper::VERSION . '" >';
 }