Пример #1
0
 function testDefault()
 {
     $filter = new DeclFilter();
     $filter->addStaticKeyFilters(array('hello' => 'digits'));
     $filter->addCatchAllFilter('alpha');
     $data = $filter->filter(array('hello' => '123abc', 'world' => '123abc'));
     $this->assertEquals($data['world'], 'abc');
     $this->assertEquals($data['hello'], '123');
 }
Пример #2
0
 function performRequest($url, $postBody = null)
 {
     $cachelib = TikiLib::lib('cache');
     $tikilib = TikiLib::lib('tiki');
     if ($cache = $cachelib->getSerialized($url . $postBody)) {
         if (time() < $cache['expires']) {
             return $cache['data'];
         }
         $cachelib->invalidate($url . $postBody);
     }
     $client = $tikilib->get_http_client($url);
     $method = null;
     if (empty($postBody)) {
         $method = 'GET';
         $client->setHeaders(array('Accept' => 'application/json,text/x-yaml', 'OIntegrate-Version' => '1.0'));
     } else {
         $client->setHeaders(array('Accept' => 'application/json,text/x-yaml', 'OIntegrate-Version' => '1.0'));
         $client->setRawData($postBody, 'application/x-www-form-urlencoded');
     }
     if (count($this->schemaVersion)) {
         $client->setHeaders('OIntegrate-SchemaVersion', implode(', ', $this->schemaVersion));
     }
     if (count($this->acceptTemplates)) {
         $client->setHeaders('OIntegrate-AcceptTemplate', implode(', ', $this->acceptTemplates));
     }
     $httpResponse = $client->request($method);
     $content = $httpResponse->getBody();
     $contentType = $httpResponse->getHeader('Content-Type');
     $cacheControl = $httpResponse->getHeader('Cache-Control');
     $response = new OIntegrate_Response();
     $response->contentType = $contentType;
     $response->cacheControl = $cacheControl;
     $response->data = $this->unserialize($contentType, $content);
     $filter = new DeclFilter();
     $filter->addCatchAllFilter('xss');
     $response->data = $filter->filter($response->data);
     $response->version = $httpResponse->getHeader('OIntegrate-Version');
     $response->schemaVersion = $httpResponse->getHeader('OIntegrate-SchemaVersion');
     if (!$response->schemaVersion && isset($response->data->_version)) {
         $response->schemaVersion = $response->data->_version;
     }
     $response->schemaDocumentation = $httpResponse->getHeader('OIntegrate-SchemaDocumentation');
     global $prefs;
     // Respect cache duration asked for
     if (preg_match('/max-age=(\\d+)/', $cacheControl, $parts)) {
         $expiry = time() + $parts[1];
         $cachelib->cacheItem($url, serialize(array('expires' => $expiry, 'data' => $response)));
         // Unless service specifies not to cache result, apply a default cache
     } elseif (false !== strpos($cacheControl, 'no-cache') && $prefs['webservice_consume_defaultcache'] > 0) {
         $expiry = time() + $prefs['webservice_consume_defaultcache'];
         $cachelib->cacheItem($url, serialize(array('expires' => $expiry, 'data' => $response)));
     }
     return $response;
 }