urlchecker.sh 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #!/usr/local/bin/php
  2. <?php
  3. error_reporting(E_ALL & ~ (E_WARNING | E_NOTICE | E_STRICT | E_DEPRECATED));
  4. ini_set('display_startup_errors',0);
  5. ini_set('display_errors',0);
  6. ini_set('log_errors',0);
  7. ini_set('allow_url_fopen', "on");
  8. $argv0 = $argv[0];
  9. $argv1 = $argv[1]??null;
  10. function isUrl(string $url): bool
  11. {
  12. $url= strtolower($url);
  13. $pattern1 = "/^http(s)?\:\/\/(www\.)?[\w-]+\.\w+$/";
  14. $pattern2 = "/^http(s)?\:\/\/(www\.)?[\w-]+\.[\w-]+\.\w+$/";
  15. $pattern3 = "/^http(s)?\:\/\/(www\.)?[\w-]+\.[\w-]+\.[\w-]+\.\w+$/";
  16. if (preg_match($pattern1, $url) || preg_match($pattern2, $url) || preg_match($pattern3, $url)) {
  17. $retval=true;
  18. } else {
  19. $retval=false;
  20. }
  21. return $retval;
  22. }
  23. function left(?string $string, int $length): string
  24. {
  25. if (!isset($string) || $string === "") {
  26. return "";
  27. }
  28. return mb_substr($string, 0, $length);
  29. }
  30. if (is_null($argv1)) {
  31. echo("0");
  32. exit;
  33. }
  34. $argv1 = strtolower($argv1);
  35. if ((left($argv1, 7) !== "http://") && (left($argv1, 8) !== "https://")) {
  36. echo("0 = $argv1 [invalid scheme]");
  37. exit;
  38. }
  39. if (!isurl($argv1)) {
  40. echo("0 = $argv1 [invalid url]");
  41. exit;
  42. }
  43. $cont = file_get_contents($argv1, false);
  44. if ($cont) {
  45. echo("1 = $argv1 [valid]");
  46. } else {
  47. echo("0 = $argv1 [invalid failed]");
  48. }
  49. exit;
  50. ?>