Auth_Model::parseEmail()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1); defined('BASEPATH') or exit('No direct script access allowed');
2
3
class Auth_Model extends CI_Model {
4 12
	public function __construct() {
5 12
		parent::__construct();
6
7 12
		$this->load->database();
8
9 12
		$this->load->library('email');
10 12
	}
11
12
	/**
13
	 * @param string $email
14
	 * @return bool
15
	 */
16
	public function verificationStart(string $email) : bool {
17
		//user is trying to create an account, send them an email verification email
18
		//at this point we know the email is valid and currently not used
19
		//we need to add row to database, as well as send the user an email
20
21
		$verificationCode = sha1(md5(microtime()));
22
23
		$success = FALSE;
24
		try {
25
			//add verification code to database
26
			if($this->db->select('*')->where('email', $email)->get('auth_signup_verification')->num_rows() > 0) {
27
				//email exists in verification DB, do a simple update.
28
				if(!$this->db->update('auth_signup_verification', array(
29
						'verification_code'      => $verificationCode,
30
						'verification_code_time' => time()
31
				), array('email' => $email))
32
				) {
33
					throw new Exception('Unable to insert email into database.');
34
				}
35
			} else {
36
				if(!$this->db->insert('auth_signup_verification', array(
37
						'email'                  => $email,
38
						'verification_code'      => $verificationCode,
39
						'verification_code_time' => time()
40
				))
41
				) {
42
					throw new Exception('Unable to insert email into database.');
43
				}
44
			}
45
			//send email to user to verify signup
46
			$message = $this->load->view($this->config->item('email_templates', 'ion_auth').$this->config->item('email_activate', 'ion_auth'), array(
47
					'email'             => $email,
48
					'verification_code' => $verificationCode,
49
					'verification_url'  => base_url("user/signup/{$verificationCode}")
50
			), TRUE);
51
52
			//TODO: Make an easy email helper
53
			$this->email->from($this->config->item('admin_email', 'ion_auth'), $this->config->item('site_title', 'ion_auth'));
54
			$this->email->to($email);
55
			$this->email->subject($this->config->item('site_title', 'ion_auth').' - Email Verification');
56
			$this->email->message($message);
57
			if(!$this->email->send()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->email->send() of type null|boolean is loosely compared to false; this is ambiguous if the boolean can be false. You might want to explicitly use !== null instead.

If an expression can have both false, and null as possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.

$a = canBeFalseAndNull();

// Instead of
if ( ! $a) { }

// Better use one of the explicit versions:
if ($a !== null) { }
if ($a !== false) { }
if ($a !== null && $a !== false) { }
Loading history...
58
				throw new Exception('Unable to send email to address provided.');
59
			}
60
61
			$success = TRUE;
62
		} catch (Exception $e) {
63
			//echo 'Caught exception: ',  $e->getMessage(), "\n";
64
65
			//revert verification
66
			$this->db->delete('auth_signup_verification', array('email' => $email));
0 ignored issues
show
Documentation introduced by
array('email' => $email) is of type array<string,string,{"email":"string"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
67
		}
68
69
		return $success;
70
	}
71
72
	/**
73
	 * @param string $verificationCode
74
	 * @return mixed
75
	 */
76
	public function verificationCheck(string $verificationCode) {
77
		//user is trying to validate their email for signup, check if verification code is still valid/exists
78
		$query = $this->db->select('email, verification_code_time')
79
		                  ->from('auth_signup_verification')
80
		                  ->where(array('verification_code' => $verificationCode))
81
		                  ->get();
82
83
		$return = FALSE;
84
		if($query->num_rows() > 0) {
85
			$result = $query->row();
86
87
			if((time() - $result->verification_code_time) > 46400000) {
88
				//expired, past the 24hr mark
89
90
				$this->session->set_flashdata('errors', 'Verification code expired. Please re-submit signup.');
91
				$this->db->delete('auth_signup_verification')
92
				         ->where(array('verification_code' => $verificationCode));
93
			} else {
94
				//not expired, verification is valid, return email
95
				$return =  $result->email;
96
			}
97
		}
98
		return $return;
99
	}
100
101
	/**
102
	 * @param string $email
103
	 * @return bool
104
	 */
105 1
	public function verificationComplete(string $email) : bool {
106
		//user has completed signup, remove verification from DB
107 1
		return $this->db->delete('auth_signup_verification', array('email' => $email));
0 ignored issues
show
Documentation introduced by
array('email' => $email) is of type array<string,string,{"email":"string"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
108
	}
109
110
111
	/**
112
	 * @param $identity
113
	 *
114
	 * @return string|null
115
	 */
116 1
	public function getEmailFromIdentity(string $identity) : ?string {
117
		//login allows using email or username, but ion_auth doesn't support this
118
		//check if identity is email, and if not, try and find it
119
		//returns: email or FALSE
120
		//CHECK: How should we handle invalid emails being passed to this?
121 1
		$email = $identity;
122
123 1
		if(!strpos($identity, '@')) {
124
			//identity does not contain @, assume username
125
			$this->load->database();
126
127
			$query = $this->db->select('email')
128
			                  ->from('auth_users')
129
			                  ->where('username', $identity)
130
			                  ->get();
131
132
			if($query->num_rows() > 0) {
133
				//username exists, grab email
134
				$email = $query->row('email');
135
			}else{
136
				//username doesn't exist, return FALSE
137
				$email = NULL;
138
			}
139
		}
140
141 1
		return $email;
142
	}
143
144
	//NOTE: This assumes we know the email is valid.
145 2
	public function parseEmail(string $email) : string {
146 2
		$email_parts = explode('@', $email);
147 2
		return $email_parts[0].'@'.strtolower($email_parts[1]); //Only the first half of the email can be case sensitive
148
	}
149
}
150