Completed
Push — master ( 23153f...eb881d )
by Angus
03:14
created

User_Model::set_user_expire_time()   C

Complexity

Conditions 7
Paths 12

Size

Total Lines 27
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
cc 7
eloc 22
nc 12
nop 1
dl 0
loc 27
rs 6.7272
c 0
b 0
f 0
ccs 0
cts 20
cp 0
crap 56
1
<?php declare(strict_types=1); defined('BASEPATH') OR exit('No direct script access allowed');
2
3
class User_Model extends CI_Model {
4
	public $id;
5
	public $username;
6
	public $email;
7
8 119
	public function __construct() {
9 119
		parent::__construct();
10
11
		//This needs to be set here as needs to be set before `logged_in` is called.
12 119
		if($remember = $this->input->cookie('remember_time')) {
13
			$this->set_user_expire_time($remember);
14
		}
15
16
		//CHECK: Should this be placed elsewhere?
17 119
		if($this->logged_in()) {
18 3
			if(!$this->session->userdata('username')) {
19 3
				$this->session->set_userdata('username', $this->ion_auth->user()->row()->username);
20
			}
21 3
			if(!$this->session->userdata('email')) {
22
				//CHECK: This seems like a bad idea?
23 3
				$this->session->set_userdata('email', $this->ion_auth->user()->row()->email);
24
			}
25
		}
26
27 119
		$this->id       = (int) $this->ion_auth->get_user_id();
0 ignored issues
show
Bug introduced by
The method get_user_id() does not seem to exist on object<Ion_auth_model>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
28 119
		$this->username = $this->session->userdata('username');
29 119
		$this->email    = $this->session->userdata('email');
30 119
	}
31
32 119
	public function logged_in() : bool {
33 119
		return $this->ion_auth->logged_in();
0 ignored issues
show
Bug introduced by
The method logged_in() does not seem to exist on object<Ion_auth_model>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
34
	}
35
36 3
	public function login_redirect() {
37 3
		if(!strpos(current_url(), '/import_list') && !strpos(current_url(), '/export_list')) {
38 2
			$this->session->set_flashdata('referred_from', current_url());
39
		}
40
		//FIXME: We should handle the redirect here too, but it causes issues with tests
41
		//redirect('user/login');
42 3
	}
43
44 2
	public function username_exists(string $username) : bool {
45 2
		$this->load->database();
46
47 2
		$query = $this->db->select('*')
48 2
		                  ->from('auth_users')
49 2
		                  ->where('username', $username)
50 2
		                  ->get();
51
52 2
		return (bool) $query->num_rows();
53
	}
54
55
	/**
56
	 * @param $identity
57
	 *
58
	 * @return mixed
59
	 */
60 4 View Code Duplication
	public function find_email_from_identity(string $identity) {
61
		//login allows using email or username, but ion_auth doesn't support this
62
		//check if identity is email, and if not, try and find it
63
		//returns: email or FALSE
64
		//CHECK: How should we handle invalid emails being passed to this?
65 4
		$email = $identity;
66
67 4
		if(!strpos($identity, '@')) {
68
			//identity does not contain @, assume username
69 2
			$this->load->database();
70
71 2
			$query = $this->db->select('email')
72 2
			                  ->from('auth_users')
73 2
			                  ->where('username', $identity)
74 2
			                  ->get();
75
76 2
			if($query->num_rows() > 0) {
77
				//username exists, grab email
78 1
				$email = $query->row('email');
79
			}else{
80
				//username doesn't exist, return FALSE
81 1
				$email = FALSE;
82
			}
83
		}
84
85 4
		return $email;
86
	}
87
88 2 View Code Duplication
	public function get_user_by_username(string $username) {
89 2
		$user = NULL;
90
91 2
		$query = $this->db->select('*')
92 2
		                  ->from('auth_users')
93 2
		                  ->where('username', $username)
94 2
		                  ->get();
95
96 2
		if($query->num_rows() > 0) {
97 1
			$user = $query->row();
98
		}
99 2
		return $user;
100
	}
101
102 1
	public function get_gravatar_url($email = NULL, $size = NULL) : string {
103 1
		$email = $email ?? $this->email;
104
		//TODO: FIXME ON PROFILE PAGES
105 1
		return $this->gravatar->get($email, $size);
106
	}
107
108
	public function get_new_api_key() : string {
109
		$api_key = NULL;
110
		if($this->logged_in()) {
111
			$api_key = substr("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", mt_rand(0, 51), 1) . substr(md5((string) time()), 1);
112
113
			$this->db->where('id', $this->id);
114
			$this->db->update('auth_users', ['api_key' => $api_key]);
115
		}
116
117
		return $api_key;
118
	}
119
	
120 View Code Duplication
	public function get_id_from_api_key(string $api_key) {
121
		$query = $this->db->select('id')
122
		                  ->from('auth_users')
123
		                  ->where('api_key', $api_key)
124
		                  ->get();
125
126
		if($query->num_rows() > 0) {
127
			$userID = $query->row('id');
128
		}
129
130
		return $userID ?? FALSE;
131
	}
132
133
	public function set_user_expire_time($remember) : int {
134
		$expire_time = 0;
135
		switch($remember) {
136
			case '1day':
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
137
				$expire_time = 86400;
138
				break;
139
			case '3day':
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
140
				//This is default so do nothing.
141
				break;
142
			case '1week':
143
				$expire_time = 604800;
144
				break;
145
			case '1month':
146
				$expire_time = 2419200;
147
				break;
148
			case '3month':
149
				$expire_time = 7257600;
150
				break;
151
			default:
152
				//Somehow remember_time isn't set?
153
				break;
154
		}
155
		if($expire_time > 0) {
156
			$this->config->set_item_by_index('user_expire', $expire_time, 'ion_auth');
157
		}
158
		return $expire_time;
159
	}
160
161
	/** NOTICES **/
162
163
	public function getLatestNotice() {
164
		$query = $this->db
165
			->select('tn.notice, tn.created_at')
166
			->from('tracker_notices AS tn')
167
			->where("id > IFNULL((SELECT hidden_notice_id FROM tracker_user_notices WHERE user_id = {$this->User->id}), '0')", NULL, FALSE)
168
			->order_by('tn.id DESC')
169
			->limit(1)
170
			->get();
171
172
		$noticeData = [];
173
		if($query->num_rows() > 0) {
174
			$row = $query->row();
175
176
			$noticeData = [
177
				'date' => str_replace('-', '/', substr($row->created_at, 0, 10)),
178
				'text' => str_replace('|', '<br/>', htmlspecialchars($row->notice))
179
			];
180
		}
181
182
		return $noticeData;
183
	}
184
	public function hideLatestNotice() {
185
		$idQuery = $this->db->select('1')
186
		                    ->where('user_id', $this->User->id)
187
		                    ->get('tracker_user_notices');
188
		if($idQuery->num_rows() > 0) {
189
			$success = (bool) $this->db->set('hidden_notice_id', '(SELECT id FROM tracker_notices ORDER BY id DESC LIMIT 1)', FALSE)
190
			                           ->where('user_id', $this->User->id)
191
			                           ->update('tracker_user_notices');
192
		} else {
193
			$success = (bool) $this->db->insert('tracker_user_notices', [
194
				'user_id'           => $this->User->id,
195
				'hidden_notice_id'  => '(SELECT id FROM tracker_notices ORDER BY id DESC LIMIT 1)'
196
			], FALSE);
197
		}
198
199
		return $success;
200
	}
201
}
202