Exemple #1
0
 /**
  * Decode a string from the values that have been generated
  *
  * @param text string The string that has been encoded
  * 
  * @return string
  */
 public static function decrypt($text)
 {
     $config = Config::settings('encrypt');
     $base64key = $config['key'];
     $base64ivector = $config['iv'];
     return bzdecompress(trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, base64_decode($base64key), base64_decode($text), MCRYPT_MODE_CBC, base64_decode($base64ivector))));
 }
Exemple #2
0
 /**
  * Static method to decompress data
  *
  * @param  string $data
  * @return mixed
  */
 public static function decompress($data)
 {
     // Decompress the file
     if (@file_exists($data)) {
         $bz = bzopen($data, 'r');
         $uncompressed = '';
         // Read the uncompressed data.
         while (!feof($bz)) {
             $uncompressed .= bzread($bz, 4096);
         }
         // Close the Bzip2 compressed file and write
         // the data to the uncompressed file.
         bzclose($bz);
         if (stripos($data, '.tbz2') !== false) {
             $newFile = str_replace('.tbz2', '.tar', $data);
         } else {
             if (stripos($data, '.tbz') !== false) {
                 $newFile = str_replace('.tbz', '.tar', $data);
             } else {
                 $newFile = str_replace('.bz2', '', $data);
             }
         }
         file_put_contents($newFile, $uncompressed);
         return $newFile;
         // Else, decompress the string
     } else {
         return bzdecompress($data);
     }
 }
Exemple #3
0
 /**
  * Decompress the given value with the specific compression
  *
  * @param String $sValue
  *
  * @return String
  *
  * @throws Exception
  */
 public function decompress($sValue)
 {
     if (!is_string($sValue)) {
         throw new Exception('Invalid first argument, must be a string');
     }
     return bzdecompress($sValue);
 }
 public function testEncode()
 {
     $testString = 'a string to be compressed';
     $result = $this->encoder->encode($testString);
     $uncompressedResult = bzdecompress($result);
     $this->assertSame($testString, $uncompressedResult);
 }
Exemple #5
0
 function recover($code)
 {
     if (!$this->getAuthUser()) {
         $this->jerr("invalid");
     }
     // fixme!!!!
     $fn = $this->rootDir . '/Pman/' . $code . '.js';
     list($app, $mod) = explode('/', $code);
     if (!file_exists($fn)) {
         $this->jerr("file does not exist.:" . $fn);
     }
     $data = file_get_contents($fn);
     preg_match("#/*\n--SOURCE--\n([^\\*]+)#m", $data, $matches);
     // echo "<PRE>";print_R($matches);
     $base = $matches[1];
     $str = bzdecompress(base64_decode($base));
     $js = json_decode($str);
     //echo "<PRE>";print_R($str);
     $b = DB_DataObject::factory('builder');
     $b->app = $app;
     $b->module = $mod;
     $b->btype = 'FORM';
     $b->name = $js->name;
     if (!$b->find(true)) {
         $b->insert();
     }
     echo "<PRE>" . htmlspecialchars(print_r($b, true));
     $b->json = $str;
     $b->update();
     $this->jok("DONE");
     exit;
 }
Exemple #6
0
 /**
  * {@inheritdoc}
  */
 public function decompress($data, $length)
 {
     $output = @bzdecompress(substr($data, 0, $length));
     if (!is_string($output)) {
         throw new InvalidInputDataException('The decompression input data is invalid.', $output);
     }
     return $output;
 }
 protected function ReadInternal($Buffer, $Length, $SherlockFunction)
 {
     if ($Buffer->Remaining() === 0) {
         throw new InvalidPacketException('Failed to read any data from socket', InvalidPacketException::BUFFER_EMPTY);
     }
     $Header = $Buffer->GetLong();
     if ($Header === -1) {
         // We don't have to do anything
     } else {
         if ($Header === -2) {
             $Packets = [];
             $IsCompressed = false;
             $ReadMore = false;
             do {
                 $RequestID = $Buffer->GetLong();
                 switch ($this->Engine) {
                     case SourceQuery::GOLDSOURCE:
                         $PacketCountAndNumber = $Buffer->GetByte();
                         $PacketCount = $PacketCountAndNumber & 0xf;
                         $PacketNumber = $PacketCountAndNumber >> 4;
                         break;
                     case SourceQuery::SOURCE:
                         $IsCompressed = ($RequestID & 0x80000000) !== 0;
                         $PacketCount = $Buffer->GetByte();
                         $PacketNumber = $Buffer->GetByte() + 1;
                         if ($IsCompressed) {
                             $Buffer->GetLong();
                             // Split size
                             $PacketChecksum = $Buffer->GetUnsignedLong();
                         } else {
                             $Buffer->GetShort();
                             // Split size
                         }
                         break;
                 }
                 $Packets[$PacketNumber] = $Buffer->Get();
                 $ReadMore = $PacketCount > sizeof($Packets);
             } while ($ReadMore && $SherlockFunction($Buffer, $Length));
             $Data = Implode($Packets);
             // TODO: Test this
             if ($IsCompressed) {
                 // Let's make sure this function exists, it's not included in PHP by default
                 if (!Function_Exists('bzdecompress')) {
                     throw new \RuntimeException('Received compressed packet, PHP doesn\'t have Bzip2 library installed, can\'t decompress.');
                 }
                 $Data = bzdecompress($Data);
                 if (CRC32($Data) !== $PacketChecksum) {
                     throw new InvalidPacketException('CRC32 checksum mismatch of uncompressed packet data.', InvalidPacketException::CHECKSUM_MISMATCH);
                 }
             }
             $Buffer->Set(SubStr($Data, 4));
         } else {
             throw new InvalidPacketException('Socket read: Raw packet header mismatch. (0x' . DecHex($Header) . ')', InvalidPacketException::PACKET_HEADER_MISMATCH);
         }
     }
     return $Buffer;
 }
