Exemplo n.º 1
0
 public static function inputc($message = "")
 {
     gio::output("{$message}: ");
     $stdin = fopen("php://stdin", "r");
     $ret = fgetc($stdin);
     return trim($ret);
 }
Exemplo n.º 2
0
 public static function ecash($number = 1, $coinvalue = 1)
 {
     $badsav = 0;
     $newcoin = array();
     $starttime = time();
     while ($number) {
         gio::output(sprintf("Remaining: %d", $number));
         $newcoin[$coinvalue] = self::minecoin($coinvalue);
         if (!self::savecoins($newcoin)) {
             $badsav++;
         }
         $number--;
     }
     if ($badsav) {
         gio::output("Failed to save {$badsav} mined coins!");
     }
     $endtime = time();
     $dur = $endtime - $starttime;
     $secs = $dur % 60;
     $mins = floor($dur / 60);
     $hrs = floor($mins / 60);
     $mins -= $hrs * 60;
     return array("Hours" => $hrs, "Minutes" => $mins, "Seconds" => $secs);
 }
Exemplo n.º 3
0
 private function _processrequest()
 {
     if (($this->msgsock = @socket_accept($this->sock)) === false) {
         gio::log("socket_accept() failed: reason: " . socket_strerror(socket_last_error($this->sock)), E_USER_ERROR);
         break;
     }
     gio::log("\nSocket: CONNECTION RECEIVED", VERBOSE);
     $this->_welcome();
     do {
         if (false === ($buf = @socket_read($this->msgsock, config::$maximumTransmissionLength, PHP_NORMAL_READ))) {
             gio::log("socket_read() failed: reason: " . socket_strerror(socket_last_error($this->msgsock)), E_USER_WARNING);
             break;
         }
         if (!($buf = trim($buf))) {
             continue;
         }
         gio::log("NEW REQUEST RECEIVED", VERBOSE);
         if ($buf == 'quit') {
             gio::log("Socket: Connection terminated by client!", VERBOSE);
             break;
         }
         if ($buf == 'restart') {
             $this->restart = true;
             gio::log("Socket: Restarting the server !!!", VERBOSE);
             $buf = 'shutdown';
         }
         if ($buf == 'shutdown') {
             gio::output("Processing request to stop the server ...");
             self::_out("Shuting down...");
             self::_flush();
             $this->stop = true;
             break;
         }
         if (!$this->stop) {
             $sockreply = Gmsg::process($buf);
             $this->_out($sockreply);
             $this->_flush();
             gio::log("Socket: REPLY SENT\n", VERBOSE);
         } else {
             $this->_out("...Terminating Server Process...");
             break;
         }
     } while (true);
     socket_close($this->msgsock);
     unset($this->msgsock);
     gio::log("\nSocket: CONNECTION ENDED", VERBOSE);
 }
Exemplo n.º 4
0
 public static function give($to, $amt)
 {
     $ramt = $amt;
     $rem = mine::countcoins($coins);
     if ($rem < $amt) {
         gio::output("Not enough coins");
         return false;
     }
     $paths = Tools::address($to);
     if (!$paths) {
         gio::output("The destination account is invalid");
         return false;
     }
     $getcoins = array();
     $c = 1;
     if (!is_array($coins)) {
         return false;
     }
     $vals = array_keys($coins);
     rsort($vals);
     foreach ($vals as $val) {
         if ($amt < $val) {
             continue;
         }
         if ($amt <= 0) {
             break;
         }
         $ch[$val] = floor($amt / $val);
         if ($ch[$val] > count($coins[$val])) {
             $ch[$val] = count($coins[$val]);
         }
         $amt -= $ch[$val] * $val;
     }
     foreach ($ch as $v => $n) {
         while ($n > 0 && (list($id, $coin) = each($coins[$v]))) {
             $getcoins[$id] = $coin;
             unset($coins[$v][$id]);
             $n--;
         }
     }
     foreach ($getcoins as $k => $v) {
         $secret[$k] = Tools::makesecrets();
         $getcoins[$k]['secret'] = GsonCrypt::seal($secret[$k], $paths['srakey']);
     }
     $net = new Gnet();
     if (!$net->connect($paths['address'], intval($paths['port']))) {
         gio::output("Unable to connect to the destination account");
         return false;
     } else {
         $m = Gmsg::create(Gmsg::prepare($getcoins, "deposit", $paths['account']));
         $m = GsonCrypt::seal($m, $paths['bank']);
         if (!$m) {
             gio::output("Unable to send!");
             gio::output("POSSIBLE CAUSES:");
             gio::output("The destination bank's certificate is not avaiable!");
             gio::output("Account may not be registered with sra!");
             gio::output("Account may have been deregistered with sra!");
             return false;
         }
         $r = $net->send($m);
         $s = GsonCrypt::unseal($r);
         $r = $s ? Gmsg::extract($s) : Gmsg::extract($r);
         unset($net);
         if (!$r || !$r['status']) {
             gio::output("Deposit of {$ramt} coins to {$to} Failed!");
             gio::output($r['response']);
             return false;
         } else {
             $old = storage::load($paths['srakey']);
             foreach ($getcoins as $id => $coin) {
                 $getcoins[$id]['secret'] = $secret[$id];
                 $val = $coin['value'];
                 $old[$val][$id] =& $getcoins[$id];
             }
             storage::save($old, $paths['srakey']);
             storage::save($coins);
             gio::output("Deposit of {$ramt} coins to {$to} was successful");
             return true;
         }
     }
 }
