/** * Calculate a hash of a Stream * * @param StreamInterface $stream Stream to calculate the hash for * @param string $algo Hash algorithm (e.g. md5, crc32, etc) * @param bool $rawOutput Whether or not to use raw output * * @return bool|string Returns false on failure or a hash string on success */ public static function getHash(StreamInterface $stream, $algo, $rawOutput = false) { $pos = $stream->ftell(); if (!$stream->seek(0)) { return false; } $ctx = hash_init($algo); while ($data = $stream->read(8192)) { hash_update($ctx, $data); } $out = hash_final($ctx, (bool) $rawOutput); $stream->seek($pos); return $out; }
/** * Send HTTP response headers and body * * @return \Slim\Interfaces\Http\Response Self * @api */ public function send() { // Send headers if (headers_sent() === false) { if (strpos(PHP_SAPI, 'cgi') === 0) { header(sprintf('Status: %s', $this->getReasonPhrase())); } else { header(sprintf('%s %s', $this->getProtocolVersion(), $this->getReasonPhrase())); } foreach ($this->headers as $name => $value) { foreach ($value as $hVal) { header("{$name}: {$hVal}", false); } } } // Send body $this->body->seek(0); while ($this->body->feof() === false) { echo $this->body->read(1024); } return $this; }
/** * Process response headers * * @param StreamInterface $stream */ protected function processResponseHeaders(StreamInterface $stream) { // Set the size on the stream if it was returned in the response foreach ($this->lastResponseHeaders as $header) { if (($pos = stripos($header, 'Content-Length:')) === 0) { $stream->setSize(trim(substr($header, 15))); } } }
/** * {@inheritdoc} */ protected function doGetContents() { return stream_get_contents($this->stream->getStream()); }
/** * @param StreamInterface $stream * @param MailchimpExport_DataCallbackInterface $dataCallable * @throws MailchimpExport_Exception */ protected function readFromStream(StreamInterface $stream, MailchimpExport_DataCallbackInterface $dataCallable) { $headings = null; $first = true; $hasHeadingRow = $dataCallable->hasHeadingRow(); // Read until the stream is closed while (!$stream->feof()) { // Read a line from the stream $currentRow = trim($stream->readLine()); if (strlen($currentRow) == 0) { continue; } $currentDataSet = json_decode($currentRow, true); if (!$currentDataSet) { throw new \MailchimpExport_Exception('invalid response: ' . $currentRow); } $currentRow = null; // check for errors if (isset($currentDataSet['error']) && isset($currentDataSet['code'])) { throw new \MailchimpExport_Exception($currentDataSet['error'], $currentDataSet['code']); } if ($first && $hasHeadingRow) { $headings = $currentDataSet; $first = false; continue; } // if we have headings => combine them with the dataset if ($headings !== null) { // Sometimes mailchimps gives an smaller array of data because of missing data. // This will cause an error in array_column while (count($headings) > count($currentDataSet)) { $currentDataSet[] = null; } $returnValue = array_combine($headings, $currentDataSet); } else { $returnValue = $currentDataSet; } $dataCallable->addDataSet($returnValue); $returnValue = null; $currentDataSet = null; } }
protected function processResponseHeaders(StreamInterface $stream) { foreach ($this->lastResponseHeaders as $header) { if (stripos($header, 'Content-Length:') === 0) { $stream->setSize(trim(substr($header, 15))); } } }
/** * Constructor * * @param \Slim\Interfaces\EnvironmentInterface $env * @param \Slim\Interfaces\Http\HeadersInterface $headers * @param \Slim\Interfaces\Http\CookiesInterface $cookies * @param string $body * @api */ public function __construct(EnvironmentInterface $env, HeadersInterface $headers, CookiesInterface $cookies, $body = null) { $this->env = $env; $this->headers = $headers; $this->cookies = $cookies; $this->bodyRaw = new \Guzzle\Stream\Stream(fopen('php://temp', 'r+')); if (is_string($body) === true) { $this->bodyRaw->write($body); } $this->bodyRaw->seek(0); }