public function onRun()
 {
     try {
         $this->final = zlib_encode($this->data, ZLIB_ENCODING_DEFLATE, $this->level);
         $this->data = null;
     } catch (\Throwable $e) {
     }
 }
 public function onRun()
 {
     try {
         $this->final = \zlib_encode($this->data, ZLIB_ENCODING_DEFLATE, $this->level);
         $this->data = \null;
     } catch (\Exception $e) {
     }
 }
Example #3
0
 /**
  * 
  */
 static function encode($data)
 {
     $compressedData = zlib_encode($data, ZLIB_ENCODING_DEFLATE);
     if ($compressedData) {
         return base64_encode($compressedData);
     }
     return false;
 }
 public function saveSkin($human, $fn, $folder = null)
 {
     if ($folder === null) {
         $folder = $this->owner->getDataFolder();
     }
     $bin = zlib_encode($human->getSkinData(), ZLIB_ENCODING_DEFLATE, 9);
     file_put_contents($folder . $fn, $bin);
     return strlen($bin);
 }
Example #5
0
 public function encode($data = '', $level = -1, $encoding = ZLIB_ENCODING_GZIP)
 {
     if (!is_scalar($data)) {
         return Error::set('Error', 'valueParameter', '1.(data)');
     }
     if (!is_numeric($level) || !is_numeric($encoding)) {
         return Error::set('Error', 'numericParameter', '2.(level) & 3.(encoding)');
     }
     return zlib_encode($data, $encoding, $level);
 }
Example #6
0
 public function onRun()
 {
     $old = json_decode(zlib_decode(file_get_contents($this->path)));
     if (is_object($old)) {
         $time = $old->registerTime;
         if ($time !== -1) {
             $this->isReg = false;
         }
     }
     file_put_contents($this->path, zlib_encode($this->contents, ZLIB_ENCODING_DEFLATE));
 }
Example #7
0
 private function encodeData()
 {
     $this->addLine();
     $this->addLine("----------------------REPORT THE DATA BELOW THIS LINE-----------------------");
     $this->addLine();
     $this->addLine("===BEGIN CRASH DUMP===");
     $this->encodedData = zlib_encode(json_encode($this->data, JSON_UNESCAPED_SLASHES), ZLIB_ENCODING_DEFLATE, 9);
     foreach (str_split(base64_encode($this->encodedData), 76) as $line) {
         $this->addLine($line);
     }
     $this->addLine("===END CRASH DUMP===");
 }
 public function write(AccountInfo $info)
 {
     $path = $this->getPath($info->name);
     if ($isOverwrite = is_file($path)) {
         if (!$this->overwrite) {
             return;
         }
     }
     file_put_contents($path, zlib_encode($info->serialize(), ZLIB_ENCODING_DEFLATE));
     if (!$isOverwrite and $info->registerTime !== -1) {
         $stmt = $this->sql->prepare("INSERT OR REPLACE INTO reg (ip, name, time) VALUES (:ip, :name, :time)");
         $stmt->bindValue(":ip", $info->lastIp, SQLITE3_TEXT);
         $stmt->bindValue(":name", strtolower($info->name), SQLITE3_TEXT);
         $stmt->bindValue(":time", $info->registerTime, SQLITE3_INTEGER);
         $stmt->execute();
     }
 }
Example #9
0
 public function onRun()
 {
     if (is_file($this->newPath)) {
         $this->success = Database::RENAME_TARGET_PRESENT;
         return;
     }
     if (!is_file($this->oldPath)) {
         $this->setResult("File didn't exist", false);
         $this->success = Database::RENAME_SOURCE_ABSENT;
         return;
     }
     if (!is_dir($dir = dirname($this->newPath))) {
         mkdir($dir);
     }
     $data = json_decode(zlib_decode(file_get_contents($this->oldPath)));
     $data->multiHash = ["renamed;{$this->oldName}" => $data->passwordHash];
     $data->passwordHash = "{RENAMED}";
     unlink($this->oldPath);
     file_put_contents($this->newPath, zlib_encode(json_encode($data), ZLIB_ENCODING_DEFLATE));
     $this->success = Database::SUCCESS;
 }
Example #10
0
 /**
  * Broadcasts a list of packets in a batch to a list of players
  *
  * @param Player[]            $players
  * @param DataPacket[]|string $packets
  * @param bool                 $forceSync
  */
 public function batchPackets(array $players, array $packets, $forceSync = false)
 {
     $str = "";
     foreach ($packets as $p) {
         if ($p instanceof DataPacket) {
             if (!$p->isEncoded) {
                 $p->encode();
             }
             $str .= $p->buffer;
         } else {
             $str .= $p;
         }
     }
     $targets = [];
     foreach ($players as $p) {
         $targets[] = $this->identifiers[\spl_object_hash($p)];
     }
     if (!$forceSync and $this->networkCompressionAsync) {
         $task = new CompressBatchedTask($str, $targets, $this->networkCompressionLevel);
         $this->getScheduler()->scheduleAsyncTask($task);
     } else {
         $this->broadcastPacketsCallback(\zlib_encode($str, ZLIB_ENCODING_DEFLATE, $this->networkCompressionLevel), $targets);
     }
 }
Example #11
0
 private function compressPacket($packet)
 {
     $packet = $this->uncompressedOut . $packet;
     if ($packet == "") {
         return "";
     }
     $len = strlen($packet);
     while ($len < self::MAX_UNCOMPRESSED_BUFLEN && !empty($this->out)) {
         $packet .= $this->compilePacket();
         $len = strlen($this->uncompressedOut);
     }
     $this->uncompressedOut = substr($packet, self::MAX_UNCOMPRESSED_BUFLEN);
     $packet = substr($packet, 0, self::MAX_UNCOMPRESSED_BUFLEN);
     $len = strlen($packet);
     $deflated = zlib_encode($packet, ZLIB_ENCODING_DEFLATE);
     if ($len < strlen($deflated)) {
         $out = substr_replace(pack("V", strlen($packet)), chr(++$this->compressionId), 3, 1) . "" . $packet;
     } else {
         $out = substr_replace(pack("V", strlen($deflated)), chr(++$this->compressionId), 3, 1) . substr(pack("V", $len), 0, 3) . $deflated;
     }
     return $out;
 }
 public function doSlowCleanUp()
 {
     for ($i = 0; $i < 1024; ++$i) {
         if ($this->locationTable[$i][0] === 0 or $this->locationTable[$i][1] === 0) {
             continue;
         }
         fseek($this->filePointer, $this->locationTable[$i][0] << 12);
         $chunk = fread($this->filePointer, $this->locationTable[$i][1] << 12);
         $length = Binary::readInt(substr($chunk, 0, 4));
         if ($length <= 1) {
             $this->locationTable[$i] = [0, 0, 0];
             //Non-generated chunk, remove it from index
         }
         try {
             $chunk = zlib_decode(substr($chunk, 5));
         } catch (\Throwable $e) {
             $this->locationTable[$i] = [0, 0, 0];
             //Corrupted chunk, remove it
             continue;
         }
         $chunk = chr(self::COMPRESSION_ZLIB) . zlib_encode($chunk, ZLIB_ENCODING_DEFLATE, 9);
         $chunk = Binary::writeInt(strlen($chunk)) . $chunk;
         $sectors = (int) ceil(strlen($chunk) / 4096);
         if ($sectors > $this->locationTable[$i][1]) {
             $this->locationTable[$i][0] = $this->lastSector + 1;
             $this->lastSector += $sectors;
         }
         fseek($this->filePointer, $this->locationTable[$i][0] << 12);
         fwrite($this->filePointer, str_pad($chunk, $sectors << 12, "", STR_PAD_RIGHT));
     }
     $this->writeLocationTable();
     $n = $this->cleanGarbage();
     $this->writeLocationTable();
     return $n;
 }
