Passed
Push — development ( 5c6121...81d1fa )
by Thomas
02:06
created

SiteMaps::run()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 16
nc 3
nop 0
dl 0
loc 26
rs 8.5806
c 0
b 0
f 0
1
<?php
2
/***************************************************************************
3
 *  For license information see doc/license.txt
4
 *
5
 *
6
 *  Generate sitemap.xml as specified by http://www.sitemaps.org
7
 *  And send ping to search engines
8
 ***************************************************************************/
9
10
checkJob(new SiteMaps());
11
12
class SiteMaps
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
13
{
14
    public $name = 'sitemaps';
15
    public $interval = 604800; // once a week
16
17
    /**
18
     * @var sitemapxml
19
     */
20
    public $oSiteMapXml;
21
22
    public function run()
23
    {
24
        global $opt;
25
        if ($opt['cron']['sitemaps']['generate'] === true) {
26
            $this->oSiteMapXml = new SiteMapXml();
27
28
            $page = $opt['page'];
29
30
            $this->oSiteMapXml->open(
31
                $opt['rootpath'],
32
                $page['https']['mode'] == HTTPS_ENFORCED ? $page['absolute_https_url'] : $page['absolute_http_url']
33
            );
34
35
            $this->oSiteMapXml->write('index.php', time(), 'always', 1.0);
36
            $this->oSiteMapXml->write('tops.php', time() - 24 * 60 * 60, 'daily', 0.5);
37
            $this->oSiteMapXml->write('newcachesrest.php', time() - 24 * 60 * 60, 'daily', 0.5);
38
            $this->writeViewGeocacheUrls();
39
            $this->writeNewGeocacheUrls();
40
41
            $this->oSiteMapXml->close();
42
43
            if ($opt['cron']['sitemaps']['submit'] == true) {
44
                $this->pingSearchEngines();
45
            }
46
        }
47
    }
48
49
    public function pingSearchEngines()
50
    {
51
        global $opt;
52
53
        $page = $opt['page'];
54
        $url = ($page['https']['mode'] == HTTPS_ENFORCED ? $page['absolute_https_url'] : $page['absolute_http_url']);
55
56
        $url = urlencode($url. 'sitemap.xml');
57
58
        $this->pingSearchEngine('http://www.google.com/webmasters/ping?sitemap=' . $url);
59
        $this->pingSearchEngine(
60
            'http://search.yahooapis.com/SiteExplorerService/V1/updateNotification?appid=USERID&url=' . $url
61
        );
62
        $this->pingSearchEngine('http://submissions.ask.com/ping?sitemap=' . $url);
63
        $this->pingSearchEngine('http://www.bing.com/webmaster/ping.aspx?siteMap=' . $url);
64
    }
65
66
    /**
67
     * @param string $url
68
     * @return bool
69
     */
70
    public function pingSearchEngine($url)
71
    {
72
        $curl = curl_init($url);
73
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
74
        curl_exec($curl);
75
76
        if (curl_errno($curl) != 0) {
77
            curl_close($curl);
78
79
            return false;
80
        }
81
82
        $respCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
83
        if ($respCode != 200) {
84
            curl_close($curl);
85
86
            return false;
87
        }
88
89
        curl_close($curl);
90
91
        return true;
92
    }
93
94
    public function writeNewGeocacheUrls()
95
    {
96
        $nCount = sql_value('SELECT COUNT(*) FROM `caches` WHERE `caches`.`status`=1', 0);
97
        $nIndex = 0;
98
        while ($nIndex < $nCount) {
99
            $this->oSiteMapXml->write('newcaches.php?startat=' . $nIndex, time(), 'always', 0.7);
100
            $nIndex += 100;
101
        }
102
    }
103
104
    public function writeArticleUrls()
105
    {
106
        $rs = sql("SELECT `href` FROM `sys_menu` WHERE `href` LIKE 'articles.php?page=%'");
107
        while ($r = sql_fetch_assoc($rs)) {
108
            $this->oSiteMapXml->write($r['href'], time() - 31 * 24 * 60 * 60, 0.3);
109
        }
110
        sql_free_result($rs);
111
    }
112
113
    public function writeViewGeocacheUrls()
114
    {
115
        $rs = sql(
116
            'SELECT SQL_BUFFER_RESULT `caches`.`wp_oc`, `caches`.`cache_id`, `cache_desc`.`language`
117
             FROM `caches`
118
             INNER JOIN `cache_desc` ON `caches`.`cache_id`=`cache_desc`.`cache_id`
119
             INNER JOIN `cache_status` ON `caches`.`status`=`cache_status`.`id`
120
             WHERE `cache_status`.`allow_user_view`=1'
121
        );
122
123
        while ($r = sql_fetch_assoc($rs)) {
124
            $dLastMod = sql_value(
125
                "SELECT MAX(`last_modified`) `last_modified` FROM
126
                (SELECT `listing_last_modified` AS `last_modified` FROM `caches` WHERE `cache_id` ='&1' UNION
127
                SELECT MAX(`last_modified`) AS `last_modified` FROM `cache_logs` WHERE `cache_id` ='&1') `tmp_result`",
128
                time(),
129
                $r['cache_id']
130
            );
131
            $this->oSiteMapXml->write(
132
                'viewcache.php?wp=' . $r['wp_oc'] . '&desclang=' . $r['language'],
133
                strtotime($dLastMod),
134
                0.6
135
            );
136
        }
137
        sql_free_result($rs);
138
    }
139
}
140