2 // +----------------------------------------------------------------------+
 
   3 // | Anuko Time Tracker
 
   4 // +----------------------------------------------------------------------+
 
   5 // | Copyright (c) Anuko International Ltd. (https://www.anuko.com)
 
   6 // +----------------------------------------------------------------------+
 
   7 // | LIBERAL FREEWARE LICENSE: This source code document may be used
 
   8 // | by anyone for any purpose, and freely redistributed alone or in
 
   9 // | combination with other software, provided that the license is obeyed.
 
  11 // | There are only two ways to violate the license:
 
  13 // | 1. To redistribute this code in source form, with the copyright
 
  14 // |    notice or license removed or altered. (Distributing in compiled
 
  15 // |    forms without embedded copyright notices is permitted).
 
  17 // | 2. To redistribute modified versions of this code in *any* form
 
  18 // |    that bears insufficient indications that the modifications are
 
  19 // |    not the work of the original author(s).
 
  21 // | This license applies to this document only, not any other software
 
  22 // | that it may be combined with.
 
  24 // +----------------------------------------------------------------------+
 
  26 // | https://www.anuko.com/time_tracker/credits.htm
 
  27 // +----------------------------------------------------------------------+
 
  29 import('ttUserHelper');
 
  30 import('ttRoleHelper');
 
  31 import('ttTaskHelper');
 
  32 import('ttProjectHelper');
 
  33 import('ttClientHelper');
 
  34 import('ttInvoiceHelper');
 
  35 import('ttCustomFieldHelper');
 
  36 import('ttExpenseHelper');
 
  37 import('ttFavReportHelper');
 
  39 // ttOrgImportHelper class is used to import organization data from an XML file
 
  40 // prepared by ttOrgExportHelper and consisting of nested groups with their info.
 
  41 class ttOrgImportHelper {
 
  42   var $errors               = null; // Errors go here. Set in constructor by reference.
 
  43   var $schema_version       = null; // Database schema version from XML file we import from.
 
  44   var $conflicting_logins   = null; // A comma-separated list of logins we cannot import.
 
  45   var $canImport      = true;    // False if we cannot import data due to a conflict such as login collision.
 
  46   var $firstPass      = true;    // True during first pass through the file.
 
  47   var $org_id         = null;    // Organization id (same as top group_id).
 
  48   var $current_group_id        = null; // Current group id during parsing.
 
  49   var $current_parent_group_id = null; // Current parent group id during parsing.
 
  50   var $top_role_id    = 0;       // Top role id.
 
  52   // Entity maps for current group. They map XML ids with database ids.
 
  53   var $currentGroupRoleMap    = array();
 
  54   var $currentGroupTaskMap    = array();
 
  55   var $currentGroupProjectMap = array();
 
  56   var $currentGroupClientMap  = array();
 
  57   var $currentGroupUserMap    = array();
 
  58   var $currentGroupInvoiceMap = array();
 
  59   var $currentGroupLogMap     = array();
 
  60   var $currentGroupCustomFieldMap = array();
 
  61   var $currentGroupCustomFieldOptionMap = array();
 
  64   function __construct(&$errors) {
 
  65     $this->errors = &$errors;
 
  66     $this->top_role_id = ttRoleHelper::getRoleByRank(512, 0);
 
  69   // startElement - callback handler for opening tags in XML.
 
  70   function startElement($parser, $name, $attrs) {
 
  73     // First pass through the file determines if we can import data.
 
  74     // We require 2 things:
 
  75     //   1) Database schema version must be set. This ensures we have a compatible file.
 
  76     //   2) No login coillisions are allowed.
 
  77     if ($this->firstPass) {
 
  78       if ($name == 'ORG' && $this->canImport) {
 
  79          if ($attrs['SCHEMA'] == null) {
 
  80            // We need (database) schema attribute to be available for import to work.
 
  81            // Old Time Tracker export files don't have this.
 
  82            // Current import code does not work with old format because we had to
 
  83            // restructure data in export files for subgroup support.
 
  84            $this->canImport = false;
 
  85            $this->errors->add($i18n->get('error.format'));
 
  90       // In first pass we check user logins for potential collisions with existing.
 
  91       if ($name == 'USER' && $this->canImport) {
 
  92         $login = $attrs['LOGIN'];
 
  93         if ('' != $attrs['STATUS'] && ttUserHelper::getUserByLogin($login)) {
 
  94           // We have a login collision. Append colliding login to a list of things we cannot import.
 
  95           $this->conflicting_logins .= ($this->conflicting_logins ? ", $login" : $login);
 
  96           // The above is printed in error message with all found colliding logins.
 
 101     // Second pass processing. We import data here, one tag at a time.
 
 102     if (!$this->firstPass && $this->canImport && $this->errors->no()) {
 
 103       $mdb2 = getConnection();
 
 105       // We are in second pass and can import data.
 
 106       if ($name == 'GROUP') {
 
 107         // Create a new group.
 
 108         $this->current_group_id = $this->createGroup(array(
 
 109           'parent_id' => $this->current_parent_group_id,
 
 110           'org_id' => $this->org_id,
 
 111           'name' => $attrs['NAME'],
 
 112           'currency' => $attrs['CURRENCY'],
 
 113           'decimal_mark' => $attrs['DECIMAL_MARK'],
 
 114           'lang' => $attrs['LANG'],
 
 115           'date_format' => $attrs['DATE_FORMAT'],
 
 116           'time_format' => $attrs['TIME_FORMAT'],
 
 117           'week_start' => $attrs['WEEK_START'],
 
 118           'tracking_mode' => $attrs['TRACKING_MODE'],
 
 119           'project_required' => $attrs['PROJECT_REQUIRED'],
 
 120           'task_required' => $attrs['TASK_REQUIRED'],
 
 121           'record_type' => $attrs['RECORD_TYPE'],
 
 122           'bcc_email' => $attrs['BCC_EMAIL'],
 
 123           'allow_ip' => $attrs['ALLOW_IP'],
 
 124           'password_complexity' => $attrs['PASSWORD_COMPLEXITY'],
 
 125           'plugins' => $attrs['PLUGINS'],
 
 126           'lock_spec' => $attrs['LOCK_SPEC'],
 
 127           'workday_minutes' => $attrs['WORKDAY_MINUTES'],
 
 128           'custom_logo' => $attrs['CUSTOM_LOGO'],
 
 129           'config' => $attrs['CONFIG']));
 
 131         // Special handling for top group.
 
 132         if (!$this->org_id && $this->current_group_id) {
 
 133           $this->org_id = $this->current_group_id;
 
 134           $sql = "update tt_groups set org_id = $this->current_group_id where org_id is NULL and id = $this->current_group_id";
 
 135           $affected = $mdb2->exec($sql);
 
 137         // Set parent group to create subgroups with this group as parent at next entry here.
 
 138         $this->current_parent_group_id = $this->current_group_id;
 
 142       if ($name == 'ROLES') {
 
 143         // If we get here, we have to recycle $currentGroupRoleMap.
 
 144         unset($this->currentGroupRoleMap);
 
 145         $this->currentGroupRoleMap = array();
 
 146         // Role map is reconstructed after processing <role> elements in XML. See below.
 
 150       if ($name == 'ROLE') {
 
 151         // We get here when processing <role> tags for the current group.
 
 152         $role_id = ttRoleHelper::insert(array(
 
 153           'group_id' => $this->current_group_id,
 
 154           'org_id' => $this->org_id,
 
 155           'name' => $attrs['NAME'],
 
 156           'description' => $attrs['DESCRIPTION'],
 
 157           'rank' => $attrs['RANK'],
 
 158           'rights' => $attrs['RIGHTS'],
 
 159           'status' => $attrs['STATUS']));
 
 162           $this->currentGroupRoleMap[$attrs['ID']] = $role_id;
 
 163         } else $this->errors->add($i18n->get('error.db'));
 
 167       if ($name == 'TASKS') {
 
 168         // If we get here, we have to recycle $currentGroupTaskMap.
 
 169         unset($this->currentGroupTaskMap);
 
 170         $this->currentGroupTaskMap = array();
 
 171         // Task map is reconstructed after processing <task> elements in XML. See below.
 
 175       if ($name == 'TASK') {
 
 176         // We get here when processing <task> tags for the current group.
 
 177         $task_id = ttTaskHelper::insert(array(
 
 178           'group_id' => $this->current_group_id,
 
 179           'org_id' => $this->org_id,
 
 180           'name' => $attrs['NAME'],
 
 181           'description' => $attrs['DESCRIPTION'],
 
 182           'status' => $attrs['STATUS']));
 
 185           $this->currentGroupTaskMap[$attrs['ID']] = $task_id;
 
 186         } else $this->errors->add($i18n->get('error.db'));
 
 190       if ($name == 'PROJECTS') {
 
 191         // If we get here, we have to recycle $currentGroupProjectMap.
 
 192         unset($this->currentGroupProjectMap);
 
 193         $this->currentGroupProjectMap = array();
 
 194         // Project map is reconstructed after processing <project> elements in XML. See below.
 
 198       if ($name == 'PROJECT') {
 
 199         // We get here when processing <project> tags for the current group.
 
 201         // Prepare a list of task ids.
 
 202         $tasks = explode(',', $attrs['TASKS']);
 
 203         foreach ($tasks as $id)
 
 204           $mapped_tasks[] = $this->currentGroupTaskMap[$id];
 
 206         $project_id = ttProjectHelper::insert(array(
 
 207           'group_id' => $this->current_group_id,
 
 208           'org_id' => $this->org_id,
 
 209           'name' => $attrs['NAME'],
 
 210           'description' => $attrs['DESCRIPTION'],
 
 211           'tasks' => $mapped_tasks,
 
 212           'status' => $attrs['STATUS']));
 
 215           $this->currentGroupProjectMap[$attrs['ID']] = $project_id;
 
 216         } else $this->errors->add($i18n->get('error.db'));
 
 220       if ($name == 'CLIENTS') {
 
 221         // If we get here, we have to recycle $currentGroupClientMap.
 
 222         unset($this->currentGroupClientMap);
 
 223         $this->currentGroupClientMap = array();
 
 224         // Client map is reconstructed after processing <client> elements in XML. See below.
 
 228       if ($name == 'CLIENT') {
 
 229         // We get here when processing <client> tags for the current group.
 
 231         // Prepare a list of project ids.
 
 232         $projects = explode(',', $attrs['PROJECTS']);
 
 233         foreach ($projects as $id)
 
 234           $mapped_projects[] = $this->currentGroupProjectMap[$id];
 
 236         $client_id = ttClientHelper::insert(array(
 
 237           'group_id' => $this->current_group_id,
 
 238           'org_id' => $this->org_id,
 
 239           'name' => $attrs['NAME'],
 
 240           'address' => $attrs['ADDRESS'],
 
 241           'tax' => $attrs['TAX'],
 
 242           'projects' => $mapped_projects,
 
 243           'status' => $attrs['STATUS']));
 
 246           $this->currentGroupClientMap[$attrs['ID']] = $client_id;
 
 247         } else $this->errors->add($i18n->get('error.db'));
 
 251       if ($name == 'USERS') {
 
 252         // If we get here, we have to recycle $currentGroupUserMap.
 
 253         unset($this->currentGroupUserMap);
 
 254         $this->currentGroupUserMap = array();
 
 255         // User map is reconstructed after processing <user> elements in XML. See below.
 
 259       if ($name == 'USER') {
 
 260         // We get here when processing <user> tags for the current group.
 
 262         $role_id = $attrs['ROLE_ID'] === '0' ? $this->top_role_id :  $this->currentGroupRoleMap[$attrs['ROLE_ID']]; // 0 (not null) means top manager role.
 
 264         $user_id = ttUserHelper::insert(array(
 
 265           'group_id' => $this->current_group_id,
 
 266           'org_id' => $this->org_id,
 
 267           'role_id' => $role_id,
 
 268           'client_id' => $this->currentGroupClientMap[$attrs['CLIENT_ID']],
 
 269           'name' => $attrs['NAME'],
 
 270           'login' => $attrs['LOGIN'],
 
 271           'password' => $attrs['PASSWORD'],
 
 272           'rate' => $attrs['RATE'],
 
 273           'email' => $attrs['EMAIL'],
 
 274           'status' => $attrs['STATUS']), false);
 
 277           $this->currentGroupUserMap[$attrs['ID']] = $user_id;
 
 278         } else $this->errors->add($i18n->get('error.db'));
 
 282       if ($name == 'USER_PROJECT_BIND') {
 
 283         if (!ttUserHelper::insertBind(array(
 
 284           'user_id' => $this->currentGroupUserMap[$attrs['USER_ID']],
 
 285           'project_id' => $this->currentGroupProjectMap[$attrs['PROJECT_ID']],
 
 286           'group_id' => $this->current_group_id,
 
 287           'org_id' => $this->org_id,
 
 288           'rate' => $attrs['RATE'],
 
 289           'status' => $attrs['STATUS']))) {
 
 290           $this->errors->add($i18n->get('error.db'));
 
 295       if ($name == 'INVOICES') {
 
 296         // If we get here, we have to recycle $currentGroupInvoiceMap.
 
 297         unset($this->currentGroupInvoiceMap);
 
 298         $this->currentGroupInvoiceMap = array();
 
 299         // Invoice map is reconstructed after processing <invoice> elements in XML. See below.
 
 303       if ($name == 'INVOICE') {
 
 304         // We get here when processing <invoice> tags for the current group.
 
 305         $invoice_id = ttInvoiceHelper::insert(array(
 
 306           'group_id' => $this->current_group_id,
 
 307           'org_id' => $this->org_id,
 
 308           'name' => $attrs['NAME'],
 
 309           'date' => $attrs['DATE'],
 
 310           'client_id' => $this->currentGroupClientMap[$attrs['CLIENT_ID']],
 
 311           'status' => $attrs['STATUS']));
 
 314           $this->currentGroupInvoiceMap[$attrs['ID']] = $invoice_id;
 
 315         } else $this->errors->add($i18n->get('error.db'));
 
 319       if ($name == 'LOG') {
 
 320         // If we get here, we have to recycle $currentGroupLogMap.
 
 321         unset($this->currentGroupLogMap);
 
 322         $this->currentGroupLogMap = array();
 
 323         // Log map is reconstructed after processing <log_item> elements in XML. See below.
 
 327       if ($name == 'LOG_ITEM') {
 
 328         // We get here when processing <log_item> tags for the current group.
 
 329         $log_item_id = ttTimeHelper::insert(array(
 
 330           'user_id' => $this->currentGroupUserMap[$attrs['USER_ID']],
 
 331           'group_id' => $this->current_group_id,
 
 332           'org_id' => $this->org_id,
 
 333           'date' => $attrs['DATE'],
 
 334           'start' => $attrs['START'],
 
 335           'finish' => $attrs['FINISH'],
 
 336           'duration' => $attrs['DURATION'],
 
 337           'client' => $this->currentGroupClientMap[$attrs['CLIENT_ID']],
 
 338           'project' => $this->currentGroupProjectMap[$attrs['PROJECT_ID']],
 
 339           'task' => $this->currentGroupTaskMap[$attrs['TASK_ID']],
 
 340           'invoice' => $this->currentGroupInvoiceMap[$attrs['INVOICE_ID']],
 
 341           'note' => (isset($attrs['COMMENT']) ? $attrs['COMMENT'] : ''),
 
 342           'billable' => $attrs['BILLABLE'],
 
 343           'paid' => $attrs['PAID'],
 
 344           'status' => $attrs['STATUS']));
 
 347           $this->currentGroupLogMap[$attrs['ID']] = $log_item_id;
 
 348         } else $this->errors->add($i18n->get('error.db'));
 
 352       if ($name == 'CUSTOM_FIELDS') {
 
 353         // If we get here, we have to recycle $currentGroupCustomFieldMap.
 
 354         unset($this->currentGroupCustomFieldMap);
 
 355         $this->currentGroupCustomFieldMap = array();
 
 356         // Custom field map is reconstructed after processing <custom_field> elements in XML. See below.
 
 360       if ($name == 'CUSTOM_FIELD') {
 
 361         // We get here when processing <custom_field> tags for the current group.
 
 362         $custom_field_id = ttCustomFieldHelper::insertField(array(
 
 363           'group_id' => $this->current_group_id,
 
 364           // 'org_id' => $this->org_id, TODO: add this when org_id field is added to the table.
 
 365           'type' => $attrs['TYPE'],
 
 366           'label' => $attrs['LABEL'],
 
 367           'required' => $attrs['REQUIRED'],
 
 368           'status' => $attrs['STATUS']));
 
 369         if ($custom_field_id) {
 
 371           $this->currentGroupCustomFieldMap[$attrs['ID']] = $custom_field_id;
 
 372         } else $this->errors->add($i18n->get('error.db'));
 
 376       if ($name == 'CUSTOM_FIELD_OPTIONS') {
 
 377         // If we get here, we have to recycle $currentGroupCustomFieldOptionMap.
 
 378         unset($this->currentGroupCustomFieldOptionMap);
 
 379         $this->currentGroupCustomFieldOptionMap = array();
 
 380         // Custom field option map is reconstructed after processing <custom_field_option> elements in XML. See below.
 
 384       if ($name == 'CUSTOM_FIELD_OPTION') {
 
 385         // We get here when processing <custom_field_option> tags for the current group.
 
 386         $custom_field_option_id = ttCustomFieldHelper::insertOption(array(
 
 387           // 'group_id' => $this->current_group_id, TODO: add this when group_id field is added to the table.
 
 388           // 'org_id' => $this->org_id, TODO: add this when org_id field is added to the table.
 
 389           'field_id' => $this->currentGroupCustomFieldMap[$attrs['FIELD_ID']],
 
 390           'value' => $attrs['VALUE']));
 
 391         if ($custom_field_option_id) {
 
 393           $this->currentGroupCustomFieldOptionMap[$attrs['ID']] = $custom_field_option_id;
 
 394         } else $this->errors->add($i18n->get('error.db'));
 
 398       if ($name == 'CUSTOM_FIELD_LOG_ENTRY') {
 
 399         // We get here when processing <custom_field_log_entry> tags for the current group.
 
 400         if (!ttCustomFieldHelper::insertLogEntry(array(
 
 401           // 'group_id' => $this->current_group_id, TODO: add this when group_id field is added to the table.
 
 402           // 'org_id' => $this->org_id, TODO: add this when org_id field is added to the table.
 
 403           'log_id' => $this->currentGroupLogMap[$attrs['LOG_ID']],
 
 404           'field_id' => $this->currentGroupCustomFieldMap[$attrs['FIELD_ID']],
 
 405           'option_id' => $this->currentGroupCustomFieldOptionMap[$attrs['OPTION_ID']],
 
 406           'value' => $attrs['VALUE'],
 
 407           'status' => $attrs['STATUS']))) {
 
 408           $this->errors->add($i18n->get('error.db'));
 
 413       if ($name == 'EXPENSE_ITEM') {
 
 414         // We get here when processing <expense_item> tags for the current group.
 
 415         $expense_item_id = ttExpenseHelper::insert(array(
 
 416           'date' => $attrs['DATE'],
 
 417           'user_id' => $this->currentGroupUserMap[$attrs['USER_ID']],
 
 418           'group_id' => $this->current_group_id,
 
 419           // 'org_id' => $this->org_id, TODO: add this when org_id field is added to the table.
 
 420           'client_id' => $this->currentGroupClientMap[$attrs['CLIENT_ID']],
 
 421           'project_id' => $this->currentGroupProjectMap[$attrs['PROJECT_ID']],
 
 422           'name' => $attrs['NAME'],
 
 423           'cost' => $attrs['COST'],
 
 424           'invoice_id' => $this->currentGroupInvoiceMap[$attrs['INVOICE_ID']],
 
 425           'paid' => $attrs['PAID'],
 
 426           'status' => $attrs['STATUS']));
 
 427         if (!$expense_item_id) $this->errors->add($i18n->get('error.db'));
 
 431       if ($name == 'MONTHLY_QUOTA') {
 
 432         if (!$this->insertMonthlyQuota($this->current_group_id,
 
 433           // 'org_id' => $this->org_id, TODO: add this when org_id field is added to the table.
 
 436           $attrs['MINUTES'])) {
 
 437           $this->errors->add($i18n->get('error.db'));
 
 442       if ($name == 'FAV_REPORT') {
 
 444         if (strlen($attrs['USERS']) > 0) {
 
 445           $arr = explode(',', $attrs['USERS']);
 
 447             $user_list .= (strlen($user_list) == 0 ? '' : ',').$this->currentGroupUserMap[$v];
 
 449         $fav_report_id = ttFavReportHelper::insertReport(array(
 
 450           'name' => $attrs['NAME'],
 
 451           'user_id' => $this->currentGroupUserMap[$attrs['USER_ID']],
 
 452           'client' => $this->currentGroupClientMap[$attrs['CLIENT_ID']],
 
 453           'option' => $this->currentGroupCustomFieldOptionMap[$attrs['CF_1_OPTION_ID']],
 
 454           'project' => $this->currentGroupProjectMap[$attrs['PROJECT_ID']],
 
 455           'task' => $this->currentGroupTaskMap[$attrs['TASK_ID']],
 
 456           'billable' => $attrs['BILLABLE'],
 
 457           'users' => $user_list,
 
 458           'period' => $attrs['PERIOD'],
 
 459           'from' => $attrs['PERIOD_START'],
 
 460           'to' => $attrs['PERIOD_END'],
 
 461           'chclient' => (int) $attrs['SHOW_CLIENT'],
 
 462           'chinvoice' => (int) $attrs['SHOW_INVOICE'],
 
 463           'chpaid' => (int) $attrs['SHOW_PAID'],
 
 464           'chip' => (int) $attrs['SHOW_IP'],
 
 465           'chproject' => (int) $attrs['SHOW_PROJECT'],
 
 466           'chstart' => (int) $attrs['SHOW_START'],
 
 467           'chduration' => (int) $attrs['SHOW_DURATION'],
 
 468           'chcost' => (int) $attrs['SHOW_COST'],
 
 469           'chtask' => (int) $attrs['SHOW_TASK'],
 
 470           'chfinish' => (int) $attrs['SHOW_END'],
 
 471           'chnote' => (int) $attrs['SHOW_NOTE'],
 
 472           'chcf_1' => (int) $attrs['SHOW_CUSTOM_FIELD_1'],
 
 473           'chunits' => (int) $attrs['SHOW_WORK_UNITS'],
 
 474           'group_by1' => $attrs['GROUP_BY1'],
 
 475           'group_by2' => $attrs['GROUP_BY2'],
 
 476           'group_by3' => $attrs['GROUP_BY3'],
 
 477           'chtotalsonly' => (int) $attrs['SHOW_TOTALS_ONLY']));
 
 478          if (!$fav_report_id) $this->errors->add($i18n->get('error.db'));
 
 484   // importXml - uncompresses the file, reads and parses its content. During parsing,
 
 485   // startElement, endElement, and dataElement functions are called as many times as necessary.
 
 486   // Actual import occurs in the endElement handler.
 
 487   function importXml() {
 
 490     // Do we have a compressed file?
 
 492     $file_ext = substr($_FILES['xmlfile']['name'], strrpos($_FILES['xmlfile']['name'], '.') + 1);
 
 493     if (in_array($file_ext, array('bz','tbz','bz2','tbz2'))) {
 
 497     // Create a temporary file.
 
 498     $dirName = dirname(TEMPLATE_DIR . '_c/.');
 
 499     $filename = tempnam($dirName, 'import_');
 
 501     // If the file is compressed - uncompress it.
 
 503       if (!$this->uncompress($_FILES['xmlfile']['tmp_name'], $filename)) {
 
 504         $this->errors->add($i18n->get('error.sys'));
 
 507       unlink($_FILES['xmlfile']['tmp_name']);
 
 509       if (!move_uploaded_file($_FILES['xmlfile']['tmp_name'], $filename)) {
 
 510         $this->errors->add($i18n->get('error.upload'));
 
 515     // Initialize XML parser.
 
 516     $parser = xml_parser_create();
 
 517     xml_set_object($parser, $this);
 
 518     xml_set_element_handler($parser, 'startElement', false);
 
 520     // We need to parse the file 2 times:
 
 521     //   1) First pass: determine if import is possible.
 
 522     //   2) Second pass: import data, one tag at a time.
 
 524     // Read and parse the content of the file. During parsing, startElement is called back for each tag.
 
 525     $file = fopen($filename, 'r');
 
 526     while ($data = fread($file, 4096)) {
 
 527       if (!xml_parse($parser, $data, feof($file))) {
 
 528         $this->errors->add(sprintf($i18n->get('error.xml'),
 
 529           xml_get_current_line_number($parser),
 
 530           xml_error_string(xml_get_error_code($parser))));
 
 533     if ($this->conflicting_logins) {
 
 534       $this->canImport = false;
 
 535       $this->errors->add($i18n->get('error.user_exists'));
 
 536       $this->errors->add(sprintf($i18n->get('error.cannot_import'), $this->conflicting_logins));
 
 539     $this->firstPass = false; // We are done with 1st pass.
 
 540     xml_parser_free($parser);
 
 541     if ($file) fclose($file);
 
 542     if (!$this->canImport) {
 
 546     if ($this->errors->yes()) return; // Exit if we have errors.
 
 548     // Now we can do a second pass, where real work is done.
 
 549     $parser = xml_parser_create();
 
 550     xml_set_object($parser, $this);
 
 551     xml_set_element_handler($parser, 'startElement', false);
 
 553     // Read and parse the content of the file. During parsing, startElement is called back for each tag.
 
 554     $file = fopen($filename, 'r');
 
 555     while ($data = fread($file, 4096)) {
 
 556       if (!xml_parse($parser, $data, feof($file))) {
 
 557         $this->errors->add(sprintf($i18n->get('error.xml'),
 
 558           xml_get_current_line_number($parser),
 
 559           xml_error_string(xml_get_error_code($parser))));
 
 562     xml_parser_free($parser);
 
 563     if ($file) fclose($file);
 
 567   // uncompress - uncompresses the content of the $in file into the $out file.
 
 568   function uncompress($in, $out) {
 
 569     // Do we have the uncompress function?
 
 570     if (!function_exists('bzopen'))
 
 573     // Initial checks of file names and permissions.
 
 574     if (!file_exists($in) || !is_readable ($in))
 
 576     if ((!file_exists($out) && !is_writable(dirname($out))) || (file_exists($out) && !is_writable($out)))
 
 579     if (!$out_file = fopen($out, 'wb'))
 
 581     if (!$in_file = bzopen ($in, 'r'))
 
 584     while (!feof($in_file)) {
 
 585       $buffer = bzread($in_file, 4096);
 
 586       fwrite($out_file, $buffer, 4096);
 
 593   // createGroup function creates a new group.
 
 594   private function createGroup($fields) {
 
 597     $mdb2 = getConnection();
 
 599     $columns = '(parent_id, org_id, name, currency, decimal_mark, lang, date_format, time_format'.
 
 600       ', week_start, tracking_mode, project_required, task_required, record_type, bcc_email'.
 
 601       ', allow_ip, password_complexity, plugins, lock_spec'.
 
 602       ', workday_minutes, config, created, created_ip, created_by)';
 
 604     $values = ' values (';
 
 605     $values .= $mdb2->quote($fields['parent_id']);
 
 606     $values .= ', '.$mdb2->quote($fields['org_id']);
 
 607     $values .= ', '.$mdb2->quote(trim($fields['name']));
 
 608     $values .= ', '.$mdb2->quote(trim($fields['currency']));
 
 609     $values .= ', '.$mdb2->quote($fields['decimal_mark']);
 
 610     $values .= ', '.$mdb2->quote($fields['lang']);
 
 611     $values .= ', '.$mdb2->quote($fields['date_format']);
 
 612     $values .= ', '.$mdb2->quote($fields['time_format']);
 
 613     $values .= ', '.(int)$fields['week_start'];
 
 614     $values .= ', '.(int)$fields['tracking_mode'];
 
 615     $values .= ', '.(int)$fields['project_required'];
 
 616     $values .= ', '.(int)$fields['task_required'];
 
 617     $values .= ', '.(int)$fields['record_type'];
 
 618     $values .= ', '.$mdb2->quote($fields['bcc_email']);
 
 619     $values .= ', '.$mdb2->quote($fields['allow_ip']);
 
 620     $values .= ', '.$mdb2->quote($fields['password_complexity']);
 
 621     $values .= ', '.$mdb2->quote($fields['plugins']);
 
 622     $values .= ', '.$mdb2->quote($fields['lock_spec']);
 
 623     $values .= ', '.(int)$fields['workday_minutes'];
 
 624     $values .= ', '.$mdb2->quote($fields['config']);
 
 625     $values .= ', now(), '.$mdb2->quote($_SERVER['REMOTE_ADDR']).', '.$mdb2->quote($user->id);
 
 628     $sql = 'insert into tt_groups '.$columns.$values;
 
 629     $affected = $mdb2->exec($sql);
 
 630     if (is_a($affected, 'PEAR_Error')) {
 
 631       $this->errors->add($i18n->get('error.db'));
 
 635     $group_id = $mdb2->lastInsertID('tt_groups', 'id');
 
 639   // insertMonthlyQuota - a helper function to insert a monthly quota.
 
 640   private function insertMonthlyQuota($group_id, $year, $month, $minutes) {
 
 641     $mdb2 = getConnection();
 
 642     $sql = "INSERT INTO tt_monthly_quotas (group_id, year, month, minutes) values ($group_id, $year, $month, $minutes)";
 
 643     $affected = $mdb2->exec($sql);
 
 644     return (!is_a($affected, 'PEAR_Error'));