Example #13
0
 public function onRun()
 {
     $orderedIds = "";
     $orderedData = "";
     $orderedSkyLight = "";
     $orderedLight = "";
     $ids = "";
     $meta = "";
     $blockLight = "";
     $skyLight = "";
     foreach ($this->sections as $section) {
         $ids .= $section->getIdArray();
         $meta .= $section->getDataArray();
         $blockLight .= $section->getLightArray();
         $skyLight .= $section->getSkyLightArray();
     }
     for ($x = 0; $x < 16; ++$x) {
         for ($z = 0; $z < 16; ++$z) {
             $orderedIds .= $this->getColumn($ids, $x, $z);
             $orderedData .= $this->getHalfColumn($meta, $x, $z);
             $orderedSkyLight .= $this->getHalfColumn($skyLight, $x, $z);
             $orderedLight .= $this->getHalfColumn($blockLight, $x, $z);
         }
     }
     $biomeColors = pack("N*", ...$this->biomeColors);
     $ordered = zlib_encode(Binary::writeLInt($this->chunkX) . Binary::writeLInt($this->chunkZ) . $orderedIds . $orderedData . $orderedSkyLight . $orderedLight . $this->biomeIds . $biomeColors . $this->tiles, ZLIB_ENCODING_DEFLATE, $this->compressionLevel);
     $this->setResult($ordered);
 }
Example #14
0
 public function saveChunkToDisk($x, $z, $payload, $ordering = FullChunkDataPacket::ORDER_COLUMNS)
 {
     // When the payload of the chunk has been calculated it, save it if possible to save future CPU cycles
     /** @var Player $player */
     if (file_exists("chunk_cache/" . $this->getName() . "/" . $x . "_" . $z . ".dat")) {
         $this->loadChunkFromDisk($x, $z);
         return true;
     }
     $pk = new FullChunkDataPacket();
     $pk->chunkX = $x;
     $pk->chunkZ = $z;
     $pk->order = $ordering;
     $pk->data = $payload;
     $pk->encode();
     // all chunks are zlib_encoded, level is arbitrary but 6 is a good match between device CPU power needed
     // and bandwidth
     $data = zlib_encode(Binary::writeInt(strlen($pk->buffer)) . $pk->buffer, ZLIB_ENCODING_DEFLATE, 6);
     $this->chunkCache[$x . ":" . $z] = $data;
     if (!$this->server->getKatana()->getProperty("cache.save-to-disk", true)) {
         return true;
     }
     file_put_contents("chunk_cache/" . $this->getName() . "/" . $x . "_" . $z . ".dat", $data);
     return true;
 }
Example #15
0
 public function writeCompressed($compression = ZLIB_ENCODING_GZIP, $level = 7)
 {
     if (($write = $this->write()) !== false) {
         return zlib_encode($write, $compression, $level);
     }
     return false;
 }
Example #16
0
 public function getEncoded()
 {
     return "===BEGIN CRASH DUMP===" . PHP_EOL . base64_encode(zlib_encode(json_encode($this->data, JSON_UNESCAPED_SLASHES), ZLIB_ENCODING_DEFLATE, 9)) . PHP_EOL . "===END CRASH DUMP===";
 }
Example #17
0
 /**
  * Batch a Data packet
  *
  * @param DataPacket $packet
  *
  * @return bool
  */
 public function batchDataPacket(DataPacket $packet)
 {
     $str = "";
     if ($packet instanceof DataPacket) {
         if (!$packet->isEncoded) {
             $packet->encode();
         }
         $str .= Binary::writeInt(strlen($packet->buffer)) . $packet->buffer;
     } else {
         $str .= Binary::writeInt(strlen($packet)) . $packet;
     }
     $pk = new BatchPacket();
     $pk->payload = zlib_encode($str, ZLIB_ENCODING_DEFLATE, 7);
     $pk->encode();
     $pk->isEncoded = true;
     $this->dataPacket($pk);
 }
Example #18
0
 public function requestChunkTask($x, $z)
 {
     $chunk = $this->getChunk($x, $z, false);
     if (!$chunk instanceof Chunk) {
         throw new ChunkException("Invalid Chunk sent");
     }
     $tiles = "";
     $nbt = new NBT(NBT::LITTLE_ENDIAN);
     foreach ($chunk->getTiles() as $tile) {
         if ($tile instanceof Spawnable) {
             $nbt->setData($tile->getSpawnCompound());
             $tiles .= $nbt->write();
         }
     }
     $extraData = new BinaryStream();
     $extraData->putLInt(count($chunk->getBlockExtraDataArray()));
     foreach ($chunk->getBlockExtraDataArray() as $key => $value) {
         $extraData->putLInt($key);
         $extraData->putLShort($value);
     }
     $ordered = $chunk->getBlockIdArray() . $chunk->getBlockDataArray() . $chunk->getBlockSkyLightArray() . $chunk->getBlockLightArray() . pack("C*", ...$chunk->getHeightMapArray()) . pack("N*", ...$chunk->getBiomeColorArray()) . $extraData->getBuffer() . $tiles;
     $pk = new FullChunkDataPacket();
     $pk->chunkX = $x;
     $pk->chunkZ = $z;
     $pk->order = FullChunkDataPacket::ORDER_COLUMNS;
     $pk->data = $ordered;
     $pk->encode();
     $str = "";
     $str .= Binary::writeInt(strlen($pk->buffer)) . $pk->buffer;
     $this->getLevel()->chunkRequestCallback($x, $z, zlib_encode($str, ZLIB_ENCODING_DEFLATE, 7));
     return null;
 }
Example #19
0
 /**
  * @param $chunkX
  * @param $chunkZ
  * @param $payload
  *
  * @return DataPacket
  */
 public static function getChunkCacheFromData($chunkX, $chunkZ, $payload)
 {
     $pk = new FullChunkDataPacket();
     $pk->chunkX = $chunkX;
     $pk->chunkZ = $chunkZ;
     $pk->data = $payload;
     $pk->encode();
     $batch = new BatchPacket();
     $batch->payload = zlib_encode($pk->getBuffer(), ZLIB_ENCODING_DEFLATE, Server::getInstance()->networkCompressionLevel);
     $batch->setChannel(Network::CHANNEL_WORLD_CHUNKS);
     $batch->encode();
     $batch->isEncoded = true;
     return $batch;
 }
