PageRenderTime 27ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/trunk/h-source/Library/Functions.php

https://gitlab.com/7slayer/h-node
PHP | 479 lines | 346 code | 71 blank | 62 comment | 36 complexity | dd0b54514eedfddac843488e8277b7ce MD5 | raw file
  1. <?php
  2. // EasyGiant is a PHP framework for creating and managing dynamic content
  3. //
  4. // Copyright (C) 2009 - 2014 Antonio Gallo (info@laboratoriolibero.com)
  5. // See COPYRIGHT.txt and LICENSE.txt.
  6. //
  7. // This file is part of EasyGiant
  8. //
  9. // EasyGiant is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU General Public License as published by
  11. // the Free Software Foundation, either version 3 of the License, or
  12. // (at your option) any later version.
  13. //
  14. // EasyGiant is distributed in the hope that it will be useful,
  15. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. // GNU General Public License for more details.
  18. //
  19. // You should have received a copy of the GNU General Public License
  20. // along with EasyGiant. If not, see <http://www.gnu.org/licenses/>.
  21. if (!defined('EG')) die('Direct access not allowed!');
  22. /*
  23. SANITIZE FUNCTIONS
  24. */
  25. function none($string) {
  26. return $string;
  27. }
  28. function forceInt($string) {
  29. return (int)$string;
  30. }
  31. function forceNat($string)
  32. {
  33. $string = (int)$string;
  34. if ($string <= 0) return 1;
  35. return $string;
  36. }
  37. function sanitizeDb($stringa) {
  38. if (DATABASE_TYPE === 'Mysql')
  39. {
  40. $stringa = mysql_real_escape_string($stringa);
  41. return $stringa;
  42. }
  43. if (DATABASE_TYPE === 'Mysqli')
  44. {
  45. $mysqli = Db_Mysqli::getInstance();
  46. $db = $mysqli->getDb();
  47. $stringa = $db->real_escape_string($stringa);
  48. return $stringa;
  49. }
  50. return $stringa;
  51. }
  52. function sanitizeAll($stringa) {
  53. $stringa=sanitizeHtml($stringa);
  54. $stringa=sanitizeDb($stringa);
  55. return $stringa;
  56. }
  57. function sanitizeHtml($stringa) {
  58. $charset = Params::$htmlentititiesCharset;
  59. $stringa=htmlentities($stringa,ENT_QUOTES,$charset);
  60. return $stringa;
  61. }
  62. //check if only alphabetic + optional characters are present in the string $string. Set $string to $altString if other characters are found
  63. //$optChar: allowed characters divided by '|' Ex: '+|-|;'
  64. function sanitizeCustom($string,$optChar,$altString = 'EasyGiant')
  65. {
  66. $optChar = html_entity_decode($optChar,ENT_QUOTES);
  67. $optCharArray = explode('|',$optChar);
  68. $temp = $string;
  69. foreach($optCharArray as $char)
  70. {
  71. $temp = str_replace($char,null,$temp);
  72. }
  73. if (ctype_alnum($temp))
  74. {
  75. return $string;
  76. }
  77. else
  78. {
  79. return $altString;
  80. }
  81. }
  82. /*
  83. SANITIZE DEEP
  84. */
  85. function stripslashesDeep($value) {
  86. if(get_magic_quotes_gpc()) {#if stripslashes
  87. return array_map_recursive('stripslashes', $value);
  88. }
  89. return $value;
  90. }
  91. //from http://www.php.net/array_map#112857
  92. function array_map_recursive($callback, $array) {
  93. foreach ($array as $key => $value) {
  94. if (is_array($array[$key])) {
  95. $array[$key] = array_map_recursive($callback, $array[$key]);
  96. }
  97. else {
  98. $array[$key] = call_user_func($callback, $array[$key]);
  99. }
  100. }
  101. return $array;
  102. }
  103. function sanitizeHtmlDeep($value) {
  104. return array_map('sanitizeHtml', $value);
  105. }
  106. function sanitizeDbDeep($value) {
  107. return array_map('sanitizeDb', $value);
  108. }
  109. function sanitizeCustomDeep($stringArray,$optChar,$altString = 'EasyGiant')
  110. {
  111. $result = array();
  112. foreach ($stringArray as $key => $value)
  113. {
  114. $result[$key] = sanitizeCustom($value,$optChar,$altString);
  115. }
  116. return $result;
  117. }
  118. function sanitizeAllDeep($value) {
  119. return array_map('sanitizeAll', $value);
  120. }
  121. function forceIntDeep($value) {
  122. return array_map('forceInt', $value);
  123. }
  124. function forceNatDeep($value) {
  125. return array_map('forceNat', $value);
  126. }
  127. function noneDeep($value) {
  128. return array_map('none', $value);
  129. }
  130. function md5Deep($value)
  131. {
  132. return array_map('md5', $value);
  133. }
  134. function sha1Deep($value)
  135. {
  136. return array_map('sha1', $value);
  137. }
  138. function strip_tagsDeep($value) {
  139. return array_map('strip_tags', $value);
  140. }
  141. function sanitizeAlnum($string)
  142. {
  143. return ctype_alnum($string) ? sanitizeAll($string) : '';
  144. }
  145. function sanitizeIp($ip)
  146. {
  147. return preg_match('/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/',$ip) ? sanitizeAll($ip) : '';
  148. }
  149. /*
  150. CHECK FUNCTIONS
  151. */
  152. //check if a string has the mail format (abc.efg@hij.klm.on)
  153. //modification of the rule found at http://www.sastgroup.com/tutorials/8-espressioni-regolari-per-validare-un-po-di-tutto
  154. //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}$/
  155. function checkMail($string)
  156. {
  157. 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))
  158. {
  159. return true;
  160. }
  161. else
  162. {
  163. return false;
  164. }
  165. }
  166. function wrap($string,$tag_class) {#wrap the string with the tag and its class
  167. #$tag_class has to be an associative array (tag1=>class1,$tag2=>class2,.. )!!
  168. $str_front=null;
  169. $str_rear=null;
  170. if (is_array($tag_class)) {
  171. foreach ($tag_class as $tag => $class) {
  172. $tag = str_replace('+','',$tag);
  173. if (!is_array($class))
  174. {
  175. $str_class=isset($class) ? " class=\"".$class."\"" : null;
  176. }
  177. else
  178. {
  179. $str_class = null;
  180. foreach ($class as $attr => $val)
  181. {
  182. $str_class .= " ".$attr."='".$val."' ";
  183. }
  184. }
  185. $str_front.="<".$tag.$str_class.">\n";
  186. $str_rear.="</".$tag.">\n";
  187. }
  188. }
  189. return $str_front.$string.$str_rear;
  190. }
  191. //check that $date is a ISO date (YYYY-MM-DD)
  192. function checkIsoDate($date)
  193. {
  194. if (preg_match('/^[0-9]{4}\-[0-9]{2}\-[0-9]{2}$/',$date))
  195. {
  196. $dateArray = explode('-',$date);
  197. if ((int)$dateArray[1] <= 12 and (int)$dateArray[1] >= 1 )
  198. {
  199. if ((int)$dateArray[2] >= 1 and (int)$dateArray[2] <= 31)
  200. {
  201. return checkdate((int)$dateArray[1],(int)$dateArray[2],(int)$dateArray[0]);
  202. }
  203. }
  204. }
  205. return false;
  206. }
  207. //check if $string is an integer string
  208. function checkInteger($string)
  209. {
  210. if (preg_match('/^\-?[0-9]{1,}$/',$string))
  211. {
  212. return true;
  213. }
  214. return false;
  215. }
  216. //check if $string is decimal with the format indicated in $format
  217. //$format: M,D M is the maximum number of digits, D is the number of digits to the right of the decimal point
  218. function checkDecimal($string, $format)
  219. {
  220. $t = explode(",",$format);
  221. $M = (int)$t[0];
  222. $D = (int)$t[1];
  223. $I = $M - $D;
  224. if (preg_match("/^[0-9]{1,$I}(\.[0-9]{1,$D})?$/",$string))
  225. {
  226. return true;
  227. }
  228. return false;
  229. }
  230. //get label name from field name
  231. function getFieldLabel($fieldName)
  232. {
  233. if (class_exists("Lang_".Params::$language."_Formats_Fields"))
  234. {
  235. return call_user_func(array("Lang_".Params::$language."_Formats_Fields", "getLabel"), $fieldName);
  236. }
  237. return call_user_func(array("Lang_En_Formats_Fields", "getLabel"), $fieldName);
  238. // if (strstr($fieldName,","))
  239. // {
  240. // $temp = explode(",",$fieldName);
  241. // for ($i=0; $i< count($temp); $i++)
  242. // {
  243. // $temp[$i] = getFieldLabel($temp[$i]);
  244. // }
  245. // return implode (" and ",$temp);
  246. // }
  247. // else
  248. // {
  249. // $fieldName = str_replace("_"," ", $fieldName);
  250. // return ucfirst($fieldName);
  251. // }
  252. }
  253. //generate a random password
  254. //$start: start number of mt_rand
  255. //$end: end number of mt_rand
  256. function randString($length,$start = 33, $end = 126)
  257. {
  258. $random = '';
  259. for ($i = 0; $i < $length; $i++)
  260. {
  261. $random .= chr(mt_rand($start, $end));
  262. }
  263. return $random;
  264. }
  265. //generate a random string
  266. //$charNumb:number of characters of the final string
  267. //$allowedChars: allowed characters
  268. function generateString($charNumb = 8,$allowedChars = '0123456789abcdefghijklmnopqrstuvwxyz')
  269. {
  270. $str = null;
  271. for ($i = 0; $i < $charNumb; $i++)
  272. {
  273. $str .= substr($allowedChars, mt_rand(0, strlen($allowedChars)-1), 1);
  274. }
  275. return $str;
  276. }
  277. function getIp()
  278. {
  279. $ip = "";
  280. if (isset($_SERVER))
  281. {
  282. if (!empty($_SERVER["HTTP_X_FORWARDED_FOR"]))
  283. {
  284. $ip = sanitizeIp($_SERVER["HTTP_X_FORWARDED_FOR"]);
  285. } else if (!empty($_SERVER["HTTP_CLIENT_IP"])) {
  286. $ip = sanitizeIp($_SERVER["HTTP_CLIENT_IP"]);
  287. } else if (!empty($_SERVER["REMOTE_ADDR"])) {
  288. $ip = sanitizeIp($_SERVER["REMOTE_ADDR"]);
  289. }
  290. } else {
  291. if ( getenv( 'HTTP_X_FORWARDED_FOR' ) !== false ) {
  292. $ip = sanitizeIp(getenv( 'HTTP_X_FORWARDED_FOR' ));
  293. } else if ( getenv( 'HTTP_CLIENT_IP' ) !== false ) {
  294. $ip = sanitizeIp(getenv( 'HTTP_CLIENT_IP' ));
  295. } else if ( getenv( 'REMOTE_ADDR' ) !== false ) {
  296. $ip = sanitizeIp(getenv( 'REMOTE_ADDR' ));
  297. }
  298. }
  299. return $ip;
  300. }
  301. function getUserAgent() {
  302. if (isset($_SERVER['HTTP_USER_AGENT']))
  303. {
  304. return md5($_SERVER['HTTP_USER_AGENT']);
  305. }
  306. else
  307. {
  308. return md5('firefox');
  309. }
  310. }
  311. //encode a string to drop ugly characters
  312. function encode($url)
  313. {
  314. $url = utf8_decode(html_entity_decode($url,ENT_QUOTES,'UTF-8'));
  315. $temp = null;
  316. for ($i=0;$i<eg_strlen($url); $i++)
  317. {
  318. // echo substr($url,$i,1)."<br />";
  319. if (strcmp(substr($url,$i,1),' ') === 0)
  320. {
  321. $temp .= '_';
  322. }
  323. else if (strcmp(substr($url,$i,1),"'") === 0)
  324. {
  325. $temp .= '';
  326. }
  327. else
  328. {
  329. if (preg_match('/^[a-zA-Z\_0-9]$/',substr($url,$i,1)))
  330. {
  331. $temp .= substr($url,$i,1);
  332. }
  333. else
  334. {
  335. $temp .= '_';
  336. }
  337. }
  338. }
  339. $temp = urlencode($temp);
  340. return $temp;
  341. }
  342. function callFunction($function, $string, $caller = "CallFunction")
  343. {
  344. if (strstr($function,'::')) //static method
  345. {
  346. $temp = explode('::',$function);
  347. if (!method_exists($temp[0],$temp[1]))
  348. {
  349. throw new Exception('Error in <b>'.$caller.'</b>: method <b>'.$temp[1].'</b> of class <b>'.$temp[0].'</b> does not exists.');
  350. }
  351. return call_user_func(array($temp[0], $temp[1]),$string);
  352. }
  353. else if (strstr($function,'.')) //method
  354. {
  355. $temp = explode('.',$function);
  356. $obj = new $temp[0]; //new instance of the object
  357. if (!method_exists($obj,$temp[1]))
  358. {
  359. throw new Exception('Error in <b>'.$caller.'</b>: method <b>'.$temp[1].'</b> of class <b>'.$temp[0].'</b> does not exists.');
  360. }
  361. return call_user_func(array($obj, $temp[1]),$string);
  362. }
  363. else //function
  364. {
  365. if (!function_exists($function)) {
  366. throw new Exception('Error in <b>'.$caller.'</b>: function <b>'.$function.'</b> does not exists.');
  367. }
  368. //apply the function
  369. return call_user_func($function,$string);
  370. }
  371. }
  372. function xml_encode($string)
  373. {
  374. $trans = get_html_translation_table(HTML_ENTITIES, ENT_QUOTES);
  375. foreach ($trans as $k=>$v)
  376. {
  377. $trans[$k]= "&#".ord($k).";";
  378. }
  379. return strtr($string, $trans);
  380. }
  381. //Convert Hex Color to RGB
  382. //http://bavotasan.com/2011/convert-hex-color-to-rgb-using-php/
  383. function hex2rgb($hex) {
  384. $hex = str_replace("#", "", $hex);
  385. if(strlen($hex) == 3) {
  386. $r = hexdec(substr($hex,0,1).substr($hex,0,1));
  387. $g = hexdec(substr($hex,1,1).substr($hex,1,1));
  388. $b = hexdec(substr($hex,2,1).substr($hex,2,1));
  389. } else {
  390. $r = hexdec(substr($hex,0,2));
  391. $g = hexdec(substr($hex,2,2));
  392. $b = hexdec(substr($hex,4,2));
  393. }
  394. $rgb = array($r, $g, $b);
  395. //return implode(",", $rgb); // returns the rgb values separated by commas
  396. return $rgb; // returns an array with the rgb values
  397. }