aboutsummaryrefslogtreecommitdiff
path: root/h-source/Library/Functions.php
blob: ef868125a8175f0e5f99bd0ea42cc616eb13badc (plain) (blame)
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
<?php

// EasyGiant is a PHP framework for creating and managing dynamic content
//
// Copyright (C) 2009 - 2014  Antonio Gallo (info@laboratoriolibero.com)
// See COPYRIGHT.txt and LICENSE.txt.
//
// This file is part of EasyGiant
//
// EasyGiant is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// EasyGiant is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with EasyGiant.  If not, see <http://www.gnu.org/licenses/>.

if (!defined('EG')) die('Direct access not allowed!');


/*
	SANITIZE FUNCTIONS
*/

function none($string) {
	return $string;
}

function forceInt($string) {
	return (int)$string;
}

function forceNat($string)
{
	$string = (int)$string;
	if ($string <= 0) return 1;
	return $string;
}

function sanitizeDb($stringa) {

	if (DATABASE_TYPE === 'Mysql')
	{
		$stringa = mysql_real_escape_string($stringa);
		return $stringa;
	}

	if (DATABASE_TYPE === 'Mysqli')
	{
		$mysqli = Db_Mysqli::getInstance();
		$db = $mysqli->getDb();
		$stringa = $db->real_escape_string($stringa);
		return $stringa;
	}

	return $stringa;
}

function sanitizeAll($stringa) {

	$stringa=sanitizeHtml($stringa);
	$stringa=sanitizeDb($stringa);
	return $stringa;

}

function sanitizeHtml($stringa) {

	$charset = Params::$htmlentititiesCharset;
	$stringa=htmlentities($stringa,ENT_QUOTES,$charset);
	return $stringa;

}

//check if only alphabetic + optional characters are present in the string $string. Set $string to $altString if other characters are found
//$optChar: allowed characters divided by '|'  Ex: '+|-|;'
function sanitizeCustom($string,$optChar,$altString = 'EasyGiant')
{
	
	$optChar = html_entity_decode($optChar,ENT_QUOTES);
	$optCharArray = explode('|',$optChar);
	$temp = $string;
	foreach($optCharArray as $char)
	{
		$temp = str_replace($char,null,$temp);
	}
	if (ctype_alnum($temp))
	{
		return $string;
	}
	else
	{
		return $altString;
	}
}




/*
SANITIZE DEEP
*/

function stripslashesDeep($value) {
	if(get_magic_quotes_gpc()) {#if stripslashes
		return array_map_recursive('stripslashes', $value);
	}
	return $value;
}

//from http://www.php.net/array_map#112857
function array_map_recursive($callback, $array) {
	foreach ($array as $key => $value) {
		if (is_array($array[$key])) {
			$array[$key] = array_map_recursive($callback, $array[$key]);
		}
		else {
			$array[$key] = call_user_func($callback, $array[$key]);
		}
	}
	return $array;
}
    
function sanitizeHtmlDeep($value) {
	return array_map('sanitizeHtml', $value);
}


function sanitizeDbDeep($value) {
	return array_map('sanitizeDb', $value);
}


function sanitizeCustomDeep($stringArray,$optChar,$altString = 'EasyGiant')
{
	$result = array();
	foreach ($stringArray as $key => $value)
	{
		$result[$key] = sanitizeCustom($value,$optChar,$altString);
	}
	return $result;
}


function sanitizeAllDeep($value) {
	return array_map('sanitizeAll', $value);
}


function forceIntDeep($value) {
	return array_map('forceInt', $value);
}

function forceNatDeep($value) {
	return array_map('forceNat', $value);
}

function noneDeep($value) {
	return array_map('none', $value);
}


function md5Deep($value) 
{
	return array_map('md5', $value);
}

function sha1Deep($value)
{
	return array_map('sha1', $value);
}

function strip_tagsDeep($value) {
	return array_map('strip_tags', $value);
}





function sanitizeAlnum($string)
{
	return ctype_alnum($string) ? sanitizeAll($string) : '';
}


function sanitizeIp($ip)
{
	return preg_match('/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/',$ip) ? sanitizeAll($ip) : '';
}


/*
	CHECK FUNCTIONS
*/

