12 $this->_properties=array();
17 public function init($process) {
18 $this->_section=$process;
22 if ($status==
'end') $status=
'init';
23 if ($status==
'init') {
36 private function make_fildset($line,$remap=0) {
39 $fieldset_clean=array();
40 $fieldset = explode($sep, $line);
41 foreach ($fieldset as $no => $fld) {
42 if (strpos($fld,
'(')) $fld=substr($fld,0,strpos($fld,
'(',0));
43 if (strpos($fld,
'/')) $fld=substr($fld,0,strpos($fld,
'/',0));
44 if (strpos($fld,
' ')) $fld=substr($fld,0,strpos($fld,
' ',0));
45 if (is_array($remap)) {
46 foreach ($remap as $old => $new) {
47 If ($fld==$old) $fld=$new;
50 if ($fld) $fieldset_clean[]=$fld;
58 private function get_fildset() {
64 private function sql_literal($value, $oDB,
$server) {
65 if ($value ===
null || strtoupper((
string)$value) ===
'NULL') {
69 if (dbx()->is_int_value($value)) {
70 return (
string)(int)$value;
74 return "'" . $oDB->escape((
string)$value,
$server) .
"'";
77 return "'" . str_replace(
"'",
"''", (
string)$value) .
"'";
80 private function make_where($where,$record,$oDB=
null,$dd=
'') {
82 $oDB = dbx()->get_system_obj(
'dbxDB');
84 $server = ($oDB && $dd) ? $oDB->get_dd_server($dd) :
'default';
86 foreach ($record as $name => $value) {
88 if (strpos((
string)$where,
"'" . $xkey .
"'") !==
false) {
89 $where=str_replace(
"'" . $xkey .
"'", $this->sql_literal($value, $oDB,
$server), $where);
91 if (strpos((
string)$where,
'"' . $xkey .
'"') !==
false) {
92 $where=str_replace(
'"' . $xkey .
'"', $this->sql_literal($value, $oDB,
$server), $where);
94 $where=str_replace($xkey, $this->sql_literal($value, $oDB,
$server), $where);
95 if (!(strpos($where,
'}')))
break;
101 private function encodeFLEX($str) {
102 $str=str_replace(
"„",
"ä",$str);
103 $str=str_replace(
"",
"ü",$str);
104 $str=str_replace(
"”",
"ö",$str);
105 $str=str_replace(
"á",
"ß",$str);
106 $str=str_replace(
"¯",
"Ä",$str);
107 $str=str_replace(
"�",
"Ü",$str);
108 $str=str_replace(
'³',
'ü',$str);
109 $str=str_replace(
'õ',
'ö',$str);
114 private function encodeToUtf8($string) {
118 $string=mb_convert_encoding($string,
"UTF-8", mb_detect_encoding($string,
"Windows-1252, ISO-8859-15, ISO-8859-1, ISO-8859-2, CP850, ISO-8859-15, UTF-8",
true));
122 $string = $this->encodeFLEX($string);
127 public function import() {
135 $errorFile= $file.
'.err';
136 if (!file_exists($file))
return 'end';
138 $inf =
new SplFileInfo($file);
139 $ext = ($inf->getExtension());
140 $max = ini_get(
'max_execution_time');
144 $max_quick=
dbx()->get_config(
'dbxAdmin',
'max_quick');
152 $maxLines = $this->
get_property(
'run_lines',$max_quick);
163 $deadline = time()+$maxRuntime;
164 $filesize = filesize($file);
176 $oDB =
dbx()->get_system_obj(
'dbxDB');
180 dbx()->debug(
"#IMPORT File=($file) Pos=($filePos) von ($filesize) Lines=($lineCount) Max=($maxLines) where =($where)");
184 ($fp = fopen($file,
'r')) OR die(
'failed to open file:'.$file);
185 if($filePos) fseek($fp, $filePos);
187 while( $deadline >= time() AND ( $line=fgets($fp, $bytes ) ) ) {
188 $line=str_replace (array(
"\r\n",
"\n",
"\r"),
'',$line);
189 if ($utf8) $line=$this->encodeToUtf8($line);
191 dbx()->debug2($line.
"\n");
193 if(trim($line)==
'') {
continue; }
196 $this->make_fildset($line,$remap);
198 if (!$all) $maxLines--;
199 $fieldset=$this->get_fildset();
201 $data =explode($sep, $line);
206 foreach ($fieldset as $no => $name) {
207 $record[$name]=$data[$no];
218 if ($owner !=
null) {
219 if (!isset($record[
'owner'])) {
220 $record[
'owner']=$owner;
221 if (isset($record[
'userid'])) $record[
'owner']=$record[
'userid'];
225 if (isset($record[
'pass'])) $record[
'pass']=password_hash((
string)$record[
'pass'], PASSWORD_DEFAULT);
229 $rowWhere = $this->make_where($where,$record,$oDB,$dd);
230 $ok=$oDB->save($dd,$record,$rowWhere,0,1,0,0);
232 $ok=$oDB->insert($dd,$record,0,1,0,0);
236 if ( $ok) $queryCount++;
238 dbx()->debug(
"IMPORT CSV ($dd) OK=($ok) Where=($rowWhere ?? $where) Querys=($queryCount) errors=($errors) Record=",$record);
245 if (!$maxLines)
break;
255 $percent =(round($done/$filesize, 2)*100);
282 $status=$this->
import();