Exemple #8
0
 function execute(array $params = array())
 {
     $input = (string) $params['input'];
     $quick = $params['quick'];
     if (!is_bool($quick)) {
         $quick = false;
     }
     return bzdecompress($input, $quick);
 }
Exemple #9
0
 /**
  * Extract a Bzip2 compressed file to a given path
  *
  * @param   string   $archive      Path to Bzip2 archive to extract
  * @param   string   $destination  Path to extract archive to
  * @param   array    $options      Extraction options [unused]
  *
  * @return  boolean  True if successful
  * @since   11.1
  */
 public function extract($archive, $destination, $options = array())
 {
     // Initialise variables.
     $this->_data = null;
     if (!extension_loaded('bz2')) {
         $this->set('error.message', JText::_('JLIB_FILESYSTEM_BZIP_NOT_SUPPORTED'));
         return JError::raiseWarning(100, $this->get('error.message'));
     }
     if (!isset($options['use_streams']) || $options['use_streams'] == false) {
         // Old style: read the whole file and then parse it
         if (!($this->_data = JFile::read($archive))) {
             $this->set('error.message', 'Unable to read archive');
             return JError::raiseWarning(100, $this->get('error.message'));
         }
         $buffer = bzdecompress($this->_data);
         unset($this->_data);
         if (empty($buffer)) {
             $this->set('error.message', 'Unable to decompress data');
             return JError::raiseWarning(100, $this->get('error.message'));
         }
         if (JFile::write($destination, $buffer) === false) {
             $this->set('error.message', 'Unable to write archive');
             return JError::raiseWarning(100, $this->get('error.message'));
         }
     } else {
         // New style! streams!
         $input = JFactory::getStream();
         $input->set('processingmethod', 'bz');
         // use bzip
         if (!$input->open($archive)) {
             $this->set('error.message', JText::_('JLIB_FILESYSTEM_BZIP_UNABLE_TO_READ'));
             return JError::raiseWarning(100, $this->get('error.message'));
         }
         $output = JFactory::getStream();
         if (!$output->open($destination, 'w')) {
             $this->set('error.message', JText::_('JLIB_FILESYSTEM_BZIP_UNABLE_TO_WRITE'));
             $input->close();
             // close the previous file
             return JError::raiseWarning(100, $this->get('error.message'));
         }
         $written = 0;
         do {
             $this->_data = $input->read($input->get('chunksize', 8196));
             if ($this->_data) {
                 if (!$output->write($this->_data)) {
                     $this->set('error.message', JText::_('JLIB_FILESYSTEM_BZIP_UNABLE_TO_WRITE_FILE'));
                     return JError::raiseWarning(100, $this->get('error.message'));
                 }
             }
         } while ($this->_data);
         $output->close();
         $input->close();
     }
     return true;
 }
Exemple #10
0
 /**
  * @inheritDoc
  */
 public function decode($data)
 {
     $data = bzdecompress($data);
     if ($this->isErrorCode($data)) {
         throw new DecodeFailedException("bzdecompress failed.");
     }
     if (!$data) {
         throw new DecodeFailedException("bzdecompress returned no data.");
     }
     return $data;
 }
 /**
  * Reassembles the data of a split and/or compressed packet into a single
  * packet object
  *
  * @param array $splitPackets An array of packet data
  * @param bool $isCompressed whether the data of this packet is compressed
  * @param int $packetChecksum The CRC32 checksum of the decompressed
  *        packet data
  * @throws PacketFormatException if the calculated CRC32 checksum does not
  *         match the expected value
  * @return SteamPacket The reassembled packet
  * @see packetFromData()
  */
 public static function reassemblePacket($splitPackets, $isCompressed = false, $packetChecksum = 0)
 {
     $packetData = join('', $splitPackets);
     if ($isCompressed) {
         $packetData = bzdecompress($packetData);
         if (crc32($packetData) != $packetChecksum) {
             throw new PacketFormatException('CRC32 checksum mismatch of uncompressed packet data.');
         }
     }
     $packetData = substr($packetData, 4);
     return self::getPacketFromData($packetData);
 }
 private function populateWHoisDump()
 {
     $this->info('Downloading and populating Afrinic WHOIS dump');
     $bz2Content = $this->getContents(env('WHOIS_DB_AFRINIC_BASE_URL'));
     $rawData = bzdecompress($bz2Content);
     $dataParts = array_filter(explode("\n", $rawData));
     // Categorise the whois data dump
     foreach ($dataParts as $dataPart) {
         $elementType = explode(':', $dataPart, 2)[0];
         $this->whoisDump[$elementType][] = str_replace('\\t', "\t", str_replace('\\n', "\n", $dataPart));
     }
 }