Exemplo n.º 5
0
function main($mainmsg)
{
    gio::output(config::$walletMessage);
    if (account::isCreated()) {
        $serv = popen("service.ecsh", 'r');
    }
    $net = new Gnet();
    $inerr = 0;
    while (true) {
        $inmsg = array();
        if (account::isCreated()) {
            $inmsg["d"] = "Destroy Account";
            $dmsg = $mainmsg + $inmsg;
        } else {
            $inmsg["c"] = "Create Bank Wallet";
            $dmsg = $inmsg;
        }
        $dmsg["x"] = "Exit";
        if (!$inerr) {
            gio::output();
            foreach ($dmsg as $k => $v) {
                $k = strtoupper($k);
                gio::output("{$k} - {$v}");
            }
            gio::output();
            $msg = "Enter your choice and press enter";
        }
        $inerr = 0;
        $c = gio::input("{$msg}");
        if (!array_key_exists($c, $dmsg)) {
            $c = null;
        }
        switch ($c) {
            case 1:
                $a = account::address();
                gio::output("Bank's account address is: {$a}");
                break;
            case 2:
                $n = account::coins(config::$accountId, $coins);
                gio::output("You have {$n} coins");
                if ($n && gio::confirm("Do you wish to see their Id's")) {
                    foreach ($coins as $val => $ccs) {
                        foreach ($ccs as $i => $coin) {
                            gio::output("Ecash ID: {$i}");
                            foreach ($coin as $id => $c) {
                                if ($id == "token") {
                                    $c = md5($c);
                                }
                                if ($id == "hash") {
                                    $c = sha1($c);
                                }
                                if ($id == "mined") {
                                    $c = date(config::$timeFormat, $c);
                                }
                                gio::output("{$id}=>{$c}");
                            }
                            gio::output();
                        }
                    }
                }
                break;
            case 3:
                $m = transaction::prequest();
                gio::output($m[1]);
                break;
            case 4:
                transaction::pgrant();
                break;
            case 5:
                $acc = gio::input("Enter the wallet number [EMPTY for bank's self]");
                $o = gio::input("Enter the order id [EMPTY for all]");
                $f = null;
                if (gio::confirm("Do you want to create a file on your desktop")) {
                    do {
                        $f = gio::input("Enter the file name");
                    } while (!$f);
                }
                transaction::reports($o, $acc, $f);
                break;
            case 6:
                $m = transaction::clearrequests(null, null);
                gio::output($m[1]);
                break;
            case 7:
                account::merckey(gio::input("Enter the name of the file to write to your desktop"));
                break;
            case "c":
                $serv = account::create();
                break;
            case "d":
                gio::output("This action will irreversibly destroy your wallet and its contents");
                if (gio::confirm("Are you sure you want to destroy your account")) {
                    account::destroy();
                }
                break;
            case "x":
                @$net->send('shutdown');
                $net = null;
                if ($serv) {
                    $ret = pclose($serv);
                }
                break 2;
            default:
                $inerr = 1;
                $msg = "Retry";
        }
        if (!$inerr) {
            gio::output("\n\n\n");
        }
    }
    if (isset($ret) && $ret != 0) {
        gio::output("An error occured while exiting...");
    }
    gio::output(config::$exitMessage);
    sleep(3);
}
Exemplo n.º 6
0
function main($mainmsg)
{
    gio::output(config::$walletMessage);
    $serv = popen("service.ecsh", 'r');
    $net = new Gnet();
    $inerr = 0;
    while (true) {
        if (!$inerr) {
            gio::output();
            foreach ($mainmsg as $k => $v) {
                gio::output("{$k} - {$v}");
            }
            gio::output();
            $msg = "Enter your choice and press enter";
        }
        $inerr = 0;
        $c = gio::input("{$msg}");
        switch ($c) {
            case 1:
                $tid = gio::input("Enter the transaction Id");
                $st = transaction::status($tid);
                if (!$st) {
                    gio::output("Transaction reference '{$tid}' not found");
                } else {
                    $rpt['order id'] = $st['order id'];
                    $rpt['order amount'] = $st['amount'];
                    $rpt['from'] = $st['client'];
                    $rpt['to'] = $st['merchant'];
                    $rpt['transaction time'] = date(config::$timeFormat, $st['acknowledgement']['completed']);
                    $rpt['description'] = $st['description'];
                    $rpt['transaction reference'] = $st['acknowledgement']['id'];
                    $rpt['response code'] = $st['acknowledgement']['status'];
                    $rpt['response message'] = $st['acknowledgement']['message'];
                    $rpt['ecash value tendered'] = $st['acknowledgement']['amount'];
                    $rpt['ecash tendered'] = $st['acknowledgement']['ecashids'];
                    gio::display($rpt);
                    if (gio::confirm("Do you want to save the report to a file")) {
                        $dest = gio::input("Enter full path to the file");
                        gio::display($rpt, $x);
                        if (!gio::saverawfile(join("\r\n", $x), $dest)) {
                            gio::output("Could not save the report to {$dest}");
                        } else {
                            gio::output("Report successfully saved to {$dest}");
                        }
                        unset($x);
                    }
                }
                break;
            case 2:
                $maxallowed = 1000;
                $v = gio::input("What value of eCash do you wish to mine", "integer");
                $n = gio::input("How many eCashes do you wish to mine", "integer");
                $c = mine::countcoins($null);
                unset($null);
                if ($n > $maxallowed || $c + $n > $maxallowed) {
                    $rem = $maxallowed - $c;
                    gio::output("You can not mine above {$maxallowed} eCashes!");
                    gio::output("You can mine {$rem} more eCashes!");
                } else {
                    $res = mine::coins($n, $v);
                    if ($res) {
                        gio::output("You have successfully mined {$n} eCashes.");
                        gio::output("Mining process took " . Tools::arrtostr($res, ", "));
                    } else {
                        gio::output("Mining operation failed!");
                    }
                }
                break;
            case 3:
                $n = mine::countcoins($coins);
                gio::output("A total of {$n} eCash value is available");
                if ($n && gio::confirm("Do you wish to see their Id's")) {
                    foreach ($coins as $val => $ccs) {
                        foreach ($ccs as $i => $coin) {
                            gio::output("Ecash ID: {$i}");
                            foreach ($coin as $id => $c) {
                                if ($id == "token") {
                                    $c = md5($c);
                                }
                                if ($id == "hash") {
                                    $c = sha1($c);
                                }
                                if ($id == "mined") {
                                    $c = date(config::$timeFormat, $c);
                                }
                                gio::output("{$id}=>{$c}");
                            }
                            gio::output();
                        }
                    }
                }
                break;
            case 4:
                $to = gio::input("Enter the wallet address");
                $amt = gio::input("Enter the amount to give");
                $res = account::give($to, $amt);
                break;
            case "x":
                $net->connect(config::$serviceAddress, intval(config::$servicePort));
                $net->send("shutdown");
                if ($serv) {
                    $ret = pclose($serv);
                }
                break 2;
            default:
                $inerr = 1;
                $msg = "Retry";
        }
        if (!$inerr) {
            gio::output("\n\n\n");
        }
    }
    if (isset($ret) && $ret != 0) {
        gio::output("An error occured while exiting...");
    }
    gio::output(config::$exitMessage);
    sleep(3);
}
Exemplo n.º 7
0
 public static function reports($oid = 0, $account = null, $file = "")
 {
     if (empty($account)) {
         $account = config::$accountId;
     }
     $r = self::status(array("oid" => $oid), $account, true);
     if (!$r[0]) {
         gio::output($r[1]);
     } else {
         foreach ($r[1] as $oid => $o) {
             $w['order id'] = $oid;
             $w['amount requested'] = $o['amount'];
             $w['description'] = $o['description'];
             $w['from'] = $o['client'];
             $w['to'] = $o['merchant'];
             $w['amount tendered'] = $o['acknowledgement']['amount'];
             $w['transaction time'] = date(config::$timeFormat, $o['acknowledgement']['completed']);
             $w['transaction reference'] = $o['acknowledgement']['id'];
             $w['response code'] = $o['acknowledgement']['status'];
             $w['response message'] = $o['acknowledgement']['message'];
             $w['ecash tendered'] = $o['acknowledgement']['ecashids'];
             if (isset($o['acknowledgement']['rejectedecash'])) {
                 $w['rejected ecash'] = $o['acknowledgement']['rejectedecash'];
             }
             if ($file) {
                 gio::display($w, $wr[]);
             } else {
                 gio::output();
                 gio::display($w);
                 gio::output();
             }
         }
         if ($file) {
             foreach ($wr as $k => $v) {
                 $wr[$k] = join("\r\n", $wr[$k]);
             }
             $file .= ".txt";
             $ofile = Tools::makepath(array(getenv('USERPROFILE'), "Desktop", "{$file}"));
             if (gio::saverawfile(join("\r\n\r\n\r\n", $wr), $ofile)) {
                 gio::output("The file {$file} was sucessfully written to your desktop");
             } else {
                 gio::output("Error while writing {$file} to your desktop");
             }
         }
     }
 }
Exemplo n.º 8
0
function main($mainmsg)
{
    gio::output(config::$walletMessage);
    $net = new Gnet();
    $inerr = 0;
    while (true) {
        $inmsg = array();
        if (account::isCreated()) {
            $inmsg["d"] = "Destroy Account";
            $dmsg = $mainmsg + $inmsg;
        } else {
            $inmsg["c"] = "Create Wallet Account [Merchant/Client]";
            $dmsg = $inmsg;
        }
        $dmsg["x"] = "Exit";
        if (!$inerr) {
            gio::output();
            foreach ($dmsg as $k => $v) {
                $k = strtoupper($k);
                gio::output("{$k} - {$v}");
            }
            gio::output();
            $msg = "Enter your choice and press enter";
        }
        $inerr = 0;
        $c = gio::input("{$msg}");
        if (!array_key_exists($c, $dmsg)) {
            $c = null;
        }
        switch ($c) {
            case 1:
                $a = account::address();
                gio::output("Your wallet address is: {$a}");
                break;
            case 2:
                $m = transaction::request();
                gio::output($m[1]);
                break;
            case 3:
                transaction::grant();
                break;
            case 4:
                $n = account::balance($coins);
                gio::output("Total value of eCash units: {$n}");
                if ($n && gio::confirm("Do you wish to see their Id's")) {
                    foreach ($coins as $val => $ccs) {
                        foreach ($ccs as $i => $coin) {
                            gio::output("Ecash ID: {$i}");
                            foreach ($coin as $id => $c) {
                                if ($id == "token") {
                                    $c = md5($c);
                                }
                                if ($id == "hash") {
                                    $c = sha1($c);
                                }
                                if ($id == "mined") {
                                    $c = date(config::$timeFormat, $c);
                                }
                                gio::output("{$id}=>{$c}");
                            }
                            gio::output();
                        }
                    }
                }
                break;
            case 5:
                $o = gio::input("Enter the order id [EMPTY for all]");
                $f = null;
                if (gio::confirm("Do you want to create a file on your desktop")) {
                    do {
                        $f = gio::input("Enter the file name");
                    } while (!$f);
                }
                transaction::reports($o, $f);
                break;
            case 6:
                transaction::clearallrequests();
                break;
            case 7:
                account::merckey(gio::input("Enter the name of the file to write to your desktop"));
                break;
            case 8:
                $maxallowed = 1000;
                $v = gio::input("What value of eCash do you wish to mine", "integer");
                $n = gio::input("How many eCashes do you wish to mine", "integer");
                $c = mine::countcoins($null);
                unset($null);
                if ($n > $maxallowed || $c + $n > $maxallowed) {
                    $rem = $maxallowed - $c;
                    gio::output("You can not mine above {$maxallowed} eCashes!");
                    gio::output("You can mine {$rem} more eCashes!");
                } else {
                    $res = mine::ecash($n, $v);
                    if ($res) {
                        gio::output("You have successfully mined {$n} eCashes.");
                        gio::output("Mining process took " . Tools::arrtostr($res, ", "));
                    } else {
                        gio::output("Mining operation failed!");
                    }
                }
                break;
            case "c":
                account::create();
                break;
            case "d":
                gio::output("This action will irreversibly destroy your wallet and its contents");
                if (gio::confirm("Are you sure you want to destroy your account")) {
                    account::destroy();
                }
                break;
            case "x":
                $net = null;
                break 2;
            default:
                $inerr = 1;
                $msg = "Retry";
        }
        if (!$inerr) {
            gio::output("\n\n\n");
        }
    }
    if (isset($ret) && $ret != 0) {
        gio::output("An error occured while exiting...");
    }
    gio::output(config::$exitMessage);
    sleep(3);
}
Exemplo n.º 9
0
 public static function merckey($file = "")
 {
     $s = GsonCrypt::sign(md5(gio::readfile(GsonCrypt::getcert())));
     if ($file) {
         $file .= ".txt";
         $ofile = Tools::makepath(array(getenv('USERPROFILE'), "Desktop", "{$file}"));
         if (gio::saverawfile("MERCHANT/ACCOUNT KEY: '{$s}'", $ofile)) {
             gio::output("The file {$file} was sucessfully written to your desktop");
         } else {
             gio::output("Error while writing {$file} to your desktop");
         }
     } else {
         return $s;
     }
 }