This repository has been archived by the owner on Aug 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
securepass.client.inc
88 lines (82 loc) · 2.54 KB
/
securepass.client.inc
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
<?php
use Securepass\Securepass;
use Securepass\Exception\Exception as SecurepassException;
/**
* Check if API keys are correctly configured.
*
* @param boolean $verbose Print debug message
*/
function securepass_get_configurations($verbose = false) {
$app_id = variable_get('securepass_app_id', FALSE);
$app_secret = variable_get('securepass_app_secret', FALSE);
if ($app_id && $app_secret) {
$config = array('app_id' => $app_id,
'app_secret' => $app_secret);
return $config;
}
if ($verbose) {
drupal_set_message(t('Securepass API Keys are not configured, you can <a href="@url"> configure api keys by clicking here</a>',
array('@url' => url('admin/config/system/securepass/api-keys'))), 'warning');
}
}
/**
* Get Securepass client
*/
function securepass_client() {
if ($config = securepass_get_configurations()) {
try {
$securepass = new Securepass($config);
return $securepass;
}
catch (\Exception $e) {
securepass_api_log_error($e);
}
}
}
/**
* Ping Securepass API
*/
function securepass_api_check_status() {
try {
securepass_api_execute('ping');
return true;
}
catch (SecurepassDrupalException $e) {
return $e->getMessage();
}
}
function securepass_api_execute($method, $type = false, $params = array()) {
$debug = variable_get('securepass_debug', false);
try {
$client = securepass_client();
if (method_exists($client, $method)) {
// try to understand which method has been called, some methods does't have parameters (ex. ->ping())
$reflection = new ReflectionMethod($client, $method);
$method_params = $reflection->getParameters();
if (!count($method_params)) {
$res = $client->{$method}();
}
else {
$res = $client->{$method}($type, $params);
}
return $res;
}
else {
throw new InvalidArgumentException(t('This method not exists, please refer the documentation.'));
}
}
catch (Exception $e) {
securepass_api_log_error($e, $debug);
throw new SecurepassDrupalException($e->getMessage());
}
}
function securepass_api_log_error($exception, $verbose = false) {
$exception_class = get_class($exception);
$error_message = $exception->getMessage();
watchdog('securepass', 'Securepass API error: "@exception_class" - "@error"',
array('@exception_class' => $exception_class,
'@error' => $error_message), WATCHDOG_ERROR);
if ($verbose) {
drupal_set_message('Securepass API error: ' . $error_message, 'error');
}
}