Exemple #13
0
function getCSV($url, $method)
{
    global $mdb, $redis;
    $file = $redis->get("RC:{$url}");
    if ($file == null) {
        $file = file_get_contents($url);
        $redis->setex("RC:{$url}", 900, $file);
    }
    $csv = bzdecompress($file);
    Util::out("Parsing {$url}");
    parseCSV($csv, $method);
}
Exemple #14
0
 /**
  * @param string $file
  * @return Database
  * @throws IOException
  * @throws \RuntimeException
  */
 public function decompress(string $file) : self
 {
     if (($content = file_get_contents($file)) === false) {
         throw new IOException('Could not read file: ' . $file);
     }
     if (($content = bzdecompress($content)) === false) {
         throw new \RuntimeException('Could not decompress data from file: ' . $file);
     }
     if (file_put_contents($file, $content) === false) {
         throw new IOException('Could not write data to file: ' . $file);
     }
     return $this;
 }
 public static function extract($path_file)
 {
     $pathinfo = pathinfo($path_file);
     if ($pathinfo['extension'] == 'bzip2' or $pathinfo['extension'] == 'bz2') {
         $uncompressed_pathfile = $pathinfo['dirname'] . $pathinfo['basename'] . '.csv';
         $compressed_str = file_get_contents($path_file);
         file_put_contents($uncompressed_pathfile, bzdecompress($compressed_str));
         unset($compressed_str);
         $file_tree_name = $uncompressed_pathfile;
     } else {
         $file_tree_name = $path_file;
     }
     return $file_tree_name;
 }
Exemple #16
0
 /**
  * Extract a Bzip2 compressed file to a given path
  *
  * @param   string  $archive      Path to Bzip2 archive to extract
  * @param   string  $destination  Path to extract archive to
  * @param   array   $options      Extraction options [unused]
  *
  * @return  boolean  True if successful
  *
  * @since   11.1
  * @throws  RuntimeException
  */
 public function extract($archive, $destination, array $options = array())
 {
     $this->_data = null;
     if (!extension_loaded('bz2')) {
         throw new RuntimeException('The bz2 extension is not available.');
     }
     if (!isset($options['use_streams']) || $options['use_streams'] == false) {
         // Old style: read the whole file and then parse it
         $this->_data = file_get_contents($archive);
         if (!$this->_data) {
             throw new RuntimeException('Unable to read archive');
         }
         $buffer = bzdecompress($this->_data);
         unset($this->_data);
         if (empty($buffer)) {
             throw new RuntimeException('Unable to decompress data');
         }
         if (JFile::write($destination, $buffer) === false) {
             throw new RuntimeException('Unable to write archive');
         }
     } else {
         // New style! streams!
         $input = JFactory::getStream();
         // Use bzip
         $input->set('processingmethod', 'bz');
         if (!$input->open($archive)) {
             throw new RuntimeException('Unable to read archive (bz2)');
         }
         $output = JFactory::getStream();
         if (!$output->open($destination, 'w')) {
             $input->close();
             throw new RuntimeException('Unable to write archive (bz2)');
         }
         do {
             $this->_data = $input->read($input->get('chunksize', 8196));
             if ($this->_data) {
                 if (!$output->write($this->_data)) {
                     $input->close();
                     throw new RuntimeException('Unable to write archive (bz2)');
                 }
             }
         } while ($this->_data);
         $output->close();
         $input->close();
     }
     return true;
 }
