<?php
    require_once "db.php";

//     error_reporting(E_ALL);
//     ini_set('display_errors', 'On');

    $my_dist_host = '167.114.96.213';
    $my_dist_user = 'auditor';
    $my_dist_pass = 'yb9738z';
    $my_dist_db = 'auditor_distributed';

    define('MY_IP', '167.114.96.216');

    define('MONGO_SERVER', 'mongodb://167.114.96.216:27017');

    if (!empty($_GET['project']))
    {
        // $_GET['project'] = "flipkart.com";
        define('URL_TO_CRAWL', $_GET['project']);
    }
    if(!empty($_GET['p_id']))
    {
        $dist_db = conDist($my_dist_host, $my_dist_user, $my_dist_pass, $my_dist_db);

        $qry = "SELECT id,dup_running,expired,pages_to_crawl FROM projects WHERE id_hash='".$_GET['p_id']."'";
        $chk = mysqli_query($dist_db, $qry);
        $res = mysqli_fetch_assoc($chk);

        define('ORIG_PID', $res['id']);

        if($res['expired'] == 2 && $res['pages_to_crawl'] == 0) {
            $result = json_encode(
                array(
                    'response_code' => 102,
                    'quota_reached' => 1,
                    'response_message' => "Quota limit reached. Project not analyzed."
                )
            );
            echo $result; exit;
        }
        $pid = (int)$res['id'];

        if($res['dup_running'] == 1) {
            $qry = "SELECT id FROM projects WHERE domain='".$_GET['project']."' AND dup_running=0";
            $chk = mysqli_query($dist_db, $qry);
            $res = mysqli_fetch_assoc($chk);
            $pid = (int)$res['id'];
        }

        $qry3 = "SELECT history_created,dump_deleted,status,last_crawled_on,crawl_no,runnow FROM projects WHERE id=$pid";
        if($chk3 = mysqli_query($dist_db, $qry3)) {
            $res3 = mysqli_fetch_assoc($chk3);
            if(($res3['history_created'] != 2 || $res3['dump_deleted'] != 2 || $res3['status'] != 2) && $res3['last_crawled_on'] != "") {
                if($res3['runnow'] == 1 && $res3['crawl_no'] == 1) {
                    $result = json_encode(
                        array(
                            'response_code' => 101,
                            'response_message' => "Processing data. Please check after a minute..."
                        )
                    );
                    echo $result; exit;
                } else {
                    define('SHOW_OLD_WEEK', 1);
                }
            } else {
                // $time_diff = (abs(strtotime('2015-09-20 20:40:12') - strtotime(date('Y-m-d H:i:s'))) / 86400);
                // if($time_diff >= 7 || $res3['crawl_no'] < 2 ) {
                //     $qry = "UPDATE projects SET runnow=1 WHERE id=$pid";
                //     if(!mysqli_query($dist_db, $qry)) {
                //         $result = json_encode(
                //             array(
                //                 'response_code' => 101,
                //                 'response_message' => "Error Creating Runnow"
                //             )
                //         );
                //         echo $result; exit;
                //     } else {
                //         $result = json_encode(
                //             array(
                //                 'response_code' => 100,
                //                 'runnow' => 1,
                //                 'response_message' => "runnow created"
                //             )
                //         );
                //         echo $result; exit;
                //     }
                // }
            }
        }
        // $_GET['p_id'] = 7;
        define('P_ID', $pid);
    }

    if(!empty($_GET['crawl_pages']))
        define('TOTAL_PAGES', (int)$_GET['crawl_pages']);

    define('DATA_SEPERATOR', hash('sha1', "RankWatchCrawl"));
    define('CRAWL_DIR', "../CrawlData/");
    define('PROCESS_DIR', "process/");
    define('STATS_DIR', CRAWL_DIR.URL_TO_CRAWL."/");
    $statsDir = CRAWL_DIR.URL_TO_CRAWL;
    define('STRIPPED_DIR', $statsDir.".stripped/");
    define('DUPLICATE_DUMP', $statsDir.".stripped.dupStats.txt");
    define('DUPLICATE_COLOR', "background-color:#FE642E;");

    define('DUPLICATE_DIR', __DIR__."/Duplicates/".URL_TO_CRAWL.".duplicate/");

    define('CRAWL_USER_AGENT', "USER_AGENT = 'Googlebot/2.1 (+http://www.google.com/bot.html)'");
    define('CRAWL_DEPTH_LIMIT', 4);

    define('CRAWL_TIME', 7);


    // thresholds for server connect times
    define('T_CONNECT_DANGER', 0.60);
    define('T_RESPONSE_DANGER', 1.6);
    define('T_TRANSFER_DANGER', 20);

    define('T_CONNECT_CONSIDER', 0.40);
    define('T_RESPONSE_CONSIDER', 1.2);
    define('T_TRANSFER_CONSIDER', 30);

    define('T_CONNECT_OPTIMAL', 0.20);
    define('T_RESPONSE_OPTIMAL', 0.60);
    define('T_TRANSFER_OPTIMAL', 60);

    // thresholds for page content
    define('T_TITLE_HIGH', 100);
    define('T_TITLE_LOW', 10);
    define('T_META_HIGH', 250);
    define('T_META_LOW', 100);
    define('T_DESC_HIGH', 200);
    define('T_DESC_LOW', 50);
    define('T_PAGE_SIZE_HIGH', 100000);
    define('T_URL_HIGH', 115);
    define('T_TEXT_HTML_RATIO', 15);
    define('T_CONTENT_THIN', 200);
    define('T_STYLE_NO', 4);
    define('T_PAGE_LOAD', 0.5);
    define('T_IMG_NO', 1); // remove
    define('T_H1_NO', 2);
    define('T_H2_NO', 4);
    define('T_H3_NO', 1); // remove
    define('T_H4_NO', 1); // remove
    define('T_LINK_INT_HIGH', 100); // remove
    define('T_LINK_EXT_HIGH', 100); // remove
    define('T_SPEED_SLOW', 30); // remove
    define('T_SPELL_COUNT', 10); // remove


    define('T_ERR_MAJOR', ",
        dde,
        eti,
        dti,
        dkw,
        tu,
        mrf,
    ");
    define('T_ERR_MEDIUM', ",
        frm,
        fls,
        sti,
        lt,
        mds,
        lu,
        upu,
        unu,
        spu,
        eex,
        ent,
        nfc,
        thn,
        low,
        big,
        t,
        mnf,
        mh1,
        eh1,
        mh2,
        eh2,
        mal,
        eim,
    ");
    // esp,

    define('T_ERR_MINOR', ",
        cms,
        dms,
        mlg,
        mkw,
        lkw,
        skw,
        lds,
        sds,
        mst,
    ");



    define('DDE_WEIGHT', 5);
    define('ETI_WEIGHT', 10);
    define('DTI_WEIGHT', 7);
    define('DKW_WEIGHT', 3);
    define('TU_WEIGHT', 10);
    define('MRF_WEIGHT', 7);

    define('FRM_WEIGHT', 1);
    define('FLS_WEIGHT', 1);
    define('STI_WEIGHT', 2);
    define('LT_WEIGHT', 2);
    define('MDS_WEIGHT', 1);
    define('LU_WEIGHT', 2);
    define('UPU_WEIGHT', 1);
    define('UNU_WEIGHT', 1);
    define('SPU_WEIGHT', 1);
    define('EEX_WEIGHT', 3);
    define('ENT_WEIGHT', 3);
    define('NFC_WEIGHT', 1);
    define('THN_WEIGHT', 3);
    define('LOW_WEIGHT', 3);
    define('ESP_WEIGHT', 1);
    define('BIG_WEIGHT', 2);
    define('T_WEIGHT', 2);
    define('MNF_WEIGHT', 1);
    define('MH1_WEIGHT', 3);
    define('EH1_WEIGHT', 3);
    define('MH2_WEIGHT', 2);
    define('EH2_WEIGHT', 2);
    define('MAL_WEIGHT', 2);
    define('EIM_WEIGHT', 2);

    define('CMS_WEIGHT', 0);
    define('DMS_WEIGHT', 0);
    define('MLG_WEIGHT', 0);
    define('MKW_WEIGHT', 0);
    define('LKW_WEIGHT', 0);
    define('SKW_WEIGHT', 0);
    define('LDS_WEIGHT', 0);
    define('SDS_WEIGHT', 0);
    define('MST_WEIGHT', 0);

    define('T_GRADE_A_LOW', 0);
    define('T_GRADE_A_HIGH', 3);

    define('T_GRADE_B_LOW', 4);
    define('T_GRADE_B_HIGH', 6);

    define('T_GRADE_C_LOW', 7);
    define('T_GRADE_C_HIGH', 9);

    define('T_GRADE_D_LOW', 10);
    define('T_GRADE_D_HIGH', 13);

    define('T_GRADE_E_LOW', 14);
    define('T_GRADE_E_HIGH', 16);

	define('T_GRADE_F_LOW', 17);


    $desc_arr = array(
        'mal' => array('Missing Image Alt', 'have missing ALT in images', 'eimg.mal'),
        'mkw' => array('Missing Meta Keyword', 'have meta keywords missing', 'emet.mkw'),
        'lkw' => array('Long Meta Keyword', 'have too long meta keyword', 'emet.lkw'),
        'skw' => array('Short Meta Keyword', 'have too short meta keyword', 'emet.skw'),
        'mds' => array('Missing Meta Description', 'have meta description missing', 'emet.mds'),
        'lds' => array('Long Meta Description', 'have too long meta description', 'emet.lds'),
        'sds' => array('Short Meta Description', 'have too short meta keyword', 'emet.sds'),
        'thn' => array('Thin Content', 'have thin content', 'epage.thn'),
        'low' => array('Text to Html Ratio', 'have low text to html ratio', 'etth.low'),
        'dti' => array('Duplicate Title', 'have duplicate title in them', 'etit.dti'),
        'dkw' => array('Duplicate Meta Keywords', 'have duplicate meta keywords in them', 'emet.dkw'),
        'dde' => array('Duplicate Meta Description', 'have duplicate meta description in them', 'emet.dde'),
        'tu' => array('Duplicate Content', 'have duplicate content in them', 'tu'),
        'lt' => array('Long Title', 'have too long title', 'etit.lt'),
        'sti' => array('Short Title', 'have too short title', 'etit.sti'),
        'eti' => array('Empty/Missing Title', 'have empty title', 'etit.eti'),
        'mrf' => array('Meta Refresh', 'have meta refresh', 'emet.mrf'),
        'mre' => array('Meta Redirect', 'have meta redirect', 'emet.mre'),
        'mnf' => array('Meta Nofollow', 'have meta nofollow', 'emet.mnf'),
        'mlg' => array('Missing Language Declaration', 'have language declaration missing', 'mlg'),
        'mst' => array('Too Many Stylesheets', 'have too many stylesheets included', 'esht.mst'),
        'mh1' => array('Missing H1 Tag', 'have missing h1 tag', 'eh1s.mh1'),
        'eh1' => array('Too Many H1 Tags', 'have too many h1 tags', 'eh1s.eh1'),
        'mh2' => array('Missing H2 Tag', 'have missing h2 tag', 'eh2s.mh2'),
        'eh2' => array('Too Many H2 Tags', 'have too many h2 tags', 'eh2s.eh2'),
        'lu' => array('Long URL', 'have too long URL', 'eurl.lu'),
        'spu' => array('Special Characters in URL', 'have special characters in URL', 'eurl.spu'),
        'unu' => array('Underscore Characters in URL', 'have underscores in URL', 'eurl.unu'),
        'upu' => array('UpperCase Characters in URL', 'have uppercase characters in URL', 'eurl.upu'),
        'frm' => array('Using Iframes', 'have iframes in content', 'efrm.frm'),
        'fls' => array('Using Flash', 'have flash in content', 'efls.fls'),
        'ent' => array('Too Many Internal Links', 'have too many internal links', 'eln.ent'),
        'eex' => array('Too Many External Links', 'have too many external links', 'eln.eex'),
        'nfc' => array('NoFollow Links', 'have nofollow links', 'eln.nfc'),
        'eim' => array('Too many images', 'have too many images', 'eimg.eim'),
        // 'esp' => array('Too many spelling mistakes', 'have too many spelling mistakes', 'espl.esp'),
        't' => array('Slow Page Speed', 'have slow response time', 't'),
        'big' => array('Page Size too Big', 'have big page size', 'epage.big'),
        'cms' => array('Missing Characterset Declaration', 'have characterset declaration missing', 'emet.cms'),
        'dms' => array('Missing Doctype Declaration', 'have doctype declaration missing', 'edoc.dms')
    );

    foreach ($desc_arr as $eckey => $ecvalue) {
        $err_col_arr[$ecvalue[2]] = 1;
    }


    $processed_arr = array(
    );

    $final_arr = array_merge($desc_arr, $processed_arr);


    $ERR_VALUES = array(
        'emet.kws' => 1,
        'emet.desc' => 1,
        'etit.tit' => 1,
        'etth.ratio' => 1,
        'dp' => 1,
        'tu' => 1,
        't' => 1,
        'epage.wrd' => 1,
        'u' => 1,
        'eln.int' => 1,
        'eln.ext' => 1,
        'eln.nfc' => 1,
        'eln.fc' => 1,
        'eln.t' => 1,
        'eimg.c' => 1,
        'esht.sts' => 1,
        'espl.c' => 1,
        'eh1s.h1t' => 1,
        'eh2s.h2t' => 1,
        'epage.hsi' => 1,
    );



	/**
	 *   Function: Clear old data from existing Directories before statting process.
	 *
	 *   @param $dir: directory to empty
	 *   @param $mongoDB: mongoDb connection resource
	 */
	function clear_dirs($dir, $mongoDB)
	{
	    if (!empty($dir) && is_dir($dir)) {
	        $oldDups = array_diff(scandir($dir), array('.', '..'));
	        foreach ($oldDups as $oldFile) {
	            $oldFilePath = $dir.$oldFile;
	            if (file_exists("$oldFilePath")) {
	                if (!unlink("$oldFilePath")) {
	                    log_error($projId, "file", error_get_last(), $mongoDB);
	                }
	            }
	        }
	    }
	}



	/**
	 * [calculate_grade description]
	 * @param  [type] $page_errors [description]
	 * @return [type]              [description]
	 */
	function calculateGrade($url_grade_errors) {
	    $gradeSum = 0;
	    foreach ($url_grade_errors as $ugkey => $ugvalue) {
	        $err_weight = strtoupper($ugkey)."_WEIGHT";

	        $gradeSum += constant($err_weight);
	    }
	    if($gradeSum <= T_GRADE_A_HIGH)
	        return "A";
	    if($gradeSum >= T_GRADE_B_LOW && $gradeSum <= T_GRADE_B_HIGH)
	        return "B";
	    if($gradeSum >= T_GRADE_C_LOW && $gradeSum <= T_GRADE_C_HIGH)
	        return "C";
	    if($gradeSum >= T_GRADE_D_LOW && $gradeSum <= T_GRADE_D_HIGH)
	        return "D";
	    if($gradeSum >= T_GRADE_E_LOW && $gradeSum <= T_GRADE_E_HIGH)
	        return "E";
	    if($gradeSum >= T_GRADE_F_LOW)
	        return "F";
        return "F";

	}


    function rel2abs($rel, $base)
    {
        return $rel;
        /* return if already absolute URL */
        if (parse_url($rel, PHP_URL_SCHEME) != '') return $rel;

        /* queries and anchors */
        if ($rel[0]=='#' || $rel[0]=='?') return $base.$rel;

        /* parse base URL and convert to local variables:
           $scheme, $host, $path */
        extract(parse_url($base));

        /* remove non-directory element from path */
        $path = preg_replace('#/[^/]*$#', '', $path);

        /* destroy path if relative url points to root */
        if ($rel[0] == '/') $path = '';

        /* dirty absolute URL */
        $abs = "$host$path/$rel";

        /* replace '//' or '/./' or '/foo/../' with '/' */
        $re = array('#(/\.?/)#', '#/(?!\.\.)[^/]+/\.\./#');
        for($n=1; $n>0; $abs=preg_replace($re, '/', $abs, -1, $n)) {}

        /* absolute URL is ready! */
        return $scheme.'://'.$abs;
    }


    /**
     * 	Function: logs custom erros in DB and provides front end error managment.
     *
     *	@param $uid: logged in user id
     *	@param $section: section where error occured, (file,mysql,etc)
     *	@param $cause: detaild error reporting, mostly error_get_last() output
     *	@param $mongoDB: mongoDB connection resource
     */
    function log_error($pid = 0, $section, $cause, $mongoDB)
    {
        $errorArr = array(
            'u_id' => 1,
            'p_id' => $pid,
            'section' => $section,
            'cause' => $cause,
            'created' => time(),
        );

        $ts = $mongoDB->auditor_log->insert($errorArr);

        // send response to show on front end

  //       echo "<script type='text/javascript'>
		// 	$.growlUI('Error', 'Internal Error Occured');
		// </script>";
    }


    function resize_image($file, $nwidth, $nheight)
    {
        $newheight = $nheight;
        $newwidth = $nwidth;
        // $filename = 'images/picture.jpg';
        //the resize will be a percent of the original size
        // $percent = 0.5;
        $con_type = mime_content_type($file);
        // Content type
        header('Content-Type: '.$con_type);

        // Get new sizes
        list($width, $height) = getimagesize($file);
        // $newwidth = $width * $percent;
        // $newheight = $height * $percent;

        // Load
        $thumb = imagecreatetruecolor($newwidth, $newheight);
        $source = imagecreatefromjpeg($file);

        // Resize
        imagecopyresized($thumb, $source, 0, 0, 0, 0, 20, 20, $width, $height);

        // Output and free memory
        //the resized image will be 400x300
        return $thumb;
        // imagedestroy($thumb);

    }


    function get_error_desc($err, $errval, $no_pages)
    {
        $show_err = "";
        $show_err_more = "";
        $more_details = 0;

        // array_walk_recursive(
        //     $errval,
        //     function (&$value) {
        //         if (!is_numeric($value)) {
        //             $value = html_entity_decode($value, ENT_QUOTES);
        //         }
        //     }
        // );

        if(!empty($errval['u'])) $errval['u'] = rawurlencode(substr($errval['u'], 0, 72))."...";
        $link_id = "see_more_link";

        //minor
        if($err == 'esp')
        {
            $show_err = "You have <b>".$errval['espl']['c']."</b> spelling errors on the URL <i>".$errval['u']."</i>. Please click <a href='javascript:void(0);' id='".$link_id."'><b>here</b></a> to see all corrections.";
            $show_err_more = "";
            // $more_details = 1;
        }
        if($err == 'cms')
        {
            $show_err = "Your URL <i>".$errval['u']."</i> has missing char encoding.";
            $show_err_more = "";
            // $more_details = 1;
        }
        if($err == 'dms')
        {
            $show_err = "Your URL <i>".$errval['u']."</i> has missing doctype decleration.";
            $show_err_more = "";
            // $more_details = 1;
        }
        if($err == "big")
        {
            if(empty($errval['epage']['hsi']))
                $errval['epage']['hsi'] = "300000";
            $show_err = "Your URL <i>".$errval['u']."</i> is too large with the size at <b>".round($errval['epage']['hsi']/1000)." KBs.</b> ";
            $show_err_more = "";
            // $more_details = 1;
        }
        if($err == 't')
        {
            $show_err = "Your URL <i>".$errval['u']."</i> has taken <b>".round($errval['t'])." sec</b> to load which is slower than the industry standards.";
            $show_err_more = "The speed of a page is made up of both Front-end and Server-side components. Currently, the load speed of the URL ".$errval['u']." is  is slow. As per industry standards the load speed of a URL should be less than 30 seconds. ";
            // $more_details = 1;
        }
        if($err == 'frm')
        {
            $show_err = "Your page has iframes in it.";
            $show_err_more = "";
        }
        if($err == 'fls')
        {
            $show_err = "Your page has flash content in it.";
            $show_err_more = "";
        }
        if($err == 'spu')
        {
            $show_err = "Your URL <i>'".$errval['u']."'</i> has unsafe/special characters.";
            $show_err_more = "unsafe/special characters are restricted by the Internet Engineering Task Force (IETF) who have clearly defined these specifications. Unsafe characters are classified as blank/empty spaces and ' < > # % { } | \ ^ ~ [ ] `";
        }
        if($err == 'ent')
        {
            $show_err = "Your URL has <a href='javascript:void(0);' id='".$link_id."'><b>".$errval['eln']['int']."</b></a> links.";
            $show_err_more = "";
            // $more_details = 1;
        }
        if($err == 'eex')
        {
            $show_err = "Your URL has <a href='javascript:void(0);' id='".$link_id."'><b>".$errval['eln']['ext']."</b></a> external links.";
            $show_err_more = "";
            // $more_details = 1;
        }
        if($err == 'nfc')
        {
            $show_err = "Your Page has <b>".$errval['eln']['nfc']."</b> nofollow links in it.";
            $show_err_more = "";
        }
        if($err == "eim")
        {
            $show_err = "Your URL <i>'".$errval['u']."'</i> has <a href='javascript:void(0);' id='".$link_id."'><b>".$errval['eimg']['c']."</b></a> images.";
            $show_err_more = "If your images are too large they will create numerous problems and issues on your site. First, your website will be slow. Second, your site visitors will become frustrated with the slow speed of the website Third, large images can take up a lot of space on your server";
            // $more_details = 1;
        }
        elseif($err == 'mst')
        {
                $show_err = "Your URL is linking <a href='javascript:void(0);' id='".$link_id."'><b>".count($errval['esht']['sts'])."</b></a> stylesheets.";
            $show_err_more = "";
            // $more_details = 1;
        }
        elseif($err == 'mlg')
        {
            $show_err = "This page has missing Language Declaration.";
            $show_err_more = "";
        }
        elseif($err == 'mkw')
        {
            $show_err = "Meta Keywords are missing from your webpage.";
            $show_err_more = "Not the most important element of a web page. But it is advisable to include Meta Keywords on your webpage as it gives structure to the page.";
        }
        elseif($err == 'skw')
        {
            $show_err = "Your Meta Keyword <i>'".$errval['emet']['kws']."'</i> is <b>".mb_strlen(html_entity_decode($errval['emet']['kws'], ENT_QUOTES))."</b> characters long.";
            $show_err_more = "According to Google's Guidelines it should not be less than <b>".T_META_LOW."</b> characters. Meta keywords are distinguished from regular keywords because they appear “behind the scenes,” in the source code of your page, rather than on the live, visible page itself.";
            // $more_details = 1;
        }
        elseif($err == 'sti')
        {
            $show_err = "Your Title Tag <i>'".$errval['etit']['tit']."'</i> is <b>".mb_strlen(html_entity_decode($errval['etit']['tit'], ENT_QUOTES))."</b> characters long.";
            $show_err_more = "According to Google's Guidelines it should not be less than 25 characters. Making your title tags close to 70 characters is a good way to get the most out of your title tags and optimize your chances of showing up for different keyword searches.";
        }

        // medium
        elseif($err == 'mds')
        {
            $show_err = "The Meta Description is missing from the webpage.";
            $show_err_more = "Meta descriptions are HTML attributes that provide concise explanations of the contents of web pages. Meta descriptions are commonly used on search engine result pages (SERPs) to display preview snippets for a given page.";
        }
        elseif($err == 'unu')
        {
            $show_err = "Your URL <i>'".$errval['u']."'</i> has underscore ( _ ) in it.";
            $show_err_more = "Search engines treat dashes and underscores differently from one another. Google has clearly stated that when it comes to URL structure, using hyphens rather than underscores makes it much easier for them to identify what the page is about. Read more here- https://support.google.com/webmasters/answer/76329?hl=en";
        }
        elseif($err == 'mh2')
        {
            $show_err = "Your URL <i>'".$errval['u']."'</i> does not have the H2 Tag.";
            $show_err_more = "Unlike the H1 tag, you may have multiple H2, H3, H4, H5, and H6 sub-headings. Think of these sub-headings as ways of organizing the content on your page for your readers more than for the search engines.
";
        }
        elseif($err == 'upu')
        {
            $show_err = "Your URL <i>'".$errval['u']."'</i> has upper case characters in it.";
            $show_err_more = "It is considered bad SEO practice if you have capitals in your URL as different servers handle capitalizations differently and those pages will be seen as two different pages. Consequently, if the different pages (URL's) lead to the same page it is bad for your search rankings.";
        }
        elseif($err == 'mnf')
        {
            $show_err = "Your URL <i>'".$errval['u']."'</i> uses meta nofollow tag.";
            $show_err_more = "";
        }
        elseif($err == 'mre')
        {
            $show_err = "Your URL <i>'".$errval['u']."'</i> uses meta refresh redirect";
            $show_err_more = "";
        }
        elseif($err == 'mrf')
        {
                $show_err = "Meta Refresh tag was found on this page. ";
            $show_err_more = "";
        }

        elseif($err == 'sds')
        {
            $show_err = "Your Meta Description <i>'".$errval['emet']['desc']."'</i> is <b>".mb_strlen(html_entity_decode($errval['emet']['desc'], ENT_QUOTES))."</b> characters long.";
            $show_err_more = "According to Google's Guidelines it should not be less than <b>".T_DESC_LOW."</b> characters. Ideally, the Meta Description should be between 140-160 characters.The meta description is used by search engines to describe the Web page in their results.";
            // $more_details = 1;
        }
        elseif($err == 'lds')
        {
            $show_err = "Your Meta Description <i>'".$errval['emet']['desc']."'</i> is <b>".mb_strlen(html_entity_decode($errval['emet']['desc'], ENT_QUOTES))."</b> characters long.";
            $show_err_more = "According to Google's Guidelines it should not be greater than 200 characters as Google starts cutting off descriptions after 155-160 characters. The meta description is a meta tag that is used to describe the current Web page. It provides a short summary of the page so that readers can decide whether the page is what they are looking for.";
            // $more_details = 1;
        }
        elseif($err == "low")
        {
            $show_err = "Your URL <i>'".$errval['u']."'</i> has low Text-to-HTML Ratio at <b>".round($errval['etth']['ratio'])."</b>.";
            $show_err_more = "Every website has a certain amount of backend code and a certain amount of regular text in the front-end. The text to HTML ratio refers to the amount of text on a web page versus the HTML code on the same web page.";
        }

        // major
        elseif($err == "eti")
        {
            $show_err = "The Title Tag is missing from your webpage.";
            $show_err_more = "The title tag is the single most important on-page SEO element. It is critical to both user experience and search engine optimization as it defines the title of the document.";
        }
        elseif($err == 'eh2')
        {
            $show_err = "Your URL <i>'".$errval['u']."'</i> has <a href='javascript:void(0);' id='".$link_id."'><b>".count($errval['eh2s']['h2t'])."</b></a> H2 Tags.";
            $show_err_more = "Ideally, there should be a maximum number of <b>".T_H2_NO."</b> H2 tags on your webpage.";
        }
        elseif($err == 'eh1')
        {
            $show_err = "Your URL <i>'".$errval['u']."'</i> has <a href='javascript:void(0);' id='".$link_id."'><b>".count($errval['eh1s']['h1t'])."</b></a> H1 Tags.";
            $show_err_more = "Ideally, there should be a maximum number of <b>".T_H1_NO."</b> H1 tags on your webpage.";
        }
        elseif($err == 'mh1')
        {
            $show_err = "Your URL <i>'".$errval['u']."'</i> does not have the H1 Tag.";
            $show_err_more = "The proportion of the H1 Tag's  influence on rankings compared to the overall picture has diminished over time, but is still an important ranking factor.";
        }
        elseif($err == 'lu')
        {
            $show_err = "Your URL is <b>".mb_strlen($errval['u'])."</b> characters long.";
            $show_err_more = "According to Google's Guidelines it should not be less than 50 characters. Ideally, the Meta Description should be between 140-160 characters.The meta description is used by search engines to describe the Web page in their results. ";
        }
        elseif($err == 'dde')
        {
            $show_err = "Your Meta Description <i>'".$errval['emet']['desc']."'</i> is used on <a href='javascript:void(0);' id='".$link_id."'><b>".$no_pages."</b></a> other pages.";
            $show_err_more = "As suggested by Matt Cutts himself, it is better to have unique meta descriptions and even no meta descriptions at all, than to show duplicate meta descriptions across pages.";
            // $more_details = 1;
        }

        elseif($err == 'lt')
        {
            $show_err = "Your Title Tag <i>'".$errval['etit']['tit']."'</i> is <b>".mb_strlen(html_entity_decode($errval['etit']['tit'], ENT_QUOTES))."</b> characters long.";
            $show_err_more = "According to Google's Guidelines it should not be greater than 100 characters. Search engines display only the first 70 characters of a web page’s title tag. The rest gets chopped off and replaced with ellipses.";
            // $more_details = 1;
        }
        elseif($err == 'dkw')
        {
            $show_err = "Your Meta Keyword <i>'".$errval['emet']['kws']."'</i> is used on <a href='javascript:void(0);' id='".$link_id."'><b>".$no_pages."</b></a> other pages.";
            $show_err_more = "Having duplicate Meta Keywords will be considered as 'Keyword Stuffing'. The most important thing to keep in mind when selecting or optimizing your meta keywords is to be sure that each keyword accurately reflects the content of your page.";
            // $more_details = 1;
        }

        elseif($err == 'dti')
        {
            $show_err = "Your Title Tag is used on <a href='javascript:void(0);' id='".$link_id."'><b>".$no_pages."</b></a> other pages.";
            $show_err_more = "Having duplicate title tags may result in only 1 of your pages getting indexed or your website ranking on only 1 keyword. Google is all about original content and anything you can do to make the content fresh and different will help Google, you and your visitors.";
            // $more_details = 1;
        }
        elseif($err == "mal")
        {
            $show_err = "Your URL <i>'".$errval['u']."'</i> has missing Alt-Tags.<br /> Click <a href='javascript:void(0);' id='".$link_id."'><b>Here</b></a> to see the complete list of missing alt tags.";
            $show_err_more = "The alt attribute is used to describe the contents of an image file. It's important for several reasons:<br /> 1) It provides Google with useful information about the subject matter of the image. Google uses this information to help determine the best image to return for a user's query.<br /> 2) It helps users with visual impairments, or people using screen readers or who have low-bandwidth connections—may not be able to see images on web pages. Descriptive alt text provides these users with important information.";
            // $more_details = 1;

        }
        elseif($err == 'lkw')
        {
            $show_err = "Your Meta <i>'".$errval['emet']['kws']."'</i> Keyword is <b>".mb_strlen(html_entity_decode($errval['emet']['kws'], ENT_QUOTES))."</b> characters long.";
            $show_err_more = "According to Google's Guidelines it should not be greater than 250 characters. Meta Keywords are a specific type of meta tag that appear in the HTML code of a Web page and help tell search engines what the topic of the page is.";
            // $more_details = 1;
        }
        elseif($err == 'tu')
        {
            $show_err = "You have <b>".$errval['dp']." %</b> of duplicate content on <a href='javascript:void(0);' id='".$link_id."'><b>".$errval['tu']."</b></a> page(s) of your website.";
            $show_err_more = "Duplicate content generally refers to substantive blocks of content within or across domains that either completely match other content or are appreciably similar.";
            // $more_details = 1;
        }
        elseif($err == 'thn')
        {
            $show_err = "Your URL <i>'".$errval['u']."'</i> has <b>'".$errval['epage']['wrd']."'</b> words and is categorised as having thin content.";
            $show_err_more = "Thin content is a webpage with little or no original content. According to Google Guidelines, a web page has thin content if it has less than 200 words. One of the most important steps in improving your site's ranking in Google search results is to ensure that it contains plenty of rich information that includes relevant keywords, used appropriately, that  indicate the subject matter of your content.";
        }
        return array($show_err, $show_err_more, $more_details);
    }

    function conDist($my_dist_host, $my_dist_user, $my_dist_pass, $my_dist_db, $try = '')
    {
        if (empty($try)) {
            $try = 0;
        }

        $con_dist = new mysqli($my_dist_host, $my_dist_user, $my_dist_pass, $my_dist_db);
        if (mysqli_connect_error()) {
            if ($try <= 2) {
                $try += 1;
                // logError(mysqli_connect_error());
                conDist($try);
            }
        }

        return $con_dist;
    }


function getReducesData($totalScraped, $tCountNon200, $tCountNon200_ni, $tCountFailed, $total_pages)
{
    $auditor_count = 0;
    $non200_count = 0;
    $nonindex_count = 0;
    $failed_count = 0;
    $pages_found = 0;
    while($pages_found <= $total_pages) {

        if($totalScraped > 0) {
            $pages_found++;
            $auditor_count++;

            $totalScraped--;
            // $total_pages--;
        }

        if($tCountNon200 > 0) {
            $pages_found++;
            $non200_count++;

            $tCountNon200--;
            // $total_pages--;
        }

        if($tCountNon200_ni > 0) {
            $pages_found++;
            $nonindex_count++;

            $tCountNon200_ni--;
            // $total_pages--;
        }

        if($tCountFailed > 0) {
            $pages_found++;
            $failed_count++;

            $tCountFailed--;
            // $total_pages--;
        }
    }
    return array("unique"=>$auditor_count,"non200"=>$non200_count,"nonindex"=>$nonindex_count,"failed"=>$failed_count);
}


function getProjData($my_dist_host, $my_dist_user, $my_dist_pass, $my_dist_db)
{
    $dist_db = conDist($my_dist_host, $my_dist_user, $my_dist_pass, $my_dist_db);

    $qry = "SELECT * FROM projects WHERE id=".P_ID;
    if($chk = mysqli_query($dist_db, $qry)) {
        $res = mysqli_fetch_assoc($chk);
        return $res;
    }
}


function getOrigProjData($my_dist_host, $my_dist_user, $my_dist_pass, $my_dist_db)
{
    $dist_db = conDist($my_dist_host, $my_dist_user, $my_dist_pass, $my_dist_db);

    $qry = "SELECT * FROM projects WHERE id=".ORIG_PID;
    if($chk = mysqli_query($dist_db, $qry)) {
        $res = mysqli_fetch_assoc($chk);
        return $res;
    }
}





    // define('T_ERR_MAJOR', ",
    //     eti,
    //     eim,
    //     mal,
    //     lkw,
    //     tu,
    //     thn,
    //     dti,
    //     dde,
    //     dkw,
    //     lt,
    //     lu,
    //     mh1,
    //     eh1,
    //     eh2,
    //     can,
    //     t,
    // ");
    // define('T_ERR_MEDIUM', ",
    //     mds,
    //     low,
    //     lds,
    //     sds,
    //     mrf,
    //     mre,
    //     mnf,
    //     cms,
    //     dms,
    //     upu,
    //     mh2,
    //     unu,
    //     frm,
    //     fls,
    //     ent,
    //     eex,
    //     nfc,
    //     esp,
    //     big,
    // ");
    // define('T_ERR_MINOR', ",
    //     sti,
    //     skw,
    //     mkw,
    //     mlg,
    //     mst,
    //     spu,
    // ");
    // define('EIM_WEIGHT', 10);
    // define('MAL_WEIGHT', 10);
    // define('MKW_WEIGHT', 10);
    // define('LKW_WEIGHT', 10);
    // define('SKW_WEIGHT', 10);
    // define('MDS_WEIGHT', 10);
    // define('LDS_WEIGHT', 10);
    // define('SDS_WEIGHT', 10);
 //    define('NIN_WEIGHT', 10);
    // define('TU_WEIGHT', 10);
    // define('THN_WEIGHT', 10);
    // define('LOW_WEIGHT', 10);
    // define('LT_WEIGHT', 10);
    // define('STI_WEIGHT', 10);
    // define('ETI_WEIGHT', 10);
    // define('MRF_WEIGHT', 10);
    // define('MRE_WEIGHT', 10);
    // define('MNF_WEIGHT', 10);
    // define('CMS_WEIGHT', 10);
    // define('DMS_WEIGHT', 10);
    // define('MLG_WEIGHT', 10);
    // define('CAN_WEIGHT', 10);
    // define('MST_WEIGHT', 10);
    // define('MH1_WEIGHT', 10);
    // define('EH1_WEIGHT', 10);
    // define('MH2_WEIGHT', 10);
    // define('EH2_WEIGHT', 10);
    // define('LU_WEIGHT', 10);
    // define('SPU_WEIGHT', 10);
    // define('UNU_WEIGHT', 10);
 //    define('UPU_WEIGHT', 10);
 //    define('DTI_WEIGHT', 10);
 //    define('DKW_WEIGHT', 10);
 //    define('DDE_WEIGHT', 10);
 //    define('FLS_WEIGHT', 3);
 //    define('FRM_WEIGHT', 3);
 //    define('ENT_WEIGHT', 3);
 //    define('EEX_WEIGHT', 3);
 //    define('T_WEIGHT', 10);
 //    define('NFC_WEIGHT', 3);
 //    define('ESP_WEIGHT', 3);
    // define('BIG_WEIGHT', 3);
