This repository was archived by the owner on Aug 2, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFunctions.class.php
More file actions
326 lines (304 loc) · 7.76 KB
/
Functions.class.php
File metadata and controls
326 lines (304 loc) · 7.76 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
<?php
/**
* Functions class
*
* This class contains misc useful functions.
*
* @version 1.6
* @author Mihai Zaharie <mihai@zaharie.ro>
*/
class Functions
{
/**
* Escape for SQL
*
* Returns a sanitized string that can be used in SQL queries
*
* @return string
*/
public static function escapeForSQL($mysqli, $query)
{
if (get_magic_quotes_gpc())
{
$query = stripslashes($query);
}
$query = $mysqli->real_escape_string($query);
return $query;
}
/**
* Get IP function
*
* Returns the user's IP address
*
* @return string
*/
public static function getIPAddress()
{
if (isset($_SERVER['HTTP_X_FORWARD_FOR']))
{
return $_SERVER['HTTP_X_FORWARD_FOR'];
}
else
{
return $_SERVER['REMOTE_ADDR'];
}
}
/**
* Validate the string against a regex
*
* Returns true if the regex matches
*
* @return bool
*/
public static function matchesRegex($string, $regex)
{
if (preg_match($regex, $string) > 0)
{
return true;
}
return false;
}
/**
* Truncate a string at an exact length respecting word boundaries
*
* Removes any special characters from a string and returns a truncated
* version with an optional ellipsis at the end
*
* Note: a length value of 0 means the description will not be actually
* truncated
*
* @return string
*/
public static function truncate($string, $length, $ellipsis = '')
{
$length = ($length > 0) ? $length : strlen($string);
$string = str_replace("\n", '', $string);
$string = strip_tags($string);
$data = (array) explode('\n\n', wordwrap($string, $length, '\n\n'));
//$output = $this->stripExtendedChars($data[0]); // can break romanian characters
$output = $data[0];
return $output . ((strlen($data[0]) < strlen($string)) ? $ellipsis : '');
}
/**
* Strip all the characters that can't be found on a normal keyboard
*
* Removes any special characters from a string and returns the remaining
* string
*
* @return string
*/
public static function stripExtendedChars($string, $transliterate = false)
{
$output = preg_replace('/([^\s\w`~!@#$%^&*()-_=+{}[\]<>|\\;:\'",.?]+)/i', '', $string);
if ($transliterate)
{
$output = iconv('UTF-8', 'ISO-8859-1//IGNORE', $output);
}
return $output;
}
/**
* Removes escape slashes and converts any HTML special characters to their
* HTML entities
*
* Returns the string without escape slashes and with HTML entities
*
* @return string
*/
public static function htmlSafe($string)
{
if (get_magic_quotes_gpc())
{
$string = stripslashes($string);
}
return htmlspecialchars($string);
}
/**
* Converts any HTML special characters to their actual characters
*
* Returns the string with HTML entities decoded
*
* @return string
*/
public static function realHTML($string)
{
return htmlspecialchars_decode($string);
}
/**
* Make a string url safe
*
* Returns the string without any special charaters
*
* @return string
*/
public static function makeURLString($string)
{
$string = iconv('UTF-8', 'ASCII//TRANSLIT', $string); // Transliterate UTF-8 characters to ASCII
$string = trim($string); // Remove trailing spaces
$string = preg_replace('/[^a-z0-9\s]/i', '', $string); // Remove non alphanum characters
$string = str_replace(' ', '-', $string); // Replace spaces with dashes
$string = strtolower($string); // Convert all letters to lowercase
return $string;
}
/**
* Format bytes value based on size
*
* Returns the size and measurement unit
*
* @return string
*/
public static function formatBytes($size)
{
$units = array(' B', ' KB', ' MB', ' GB', ' TB');
for ($i = 0; $size >= 1024 && $i < count($units) - 1; $i++)
{
$size /= 1024;
}
return round($size, 2) . $units[$i];
}
/**
* Check if a variable is empty
*
* Returns true if the variable is empty
*
* @return bool
*/
public static function isEmpty($var, $allow_false = false, $allow_whitespace = false)
{
if (!isset($var) ||
is_null($var) || (
!is_array($var)
&& $allow_whitespace == false
&& trim($var) == ''
&& !is_bool($var)
) || (
$allow_false === false
&& is_bool($var)
&& $var === false
) || (
is_array($var)
&& empty($var)
)
)
{
return true;
} else {
return false;
}
}
/**
* Hash a string to be used as a password
*
* Returns the hash of the input string
*
* @return string
*/
public static function hashPassword($password)
{
$password = sha1(substr($password, -2, 2) . $password);
return $password;
}
/**
* Returns string with newline formatting converted into HTML paragraphs.
*
* @param string $string String to be formatted
* @param boolean $lineBreaks When true, single-line line-breaks will be converted to HTML break tags
* @param boolean $xml When true, an XML self-closing tag will be applied to break tags (<br />)
* @return string
*/
public static function nl2p($string, $lineBreaks = true, $xml = true)
{
// Remove existing HTML formatting to avoid double-wrapping things
$string = str_replace(array('<p>', '</p>', '<br>', '<br />'), '', $string);
// It is conceivable that people might still want single line-breaks
// without breaking into a new paragraph.
if ($lineBreaks == true)
{
$output = '<p>' . preg_replace('/(\n{2,}|\r{2,}|(?:\r\n){2,})/i', "</p>\n<p>", trim($string)) . '</p>';
$output = preg_replace('/(?<!>)(\n|\r|\r\n)(?!>)/i', '<br' . ($xml == true ? ' /' : '') . '>', $output);
return str_replace('<br /><br />', '<br />', $output);
}
else
{
return '<p>' . preg_replace('/((?:\n|\r|\r\n)+)/i', "</p>\n<p>", trim($string)) . '</p>';
}
}
/**
* Checks if a form appears to have been submitted
*
* @param string $method The method of the form (get or post)
* @param string $submitName The name of the submit input
* @return boolean
*/
public static function gotFormData($method, $submitName = 'submit')
{
if ($method == 'get') { $data =& $_GET; } else { $data =& $_POST; }
return (isset($data) && !empty($data) && isset($data[$submitName]) && !empty($data[$submitName]));
}
/**
* Swaps two elements of an array
*
* @param array $array The array
* @param integer $first The first element
* @param second $first The second element
* @return boolean true
*/
public static function arraySwap(&$array, $first, $second)
{
$temp = $array[$first];
$array[$first] = $array[$second];
$array[$second] = $temp;
return true;
}
/**
* Kill the script and throws an error. This is useful for AJAX scripts.
* Returns the error in JSON format and a 400 Bad Request header.
*
* @param string $text The error text that will be displayed, usually in STATIC standard notation for later mapping to a language-based human-readable message
* @param string $details Optional parameter that will be appended to the error message
*/
public static function throwError($text, $details = '')
{
header($_SERVER['SERVER_PROTOCOL'] . ' 400 Bad Request');
$message['error'] = $text;
if ($details != '')
{
$message['details'] = $details;
}
die(json_encode($message));
}
/**
* Deletes the locations (files and folders)
*
* @param mixed $locations The location(s) to delete
* @return boolean true
*/
public static function cleanup($locations)
{
if (is_array($locations))
{
foreach ($locations as $location)
{
self::rrmdir($location);
}
}
else
{
self::rrmdir($location);
}
return true;
}
/**
* Deletes a files or folder (doesn't have to be empty)
*
* @param string $path The path to delete
* @return boolean
*/
public static function rrmdir($path)
{
$folderContents = (is_dir($path) && glob($path . '/*')) ? glob($path . '/*') : array();
return is_file($path) ?
@unlink($path) :
array_map(array('self', 'rrmdir'), $folderContents) == @rmdir($path)
;
}
}