PluginProbe
AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress / trunk
AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress vtrunk
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 / Shared / EditorActions.php

EditorActions.php in AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress trunk, at back/Controllers/Shared/EditorActions.php

288 lines 11.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\Shared;
4
5 use AcyMailing\Classes\CampaignClass;
6 use AcyMailing\Classes\MailClass;
7 use AcyMailing\Helpers\MailerHelper;
8
9 trait EditorActions
10 {
11 public function autoSave(): void
12 {
13 wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token');
14
15 $mailClass = new MailClass();
16 $mail = new \stdClass();
17
18 $language = acym_getVar('string', 'language', 'main');
19 $mail->id = acym_getVar('int', 'mailId', 0);
20 $mail->autosave = base64_decode(acym_getVar('string', 'autoSave', '', 'REQUEST', ACYM_ALLOWRAW));
21
22 if (empty($mail->id) || !$mailClass->hasUserAccess($mail->id, true) || !$mailClass->autoSave($mail, $language)) {
23 acym_sendAjaxResponse('', [], false);
24 } else {
25 acym_sendAjaxResponse();
26 }
27 }
28
29 public function ajaxCheckVideoUrl(): void
30 {
31 wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token');
32 $videoUrl = acym_getVar('string', 'url', '');
33
34 if (!acym_isValidUrl($videoUrl)) {
35 acym_sendAjaxResponse('', [], false);
36 }
37
38 $image = '';
39 $imageName = '';
40
41 $youtubeMatch = '';
42 $vimeoMatch = '';
43 $dailymotionMatch = '';
44
45 preg_match('/(?:https?:\/{2})?(?:w{3}\.)?youtu(?:be)?\.(?:com|be)(?:\/watch\?v=|\/)([^\s&?\/]+)/', $videoUrl, $youtubeMatch);
46 preg_match('/^.*(vimeo\.com\/)((channels\/[A-z]+\/)|(groups\/[A-z]+\/videos\/))?([0-9]+)/', $videoUrl, $vimeoMatch);
47 preg_match('/^(?:(?:http|https):\/\/)?(?:www.)?(dailymotion\.com|dai\.ly)\/((video\/([^_?]+))|(hub\/([^_?]+)|([^\/_?]+)))(?:\?.*)?$/', $videoUrl, $dailymotionMatch);
48
49 if (!empty($youtubeMatch)) {
50 $image = 'https://img.youtube.com/vi/'.$youtubeMatch[1].'/0.jpg';
51 $imageName = $youtubeMatch[1];
52 } elseif (!empty($dailymotionMatch)) {
53 $dailymotionImage = $dailymotionMatch[4] ?? $dailymotionMatch[2];
54
55 $image = 'https://www.dailymotion.com/thumbnail/video/'.$dailymotionImage;
56 $imageName = $dailymotionMatch[2];
57 } elseif (!empty($vimeoMatch)) {
58 $image = unserialize(file_get_contents('https://vimeo.com/api/v2/video/'.$vimeoMatch[5].'.php'));
59 $image = $image[0]['thumbnail_large'];
60 $imageName = $vimeoMatch[5];
61 }
62
63 if (empty($image) || !acym_isValidUrl($image)) {
64 acym_sendAjaxResponse('', [], false);
65 }
66
67 acym_sendAjaxResponse('', ['new_image_name' => $this->saveVideoPreview($image, urlencode($imageName).'.jpg')]);
68 }
69
70 private function saveVideoPreview(string $image, string $fileName): string
71 {
72 if (!$this->config->get('add_play_button_video', 1)) {
73 return $image;
74 }
75
76 $imageVideo = imagecreatefromjpeg($image);
77 $playButton = @imagecreatefrompng(ACYM_ROOT.ACYM_MEDIA_FOLDER.'images'.DS.'editor'.DS.'video_insertion'.DS.'play_button.png');
78 if ($playButton === false || $imageVideo === false) {
79 return $image;
80 }
81 $imageWidth = imagesx($imageVideo);
82 $imageHeight = imagesy($imageVideo);
83 $logoWidth = imagesx($playButton);
84 $logoHeight = imagesy($playButton);
85
86 $left = round(($imageWidth - $logoWidth) / 2);
87 $top = round(($imageHeight - $logoHeight) / 2);
88 imagecopy($imageVideo, $playButton, $left, $top, 0, 0, $logoWidth, $logoHeight);
89 $tmpFilePath = ACYM_TMP_FOLDER.'tmp.jpg';
90 acym_createFolder(ACYM_TMP_FOLDER);
91 imagepng($imageVideo, $tmpFilePath, 9);
92 $input = imagecreatefrompng($tmpFilePath);
93 $output = imagecreatetruecolor($imageWidth, $imageHeight);
94 $white = imagecolorallocate($output, 255, 255, 255);
95
96 imagefilledrectangle($output, 0, 0, $imageWidth, $imageHeight, $white);
97 imagecopy($output, $input, 0, 0, 0, 0, $imageWidth, $imageHeight);
98
99 ob_start();
100 $status = imagejpeg($output, null, 95);
101 $imageContent = ob_get_clean();
102 if ($status && acym_writeFile(ACYM_ROOT.ACYM_UPLOAD_FOLDER.$fileName, $imageContent)) {
103 acym_deleteFile($tmpFilePath);
104
105 return ACYM_UPLOADS_URL.$fileName;
106 }
107
108 acym_deleteFile($tmpFilePath);
109
110 return '';
111 }
112
113 public function setNewThumbnail(): void
114 {
115 if (!acym_isAdmin() || (!acym_isAllowed('mails') && !acym_isAllowed('campaigns'))) {
116 die('Access denied for thumbnail creation');
117 }
118
119 wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token');
120 $contentThumbnail = acym_getVar('string', 'content', '');
121 if (strpos($contentThumbnail, 'data:image/png') !== 0) {
122 acym_sendAjaxResponse('This file is not allowed.', [], false);
123 }
124
125 $mailId = acym_getVar('int', 'mailId', 0);
126 if (!empty($mailId)) {
127 $mailClass = new MailClass();
128 $mail = $mailClass->getOneById($mailId);
129 if (!empty($mail)) {
130 $file = $mail->thumbnail;
131 }
132 }
133
134 if (empty($file) || strpos($file, 'http') === 0) {
135 $thumbNb = $this->config->get('numberThumbnail', 2);
136 $file = 'thumbnail_'.($thumbNb + 1).'.png';
137 $this->config->saveConfig(['numberThumbnail' => $thumbNb + 1]);
138 }
139
140 $extension = acym_fileGetExt($file);
141 if (strpos($file, 'thumbnail_') === false || !in_array($extension, ['png', 'jpeg', 'jpg', 'gif', 'webp'])) {
142 acym_sendAjaxResponse('This file is not allowed.', [], false);
143 }
144
145 acym_createFolder(ACYM_UPLOAD_FOLDER_THUMBNAIL);
146 file_put_contents(ACYM_UPLOAD_FOLDER_THUMBNAIL.$file, base64_decode(preg_replace('#^data:image/\w+;base64,#i', '', $contentThumbnail)));
147
148 acym_sendAjaxResponse('', ['fileName' => $file]);
149 }
150
151 public function setNewIconShare(): void
152 {
153 wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token');
154 $socialName = acym_getVar('string', 'social', '');
155 $socialMedias = acym_getSocialMedias();
156 if (!in_array($socialName, $socialMedias)) {
157 acym_sendAjaxResponse(acym_translationSprintf('ACYM_UNKNOWN_SOCIAL', $socialName), [], false);
158 }
159
160 $file = acym_getVar('array', 'file', [], 'FILES');
161 $extension = pathinfo($file['name'], PATHINFO_EXTENSION);
162 $newPath = ACYM_UPLOAD_FOLDER.'socials'.DS.$socialName;
163 $newPathComplete = $newPath.'.'.$extension;
164
165 $allowedExtensions = acym_getImageFileExtensions(true);
166 if (!in_array($extension, $allowedExtensions)) {
167 $errorMessage = acym_translationSprintf('ACYM_ACCEPTED_TYPE', esc_html($extension), implode(', ', $allowedExtensions));
168 } elseif (!acym_uploadFile($file['tmp_name'], ACYM_ROOT.$newPathComplete)) {
169 $errorMessage = acym_translationSprintf('ACYM_ERROR_UPLOADING_FILE_X', $newPathComplete);
170 } elseif (strtolower($extension) === 'svg' && !acym_isSvgFileSafe(ACYM_ROOT.$newPathComplete)) {
171 acym_deleteFile(ACYM_ROOT.$newPathComplete);
172 $errorMessage = acym_translationSprintf('ACYM_ERROR_UPLOADING_FILE_X', $newPathComplete);
173 }
174
175 if (!empty($errorMessage)) {
176 acym_sendAjaxResponse($errorMessage, [], false);
177 }
178
179 $newImg = acym_rootURI().$newPathComplete;
180 $newImgWithoutExtension = acym_rootURI().$newPath;
181
182 $socialIcons = json_decode($this->config->get('social_icons', '{}'), true);
183 $socialIcons[$socialName] = $newImg;
184 $this->config->saveConfig(['social_icons' => json_encode($socialIcons)]);
185
186 acym_sendAjaxResponse(
187 acym_translation('ACYM_ICON_IMPORTED'),
188 [
189 'url' => $newImgWithoutExtension,
190 'extension' => $extension,
191 ]
192 );
193 }
194
195 public function sendTest(): void
196 {
197 wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token');
198 $controller = acym_getVar('string', 'controller', 'mails');
199 $level = 'info';
200
201 $testNote = acym_getVar('string', 'test_note', '');
202 $mailClass = new MailClass();
203 $mailerHelper = new MailerHelper();
204 $mailerHelper->autoAddUser = true;
205 $mailerHelper->report = false;
206
207 if (in_array($controller, ['mails', 'frontmails'])) {
208 $mailId = acym_getVar('int', 'id', 0);
209 } else {
210 $campaignId = acym_getVar('int', 'id', 0);
211 $campaignClass = new CampaignClass();
212 $campaign = $campaignClass->getOneById($campaignId);
213 if (empty($campaign)) {
214 acym_sendAjaxResponse('', ['level' => 'error', 'message' => acym_translation('ACYM_CAMPAIGN_NOT_FOUND')], false);
215 }
216 if (!$campaignClass->hasUserAccess($campaignId)) {
217 die('A test of this campaign cannot be sent');
218 }
219
220 $mailId = $campaign->mail_id;
221 $mailerHelper->isAbTest = $campaignClass->isAbTestMail($mailId);
222
223 $campaignVersion = acym_getVar('string', 'lang_version', 'main');
224 if (!empty($campaignVersion) && $campaignVersion !== 'main') {
225 if ($campaignVersion === 'B') {
226 $versions = $mailClass->getVersionsById($mailId);
227 if (!empty($versions)) {
228 $mailIds = array_keys($versions);
229 $mailId = array_pop($mailIds);
230 }
231 } else {
232 $translationId = $mailClass->getTranslationId($mailId, $campaignVersion);
233 if (!empty($translationId)) {
234 $mailId = $translationId;
235 }
236 }
237 }
238 }
239
240 $mail = $mailClass->getOneById($mailId);
241
242 if (empty($mail) || !$mailClass->hasUserAccess($mailId)) {
243 acym_sendAjaxResponse('', ['level' => 'error', 'message' => acym_translation('ACYM_EMAIL_NOT_FOUND')], false);
244 }
245
246 $report = [];
247
248 $testEmails = explode(',', acym_getVar('string', 'test_emails'));
249 $options = [
250 'isTest' => true,
251 'testNote' => $testNote,
252 ];
253 foreach ($testEmails as $oneAddress) {
254 if (!$mailerHelper->sendOne($mail->id, $oneAddress, $options)) {
255 $level = 'error';
256 }
257
258 if (!empty($mailerHelper->reportMessage)) {
259 $report[] = $mailerHelper->reportMessage;
260 }
261 }
262
263 acym_sendAjaxResponse('', ['level' => $level, 'message' => implode('<br/>', $report)], $level !== 'error');
264 }
265
266 public function getMailContent(): void
267 {
268 $from = acym_getVar('int', 'from', 0);
269
270 if (empty($from)) {
271 acym_sendAjaxResponse(acym_translation('ACYM_EMAIL_NOT_FOUND'), [], false);
272 }
273
274 $mailClass = new MailClass();
275 $mail = $mailClass->getOneById($from);
276
277 if (empty($mail)) {
278 acym_sendAjaxResponse(acym_translation('ACYM_EMAIL_NOT_FOUND'), [], false);
279 }
280
281 if ($mail->drag_editor == 0) {
282 acym_sendAjaxResponse('Only available for the drag and drop editor.', [], false);
283 }
284
285 acym_sendAjaxResponse();
286 }
287 }
288