//check if a string has the mail format (abc.efg@hij.klm.on)
//modification of the rule found at http://www.sastgroup.com/tutorials/8-espressioni-regolari-per-validare-un-po-di-tutto
//original rule: /^[^0-9][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[@][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[.][a-zA-Z]{2,4}$/
function checkMail($string)
{
	if (preg_match('/^[a-zA-Z0-9_\-]+([.][a-zA-Z0-9_\-]+){0,2}[@][a-zA-Z0-9_\-]+([.][a-zA-Z0-9_\-]+){0,2}[.][a-zA-Z]{2,4}$/',$string))
	{
		return true;
	}
	else
	{
		return false;
	}
}



function wrap($string,$tag_class) {#wrap the string with the tag and its class
	#$tag_class has to be an associative array (tag1=>class1,$tag2=>class2,.. )!!
	$str_front=null;
	$str_rear=null;
	if (is_array($tag_class)) {
		foreach ($tag_class as $tag => $class) {
				$tag = str_replace('+','',$tag);
				if (!is_array($class))
				{
					$str_class=isset($class) ? " class=\"".$class."\"" : null;
				}
				else
				{
					$str_class = null;
					foreach ($class as $attr => $val)
					{
						$str_class .= " ".$attr."='".$val."' ";
					}
				}
				$str_front.="<".$tag.$str_class.">\n";
				$str_rear.="</".$tag.">\n";
		}
	}
	return $str_front.$string.$str_rear;
}

//check that $date is a ISO date (YYYY-MM-DD)
function checkIsoDate($date)
{
	if (preg_match('/^[0-9]{4}\-[0-9]{2}\-[0-9]{2}$/',$date))
	{
		$dateArray = explode('-',$date);
		if ((int)$dateArray[1] <= 12 and (int)$dateArray[1] >= 1 )
		{
			if ((int)$dateArray[2] >= 1 and (int)$dateArray[2] <= 31)
			{
				return checkdate((int)$dateArray[1],(int)$dateArray[2],(int)$dateArray[0]);
			}
		}
	}
	return false;
}

//check if $string is an integer string
function checkInteger($string)
{
	if (preg_match('/^\-?[0-9]{1,}$/',$string))
	{
		return true;
	}
	return false;
}

//check if $string is decimal with the format indicated in $format
//$format: M,D M is the maximum number of digits, D is the number of digits to the right of the decimal point
function checkDecimal($string, $format)
{
	$t = explode(",",$format);
	$M = (int)$t[0];
	$D = (int)$t[1];
	$I = $M - $D;
	
	if (preg_match("/^[0-9]{1,$I}(\.[0-9]{1,$D})?$/",$string))
	{
		return true;
	}
	return false;
}

//get label name from field name
function getFieldLabel($fieldName)
{
	if (class_exists("Lang_".Params::$language."_Formats_Fields"))
	{
		return call_user_func(array("Lang_".Params::$language."_Formats_Fields", "getLabel"), $fieldName);
	}
	
	return call_user_func(array("Lang_En_Formats_Fields", "getLabel"), $fieldName);
	
// 	if (strstr($fieldName,","))
// 	{
// 		$temp = explode(",",$fieldName);
// 		for ($i=0; $i< count($temp); $i++)
// 		{
// 			$temp[$i] = getFieldLabel($temp[$i]);
// 		}
// 		return implode (" and ",$temp);
// 	}
// 	else
// 	{
// 		$fieldName = str_replace("_"," ", $fieldName);
// 		return ucfirst($fieldName);
// 	}
}

//generate a random password
//$start: start number of mt_rand
//$end: end number of mt_rand
function randString($length,$start = 33, $end = 126)
{
	$random = '';
	for ($i = 0; $i < $length; $i++)
	{
		$random .= chr(mt_rand($start, $end));
	}
	return $random;
}

//generate a random string
//$charNumb:number of characters of the final string
//$allowedChars: allowed characters
function generateString($charNumb = 8,$allowedChars = '0123456789abcdefghijklmnopqrstuvwxyz')
{
	$str = null;
	for ($i = 0; $i < $charNumb; $i++)
	{
		$str .= substr($allowedChars, mt_rand(0, strlen($allowedChars)-1), 1);
	}
	return $str;
}


