<commit_before><?php
/**
 * Name: Functions library 2
 * Project: LIB/Part of Library In Backyard
 * 
 ** 
 * Purpose: 
 * využíváno jen K:\Work\alfa.gods.cz\www\tools\check-pages.php
 * 
 * php_sockets required for socket_create
 *
 **
 * History 
 * 120215, function GetHTTPstatusCode ($URL_STRING){ .. from r.godsapps.eu/index.php   
 * 120215, function get_data ($URL_STRING, $User-agent){ .. from r.godsapps.eu/magic-link.php
 * //120427, if the result is not number, maybe the server doesn't understand HEAD, let's try GET
 * 120427,   if($address != "192.168.3.11"){//gethostbyname returns this IP address on www.alfa.gods.cz if domain name does not exist  //@TODO - zautoamtizovat správnou IP adresu  
 * 
 *
 ** TODO  
 * 
 * 
 */
   
function GetHTTPstatusCode ($URL_STRING){
   $url = parse_url($URL_STRING);
   if ($url['scheme'] == 'http') {

    //X if(!isset($url['path']))$url['path']='/';    
    $host = $url['host'];
    $port = (isset($url['port'])?$url['port']:80);
    $path = (isset($url['path'])?$url['path']:'/');
    my_error_log("url: ".print_r($url,TRUE),4,16);//debug
    //X if(!$port)
    //X    $port = 80;

    $request = "HEAD $path HTTP/1.1\r\n"
              ."Host: $host\r\n"
              ."Connection: close\r\n"
              ."\r\n";

    my_error_log("IPv4 is ".$address = gethostbyname($host),5,16);//set & log
  if($address != "192.168.3.11"){//gethostbyname returns this IP address on www.alfa.gods.cz if domain name does not exist
    $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
    $socketResult = @socket_connect($socket, $address, $port);
    if ($socketResult) {

        socket_write($socket, $request, strlen($request));
        $socketRead = socket_read($socket, 1024);
        $response = explode(' ', $socketRead);
        my_error_log("HEAD HTTP response: ".print_r($response,TRUE),4,16);//debug
        
        //120427, if the result is not number, maybe the server doesn't understand HEAD, let's try GET
        if (!is_numeric($response[1])){
          $request = "GET $path HTTP/1.1\r\n"
              ."Host: $host\r\n"
              ."Connection: close\r\n"
              ."\r\n";

          socket_write($socket, $request, strlen($request));
          $response = explode(' ', socket_read($socket, 1024));
          my_error_log("GET HTTP response: ".print_r($response,TRUE),4,16);//debug
          if (!is_numeric($response[1])){
              my_error_log("REQUEST = $request RETURNED RESPONSE = {$response[1]} INSTEAD OF HTTP status",3);
          } 
        } elseif($response[1]>300 && $response[1]<400) {
            //var_dump($socketRead);
              $tempPosition = strpos($socketRead,"Location:");
              $tempLocation = substr($socketRead,$tempPosition+strlen("Location:"));
              $tempResponse = explode(PHP_EOL,$tempLocation);
              //die (var_dump(trim($tempResponse[0])));
              $response[1] .= " follow to ".trim($tempResponse[0]);
        }     
        
    } else {
        $socketLastError = socket_last_error($socket);
        $socketLastErrorString = trim(iconv(mb_detect_encoding(socket_strerror($socketLastError), mb_detect_order(), true), "UTF-8", socket_strerror($socketLastError)));//http://stackoverflow.com/questions/7979567/php-convert-any-string-to-utf-8-without-knowing-the-original-character-set-or
        //my_error_log("socket_connect to $host $path failed with {$socketLastError}",3,13);//debug        
        error_log("socket_connect to $host $path failed with {$socketLastError}: {$socketLastErrorString}");//debug        
    }

    //print "<p>Response: ". $response[1] ."</p>\r\n";

    socket_close($socket);
    //my_error_log("result=".$result = (isset($response[1])?($response[1]):'socket_error'),5,16);//set & log 
    my_error_log("result=".$result = (isset($response[1])?($response[1]):($socketLastErrorString)),5,16);//set & log 
    return $result;
    }//if($address != "192.168.3.11")
    else {
      return 'DNS_error';
    }
   } else {
       my_error_log("Scheme: {$url['scheme']} not supported by GetHTTPstatusCode",4,16);//debug
       return 0;
   }
}

