| @@ -8,14 +8,16 @@ | ||
| 8 | 8 | use AcyMailing\Classes\UserClass; |
| 9 | 9 | use AcyMailing\Controllers\SegmentsController; |
| 10 | 10 | use AcyMailing\Helpers\AutomationHelper; |
| 11 | 11 | use AcyMailing\Helpers\EncodingHelper; |
| 12 | -use AcyMailing\Helpers\EntitySelectHelper; | |
| 13 | 12 | use AcyMailing\Helpers\ExportHelper; |
| 14 | 13 | |
| 15 | 14 | trait Export |
| 16 | 15 | { |
| 17 | - public function export() | |
| 16 | + /** | |
| 17 | + * Export page where the user selects the export option | |
| 18 | + */ | |
| 19 | + public function export(): void | |
| 18 | 20 | { |
| 19 | 21 | acym_setVar('layout', 'export'); |
| 20 | 22 | $this->breadcrumb[acym_translation('ACYM_EXPORT_SUBSCRIBERS')] = acym_completeLink('users&task=export'); |
| 21 | 23 | |
| @@ -46,21 +48,18 @@ | ||
| 46 | 48 | } |
| 47 | 49 | |
| 48 | 50 | $fields = acym_getColumns('user'); |
| 49 | 51 | |
| 52 | + // Never expose sensitive fields in the export UI | |
| 53 | + $fields = array_diff($fields, ['key', 'id']); | |
| 54 | + | |
| 55 | + // Get custom fields and exclude name and email as we already have them | |
| 50 | 56 | $fieldClass = new FieldClass(); |
| 51 | 57 | $customFields = $fieldClass->getAll(); |
| 52 | 58 | |
| 53 | - $entityHelper = new EntitySelectHelper(); | |
| 54 | 59 | $encodingHelper = new EncodingHelper(); |
| 55 | 60 | $userClass = new UserClass(); |
| 56 | 61 | |
| 57 | - if ($preselectList) { | |
| 58 | - $entitySelect = $entityHelper->entitySelect('list', ['join' => 'join_lists-'.implode(',', $checkedElements)], $entityHelper->getColumnsForList('lists.list_id', true)); | |
| 59 | - } else { | |
| 60 | - $entitySelect = $entityHelper->entitySelect('list', ['join' => ''], $entityHelper->getColumnsForList('', true)); | |
| 61 | - } | |
| 62 | - | |
| 63 | 62 | $data = [ |
| 64 | 63 | 'lists' => $lists, |
| 65 | 64 | 'checkedElements' => $checkedElements, |
| 66 | 65 | 'fields' => $fields, |
| @@ -66,9 +65,8 @@ | ||
| 66 | 65 | 'fields' => $fields, |
| 67 | 66 | 'customfields' => $customFields, |
| 68 | 67 | 'coreFields' => [1, 2, $fieldClass->getLanguageFieldId()], |
| 69 | 68 | 'isPreselectedList' => $preselectList, |
| 70 | - 'entitySelect' => $entitySelect, | |
| 71 | 69 | 'exportListStatus' => $filtersListing['list_status'], |
| 72 | 70 | 'encodingHelper' => $encodingHelper, |
| 73 | 71 | 'userClass' => $userClass, |
| 74 | 72 | 'segments' => $availableSegments, |
| @@ -77,19 +75,24 @@ | ||
| 77 | 75 | |
| 78 | 76 | parent::display($data); |
| 79 | 77 | } |
| 80 | 78 | |
| 81 | - public function doexport() | |
| 79 | + /** | |
| 80 | + * This method downloads the exported file directly | |
| 81 | + */ | |
| 82 | + public function doexport(): void | |
| 82 | 83 | { |
| 83 | - acym_checkToken(); | |
| 84 | + wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token'); | |
| 84 | 85 | acym_increasePerf(); |
| 85 | 86 | |
| 87 | + // Get passed data and check if we have everything we need | |
| 86 | 88 | $usersToExport = acym_getVar('string', 'export_users-to-export', 'all'); |
| 87 | - $listsToExport = json_decode(acym_getVar('string', 'acym__entity_select__selected', '{}')); | |
| 88 | - if ($usersToExport == 'list' && empty($listsToExport)) { | |
| 89 | + $selectedLists = acym_getVar('string', 'acym__entity_select__selected', '[]'); | |
| 90 | + $listsToExport = json_decode(empty($selectedLists) ? '[]' : $selectedLists, true); | |
| 91 | + if ($usersToExport === 'list' && empty($listsToExport)) { | |
| 89 | 92 | acym_enqueueMessage(acym_translation('ACYM_EXPORT_SELECT_LIST'), 'error'); |
| 90 | 93 | |
| 91 | - return $this->exportError(acym_translation('ACYM_EXPORT_SELECT_LIST')); | |
| 94 | + $this->exportError(acym_translation('ACYM_EXPORT_SELECT_LIST')); | |
| 92 | 95 | } |
| 93 | 96 | acym_arrayToInteger($listsToExport); |
| 94 | 97 | |
| 95 | 98 | $exportUsersType = 'all'; |
| @@ -103,13 +106,14 @@ | ||
| 103 | 106 | $selectedUsersArray = explode(',', $selectedUsers); |
| 104 | 107 | acym_arrayToInteger($selectedUsersArray); |
| 105 | 108 | } |
| 106 | 109 | |
| 110 | + // Make sure the user selected fields and didn't inject something | |
| 107 | 111 | $fieldsToExport = acym_getVar('array', 'export_fields', []); |
| 108 | 112 | if (empty($fieldsToExport)) { |
| 109 | 113 | acym_setVar('elements_checked', empty($selectedUsersArray) ? [] : $selectedUsersArray); |
| 110 | 114 | |
| 111 | - return $this->exportError(acym_translation('ACYM_EXPORT_SELECT_FIELD')); | |
| 115 | + $this->exportError(acym_translation('ACYM_EXPORT_SELECT_FIELD')); | |
| 112 | 116 | } |
| 113 | 117 | |
| 114 | 118 | $tableFields = acym_getColumns('user'); |
| 115 | 119 | $fieldClass = new FieldClass(); |
| @@ -114,8 +118,12 @@ | ||
| 114 | 118 | $tableFields = acym_getColumns('user'); |
| 115 | 119 | $fieldClass = new FieldClass(); |
| 116 | 120 | $customFields = $fieldClass->getAll(); |
| 117 | 121 | |
| 122 | + $forbiddenFields = ['key', 'id']; | |
| 123 | + $fieldsToExport = array_diff($fieldsToExport, $forbiddenFields); | |
| 124 | + $tableFields = array_diff($tableFields, $forbiddenFields); | |
| 125 | + | |
| 118 | 126 | $customFieldsToExport = []; |
| 119 | 127 | $specialFieldsToExport = []; |
| 120 | 128 | foreach ($fieldsToExport as $i => $oneField) { |
| 121 | 129 | if (in_array($oneField, ['subscribe_date', 'unsubscribe_date'])) { |
| @@ -127,13 +135,10 @@ | ||
| 127 | 135 | } |
| 128 | 136 | } |
| 129 | 137 | |
| 130 | 138 | $notAllowedFields = array_diff($fieldsToExport, $tableFields); |
| 131 | - if (in_array('id', $fieldsToExport)) { | |
| 132 | - $notAllowedFields[] = 'id'; | |
| 133 | - } | |
| 134 | 139 | if (!empty($notAllowedFields)) { |
| 135 | - return $this->exportError(acym_translationSprintf('ACYM_NOT_ALLOWED_FIELDS', implode(', ', $notAllowedFields), implode(', ', $tableFields))); | |
| 140 | + $this->exportError(acym_translationSprintf('ACYM_NOT_ALLOWED_FIELDS', implode(', ', $notAllowedFields), implode(', ', $tableFields))); | |
| 136 | 141 | } |
| 137 | 142 | |
| 138 | 143 | $charset = acym_getVar('string', 'export_charset', 'UTF-8'); |
| 139 | 144 | $excelSecurity = acym_getVar('string', 'export_excelsecurity', 0); |
| @@ -142,19 +147,21 @@ | ||
| 142 | 147 | if (!in_array($separator, array_keys($realSeparators))) { |
| 143 | 148 | $separator = 'comma'; |
| 144 | 149 | } |
| 145 | 150 | |
| 146 | - | |
| 147 | - $newConfig = new \stdClass(); | |
| 148 | - $newConfig->export_separator = $separator; | |
| 149 | - $newConfig->export_charset = $charset; | |
| 150 | - $newConfig->export_excelsecurity = $excelSecurity; | |
| 151 | - $newConfig->export_fields = implode(',', array_merge($fieldsToExport, array_keys($customFieldsToExport))); | |
| 151 | + // Save the selected options for the next time | |
| 152 | + $newConfig = [ | |
| 153 | + 'export_separator' => $separator, | |
| 154 | + 'export_charset' => $charset, | |
| 155 | + 'export_excelsecurity' => $excelSecurity, | |
| 156 | + 'export_fields' => implode(',', array_merge($fieldsToExport, array_keys($customFieldsToExport))), | |
| 157 | + ]; | |
| 152 | 158 | if (empty($selectedUsers)) { |
| 153 | - $newConfig->export_lists = implode(',', $listsToExport); | |
| 159 | + $newConfig['export_lists'] = implode(',', $listsToExport); | |
| 154 | 160 | } |
| 155 | - $this->config->save($newConfig); | |
| 161 | + $this->config->saveConfig($newConfig); | |
| 156 | 162 | |
| 163 | + // Prepare the export query | |
| 157 | 164 | foreach ($fieldsToExport as $oneField) { |
| 158 | 165 | acym_secureDBColumn($oneField); |
| 159 | 166 | } |
| 160 | 167 | $query = 'SELECT DISTINCT user.`id`, user.`'.implode('`, user.`', $fieldsToExport).'` FROM #__acym_user AS user'; |
| @@ -230,18 +237,22 @@ | ||
| 230 | 237 | if (!empty($where)) { |
| 231 | 238 | $query .= ' WHERE ('.implode(') AND (', $where).')'; |
| 232 | 239 | } |
| 233 | 240 | |
| 241 | + // We have all we need for the export, prepare the headers for the download | |
| 234 | 242 | $exportHelper = new ExportHelper(); |
| 235 | - $exportHelper->exportCSV($query, $fieldsToExport, $customFieldsToExport, $specialFieldsToExport, $realSeparators[$separator], $charset, null, $flagSegment); | |
| 243 | + $exportHelper->exportCSV($query, $fieldsToExport, $customFieldsToExport, $specialFieldsToExport, $realSeparators[$separator], $charset, '', $flagSegment); | |
| 236 | 244 | |
| 237 | 245 | exit; |
| 238 | 246 | } |
| 239 | 247 | |
| 240 | - private function exportError($message) | |
| 248 | + /** | |
| 249 | + * Aborts export and displays an error message | |
| 250 | + */ | |
| 251 | + private function exportError(string $message): void | |
| 241 | 252 | { |
| 242 | 253 | acym_enqueueMessage($message, 'error', 0); |
| 243 | 254 | acym_setNoTemplate(false); |
| 244 | 255 | |
| 245 | - return acym_redirect(acym_completeLink('users&task=export', false, true)); | |
| 256 | + acym_redirect(acym_completeLink('users&task=export', false, true)); | |
| 246 | 257 | } |
| 247 | 258 | } |