PluginProbe
AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress / 11.0.5
AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress v11.0.5
11.1.1 11.1.0 11.0.5 11.0.4 11.0.3 11.0.2 11.0.1 11.0.0 10.11.1 10.11.0 10.10.2 10.10.1 10.10.0 10.9.1 trunk 10.0.0 10.0.1 10.1.0 10.1.1 10.1.2 10.1.3 10.1.4 10.2.0 10.2.1 10.2.2 All 62 releases
acymailing / back / Views / Users / tmpl / ajaxencoding.php

ajaxencoding.php in AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress 11.0.5, at back/Views/Users/tmpl/ajaxencoding.php

280 lines 10.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 // phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- View file, its variables are local to the include scope, not true globals.
3 defined('ABSPATH') || die('Restricted Access');
4
5 use AcyMailing\Classes\FieldClass;
6 use AcyMailing\Classes\UserClass;
7 use AcyMailing\Helpers\EncodingHelper;
8
9 $encodingHelper = new EncodingHelper();
10 $fieldClass = new FieldClass();
11 $userClass = new UserClass();
12 $filename = strtolower(acym_getVar('cmd', 'acym_import_filename', ''));
13 $encoding = acym_getVar('cmd', 'encoding');
14
15 $extension = '.'.acym_fileGetExt($filename);
16 $uploadPath = ACYM_MEDIA.'import'.DS.str_replace(['.', ' '], '_', substr($filename, 0, strpos($filename, $extension))).$extension;
17
18 if (!file_exists($uploadPath)) {
19 acym_display(acym_translationSprintf('ACYM_FAIL_OPEN', '<b><i>'.esc_html($uploadPath).'</i></b>'), 'error');
20
21 return;
22 }
23
24 $this->content = file_get_contents($uploadPath);
25
26 if (empty($encoding)) {
27 $encoding = $encodingHelper->detectEncoding($this->content);
28 }
29
30 $content = $encodingHelper->change($this->content, $encoding, 'UTF-8');
31
32
33 // Get the lines in an array
34 $content = str_replace(["\r\n", "\r"], "\n", $content);
35 $this->lines = explode("\n", $content);
36
37 // Get the right separator
38 $this->separator = ',';
39 $listSeparators = ["\t", ';', ','];
40 foreach ($listSeparators as $sep) {
41 if (strpos($this->lines[0], $sep) !== false) {
42 $this->separator = $sep;
43 break;
44 }
45 }
46
47 $nbPreviewLines = 0;
48 $i = 0;
49 $data = [];
50
51 while (isset($this->lines[$i])) {
52 // If empty line, unset it, else count it as line to import (we will display only the 10 first lines to import)
53 if (empty($this->lines[$i])) {
54 unset($this->lines[$i]);
55 continue;
56 } else {
57 $nbPreviewLines++;
58 }
59
60 // If there is a ", the line could be broken as there could be a return line in a value to import
61 if (strpos($this->lines[$i], '"') !== false) {
62 $data[$i] = [];
63 $j = $i + 1;
64 $position = -1;
65
66 // Concatenate 30 lines max
67 while ($j < ($i + 30)) {
68 // Test if the value is encapsulated by quotes
69 $quoteOpened = substr($this->lines[$i], $position + 1, 1) == '"';
70
71 // If encapsulated, search the end of the value
72 if ($quoteOpened) {
73 $nextQuotePosition = strpos($this->lines[$i], '"', $position + 2);
74
75 // If we didn't find the whole value, then concatenate the current line with the next one
76 if ($nextQuotePosition === false) {
77 // If end of import file, error...
78 if (!isset($this->lines[$j])) {
79 break;
80 }
81
82 // Take the right charset
83 $this->lines[$i] .= "\n".rtrim($this->lines[$j], $this->separator);
84 unset($this->lines[$j]);
85 $j++;
86 continue;
87 } else {
88 // Found the entire value, add it to data and move the position
89 $quoteOpened = false;
90
91 // If the line is completed
92 if (strlen($this->lines[$i]) - 1 == $nextQuotePosition) {
93 $data[$i][] = substr($this->lines[$i], $position + 1);
94 break;
95 }
96
97 $data[$i][] = substr($this->lines[$i], $position + 1, $nextQuotePosition + 1 - ($position + 1));
98 $position = $nextQuotePosition + 1;
99 }
100 } else {
101 // If not encapsulated by quotes, search the next separator
102 $nextSeparatorPosition = strpos($this->lines[$i], $this->separator, $position + 1);
103 // If not found, the line is completed
104 if ($nextSeparatorPosition === false) {
105 $data[$i][] = substr($this->lines[$i], $position + 1);
106 break;
107 } else {
108 // If found the next separator, add the value in $data[$i] and change the position
109 $data[$i][] = substr($this->lines[$i], $position + 1, $nextSeparatorPosition - ($position + 1));
110 $position = $nextSeparatorPosition;
111 }
112 }
113 }
114
115 // We unset some lines, don't forget to change the remaining lines keys
116 $this->lines = array_merge($this->lines);
117 } else {
118 $data[$i] = explode($this->separator, rtrim(trim($this->lines[$i]), $this->separator));
119 }
120
121 // We display only the first ten lines
122 if ($nbPreviewLines == 10) {
123 break;
124 }
125
126 if ($nbPreviewLines != 1) {
127 $i++;
128 continue;
129 }
130
131 // If it's the first line, check if there is an header or not
132 if (strpos($this->lines[$i], '@')) {
133 $noHeader = 1;
134 } else {
135 $noHeader = 0;
136 }
137
138 // Get the column names and count them
139 $columnNames = explode($this->separator, $this->lines[$i]);
140 $nbColumns = count($columnNames);
141 if (!empty($i)) {
142 unset($this->lines[$i]);
143 }
144 ksort($this->lines);
145 }
146
147 $this->lines = $data;
148 $nbLines = count($this->lines);
149
150 ?>
151 <div class="table-scroll">
152 <table cellspacing="10" cellpadding="10" id="importdata" class="unstriped">
153 <?php
154 if ($noHeader || !isset($this->lines[1])) {
155 $firstValueLine = $columnNames;
156 } else {
157 $firstValueLine = $this->lines[1];
158 foreach ($firstValueLine as &$oneValue) {
159 $oneValue = trim($oneValue, '\'" ');
160 }
161 }
162
163 // Possible values for columns imported as new custom field
164 $fieldAssignment = [];
165
166 $fieldAssignment[] = acym_selectOption('0', 'ACYM_IGNORED');
167 $separator = acym_selectOption('3', '----------------------');
168 $separator->disable = true;
169 $fieldAssignment[] = $separator;
170
171
172 // This variable will be used to re-assign columns when we change the encoding option
173 $fields = $userClass->getAllColumnsUserAndCustomField(false, true);
174 if (acym_isAdmin()) {
175 $fields['listids'] = 'listids';
176 $fields['listname'] = 'listname';
177 }
178
179 $cleanFields = [];
180 foreach ($fields as $value => $label) {
181 if (in_array($value, ['id', 'automation'])) {
182 continue;
183 }
184
185 if (is_numeric($value)) {
186 $field = $fieldClass->getOneById($value);
187
188 $value = 'cf_'.$value;
189 $fieldAssignment[] = acym_selectOption($value, $label);
190
191 $cleanFields[$value] = empty($field) ? strtolower($label) : $field->namekey;
192 } else {
193 $fieldAssignment[] = acym_selectOption($value, $label);
194 $cleanFields[$value] = strtolower($label);
195 }
196 }
197
198 // Add first line to assign the imported columns, also add a button to ignore all unassigned columns if there are at least 6 columns
199 echo '<tr>';
200
201 // Don't auto-assign two times the same column
202 $alreadyFound = [];
203
204 foreach ($columnNames as $key => $oneColumn) {
205 $columnNames[$key] = strtolower(trim($columnNames[$key], '\'" '));
206
207 $customValue = '';
208
209 // If we assigned the column then changed the charset option (reloading the page), keep the assigned value
210 $selectedField = acym_getVar('cmd', 'fieldAssignment'.$key, '');
211
212 // Auto-detect the column assignment
213 if (empty($selectedField) && $selectedField !== 0) {
214 // If the column name matches one of the AcyMailing fields, take this field
215 if (isset($cleanFields[$columnNames[$key]])) {
216 $selectedField = $columnNames[$key];
217 } elseif (in_array($columnNames[$key], $cleanFields)) {
218 $selectedField = array_search(strtolower($columnNames[$key]), $cleanFields);
219 } else {
220 $selectedField = '0';
221 }
222
223 // Detect if it's the mail field, if there are only two columns and it isn't the email one, it is the name column
224 if (!$selectedField && !empty($firstValueLine)) {
225 if (isset($firstValueLine[$key]) && strpos($firstValueLine[$key], '@')) {
226 $selectedField = 'email';
227 } elseif ($nbColumns === 2) {
228 $selectedField = 'name';
229 }
230 }
231 if (in_array($selectedField, $alreadyFound)) {
232 $selectedField = '0';
233 }
234 } elseif ($selectedField == 2) {
235 // If custom field, fill in the input with the right value
236 $customValue = acym_getVar('cmd', 'newcustom'.$key);
237 }
238
239 $alreadyFound[] = $selectedField;
240
241 echo '<td valign="top">';
242 acym_select(
243 $fieldAssignment,
244 'fieldAssignment'.$key,
245 $selectedField,
246 ['class' => 'fieldAssignment'],
247 'value',
248 'text',
249 null,
250 false,
251 true
252 );
253 echo '<br />';
254 }
255 echo '</tr>';
256
257 // If the first imported line is the header, display a line to show it...
258 if (!$noHeader) {
259 echo '<tr class="acym__users__import__generic__column_name">';
260 foreach ($columnNames as $key => $oneColumn) {
261 echo '<td><b>'.esc_html($oneColumn).'</b></td>';
262 }
263 echo '</tr>';
264 }
265
266 // For each imported line, display it so that the user can preview with the right encoding...
267 for ($i = 1 - $noHeader; $i < 11 - $noHeader && $i < $nbLines; $i++) {
268 $values = $this->lines[$i];
269
270 echo '<tr>';
271 foreach ($values as &$oneValue) {
272 $oneValue = htmlspecialchars(trim($oneValue, '\'" '), ENT_COMPAT | ENT_IGNORE, 'UTF-8');
273 echo '<td>'.esc_html(htmlspecialchars_decode($oneValue)).'</td>';
274 }
275 echo '</tr>';
276 }
277 ?>
278 </table>
279 </div>
280