Permalink
Browse files

Pushing development progress.

Incomplete. Work in progress. Beware.
  • Loading branch information...
evansims committed Jun 9, 2012
1 parent 556466e commit 5843c87199af41081ef450fb2625c35e7c709636
View
@@ -0,0 +1,166 @@
+<?php
+
+ class Controllers {
+
+ public static function Home() {
+ $user = Service::$user;
+
+ $passwordChanged = (isset($user->password_last_changed) ? timeSince($user->password_last_changed) . ' ago' : 'never');
+ if(strpos($passwordChanged, 'year') || strpos($passwordChanged, 'month')) $passwordChanged .= '. You should change your password';
+
+ Views::Render("home", array(
+ 'user' => $user,
+ 'passwordChanged' => $passwordChanged,
+ 'accountRegistered' => timeSince($user->registered) . ' ago'
+ ));
+ Cleanup();
+ }
+
+ public static function Accounts() {
+ $user = &Service::$user;
+
+ $add_action_message = null;
+ $add_action_error = null;
+ $add_action_value = '';
+
+ $edit_action_message = null;
+ $edit_action_error = null;
+
+ if(isset($_POST['activity'])) {
+
+ if($_POST['activity'] == 'add') {
+ $ret = Service::registerAddress($_POST['email']);
+ if(isset($ret->success) && $ret->success) {
+ $add_action_message = 'Address registered successfully.';
+ $user->emails = $ret->emails;
+ } else {
+ $add_action_value = $_POST['email'];
+ if(isset($ret->error)) {
+ $add_action_error = $ret->error;
+ } else {
+ $add_action_error = 'There was a problem registering this address.';
+ }
+ }
+ } elseif($_POST['activity'] == 'promote') {
+ $ret = Service::promoteAddress($_POST['email']);
+ if(isset($ret->success) && $ret->success) {
+ $edit_action_message = "Address promoted successfully. {$_POST['email']} is now your primary account.";
+ $user->emails = $ret->emails;
+ } else {
+ if(isset($ret->error)) {
+ $edit_action_error = $ret->error;
+ } else {
+ $edit_action_error = 'There was a problem promoting this address.';
+ }
+ }
+ } elseif($_POST['activity'] == 'remove') {
+ $ret = Service::removeAddress($_POST['email']);
+ if(isset($ret->success) && $ret->success) {
+ $edit_action_message = "Address removed successfully.";
+ $user->emails = $ret->emails;
+ } else {
+ if(isset($ret->error)) {
+ $edit_action_error = $ret->error;
+ } else {
+ $edit_action_error = 'There was a problem removing this address.';
+ }
+ }
+ }
+
+ }
+
+ Views::Render("accounts", array(
+ 'user' => $user,
+ 'add_action_message' => $add_action_message,
+ 'add_action_error' => $add_action_error,
+ 'add_action_value' => $add_action_value,
+ 'edit_action_message' => $edit_action_message,
+ 'edit_action_error' => $edit_action_error
+ ));
+ Cleanup();
+ }
+
+ public static function Security() {
+ global $secretQuestions;
+
+ $questions = array();
+ for($i = 0; $i <= 10; $i++) {
+ $r = null;
+ while($r == null || isset($questions[$r])) {
+ $r = mt_rand(0, count($secretQuestions) - 1);
+ }
+ $questions[$r] = $secretQuestions[$r];
+ }
+ $user = &Service::$user;
+
+ $passwordChanged = (isset($user->password_last_changed) ? timeSince($user->password_last_changed) . ' ago' : 'never');
+ if(strpos($passwordChanged, 'year') || strpos($passwordChanged, 'month')) $passwordChanged .= '. You should change your password';
+
+ if(isset($_POST['activity'])) {
+
+ if($_POST['activity'] == 'password') {
+ $password = (isset($_POST['password']) ? $_POST['password'] : null);
+ $new_password = (isset($_POST['new_password']) ? $_POST['new_password'] : null);
+ $confirm_password = (isset($_POST['confirm_password']) ? $_POST['confirm_password'] : null);
+
+ $ret = Service::registerAddress($_POST['email']);
+ if(isset($ret->success) && $ret->success) {
+ $add_action_message = 'Address registered successfully.';
+ $user->emails = $ret->emails;
+ } else {
+ $add_action_value = $_POST['email'];
+ if(isset($ret->error)) {
+ $add_action_error = $ret->error;
+ } else {
+ $add_action_error = 'There was a problem registering this address.';
+ }
+ }
+ } elseif($_POST['activity'] == 'yubikey_pair') {
+ $ret = Service::promoteAddress($_POST['email']);
+ if(isset($ret->success) && $ret->success) {
+ $edit_action_message = "Address promoted successfully. {$_POST['email']} is now your primary account.";
+ $user->emails = $ret->emails;
+ } else {
+ if(isset($ret->error)) {
+ $edit_action_error = $ret->error;
+ } else {
+ $edit_action_error = 'There was a problem promoting this address.';
+ }
+ }
+ } elseif($_POST['activity'] == 'question') {
+ $ret = Service::removeAddress($_POST['email']);
+ if(isset($ret->success) && $ret->success) {
+ $edit_action_message = "Address removed successfully.";
+ $user->emails = $ret->emails;
+ } else {
+ if(isset($ret->error)) {
+ $edit_action_error = $ret->error;
+ } else {
+ $edit_action_error = 'There was a problem removing this address.';
+ }
+ }
+ }
+
+ }
+
+ Views::Render("security", array(
+ 'user' => $user,
+ 'questions' => $questions,
+ 'passwordChanged' => $passwordChanged
+ ));
+ Cleanup();
+ }
+
+ public static function Login() {
+ Views::Render("login");
+ Cleanup();
+ }
+
+ public static function Logout() {
+ Sessions::ResetCookie();
+ Sessions::storagePut('login_message', 'You have been successfully logged out.', true);
+ Views::Redirect('login');
+ }
+
+ }
+
View
@@ -0,0 +1,15 @@
+<?php
+
+ class Localize {
+
+ public static function contextPlural($string, $count, $singular, $plural, $none = null) {
+
+ if ( ! $none) $none = $string;
+
+ if ($count === 0) return str_replace(array(':count', ':context'), array($count, $plural), $none);
+ elseif ($count === 1) return str_replace(array(':count', ':context'), array($count, $singular), $string);
+ else return str_replace(array(':count', ':context'), array($count, $plural), $string);
+
+ }
+
+ }
View
@@ -0,0 +1,144 @@
+<?php
+
+ class Service {
+
+ public static $session;
+ public static $api;
+ public static $user;
+
+ public static function About() {
+ return Service::apiCall("GET", "/about");
+ }
+
+ public static function Register($email, $password) {
+
+ }
+
+ public static function Login($user_id, $password) {
+ return Service::apiCall("GET", "/user/{$user_id}/password", array('password' => $password));
+ }
+
+ public static function Session($user_id, $session_id) {
+ return Service::apiCall("GET", "/user/{$user_id}/sessions/{$session_id}");
+ }
+
+ public static function User($user_id) {
+ $user = Service::apiCall("GET", "/user/{$user_id}");
+ if($user && isset($user->user)) {
+ Service::$user = $user->user;
+ }
+ return $user;
+ }
+
+ public static function getEmails() {
+ return Service::apiCall("GET", "/user/:user_id/emails");
+ }
+
+ public static function getPasswordChangeDate() {
+ return Service::apiCall("GET", "/user/:user_id/password");
+ }
+
+ public static function registerAddress($address) {
+ return Service::apiCall("POST", "/user/:user_id/emails", array('email' => $address));
+ }
+
+ public static function promoteAddress($address) {
+ return Service::apiCall("PUT", "/user/:user_id/emails/{$address}", array('primary' => 1));
+ }
+
+ public static function removeAddress($address) {
+ return Service::apiCall("DELETE", "/user/:user_id/emails/{$address}");
+ }
+
+ public static function checkPassword($password) {
+ return Service::apiCall("GET", "/user/:user_id/password/", array('password' => $password));
+ }
+
+ public static function updatePassword($password) {
+ return Service::apiCall("POST", "/user/:user_id/password/", array('password' => $password));
+ }
+
+ public static function Admin() {
+ return false;
+ }
+
+ static function apiCall($method, $url, $params = array()) {
+
+ $api = &Service::$api;
+
+ if(!$api) {
+ $api = curl_init();
+ }
+
+ if ($api)
+ {
+ $params = array_merge(array(
+ 'api_secret' => CFG_API_KEY,
+ 'api_version' => '2'
+ ), $params);
+
+ if(strpos($url, ':') !== false && Sessions::$data) {
+ foreach(Sessions::$data as $skey => $sval) {
+ $url = @str_replace(":{$skey}", $sval, $url);
+ }
+ }
+
+ foreach($params as $key => $val) {
+ if(strpos($val, '%') !== false) {
+ foreach($session as $skey => $sval) {
+ $val = @str_replace("%{$skey}%", $sval, $val);
+ }
+ $params[$key] = $val;
+ }
+ }
+
+ if($method == 'GET') {
+ curl_setopt($api, CURLOPT_POST, false);
+ curl_setopt($api, CURLOPT_POSTFIELDS, NULL);
+ curl_setopt($api, CURLOPT_HTTPGET, true);
+
+ if(count($params)) {
+ $url .= '?';
+ foreach($params as $p => $v) $url .= $p . '=' . urlencode($v) . '&';
+ $url = rtrim($url, '&');
+ }
+ } elseif($method == 'POST') {
+ curl_setopt($api, CURLOPT_POST, true);
+ curl_setopt($api, CURLOPT_POSTFIELDS, $params);
+ } elseif($method == 'PUT') {
+ curl_setopt($api, CURLOPT_POST, false);
+ curl_setopt($api, CURLOPT_POSTFIELDS, NULL);
+ curl_setopt($api, CURLOPT_CUSTOMREQUEST, "PUT");
+
+ if(count($params)) {
+ $url .= '?';
+ foreach($params as $p => $v) $url .= $p . '=' . urlencode($v) . '&';
+ $url = rtrim($url, '&');
+ }
+ } elseif($method == 'DELETE') {
+ curl_setopt($api, CURLOPT_POST, false);
+ curl_setopt($api, CURLOPT_POSTFIELDS, NULL);
+ curl_setopt($api, CURLOPT_CUSTOMREQUEST, "DELETE");
+
+ if(count($params)) {
+ $url .= '?';
+ foreach($params as $p => $v) $url .= $p . '=' . urlencode($v) . '&';
+ $url = rtrim($url, '&');
+ }
+ }
+
+ curl_setopt($api, CURLOPT_URL, CFG_API_ENDPOINT . $url);
+
+ curl_setopt($api, CURLOPT_TIMEOUT, 3);
+
+ curl_setopt($api, CURLOPT_HEADER, false);
+ curl_setopt($api, CURLOPT_RETURNTRANSFER, true);
+
+ $raw = curl_exec($api);
+ $resp = json_decode($raw);
+ return $resp;
+ }
+
+ }
+
+ }
Oops, something went wrong.

0 comments on commit 5843c87

Please sign in to comment.