function GenerateSpell($String)
{
    include_once ROOT_PATH . '/Cache/' . 'spell.cache.php';
    $SpellList = unserialize(bzdecompress(base64_decode($content)));
    arsort($SpellList);
    $String = ConvertEncodingToGB2312($String);
    $ReturnStr = '';
    for ($i = 0; $i < strlen($String); $i++) {
        $AscCode = ord(substr($String, $i, 1));
        if ($AscCode > 160) {
            $AscCodeNext = ord(substr($String, ++$i, 1));
            $AscCode = $AscCode * 256 + $AscCodeNext - 65536;
        }
        $ReturnStr .= Spell($AscCode, $SpellList);
    }
    return preg_replace("/[^a-z0-9]*/", '', $ReturnStr);
}
Exemple #18
0
 /**
  * Extract a Bzip2 compressed file to a given path
  *
  * @param   string  $archive      Path to Bzip2 archive to extract
  * @param   string  $destination  Path to extract archive to
  *
  * @return  boolean  True if successful
  *
  * @since   1.0
  * @throws  Exception
  */
 public function extract($archive, $destination)
 {
     $this->data = null;
     $this->options['use_streams'] = false;
     if (!isset($this->options['use_streams']) || $this->options['use_streams'] == false) {
         // Old style: read the whole file and then parse it
         $this->data = file_get_contents($archive);
         if (!$this->data) {
             throw new Exception('Unable to read archive');
         }
         $buffer = bzdecompress($this->data);
         unset($this->data);
         if (empty($buffer)) {
             throw new Exception('Unable to decompress data');
         }
         if (xapp_File2::write($destination, $buffer) === false) {
             throw new Exception('Unable to write archive');
         }
     } else {
         // New style! streams!
         $input = Stream::getStream();
         // Use bzip
         $input->set('processingmethod', 'bz');
         if (!$input->open($archive)) {
             throw new Exception('Unable to read archive (bz2)');
         }
         $output = Stream::getStream();
         if (!$output->open($destination, 'w')) {
             $input->close();
             throw new Exception('Unable to write archive (bz2)');
         }
         do {
             $this->data = $input->read($input->get('chunksize', 8196));
             if ($this->data) {
                 if (!$output->write($this->data)) {
                     $input->close();
                     throw new Exception('Unable to write archive (bz2)');
                 }
             }
         } while ($this->data);
         $output->close();
         $input->close();
     }
     // @codeCoverageIgnoreEnd
     return true;
 }
Exemple #19
0
 /**
  * Extract a Bzip2 compressed file to a given path
  *
  * @access	public
  * @param	string	$archive		Path to Bzip2 archive to extract
  * @param	string	$destination	Path to extract archive to
  * @param	array	$options		Extraction options [unused]
  * @return	boolean	True if successful
  * @since	1.5
  */
 function extract($archive, $destination, $options = array())
 {
     // Initialize variables
     $this->_data = null;
     if (!extension_loaded('bz2')) {
         return PEAR::raiseError('BZip2 Not Supported');
     }
     if (!($this->_data = JFile::read($archive))) {
         return PEAR::raiseError('Unable to read archive');
     }
     $buffer = bzdecompress($this->_data);
     if (empty($buffer)) {
         return PEAR::raiseError('Unable to decompress data');
     }
     if (JFile::write($destination, $buffer) === false) {
         return PEAR::raiseError('Unable to write archive');
     }
     return true;
 }
 public static function reassemblePacket($splitPackets, $isCompressed = false, $packetChecksum = 0)
 {
     $packetData = "";
     foreach ($splitPackets as $splitPacket) {
         if ($splitPacket == null) {
             throw new UncompletePacketException();
         }
         $packetData .= $splitPacket;
     }
     if ($isCompressed) {
         $packetData = bzdecompress($packetData);
         if (crc32($packetData) != $packetChecksum) {
             throw new PacketFormatException("CRC32 checksum mismatch of uncompressed packet data.");
         }
     }
     // Omit leading 0xFFFFFFFF
     $packetData = substr($packetData, 4);
     return self::getPacketFromData($packetData);
 }
 function restore($src)
 {
     global $dbh;
     $SQL = '';
     if ($src) {
         $this->filename = $src;
         if ($this->open_restore_stream() && $this->buffer) {
             // open source file
             $SQL = preg_split('/;\\s*\\n|;\\n/m', bzdecompress($this->buffer));
             for ($i = 0; $i < sizeof($SQL); $i++) {
                 if ($SQL[$i]) {
                     $result = pmb_mysql_query($SQL[$i], $dbh);
                 }
             }
         } else {
             die("can't open file to restore");
             return FALSE;
         }
     }
     return TRUE;
 }
 function read()
 {
     $this->algorithm = ord($this->read_byte());
     $this->data = $this->read_bytes($this->length);
     switch ($this->algorithm) {
         case 0:
             $this->data = \OpenPGP\Message::parse($this->data);
             break;
         case 1:
             $this->data = \OpenPGP\Message::parse(gzinflate($this->data));
             break;
         case 2:
             $this->data = \OpenPGP\Message::parse(gzuncompress($this->data));
             break;
         case 3:
             $this->data = \OpenPGP\Message::parse(bzdecompress($this->data));
             break;
         default:
             throw new Exception("Bad value for Compression Algorithm (decompress)");
     }
 }
Exemple #23
0
 /**
  * Extract a Bzip2 compressed file to a given path
  *
  * @param   string  $archive      Path to Bzip2 archive to extract
  * @param   string  $destination  Path to extract archive to
  * @param   array   $options      Extraction options [unused]
  *
  * @return  boolean  True if successful
  *
  * @since   11.1
  * @throws  RuntimeException
  */
 public function extract($archive, $destination, array $options = array())
 {
     $this->_data = null;
     if (!static::isSupported()) {
         throw new RuntimeException('The bz2 extension is not available.');
     }
     if (isset($options['use_streams']) && $options['use_streams'] != false) {
         return $this->extractStream($archive, $destination, $options);
     }
     // Old style: read the whole file and then parse it
     $this->_data = file_get_contents($archive);
     if (!$this->_data) {
         throw new RuntimeException('Unable to read archive');
     }
     $buffer = bzdecompress($this->_data);
     unset($this->_data);
     if (empty($buffer)) {
         throw new RuntimeException('Unable to decompress data');
     }
     if (JFile::write($destination, $buffer) === false) {
         throw new RuntimeException('Unable to write archive');
     }
     return true;
 }
