-
Notifications
You must be signed in to change notification settings - Fork 0
/
Engine.php
executable file
·74 lines (64 loc) · 1.94 KB
/
Engine.php
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
<?php
namespace Acms\Plugins\ChatWork;
use Common;
use Field;
class Engine
{
/**
* @var \Field
*/
protected $config;
/**
* @var \ACMS_POST
*/
protected $module;
/**
* @var string
*/
protected $endpoint = 'https://api.chatwork.com/v2/rooms/%s/messages';
/**
* Engine constructor.
* @param string $code
* @param \ACMS_POST
*/
public function __construct($code, $module)
{
$info = $module->loadForm($code);
if (empty($info)) {
throw new \RuntimeException('Not Found Form.');
}
$this->config = $info['data']->getChild('mail');
$this->module = $module;
}
/**
* Send
*/
public function send()
{
$accessToken = $this->config->get('chatwork_form_token');
$messageTpl = $this->config->get('chatwork_form_message');
$room_id = $this->config->get('chatwork_form_room_id');
$body = Common::getMailTxtFromTxt($messageTpl, $this->module->Post->getChild('field'));
$headers = array(
'X-ChatWorkToken: '.$accessToken
);
$option = array(
'body' => $body
);
$ch = curl_init(sprintf($this->endpoint, $room_id));
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($option));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if (empty($response) || $status !== 200) {
throw new \RuntimeException("$status: $response");
}
}
}