-
Notifications
You must be signed in to change notification settings - Fork 99
/
Copy pathfunctions.php
297 lines (231 loc) · 6.87 KB
/
functions.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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
<?php
function read_config()
{
$config=array();
$contents=file_get_contents('config.txt');
$lines=explode("\n", $contents);
foreach ($lines as $line) {
$content=explode('#', $line);
$fields=explode('=', trim($content[0]));
if (count($fields)==2) {
if (is_numeric(strpos($fields[0], '.'))) {
$parts=explode('.', $fields[0]);
$config[$parts[0]][$parts[1]]=$fields[1];
} else {
$config[$fields[0]]=$fields[1];
}
}
}
return $config;
}
function json_rpc_send($host, $port, $secure, $user, $password, $method, $params=array(), &$rawresponse=false)
{
if (!function_exists('curl_init')) {
output_html_error('This web demo requires the curl extension for PHP. Please contact your web hosting provider or system administrator for assistance.');
exit;
}
$url=($secure ? 'https' : 'http').'://'.$host.':'.$port.'/';
$payload=json_encode(array(
'id' => time(),
'method' => $method,
'params' => $params,
));
// echo '<PRE>'; print_r($payload); echo '</PRE>';
$ch=curl_init($url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, $user.':'.$password);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: '.strlen($payload)
));
$response=curl_exec($ch);
if ($rawresponse!==false)
$rawresponse=$response;
// echo '<PRE>'; print_r($response); echo '</PRE>';
$result=json_decode($response, true);
if (!is_array($result)) {
$info=curl_getinfo($ch);
$result=array('error' => array(
'code' => 'HTTP '.$info['http_code'],
'message' => strip_tags($response).' '.$url
));
}
return $result;
}
function set_multichain_chain($chain)
{
global $multichain_chain;
$multichain_chain=$chain;
}
function multichain($method) // other params read from func_get_args()
{
global $multichain_chain;
$args=func_get_args();
return json_rpc_send($multichain_chain['rpchost'], $multichain_chain['rpcport'], $multichain_chain['rpcsecure'],
$multichain_chain['rpcuser'], $multichain_chain['rpcpassword'], $method, array_slice($args, 1));
}
function multichain_with_raw(&$rawresponse, $method) // other params read from func_get_args()
{
global $multichain_chain;
$args=func_get_args();
$rawresponse='';
return json_rpc_send($multichain_chain['rpchost'], $multichain_chain['rpcport'], $multichain_chain['rpcsecure'],
$multichain_chain['rpcuser'], $multichain_chain['rpcpassword'], $method, array_slice($args, 2), $rawresponse);
}
function output_html_error($html)
{
echo '<div class="bg-danger" style="padding:1em;">Error: '.$html.'</div>';
}
function output_rpc_error($error)
{
output_html_error(html($error['code']).'<br/>'.html($error['message']));
}
function output_success_text($success)
{
echo '<div class="bg-success" style="padding:1em;">'.nl2br(html($success)).'</div>';
}
function output_error_text($error)
{
echo '<div class="bg-danger" style="padding:1em;">'.nl2br(html($error)).'</div>';
}
function no_displayed_error_result(&$result, $response)
{
if (is_array($response['error'])) {
$result=null;
output_rpc_error($response['error']);
return false;
} else {
$result=$response['result'];
return true;
}
}
function html($string)
{
return htmlspecialchars($string);
}
function chain_page_url_html($chain, $page=null, $params=array())
{
$url='./?chain='.$chain;
if (strlen($page))
$url.='&page='.$page;
foreach ($params as $key => $value)
$url.='&'.rawurlencode($key).'='.rawurlencode($value);
return html($url);
}
function array_get_column($array, $key) // see array_column() in recent versions of PHP
{
$result=array();
foreach ($array as $index => $element)
if (array_key_exists($key, $element))
$result[$index]=$element[$key];
return $result;
}
function multichain_getinfo()
{
global $multichain_getinfo;
if (!is_array($multichain_getinfo))
no_displayed_error_result($multichain_getinfo, multichain('getinfo'));
return $multichain_getinfo;
}
function multichain_has_protocol($version)
{
$getinfo=multichain_getinfo();
return $getinfo['protocolversion']>=$version;
}
function multichain_has_multi_item_keys()
{
return multichain_has_protocol(20001);
}
function multichain_has_json_text_items()
{
return multichain_has_protocol(20001);
}
function multichain_has_custom_permissions()
{
return multichain_has_protocol(20003);
}
function multichain_has_off_chain_items()
{
return multichain_has_protocol(20003);
}
function multichain_has_smart_filters()
{
return multichain_has_protocol(20004);
}
function multichain_labels()
{
global $multichain_labels;
if (!is_array($multichain_labels)) {
if (no_displayed_error_result($items, multichain('liststreampublishers', 'root', '*', true, 10000))) {
$multichain_labels=array();
foreach ($items as $item)
$multichain_labels[$item['publisher']]=pack('H*', $item['last']['data']);
}
}
return $multichain_labels;
}
function multichain_max_data_size()
{
global $multichain_max_data_size;
if (!isset($multichain_max_data_size))
if (no_displayed_error_result($params, multichain('getblockchainparams')))
$multichain_max_data_size=min(
$params['maximum-block-size']-80-320,
$params['max-std-tx-size']-320,
$params['max-std-op-return-size']
);
return $multichain_max_data_size;
}
function format_address_html($address, $local, $labels, $link=null)
{
$label=@$labels[$address];
if (strlen($link)) {
$prefix='<a href="'.html($link).'">';
$suffix='</a>';
} else {
$prefix='';
$suffix='';
}
if (isset($label))
$string=html($label).' ('.$prefix.html($address).$suffix.($local ? ', local' : '').')';
else
$string=$prefix.html($address).$suffix.($local ? ' (local)' : '');
return $string;
}
function string_to_txout_bin($string)
{
return ltrim($string, "\x00"); // ensures that first byte 0x00 means it's a file
}
function file_to_txout_bin($filename, $mimetype, $content)
{
return "\x00".$filename."\x00".$mimetype."\x00".$content;
}
function txout_bin_to_file($data)
{
$parts=explode("\x00", $data, 4);
if ( (count($parts)!=4) || ($parts[0]!='') )
return null;
return array(
'filename' => $parts[1],
'mimetype' => $parts[2],
'content' => $parts[3],
);
}
function fileref_to_string($vout, $filename, $mimetype, $filesize)
{
return "\x00".$vout."\x00".$filename."\x00".$mimetype."\x00".$filesize;
}
function string_to_fileref($string)
{
$parts=explode("\x00", $string);
if ( (count($parts)!=5) || ($parts[0]!='') )
return null;
return array(
'vout' => $parts[1],
'filename' => $parts[2],
'mimetype' => $parts[3],
'filesize' => $parts[4],
);
}