1<?php
2
3
4verifyCsrfGetToken();
5@ob_end_flush();
6
7$file = $_GET['file'];
8if (!file_exists($GLOBALS['tmpdir'] . '/' . $file) || !file_exists($GLOBALS['tmpdir'] . '/' . $file . '.data')) {
9    echo s('File not found');
10
11    return;
12}
13$omit_invalid = false;
14if ($_GET['omitinvalid']) {
15    $omit_invalid = true;
16}
17
18$importdata = unserialize(file_get_contents($GLOBALS['tmpdir'] . '/' . $file . '.data'));
19
20$email_list = file_get_contents($GLOBALS['tmpdir'] . '/' . $file);
21include_once dirname(__FILE__) . '/../inc/userlib.php';
22
23// Clean up email file
24$email_list = trim($email_list);
25$email_list = str_replace("\r", "\n", $email_list);
26$email_list = str_replace("\n\r", "\n", $email_list);
27$email_list = str_replace("\n\n", "\n", $email_list);
28
29if (isset($importdata['import_record_delimiter'])) {
30    $import_record_delimiter = $importdata['import_record_delimiter'];
31} else {
32    $import_record_delimiter = "\n";
33}
34
35// Change delimiter for new line.
36if (isset($import_record_delimiter) && $import_record_delimiter != '' && $import_record_delimiter != "\n") {
37    $email_list = str_replace($import_record_delimiter, "\n", $email_list);
38}
39
40// Split file/emails into array
41$email_list = explode("\n", $email_list);
42
43// Parse the lines into records
44$hasinfo = 0;
45$count_invalid_emails = 0;
46foreach ($email_list as $line) {
47    $info = '';
48    $email = trim($line); //# just take the entire line up to the first space to be the email
49    if (strpos($email, ' ')) {
50        list($email, $info) = explode(' ', $email);
51    }
52
53    if (!is_email($email) && $omit_invalid) {
54        unset($email, $info);
55        $count_invalid_emails++;
56    }
57    //# actually looks like the "info" bit will get lost, but
58    //# in a way, that doesn't matter
59    $user_list[$email] = array(
60        'info' => $info,
61    );
62}
63
64$count_email_add = 0;
65$count_email_exist = 0;
66$count_list_add = 0;
67$additional_emails = 0;
68$foundBlacklisted = 0;
69$some = 0;
70$num_lists = count($importdata['importlists']);
71$todo = count($user_list);
72$done = 0;
73$report = '';
74if ($hasinfo) {
75    // we need to add an info attribute if it does not exist
76    $req = Sql_Query('select id from ' . $tables['attribute'] . ' where name = "info"');
77    if (!Sql_Affected_Rows()) {
78        // it did not exist
79        Sql_Query(sprintf('insert into %s (name,type,listorder,default_value,required,tablename)
80       values("info","textline",0,"",0,"info")', $tables['attribute']));
81    }
82}
83
84// which attributes were chosen, apply to all users
85$res = Sql_Query('select * from ' . $tables['attribute']);
86$attributes = array();
87while ($row = Sql_Fetch_Array($res)) {
88    $fieldname = 'attribute' . $row['id'];
89    if (isset($importdata[$fieldname])) {
90        if (is_array($importdata[$fieldname])) {
91            $attributes[$row['id']] = implode(',', $importdata[$fieldname]);
92        } else {
93            $attributes[$row['id']] = $importdata[$fieldname];
94        }
95    } else {
96        $attributes[$row['id']] = '';
97    }
98}
99
100foreach ($user_list as $email => $data) {
101    //# a lot of spreadsheet include those annoying quotes
102    $email = str_replace('"', '', $email);
103    set_time_limit(60);
104    if ($done % 50 == 0) {
105        //  print "$done / $todo<br/>";
106        echo '<script type="text/javascript">
107      var parentJQuery = window.parent.jQuery;
108      parentJQuery("#progressbar").updateProgress("' . $done . ',' . $todo . '");
109      </script>';
110        flush();
111    }
112    ++$done;
113    if (strlen($email) > 4) {
114        $email = addslashes($email);
115        // Annoying hack => Much too time consuming. Solution => Set email in users to UNIQUE()
116        $result = Sql_query('SELECT id,uniqid FROM ' . $tables['user'] . " WHERE email = '$email'");
117        if (Sql_affected_rows()) {
118            // Email exist, remember some values to add them to the lists
119            $user = Sql_fetch_array($result);
120            $userid = $user['id'];
121            $uniqid = $user['uniqid'];
122            $old_listmembership = array();
123            $history_entry = $GLOBALS['I18N']->get('Import of existing subscriber');
124            $old_data = Sql_Fetch_Array_Query(sprintf('select * from %s where id = %d', $tables['user'], $userid));
125            $old_data = array_merge($old_data, getUserAttributeValues('', $userid));
126            // and membership of lists
127            $req = Sql_Query("select * from {$tables['listuser']} where userid = $userid");
128            while ($row = Sql_Fetch_Array($req)) {
129                $old_listmembership[$row['listid']] = listName($row['listid']);
130            }
131            ++$count_email_exist;
132        } else {
133
134            // Email does not exist
135
136            // Create unique number
137            $randval = mt_rand();
138            $uniqid = getUniqid();
139            $old_listmembership = array();
140            $old_data = array();
141
142            $query = sprintf('INSERT INTO %s (email,entered,confirmed,uniqid,htmlemail,uuid) values("%s",now(),%d,"%s","%s", "%s")',
143                $tables['user'], $email, $importdata['notify'] != 'yes', $uniqid,
144                isset($importdata['htmlemail']) ? '1' : '0', (string)uuid::generate(4));
145            $result = Sql_query($query);
146            $userid = Sql_Insert_Id($tables['user'], 'id');
147
148            ++$count_email_add;
149            $some = 1;
150            $history_entry = $GLOBALS['I18N']->get('Import of new subscriber');
151
152            // add the attributes for this user
153            foreach ($attributes as $attr => $value) {
154                if (is_array($value)) {
155                    $value = implode(',', $value);
156                }
157                Sql_query(sprintf('replace into %s (attributeid,userid,value) values("%s","%s","%s")',
158                    $tables['user_attribute'], $attr, $userid, addslashes($value)));
159            }
160        }
161
162        //add this user to the lists identified, execpt when found in the blacklist
163        $addition = 0;
164        $listoflists = '';
165        $isBlackListed = isBlackListed($email);
166        if (!$isBlackListed) {
167            foreach ($importdata['importlists'] as $key => $listid) {
168                $query = 'insert ignore INTO ' . $tables['listuser'] . " (userid,listid,entered) values($userid,$listid,now())";
169                $result = Sql_query($query);
170                // if the affected rows is 0, the user was already subscribed
171                $addition = $addition || Sql_Affected_Rows() == 1;
172                if (!empty($importdata['listname'][$key])) {
173                    $listoflists .= '  * ' . $importdata['listname'][$key] . "\n";
174                }
175            }
176            if ($addition) {
177                ++$additional_emails;
178            }
179        } else {
180            //# mark blacklisted, just in case ##17288
181            Sql_Query(sprintf('update %s set blacklisted = 1 where id = %d', $tables['user'], $userid));
182            ++$foundBlacklisted;
183        }
184
185        $subscribemessage = str_replace('[LISTS]', $listoflists, getUserConfig('subscribemessage', $userid));
186        if (!TEST && $importdata['notify'] == 'yes' && $addition) {
187            sendMail($email, getConfig('subscribesubject'), $subscribemessage, system_messageheaders(), $envelope);
188            if ($throttle_import) {
189                sleep($throttle_import);
190            }
191        }
192        // history stuff
193        $current_data = Sql_Fetch_Array_Query(sprintf('select * from %s where id = %d', $tables['user'], $userid));
194        $current_data = array_merge($current_data, getUserAttributeValues('', $userid));
195        foreach ($current_data as $key => $val) {
196            if (!is_numeric($key)) {
197                if (isset($old_data[$key]) && $old_data[$key] != $val && $key != 'modified') {
198                    $history_entry .= "$key = $val\nchanged from $old_data[$key]\n";
199                }
200            }
201        }
202        if (!$history_entry) {
203            $history_entry = "\n" . $GLOBALS['I18N']->get('No data changed');
204        }
205        // check lists
206        $listmembership = array();
207        $req = Sql_Query("select * from {$tables['listuser']} where userid = $userid");
208        while ($row = Sql_Fetch_Array($req)) {
209            $listmembership[$row['listid']] = listName($row['listid']);
210        }
211        $history_entry .= "\n" . $GLOBALS['I18N']->get('List subscriptions:') . "\n";
212        foreach ($old_listmembership as $key => $val) {
213            $history_entry .= $GLOBALS['I18N']->get('Was subscribed to:') . " $val\n";
214        }
215        foreach ($listmembership as $key => $val) {
216            $history_entry .= $GLOBALS['I18N']->get('Is now subscribed to:') . " $val\n";
217        }
218        if (!count($listmembership)) {
219            $history_entry .= $GLOBALS['I18N']->get('Not subscribed to any lists') . "\n";
220        }
221
222        addUserHistory($email, $GLOBALS['I18N']->get('Import by') . ' ' . adminName(), $history_entry);
223    } // end if
224} // end while
225
226// lets be gramatically correct :-)
227$displists = ($num_lists == 1) ? $GLOBALS['I18N']->get('list') : $GLOBALS['I18N']->get('lists');
228$dispemail = ($count_email_add == 1) ? $GLOBALS['I18N']->get('new email was') : $GLOBALS['I18N']->get('new emails were');
229$dispemail2 = ($additional_emails == 1) ? $GLOBALS['I18N']->get('email was') : $GLOBALS['I18N']->get('emails were');
230
231if ($count_email_exist) {
232    $report .= '<br/> ' . s('%d emails already existed in the database', $count_email_exist);
233}
234if ($count_invalid_emails !== 0) {
235    $report .= '<br/> ' . s('%d invalid emails', $count_invalid_emails);
236}
237if (!$some && !$additional_emails) {
238    $report .= '<br/>' . s('All the emails already exist in the database.');
239} else {
240    $report .= "<br/>$count_email_add $dispemail " . s('succesfully imported to the database and added to') . " $num_lists $displists.<br/>$additional_emails $dispemail2 " . $GLOBALS['I18N']->get('subscribed to the') . " $displists";
241}
242if ($foundBlacklisted) {
243    $report .= '<br/>' . s('%d emails were found on the do-not-send-list and have not been added to the lists',
244            $foundBlacklisted);
245}
246
247$htmlupdate = $report . '<br/>' .'<div class="input button btn btn-default">'.PageLinkButton('import1', s('Import some more emails')).'</div>';
248$htmlupdate = str_replace("'", "\'", $htmlupdate);
249
250$status = '<script type="text/javascript">
251      var parentJQuery = window.parent.jQuery;
252      parentJQuery("#progressbar").progressbar("destroy");
253      parentJQuery("#busyimage").hide();
254      parentJQuery("#progresscount").html(\'' . $htmlupdate . '\');
255      </script>';
256
257@unlink($GLOBALS['tmpdir'] . '/' . $file);
258@unlink($GLOBALS['tmpdir'] . '/' . $file . '.data');
259
260//  print ActionResult($report);
261foreach ($GLOBALS['plugins'] as $pluginname => $plugin) {
262    $plugin->importReport($report);
263}
264