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
|
50 |
|
public function __construct(){ |
10
|
50 |
|
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
|
50 |
|
$this->global_data['user'] = ($this->ion_auth->user() ? $this->ion_auth->user()->row() : ['username' => '']); |
14
|
50 |
|
$this->global_data['username'] = $this->User->username; |
15
|
|
|
|
16
|
|
|
//TODO: Move this to a lib or something. |
17
|
50 |
|
$this->global_data['analytics_tracking_id'] = $this->config->item('tracking_id'); |
18
|
|
|
|
19
|
50 |
|
$this->global_data['theme'] = $this->User_Options->get('theme'); |
20
|
50 |
|
if(ENVIRONMENT !== 'development') { |
21
|
50 |
|
$css_path = "css/main.{$this->User_Options->get('theme')}"; |
22
|
50 |
|
$this->global_data['complied_css_path'] = asset_url()."{$css_path}.".filemtime(APPPATH . "../public/assets/{$css_path}.css").".css"; |
23
|
|
|
|
24
|
50 |
|
$js_path = 'js/compiled.min'; |
25
|
50 |
|
$this->global_data['complied_js_path'] = asset_url()."{$js_path}.".filemtime(APPPATH . "../public/assets/{$js_path}.js").".js"; |
26
|
|
|
} |
27
|
50 |
|
} |
28
|
|
|
|
29
|
21 |
|
public function _render_page(/*(array) $paths*/) : void { |
30
|
|
|
//We could just use global, but this is the only var we need in both header+footer |
31
|
21 |
|
$this->footer_data['page'] = $this->header_data['page']; |
32
|
|
|
|
33
|
21 |
|
$this->header_data['show_header'] = (array_key_exists('show_header', $this->header_data) ? $this->header_data['show_header'] : TRUE); |
34
|
21 |
|
$this->footer_data['show_footer'] = (array_key_exists('show_footer', $this->footer_data) ? $this->footer_data['show_footer'] : TRUE); |
35
|
|
|
|
36
|
21 |
|
$this->load->view('common/header', ($this->global_data + $this->header_data)); |
37
|
21 |
|
foreach(func_get_args() as $path) { |
38
|
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? |
39
|
|
|
|
40
|
21 |
|
$this->load->view($path, ($this->global_data + $this->body_data)); |
41
|
|
|
} |
42
|
|
|
//using the union operator + makes sure global_data always takes priority |
43
|
|
|
//SEE: http://stackoverflow.com/a/2140094/1168377 |
44
|
21 |
|
$this->load->view('common/footer', ($this->global_data + $this->footer_data)); |
45
|
21 |
|
} |
46
|
|
|
public function _render_json($json_input, bool $download = FALSE, string $filenamePrefix = 'tracker') : void { |
47
|
|
|
$json = is_array($json_input) ? json_encode($json_input) : $json_input; |
48
|
|
|
|
49
|
|
|
$this->output->set_content_type('application/json', 'utf-8'); |
50
|
|
|
$this->_render_content($json ?? '{}','json', $download, $filenamePrefix); |
51
|
|
|
} |
52
|
|
|
public function _render_content(string $content, string $filenameExt, bool $download = FALSE, string $filenamePrefix = 'tracker') : void { |
53
|
|
|
if($download) { |
54
|
|
|
$date = date('Ymd_Hi', time()); |
55
|
|
|
$this->output->set_header('Content-Disposition: attachment; filename="'.$filenamePrefix.'-'.$date.'.'.$filenameExt.'"'); |
56
|
|
|
$this->output->set_header('Content-Length: '.strlen($content)); |
57
|
|
|
} |
58
|
|
|
$this->output->set_output($content); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
class CLI_Controller extends CI_Controller { |
63
|
1 |
|
public function __construct() { |
64
|
1 |
|
parent::__construct(); |
|
|
|
|
65
|
|
|
|
66
|
|
|
//NOTE: This should fail, assuming routes.php does handles things properly. |
67
|
|
|
// It's good to have "just in case" fallbacks though. |
68
|
1 |
|
is_cli() or exit("ERROR: This controller can only be called via command line: php index.php ..."); |
69
|
1 |
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/**** AUTH CONTROLLERS ****/ |
73
|
|
|
class User_Controller extends MY_Controller { |
74
|
46 |
|
public function __construct() { |
75
|
46 |
|
parent::__construct(); |
76
|
|
|
|
77
|
46 |
|
$this->load->database(); |
78
|
46 |
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
class Auth_Controller extends User_Controller { |
82
|
13 |
|
public function __construct(bool $redirect = TRUE) { |
83
|
13 |
|
parent::__construct(); |
84
|
|
|
|
85
|
13 |
|
if(!$this->ion_auth->logged_in()) { |
86
|
13 |
|
if($redirect || $_SERVER['REQUEST_METHOD'] === 'GET') { |
87
|
7 |
|
$this->User->login_redirect(); |
88
|
7 |
|
redirect('user/login'); |
89
|
|
|
} else { |
90
|
6 |
|
$this->output->set_status_header(401, 'Session has expired, please re-log to continue.'); |
91
|
6 |
|
exit_ci(); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
class No_Auth_Controller extends User_Controller { |
98
|
30 |
|
public function __construct() { |
99
|
30 |
|
parent::__construct(); |
100
|
|
|
|
101
|
30 |
|
if($this->ion_auth->logged_in()) redirect('/'); |
102
|
27 |
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
class Admin_Controller extends Auth_Controller { |
106
|
|
|
public function __construct() { |
107
|
|
|
parent::__construct(); |
108
|
|
|
|
109
|
|
|
if(!$this->ion_auth->is_admin()) { |
110
|
|
|
//user is not an admin, redirect them to front page |
111
|
|
|
//TODO (CHECK): Should we note that "you must be an admin to view this page"? |
112
|
|
|
|
113
|
|
|
redirect('/'); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/**** AJAX CONTROLLERS ****/ |
119
|
|
|
class AJAX_Controller extends CI_Controller { |
120
|
4 |
|
public function __construct() { |
121
|
4 |
|
parent::__construct(); |
|
|
|
|
122
|
|
|
|
123
|
|
|
//TODO: general security stuff |
124
|
4 |
|
} |
125
|
|
|
} |
126
|
|
|
|
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: