<?php
/**
*
* @package acp
* @author Hathor, Wagi4
* @version $Id: import.php, 2008/03/22 Wagi4 $
* @copyright (c) 2005 phpBB Group; 2008 DSphpBB
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/
//
//Definition-Part
//
define('IN_PHPBB', true);
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './../../';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
require_once($phpbb_root_path . 'common.' . $phpEx);
include_once($phpbb_root_path . 'includes/functions_staemme.' . $phpEx);
include_once($phpbb_root_path . 'includes/functions_user.' . $phpEx);
$user->add_lang("acp/staemme");
$folder = $phpbb_root_path . $staemme_config['import_path'];
$path_parts = pathinfo($_SERVER['SCRIPT_FILENAME']);
$folderpart_mysql = $path_parts["dirname"] . "/";
$folder_mysql = $folderpart_mysql . $phpbb_root_path . $staemme_config['import_path'];
$files = get_worlddata_files(true);
$fileEx = "txt";
//
// user settings
//
$multi_inserts = 300; //number of multiple inserts
$debug_mode = 0; //enable additional debug output
$start_alt = false; //force alternative import method (row by row insert)
//
//Function-Part
//
$skip = isset($_REQUEST['skip']) ? request_var('skip', 0) : false;
$skip_lines = isset($_REQUEST['skip_lines']) ? request_var('skip_lines', 0) : false;
$override = isset($_REQUEST['override']) ? request_var('override', '') : false;
$timeouts = isset($_REQUEST['timeouts']) ? request_var('timeouts', 0) : 0;
echo '<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head><script type="text/javascript">
var last_rows = 0;
var grace = true;
var start_file = '.intval($skip).';
var start_line = '.intval($skip_lines).';
function checkready () {
var Ausdruck = /(LOCK\=LD)/;
if(Ausdruck.exec(document.body.innerHTML) != null) msgbox("LD");
var Ausdruck = /(LOCK\=LI)/;
if(Ausdruck.exec(document.body.innerHTML) != null) msgbox("LI");
var Ausdruck = /(LOCK\=LG)/;
if(Ausdruck.exec(document.body.innerHTML) != null) msgbox("LG");
var Ausdruck = /FILEID=([0-9]+)/g;
var matches = document.body.innerHTML.match(Ausdruck);
var file_id;
var skip_lines;
if(matches != null)
{
matches = matches[matches.length - 1].match(/FILEID=([0-9]+)/);
file_id = matches[1];
} else
{
file_id = start_file;
}
if(skip_lines != 0 && file_id > start_file) skip_lines = 0;
if(file_id == start_file && skip_lines < start_line) skip_lines = start_line;
var Parsed = /PARSED=([0-9]+)/g;
var matches = document.body.innerHTML.match(Parsed);
if(matches != null)
{
matches = matches[matches.length - 1].match(/PARSED=([0-9]+)/);
skip_lines = matches[1];
} else if(start_line != 0)
{
skip_lines = start_line;
}
else skip_lines = -1;
var force = false;
if(last_rows == skip_lines && grace)
{
grace = false;
}
else if(last_rows == skip_lines && !grace)
{
force = true;
}
last_rows = skip_lines;
var Ausdruck = /(Maximum execution time)/g;
if((Ausdruck.exec(document.body.innerHTML) != null || force) && file_id != null)
{
redir(file_id, skip_lines);
};
var Ausdruck = /FINISHED\=1/;
if(Ausdruck.exec(document.body.innerHTML) != null)
{}
else
{
aktiv = window.setTimeout("checkready()", 15000);
}
}
function msgbox(id)
{
var text_var;
if(id == "LD") text_var = "'.$user->lang['JS_DELETE_IN_PROGRESS'].'";
else if(id == "LI") text_var = "'.$user->lang['JS_IMPORT_IN_PROGRESS'].'";
else if(id == "LG") text_var = "'.$user->lang['JS_DOWNLOAD_IN_PROGRESS'].'";
var box=window.confirm(text_var);
if(box==true)
{
window.location = "import.php?override=" + id;
}
else if(box==false)
{
}
}
function stop () {
window.clearTimeout(aktiv);
}
function redir (target, skip_lines)
{
if(skip_lines != -1)
{
window.location = "import.php?skip_lines=" + skip_lines + "&skip=" + target'.(($timeouts != 0) ? ' + "&timeouts='.$timeouts.'"' : "").';
}
else
{
window.location = "import.php?skip=" + target'.(($timeouts != 0) ? ' + "&timeouts='.$timeouts.'"' : "").';
}
}
';
//no reload in cronjob = not supported
if(!defined('IN_CRONJOB'))
{
echo 'var aktiv = window.setTimeout("checkready()", 15000);';
}
echo '</script><body>';
if($skip === false)
{
//delWorld.lock
if(file_exists($folder . 'delWorld.lock') && $override != 'LD') die($user->lang['DELETE_IN_PROGRESS'].'<input type="hidden" name="lock" value="(LOCK=LD)" /></body></html>');
//importWorld.lock
if(file_exists($folder . 'importWorld.lock') && $override != 'LI') die($user->lang['IMPORT_IN_PROGRESS'].'<input type="hidden" name="lock" value="(LOCK=LI)" /></body></html>');
//getWorld.lock
if(file_exists($folder . 'getWorld.lock') && $override != 'LG') die($user->lang['DOWNLOAD_IN_PROGRESS'].'<input type="hidden" name="lock" value="(LOCK=LG)" /></body></html>');
add_lock($folder, "import");
}
$db->sql_return_on_error(true);
if(!$start_alt)
{
$s_time = microtime(true);
$id = -1;
foreach($files as $file_name => $file_table)
{
++$id;
if(is_numeric($skip) && $id <= $skip)
{
msg('<center>'.sprintf($user->lang['SKIP_FILE'], "$file_name.$fileEx").'</center>', true, false);
continue;
}
$f_time = microtime(true);
$file_name = "$file_name.$fileEx";
msg('<center>'.sprintf($user->lang['IMPORT_FILE'], $file_name), true, false);
if(file_exists($folder . $file_name) && filesize($folder . $file_name))
{
$sql = "TRUNCATE TABLE $file_table";
$db->sql_query($sql);
$error = $db->_sql_error();
$sql = "LOAD DATA LOCAL INFILE '".$folder_mysql.$file_name."' INTO TABLE ".$file_table." FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n'";
if(!($result = $db->sql_query($sql)))
{
$error = $db->_sql_error();
if($error['code'] == 1148)
{
msg('</center><center>'.$user->lang['LOCAL_INFILE_DISABLED'].'</center>', true, false);
$sql = "LOAD DATA INFILE '".$folder_mysql.$file_name."' INTO TABLE ".$file_table." FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n'";
if(!($result = $db->sql_query($sql)))
{
$error = $db->_sql_error();
if($error['code'] != 0)
{
msg('</center><center>'.$user->lang['INFILE_DISABLED'].'</center>', true, false);
$start_alt = true;
break;
}
}
break;
}
elseif($error['code'] != 0)
{
msg("<center><font color=\"red\">" . $error['message'] . "<font color=\"red\"></center>", true, false);
$start_alt = true;
break;
}
}
else
{
msg(sprintf($user->lang['IMPORT_SUCCESS'], round(microtime(true) - $f_time, 2)).'</center>', true, false);
}
} else continue;
}
}
//starting alternative Import
if($start_alt)
{
msg('<center>'.$user->lang['IMPORT_ALTERNATIVE'].'</center>', false, false);
//
//Truncate tables and import files
//
$start_time = microtime(true);
if(!isset($s_time)) $s_time = $start_time;
$id = -1;
foreach($files as $file_name => $file_table)
{
++$id;
if(is_numeric($skip) && $id < $skip)
{
msg('<center>'.sprintf($user->lang['SKIP_FILE'], "$file_name.$fileEx").'</center>', true, false);
continue;
}
$f_time = microtime(true);
if(empty($file_name) || empty($file_table)) continue;
$file_name = "$file_name.$fileEx";
msg("<center><table><tr>", false, false);
msg('<td colspan="10" align="center">'.sprintf($user->lang['IMPORT_FILE'], $file_name).'<input type="hidden" name="file" value="FILEID='.$id.'"></center></td></tr><tr>', true, false);
if(file_exists($folder . $file_name) && ($file_size = filesize($folder . $file_name)) && !$skip_lines)
{
if($debug_mode) msg("Truncating table <i>$file_table</i>.");
$sql = "TRUNCATE TABLE $file_table";
$db->sql_query($sql);
$error = $db->_sql_error();
if($error['code'] != 0) msg("ERROR: ".$error['message']);
} else if(!$skip_lines) continue;
$handle = fopen($folder.$file_name,"r");
$sql = "";
$i = 0; $j = 0;
$parsed_size = $last_msg = 0;
if($skip_lines)
{
msg('<input type="hidden" name="parsed" value="ALREADYPARSED='.$skip_lines.'" />', true, false);
fseek($handle, $skip_lines);
$skip_lines = false;
}
while(!feof($handle))
{
$line = fgets($handle);
++$j;
if(strlen($line) < 1) continue;
$parsed_size += strlen($line);
$line = str_replace(",", "','", $line);
if($i == 0) $sql = "INSERT DELAYED IGNORE INTO $file_table VALUES ('".$line."')";
else $sql .= ",('".$line."')";
++$i;
//if $multi_inserts entries are parsed => dump them into the DB
if($i >= $multi_inserts)
{
$db->sql_query($sql);
msg('<input type="hidden" name="parsed" value="(PARSED='.ftell($handle).')" />', true, false);
$error = $db->_sql_error();
if($debug_mode && $error['code'] != 0) msg("-> ERROR: ".$error['message']);
$i = 0;
if((microtime(true) - $start_time) > 10)
{
$calc_time = round(10*$parsed_size / $file_size, 0) * 10; //Prozent
if((($calc_time - $last_msg) >= 10))
{
$last_msg = $calc_time;
msg("<td style=\"display:inline;\">".$calc_time."%</td> ", true, false);
}
if((microtime(true) - $start_time) > 28)
{
$max_time = ini_get('max_execution_time');
msg(sprintf($user->lang['TIMELIMIT_REACHED'], $max_time, $max_time).'</center><p>');
set_time_limit($max_time);
if($debug_mode) msg("Timelimit has been increased", true, false);
$start_time = microtime(true);
}
}
}
}
fclose($handle);
//dump remaining elements
if($i > 0)
{
$db->sql_query($sql);
$error = $db->_sql_error();
if($last_msg < 100) msg("<td align=\"center\">100%</td>", false, false);
}
msg("</tr></table></center>", true, false);
if ($error['code'] == 0 || $error['code'] == 1136) msg("<center>".sprintf($user->lang['IMPORT_SUCCESS'], round(microtime(true) - $f_time, 2)).'</center>', true, false);
else msg("<center><b><font color=\"red\">".$error['message']."</font></b></center>", true, false);
$skip_lines = false;
}
}
set_staemme_config('import_date', time());
if($staemme_config['del_after_import'] == 1)
{
include_once("delete.$phpEx");
/*
add_lock($folder, "del");
foreach($files as $file_name => $file_table)
{
$file_name = "$file_name.$fileEx";
if(file_exists($folder . $file_name))
{
if(unlink($folder . $file_name)) msg('<center>'.sprintf($user->lang['DELETE_FILE_SUCCESS'], $folder.$file_name).'</center>');
else msg('<center>'.sprintf($user->lang['DELETE_FILE_ERROR'], $folder.$file_name).'</center>');
} else msg('<center>'.sprintf($user->lang['DELETE_FILE_NOFILE'], $folder.$file_name).'</center>');
}
remove_lock($folder, "del");
*/
}
remove_lock($folder, "import");
msg('<center>'.sprintf($user->lang['IMPORT_FINISHED'], ini_get('max_execution_time') * $timeouts + round(microtime(true) - $s_time, 2)).'<input type="hidden" name="finished" value="FINISHED=1" /></center>', false, false);
msg('</body><html>');
?>