Example #20
0
<?php

$key = "TEST_KEY_COMPRESSION_SET";
$tmp_object = new stdClass();
$tmp_object->str_attr = "אבגabcéàç";
$tmp_object->str2 = str_repeat("0", 100);
$tmp_object->int_attr = 123;
$serialized = serialize($tmp_object);
$compressed = zlib_encode($serialized, ZLIB_ENCODING_DEFLATE);
$memcache = new Memcache();
$memcache->addServer('localhost', 11211);
$memcache->set($key, $tmp_object, MEMCACHE_COMPRESSED);
$socket = stream_socket_client('localhost:11211');
fwrite($socket, "get " . $key . "\r\n");
$line1 = fgets($socket);
$line2 = fgets($socket);
$line3 = fgets($socket);
fclose($socket);
$line1_parts = explode(' ', substr($line1, 0, -2));
$line2 = substr($line2, 0, -2);
var_dump($line1_parts);
var_dump($line2 === $compressed);
$memcache->delete($key);
 private function setEncodingToUse($encodingUsed)
 {
     switch ($encodingUsed) {
         case 'identity':
             $this->compressFunction = function ($data) {
                 return $data;
             };
             $this->outputResponse->removeHeader('Content-Encoding');
             break;
         case 'x-gzip':
         case 'gzip':
             $this->compressFunction = function ($data) {
                 return gzencode($data);
             };
             $this->outputResponse->setHeader('Content-Encoding', 'gzip');
             break;
         case 'deflate':
             $this->compressFunction = function ($data) {
                 return zlib_encode($data, 15);
             };
             $this->outputResponse->setHeader('Content-Encoding', 'deflate');
             break;
         case 'bzip2':
             $this->compressFunction = function ($data) {
                 return bzcompress($data);
             };
             $this->outputResponse->setHeader('Content-Encoding', 'bzip2');
             break;
         default:
             throw new \LogicException('Unknown encoding');
     }
 }
                $inflated = null;
            }
        } else {
            $inflated = inflate_add($resource, $buffer, ZLIB_FINISH);
        }
    }
}
$modes = ['ZLIB_ENCODING_RAW' => ZLIB_ENCODING_RAW, 'ZLIB_ENCODING_GZIP' => ZLIB_ENCODING_GZIP, 'ZLIB_ENCODING_DEFLATE' => ZLIB_ENCODING_DEFLATE];
$flushSizes = [1, 4, 32768];
$flushTypes = ['ZLIB_SYNC_FLUSH' => ZLIB_SYNC_FLUSH, 'ZLIB_PARTIAL_FLUSH' => ZLIB_PARTIAL_FLUSH, 'ZLIB_FULL_FLUSH' => ZLIB_FULL_FLUSH, 'ZLIB_NO_FLUSH' => ZLIB_NO_FLUSH, 'ZLIB_BLOCK' => ZLIB_BLOCK];
$uncompressed = "";
for ($i = 0; $i < 32768 * 2; $i++) {
    $uncompressed .= chr(rand(48, 125));
}
foreach ($modes as $modeKey => $mode) {
    $compressed = zlib_encode($uncompressed, $mode);
    $compressedLen = strlen($compressed);
    foreach ($flushSizes as $flushSize) {
        foreach ($flushTypes as $flushTypeKey => $flushType) {
            $inflated = "";
            $stream = inflateStream($mode, $flushSize, $flushType);
            for ($i = 0; $i < $compressedLen; $i++) {
                $inflated .= $stream->send($compressed[$i]);
            }
            $inflated .= $stream->send(null);
            if ($inflated !== $uncompressed) {
                echo "Error: {$modeKey} | {$flushSize} | {$flushTypeKey}\n";
            }
        }
    }
}
Example #23
0
    /**
     * Logic to import csv files with content item data
     *
     * @access public
     * @return void
     * @since 1.5
     */
    function importcsv()
    {
        // Check for request forgeries
        if (JRequest::getCmd('task') != 'importcsv') {
            JRequest::checkToken() or jexit('Invalid Token');
            echo '<link rel="stylesheet" href="' . JURI::base(true) . '/components/com_flexicontent/assets/css/flexicontentbackend.css?' . FLEXI_VERSION . '" />';
            $fc_css = JURI::base(true) . '/components/com_flexicontent/assets/css/j3x.css';
            echo '<link rel="stylesheet" href="' . $fc_css . '?' . FLEXI_VERSION . '" />';
        } else {
            // output this before every other output
            echo 'success||||' . (FLEXI_J30GE ? JSession::getFormToken() : JUtility::getToken()) . '||||';
        }
        // Get item model
        $itemmodel = $this->getModel('item');
        $model = $this->getModel('import');
        // Set some variables
        $link = 'index.php?option=com_flexicontent&view=import';
        // $_SERVER['HTTP_REFERER'];
        $task = JRequest::getCmd('task');
        $app = JFactory::getApplication();
        $db = JFactory::getDBO();
        $user = JFactory::getUser();
        $session = JFactory::getSession();
        $has_zlib = version_compare(PHP_VERSION, '5.4.0', '>=');
        $parse_log = "\n\n\n" . '<b>please click</b> <a href="' . $link . '">here</a> to return previous page' . "\n\n\n";
        $log_filename = 'importcsv_' . $user->id . '.php';
        jimport('joomla.log.log');
        JLog::addLogger(array('text_file' => $log_filename));
        // *************************
        // Execute according to task
        // *************************
        switch ($task) {
            // ***********************************************************************************************
            // RESET/CLEAR an already started import task, e.g. import process was interrupted for some reason
            // ***********************************************************************************************
            case 'clearcsv':
                // Clear any import data from session
                $conf = $has_zlib ? base64_encode(zlib_encode(serialize(null), -15)) : base64_encode(serialize(null));
                $session->set('csvimport_config', $conf, 'flexicontent');
                $session->set('csvimport_lineno', 0, 'flexicontent');
                // Set a message that import task was cleared and redirect
                $app->enqueueMessage('Import task cleared', 'notice');
                $this->setRedirect($link);
                return;
                break;
                // ****************************************************
                // CONTINUE an already started (multi-step) import task
                // ****************************************************
            // ****************************************************
            // CONTINUE an already started (multi-step) import task
            // ****************************************************
            case 'importcsv':
                $conf = $session->get('csvimport_config', "", 'flexicontent');
                $conf = unserialize($conf ? $has_zlib ? zlib_decode(base64_decode($conf)) : base64_decode($conf) : "");
                $lineno = $session->get('csvimport_lineno', 999999, 'flexicontent');
                if (empty($conf)) {
                    $app->enqueueMessage('Can not continue import, import task not initialized or already finished', 'error');
                    $this->setRedirect($link);
                    return;
                }
                // CONTINUE to do the import
                // ...
                break;
                // *************************************************************************
                // INITIALIZE (prepare) import by getting configuration and reading CSV file
                // *************************************************************************
            // *************************************************************************
            // INITIALIZE (prepare) import by getting configuration and reading CSV file
            // *************************************************************************
            case 'initcsv':
            case 'testcsv':
                $conf = array();
                $conf['failure_count'] = $conf['success_count'] = 0;
                // Retrieve Basic configuration
                $conf['type_id'] = JRequest::getInt('type_id', 0);
                $conf['language'] = JRequest::getVar('language', '');
                $conf['state'] = JRequest::getInt('state', '');
                $conf['access'] = JRequest::getInt('access', '');
                // Main and secondary categories
                $conf['maincat'] = JRequest::getInt('maincat', 0);
                $conf['maincat_col'] = JRequest::getInt('maincat_col', 0);
                $conf['seccats'] = JRequest::getVar('seccats', array(), 'post', 'array');
                $conf['seccats_col'] = JRequest::getInt('seccats_col', 0);
                // Tags
                $conf['tags_col'] = JRequest::getInt('tags_col', 0);
                // Publication: META data
                $conf['created_by_col'] = JRequest::getInt('created_by_col', 0);
                $conf['modified_by_col'] = JRequest::getInt('modified_by_col', 0);
                // Publication: META data
                $conf['metadesc_col'] = JRequest::getInt('metadesc_col', 0);
                $conf['metakey_col'] = JRequest::getInt('metakey_col', 0);
                // Publication: dates
                $conf['modified_col'] = JRequest::getInt('modified_col', 0);
                $conf['created_col'] = JRequest::getInt('created_col', 0);
                $conf['publish_up_col'] = JRequest::getInt('publish_up_col', 0);
                $conf['publish_down_col'] = JRequest::getInt('publish_down_col', 0);
                // Advanced configuration
                $conf['ignore_unused_cols'] = JRequest::getInt('ignore_unused_cols', 0);
                $conf['id_col'] = JRequest::getInt('id_col', 0);
                $conf['items_per_step'] = JRequest::getInt('items_per_step', 5);
                if ($conf['items_per_step'] > 50) {
                    $conf['items_per_step'] = 50;
                }
                if (!$conf['items_per_step']) {
                    $conf['items_per_step'] = 5;
                }
                // CSV file format
                $conf['mval_separator'] = JRequest::getVar('mval_separator');
                $conf['mprop_separator'] = JRequest::getVar('mprop_separator');
                $conf['field_separator'] = JRequest::getVar('field_separator');
                $conf['enclosure_char'] = JRequest::getVar('enclosure_char');
                $conf['record_separator'] = JRequest::getVar('record_separator');
                $conf['debug_records'] = JRequest::getInt('debug_records', 0);
                // Debug, print parsed data without importing
                // ********************************************************************************************
                // Obligatory form fields, js validation should have prevented form submission but check anyway
                // ********************************************************************************************
                // Check for the required Content Type Id
                if (!$conf['type_id']) {
                    $app->enqueueMessage('Please select Content Type for the imported items', 'error');
                    $app->redirect($link);
                }
                // Check for the required main category
                if (!$conf['maincat'] && !$conf['maincat_col']) {
                    $app->enqueueMessage('Please select main category for the imported items', 'error');
                    $app->redirect($link);
                }
                // ********************************************************************************************************************
                // Check for (required) CSV file format variables, js validation should have prevented form submission but check anyway
                // ********************************************************************************************************************
                if ($conf['mval_separator'] == '' || $conf['mprop_separator'] == '') {
                    $app->enqueueMessage('CSV format not valid, please enter multi-value, and multi-property Separators', 'error');
                    $app->redirect($link);
                }
                if ($conf['field_separator'] == '' || $conf['record_separator'] == '') {
                    $app->enqueueMessage('CSV format not valid, please enter Field Separator and Item Separator', 'error');
                    $app->redirect($link);
                }
                // Retrieve the uploaded CSV file
                $csvfile = @$_FILES["csvfile"]["tmp_name"];
                if (!is_file($csvfile)) {
                    $app->enqueueMessage('Upload file error!', 'error');
                    $app->redirect($link);
                }
                // ******************************************************************************************************
                // Retrieve CSV file format variables, EXPANDING the Escape Characters like '\n' ... provided by the form
                // ******************************************************************************************************
                $pattern = '/(?<!\\\\)(\\\\(?:n|r|t|v|f|[0-7]{1,3}|x[0-9a-f]{1,2}))/i';
                $replace = 'eval(\'return "$1";\')';
                $conf['mval_separator'] = preg_replace_callback($pattern, function ($matches) {
                    $r = $matches[1];
                    eval("\$r = \"{$r}\";");
                    return $r;
                }, $conf['mval_separator']);
                $conf['mprop_separator'] = preg_replace_callback($pattern, function ($matches) {
                    $r = $matches[1];
                    eval("\$r = \"{$r}\";");
                    return $r;
                }, $conf['mprop_separator']);
                $conf['field_separator'] = preg_replace_callback($pattern, function ($matches) {
                    $r = $matches[1];
                    eval("\$r = \"{$r}\";");
                    return $r;
                }, $conf['field_separator']);
                $conf['enclosure_char'] = preg_replace_callback($pattern, function ($matches) {
                    $r = $matches[1];
                    eval("\$r = \"{$r}\";");
                    return $r;
                }, $conf['enclosure_char']);
                $conf['record_separator'] = preg_replace_callback($pattern, function ($matches) {
                    $r = $matches[1];
                    eval("\$r = \"{$r}\";");
                    return $r;
                }, $conf['record_separator']);
                // ****************************************************
                // Read & Parse the CSV file according the given format
                // ****************************************************
                $contents = FLEXIUtilities::csvstring_to_array(file_get_contents($csvfile), $conf['field_separator'], $conf['enclosure_char'], $conf['record_separator']);
                // Basic error checking, for empty data
                if (!$contents || count($contents[0]) <= 0) {
                    $app->enqueueMessage('CSV file format is not correct!', 'error');
                    $app->redirect($link);
                }
                // ********************************************************************************
                // Get field names (from the header line (row 0), and remove it form the data array
                // ********************************************************************************
                $conf['columns'] = flexicontent_html::arrayTrim($contents[0]);
                unset($contents[0]);
                $q = 'SELECT id, name, field_type, label FROM #__flexicontent_fields AS fi' . ' JOIN #__flexicontent_fields_type_relations AS ftrel ON ftrel.field_id = fi.id AND ftrel.type_id=' . $conf['type_id'];
                $db->setQuery($q);
                $conf['thefields'] = $db->loadObjectList('name');
                unset($conf['thefields']['tags']);
                // Prevent Automated Raw insertion of tags, we will use special code
                // ******************************************************************
                // Check for REQUIRED columns and decide CORE property columns to use
                // ******************************************************************
                $core_props = array();
                if ($conf['id_col'] && !in_array('id', $conf['columns'])) {
                    $app->enqueueMessage('CSV file lacks column <b>\'id\'</b> (Item ID)', 'error');
                    $app->redirect($link);
                } else {
                    if ($conf['id_col']) {
                        $core_props['id'] = 'Item ID';
                    }
                }
                if (!in_array('title', $conf['columns'])) {
                    $app->enqueueMessage('CSV file lacks column <b>\'title\'</b>', 'error');
                    $app->redirect($link);
                }
                $core_props['title'] = 'Title (core)';
                $core_props['text'] = 'Description (core)';
                $core_props['alias'] = 'Alias (core)';
                if (!$conf['language'] && !in_array('language', $conf['columns'])) {
                    $app->enqueueMessage('CSV file lacks column <b>\'language\'</b>', 'error');
                    $app->redirect($link);
                } else {
                    if (!$conf['language']) {
                        $core_props['language'] = 'Language';
                    }
                }
                if (!strlen($conf['state']) && !in_array('state', $conf['columns'])) {
                    $app->enqueueMessage('CSV file lacks column <b>\'state\'</b>', 'error');
                    $app->redirect($link);
                } else {
                    if (!strlen($conf['state'])) {
                        $core_props['state'] = 'State';
                    }
                }
                if ($conf['access'] === 0 && !in_array('access', $conf['columns'])) {
                    $app->enqueueMessage('CSV file lacks column <b>\'access\'</b>', 'error');
                    $app->redirect($link);
                } else {
                    if ($conf['access'] === 0) {
                        $core_props['access'] = 'Access';
                    }
                }
                if ($conf['maincat_col'] && !in_array('catid', $conf['columns'])) {
                    $app->enqueueMessage('CSV file lacks column <b>\'catid\'</b> (Primary category)', 'error');
                    $app->redirect($link);
                } else {
                    if ($conf['maincat_col']) {
                        $core_props['catid'] = 'Primary category';
                    }
                }
                if ($conf['seccats_col'] && !in_array('cid', $conf['columns'])) {
                    $app->enqueueMessage('CSV file lacks column <b>\'cid\'</b> (Secondary categories)', 'error');
                    $app->redirect($link);
                } else {
                    if ($conf['seccats_col']) {
                        $core_props['cid'] = 'Secondary categories';
                    }
                }
                if ($conf['created_col'] && !in_array('created', $conf['columns'])) {
                    $app->enqueueMessage('CSV file lacks column <b>\'created\'</b> (Creation date)', 'error');
                    $app->redirect($link);
                } else {
                    if ($conf['created_col']) {
                        $core_props['created'] = 'Creation Date';
                    }
                }
                if ($conf['created_by_col'] && !in_array('created_by', $conf['columns'])) {
                    $app->enqueueMessage('CSV file lacks column <b>\'created_by\'</b> (Creator - Author)', 'error');
                    $app->redirect($link);
                } else {
                    if ($conf['created_by_col']) {
                        $core_props['created_by'] = 'Creator (Author)';
                    }
                }
                if ($conf['modified_col'] && !in_array('modified', $conf['columns'])) {
                    $app->enqueueMessage('CSV file lacks column <b>\'modified\'</b> (Modification date)', 'error');
                    $app->redirect($link);
                } else {
                    if ($conf['modified_col']) {
                        $core_props['modified'] = 'Modification Date';
                    }
                }
                if ($conf['modified_by_col'] && !in_array('modified_by', $conf['columns'])) {
                    $app->enqueueMessage('CSV file lacks column <b>\'modified_by\'</b> (Last modifier)', 'error');
                    $app->redirect($link);
                } else {
                    if ($conf['modified_by_col']) {
                        $core_props['modified_by'] = 'Last modifier';
                    }
                }
                if ($conf['metadesc_col'] && !in_array('metadesc', $conf['columns'])) {
                    $app->enqueueMessage('CSV file lacks column <b>\'metadesc\'</b> (META Description)', 'error');
                    $app->redirect($link);
                } else {
                    if ($conf['metadesc_col']) {
                        $core_props['metadesc'] = 'META Description';
                    }
                }
                if ($conf['metakey_col'] && !in_array('metakey', $conf['columns'])) {
                    $app->enqueueMessage('CSV file lacks column <b>\'metakey\'</b> (META Keywords)', 'error');
                    $app->redirect($link);
                } else {
                    if ($conf['metakey_col']) {
                        $core_props['metakey'] = 'META Keywords';
                    }
                }
                if ($conf['publish_up_col'] && !in_array('publish_up', $conf['columns'])) {
                    $app->enqueueMessage('CSV file lacks column <b>\'publish_up\'</b> (Start publication date)', 'error');
                    $app->redirect($link);
                } else {
                    if ($conf['publish_up_col']) {
                        $core_props['publish_up'] = 'Start publication date';
                    }
                }
                if ($conf['publish_down_col'] && !in_array('publish_down', $conf['columns'])) {
                    $app->enqueueMessage('CSV file lacks column <b>\'publish_down\'</b> (End publication Date)', 'error');
                    $app->redirect($link);
                } else {
                    if ($conf['publish_down_col']) {
                        $core_props['publish_down'] = 'End publication Date';
                    }
                }
                if ($conf['tags_col'] == 1 && !in_array('tags_names', $conf['columns'])) {
                    $app->enqueueMessage('CSV file lacks column <b>\'tags_names\'</b> (Comma separated list of tag names)', 'error');
                    $app->redirect($link);
                } else {
                    if ($conf['tags_col'] == 1) {
                        $core_props['tags_names'] = 'Tag names';
                        $tags_model = $this->getModel('tags');
                    }
                }
                if ($conf['tags_col'] == 2 && !in_array('tags_raw', $conf['columns'])) {
                    $app->enqueueMessage('CSV file lacks column <b>\'tags_raw\'</b> (Comma separated list of tag ids)', 'error');
                    $app->redirect($link);
                } else {
                    if ($conf['tags_col'] == 2) {
                        $core_props['tags_raw'] = 'Tags';
                        $tags_model = $this->getModel('tags');
                    }
                }
                $conf['core_props'] =& $core_props;
                // *********************************************************
                // Verify that all non core property columns are field names
                // *********************************************************
                $unused_columns = array();
                foreach ($conf['columns'] as $colname) {
                    if (!isset($conf['core_props'][$colname]) && !isset($conf['thefields'][$colname])) {
                        $unused_columns[] = $colname;
                        if ($conf['ignore_unused_cols']) {
                            JError::raiseNotice(500, "Column '" . $colname . "' : &nbsp; field name NOT FOUND (column will be ignored)");
                        }
                    }
                }
                if (count($unused_columns) && !$conf['ignore_unused_cols']) {
                    $app->enqueueMessage('
					File has unused ' . count($unused_columns) . ' columns: <b>' . implode(', ', $unused_columns) . '</b>' . ' <br/>these field names are not assigned to choosen <b>content type</b>' . ' <br/><br/>please enable option: <b>\'Ignore unused columns\'</b>', 'error');
                    $app->redirect($link);
                }
                // **********************************************************
                // Verify that custom specified item ids do not already exist
                // **********************************************************
                if ($conf['id_col']) {
                    // Get 'id' column no
                    $id_col_no = 0;
                    foreach ($conf['columns'] as $col_no => $column) {
                        if ($conf['columns'][$col_no] == 'id') {
                            $id_col_no = $col_no;
                            break;
                        }
                    }
                    // Get custom IDs in csv file
                    $custom_id_arr = array();
                    foreach ($contents as $fields) {
                        $custom_id_arr[] = $fields[$id_col_no];
                    }
                    $custom_id_list = "'" . implode("','", $custom_id_arr) . "'";
                    // Cross check them if they already exist in the DB
                    $q = "SELECT id FROM #__content WHERE id IN (" . $custom_id_list . ")";
                    $db->setQuery($q);
                    $existing_ids = FLEXI_J16GE ? $db->loadColumn() : $db->loadResultArray();
                    if ($existing_ids && count($existing_ids)) {
                        $app->enqueueMessage('File has ' . count($existing_ids) . ' item IDs that already exist: \'' . implode("\\' , \\'", $existing_ids) . '\', please fix or set to ignore \'id\' column', 'error');
                        $app->redirect($link);
                    }
                }
                // Trim item's data
                foreach ($contents as $fields) {
                    $fields = flexicontent_html::arrayTrim($fields);
                }
                // Set csvfile contens and columns information
                $conf['contents'] =& $contents;
                // ***************************************************************
                // Verify that imported files exist in the media/documents folders
                // ***************************************************************
                // Get fields that use files
                $conf['media_folder'] = JRequest::getVar('media_folder');
                $conf['docs_folder'] = JRequest::getVar('docs_folder');
                $this->checkfiles($conf, $parse_log, $task);
                $this->parsevalues($conf, $parse_log, $task);
                if ($task == 'initcsv') {
                    // Set import configuration and file data into session
                    $session->set('csvimport_config', $has_zlib ? base64_encode(zlib_encode(serialize($conf), -15)) : base64_encode(serialize($conf)), 'flexicontent');
                    $session->set('csvimport_lineno', 0, 'flexicontent');
                    // Set a message that import task was prepared and redirect
                    $app->enqueueMessage('Import task prepared. <br/>' . 'File has ' . count($conf['contents_parsed']) . ' records (content items)' . ' and ' . count($conf['columns']) . ' columns (fields)', 'message');
                    $this->setRedirect($link);
                    return;
                } else {
                    // task == 'testcsv'
                    $conf['debug_records'] = $conf['debug_records'] ? $conf['debug_records'] : 2;
                }
                break;
                // ************************
                // UNKNWOWN task, terminate
                // ************************
            // ************************
            // UNKNWOWN task, terminate
            // ************************
            default:
                // Set an error message about unknown task and redirect
                $app->enqueueMessage('Unknown task: ' . $task, 'error');
                $this->setRedirect($link);
                return;
                break;
        }
        // *********************************************************************************
        // Handle each row (item) using store() method of the item model to create the items
        // *********************************************************************************
        if ($conf['tags_col']) {
            $tags_model = $this->getModel('tags');
        }
        $colcount = count($conf['columns']);
        $itemcount = count($conf['contents_parsed']);
        $items_per_call = JRequest::getInt('items_per_call', 0);
        JRequest::setVar('import_media_folder', $conf['media_folder']);
        JRequest::setVar('import_docs_folder', $conf['docs_folder']);
        $lineno = $task == 'testcsv' ? 1 : $lineno + 1;
        $linelim = $items_per_call ? $lineno + $items_per_call - 1 : $itemcount;
        $linelim = $linelim > $itemcount ? $itemcount : $linelim;
        //echo "lineno: $lineno -- linelim: $linelim<br/>";
        for (; $lineno <= $linelim; $lineno++) {
            $_d =& $conf['contents_parsed'][$lineno];
            $data = array();
            $data['type_id'] = $conf['type_id'];
            $data['language'] = $conf['language'];
            $data['catid'] = $conf['maincat'];
            // Default value maybe overriden by column
            $data['cid'] = $conf['seccats'];
            // Default value maybe overriden by column
            $data['vstate'] = 2;
            $data['state'] = $conf['state'];
            $data['access'] = $conf['access'];
            // Prepare request variable used by the item's Model
            if ($task != 'testcsv') {
                foreach ($_d as $fieldname => $field_values) {
                    if ($fieldname == 'tags_names') {
                        if ($conf['tags_col'] == 1) {
                            // Get tag names from comma separated list, filtering out bad characters
                            $remove = array("\n", "\r\n", "\r");
                            $tns_list = str_replace($remove, ' ', $field_values);
                            $tns_list = strip_tags($tns_list);
                            $tns_list = preg_replace("/[\"\\\\]/u", "", $tns_list);
                            //  "/[\"'\\\]/u"
                            $tns = array_unique(preg_split("/\\s*,\\s*/u", $tns_list));
                            $tns_quoted = array();
                            foreach ($tns as $tindex => $tname) {
                                if ($tname) {
                                    $tns_quoted[] = $db->Quote($tname);
                                }
                            }
                            if (count($tns_quoted)) {
                                $tns_list_quoted = implode(",", $tns_quoted);
                                $q = "SELECT name FROM #__flexicontent_tags WHERE name IN (" . $tns_list_quoted . ")";
                                $db->setQuery($q);
                                $tns_e = FLEXI_J16GE ? $db->loadColumn() : $db->loadResultArray();
                                $tns_m = array_diff($tns, $tns_e);
                                if (count($tns_m)) {
                                    // Create a newline separated list of tag names and then import missing tags,
                                    // thus making sure they are inserted into the tags DB table if not already present
                                    $tns_list_m = implode("\n", $tns_m);
                                    $tags_model->importList($tns_list_m);
                                }
                                // Get tag ids
                                $q = "SELECT id FROM #__flexicontent_tags WHERE name IN (" . $tns_list_quoted . ")";
                                $db->setQuery($q);
                                $data['tag'] = FLEXI_J16GE ? $db->loadColumn() : $db->loadResultArray();
                            }
                        }
                    } else {
                        if ($fieldname == 'tags_raw') {
                            if ($conf['tags_col'] == 2) {
                                // Get tag ids from comma separated list, filtering out bad characters
                                $_tis_list = preg_replace("/[\"'\\\\]/u", "", $field_values);
                                $_tis = array_unique(array_map('intval', $_tis));
                                $_tis = array_flip($_tis);
                                // Check to use only existing tag ids
                                $_tis_list = implode(",", array_keys($_tis));
                                $q = "SELECT id FROM #__flexicontent_tags WHERE id IN (" . $_tis_list . ")";
                                $db->setQuery($q);
                                $data['tag'] = FLEXI_J16GE ? $db->loadColumn() : $db->loadResultArray();
                            }
                        } else {
                            if (isset($conf['core_props'][$fieldname])) {
                                $data[$fieldname] = $field_values;
                            } else {
                                if (FLEXI_J16GE) {
                                    $data['custom'][$fieldname] = $field_values;
                                }
                            }
                        }
                    }
                }
            }
            // Set/Force id to zero to indicate creation of new item, in case item 'id' column is being used
            $c_item_id = @$data['id'];
            $data['id'] = 0;
            $session->set('csvimport_lineno', $lineno, 'flexicontent');
            // If testing format then output some information
            if ($task == 'testcsv') {
                if ($lineno == 1) {
                    $parse_log .= '
						<span class="fc-mssg fc-info">
						Testing file format <br/>
						COLUMNS: ' . implode(', ', $conf['columns']) . '<br/>
						</span><hr/>
					';
                }
                foreach ($_d as $i => $flddata) {
                    if (is_string($_d[$i])) {
                        if (mb_strlen($_d[$i], 'UTF-8') > 80) {
                            $_d[$i] = mb_substr(strip_tags($_d[$i]), 0, 80, 'UTF-8') . ' ... ';
                        }
                    }
                }
                if ($lineno <= $conf['debug_records']) {
                    $parse_log .= "<pre><b>Item no {$lineno}:</b>\n" . print_r($_d, true) . "</pre><hr/>";
                } else {
                    $parse_log .= "<b>Item no {$lineno}:</b> <br/>" . "<u>TITLE</u>: " . $_d['title'] . "<br/>" . "<u>TEXT</u>: " . $_d['text'] . "<hr/>";
                }
            } else {
                if (!$itemmodel->store($data)) {
                    $conf['failure_count']++;
                    $msg = 'Failed item no: ' . $lineno . ". titled as: '" . $data['title'] . "' : " . $itemmodel->getError();
                    JLog::add($msg);
                    echo $msg . "<br/>";
                } else {
                    $conf['success_count']++;
                    $msg = 'Imported item no: ' . $lineno . ". titled as: '" . $data['title'] . "'";
                    JLog::add($msg);
                    echo $msg . "<br/>";
                    // Try to rename entry if id column is being used
                    if ($conf['id_col'] && $c_item_id) {
                        $item_id = $itemmodel->getId();
                        $q = "UPDATE #__content SET id='" . $c_item_id . "' WHERE id='" . $item_id . "'";
                        $db->setQuery($q);
                        $db->execute();
                        $q = "UPDATE #__flexicontent_items_ext SET item_id='" . $c_item_id . "' WHERE item_id='" . $item_id . "'";
                        $db->setQuery($q);
                        $db->execute();
                        $q = "UPDATE #__flexicontent_items_tmp SET id='" . $c_item_id . "' WHERE id='" . $item_id . "'";
                        $db->setQuery($q);
                        $db->execute();
                        $q = "UPDATE #__flexicontent_tags_item_relations SET itemid='" . $c_item_id . "' WHERE itemid='" . $item_id . "'";
                        $db->setQuery($q);
                        $db->execute();
                        $q = "UPDATE #__flexicontent_cats_item_relations SET itemid='" . $c_item_id . "' WHERE itemid='" . $item_id . "'";
                        $db->setQuery($q);
                        $db->execute();
                        $q = "UPDATE #__flexicontent_fields_item_relations SET item_id='" . $c_item_id . "' WHERE item_id='" . $item_id . "'";
                        $db->setQuery($q);
                        $db->execute();
                        $q = "UPDATE #__flexicontent_items_versions SET item_id='" . $c_item_id . "' WHERE item_id='" . $item_id . "'";
                        $db->setQuery($q);
                        $db->execute();
                        $q = "UPDATE #__flexicontent_versions SET item_id='" . $c_item_id . "' WHERE item_id='" . $item_id . "'";
                        $db->setQuery($q);
                        $db->execute();
                        $q = "UPDATE #__flexicontent_favourites SET itemid='" . $c_item_id . "' WHERE itemid='" . $item_id . "'";
                        $db->setQuery($q);
                        $db->execute();
                        if (FLEXI_J16GE) {
                            $q = "UPDATE #__assets SET id='" . $c_item_id . "' WHERE id='" . $item_id . "'";
                        } else {
                            $q = "UPDATE #__flexiaccess_acl SET axo='" . $c_item_id . "'" . " WHERE acosection = " . $db->Quote('com_content') . " AND axosection = " . $db->Quote('item') . " AND axo='" . $item_id . "'";
                        }
                        $db->setQuery($q);
                        $db->execute();
                    }
                }
            }
        }
        //fclose($fp);
        // Done nothing more to do
        if ($task == 'testcsv') {
            echo $parse_log;
            echo "\n\n\n" . '<b>please click</b> <a href="' . $link . '">here</a> to return previous page' . "\n\n\n";
            jexit();
        }
        if ($lineno == $itemcount) {
            // Clean item's cache
            $cache = FLEXIUtilities::getCache($group = '', 0);
            $cache->clean('com_flexicontent_items');
            $cache->clean('com_flexicontent_filters');
            $cache = FLEXIUtilities::getCache($group = '', 1);
            $cache->clean('com_flexicontent_items');
            $cache->clean('com_flexicontent_filters');
            // Set a total results message and redirect
            $msg = 'Imported items: ' . $conf['success_count'] . ' , failed items: ' . $conf['failure_count'] . ', please review (in the logs folder) the import log file: ' . $log_filename;
            //$app->enqueueMessage($msg, ($conf['failure_count']==0 && $conf['success_count']>0) ? 'message' : 'warning');
            //$this->setRedirect($link);  // commented out this via AJAX call now
        }
        jexit();
    }
