| 1 |
<?php |
| 2 |
|
| 3 |
use AcyMailing\Classes\FieldClass; |
| 4 |
use AcyMailing\Classes\UserClass; |
| 5 |
use AcyMailing\Helpers\EncodingHelper; |
| 6 |
|
| 7 |
$encodingHelper = new EncodingHelper(); |
| 8 |
$fieldClass = new FieldClass(); |
| 9 |
$userClass = new UserClass(); |
| 10 |
$filename = strtolower(acym_getVar('cmd', 'acym_import_filename', '')); |
| 11 |
$encoding = acym_getVar('cmd', 'encoding'); |
| 12 |
|
| 13 |
$extension = '.'.acym_fileGetExt($filename); |
| 14 |
$uploadPath = ACYM_MEDIA.'import'.DS.str_replace(['.', ' '], '_', substr($filename, 0, strpos($filename, $extension))).$extension; |
| 15 |
|
| 16 |
if (!file_exists($uploadPath)) { |
| 17 |
acym_display(acym_translationSprintf('ACYM_FAIL_OPEN', '<b><i>'.acym_escape($uploadPath).'</i></b>'), 'error'); |
| 18 |
|
| 19 |
return; |
| 20 |
} |
| 21 |
|
| 22 |
$this->content = file_get_contents($uploadPath); |
| 23 |
|
| 24 |
if (empty($encoding)) { |
| 25 |
$encoding = $encodingHelper->detectEncoding($this->content); |
| 26 |
} |
| 27 |
|
| 28 |
$content = $encodingHelper->change($this->content, $encoding, 'UTF-8'); |
| 29 |
|
| 30 |
|
| 31 |
$content = str_replace(["\r\n", "\r"], "\n", $content); |
| 32 |
$this->lines = explode("\n", $content); |
| 33 |
|
| 34 |
$this->separator = ','; |
| 35 |
$listSeparators = ["\t", ';', ',']; |
| 36 |
foreach ($listSeparators as $sep) { |
| 37 |
if (strpos($this->lines[0], $sep) !== false) { |
| 38 |
$this->separator = $sep; |
| 39 |
break; |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
$nbPreviewLines = 0; |
| 44 |
$i = 0; |
| 45 |
$data = []; |
| 46 |
|
| 47 |
while (isset($this->lines[$i])) { |
| 48 |
if (empty($this->lines[$i])) { |
| 49 |
unset($this->lines[$i]); |
| 50 |
continue; |
| 51 |
} else { |
| 52 |
$nbPreviewLines++; |
| 53 |
} |
| 54 |
|
| 55 |
if (strpos($this->lines[$i], '"') !== false) { |
| 56 |
$data[$i] = []; |
| 57 |
$j = $i + 1; |
| 58 |
$position = -1; |
| 59 |
|
| 60 |
while ($j < ($i + 30)) { |
| 61 |
$quoteOpened = substr($this->lines[$i], $position + 1, 1) == '"'; |
| 62 |
|
| 63 |
if ($quoteOpened) { |
| 64 |
$nextQuotePosition = strpos($this->lines[$i], '"', $position + 2); |
| 65 |
|
| 66 |
if ($nextQuotePosition === false) { |
| 67 |
if (!isset($this->lines[$j])) { |
| 68 |
break; |
| 69 |
} |
| 70 |
|
| 71 |
$this->lines[$i] .= "\n".rtrim($this->lines[$j], $this->separator); |
| 72 |
unset($this->lines[$j]); |
| 73 |
$j++; |
| 74 |
continue; |
| 75 |
} else { |
| 76 |
$quoteOpened = false; |
| 77 |
|
| 78 |
if (strlen($this->lines[$i]) - 1 == $nextQuotePosition) { |
| 79 |
$data[$i][] = substr($this->lines[$i], $position + 1); |
| 80 |
break; |
| 81 |
} |
| 82 |
|
| 83 |
$data[$i][] = substr($this->lines[$i], $position + 1, $nextQuotePosition + 1 - ($position + 1)); |
| 84 |
$position = $nextQuotePosition + 1; |
| 85 |
} |
| 86 |
} else { |
| 87 |
$nextSeparatorPosition = strpos($this->lines[$i], $this->separator, $position + 1); |
| 88 |
if ($nextSeparatorPosition === false) { |
| 89 |
$data[$i][] = substr($this->lines[$i], $position + 1); |
| 90 |
break; |
| 91 |
} else { |
| 92 |
$data[$i][] = substr($this->lines[$i], $position + 1, $nextSeparatorPosition - ($position + 1)); |
| 93 |
$position = $nextSeparatorPosition; |
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
$this->lines = array_merge($this->lines); |
| 99 |
} else { |
| 100 |
$data[$i] = explode($this->separator, rtrim(trim($this->lines[$i]), $this->separator)); |
| 101 |
} |
| 102 |
|
| 103 |
if ($nbPreviewLines == 10) { |
| 104 |
break; |
| 105 |
} |
| 106 |
|
| 107 |
if ($nbPreviewLines != 1) { |
| 108 |
$i++; |
| 109 |
continue; |
| 110 |
} |
| 111 |
|
| 112 |
if (strpos($this->lines[$i], '@')) { |
| 113 |
$noHeader = 1; |
| 114 |
} else { |
| 115 |
$noHeader = 0; |
| 116 |
} |
| 117 |
|
| 118 |
$columnNames = explode($this->separator, $this->lines[$i]); |
| 119 |
$nbColumns = count($columnNames); |
| 120 |
if (!empty($i)) { |
| 121 |
unset($this->lines[$i]); |
| 122 |
} |
| 123 |
ksort($this->lines); |
| 124 |
} |
| 125 |
|
| 126 |
$this->lines = $data; |
| 127 |
$nbLines = count($this->lines); |
| 128 |
|
| 129 |
?> |
| 130 |
<div class="table-scroll"> |
| 131 |
<table cellspacing="10" cellpadding="10" id="importdata" class="unstriped"> |
| 132 |
<?php |
| 133 |
if ($noHeader || !isset($this->lines[1])) { |
| 134 |
$firstValueLine = $columnNames; |
| 135 |
} else { |
| 136 |
$firstValueLine = $this->lines[1]; |
| 137 |
foreach ($firstValueLine as &$oneValue) { |
| 138 |
$oneValue = trim($oneValue, '\'" '); |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
$fieldAssignment = []; |
| 143 |
|
| 144 |
$fieldAssignment[] = acym_selectOption('0', 'ACYM_IGNORED'); |
| 145 |
$separator = acym_selectOption('3', '----------------------'); |
| 146 |
$separator->disable = true; |
| 147 |
$fieldAssignment[] = $separator; |
| 148 |
|
| 149 |
|
| 150 |
$fields = $userClass->getAllColumnsUserAndCustomField(); |
| 151 |
if (acym_isAdmin()) { |
| 152 |
$fields['listids'] = 'listids'; |
| 153 |
$fields['listname'] = 'listname'; |
| 154 |
} |
| 155 |
|
| 156 |
$cleanFields = []; |
| 157 |
foreach ($fields as $value => $label) { |
| 158 |
if (in_array($value, ['id', 'automation'])) { |
| 159 |
continue; |
| 160 |
} |
| 161 |
|
| 162 |
if (is_numeric($value)) { |
| 163 |
$field = $fieldClass->getOneById($value); |
| 164 |
|
| 165 |
$value = 'cf_'.$value; |
| 166 |
$fieldAssignment[] = acym_selectOption($value, $label); |
| 167 |
|
| 168 |
$cleanFields[$value] = empty($field) ? strtolower($label) : $field->namekey; |
| 169 |
} else { |
| 170 |
$fieldAssignment[] = acym_selectOption($value, $label); |
| 171 |
$cleanFields[$value] = strtolower($label); |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
echo '<tr>'; |
| 176 |
|
| 177 |
$alreadyFound = []; |
| 178 |
|
| 179 |
foreach ($columnNames as $key => $oneColumn) { |
| 180 |
$columnNames[$key] = strtolower(trim($columnNames[$key], '\'" ')); |
| 181 |
|
| 182 |
$customValue = ''; |
| 183 |
|
| 184 |
$selectedField = acym_getVar('cmd', 'fieldAssignment'.$key, ''); |
| 185 |
|
| 186 |
if (empty($selectedField) && $selectedField !== 0) { |
| 187 |
if (isset($cleanFields[$columnNames[$key]])) { |
| 188 |
$selectedField = $columnNames[$key]; |
| 189 |
} elseif (in_array($columnNames[$key], $cleanFields)) { |
| 190 |
$selectedField = array_search(strtolower($columnNames[$key]), $cleanFields); |
| 191 |
} else { |
| 192 |
$selectedField = '0'; |
| 193 |
} |
| 194 |
|
| 195 |
if (!$selectedField && !empty($firstValueLine)) { |
| 196 |
if (isset($firstValueLine[$key]) && strpos($firstValueLine[$key], '@')) { |
| 197 |
$selectedField = 'email'; |
| 198 |
} elseif ($nbColumns === 2) { |
| 199 |
$selectedField = 'name'; |
| 200 |
} |
| 201 |
} |
| 202 |
if (in_array($selectedField, $alreadyFound)) { |
| 203 |
$selectedField = '0'; |
| 204 |
} |
| 205 |
} elseif ($selectedField == 2) { |
| 206 |
$customValue = acym_getVar('cmd', 'newcustom'.$key); |
| 207 |
} |
| 208 |
|
| 209 |
$alreadyFound[] = $selectedField; |
| 210 |
|
| 211 |
echo '<td valign="top">'.acym_select( |
| 212 |
$fieldAssignment, |
| 213 |
'fieldAssignment'.$key, |
| 214 |
$selectedField, |
| 215 |
['class' => 'fieldAssignment'] |
| 216 |
).'<br />'; |
| 217 |
} |
| 218 |
echo '</tr>'; |
| 219 |
|
| 220 |
if (!$noHeader) { |
| 221 |
foreach ($columnNames as $key => $oneColumn) { |
| 222 |
$columnNames[$key] = htmlspecialchars($columnNames[$key], ENT_COMPAT | ENT_IGNORE, 'UTF-8'); |
| 223 |
} |
| 224 |
echo '<tr class="acym__users__import__generic__column_name"><td><b>'.implode('</b></td><td><b>', $columnNames).'</b></td></tr>'; |
| 225 |
} |
| 226 |
|
| 227 |
for ($i = 1 - $noHeader ; $i < 11 - $noHeader && $i < $nbLines ; $i++) { |
| 228 |
$values = $this->lines[$i]; |
| 229 |
|
| 230 |
echo '<tr>'; |
| 231 |
foreach ($values as &$oneValue) { |
| 232 |
$oneValue = htmlspecialchars(trim($oneValue, '\'" '), ENT_COMPAT | ENT_IGNORE, 'UTF-8'); |
| 233 |
echo '<td>'.htmlspecialchars_decode($oneValue).'</td>'; |
| 234 |
} |
| 235 |
echo '</tr>'; |
| 236 |
} |
| 237 |
?> |
| 238 |
</table> |
| 239 |
</div> |
| 240 |
|