pssht  latest
SSH server library written in PHP
Zlib.php
1 <?php
2 
3 /*
4 * This file is part of pssht.
5 *
6 * (c) François Poirotte <clicky@erebot.net>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11 
13 
17 class Zlib implements
19  \fpoirotte\Pssht\Algorithms\AvailabilityInterface
20 {
22  static protected $deflateFactory;
23 
25  static protected $inflateFactory;
26 
28  protected $stream;
29 
31  protected $mode;
32 
33  public function __construct($mode)
34  {
35  if (self::$deflateFactory === null || self::$inflateFactory === null) {
36  throw new \RuntimeException('(De)Compression is not available');
37  }
38 
39  if ($mode == self::MODE_COMPRESS) {
40  list($cls, $method) = self::$deflateFactory;
41  $flags =
42  $cls::TYPE_ZLIB |
43  $cls::LEVEL_DEF |
44  $cls::FLUSH_SYNC;
45  } else {
46  list($cls, $method) = self::$inflateFactory;
47  $flags = 0;
48  }
49 
50  $reflector = new \ReflectionMethod($cls, $method);
51  if ($reflector->isConstructor()) {
52  $this->stream = new $cls($flags);
53  } else {
54  $this->stream = $reflector->invoke(null, $flags);
55  }
56  $this->mode = $mode;
57  }
58 
59  public function getMode()
60  {
61  return $this->mode;
62  }
63 
64  public static function isAvailable()
65  {
66  // PECL_HTTP v2.x uses classes in a dedicated namespace
67  // and a regular constructor.
68  if (class_exists('http\\Encoding\\Stream\\Deflate') &&
69  class_exists('\\http\\Encoding\\Stream\\Inflate')) {
70  self::$deflateFactory = array('\\http\\Encoding\\Stream\\Deflate', '__construct');
71  self::$inflateFactory = array('\\http\\Encoding\\Stream\\Inflate', '__construct');
72  return true;
73  }
74  // PECL_HTTP v1.x uses classes in the global scope
75  // and a static factory method.
76  if (class_exists('\\HttpDeflateStream') &&
77  class_exists('\\HttpInflateStream')) {
78  self::$deflateFactory = array('\\HttpDeflateStream', 'factory');
79  self::$inflateFactory = array('\\HttpInflateStream', 'factory');
80  return true;
81  }
82  return false;
83  }
84 
85  public static function getName()
86  {
87  return 'zlib';
88  }
89 
90  public function update($data)
91  {
92  return $this->stream->update($data);
93  }
94 }
static $inflateFactory
Factory used to inflate (decompress) data.
Definition: Zlib.php:25
static $deflateFactory
Factory used to deflate (compress) data.
Definition: Zlib.php:22
$mode
Compression/decompression mode.
Definition: Zlib.php:31
$stream
Compression/decompression stream.
Definition: Zlib.php:28
static getName()
Return the name of the algorithm.
Definition: Zlib.php:85