customTest.php 4.47 KB
Newer Older
Игорь's avatar
init    
Игорь committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?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');
        }
    }
}