<?php

/*
 * @author Balaji
 * @name: A to Z SEO Tools - PHP Script
 * @copyright 2023 ProThemes.Biz
 *
 */

function googleRank($searchDomain, $keyword, $position = 50, $googleDomain) {

    $currentPage = 1;
    $resPerPage = 0;
    $totalCount = 1;

    $googleUrl = "https://www." . $googleDomain . "/search?hl=en&q=" . urlencode($keyword);
    $data = curlGET_Text($googleUrl);
    $keywordFormat = getMyData(LIB_DIR . 'keyword_position.tdata');

    while (true) {
        $dom = new DOMDocument();
        @$dom->loadHTML($data);
        $xpath = new DOMXPath($dom);

        $hrefs = $xpath->evaluate($keywordFormat);
        $linkcount = $hrefs->length;

        for ($i = 0; $i < $linkcount; $i++) {

            $href = $hrefs->item($i);
            $url = $href->getAttribute('href');

            if($url === NULL) continue;
            if($url === '') continue;
            if($url === '#') continue;
            if(substr($url, 0 ,38) === 'https://webcache.googleusercontent.com') continue;
            if(substr($url, 0 ,28) === 'https://translate.google.com') continue;
            if(substr($url, 0 ,10) === '/search?hl') continue;
            if(substr($url, 0 ,10) === '/search?sc') continue;

            $searchDomain = "http://".clean_url($searchDomain);
            $searchDomainArr = parse_url($searchDomain);
            $searchDomain = $searchDomainArr['host'];

            $url = cleanWWW($url);

            if (strstr($url, $searchDomain)) {
                return array(
                    $url,
                    $totalCount,
                    $currentPage);

            }

            if ($totalCount == $position) {
                return;
            }

            $totalCount++;
        }

        if (!strstr($data, "Next"))
            break;

        sleep(rand(2, 4));
        $resPerPage = $resPerPage + 10;
        $currentPage++;
        flush();

        $googleUrl = "https://www." . $googleDomain . "/search?hl=en&q=" . urlencode($keyword) .
            "&start=" . $resPerPage . "&sa=N";
        $data = curlGET_Text($googleUrl);
    }
}

function dooRank($searchDomain, $keyword, $position = 50, $yahooDomain = 'yahoo.com') {
    $data = $resData = $yahooUrl = '';
    
    $yahooTLD = clean_url($yahooDomain);
    $yahooTLD = explode(".", $yahooTLD);
    $yahooTLD = $yahooTLD[1];

    if ($yahooTLD == 'com')
        $yahooTLD = '';
    else
        $yahooTLD = $yahooTLD . '.';

    $currentPage = 1;
    $resPerPage = 0;
    $totalCount = 1;

    $rn = $resPerPage == '0' ? '' : $resPerPage;

    $yahooUrl = "https://" . $yahooTLD . "search." . $yahooDomain . "/search?p=" .
        urlencode($keyword) . "&b=" . $rn . "1";

    $data = curlGET_Text($yahooUrl);
    $resData = getCenterText('searchCenterMiddle">', '</ol>', $data);
    $resPerPage = 10;
    while (true)
    {

        $dom = new DOMDocument();
        @$dom->loadHTML($resData);

        $xpath = new DOMXPath($dom);
        $hrefs = $dom->getElementsByTagName('a');


        $linkcount = $hrefs->length;

        for ($i = 0; $i < $linkcount; $i++)
        {

            $href = $hrefs->item($i);
            $url = $href->getAttribute('href');
            $searchDomain = clean_url($searchDomain);
            $url = clean_url($url);

            if (strstr($url, $searchDomain)) {
                return array(
                    $url,
                    $totalCount,
                    $currentPage);
            }

            if ($totalCount == $position) {
                return;
            }

            $totalCount++;
        }

        if (!strstr($data, "Next")){
            break;
        }

        sleep(rand(3, 5));
        $resPerPage = $resPerPage + 10;
        $currentPage++;
        flush();
        $yahooUrl = "https://" . $yahooTLD . "search." . $yahooDomain . "/search?p=" .
            urlencode($keyword) . "&b=" . $resPerPage;
        $data = curlGET_Text($yahooUrl);
        $resData = getCenterText('searchCenterMiddle">', '</ol>', $data);
    }
}

function updateGoogleKeyFormat(){

    $fL = 'keyword_position.tdata';

    $l = 'http://api.prothemes.biz/tools/keyword-position-format.txt';
    $l = simpleCurlGET($l);

    if($l != '') putMyData(LIB_DIR.$fL, $l);

}