<?php
namespace front\factory;
class AuditSEO 
{
  public static function robots_allowed( $robots_txt, $useragent )
  {
    $agents = array( preg_quote( '*' ) );
    if ( $useragent) $agents[] = preg_quote( $useragent );
      $agents = implode( '|', $agents );
    
    $robotstxt = @file( $robots_txt );
    if ( empty( $robotstxt ) ) 
      return true;

    $rules = array();
    $ruleApplies = false;
    foreach ( $robotstxt as $line ) 
    {
      if ( !$line = trim( $line ) ) 
        continue;

      if ( preg_match('/^\s*User-agent: (.*)/i', $line, $match ) )
        $ruleApplies = preg_match( "/($agents)/i", $match[1] );
      
      if ( $ruleApplies && preg_match( '/^\s*Disallow:(.*)/i', $line, $regs ) ) 
      {
        if ( !$regs[1] ) 
          return true;
        
        $rules[] = preg_quote( trim( $regs[1] ), '/' );
      }
    }

    foreach ( $rules as $rule ) 
    {
      if ( preg_match( "/^$rule/", $parsed['path'] ) ) 
        return false;
    }

    return true;
  }
  
  public static function data05( $url )
  {
    global $mdb;
    
    $data04 = $mdb -> get( 'as_sites', [
                'effective_url',
                'page_speed_insight_mobile'
              ], [
                'url' => $url
              ] );
    
    if ( !$data04['page_speed_insight_mobile'] )
    {
      $ch = curl_init();
      curl_setopt( $ch, CURLOPT_URL, 'https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url=' . $data04['effective_url'] . '&category=performance&strategy=mobile' );
      curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
      curl_setopt( $ch, CURLOPT_VERBOSE, 1 );
      curl_setopt( $ch, CURLOPT_TIMEOUT, 60 );
      curl_setopt( $ch, CURLOPT_HEADER, true );
      curl_setopt( $ch, CURLOPT_CAINFO, 'cacert.pem' );
      curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
      curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
      curl_setopt( $ch, CURLOPT_HEADER, false );
      curl_setopt( $ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.103 Safari/537.36' );
      $response = curl_exec( $ch );
      curl_close ( $ch );
      
      $mdb -> update( 'as_sites', [ 'page_speed_insight_mobile' => $response ], [ 'url' => $url ] );
      
      $data04['page_speed_insight_mobile'] = json_decode( $response, true );
    }
    else
    {
      $data04['page_speed_insight_mobile'] = json_decode( $data04['page_speed_insight_mobile'], true );
    }
    return $data04;
  }
  
  public static function data04( $url )
  {
    global $mdb;
    
    $data04 = $mdb -> get( 'as_sites', [
                'effective_url',
                'page_speed_insight_desktop'
              ], [
                'url' => $url
              ] );
    
    if ( !$data04['page_speed_insight_desktop'] )
    {
      $ch = curl_init();
      curl_setopt( $ch, CURLOPT_URL, 'https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url=' . $data04['effective_url'] . '&category=performance&strategy=desktop' );
      curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
      curl_setopt( $ch, CURLOPT_VERBOSE, 1 );
      curl_setopt( $ch, CURLOPT_TIMEOUT, 60 );
      curl_setopt( $ch, CURLOPT_HEADER, true );
      curl_setopt( $ch, CURLOPT_CAINFO, 'cacert.pem' );
      curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
      curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
      curl_setopt( $ch, CURLOPT_HEADER, false );
      curl_setopt( $ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.103 Safari/537.36' );
      $response = curl_exec( $ch );
      curl_close ( $ch );
      
      $mdb -> update( 'as_sites', [ 'page_speed_insight_desktop' => $response ], [ 'url' => $url ] );
      
      $data04['page_speed_insight_desktop'] = json_decode( $response, true );
    }
    else
    {
      $data04['page_speed_insight_desktop'] = json_decode( $data04['page_speed_insight_desktop'], true );
    }
    return $data04;
  }
  
  public static function data03( $url )
  {
    global $mdb;
    
    $data03 = $mdb -> get( 'as_sites', [
                'meta_title',
                'meta_description',
                'meta_keywords',
                'code_length',
                'text_length',
                'words_count',
                'h1_count',
                'h2_count',
                'h3_count',
                'h4_count',
                'h5_count',
                'h6_count'
              ], [
                'url' => $url
              ] );
    
    $data03['code_to_text_ratio'] = round( ( $data03['text_length'] / ( $data03['text_length'] + $data03['code_length'] ) * 100 ), 0 ) . '%';
        
    return $data03;
  }
  
  public static function data02( $url )
  {
    global $mdb;
    
    $data02 = $mdb -> get( 'as_sites', [
                'meta_robots',
                'robots_txt',
                'redirect_www',
                'https'
              ], [
                'url' => $url
              ] );
    
    $data02['robots_txt'] = self::robots_allowed( $data02['robots_txt'], 'GoogleBot' ) ? 'tak' : 'nie';
    $data02['redirect_www'] = $data02['redirect_www'] ? 'tak' : 'nie';
    $data02['https'] = $data02['https'] ? 'tak' : 'nie';
        
    return $data02;
  }
  
  public static function data01( $url )
  {
    global $mdb;
    
    $data01 = $mdb -> get( 'as_sites', [
                'effective_url',
                'ip',
                'location',
                'favicon',
                'cms'
              ], [
                'url' => $url
              ] );
    
    $location = json_decode( $data01['location'] );    
    $data01['location'] = $location -> country;
    $data01['favicon'] = $data01['favicon'] != null ? 'tak' : 'nie';
    
    return $data01;
  }
  
  public static function cms( $html )
  {
    $dom     = new \DOMDocument();
    @$dom -> loadHTML( $html );
    $metas   = $dom -> getElementsByTagName( 'meta' );
    $cms = '';
    
    for ( $i = 0; $i < $metas -> length; $i++ )
    {
      $meta = $metas -> item( $i );
      if ( $meta -> getAttribute( 'name' ) == 'generator' )
        $cms = $meta -> getAttribute( 'content' );
    }
    return $cms;
  }
  
  public static function meta_title( $html )
  {
    $dom     = new \DOMDocument();
    @$dom -> loadHTML( $html );
    $metas   = $dom -> getElementsByTagName( 'title' );
    $meta_title = '';
    
    for ( $i = 0; $i < $metas -> length; $i++ )
    {
      $meta = $metas -> item( $i );
      $meta_title = $meta -> textContent;
    }
    return $meta_title;
  }
  
  public static function meta_description( $html )
  {
    $dom     = new \DOMDocument();
    @$dom -> loadHTML( $html );
    $metas   = $dom -> getElementsByTagName( 'meta' );
    $meta_description = '';
    
    for ( $i = 0; $i < $metas -> length; $i++ )
    {
      $meta = $metas -> item( $i );
      if ( $meta -> getAttribute( 'name' ) == 'description' )
        $meta_description = $meta -> getAttribute( 'content' );
    }
    return $meta_description;
  }
  
  public static function meta_keywords( $html )
  {
    $dom     = new \DOMDocument();
    @$dom -> loadHTML( $html );
    $metas   = $dom -> getElementsByTagName( 'meta' );
    $meta_keywords = '';
    
    for ( $i = 0; $i < $metas -> length; $i++ )
    {
      $meta = $metas -> item( $i );
      if ( $meta -> getAttribute( 'name' ) == 'keywords' )
        $meta_keywords = $meta -> getAttribute( 'content' );
    }
    return $meta_keywords;
  }
  
  public static function meta_robots( $html )
  {
    $dom     = new \DOMDocument();
    @$dom -> loadHTML( $html );
    $metas   = $dom -> getElementsByTagName( 'meta' );
    $meta_robots = '';
    
    for ( $i = 0; $i < $metas -> length; $i++ )
    {
      $meta = $metas -> item( $i );
      if ( $meta -> getAttribute( 'name' ) == 'robots' || $meta -> getAttribute( 'name' ) == 'googlebot' )
        $meta_robots = $meta -> getAttribute( 'content' );
    }
    return $meta_robots;
  }
  
  public static function header_count( $html, $header = 'h1' )
  {
    $dom     = new \DOMDocument();
    @$dom -> loadHTML( $html );
    $headers = $dom -> getElementsByTagName( $header );
    return $headers -> length;
  }
  
  public static function favicon( $html )
  {
    $dom     = new \DOMDocument();
    @$dom -> loadHTML( $html );
    $links   = $dom -> getElementsByTagName( 'link' );
    $favicon = '';

    for ( $i = 0; $i < $links -> length; $i++ )
    {
      $link = $links -> item( $i );
      if ( $link -> getAttribute( 'rel' ) == 'icon' || $link -> getAttribute( 'rel' ) == "Shortcut Icon" || $link -> getAttribute( 'rel' ) == "shortcut icon" )
        $favicon = $link -> getAttribute( 'href' );
    }
    return $favicon;
  }
  
  public static function audit( $url )
  {
    global $mdb;
    
    if ( !$url )
      return false;
    
    if ( strpos( 'http://', $url ) === false and strpos( 'https://', $url ) === false )
      $url = 'http://' . $url;
    
    $url_tmp = parse_url( $url );
    $url = strip_tags( $url_tmp['host'] );
    
    if ( $mdb -> count( 'as_sites', [ 'url' => $url ] ) )
      return $url;
    else
    {
      $ch = curl_init();
      curl_setopt( $ch, CURLOPT_URL, $url );
      curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
      curl_setopt( $ch, CURLOPT_VERBOSE, 1 );
      curl_setopt( $ch, CURLOPT_TIMEOUT, 60 );
      curl_setopt( $ch, CURLOPT_HEADER, true );
      curl_setopt( $ch, CURLOPT_CAINFO, 'cacert.pem' );
      curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
      curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
      curl_setopt( $ch, CURLOPT_HEADER, false );
      curl_setopt( $ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.103 Safari/537.36' );
      $response = curl_exec( $ch );
      
      $effective_url = curl_getinfo( $ch, CURLINFO_EFFECTIVE_URL );
      $ip = curl_getinfo( $ch, CURLINFO_PRIMARY_IP );      
      $location = file_get_contents( "http://ipinfo.io/{$ip}/json" );      
      $favicon = self::favicon( $response );
      $cms = self::cms( $response );
      $meta_robots = self::meta_robots( $response );
      $meta_title = self::meta_title( $response );
      $meta_description = self::meta_description( $response );
      $meta_keywords = self::meta_keywords( $response );
      $h1_count = self::header_count( $response, 'h1' );
      $h2_count = self::header_count( $response, 'h2' );
      $h3_count = self::header_count( $response, 'h3' );
      $h4_count = self::header_count( $response, 'h4' );
      $h5_count = self::header_count( $response, 'h5' );
      $h6_count = self::header_count( $response, 'h6' );
      
      $html_length = strlen( $response );
      $text_length = strlen( strip_tags( $response ) );
      $code_length = $html_length - $text_length;
      
      $words_count = str_word_count( strip_tags( $response ) );
      
      curl_close ( $ch );
      
      $ch = curl_init();
      curl_setopt( $ch, CURLOPT_URL, $effective_url . '/robots.txt' );
      curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
      curl_setopt( $ch, CURLOPT_VERBOSE, 1 );
      curl_setopt( $ch, CURLOPT_TIMEOUT, 60 );
      curl_setopt( $ch, CURLOPT_HEADER, true );
      curl_setopt( $ch, CURLOPT_CAINFO, 'cacert.pem' );
      curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
      curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
      curl_setopt( $ch, CURLOPT_HEADER, false );
      curl_setopt( $ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.103 Safari/537.36' );
      $robots_txt = curl_exec( $ch );
      curl_close ( $ch );
      
      $url_parse = parse_url( $effective_url );
      $url_tmp = $url_parse['host'];
      $redirect_www = false;
      
      if ( strpos( $url_tmp, $url_parse['scheme'] . '://www.' ) === 0 )
      {
        $ch = curl_init();
        curl_setopt( $ch, CURLOPT_URL, str_replace( $url_parse['scheme'] . '://www.', $url_parse['scheme'] . '://', $effective_url ) );
        curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
        curl_setopt( $ch, CURLOPT_VERBOSE, 1 );
        curl_setopt( $ch, CURLOPT_TIMEOUT, 60 );
        curl_setopt( $ch, CURLOPT_HEADER, true );
        curl_setopt( $ch, CURLOPT_CAINFO, 'cacert.pem' );
        curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
        curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
        curl_setopt( $ch, CURLOPT_HEADER, false );
        curl_setopt( $ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.103 Safari/537.36' );
        curl_exec( $ch );
        $effective_url_tmp  = curl_getinfo( $ch, CURLINFO_EFFECTIVE_URL );
        curl_close ( $ch );
        
        if ( $effective_url_tmp == $effective_url )
          $redirect_www = true;
      }
      else
      {
        $ch = curl_init();
        curl_setopt( $ch, CURLOPT_URL, str_replace( $url_parse['scheme'] . '://', $url_parse['scheme'] . '://www.', $effective_url ) );
        curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
        curl_setopt( $ch, CURLOPT_VERBOSE, 1 );
        curl_setopt( $ch, CURLOPT_TIMEOUT, 60 );
        curl_setopt( $ch, CURLOPT_HEADER, true );
        curl_setopt( $ch, CURLOPT_CAINFO, 'cacert.pem' );
        curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
        curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
        curl_setopt( $ch, CURLOPT_HEADER, false );
        curl_setopt( $ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.103 Safari/537.36' );
        curl_exec( $ch );
        $effective_url_tmp  = curl_getinfo( $ch, CURLINFO_EFFECTIVE_URL );
        curl_close ( $ch );
        
        if ( $effective_url_tmp == $effective_url )
          $redirect_www = true;
      }
      
      $mdb -> insert( 'as_sites', [ 
                'url' => $url, 
                'html' => $response,
                'effective_url' => $effective_url,
                'ip' => $ip,
                'location' => $location,
                'favicon' => $favicon,
                'cms' => $cms,
                'meta_robots' => $meta_robots,
                'robots_txt' => $robots_txt,
                'redirect_www' => $redirect_www ? 1 : 0,
                'https' => $url_parse['scheme'] == 'https' ? 1 : 0,
                'meta_title' => $meta_title,
                'meta_description' => $meta_description,
                'meta_keywords' => $meta_keywords,
                'code_length' => $code_length,
                'text_length' => $text_length,
                'words_count' => $words_count,
                'h1_count' => $h1_count,
                'h2_count' => $h2_count,
                'h3_count' => $h3_count,
                'h4_count' => $h4_count,
                'h5_count' => $h5_count,
                'h6_count' => $h6_count
              ] );
      
      return $url;
    }
  }
}
