You Thought:
+
+ =$post['first_name']?> =$post['last_name']?> ... thought:
+
+
+
+
=$post['first_name']?> =$post['last_name']?> ... thought:
+ + + +=$post['content']?>
+ + +broadcasted on:
+ + + user_id): ?> + '> + + + +diff --git a/controllers/c_base.php b/controllers/c_base.php
new file mode 100755
index 0000000..f98b448
--- /dev/null
+++ b/controllers/c_base.php
@@ -0,0 +1,31 @@
+userObj = new User();
+
+ // authenticate / load user
+ $this->user = $this->userObj->authenticate();
+
+ // set up templates
+ $this->template = View::instance('_v_template');
+ $this->email_template = View::instance('_v_email');
+
+ // so we can use $user in views
+ $this->template->set_global('user', $this->user);
+
+ }
+
+} // eoc
diff --git a/controllers/c_index.php b/controllers/c_index.php
new file mode 100755
index 0000000..9f2a45d
--- /dev/null
+++ b/controllers/c_index.php
@@ -0,0 +1,29 @@
+template->content = View::instance('v_index_index');
+ $this->template->content->loginContent = View::instance('v_users_login');
+
+ $this->template->title = "ThoughtBranch";
+
+ // render the view
+ echo $this->template;
+
+ }
+
+
+} // eoc
diff --git a/controllers/c_posts.php b/controllers/c_posts.php
new file mode 100755
index 0000000..9889c62
--- /dev/null
+++ b/controllers/c_posts.php
@@ -0,0 +1,202 @@
+user) {
+ Router::redirect("/");
+ }
+ }
+
+/*-------------------------------------------------------------------------------------------------
+ display new post form
+-------------------------------------------------------------------------------------------------*/
+
+ public function add() {
+
+ // setup view
+ $this->template->content = View::instance('v_posts_add');
+ $this->template->title = "New Thought";
+
+ // render template
+ echo $this->template;
+
+ }
+
+/*-------------------------------------------------------------------------------------------------
+ process new posts
+-------------------------------------------------------------------------------------------------*/
+
+ public function p_add() {
+
+ // associate this post with this user
+ $_POST['user_id'] = $this->user->user_id;
+
+ // unix timestamp of when this post was created / modified
+ $_POST['created'] = Time::now();
+ $_POST['modified'] = Time::now();
+
+ // insert
+ // Note we didn't have to sanitize any of the $_POST data because we're using the insert method which does it for us
+ DB::instance(DB_NAME)->insert('posts', $_POST);
+
+ // quick and dirty feedback
+ // echo "Your post has been added. Add another";
+
+ // setup view
+ $this->template->content = View::instance('v_posts_p_add');
+ $this->template->title = "Thought Added";
+
+ // render view
+ echo $this->template;
+
+ }
+
+/*-------------------------------------------------------------------------------------------------
+ view all posts
+-------------------------------------------------------------------------------------------------*/
+
+ public function index() {
+
+ // set up the View
+ $this->template->content = View::instance('v_posts_index');
+ $this->template->title = "Thought Bank";
+
+ // query
+ $q = 'SELECT
+ posts.post_id,
+ posts.content,
+ posts.created,
+ posts.user_id AS post_user_id,
+ users_users.user_id AS follower_id,
+ users.user_id,
+ users.first_name,
+ users.last_name
+ FROM posts
+ INNER JOIN users_users
+ ON posts.user_id = users_users.user_id_followed
+ INNER JOIN users
+ ON posts.user_id = users.user_id
+ WHERE users_users.user_id = '.$this->user->user_id .'
+ ORDER BY posts.created DESC';
+
+ // run the query, store results in the variable $posts
+ $posts = DB::instance(DB_NAME)->select_rows($q);
+
+ // pass data to the View
+ $this->template->content->posts = $posts;
+
+ // render the View
+ echo $this->template;
+
+ }
+
+/*-------------------------------------------------------------------------------------------------
+
+-------------------------------------------------------------------------------------------------*/
+
+ public function users() {
+
+ // set up the View
+ $this->template->content = View::instance("v_posts_users");
+ $this->template->title = "Follow Users";
+
+ // build the query to get all users
+ $q = "SELECT *
+ FROM users";
+
+ // execute the query to get all the users
+ // store the result array in the variable $users
+ $users = DB::instance(DB_NAME)->select_rows($q);
+
+ // build the query to figure out what connections does this user already have?
+ // example: who are they following
+ $q = "SELECT *
+ FROM users_users
+ WHERE user_id = ".$this->user->user_id;
+
+ // execute this query with the select_array method
+ // select_array will return our results in an array and use the "users_id_followed" field as the index.
+ // this will come in handy when we get to the view
+ // store our results (an array) in the variable $connections
+ $connections = DB::instance(DB_NAME)->select_array($q, 'user_id_followed');
+
+ // pass data (users and connections) to the view
+ $this->template->content->users = $users;
+ $this->template->content->connections = $connections;
+
+ // render the view
+ echo $this->template;
+
+ }
+
+/*-------------------------------------------------------------------------------------------------
+ creates a row in the users_users table representing that one user is following another
+-------------------------------------------------------------------------------------------------*/
+
+ public function follow($user_id_followed) {
+
+ // prepare the data array to be inserted
+ $data = Array(
+ "created" => Time::now(),
+ "user_id" => $this->user->user_id,
+ "user_id_followed" => $user_id_followed
+ );
+
+ // do the insert
+ DB::instance(DB_NAME)->insert('users_users', $data);
+
+ // send them back
+ Router::redirect("/posts/users");
+
+ }
+
+/*-------------------------------------------------------------------------------------------------
+ removes the specified row in the users_users table, removing the follow between two users
+-------------------------------------------------------------------------------------------------*/
+
+ public function unfollow($user_id_followed) {
+
+ // delete this connection
+ $where_condition = 'WHERE user_id = '.$this->user->user_id.' AND user_id_followed = '.$user_id_followed;
+ DB::instance(DB_NAME)->delete('users_users', $where_condition);
+
+ // send them back
+ Router::redirect("/posts/users");
+
+ }
+
+/*-------------------------------------------------------------------------------------------------
+ confirm delete
+-------------------------------------------------------------------------------------------------*/
+
+ public function delete($post_id) {
+
+ // setup view
+ $this->template->content = View::instance('v_post_delete');
+ $this->template->content->post_id = $post_id;
+
+ // render view
+ echo $this->template;
+
+ }
+
+/*-------------------------------------------------------------------------------------------------
+ delete post
+-------------------------------------------------------------------------------------------------*/
+
+ public function p_delete($post_id) {
+
+ // delete this connection
+ $where_condition = 'WHERE post_id = '.$post_id;
+ DB::instance(DB_NAME)->delete('posts', $where_condition);
+
+ // send them back
+ Router::redirect("/posts");
+
+ }
+
+} // eoc
\ No newline at end of file
diff --git a/controllers/c_users.php b/controllers/c_users.php
new file mode 100755
index 0000000..1b087bb
--- /dev/null
+++ b/controllers/c_users.php
@@ -0,0 +1,353 @@
+template->content = View::instance('v_users_signup');
+
+ // set page title
+ $this->template->title = "Sign up";
+
+ // render view
+ echo $this->template;
+ exit;
+
+ }
+
+/*-------------------------------------------------------------------------------------------------
+ process the sign up form
+-------------------------------------------------------------------------------------------------*/
+
+ public function p_signup() {
+
+ // setup view
+ $this->template->content = View::instance('v_users_signup');
+
+ // initial error to false
+ $error = false;
+
+ // initiate error
+ $this->template->content->error = '
';
+
+ // if we have no post data just display the View with signup form
+ if(!$_POST) {
+ echo $this->template;
+ return;
+ }
+
+ // otherwise...
+ // loop through the POST data
+ foreach($_POST as $field_name => $value) {
+
+ // if a field was blank, add a message to the error View variable
+ if(trim($value) == "") {
+ $error = true;
+ $this->template->content->error = 'All fields are required.';
+ }
+ }
+
+ // check whether this user's email already exists (sanitize input first)
+ $_POST = DB::instance(DB_NAME)->sanitize($_POST);
+ $exists = DB::instance(DB_NAME)->select_field("SELECT email FROM users WHERE email = '" . $_POST['email'] . "'");
+
+ if (isset($exists)) {
+ $error = true;
+ $this->template->content->error = 'This email address has already been registered, please try again.';
+ echo $this->template;
+ }
+
+ // check if password is typed correctly
+ else if ($_POST['password'] != $_POST['retype']) {
+ $error = true;
+ $this->template->content->error = 'Password fields don't match.';
+ echo $this->template;
+ }
+
+ // if no previous errors, add to database
+ else if(!$error) {
+
+ // add XSS and html tag filtering
+ $firstname = $_POST['first_name'];
+ $firstname = strip_tags(htmlentities(stripslashes(nl2br($firstname)),ENT_NOQUOTES,"Utf-8"));
+
+ // add XSS and html tag filtering
+ $lastname = $_POST['last_name'];
+ $lastname = strip_tags(htmlentities(stripslashes(nl2br($lastname)),ENT_NOQUOTES,"Utf-8"));
+
+ // unset the 'retype' field (not needed in db)
+ unset($_POST['retype']);
+
+ // more data we want stored with the user
+ $_POST['created'] = Time::now();
+ $_POST['modified'] = Time::now();
+
+ // encrypt the password
+ $_POST['password'] = sha1(PASSWORD_SALT.$_POST['password']);
+
+ // create an encrypted token via their email address and a random string
+ $_POST['token'] = sha1(TOKEN_SALT.$_POST['email'].Utils::generate_random_string());
+
+ // insert this user into the database
+ $user_id = DB::instance(DB_NAME)->insert('users', $_POST);
+
+ // all users follow their own posts by default
+ $data = Array(
+ "created" => Time::now(),
+ "user_id" => $user_id,
+ "user_id_followed" => $user_id
+ );
+
+ // do the insert
+ DB::instance(DB_NAME)->insert('users_users', $data);
+
+ // log user in using the token we generated
+ setcookie("token", $_POST['token'], strtotime('+1 year'), '/');
+
+ // send an email a welcome message to the new user
+ // build a multi-dimension array of recipients of this email
+ $to[] = Array("name" => $_POST['first_name'], "email" => $_POST['email']);
+ $from = Array("name" => APP_NAME, "email" => APP_EMAIL);
+ $subject = "Welcome to ThoughtBranch";
+ $body = View::instance('e_users_welcome');
+
+ // Send email
+ Email::send($to, $from, $subject, $body, true, '');
+
+ // signup confirm
+ Router::redirect("/users/profile");
+ }
+ else {
+ echo $this->template;
+
+ }
+
+ }
+
+
+/*-------------------------------------------------------------------------------------------------
+ display a form so users can login
+-------------------------------------------------------------------------------------------------*/
+
+ public function login($error = NULL) {
+
+ // setup view
+ $this->template->content = View::instance('v_users_login');
+
+ // setup page title
+ $this->template->title = "Login";
+
+ // pass data to the view
+ $this->template->content->error = $error;
+
+ // render view
+ echo $this->template;
+
+ }
+
+/*-------------------------------------------------------------------------------------------------
+ process login form
+-------------------------------------------------------------------------------------------------*/
+
+ public function p_login() {
+
+ // sanitize the user entered data to prevent SQL Injection Attacks
+ $_POST = DB::instance(DB_NAME)->sanitize($_POST);
+
+ // hash submitted password so we can compare it against the one in the DB
+ $_POST['password'] = sha1(PASSWORD_SALT.$_POST['password']);
+
+ // search the DB for this email and password
+ // retrieve the token if it's available
+ $q = "SELECT token
+ FROM users
+ WHERE email = '".$_POST['email']."'
+ AND password = '".$_POST['password']."'";
+
+ $token = DB::instance(DB_NAME)->select_field($q);
+
+ // looks for matching token in DB
+ if(!$token) {
+
+ // token not found. Login failed, sends user back to login
+ Router::redirect("/users/login/error");
+
+ }
+ else {
+
+ setcookie("token", $token, strtotime('+1 year'), '/');
+
+ // token found, login successful
+ Router::redirect("/users/profile/");
+ // echo "You are logged in!";
+ }
+
+ }
+
+/*-------------------------------------------------------------------------------------------------
+ logout, redirects to "/"
+-------------------------------------------------------------------------------------------------*/
+
+ public function logout() {
+
+ // generate and save a new token for next login
+ $new_token = sha1(TOKEN_SALT.$this->user->email.Utils::generate_random_string());
+
+ // create the data array we'll use with the update method
+ // in this care, we're only updated one field, so our array only has one entry
+ $data = Array("token" => $new_token);
+
+ // do the update
+ DB::instance(DB_NAME)->update("users", $data, "WHERE token = '".$this->user->token."'");
+
+ // delete their token cookie by setting it to a date in the past - effectively logging them out
+ setcookie("token", "", strtotime('-1 year'), '/');
+
+ // send them back to the main index
+ Router::redirect("/");
+
+ }
+
+/*-------------------------------------------------------------------------------------------------
+
+-------------------------------------------------------------------------------------------------*/
+
+ public function profile($error = NULL) {
+
+ // if user is blank, they're not logged in; redirect them to the login page
+ if(!$this->user) {
+ Router::redirect('/users/login');
+ }
+
+ // if they weren't redirected away, continue:
+
+
+
+ // setup view
+ $this->template->content = View::instance('v_users_profile');
+ $this->template->title = $this->user->first_name .' ' . $this->user->last_name. " | Profile";
+
+ // pass errors, if any
+ $this->template->content->error = $error;
+
+ // render view
+ echo $this->template;
+
+ }
+
+/*-------------------------------------------------------------------------------------------------
+ profile upload avatar
+-------------------------------------------------------------------------------------------------*/
+
+ public function p_profile() {
+ // if user specified a new image file, upload it
+
+ if ($_FILES["file"]["error"] == 0)
+ {
+ //upload an image
+ $image = Upload::newupload($_FILES, "/uploads/avatars/", array("jpg", "JPG", "jpeg", "JPEG", "gif", "GIF", "png", "PNG"), $this->user->user_id);
+
+ if($image == 'Invalid file type.') {
+ // return an error
+ Router::redirect("/users/profile/error");
+ }
+ else {
+ // process the upload
+ $data = Array("image" => $image);
+ DB::instance(DB_NAME)->update("users", $data, "WHERE user_id = ".$this->user->user_id);
+
+ // resize the image
+ $imgObj = new Image($_SERVER["DOCUMENT_ROOT"] . '/uploads/avatars/' . $image);
+ $imgObj->resize(200,200, "crop");
+ $imgObj->save_image($_SERVER["DOCUMENT_ROOT"] . '/uploads/avatars/' . $image);
+ }
+ }
+ else
+ {
+ // return an error
+ Router::redirect("/users/profile/error");
+ }
+
+ // Redirect back to the profile page
+ router::redirect('/users/profile');
+ }
+
+
+/*-------------------------------------------------------------------------------------------------
+ unsubscribe page
+-------------------------------------------------------------------------------------------------*/
+
+ public function unsubscribe($error = NULL) {
+
+ // setup View
+ $this->template->title = "Delete Account";
+ $this->template->content = View::instance('v_users_unsubscribe');
+ $this->template->content->error = $error;
+
+ // render template
+ echo $this->template;
+
+ }
+
+/*-------------------------------------------------------------------------------------------------
+ process unsubscribe
+-------------------------------------------------------------------------------------------------*/
+
+ public function p_unsubscribe() {
+
+ $error = '';
+ if ($_POST['password'] != $_POST['conf_password']) {
+ $error = 'InvalidPassword';
+ }
+
+ else {
+ // sanitize the user entered data to prevent SQL Injection Attacks
+ $_POST = DB::instance(DB_NAME)->sanitize($_POST);
+
+ // hash submitted password so we can compare it against one in the db
+ $_POST['password'] = sha1(PASSWORD_SALT.$_POST['password']);
+
+ // search the db for this email and password
+ // retrieve the token if it's available
+ $q = "SELECT token
+ FROM users
+ WHERE email = '".$this->user->email."'
+ AND password = '".$_POST['password']."'";
+
+ $token = DB::instance(DB_NAME)->select_field($q);
+ if (!$token) {
+ $error = 'InvalidPassword';
+ }
+
+ else {
+ // all checks passed, now cleanup the DB from this user
+ // deletes user, their posts, and all connections in users_users
+ $w = 'WHERE user_id = '.$this->user->user_id;
+ DB::instance(DB_NAME)->delete('users', $w);
+ }
+
+ }
+ if ($error != '') {
+ Router::redirect("/users/unsubscribe/$error");
+ }
+
+ else {
+ Router::redirect("/");
+ }
+ }
+
+
+} // eoc
\ No newline at end of file
diff --git a/views/_v_email.php b/views/_v_email.php
new file mode 100755
index 0000000..e7bcc7f
--- /dev/null
+++ b/views/_v_email.php
@@ -0,0 +1 @@
+=$content?>
\ No newline at end of file
diff --git a/views/_v_template.php b/views/_v_template.php
new file mode 100755
index 0000000..7c2106b
--- /dev/null
+++ b/views/_v_template.php
@@ -0,0 +1,91 @@
+
+
+
+
+ Welcome to ThoughtBranch. Record your thoughts and share your mind with the world! +
+ + +Are you sure you want to delete this post? If not, click here to return.
+ + + ++ Click here to start following other users. +
+ + + + +=$post['content']?>
+ + +broadcasted on:
+ + + user_id): ?> + '> + + + +