Example #24
0
 public function saveChunk($X, $Z, $force = false)
 {
     $X = (int) $X;
     $Z = (int) $Z;
     if (!$this->isChunkLoaded($X, $Z)) {
         return false;
     }
     $index = self::getIndex($X, $Z);
     if ($force !== true and (!isset($this->chunkChange[$index]) or $this->chunkChange[$index][-1] === false)) {
         //No changes in chunk
         return true;
     }
     $path = $this->getChunkPath($X, $Z);
     if (!file_exists(dirname($path))) {
         @mkdir(dirname($path), 0755);
     }
     $bitmap = 0;
     $this->cleanChunk($X, $Z);
     for ($Y = 0; $Y < 8; ++$Y) {
         if ($this->chunks[$index][$Y] !== false and (isset($this->chunkChange[$index][$Y]) and $this->chunkChange[$index][$Y] === 0 or !$this->isMiniChunkEmpty($X, $Z, $Y))) {
             $bitmap |= 1 << $Y;
         } else {
             $this->chunks[$index][$Y] = false;
         }
         $this->chunkChange[$index][$Y] = 0;
     }
     $this->chunkInfo[$index][0] = $bitmap;
     $this->chunkChange[$index][-1] = false;
     $chunk = "";
     $chunk .= chr($bitmap);
     $chunk .= Binary::writeInt($this->chunkInfo[$index][1]);
     $namedtag = new NBT(NBT::BIG_ENDIAN);
     $namedtag->setData($this->chunkInfo[$index][2]);
     $namedtag = $namedtag->write();
     $chunk .= Binary::writeInt(strlen($namedtag)) . $namedtag;
     $chunk .= $this->chunkInfo[$index][3];
     //biomes
     for ($Y = 0; $Y < 8; ++$Y) {
         $t = 1 << $Y;
         if (($bitmap & $t) === $t) {
             $chunk .= $this->chunks[$index][$Y];
         }
     }
     file_put_contents($path, zlib_encode($chunk, self::ZLIB_ENCODING, self::ZLIB_LEVEL));
     return true;
 }
