· 6 years ago · Jun 10, 2019, 03:20 PM
1<?php
2defined('BASEPATH') OR exit('No direct script access allowed');
3
4
5class Admin extends CI_Controller {
6
7
8
9
10 /**
11 * Index Page for this controller.
12 *
13 * Maps to the following URL
14 * http://example.com/index.php/welcome
15 * - or -
16 * http://example.com/index.php/welcome/index
17 * - or -
18 * Since this controller is set as the default controller in
19 * config/routes.php, it's displayed at http://example.com/
20 *
21 * So any other public methods not prefixed with an underscore will
22 * map to /index.php/welcome/<method_name>
23 * @see https://codeigniter.com/user_guide/general/urls.html
24 */
25
26 public function __construct(){
27 parent::__construct();
28
29 $this->load->model('mail_model');
30 $this->load->model('Security_model');
31 $this->load->model('admin_model'); //Load the Model here
32 $this->load->helper('url');
33 $this->load->helper(array('form', 'url'));
34 $this->load->library('session');
35
36 }
37
38
39 public function dashboard()
40 {
41
42if(is_null(($this->session->userdata('logged_in')))) {
43 redirect('login', 'refresh');
44}
45
46$data['freelancer'] = $this->admin_model->total_Freelancer();
47$data['customer'] = $this->admin_model->total_Customer();
48//$this->session->set_flashdata('error', $this->admin_model->errorMessage());
49$this->load->view('templates/header');
50$this->load->view('templates/sidebar');
51$this->load->view('admin/dashboard',$data);
52$this->load->view('templates/footer');
53
54 }
55
56
57 public function index()
58 {
59
60if(is_null(($this->session->userdata('logged_in')))) {
61 redirect('login', 'refresh');
62}
63
64$data['samp'] = $this->admin_model->getAll();
65//$this->session->set_flashdata('error', $this->admin_model->errorMessage());
66$this->load->view('templates/header');
67$this->load->view('templates/sidebar');
68$this->load->view('admin/index',$data);
69$this->load->view('templates/footer');
70
71 }
72
73
74 public function addIMG()
75 {
76
77 //$data['samp'] = $this->customer_model->getAll();
78
79// $this->load->view('templates/header');
80// $this->load->view('templates/sidebar');
81$this->load->view('admin/saveImage',array('error' => ' ' ));
82// $this->load->view('templates/footer');
83
84 }
85
86
87 public function add()
88 {
89
90if(is_null(($this->session->userdata('logged_in')))) {
91 redirect('login', 'refresh');
92}
93
94
95 //$data['samp'] = $this->customer_model->getAll();
96$data['types'] = $this->admin_model->getAdminTypes();
97 $this->load->view('templates/header');
98 $this->load->view('templates/sidebar');
99 $this->load->view('admin/add',$data);
100 $this->load->view('templates/footer');
101
102 }
103
104
105 public function add_admin()
106 {
107
108if(is_null(($this->session->userdata('logged_in')))) {
109 redirect('login', 'refresh');
110}
111
112 $config['upload_path'] = "./public/images/";
113 $config['allowed_types'] = 'gif|jpg|png';
114 $config['max_size'] = '1000';
115 $config['max_width'] = '10240';
116 $config['max_height'] = '7680';
117
118 $this->load->library('upload', $config);
119
120
121 if (!$this->upload->do_upload())
122 {
123
124
125 // $this->load->view('admin/saveImage', $error);
126 $this->session->set_flashdata('message', $this->upload->display_errors());
127 redirect('admin/add', 'refresh');
128 }
129
130 else if($this->admin_model->verifyEmailDuplicate($this->input->post('email'))==false)
131 {
132 $this->session->set_flashdata('message', "Email is already taken");
133 redirect('admin/add', 'refresh');
134 }
135
136 else
137 {
138 $data = array('upload_data' => $this->upload->data());
139
140 //echo $this->upload->data('file_name');
141 $data = array(
142'FN' => $this->Security_model->secured_encrypt($this->input->post('fn')),
143'MN' => $this->Security_model->secured_encrypt($this->input->post('mn')),
144'LN' => $this->Security_model->secured_encrypt($this->input->post('ln')),
145'email' => $this->Security_model->secured_encrypt($this->input->post('email')),
146'pwd' => $this->Security_model->hash_pass($this->input->post('pwd')),
147'phone' => $this->input->post('phone'),
148'pic' => "public/images/".$this->upload->data('file_name'),
149'adminTypeID' =>$this->input->post('admintype') ,
150'adminStatus' => $this->input->post('status'),
151'dateRegistered' => date("Y/m/d H:i:s")
152);
153
154$this->db->insert('adminusertb', $data);
155$this->session->set_flashdata('message', 'Successfully added Freelancer');
156redirect('admin/index', 'refresh');
157
158
159
160 }
161
162
163 }
164
165
166
167
168 public function edit()
169 {
170
171if(is_null(($this->session->userdata('logged_in')))) {
172 redirect('login', 'refresh');
173}
174
175$id = $this->uri->segment(3);
176
177$x = $this->db->query("SELECT a.*, al.* FROM adminusertb a INNER JOIN admintypestb al ON a.adminTypeID = al.adminTypeID WHERE a.adminUserID =$id");
178 $data['types'] = $this->admin_model->getAdminTypes();
179 $data['samp'] = $x->row();
180 $this->load->view('templates/header');
181 $this->load->view('templates/sidebar');
182 $this->load->view('admin/edit',$data);
183 $this->load->view('templates/footer');
184 }
185
186
187 public function edit_save()
188 {
189
190if(is_null(($this->session->userdata('logged_in')))) {
191 redirect('login', 'refresh');
192}
193
194 $id= $this->Security_model->secured_decrypt($this->input->post('id'));
195
196 $config['upload_path'] = "./public/images/";
197 $config['allowed_types'] = 'gif|jpg|png';
198 $config['max_size'] = '1000';
199 $config['max_width'] = '10240';
200 $config['max_height'] = '7680';
201
202 $this->load->library('upload', $config);
203
204
205 if (!$this->upload->do_upload())
206 {
207
208 if(strpos($this->upload->display_errors(), 'You did not select a file to upload.')){
209 $data = array(
210'FN' => $this->Security_model->secured_encrypt($this->input->post('fn')),
211'MN' => $this->Security_model->secured_encrypt($this->input->post('mn')),
212'LN' => $this->Security_model->secured_encrypt($this->input->post('ln')),
213'email' => $this->Security_model->secured_encrypt($this->input->post('email')),
214'phone' => $this->input->post('phone'),
215'adminTypeID' =>$this->input->post('admintype') ,
216'adminStatus' => $this->input->post('status'),
217'dateModified' => date("Y/m/d H:i:s")
218);
219 }
220 else {
221
222 $this->session->set_flashdata('message', $this->upload->display_errors());
223 redirect('admin/add', 'refresh');
224
225
226 }
227 }
228
229 else{
230
231
232
233
234 $data = array(
235'FN' => $this->Security_model->secured_encrypt($this->input->post('fn')),
236'MN' => $this->Security_model->secured_encrypt($this->input->post('mn')),
237'LN' => $this->Security_model->secured_encrypt($this->input->post('ln')),
238'email' => $this->Security_model->secured_encrypt($this->input->post('email')),
239'phone' => $this->input->post('phone'),
240'pic' => "public/images/".$this->upload->data('file_name'),
241'adminTypeID' =>$this->input->post('admintype') ,
242'adminStatus' => $this->input->post('status'),
243'dateModified' => date("Y/m/d h:i:s")
244);
245
246}
247 $this->db->where('adminUserID',$id);
248 $this->db->update('adminusertb',$data);
249
250 redirect('admin', 'refresh');
251
252
253 }
254
255
256
257 function do_upload()
258 {
259
260
261 $config['upload_path'] = "./public/images/";
262 $config['allowed_types'] = 'gif|jpg|png';
263 $config['max_size'] = '100';
264 $config['max_width'] = '1024';
265 $config['max_height'] = '768';
266 $config['file_name'] = date("Ymd_his");
267 $this->load->library('upload', $config);
268
269 if ( ! $this->upload->do_upload())
270 {
271 $error = array('error' => $this->upload->display_errors());
272
273 $this->load->view('admin/saveImage', $error);
274 }
275 else
276 {
277 $data = array('upload_data' => $this->upload->data());
278
279
280 echo $this->upload->data('file_name');
281
282 }
283 }
284
285
286
287 public function loginPage()
288 {
289
290
291 // $data['error'] =$error;
292
293
294 $this->load->view('welcome_message');
295 }
296
297
298 public function sendMail(){
299
300 $this->mail_model->sendMail();
301 }
302
303
304 public function archive()
305 {
306 if(is_null(($this->session->userdata('logged_in')))) {
307 redirect('login', 'refresh');
308}
309
310 $id = $this->uri->segment(3);
311
312 $this->db->where('adminUserID', $id);
313 $this->db->set('adminStatus', 'Archived');
314 $this->db->update('adminusertb');
315 redirect('admin/index', 'refresh');
316 }
317
318
319 public function account()
320 {
321
322if(is_null(($this->session->userdata('logged_in')))) {
323 redirect('login', 'refresh');
324}
325
326 $id = $this->session->userdata('id');
327
328 $x = $this->db->query("SELECT a.*, al.* FROM adminusertb a INNER JOIN admintypestb al ON a.adminTypeID = al.adminTypeID WHERE a.adminUserID =$id");
329 $data['types'] = $this->admin_model->getAdminTypes();
330 $data['samp'] = $x->row();
331 $this->load->view('templates/header');
332 $this->load->view('templates/sidebar');
333 $this->load->view('admin/account',$data);
334 $this->load->view('templates/footer');
335 }
336
337
338 public function account_save()
339 {
340
341if(is_null(($this->session->userdata('logged_in')))) {
342 redirect('login', 'refresh');
343}
344
345 $id= $this->Security_model->secured_decrypt($this->input->post('id'));
346
347 $config['upload_path'] = "./public/images/";
348 $config['allowed_types'] = 'gif|jpg|png';
349 $config['max_size'] = '1000';
350 $config['max_width'] = '10240';
351 $config['max_height'] = '7680';
352
353 $this->load->library('upload', $config);
354
355
356 if (!$this->upload->do_upload())
357 {
358
359 if(strpos($this->upload->display_errors(), 'You did not select a file to upload.')){
360 $data = array(
361'FN' => $this->Security_model->secured_encrypt($this->input->post('fn')),
362'MN' => $this->Security_model->secured_encrypt($this->input->post('mn')),
363'LN' => $this->Security_model->secured_encrypt($this->input->post('ln')),
364'email' => $this->Security_model->secured_encrypt($this->input->post('email')),
365'phone' => $this->input->post('phone'),
366'adminTypeID' =>$this->input->post('admintype') ,
367'adminStatus' => $this->input->post('status'),
368'dateModified' => date("Y/m/d h:i:s")
369);
370 }
371 else {
372
373 $this->session->set_flashdata('message', $this->upload->display_errors());
374 redirect('admin/account/profile', 'refresh');
375
376
377 }
378 }
379
380 else{
381
382 $data = array(
383'FN' => $this->Security_model->secured_encrypt($this->input->post('fn')),
384'MN' => $this->Security_model->secured_encrypt($this->input->post('mn')),
385'LN' => $this->Security_model->secured_encrypt($this->input->post('ln')),
386'email' => $this->Security_model->secured_encrypt($this->input->post('email')),
387'phone' => $this->input->post('phone'),
388'pic' => "public/images/".$this->upload->data('file_name'),
389'adminTypeID' =>$this->input->post('admintype') ,
390'adminStatus' => $this->input->post('status'),
391'dateModified' => date("Y/m/d h:i:s")
392);
393
394}
395 $this->db->where('adminUserID',$id);
396 $this->db->update('adminusertb',$data);
397
398 //save new session
399 $adminData = $this->admin_model->getAdmin($id);
400
401 $newdata = array(
402 'id'=> $id,
403 'Fullname' => $this->Security_model->secured_decrypt($adminData->FN)." ".$this->Security_model->secured_decrypt($adminData->MN)." ".$this->Security_model->secured_decrypt($adminData->LN),
404 'email' => $this->Security_model->secured_decrypt($adminData->email),
405 'adminType' => $adminData->adminType,
406 'pic' => $adminData->pic,
407 'logged_in' => TRUE
408 );
409
410 $this->session->set_userdata($newdata);
411
412 redirect('admin/account/profile', 'refresh');
413
414
415 }
416
417
418//FORGOT PASSWORD
419
420 public function createForgotPassword(){
421
422 $this->load->view('admin/forgot_password');
423
424
425 }
426
427 public function createForgotPassword_send(){
428 // var_dump($this->admin_model->tokenIsExpired($this->input->post('email')));
429
430 if($this->admin_model->verifyEmail($this->input->post('email'))==false){
431 $this->session->set_flashdata('message', "Email does not exist");
432 redirect('account/forgotPassword', 'refresh');
433 }
434
435 else if($this->admin_model->tokenIsExpired($this->input->post('email'))==false){
436 $this->session->set_flashdata('message', "The password reset for this email has already been generated a while ago");
437 redirect('account/forgotPassword', 'refresh');
438 }
439
440
441
442 else{
443
444 $length = 78;
445 $token = bin2hex(random_bytes($length));
446
447 $id = $this->admin_model->getAdminID($this->input->post('email'));
448
449 $this->mail_model->resetPass($this->input->post('email'),$token,$this->Security_model->secured_decrypt($this->admin_model->getFN($this->input->post('email'))));
450
451 $data = array(
452 'token' => $token,
453 'adminUserID' => $id,
454 'date_generated' => date("Y/m/d H:i:s")
455 );
456
457 $this->db->insert('forgotPwdAdminTB ', $data);
458 $this->session->set_flashdata('message', 'Please Check email for instructions on password reset');
459 redirect('account/forgotPassword', 'refresh');
460
461
462
463 }
464
465 }
466
467
468
469 public function ForgotPassword(){
470
471 $token = $this->uri->segment(3);
472 if($this->admin_model->linkIsExpired($token)==true){
473 $this->session->set_flashdata('message', "The link was expired");
474 redirect('account/forgotPassword', 'refresh');
475 }
476
477 else{
478 $data["token"] = $token;
479 $data["id"] = $this->admin_model->getIDFromToken($token);
480 $this->load->view('admin/reset_password',$data);
481 }
482
483
484
485 }
486
487 public function ForgotPassword_save(){
488
489
490 $captcha= $this->input->post('g-recaptcha-response');
491 $token= $this->input->post('token');
492 $id = $this->Security_model->secured_decrypt($this->input->post('id'));
493
494 $secretKey = "6LeeXqAUAAAAAK4KtAaKlb50CZD9ZeJN_j2av43j";
495
496 $url = 'https://www.google.com/recaptcha/api/siteverify?secret=' . urlencode($secretKey) . '&response=' . urlencode($captcha);
497
498 $response = file_get_contents($url);
499 $responseKeys = json_decode($response,true);
500
501 if(!$responseKeys["success"]) {
502
503 $this->session->set_flashdata('message', 'Complete captcha to reset password');
504
505 redirect("account/resetPassword/$token", 'refresh');
506 }
507
508
509 else{
510
511 $data = array(
512 'pwd' => $this->Security_model->hash_pass($this->input->post('pwd')),
513 'dateModified' => date("Y/m/d H:i:s")
514 );
515
516 $this->db->where('adminUserID',$id);
517 $this->db->update('adminusertb',$data);
518
519 redirect("login", 'refresh');
520
521 }
522
523
524
525
526
527
528
529
530
531 }
532
533 public function four_four(){
534 echo "404";
535 }
536
537
538
539
540
541
542}