Exemple #24
0
 /**
  * Uncompress file. And save it to the targetFile.
  *
  * @access    Private
  * @param     Filecontent, int, int, boolean
  * @return    none
  */
 private function _uncompress($content, $mode, $uncompressed_size, $target_file_name = FALSE)
 {
     switch ($mode) {
         case 0:
             return $target_file_name ? file_put_contents($target_file_name, $content) : $content;
         case 1:
             $this->set_error('Shrunk mode is not supported... yet?');
             return FALSE;
         case 2:
         case 3:
         case 4:
         case 5:
             $this->set_error('Compression factor ' . ($mode - 1) . ' is not supported... yet?');
             return FALSE;
         case 6:
             $this->set_error('Implode is not supported... yet?');
             return FALSE;
         case 7:
             $this->set_error('Tokenizing compression algorithm is not supported... yet?');
             return FALSE;
         case 8:
             // Deflate
             return $target_file_name ? file_put_contents($target_file_name, gzinflate($content, $uncompressed_size)) : gzinflate($content, $uncompressed_size);
         case 9:
             $this->set_error('Enhanced Deflating is not supported... yet?');
             return FALSE;
         case 10:
             $this->set_error('PKWARE Date Compression Library Impoloding is not supported... yet?');
             return FALSE;
         case 12:
             // Bzip2
             return $target_file_name ? file_put_contents($target_file_name, bzdecompress($content)) : bzdecompress($content);
         case 18:
             $this->set_error('IBM TERSE is not supported... yet?');
             return FALSE;
         default:
             $this->set_error('Unknown uncompress method: $mode');
             return FALSE;
     }
 }
Exemple #25
0
<?php

