Functions.class.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. <?php
  2. /*
  3. @nom: constant
  4. @auteur: Idleman (idleman@idleman.fr)
  5. @description: Classe de stockage des fonctions utiles (toutes disponibles en static)
  6. */
  7. class Functions
  8. {
  9. private $id;
  10. public $debug=0;
  11. /**
  12. * Securise la variable utilisateur entrée en parametre
  13. * @author Valentin
  14. * @param<String> variable a sécuriser
  15. * @param<Integer> niveau de securisation
  16. * @return<String> variable securisée
  17. */
  18. public static function secure($var,$level = 1){
  19. $var = htmlspecialchars($var, ENT_QUOTES, "UTF-8");
  20. if($level<1)$var = mysql_real_escape_string($var);
  21. if($level<2)$var = addslashes($var);
  22. return $var;
  23. }
  24. /**
  25. * Return l'environnement/serveur sur lequel on se situe, permet de changer les
  26. * connexions bdd en fonction de la dev, la préprod ou la prod
  27. */
  28. public static function whereImI(){
  29. $maps = array (
  30. 'LOCAL'=>array('localhost','127.0.0.1','0.0.0.1','::0.0.0.0'),
  31. 'LAN'=>array('192.168.10.','valentin'),
  32. 'PWAN'=>array('test.sys1.fr'),
  33. 'WAN'=>array('www.sys1.fr'),
  34. );
  35. $return = 'UNKNOWN';
  36. foreach($maps as $map=>$values){
  37. foreach($values as $ip){
  38. $pos = strpos(strtolower($_SERVER['HTTP_HOST']),$ip);
  39. if ($pos!==false){
  40. $return = $map;
  41. }
  42. }
  43. }
  44. return $return;
  45. }
  46. public static function isLocal($perimeter='LOCAL'){
  47. $return = false;
  48. $localTab = array('localhost','127.0.0.1','0.0.0.1','::0.0.0.0');
  49. $lanTab = array('192.168.10.','valentin');
  50. switch($perimeter){
  51. case 'LOCAL':
  52. foreach($localTab as $ip){
  53. $pos = strpos(strtolower($_SERVER['HTTP_HOST']),$ip);
  54. if ($pos!==false){
  55. $return = true;
  56. }
  57. }
  58. break;
  59. case 'LAN':
  60. foreach($lanTab as $ip){
  61. $pos = strpos(strtolower($_SERVER['HTTP_HOST']),$ip);
  62. if ($pos!==false){
  63. $return = true;
  64. }
  65. }
  66. break;
  67. case 'ALL':
  68. foreach($localTab as $ip){
  69. $pos = strpos(strtolower($_SERVER['HTTP_HOST']),$ip);
  70. if ($pos!==false){
  71. $return = true;
  72. }
  73. }
  74. foreach($lanTab as $ip){
  75. $pos = strpos(strtolower($_SERVER['HTTP_HOST']),$ip);
  76. if ($pos!==false){
  77. $return = true;
  78. }
  79. }
  80. break;
  81. }
  82. return $return;
  83. }
  84. /**
  85. * Convertis la chaine passée en timestamp quel que soit sont format
  86. * (prend en charge les formats type dd-mm-yyy , dd/mm/yyy, yyyy/mm/ddd...)
  87. */
  88. public static function toTime($string){
  89. $string = str_replace('/','-',$string);
  90. $string = str_replace('\\','-',$string);
  91. $string = str_replace('Janvier','Jan',$string);
  92. $string = str_replace('Fevrier','Feb',$string);
  93. $string = str_replace('Mars','Mar',$string);
  94. $string = str_replace('Avril','Apr',$string);
  95. $string = str_replace('Mai','May',$string);
  96. $string = str_replace('Juin','Jun',$string);
  97. $string = str_replace('Juillet','Jul',$string);
  98. $string = str_replace('Aout','Aug',$string);
  99. $string = str_replace('Septembre','Sept',$string);
  100. $string = str_replace('Octobre','Oct',$string);
  101. $string = str_replace('Novembre','Nov',$string);
  102. $string = str_replace('Decembre','Dec',$string);
  103. return strtotime($string);
  104. }
  105. /**
  106. * Recupere l'ip de l'internaute courant
  107. * @author Valentin
  108. * @return<String> ip de l'utilisateur
  109. */
  110. public static function getIP(){
  111. if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])){
  112. $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];}
  113. elseif(isset($_SERVER['HTTP_CLIENT_IP'])){
  114. $ip = $_SERVER['HTTP_CLIENT_IP'];}
  115. else{ $ip = $_SERVER['REMOTE_ADDR'];}
  116. return $ip;
  117. }
  118. /**
  119. * Retourne une version tronquée au bout de $limit caracteres de la chaine fournie
  120. * @author Valentin
  121. * @param<String> message a tronquer
  122. * @param<Integer> limite de caracteres
  123. * @return<String> chaine tronquée
  124. */
  125. public static function truncate($msg,$limit){
  126. $msg = utf8_encode(html_entity_decode($msg));
  127. if(strlen($msg)>$limit){
  128. $nb=$limit-3 ;
  129. $fin='...' ;
  130. }else{
  131. $nb=strlen($msg);
  132. $fin='';
  133. }
  134. return substr($msg, 0, $nb).$fin;
  135. }
  136. function getExtension($fileName){
  137. $dot = explode('.',$fileName);
  138. return $dot[sizeof($dot)-1];
  139. }
  140. /**
  141. * Definis si la chaine fournie est existante dans la reference fournie ou non
  142. * @param unknown_type $string
  143. * @param unknown_type $reference
  144. * @return false si aucune occurence du string, true dans le cas contraire
  145. */
  146. public static function contain($string,$reference){
  147. $return = true;
  148. $pos = strpos($reference,$string);
  149. if ($pos === false) {
  150. $return = false;
  151. }
  152. return strtolower($return);
  153. }
  154. /**
  155. * Définis si la chaine passée en parametre est une url ou non
  156. */
  157. public static function isUrl($url){
  158. $return =false;
  159. if (preg_match('/^(http|https|ftp)://([A-Z0-9][A-Z0-9_-]*(?:.[A-Z0-9][A-Z0-9_-]*)+):?(d+)?/?/i', $url)) {
  160. $return =true;
  161. }
  162. return $return;
  163. }
  164. /**
  165. * Définis si la chaine passée en parametre est une couleur héxadécimale ou non
  166. */
  167. public static function isColor($color){
  168. $return =false;
  169. if (preg_match('/^#(?:(?:[a-fd]{3}){1,2})$/i', $color)) {
  170. $return =true;
  171. }
  172. return $return;
  173. }
  174. /**
  175. * Définis si la chaine passée en parametre est un mail ou non
  176. */
  177. public static function isMail($mail){
  178. $return =false;
  179. if (filter_var($mail, FILTER_VALIDATE_EMAIL)) {
  180. $return =true;
  181. }
  182. return $return;
  183. }
  184. /**
  185. * Définis si la chaine passée en parametre est une IP ou non
  186. */
  187. public static function isIp($ip){
  188. $return =false;
  189. if (preg_match('^(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:[.](?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}$',$ip)) {
  190. $return =true;
  191. }
  192. return $return;
  193. }
  194. public static function sourceName($string){
  195. $name = strtolower($string);
  196. $name = str_replace(' ','-',$name);
  197. $name = str_replace('&#039;','-',$name);
  198. $name = str_replace('\'','-',$name);
  199. $name = str_replace(',','-',$name);
  200. $name = str_replace(':','-',$name);
  201. $name = str_replace('&agrave;','a',$name);
  202. $name = trim($name);
  203. $name = html_entity_decode($name,null,'UTF-8');
  204. return $name;
  205. }
  206. public static function makeCookie($name, $value, $expire='') {
  207. if($expire == '') {
  208. setcookie($name, $value, mktime(0,0,0, date("d"),
  209. date("m"), (date("Y")+1)),'/');
  210. }else {
  211. setcookie($name, '', mktime(0,0,0, date("d"),
  212. date("m"), (date("Y")-1)),'/');
  213. }
  214. }
  215. public static function destroyCookie($name){
  216. Fonction::makeCookie($name,'',time()-3600);
  217. unset($_COOKIE[$name]);
  218. }
  219. static function wordwrap($str, $width = 75, $break = "\n", $cut = false)
  220. {
  221. $str = html_entity_decode($str);
  222. $str = htmlentities (wordwrap($str,$width,$break,$cut));
  223. $str = str_replace('&lt;br/&gt;','<br/>',$str);
  224. $str = str_replace('&amp;','&',$str);
  225. return $str;
  226. }
  227. public static function createFile($filePath,$content){
  228. $fichier = fopen($filePath,"w+");
  229. $fwriteResult = fwrite($fichier,$content);
  230. fclose($fichier);
  231. }
  232. public static function convertFileSize($bytes)
  233. {
  234. if($bytes<1024){
  235. return round(($bytes / 1024), 2).' o';
  236. }elseif(1024<$bytes && $bytes<1048576){
  237. return round(($bytes / 1024), 2).' ko';
  238. }elseif(1048576<$bytes && $bytes<1073741824){
  239. return round(($bytes / 1024)/1024, 2).' Mo';
  240. }elseif(1073741824<$bytes){
  241. return round(($bytes / 1024)/1024/1024, 2).' Go';
  242. }
  243. }
  244. public static function hexaValue($str){
  245. $code = dechex(crc32($str));
  246. $code = substr($code, 0, 6);
  247. return $code;
  248. }
  249. public static function scanRecursiveDir($dir){
  250. $files = scandir($dir);
  251. $allFiles = array();
  252. foreach($files as $file){
  253. if($file!='.' && $file!='..'){
  254. if(is_dir($dir.$file)){
  255. $allFiles = array_merge($allFiles,Fonction::scanRecursiveDir($dir.$file));
  256. }else{
  257. $allFiles[]=str_replace('//','/',$dir.'/'.$file);
  258. }
  259. }
  260. }
  261. return $allFiles;
  262. }
  263. function stripAccents($string){
  264. return strtr(html_entity_decode($string),'àáâãäçèéêëìíîïñòóôõöùúûüýÿÀÁÂÃÄÇÈÉÊËÌÍÎÏÑÒÓÔÕÖÙÚÛÜÝ',
  265. 'aaaaaceeeeiiiinooooouuuuyyAAAAACEEEEIIIINOOOOOUUUUY');
  266. }
  267. }
  268. ?>