-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathvalidation_functions.php
More file actions
114 lines (99 loc) · 3.7 KB
/
Copy pathvalidation_functions.php
File metadata and controls
114 lines (99 loc) · 3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
// is_blank('abcd')
// * validate data presence
// * uses trim() so empty spaces don't count
// * uses === to avoid false positives
// * better than empty() which considers "0" to be empty
function is_blank($value) {
return !isset($value) || trim($value) === '';
}
// has_presence('abcd')
// * validate data presence
// * reverse of is_blank()
function has_presence($value) {
return !is_blank($value);
}
// has_length_greater_than('abcd', 3)
// * validate string length
// * spaces count towards length
// * use trim() if spaces should not count
function has_length_greater_than($value, $min) {
$length = strlen($value);
return $length > $min;
}
// has_length_less_than('abcd', 5)
// * validate string length
// * spaces count towards length
// * use trim() if spaces should not count
function has_length_less_than($value, $max) {
$length = strlen($value);
return $length < $max;
}
// has_length_exactly('abcd', 4)
// * validate string length
// * spaces count towards length
// * use trim() if spaces should not count
function has_length_exactly($value, $exact) {
$length = strlen($value);
return $length == $exact;
}
// has_length('abcd', ['min' => 3, 'max' => 5])
// * validate string length
// * combines functions_greater_than, _less_than, _exactly
// * spaces count towards length
// * use trim() if spaces should not count
function has_length($value, $options) {
if(isset($options['min']) && !has_length_greater_than($value, $options['min'])) {
return false;
} elseif(isset($options['max']) && !has_length_less_than($value, $options['max'])) {
return false;
} elseif(isset($options['exact']) && !has_length_exactly($value, $options['exact'])) {
return false;
} else {
return true;
}
}
// has_inclusion_of( 5, [1,3,5,7,9] )
// * validate inclusion in a set
function has_inclusion_of($value, $set) {
return in_array($value, $set);
}
// has_exclusion_of( 5, [1,3,5,7,9] )
// * validate exclusion from a set
function has_exclusion_of($value, $set) {
return !in_array($value, $set);
}
// has_string('nobody@nowhere.com', '.com')
// * validate inclusion of character(s)
// * strpos returns string start position or false
// * uses !== to prevent position 0 from being considered false
// * strpos is faster than preg_match()
function has_string($value, $required_string) {
return strpos($value, $required_string) !== false;
}
// has_valid_email_format('nobody@nowhere.com')
// * validate correct format for email addresses
// * format: [chars]@[chars].[2+ letters]
// * preg_match is helpful, uses a regular expression
// returns 1 for a match, 0 for no match
// http://php.net/manual/en/function.preg-match.php
function has_valid_email_format($value) {
$email_regex = '/\A[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}\Z/i';
return preg_match($email_regex, $value) === 1;
}
// has_unique_page_menu_name('History')
// * Validates uniqueness of pages.menu_name
// * For new records, provide only the menu_name.
// * For existing records, provide current ID as second arugment
// has_unique_page_menu_name('History', 4)
function has_unique_page_user_name($username, $current_id="0") {
global $db;
$sql = "SELECT * FROM users ";
$sql .= "WHERE username='" . db_escape($db, $username) . "' ";
$sql .= "AND user_id != '" . db_escape($db, $current_id) . "'";
$user_set = mysqli_query($db, $sql);
$user_count = mysqli_num_rows($user_set);
mysqli_free_result($user_set);
return $user_count === 0;
}
?>