Guest User

Untitled

a guest
Sep 5th, 2017
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 12.12 KB | None | 0 0
  1. <?php include('includes/helpers/EmailAddressValidator.php');?>
  2. <?php include('includes/helpers/parsecsv.php');?>
  3. <?php
  4.     include('includes/config.php');
  5.     //--------------------------------------------------------------//
  6.     function dbConnect() { //Connect to database
  7.     //--------------------------------------------------------------//
  8.         // Access global variables
  9.         global $mysqli;
  10.         global $dbHost;
  11.         global $dbUser;
  12.         global $dbPass;
  13.         global $dbName;
  14.         global $dbPort;
  15.        
  16.         // Attempt to connect to database server
  17.         if(isset($dbPort)) $mysqli = new mysqli($dbHost, $dbUser, $dbPass, $dbName, $dbPort);
  18.         else $mysqli = new mysqli($dbHost, $dbUser, $dbPass, $dbName);
  19.    
  20.         // If connection failed...
  21.         if ($mysqli->connect_error) {
  22.             fail();
  23.         }
  24.        
  25.         global $charset; mysqli_set_charset($mysqli, isset($charset) ? $charset : "utf8");
  26.        
  27.         return $mysqli;
  28.     }
  29.     //--------------------------------------------------------------//
  30.     function fail() { //Database connection fails
  31.     //--------------------------------------------------------------//
  32.         print 'Database error';
  33.         exit;
  34.     }
  35.     // connect to database
  36.     dbConnect();
  37. ?>
  38. <?php
  39.  
  40. //setup cron
  41. $q = 'SELECT id, cron_csv FROM login LIMIT 1';
  42. $r = mysqli_query($mysqli, $q);
  43. if ($r)
  44. {
  45.     while($row = mysqli_fetch_array($r))
  46.     {
  47.         $cron = $row['cron_csv'];
  48.         $userid = $row['id'];
  49.        
  50.         if($cron==0)
  51.         {
  52.             $q2 = 'UPDATE login SET cron_csv=1 WHERE id = '.$userid;
  53.             $r2 = mysqli_query($mysqli, $q2);
  54.             if ($r2) exit;
  55.         }
  56.     }  
  57. }
  58.  
  59. //Get the first CSV file
  60. if(!isset($server_path))
  61. {
  62.     $server_path_array = explode('import-csv.php', $_SERVER['SCRIPT_FILENAME']);
  63.     $server_path = $server_path_array[0];
  64. }
  65. $csvfile = '';
  66. if ($handle = opendir($server_path.'uploads/csvs'))
  67. {
  68.     while (false !== ($file = readdir($handle)))
  69.     {
  70.         if($file!='.' && $file!='..' && $file!='.DS_Store' && $file!='.svn')
  71.         {
  72.             $csvfile = $server_path.'uploads/csvs/'.$file;
  73.             break;
  74.         }
  75.     }
  76.     closedir($handle);
  77. }
  78. if($csvfile=='') exit; //Nothing to process, quit here.
  79.  
  80. //Initialize data
  81. $data_array = explode('-', $file);
  82. $userID = $data_array[0];
  83. $listID = str_replace('.csv', '', $data_array[1]);
  84.  
  85. //Check if CSV is currently processing
  86. $q = 'SELECT lists.app, lists.currently_processing, lists.prev_count, lists.total_records, login.timezone FROM lists, login WHERE lists.id = '.$listID.' AND login.app = lists.app';
  87. $r = mysqli_query($mysqli, $q);
  88. if ($r)
  89. {
  90.     while($row = mysqli_fetch_array($r))
  91.     {
  92.         $timezone = $row['timezone'];
  93.         $current_processing = $row['currently_processing'];
  94.         $prev_count = $row['prev_count'];
  95.         $total_records = $row['total_records'];
  96.         $app = $row['app'];
  97.        
  98.         //set timezone
  99.         if($timezone=='') date_default_timezone_set(date_default_timezone_get());
  100.         else date_default_timezone_set($timezone);
  101.     }
  102. }
  103.  
  104. //get comma separated lists belonging to this app
  105. $q2 = 'SELECT id FROM lists WHERE app = '.$app;
  106. $r2 = mysqli_query($mysqli, $q2);
  107. if ($r2)
  108. {
  109.     $all_lists = '';
  110.     while($row = mysqli_fetch_array($r2)) $all_lists .= $row['id'].',';
  111.     $all_lists = substr($all_lists, 0, -1);
  112. }
  113.  
  114. //If CSV is not processed before, process it
  115. if(!$current_processing)
  116. {
  117.     $csv = new parseCSV();
  118.     if(isset($_GET['offset'])) $csv->offset = $_GET['offset'];
  119.     $csv->heading = false;
  120.     $csv->auto($csvfile);
  121.     $databasetable = "subscribers";
  122.     $fieldseparator = ",";
  123.     $time = time();
  124.  
  125.     //Set currently_processing status to 1 'lists' table and prev_count
  126.     set_currently_processing(1);
  127.     if(!isset($_GET['offset'])) set_prev_count();
  128.    
  129.     //Process the CSV
  130.     foreach ($csv->data as $key => $line)
  131.     {      
  132.         //get the columns  
  133.         $linearray = array();
  134.         if(count($csv->data)==1)
  135.         {
  136.             $file = fopen($csvfile,"r");
  137.             $size = filesize($csvfile);
  138.             $csvcontent = fread($file,$size);
  139.             fclose($file);
  140.             $linearray = explode($fieldseparator,$csvcontent);
  141.             $columns = count($linearray);
  142.             $columns_additional = $columns - 2;
  143.         }
  144.         else
  145.         {
  146.             foreach($line as $val) array_push($linearray, $val);
  147.             $columns = count($linearray);
  148.             $columns_additional = $columns - 2;
  149.         }
  150.        
  151.         //check for duplicates
  152.         $q = 'SELECT email FROM subscribers WHERE list = '.$listID.' AND (email = "'.$linearray[0].'" || email = "'.trim($linearray[1]).'")';
  153.         $r = mysqli_query($mysqli, $q);
  154.         $duplicates = false;
  155.         //EDIT -> only set a flag if there are duplicates; Updating should happen in both cases
  156.         if (mysqli_num_rows($r) > 0) {
  157.             $duplicates = true;
  158.         }
  159.         else
  160.         {
  161.             $duplicates = false;
  162.         }  
  163.  
  164.         //Get the list of custom fields for this list
  165.         $q2 = 'SELECT custom_fields FROM lists WHERE id = '.$listID;
  166.         $r2 = mysqli_query($mysqli, $q2);
  167.         if ($r2)
  168.         {
  169.             $custom_fields = '';
  170.            
  171.             while($row = mysqli_fetch_array($r2))
  172.             {
  173.                 $custom_fields = $row['custom_fields'];
  174.             }  
  175.            
  176.             //if there are custom fields in this list,
  177.             if($custom_fields!='')
  178.             {
  179.             $custom_fields_value = '';
  180.                 $custom_fields_array = explode('%s%', $custom_fields);
  181.                 $custom_fields_count = count($custom_fields_array);
  182.                
  183.                 //prepare custom field string
  184.                 for($i=2;$i<$columns_additional+2;$i++)
  185.                 {
  186.                 $custom_fields_array2 = explode(':', $custom_fields_array[$i-2]);
  187.                 //if custom field format is Date
  188.                     if($custom_fields_array2[1]=='Date')
  189.                     {
  190.                         if($linearray[$i]=="") $value = $linearray[$i];
  191.                         else
  192.                         {
  193.                             $date_value1 = strtotime($linearray[$i]);
  194.                             $date_value2 = strftime("%b %d, %Y 12am", $date_value1);
  195.                             $value = strtotime($date_value2);
  196.                         }
  197.                         $custom_fields_value .= $value;
  198.                         $custom_fields_value .= '%s%';
  199.                     }
  200.                     //else if custom field format is Text
  201.                     else
  202.                     {
  203.                         $custom_fields_value .= strip_tags($linearray[$i]);
  204.                         $custom_fields_value .= '%s%';
  205.                     }
  206.                 }
  207.             }
  208.         }
  209.        
  210.         //Check if user set the list to unsubscribe from all lists
  211.         $q = 'SELECT unsubscribe_all_list FROM lists WHERE id = '.$listID;
  212.         $r = mysqli_query($mysqli, $q);
  213.         if ($r) while($row = mysqli_fetch_array($r)) $unsubscribe_all_list = $row['unsubscribe_all_list'];
  214.  
  215.         //Check if this email is previously marked as bounced, if so, we shouldn't add it
  216.         if($unsubscribe_all_list)
  217.             $q = 'SELECT email from subscribers WHERE ( (email = "'.$linearray[0].'" || email = " '.$linearray[1].'" || email = "'.$linearray[1].'") AND bounced = 1 ) OR ( (email = "'.$linearray[0].'" || email = " '.$linearray[1].'" || email = "'.$linearray[1].'") AND list IN ('.$all_lists.') AND (complaint = 1 OR unsubscribed = 1) )';
  218.         else
  219.             $q = 'SELECT email from subscribers WHERE ( (email = "'.$linearray[0].'" || email = " '.$linearray[1].'" || email = "'.$linearray[1].'") AND bounced = 1 ) OR ( (email = "'.$linearray[0].'" || email = " '.$linearray[1].'" || email = "'.$linearray[1].'") AND list IN ('.$all_lists.') AND complaint = 1 )';
  220.         $r = mysqli_query($mysqli, $q);
  221.         if (mysqli_num_rows($r) > 0) {}
  222.         else
  223.         {              
  224.             $validator = new EmailAddressValidator;
  225.            
  226.             //if CSV has only 1 column, insert into email column
  227.             //EDIT -> Only insert this if it is not a duplicate
  228.             if($columns==1 && !$duplicates)
  229.             {
  230.                 if ($validator->check_email_address(trim($linearray[0])))
  231.                 {
  232.                     //insert email into database
  233.                     $query = 'INSERT INTO '.$databasetable.' (userID, email, list, timestamp) values('.$userID.', "'.trim($linearray[0]).'", '.$listID.', '.$time.')';
  234.                     mysqli_query($mysqli, $query);
  235.                     $inserted_id = mysqli_insert_id($mysqli);
  236.                 }
  237.             }
  238.             //if CSV has 2 columns, insert into name and email columns
  239.             //EDIT -> Only insert this if it is not a duplicate
  240.             else if($columns==2 && !$duplicates)
  241.             {
  242.                 if ($validator->check_email_address(trim($linearray[1])))
  243.                 {
  244.                     //insert name & email into database
  245.                     $query = 'INSERT INTO '.$databasetable.' (userID, name, email, list, timestamp) values('.$userID.', "'.strip_tags($linearray[0]).'", "'.trim($linearray[1]).'", '.$listID.', '.$time.')';
  246.                     mysqli_query($mysqli, $query);
  247.                     $inserted_id = mysqli_insert_id($mysqli);
  248.                 }
  249.             }
  250.             //if number of CSV columns matches database, insert name, email and all custom fields
  251.             else if($columns==$custom_fields_count+2)
  252.             {
  253.                 if ($validator->check_email_address(trim($linearray[1])))
  254.                 {
  255.                     //EDIT -> Only insert if it is not a duplicate
  256.                     if (!$duplicates) {
  257.                         //insert name & email into database
  258.                         $query = 'INSERT INTO '.$databasetable.' (userID, name, email, list, timestamp) values('.$userID.', "'.strip_tags($linearray[0]).'", "'.trim($linearray[1]).'", '.$listID.', '.$time.')';
  259.                         mysqli_query($mysqli, $query);
  260.                         $inserted_id = mysqli_insert_id($mysqli);
  261.                     //EDIT-> If it is a duplicate, find the ID and update the custom fields.
  262.                     } else {
  263.                         $em_query = 'SELECT id FROM subscribers WHERE list = '.$listID.' AND (email = "'.$linearray[0].'" || email = "'.trim($linearray[1]).'")';
  264.                         $em_res = mysqli_query($mysqli, $em_query);
  265.                         if ($em_res) {
  266.                             while($em_row = mysqli_fetch_array($em_res)) $inserted_id = $em_row['id'];
  267.  
  268.                         }
  269.                     }
  270.                    
  271.                     //update custom fields values
  272.                  //   $q3 = 'UPDATE '.$databasetable.' SET custom_fields = "'.substr($custom_fields_value, 0, -3).'" WHERE id = '.$inserted_id;
  273.                     $q3 = 'UPDATE '.$databasetable.' SET custom_fields = "'.substr($custom_fields_value, 0, -3).'" WHERE id = '.$inserted_id;
  274.                     $r3 = mysqli_query($mysqli, $q3);
  275.                     if ($r3){}
  276.                 }
  277.             }
  278.             else
  279.             {
  280.                 exit;
  281.             }
  282.         }
  283.        
  284.         //Check if all are imported    
  285.         if(isset($_GET['offset']))
  286.         {
  287.             if($key+1 == $total_records-$_GET['offset'])
  288.             {
  289.                 //set currently_processing to 0
  290.                 $q = 'UPDATE lists SET currently_processing=0, prev_count=0, total_records=0 WHERE id = '.$listID;
  291.                 mysqli_query($mysqli, $q);             
  292.                
  293.                 //delete CSV file
  294.                 unlink($csvfile);
  295.             }
  296.         }
  297.         else
  298.         {
  299.             if($key+1 == $total_records)
  300.             {
  301.                 //set currently_processing to 0
  302.                 $q = 'UPDATE lists SET currently_processing=0, prev_count=0, total_records=0 WHERE id = '.$listID;
  303.                 mysqli_query($mysqli, $q);
  304.                
  305.                 //delete CSV file
  306.                 unlink($csvfile);
  307.             }
  308.         }
  309.     }
  310. }
  311. //Otherwise, check if CSV import timed out
  312. else
  313. {
  314.     $q = 'SELECT COUNT(*) FROM subscribers WHERE list = '.$listID.' AND unsubscribed = 0 AND bounced = 0 AND complaint = 0 AND confirmed = 1';
  315.     $r = mysqli_query($mysqli, $q);
  316.     if($r) while($row = mysqli_fetch_array($r)) $before_count = $row['COUNT(*)'];
  317.     sleep(8);
  318.     $q2 = 'SELECT COUNT(*) FROM subscribers WHERE list = '.$listID.' AND unsubscribed = 0 AND bounced = 0 AND complaint = 0 AND confirmed = 1';
  319.     $r2 = mysqli_query($mysqli, $q2);
  320.     if($r2) while($row = mysqli_fetch_array($r2)) $after_count = $row['COUNT(*)'];
  321.    
  322.     if($before_count==$after_count)
  323.     {
  324.         //Set currently_processing status to 0 in 'lists' table
  325.         set_currently_processing(0);
  326.        
  327.         //Calculate offset
  328.         $offset = $after_count-$prev_count;
  329.    
  330.         //continue importing
  331.         $ch = curl_init();
  332.         curl_setopt($ch, CURLOPT_HEADER, 0);
  333.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  334.         curl_setopt($ch, CURLOPT_URL, APP_PATH.'/import-csv.php?offset='.$offset);
  335.         curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  336.         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  337.         $data = curl_exec($ch);
  338.        
  339.         exit;
  340.     }
  341. }
  342.  
  343. //Once everything is imported, reset count and remove CSV
  344. //set currently_processing to 0
  345. $q = 'UPDATE lists SET currently_processing=0, prev_count=0, total_records=0 WHERE id = '.$listID;
  346. mysqli_query($mysqli, $q);
  347. //delete CSV file
  348. unlink($csvfile);
  349.  
  350. function set_currently_processing($val)
  351. {
  352.     global $listID;
  353.     global $mysqli;
  354.    
  355.     $q = 'UPDATE lists SET currently_processing='.$val.' WHERE id = '.$listID;
  356.     mysqli_query($mysqli, $q);
  357. }
  358. function set_prev_count()
  359. {
  360.     global $listID;
  361.     global $mysqli;
  362.    
  363.     $q = 'SELECT COUNT(id) FROM subscribers WHERE list = '.$listID.' AND unsubscribed = 0 AND bounced = 0 AND complaint = 0 AND confirmed = 1';
  364.     $r = mysqli_query($mysqli, $q);
  365.     if ($r) while($row = mysqli_fetch_array($r)) $count = $row['COUNT(id)'];
  366.    
  367.     $q2 = 'UPDATE lists SET prev_count = '.$count.' WHERE id = '.$listID;
  368.     $r2 = mysqli_query($mysqli, $q2);
  369. }
  370. ?>
Add Comment
Please, Sign In to add comment