PageRenderTime 224ms CodeModel.GetById 120ms app.highlight 11ms RepoModel.GetById 90ms app.codeStats 0ms

/controller/user.php

https://bitbucket.org/lxa478/qcrt
PHP | 192 lines | 154 code | 34 blank | 4 comment | 29 complexity | 3a062d40c83075afbf8877df9d847b10 MD5 | raw file
  1<?php
  2	class user{
  3	
  4		function beforeRoute(){
  5			//Check Authentication
  6			if (!F3::get('SESSION.user') && F3::get('PARAMS[0]')!='/user/login' && F3::get('PARAMS[0]')!='/user/forgot'){
  7				F3::reroute('/user/login');
  8			}
  9			
 10			if(F3::get('SESSION.user') && F3::get('PARAMS[0]')!='/user/login' && F3::get('PARAMS[0]')!='/user/forgot'){
 11				$user = new Axon('user');
 12				$user->load('id="'.F3::get('SESSION.user').'"');
 13				
 14				if($user->active != 1){
 15					$this->logout();
 16				}	
 17			}
 18		}
 19		
 20		public function home(){
 21			F3::set('menu','home');
 22			F3::set('header','html/user/header.html');
 23			F3::set('content','html/user/home.html');
 24			F3::set('footer','html/user/footer.html');
 25			F3::set('js','js/user/home.js');
 26			F3::set('html_title','User Home Page');
 27			
 28			$stores = new Axon('store');
 29			$astores = $stores->afind('active=1 AND site_id="'.F3::get('SESSION.site').'"');
 30			F3::set('stores', $astores);
 31			
 32			if(F3::get('PARAMS["store_id"]')){
 33				$my_store = new Axon('store');
 34				$my_store->load('id="'.F3::get('PARAMS["store_id"]').'" AND site_id="'.F3::get('SESSION.site').'"');
 35				if($my_store->dry() || $my_store->active!=1){
 36					F3::reroute('/user');
 37				}else{
 38					F3::set('store', $my_store);
 39					
 40					$window = new Axon('window');
 41					F3::set('windows', $window->afind('store_id="'.F3::get('PARAMS["store_id"]').'" AND type="window" AND active="1"'));
 42					
 43					$view = new Axon('window');
 44					F3::set('views', $view->afind('store_id="'.F3::get('PARAMS["store_id"]').'" AND type="view" AND active="1"'));
 45					
 46					$door = new Axon('door');
 47					$door->afind('store_id="'.F3::get('PARAMS["store_id"]').'"');
 48					F3::set('doors', $door->afind('store_id="'.F3::get('PARAMS["store_id"]').'" AND active="1"'));
 49				}
 50			}
 51			
 52			//Get Thumbnail Function			
 53			F3::set('img_thumb', function($key){
 54				$path_parts = pathinfo($key);					
 55				return $path_parts['dirname'].'/'.$path_parts['filename'].'_thumb.'.$path_parts['extension'];
 56			});
 57			
 58			echo Template::serve('html/user/layout.html');
 59		}
 60		
 61		public function account(){
 62			if($_POST){
 63				$user = new Axon('user');
 64				$user->load('id="'.F3::get('SESSION.user').'"');
 65			
 66				$user->email = $_POST['email'];
 67				$user->firstname = $_POST['firstname'];
 68				$user->lastname = $_POST['lastname'];
 69			
 70				if($_POST['password']!=''){
 71					$user->password = sha1($_POST['password']);
 72				}
 73			
 74				$user->save();
 75			}else{	
 76				$user = new Axon('user');
 77				$user->load('id="'.F3::get('SESSION.user').'"');
 78			
 79				F3::set('user', $user);
 80			
 81				F3::set('menu','account');
 82				F3::set('header','html/user/header.html');
 83				F3::set('content','html/user/account.html');
 84				F3::set('footer','html/user/footer.html');
 85				F3::set('js','js/user/account.js');
 86				F3::set('html_title','User Account Page');
 87				echo Template::serve('html/user/layout.html');
 88			}
 89		}
 90		
 91		public function checkUserEmail(){
 92			$user = new Axon('user');
 93			$user->load('email="'.$_POST['email'].'"');
 94
 95
 96			if($user->dry()){
 97				echo "true";
 98			}else{
 99				if($user->id==$_POST['user_id']){
100					echo "true";
101				}else{
102					echo "false";
103				}
104			}
105		}
106		
107		public function login(){
108			if($_POST){
109				$email = $_POST['email'];
110				$password = $_POST['password'];
111				
112				F3::set('AUTH', array('table'=>'user','id'=>'email','pw'=>'password'));
113				$auth = Auth::sql($email, sha1($password));
114				
115				$sites = new Axon('site');
116				$sites->load('active="1" AND subdomain="'.F3::get('subdomain').'"');
117				
118				if ($auth && $auth->active == 1 && ($auth->site_id == $sites->id || $auth->site_id == 0)) {		
119					F3::set('SESSION.token', $auth->user_token);
120		  			F3::set('SESSION.user', $auth->id);
121					F3::set('SESSION.site', $sites->id);
122					return;
123				} else {
124					//User is not authenticated - send error
125					header('HTTP/1.1 420 Invalid Login Credentials');
126					return;
127				}
128			}else{
129				F3::set('header','html/public/header.html');
130				F3::set('content','html/user/login.html');
131				F3::set('footer','html/public/footer.html');
132				F3::set('js','js/user/login.js');
133				F3::set('html_title','User Sign In');
134				echo Template::serve('html/layout.html');
135			}
136		}
137		
138		public function logout(){
139			if (F3::get('SESSION.user')){
140				F3::set('SESSION.user', null);
141			}
142			if (F3::get('SESSION.token')){
143				F3::set('SESSION.token', null);
144			}
145			F3::reroute('/');
146		}
147		
148		public function forgot(){
149			if($_POST){
150				$email = $_POST['email'];
151				$user = new Axon('user');
152				$user->load('email="'.$email.'"');
153				
154				if ($user->email) {
155					
156					$arr = str_split('abcdefghkABCDEFGHK23456789'); // get all the characters into an array
157    				shuffle($arr); // randomize the array
158    				$arr = array_slice($arr, 0, 6); // get the first six (random) characters out
159    				$tempPw = implode('', $arr); // smush them back into a string
160    				
161    				$user->password = sha1($tempPw);
162					
163					$mail=new SMTP('mail.directedgemedia.com',465,'SSL','jake@directedgemedia.com','myers478');
164					$mail->set('from','<support@qcrt.com>');
165					$mail->set('reply-to', 'support@qcrt.com');
166					$mail->set('x-mailer', 'PHP/' . phpversion());
167					$mail->set('to', $admin->email);
168					$mail->set('subject','Your Temporary Password');
169					$mail->send("Here is your temporary password: ".$tempPw."\n\nPlease log in and update your password.");
170					
171					$user->save();
172					return;
173					
174				} else {
175					//User is not authenticated - send error
176					header('HTTP/1.1 420 Email Not Found');
177					return;
178				}
179				
180			}else{
181				F3::set('header','html/public/header.html');
182				F3::set('content','html/user/forgot.html');
183				F3::set('footer','html/public/footer.html');
184				F3::set('js','js/user/forgot.js');
185				F3::set('html_title','Forgot your password?');
186				echo Template::serve('html/layout.html');
187			}
188		}
189		
190		function afterRoute(){}
191	}
192?>