Exemple #1
0
 public static function logout($sessionID)
 {
     self::loadConfig();
     $url = self::$config['syncURLPrefix'] . "logout";
     $response = HTTP::post($url, array("version" => self::$config['apiVersion'], "sessionid" => $sessionID));
     self::checkResponse($response);
     $xml = new SimpleXMLElement($response->getBody());
     if (!$xml->loggedout) {
         throw new Exception("Error logging out");
     }
 }
Exemple #2
0
 public function __call($method, $args)
 {
     $url = (defined('PHPFOX_API_URL') ? PHPFOX_API_URL : 'http://api.phpfox.com/') . $method;
     $Http = new HTTP($url);
     $Http->auth($this->_id, $this->_key);
     if (\Phpfox::isTrial()) {
         $Http->header('PHPFOX_IS_TRIAL', '1');
     }
     $Http->using(['domain' => \Phpfox::getParam('core.path')]);
     $Http->using(['version' => \Phpfox::VERSION]);
     foreach ($args as $key => $value) {
         if (is_string($value)) {
             // $value = [$key => $value];
         }
         $Http->using($value);
     }
     return $Http->post();
 }
Exemple #3
0
 public function testAddFileClientZip()
 {
     API::userClear(self::$config['userID']);
     $auth = array('username' => self::$config['username'], 'password' => self::$config['password']);
     // Get last storage sync
     $response = API::userGet(self::$config['userID'], "laststoragesync?auth=1", array(), $auth);
     $this->assert404($response);
     $xml = API::createItem("book", false, $this);
     $data = API::parseDataFromItemEntry($xml);
     $key = $data['key'];
     $fileContentType = "text/html";
     $fileCharset = "UTF-8";
     $fileFilename = "file.html";
     $fileModtime = time();
     $xml = API::createAttachmentItem("imported_url", $key, $this);
     $data = API::parseDataFromItemEntry($xml);
     $key = $data['key'];
     $etag = $data['etag'];
     $json = json_decode($data['content']);
     $json->contentType = $fileContentType;
     $json->charset = $fileCharset;
     $json->filename = $fileFilename;
     $response = API::userPut(self::$config['userID'], "items/{$key}?key=" . self::$config['apiKey'], json_encode($json), array("Content-Type: application/json", "If-Match: {$etag}"));
     $this->assert200($response);
     // Get file info
     $response = API::userGet(self::$config['userID'], "items/{$data['key']}/file?auth=1&iskey=1&version=1&info=1", array(), $auth);
     $this->assert404($response);
     $zip = new ZipArchive();
     $file = "work/{$key}.zip";
     if ($zip->open($file, ZIPARCHIVE::CREATE) !== TRUE) {
         throw new Exception("Cannot open ZIP file");
     }
     $zip->addFromString($fileFilename, self::getRandomUnicodeString());
     $zip->addFromString("file.css", self::getRandomUnicodeString());
     $zip->close();
     $hash = md5_file($file);
     $filename = $key . ".zip";
     $size = filesize($file);
     $fileContents = file_get_contents($file);
     // Get upload authorization
     $response = API::userPost(self::$config['userID'], "items/{$data['key']}/file?auth=1&iskey=1&version=1", $this->implodeParams(array("md5" => $hash, "filename" => $filename, "filesize" => $size, "mtime" => $fileModtime, "zip" => 1)), array("Content-Type: application/x-www-form-urlencoded"), $auth);
     $this->assert200($response);
     $this->assertContentType("application/xml", $response);
     $xml = new SimpleXMLElement($response->getBody());
     self::$toDelete[] = "{$hash}/{$filename}";
     $boundary = "---------------------------" . rand();
     $postData = "";
     foreach ($xml->params->children() as $key => $val) {
         $postData .= "--" . $boundary . "\r\nContent-Disposition: form-data; " . "name=\"{$key}\"\r\n\r\n{$val}\r\n";
     }
     $postData .= "--" . $boundary . "\r\nContent-Disposition: form-data; " . "name=\"file\"\r\n\r\n" . $fileContents . "\r\n";
     $postData .= "--" . $boundary . "--";
     // Upload to S3
     $response = HTTP::post((string) $xml->url, $postData, array("Content-Type: multipart/form-data; boundary=" . $boundary));
     $this->assert201($response);
     //
     // Register upload
     //
     $response = API::userPost(self::$config['userID'], "items/{$data['key']}/file?auth=1&iskey=1&version=1", "update=" . $xml->key . "&mtime=" . $fileModtime, array("Content-Type: application/x-www-form-urlencoded"), $auth);
     $this->assert204($response);
     // Verify attachment item metadata
     $response = API::userGet(self::$config['userID'], "items/{$data['key']}?key=" . self::$config['apiKey'] . "&content=json");
     $xml = API::getXMLFromResponse($response);
     $json = json_decode(array_shift($xml->xpath('/atom:entry/atom:content')));
     $this->assertEquals($hash, $json->md5);
     $this->assertEquals($fileFilename, $json->filename);
     $this->assertEquals($fileModtime, $json->mtime);
     $response = API::userGet(self::$config['userID'], "laststoragesync?auth=1", array(), array('username' => self::$config['username'], 'password' => self::$config['password']));
     $this->assert200($response);
     $mtime = $response->getBody();
     $this->assertRegExp('/^[0-9]{10}$/', $mtime);
     // File exists
     $response = API::userPost(self::$config['userID'], "items/{$data['key']}/file?auth=1&iskey=1&version=1", $this->implodeParams(array("md5" => $hash, "filename" => $filename, "filesize" => $size, "mtime" => $fileModtime + 1000, "zip" => 1)), array("Content-Type: application/x-www-form-urlencoded"), $auth);
     $this->assert200($response);
     $this->assertContentType("application/xml", $response);
     $this->assertEquals("<exists/>", $response->getBody());
 }
