-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRocketchatModule.class.php
More file actions
129 lines (111 loc) · 4 KB
/
Copy pathRocketchatModule.class.php
File metadata and controls
129 lines (111 loc) · 4 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
<?php
/**
* @Author: Anton Baranov
* @Author: Dmitrij Omelchuk
* @Last Modified by: Anton Baranov
* @Last Modified time: 2020-05-13 00:56:53
*/
class RocketChat {
private $api_token;
private $api_user;
private $api_endpoint;
function __construct($api_url, $api_token, $api_user){
$this->api_endpoint = $api_url. '/api/v1';
$this->api_token = $api_token;
$this->api_user = $api_user;
}
public function call_get($method, $args = array(), $timeout = 10){
return $this->request_get($method, $args, $timeout);
}
public function call($method, $args = array(), $timeout = 10){
return $this->request($method, $args, $timeout);
}
private function request($method, $args = array(), $timeout = 10){
$url = "{$this->api_endpoint}/{$method}";
$headers = array();
$headers[] = "Content-type: application/json";
$headers[] = "X-Auth-Token: " . $this->api_token;
$headers[] = "X-User-Id: " . $this->api_user;
if (function_exists('curl_version')){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($args));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
curl_close($ch);
} else {
$post_data = http_build_query($args);
$result = file_get_contents($url, false, stream_context_create(array(
'http' => array(
'protocol_version' => 1.1,
'method' => 'POST',
'header' => $headers,
'data' => json_encode($post_data)
),
)));
}
return $result ? json_decode($result, true) : false;
}
private function request_get($method, $args = array(), $timeout = 10){
$url = "{$this->api_endpoint}/{$method}";
$headers = array();
$headers[] = "Content-type: application/json";
$headers[] = "X-Auth-Token: " . $this->api_token;
$headers[] = "X-User-Id: " . $this->api_user;
if (function_exists('curl_version')){
$ch = curl_init();
// $url = $url . '?' . http_build_query($args);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_HTTPGET, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($args));
$result = curl_exec($ch);
curl_close($ch);
} else {
$post_data = http_build_query($args);
$result = file_get_contents($url, false, stream_context_create(array(
'http' => array(
'protocol_version' => 1.1,
'method' => 'GET',
'header' => $headers,
'data' => json_encode($post_data)
),
)));
}
return $result ? json_decode($result, true) : false;
}
}
class RocketChatModule extends AngieModule {
protected $name = 'rocketchat';
protected $version = '1.0.2';
var $is_system = false;
function defineRoutes() {
}
function defineHandlers() {
/*
EventsManager::listen('on_project_created', 'on_project_created');
EventsManager::listen('on_project_deleted', 'on_project_deleted');
*/
EventsManager::listen('on_object_inserted', 'on_object_inserted');
EventsManager::listen('on_object_updated', 'on_object_updated');
EventsManager::listen('on_object_deleted', 'on_object_deleted');
EventsManager::listen('on_object_opened', 'on_object_opened');
EventsManager::listen('on_object_completed', 'on_object_completed');
}
function getDisplayName() {
return lang('RocketChat');
}
function getDescription() {
return lang('Display notifications in Rocketchat.');
}
function getUninstallMessage() {
return lang('Rocketchat will be deactivated. Are you sure?');
}
}