Example #25
0
 public function do($data)
 {
     return zlib_encode($data, ZLIB_ENCODING_GZIP);
 }
Example #26
0
 /**
  * @param $chunkX
  * @param $chunkZ
  * @param $payload
  *
  * @return DataPacket
  */
 public static function getChunkCacheFromData($chunkX, $chunkZ, $payload, $ordering = FullChunkDataPacket::ORDER_COLUMNS)
 {
     $pk = new FullChunkDataPacket();
     $pk->chunkX = $chunkX;
     $pk->chunkZ = $chunkZ;
     $pk->order = $ordering;
     $pk->data = $payload;
     $pk->encode();
     $batch = new BatchPacket();
     $batch->payload = zlib_encode(Binary::writeInt(strlen($pk->getBuffer())) . $pk->getBuffer(), ZLIB_ENCODING_DEFLATE, Server::getInstance()->networkCompressionLevel);
     $batch->encode();
     $batch->isEncoded = true;
     return $batch;
 }
 public function doSlowCleanUp()
 {
     for ($i = 0; $i < 1024; ++$i) {
         if ($this->locationTable[$i][0] === 0 or $this->locationTable[$i][1] === 0) {
             continue;
         }
         \fseek($this->filePointer, $this->locationTable[$i][0] << 12);
         $chunk = \fread($this->filePointer, $this->locationTable[$i][1] << 12);
         $length = \unpack("N", \substr($chunk, 0, 4))[1] << 32 >> 32;
         if ($length <= 1) {
             $this->locationTable[$i] = [0, 0, 0];
             //Non-generated chunk, remove it from index
         }
         try {
             $chunk = \zlib_decode(\substr($chunk, 5));
         } catch (\Exception $e) {
             $this->locationTable[$i] = [0, 0, 0];
             //Corrupted chunk, remove it
             continue;
         }
         $chunk = \chr(self::COMPRESSION_ZLIB) . \zlib_encode($chunk, ZLIB_ENCODING_DEFLATE, 9);
         $chunk = \pack("N", \strlen($chunk)) . $chunk;
         $sectors = (int) \ceil(\strlen($chunk) / 4096);
         if ($sectors > $this->locationTable[$i][1]) {
             $this->locationTable[$i][0] = $this->lastSector + 1;
             $this->lastSector += $sectors;
         }
         \fseek($this->filePointer, $this->locationTable[$i][0] << 12);
         \fwrite($this->filePointer, \str_pad($chunk, $sectors << 12, "", STR_PAD_RIGHT));
     }
     $this->writeLocationTable();
     $n = $this->cleanGarbage();
     $this->writeLocationTable();
     return $n;
 }
