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
acymailing / back / Controllers / Users / Export.php

Export.php in AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress 11.1.0, at back/Controllers/Users/Export.php

259 lines 10.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace AcyMailing\Controllers\Users;
4
5 use AcyMailing\Classes\FieldClass;
6 use AcyMailing\Classes\ListClass;
7 use AcyMailing\Classes\SegmentClass;
8 use AcyMailing\Classes\UserClass;
9 use AcyMailing\Controllers\SegmentsController;
10 use AcyMailing\Helpers\AutomationHelper;
11 use AcyMailing\Helpers\EncodingHelper;
12 use AcyMailing\Helpers\ExportHelper;
13
14 trait Export
15 {
16 /**
17 * Export page where the user selects the export option
18 */
19 public function export(): void
20 {
21 acym_setVar('layout', 'export');
22 $this->breadcrumb[acym_translation('ACYM_EXPORT_SUBSCRIBERS')] = acym_completeLink('users&task=export');
23
24 $listClass = new ListClass();
25 $lists = $listClass->getAll();
26
27 $preselectList = acym_getVar('boolean', 'preselectList', false);
28 $checkedElements = acym_getVar('array', 'elements_checked', []);
29
30 $filtersListing = [];
31
32 $filtersListing['list'] = $this->getVarFiltersListing('int', 'users_list', 0);
33 $filtersListing['list_status'] = $this->getVarFiltersListing('string', 'list_status', 'all');
34
35 $filtersSegment = $this->getVarFiltersListing('int', 'segment', 0);
36 $segmentClass = new SegmentClass();
37 $availableSegments = $segmentClass->getAllForSelect();
38
39 $list = acym_getVar('int', 'users_list', 0);
40 if (!empty($list)) {
41 $preselectList = true;
42 $checkedElements = [$list];
43 }
44
45 if (!empty($filtersListing['list'])) {
46 $preselectList = true;
47 $checkedElements = [$filtersListing['list']];
48 }
49
50 $fields = acym_getColumns('user');
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
56 $fieldClass = new FieldClass();
57 $customFields = $fieldClass->getAll();
58
59 $encodingHelper = new EncodingHelper();
60 $userClass = new UserClass();
61
62 $data = [
63 'lists' => $lists,
64 'checkedElements' => $checkedElements,
65 'fields' => $fields,
66 'customfields' => $customFields,
67 'coreFields' => [1, 2, $fieldClass->getLanguageFieldId()],
68 'isPreselectedList' => $preselectList,
69 'exportListStatus' => $filtersListing['list_status'],
70 'encodingHelper' => $encodingHelper,
71 'userClass' => $userClass,
72 'segments' => $availableSegments,
73 'preselectedSegment' => $filtersSegment,
74 ];
75
76 parent::display($data);
77 }
78
79 /**
80 * This method downloads the exported file directly
81 */
82 public function doexport(): void
83 {
84 wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token');
85 acym_increasePerf();
86
87 // Get passed data and check if we have everything we need
88 $usersToExport = acym_getVar('string', 'export_users-to-export', 'all');
89 $selectedLists = acym_getVar('string', 'acym__entity_select__selected', '[]');
90 $listsToExport = json_decode(empty($selectedLists) ? '[]' : $selectedLists, true);
91 if ($usersToExport === 'list' && empty($listsToExport)) {
92 acym_enqueueMessage(acym_translation('ACYM_EXPORT_SELECT_LIST'), 'error');
93
94 $this->exportError(acym_translation('ACYM_EXPORT_SELECT_LIST'));
95 }
96 acym_arrayToInteger($listsToExport);
97
98 $exportUsersType = 'all';
99 if ($usersToExport == 'list') {
100 $exportUsersType = acym_getVar('string', 'export_list', 'all');
101 }
102
103 $selectedUsers = acym_getVar('string', 'selected_users', null);
104
105 if (!empty($selectedUsers)) {
106 $selectedUsersArray = explode(',', $selectedUsers);
107 acym_arrayToInteger($selectedUsersArray);
108 }
109
110 // Make sure the user selected fields and didn't inject something
111 $fieldsToExport = acym_getVar('array', 'export_fields', []);
112 if (empty($fieldsToExport)) {
113 acym_setVar('elements_checked', empty($selectedUsersArray) ? [] : $selectedUsersArray);
114
115 $this->exportError(acym_translation('ACYM_EXPORT_SELECT_FIELD'));
116 }
117
118 $tableFields = acym_getColumns('user');
119 $fieldClass = new FieldClass();
120 $customFields = $fieldClass->getAll();
121
122 $forbiddenFields = ['key', 'id'];
123 $fieldsToExport = array_diff($fieldsToExport, $forbiddenFields);
124 $tableFields = array_diff($tableFields, $forbiddenFields);
125
126 $customFieldsToExport = [];
127 $specialFieldsToExport = [];
128 foreach ($fieldsToExport as $i => $oneField) {
129 if (in_array($oneField, ['subscribe_date', 'unsubscribe_date'])) {
130 $specialFieldsToExport[] = $oneField;
131 unset($fieldsToExport[$i]);
132 } elseif (!empty($customFields[$oneField])) {
133 $customFieldsToExport[$oneField] = $customFields[$oneField]->namekey;
134 unset($fieldsToExport[$i]);
135 }
136 }
137
138 $notAllowedFields = array_diff($fieldsToExport, $tableFields);
139 if (!empty($notAllowedFields)) {
140 $this->exportError(acym_translationSprintf('ACYM_NOT_ALLOWED_FIELDS', implode(', ', $notAllowedFields), implode(', ', $tableFields)));
141 }
142
143 $charset = acym_getVar('string', 'export_charset', 'UTF-8');
144 $excelSecurity = acym_getVar('string', 'export_excelsecurity', 0);
145 $separator = acym_getVar('string', 'export_separator', 'comma');
146 $realSeparators = ['comma' => ',', 'semicol' => ';'];
147 if (!in_array($separator, array_keys($realSeparators))) {
148 $separator = 'comma';
149 }
150
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 ];
158 if (empty($selectedUsers)) {
159 $newConfig['export_lists'] = implode(',', $listsToExport);
160 }
161 $this->config->saveConfig($newConfig);
162
163 // Prepare the export query
164 foreach ($fieldsToExport as $oneField) {
165 acym_secureDBColumn($oneField);
166 }
167 $query = 'SELECT DISTINCT user.`id`, user.`'.implode('`, user.`', $fieldsToExport).'` FROM #__acym_user AS user';
168
169 $where = [];
170 $flagSegment = 0;
171
172 if (!empty($selectedUsersArray)) {
173 acym_arrayToInteger($selectedUsersArray);
174 $where[] = 'user.id IN ('.implode(',', $selectedUsersArray).')';
175 } else {
176 if ($usersToExport == 'list' && !empty($listsToExport)) {
177 acym_arrayToInteger($listsToExport);
178
179 $listJoin = '#__acym_user_has_list AS userlist ON userlist.user_id = user.id AND userlist.list_id IN ('.implode(',', $listsToExport).')';
180
181 if ($exportUsersType == 'none') {
182 $query .= ' LEFT JOIN '.$listJoin;
183 $where[] = 'userlist.status IS NULL';
184 } else {
185 $query .= ' JOIN '.$listJoin;
186 }
187 if ($exportUsersType == 'sub') $where[] = 'userlist.status = 1';
188 if ($exportUsersType == 'unsub') $where[] = 'userlist.status = 0';
189 }
190
191 $segmentChosen = acym_getVar('string', 'export_segment', '');
192 if ('' !== $segmentChosen) {
193 $segmentClass = new SegmentClass();
194 $segment = $segmentClass->getOneById($segmentChosen);
195 if (!empty($segment)) {
196 $automationHelpers = [];
197 foreach ($segment->filters as $or => $orValues) {
198 if (empty($orValues)) continue;
199 $automationHelpers[$or] = new AutomationHelper();
200 foreach ($orValues as $and => $andValues) {
201 $and = intval($and);
202 foreach ($andValues as $filterName => $options) {
203 acym_trigger('onAcymProcessFilter_'.$filterName, [&$automationHelpers[$or], &$options, $and.'_'.$or]);
204 }
205 }
206 }
207 $flagSegment = SegmentsController::FLAG_EXPORT_USERS;
208 foreach ($automationHelpers as $automationHelper) {
209 $automationHelper->addFlag($flagSegment);
210 }
211
212 $where[] = 'user.automation LIKE "%a'.intval($flagSegment).'a%"';
213 }
214 }
215 }
216
217 $filtersListingSearch = $this->getVarFiltersListing('string', 'users_search', '');
218 $filtersListingStatus = $this->getVarFiltersListing('string', 'users_status', '');
219
220 if (!empty($filtersListingSearch)) {
221 $search = acym_escapeDB('%'.$filtersListingSearch.'%');
222 $where[] = 'user.name LIKE '.$search.' OR user.email LIKE '.$search.' OR user.id LIKE '.$search;
223 }
224
225 if (!empty($filtersListingStatus)) {
226 if ($filtersListingStatus == 'active') {
227 $where[] = 'user.active = 1';
228 } elseif ($filtersListingStatus == 'inactive') {
229 $where[] = 'user.active = 0';
230 } elseif ($filtersListingStatus == 'confirmed') {
231 $where[] = 'user.confirmed = 1';
232 } else {
233 $where[] = 'user.confirmed = 0';
234 }
235 }
236
237 if (!empty($where)) {
238 $query .= ' WHERE ('.implode(') AND (', $where).')';
239 }
240
241 // We have all we need for the export, prepare the headers for the download
242 $exportHelper = new ExportHelper();
243 $exportHelper->exportCSV($query, $fieldsToExport, $customFieldsToExport, $specialFieldsToExport, $realSeparators[$separator], $charset, '', $flagSegment);
244
245 exit;
246 }
247
248 /**
249 * Aborts export and displays an error message
250 */
251 private function exportError(string $message): void
252 {
253 acym_enqueueMessage($message, 'error', 0);
254 acym_setNoTemplate(false);
255
256 acym_redirect(acym_completeLink('users&task=export', false, true));
257 }
258 }
259