-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.util.php
More file actions
138 lines (101 loc) · 2.68 KB
/
functions.util.php
File metadata and controls
138 lines (101 loc) · 2.68 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
130
131
132
133
134
135
136
137
138
<?php
/* REST API class */
class APIREST {
private $url;
public function __construct($url) {
$this->url = $url;
}
/**
* @param $httpheader array of headers
* @return response
*/
public function call($httpheader, $method, $query = NULL) {
try {
$curl = curl_init();
if (FALSE === $curl)
throw new Exception('Failed to initialize');
$curl_opt = array(
CURLOPT_URL => $this->url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 60, /* number of seconds to wait for response */
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => $method,
CURLOPT_POSTFIELDS => $query,
CURLOPT_HTTPHEADER => $httpheader
);
curl_setopt_array($curl, $curl_opt);
$response = curl_exec($curl);
if (FALSE === $response)
throw new Exception(curl_error($curl), curl_errno($curl));
$http_status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if (200 != $http_status)
throw new Exception($response, $http_status);
curl_close($curl);
} catch(Exception $e) {
$response= $e->getCode() . $e->getMessage();
echo $response;
}
return $response;
}
}
/* get timestamp */
function milliseconds() {
list($msec, $sec) = explode(' ', microtime());
return (int) ($sec . substr($msec, 2, 3));
}
/* encrypt key */
function hmac($msg, $secret) {
$hmac = hash_hmac('sha256', $msg, $secret, true);
$hmac = base64_encode($hmac);
return $hmac;
}
/* array depth */
function countdim($array) {
if (is_array(reset($array))) {
$return = countdim(reset($array)) + 1;
} else {
$return = 1;
}
return $return;
}
/*
asset pair only reported on weekdays
expect some asset pairs are not recorded on weekends, for instance fiat currencies like $AUD to $USD
*/
function weekends($class) {
if ($class == 'fiat' || $class == 'stock') {
$weekend = FALSE;
} else {
$weekend = TRUE;
}
return $weekend;
}
/* count number of observations falling on weekends */
function count_on_weekends($from, $to, $period_ms) {
$wmiss = 0;
for ($j = $from; $j <= $to; $j += $period_ms) {
$day = date('l', $j / 1000);
if (!($day == 'Sunday' || $day == 'Monday')) $wmiss++;
}
return $wmiss;
}
/* email alert */
function alert_email($config) {
$subject = 'Vert agent alert';
$headers = 'From: ' . $config['alert_email_from'];
try {
if (!mail($config['alert_email_to'], $subject, $config['chatText'], $headers)) {
throw new Exception('Failed to send email alert');
} else {
echo 'Email alert sent successfully.';
return TRUE;
}
} catch (Exception $e) {
$msg = 'An email alert error occurred: ' . $e->getMessage();
echo $msg;
error_log($msg);
return FALSE;
}
}