function getIp()
{
    $ip = "";

    if (isset($_SERVER))
    {
        if (!empty($_SERVER["HTTP_X_FORWARDED_FOR"]))
        {
            $ip = sanitizeIp($_SERVER["HTTP_X_FORWARDED_FOR"]);
        } else if (!empty($_SERVER["HTTP_CLIENT_IP"])) {
            $ip = sanitizeIp($_SERVER["HTTP_CLIENT_IP"]);
        } else if (!empty($_SERVER["REMOTE_ADDR"])) {
            $ip = sanitizeIp($_SERVER["REMOTE_ADDR"]);
        }
    } else {
        if ( getenv( 'HTTP_X_FORWARDED_FOR' ) !== false ) {
            $ip = sanitizeIp(getenv( 'HTTP_X_FORWARDED_FOR' ));
        } else if ( getenv( 'HTTP_CLIENT_IP' ) !== false ) {
            $ip = sanitizeIp(getenv( 'HTTP_CLIENT_IP' ));
        } else if ( getenv( 'REMOTE_ADDR' ) !== false ) {
            $ip = sanitizeIp(getenv( 'REMOTE_ADDR' ));
        }
    }
    return $ip;
}



function getUserAgent() {
	if (isset($_SERVER['HTTP_USER_AGENT']))
	{
		return md5($_SERVER['HTTP_USER_AGENT']);
	} 
	else
	{
		return md5('firefox');
	}
}

//encode a string to drop ugly characters
function encode($url)
{
	$url = utf8_decode(html_entity_decode($url,ENT_QUOTES,'UTF-8'));
	
	$temp = null;
	
	for ($i=0;$i<eg_strlen($url); $i++)
	{
// 		echo substr($url,$i,1)."<br />";
		if (strcmp(substr($url,$i,1),' ') === 0)
		{
			$temp .= '_';
		}
		else if (strcmp(substr($url,$i,1),"'") === 0)
		{
			$temp .= '';
		}
		else
		{
			if (preg_match('/^[a-zA-Z\_0-9]$/',substr($url,$i,1)))
			{
				$temp .= substr($url,$i,1);
			}
			else
			{
				$temp .= '_';
			}
		}
	}

	$temp = urlencode($temp);
	return $temp;
}

function callFunction($function, $string, $caller = "CallFunction")
{
	if (strstr($function,'::')) //static method
	{
		$temp = explode('::',$function);
		
		if (!method_exists($temp[0],$temp[1]))
		{
			throw new Exception('Error in <b>'.$caller.'</b>: method <b>'.$temp[1].'</b> of class <b>'.$temp[0].'</b> does not exists.');
		}
		
		return call_user_func(array($temp[0], $temp[1]),$string);
	}
	else if (strstr($function,'.')) //method
	{
		$temp = explode('.',$function);
		
		$obj = new $temp[0]; //new instance of the object
		
		if (!method_exists($obj,$temp[1]))
		{
			throw new Exception('Error in <b>'.$caller.'</b>: method <b>'.$temp[1].'</b> of class <b>'.$temp[0].'</b> does not exists.');
		}
		
		return call_user_func(array($obj, $temp[1]),$string);
	}
	else //function
	{
		if (!function_exists($function)) {
			throw new Exception('Error in <b>'.$caller.'</b>: function <b>'.$function.'</b> does not exists.');
		}
		//apply the function
		return call_user_func($function,$string);
	}
}

function xml_encode($string)
{
	$trans = get_html_translation_table(HTML_ENTITIES, ENT_QUOTES);
	foreach ($trans as $k=>$v)
	{
		$trans[$k]= "&#".ord($k).";";
	}
	
	return strtr($string, $trans);
}

//Convert Hex Color to RGB
//http://bavotasan.com/2011/convert-hex-color-to-rgb-using-php/
function hex2rgb($hex) {
   $hex = str_replace("#", "", $hex);

   if(strlen($hex) == 3) {
      $r = hexdec(substr($hex,0,1).substr($hex,0,1));
      $g = hexdec(substr($hex,1,1).substr($hex,1,1));
      $b = hexdec(substr($hex,2,1).substr($hex,2,1));
   } else {
      $r = hexdec(substr($hex,0,2));
      $g = hexdec(substr($hex,2,2));
      $b = hexdec(substr($hex,4,2));
   }
   $rgb = array($r, $g, $b);
   //return implode(",", $rgb); // returns the rgb values separated by commas
   return $rgb; // returns an array with the rgb values
}