Example #28
0
 /**
  * Broadcasts a list of packets in a batch to a list of players
  *
  * @param Player[]            $players
  * @param DataPacket[]|string $packets
  * @param bool                 $forceSync
  * @param int                 $channel
  */
 public function batchPackets(array $players, array $packets, $forceSync = false, $channel = 0)
 {
     Timings::$playerNetworkTimer->startTiming();
     $str = "";
     foreach ($packets as $p) {
         if ($p instanceof DataPacket) {
             if (!$p->isEncoded) {
                 $p->encode();
             }
             $str .= Binary::writeInt(strlen($p->buffer)) . $p->buffer;
         } else {
             $str .= Binary::writeInt(strlen($p)) . $p;
         }
     }
     $targets = [];
     foreach ($players as $p) {
         if ($p->isConnected()) {
             $targets[] = $this->identifiers[spl_object_hash($p)];
         }
     }
     if (!$forceSync and $this->networkCompressionAsync) {
         $task = new CompressBatchedTask($str, $targets, $this->networkCompressionLevel, $channel);
         $this->getScheduler()->scheduleAsyncTask($task);
     } else {
         $this->broadcastPacketsCallback(zlib_encode($str, ZLIB_ENCODING_DEFLATE, $this->networkCompressionLevel), $targets, $channel);
     }
     Timings::$playerNetworkTimer->stopTiming();
 }
