public function translate($from, $to, $from_text)
 {
     if (!$from) {
         throw new MissingFromLanguageException();
     } elseif (!$to) {
         throw new MissingToLanguageException();
     } elseif (!$from_text) {
         throw new MissingTextLanguageException();
     }
     $from_text = urlencode($from_text);
     $url = $this->googleTranslateServiceUrl . "/translate_a/t?" . http_build_query($this->options) . "&text={$from_text}&hl={$from}&tl={$to}&uptl={$to}&alttl={$from}";
     $stream = new Stream(fopen($url, 'r'));
     $content = $stream;
     $tab = explode(",", $content);
     $new_tab = array();
     foreach ($tab as $elem) {
         if ($elem == '') {
             $new_tab[] = "\"\"";
         } else {
             $new_tab[] = $elem;
         }
     }
     $result = implode(",", $new_tab);
     $result = json_decode($result, true);
     if (!is_array($result) && count($result) == 0) {
         throw new TranslateServerIsDownException();
     }
     $stream->close();
     return array($result[0][0][0], $result[0][0][1]);
 }
 public function onBeforeSend(Event $event)
 {
     $request = $event['request'];
     if ('Guzzle\\Http\\Message\\EntityEnclosingRequest' === get_class($request)) {
         if (null === $request->getBody()) {
             $request->setBody('{}');
         }
         $hashedBody = $this->chunkedBase64Encode(Stream::getHash($request->getBody(), 'sha1', true));
     } else {
         $hashedBody = $this->sha1AndBase64Encode('');
     }
     $timestamp = gmdate("Y-m-d\\TH:i:s\\Z");
     $request->setHeader('Accept', 'application/json');
     $request->setHeader('Content-Type', 'application/json');
     $request->setHeader('X-Chef-Version', '0.10.8');
     $request->setHeader('X-Ops-Sign', 'version=1.0');
     $request->setHeader('X-Ops-Timestamp', $timestamp);
     $request->setHeader('X-Ops-Userid', $this->getClientName());
     $request->setHeader('X-Ops-Content-Hash', $hashedBody);
     $signature = $this->signRequest($request->getMethod(), $request->getPath(), $hashedBody, $timestamp);
     foreach (explode('\\n', $signature) as $i => $chunk) {
         $n = $i + 1;
         $request->setHeader("X-Ops-Authorization-{$n}", $chunk);
     }
 }
 /**
  * {@inheritdoc}
  */
 protected function getDelay($retries, RequestInterface $request, Response $response = null, HttpException $e = null)
 {
     if ($response) {
         // Validate the checksum against our computed checksum
         if ($checksum = (string) $response->getHeader('x-amz-crc32')) {
             // Retry the request if the checksums don't match, otherwise, return null
             return $checksum != hexdec(Stream::getHash($response->getBody(), 'crc32b')) ? true : null;
         }
     }
 }
示例#4
0
 public function rewind()
 {
     return $this->rewindFunction ? call_user_func($this->rewindFunction, $this) : parent::rewind();
 }
 /**
  * @covers Guzzle\Stream\Stream::rewind
  */
 public function testRewindIsSeekZero()
 {
     $stream = new Stream(fopen('php://temp', 'w+'));
     $stream->write('foobazbar');
     $this->assertTrue($stream->rewind());
     $this->assertEquals('foobazbar', $stream->read(9));
 }
 /**
  * {@inheritdoc}
  */
 public function getContentMd5($rawOutput = false, $base64Encode = false)
 {
     $hash = Stream::getHash($this, 'md5', $rawOutput);
     return $hash && $base64Encode ? base64_encode($hash) : $hash;
 }
示例#7
0
 /**
  * Get the payload part of a signature from a request.
  *
  * @param RequestInterface $request
  *
  * @return string
  */
 protected function getPayload(RequestInterface $request)
 {
     // Calculate the request signature payload
     if ($request->hasHeader('x-amz-content-sha256')) {
         // Handle streaming operations (e.g. Glacier.UploadArchive)
         return (string) $request->getHeader('x-amz-content-sha256');
     }
     if ($request instanceof EntityEnclosingRequestInterface) {
         if ($request->getMethod() == 'POST' && count($request->getPostFields())) {
             return hash('sha256', (string) $request->getPostFields());
         } elseif ($body = $request->getBody()) {
             return Stream::getHash($request->getBody(), 'sha256');
         }
     }
     return self::DEFAULT_PAYLOAD;
 }
 private function checkTransFile($mothed, $args)
 {
     if (ucfirst($mothed) === 'PutObject' || ucfirst($mothed) === 'UploadPart') {
         if (array_key_exists('SourceFile', $args[0]) && array_key_exists('Body', $args[0])) {
             throw new OnlyBodyOrFileException();
         }
         if (!array_key_exists('SourceFile', $args[0]) || array_key_exists('Body', $args[0])) {
             return false;
         }
         if (!array_key_exists('Bucket', $args[0]) || !array_key_exists('Key', $args[0])) {
             return false;
         }
         if (ucfirst($mothed) === 'UploadPart' && (!array_key_exists('PartNumber', $args[0]) || !array_key_exists('UploadId', $args[0]))) {
             return false;
         }
         if (array_key_exists('ContentLength', $args[0]) && (int) $args[0]['ContentLength'] <= 0) {
             return false;
         }
         if (!file_exists($args[0]['SourceFile'])) {
             return false;
         }
         if (ucfirst($mothed) === 'PutObject') {
             include_once 'Obs/S3/Model/MultipartUpload/SetObjectHead.php';
             $head = new \SetObjectHead();
             if ($head->getFileSize($args[0]['SourceFile']) > 524288000) {
                 return true;
             }
         }
         if (!array_key_exists('ContentLength', $args[0])) {
             include_once 'Obs/S3/Model/MultipartUpload/SetObjectHead.php';
             $head = new \SetObjectHead();
             $body = new Stream(fopen($args[0]['SourceFile'], 'rb'));
             if ($head->getFileSize($args[0]['SourceFile']) === $body->getSize()) {
                 return false;
             }
         }
         return true;
     }
     return false;
 }
 public function testCanDetachStream()
 {
     $r = fopen('php://temp', 'w+');
     $stream = new Stream($r);
     $stream->detachStream();
     $this->assertNull($stream->getStream());
 }