-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathblockchain.php
More file actions
80 lines (71 loc) · 2.5 KB
/
blockchain.php
File metadata and controls
80 lines (71 loc) · 2.5 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
<?php
/************** blockchain 101 *****************/
/***********************************************/
/** Isaac Jacobs Krishna Deilson Christopher ***/
/***********************************************/
/***** GNU General Public License v3 - GPLv3 ***/
/***********************************************/
$GLOBALS['blockchainfile'] = "blockchain.file";
$GLOBALS['filehandle'] = NULL;
// if(empty($_POST) || !isset($_POST['content']) || $_POST['content'] == ""){
// // showForm();
// }else{
// $block = addToBlockChain();
// displayNewBlock($block);
// }
// showForm();
function showForm(){
echo
<form action="" method="post">
<label for="content">Add text to the block chain:</label>
<input id="content" name="content" type="text">
<button type="submit" value="1">Add</button>
form;
}
function getPreviousHash(){
if(!$GLOBALS['filehandle'] = fopen($GLOBALS['blockchainfile'],"a+b")) die("unable to get file handle.");
flock($GLOBALS['filehandle'],LOCK_EX);
if(is_file($GLOBALS['blockchainfile']) && filesize($GLOBALS['blockchainfile']) > 0){
while(($block = fgets($GLOBALS['filehandle'])) !== false){
$lastBlock = $block;
}
// fclose($GLOBALS['filehandle']);
return hash("sha1",$lastBlock);
}else{
return hash("sha1","EMDM");
}
}
function addToBlockChain(){
$hash = getPreviousHash();
$data = encode(trim($_POST['content']));
$block = ["time"=>time(),"content"=>$data,"PreviousBlockHash"=>$hash];
$block = json_encode($block);
fwrite($GLOBALS['filehandle'],$block.PHP_EOL);
flock($GLOBALS['filehandle'],LOCK_UN);
return $block;
}
/*********************bells and whistles here! */
function displayNewBlock($block){
$a = json_decode($block);
$a->content = decode($a->content);
echo "<h3>A new block was added to the blockchain.</h3>";
echo "<h5>Block details:</h5>";
foreach($a as $k => $v){
$v = trim($v);
$echoTxt = "<pre>{$k} : <span id='{$k}'>{$v}</span></pre>";
$echoTxt .= ($k == "content") ? "<button id='evalThis'>RUN</button>":"";
echo($echoTxt);
}
includeScript();
}
function includeScript(){
echo "<script>";
echo file_get_contents("smartContract.js");
echo "</script>";
}
function decode($string){
return zlib_decode(hex2bin($string));
}
function encode($string){
return bin2hex(zlib_encode($string, ZLIB_ENCODING_RAW));
}