forked from bsakhanov/mod_callback_mail
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelper.php
More file actions
205 lines (176 loc) · 7.19 KB
/
helper.php
File metadata and controls
205 lines (176 loc) · 7.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<?php defined('_JEXEC') or die;
/*
* @package mod_callback_mail
* @copyright Copyright (C) 2019 Aleksey A. Morozov (AlekVolsk). All rights reserved.
* @license GNU General Public License version 3 or later; see http://www.gnu.org/licenses/gpl-3.0.txt
*/
use Joomla\CMS\Factory;
use Joomla\Registry\Registry;
use Joomla\CMS\Filter\OutputFilter;
use Joomla\CMS\Uri\Uri;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Helper\ModuleHelper;
use Joomla\CMS\Session\Session;
use Joomla\CMS\Log\Log;
class ModCallbackMailHelper
{
static public function getFields($params)
{
$items = (array)$params->get('items');
foreach ($items as $key => $item) {
$items[$key]->fname = OutputFilter::stringURLSafe($item->fname);
}
return $items;
}
static protected function checkForm()
{
$rsl = explode(':', filter_input(INPUT_POST, 'rsl', FILTER_SANITIZE_STRING));
$rst = filter_input(INPUT_POST, 'rst', FILTER_SANITIZE_STRING);
$base = Uri::base();
return
!empty($_POST) && (
($_SERVER['HTTP_REFERER'] == $base || $_SERVER['HTTP_REFERER'] == $rst) &&
(isset($_SERVER['HTTP_X_REQUESTED_WITH']) ? $_SERVER['HTTP_X_REQUESTED_WITH'] == XMLHttpRequest : true) &&
(count($rsl) === 2 && (int)$rsl[0] > 0 && (int)$rsl[1] > 0)
);
}
static protected function getLayoutPath($extension, $layout = 'default')
{
$template = Factory::getApplication()->getTemplate();
$defaultLayout = $layout;
if (strpos($layout, ':') !== false) {
$temp = explode(':', $layout);
$template = $temp[0] === '_' ? $template : $temp[0];
$layout = $temp[1];
$defaultLayout = $temp[1] ?: 'default';
}
$tPath = JPATH_THEMES . '/' . $template . '/html/layouts/' . $extension . '/' . $layout . '.php';
$bPath = JPATH_BASE . '/modules/' . $extension . '/layouts/' . $defaultLayout . '.php';
$dPath = JPATH_BASE . '/modules/' . $extension . '/layouts/default.php';
if (file_exists($tPath)) {
return $tPath;
}
if (file_exists($bPath)) {
return $bPath;
}
return $dPath;
}
static private function printJson($message, $result = false, $custom = [])
{
if (empty($message)) {
$message = '< empty message >';
}
$jsonData = ['result' => $result, 'message' => $message];
foreach ($custom as $key => $value) {
$jsonData[$key] = $value;
}
echo json_encode($jsonData);
exit;
}
static public function getAjax()
{
$extension = 'mod_callback_mail';
Log::addLogger(['text_file' => $extension . '.php', 'text_entry_format' => '{DATETIME} {PRIORITY} {MESSAGE}'], Log::ALL);
if (!Session::checkToken()) {
$data = [
'referer' => $_SERVER['HTTP_REFERER'],
'result' => 'Invalid token'
];
Log::add(json_encode($data), Log::ERROR);
self::printJson(Text::_('JINVALID_TOKEN'));
}
if (!self::checkForm()) {
$data = [
'referer' => $_SERVER['HTTP_REFERER'],
'result' => 'Invalid checkform'
];
Log::add(json_encode($data), Log::ERROR);
self::printJson(Text::_('JINVALID_TOKEN'));
}
$moduleHash = explode(':', filter_input(INPUT_POST, 'mh', FILTER_SANITIZE_STRING));
if (!$moduleHash || !$moduleHash[0] || !$moduleHash[1]) {
$data = [
'referer' => $_SERVER['HTTP_REFERER'],
'result' => 'Empty form hash'
];
Log::add(json_encode($data), Log::ERROR);
self::printJson(Text::_('JINVALID_TOKEN'));
}
$session = Factory::getSession();
$sessionHash = $session->get('mod_callback_mail_' . $moduleHash[0], '');
if ($sessionHash !== $moduleHash[1])
{
$data = [
'referer' => $_SERVER['HTTP_REFERER'],
'result' => 'Invalid form hash'
];
Log::add(json_encode($data), Log::ERROR);
self::printJson(Text::_('JINVALID_TOKEN'));
}
$language = Factory::getLanguage();
$language->load($extension, JPATH_BASE, null, true);
$module = ModuleHelper::getModuleById($moduleHash[0]);
if (!$module->id)
{
$data = [
'referer' => $_SERVER['HTTP_REFERER'],
'result' => 'Empty module params'
];
Log::add(json_encode($data), Log::ERROR);
self::printJson(Text::_('JINVALID_TOKEN'));
}
$params = new Registry();
$params->loadString($module->params);
$fields = self::getFields($params);
$sender_type = $params->get('sender_type', 1);
$data = [
'title' => trim($params->get('mailtitle', Text::sprintf('MOD_CALLBACK_MAIL_PRM_MAILTITLE_DEFAULT', Uri::base()))),
'prependText' => explode("\n", trim($params->get('premsg', ''))),
'appendText' => explode("\n", trim($params->get('postmsg', ''))),
'items' => [],
];
$c = count($fields);
for ($i = 0; $i < $c; $i++) {
$val = trim(filter_input(INPUT_POST, $fields['items' . $i]->fname, FILTER_SANITIZE_STRING));
if ($fields['items' . $i]->ftype == 'checkbox') {
$val = $val ? Text::_('JYES') : Text::_('JNO');
}
$item = new StdClass();
$item->name = $fields['items' . $i]->ftitle;
$item->value = $val;
$data['items'][] = $item;
}
unset($item);
$layout = str_replace('\\', '/', self::getLayoutPath($extension, $params->get('msgtemplate')));
ob_start();
include $layout;
$out = ob_get_clean();
$data['referer'] = $_SERVER['HTTP_REFERER'];
$data['out'] = $out;
unset($data['prependText'], $data['appendText'], $data['items']);
$config = Factory::getConfig();
$sender_mail = $sender_type ? $config->get('mailfrom') : $params->get('sender_mail');
$sender_name = $sender_type ? $config->get('fromname') : $params->get('sender_name');
$mailer = Factory::getMailer();
$mailer->setSender([$sender_mail, $sender_name]);
$mailer->addRecipient($params->get('recipient_mail'), $params->get('recipient_name'));
$mailer->isHtml(true);
$mailer->encoding = 'base64';
$mailer->setSubject($data['title']);
$mailer->setBody($out);
$send = $mailer->send();
if ($send === false) {
$data['result'] = false;
Log::add(json_encode($data), Log::ERROR);
self::printJson(Text::_('MOD_CALLBACK_MAIL_SUBMIT_FAILED_MSG'));
} elseif ($send !== true) {
$data['result'] = $send->getMessage();
Log::add(json_encode($data), Log::ERROR);
self::printJson(Text::_('MOD_CALLBACK_MAIL_SUBMIT_FAILED_MSG'));
} else {
$data['result'] = true;
Log::add(json_encode($data), Log::INFO);
self::printJson(Text::_('MOD_CALLBACK_MAIL_SUBMIT_SUCCESSFULLY_MSG'), true);
}
}
}