<?php

include ('includes/db1config.php');
include ('../includes/mysql_connect.php');
//$url="http://www.pilotwebsolutions.com/index.php";
$url="http://www.pilotwebsolutions.com/custom/images/home-headerXX.png";

function check_url($url) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch , CURLOPT_RETURNTRANSFER, 1);
    $data = curl_exec($ch);
    $headers = curl_getinfo($ch);
    curl_close($ch);
    return $headers['http_code'];
}


//$check_url_status = check_url($url);
//if ($check_url_status == '200'){
//   echo "Link Works $check_url_status<br>";
//} else {
//   echo "Broken Link $check_url_status<br>";
//}


function check_url_hdr($url) {
   $headers = @get_headers( $url);
   $headers = (is_array($headers)) ? implode( "\n ", $headers) : $headers;

   return (bool)preg_match('#^HTTP/.*\s+[(200|301|302)]+\s#i', $headers);
}

if(isset($_POST['category']) && $_POST['category'] === 'Page titles' ){//Page titles
  testPageTitle($_POST['category']);
}

function testPageTitle($category){
  $qry = "DELETE from seo_issues WHERE category='".$category."'";
  $rs  = mysql_query($qry);
  
  $selectContent = "select pageID,seo_keywords,seo_title from content where active=1";
  $content       = mysql_query($selectContent);
  
  while ( $nt = mysql_fetch_array($content) ) {

    if($nt['seo_title'] == '' || $nt['seo_keywords'] == '' ){
      $test_result_variable   = "It is recommended to enter a unique title and set of keywords for each page on your website.";
      insertIssues('Page titles',$nt['pageID'],$test_result_variable);
    } 

    if(strlen($nt['seo_title']) <45 ){
      $test_result_variable   = "A good length for a page title is 50-60 characters. If you use too few, you are not using the full power of the title to help your search results.";
       insertIssues('Page titles',$nt['pageID'],$test_result_variable);
    } 

    if(strlen($nt['seo_title']) >60 ){
      $test_result_variable  = "A good length for a page title is 50-60 characters. If you use too many, search engines may ignore valuable words in your page title.";
      insertIssues('Page titles',$nt['pageID'],$test_result_variable);
    } 

    if( $nt['seo_title'] != $nt['seo_keywords'] ){
      $test_result_variable = "It is helpful to use at least one of the page's keywords in the page title. This gives the keyword more value to search engines if it is also in the page title.";
      insertIssues('Page titles',$nt['pageID'],$test_result_variable);
    } 

  }
  $getSeoIssues = "select COUNT(recordkey) from seo_issues";
  $rs    = mysql_query($getSeoIssues);
  $count = mysql_fetch_row($rs);

  if( $count[0] > 0 ){
    $status_variable = "Actions recommended";
    $updatechecklist = "UPDATE seo_checklist SET status = '".$status_variable."' WHERE task = 'Page titles' ";
    $rs = mysql_query($updatechecklist);
    echo json_encode($status_variable);
  } else {
    $status_variable = "tested ok";
    $updatechecklist = "UPDATE seo_checklist SET status = ".$status_variable." WHERE task = 'Page titles' ";
    $rs = mysql_query($updatechecklist);
    echo json_encode($status_variable);
  }
}

function insertIssues($category,$pageid,$testvariable){
  $insert = 'INSERT INTO seo_issues (category,page_id,description) VALUES ("'.$category.'","'.$pageid.'","'.$testvariable.'")';
  $rs     = mysql_query($insert);
  return mysql_insert_id();
}

//if (check_url_hdr($url)){
//   echo "Link Works<br>";
//}else{
//   echo "Broken Link<br>";
//}
   


?>