Example #1
0
 public function get()
 {
     if (parent::get() !== false) {
         $s = file_get_contents(APPLICATION_PATH . "/apiroutes.xml");
         $s = str_replace("/:version/", "/" . RestAPIHelper::VERSION . "/", $s);
         $s = str_replace("\t", "", $s);
         $s = preg_replace('/>\\s+/', '>', $s);
         $s = preg_replace('/<\\s+/', '<', $s);
         $xslt = RestAPIHelper::getFolder(RestFolderEnum::FE_XSL_FOLDER) . "/apiroutes.xsl";
         $xsl = new DOMDocument();
         $xsl->load($xslt);
         $xml = new DOMDocument();
         $xml->loadXML($s, LIBXML_NSCLEAN | LIBXML_COMPACT);
         $proc = new XSLTProcessor();
         $proc->registerPHPFunctions();
         $proc->importStylesheet($xsl);
         $s = $proc->transformToXml($xml);
         $xml = new SimpleXMLElement($s);
         $xml->registerXPathNamespace('appdb', 'http://appdb.egi.eu/api/' . RestAPIHelper::VERSION . '/appdb');
         $x = $xml->xpath("//appdb:resource");
         $s = array();
         while (list(, $node) = each($x)) {
             $s[] = trim($node->asXML());
         }
         $this->_pageOffset = 0;
         $this->_pageLength = count($s);
         $this->_total = count($s);
         $s = RestAPIHelper::wrapResponse($s, "resource", "list", count($s));
         return new XMLRestResponse($s, $this);
     } else {
         return false;
     }
 }
Example #2
0
 private function getRecord($id, $prefix)
 {
     if ($id == "" || $prefix == "") {
         return $this->getError("badArgument", "The request is missing required arguments");
     } else {
         if ($prefix == "oa_dc") {
             if (substr($id, 0, 17) == "oai:appdb.egi.eu:") {
                 $item = substr($id, 17);
                 while (substr($item, 0, 1) == "/") {
                     $item = substr($item, 1);
                 }
                 $items = explode("/", $item);
                 $resource = $items[0];
                 $itemid = $items[1];
                 switch ($resource) {
                     case "applications":
                         $res = new RestAppItem(array("id" => $itemid));
                         break;
                     case "people":
                         $res = new RestPplItem(array("id" => $itemid));
                         break;
                     default:
                         return $this->buildResponse($this->getError("badArgument", "Requested invalid resource"), "GetRecord", $prefix);
                 }
                 debug_log("[OaiController::getRecord]: Getting " . "http://" . $_SERVER["APPLICATION_API_HOSTNAME"] . "/rest/latest/{$item}");
                 $res = strval($res->get());
                 $res = $this->buildResponse($res, "GetRecord", $prefix);
                 $xf = RestAPIHelper::getFolder(RestFolderEnum::FE_XSL_FOLDER) . "oai-applications.xsl";
                 $xsl = new DOMDocument();
                 $xsl->load($xf);
                 $proc = new XSLTProcessor();
                 $proc->registerPHPFunctions();
                 $proc->importStylesheet($xsl);
                 $xml = new DOMDocument();
                 $xml->loadXML($res, LIBXML_NSCLEAN | LIBXML_COMPACT);
                 $xml = $proc->transformToXml($xml);
                 return $xml;
             } else {
                 return $this->buildResponse($this->getError("idDoesNotExist", "Item not found"));
             }
         } else {
             return $this->buildResponse($this->getError("cannotDisseminateFormat", "The metadata format identified by the value given for the metadataPrefix argument is not supported by the item or by the repository."));
         }
     }
 }
