$send_mail = isset($_POST['sendMail']) && $_POST['sendMail'] ? 1 : 0;
 $isOverwrite = isset($_POST['overwrite']) && $_POST['overwrite'] ? true : false;
 $deleteUsersNotInList = isset($_POST['delete_users_not_in_list']) ? true : false;
 $sessions = array();
 $session_counter = 0;
 if ($file_type == 'xml') {
     // XML
     // SimpleXML for PHP5 deals with various encodings, but how many they are, what are version issues, do we need to waste time with configuration options?
     // For avoiding complications we go some sort of "PHP4 way" - we convert the input xml-file into UTF-8 before passing it to the parser.
     // Instead of:
     // $root = @simplexml_load_file($_FILES['import_file']['tmp_name']);
     // we may use the following construct:
     // $root = @simplexml_load_string(api_utf8_encode_xml(file_get_contents($_FILES['import_file']['tmp_name'])));
     // To ease debugging let us use:
     $content = file_get_contents($_FILES['import_file']['tmp_name']);
     $content = api_utf8_encode_xml($content);
     $root = @simplexml_load_string($content);
     unset($content);
     if (is_object($root)) {
         if (count($root->Users->User) > 0) {
             // Creating/updating users from <Sessions> <Users> base node.
             foreach ($root->Users->User as $node_user) {
                 $username = $username_old = trim(api_utf8_decode($node_user->Username));
                 if (UserManager::is_username_available($username)) {
                     $password = api_utf8_decode($node_user->Password);
                     if (empty($password)) {
                         $password = api_generate_password();
                     }
                     switch ($node_user->Status) {
                         case 'student':
                             $status = 5;
/**
 * Read the XML-file
 * @param string $file Path to the XML-file
 * @return array All user information read from the file
 */
function parse_xml_data($file)
{
    global $users;
    $users = array();
    $parser = xml_parser_create('UTF-8');
    xml_set_element_handler($parser, 'element_start', 'element_end');
    xml_set_character_data_handler($parser, 'character_data');
    xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, false);
    xml_parse($parser, api_utf8_encode_xml(file_get_contents($file)));
    xml_parser_free($parser);
    return $users;
}
 /**
  * Parses an imsmanifest.xml file and puts everything into the $manifest array
  * @param	string	Path to the imsmanifest.xml file on the system. If not defined, uses the base path of the course's scorm dir
  * @return	array	Structured array representing the imsmanifest's contents
  */
 public function parse_manifest($file = '')
 {
     if ($this->debug > 0) {
         error_log('In scorm::parse_manifest(' . $file . ')', 0);
     }
     if (empty($file)) {
         // Get the path of the imsmanifest file.
     }
     if (is_file($file) && is_readable($file) && ($xml = @file_get_contents($file))) {
         // Parsing using PHP5 DOMXML methods.
         if ($this->debug > 0) {
             error_log('In scorm::parse_manifest() - Parsing using PHP5 method', 0);
         }
         //$this->manifest_encoding = api_detect_encoding_xml($xml); // This is the usual way for reading the encoding.
         // This method reads the encoding, it tries to be correct even in cases of wrong or missing encoding declarations.
         $this->manifest_encoding = self::detect_manifest_encoding($xml);
         // UTF-8 is supported by DOMDocument class, this is for sure.
         $xml = api_utf8_encode_xml($xml, $this->manifest_encoding);
         $doc = new DOMDocument();
         $res = @$doc->loadXML($xml);
         if ($res === false) {
             if ($this->debug > 0) {
                 error_log('New LP - In scorm::parse_manifest() - Exception thrown when loading ' . $file . ' in DOMDocument', 0);
             }
             // Throw exception?
             return null;
         }
         if ($this->debug > 1) {
             error_log('New LP - Called  (encoding:' . $doc->xmlEncoding . ' - saved: ' . $this->manifest_encoding . ')', 0);
         }
         $root = $doc->documentElement;
         if ($root->hasAttributes()) {
             $attributes = $root->attributes;
             if ($attributes->length !== 0) {
                 foreach ($attributes as $attrib) {
                     // <manifest> element attributes
                     $this->manifest[$attrib->name] = $attrib->value;
                 }
             }
         }
         $this->manifest['name'] = $root->tagName;
         if ($root->hasChildNodes()) {
             $children = $root->childNodes;
             if ($children->length !== 0) {
                 foreach ($children as $child) {
                     // <manifest> element children (can be <metadata>, <organizations> or <resources> )
                     if ($child->nodeType == XML_ELEMENT_NODE) {
                         switch ($child->tagName) {
                             case 'metadata':
                                 // Parse items from inside the <metadata> element.
                                 $this->metadata = new scormMetadata('manifest', $child);
                                 break;
                             case 'organizations':
                                 // Contains the course structure - this element appears 1 and only 1 time in a package imsmanifest.
                                 // It contains at least one 'organization' sub-element.
                                 $orgs_attribs = $child->attributes;
                                 foreach ($orgs_attribs as $orgs_attrib) {
                                     // Attributes of the <organizations> element.
                                     if ($orgs_attrib->nodeType == XML_ATTRIBUTE_NODE) {
                                         $this->manifest['organizations'][$orgs_attrib->name] = $orgs_attrib->value;
                                     }
                                 }
                                 $orgs_nodes = $child->childNodes;
                                 $i = 0;
                                 $found_an_org = false;
                                 foreach ($orgs_nodes as $orgnode) {
                                     // <organization> elements - can contain <item>, <metadata> and <title>
                                     // Here we are at the 'organization' level. There might be several organization tags but
                                     // there is generally only one.
                                     // There are generally three children nodes we are looking for inside and organization:
                                     // -title
                                     // -item (may contain other item tags or may appear several times inside organization)
                                     // -metadata (relative to the organization)
                                     $found_an_org = false;
                                     switch ($orgnode->nodeType) {
                                         case XML_TEXT_NODE:
                                             // Ignore here.
                                             break;
                                         case XML_ATTRIBUTE_NODE:
                                             // Just in case there would be interesting attributes inside the organization tag.
                                             // There shouldn't as this is a node-level, not a data level.
                                             //$manifest['organizations'][$i][$orgnode->name] = $orgnode->value;
                                             //$found_an_org = true;
                                             break;
                                         case XML_ELEMENT_NODE:
                                             // <item>, <metadata> or <title> (or attributes)
                                             $organizations_attributes = $orgnode->attributes;
                                             foreach ($organizations_attributes as $orgs_attr) {
                                                 $this->organizations_att[$orgs_attr->name] = $orgs_attr->value;
                                             }
                                             $oOrganization = new scormOrganization('manifest', $orgnode, $this->manifest_encoding);
                                             if ($oOrganization->identifier != '') {
                                                 $name = $oOrganization->get_name();
                                                 if (empty($name)) {
                                                     // If the org title is empty, use zip file name.
                                                     $myname = $this->zipname;
                                                     if ($this->lastzipnameindex != 0) {
                                                         $myname = $myname + $this->lastzipnameindex;
                                                         $this->lastzipnameindex++;
                                                     }
                                                     $oOrganization->set_name($this->zipname);
                                                 }
                                                 $this->organizations[$oOrganization->identifier] = $oOrganization;
                                             }
                                             break;
                                     }
                                 }
                                 break;
                             case 'resources':
                                 if ($child->hasAttributes()) {
                                     $resources_attribs = $child->attributes;
                                     foreach ($resources_attribs as $res_attr) {
                                         if ($res_attr->type == XML_ATTRIBUTE_NODE) {
                                             $this->manifest['resources'][$res_attr->name] = $res_attr->value;
                                         }
                                     }
                                 }
                                 if ($child->hasChildNodes()) {
                                     $resources_nodes = $child->childNodes;
                                     $i = 0;
                                     foreach ($resources_nodes as $res_node) {
                                         $oResource = new scormResource('manifest', $res_node);
                                         if ($oResource->identifier != '') {
                                             $this->resources[$oResource->identifier] = $oResource;
                                             $i++;
                                         }
                                     }
                                 }
                                 // Contains links to physical resources.
                                 break;
                             case 'manifest':
                                 // Only for sub-manifests.
                                 break;
                         }
                     }
                 }
             }
         }
         unset($doc);
         // End parsing using PHP5 DOMXML methods.
     } else {
         if ($this->debug > 1) {
             error_log('New LP - Could not open/read file ' . $file, 0);
         }
         $this->set_error_msg("File {$file} could not be read");
         return null;
     }
     // TODO: Close the DOM handler.
     return $this->manifest;
 }