Example #29
0
 /**
  * Save skin
  * @param Human $human
  * @param str $fn
  * @param str $fmt
  * @return int
  */
 public static function saveSkin(Human $human, $fn, $fmt = self::AUTO_FMT)
 {
     if ($fmt === self::AUTO_FMT) {
         $fmt = self::RAW_FMT;
         if (self::isPngExt($fn)) {
             $fmt = self::PNG_FMT;
         }
     }
     if (extension_loaded("gd") && $fmt == self::PNG_FMT) {
         $img = imagecreatetruecolor(64, 32);
         // Allocate colors...
         $colors = [];
         $bytes = $human->getSkinData();
         echo "BYTES=" . strlen($bytes) . "\n";
         //##DEBUG
         $x = $y = $c = 0;
         while ($y < 32) {
             $cid = substr($bytes, $c, 3);
             if (!isset($colors[$cid])) {
                 $colors[$cid] = imagecolorallocate($img, ord($cid[0]), ord($cid[1]), ord($cid[2]));
             }
             imagesetpixel($img, $x, $y, $colors[$cid]);
             $x++;
             $c += 4;
             if ($x === 64) {
                 $x = 0;
                 $y++;
             }
         }
         echo "COLORS=" . count($colors) . "\n";
         //##DEBUG
         if (!imagepng($img, $fn)) {
             imagedestroy($img);
             return 0;
         }
         imagedestroy($img);
         return filesize($fn);
     }
     $bin = zlib_encode($human->getSkinData(), ZLIB_ENCODING_DEFLATE, 9);
     file_put_contents($fn, $bin);
     return strlen($bin);
 }
Example #30
-1
 public function sendChunk($x, $z, $payload, $ordering = FullChunkDataPacket::ORDER_COLUMNS)
 {
     if ($this->connected === false) {
         return;
     }
     $this->usedChunks[Level::chunkHash($x, $z)] = true;
     $this->chunkLoadCount++;
     $pk = new FullChunkDataPacket();
     $pk->chunkX = $x;
     $pk->chunkZ = $z;
     $pk->order = $ordering;
     $pk->data = $payload;
     $pk->encode();
     $bt = new BatchPacket();
     $str = $pk->buffer;
     $bt->payload = zlib_encode($str, ZLIB_ENCODING_DEFLATE, 7);
     $bt->encode();
     $bt->isEncoded = true;
     $this->dataPacket($bt);
     if ($this->spawned) {
         foreach ($this->level->getChunkEntities($x, $z) as $entity) {
             if ($entity !== $this and !$entity->closed and !$entity->dead) {
                 $entity->spawnTo($this);
             }
         }
     }
 }