Example #3
0
 /**
  * realization of get() from iRestResource. If authorization succeeds, then 
  * sets the GET model. Calls accessDenied() and returns FALSE otherwise.
  */
 public function get()
 {
     $this->_list = false;
     $this->_method = RestMethodEnum::RM_GET;
     if ($this->authorize(RestMethodEnum::RM_GET)) {
         $cachefile = RestAPIHelper::getFolder(RestFolderEnum::FE_CACHE_FOLDER) . '/query_' . get_class($this) . '_' . md5(var_export($this->_pars, true)) . '.xml';
         //debug_log("checking API cache file '" . $cachefile . "'");
         if (file_exists($cachefile) && $this->isCacheable()) {
             $cache = file_get_contents($cachefile);
             // invalidate cache if its life span has been exeeced
             $xml = new SimpleXMLElement($cache);
             $maxcachelife = 0;
             foreach ($xml->xpath('//appdb:appdb') as $x) {
                 $cachetime = strval($x->attributes()->cached);
                 if (is_numeric($cachetime)) {
                     $cachelife = time() - $cachetime;
                     if ($cachelife > $maxcachelife) {
                         $maxcachelife = $cachelife;
                     }
                 }
             }
             if ($maxcachelife > $this->getCacheLife()) {
                 // unlink cache file and perform proper query
                 @unlink($cachefile);
                 $this->_model = $this->getModel();
                 return new XMLFragmentRestResponse("", $this);
             } else {
                 // serve existing cache
                 //debug_log("serving cached data");
                 // TODO: remove this code block, and add cache hooks to the RestResource interface which will properly implement
                 // needed actions in subclasses
                 if (get_class($this) == "RestAppItem" || get_class($this) == "RestPplItem" || get_class($this) == "RestBroker") {
                     $xml = new SimpleXMLElement($cache);
                     foreach ($xml->xpath('//appdb:appdb') as $x) {
                         $cachetime = strval($x->attributes()->cached);
                         if (is_numeric($cachetime)) {
                             if (time() - $cachetime > $this->getCacheLife()) {
                                 // TODO: read min cache time from config (do not hardcode to 1min)
                                 $x->attributes()->cached = time();
                                 foreach ($x->xpath('//application:application|person:person') as $y) {
                                     $hitCount = strval($y->attributes()->hitcount);
                                     if (is_numeric($hitCount)) {
                                         $y->attributes()->hitcount = $hitCount + 1;
                                     }
                                 }
                             }
                         }
                     }
                     $cache = $xml->asXML();
                     $f = fopen($cachefile, "w");
                     fwrite($f, $cache);
                     fclose($f);
                 }
                 return new XMLRestResponse($cache, $this);
             }
         } else {
             $this->_model = $this->getModel();
             return new XMLFragmentRestResponse("", $this);
         }
     } else {
         return $this->accessDenied();
     }
 }
Example #4
0
 private function gridops_is_down()
 {
     @exec(APPLICATION_PATH . "/../bin/gridops_down");
     if (file_exists(RestAPIHelper::getFolder(RestFolderEnum::FE_CACHE_FOLDER) . "/gridops_downtime")) {
         return true;
     } else {
         return false;
     }
 }
Example #5
0
 public function hidePrivateData($data)
 {
     return $data;
     $xf = RestAPIHelper::getFolder(RestFolderEnum::FE_XSL_FOLDER) . 'virtualization.private.xsl';
     $xsl = new DOMDocument();
     $xsl->load($xf);
     $proc = new XSLTProcessor();
     $proc->registerPHPFunctions();
     $proc->importStylesheet($xsl);
     $xml = new DOMDocument();
     $xml->loadXML($data, LIBXML_NSCLEAN | LIBXML_COMPACT);
     $res = $proc->transformToXml($xml);
     return $res;
 }