$string = "Life it seems, will fade away\nDrifting further everyday\nGetting lost within myself\nNothing matters no one else";
var_dump(bzcompress());
var_dump(bzcompress(1, 1, 1));
var_dump(bzcompress($string, 100));
var_dump(bzcompress($string, 100, -1));
var_dump(bzcompress($string, 100, 1000));
var_dump(bzcompress($string, -1, 1));
$data = bzcompress($string);
$data2 = bzcompress($string, 1, 10);
$data3 = $data2;
$data3[3] = 0;
var_dump(bzdecompress());
var_dump(bzdecompress(1, 1, 1));
var_dump(bzdecompress(1, 1));
var_dump(bzdecompress($data3));
var_dump(bzdecompress($data3, 1));
var_dump(bzdecompress($data, -1));
var_dump(bzdecompress($data, 0));
var_dump(bzdecompress($data, 1000));
var_dump(bzdecompress($data));
var_dump(bzdecompress($data2));
echo "Done\n";
Exemple #26
0
 function read_zip($name)
 {
     // Clear current file
     $this->datasec = array();
     // File information
     $this->name = $name;
     $this->mtime = filemtime($name);
     $this->size = filesize($name);
     // Read file
     $fh = fopen($name, "rb");
     $filedata = fread($fh, $this->size);
     fclose($fh);
     // Break into sections
     $filesecta = explode("PK", $filedata);
     // ZIP Comment
     $unpackeda = unpack('x16/v1length', $filesecta[1]);
     $this->comment = substr($filesecta[1], 18, $unpackeda['length']);
     $this->comment = str_replace(array("\r\n", "\r"), "\n", $this->comment);
     // CR + LF and CR -> LF
     // Cut entries from the central directory
     $filesecta = explode("PK", $filedata);
     $filesecta = explode("PK", $filesecta[0]);
     array_shift($filesecta);
     // Removes empty entry/signature
     foreach ($filesecta as $filedata) {
         // CRC:crc, FD:file date, FT: file time, CM: compression method, GPF: general purpose flag, VN: version needed, CS: compressed size, UCS: uncompressed size, FNL: filename length
         $entrya = array();
         $entrya['error'] = "";
         $unpackeda = unpack("v1version/v1general_purpose/v1compress_method/v1file_time/v1file_date/V1crc/V1size_compressed/V1size_uncompressed/v1filename_length", $filedata);
         // Check for encryption
         $isencrypted = $unpackeda['general_purpose'] & 0x1 ? true : false;
         // Check for value block after compressed data
         if ($unpackeda['general_purpose'] & 0x8) {
             $unpackeda2 = unpack("V1crc/V1size_compressed/V1size_uncompressed", substr($filedata, -12));
             $unpackeda['crc'] = $unpackeda2['crc'];
             $unpackeda['size_compressed'] = $unpackeda2['size_uncompressed'];
             $unpackeda['size_uncompressed'] = $unpackeda2['size_uncompressed'];
             unset($unpackeda2);
         }
         $entrya['name'] = substr($filedata, 26, $unpackeda['filename_length']);
         if (substr($entrya['name'], -1) == "/") {
             continue;
         }
         $entrya['dir'] = dirname($entrya['name']);
         $entrya['dir'] = $entrya['dir'] == "." ? "" : $entrya['dir'];
         $entrya['name'] = basename($entrya['name']);
         $filedata = substr($filedata, 26 + $unpackeda['filename_length']);
         if (strlen($filedata) != $unpackeda['size_compressed']) {
             $entrya['error'] = "Compressed size is not equal to the value given in header.";
         }
         if ($isencrypted) {
             $entrya['error'] = "Encryption is not supported.";
         } else {
             switch ($unpackeda['compress_method']) {
                 case 0:
                     // Stored
                     // Not compressed, continue
                     break;
                 case 8:
                     // Deflated
                     $filedata = gzinflate($filedata);
                     break;
                 case 12:
                     // BZIP2
                     if (!extension_loaded("bz2")) {
                         @dl(strtolower(substr(PHP_OS, 0, 3)) == "win" ? "php_bz2.dll" : "bz2.so");
                     }
                     if (extension_loaded("bz2")) {
                         $filedata = bzdecompress($filedata);
                     } else {
                         $entrya['error'] = "Required BZIP2 Extension not available.";
                     }
                     break;
                 default:
                     $entrya['error'] = "Compression method ({$unpackeda['compress_method']}) not supported.";
             }
             if (!$entrya['error']) {
                 if ($filedata === false) {
                     $entrya['error'] = "Decompression failed.";
                 } elseif (strlen($filedata) != $unpackeda['size_uncompressed']) {
                     $entrya['error'] = "File size is not equal to the value given in header.";
                 } elseif (crc32($filedata) != $unpackeda['crc']) {
                     $entrya['error'] = "CRC32 checksum is not equal to the value given in header.";
                 }
             }
             $entrya['filemtime'] = mktime(($unpackeda['file_time'] & 0xf800) >> 11, ($unpackeda['file_time'] & 0x7e0) >> 5, ($unpackeda['file_time'] & 0x1f) << 1, ($unpackeda['file_date'] & 0x1e0) >> 5, $unpackeda['file_date'] & 0x1f, (($unpackeda['file_date'] & 0xfe00) >> 9) + 1980);
             $entrya['data'] = $filedata;
         }
         $this->files[] = $entrya;
     }
     return $this->files;
 }
 /**
  * Extract archive
  */
 function extract($dst)
 {
     // Loop the file, looking for files and folders
     $dd_try = false;
     rewind($this->fp);
     while (!feof($this->fp)) {
         // Check if the signature is valid...
         $signature = fread($this->fp, 4);
         switch ($signature) {
             // 'Local File Header'
             case "PK":
                 // Lets get everything we need.
                 // We don't store the version needed to extract, the general purpose bit flag or the date and time fields
                 $data = unpack("@4/vc_method/@10/Vcrc/Vc_size/Vuc_size/vname_len/vextra_field", fread($this->fp, 26));
                 $file_name = fread($this->fp, $data['name_len']);
                 // filename
                 if ($data['extra_field']) {
                     fread($this->fp, $data['extra_field']);
                     // extra field
                 }
                 $target_filename = "{$dst}{$file_name}";
                 if (!$data['uc_size'] && !$data['crc'] && substr($file_name, -1, 1) == '/') {
                     if (!is_dir($target_filename)) {
                         $str = '';
                         $folders = explode('/', $target_filename);
                         // Create and folders and subfolders if they do not exist
                         foreach ($folders as $folder) {
                             $folder = trim($folder);
                             if (!$folder) {
                                 continue;
                             }
                             $str = !empty($str) ? $str . '/' . $folder : $folder;
                             if (!is_dir($str)) {
                                 if (!@mkdir($str, 0777)) {
                                     trigger_error("Could not create directory {$folder}");
                                 }
                                 phpbb_chmod($str, CHMOD_READ | CHMOD_WRITE);
                             }
                         }
                     }
                     // This is a directory, we are not writting files
                     continue;
                 } else {
                     // Some archivers are punks, they don't include folders in their archives!
                     $str = '';
                     $folders = explode('/', pathinfo($target_filename, PATHINFO_DIRNAME));
                     // Create and folders and subfolders if they do not exist
                     foreach ($folders as $folder) {
                         $folder = trim($folder);
                         if (!$folder) {
                             continue;
                         }
                         $str = !empty($str) ? $str . '/' . $folder : $folder;
                         if (!is_dir($str)) {
                             if (!@mkdir($str, 0777)) {
                                 trigger_error("Could not create directory {$folder}");
                             }
                             phpbb_chmod($str, CHMOD_READ | CHMOD_WRITE);
                         }
                     }
                 }
                 if (!$data['uc_size']) {
                     $content = '';
                 } else {
                     $content = fread($this->fp, $data['c_size']);
                 }
                 $fp = fopen($target_filename, "w");
                 switch ($data['c_method']) {
                     case 0:
                         // Not compressed
                         fwrite($fp, $content);
                         break;
                     case 8:
                         // Deflate
                         fwrite($fp, gzinflate($content, $data['uc_size']));
                         break;
                     case 12:
                         // Bzip2
                         fwrite($fp, bzdecompress($content));
                         break;
                 }
                 fclose($fp);
                 break;
                 // We hit the 'Central Directory Header', we can stop because nothing else in here requires our attention
                 // or we hit the end of the central directory record, we can safely end the loop as we are totally finished with looking for files and folders
             // We hit the 'Central Directory Header', we can stop because nothing else in here requires our attention
             // or we hit the end of the central directory record, we can safely end the loop as we are totally finished with looking for files and folders
             case "PK":
                 // This case should simply never happen.. but it does exist..
             // This case should simply never happen.. but it does exist..
             case "PK":
                 break 2;
                 // 'Packed to Removable Disk', ignore it and look for the next signature...
             // 'Packed to Removable Disk', ignore it and look for the next signature...
             case 'PK00':
                 continue 2;
                 // We have encountered a header that is weird. Lets look for better data...
             // We have encountered a header that is weird. Lets look for better data...
             default:
                 if (!$dd_try) {
                     // Unexpected header. Trying to detect wrong placed 'Data Descriptor';
                     $dd_try = true;
                     fseek($this->fp, 8, SEEK_CUR);
                     // Jump over 'crc-32'(4) 'compressed-size'(4), 'uncompressed-size'(4)
                     continue 2;
                 }
                 trigger_error("Unexpected header, ending loop");
                 break 2;
         }
         $dd_try = false;
     }
 }
