From: Nik Okuntseff Date: Thu, 8 Nov 2018 19:01:42 +0000 (+0000) Subject: Resuming work on new import - renamed a class. X-Git-Tag: timetracker_1.19-1~684 X-Git-Url: http://wagnertech.de/git?a=commitdiff_plain;h=f54ca9d66b525ec1cd7076744c8a022e8b4b488d;p=timetracker.git Resuming work on new import - renamed a class. --- diff --git a/WEB-INF/lib/ttImportHelper2.class.php b/WEB-INF/lib/ttImportHelper2.class.php deleted file mode 100644 index 5de6d8c5..00000000 --- a/WEB-INF/lib/ttImportHelper2.class.php +++ /dev/null @@ -1,216 +0,0 @@ -errors = &$errors; - } - - // startElement - callback handler for opening tag of an XML element. - // In this function we assign passed in attributes to currentElement. - function startElement($parser, $name, $attrs) { - if ($name == 'GROUP' - || $name == 'USER') { - $this->currentElement = $attrs; - } - $this->currentTag = $name; - } - - // endElement - callback handler for the closing tag of an XML element. - // When we are here, currentElement is an array of the element attributes (as set in startElement). - // Here we do the actual import of data into the database. - function endElement($parser, $name) { - // During first pass we only check user logins. - if ($this->firstPass) { - if ($name == 'USER' && $this->canImport) { - if ('' != $this->currentElement['STATUS'] && ttUserHelper::getUserByLogin($this->currentElement['LOGIN'])) { - // We have a login collision, cannot import any data. - $this->canImport = false; - } - } - $this->currentTag = ''; - } - - // During second pass we import data. - if (!$this->firstPass && $this->canImport) { - // TODO: write code here. - } - } - - // dataElement - callback handler for text data fragments. It builds up currentElement array with text pieces from XML. - function dataElement($parser, $data) { - if ($this->currentTag == 'NAME' - || $this->currentTag == 'DESCRIPTION' - || $this->currentTag == 'LABEL' - || $this->currentTag == 'VALUE' - || $this->currentTag == 'COMMENT' - || $this->currentTag == 'ADDRESS' - || $this->currentTag == 'ALLOW_IP' - || $this->currentTag == 'PASSWORD_COMPLEXITY') { - if (isset($this->currentElement[$this->currentTag])) - $this->currentElement[$this->currentTag] .= trim($data); - else - $this->currentElement[$this->currentTag] = trim($data); - } - } - - // importXml - uncompresses the file, reads and parses its content. During parsing, - // startElement, endElement, and dataElement functions are called as many times as necessary. - // Actual import occurs in the endElement handler. - function importXml() { - global $i18n; - - // Do we have a compressed file? - $compressed = false; - $file_ext = substr($_FILES['xmlfile']['name'], strrpos($_FILES['xmlfile']['name'], '.') + 1); - if (in_array($file_ext, array('bz','tbz','bz2','tbz2'))) { - $compressed = true; - } - - // Create a temporary file. - $dirName = dirname(TEMPLATE_DIR . '_c/.'); - $filename = tempnam($dirName, 'import_'); - - // If the file is compressed - uncompress it. - if ($compressed) { - if (!$this->uncompress($_FILES['xmlfile']['tmp_name'], $filename)) { - $this->errors->add($i18n->get('error.sys')); - return; - } - unlink($_FILES['xmlfile']['tmp_name']); - } else { - if (!move_uploaded_file($_FILES['xmlfile']['tmp_name'], $filename)) { - $this->errors->add($i18n->get('error.upload')); - return; - } - } - - // Initialize XML parser. - $parser = xml_parser_create(); - xml_set_object($parser, $this); - xml_set_element_handler($parser, 'startElement', 'endElement'); - xml_set_character_data_handler($parser, 'dataElement'); - - // We need to parse the file 2 times: - // 1) First pass: determine if import is possible - there must be no login collisions. - // 2) Second pass: if we can import, then do import in a second pass. - // This is different from earlier approach for single group import, where we could - // do both things in one pass because user info was in the beginning of XML file. - // Now, with subgroups, users can be located anywhere in the file. - - // Read and parse the content of the file. During parsing, startElement, endElement, and dataElement functions are called. - $file = fopen($filename, 'r'); - while ($data = fread($file, 4096)) { - if (!xml_parse($parser, $data, feof($file))) { - $this->errors->add(sprintf("XML error: %s at line %d", - xml_error_string(xml_get_error_code($parser)), - xml_get_current_line_number($parser))); - } - if (!$this->canImport) { - $this->errors->add($i18n->get('error.user_exists')); - break; - } - } - $this->firstPass = false; // We are done with 1st pass. - xml_parser_free($parser); - if ($file) fclose($file); - if (!$this->canImport) { - unlink($filename); - return; - } - - // Now we can do a second pass, where real work is done. - $parser = xml_parser_create(); - xml_set_object($parser, $this); - xml_set_element_handler($parser, 'startElement', 'endElement'); - xml_set_character_data_handler($parser, 'dataElement'); - - // Read and parse the content of the file. During parsing, startElement, endElement, and dataElement functions are called. - $file = fopen($filename, 'r'); - while ($data = fread($file, 4096)) { - if (!xml_parse($parser, $data, feof($file))) { - $this->errors->add(sprintf("XML error: %s at line %d", - xml_error_string(xml_get_error_code($parser)), - xml_get_current_line_number($parser))); - } - } - xml_parser_free($parser); - if ($file) fclose($file); - unlink($filename); - } - - // uncompress - uncompresses the content of the $in file into the $out file. - function uncompress($in, $out) { - // Do we have the uncompress function? - if (!function_exists('bzopen')) - return false; - - // Initial checks of file names and permissions. - if (!file_exists($in) || !is_readable ($in)) - return false; - if ((!file_exists($out) && !is_writable(dirname($out))) || (file_exists($out) && !is_writable($out))) - return false; - - if (!$out_file = fopen($out, 'wb')) - return false; - if (!$in_file = bzopen ($in, 'r')) - return false; - - while (!feof($in_file)) { - $buffer = bzread($in_file, 4096); - fwrite($out_file, $buffer, 4096); - } - bzclose($in_file); - fclose ($out_file); - return true; - } -} diff --git a/WEB-INF/lib/ttOrgImportHelper.class.php b/WEB-INF/lib/ttOrgImportHelper.class.php new file mode 100644 index 00000000..cce763da --- /dev/null +++ b/WEB-INF/lib/ttOrgImportHelper.class.php @@ -0,0 +1,216 @@ +errors = &$errors; + } + + // startElement - callback handler for opening tag of an XML element. + // In this function we assign passed in attributes to currentElement. + function startElement($parser, $name, $attrs) { + if ($name == 'GROUP' + || $name == 'USER') { + $this->currentElement = $attrs; + } + $this->currentTag = $name; + } + + // endElement - callback handler for the closing tag of an XML element. + // When we are here, currentElement is an array of the element attributes (as set in startElement). + // Here we do the actual import of data into the database. + function endElement($parser, $name) { + // During first pass we only check user logins. + if ($this->firstPass) { + if ($name == 'USER' && $this->canImport) { + if ('' != $this->currentElement['STATUS'] && ttUserHelper::getUserByLogin($this->currentElement['LOGIN'])) { + // We have a login collision, cannot import any data. + $this->canImport = false; + } + } + $this->currentTag = ''; + } + + // During second pass we import data. + if (!$this->firstPass && $this->canImport) { + // TODO: write code here. Nothing is imported currently. + } + } + + // dataElement - callback handler for text data fragments. It builds up currentElement array with text pieces from XML. + function dataElement($parser, $data) { + if ($this->currentTag == 'NAME' + || $this->currentTag == 'DESCRIPTION' + || $this->currentTag == 'LABEL' + || $this->currentTag == 'VALUE' + || $this->currentTag == 'COMMENT' + || $this->currentTag == 'ADDRESS' + || $this->currentTag == 'ALLOW_IP' + || $this->currentTag == 'PASSWORD_COMPLEXITY') { + if (isset($this->currentElement[$this->currentTag])) + $this->currentElement[$this->currentTag] .= trim($data); + else + $this->currentElement[$this->currentTag] = trim($data); + } + } + + // importXml - uncompresses the file, reads and parses its content. During parsing, + // startElement, endElement, and dataElement functions are called as many times as necessary. + // Actual import occurs in the endElement handler. + function importXml() { + global $i18n; + + // Do we have a compressed file? + $compressed = false; + $file_ext = substr($_FILES['xmlfile']['name'], strrpos($_FILES['xmlfile']['name'], '.') + 1); + if (in_array($file_ext, array('bz','tbz','bz2','tbz2'))) { + $compressed = true; + } + + // Create a temporary file. + $dirName = dirname(TEMPLATE_DIR . '_c/.'); + $filename = tempnam($dirName, 'import_'); + + // If the file is compressed - uncompress it. + if ($compressed) { + if (!$this->uncompress($_FILES['xmlfile']['tmp_name'], $filename)) { + $this->errors->add($i18n->get('error.sys')); + return; + } + unlink($_FILES['xmlfile']['tmp_name']); + } else { + if (!move_uploaded_file($_FILES['xmlfile']['tmp_name'], $filename)) { + $this->errors->add($i18n->get('error.upload')); + return; + } + } + + // Initialize XML parser. + $parser = xml_parser_create(); + xml_set_object($parser, $this); + xml_set_element_handler($parser, 'startElement', 'endElement'); + xml_set_character_data_handler($parser, 'dataElement'); + + // We need to parse the file 2 times: + // 1) First pass: determine if import is possible - there must be no login collisions. + // 2) Second pass: if we can import, then do import in a second pass. + // This is different from earlier approach for single group import, where we could + // do both things in one pass because user info was in the beginning of XML file. + // Now, with subgroups, users can be located anywhere in the file. + + // Read and parse the content of the file. During parsing, startElement, endElement, and dataElement functions are called. + $file = fopen($filename, 'r'); + while ($data = fread($file, 4096)) { + if (!xml_parse($parser, $data, feof($file))) { + $this->errors->add(sprintf("XML error: %s at line %d", + xml_error_string(xml_get_error_code($parser)), + xml_get_current_line_number($parser))); + } + if (!$this->canImport) { + $this->errors->add($i18n->get('error.user_exists')); + break; + } + } + $this->firstPass = false; // We are done with 1st pass. + xml_parser_free($parser); + if ($file) fclose($file); + if (!$this->canImport) { + unlink($filename); + return; + } + + // Now we can do a second pass, where real work is done. + $parser = xml_parser_create(); + xml_set_object($parser, $this); + xml_set_element_handler($parser, 'startElement', 'endElement'); + xml_set_character_data_handler($parser, 'dataElement'); + + // Read and parse the content of the file. During parsing, startElement, endElement, and dataElement functions are called. + $file = fopen($filename, 'r'); + while ($data = fread($file, 4096)) { + if (!xml_parse($parser, $data, feof($file))) { + $this->errors->add(sprintf("XML error: %s at line %d", + xml_error_string(xml_get_error_code($parser)), + xml_get_current_line_number($parser))); + } + } + xml_parser_free($parser); + if ($file) fclose($file); + unlink($filename); + } + + // uncompress - uncompresses the content of the $in file into the $out file. + function uncompress($in, $out) { + // Do we have the uncompress function? + if (!function_exists('bzopen')) + return false; + + // Initial checks of file names and permissions. + if (!file_exists($in) || !is_readable ($in)) + return false; + if ((!file_exists($out) && !is_writable(dirname($out))) || (file_exists($out) && !is_writable($out))) + return false; + + if (!$out_file = fopen($out, 'wb')) + return false; + if (!$in_file = bzopen ($in, 'r')) + return false; + + while (!feof($in_file)) { + $buffer = bzread($in_file, 4096); + fwrite($out_file, $buffer, 4096); + } + bzclose($in_file); + fclose ($out_file); + return true; + } +} diff --git a/WEB-INF/templates/footer.tpl b/WEB-INF/templates/footer.tpl index 73feb65a..d459f13a 100644 --- a/WEB-INF/templates/footer.tpl +++ b/WEB-INF/templates/footer.tpl @@ -12,7 +12,7 @@
-
 Anuko Time Tracker 1.18.12.4389 | Copyright © Anuko | +  Anuko Time Tracker 1.18.12.4390 | Copyright © Anuko | {$i18n.footer.credits} | {$i18n.footer.license} | {$i18n.footer.improve} diff --git a/import.php b/import.php index 3e375c98..235924ac 100644 --- a/import.php +++ b/import.php @@ -28,7 +28,7 @@ require_once('initialize.php'); import('ttImportHelper'); -import('ttImportHelper2'); +import('ttOrgImportHelper'); import('form.Form'); // Access check. @@ -44,7 +44,7 @@ $form->addInput(array('type'=>'submit','name'=>'btn_submit','value'=>$i18n->get( if ($request->isPost()) { if (defined('SUBGROUP_DEBUG') && isTrue(SUBGROUP_DEBUG)) { - $import = new ttImportHelper2($err); + $import = new ttOrgImportHelper($err); } else { $import = new ttImportHelper($err); }