<?php
class customTest
{
    private static
        $test = null,
        $seo = false,
        $bad_links = array(),
        $used_links = array(),
        $check_count = 0,
        $routes = array();

    private function checkLink($href)
    {
        $href = trim($href);
        if($href != '' && !preg_match("/^(tel|mailto):/i", $href) && !preg_match("/^http(s)?:/i", $href) && mb_substr($href, 0, 2) !== '//' && !in_array($href, self::$used_links) && !in_array($href, self::$routes)){
            $ch = curl_init(sfConfig::get('app_host') . $href);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            $r = curl_exec($ch);
            $info = curl_getinfo($ch);
            curl_close($ch);

            self::$used_links[] = $href;
            self::$check_count++;

            if($info['http_code'] === 200){
                self::$test->log("  -" . $href . " (" . $info['http_code'] . ")");

                /*
                if($this->seo){
                    $find_html = str_get_html($r);
                    if(true || $find_html->find('H1', 0)->plaintext !== 'h'){
                        $this->logSection("  *h1*" . $url, $find_html->find('title', 0)->plaintext, null, 'ERROR');
                    }
                }
                */
            }else{
                self::$test->logSection("  -" . $href, "(" . $info['http_code'] . ")", null, 'ERROR');
                self::$bad_links[] = $href . " (" . $info['http_code'] . ")";
            }
        }

    }

    public function execute($test)
    {
        require_once sfConfig::get('sf_lib_dir') . '/Classes/simple_html_dom.php';

        self::$test = $test;

        $filename = sfConfig::get('sf_apps_dir') . '/www/config/routing.yml';
        if (file_exists($filename)) {
            $parser = new sfYamlParser($filename);
            $values = $parser->parse(file_get_contents($filename));

            foreach ($values as $name => $value){
                self::$routes[] = $value['url'];
            }

            foreach ($values as $name => $value){
                $url = $value['url'];

                if(preg_match("/:.*[^\/]/", $url)){
                    continue;
                }

                $ch = curl_init(sfConfig::get('app_host') . $url);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
                $r = curl_exec($ch);
                $info = curl_getinfo($ch);
                if($info['http_code'] === 200){
                    $test->log("\n-" . $name . " (" . $info['http_code'] . ")");
                }else{
                    $test->logSection("  -" . $name, "(" . $info['http_code'] . ")", null, 'ERROR');
                }
                curl_close($ch);

                self::$used_links[] = $url;
                self::$check_count++;

                if($info['http_code'] === 200){
                    if ($name === 'sitemap') {
                        $sitemap = simplexml_load_string($r, 'SimpleXMLIterator');
                        if ($sitemap !== false && is_object($sitemap) && count($sitemap) > 0) {
                            foreach ($sitemap as $sitemap_url) {
                                $loc = str_replace(sfConfig::get('app_host'), '', (string)$sitemap_url->loc);
                                self::checkLink($loc);
                            }
                        }
                        continue;
                    }

                    $html = str_get_html($r);
                    if($html !== false){

                        /*
                        if($seo){
                            if(true || $html->find('H1', 0)->plaintext !== 'h'){
                                $this->logSection("  *h1*" . $url, $html->find('title', 0)->plaintext, null, 'ERROR');
                            }
                        }
                        */

                        if($html->find('a')){
                            foreach ($html->find('a') as $a){
                                self::checkLink($a->href);
                            }
                        }
                    }
                }else{
                    self::$bad_links[] = $url . " (" . $info['http_code'] . ")";
                }
            }
        }
        $test->logSection("CHECK", self::$check_count, null);
        if(count(self::$bad_links) > 0){
            $test->logSection('BAD LINKS', count(self::$bad_links), null, 'ERROR');
        }
    }
}