/** * Allows for capturing fatal errors (missing includes, undefined functions etc). * Creates a new {@link FatalException} that wraps the PHP fatal error. */ function openclerk_exceptions_fatal_handler() { $error = error_get_last(); if ($error['type'] == E_ERROR || $error['type'] == E_CORE_ERROR || $error['type'] == E_COMPILE_ERROR) { log_uncaught_exception(new FatalException($error)); // events Events::trigger('exception_fatal', $error); } }
function renderJSON($arguments) { try { return \Openclerk\Cache::get(db(), $this->getEndpoint(), $this->getHash($arguments), $this->getAge(), array($this, 'getCached'), array($arguments)); } catch (\Exception $e) { // render an API exception // we wrap exceptions here, not in getCached(), because we don't want to be // caching exceptions or errors // TODO use an error HTTP code $json = array('success' => false, 'error' => $e->getMessage(), 'time' => date('c')); return json_encode($json); if (function_exists('log_uncaught_exception')) { log_uncaught_exception(new CaughtApiException("API threw '" . $e->getMessage() . "'", $e)); } } }
/** * Try and get the JSON result for this API, and return either * `{success: true: result: $json}` or * `{success: false, error: $message}` if an exception occured. * * This means this {@link Api} can be used directly with `openclerk/routing` * as a route callback: * <pre> * foreach (DiscoveredComponents\Apis::getAllInstances() as $uri => $handler) { * \Openclerk\Router::addRoutes(array( * $uri => $handler, * )); * } * </pre> * * If openclerk/exceptions is installed, logs a new uncaught * {@link CaughtApiException} if there was an exception that occured. * * Caching can be achieved with a {@link CachedApi}. */ function render($arguments) { header("Content-Type: application/json"); header("Access-Control-Allow-Origin: *"); try { $json = array('success' => true, 'result' => $this->getJSON($arguments)); echo json_encode($json); } catch (\Exception $e) { // render an API exception // TODO use an error HTTP code $json = array('success' => false, 'error' => $e->getMessage()); echo json_encode($json); if (function_exists('log_uncaught_exception')) { log_uncaught_exception(new CaughtApiException("API threw '" . $e->getMessage() . "'", $e)); } } }
throw new Exception("Fake login must be enabled through 'allow_fake_login' first."); } // login as a new user $query = db()->prepare("SELECT * FROM users WHERE id=? LIMIT 1"); $query->execute(array(require_get("id"))); if (!($user = $query->fetch())) { throw new Exception("No user account found: " . require_get("id")); } if ($user['is_admin']) { throw new Exception("Cannot login as an administrator"); } // create a log message class FakeLogin extends Exception { } log_uncaught_exception(new FakeLogin("Login emulated for user " . $user['id'])); // create new login key $user_key = sprintf("%04x%04x%04x%04x", rand(0, 0xffff), rand(0, 0xffff), rand(0, 0xffff), rand(0, 0xffff)); $query = db()->prepare("INSERT INTO valid_user_keys SET user_id=?, user_key=?, created_at=NOW()"); $query->execute(array($user["id"], $user_key)); // update session data $_SESSION["user_id"] = $user["id"]; $_SESSION["user_key"] = $user_key; $_SESSION["user_name"] = $user["name"]; $_SESSION["autologin_disable"] = 0; unset($_SESSION["autologin_disable"]); // remove any autologin setcookie('autologin_id', "", time() - 3600); setcookie('autologin_key', "", time() - 3600); // redirect to graphs page redirect(url_for('profile'));
{ return $this->title; } function load() { require __DIR__ . "/../locale/" . $this->key . ".php"; return $result; } } $locales = array('de' => 'German', 'fr' => 'French', 'jp' => 'Japanese', 'ru' => 'Russian', 'zh' => 'Chinese'); foreach ($locales as $locale => $title) { I18n::addAvailableLocale(new GenericLocale($locale, $title)); } I18n::addDefaultKeys(array(':site_name' => get_site_config('site_name'))); // set locale as necessary if (isset($_COOKIE["locale"]) && in_array($_COOKIE["locale"], array_keys(I18n::getAvailableLocales()))) { I18n::setLocale($_COOKIE["locale"]); } \Openclerk\Events::on('i18n_missing_string', function ($data) { $locale = $data['locale']; $key = $data['key']; log_uncaught_exception(new LocaleException("Locale '{$locale}': Missing key '{$key}'")); }); /** * Helper function to mark strings that need to be translated on the client-side. */ function ct($s) { // do not do any translation here - we have to do it on the client side! return $s; }
$new_address_id = db()->lastInsertId(); // create a new outstanding premium $q = db()->prepare("INSERT INTO outstanding_premiums SET user_id=:user_id, premium_address_id=:pid, address_id=:aid, balance=:balance, months=:months, years=:years"); $q->execute(array("user_id" => user_id(), "pid" => $address['id'], "aid" => $new_address_id, "balance" => $cost, "months" => $months, "years" => $years)); $purchase_id = db()->lastInsertId(); // address is now in use $q = db()->prepare("UPDATE premium_addresses SET is_used=1,used_at=NOW() WHERE id=?"); $q->execute(array($address['id'])); // try sending email, if an email address has been registered if ($user['email']) { send_user_email($user, "purchase", array("name" => $user['name'] ? $user['name'] : $user['email'], "amount" => number_format_autoprecision($cost), "currency" => get_currency_abbr($currency), "currency_name" => get_currency_name($currency), "address" => $address['address'], "explorer" => get_explorer_address($currency, $address['address']), "url" => absolute_url(url_for("user#user_outstanding")))); } // success! inform the user redirect(url_for('user#user_outstanding', array('new_purchase' => $purchase_id))); } catch (PurchaseException $e) { log_uncaught_exception($e); $errors[] = $e->getMessage(); } } page_header(t("Purchase Premium"), "page_purchase", array('js' => 'purchase')); ?> <h1><?php echo ht("Purchase Premium with :currency", array(':currency' => get_currency_name($currency))); ?> </h1> <div class="columns2"> <div class="column"> <form action="<?php echo htmlspecialchars(url_for('purchase'));