1: <?php
2:
3: namespace Liberty;
4:
5: if (!extension_loaded('curl')) {
6: throw new \Exception('cURL extension seems not to be installed');
7: }
8:
9: use Liberty\SSV;
10: use Liberty\INI;
11: use Exception;
12:
13: 14: 15: 16: 17: 18: 19: 20: 21: 22:
23:
24:
25:
26: class WebPeer extends SSV {
27:
28: protected $path;
29:
30:
31: public function __construct($path)
32: {
33: if(!file_exists($path . "/broadcast.ssv")) {
34: $res = fopen($path . "/broadcast.ssv", "a");
35: fputs($res, "peer address fails\n");
36: fputs($res, "https://libertycryptocurrency.org/libertynucleus LdRcxV7qHiGdMxS446XVVojgZcpduzPE9a 0\n");
37: fclose($res);
38: }
39: parent::__construct($path . "/broadcast.ssv");
40: $this->path = $path;
41: }
42:
43:
44:
45:
46: public function broadcast($feeaddr)
47: {
48: for($a=1; $a<$this->rowsCount()+1; $a++) {
49: if($this->row($a)["peer"] == $this->peerNode()) {
50: continue;
51: }
52:
53: $broadcast = $this->row($a)["peer"] . "/server.php?broadcast=" . $this->peerNode() . "&address=" . $feeaddr;
54:
55: if(self::curl($broadcast) !== false) {
56: self::logger($this->peerNode() . ": Broadcasting to " . $this->row($a)["peer"]);
57: return true;
58: } else {
59: self::logger("Cannot broadcast to " . $this->row($a)["peer"]);
60: return false;
61: }
62: }
63: }
64:
65:
66:
67:
68: public function peerAdd($url, $feeaddr)
69: {
70: $result = $this->findBy($url, "peer");
71:
72: if(count($result) === 0) {
73: $this->add( array("peer" => $url, "address" => $feeaddr, "fails" => "0") );
74: return true;
75: }
76:
77: return false;
78: }
79:
80:
81:
82:
83: public function peerAddress()
84: {
85: $addrs = array();
86: $b=0;
87:
88: for($a=1; $a<$this->rowsCount()+1; $a++) {
89: $server = $this->row($a);
90: if($server["fails"] > 0) continue;
91: $addrs[$b] = $server["address"];
92: $b++;
93: }
94:
95: return $addrs;
96: }
97:
98:
99:
100:
101: public function peerList()
102: {
103: $peers = array();
104: $b=0;
105:
106: for($a=1; $a<$this->rowsCount()+1; $a++) {
107: $server = $this->row($a);
108: if($server["fails"] > 0) continue;
109: $peers[$b] = $server["peer"];
110: $b++;
111: }
112:
113: return $peers;
114: }
115:
116:
117:
118:
119:
120: public function peerNode()
121: {
122: if(isset($_SERVER["SERVER_NAME"])) {
123: $script = "http://" . $_SERVER["SERVER_NAME"] . $_SERVER["SCRIPT_NAME"];
124: $local = str_replace("/server.php", "", $script);
125: } else {
126: $local = "";
127: }
128: return $local;
129: }
130:
131:
132:
133:
134: public function remote($peer)
135: {
136: $peers = array();
137:
138: $url = $peer . "/server.php?nucleus=1";
139:
140: if($json = self::curl($url)) {
141: $obj = json_decode($json);
142:
143: for($b=0; isset($obj->Peer->$b); $b++) {
144: $peers["Peer"][$b] = $obj->Peer->$b;
145: $peers["Address"][$b] = $obj->Address->$b;
146: }
147:
148: return $peers;
149: } else {
150: return false;
151: }
152: }
153:
154:
155:
156: public function peerUpdate()
157: {
158: for($a=1; $a<$this->rowsCount()+1; $a++) {
159:
160: $row = $this->row($a);
161:
162: self::logger("Connecting.. " . $row["peer"]);
163:
164: if($row["peer"] == $this->peerNode()) {
165: self::logger("Server is itself");
166: continue;
167: }
168:
169: if( $peers = $this->remote($row["peer"]) ) {
170: for($b=0; $b<count($peers["Peer"]); $b++) {
171: if( $this->peerAdd($peers["Peer"][$b], $peers["Address"][$b]) ) {
172: self::logger("Peer Adding: " . $peers["Peer"][$b] . "/" . $peers["Address"][$b]);
173: } else {
174: self::logger("Peer was added.");
175: }
176: }
177:
178: $row["fails"] = 0;
179: $this->edit($a, $row);
180:
181: } else {
182: self::logger("Cannot connect to " . $row["peer"]);
183: $row["fails"] = 1;
184: $this->edit($a, $row);
185: }
186: }
187: }
188:
189:
190:
191:
192:
193: public static function curl($url, $post=""){
194: $ch = curl_init();
195: curl_setopt($ch, CURLOPT_URL, $url);
196: curl_setopt($ch, CURLOPT_USERAGENT, 'LibertyAudit/1.0');
197: curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
198: if(substr($url, 0, 5) == "https") {
199: curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
200: curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
201: }
202: curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
203: curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
204: curl_setopt($ch, CURLOPT_TIMEOUT, 30);
205:
206: if(isset($post)) {
207:
208: curl_setopt($ch, CURLOPT_POST, 1);
209: curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
210: }
211:
212: $data = curl_exec($ch);
213: $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
214: curl_close($ch);
215:
216: self::logger("cURL: " . $url);
217:
218: return ($httpcode>=200 && $httpcode<300) ? $data : false;
219: }
220:
221:
222:
223:
224:
225: public static function logger($message) {
226: $res = fopen("webpeer.log", "a");
227: fputs($res, $message . PHP_EOL);
228: fclose($res);
229:
230: $file = file("webpeer.log");
231: if(count($file) > 400) {
232: $res = fopen("webpeer.log", "wb");
233: fclose($res);
234: }
235: }
236:
237:
238:
239: }
240:
241:
242: ?>
243: