util.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. function email_check($email) {
  3. $isValid=TRUE;
  4. $atIndex = strrpos($email, "@");
  5. if (is_bool($atIndex) && !$atIndex) {
  6. $isValid = FALSE;
  7. } else {
  8. $domain = substr($email, $atIndex+1);
  9. $local = substr($email, 0, $atIndex);
  10. $localLen = strlen($local);
  11. $domainLen = strlen($domain);
  12. //user troppo lungo
  13. if ($localLen < 1 || $localLen > 64) {
  14. $isValid = FALSE;
  15. //l'user incomincia o termina con un . o a 2 .. consecutivi
  16. } elseif ($local[0] == '.' || $local[$localLen-1] == '.' || consecutive_dot($local)) {
  17. $isValid = FALSE;
  18. } elseif (! isValidDomain($domain)) {
  19. $isValid = FALSE;
  20. } elseif(!preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/', str_replace("\\\\","",$local))) {
  21. // character not valid in local part unless
  22. // local part is quoted
  23. if (!preg_match('/^"(\\\\"|[^"])+"$/', str_replace("\\\\","",$local))) {
  24. $isValid = FALSE;
  25. }
  26. }
  27. }
  28. return $isValid;
  29. }
  30. function isValidDomain($domain) {
  31. $isValid=true;
  32. $domainLen = strlen($domain);
  33. if ($domainLen < 4 || $domainLen > 255) {
  34. $isValid = FALSE;
  35. //l'user incomincia o termina con un . o a 2 .. consecutivi
  36. } elseif (consecutive_dot($domain) || $domain[0] == '.' || $domain[$domainLen-1] == '.' ) {
  37. $isValid = FALSE;
  38. } elseif (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain)) {
  39. $isValid = FALSE;
  40. //ha almeno un punto
  41. } elseif (! strpos($domain, ".")) {
  42. $isValid = FALSE;
  43. } else {
  44. //il dominio di primo livello è almeno 2 caratteri
  45. $arr=explode(".", $domain);
  46. if(strlen($arr[count($arr)-1])<2)
  47. $isValid = FALSE;
  48. }
  49. return $isValid;
  50. }
  51. function consecutive_dot($string) {
  52. $ret=false;
  53. if(preg_match('/\\.\\./',$string))
  54. $ret=true;
  55. return $ret;
  56. }
  57. /**
  58. * estrae dominio di secondo livello
  59. */
  60. function dom($name){
  61. $zon=explode(".",$name);
  62. return implode(".",array_slice($zon,-3));
  63. }
  64. /* gets the data from a URL */
  65. function get_data($url) {
  66. $ch = curl_init();
  67. $timeout = 5;
  68. curl_setopt($ch, CURLOPT_URL, $url);
  69. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  70. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  71. $data = curl_exec($ch);
  72. curl_close($ch);
  73. return $data;
  74. }
  75. ?>