Completed
Push — master ( 2bd0a4...b5b0a5 )
by Angus
07:46
created

MY_Form_validation::is_valid_json()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 2
nop 1
dl 0
loc 7
ccs 3
cts 3
cp 1
crap 3
rs 9.4285
c 0
b 0
f 0
1
<?php declare(strict_types=1); defined('BASEPATH') or exit('No direct script access allowed');
2
3
class MY_Form_validation extends CI_Form_validation {
4
	private $user_tables;
5
6 64
	public function __construct() {
7 64
		parent::__construct();
8 64
		log_message('debug', 'MY_Form_validation Class Initialized');
0 ignored issues
show
Unused Code introduced by
The call to the function log_message() seems unnecessary as the function has no side-effects.
Loading history...
9
10 64
		$this->CI->config->load('ion_auth', TRUE);
11 64
		$this->user_tables = $this->CI->config->item('tables', 'ion_auth');
12 64
13 64
		$this->set_error_delimiters(
14
			$this->CI->config->item('error_start_delimiter', 'ion_auth'),
15
			$this->CI->config->item('error_end_delimiter', 'ion_auth')
16
		);
17
	}
18
19
	public function run($group = '') : bool {
20 7
		if(!$this->CI->security->verified) {
21 7
			$this->_error_array['csrf_token'] = $this->_build_error_msg('Session expired, please resubmit.');
22 3
			return FALSE;
23
		}
24 7
25
		return parent::run($group);
26
	}
27
28
	/*** User Validation ***/
29
	/**
30 3
	 * @param string $username
31 3
	 * @return bool
32 1
	 */
33
	public function valid_username(string $username) : bool {
34 2
		if(!($isValid = (bool) preg_match('/^[a-zA-Z0-9_-]{4,15}$/', $username))) {
35 1
			$this->set_message('valid_username', 'Username is invalid format.');
36
		}
37 3
		return $isValid;
38
	}
39
	/**
40
	 * @param string $password
41
	 * @return bool
42
	 */
43 2
	public function valid_password(string $password) : bool {
44 2
		if(!($isValid = $this->min_length($password, $this->CI->config->item('min_password_length', 'ion_auth')))) {
45 1
			$this->set_message('valid_password', 'The password is too short!');
46
		}
47 2
		elseif(!($isValid = $this->max_length($password, $this->CI->config->item('max_password_length', 'ion_auth')))) {
48
			$this->set_message('valid_password', 'The password is too long!');
49
		}
50
		return $isValid;
51
	}
52
	/**
53 2
	 * @param string $username
54 2
	 * @return bool
55 1
	 */
56
	public function is_unique_username(string $username) : bool {
57 2 View Code Duplication
		if(!($isValid = $this->is_unique($username, "{$this->user_tables['users']}.username"))) {
58
			$this->set_message('is_unique_username', 'The username already exists in our database.');
59
		}
60 3
		return $isValid;
61 3
	}
62 3
	/**
63 1
	 * @param string $email
64
	 * @return bool
65 3
	 */
66
	public function is_unique_email(string $email) : bool {
67 View Code Duplication
		if(!($isValid = $this->is_unique($email, "{$this->user_tables['users']}.email"))) {
68 5
			$this->set_message('is_unique_email', 'The email already exists in our database.');
69 5
		}
70
		return $isValid;
71
	}
72 2
73 2
	public function is_valid_json(string $json_string) : bool {
74
		$isValid = FALSE;
75
		if(json_decode($json_string) && json_last_error() === JSON_ERROR_NONE) {
76 2
			$isValid = TRUE;
77 2
		}
78
		return $isValid;
79
	}
80
81
	public function is_valid_tag_string(string $tag_string) : bool {
82
		return (bool) preg_match('/^[a-z0-9\\-_,:]{0,255}$/', $tag_string) && (count(preg_grep('/^mal:(?:[0-9]+|none)$/', explode(',', $tag_string))) <= 1);
83
	}
84 2
85 2
	public function is_valid_category(string $category) : bool {
86 1
		return in_array($category, array_keys($this->CI->Tracker->enabledCategories));
87
	}
88 2
89
	public function not_contains(string $haystack, string $needle) : bool {
90
		return strpos($haystack, $needle) === FALSE;
91
	}
92
93
	public function not_equals(string $originalString, string $matchingString) : bool {
94
		return $originalString !== $matchingString;
95
	}
96 3
97 3
	public function is_valid_option_value(string $value, string $option) : bool {
98 3
		if(!($isValid = in_array($value, $this->CI->User_Options->options[$option]['valid_options']))) {
99 2
			$this->set_message('is_valid_option_value', 'The %s field has an invalid value.');
100
		}
101 3
		return $isValid;
102
	}
103
104
	/** MISC FUNCTIONS **/
105
	/**
106
	 * @param string $ruleName
107
	 * @return bool
108
	 */
109
	public function isRuleValid(string $ruleName) : bool {
110
		$isValid = FALSE;
111
		if(is_string($ruleName) && $this->has_rule($ruleName)){
112
			$isValid = !array_key_exists($ruleName, $this->error_array());
113
		}
114
		return $isValid;
115
	}
116
}
117