forked from tetratec/PHP-Library
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidation.php
More file actions
157 lines (139 loc) · 3.98 KB
/
Copy pathvalidation.php
File metadata and controls
157 lines (139 loc) · 3.98 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
/*
This class holds PHP functions for use with validation concepts
*/
class Validation {
/**
* Cleans user input to prevent any scripting to be entered
* @param <string> $input Input value to be cleaned
* @return <string> Cleansed value
* @category Validation
* <code>
* $result = Validation::cleanInput('&^^%<h1><sript>alert('adasda');</script>');
* </code>
*/
function cleanInput($input) {
try {
$search = array(
'@<script[^>]*?>.*?</script>@si', // Strip out javascript
'@<[\/\!]*?[^<>]*?>@si', // Strip out HTML tags
'@<style[^>]*?>.*?</style>@siU', // Strip style tags properly
'@<![\s\S]*?--[ \t\n\r]*>@' // Strip multi-line comments
);
$output = preg_replace($search, '', $input);
return $output;
} catch (Exception $err) {
return $err->getMessage();
}
}
/**
* Checks to see if the IP is in the valid IP format
* @param <string> $ip IP address to be checked
* @return <boolean> Is the IP valid or not
* @category Validation
* <code>
* $result = Validation::isValidIp('12.612.212w.12');
* </code>
*/
function isValidIp($ip) {
try {
if (preg_match('/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/', $ip)) {
return TRUE;
}
return FALSE;
} catch (Exception $err) {
return $err->getMessage();
}
}
/**
* Check to see if the given email is in the correct format
* @param <string> $email Email address to be checked
* @param <boolean> $testMx (optional) Should the email be tested for existance or not
* @return <boolean> Is the email in the correct format or not
* @category Validation
* <code>
* $result = Validation::isValidEmail('info@r@email.co.co');
* </code>
*/
function isValidEmail($email, $testMx = FALSE) {
try {
if (eregi("^([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,4})$", $email))
if ($testMx) {
list($username, $domain) = split("@", $email);
return getmxrr($domain, $mxrecords);
} else {
return TRUE;
} else {
return FALSE;
}
} catch (Exception $err) {
return $err->getMessage();
}
}
/**
* Checks if the given date is in the correct format (yyyy-mm-dd hh:mm:ss)
* @param <string> $timestamp Timestamp to be checked
* @return <boolean> If the timestamp is in the correct format or not
* @category Validation
* <code>
* $result = Validation::isValidTimestamp('1234-13-13 12:712:12');
* </code>
*/
function isValidTimestamp($timestamp) {
try {
if (preg_match("/^(\d{4})-(\d{2})-(\d{2}) ([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$/", $timestamp, $matches)) {
if (checkdate($matches[2], $matches[3], $matches[1])) {
return TRUE;
}
}
return FALSE;
} catch (Exception $err) {
return $err->getMessage();
}
}
/**
* Function cleans data array
* @param <array> $data Data array to be cleaned
* @return <array> Cleaned array
* @category Validation
* <code>
* $result = Validation::cleanData(array('hello', 'bye'));
* </code>
*/
function cleanData($data) {
try {
foreach ($data as $key => $val) {
if (is_array($val)) {
$data[$key] = $this->_clean_data_array($val);
}
}
return $data;
} catch (Exception $err) {
return $err->getMessage();
}
}
/**
* Converts all the html special characters to html entities
* @param <string> $input String to convert
* @return <string> Converted HTML entities string
* @category Strings
* <code>
* $result = Strings::convertChars('jdhskjhdj kadjasjdh asdsa <as><p><h1>jhjh</h1>&');
* </code>
*/
function convertChars($input) {
try {
$text = trim($input); //<-- LINE 31
$text = preg_replace("/(\r\n|\n|\r)/", "\n", $text); // cross-platform newlines
$text = preg_replace("/\n\n\n\n+/", "\n", $text); // take care of duplicates
$text = htmlspecialchars($text, ENT_QUOTES, 'UTF-8');
$text = stripslashes($text);
$text = str_replace("\n", " ", $text);
$text = str_replace("\t", " ", $text);
return $text;
} catch (Exception $err) {
return $err->getMessage();
}
}
}
?>