|
1
|
|
|
<?php defined('BASEPATH') OR exit('No direct script access allowed'); |
|
2
|
|
|
|
|
3
|
|
|
class MY_Controller extends CI_Controller { |
|
4
|
|
|
protected $header_data = array(); |
|
5
|
|
|
protected $body_data = array(); |
|
6
|
|
|
protected $footer_data = array(); |
|
7
|
|
|
public $global_data = array(); |
|
8
|
|
|
|
|
9
|
42 |
|
public function __construct(){ |
|
10
|
42 |
|
parent::__construct(); |
|
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
//FIXME: This is pretty much a phpUnit hack. Without it phpUnit fails here. We need a proper way to fake user/admin testing. |
|
13
|
42 |
|
$this->global_data['user'] = ($this->ion_auth->user() ? $this->ion_auth->user()->row() : ['username' => '']); |
|
14
|
42 |
|
$this->global_data['username'] = $this->User->username; |
|
15
|
|
|
|
|
16
|
|
|
//TODO: Move this to a lib or something. |
|
17
|
42 |
|
$this->global_data['analytics_tracking_id'] = $this->config->item('tracking_id'); |
|
18
|
|
|
|
|
19
|
42 |
|
$this->global_data['theme'] = $this->User_Options->get('theme'); |
|
20
|
42 |
|
$css_path = "css/main.{$this->User_Options->get('theme')}"; |
|
21
|
42 |
|
$this->global_data['complied_css_path'] = asset_url()."{$css_path}.".filemtime(APPPATH . "../public/assets/{$css_path}.css").".css"; |
|
22
|
|
|
|
|
23
|
42 |
|
$js_path = 'js/compiled.min'; |
|
24
|
42 |
|
$this->global_data['complied_js_path'] = asset_url()."{$js_path}.".filemtime(APPPATH . "../public/assets/{$js_path}.js").".js"; |
|
25
|
42 |
|
} |
|
26
|
|
|
|
|
27
|
21 |
|
public function _render_page(/*(array) $paths*/) : void { |
|
28
|
|
|
//We could just use global, but this is the only var we need in both header+footer |
|
29
|
21 |
|
$this->footer_data['page'] = $this->header_data['page']; |
|
30
|
|
|
|
|
31
|
21 |
|
$this->header_data['show_header'] = (array_key_exists('show_header', $this->header_data) ? $this->header_data['show_header'] : TRUE); |
|
32
|
21 |
|
$this->footer_data['show_footer'] = (array_key_exists('show_footer', $this->footer_data) ? $this->footer_data['show_footer'] : TRUE); |
|
33
|
|
|
|
|
34
|
21 |
|
$this->load->view('common/header', ($this->global_data + $this->header_data)); |
|
35
|
21 |
|
foreach(func_get_args() as $path) { |
|
36
|
21 |
|
view_exists($path) or show_404(); //TODO (FIXME): This seems bad performance wise in the long run. Is there any reason to have it in production? |
|
37
|
|
|
|
|
38
|
21 |
|
$this->load->view($path, ($this->global_data + $this->body_data)); |
|
39
|
|
|
} |
|
40
|
|
|
//using the union operator + makes sure global_data always takes priority |
|
41
|
|
|
//SEE: http://stackoverflow.com/a/2140094/1168377 |
|
42
|
21 |
|
$this->load->view('common/footer', ($this->global_data + $this->footer_data)); |
|
43
|
21 |
|
} |
|
44
|
|
|
public function _render_json($json_input, bool $download = FALSE, string $filenamePrefix = 'tracker') : void { |
|
45
|
|
|
$json = is_array($json_input) ? json_encode($json_input) : $json_input; |
|
46
|
|
|
|
|
47
|
|
|
$this->output->set_content_type('application/json', 'utf-8'); |
|
48
|
|
|
$this->_render_content($json,'json', $download, $filenamePrefix); |
|
49
|
|
|
} |
|
50
|
|
|
public function _render_content(string $content, string $filenameExt, bool $download = FALSE, string $filenamePrefix = 'tracker') : void { |
|
51
|
|
|
if($download) { |
|
52
|
|
|
$date = date('Ymd_Hi', time()); |
|
53
|
|
|
$this->output->set_header('Content-Disposition: attachment; filename="'.$filenamePrefix.'-'.$date.'.'.$filenameExt.'"'); |
|
54
|
|
|
$this->output->set_header('Content-Length: '.strlen($content)); |
|
55
|
|
|
} |
|
56
|
|
|
$this->output->set_output($content); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
class CLI_Controller extends CI_Controller { |
|
61
|
1 |
|
public function __construct() { |
|
62
|
1 |
|
parent::__construct(); |
|
|
|
|
|
|
63
|
|
|
|
|
64
|
|
|
//NOTE: This should fail, assuming routes.php does handles things properly. |
|
65
|
|
|
// It's good to have "just in case" fallbacks though. |
|
66
|
1 |
|
is_cli() or exit("ERROR: This controller can only be called via command line: php index.php ..."); |
|
67
|
1 |
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/**** AUTH CONTROLLERS ****/ |
|
71
|
|
|
class User_Controller extends MY_Controller { |
|
72
|
41 |
|
public function __construct() { |
|
73
|
41 |
|
parent::__construct(); |
|
74
|
|
|
|
|
75
|
41 |
|
$this->load->database(); |
|
76
|
41 |
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
class Auth_Controller extends User_Controller { |
|
80
|
8 |
|
public function __construct(bool $redirect = TRUE) { |
|
81
|
8 |
|
parent::__construct(); |
|
82
|
|
|
|
|
83
|
8 |
|
if(!$this->ion_auth->logged_in()) { |
|
|
|
|
|
|
84
|
8 |
|
if($redirect || $_SERVER['REQUEST_METHOD'] === 'GET') { |
|
85
|
2 |
|
$this->User->login_redirect(); |
|
86
|
2 |
|
redirect('user/login'); |
|
87
|
|
|
} else { |
|
88
|
6 |
|
$this->output->set_status_header(401, 'Session has expired, please re-log to continue.'); |
|
89
|
6 |
|
exit_ci(); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
class No_Auth_Controller extends User_Controller { |
|
96
|
30 |
|
public function __construct() { |
|
97
|
30 |
|
parent::__construct(); |
|
98
|
|
|
|
|
99
|
30 |
|
if($this->ion_auth->logged_in()) redirect('/'); |
|
|
|
|
|
|
100
|
27 |
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
class Admin_Controller extends Auth_Controller { |
|
104
|
|
|
public function __construct() { |
|
105
|
|
|
parent::__construct(); |
|
106
|
|
|
|
|
107
|
|
|
if(!$this->ion_auth->is_admin()) { |
|
|
|
|
|
|
108
|
|
|
//user is not an admin, redirect them to front page |
|
109
|
|
|
//TODO (CHECK): Should we note that "you must be an admin to view this page"? |
|
110
|
|
|
|
|
111
|
|
|
redirect('/'); |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/**** AJAX CONTROLLERS ****/ |
|
117
|
|
|
class AJAX_Controller extends CI_Controller { |
|
118
|
4 |
|
public function __construct() { |
|
119
|
4 |
|
parent::__construct(); |
|
|
|
|
|
|
120
|
|
|
|
|
121
|
|
|
//TODO: general security stuff |
|
122
|
4 |
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: