|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* MikoPBX - free phone system for small business |
|
4
|
|
|
* Copyright (C) 2017-2020 Alexey Portnov and Nikolay Beketov |
|
5
|
|
|
* |
|
6
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
7
|
|
|
* it under the terms of the GNU General Public License as published by |
|
8
|
|
|
* the Free Software Foundation; either version 3 of the License, or |
|
9
|
|
|
* (at your option) any later version. |
|
10
|
|
|
* |
|
11
|
|
|
* This program is distributed in the hope that it will be useful, |
|
12
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14
|
|
|
* GNU General Public License for more details. |
|
15
|
|
|
* |
|
16
|
|
|
* You should have received a copy of the GNU General Public License along with this program. |
|
17
|
|
|
* If not, see <https://www.gnu.org/licenses/>. |
|
18
|
|
|
*/ |
|
19
|
|
|
|
|
20
|
|
|
namespace MikoPBX\Core\Workers; |
|
21
|
|
|
require_once 'Globals.php'; |
|
22
|
|
|
use MikoPBX\Core\System\{BeanstalkClient, MikoPBXConfig, Notifications, Util}; |
|
23
|
|
|
use Throwable; |
|
24
|
|
|
|
|
25
|
|
|
|
|
26
|
|
|
class WorkerNotifyByEmail extends WorkerBase |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* Entry point |
|
30
|
|
|
* |
|
31
|
|
|
* @param $argv |
|
32
|
|
|
*/ |
|
33
|
|
|
public function start($argv): void |
|
34
|
|
|
{ |
|
35
|
|
|
$client = new BeanstalkClient(__CLASS__); |
|
36
|
|
|
$client->subscribe(__CLASS__, [$this, 'workerNotifyByEmail']); |
|
37
|
|
|
$client->subscribe($this->makePingTubeName(self::class), [$this, 'pingCallBack']); |
|
38
|
|
|
|
|
39
|
|
|
while ($this->needRestart === false) { |
|
40
|
|
|
$client->wait(); |
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
private function replaceParams(string $src, array $params):string |
|
45
|
|
|
{ |
|
46
|
|
|
return str_replace( |
|
47
|
|
|
[ |
|
48
|
|
|
"\n", |
|
49
|
|
|
"NOTIFICATION_MISSEDCAUSE", |
|
50
|
|
|
"NOTIFICATION_CALLERID", |
|
51
|
|
|
"NOTIFICATION_TO", |
|
52
|
|
|
"NOTIFICATION_DURATION", |
|
53
|
|
|
"NOTIFICATION_DATE" |
|
54
|
|
|
], |
|
55
|
|
|
[ |
|
56
|
|
|
"<br>", |
|
57
|
|
|
'NOANSWER', |
|
58
|
|
|
$params['from_number'], |
|
59
|
|
|
$params['to_number'], |
|
60
|
|
|
$params['duration'], |
|
61
|
|
|
explode('.', $params['start'])[0] |
|
62
|
|
|
], |
|
63
|
|
|
$src); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Main worker |
|
68
|
|
|
* @param $message |
|
69
|
|
|
*/ |
|
70
|
|
|
public function workerNotifyByEmail($message): void |
|
71
|
|
|
{ |
|
72
|
|
|
$notifier = new Notifications(); |
|
73
|
|
|
$config = new MikoPBXConfig(); |
|
74
|
|
|
$settings = $config->getGeneralSettings(); |
|
75
|
|
|
|
|
76
|
|
|
/** @var BeanstalkClient $message */ |
|
77
|
|
|
$data = json_decode($message->getBody(), true); |
|
78
|
|
|
|
|
79
|
|
|
$template_body = $settings['MailTplMissedCallBody']; |
|
80
|
|
|
$template_subject = $settings['MailTplMissedCallSubject']; |
|
81
|
|
|
if(empty($template_subject)){ |
|
82
|
|
|
$template_subject = Util::translate("You have missing call") . ' <-- NOTIFICATION_CALLERID'; |
|
83
|
|
|
} |
|
84
|
|
|
$template_Footer = $settings['MailTplMissedCallFooter']; |
|
85
|
|
|
$emails = []; |
|
86
|
|
|
|
|
87
|
|
|
$tmpArray = []; |
|
88
|
|
|
foreach ($data as $call) { |
|
89
|
|
|
$keyHash = $call['email'].$call['start'].$call['from_number'].$call['to_number']; |
|
90
|
|
|
if(in_array($keyHash, $tmpArray, true)){ |
|
91
|
|
|
continue; |
|
92
|
|
|
} |
|
93
|
|
|
$tmpArray[] = $keyHash; |
|
94
|
|
|
if (!isset($emails[$call['email']])) { |
|
95
|
|
|
$emails[$call['email']] = [ |
|
96
|
|
|
'subject' => $this->replaceParams($template_subject, $call), |
|
97
|
|
|
'body' => '', |
|
98
|
|
|
'footer' => $this->replaceParams($template_Footer, $call), |
|
99
|
|
|
]; |
|
100
|
|
|
} |
|
101
|
|
|
if (!empty($template_body)) { |
|
102
|
|
|
$email = $this->replaceParams($template_body, $call); |
|
103
|
|
|
$emails[$call['email']]['body'] .= "$email <br><hr><br>"; |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
foreach ($emails as $to => $email) { |
|
108
|
|
|
$subject = $email['subject']; |
|
109
|
|
|
$body = "{$email['body']}<br>{$email['footer']}"; |
|
110
|
|
|
$notifier->sendMail($to, $subject, $body); |
|
111
|
|
|
} |
|
112
|
|
|
sleep(1); |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
// Start worker process |
|
117
|
|
|
WorkerNotifyByEmail::startWorker($argv??null); |