Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class System {
- public function __construct() { }
- public function init() {
- if(c('user')->is_signed_in() === true) {
- c('template')->assign(array('signed_in_user' => c('user')->get_user()));
- }
- $u = c('uri')->u(NULL);
- if($u !== false && !empty($u[0])) {
- $m = $u[0];
- unset($u[0]);
- $a = $u;
- if(method_exists(&$this, $m)) {
- return call_user_func_array(array(&$this, $m), $a);
- }
- }
- return false;
- }
- public function account($option = NULL, $action = NULL, $data = NULL) {
- if(!empty($option)) {
- switch($option) {
- case 'register':
- if(c('user')->is_signed_in() === false) {
- if(isset($_POST['register'])) {
- // grab registration data if the form has been submitted
- $acc = array();
- $acc['first_name'] = addslashes(trim(preg_replace('/[^a-zA-Z\-\_0-9]+/', '', $_POST['firstname'])));
- $acc['last_name'] = addslashes(trim(preg_replace('/[^a-zA-Z\-\_0-9]+/', '', $_POST['lastname'])));
- $acc['password1'] = addslashes(trim($_POST['password1']));
- $acc['password2'] = addslashes(trim($_POST['password2']));
- $acc['username'] = addslashes(trim(preg_replace('/[^a-zA-Z\-\_0-9]+/', '', $_POST['username'])));
- $acc['email'] = addslashes(trim($_POST['email']));
- $e = 0;
- $errors[] = array();
- // cycle through and check data
- // incase user has javascript disabled
- if(empty($acc['first_name'])) {
- // increment the error counter and set the error message
- $e++;
- $errors['firstname'] = 'This is a required field';
- }
- if(empty($acc['last_name'])) {
- $e++;
- $errors['lastname'] = 'This is a required field';
- }
- if(empty($acc['password1'])) {
- $e++;
- $errors['password1'] = 'This is a required field';
- }
- if(empty($acc['password2'])) {
- $e++;
- $errors['password2'] = 'This is a required field';
- }
- if(!empty($acc['password1']) && !empty($acc['password2']) && $acc['password1'] !== $acc['password2']) {
- $e++;
- $errors['password1'] = 'Passwords do not match';
- $errors['password2'] = '';
- $acc['password1'] = '';
- $acc['password2'] = '';
- }
- if(empty($acc['username'])) {
- $e++;
- $errors['username'] = 'This is a required field';
- } elseif(c('user')->is_username($acc['username'])) {
- // if an account already exists with this username
- $e++;
- $errors['username'] = 'Username already taken';
- $acc['username'] = '';
- }
- if(empty($acc['email'])) {
- $e++;
- $errors['email'] = 'This is a required field';
- } elseif(!preg_match('/[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.(([0-9]{1,3})|([a-zA-Z]{2,3})|(aero|coop|info|museum|name))/', $acc['email'])) {
- // if the email address isn't in the correct format
- $e++;
- $errors['email'] = 'Email address not valid';
- $acc['email'] = '';
- } elseif(c('user')->is_email($acc['email'])) {
- // if an account alread exists with this email address
- $e++;
- $errors['email'] = 'Email address already used';
- $acc['email'] = '';
- }
- // if there were no errors then continue the registration
- // there is no need to check to see if errors were set
- // the system will presume errors are present
- if($e === 0) {
- // encrypt the password
- $acc['password'] = c('user')->password($acc['password1']);
- unset($acc['password1'], $acc['password2']);
- $fields = array_keys($acc);
- if(count($fields) > 0 && count($acc) > 0) {
- // create the user account
- $q = c('database')->insert("INSERT INTO `user_account` (".implode(', ', $fields).", created) VALUES ('".implode('\', \'', $acc)."', '".time()."')");
- if($q->rowCount() === 1) {
- // send account validation email
- // inform the user how to complete their registration
- if(c('user')->account_validation(c('database')->last_insert_id()) !== false) {
- c('template')->assign(array('account' => $acc));
- c('template')->display('account/registered');
- }
- return;
- }
- }
- // if the system sees this line then there has been an error not worth explaining
- $errors['none'] = 'Unexpected error';
- }
- }
- c('template')->assign(array('account' => $acc, 'errors' => $errors));
- c('template')->display('account/register');
- } else {
- header('Location: /account');
- }
- break;
- case 'activate':
- if(c('user')->is_signed_in() === false) {
- $code = '';
- if(!is_null($action)) {
- $code = $action;
- } elseif(isset($_POST['code'])) {
- $code = addslashes(trim($_POST['code']));
- }
- if(!empty($code)) {
- $q = c('database')->select("SELECT uid FROM user_activation WHERE code = '".$code."'");
- if($q !== false && $q->rowCount() === 1) {
- $u = $q->fetch(PDO::FETCH_NUM);
- $u = $u[0];
- $q = c('database')->update("UPDATE user_account SET status = 1 WHERE id = ".$u);
- if($q !== false && $q->rowCount() === 1) {
- c('database')->delete("DELETE FROM user_activation WHERE uid = ".$u." AND code = '".$code."'");
- c('template')->display('account/activated');
- return;
- }
- }
- }
- c('template')->display('account/activate');
- } else {
- header('Location: /account');
- }
- break;
- case 'signin':
- if(c('user')->is_signed_in() === false) {
- if(isset($_POST['signin'])) {
- // grab the user info and sanitise
- $username = addslashes(trim($_POST['username']));
- $password = addslashes(trim($_POST['password']));
- $e = 0;
- $errors = array();
- // cycle through and check for errors
- if(empty($username)) {
- $e++;
- $errors[] = '<img src="/images/invalid.png" /> Please Enter a Username';
- } elseif(c('user')->is_username($username) === false) {
- $e++;
- $errors[] = '<img src="/images/invalid.png" /> Username Not Found Within Our System';
- }
- if(empty($password)) {
- $e++;
- $errors[] = '<img src="/images/invalid.png" /> Please Enter a Password';
- }
- if($e === 0) {
- // attempt to sign the user in if no errors were found
- $signin = c('user')->signin($username, $password);
- if($signin === false) {
- // credentials are invalid
- $e++;
- $errors[] = '<img src="/images/invalid.png" /> Credentials Do Not Match Our Records';
- } elseif($signin === 0) {
- // non-activated account
- $e++;
- $errors[] = '<img src="/images/invalid.png" /> Your account hasn\'t been activated';
- } elseif($signin === 2) {
- // banned account
- $e++;
- $errors[] = '<img src="/images/invalid.png" /> Your account is banned';
- } else {
- // double check to make sure the user is loggin in
- if(c('user')->is_signed_in() === true) {
- // if they are show them their homepage
- c('template')->assign(array('message' => 'Signing You In Now', 'uri' => '/account'));
- c('template')->display('redirect');
- } else {
- // if they aren't, make them try again
- c('template')->assign(array('errors' => array('<img src="/images/invalid.png" /> Unexpected Error')));
- c('template')->display('account/signin');
- }
- return;
- }
- }
- }
- // the system will only see this if all the previous
- // conditions aren't met
- c('template')->assign(array('errors' => $errors));
- c('template')->display('account/signin');
- } else {
- header('Location: /account');
- }
- break;
- case 'signout':
- if(c('user')->is_signed_in() === true) {
- if(c('user')->signout() === true) {
- c('template')->assign(array('message' => 'Signing you out now.', 'uri' => '/'));
- c('template')->display('redirect');
- }
- } else {
- c('template')->assign(array('message' => 'Signing you out now.', 'uri' => '/'));
- c('template')->display('redirect');
- }
- break;
- case 'password':
- if(c('user')->is_signed_in() === false) {
- if(is_null($action)) {
- if(isset($_POST['email'])) {
- $email = addslashes(trim($_POST['email']));
- $e = 0;
- $errors = array();
- if(empty($email)) {
- $e++;
- $errors[] = '<img src="/system/templates/images/invalid.png" /> Please enter an email address';
- } elseif(c('user')->is_email($email) === false) {
- $e++;
- $errors[] = '<img src="/system/templates/images/invalid.png" /> Supplied email address not in use';
- }
- if($e === 0) {
- $reset = c('user')->reset_password($email);
- if($reset === true) {
- c('template')->display('account/password-email');
- return;
- } elseif($reset === 'requested') {
- c('template')->assign(array('requested' => true));
- c('template')->display('account/password');
- return;
- }
- $errors[] = '<img src="/system/templates/images/invalid.png" /> Unexpected error';
- }
- }
- } else {
- if(!empty($action)) {
- if(!isset($_POST['reset'])) {
- c('template')->assign(array('form' => true, 'code' => $action));
- c('template')->display('account/password');
- return;
- } else {
- $password1 = addslashes(trim($_POST['password1']));
- $password2 = addslashes(trim($_POST['password2']));
- $e = 0;
- $errors = array();
- if(empty($password1)) {
- $e++;
- $errors[] = '<img src="/system/templates/images/invalid.png" /> Please enter a password';
- }
- if(empty($password2)) {
- $e++;
- $errors[] = '<img src="/system/templates/images/invalid.png" /> Please confirm the password';
- }
- if(!empty($password1) && !empty($password2)) {
- if($password1 !== $password2) {
- $e++;
- $errors[] = '<img src="/system/templates/images/invalid.png" /> Passwords do not match';
- }
- }
- if($e === 0) {
- if(c('user')->reset_password(NULL, $action, $password1) === true) {
- c('template')->display('account/password');
- return;
- }
- }
- }
- }
- }
- c('template')->assign(array('form' => true, 'errors' => $errors));
- c('template')->display('account/password-email');
- }
- break;
- }
- } else {
- if(c('user')->is_signed_in() === true) {
- c('template')->display('account/home');
- } else {
- header('Location: /account/signin');
- }
- }
- }
- public function newsletter($option = NULL, $action = NULL, $data = NULL) {
- if(!is_null($option)) {
- switch($option) {
- case 'signup':
- if(isset($_POST['signup'])) {
- if(c('user')->is_signed_in() === true) {
- if(($user = c('user')->get_user(NULL)) !== false) {
- $signup = c('newsletter')->signup($user['email'], $user['first_name'].' '.$user['last_name'], $user['id']);
- if($signup === true) {
- c('template')->display('newsletter/registered');
- return;
- } elseif($signup === 'signedup') {
- }
- }
- } else {
- $name = addslashes(trim($_POST['name']));
- $email = addslashes(trim($_POST['email']));
- $e = 0;
- $errors = array();
- if(empty($name)) {
- $e++;
- $errors[] = '<img src="/system/templates/images/invalid.png" /> Please enter a name';
- }
- if(empty($email)) {
- $e++;
- $errors[] = '<img src="/system/templates/images/invalid.png" /> Please enter an email address';
- }
- if($e === 0) {
- $signup = c('newsletter')->signup($email, $name, NULL);
- if($signup === 'validate') {
- c('template')->display('newsletter/validate');
- return;
- } elseif($signup === 'signedup') {
- $errors[] = '<img src="/system/templates/images/invalid.png" /> There is already a subscription using the supplied email address';
- }
- }
- c('template')->assign(array('errors' => $errors));
- }
- }
- c('template')->display('newsletter/register');
- break;
- case 'validate':
- if(c('user')->is_signed_in() === false) {
- $code = '';
- if(!is_null($action)) {
- $code = $action;
- } elseif(isset($_POST['code'])) {
- $code = addslashes(trim($_POST['code']));
- }
- if(!empty($code)) {
- $q = c('database')->select("SELECT nid FROM newsletter_validation WHERE code = '".$code."'");
- if($q !== false && $q->rowCount() === 1) {
- $n = $q->fetch(PDO::FETCH_NUM);
- $n = $n[0];
- $q = c('database')->update("UPDATE newsletter_subscription SET status = 1 WHERE id = ".$n);
- if($q !== false && $q->rowCount() === 1) {
- c('database')->delete("DELETE FROM newsletter_validation WHERE nid = ".$n." AND code = '".$code."'");
- c('template')->display('newsletter/validated');
- return;
- }
- }
- }
- c('template')->assign(array('form' => true));
- c('template')->display('newsletter/validate');
- } else {
- header('Location: /account');
- }
- break;
- case 'remove':
- break;
- }
- } else {
- }
- }
- public function properties($option = NULL, $action = NULL, $data = NULL) {
- if(!is_null($option)) {
- switch($option) {
- case 'search':
- if(isset($_POST['search'])) {
- $url = c('property')->make_search_url($_POST);
- header('Location: /properties/search/'.$url);
- } else {
- if(is_null($data)) {
- if(!is_null($action)) {
- c('template')->assign(array('url' => $action));
- $action = explode('_', $action);
- if(!empty($action[1])) {
- if($action[0] !== 'properties-for-sale') {
- $filter = c('property')->get_search_url($action[0]);
- }
- $page = $action[1];
- } elseif($action[0] !== 'properties-for-sale') {
- $filter = c('property')->get_search_url($action[0]);
- $page = 1;
- } else {
- $page = 1;
- }
- } else {
- $page = 1;
- }
- if(($properties = c('property')->get_properties($page)) !== false) {
- if(isset($filter)) {
- c('template')->assign(array('search' => $filter));
- } else {
- c('template')->assign(array('search' => true));
- }
- c('template')->assign(array('properties' => $properties));
- c('template')->assign(array('pagination' => c('property')->get_d()));
- c('template')->display('property/list');
- return true;
- } else {
- if(isset($filter)) {
- c('template')->assign(array('search' => $filter));
- } else {
- c('template')->assign(array('search' => NULL));
- }
- c('template')->display('property/none');
- return true;
- }
- } else {
- if($data === 'save') {
- if(!is_null($action)) {
- $action = explode('_', $action);
- if(!empty($action[1])) {
- if($action[0] !== 'properties-for-sale') {
- c('user')->search_save(c('property')->get_search_url($action[0]));
- header('Location: /properties/search/'.implode('_', $action));
- }
- } elseif($action[0] !== 'properties-for-sale') {
- c('user')->search_save(c('property')->get_search_url($action[0]));
- header('Location: /properties/search/'.implode('_', $action));
- }
- }
- }
- }
- }
- break;
- case 'property':
- if(!is_null($data)) {
- switch($data) {
- case 'update':
- return true;
- break;
- }
- }
- if(!is_null($action)) {
- $action = explode('_', $action);
- if(!empty($action[0])) {
- $user = c('user')->get_user();
- $property = c('property')->get_property($action[0]);
- if($user !== false && $user['id'] === $property['uid']) {
- c('template')->assign(array('owner' => true));
- }
- $property['url'] = implode('_', $action);
- c('template')->assign(array('property' => $property));
- c('template')->display('property/property');
- }
- }
- break;
- }
- }
- }
- public function ajax($option, $action) {
- if(!empty($option)) {
- switch($option) {
- case 'account':
- if(!empty($action)) {
- switch($action) {
- case 'username':
- //echo '<img src="/system/templates/images/invalid.png" /> Username taken';
- break;
- case 'email':
- //echo '<img src="/system/templates/images/invalid.png" /> Email address already used';
- break;
- }
- }
- break;
- }
- }
- }
- public static function make_options_array($string) {
- if(!empty($string)) {
- $split = preg_split("/=\'|\'\&|\'+/", $string, NULL, PREG_SPLIT_NO_EMPTY);
- $array = array();
- for($i=0;$i<count($split);$i++) {
- if(self::is_even($i)) {
- $array[$split[$i]] = $split[$i+1];
- }
- }
- if(is_array($array)) {
- return $array;
- }
- }
- return false;
- }
- public static function is_odd($number) { return ($number & 1); }
- public static function is_even($number) { return (!($number & 1)); }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement