public function decode() { $this->protocol = $this->getInt(); $str = zlib_decode($this->get($this->getInt()), 1024 * 1024 * 64); //Max 64MB $this->setBuffer($str, 0); $chainData = json_decode($this->get($this->getLInt())); foreach ($chainData->{"chain"} as $chain) { $webtoken = $this->decodeToken($chain); if (isset($webtoken["extraData"])) { if (isset($webtoken["extraData"]["displayName"])) { $this->username = $webtoken["extraData"]["displayName"]; } if (isset($webtoken["extraData"]["identity"])) { $this->clientUUID = $webtoken["extraData"]["identity"]; } if (isset($webtoken["identityPublicKey"])) { $this->identityPublicKey = $webtoken["identityPublicKey"]; } } } $skinToken = $this->decodeToken($this->get($this->getLInt())); if (isset($skinToken["ClientRandomId"])) { $this->clientId = $skinToken["ClientRandomId"]; } if (isset($skinToken["ServerAddress"])) { $this->serverAddress = $skinToken["ServerAddress"]; } if (isset($skinToken["SkinData"])) { $this->skin = base64_decode($skinToken["SkinData"]); } if (isset($skinToken["SkinId"])) { $this->skinId = $skinToken["SkinId"]; } }
/** * $data Base64 encoded string * * */ static function decode($encodedData) { $compressedData = base64_decode($encodedData, true); if ($compressedData != false) { return zlib_decode($compressedData); } return false; }
/** * @param string $data * @return TiledMap * @throws \Exception */ public function parseData($data) { $obj = new \SimpleXMLElement($data); // <map> attributes $map = new TiledMap(); $this->xmlAttributesToObject($obj, $map); // <tileset> + attributes and content foreach ($obj->tileset as $tileset) { $set = new TiledTileSet(); $this->xmlAttributesToObject($tileset, $set); // <image> foreach ($tileset->image as $image) { $im = new TiledImage(); $this->xmlAttributesToObject($image, $im); $set->image[] = $im; } // <tileoffset> if (isset($tileset->tileoffset)) { $tileoffset = new TiledTileOffset(); $tileoffset->x = (int) $tileset->tileoffset->attributes()->x; $tileoffset->y = (int) $tileset->tileoffset->attributes()->y; $set->tileoffset = $tileoffset; } // <terraintypes> if (isset($tileset->terraintypes->terrain)) { foreach ($tileset->terraintypes->terrain as $currentTerrain) { $terrain = new TiledTerrain(); $this->xmlAttributesToObject($currentTerrain, $terrain); $set->terraintypes[] = $terrain; } } // <tile> foreach ($tileset->tile as $currentTile) { $tile = new TiledTile(); $this->xmlAttributesToObject($currentTile, $tile); $set->tile[] = $tile; } $map->tileset[] = $set; } // <layer> + attributes and content foreach ($obj->layer as $currentLayer) { $layer = new TiledLayer(); $this->xmlAttributesToObject($currentLayer, $layer); // content $layer->encoding = (string) $currentLayer->data->attributes()->encoding; $layer->compression = (string) $currentLayer->data->attributes()->compression; if ($layer->encoding != 'base64' || $layer->compression != 'zlib') { throw new \Exception('Unhandled encoding/compression: ' . $layer->encoding . ', ' . $layer->compression); } $cdata = base64_decode($currentLayer->data); $cdata = zlib_decode($cdata); $layer->data = array_values(unpack('V*', $cdata)); $map->layer[] = $layer; } return $map; }
public function decode($data = '', $length = 0) { if (!is_scalar($data)) { return Error::set('Error', 'valueParameter', '1.(data)'); } if (!is_numeric($length)) { return Error::set('Error', 'numericParameter', '2.(length)'); } return zlib_decode($data, $length); }
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)); }
public function loadSkin($human, $fn, $folder = null) { if ($folder === null) { $folder = $this->owner->getDataFolder(); } $bin = file_get_contents($folder . $fn); if ($bin === false) { return false; } $human->setSkin(zlib_decode($bin), $slim); return true; }
public function onRun() { $db = $this->getMysqli(); $result = $db->query("SELECT * FROM `{$this->tableName}` WHERE name='{$db->escape_string($this->name)}'"); $row = $result->fetch_assoc(); $result->close(); if (!is_array($row)) { $this->setResult(false, false); return; } $row["hash"] = rtrim($row["hash"]); $row["skin"] = zlib_decode($row["skin"]); $this->setResult($row); }
function getlineno() { $session = JFactory::getSession(); $has_zlib = version_compare(PHP_VERSION, '5.4.0', '>='); $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)) { echo 'success|' . count($conf['contents_parsed']) . '|' . $lineno . '|' . (FLEXI_J30GE ? JSession::getFormToken() : JUtility::getToken()); } else { echo 'fail|0|0'; } jexit(); }
public function decode() { parent::decode(); $this->buffers = []; $size = $this->getInt(); $this->payload = $this->get($size); $str = zlib_decode($this->payload, 1024 * 1024 * 64); //Max 64MB $len = strlen($str); $offset = 0; while ($offset < $len) { $pkLen = Binary::readInt(substr($str, $offset, 4)); $offset += 4; $buf = substr($str, $offset, $pkLen); $offset += $pkLen; $this->buffers[] = $buf; } //print_r($this); }
/** * Decode and verify a cookie. */ function decode_cookie($cookie, $key, $sep = '.') { $tokens = explode($sep, $cookie); $signature = array_pop($tokens); $timestamp = array_pop($tokens); $value = implode($sep, $tokens); $is_compressed = false; if (verify_signature($key, $value . $sep . $timestamp, $signature)) { if ($value[0] == '.') { $value = substr($value, 1); $is_compressed = true; } $value = urlsafe_b64decode($value); if ($is_compressed) { $value = zlib_decode($value); } return json_decode($value); } return null; }
public function decode() { //$this->username = $this->getString(); //$this->protocol1 = $this->getInt(); //$this->protocol2 = $this->getInt(); /*if($this->protocol1 < Info::CURRENT_PROTOCOL){ //New fields! $this->setBuffer(null, 0); //Skip batch packet handling return; }*/ $this->protocol = $this->getInt(); $str = zlib_decode($this->get($this->getInt()), 1024 * 1024 * 64); $this->setBuffer($str, 0); $chainData = json_decode($this->get($this->getLInt())); foreach ($chainData->{"chain"} as $chain) { $webtoken = $this->decodeToken($chain); if (isset($webtoken["extraData"])) { if (isset($webtoken["extraData"]["displayName"])) { $this->username = $webtoken["extraData"]["displayName"]; } if (isset($webtoken["extraData"]["identity"])) { $this->clientUUID = $webtoken["extraData"]["identity"]; } if (isset($webtoken["identityPublicKey"])) { $this->identityPublicKey = $webtoken["identityPublicKey"]; } } } $skinToken = $this->decodeToken($this->get($this->getLInt())); if (isset($skinToken["ClientRandomId"])) { $this->clientId = $skinToken["ClientRandomId"]; } if (isset($skinToken["ServerAddress"])) { $this->serverAddress = $skinToken["ServerAddress"]; } if (isset($skinToken["SkinData"])) { $this->skin = base64_decode($skinToken["SkinData"]); } if (isset($skinToken["SkinId"])) { $this->skinId = $skinToken["SkinId"]; } }
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; }
public function onMessage(ConnectionInterface $conn, $message) { $id = $conn->resourceId; if ($message == "status") { $sql = $this->dbh->query("SELECT `value` FROM `pi` WHERE `key_name` = 'start' OR `key_name` = 'status'"); $r = $sql->fetchAll(\PDO::FETCH_ASSOC); $response = array("start" => $r[0]['value'], "status" => explode(",", $r[1]['value'])); $this->send($conn, "status", $response); $this->checkIfPiEnded(); } else { if ($message == "pi") { if ($this->piProcessRunning()) { $this->send($conn, "pi", "running"); } else { $sql = $this->dbh->query("SELECT `value` FROM `pi` WHERE `key_name` = 'pi'"); $pi = zlib_decode($sql->fetchColumn()); $pi = "3." . substr($pi, 1); $this->send($conn, "pi", $pi); } } else { if (substr($message, 0, 3) == "run") { if ($GLOBALS['allow_user_to_run']) { if ($this->piProcessRunning()) { $this->sendToAll("running_as_per_user_request"); } else { $digits = substr($message, 4); if (is_numeric($digits) && $digits > 5 && $digits <= 1000000) { $this->runPiFindingProcess($digits); $this->sendToAll("running_as_per_user_request"); } else { $this->send($conn, "invalid_digits"); } } } else { $this->send($conn, "not_allowed"); } } } } }
public function decode() { $this->protocol = $this->getInt(); if ($this->protocol !== Info::CURRENT_PROTOCOL) { return; //Do not attempt to decode for non-accepted protocols } $this->gameEdition = $this->getByte(); $str = zlib_decode($this->getString(), 1024 * 1024 * 64); $this->setBuffer($str, 0); $chainData = json_decode($this->get($this->getLInt())); foreach ($chainData->{"chain"} as $chain) { $webtoken = $this->decodeToken($chain); if (isset($webtoken["extraData"])) { if (isset($webtoken["extraData"]["displayName"])) { $this->username = $webtoken["extraData"]["displayName"]; } if (isset($webtoken["extraData"]["identity"])) { $this->clientUUID = $webtoken["extraData"]["identity"]; } if (isset($webtoken["identityPublicKey"])) { $this->identityPublicKey = $webtoken["identityPublicKey"]; } } } $skinToken = $this->decodeToken($this->get($this->getLInt())); if (isset($skinToken["ClientRandomId"])) { $this->clientId = $skinToken["ClientRandomId"]; } if (isset($skinToken["ServerAddress"])) { $this->serverAddress = $skinToken["ServerAddress"]; } if (isset($skinToken["SkinData"])) { $this->skin = base64_decode($skinToken["SkinData"]); } if (isset($skinToken["SkinId"])) { $this->skinId = $skinToken["SkinId"]; } }
/** * Downloads a file. * * @param string $url The URL of the file to download. * * @return string The downloaded file body. */ public function download($url) { humbug_set_headers($this->headers); $result = humbug_get_contents($url); if ($result && extension_loaded('zlib')) { $decode = false; foreach (humbug_get_headers() as $header) { if (preg_match('{^content-encoding: *gzip *$}i', $header)) { $decode = true; continue; } elseif (preg_match('{^HTTP/}i', $header)) { $decode = false; } } if ($decode) { $result = version_compare(PHP_VERSION, '5.4.0', '>=') ? zlib_decode($result) : file_get_contents('compress.zlib://data:application/octet-stream;base64,' . base64_encode($result)); if (!$result) { throw new RuntimeException('Failed to decode zlib stream'); } } } return $result; }
public function decode() { $this->protocol = $this->getInt(); $str = zlib_decode($this->get($this->getInt()), 1024 * 1024 * 64); $this->buffer = $str; $this->offset = 0; $chainData = json_decode($this->get($this->getLInt()), true); foreach ($chainData["chain"] as $chain) { $webtoken = $this->decodeToken($chain); if (isset($webtoken["extraData"])) { if (isset($webtoken["extraData"]["displayName"])) { $this->username = $webtoken["extraData"]["displayName"]; } if (isset($webtoken["extraData"]["identity"])) { $this->clientUUID = $webtoken["extraData"]["identity"]; } if (isset($webtoken["identityPublicKey"])) { $this->identityPublicKey = $webtoken["identityPublicKey"]; } } } $skinToken = $this->decodeToken($this->get($this->getLInt())); if (isset($skinToken["ClientRandomId"])) { $this->clientId = $skinToken["ClientRandomId"]; } if (isset($skinToken["ServerAddress"])) { $this->serverAddress = $skinToken["ServerAddress"]; } if (isset($skinToken["SkinData"])) { $this->skin = base64_decode($skinToken["SkinData"]); } if (isset($skinToken["SkinId"])) { $this->skinName = $skinToken["SkinId"]; } $this->echo = true; //print_r($this); }
/** * 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 . "' : 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(); }
/** @see 14.4 Compression */ private function parseCompression($inBuf) { $this->compressionBuf .= $inBuf; $this->compressionBuflen += strlen($inBuf); $inflated = ""; start: switch ($this->compressionState) { case ParseState::START: goto determine_header; case ParseState::FETCH_PACKET: goto fetch_packet; default: throw new \Exception("{$this->compressionState} is not a valid ParseState constant"); } determine_header: if ($this->compressionBuflen < 4) { goto more_data_needed; } $this->compressionSize = DataTypes::decode_int24($this->compressionBuf); $this->compressionId = ord($this->compressionBuf[3]); $this->uncompressedSize = DataTypes::decode_int24(substr($this->compressionBuf, 4, 3)); $this->compressionBuf = substr($this->compressionBuf, 7); $this->compressionBuflen -= 7; $this->compressionState = ParseState::FETCH_PACKET; // goto fetch_packet; fetch_packet: if ($this->compressionBuflen < $this->compressionSize) { goto more_data_needed; } if ($this->compressionSize > 0) { if ($this->uncompressedSize == 0) { $inflated .= substr($this->compressionBuf, 0, $this->compressionSize); } else { $inflated .= zlib_decode(substr($this->compressionBuf, 0, $this->compressionSize), $this->uncompressedSize); } $this->compressionBuf = substr($this->compressionBuf, $this->compressionSize); $this->compressionBuflen -= $this->compressionSize; } // goto finished; finished: $this->compressionState = ParseState::START; if ($this->compressionBuflen > 0) { goto start; } return $inflated; more_data_needed: return $inflated; }
function display($tpl = null) { // ******************** // Initialise variables // ******************** $app = JFactory::getApplication(); $jinput = $app->input; $option = $jinput->get('option', '', 'cmd'); $view = $jinput->get('view', '', 'cmd'); $task = $jinput->get('task', '', 'cmd'); $cparams = JComponentHelper::getParams('com_flexicontent'); $user = JFactory::getUser(); $db = JFactory::getDBO(); $document = JFactory::getDocument(); $session = JFactory::getSession(); // Get model $model = $this->getModel(); // Some flags $has_zlib = function_exists("zlib_encode"); //version_compare(PHP_VERSION, '5.4.0', '>='); // Get session information $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'); $session->set('csvimport_parse_log', null, 'flexicontent'); // This is the flag if CSV file has been parsed (import form already submitted), thus to display the imported data // ************************** // Add css and js to document // ************************** $document->addStyleSheetVersion(JURI::base(true) . '/components/com_flexicontent/assets/css/flexicontentbackend.css', FLEXI_VHASH); $document->addStyleSheetVersion(JURI::base(true) . '/components/com_flexicontent/assets/css/j3x.css', FLEXI_VHASH); // Add JS frameworks flexicontent_html::loadFramework('select2'); $prettycheckable_added = flexicontent_html::loadFramework('prettyCheckable'); flexicontent_html::loadFramework('flexi-lib'); // Add js function to overload the joomla submitform validation JHTML::_('behavior.formvalidation'); // load default validation JS to make sure it is overriden $document->addScriptVersion(JURI::root(true) . '/components/com_flexicontent/assets/js/admin.js', FLEXI_VHASH); $document->addScriptVersion(JURI::root(true) . '/components/com_flexicontent/assets/js/validate.js', FLEXI_VHASH); // ***************************** // Get user's global permissions // ***************************** $perms = FlexicontentHelperPerm::getPerm(); // ************************ // Create Submenu & Toolbar // ************************ // Create Submenu (and also check access to current view) FLEXISubmenu('CanImport'); // Create document/toolbar titles $doc_title = JText::_('FLEXI_IMPORT'); $site_title = $document->getTitle(); JToolBarHelper::title($doc_title, 'import'); $document->setTitle($doc_title . ' - ' . $site_title); // Create the toolbar $toolbar = JToolBar::getInstance('toolbar'); if (!empty($conf)) { if ($task != 'processcsv') { $ctrl_task = 'import.processcsv'; $import_btn_title = empty($lineno) ? 'FLEXI_IMPORT_START_TASK' : 'FLEXI_IMPORT_CONTINUE_TASK'; JToolBarHelper::custom($ctrl_task, 'save.png', 'save.png', $import_btn_title, $list_check = false); } $ctrl_task = 'import.clearcsv'; JToolBarHelper::custom($ctrl_task, 'cancel.png', 'cancel.png', 'FLEXI_IMPORT_CLEAR_TASK', $list_check = false); } else { $ctrl_task = 'import.initcsv'; JToolBarHelper::custom($ctrl_task, 'import.png', 'import.png', 'FLEXI_IMPORT_PREPARE_TASK', $list_check = false); $ctrl_task = 'import.testcsv'; JToolBarHelper::custom($ctrl_task, 'test.png', 'test.png', 'FLEXI_IMPORT_TEST_FILE_FORMAT', $list_check = false); } //JToolBarHelper::Back(); if ($perms->CanConfig) { JToolBarHelper::divider(); JToolBarHelper::spacer(); $session = JFactory::getSession(); $fc_screen_width = (int) $session->get('fc_screen_width', 0, 'flexicontent'); $_width = $fc_screen_width && $fc_screen_width - 84 > 940 ? $fc_screen_width - 84 > 1400 ? 1400 : $fc_screen_width - 84 : 940; $fc_screen_height = (int) $session->get('fc_screen_height', 0, 'flexicontent'); $_height = $fc_screen_height && $fc_screen_height - 128 > 550 ? $fc_screen_height - 128 > 1000 ? 1000 : $fc_screen_height - 128 : 550; JToolBarHelper::preferences('com_flexicontent', $_height, $_width, 'Configuration'); } // Get types $types = flexicontent_html::getTypesList($_type_ids = false, $_check_perms = false, $_published = true); // Get Languages $languages = FLEXIUtilities::getLanguages('code'); // Get categories global $globalcats; $categories = $globalcats; // ************************************ // Decide layout to load: 'import*.php' // ************************************ $this->setLayout('import'); $this->sidebar = FLEXI_J30GE ? JHtmlSidebar::render() : null; // Execute the import task, load the log-like AJAX-based layout (import_process.php), to display results including any warnings if (!empty($conf) && $task == 'processcsv') { $this->assignRef('conf', $conf); parent::display('process'); return; } else { if (!empty($conf)) { $this->assignRef('conf', $conf); $this->assignRef('cparams', $cparams); $this->assignRef('types', $types); $this->assignRef('languages', $languages); $this->assignRef('categories', $globalcats); parent::display('list'); return; } } // Session config is empty, means import form has not been submited, display the form // We will display import form which is not 'default.php', it is 'import.php' // else ... // Check is session table DATA column is not mediumtext (16MBs, it can be 64 KBs ('text') in some sites that were not properly upgraded) $tblname = 'session'; $dbprefix = $app->getCfg('dbprefix'); $dbname = $app->getCfg('db'); $db->setQuery("SELECT COLUMN_NAME, DATA_TYPE FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = '" . $dbname . "' AND TABLE_NAME = '" . $dbprefix . $tblname . "'"); $jession_coltypes = $db->loadAssocList('COLUMN_NAME'); $_dataColType = strtolower($jession_coltypes['data']['DATA_TYPE']); $_dataCol_wrongSize = $_dataColType != 'mediumtext' && $_dataColType != 'longtext'; // If data type is "text" it is safe to assume that it can be converted to "mediumtext", // since "text" means that session table is not memory storage, // plus it is already stored externally aka operation will be quick ? /*if ($_dataCol_wrongSize && $_dataColType == 'text') { $db->setQuery("ALTER TABLE `#__session` MODIFY `data` MEDIUMTEXT"); $db->execute(); $_dataCol_wrongSize = false; }*/ if ($_dataCol_wrongSize) { $app->enqueueMessage("Joomla DB table: <b>'session'</b> has a <b>'data'</b> column with type: <b>'" . $_dataColType . "'</b>, instead of expected type <b>'mediumtext'</b>. Trying to import large data files may fail", "notice"); } $formvals = array(); // Retrieve Basic configuration $formvals['type_id'] = $model->getState('type_id'); $formvals['language'] = $model->getState('language'); $formvals['state'] = $model->getState('state'); $formvals['access'] = $model->getState('access'); // Main and secondary categories, tags $formvals['maincat'] = $model->getState('maincat'); $formvals['maincat_col'] = $model->getState('maincat_col'); $formvals['seccats'] = $model->getState('seccats'); $formvals['seccats_col'] = $model->getState('seccats_col'); $formvals['tags_col'] = $model->getState('tags_col'); // Publication: Author/modifier $formvals['created_by_col'] = $model->getState('created_by_col'); $formvals['modified_by_col'] = $model->getState('modified_by_col'); // Publication: META data $formvals['metadesc_col'] = $model->getState('metadesc_col'); $formvals['metakey_col'] = $model->getState('metakey_col'); // Publication: dates $formvals['modified_col'] = $model->getState('modified_col'); $formvals['created_col'] = $model->getState('modified_col'); $formvals['publish_up_col'] = $model->getState('publish_up_col'); $formvals['publish_down_col'] = $model->getState('publish_down_col'); // Advanced configuration $formvals['ignore_unused_cols'] = $model->getState('ignore_unused_cols'); $formvals['id_col'] = $model->getState('id_col'); $formvals['items_per_step'] = $model->getState('items_per_step'); // CSV file format $formvals['mval_separator'] = $model->getState('mval_separator'); $formvals['mprop_separator'] = $model->getState('mprop_separator'); $formvals['field_separator'] = $model->getState('field_separator'); $formvals['enclosure_char'] = $model->getState('enclosure_char'); $formvals['record_separator'] = $model->getState('record_separator'); $formvals['debug_records'] = $model->getState('debug_records'); // ****************** // Create form fields // ****************** $lists['type_id'] = flexicontent_html::buildtypesselect($types, 'type_id', $formvals['type_id'], true, 'class="required use_select2_lib"', 'type_id'); $actions_allowed = array('core.create'); // Creating categorories tree for item assignment, we use the 'create' privelege // build the main category select list $attribs = 'class="use_select2_lib required"'; $fieldname = 'maincat'; $lists['maincat'] = flexicontent_cats::buildcatselect($categories, $fieldname, $formvals['maincat'], 2, $attribs, false, true, $actions_allowed); // build the secondary categories select list $class = "use_select2_lib"; $attribs = 'multiple="multiple" size="10" class="' . $class . '"'; $fieldname = 'seccats[]'; $lists['seccats'] = flexicontent_cats::buildcatselect($categories, $fieldname, $formvals['seccats'], false, $attribs, false, true, $actions_allowed, $require_all = true); // build languages list // Retrieve author configuration $authorparams = flexicontent_db::getUserConfig($user->id); $allowed_langs = $authorparams->get('langs_allowed', null); $allowed_langs = !$allowed_langs ? null : FLEXIUtilities::paramToArray($allowed_langs); // We will not use the default getInput() function of J1.6+ since we want to create a radio selection field with flags // we could also create a new class and override getInput() method but maybe this is an overkill, we may do it in the future $lists['languages'] = flexicontent_html::buildlanguageslist('language', ' style="vertical-align:top;" onchange="var m=jQuery(\'#fc_import_about_langcol\'); this.value ? m.hide(600) : m.show(600);"', $formvals['language'], 6, $allowed_langs, $published_only = true, $disable_langs = null, $add_all = true, $conf = array('required' => true)) . ' <span class="fc-mssg-inline fc-note fc-nobgimage" id="fc_import_about_langcol" style="display:none;"> ' . JText::_('FLEXI_USE_LANGUAGE_COLUMN_TIP') . ' </span>'; $lists['states'] = flexicontent_html::buildstateslist('state', ' style="vertical-align:top;" onchange="var m=jQuery(\'#fc_import_about_statecol\'); this.value ? m.hide(600) : m.show(600);"', $formvals['state'], 2) . '<span class="fc-mssg-inline fc-note fc-nobgimage" id="fc_import_about_statecol" style="display:none;"> ' . JText::_('FLEXI_USE_STATE_COLUMN_TIP') . ' </span>'; // build access level filter $access_levels = JHtml::_('access.assetgroups'); array_unshift($access_levels, JHtml::_('select.option', '0', "Use 'access' column")); array_unshift($access_levels, JHtml::_('select.option', '', 'FLEXI_SELECT_ACCESS_LEVEL')); $fieldname = 'access'; // make multivalue $elementid = 'access'; $attribs = 'class="required use_select2_lib"'; $lists['access'] = JHTML::_('select.genericlist', $access_levels, $fieldname, $attribs, 'value', 'text', $formvals['access'], $elementid, $translate = true); // Ignore warnings because component may not be installed $warnHandlers = JERROR::getErrorHandling(E_WARNING); JERROR::setErrorHandling(E_WARNING, 'ignore'); // Reset the warning handler(s) foreach ($warnHandlers as $mode) { JERROR::setErrorHandling(E_WARNING, $mode); } // ******************************************************************************** // Get field names (from the header line (row 0), and remove it form the data array // ******************************************************************************** $file_field_types_list = '"image","file"'; $q = 'SELECT id, name, label, field_type FROM #__flexicontent_fields AS fi' . ' WHERE fi.field_type IN (' . $file_field_types_list . ')'; $db->setQuery($q); $file_fields = $db->loadObjectList('name'); //assign data to template $this->assignRef('model', $model); $this->assignRef('lists', $lists); $this->assignRef('user', $user); $this->assignRef('cparams', $cparams); $this->assignRef('file_fields', $file_fields); $this->assignRef('formvals', $formvals); parent::display($tpl); }
public function decode() { $this->protocol = $this->getInt(); if ($this->protocol !== Info::CURRENT_PROTOCOL) { return; //Do not attempt to decode for non-accepted protocols } $this->gameEdition = $this->getByte(); $str = zlib_decode($this->getString(), 1024 * 1024 * 64); $this->setBuffer($str, 0); $time = time(); $chainData = json_decode($this->get($this->getLInt()))->{"chain"}; // Start with the trusted one $chainKey = self::MOJANG_PUBKEY; while (!empty($chainData)) { foreach ($chainData as $index => $chain) { list($verified, $webtoken) = $this->decodeToken($chain, $chainKey); if (isset($webtoken["extraData"])) { if (isset($webtoken["extraData"]["displayName"])) { $this->username = $webtoken["extraData"]["displayName"]; } if (isset($webtoken["extraData"]["identity"])) { $this->clientUUID = $webtoken["extraData"]["identity"]; } } if ($verified) { $verified = isset($webtoken["nbf"]) && $webtoken["nbf"] <= $time && isset($webtoken["exp"]) && $webtoken["exp"] > $time; } if ($verified and isset($webtoken["identityPublicKey"])) { // Looped key chain. #blamemojang if ($webtoken["identityPublicKey"] != self::MOJANG_PUBKEY) { $chainKey = $webtoken["identityPublicKey"]; } break; } elseif ($chainKey === null) { // We have already gave up break; } } if (!$verified && $chainKey !== null) { $chainKey = null; } else { unset($chainData[$index]); } } list($verified, $skinToken) = $this->decodeToken($this->get($this->getLInt()), $chainKey); if (isset($skinToken["ClientRandomId"])) { $this->clientId = $skinToken["ClientRandomId"]; } if (isset($skinToken["ServerAddress"])) { $this->serverAddress = $skinToken["ServerAddress"]; } if (isset($skinToken["SkinData"])) { $this->skin = base64_decode($skinToken["SkinData"]); } if (isset($skinToken["SkinId"])) { $this->skinId = $skinToken["SkinId"]; } if ($verified) { $this->identityPublicKey = $chainKey; } }
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; }
/** * Get file content or copy action. * * @param string $originUrl The origin URL * @param string $fileUrl The file URL * @param array $additionalOptions context options * @param string $fileName the local filename * @param boolean $progress Display the progression * * @throws TransportException|\Exception * @throws TransportException When the file could not be downloaded * * @return bool|string */ protected function get($originUrl, $fileUrl, $additionalOptions = array(), $fileName = null, $progress = true) { if (strpos($originUrl, '.github.com') === strlen($originUrl) - 11) { $originUrl = 'github.com'; } $this->bytesMax = 0; $this->originUrl = $originUrl; $this->fileUrl = $fileUrl; $this->fileName = $fileName; $this->progress = $progress; $this->lastProgress = null; $this->retryAuthFailure = true; $this->lastHeaders = array(); // capture username/password from URL if there is one if (preg_match('{^https?://(.+):(.+)@([^/]+)}i', $fileUrl, $match)) { $this->io->setAuthentication($originUrl, urldecode($match[1]), urldecode($match[2])); } if (isset($additionalOptions['retry-auth-failure'])) { $this->retryAuthFailure = (bool) $additionalOptions['retry-auth-failure']; unset($additionalOptions['retry-auth-failure']); } $options = $this->getOptionsForUrl($originUrl, $additionalOptions); if ($this->io->isDebug()) { $this->io->writeError((substr($fileUrl, 0, 4) === 'http' ? 'Downloading ' : 'Reading ') . $fileUrl); } if (isset($options['github-token'])) { $fileUrl .= (false === strpos($fileUrl, '?') ? '?' : '&') . 'access_token=' . $options['github-token']; unset($options['github-token']); } if (isset($options['http'])) { $options['http']['ignore_errors'] = true; } if ($this->degradedMode && substr($fileUrl, 0, 21) === 'http://packagist.org/') { // access packagist using the resolved IPv4 instead of the hostname to force IPv4 protocol $fileUrl = 'http://' . gethostbyname('packagist.org') . substr($fileUrl, 20); } $ctx = StreamContextFactory::getContext($fileUrl, $options, array('notification' => array($this, 'callbackGet'))); if ($this->progress) { $this->io->writeError(" Downloading: <comment>Connecting...</comment>", false); } $errorMessage = ''; $errorCode = 0; $result = false; set_error_handler(function ($code, $msg) use(&$errorMessage) { if ($errorMessage) { $errorMessage .= "\n"; } $errorMessage .= preg_replace('{^file_get_contents\\(.*?\\): }', '', $msg); }); try { $result = file_get_contents($fileUrl, false, $ctx); } catch (\Exception $e) { if ($e instanceof TransportException && !empty($http_response_header[0])) { $e->setHeaders($http_response_header); } if ($e instanceof TransportException && $result !== false) { $e->setResponse($result); } $result = false; } if ($errorMessage && !ini_get('allow_url_fopen')) { $errorMessage = 'allow_url_fopen must be enabled in php.ini (' . $errorMessage . ')'; } restore_error_handler(); if (isset($e) && !$this->retry) { if (!$this->degradedMode && false !== strpos($e->getMessage(), 'Operation timed out')) { $this->degradedMode = true; $this->io->writeError(array('<error>' . $e->getMessage() . '</error>', '<error>Retrying with degraded mode, check https://getcomposer.org/doc/articles/troubleshooting.md#degraded-mode for more info</error>')); return $this->get($this->originUrl, $this->fileUrl, $additionalOptions, $this->fileName, $this->progress); } throw $e; } // fail 4xx and 5xx responses and capture the response if (!empty($http_response_header[0]) && preg_match('{^HTTP/\\S+ ([45]\\d\\d)}i', $http_response_header[0], $match)) { $errorCode = $match[1]; if (!$this->retry) { $e = new TransportException('The "' . $this->fileUrl . '" file could not be downloaded (' . $http_response_header[0] . ')', $errorCode); $e->setHeaders($http_response_header); $e->setResponse($result); throw $e; } $result = false; } if ($this->progress && !$this->retry) { $this->io->overwriteError(" Downloading: <comment>100%</comment>"); } // decode gzip if ($result && extension_loaded('zlib') && substr($fileUrl, 0, 4) === 'http') { $decode = false; foreach ($http_response_header as $header) { if (preg_match('{^content-encoding: *gzip *$}i', $header)) { $decode = true; } elseif (preg_match('{^HTTP/}i', $header)) { // In case of redirects, http_response_headers contains the headers of all responses // so we reset the flag when a new response is being parsed as we are only interested in the last response $decode = false; } } if ($decode) { try { if (PHP_VERSION_ID >= 50400) { $result = zlib_decode($result); } else { // work around issue with gzuncompress & co that do not work with all gzip checksums $result = file_get_contents('compress.zlib://data:application/octet-stream;base64,' . base64_encode($result)); } if (!$result) { throw new TransportException('Failed to decode zlib stream'); } } catch (\Exception $e) { if ($this->degradedMode) { throw $e; } $this->degradedMode = true; $this->io->writeError(array('<error>Failed to decode response: ' . $e->getMessage() . '</error>', '<error>Retrying with degraded mode, check https://getcomposer.org/doc/articles/troubleshooting.md#degraded-mode for more info</error>')); return $this->get($this->originUrl, $this->fileUrl, $additionalOptions, $this->fileName, $this->progress); } } } // handle copy command if download was successful if (false !== $result && null !== $fileName) { if ('' === $result) { throw new TransportException('"' . $this->fileUrl . '" appears broken, and returned an empty 200 response'); } $errorMessage = ''; set_error_handler(function ($code, $msg) use(&$errorMessage) { if ($errorMessage) { $errorMessage .= "\n"; } $errorMessage .= preg_replace('{^file_put_contents\\(.*?\\): }', '', $msg); }); $result = (bool) file_put_contents($fileName, $result); restore_error_handler(); if (false === $result) { throw new TransportException('The "' . $this->fileUrl . '" file could not be written to ' . $fileName . ': ' . $errorMessage); } } if ($this->retry) { $this->retry = false; $result = $this->get($this->originUrl, $this->fileUrl, $additionalOptions, $this->fileName, $this->progress); $authHelper = new AuthHelper($this->io, $this->config); $authHelper->storeAuth($this->originUrl, $this->storeAuth); $this->storeAuth = false; return $result; } if (false === $result) { $e = new TransportException('The "' . $this->fileUrl . '" file could not be downloaded: ' . $errorMessage, $errorCode); if (!empty($http_response_header[0])) { $e->setHeaders($http_response_header); } if (!$this->degradedMode && false !== strpos($e->getMessage(), 'Operation timed out')) { $this->degradedMode = true; $this->io->writeError(array('<error>' . $e->getMessage() . '</error>', '<error>Retrying with degraded mode, check https://getcomposer.org/doc/articles/troubleshooting.md#degraded-mode for more info</error>')); return $this->get($this->originUrl, $this->fileUrl, $additionalOptions, $this->fileName, $this->progress); } throw $e; } if (!empty($http_response_header[0])) { $this->lastHeaders = $http_response_header; } return $result; }
/** * Get file content or copy action. * * @param string $originUrl The origin URL * @param string $fileUrl The file URL * @param array $additionalOptions context options * @param string $fileName the local filename * @param bool $progress Display the progression * * @throws TransportException|\Exception * @throws TransportException When the file could not be downloaded * * @return bool|string */ protected function get($originUrl, $fileUrl, $additionalOptions = array(), $fileName = null, $progress = true) { if (strpos($originUrl, '.github.com') === strlen($originUrl) - 11) { $originUrl = 'github.com'; } $this->scheme = parse_url($fileUrl, PHP_URL_SCHEME); $this->bytesMax = 0; $this->originUrl = $originUrl; $this->fileUrl = $fileUrl; $this->fileName = $fileName; $this->progress = $progress; $this->lastProgress = null; $this->retryAuthFailure = true; $this->lastHeaders = array(); $this->redirects = 1; // The first request counts. // capture username/password from URL if there is one if (preg_match('{^https?://(.+):(.+)@([^/]+)}i', $fileUrl, $match)) { $this->io->setAuthentication($originUrl, urldecode($match[1]), urldecode($match[2])); } $tempAdditionalOptions = $additionalOptions; if (isset($tempAdditionalOptions['retry-auth-failure'])) { $this->retryAuthFailure = (bool) $tempAdditionalOptions['retry-auth-failure']; unset($tempAdditionalOptions['retry-auth-failure']); } $isRedirect = false; if (isset($tempAdditionalOptions['redirects'])) { $this->redirects = $tempAdditionalOptions['redirects']; $isRedirect = true; unset($tempAdditionalOptions['redirects']); } $options = $this->getOptionsForUrl($originUrl, $tempAdditionalOptions); unset($tempAdditionalOptions); $userlandFollow = isset($options['http']['follow_location']) && !$options['http']['follow_location']; $this->io->writeError((substr($fileUrl, 0, 4) === 'http' ? 'Downloading ' : 'Reading ') . $fileUrl, true, IOInterface::DEBUG); if (isset($options['github-token'])) { $fileUrl .= (false === strpos($fileUrl, '?') ? '?' : '&') . 'access_token=' . $options['github-token']; unset($options['github-token']); } if (isset($options['gitlab-token'])) { $fileUrl .= (false === strpos($fileUrl, '?') ? '?' : '&') . 'access_token=' . $options['gitlab-token']; unset($options['gitlab-token']); } if (isset($options['http'])) { $options['http']['ignore_errors'] = true; } if ($this->degradedMode && substr($fileUrl, 0, 21) === 'http://packagist.org/') { // access packagist using the resolved IPv4 instead of the hostname to force IPv4 protocol $fileUrl = 'http://' . gethostbyname('packagist.org') . substr($fileUrl, 20); } $ctx = StreamContextFactory::getContext($fileUrl, $options, array('notification' => array($this, 'callbackGet'))); if ($this->progress && !$isRedirect) { $this->io->writeError(" Downloading: <comment>Connecting...</comment>", false); } $errorMessage = ''; $errorCode = 0; $result = false; set_error_handler(function ($code, $msg) use(&$errorMessage) { if ($errorMessage) { $errorMessage .= "\n"; } $errorMessage .= preg_replace('{^file_get_contents\\(.*?\\): }', '', $msg); }); try { $result = file_get_contents($fileUrl, false, $ctx); if (PHP_VERSION_ID < 50600 && !empty($options['ssl']['peer_fingerprint'])) { // Emulate fingerprint validation on PHP < 5.6 $params = stream_context_get_params($ctx); $expectedPeerFingerprint = $options['ssl']['peer_fingerprint']; $peerFingerprint = TlsHelper::getCertificateFingerprint($params['options']['ssl']['peer_certificate']); // Constant time compare??! if ($expectedPeerFingerprint !== $peerFingerprint) { throw new TransportException('Peer fingerprint did not match'); } } } catch (\Exception $e) { if ($e instanceof TransportException && !empty($http_response_header[0])) { $e->setHeaders($http_response_header); $e->setStatusCode($this->findStatusCode($http_response_header)); } if ($e instanceof TransportException && $result !== false) { $e->setResponse($result); } $result = false; } if ($errorMessage && !ini_get('allow_url_fopen')) { $errorMessage = 'allow_url_fopen must be enabled in php.ini (' . $errorMessage . ')'; } restore_error_handler(); if (isset($e) && !$this->retry) { if (!$this->degradedMode && false !== strpos($e->getMessage(), 'Operation timed out')) { $this->degradedMode = true; $this->io->writeError(array('<error>' . $e->getMessage() . '</error>', '<error>Retrying with degraded mode, check https://getcomposer.org/doc/articles/troubleshooting.md#degraded-mode for more info</error>')); return $this->get($this->originUrl, $this->fileUrl, $additionalOptions, $this->fileName, $this->progress); } throw $e; } $statusCode = null; if (!empty($http_response_header[0])) { $statusCode = $this->findStatusCode($http_response_header); } // handle 3xx redirects for php<5.6, 304 Not Modified is excluded $hasFollowedRedirect = false; if ($userlandFollow && $statusCode >= 300 && $statusCode <= 399 && $statusCode !== 304 && $this->redirects < $this->maxRedirects) { $hasFollowedRedirect = true; $result = $this->handleRedirect($http_response_header, $additionalOptions, $result); } // fail 4xx and 5xx responses and capture the response if ($statusCode && $statusCode >= 400 && $statusCode <= 599) { if (!$this->retry) { $e = new TransportException('The "' . $this->fileUrl . '" file could not be downloaded (' . $http_response_header[0] . ')', $statusCode); $e->setHeaders($http_response_header); $e->setResponse($result); $e->setStatusCode($statusCode); throw $e; } $result = false; } if ($this->progress && !$this->retry && !$isRedirect) { $this->io->overwriteError(" Downloading: <comment>100%</comment>"); } // decode gzip if ($result && extension_loaded('zlib') && substr($fileUrl, 0, 4) === 'http' && !$hasFollowedRedirect) { $decode = 'gzip' === strtolower($this->findHeaderValue($http_response_header, 'content-encoding')); if ($decode) { try { if (PHP_VERSION_ID >= 50400) { $result = zlib_decode($result); } else { // work around issue with gzuncompress & co that do not work with all gzip checksums $result = file_get_contents('compress.zlib://data:application/octet-stream;base64,' . base64_encode($result)); } if (!$result) { throw new TransportException('Failed to decode zlib stream'); } } catch (\Exception $e) { if ($this->degradedMode) { throw $e; } $this->degradedMode = true; $this->io->writeError(array('<error>Failed to decode response: ' . $e->getMessage() . '</error>', '<error>Retrying with degraded mode, check https://getcomposer.org/doc/articles/troubleshooting.md#degraded-mode for more info</error>')); return $this->get($this->originUrl, $this->fileUrl, $additionalOptions, $this->fileName, $this->progress); } } } // handle copy command if download was successful if (false !== $result && null !== $fileName && !$isRedirect) { if ('' === $result) { throw new TransportException('"' . $this->fileUrl . '" appears broken, and returned an empty 200 response'); } $errorMessage = ''; set_error_handler(function ($code, $msg) use(&$errorMessage) { if ($errorMessage) { $errorMessage .= "\n"; } $errorMessage .= preg_replace('{^file_put_contents\\(.*?\\): }', '', $msg); }); $result = (bool) file_put_contents($fileName, $result); restore_error_handler(); if (false === $result) { throw new TransportException('The "' . $this->fileUrl . '" file could not be written to ' . $fileName . ': ' . $errorMessage); } } // Handle SSL cert match issues if (false === $result && false !== strpos($errorMessage, 'Peer certificate') && PHP_VERSION_ID < 50600) { // Certificate name error, PHP doesn't support subjectAltName on PHP < 5.6 // The procedure to handle sAN for older PHP's is: // // 1. Open socket to remote server and fetch certificate (disabling peer // validation because PHP errors without giving up the certificate.) // // 2. Verifying the domain in the URL against the names in the sAN field. // If there is a match record the authority [host/port], certificate // common name, and certificate fingerprint. // // 3. Retry the original request but changing the CN_match parameter to // the common name extracted from the certificate in step 2. // // 4. To prevent any attempt at being hoodwinked by switching the // certificate between steps 2 and 3 the fingerprint of the certificate // presented in step 3 is compared against the one recorded in step 2. if (TlsHelper::isOpensslParseSafe()) { $certDetails = $this->getCertificateCnAndFp($this->fileUrl, $options); if ($certDetails) { $this->peerCertificateMap[$this->getUrlAuthority($this->fileUrl)] = $certDetails; $this->retry = true; } } else { $this->io->writeError(sprintf('<error>Your version of PHP, %s, is affected by CVE-2013-6420 and cannot safely perform certificate validation, we strongly suggest you upgrade.</error>', PHP_VERSION)); } } if ($this->retry) { $this->retry = false; $result = $this->get($this->originUrl, $this->fileUrl, $additionalOptions, $this->fileName, $this->progress); if ($this->storeAuth && $this->config) { $authHelper = new AuthHelper($this->io, $this->config); $authHelper->storeAuth($this->originUrl, $this->storeAuth); $this->storeAuth = false; } return $result; } if (false === $result) { $e = new TransportException('The "' . $this->fileUrl . '" file could not be downloaded: ' . $errorMessage, $errorCode); if (!empty($http_response_header[0])) { $e->setHeaders($http_response_header); } if (!$this->degradedMode && false !== strpos($e->getMessage(), 'Operation timed out')) { $this->degradedMode = true; $this->io->writeError(array('<error>' . $e->getMessage() . '</error>', '<error>Retrying with degraded mode, check https://getcomposer.org/doc/articles/troubleshooting.md#degraded-mode for more info</error>')); return $this->get($this->originUrl, $this->fileUrl, $additionalOptions, $this->fileName, $this->progress); } throw $e; } if (!empty($http_response_header[0])) { $this->lastHeaders = $http_response_header; } return $result; }
public function processBatch(BatchPacket $packet, Player $p) { $str = zlib_decode($packet->payload, 1024 * 1024 * 64); //Max 64MB $len = strlen($str); $offset = 0; try { while ($offset < $len) { $pkLen = Binary::readInt(substr($str, $offset, 4)); $offset += 4; $buf = substr($str, $offset, $pkLen); $offset += $pkLen; if (($pk = $this->getPacket(ord($buf[1]))) !== null) { if ($pk::NETWORK_ID === Info::BATCH_PACKET) { throw new \InvalidStateException("Invalid BatchPacket inside BatchPacket"); } $pk->setBuffer($buf, 2); $pk->decode(); $p->handleDataPacket($pk); if ($pk->getOffset() <= 0) { return; } } } } catch (\Throwable $e) { if (\pocketmine\DEBUG > 1) { $logger = $this->server->getLogger(); if ($logger instanceof MainLogger) { $logger->debug("BatchPacket " . " 0x" . bin2hex($packet->payload)); $logger->logException($e); } } } }
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; }
public function get($url) { $context = $this->getStreamContext($url); $result = file_get_contents($url, NULL, $context); if ($result && extension_loaded('zlib')) { $decode = FALSE; foreach ($http_response_header as $header) { if (preg_match('{^content-encoding: *gzip *$}i', $header)) { $decode = TRUE; continue; } elseif (preg_match('{^HTTP/}i', $header)) { $decode = FALSE; } } if ($decode) { if (version_compare(PHP_VERSION, '5.4.0', '>=')) { $result = zlib_decode($result); } else { // work around issue with gzuncompress & co that do not work with all gzip checksums $result = file_get_contents('compress.zlib://data:application/octet-stream;base64,' . base64_encode($result)); } if (!$result) { throw new RuntimeException('Failed to decode zlib stream'); } } } return $result; }
public function __construct(Plugin $plugin, $xfg) { $this->owner = $plugin; $this->keepers = []; $cfg = (new Config($plugin->getDataFolder() . "shops.yml", Config::YAML))->getAll(); $this->state = []; foreach ($cfg as $i => $j) { $this->keepers[$i] = []; if (isset($j["messages"])) { $this->keepers[$i]["messages"] = $j["messages"]; } else { $this->keepers[$i]["messages"] = []; } $this->keepers[$i]["attack"] = isset($j["attack"]) ? $j["attack"] : 5; $this->keepers[$i]["slim"] = isset($j["slim"]) ? $j["slim"] : false; $this->keepers[$i]["displayName"] = isset($j["display"]) ? $j["display"] : "default"; // Load the skin in memory if (is_file($plugin->getDataFolder() . $j["skin"])) { $this->keepers[$i]["skin"] = zlib_decode(file_get_contents($plugin->getDataFolder() . $j["skin"])); } else { $this->keepers[$i]["skin"] = null; } if (isset($cfg[$i]["msgs"])) { $this->keepers[$i]["msgs"] = $cfg[$i]["msgs"]; } $items = isset($cfg[$i]["items"]) && $cfg[$i]["items"] ? $cfg[$i]["items"] : ["IRON_SWORD,2", "APPLE,10,1"]; $this->keepers[$i]["items"] = []; foreach ($items as $n) { $t = explode(",", $n); if (count($t) < 2 || count($t) > 3) { $plugin->getLogger()->error(mc::_("Item error: %1%", $n)); continue; } $item = Item::fromString(array_shift($t)); if ($item->getId() == Item::AIR) { $plugin->getLogger()->error(mc::_("Unknown Item error: %1%", $n)); continue; } $price = intval(array_pop($t)); if ($price <= 0) { $plugin->getLogger()->error(mc::_("Invalid price: %1%", $n)); continue; } if (count($t)) { $qty = intval($t[0]); if ($qty <= 0 || $qty >= $item->getMaxStackSize()) { $plugin->getLogger()->error(mc::_("Bad quantity: %1%", $n)); continue; } $item->setCount($qty); } echo "Item: " . $item->getId() . "," . $item->getCount() . "\n"; //##DEBUG $this->keepers[$i]["items"][implode(":", [$item->getId(), $item->getDamage()])] = [$item, $price]; } if (count($this->keepers[$i]["items"])) { continue; } $plugin->getLogger()->error(mc::_("ShopKeep %1% disabled!", $i)); unset($this->keepers[$i]); continue; } if (count($this->keepers) == 0) { $plugin->getLogger()->error(mc::_("No shopkeepers found!")); $this->keepers = null; return; } Entity::registerEntity(TraderNpc::class, true); $this->owner->getServer()->getPluginManager()->registerEvents($this, $this->owner); $this->owner->getServer()->getScheduler()->scheduleRepeatingTask(new PluginCallbackTask($this->owner, [$this, "spamPlayers"], [$xfg["range"], $xfg["freq"]]), $xfg["ticks"]); }
protected function get($originUrl, $fileUrl, $additionalOptions = array(), $fileName = null, $progress = true) { if (strpos($originUrl, '.github.com') === strlen($originUrl) - 11) { $originUrl = 'github.com'; } $this->bytesMax = 0; $this->originUrl = $originUrl; $this->fileUrl = $fileUrl; $this->fileName = $fileName; $this->progress = $progress; $this->lastProgress = null; $this->retryAuthFailure = true; $this->lastHeaders = array(); if (preg_match('{^https?://(.+):(.+)@([^/]+)}i', $fileUrl, $match)) { $this->io->setAuthentication($originUrl, urldecode($match[1]), urldecode($match[2])); } if (isset($additionalOptions['retry-auth-failure'])) { $this->retryAuthFailure = (bool) $additionalOptions['retry-auth-failure']; unset($additionalOptions['retry-auth-failure']); } $options = $this->getOptionsForUrl($originUrl, $additionalOptions); if ($this->io->isDebug()) { $this->io->writeError((substr($fileUrl, 0, 4) === 'http' ? 'Downloading ' : 'Reading ') . $fileUrl); } if (isset($options['github-token'])) { $fileUrl .= (false === strpos($fileUrl, '?') ? '?' : '&') . 'access_token=' . $options['github-token']; unset($options['github-token']); } if (isset($options['http'])) { $options['http']['ignore_errors'] = true; } $ctx = StreamContextFactory::getContext($fileUrl, $options, array('notification' => array($this, 'callbackGet'))); if ($this->progress) { $this->io->writeError(" Downloading: <comment>Connecting...</comment>", false); } $errorMessage = ''; $errorCode = 0; $result = false; set_error_handler(function ($code, $msg) use(&$errorMessage) { if ($errorMessage) { $errorMessage .= "\n"; } $errorMessage .= preg_replace('{^file_get_contents\\(.*?\\): }', '', $msg); }); try { $result = file_get_contents($fileUrl, false, $ctx); } catch (\Exception $e) { if ($e instanceof TransportException && !empty($http_response_header[0])) { $e->setHeaders($http_response_header); } if ($e instanceof TransportException && $result !== false) { $e->setResponse($result); } $result = false; } if ($errorMessage && !ini_get('allow_url_fopen')) { $errorMessage = 'allow_url_fopen must be enabled in php.ini (' . $errorMessage . ')'; } restore_error_handler(); if (isset($e) && !$this->retry) { throw $e; } if (!empty($http_response_header[0]) && preg_match('{^HTTP/\\S+ ([45]\\d\\d)}i', $http_response_header[0], $match)) { $errorCode = $match[1]; if (!$this->retry) { $e = new TransportException('The "' . $this->fileUrl . '" file could not be downloaded (' . $http_response_header[0] . ')', $errorCode); $e->setHeaders($http_response_header); $e->setResponse($result); throw $e; } $result = false; } if ($result && extension_loaded('zlib') && substr($fileUrl, 0, 4) === 'http') { $decode = false; foreach ($http_response_header as $header) { if (preg_match('{^content-encoding: *gzip *$}i', $header)) { $decode = true; continue; } elseif (preg_match('{^HTTP/}i', $header)) { $decode = false; } } if ($decode) { if (version_compare(PHP_VERSION, '5.4.0', '>=')) { $result = zlib_decode($result); } else { $result = file_get_contents('compress.zlib://data:application/octet-stream;base64,' . base64_encode($result)); } if (!$result) { throw new TransportException('Failed to decode zlib stream'); } } } if ($this->progress && !$this->retry) { $this->io->overwriteError(" Downloading: <comment>100%</comment>"); } if (false !== $result && null !== $fileName) { if ('' === $result) { throw new TransportException('"' . $this->fileUrl . '" appears broken, and returned an empty 200 response'); } $errorMessage = ''; set_error_handler(function ($code, $msg) use(&$errorMessage) { if ($errorMessage) { $errorMessage .= "\n"; } $errorMessage .= preg_replace('{^file_put_contents\\(.*?\\): }', '', $msg); }); $result = (bool) file_put_contents($fileName, $result); restore_error_handler(); if (false === $result) { throw new TransportException('The "' . $this->fileUrl . '" file could not be written to ' . $fileName . ': ' . $errorMessage); } } if ($this->retry) { $this->retry = false; $result = $this->get($this->originUrl, $this->fileUrl, $additionalOptions, $this->fileName, $this->progress); $authHelper = new AuthHelper($this->io, $this->config); $authHelper->storeAuth($this->originUrl, $this->storeAuth); $this->storeAuth = false; return $result; } if (false === $result) { $e = new TransportException('The "' . $this->fileUrl . '" file could not be downloaded: ' . $errorMessage, $errorCode); if (!empty($http_response_header[0])) { $e->setHeaders($http_response_header); } throw $e; } if (!empty($http_response_header[0])) { $this->lastHeaders = $http_response_header; } return $result; }
/** * Get file content or copy action. * * @param string $originUrl The origin URL * @param string $fileUrl The file URL * @param array $additionalOptions context options * @param string $fileName the local filename * @param boolean $progress Display the progression * * @throws TransportException|\Exception * @throws TransportException When the file could not be downloaded * * @return bool|string */ protected function get($originUrl, $fileUrl, $additionalOptions = array(), $fileName = null, $progress = true) { $this->bytesMax = 0; $this->originUrl = $originUrl; $this->fileUrl = $fileUrl; $this->fileName = $fileName; $this->progress = $progress; $this->lastProgress = null; // capture username/password from URL if there is one if (preg_match('{^https?://(.+):(.+)@([^/]+)}i', $fileUrl, $match)) { $this->io->setAuthentication($originUrl, urldecode($match[1]), urldecode($match[2])); } $options = $this->getOptionsForUrl($originUrl, $additionalOptions); if ($this->io->isDebug()) { $this->io->write((substr($fileUrl, 0, 4) === 'http' ? 'Downloading ' : 'Reading ') . $fileUrl); } if (isset($options['github-token'])) { $fileUrl .= (false === strpos($fileUrl, '?') ? '?' : '&') . 'access_token=' . $options['github-token']; unset($options['github-token']); } $ctx = StreamContextFactory::getContext($fileUrl, $options, array('notification' => array($this, 'callbackGet'))); if ($this->progress) { $this->io->write(" Downloading: <comment>connection...</comment>", false); } $errorMessage = ''; $errorCode = 0; $result = false; set_error_handler(function ($code, $msg) use(&$errorMessage) { if ($errorMessage) { $errorMessage .= "\n"; } $errorMessage .= preg_replace('{^file_get_contents\\(.*?\\): }', '', $msg); }); try { $result = file_get_contents($fileUrl, false, $ctx); } catch (\Exception $e) { if ($e instanceof TransportException && !empty($http_response_header[0])) { $e->setHeaders($http_response_header); } } if ($errorMessage && !ini_get('allow_url_fopen')) { $errorMessage = 'allow_url_fopen must be enabled in php.ini (' . $errorMessage . ')'; } restore_error_handler(); if (isset($e) && !$this->retry) { throw $e; } // fix for 5.4.0 https://bugs.php.net/bug.php?id=61336 if (!empty($http_response_header[0]) && preg_match('{^HTTP/\\S+ ([45]\\d\\d)}i', $http_response_header[0], $match)) { $result = false; $errorCode = $match[1]; } // decode gzip if ($result && extension_loaded('zlib') && substr($fileUrl, 0, 4) === 'http') { $decode = false; foreach ($http_response_header as $header) { if (preg_match('{^content-encoding: *gzip *$}i', $header)) { $decode = true; continue; } elseif (preg_match('{^HTTP/}i', $header)) { $decode = false; } } if ($decode) { if (version_compare(PHP_VERSION, '5.4.0', '>=')) { $result = zlib_decode($result); } else { // work around issue with gzuncompress & co that do not work with all gzip checksums $result = file_get_contents('compress.zlib://data:application/octet-stream;base64,' . base64_encode($result)); } } } if ($this->progress) { $this->io->overwrite(" Downloading: <comment>100%</comment>"); } // handle copy command if download was successful if (false !== $result && null !== $fileName) { if ('' === $result) { throw new TransportException('"' . $this->fileUrl . '" appears broken, and returned an empty 200 response'); } $errorMessage = ''; set_error_handler(function ($code, $msg) use(&$errorMessage) { if ($errorMessage) { $errorMessage .= "\n"; } $errorMessage .= preg_replace('{^file_put_contents\\(.*?\\): }', '', $msg); }); $result = (bool) file_put_contents($fileName, $result); restore_error_handler(); if (false === $result) { throw new TransportException('The "' . $this->fileUrl . '" file could not be written to ' . $fileName . ': ' . $errorMessage); } } if ($this->retry) { $this->retry = false; return $this->get($this->originUrl, $this->fileUrl, $additionalOptions, $this->fileName, $this->progress); } if (false === $result) { $e = new TransportException('The "' . $this->fileUrl . '" file could not be downloaded: ' . $errorMessage, $errorCode); if (!empty($http_response_header[0])) { $e->setHeaders($http_response_header); } throw $e; } return $result; }
private static function decode($data) { $decoded = zlib_decode($data); if ($decoded === false) { throw new \Exception('Cannot decode data: ' . base64_encode($data)); } return $decoded; }