Exemple #4
0
 public static function post($url, $data, $headers = array(), $auth = false)
 {
     $url = self::$config['apiURLPrefix'] . $url;
     if (self::$apiVersion) {
         $headers[] = "Zotero-API-Version: " . self::$apiVersion;
     }
     if (!$auth && self::$apiKey) {
         $headers[] = "Authorization: Bearer " . self::$apiKey;
     }
     $response = HTTP::post($url, $data, $headers, $auth);
     return $response;
 }
 /**
  * Takes a photo ID, returns owner's NSID and path_alias
  * (the username which appears in the URL), if available.
  * @param string $flickrPhotoId
  * @return array an array containing the NSID first and the path_alias second. The path_alias
  *     is not guaranteed to exist, in which case the array will have a single item;
  *     if there is no such photo (or some other error happened), the array will be empty.
  */
 protected function getUserIdsFromPhotoId($flickrPhotoId)
 {
     $userIds = array();
     $params = array('postData' => array('method' => 'flickr.photos.getInfo', 'api_key' => $this->flickrApiKey, 'photo_id' => $flickrPhotoId, 'format' => 'json', 'nojsoncallback' => 1));
     $response = HTTP::post($this->flickrApiUrl, $params);
     if ($response !== false) {
         $response = json_decode($response, true);
     }
     if (isset($response['photo']['owner']['nsid'])) {
         $userIds[] = $response['photo']['owner']['nsid'];
     }
     // what Flickr calls 'username' can change at any time and so is worthless for blacklisting
     // path_alias is the username in the pretty URL; once set, it cannot be changed.
     if (isset($response['photo']['owner']['path_alias'])) {
         $userIds[] = $response['photo']['owner']['path_alias'];
     }
     return $userIds;
 }
 protected function _execute($data)
 {
     $request = new HTTP($this->_apiURL);
     $response = $request->post($data);
     return $this->_parseResponse($response);
 }
Exemple #7
0
require '../../model/Date.inc.php';
require '../../model/Utilities.inc.php';
class Z_Tests
{
    public static $AWS;
}
//
// Set up AWS service factory
//
$awsConfig = ['region' => $config['awsRegion']];
// IAM role authentication
if (empty($config['awsAccessKey'])) {
    $awsConfig['credentials.cache'] = new Guzzle\Cache\DoctrineCacheAdapter(new Doctrine\Common\Cache\FilesystemCache('work/cache'));
} else {
    $awsConfig['key'] = $config['awsAccessKey'];
    $awsConfig['secret'] = $config['awsSecretKey'];
}
Z_Tests::$AWS = \Aws\Common\Aws::factory($awsConfig);
unset($awsConfig);
// Wipe data and create API key
require_once 'http.inc.php';
$response = HTTP::post($config['apiURLPrefix'] . "test/setup?u=" . $config['userID'], " ", [], ["username" => $config['rootUsername'], "password" => $config['rootPassword']]);
$json = json_decode($response->getBody());
if (!$json) {
    echo $response->getBody();
    throw new Exception("Invalid test setup response");
}
$config['apiKey'] = $json->apiKey;
\Zotero\Tests\Config::update($config);
// Set up groups
require 'groups.inc.php';
Exemple #8
0
 public function testPOSTFileKV()
 {
     $res = HTTP::post('http://httpbin.org/post')->attach('asset', __DIR__ . '/files/cat.png')->send();
     $this->assertFalse($res->error());
     $this->assertEquals(200, $res->status());
     $this->assertEquals((object) array('asset' => 'data:image/jpeg;base64,' . base64_encode(file_get_contents(__DIR__ . '/files/cat.png'))), $res->body()->files);
 }
Exemple #9
0
 public static function post($url, $data, $headers = array(), $auth = false)
 {
     self::loadConfig();
     $url = self::$config['apiURLPrefix'] . $url;
     $response = HTTP::post($url, $data, $headers, $auth);
     return $response;
 }
Exemple #10
0
 private static function req($sessionID, $path, $params = array(), $gzip = false, $allowError = false)
 {
     $url = self::$config['syncURLPrefix'] . $path;
     $params = array_merge(array("sessionid" => $sessionID, "version" => self::$config['syncVersion']), $params ? $params : array());
     if ($gzip) {
         $data = "";
         foreach ($params as $key => $val) {
             $data .= $key . "=" . urlencode($val) . "&";
         }
         $data = gzdeflate(substr($data, 0, -1));
         $headers = ["Content-Type: application/octet-stream", "Content-Encoding: gzip"];
     } else {
         $data = $params;
         $headers = [];
     }
     if (!empty(self::$config['zoteroVersion'])) {
         $headers[] = "X-Zotero-Version: " . self::$config['zoteroVersion'];
     }
     $response = HTTP::post($url, $data, $headers);
     self::checkResponse($response, $allowError);
     return $response;
 }
Exemple #11
0
 private function req($path, $params = array(), $gzip = false)
 {
     $url = self::$config['syncURLPrefix'] . $path;
     $params = array_merge(array("sessionid" => self::$sessionID, "version" => self::$config['apiVersion']), $params);
     if ($gzip) {
         $data = "";
         foreach ($params as $key => $val) {
             $data .= $key . "=" . urlencode($val) . "&";
         }
         $data = gzdeflate(substr($data, 0, -1));
         $headers = array("Content-Type: application/octet-stream", "Content-Encoding: gzip");
     } else {
         $data = $params;
         $headers = array();
     }
     $response = HTTP::post($url, $data, $headers);
     Sync::checkResponse($response);
     return $response;
 }