Example #6
0
 public function newproxy()
 {
     $apiroutes = new SimpleXMLElement(APPLICATION_PATH . "/apiroutes.xml", 0, true);
     $pars = array();
     $postdata = null;
     $method = strtolower($this->getRequest()->getMethod());
     $error = null;
     $extError = null;
     if ($method === "post") {
         $postdata = $_POST['data'];
         if (isset($_POST['resource']) && trim($_POST['resource']) === "broker") {
             if ($this->session->isLocked()) {
                 $this->session->unLock();
             }
             session_write_close();
             $res = $_POST['resource'];
         } else {
             $res = $this->_getParam("resource");
         }
     } else {
         $res = $this->_getParam("resource");
     }
     $url = preg_replace('/\\?.*/', '', $res);
     $qs = explode("&", preg_replace('/.*\\?/', '', $res));
     $rx = RestBroker::matchResource($url, $apiroutes, $pars);
     if (is_null($rx)) {
         // FIXME: workaround for erroneous proxy resource notation (double URL-encoded)
         // FIXME: should be fixed at the source
         $res = urldecode($res);
         $url = preg_replace('/\\?.*/', '', $res);
         $qs = explode("&", preg_replace('/.*\\?/', '', $res));
         $rx = RestBroker::matchResource($url, $apiroutes, $pars);
         if (!is_null($rx)) {
             // FIXME: workaround for erroneous people canonical URLs with query strings
             if ($rx->resource == "RestPplItem" && ($method = "get")) {
                 $qs = null;
             }
         }
     } else {
         // FIXME: workaround for erroneous people canonical URLs with query strings
         if ($rx->resource == "RestPplItem" && ($method = "get")) {
             $qs = null;
         }
     }
     if (is_array($qs)) {
         foreach ($qs as $q) {
             $i = explode("=", $q);
             if (count($i) > 1) {
                 $pars[$i[0]] = urldecode($i[1]);
             }
         }
     }
     if (!is_null($postdata)) {
         $pars['data'] = $postdata;
     }
     $routeXslt = null;
     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 = RestMethodEnum::RM_GET;
             break;
     }
     $ret = "";
     if (!is_null($rx)) {
         try {
             $resclass = strval($rx->resource);
             $this->session = new Zend_Session_Namespace('default');
             if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] != '') {
                 $src = base64_encode($_SERVER['REMOTE_ADDR']);
             } else {
                 $src = '';
             }
             $pars['src'] = $src;
             if (isset($_SERVER['SERVER_ADDR']) && $_SERVER['SERVER_ADDR'] != '') {
                 $srv = base64_encode($_SERVER['SERVER_ADDR']);
             } else {
                 $srv = '';
             }
             $pars['remoteaddr'] = $srv;
             $apikey = $userid = $passwd = '';
             if ($this->session->userid !== null) {
                 $userid = $this->session->userid;
                 if (isset($_COOKIE['SimpleSAMLAuthToken'])) {
                     $passwd = $_COOKIE['SimpleSAMLAuthToken'];
                 } else {
                     error_log("Warning: auth token cookie ('SimpleSAMLAuthToken') is undefined!");
                 }
                 $apiconf = Zend_Registry::get("api");
                 $apikey = $apiconf["key"];
             }
             $pars['userid'] = $userid;
             $pars['passwd'] = $passwd;
             $pars['apikey'] = $apikey;
             $pars['sessionid'] = session_id();
             $pars['cid'] = 0;
             if ($userid != '') {
                 $_GET['userid'] = $userid;
             }
             $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) {
             $error = RestErrorEnum::toString(RestErrorEnum::RE_INVALID_REPRESENTATION);
             $extError = "Could not instantiate REST resource for request `" . $res . "'";
             $this->getResponse()->clearAllHeaders();
             $this->getResponse()->setRawHeader("HTTP/1.0 400 Bad Request");
             $this->getResponse()->setHeader("Status", "400 Bad Request");
             if ($extError != "") {
                 error_log($error . '\\n' . $extError);
                 echo $error . '\\n' . $extError;
             } else {
                 error_log($error);
                 echo $error;
             }
             return;
         }
     } else {
         $error = RestErrorEnum::toString(RestErrorEnum::RE_INVALID_REPRESENTATION);
         $extError = "Could not resolve REST resource for request `" . $res . "'";
         $this->getResponse()->clearAllHeaders();
         $this->getResponse()->setRawHeader("HTTP/1.0 400 Bad Request");
         $this->getResponse()->setHeader("Status", "400 Bad Request");
         if ($extError != "") {
             error_log($error . '\\n' . $extError);
             echo $error . '\\n' . $extError;
         } else {
             error_log($error);
             echo $error;
         }
         return;
     }
     $s_method = strtolower(RestMethodEnum::toString($method));
     $_res = $res->{$s_method}();
     if ($_res !== false) {
         if ($_res->isFragment()) {
             $res = $_res->finalize();
         } else {
             $res = $_res;
         }
         if (!is_null($routeXslt)) {
             $res = $res->transform(RestAPIHelper::getFolder(RestFolderEnum::FE_XSL_FOLDER) . $routeXslt);
         }
         echo $res;
     } else {
         $error = RestErrorEnum::toString($res->getError());
         $extError = $res->getExtError();
         $this->getResponse()->clearAllHeaders();
         $this->getResponse()->setRawHeader("HTTP/1.0 400 Bad Request");
         $this->getResponse()->setHeader("Status", "400 Bad Request");
         if ($extError != "") {
             error_log($error . '\\n' . $extError);
             echo $error . '\\n' . $extError;
         } else {
             error_log($error);
             echo $error;
         }
     }
 }