qcrt /controller/admin.php

Language PHP Lines 320
MD5 Hash 3c643121a0808fbf859e1ea5326f7c03 Estimated Cost $6,461 (why?)
Repository https://bitbucket.org/lxa478/qcrt.git View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
<?php
	class admin{
	
		function beforeRoute(){
			//Check Authentication
			if (!F3::get('SESSION.admin') && F3::get('PARAMS[0]')!='/admin/login' && F3::get('PARAMS[0]')!='/admin/forgot'){
				F3::reroute('/admin/login');
			}
			
			if($_SERVER['HTTP_HOST'] != 'www.qcrt.com'){
				//F3::reroute('http://www.qcrt.com'.$_SERVER['REQUEST_URI']);
			}
		}
		
		public function login(){
			if($_POST){
				$username = $_POST['username'];
				$password = $_POST['password'];
				
				$sites = new Axon('site');
				$sites->load('active="1" AND subdomain="'.F3::get('subdomain').'"');
				
				F3::set('AUTH', array('table'=>'admin','id'=>'username','pw'=>'password'));
				$auth = Auth::sql($username, sha1($password));
				if ($auth && $auth->active=='1' && !$sites->dry()) {
					F3::set('SESSION.site', $sites->id);
					F3::set('SESSION.subdomain', $sites->subdomain);
		  			F3::set('SESSION.admin', $auth->id);
					return;
				} else {
					//User is not authenticated - send error
					header('HTTP/1.1 420 Invalid Login Credentials');
					return;
				}
			}else{
				F3::set('header','html/public/header.html');
				F3::set('content','html/admin/login.html');
				F3::set('footer','html/public/footer.html');
				F3::set('js','js/admin/login.js');
				F3::set('html_title','Administrator Sign In');
				echo Template::serve('html/layout.html');
			}
		}
		
		public function logout(){
			if (F3::get('SESSION.admin')){
				F3::set('SESSION.admin', null);
			}
			F3::reroute(F3::get('URL_BASE'));
		}
		
		public function forgot(){
			if($_POST){
				$email = $_POST['email'];
				$admin = new Axon('admin');
				$admin->load('email="'.$email.'"');
				
				if ($admin->email) {
					
					$arr = str_split('abcdefghkABCDEFGHK23456789'); // get all the characters into an array
    				shuffle($arr); // randomize the array
    				$arr = array_slice($arr, 0, 6); // get the first six (random) characters out
    				$tempPw = implode('', $arr); // smush them back into a string
    				
    				$admin->password = sha1($tempPw);
						
					$mail=new SMTP('mail.directedgemedia.com',465,'SSL','jake@directedgemedia.com','myers478');
					$mail->set('from','<support@qcrt.com>');
					$mail->set('reply-to', 'support@qcrt.com');
					$mail->set('x-mailer', 'PHP/' . phpversion());
					$mail->set('to', $admin->email);
					$mail->set('subject','Your Username and Temporary Password');
					$mail->send("Here is your username and temporary password:\n\nUsername: ".$admin->username."\nPassword: ".$tempPw."\n\nPlease log in and update your password.");
					
					$admin->save();
					return;
					
				} else {
					//User is not authenticated - send error
					header('HTTP/1.1 420 Email Not Found');
					return;
				}
				
			}else{
				F3::set('header','html/public/header.html');
				F3::set('content','html/admin/forgot.html');
				F3::set('footer','html/public/footer.html');
				F3::set('js','js/admin/forgot.js');
				F3::set('html_title','Forgot your username or password?');
				echo Template::serve('html/layout.html');
			}
		}		
		
		public function home(){
			F3::set('menu','home');
			F3::set('header','html/admin/header.html');
			
			$admin = new Axon('admin');
			$admin->load('id="'.F3::get('SESSION.admin').'"');
			
			F3::set('admin', $admin);
			F3::set('header','html/admin/header.html');
			F3::set('content','html/admin/home.html');
			F3::set('footer','html/admin/footer.html');
			F3::set('js','js/admin/home.js');
			
			F3::set('html_title','Admin Page');
			echo Template::serve('html/admin/layout.html');
		}
		
		public function account(){
			$admin = new Axon('admin');
			$admin->load('id="'.F3::get('SESSION.admin').'"');
		
			F3::set('menu','account');
			F3::set('admin', $admin);
			F3::set('header','html/admin/header.html');
			F3::set('content','html/admin/account.html');
			F3::set('footer','html/admin/footer.html');
			F3::set('js','js/admin/account.js');
			F3::set('html_title','Admin Page');
			echo Template::serve('html/admin/layout.html');
		}
		
		public function users(){
			$users = new Axon('user');
			$ausers = $users->afind('site_id="'.F3::get('SESSION.site').'" OR site_id=0');
		
			F3::set('users', $ausers);
			F3::set('menu','users');
			F3::set('header','html/admin/header.html');
			F3::set('content','html/admin/users.html');
			F3::set('footer','html/admin/footer.html');
			F3::set('js','js/admin/users.js');
			F3::set('html_title','Admin Page');
			echo Template::serve('html/admin/layout.html');
		}
		
		public function change_status(){
		
			if(isset($_POST['user_id']) && $_POST['user_id']!=''){
				$user = new Axon('user');
				$user->load('id="'.$_POST['user_id'].'" AND (site_id="'.F3::get('SESSION.site').'" OR site_id=0)');
				$user->active = $_POST['status'];
				$user->save();
			}
			return;
		}
		
		public function edit_user(){
			if($_POST){
				$user = new Axon('user');
				$user->load('id="'.$_POST['user_id'].'" AND (site_id="'.F3::get('SESSION.site').'" OR site_id=0)');
				$user->firstname = $_POST['firstname'];
				$user->lastname  = $_POST['lastname'];
				$user->email     = $_POST['email'];
				$user->active    = isset($_POST['status']) ? 1 : 0;
				$user->site_id   = isset($_POST['is_global']) ? $_POST['is_global'] : F3::get('SESSION.site');

				
				if(isset($_POST['auto_password'])){
					$arr = str_split('abcdefghkABCDEFGHK23456789'); // get all the characters into an array
    				shuffle($arr); // randomize the array
    				$arr = array_slice($arr, 0, 6); // get the first six (random) characters out
    				$tempPw = implode('', $arr); // smush them back into a string
    				$user->password = sha1($tempPw);
    				
    				//Send Temporary Password In Email		
					$mail=new SMTP('mail.directedgemedia.com',465,'SSL','jake@directedgemedia.com','myers478');
					$mail->set('from','<support@qcrt.com>');
					$mail->set('reply-to', 'support@qcrt.com');
					$mail->set('x-mailer', 'PHP/' . phpversion());
					$mail->set('to', $_POST['email']);
					$mail->set('subject','Your Temporary Password');
					$mail->send("Here is your temporary password:\n\Email: ".$_POST['email']."\nPassword: ".$tempPw."\n\nPlease log in and update your password.");
    				
				}else if($_POST['password']!='' && $_POST['password_confirm']!='' && $_POST['password']==$_POST['password_confirm']){
					$user->password = sha1($_POST['password']);
				}
				
				$user->save();
				F3::reroute('/admin/users');
				return;
			}else{
				$user = new Axon('user');
				$user->load('id="'.F3::get('PARAMS["id"]').'" AND (site_id="'.F3::get('SESSION.site').'" OR site_id=0)');
				
				if($user->dry()){
					F3::reroute('/admin/users');
				}else{
					F3::set('user', $user);
					F3::set('menu','users');
					F3::set('header','html/admin/header.html');
					F3::set('content','html/admin/users/edit.html');
					F3::set('footer','html/admin/footer.html');
					F3::set('js','js/admin/users/edit.js');
					F3::set('html_title','Edit User Page');
					echo Template::serve('html/admin/layout.html');
				}
			}
		}
		
		public function new_user(){
			if($_POST){
				$user = new Axon('user');
				$user->firstname = $_POST['firstname'];
				$user->lastname  = $_POST['lastname'];
				$user->email     = $_POST['email'];
				$user->active    = 1;
				$user->admin_id  = F3::get('SESSION.admin');
				$user->site_id   = isset($_POST['is_global']) ? $_POST['is_global'] : F3::get('SESSION.site');
				
				if(isset($_POST['auto_password'])){
					$arr = str_split('abcdefghkABCDEFGHK23456789'); // get all the characters into an array
    				shuffle($arr); // randomize the array
    				$arr = array_slice($arr, 0, 6); // get the first six (random) characters out
    				$tempPw = implode('', $arr); // smush them back into a string
    				$user->password = sha1($tempPw);
    				    				
    				//Send Temporary Password In Email
					$mail=new SMTP('mail.directedgemedia.com',465,'SSL','jake@directedgemedia.com','myers478');
					$mail->set('from','<support@qcrt.com>');
					$mail->set('reply-to', 'support@qcrt.com');
					$mail->set('x-mailer', 'PHP/' . phpversion());
					$mail->set('to', $_POST['email']);
					$mail->set('subject','Your Temporary Password');
					$mail->send("Here is your temporary password:\n\Email: ".$_POST['email']."\nPassword: ".$tempPw."\n\nPlease log in and update your password.");
					
				}else{
					$user->password = sha1($_POST['password']);
				}
				
				if(isset($_POST['welcome_email'])){
    				    				
    				//Send Welcome Email				
					$mail=new SMTP('mail.directedgemedia.com',465,'SSL','jake@directedgemedia.com','myers478');
					$mail->set('from','<support@qcrt.com>');
					$mail->set('reply-to', 'support@qcrt.com');
					$mail->set('x-mailer', 'PHP/' . phpversion());
					$mail->set('to', $_POST['email']);
					$mail->set('subject','Welcome to QCRT');
					$mail->send("Welcome email from QCRT.com");
					
				}
				
				$user->save();
				F3::reroute('/admin/users');
				return;
				
			}else{
				F3::set('menu','users');
				F3::set('header','html/admin/header.html');
				F3::set('content','html/admin/users/new.html');
				F3::set('footer','html/admin/footer.html');
				F3::set('js','js/admin/users/new.js');
				F3::set('html_title','New User Page');
				echo Template::serve('html/admin/layout.html');
			}
		}
		
		public function delete_user(){
			if(isset($_POST['user_id']) && $_POST['user_id']!=''){
				$user = new Axon('user');
				$user->load('id="'.$_POST['user_id'].'" AND (site_id="'.F3::get('SESSION.site').'" OR site_id=0)');
				$user->erase();
				
				echo $_POST['user_id'];
			}
		}
		
		public function checkNewUserEmail(){
			$user = new Axon('user');
			$user->load('email="'.$_POST['email'].'" AND (site_id="'.F3::get('SESSION.site').'" OR site_id=0)');


			if($user->dry()){
				echo "true";
			}else{
				echo "false";
			}
		}
		
		public function checkEditUserEmail(){
			$user = new Axon('user');
			$user->load('email="'.$_POST['email'].'" AND (site_id="'.F3::get('SESSION.site').'" OR site_id=0)');


			if($user->dry()){
				echo "true";
			}else{
				if($user->id==$_POST['user_id']){
					echo "true";
				}else{
					echo "false";
				}
			}
		}
		
		public function update(){
			$admin = new Axon('admin');
			$admin->load('id="'.F3::get('SESSION.admin').'"');
			
			$admin->email     = $_POST['email'];
			$admin->username  = $_POST['username'];
			$admin->firstname = $_POST['firstname'];
			$admin->lastname  = $_POST['lastname'];

			
			if($_POST['password']!=''){
				$admin->password = sha1($_POST['password']);
			}
			
			$admin->save();
			return;
		}
		
		function afterRoute(){}
			
	}
?>
Back to Top