-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathcron_.php
482 lines (424 loc) · 19.1 KB
/
cron_.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
<?php
/*
# Burst Apps Team contribution -https://twitter.com/BurstAppsTeam
# Devolped by Zoh - https://twitter.com/Zoh63392187
# Donations: BURST-NMEA-GRHZ-BRFE-5SG6P
*/
// This schedule / cronjob should run every minute
//if($_SERVER['REMOTE_ADDR'] != '8.8.4.4' && $_SERVER['REMOTE_ADDR']!='' )die('Not allowed');
if($_SERVER['REMOTE_ADDR']!='' )die('Not allowed');
// Set max execution time to insure that its no longer than 60 seconds before new cron job start.
//ini_set('max_execution_time', 55);
$t1=time();
// Database login details
$db_server = ''; // ip:port
$db_user = ''; // Database username
$db_pass = ''; // Database password
$db_db = ''; // Database name
$db_link = @mysqli_connect($db_server, $db_user, $db_pass) or die('('.mysqli_connect_errno().')');
@mysqli_select_db($db_link, $db_db) or die('(2)');
$memcached = new Memcached();
$memcached->addServer("localhost", 11211);
$mail_headers = "MIME-Version: 1.0" . "\r\n";
$mail_headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$mail_headers .= 'From: [email protected]' . "\r\n"; //Enter your reply e-mail address
// Collect new peers for the Chart
query_execute('INSERT INTO peer_char (`address`) SELECT address FROM peer where address not in (select address from peer_char)');
$peers = query_to_array('select * from peer_char where 1');
foreach($peers as $peer){
$result = call_api('localhost:8125/burst?requestType=getPeer&peer='.$peer['address']);
if($result['errorCode']==5 || $result['version']=='v0.0.0' || $result['version']=='')continue;
query_execute_unsafe('update peer_char set brs_version="'.ltrim($result['version'],'v').'" where address="'.$peer['address'].'"');
}
echo 'peerversionupdated<br>';
// Search for Multi-Out transactions and put it in the database
$last_block = query_execute('select height from block where 1 order by height desc limit 1');
$blocks = query_to_array('select db_id, id, attachment_bytes from transaction where type=0 and subtype=1 and height>='.($last_block['height']-50));
foreach($blocks as $block){
if($block['attachment_bytes']){
$transaction_id = $block['id'];
$multiout_attachment = parseMultiOut($block['attachment_bytes']);
foreach($multiout_attachment as $ma){
query_execute_unsafe('INSERT INTO parseMultiOut(recipient_id, transaction_id, amount, db_id)VALUES ('.fromUnsignedLong($ma[0]).','.$transaction_id.','.$ma[1].','.$block['db_id'].')');
}
}
}
// Search for Multi-Out-Same transactions and put it in the database
$transaction = query_to_array('select * from transaction where type=0 and subtype=2 and height>='.($last_block['height']-50));
foreach($transaction as $trans){
if($trans['attachment_bytes']){
$transaction_id = $trans['id'];
$multiout_attachment = parseMultiOutSame($trans['attachment_bytes']);
foreach($multiout_attachment as $ma){
$numbers_of_rewards=count($multiout_attachment);
$reward = ($trans['amount']/$numbers_of_rewards);
query_execute_unsafe("INSERT INTO parseMultiOutSame(recipient_id, transaction_id, amount,db_id)VALUES ('".fromUnsignedLong($ma)."','".$transaction_id."','".$reward."',".$trans['db_id'].")");
}
}
}
// Find the block generator and recipient of the forged block and put it in the database
$pool_blocks = query_execute('select height from block_forger where 1 order by height desc limit 1');
if($pool_blocks['height']>=1)$block_height = $pool_blocks['height'];
else $block_height = 1;
while($state==false){
$pool_blocks = query_execute('select generator_id from block where height='.$block_height);
if($pool_blocks['generator_id']!=''){
$reward_assignment = query_to_array('select * from transaction where type=20 and height<= '.$block_height.' and sender_id='.$pool_blocks['generator_id'].' order by height asc');
$reward_assignment_count = query_execute('select count(*) as count from transaction where type=20 and height<= '.$block_height.' and sender_id='.$pool_blocks['generator_id']);
$last_ra =0;
$current_count = 1;
if(!$reward_assignment){
add_block_forger($pool_blocks['generator_id'],$pool_blocks['generator_id'],$block_height);
}
foreach($reward_assignment as $ra){
if($block_height>=($ra['height']+3) && $reward_assignment_count['count']!=$current_count){
$last_ra = $ra['recipient_id'];
$current_count++;
continue;
}
if($block_height>=($ra['height']+3) && $reward_assignment_count['count']==$current_count){
$last_ra = $ra['recipient_id'];
}
if($last_ra==$pool_blocks['generator_id']){
add_block_forger($pool_blocks['generator_id'],$pool_blocks['generator_id'],$block_height);
}else{
add_block_forger($pool_blocks['generator_id'],$last_ra,$block_height);
}
$current_count++;
break;
}
$current_count=1;
$block_height++;
} else {
$state=true;
}
}
// Monitor if new account (welcome mail)
$result = query_to_array_unsafe("select * from monitor where welcome='0'");
foreach($result as $accounts){
query_to_array_unsafe("update monitor set welcome='1' where welcome='0'");
mail($accounts['email'],'Burst Notification System','Welcome to Burst Notification System<br><br>If you did not subscribe for e-mail notifications please click:<a href="https://explorer.burstcoin.network/?action=monitor&hash='.$accounts['passphrase'].'">Unsubscribe e-mail notifications</a>',$mail_headers);
query_execute('update monitor set send_mails=send_mails+1 where db_id='.$accounts['db_id']);
}
// Monitor if we have an account who forged a block og have balance change
$last_monitored_height = query_execute('select last_height from monitor_block')['last_height'];
$block_to_scan = query_to_array('select * from block_forger where height >'.($last_monitored_height) .' order by height asc');
$max_height = query_execute('select max(height) as height from block_forger')['height'];
query_execute('update monitor_block set last_height='.$max_height);
foreach($block_to_scan as $block){
$accounts = query_to_array('select * from monitor where account_id='.$block['generator_id']);
foreach($accounts as $act){
mail($act['email'],'Burst Notification System',"<b>Block Forged!</b><br><b>Account:</b> <a href='https://explorer.burstcoin.network/?action=account&account=".toUnsignedLong($act['account_id'])."&submenu=blocks'>".RS_encode(toUnsignedLong($act['account_id']))."</a><br><br><i>This e-mail is provided to you by <a href='https://burstcoin.network'>explorer.burstcoin.network</a><br><a href='https://explorer.burstcoin.network/?action=monitor&hash=".$act['passphrase']."'>Unsubscribe</a> e-mail notifications",$mail_headers);
query_execute('update monitor set send_mails=send_mails+1 where db_id='.$act['db_id']);
}
}
// Monitor a given account
$result = query_to_array_unsafe("select * from monitor");
foreach($result as $accounts){
$db_balance = get_account_balance($accounts['account_id']);
$monitor_balance = $accounts['balance'];
if($db_balance<$monitor_balance){
//withdraw
$amount = $monitor_balance-$db_balance;
query_execute_unsafe('update monitor set balance='.$db_balance.' where db_id='.$accounts['db_id']);
mail($accounts['email'],'Burst Notification System','<b>New balance:</b> '.get_burst_amount($db_balance,2).' Burst (<font color="red">-'.get_burst_amount($amount,2).'</font>)<br><b>Account:</b> <a href="https://explorer.burstcoin.network/?action=account&account='.toUnsignedLong($accounts['account_id']).'&submenu=at">'.RS_encode(toUnsignedLong($accounts['account_id'])).'</a><br><br><i>This e-mail is provided to you by <a href="https://explorer.burstcoin.network">explorer.burstcoin.network</a><br><a href="https://explorer.burstcoin.network/?action=monitor&hash='.$accounts['passphrase'].'">Unsubscribe</a> e-mail notifications',$mail_headers);
query_execute('update monitor set send_mails=send_mails+1 where db_id='.$accounts['db_id']);
}
if($db_balance>$monitor_balance){
//deposite
$amount = $db_balance-$monitor_balance;
query_execute_unsafe('update monitor set balance='.$db_balance.' where db_id='.$accounts['db_id']);
mail($accounts['email'],'Burst Notification System','<b>New balance:</b> '.get_burst_amount($db_balance,2).' Burst (<font color="green">+'.get_burst_amount($amount,2).'</font>)<br><b>Account:</b> <a href="https://explorer.burstcoin.network/?action=account&account='.toUnsignedLong($accounts['account_id']).'&submenu=at">'.RS_encode(toUnsignedLong($accounts['account_id'])).'</a><br><br><i>This e-mail is provided to you by <a href="https://explorer.burstcoin.network">explorer.burstcoin.network</a><br><a href="https://explorer.burstcoin.network/?action=monitor&hash='.$accounts['passphrase'].'">Unsubscribe</a> e-mail notifications',$mail_headers);
query_execute('update monitor set send_mails=send_mails+1 where db_id='.$accounts['db_id']);
}
}
//Update BRS version on peers active on the Chart
$peers = query_to_array('select * from peer_char where 1');
foreach($peers as $peer){
$result = call_api('localhost:8125/burst?requestType=getPeer&peer='.$peer['address']);
if($result['errorCode']==5 || $result['version']=='v0.0.0' || $result['version']=='')continue;
query_execute_unsafe('update peer_char set brs_version="'.ltrim($result['version'],'v').'" where address="'.$peer['address'].'"');
}
echo 'Done';
$t2= (time() - $t1);
if($t2>30)file_put_contents('logs/cron_',$t2." seconds: \n",FILE_APPEND);
//-------------------------------------------------- functions -------------------------------------------------//
function call_api($url){
$full_link = $url;
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json'));
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL,$full_link);
$result=curl_exec($ch);
curl_close($ch);
$json_feed = json_decode($result, true);
return $json_feed;
}
function get_burst_amount($burst,$decimals,$bypass = 0) {
$amount = ($burst/100000000);
if($amount <= 0) return 0;
if($bypass==1)return ($amount);
return number_format($amount,$decimals);
}
function get_account_balance($account_id){
$db_balance = query_execute_unsafe("select balance from account where latest=1 and id=".$account_id);
return $db_balance['balance'];
}
function get_monitor_account_balance($account_id){
$monitor_balance = query_execute_unsafe("select balance from monitor where account_id=".$account_id);
return $monitor_balance['balance'];
}
function query_execute_unsafe($sql){
global $db_link;
$t1=time();
$qq = mysqli_query($db_link, $sql);
$result = mysqli_fetch_assoc($qq);
$t2= (time() - $t1);
if($t2>2)file_put_contents('logs/sql_log',$t2." seconds: ".$sql."\n",FILE_APPEND);
return $result;
}
function query_to_array_unsafe($sql){
global $db_link;
$query = mysqli_query($db_link, $sql);
while ($array = mysqli_fetch_assoc($query)) {
$sql_array[] = $array;
}
return $sql_array;
}
function add_block_forger($generator_id,$recipient_id,$height){
query_execute('INSERT INTO block_forger(generator_id, recipient_id, height)VALUES ('.$generator_id.','.$recipient_id.','.$height.')');
}
/**
* @param $attachment string The attachment data as a string
* @return array 2 dimensional array - First array contains each transfer, second array contains [account_id, amount_transferred]
* @ array[0] = recipient
* @ array[1] = burst amount
*/
function parseMultiOut($attachment) {
$header = unpack("C*", substr($attachment, 0, 2));
$version = $header[1];
$numberOfRecipients = $header[2];
$data = unpack("P*", substr($attachment, 2, strlen($attachment)-2));
$result = array();
for ($i = 1; $i <= $numberOfRecipients; $i++) {
array_push($result, array(toUnsignedLong($data[2*$i-1]), $data[2*$i]));
}
return $result;
}
/**
* @param $attachment string The attachment data as a string
* @return array Array of accounts that the amount was transferred to
*/
function parseMultiOutSame($attachment) {
$header = unpack("C*", substr($attachment, 0, 2));
$version = $header[1];
$numberOfRecipients = $header[2];
$data = unpack("P*", substr($attachment, 2, strlen($attachment)-2));
for ($i = 1; $i <= sizeof($data); $i++) {
$data[$i] = toUnsignedLong($data[$i]);
}
return $data;
}
/**
* @param $input string the unsigned long ID
* @return string the signed long ID
* @convert back to database format
*/
function fromUnsignedLong($input) {
$input = (string)$input;
if (bccomp(bcsub($input, bcpow("2", "63")), "0") >= 0) {
return bcsub($input, bcpow(2,64));
} else return $input;
}
/**
* @param $input string the signed long ID
* @return string The unsigned long ID
* input from database
*/
function toUnsignedLong($input) {
$input = (string)$input;
if(bccomp($input, "0") <= 0){
return bcadd(bcpow("2", "64"), $input);
} else return $input;
}
function query_execute($sql){
global $db_link;
$t1=time();
$qq = mysqli_query($db_link, mysqli_real_escape_string($db_link,$sql));
$result = mysqli_fetch_assoc($qq);
$t2= (time() - $t1);
if($t2>2)file_put_contents('logs/sql_log',$t2." seconds: ".$sql."\n",FILE_APPEND);
return $result;
}
function query_to_array($sql){
global $db_link;
$query = mysqli_query($db_link, mysqli_real_escape_string($db_link,$sql));
while ($array = mysqli_fetch_assoc($query)) {
$sql_array[] = $array;
}
return $sql_array;
}
/**
* Ported from BRS by harry1453
* Licensed under GPLv3 - ported from https://github.com/burst-apps-team/burstcoin/blob/master/src/brs/crypto/ReedSolomon.java
*/
/**
* @param $input string The input string
* @param $pos int The position of the character you want to get
* @return string The char at that position
*/
function RS_charAt($input, $pos) {
return substr($input, $pos, 1);
}
/**
* @param $length int The size of the array
* @return array An empty integer array
*/
function RS_emptyArray($length) {
return array_fill(0, $length, 0);
}
/**
* @param $plain string The numeric ID to encode
* @return string The RS encoding of the ID in the form BURST-XXXX-XXXX-XXXX-XXXXX
*/
function RS_encode($plain) {
$initial_codeword = array(1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
$codeword_map = array(3, 2, 1, 0, 7, 6, 5, 4, 13, 14, 15, 16, 12, 8, 9, 10, 11);
$alphabet = "23456789ABCDEFGHJKLMNPQRSTUVWXYZ";
$base_32_length = 13;
$base_10_length = 20;
$length = strlen($plain);
$plain_string_10 = RS_emptyArray($base_10_length);
for ($i = 0; $i < $length; $i++) {
$plain_string_10[$i] = ((int)RS_charAt($plain, $i)) - (int)'0';
}
$codeword_length = 0;
$codeword = RS_emptyArray(sizeof($initial_codeword));
do { // base 10 to base 32 conversion
$new_length = 0;
$digit_32 = 0;
for ($i = 0; $i < $length; $i++) {
$digit_32 = $digit_32 * 10 + $plain_string_10[$i];
if ($digit_32 >= 32) {
$plain_string_10[$new_length] = $digit_32 >> 5;
$digit_32 &= 31;
$new_length += 1;
} else if ($new_length > 0) {
$plain_string_10[$new_length] = 0;
$new_length += 1;
}
}
$length = $new_length;
$codeword[$codeword_length] = $digit_32;
$codeword_length += 1;
} while($length > 0);
$p = array(0, 0, 0, 0);
for ($i = $base_32_length - 1; $i >= 0; $i--) {
$fb = $codeword[$i] ^ $p[3];
$p[3] = $p[2] ^ RS_gmult(30, $fb);
$p[2] = $p[1] ^ RS_gmult(6, $fb);
$p[1] = $p[0] ^ RS_gmult(9, $fb);
$p[0] = RS_gmult(17, $fb);
}
for ($i = 0; $i < sizeof($initial_codeword) - $base_32_length; $i++) {
$codeword[$i+$base_32_length] = $p[$i];
}
$cypher_string_builder = "";
for ($i = 0; $i < 17; $i++) {
$codework_index = $codeword_map[$i];
$alphabet_index = $codeword[$codework_index];
$cypher_string_builder .= RS_charAt($alphabet, $alphabet_index);
if (($i & 3) == 3 && $i < 13) {
$cypher_string_builder .= '-';
}
}
return "BURST-" . $cypher_string_builder;
}
/**
* @param $cypher_string string the RS encoded address in the form BURST-XXXX-XXXX-XXXX-XXXXX
* @return string The numeric ID of the account
* @throws Exception If the encoded address fails to decode / is not valid.
*/
function RS_decode($cypher_string) {
if (substr($cypher_string, 0, 6) === "BURST-") {
$cypher_string = substr($cypher_string, 6, strlen($cypher_string) - 6);
}
$initial_codeword = array(1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
$codeword_map = array(3, 2, 1, 0, 7, 6, 5, 4, 13, 14, 15, 16, 12, 8, 9, 10, 11);
$alphabet = "23456789ABCDEFGHJKLMNPQRSTUVWXYZ";
$base_32_length = 13;
$codeword = RS_emptyArray(sizeof($initial_codeword));
for ($i = 0; $i < sizeof($initial_codeword); $i++) {
$codeword[$i] = $initial_codeword[$i];
}
$codeword_length = 0;
for ($i = 0; $i < strlen($cypher_string); $i++) {
$position_in_alphabet = strpos($alphabet, RS_charAt($cypher_string, $i));
if ($position_in_alphabet <= -1 || $position_in_alphabet > strlen($alphabet)) {
continue;
}
if ($codeword_length > 16) {
throw new Exception("Codeword too long");
}
$codework_index = $codeword_map[$codeword_length];
$codeword[$codework_index] = $position_in_alphabet;
$codeword_length += 1;
}
if ($codeword_length != 17 || !RS_is_codeword_valid($codeword)) {
throw new Exception("Codeword invalid");
}
$length = $base_32_length;
$cypher_string_32 = RS_emptyArray($length);
for ($i = 0; $i < $length; $i++) {
$cypher_string_32[$i] = $codeword[$length - $i - 1];
}
$plain_string_builder = "";
do { // base 32 to base 10 conversion
$new_length = 0;
$digit_10 = 0;
for ($i = 0; $i < $length; $i++) {
$digit_10 = $digit_10 * 32 + $cypher_string_32[$i];
if ($digit_10 >= 10) {
$cypher_string_32[$new_length] = intdiv($digit_10, 10);
$digit_10 %= 10;
$new_length += 1;
} else if ($new_length > 0) {
$cypher_string_32[$new_length] = 0;
$new_length += 1;
}
}
$length = $new_length;
$plain_string_builder .= ($digit_10 + (int)'0');
} while ($length > 0);
return strrev($plain_string_builder);
}
function RS_gmult($a, $b) {
$gexp = array(1, 2, 4, 8, 16, 5, 10, 20, 13, 26, 17, 7, 14, 28, 29, 31, 27, 19, 3, 6, 12, 24, 21, 15, 30, 25, 23, 11, 22, 9, 18, 1);
$glog = array(0, 0, 1, 18, 2, 5, 19, 11, 3, 29, 6, 27, 20, 8, 12, 23, 4, 10, 30, 17, 7, 22, 28, 26, 21, 25, 9, 16, 13, 14, 24, 15);
if ($a == 0 || $b == 0) {
return 0;
}
$idx = ($glog[$a] + $glog[$b]) % 31;
return $gexp[$idx];
}
function RS_is_codeword_valid($codeword) {
$gexp = array(1, 2, 4, 8, 16, 5, 10, 20, 13, 26, 17, 7, 14, 28, 29, 31, 27, 19, 3, 6, 12, 24, 21, 15, 30, 25, 23, 11, 22, 9, 18, 1);
$sum = 0;
for ($i = 1; $i < 5; $i++) {
$t = 0;
for ($j = 0; $j < 31; $j++) {
if ($j > 12 && $j < 27) {
continue;
}
$pos = $j;
if ($j > 26) {
$pos -= 14;
}
$t ^= RS_gmult($codeword[$pos], $gexp[($i * $j) % 31]);
}
$sum |= $t;
}
return $sum == 0;
}