<commit_before><?php
  @ob_end_flush();

  $file = $_GET['file'];
  if (!file_exists($GLOBALS['tmpdir'].'/'.$file) || !file_exists($GLOBALS['tmpdir'].'/'.$file.'.data')) {
    print s('File not found');
    return;
  }
  
  $importdata = unserialize(file_get_contents($GLOBALS['tmpdir'].'/'.$file.'.data'));
  
  $email_list = file_get_contents($GLOBALS['tmpdir'].'/'.$file);
  include_once dirname(__FILE__)."/../commonlib/lib/userlib.php";

  // Clean up email file
  $email_list = trim($email_list);
  $email_list = str_replace("\r","\n",$email_list);
  $email_list = str_replace("\n\r","\n",$email_list);
  $email_list = str_replace("\n\n","\n",$email_list);

  if (isset($importdata['import_record_delimiter'])) {
    $import_record_delimiter = $importdata['import_record_delimiter'];
  } else {
    $import_record_delimiter = "\n";
  }

  // Change delimiter for new line.
  if(isset($import_record_delimiter) && $import_record_delimiter != "" && $import_record_delimiter != "\n") {
    $email_list = str_replace($import_record_delimiter,"\n",$email_list);
  };

  // Split file/emails into array
  $email_list = explode("\n",$email_list);

  // Parse the lines into records
  $hasinfo = 0;
  foreach ($email_list as $line) {
    $info = '';
    $email = trim($line); ## just take the entire line up to the first space to be the email
    if (strpos($email,' ')) {
      list($email,$info) = explode(' ',$email);
    }

    ## actually looks like the "info" bit will get lost, but
    ## in a way, that doesn't matter
    $user_list[$email] = array (
      'info' => $info,
    );
  }

  $count_email_add = 0;
  $count_email_exist = 0;
  $count_list_add = 0;
  $additional_emails = 0;
  $some = 0;
  $num_lists = sizeof($importdata['importlists']);
  $todo = sizeof($user_list);
  $done = 0;
  $report = '';
  if ($hasinfo) {
    # we need to add an info attribute if it does not exist
    $req = Sql_Query("select id from ".$tables["attribute"]." where name = \"info\"");
    if (!Sql_Affected_Rows()) {
      # it did not exist
      Sql_Query(sprintf('insert into %s (name,type,listorder,default_value,required,tablename)
       values("info","textline",0,"",0,"info")', $tables["attribute"]));
    }
  }

  # which attributes were chosen, apply to all users
  $res = Sql_Query("select * from ".$tables["attribute"]);
  $attributes = array();
  while ($row = Sql_Fetch_Array($res)) {
    $fieldname = "attribute" .$row["id"];
    if (isset($importdata[$fieldname])) {
      if (is_array($importdata[$fieldname])) {
        $attributes[$row["id"]] = join(',',$importdata[$fieldname]);
      } else {
        $attributes[$row["id"]] = $importdata[$fieldname];
      }
    } else {
      $attributes[$row["id"]] = '';
    }
  }

  while (list($email,$data) = each ($user_list)) {
    ## a lot of spreadsheet include those annoying quotes
    $email = str_replace('"', '', $email);      
    set_time_limit(60);
    $done++;
    if ($done % 50 == 0) {
    #  print "$done / $todo<br/>";
      print '<script type="text/javascript">
      var parentJQuery = window.parent.jQuery;
      parentJQuery("#progressbar").updateProgress("'.$done.','.$todo.'");
      </script>';      
      flush();
    }
    if (strlen($email) > 4) {
      $email = addslashes($email);
      // Annoying hack => Much too time consuming. Solution => Set email in users to UNIQUE()
      $result = Sql_query("SELECT id,uniqid FROM ".$tables["user"]." WHERE email = '$email'");
      if (Sql_affected_rows()) {
        // Email exist, remember some values to add them to the lists
        $user = Sql_fetch_array($result);
        $userid = $user["id"];
        $uniqid = $user["uniqid"];
        $old_listmembership = array();
        $history_entry = $GLOBALS['I18N']->get('Import of existing subscriber');
        $old_data = Sql_Fetch_Array_Query(sprintf('select * from %s where id = %d',$tables["user"],$userid));
        $old_data = array_merge($old_data,getUserAttributeValues('',$userid));
        # and membership of lists
        $req = Sql_Query("select * from {$tables["listuser"]} where userid = $userid");
        while ($row = Sql_Fetch_Array($req)) {
          $old_listmembership[$row["listid"]] = listName($row["listid"]);
        }
        $count_email_exist++;
      } else {

        // Email does not exist

        // Create unique number
        $randval = mt_rand();
        $uniqid = getUniqid();
        $old_listmembership = array();
<commit_msg>#17426 - increase counter after output<commit_after><?php
  @ob_end_flush();

  $file = $_GET['file'];
  if (!file_exists($GLOBALS['tmpdir'].'/'.$file) || !file_exists($GLOBALS['tmpdir'].'/'.$file.'.data')) {
    print s('File not found');
    return;
  }
  
  $importdata = unserialize(file_get_contents($GLOBALS['tmpdir'].'/'.$file.'.data'));
  
  $email_list = file_get_contents($GLOBALS['tmpdir'].'/'.$file);
  include_once dirname(__FILE__)."/../commonlib/lib/userlib.php";

  // Clean up email file
  $email_list = trim($email_list);
  $email_list = str_replace("\r","\n",$email_list);
  $email_list = str_replace("\n\r","\n",$email_list);
  $email_list = str_replace("\n\n","\n",$email_list);

  if (isset($importdata['import_record_delimiter'])) {
    $import_record_delimiter = $importdata['import_record_delimiter'];
  } else {
    $import_record_delimiter = "\n";
  }

  // Change delimiter for new line.
  if(isset($import_record_delimiter) && $import_record_delimiter != "" && $import_record_delimiter != "\n") {
    $email_list = str_replace($import_record_delimiter,"\n",$email_list);
  };

  // Split file/emails into array
  $email_list = explode("\n",$email_list);

  // Parse the lines into records
  $hasinfo = 0;
  foreach ($email_list as $line) {
    $info = '';
    $email = trim($line); ## just take the entire line up to the first space to be the email
    if (strpos($email,' ')) {
      list($email,$info) = explode(' ',$email);
    }

    ## actually looks like the "info" bit will get lost, but
    ## in a way, that doesn't matter
    $user_list[$email] = array (
      'info' => $info,
    );
  }

  $count_email_add = 0;
  $count_email_exist = 0;
  $count_list_add = 0;
  $additional_emails = 0;
  $some = 0;
  $num_lists = sizeof($importdata['importlists']);
  $todo = sizeof($user_list);
  $done = 0;
  $report = '';
  if ($hasinfo) {
    # we need to add an info attribute if it does not exist
    $req = Sql_Query("select id from ".$tables["attribute"]." where name = \"info\"");
    if (!Sql_Affected_Rows()) {
      # it did not exist
      Sql_Query(sprintf('insert into %s (name,type,listorder,default_value,required,tablename)
       values("info","textline",0,"",0,"info")', $tables["attribute"]));
    }
  }

  # which attributes were chosen, apply to all users
  $res = Sql_Query("select * from ".$tables["attribute"]);
  $attributes = array();
  while ($row = Sql_Fetch_Array($res)) {
    $fieldname = "attribute" .$row["id"];
    if (isset($importdata[$fieldname])) {
      if (is_array($importdata[$fieldname])) {
        $attributes[$row["id"]] = join(',',$importdata[$fieldname]);
      } else {
        $attributes[$row["id"]] = $importdata[$fieldname];
      }
    } else {
      $attributes[$row["id"]] = '';
    }
  }

  while (list($email,$data) = each ($user_list)) {
    ## a lot of spreadsheet include those annoying quotes
    $email = str_replace('"', '', $email);      
    set_time_limit(60);
    if ($done % 50 == 0) {
    #  print "$done / $todo<br/>";
      print '<script type="text/javascript">
      var parentJQuery = window.parent.jQuery;
      parentJQuery("#progressbar").updateProgress("'.$done.','.$todo.'");
      </script>';      
      flush();
    }
    $done++;
    if (strlen($email) > 4) {
      $email = addslashes($email);
      // Annoying hack => Much too time consuming. Solution => Set email in users to UNIQUE()
      $result = Sql_query("SELECT id,uniqid FROM ".$tables["user"]." WHERE email = '$email'");
      if (Sql_affected_rows()) {
        // Email exist, remember some values to add them to the lists
        $user = Sql_fetch_array($result);
        $userid = $user["id"];
        $uniqid = $user["uniqid"];
        $old_listmembership = array();
        $history_entry = $GLOBALS['I18N']->get('Import of existing subscriber');
        $old_data = Sql_Fetch_Array_Query(sprintf('select * from %s where id = %d',$tables["user"],$userid));
        $old_data = array_merge($old_data,getUserAttributeValues('',$userid));
        # and membership of lists
        $req = Sql_Query("select * from {$tables["listuser"]} where userid = $userid");
        while ($row = Sql_Fetch_Array($req)) {
          $old_listmembership[$row["listid"]] = listName($row["listid"]);
        }
        $count_email_exist++;
      } else {

        // Email does not exist

        // Create unique number
        $randval = mt_rand();
        $uniqid = getUniqid();
        $old_listmembership = array();

{'fraction_non_alphanumeric': 0.18407793080282164, 'fraction_numerical': 0.004366812227074236, 'mean_word_length': 3.308731307284129, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2547859', 'n_tokens_mistral': 2991, 'n_tokens_neox': 2927, 'n_words': 632}