-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzenci_webhook.module
175 lines (145 loc) · 4.94 KB
/
zenci_webhook.module
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
<?php
/**
* @file
* Webhook.
*
* React on webhook calls.
*/
// URL ZenCI API.
define('ZENCI_API_URI', 'http://git.lc/api');
define('ZENCI_WEBHOOK_PAYLOAD_PATH', 'webhook/config');
/**
* Implements hook_config_info().
*/
function zenci_webhook_config_info() {
$prefixes['zenci_webhook.settings'] = array(
'label' => t('Webhook settings'),
'group' => t('Configuration'),
);
return $prefixes;
}
/**
* Implements hook_menu().
*/
function zenci_webhook_menu() {
$items = array();
$items['admin/config/system/zenci_webhook'] = array(
'title' => 'zenci Webhook settings',
'description' => 'Webhook settings to integrate with ZenCI.',
'page callback' => 'zenci_webhook_admin_list',
'access callback' => 'user_access',
'access arguments' => array('administer site configuration'),
'file' => 'zenci_webhook.pages.inc',
'type' => MENU_NORMAL_ITEM,
);
$items['admin/config/system/zenci_webhook/list'] = array(
'title' => 'zenci hooked repositories',
'type' => MENU_DEFAULT_LOCAL_TASK,
);
// Admin settings page.
$items['admin/config/system/zenci_webhook/settings'] = array(
'title' => 'zenci Webhook settings',
'description' => 'Webhook settings to integrate with ZenCI.',
'page callback' => 'backdrop_get_form',
'page arguments' => array('zenci_webhook_admin_settings'),
'access arguments' => array('administer site configuration'),
'type' => MENU_LOCAL_TASK,
'file' => 'zenci_webhook.admin.inc',
'weight' => 10,
);
$items['admin/config/system/zenci_webhook/%/delete'] = array(
'title' => 'Remove hook',
'page callback' => 'backdrop_get_form',
'page arguments' => array('zenci_webhook_hook_remove', 4),
'access callback' => 'user_access',
'access arguments' => array('administer site configuration'),
'file' => 'zenci_webhook.pages.inc',
'type' => MENU_CALLBACK,
);
$items['webhook/%'] = array(
'type' => MENU_CALLBACK,
'title' => 'Webhook',
'page callback' => 'zenci_webhook_ajax',
'access callback' => TRUE,
'page arguments' => array(1),
'delivery callback' => 'zenci_webhook_ajax_deliver',
);
return $items;
}
function zenci_webhook_ajax($method) {
$received_json = file_get_contents("php://input", TRUE);
$headers = apache_request_headers();
$webhook = db_select('zenci_webhook_repositories', 'gh')
->fields('gh')
->condition('owner', $headers['Owner'])
->condition('repo', $headers['Repo'])
->execute()
->fetchObject();
if (empty($webhook)) {
return MENU_ACCESS_DENIED;
}
if (isset($headers['Signature'])) {
list($algorithm, $expected_hash) = explode('=', $headers['Signature'], 2);
$actual_hash = hash_hmac($algorithm, $received_json, $webhook->secret);
if ($expected_hash !== $actual_hash) {
return MENU_ACCESS_DENIED;
}
}
$data = backdrop_json_decode($received_json);
$answer = module_invoke_all('zenci_webhook', $method, $data);
// Temporary fix to just print and exit.
zenci_webhook_ajax_deliver($answer);
exit();
return $answer;
}
function zenci_webhook_ajax_deliver($page_callback_result) {
backdrop_add_http_header('Content-Type', 'application/json; charset=utf-8');
if (is_int($page_callback_result)) {
$json = array('error' => TRUE);
switch ($page_callback_result) {
case MENU_NOT_FOUND:
$json['message'] = t('The requested page could not be found.');
backdrop_add_http_header('Status', '404 Not Found');
break;
case MENU_ACCESS_DENIED:
$json['message'] = t('You are not authorized to access this page.');
backdrop_add_http_header('Status', '403 Forbidden');
break;
case MENU_SITE_OFFLINE:
$json['message'] = t('@site is currently under maintenance. We should be back shortly. Thank you for your patience.', array('@site' => config_get('system.core', 'site_name')));
backdrop_add_http_header('Status', '503 Service unavailable');
break;
}
}
elseif (is_array($page_callback_result)) {
$json = $page_callback_result;
}
else {
$json = array(
'error' => TRUE,
'message' => t('An unknown error has occurred.'),
);
}
print backdrop_json_encode($json);
if (backdrop_get_bootstrap_phase() == BACKDROP_BOOTSTRAP_FULL && (!defined('MAINTENANCE_MODE') || MAINTENANCE_MODE != 'update')) {
module_invoke_all('exit');
}
}
function zenci_webhook_get_class($settings) {
module_load_include('inc', 'zenci_webhook', 'zenci_webhook.class');
$api_token = zenci_webhook_get_api_token();
$githubapi = new zenciAPI();
$githubapi->setOwnerName($settings['owner']);
$githubapi->setRepoName($settings['repo']);
$githubapi->setToken($api_token);
return $githubapi;
}
function zenci_webhook_get_api_token() {
$config = config('zenci_webhook.settings');
// Use setting.php values if exists.
$api_token = settings_get('zenci_webhook_api_token');
if (empty($api_token)) {
$api_token = $config->get('api_token');
}
return $api_token;
}