/** * @see File_Archive_Reader::next() */ function next() { $error = parent::next(); if (PEAR::isError($error)) { return $error; } if ($this->rarFile === null) { $dataFilename = $this->source->getDataFilename(); if ($dataFilename !== null) { $this->rarTmpName = null; $this->rarFile = rar_open($dataFilename); } else { $this->rarTmpName = tempnam(File_Archive::getOption('tmpDirectory'), 'far'); //Generate the tmp data $dest = new File_Archive_Writer_Files(); $dest->newFile($this->tmpName); $this->source->sendData($dest); $dest->close(); $this->rarFile = rar_open($this->tmpName); } if (!$this->rarFile) { return PEAR::raiseError("Unable to open rar file {$dataFilename}"); } if ($this->rarList === null) { $this->rarList = rar_list($this->rarFile); reset($this->rarList); } } if ($fileReader !== null) { $this->fileReader->close(); $this->fileReader = null; } $entryName = next($this->rarList); $this->source = null; if ($entryName === false) { return false; } $this->rarEntry = rar_entry_get($this->rarFile, $entryName); if (!$this->rarEntry) { return PEAR::raiseError("Error reading entry {$entryName}"); } return true; }
$zip_entry_name = zip_entry_name($zip_entry); $zen_ar = explode('.', $zip_entry_name); $zip_entry_type = end($zen_ar); $data = array('date' => $date, 'type' => $zip_entry_type, 'name' => $zip_entry_name, 'file_encode' => base64_encode($buf)); scraperwiki::save_sqlite(array('date', 'name'), $data, 'file'); } break; case 'rar': $handle = fopen('/tmp/co_parl.rar', "w"); fwrite($handle, $bin_file); $rar = RarArchive::open('/tmp/co_parl.rar'); $rar_list = $rar->getEntries(); foreach ($rar_list as $rar_file) { $ren_ar = explode('.', $rar_file->getName()); $rar_entry_type = end($ren_ar); $entry = rar_entry_get($rar, $rar_file->getName()); $entry->extract('/tmp/' . $rar_file->getName()); $buf = file_get_contents('/tmp/' . $rar_file->getName()); $data = array('date' => $date, 'type' => $rar_entry_type, 'name' => $rar_file->getName(), 'file_encode' => base64_encode($buf)); scraperwiki::save_sqlite(array('date', 'name'), $data, 'file'); } break; default: $data = array('date' => $date, 'type' => $type, 'name' => $title . '.' . $type, 'file_encode' => base64_encode($bin_file)); scraperwiki::save_sqlite(array('date', 'name'), $data, 'file'); break; } fclose($handle); } } //$file = scraperwiki::scrape($url);
public function get_index() { $dir = App::$param['path'] . "app/tmp/" . Connection::$param["postgisdb"] . "/__vectors"; $safeName = \app\inc\Model::toAscii($_REQUEST['name'], array(), "_"); $skipFailures = $_REQUEST["ignoreerrors"] == "true" ? true : false; $append = $_REQUEST["append"] == "true" ? true : false; $overwrite = $_REQUEST["overwrite"] == "true" ? true : false; if (is_numeric($safeName[0])) { $safeName = "_" . $safeName; } //Check if file is .zip $zipCheck1 = explode(".", $_REQUEST['file']); $zipCheck2 = array_reverse($zipCheck1); if (strtolower($zipCheck2[0]) == "zip" || strtolower($zipCheck2[0]) == "rar") { $ext = array("shp", "tab", "geojson", "gml", "kml", "mif", "gdb"); $folderArr = array(); $safeNameArr = array(); for ($i = 0; $i < sizeof($zipCheck1) - 1; $i++) { $folderArr[] = $zipCheck1[$i]; } $folder = implode(".", $folderArr); if (strtolower($zipCheck2[0]) == "zip") { // ZIP start $zip = new \ZipArchive(); $res = $zip->open($dir . "/" . $_REQUEST['file']); if ($res === false) { $response['success'] = false; $response['message'] = "Could not unzip file"; return Response::json($response); } $zip->extractTo($dir . "/" . $folder); $zip->close(); // ZIP end } if (strtolower($zipCheck2[0]) == "rar") { // RAR start $rar_file = rar_open($dir . "/" . $_REQUEST['file']); if (!$rar_file) { $response['success'] = false; $response['message'] = "Could not unrar file"; return Response::json($response); } $list = rar_list($rar_file); foreach ($list as $file) { $entry = rar_entry_get($rar_file, $file); $file->extract($dir . "/" . $folder); // extract to the current dir } rar_close($rar_file); // RAR end } if ($handle = opendir($dir . "/" . $folder)) { while (false !== ($entry = readdir($handle))) { if ($entry !== "." && $entry !== "..") { $zipCheck1 = explode(".", $entry); $zipCheck2 = array_reverse($zipCheck1); if (in_array(strtolower($zipCheck2[0]), $ext)) { $_REQUEST['file'] = $folder . "/" . $entry; for ($i = 0; $i < sizeof($zipCheck1) - 1; $i++) { $safeNameArr[] = $zipCheck1[$i]; } $safeName = \app\inc\Model::toAscii(implode(".", $safeNameArr), array(), "_"); break; } $_REQUEST['file'] = $folder; } } } } $srid = $_REQUEST['srid'] ?: "4326"; $encoding = $_REQUEST['encoding'] ?: "LATIN1"; switch ($_REQUEST['type']) { case "Point": $type = "point"; break; case "Polygon": $type = "multipolygon"; break; case "Line": $type = "multilinestring"; break; case "Geometry": $type = "geometry"; break; default: $type = "PROMOTE_TO_MULTI"; break; } $model = new \app\inc\Model(); $tableExist = $model->isTableOrView(Connection::$param["postgisschema"] . "." . $safeName); $tableExist = $tableExist["success"]; if ($tableExist == true && $overwrite == false && $append == false) { $response['success'] = false; $response['message'] = "'{$safeName}' exists already, use 'Overwrite'"; $response['code'] = 406; return $response; } if ($_REQUEST["append"] == "true") { $sql = "DELETE FROM " . Connection::$param["postgisschema"] . "." . $safeName; $res = $model->prepare($sql); try { $res->execute(); } catch (\PDOException $e) { $response['success'] = false; $response['message'] = "Could not delete from {$safeName}"; $response['code'] = 406; return $response; } } $cmd = "PGCLIENTENCODING={$encoding} ogr2ogr " . ($skipFailures ? "-skipfailures " : " ") . ($append ? "-append " : " ") . ($overwrite == true && $append == false ? "-overwrite " : " ") . "-dim 2 " . ($append ? "" : "-lco 'GEOMETRY_NAME=the_geom' ") . ($append ? "" : "-lco 'FID=gid' ") . ($append ? "" : "-lco 'PRECISION=NO' ") . ($append ? "" : "-lco 'PG_USE_COPY=YES' ") . "-a_srs 'EPSG:{$srid}' " . "-f 'PostgreSQL' PG:'host=" . Connection::$param["postgishost"] . " user="******"postgisuser"] . " password="******"postgispw"] . " dbname=" . Connection::$param["postgisdb"] . " active_schema=" . Connection::$param["postgisschema"] . "' " . "'" . $dir . "/" . $_REQUEST['file'] . "' " . "-nln {$safeName} " . "-nlt {$type}"; exec($cmd . ' 2>&1', $out, $err); $geoType = $model->getGeometryColumns(Connection::$param["postgisschema"] . "." . $safeName, "type"); $key = Connection::$param["postgisschema"] . "." . $safeName . ".the_geom"; $class = new \app\models\Classification($key); $arr = $class->getAll(); // Set layer editable $join = new \app\models\Table("settings.geometry_columns_join"); $json = '{"data":{"editable":true,"_key_":"' . $key . '"}}'; $data = (array) json_decode(urldecode($json)); $join->updateRecord($data, "_key_"); if (empty($arr['data'])) { $class->insert(); $class->update("0", \app\models\Classification::createClass($geoType)); } $def = new \app\models\Tile($key); $arr = $def->get(); if (empty($arr['data'][0])) { $json = '{ "theme_column":"", "label_column":"", "query_buffer":"", "opacity":"", "label_max_scale":"", "label_min_scale":"", "meta_tiles":false, "meta_size":"3", "meta_buffer":"10", "ttl":""}'; $def->update($json); } if ($out[0] == "") { $response['success'] = true; $response['message'] = "Layer <b>{$safeName}</b> is created"; $response['type'] = $geoType; // Bust cache, in case of layer already exist \app\controllers\Tilecache::bust(Connection::$param["postgisschema"] . "." . $safeName); } else { $response['success'] = false; $response['message'] = $safeName . ": Some thing went wrong. Check the log."; $response['out'] = $out[0]; Session::createLog($out, $_REQUEST['file']); // Make sure the table is dropped if not skipping failures and it didn't exists before if ($skipFailures == false && $tableExist == false) { $sql = "DROP TABLE " . Connection::$param["postgisschema"] . "." . $safeName; $res = $model->prepare($sql); try { $res->execute(); } catch (\PDOException $e) { } } } $response['cmd'] = $cmd; return $response; }
echo $z->getFromName($path); } $z = null; } else { $z = rar_open($f->path); if ($z) { if (substr($path, -1) == '/' || $path == "") { foreach (rar_list($z) as $s) { if ($path == "" || substr_compare($path, $s->name, 0, strlen($path), true) == 0 && $path != $s->name) { $arr[] = $s->name; } } asort($arr); foreach ($arr as $n) { print '<a href="http://filebin.ca/view/' . $tag . '/' . $n . '">' . $n . '</a><br />'; } } else { rar_entry_get($z, $path)->extract(false, "/tmp/rar.extr.1"); $finfo = finfo_open(FILEINFO_MIME); header("Content-Type: " . finfo_open($finfo, "/tmp/rar.extr.1")); finfo_close($finfo); @readfile("/tmp/rar.extr.1"); unlink("/tmp/rar.extr.1"); } # rar_close($z); } $z = null; } } else { print "No such file?"; }
function content_do_import_data() { global $cur_mysql_table; // just a quick error message function for the export routine function draw_errmsg($msg, $qry, $msg2) { echo "<p><span style=\"font-size:110%;\"><b>{$msg}</b></span> -- <br />\n <i>{$qry}</i><br /><b>{$msg2}</b></p>\n"; } // used by both LOAD DATA and Sqlicity: $importLOCALfile = trim($_POST['importLOCALfile']) ? TRUE : FALSE; $local_importfile = trim($_POST['sqlicity_importfile1']); $server_importfile = trim($_POST['sqlicity_importfile2']); $column_headerlist = trim($_POST['columnheaderlist']); $linestoignore = (int) trim($_POST['linestoignore']); $replaceexistingrecords = trim($_POST['replaceexistingrecords']) ? TRUE : FALSE; // make sure they requested a file! if (!$local_importfile && !$server_importfile) { draw_errmsg('You need to specify a file to import!', '', ''); return; } // check if they uploaded something but it didn't make it onto the server if ($importLOCALfile && $local_importfile && !$_FILES['sqlicity_importfile_browser']['tmp_name']) { draw_errmsg('Your file was not properly uploaded - are you sure it is small enough for PHP to upload it?', '', ''); return; } // see if they want MySQL to do the export, rather than Sqlicity: if ($_POST['useLOADDATAINFILE']) { // check for LOCAL file reference if ($importLOCALfile) { $thefile = $_FILES['sqlicity_importfile_browser']['tmp_name']; } else { // import location can be relative or absolute if ($_POST['importfile_absolutepath']) { $thefile = $server_importfile; } else { $thefile = getcwd() . '/' . $server_importfile; } } // do the import mysql_query($query = "LOAD DATA {$_POST['ldi_lowpri_concur']}\n\t\t " . ($importLOCALfile ? 'LOCAL' : '') . "\n\t\t INFILE '{$thefile}' " . ($replaceexistingrecords ? 'REPLACE' : 'IGNORE') . "\n\t\t INTO TABLE `{$cur_mysql_table}`\n\t\t FIELDS\n\t\t TERMINATED BY '{$_POST['fields_terminatedby']}'\n\t\t ENCLOSED BY '{$_POST['fields_enclosedby']}'\n\t\t ESCAPED BY '{$_POST['fields_escapedby']}'\n\t\t LINES\n\t\t TERMINATED BY '{$_POST['lines_terminatedby']}'\n\t\t STARTING BY '{$_POST['lines_startingby']}'\n\t\t IGNORE " . (int) $_POST['linestoignore'] . " LINES\n\t\t " . ($column_headerlist ? '(' . $column_headerlist . ')' : '') . "\n\t\t {$_POST['ldi_setlist']}\n\t\t;"); // find out how many rows were inserted/replaced/ignored $import_affected_rows = mysql_affected_rows(); echo 'imported: ' . $import_affected_rows; // see if they have an error trying this query // note that we "fix" the file name for LOCAL uploads, to maintain the illusion if (mysql_error()) { draw_errmsg('Your LOAD DATA INFILE failed', nl2br($importLOCALfile ? str_replace($thefile, $local_importfile, $query) : $query), mysql_error()); return; } // quit now, the rest is for sqlicity's import functionality return; } /* IMPORTING RULES THAT I HAVE DETERMINED THROUGH EXPERIMENTATION AND TRUSTED READING: ====================================================================================== 1) escape char itself is ignored completely, next char is escaped only if it is a valid escapable char -- escape char works inside enclosers AND not! -- valid escape chars are NOT just \N and \0 -- it's the full list: http://dev.mysql.com/doc/mysql/en/string-syntax.html 2) enclosing chars, if present ON EDGES (see !5) are removed, if not present, doesn't matter !3) doubled enclosing char becomes single char -- ONLY inside same enclosers! !4) If the line is missing commas to start some columns, those columns must be set to their "defaults": "If an input line has too few fields, the table columns for which input fields are missing are set to their default values." ** -- HOWEVER, 3.23 does not recognize the "DEFAULT" keyword on inserts, like all later versions do but, there is a column in the SHOW COLUMNS results... 'default' that I can just use anyway although this does require that I know the order of the columns, which I do -- the order in the SHOW COLUMNS results !5) opening enclosing char MUST be first char, and closing enclosing char MUST come before a field terminator or line terminator -- IF enclosed string with SINGLE enclosing char NOT before field/line terminator, then this one enclosing char gets changed to "!!! (yes, a single double quote char) on 3.23.54 6) no such thing as comments exist in csvs -- EXCEPT I can have anything in between line terminator and line starting !7) escaped N (e.g. \N) is ONLY NULL if it is the ONLY thing in the field!! otherwise it is just N (escape char dropped) 8) ESCAPED and ENCLOSED must be one single char, TERMINATED and STARTED can be any string 9) lots of weird interactions when all above empty, starting on load-data page: 'In certain cases, field- and line-handling options interact' 10) NULLs (\N) are imported in or out of enclosing chars, but are exported withOUT enclosing chars. http://dev.mysql.com/doc/mysql/en/load-data.html */ // check for LOCAL file reference if ($importLOCALfile) { $thefile = $_FILES['sqlicity_importfile_browser']['tmp_name']; } else { // import location can be relative or absolute if ($_POST['importfile_absolutepath']) { $thefile = $server_importfile; } else { $thefile = getcwd() . '/' . $server_importfile; } } // the file must exist! if (!@file_exists($thefile)) { draw_errmsg('That file does NOT exist!', $importLOCALfile ? $local_importfile : $thefile, ''); return; } // find out how many rows were inserted/replaced/ignored $import_affected_rows = 0; // store all column names as keys, and their defaults as values $display_columns_defaults = array(); // store all column names, and enclosed columns+BLOB columns // SEE THE COMMENTS WITH THE SELECT PAGE CONTENTS $blob_cols = array(); // track BLOB columns $result_cols = mysql_query("SHOW COLUMNS FROM `{$cur_mysql_table}`"); // make sure table still exists if (!$result_cols) { echo "<p><b>Table <i>`{$cur_mysql_table}`</i> no longer exists!</b></p>\n"; return; } // store all the columns, with their defaults, and flag important ones while ($column = mysql_fetch_assoc($result_cols)) { // find out if this column should be enclosed on output if (isEnclosedColumn($column['Type'])) { $enclosed_cols[$column['Field']] = TRUE; } // handle BLOB type columns specially if (strtolower(substr($column['Type'], 0, 4)) == 'blob') { $blob_cols[$column['Field']] = TRUE; } // store all column names in case no explicit select_columns was given $display_columns_defaults[$column['Field']] = $column['Default']; } // is the first line supposed to be a column header list? $usefirstlineheader = trim($_POST['firstlineheader']) ? TRUE : FALSE; // field/line enclosed/escaped/etc $field_enclose_char = unescape_mysql_chars($_POST['fields_enclosedby']); $field_enclose_char = $field_enclose_char[0]; // force only one char $field_escape_char = unescape_mysql_chars($_POST['fields_escapedby']); $field_escape_char = $field_escape_char[0]; // force only one char $field_terminatedby = unescape_mysql_chars($_POST['fields_terminatedby']); $line_terminatedby = unescape_mysql_chars($_POST['lines_terminatedby']); $line_startingby = unescape_mysql_chars($_POST['lines_startingby']); // shortcut to strlen on these, for finishing comparing them with input $line_startingby_len = strlen($line_startingby); $line_terminatedby_len = strlen($line_terminatedby); $field_terminatedby_len = strlen($field_terminatedby); // prep FSM variables // whether or not there is a startingby string affects which state to start in $line_start_state = $line_startingby_len ? 1 : 2; $importFSMstate = $line_start_state; // line starting state: no line or new field? $escaped = FALSE; // was the last char the escape char, trying to escape this current char? $enclosed = FALSE; // is this an enclosed field or not? $linevalues = array(); // holds the list of values to insert $curcol = -1; // incremented every col, including first, so need to start here to hit zero for first $check_line_i = -1; // zero is skipped to avoid matching empty terminators/etc $check_field_i = -1; // zero is... same here // VERY annoying special case -- need to track an extra bit of data // inside enclosed data, single encloser followed by false L/F terminator $addtermchar = ''; // this gets reused as the string gets too long (memory issues) $insert_values_list = ''; // ... and this restarts it $restart_insert_values_list = ($replaceexistingrecords ? 'REPLACE' : 'INSERT IGNORE') . " INTO `{$cur_mysql_table}`\n (" . ($column_headerlist ? $column_headerlist : implode(',', array_map(create_function('$c', 'return "`$c`";'), array_keys($display_columns_defaults)))) . ")\n VALUES\n"; // get the parameters for this upload $import_filehandle = tmpfile(); $compression = $_POST['import_file_compression']; // make sure the file is readable if (!$import_filehandle) { draw_errmsg('That file does is not readable!', $importLOCALfile ? $local_importfile : $thefile, ''); return; } // first, pull out the sql file, based on compression type // bzip2 if ($compression == 'bz2') { // open file for reading $bz = bzopen($thefile, 'r'); // get the data from the file while ($d = bzread($bz)) { fwrite($import_filehandle, $d); } // close the compressed file bzclose($bz); } elseif ($compression == 'gz') { // open file for reading $zp = gzopen($thefile, 'r'); // get the data from the file while ($d = gzread($zp, 1024)) { fwrite($import_filehandle, $d); } // close the compressed file gzclose($zp); } elseif ($compression == 'zip') { // open the zip archive if ($zip = zip_open($thefile)) { // zip can have multiple files while ($zip_entry = zip_read($zip)) { // include the data in ALL the zipped files if (zip_entry_open($zip, $zip_entry, "r")) { fwrite($import_filehandle, zip_entry_read($zip_entry, zip_entry_filesize($zip_entry))); } zip_entry_close($zip_entry); } // close the compressed file handle zip_close($zip); } } elseif ($compression == 'rar') { // open the rar archive if ($rar = rar_open($thefile)) { // rar can have multiple files $entries = rar_list($rar); // include the data in ALL the rar'd files $rar_temp_name = tempnam(FALSE, 'sqlicity_rar'); foreach ($entries as $entry) { // get the actual entry for extracting $entry = rar_entry_get($rar, $entry); // have to create the extracted file, read it then delete it... // because rar ONLY has an extract() function, no direct read! if ($entry->extract(FALSE, $rar_temp_name)) { fwrite($import_filehandle, file_get_contents($rar_temp_name)); } } // close the compressed file handle and remove temp file unlink($rar_temp_name); rar_close($rar); } } else { // open plaintext file for reading $pt = fopen($thefile, 'r'); // get the data from the file while ($d = fread($pt, 1024)) { fwrite($import_filehandle, $d); } // close the plaintext file fclose($pt); } // else { fwrite($import_filehandle, file_get_contents($thefile))); } // needed for escaping input chars global $mysql_escaped_chars; // we just wrote the file for import, now back up to read it all in rewind($import_filehandle); // fseek($import_filehandle, 0); while (!feof($import_filehandle)) { // get this current line and iterate over all its chars // max sure to check if this line is shorter than what we asked for... $next_import_line = fread($import_filehandle, 8192); $maxlen = strlen($next_import_line); for ($i = 0; $i < $maxlen; $i++) { // get the char to run through the FSM $char = $next_import_line[$i]; // do the FSM itself switch ($importFSMstate) { // no line started yet -- in-between lines -- ONLY if line starting by exists! case 1: // have we found a line starting beginning and are checking it now? if ($check_line_i > 0) { // is this the next char? if ($char == $line_startingby[$check_line_i]) { // increment for the next char we'll check $check_line_i++; // ... unless it's the last char, then we're done -- start a new field if ($check_line_i == $line_startingby_len) { $check_line_i = -1; $importFSMstate = 2; } } else { $check_line_i = -1; } } elseif ($char == $line_startingby[0]) { // remember, starting by could be only one char, then this would have just ended it if ($line_startingby_len == 1) { $importFSMstate = 2; } else { $check_line_i = 1; } } elseif ($char == $field_escape_char) { $escaped = TRUE; } elseif ($escaped) { $escaped = FALSE; } break; // starting a new field -- only stays here for one char! // starting a new field -- only stays here for one char! case 2: // reset some markers every time we restart a field $enclosed = FALSE; $escaped = FALSE; // start this new column in the data $linevalues[++$curcol] = ''; // awkward special case: escaped NULLs $justNULLdata = FALSE; // everything here goes to data read state no matter what // except line/field terminator special cases below $importFSMstate = 3; // check for encloser, escape char and terminators if ($char == $field_enclose_char) { $enclosed = TRUE; } elseif ($char == $field_escape_char) { $escaped = TRUE; } elseif ($char == $line_terminatedby[0]) { $check_line_i = 1; } elseif ($char == $field_terminatedby[0]) { $check_field_i = 1; } else { $linevalues[$curcol] = $char; } break; // read in the data for a single field // read in the data for a single field case 3: // we might have a line terminator here if ($check_line_i > 0) { // if it matches, we prepare to check the next char if ($char === $line_terminatedby[$check_line_i]) { $check_line_i++; } else { // include everything up to what did NOT work, then add this char $linevalues[$curcol] .= $addtermchar . substr($line_terminatedby, 0, $check_line_i) . $char; $check_line_i = -1; $addtermchar = ''; } } elseif ($check_field_i > 0) { // if it matches, we prepare to check the next char if ($char === $field_terminatedby[$check_field_i]) { $check_field_i++; } else { // include everything up to what did NOT work, then add this char $linevalues[$curcol] .= $addtermchar . substr($field_terminatedby, 0, $check_field_i) . $char; $check_field_i = -1; $addtermchar = ''; } } else { // first check for escaped chars -- replace them if ($escaped) { // next one starts escaping all over again $escaped = FALSE; // try to find the escape sequence replacement $c2 = $mysql_escaped_chars[$char]; // if it doesn't exist, just use the original char $linevalues[$curcol] .= $c2 !== NULL ? $c2 : $char; // special case: escaped NULLs // to be a NULL, has to be the only thing in the field, enclosed or not if ($linevalues[$curcol] == 'N') { $justNULLdata = TRUE; } } elseif ($char == $field_escape_char) { $escaped = TRUE; } elseif ($enclosed && $char == $field_enclose_char) { $importFSMstate = 4; } elseif (!$enclosed && $char == $line_terminatedby[0]) { $check_line_i = 1; } elseif (!$enclosed && $char == $field_terminatedby[0]) { $check_field_i = 1; } else { $linevalues[$curcol] .= $char; } } break; // we just had an encloser char, handle the single next char to transfer again // we just had an encloser char, handle the single next char to transfer again case 4: // most of these options just go right back to data parsing, so that's the default $importFSMstate = 3; // double encloser -- add only one of them if ($char == $field_enclose_char) { $linevalues[$curcol] .= $field_enclose_char; } elseif ($char == $line_terminatedby[0]) { $check_line_i = 1; $addtermchar = $field_enclose_char; } elseif ($char == $field_terminatedby[0]) { $check_field_i = 1; $addtermchar = $field_enclose_char; } elseif ($char == $field_escape_char) { $linevalues[$curcol] .= $field_enclose_char; $escaped = TRUE; } else { $linevalues[$curcol] .= $field_enclose_char . $char; } break; } // lines/fields can terminate from any state except before there is any line (state 1) // so we check for termination here for all but "no line" if ($importFSMstate > 1) { // did this field just end? if ($check_field_i == $field_terminatedby_len) { // handle special NULL values // unenclosed 'NULL' is always NULL if (!$enclosed && $linevalues[$curcol] == 'NULL' || $linevalues[$curcol] == 'N' && $justNULLdata) { $linevalues[$curcol] = NULL; } // this one is done, start a new field $check_field_i = -1; $importFSMstate = 2; } // did this line just end? if ($check_line_i == $line_terminatedby_len) { // handle special NULL values // unenclosed 'NULL' is always NULL if (!$enclosed && $linevalues[$curcol] == 'NULL' || $linevalues[$curcol] == 'N' && $justNULLdata) { $linevalues[$curcol] = NULL; } // start a new line $check_line_i = -1; $curcol = -1; $importFSMstate = $line_start_state; // is this first line a column header list? if ($usefirstlineheader) { // set the column header list for all the real lines following // that means reorganizing the display columns defaults $display_columns_defaults2 = array(); foreach ($linevalues as $identifier) { // build the array of column names mapped to defaults // if the user used non-existent columns, // it's their problem when this breaks $display_columns_defaults2[$identifier] = $display_columns_defaults[$identifier]; } // make sure there is actually something in that first line if (count($display_columns_defaults2) > 0) { // make the switch, we're using this new header instead $display_columns_defaults = $display_columns_defaults2; unset($display_columns_defaults2); // and rebuild the insert values list restart $restart_insert_values_list = ($replaceexistingrecords ? 'REPLACE' : 'INSERT IGNORE') . " INTO `{$cur_mysql_table}`\n (" . implode(',', array_map(create_function('$c', 'return "`$c`";'), array_keys($display_columns_defaults))) . ")\n VALUES\n"; } // we got the header, turn this flag off $usefirstlineheader = FALSE; } else { // add this line we just finished // by building the INSERT VALUES list line items // NOTE: this will not be visible to the user! // so it doesn't need to be pretty! -- I make it this way to be readable here only // first check if we are ignoring more lines -- if so, don't insert this line if ($linestoignore > 0) { $linestoignore--; return; } // we are done ignoring lines, let's do the insert // do we need a , to start this linevalues list? only if not restarted IVL $old_IVL = TRUE; // see if we're starting a new values list now if (!$insert_values_list) { $old_IVL = FALSE; $insert_values_list = $restart_insert_values_list; } // now we can add the current linevalues as a values list item to our insert // start this current line -- comma for old IVLs adding extra list items, space for new ones $insert_values_list .= ($old_IVL ? ',' : ' ') . ' ('; // we will need this, for escaping imported data strings global $insert_escaped_chars; // now handle every field that needs to be inserted // -- we might not have values for all of them! $colnum = -1; // track which value to use, and to do separating on fields foreach ($display_columns_defaults as $colname => $coldefault) { // after first col, start with field terminator if (++$colnum > 0) { $insert_values_list .= ','; } // make sure this column was even defined! if (isset($linevalues[$colnum])) { $value = $linevalues[$colnum]; } else { $value = $coldefault; } // users can import all kinds of complete garbage // therefore, we MUST enclose EVERY data field, in case it's trash // and we must further escape all data as well $insert_values_list .= $value !== NULL ? "'" . add_mysql_slashes($value) . "'" : 'NULL'; } // finish this line $insert_values_list .= ")\n"; // see if we're ready to start a new IVL or not // if the size is getting big, run the IVL query and empty it for restart // current max length is ~200 kb: 200,000 bytes if (strlen($insert_values_list) > 200000) { // do the actual insert mysql_query($insert_values_list); // see if they have an error trying this import query if ($myserr = mysql_error()) { draw_errmsg('Your Sqlicity import failed!', '', $myserr); return; } // update the affected rows and reset the insert string $import_affected_rows += mysql_affected_rows(); $insert_values_list = ''; } } // end fist line column header conditional // reset the array of values we got off the current lines $linevalues = array(); } // end insert line ended (we just added a new line, or found the header) } } // end loop over the characters of current string from import file to parse } // end loop over remaining data in import file // get rid of the temporary file for uncompressed output fclose($import_filehandle); // run the final insert query we've built up mysql_query($insert_values_list); $import_affected_rows += mysql_affected_rows(); // see if they have an error trying this import query if ($myserr = mysql_error()) { draw_errmsg('Your Sqlicity import failed!', $insert_values_list, $myserr); return; } // tell the import results echo 'imported: ' . $import_affected_rows; }
continue; } $file = $dir . DIRECTORY_SEPARATOR . $obj; if (time() - filectime($file) > $secondsOld) { @unlink($file); } } closedir($dh); } CleanCacheDir($options['cachepath'], 300); $mimetype = minimime($path); if ($mimetype == 'application/x-rar-compressed') { $filename = rawurldecode(stripslashes($_GET['page'])); $rar_file = rar_open($path); $cachepathname = tempnam($options['cachepath'], "cbr"); $entry = rar_entry_get($rar_file, $filename); $entry->extract(false, $cachepathname); rar_close($rar_file); $size = resize($cachepathname, intval($_GET['maxwidth'])); echo '{' . "\n\r"; echo ' "width" : "' . $size[0] . '",' . "\n\r"; echo ' "height" : "' . $size[1] . '",' . "\n\r"; echo ' "src" : "' . $options['webcache'] . '/' . basename($cachepathname) . '"' . "\n\r"; echo '}'; } elseif ($mimetype == 'application/zip') { $index = 0; if (isset($_GET['page'])) { $index = intval($_GET['page']); } $zip = new ZipArchive(); if ($zip->open($path) === TRUE) {
/** */ function rar_extract($archive_name, $EXTRACT_PATH) { $rar = rar_open($archive_name); $list = rar_list($rar); foreach ($list as $file) { $file = explode("\"", $file); $entry = rar_entry_get($rar, $file[1]); $entry->extract($EXTRACT_PATH); } rar_close($rar); }
function extract_archive($archive_name, $path, $password) { if (!empty($archive_name) && !empty($path)) { $extension = pathinfo($path . $archive_name, PATHINFO_EXTENSION); $array_extension = array("tar" => 4, "gz" => 7, "bz2" => 8, "zip" => 4, "rar" => 4); $accepted_extension = false; $strlen_extension = ""; foreach ($array_extension as $archive_extension => $strlen_archive_extension) { if ($extension === $archive_extension) { $accepted_extension = true; $strlen_extension = $strlen_archive_extension; break; } } if ($accepted_extension === false) { send_json('Extension not supported !!', null); throw new Exception("Extension not supported !!"); } $only_file_name = substr($archive_name, 0, -$strlen_extension); $last_pos_only_file_name = strrpos($only_file_name, "-"); $tmp_only_file_name = substr($only_file_name, 0, $last_pos_only_file_name); $counter_duplicate = substr($only_file_name, $last_pos_only_file_name + 1); if (!is_int($last_pos_only_file_name) || !is_int($counter_duplicate)) { $tmp_only_file_name = $only_file_name; $counter_duplicate = 1; } while (file_exists($path . $only_file_name)) { $only_file_name = $tmp_only_file_name . "-" . $counter_duplicate; $counter_duplicate++; } mkdir($path . $only_file_name); if ($extension === "zip") { try { $zip = zip_open($path . $archive_name); if ($zip) { while ($zip_read = zip_read($zip)) { $zip_entry_name = zip_entry_name($zip_read); if (zip_entry_open($zip, $zip_read)) { $file_in_archive = zip_entry_read($zip_read, zip_entry_filesize($zip_read)); $exploded = explode("/", substr($path . $only_file_name . "/" . zip_entry_name($zip_read), strlen($path . $only_file_name) + 1)); for ($i = 0; $i < count($exploded) - 1; $i++) { if ($i === 0) { $path_after_onyl_file_name = $exploded[0]; } else { $path_after_onyl_file_name = $path_after_onyl_file_name . "/" . $exploded[$i]; } if (!file_exists($path . $only_file_name . "/" . $path_after_onyl_file_name)) { mkdir($path . $only_file_name . "/" . $path_after_onyl_file_name, 0777, true); } } file_put_contents($path . $only_file_name . "/" . zip_entry_name($zip_read), $file_in_archive); } } zip_close($zip); } } catch (Exception $e) { send_json($e->getMessage(), null); throw new Exception($e->getMessage()); } } elseif ($extension === "rar") { if ($password === "") { $rar = RarArchive::open($path . $archive_name); } else { $rar = RarArchive::open($path . $archive_name, $password); } if ($rar->isBroken()) { send_json("Rar archive broken !!", null); throw new Exception("Rar archive broken !!"); } $rar_file = rar_open($path . $archive_name); $list = rar_list($rar_file); foreach ($list as $file) { $entry = rar_entry_get($rar_file, $file->getName()); $entry->extract($path . $only_file_name); } rar_close($rar_file); } else { $archive = new PharData($path . $archive_name); try { $archive->extractTo($path . $only_file_name); } catch (Exception $e) { send_json($e->getMessage(), null); throw new Exception($e->getMessage()); } } send_json(null, $only_file_name); } }
public function extract($id, $to = false) { if (empty($this->toc['files'][$id])) { if (empty($this->toc['dirs'][$id])) { trigger_error("'{$id}' is not a valid entry.", E_USER_ERROR); } else { trigger_error("'{$id}' is not a file.", E_USER_ERROR); } } if (!$to) { trigger_error('Rar entry must be saved to file', E_USER_WARNING); return false; } if ($fp = rar_open($this->filename)) { $file = $this->toc['files'][$id]; $entry = rar_entry_get($fp, $file['filename']); $file['tmp_name'] = $to . '/' . md5($file['filename'] . $file['crc']); $entry->extract(false, $file['tmp_name']); /* $host_os = $entry->getHostOs(); $attr = $entry->getAttr(); switch ($host_os) { case RAR_HOST_MSDOS: case RAR_HOST_OS2: case RAR_HOST_WIN32: case RAR_HOST_MACOS: printf("%c%c%c%c%c%c\n", ($attr & 0x08) ? 'V' : '.', ($attr & 0x10) ? 'D' : '.', ($attr & 0x01) ? 'R' : '.', ($attr & 0x02) ? 'H' : '.', ($attr & 0x04) ? 'S' : '.', ($attr & 0x20) ? 'A' : '.'); break; case RAR_HOST_UNIX: case RAR_HOST_BEOS: switch ($attr & 0xF000) { case 0x4000: printf("d"); break; case 0xA000: printf("l"); break; default: printf("-"); break; } printf("%c%c%c%c%c%c%c%c%c\n", ($attr & 0x0100) ? 'r' : '-', ($attr & 0x0080) ? 'w' : '-', ($attr & 0x0040) ? (($attr & 0x0800) ? 's':'x'):(($attr & 0x0800) ? 'S':'-'), ($attr & 0x0020) ? 'r' : '-', ($attr & 0x0010) ? 'w' : '-', ($attr & 0x0008) ? (($attr & 0x0400) ? 's':'x'):(($attr & 0x0400) ? 'S':'-'), ($attr & 0x0004) ? 'r' : '-', ($attr & 0x0002) ? 'w' : '-', ($attr & 0x0001) ? 'x' : '-'); break; } */ rar_close($fp); return $file; } return false; }
public function extract($source, $target, $password) { $rarFile = rar_open(suffix($source, '.rar'), $password); $list = rar_list($rarFile); if (!empty($list)) { foreach ($list as $file) { $entry = rar_entry_get($rarFile, $file); $entry->extract($target); } } else { throw new InvalidArgumentException('Error', 'emptyVariable', '$list'); } rar_close($rarFile); }
public function extract($source = '', $target = '.', $password = NULL) { if (!is_file($source)) { return Error::set('Error', 'fileParameter', '1.(source)'); } $rarFile = rar_open($source, $password); $list = rar_list($rarFile); if (!empty($list)) { foreach ($list as $file) { $entry = rar_entry_get($rarFile, $file); $entry->extract($target); } } else { return Error::set('Error', 'emptyVariable', '$list'); } rar_close($rarFile); }