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