public function __construct($file, array $options = array()) { $defaultOptions = array('length' => 0, 'fromEncoding' => null); $this->options = array_merge($defaultOptions, $options); if ($file instanceof SplFileInfo) { $file = $file->getPathname(); } if ($file instanceof \Guzzle\Stream\StreamInterface) { $this->guzzleStream = $file; $file = $file->getStream(); } if (is_resource($file)) { $this->fileHandle = $file; $this->closeFileHandleOnDestruct = false; if (null !== $this->options['fromEncoding']) { throw new Exception('Source encoding can only be specified if constructed with file path'); } } else { if (is_string($file)) { $this->fileHandle = @fopen($file, 'r'); if ($this->fileHandle === false) { throw new InvalidArgumentException("Could not open csv file with path: '{$file}'"); } if (null !== $this->options['fromEncoding']) { stream_filter_append($this->fileHandle, 'convert.iconv.' . $this->options['fromEncoding'] . '/UTF-8'); } $this->closeFileHandleOnDestruct = true; } else { throw new InvalidArgumentException('You must provide either a stream or filename to the csv iterator, you provided a ' . gettype($file)); } } parent::__construct(array_diff_key($options, $defaultOptions)); }
/** * Encodes the given stream with the given encoding using stream filters * * If the given encoding stream filter is not available, an Exception * will be thrown. * * @param string $stream * @param string $encoding * @param int $LineLength; defaults to Mime::LINE_LENGTH (72) * @param string $eol EOL string; defaults to Mime::EOL (\n) * @return string */ public static function encodeStream($stream, $encoding, $lineLength = self::LINE_LENGTH, $eol = self::EOL) { if (!self::isEncodingSupported($encoding)) { throw new Exception("Not supported encoding: {$encoding}"); } switch ($encoding) { case self::BASE64: $filter = 'convert.base64-encode'; break; case self::QUOTED_PRINTABLE: $filter = 'convert.quoted-printable-encode'; break; default: $filter = null; } if ($filter !== null) { $params = array('line-length' => $lineLength, 'line-break-chars' => $eol); $streamFilter = stream_filter_append($stream, $filter, STREAM_FILTER_READ, $params); } $content = stream_get_contents($stream); if ($filter !== null) { stream_filter_remove($streamFilter); } fclose($stream); return $content; }
/** * Handle a "given" step. * * @param array &$world Joined "world" of variables. * @param string $action The description of the step. * @param array $arguments Additional arguments to the step. * * @return mixed The outcome of the step. */ public function runGiven(&$world, $action, $arguments) { switch ($action) { case 'an incoming message on host': $world['hostname'] = $arguments[0]; $world['type'] = 'Incoming'; break; case 'the SMTP sender address is': $world['sender'] = $arguments[0]; break; case 'the SMTP recipient address is': $world['recipient'] = $arguments[0]; break; case 'the client address is': $world['client'] = $arguments[0]; break; case 'the hostname is': $world['hostname'] = $arguments[0]; break; case 'the unmodified message content is': $world['infile'] = $arguments[0]; $world['fp'] = fopen($world['infile'], 'r'); break; case 'the modified message template is': $world['infile'] = $arguments[0]; $world['fp'] = fopen($world['infile'], 'r'); stream_filter_register('addresses', 'Horde_Kolab_Filter_Helper_AddressFilter'); stream_filter_append($world['fp'], 'addresses', STREAM_FILTER_READ, array('recipient' => $world['recipient'], 'sender' => $world['sender'])); break; default: return $this->notImplemented($action); } }
/** * Constructor * * @param io.streams.InputStream in */ public function __construct(InputStream $in) { $this->in = Streams::readableFd($in); if (!stream_filter_append($this->in, 'zlib.inflate', STREAM_FILTER_READ)) { throw new IOException('Could not append stream filter'); } }
public function RawAction() { $dataUriRegex = "/data:image\\/([\\w]*);([\\w]*),/i"; //running a regex against a data uri might be slow. //Streams R fun. // To avoid lots of processing before needed, copy just the first bit of the incoming data stream to a variable for checking. rewind the stream after. Part of the data will be MD5'd for storage. // note for $body = $this->detectRequestBody(); $tempStream = fopen('php://temp', 'r+'); stream_copy_to_stream($body, $tempStream, 500); rewind($tempStream); $uriHead = stream_get_contents($tempStream); $netid = isset($_SERVER['NETID']) ? $_SERVER['NETID'] : "notSet"; $filename = $netid; $matches = array(); // preg_match_all returns number of matches. if (0 < preg_match_all($dataUriRegex, $uriHead, $matches)) { $extension = $matches[1][0]; $encoding = $matches[2][0]; $start = 1 + strpos($uriHead, ","); $imageData = substr($uriHead, $start); // THERES NO ARRAY TO STRING CAST HERE PHP STFU $filename = (string) ("./cache/" . $filename . "-" . md5($imageData) . "." . $extension); $fileHandle = fopen($filename, "c"); stream_filter_append($fileHandle, 'convert.base64-decode', STREAM_FILTER_WRITE); stream_copy_to_stream($body, $fileHandle, -1, $start); } }
function sgfun($filter, $params = null) { $fp = fopen('php://memory', 'w'); $filter = @stream_filter_append($fp, $filter, STREAM_FILTER_WRITE, $params); if ($filter === false) { fclose($fp); $error = error_get_last() + array('message' => ''); throw new RuntimeException('Unable to access built-in filter: ' . $error['message']); } // append filter function which buffers internally $buffer = ''; sgappend($fp, function ($chunk) use(&$buffer) { $buffer .= $chunk; // always return empty string in order to skip actually writing to stream resource return ''; }, STREAM_FILTER_WRITE); $closed = false; return function ($chunk = null) use($fp, $filter, &$buffer, &$closed) { if ($closed) { throw new RuntimeException('Unable to perform operation on closed stream'); } if ($chunk === null) { $closed = true; $buffer = ''; fclose($fp); return $buffer; } // initialize buffer and invoke filters by attempting to write to stream $buffer = ''; fwrite($fp, $chunk); // buffer now contains everything the filter function returned return $buffer; }; }
/** * csp_decryptStream * * Entschlüsselt einen Filestream * Nach dem Beispiel von http://www.php.net/manual/en/filters.encryption.php * * @param resource &$fp Filepointer des zu entschlüsselnden Filestreams * @param string $crypto_key Verwendeter Schlussel (Passphrase) */ function csp_decryptStream(&$fp, $crypto_key) { $iv = substr(md5('iv#' . $crypto_key, true), 0, 8); $key = substr(md5('pass1#' . $crypto_key, true) . md5('pass2#' . $crypto_key, true), 0, 24); $opts = array('iv' => $iv, 'key' => $key); stream_filter_append($fp, 'mdecrypt.tripledes', STREAM_FILTER_WRITE, $opts); }
protected function doBackup($stream, $tables, $gzip = false) { $db = Am_Di::getInstance()->db; $stream_filter = null; $hash = null; $len = 0; if ($gzip) { $hash = hash_init('crc32b'); // gzip file header fwrite($stream, $this->getGzHeader()); if (!($stream_filter = stream_filter_append($stream, "zlib.deflate", STREAM_FILTER_WRITE, self::COMPRESSION_LEVEL))) { throw new Am_Exception_InternalError("Could not attach gzencode filter to output stream"); } } $this->out($stream, "### aMember Pro " . AM_VERSION . " database backup\n", $len, $hash); $this->out($stream, "### Created: " . date('Y-m-d H:i:s') . "\n", $len, $hash); foreach ($tables as $table) { $this->out($stream, "\n\nDROP TABLE IF EXISTS {$table};\n", $len, $hash); $r = $db->selectRow("SHOW CREATE TABLE {$table}"); $this->out($stream, $r['Create Table'] . ";\n", $len, $hash); $q = $db->queryResultOnly("SELECT * FROM {$table}"); while ($a = $db->fetchRow($q)) { $fields = join(',', array_keys($a)); $values = join(',', array_map(array($db, 'escape'), array_values($a))); $this->out($stream, "INSERT INTO {$table} ({$fields}) VALUES ({$values});\n", $len, $hash); } $db->freeResult($q); } if ($stream_filter) { stream_filter_remove($stream_filter); fwrite($stream, $this->getGzFooter($len, $hash)); } return $stream; }
/** * Constructor * * @param io.streams.InputStream in */ public function __construct(InputStream $in) { $this->in = Streams::readableFd($in); if (!stream_filter_append($this->in, 'bzip2.decompress', STREAM_FILTER_READ)) { throw new \io\IOException('Could not append stream filter'); } }
public function __construct(puzzle_stream_StreamInterface $stream) { // Skip the first 10 bytes $stream = new puzzle_stream_LimitStream($stream, -1, 10); $resource = puzzle_stream_GuzzleStreamWrapper::getResource($stream); stream_filter_append($resource, 'zlib.inflate', STREAM_FILTER_READ); $this->stream = new puzzle_stream_Stream($resource); }
public function __construct(StreamInterface $stream) { // Skip the first 10 bytes $stream = new LimitStream($stream, -1, 10); $resource = StreamWrapper::getResource($stream); stream_filter_append($resource, 'zlib.inflate', STREAM_FILTER_READ); parent::__construct(new Stream($resource)); }
/** * @dataProvider escapeProvider */ public function testEscape($in, $expected) { $stream = fopen('php://temp', 'r+'); stream_filter_append($stream, self::FILTER_ID, STREAM_FILTER_READ); fwrite($stream, $in); rewind($stream); $this->assertEquals($expected, stream_get_contents($stream)); }
/** * Constructor * * @param io.streams.OutputStream out * @param int lineLength limit maximum line length */ public function __construct(OutputStream $out, $lineLength = 0) { $params = $lineLength ? array('line-length' => $lineLength, 'line-break-chars' => "\n") : array(); $this->out = Streams::writeableFd($out); if (!stream_filter_append($this->out, 'convert.base64-encode', STREAM_FILTER_WRITE, $params)) { throw new IOException('Could not append stream filter'); } }
/** * @param callable $func filter proc * @param callable $ctor constructor * @param callable $dtor destructor */ function __construct(callable $func, callable $ctor = null, callable $dtor = null) { /* * We don't have pipe(2) support, so we'll use socketpair(2) instead. */ list($this->input, $this->output) = stream_socket_pair(STREAM_PF_UNIX, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP); stream_filter_append($this->input, "atick\\IO\\StreamFilter", STREAM_FILTER_WRITE, compact("func", "ctor", "dtor")); stream_set_blocking($this->output, false); }
/** * Parse the file and yield $closure for each line * @param callable(RemoteZipCodeObject) $closure */ public function parse($url, Closure $closure) { $handle = fopen($url, 'r'); stream_filter_append($handle, 'convert.iconv.ISO-8859-15/UTF-8', STREAM_FILTER_READ); while (($data = fgetcsv($handle, 0, "\t")) !== false) { list($zip_code, $zip_name, $municipality_code, $municipality_name, $category) = $data; $closure(new RemoteZipCodeObject($zip_code, $zip_name, $municipality_code, $municipality_name, $category)); } }
protected function _httpChunkedDecode($body) { if (stripos($this->headers['Transfer-Encoding'], 'chunked') === false) { return $body; } $stream = fopen('data://text/plain,' . urlencode($body), 'r'); stream_filter_append($stream, 'dechunk'); return trim(stream_get_contents($stream)); }
/** * Read a file and pass it through the filter. * * @param string $filename * @param LoggerInterface $logger */ private function readFile($filename, $logger) { $input = fopen(__DIR__ . '/../data/encodings/' . $filename, 'r'); stream_filter_append($input, FilterEncoding::class, STREAM_FILTER_READ, ['logger' => $logger]); while (!feof($input)) { fread($input, 8192); } fclose($input); }
function testStagedRead() { printf("---%s---\n", __FUNCTION__); $f = fopen('php://memory', 'r+'); stream_filter_append($f, 'testfilter', STREAM_FILTER_READ); fwrite($f, 'abcd'); rewind($f); var_dump(fread($f, 4)); var_dump(fread($f, 4)); }
function __construct($_file) { $this->tar_size = 0; $this->file = $_file; $this->create_time = time(); //gzip header fwrite($this->file, "‹" . pack("V", $this->create_time) . ""); $this->filter = stream_filter_append($this->file, 'zlib.deflate', STREAM_FILTER_WRITE, -1); $this->hctx = hash_init('crc32b'); }
/** * @dataProvider bodyFilterProvider() */ public function testBodyFilter($data, $result) { $params = new stdClass(); $stream = fopen('php://temp', 'r+'); stream_filter_register('horde_smtp_body', 'Horde_Smtp_Filter_Body'); stream_filter_append($stream, 'horde_smtp_body', STREAM_FILTER_WRITE, $params); fwrite($stream, $data); fclose($stream); $this->assertEquals($result, $params->body); }
public function testUserFilter() { stream_filter_register("strtoupper", "strtoupper_filter"); $fp = fopen("php://memory", 'r+'); stream_filter_append($fp, "strtoupper"); fputs($fp, "test\n"); rewind($fp); $contents = stream_get_contents($fp); $this->assertEquals("TEST\n", $contents); }
public static function encode($str, $lineLength = 74, $lineEnd = "\r\n") { $fp = fopen("php://temp", "r+"); stream_filter_append($fp, "convert.quoted-printable-encode", STREAM_FILTER_READ, array("line-length" => $lineLength, "line-break-chars" => $lineEnd)); fputs($fp, $str); rewind($fp); $encoded = str_replace("_", "=5F", stream_get_contents($fp)); fclose($fp); return $encoded; }
/** * Encodes text via quoted-printable encoding. * * @param string $text The text to encode (UTF-8). * @param string $eol The EOL sequence to use. * @param integer $wrap Wrap a line at this many characters. * * @return string The quoted-printable encoded string. */ public static function encode($text, $eol = "\n", $wrap = 76) { $fp = fopen('php://temp', 'r+'); stream_filter_append($fp, 'convert.quoted-printable-encode', STREAM_FILTER_WRITE, array('line-break-chars' => $eol, 'line-length' => $wrap)); fwrite($fp, $text); rewind($fp); $out = stream_get_contents($fp); fclose($fp); return $out; }
function main() { $fnam = tempnam('/tmp', 'test'); $f = fopen($fnam, 'w'); stream_filter_append($f, 'zlib.deflate', STREAM_FILTER_WRITE); fwrite($f, 'Hello, world.'); fclose($f); var_dump(md5(file_get_contents($fnam))); unlink($fnam); }
/** * import the data * * @param stream $_resource * @param array $_clientRecordData * @return array : * 'results' => Tinebase_Record_RecordSet, // for dryrun only * 'totalcount' => int, * 'failcount' => int, * 'duplicatecount' => int, */ public function import($_resource = NULL, $_clientRecordData = array()) { // force correct line ends require_once 'StreamFilter/StringReplace.php'; $filter = stream_filter_append($_resource, 'str.replace', STREAM_FILTER_READ, array('search' => '/(?<!\\r)\\n/', 'replace' => "\r\n", 'searchIsRegExp' => TRUE)); if (!$this->_options['importContainerId']) { throw new Tinebase_Exception_InvalidArgument('you need to define a importContainerId'); } $icalData = stream_get_contents($_resource); $parser = new qCal_Parser(); try { $ical = $parser->parse($icalData); } catch (Exception $e) { // rethrow a mal formated ics $isce = new Calendar_Exception_IcalParser(); $isce->setParseError($e); throw $isce; } $events = $this->_importResult['results'] = $this->_getEvents($ical); if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) { Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Got ' . count($events) . ' events for import.'); } // print_r($events->toArray()); // set container $events->container_id = $this->_options['importContainerId']; $cc = Calendar_Controller_Event::getInstance(); $sendNotifications = $cc->sendNotifications(FALSE); // search uid's and remove already existing -> only in import cal? $existingEvents = $cc->search(new Calendar_Model_EventFilter(array(array('field' => 'container_id', 'operator' => 'equals', 'value' => $this->_options['importContainerId']), array('field' => 'uid', 'operator' => 'in', 'value' => array_unique($events->uid)))), NULL); // insert one by one in a single transaction $existingEvents->addIndices(array('uid')); foreach ($events as $event) { $existingEvent = $existingEvents->find('uid', $event->uid); try { if (!$existingEvent) { $cc->create($event, FALSE); $this->_importResult['totalcount'] += 1; } else { if ($this->_options['forceUpdateExisting'] || $this->_options['updateExisting'] && $event->seq > $existingEvent->seq) { $event->id = $existingEvent->getId(); $event->last_modified_time = clone $existingEvent->last_modified_time; $cc->update($event, FALSE); $this->_importResult['totalcount'] += 1; } else { $this->_importResult['duplicatecount'] += 1; } } } catch (Exception $e) { $this->_importResult['failcount'] += 1; } } $cc->sendNotifications($sendNotifications); return $this->_importResult; }
/** * Pass a string through the filter. * * @param string $input * @param LoggerInterface $logger * * @return string */ private function filterString($input, $logger) : string { $stream = fopen('data://text/plain,' . $input, 'r'); stream_filter_append($stream, FilterEndOfLine::class, STREAM_FILTER_READ, ['logger' => $logger]); $output = ''; while (!feof($stream)) { $output .= fread($stream, 8192); } fclose($stream); return $output; }
public function __construct(StreamInterface $stream) { // read the first 10 bytes, ie. gzip header $header = $stream->read(10); $filenameHeaderLength = $this->getLengthOfPossibleFilenameHeader($stream, $header); // Skip the header, that is 10 + length of filename + 1 (nil) bytes $stream = new LimitStream($stream, -1, 10 + $filenameHeaderLength); $resource = StreamWrapper::getResource($stream); stream_filter_append($resource, 'zlib.inflate', STREAM_FILTER_READ); $this->stream = new Stream($resource); }
public function __construct($filename = 'data', $fh = null) { $this->__fh = is_null($fh) ? fopen('php://temp', 'w+') : $fh; fwrite($this->__fh, "‹" . pack("V", time()) . "ÿ", 10); // GZ file header fwrite($this->__fh, str_replace("", "", basename($filename)) . ""); // GZ filename = data, needed??? $this->gz_filter = stream_filter_append($this->__fh, "zlib.deflate", STREAM_FILTER_WRITE, -1); $this->uncompressed_bytes = 0; $this->file_hash = hash_init("crc32b"); }
/** * Checks if the data needs to be encoded like e.g., when outputing binary * data in-line during ITEMOPERATIONS requests. * * @param mixed $data The data to check. A string or stream resource. * @param string $tag The tag we are outputing. * * @return mixed The encoded data. A string or stream resource with * a filter attached. */ protected function _checkEncoding($data, $tag) { if ($tag == Horde_ActiveSync_Request_ItemOperations::ITEMOPERATIONS_DATA) { if (is_resource($data)) { $this->_streamFilters[] = stream_filter_append($data, 'convert.base64-encode', STREAM_FILTER_READ); } else { $data = base64_encode($data); } } return $data; }
public function testUnzipOnTheFly2() { $handle = fopen(__DIR__ . '/../data/partial.geojson.bz2', 'r'); stream_filter_append($handle, 'bzip2.decompress', STREAM_FILTER_READ); $listener = new Listener(function (Feature $item) { $this->count++; }); $parser = new JsonStreamingParser_Parser($handle, $listener); $parser->parse(); $this->assertEquals(9, $this->count); }