PluginProbe
AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress / 11.1.0
AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress v11.1.0
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
← All changes | back/Controllers/Users/Export.php +34 -22 10.2.2 → 11.1.0 View file →
@@ -8,13 +8,15 @@
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 {
16 + /**
17 + * Export page where the user selects the export option
18 + */
17 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');
@@ -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,16 +75,21 @@
77 75
78 76 parent::display($data);
79 77 }
80 78
79 + /**
80 + * This method downloads the exported file directly
81 + */
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 94 $this->exportError(acym_translation('ACYM_EXPORT_SELECT_LIST'));
92 95 }
@@ -103,8 +106,9 @@
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
@@ -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,11 +135,8 @@
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 140 $this->exportError(acym_translationSprintf('ACYM_NOT_ALLOWED_FIELDS', implode(', ', $notAllowedFields), implode(', ', $tableFields)));
136 141 }
137 142
@@ -142,18 +147,21 @@
142 147 if (!in_array($separator, array_keys($realSeparators))) {
143 148 $separator = 'comma';
144 149 }
145 150
146 - $newConfig = new \stdClass();
147 - $newConfig->export_separator = $separator;
148 - $newConfig->export_charset = $charset;
149 - $newConfig->export_excelsecurity = $excelSecurity;
150 - $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 + ];
151 158 if (empty($selectedUsers)) {
152 - $newConfig->export_lists = implode(',', $listsToExport);
159 + $newConfig['export_lists'] = implode(',', $listsToExport);
153 160 }
154 - $this->config->save($newConfig);
161 + $this->config->saveConfig($newConfig);
155 162
163 + // Prepare the export query
156 164 foreach ($fieldsToExport as $oneField) {
157 165 acym_secureDBColumn($oneField);
158 166 }
159 167 $query = 'SELECT DISTINCT user.`id`, user.`'.implode('`, user.`', $fieldsToExport).'` FROM #__acym_user AS user';
@@ -229,8 +237,9 @@
229 237 if (!empty($where)) {
230 238 $query .= ' WHERE ('.implode(') AND (', $where).')';
231 239 }
232 240
241 + // We have all we need for the export, prepare the headers for the download
233 242 $exportHelper = new ExportHelper();
234 243 $exportHelper->exportCSV($query, $fieldsToExport, $customFieldsToExport, $specialFieldsToExport, $realSeparators[$separator], $charset, '', $flagSegment);
235 244
236 245 exit;
@@ -235,8 +244,11 @@
235 244
236 245 exit;
237 246 }
238 247
248 + /**
249 + * Aborts export and displays an error message
250 + */
239 251 private function exportError(string $message): void
240 252 {
241 253 acym_enqueueMessage($message, 'error', 0);
242 254 acym_setNoTemplate(false);