Exemple #28
0
 /**
  * Returns the file data for a file by offsest in the ZIP archive
  *
  * @access	private
  * @param	int		$key	The position of the file in the archive.
  * @return	string	Uncompresed file data buffer
  * @since	1.5
  */
 function _getFileData($key)
 {
     if ($this->_metadata[$key]['_method'] == 0x8) {
         // If zlib extention is loaded use it
         if (extension_loaded('zlib')) {
             return @gzinflate(substr($this->_data, $this->_metadata[$key]['_dataStart'], $this->_metadata[$key]['csize']));
         }
     } elseif ($this->_metadata[$key]['_method'] == 0x0) {
         /* Files that aren't compressed. */
         return substr($this->_data, $this->_metadata[$key]['_dataStart'], $this->_metadata[$key]['csize']);
     } elseif ($this->_metadata[$key]['_method'] == 0x12) {
         // Is bz2 extension loaded?  If not try to load it
         if (!extension_loaded('bz2')) {
             if (ext_isWindows()) {
                 @dl('php_bz2.dll');
             } else {
                 @dl('bz2.so');
             }
         }
         // If bz2 extention is sucessfully loaded use it
         if (extension_loaded('bz2')) {
             return bzdecompress(substr($this->_data, $this->_metadata[$key]['_dataStart'], $this->_metadata[$key]['csize']));
         }
     }
     return '';
 }
Exemple #29
0
 /**
  * Returns the file data for a file by offsest in the ZIP archive
  *
  * @param   integer  $key  The position of the file in the archive.
  *
  * @return  string  Uncompressed file data buffer.
  *
  * @since   11.1
  */
 private function _getFileData($key)
 {
     if ($this->_metadata[$key]['_method'] == 0x8) {
         return gzinflate(substr($this->_data, $this->_metadata[$key]['_dataStart'], $this->_metadata[$key]['csize']));
     } elseif ($this->_metadata[$key]['_method'] == 0x0) {
         /* Files that aren't compressed. */
         return substr($this->_data, $this->_metadata[$key]['_dataStart'], $this->_metadata[$key]['csize']);
     } elseif ($this->_metadata[$key]['_method'] == 0x12) {
         // If bz2 extension is loaded use it
         if (extension_loaded('bz2')) {
             return bzdecompress(substr($this->_data, $this->_metadata[$key]['_dataStart'], $this->_metadata[$key]['csize']));
         }
     }
     return '';
 }
