Advertisement
Guest User

Untitled

a guest
May 21st, 2017
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 17.38 KB | None | 0 0
  1. <?php
  2. class System {
  3.     public function __construct() { }
  4.     public function init() {
  5.         if(c('user')->is_signed_in() === true) {
  6.             c('template')->assign(array('signed_in_user' => c('user')->get_user()));
  7.         }
  8.         $u = c('uri')->u(NULL);
  9.         if($u !== false && !empty($u[0])) {
  10.             $m = $u[0];
  11.             unset($u[0]);
  12.             $a = $u;
  13.             if(method_exists(&$this, $m)) {
  14.                 return call_user_func_array(array(&$this, $m), $a);
  15.             }
  16.         }
  17.         return false;
  18.     }
  19.     public function account($option = NULL, $action = NULL, $data = NULL) {
  20.         if(!empty($option)) {
  21.             switch($option) {
  22.                 case 'register':
  23.                     if(c('user')->is_signed_in() === false) {
  24.                         if(isset($_POST['register'])) {
  25.                             // grab registration data if the form has been submitted
  26.                             $acc = array();
  27.                             $acc['first_name'] = addslashes(trim(preg_replace('/[^a-zA-Z\-\_0-9]+/', '', $_POST['firstname'])));
  28.                             $acc['last_name'] = addslashes(trim(preg_replace('/[^a-zA-Z\-\_0-9]+/', '', $_POST['lastname'])));
  29.                             $acc['password1'] = addslashes(trim($_POST['password1']));
  30.                             $acc['password2'] = addslashes(trim($_POST['password2']));
  31.                             $acc['username'] = addslashes(trim(preg_replace('/[^a-zA-Z\-\_0-9]+/', '', $_POST['username'])));
  32.                             $acc['email'] = addslashes(trim($_POST['email']));
  33.                             $e = 0;
  34.                             $errors[] = array();
  35.                             // cycle through and check data
  36.                             // incase user has javascript disabled
  37.                             if(empty($acc['first_name'])) {
  38.                                 // increment the error counter and set the error message
  39.                                 $e++;
  40.                                 $errors['firstname'] = 'This is a required field';
  41.                             }
  42.                             if(empty($acc['last_name'])) {
  43.                                 $e++;
  44.                                 $errors['lastname'] = 'This is a required field';
  45.                             }
  46.                             if(empty($acc['password1'])) {
  47.                                 $e++;
  48.                                 $errors['password1'] = 'This is a required field';
  49.                             }
  50.                             if(empty($acc['password2'])) {
  51.                                 $e++;
  52.                                 $errors['password2'] = 'This is a required field';
  53.                             }
  54.                             if(!empty($acc['password1']) && !empty($acc['password2']) && $acc['password1'] !== $acc['password2']) {
  55.                                 $e++;
  56.                                 $errors['password1'] = 'Passwords do not match';
  57.                                 $errors['password2'] = '';
  58.                                 $acc['password1'] = '';
  59.                                 $acc['password2'] = '';
  60.                             }
  61.                             if(empty($acc['username'])) {
  62.                                 $e++;
  63.                                 $errors['username'] = 'This is a required field';
  64.                             } elseif(c('user')->is_username($acc['username'])) {
  65.                                 // if an account already exists with this username
  66.                                 $e++;
  67.                                 $errors['username'] = 'Username already taken';
  68.                                 $acc['username'] = '';
  69.                             }
  70.                             if(empty($acc['email'])) {
  71.                                 $e++;
  72.                                 $errors['email'] = 'This is a required field';
  73.                             } 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'])) {
  74.                                 // if the email address isn't in the correct format
  75.                                 $e++;
  76.                                 $errors['email'] = 'Email address not valid';
  77.                                 $acc['email'] = '';
  78.                             } elseif(c('user')->is_email($acc['email'])) {
  79.                                 // if an account alread exists with this email address
  80.                                 $e++;
  81.                                 $errors['email'] = 'Email address already used';
  82.                                 $acc['email'] = '';
  83.                             }
  84.                             // if there were no errors then continue the registration
  85.                             // there is no need to check to see if errors were set
  86.                             // the system will presume errors are present
  87.                             if($e === 0) {
  88.                                 // encrypt the password
  89.                                 $acc['password'] = c('user')->password($acc['password1']);
  90.                                 unset($acc['password1'], $acc['password2']);
  91.                                 $fields = array_keys($acc);
  92.                                 if(count($fields) > 0 && count($acc) > 0) {
  93.                                     // create the user account
  94.                                     $q = c('database')->insert("INSERT INTO `user_account` (".implode(', ', $fields).", created) VALUES ('".implode('\', \'', $acc)."', '".time()."')");
  95.                                     if($q->rowCount() === 1) {
  96.                                         // send account validation email
  97.                                         // inform the user how to complete their registration
  98.                                         if(c('user')->account_validation(c('database')->last_insert_id()) !== false) {
  99.                                             c('template')->assign(array('account' => $acc));
  100.                                             c('template')->display('account/registered');
  101.                                         }
  102.                                         return;
  103.                                     }
  104.                                 }
  105.                                 // if the system sees this line then there has been an error not worth explaining
  106.                                 $errors['none'] = 'Unexpected error';
  107.                             }
  108.                         }
  109.                         c('template')->assign(array('account' => $acc, 'errors' => $errors));
  110.                         c('template')->display('account/register');
  111.                     } else {
  112.                         header('Location: /account');
  113.                     }
  114.                 break;
  115.                 case 'activate':
  116.                     if(c('user')->is_signed_in() === false) {
  117.                         $code = '';
  118.                         if(!is_null($action)) {
  119.                             $code = $action;
  120.                         } elseif(isset($_POST['code'])) {
  121.                             $code = addslashes(trim($_POST['code']));
  122.                         }
  123.                         if(!empty($code)) {
  124.                             $q = c('database')->select("SELECT uid FROM user_activation WHERE code = '".$code."'");
  125.                             if($q !== false && $q->rowCount() === 1) {
  126.                                 $u = $q->fetch(PDO::FETCH_NUM);
  127.                                 $u = $u[0];
  128.                                 $q = c('database')->update("UPDATE user_account SET status = 1 WHERE id = ".$u);
  129.                                 if($q !== false && $q->rowCount() === 1) {
  130.                                     c('database')->delete("DELETE FROM user_activation WHERE uid = ".$u." AND code = '".$code."'");
  131.                                     c('template')->display('account/activated');
  132.                                     return;
  133.                                 }
  134.                             }
  135.                         }
  136.                         c('template')->display('account/activate');
  137.                     } else {
  138.                         header('Location: /account');
  139.                     }
  140.                 break;
  141.                 case 'signin':
  142.                     if(c('user')->is_signed_in() === false) {
  143.                         if(isset($_POST['signin'])) {
  144.                             // grab the user info and sanitise
  145.                             $username = addslashes(trim($_POST['username']));
  146.                             $password = addslashes(trim($_POST['password']));
  147.                             $e = 0;
  148.                             $errors = array();
  149.                             // cycle through and check for errors
  150.                             if(empty($username)) {
  151.                                 $e++;
  152.                                 $errors[] = '<img src="/images/invalid.png" /> Please Enter a Username';
  153.                             } elseif(c('user')->is_username($username) === false) {
  154.                                 $e++;
  155.                                 $errors[] = '<img src="/images/invalid.png" /> Username Not Found Within Our System';
  156.                             }
  157.                             if(empty($password)) {
  158.                                 $e++;
  159.                                 $errors[] = '<img src="/images/invalid.png" /> Please Enter a Password';
  160.                             }
  161.                             if($e === 0) {
  162.                                 // attempt to sign the user in if no errors were found
  163.                                 $signin = c('user')->signin($username, $password);
  164.                                 if($signin === false) {
  165.                                     // credentials are invalid
  166.                                     $e++;
  167.                                     $errors[] = '<img src="/images/invalid.png" /> Credentials Do Not Match Our Records';
  168.                                 } elseif($signin === 0) {
  169.                                     // non-activated account
  170.                                     $e++;
  171.                                     $errors[] = '<img src="/images/invalid.png" /> Your account hasn\'t been activated';
  172.                                 } elseif($signin === 2) {
  173.                                     // banned account
  174.                                     $e++;
  175.                                     $errors[] = '<img src="/images/invalid.png" /> Your account is banned';
  176.                                 } else {
  177.                                     // double check to make sure the user is loggin in
  178.                                     if(c('user')->is_signed_in() === true) {
  179.                                         // if they are show them their homepage
  180.                                         c('template')->assign(array('message' => 'Signing You In Now', 'uri' => '/account'));
  181.                                         c('template')->display('redirect');
  182.                                     } else {
  183.                                         // if they aren't, make them try again
  184.                                         c('template')->assign(array('errors' => array('<img src="/images/invalid.png" /> Unexpected Error')));
  185.                                         c('template')->display('account/signin');
  186.                                     }
  187.                                     return;
  188.                                 }
  189.                             }
  190.                         }
  191.                         // the system will only see this if all the previous
  192.                         // conditions aren't met
  193.                         c('template')->assign(array('errors' => $errors));
  194.                         c('template')->display('account/signin');
  195.                     } else {
  196.                         header('Location: /account');
  197.                     }
  198.                 break;
  199.                 case 'signout':
  200.                     if(c('user')->is_signed_in() === true) {
  201.                         if(c('user')->signout() === true) {
  202.                             c('template')->assign(array('message' => 'Signing you out now.', 'uri' => '/'));
  203.                             c('template')->display('redirect');
  204.                         }
  205.                     } else {
  206.                         c('template')->assign(array('message' => 'Signing you out now.', 'uri' => '/'));
  207.                         c('template')->display('redirect');
  208.                     }
  209.                 break;
  210.                 case 'password':
  211.                     if(c('user')->is_signed_in() === false) {
  212.                         if(is_null($action)) {
  213.                             if(isset($_POST['email'])) {
  214.                                 $email = addslashes(trim($_POST['email']));
  215.                                 $e = 0;
  216.                                 $errors = array();
  217.                                 if(empty($email)) {
  218.                                     $e++;
  219.                                     $errors[] = '<img src="/system/templates/images/invalid.png" /> Please enter an email address';
  220.                                 } elseif(c('user')->is_email($email) === false) {
  221.                                     $e++;
  222.                                     $errors[] = '<img src="/system/templates/images/invalid.png" /> Supplied email address not in use';
  223.                                 }
  224.                                 if($e === 0) {
  225.                                     $reset = c('user')->reset_password($email);
  226.                                     if($reset === true) {
  227.                                         c('template')->display('account/password-email');
  228.                                         return;
  229.                                     } elseif($reset === 'requested') {
  230.                                         c('template')->assign(array('requested' => true));
  231.                                         c('template')->display('account/password');
  232.                                         return;
  233.                                     }
  234.                                     $errors[] = '<img src="/system/templates/images/invalid.png" /> Unexpected error';
  235.                                 }
  236.                             }
  237.                         } else {
  238.                             if(!empty($action)) {
  239.                                 if(!isset($_POST['reset'])) {
  240.                                     c('template')->assign(array('form' => true, 'code' => $action));
  241.                                     c('template')->display('account/password');
  242.                                     return;
  243.                                 } else {
  244.                                     $password1 = addslashes(trim($_POST['password1']));
  245.                                     $password2 = addslashes(trim($_POST['password2']));
  246.                                     $e = 0;
  247.                                     $errors = array();
  248.                                     if(empty($password1)) {
  249.                                         $e++;
  250.                                         $errors[] = '<img src="/system/templates/images/invalid.png" /> Please enter a password';
  251.                                     }
  252.                                     if(empty($password2)) {
  253.                                         $e++;
  254.                                         $errors[] = '<img src="/system/templates/images/invalid.png" /> Please confirm the password';
  255.                                     }
  256.                                     if(!empty($password1) && !empty($password2)) {
  257.                                         if($password1 !== $password2) {
  258.                                             $e++;
  259.                                             $errors[] = '<img src="/system/templates/images/invalid.png" /> Passwords do not match';
  260.                                         }
  261.                                     }
  262.                                     if($e === 0) {
  263.                                         if(c('user')->reset_password(NULL, $action, $password1) === true) {
  264.                                             c('template')->display('account/password');
  265.                                             return;
  266.                                         }
  267.                                     }
  268.                                 }
  269.                             }
  270.                         }
  271.                         c('template')->assign(array('form' => true, 'errors' => $errors));
  272.                         c('template')->display('account/password-email');
  273.                     }
  274.                 break;
  275.             }
  276.         } else {
  277.             if(c('user')->is_signed_in() === true) {
  278.                 c('template')->display('account/home');
  279.             } else {
  280.                 header('Location: /account/signin');
  281.             }
  282.         }
  283.     }
  284.     public function newsletter($option = NULL, $action = NULL, $data = NULL) {
  285.         if(!is_null($option)) {
  286.             switch($option) {
  287.                 case 'signup':
  288.                     if(isset($_POST['signup'])) {
  289.                         if(c('user')->is_signed_in() === true) {
  290.                             if(($user = c('user')->get_user(NULL)) !== false) {
  291.                                 $signup = c('newsletter')->signup($user['email'], $user['first_name'].' '.$user['last_name'], $user['id']);
  292.                                 if($signup === true) {
  293.                                     c('template')->display('newsletter/registered');
  294.                                     return;
  295.                                 } elseif($signup === 'signedup') {
  296.                                    
  297.                                 }
  298.                             }
  299.                         } else {
  300.                             $name = addslashes(trim($_POST['name']));
  301.                             $email = addslashes(trim($_POST['email']));
  302.                             $e = 0;
  303.                             $errors = array();
  304.                             if(empty($name)) {
  305.                                 $e++;
  306.                                 $errors[] = '<img src="/system/templates/images/invalid.png" /> Please enter a name';
  307.                             }
  308.                             if(empty($email)) {
  309.                                 $e++;
  310.                                 $errors[] = '<img src="/system/templates/images/invalid.png" /> Please enter an email address';
  311.                             }
  312.                             if($e === 0) {
  313.                                 $signup = c('newsletter')->signup($email, $name, NULL);
  314.                                 if($signup === 'validate') {
  315.                                     c('template')->display('newsletter/validate');
  316.                                     return;
  317.                                 } elseif($signup === 'signedup') {
  318.                                     $errors[] = '<img src="/system/templates/images/invalid.png" /> There is already a subscription using the supplied email address';
  319.                                 }
  320.                             }
  321.                             c('template')->assign(array('errors' => $errors));
  322.                         }
  323.                     }
  324.                     c('template')->display('newsletter/register');
  325.                 break;
  326.                 case 'validate':
  327.                     if(c('user')->is_signed_in() === false) {
  328.                         $code = '';
  329.                         if(!is_null($action)) {
  330.                             $code = $action;
  331.                         } elseif(isset($_POST['code'])) {
  332.                             $code = addslashes(trim($_POST['code']));
  333.                         }
  334.                         if(!empty($code)) {
  335.                             $q = c('database')->select("SELECT nid FROM newsletter_validation WHERE code = '".$code."'");
  336.                             if($q !== false && $q->rowCount() === 1) {
  337.                                 $n = $q->fetch(PDO::FETCH_NUM);
  338.                                 $n = $n[0];
  339.                                 $q = c('database')->update("UPDATE newsletter_subscription SET status = 1 WHERE id = ".$n);
  340.                                 if($q !== false && $q->rowCount() === 1) {
  341.                                     c('database')->delete("DELETE FROM newsletter_validation WHERE nid = ".$n." AND code = '".$code."'");
  342.                                     c('template')->display('newsletter/validated');
  343.                                     return;
  344.                                 }
  345.                             }
  346.                         }
  347.                         c('template')->assign(array('form' => true));
  348.                         c('template')->display('newsletter/validate');
  349.                     } else {
  350.                         header('Location: /account');
  351.                     }
  352.                 break;
  353.                 case 'remove':
  354.                    
  355.                 break;
  356.             }
  357.         } else {
  358.            
  359.         }
  360.     }
  361.     public function properties($option = NULL, $action = NULL, $data = NULL) {
  362.         if(!is_null($option)) {
  363.             switch($option) {
  364.                 case 'search':
  365.                     if(isset($_POST['search'])) {
  366.                         $url = c('property')->make_search_url($_POST);
  367.                         header('Location: /properties/search/'.$url);
  368.                     } else {
  369.                         if(is_null($data)) {
  370.                             if(!is_null($action)) {
  371.                                 c('template')->assign(array('url' => $action));
  372.                                 $action = explode('_', $action);
  373.                                 if(!empty($action[1])) {
  374.                                     if($action[0] !== 'properties-for-sale') {
  375.                                         $filter = c('property')->get_search_url($action[0]);
  376.                                     }
  377.                                     $page = $action[1];
  378.                                 } elseif($action[0] !== 'properties-for-sale') {
  379.                                     $filter = c('property')->get_search_url($action[0]);
  380.                                     $page = 1;
  381.                                 } else {
  382.                                     $page = 1;
  383.                                 }
  384.                             } else {
  385.                                 $page = 1;
  386.                             }
  387.                             if(($properties = c('property')->get_properties($page)) !== false) {
  388.                                 if(isset($filter)) {
  389.                                     c('template')->assign(array('search' => $filter));
  390.                                 } else {
  391.                                     c('template')->assign(array('search' => true));
  392.                                 }
  393.                                 c('template')->assign(array('properties' => $properties));
  394.                                 c('template')->assign(array('pagination' => c('property')->get_d()));
  395.                                 c('template')->display('property/list');
  396.                                 return true;
  397.                             } else {
  398.                                 if(isset($filter)) {
  399.                                     c('template')->assign(array('search' => $filter));
  400.                                 } else {
  401.                                     c('template')->assign(array('search' => NULL));
  402.                                 }
  403.                                 c('template')->display('property/none');
  404.                                 return true;
  405.                             }
  406.                         } else {
  407.                             if($data === 'save') {
  408.                                 if(!is_null($action)) {
  409.                                     $action = explode('_', $action);
  410.                                     if(!empty($action[1])) {
  411.                                         if($action[0] !== 'properties-for-sale') {
  412.                                             c('user')->search_save(c('property')->get_search_url($action[0]));
  413.                                             header('Location: /properties/search/'.implode('_', $action));
  414.                                         }
  415.                                     } elseif($action[0] !== 'properties-for-sale') {
  416.                                         c('user')->search_save(c('property')->get_search_url($action[0]));
  417.                                         header('Location: /properties/search/'.implode('_', $action));
  418.                                     }
  419.                                 }
  420.                             }
  421.                         }
  422.                     }
  423.                 break;
  424.                 case 'property':
  425.                     if(!is_null($data)) {
  426.                         switch($data) {
  427.                             case 'update':
  428.                                 return true;
  429.                             break;
  430.                         }
  431.                     }
  432.                     if(!is_null($action)) {
  433.                         $action = explode('_', $action);
  434.                         if(!empty($action[0])) {
  435.                             $user = c('user')->get_user();
  436.                             $property = c('property')->get_property($action[0]);
  437.                             if($user !== false && $user['id'] === $property['uid']) {
  438.                                 c('template')->assign(array('owner' => true));
  439.                             }
  440.                             $property['url'] = implode('_', $action);
  441.                             c('template')->assign(array('property' => $property));
  442.                             c('template')->display('property/property');
  443.                         }
  444.                     }                  
  445.                 break;
  446.             }
  447.         }
  448.     }
  449.     public function ajax($option, $action) {
  450.         if(!empty($option)) {
  451.             switch($option) {
  452.                 case 'account':
  453.                     if(!empty($action)) {
  454.                         switch($action) {
  455.                             case 'username':
  456.                                 //echo '<img src="/system/templates/images/invalid.png" /> Username taken';
  457.                             break;
  458.                             case 'email':
  459.                                 //echo '<img src="/system/templates/images/invalid.png" /> Email address already used';
  460.                             break;
  461.                         }
  462.                     }
  463.                 break;
  464.             }
  465.         }
  466.     }
  467.     public static function make_options_array($string) {
  468.         if(!empty($string)) {
  469.             $split = preg_split("/=\'|\'\&|\'+/", $string, NULL, PREG_SPLIT_NO_EMPTY);
  470.             $array = array();
  471.             for($i=0;$i<count($split);$i++) {
  472.                 if(self::is_even($i)) {
  473.                     $array[$split[$i]] = $split[$i+1];
  474.                 }
  475.             }
  476.             if(is_array($array)) {
  477.                 return $array;
  478.             }
  479.         }
  480.         return false;
  481.     }
  482.     public static function is_odd($number) { return ($number & 1); }
  483.     public static function is_even($number) { return (!($number & 1)); }   
  484. }
  485. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement