function getBitcoinAddr($btcBalance = null) { require_once 'chipin/bitcoin.php'; $a = '15Mux55YKsWp9pe5eUC2jcP5R9K7XA4pPF'; if ($btcBalance !== null) { DB\delete('bitcoin_addresses', 'address = ?', array($a)); DB\insertOne('bitcoin_addresses', array('address' => $a, 'satoshis' => $btcBalance * Bitcoin\satoshisPerBTC(), 'updated_at' => new DateTime('now'))); } return $a; }
function testGetBalanceUsesLocallyCachedValueWhenAppropriate() { $address = '1K7dyLY6arFRXBidQhrtnyqksqJZdj2F37'; $actualBalance = BlockchainDotInfo\getBalanceInSatoshis($address); $cachedBalance = $actualBalance + 1000; DB\delete('bitcoin_addresses', 'address = ?', array($address)); DB\insertOne('bitcoin_addresses', array('address' => $address, 'satoshis' => $cachedBalance, 'updated_at' => new DateTime('now'))); $balance = Bitcoin\getBalance($address, null); assertEqual($cachedBalance, $balance->numSatoshis); assertEqual($cachedBalance / Bitcoin\satoshisPerBTC(), $balance->numBTC); }
function signup() { $form = new F\Form('post'); $form->addSection('account-details', array(F\newBasicTextField('username', 'Username')->minLength(5, "Sorry, the minimum allowed length for a username is five characters.")->addValidation(function ($un) { try { User::loadFromUsername($un); return array("Sorry — that username's already taken! If it belongs to you, " . "you can <a href=\"/account/lost-pass\">reset your password</a> " . "or <a href=\"/account/signin\">try to login here</a>."); } catch (NoSuchUser $_) { return array(); } }), F\newEmailAddressField('email', "Email Address")->addValidation(function ($email) { try { User::loadFromEmailAddr($email); return array("It looks like you already have an account here, registered under " . "that email address. If you've forgotten your password, you can " . "<a href=\"/account/lost-pass\">reset it here</a>."); } catch (NoSuchUser $_) { return array(); } }), F\newPasswordField('password1', 'Password')->required('Please provide a password'), F\newPasswordField('password2', 'Re-enter password')->required('Please confirm your password by entering it again.')->shouldMatch('password1', "The two entered passwords do not match."), F\newCheckboxField('chipin-updates', '', $checked = false), F\newCheckboxField('memorydealers-updates', '', $checked = false))); $form->addSubmitButton('Register'); if ($this->isPostRequestAndFormIsValid($form)) { $username = $form->getValue("username"); $passwordHashed = Passwords\hash($form->getValue("password1")); $email = $form->getValue("email"); $recordValues = array('username' => $username, 'password' => $passwordHashed, 'email' => $email, 'created_at' => new DateTime('now')); $activeUser = $this->getActiveUser(); if (empty($activeUser)) { # In this case, the user is most likely registering using the straight-up registration # form (as opposed to the post-Wizard signup form). DB\insertOne('users', $recordValues); } else { # In this case there's an "active user", one which created a widget and used the # post-Wizard signup form to register following widget creation. DB\updateByID('users', $activeUser->id, $recordValues); } $user = User::loadFromUsername($username); $this->setAuthenticatedUser($user); DB\insertOne('subscriptions', array('user_id' => $user->id, 'chipin' => $form->getValue('chipin-updates') ? 1 : 0, 'memorydealers' => $form->getValue('memorydealers-updates') ? 1 : 0)); $this->userCreatedNotification($user); $this->saveInSession('successMessage', "You have successfully registered!"); return $this->redirect("/dashboard/index/"); } else { // $captcha = $this->getCaptchaTool(); $captcha = null; return $this->render('account/signup.php', array('form' => $form, 'captcha' => $captcha)); } }
function stepTwo() { $widget = $this->requireWidget(); if ($this->isPostRequest()) { $user = $this->getActiveUser(); if (empty($user)) { $uid = DB\insertOne('users', array('created_at' => new DateTime('now')), $returnId = true); $user = User::loadFromID($uid); $this->setActiveUser($user); } $widget->ownerID = $user->id; $widget->about = $_POST['about']; list($widget->width, $widget->height) = explode('x', $_POST['size']); $widget->color = $_POST['color']; $widget->countryCode = $this->getCountryCodeForIP(); $widget->save(); //$widget->updateProgress(); $this->storeWidgetInSession($widget); return $this->redirect("/widget-wiz/step-three?w={$widget->id}"); } else { return $this->renderStep('step-two.php', $widget); } }
/** * Store the balance of the given Bitcoin address to our local database for quick * access in the near future. */ function cacheBalance($address, $satoshis) { try { DB\transaction(function () use($address, $satoshis) { DB\delete('bitcoin_addresses', 'address = ?', array($address)); DB\insertOne('bitcoin_addresses', array('address' => $address, 'satoshis' => $satoshis, 'updated_at' => new DateTime('now'))); }); } catch (\PDOException $e) { # If it's an exception about a deadlock, we'll ignore it -- it's probably due to # two processes trying to update the record at the same time. if (!contains(strtolower($e->getMessage()), "deadlock found")) { throw $e; } } }
function generateAndSave(User $user) { $confCode = generate(10); DB\insertOne('confirmation_codes', array('user_id' => $user->id, 'code' => $confCode, 'created_at' => date("Y-m-d H:i:s"), 'expires' => date("Y-m-d H:i:s", strtotime("+3 days")))); return $confCode; }
function setPriceForBTC($currency, $price) { DB\delete('ticker_data', 'currency = ?', array($currency)); DB\insertOne('ticker_data', array('currency' => $currency, 'last_price' => $price)); }
private function setBalance($address, $btc) { DB\delete('bitcoin_addresses', 'address = ?', array($address)); DB\insertOne('bitcoin_addresses', array('address' => $address, 'satoshis' => $btc * Bitcoin\satoshisPerBTC(), 'updated_at' => new DateTime('now'))); }