function GetHTTPstatusCodeByUA ($URL_STRING, $userAgent = "GetStatusCode/1"){
   $url = parse_url($URL_STRING);
   if ($url['scheme'] == 'http') {
    
    $host = $url['host'];
    $port = $url['port'];
    $path = $url['path'];
    if(!$port)<commit_msg>GetHTTPStatusCode extended of possibility to have multiple local DNS servers<commit_after><?php
/**
 * Name: Functions library 2
 * Project: LIB/Part of Library In Backyard
 * 
 ** 
 * Purpose: 
 * využíváno jen K:\Work\alfa.gods.cz\www\tools\check-pages.php
 * 
 * php_sockets required for socket_create
 *
 **
 * History 
 * 120215, function GetHTTPstatusCode ($URL_STRING){ .. from r.godsapps.eu/index.php   
 * 120215, function get_data ($URL_STRING, $User-agent){ .. from r.godsapps.eu/magic-link.php
 * //120427, if the result is not number, maybe the server doesn't understand HEAD, let's try GET
 * 120427,   if($address != "192.168.3.11"){//gethostbyname returns this IP address on www.alfa.gods.cz if domain name does not exist  //@TODO - zautoamtizovat správnou IP adresu  
 * 
 *
 ** TODO  
 * 
 * 
 */
   
function GetHTTPstatusCode ($URL_STRING){
    $localDNSserver = array('192.168.3.11');
    $url = parse_url($URL_STRING);
    if ($url['scheme'] == 'http') {

        //X if(!isset($url['path']))$url['path']='/';    
        $host = $url['host'];
        $port = (isset($url['port'])?$url['port']:80);
        $path = (isset($url['path'])?$url['path']:'/');
        my_error_log("url: ".print_r($url,TRUE),4,16);//debug
        //X if(!$port)
        //X    $port = 80;

        $request = "HEAD $path HTTP/1.1\r\n"
              ."Host: $host\r\n"
              ."Connection: close\r\n"
              ."\r\n";

        my_error_log("IPv4 is ".$address = gethostbyname($host),5,16);//set & log
        //if($address != "192.168.3.11"){//gethostbyname returns this IP address on www.alfa.gods.cz if domain name does not exist
        if(!in_array($address, $localDNSserver)){//gethostbyname returns this IP address on www.alfa.gods.cz if domain name does not exist
            $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
            $socketResult = @socket_connect($socket, $address, $port);
            if ($socketResult) {

                socket_write($socket, $request, strlen($request));
                $socketRead = socket_read($socket, 1024);
                $response = explode(' ', $socketRead);
                my_error_log("HEAD HTTP response: ".print_r($response,TRUE),4,16);//debug
        
                //120427, if the result is not number, maybe the server doesn't understand HEAD, let's try GET
                if (!is_numeric($response[1])){
                    $request = "GET $path HTTP/1.1\r\n"
                        ."Host: $host\r\n"
                        ."Connection: close\r\n"
                        ."\r\n";

                    socket_write($socket, $request, strlen($request));
                    $response = explode(' ', socket_read($socket, 1024));
                    my_error_log("GET HTTP response: ".print_r($response,TRUE),4,16);//debug
                    if (!is_numeric($response[1])){
                        my_error_log("REQUEST = $request RETURNED RESPONSE = {$response[1]} INSTEAD OF HTTP status",3);
                    } 
                } elseif($response[1]>300 && $response[1]<400) {
                    //var_dump($socketRead);
                    $tempPosition = strpos($socketRead,"Location:");
                    $tempLocation = substr($socketRead,$tempPosition+strlen("Location:"));
                    $tempResponse = explode(PHP_EOL,$tempLocation);
                    //die (var_dump(trim($tempResponse[0])));
                    $response[1] .= " follow to ".trim($tempResponse[0]);
                }     
            } else {
                $socketLastError = socket_last_error($socket);
                $socketLastErrorString = trim(iconv(mb_detect_encoding(socket_strerror($socketLastError), mb_detect_order(), true), "UTF-8", socket_strerror($socketLastError)));//http://stackoverflow.com/questions/7979567/php-convert-any-string-to-utf-8-without-knowing-the-original-character-set-or
                //my_error_log("socket_connect to $host $path failed with {$socketLastError}",3,13);//debug        
                error_log("socket_connect to $host $path failed with {$socketLastError}: {$socketLastErrorString}");//debug        
            }

            //print "<p>Response: ". $response[1] ."</p>\r\n";

            socket_close($socket);
            my_error_log("result=".$result = (isset($response[1])?($response[1]):($socketLastErrorString)),5,16);//set & log 
            return $result;
        }//if($address != "192.168.3.11")
        else {
            return 'DNS_error';
        }
   } else {
       my_error_log("Scheme: {$url['scheme']} not supported by GetHTTPstatusCode",4,16);//debug
       return 0;
   }
}

function GetHTTPstatusCodeByUA ($URL_STRING, $userAgent = "GetStatusCode/1"){
   $url = parse_url($URL_STRING);
   if ($url['scheme'] == 'http') {
    
    $host = $url['host'];
    $port = $url['port'];
    $path = $url['path'];
    if(!$port)
{'fraction_non_alphanumeric': 0.17159507878264624, 'fraction_numerical': 0.028491258363911073, 'mean_word_length': 2.590468810538551, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 7, 'xml': 0}, 'pii_count': 7, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1053176', 'n_tokens_mistral': 3131, 'n_tokens_neox': 2919, 'n_words': 573}