Exemple #30
0
 private function _unzip($filename, $is_data = false)
 {
     // Clear current file
     $this->datasec = array();
     if ($is_data) {
         $this->package['filename'] = 'default.xlsx';
         $this->package['mtime'] = time();
         $this->package['size'] = strlen($filename);
         $vZ = $filename;
     } else {
         if (!is_readable($filename)) {
             $this->error('File not found');
             return false;
         }
         // Package information
         $this->package['filename'] = $filename;
         $this->package['mtime'] = filemtime($filename);
         $this->package['size'] = filesize($filename);
         // Read file
         $oF = fopen($filename, 'rb');
         $vZ = fread($oF, $this->package['size']);
         fclose($oF);
     }
     // Cut end of central directory
     /* 		$aE = explode("\x50\x4b\x05\x06", $vZ);
     
               if (count($aE) == 1) {
               $this->error('Unknown format');
               return false;
               }
              */
     if (($pcd = strrpos($vZ, "PK")) === false) {
         $this->error('Unknown format');
         return false;
     }
     $aE = array(0 => substr($vZ, 0, $pcd), 1 => substr($vZ, $pcd + 3));
     // Normal way
     $aP = unpack('x16/v1CL', $aE[1]);
     $this->package['comment'] = substr($aE[1], 18, $aP['CL']);
     // Translates end of line from other operating systems
     $this->package['comment'] = strtr($this->package['comment'], array("\r\n" => "\n", "\r" => "\n"));
     // Cut the entries from the central directory
     $aE = explode("PK", $vZ);
     // Explode to each part
     $aE = explode("PK", $aE[0]);
     // Shift out spanning signature or empty entry
     array_shift($aE);
     // Loop through the entries
     foreach ($aE as $vZ) {
         $aI = array();
         $aI['E'] = 0;
         $aI['EM'] = '';
         // Retrieving local file header information
         //			$aP = unpack('v1VN/v1GPF/v1CM/v1FT/v1FD/V1CRC/V1CS/V1UCS/v1FNL', $vZ);
         $aP = unpack('v1VN/v1GPF/v1CM/v1FT/v1FD/V1CRC/V1CS/V1UCS/v1FNL/v1EFL', $vZ);
         // Check if data is encrypted
         //			$bE = ($aP['GPF'] && 0x0001) ? TRUE : FALSE;
         $bE = false;
         $nF = $aP['FNL'];
         $mF = $aP['EFL'];
         // Special case : value block after the compressed data
         if ($aP['GPF'] & 0x8) {
             $aP1 = unpack('V1CRC/V1CS/V1UCS', substr($vZ, -12));
             $aP['CRC'] = $aP1['CRC'];
             $aP['CS'] = $aP1['CS'];
             $aP['UCS'] = $aP1['UCS'];
             // 2013-08-10
             $vZ = substr($vZ, 0, -12);
             if (substr($vZ, -4) === "PK") {
                 $vZ = substr($vZ, 0, -4);
             }
         }
         // Getting stored filename
         $aI['N'] = substr($vZ, 26, $nF);
         if (substr($aI['N'], -1) == '/') {
             // is a directory entry - will be skipped
             continue;
         }
         // Truncate full filename in path and filename
         $aI['P'] = dirname($aI['N']);
         $aI['P'] = $aI['P'] == '.' ? '' : $aI['P'];
         $aI['N'] = basename($aI['N']);
         $vZ = substr($vZ, 26 + $nF + $mF);
         if (strlen($vZ) != $aP['CS']) {
             // check only if availabled
             $aI['E'] = 1;
             $aI['EM'] = 'Compressed size is not equal with the value in header information.';
         } else {
             if ($bE) {
                 $aI['E'] = 5;
                 $aI['EM'] = 'File is encrypted, which is not supported from this class.';
             } else {
                 switch ($aP['CM']) {
                     case 0:
                         // Stored
                         // Here is nothing to do, the file ist flat.
                         break;
                     case 8:
                         // Deflated
                         $vZ = gzinflate($vZ);
                         break;
                     case 12:
                         // BZIP2
                         if (!extension_loaded('bz2')) {
                             if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN') {
                                 @dl('php_bz2.dll');
                             } else {
                                 @dl('bz2.so');
                             }
                         }
                         if (extension_loaded('bz2')) {
                             $vZ = bzdecompress($vZ);
                         } else {
                             $aI['E'] = 7;
                             $aI['EM'] = "PHP BZIP2 extension not available.";
                         }
                         break;
                     default:
                         $aI['E'] = 6;
                         $aI['EM'] = "De-/Compression method {$aP['CM']} is not supported.";
                 }
                 if (!$aI['E']) {
                     if ($vZ === FALSE) {
                         $aI['E'] = 2;
                         $aI['EM'] = 'Decompression of data failed.';
                     } else {
                         if (strlen($vZ) != $aP['UCS']) {
                             $aI['E'] = 3;
                             $aI['EM'] = 'Uncompressed size is not equal with the value in header information.';
                         } else {
                             if (crc32($vZ) != $aP['CRC']) {
                                 $aI['E'] = 4;
                                 $aI['EM'] = 'CRC32 checksum is not equal with the value in header information.';
                             }
                         }
                     }
                 }
             }
         }
         $aI['D'] = $vZ;
         // DOS to UNIX timestamp
         $aI['T'] = mktime(($aP['FT'] & 0xf800) >> 11, ($aP['FT'] & 0x7e0) >> 5, ($aP['FT'] & 0x1f) << 1, ($aP['FD'] & 0x1e0) >> 5, $aP['FD'] & 0x1f, (($aP['FD'] & 0xfe00) >> 9) + 1980);
         //$this->Entries[] = &new SimpleUnzipEntry($aI);
         $this->package['entries'][] = array('data' => $aI['D'], 'error' => $aI['E'], 'error_msg' => $aI['EM'], 'name' => $aI['N'], 'path' => $aI['P'], 'time' => $aI['T']);
     }
     // end for each entries
 }