Functions.class.php 8.4 KB

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