-
Notifications
You must be signed in to change notification settings - Fork 19
/
xmlsitemap.class.php
837 lines (721 loc) · 29.2 KB
/
xmlsitemap.class.php
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
<?php
/* Reminder: always indent with 4 spaces (no tabs). */
// +---------------------------------------------------------------------------+
// | Geeklog 2.1 |
// +---------------------------------------------------------------------------+
// | xmlsitemap.class.php |
// | |
// | Google Sitemap Generator class |
// +---------------------------------------------------------------------------+
// | Copyright (C) 2009-2015 by the following authors: |
// | |
// | Authors: Kenji ITO - geeklog AT mystral-kk DOT net |
// | Dirk Haun - dirk AT haun-online DOT de |
// +---------------------------------------------------------------------------+
// | |
// | This program is free software; you can redistribute it and/or |
// | modify it under the terms of the GNU General Public License |
// | as published by the Free Software Foundation; either version 2 |
// | of the License, or (at your option) any later version. |
// | |
// | This program is distributed in the hope that it will be useful, |
// | but WITHOUT ANY WARRANTY; without even the implied warranty of |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
// | GNU General Public License for more details. |
// | |
// | You should have received a copy of the GNU General Public License |
// | along with this program; if not, write to the Free Software Foundation, |
// | Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
// | |
// +---------------------------------------------------------------------------+
/**
* Google Sitemap Generator classes
*
* @package XMLSitemap
*/
if (stripos($_SERVER['PHP_SELF'], basename(__FILE__)) !== false) {
die('This file cannot be used on its own.');
}
/**
* This is the built-in Geeklog class for creating an XML sitemap.
*
* USAGE:
*
* <code>
* $sitemap = new SitemapXML();
* $sitemap->setFileNames('path_to_sitemap_file',
* 'path_to_mobile_sitemap_file');
* // $sitemap->setTypes(array('article', 'staticpages'));
* // $sitemap->setPriority('article', 0.6);
* // $sitemap->setPriority('staticpages', 0.4);
* // $sitemap->setChangeFreq('article', 'weekly');
* // $sitemap->setChangeFreq('staticpages', 'monthly');
* $sitemap->create();
* </code>
*
*/
class SitemapXML
{
// Constants
const MAX_NUM_ENTRIES = 50000;
const MAX_FILE_SIZE = 10485760; // 1MB
const PING_INTERVAL = 3600; // 1 hour
const LB = "\n";
/**
* @var
*/
private $encoding;
private $num_entries;
private $changeFreqs;
private $priorities;
private $types;
private $filename;
private $mobile_filename;
private $news_filename;
private $news_topics;
private $news_age;
// Valid expressions for 'changefreq' field
private $valid_change_freqs = array('always', 'hourly', 'daily', 'weekly',
'monthly', 'yearly', 'never');
/**
* Constructor
*
* @param string $encoding the encoding of contents
*/
public function __construct($encoding = '')
{
$this->setEncoding($encoding);
$this->num_entries = 0;
$this->changeFreqs = array();
$this->priorities = array();
// Set only 'article' as default value
$this->setTypes(array('article'));
}
/**
* Set the encoding of contents
*
* @param string $encoding the encoding of contents
* @return void
*/
public function setEncoding($encoding)
{
if ($encoding == '') {
$encoding = COM_getCharset();
}
if ($encoding == '') { // This is very unlikely
$encoding = 'iso-8859-1';
}
$this->encoding = $encoding;
}
/**
* Set the name(s) of the sitemap file (and optionally the mobile
* sitemap file)
*
* NOTE: Sitemap files must be located in the top directory of the site,
* i.e., the same directory as "lib-common.php".
*
* @param string $filename name of sitemap file
* @param string $mobile_filename name of mobile sitemap file
* @param string $news_filename name of news sitemap file
*/
public function setFileNames($filename = '', $mobile_filename = '', $news_filename = '')
{
global $_CONF;
if ($filename != '') {
$this->filename = $_CONF['path_html'] . basename($filename);
}
if ($mobile_filename != '') {
$this->mobile_filename = $_CONF['path_html'] . basename($mobile_filename);
}
if ($news_filename != '') {
$this->news_filename = $_CONF['path_html'] . basename($news_filename);
}
}
/**
* Return the names of sitemap files
*
* @return array names of the sitemap, mobile sitemap, and news sitemap
*/
public function getFileNames()
{
return array($this->filename, $this->mobile_filename, $this->news_filename);
}
/**
* Check if a string stands for a valid value of priority
*
* @param string $str a string for a priority
* @return float a valid value or 0.5 (default value)
*/
public function checkPriority($str)
{
$v = (float) $str;
return (($v >= 0.0) && ($v <= 1.0)) ? $v : 0.5;
}
/**
* Set the priority of the item
*
* @param string $type 'article', 'staticpages', ...
* @param float $value the value of priority
*/
public function setPriority($type, $value)
{
$value = $this->checkPriority($value);
if ($value != 0.5) {
$this->priorities[$type] = $value;
}
}
/**
* Return the value of priority
*
* @param string $type 'article', 'staticpages', ...
* @return float 0.0..1.0 (default value is 0.5)
*/
public function getPriority($type)
{
if (isset($this->priorities[$type])) {
return (float) $this->priorities[$type];
} else {
return 0.5;
}
}
/**
* Check if a string stands for a proper frequency
*
* @param string $str a string for a frequency
* @return string a valid string or an empty string
*/
public function checkChangeFreq($str)
{
$str = strtolower($str);
return in_array($str, $this->valid_change_freqs) ? $str : '';
}
/**
* Set the change frequency of the item
*
* @param string $type 'article', 'staticpages', ...
* @param string $value any of 'always', 'hourly', 'daily', 'weekly',
* 'monthly', 'yearly', 'never'
*/
public function setChangeFreq($type, $value)
{
$value = $this->checkChangeFreq($value);
if ($value != '') {
$this->changeFreqs[$type] = $value;
}
}
/**
* Return the value of change frequency
*
* @param string $type 'article', 'staticpages', ...
* @return string any of 'always', 'hourly', 'daily', 'weekly',
* 'monthly', 'yearly', 'never', ''
*/
public function getChangeFreq($type)
{
return isset($this->changeFreqs[$type]) ? $this->changeFreqs[$type] : '';
}
/**
* Set the types of content
*
* NOTE: $types parameter is not checked to handle a case where
* a plugins is being enabled/disabled, i.e., when you can't
* depend on $_PLUGINS.
*
* @param mixed $types (string or array of string): 'article', ...
*/
public function setTypes($types)
{
$this->types = array_unique($types);
}
/**
* Get the types of content
*
* @return array array of strings of types: 'article', 'staticpages', ...
*/
public function getTypes()
{
return $this->types;
}
/**
* Set the topics for news
*
* @param mixed $topics (string or array of string): 'topicid1', ...
*/
public function setNewsTopics($topics)
{
$this->_news_topics = array_unique($topics);
}
/**
* Get the topics for news
*
* @return array array of strings of topics: 'topicid1', 'topicid2', ...
*/
public function getNewsTopics()
{
return $this->_news_topics;
}
/**
* Set the max age for news
*
* @param int max age of news articles in seconds
*/
public function setNewsAge($MaxAge)
{
$this->_news_age = intval($MaxAge);
}
/**
* Get the max age for news
*
* @return int max age of news articles in seconds
*/
public function getNewsAge()
{
return $this->_news_age;
}
/**
* Normalize a URL
*
* @param string $url URL to normalize
* @return string a normalized URL
*/
private function normalizeURL($url)
{
static $needConversion = null;
if ($needConversion === null) {
$needConversion = (strcasecmp($this->encoding, 'utf-8') !== 0);
}
if ($needConversion) {
$url = $this->toUtf8($url);
}
$url = str_replace(
array('<', '>', '&', '"', '''),
array( '<', '>', '&', '"', "'"),
$url
);
return htmlspecialchars($url, ENT_QUOTES, 'utf-8');
}
/**
* Return a string expression of the server time zone
*
* @return mixed '(+|-)\d\d:\d\d' or false
* @see PEAR Date/TimeZone.php
*/
private function getTimezoneStr()
{
global $_CONF;
static $retval = null;
if ($retval === null) {
if (isset($_CONF['timezone'])) {
$timezone = $_CONF['timezone'];
// Load Date_TimeZone class
Date_TimeZone::getDefault();
if (array_key_exists($timezone, $GLOBALS['_DATE_TIMEZONE_DATA'])) {
$offset = $GLOBALS['_DATE_TIMEZONE_DATA'][$timezone]['offset'];
if ($offset >= 0) {
$retval = '+';
} else {
$retval = '-';
$offset = - $offset;
}
$hour = floor($offset / 3600000);
$min = ($offset - 3600000 * $hour) % 60000;
$retval .= sprintf('%02d:%02d', $hour, $min);
} else {
COM_errorLog(__METHOD__ . ': $_CONF[\'timezone\'] is wrong: ' . $_CONF['timezone']);
$retval = false;
}
} else {
$retval = false;
}
}
return $retval;
}
/**
* Convert the encoding of a string to utf-8
*
* @param string $str
* @return string
*/
private function toUtf8($str)
{
if (is_callable('mb_convert_encoding')) {
$str = mb_convert_encoding($str, 'utf-8', $this->encoding);
} else if (is_callable('iconv')) {
$str = iconv($this->encoding, 'utf-8', $str);
} else if (is_callable('utf8_encode')) {
$str = utf8_encode($str);
} else {
COM_errorLog(__METHOD__ . ': No way to convert encoding to utf-8.');
}
return $str;
}
/**
* Write a sitemap into a file
*
* @param string $filename the name of the sitemap file
* @param string $sitemap the content of the sitemap
* @return boolean true = success, false = otherwise
*/
protected function write($filename, $sitemap)
{
$retval = false;
if (!@touch($filename)) {
COM_errorLog(__METHOD__ . ': Cannot write into the sitemap file: ' . $filename);
return $retval;
}
// If file name is '*.gz', then we use Zlib functions
$parts = pathinfo($filename);
if (isset($parts['extension']) &&
(strtolower($parts['extension']) === 'gz') &&
is_callable('gzopen')) {
// Save as a gzipped file
$gp = @gzopen($filename, 'r+b');
if ($gp === false) {
COM_errorLog(__METHOD__ . ': Cannot create the sitemap file: ' . $filename);
} else {
if (flock($gp, LOCK_EX)) {
ftruncate($gp, 0);
gzrewind($gp);
gzwrite($gp, $sitemap);
flock($gp, LOCK_UN);
gzclose($gp);
$retval = true;
} else {
COM_errorLog(__METHOD__ . ': Cannot lock the sitemap file for writing: ' . $filename);
}
}
} else {
$fp = @fopen($filename, 'r+b');
if ($fp === false) {
COM_errorLog(__METHOD__ . ': Cannot create the sitemap file: ' . $filename);
} else {
if (flock($fp, LOCK_EX)) {
ftruncate($fp, 0);
rewind($fp);
fwrite($fp, $sitemap);
fflush($fp);
flock($fp, LOCK_UN);
fclose($fp);
$retval = true;
} else {
COM_errorLog(__METHOD__ . ': Cannot lock the sitemap file for writing: ' . $filename);
}
}
}
return $retval;
}
/**
* Create the sitemap and save it as a file
*
* @return boolean true = success, false = otherwise
*/
public function create()
{
global $_XMLSMAP_CONF, $_CONF, $LANG_ISO639_1;
// Get file names
list ($filename, $mobile_filename, $news_filename) = $this->getFileNames();
if (!empty($filename) || !empty($mobile_filename)) {
$this->num_entries = 0;
$sitemap = '';
$types = $this->getTypes();
$what = 'url,date-modified';
$uid = 1; // anonymous user
$limit = 0; // the max number of items to be returned (0 = no limit)
$options = array();
if (count($types) === 0) {
COM_errorLog(__METHOD__ . ': No content type is specified.');
return false;
}
foreach ($types as $type) {
$result = array();
if (is_callable('PLG_collectSitemapItems')) { // New API since GL-2.1.1
$result = PLG_collectSitemapItems($type, $uid, $limit);
}
if (!is_array($result) || (count($result) === 0)) {
$result = PLG_getItemInfo($type, '*', $what, $uid, $options);
}
if (is_array($result) && (count($result) > 0)) {
foreach ($result as $entry) {
if (isset($entry['url'])) {
$url = $this->normalizeURL($entry['url']);
$sitemap .= ' <url>' . self::LB
. ' <loc>' . $url . '</loc>' . self::LB;
} else {
/**
* <loc> element is mandatory for the sitemap. So,
* when no url is provided, we simply have to skip
* the item silently.
*/
continue;
}
// The items below are all optional.
// Frequency of change
$change_freq = isset($entry['change-freq'])
? $entry['change-freq']
: $this->getChangeFreq($type);
if ($change_freq != '') {
$sitemap .= ' <changefreq>' . $change_freq
. '</changefreq>' . self::LB;
}
// Time stamp
if (isset($entry['date-modified'])) {
$date = date('Y-m-d', $entry['date-modified']);
// Add the time part for frequently changed items
if (in_array($change_freq, array('always', 'hourly', 'daily'))) {
$timezone = $this->getTimezoneStr();
if ($timezone !== false) {
$date .= 'T' . date('H:i:s', $entry['date-modified'])
. $timezone;
}
}
if (in_array($type, $_XMLSMAP_CONF['lastmod'])) {
$sitemap .= ' <lastmod>' . $date . '</lastmod>' . self::LB;
}
}
// Priority
$priority = isset($entry['priority'])
? $entry['priority']
: $this->getPriority($type);
if ($priority != 0.5) {
$sitemap .= ' <priority>' . (string) $priority
. '</priority>' . self::LB;
}
$sitemap .= ' </url>' . self::LB;
$this->num_entries++;
}
}
}
// Write the sitemap into file(s)
// Append the header and footer to the sitemap body
if ($sitemap != '') {
$sitemap = '<?xml version="1.0" encoding="UTF-8" ?>' . self::LB
. '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . self::LB
. $sitemap
. '</urlset>' . self::LB;
// Check the number of items and the size of the sitemap file
if ($this->num_entries > self::MAX_NUM_ENTRIES) {
COM_errorLog(__METHOD__ . ': The number of items in the sitemap file must be ' . self::MAX_NUM_ENTRIES . ' or smaller.');
return false;
} else if (strlen($sitemap) > self::MAX_FILE_SIZE) {
COM_errorLog(__METHOD__ . ': The size of the sitemap file must be ' . self::MAX_FILE_SIZE . ' bytes or smaller.');
return false;
}
if ($filename != '') {
if (!$this->write($filename, $sitemap)) {
return false;
}
}
if ($mobile_filename != '') {
// Modify the sitemap as Google Mobile Sitemap
$sitemap = str_replace(
'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">',
'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:mobile="http://www.google.com/schemas/sitemap-mobile/1.0">',
$sitemap
);
$sitemap = str_replace(
' </url>',
' <mobile:mobile />' . self::LB . ' </url>',
$sitemap
);
if (!$this->write($mobile_filename, $sitemap)) {
return false;
}
}
}
}
// Get News Sitemap
if ($news_filename != '') {
$this->num_entries = 0;
$sitemap = '';
$what = 'url,date-created,id,title';
$uid = 1; // anonymous user
$limit = 0; // the max number of items to be returned (0 = no limit)
$options = array();
// Figure out language id
$multi_lang = COM_isMultiLanguageEnabled();
if ($multi_lang) {
// default language for multi language site
$site_lang_id = COM_getLanguageId();
} else {
// Just one default language
$site_lang_id = $LANG_ISO639_1;
}
// See if timezone is set
$timezone = $this->getTimezoneStr();
// Retrieve complete topic list including inherited ones
$topic_list = '';
$newstopics = $this->getNewsTopics();
if (!empty($newstopics)) {
foreach ($newstopics as $tid) {
$tids = TOPIC_getChildList($tid, $uid);
if (!empty($tids)) {
if (!empty($topic_list)) {
$topic_list = $topic_list . "," . $tids;
} else {
$topic_list = $tids;
}
}
}
}
if (!empty($topic_list)) {
$options['filter']['topic-ids'] = $topic_list;
}
// Figure out max age
if ($this->getNewsAge() > 0) {
$options['filter']['date-created'] = strtotime("-" . $this->getNewsAge() . " seconds");
}
$result = PLG_getItemInfo('article', '*', $what, $uid, $options);
if (is_array($result) && (count($result) > 0)) {
foreach ($result as $entry) {
// Check URL ,date is under max age, and appropriate topics
if (isset($entry['url']) && (true)) {
$url = $this->normalizeURL($entry['url']);
$sitemap .= ' <url>' . self::LB
. ' <loc>' . $url . '</loc>' . self::LB;
} else {
/**
* <loc> element is mandatory for the sitemap. So,
* when no url is provided, we simply have to skip
* the item silently.
*/
continue;
}
// Start News Specific tags
$sitemap .= ' <news:news>' . self::LB;
// Publication
$sitemap .= ' <news:publication>' . self::LB;
$sitemap .= ' <news:name>' . $_CONF['site_name'] . '</news:name>' . self::LB;
// Language
if ($multi_lang) {
$lang_id = COM_getLanguageIdForObject($entry['id']);
if (empty($lang_id)) {
// if no lang id then assume site default lang
$lang_id = $site_lang_id;
}
} else {
$lang_id = $site_lang_id;
}
$sitemap .= ' <news:language>' . $lang_id . '</news:language>' . self::LB;
$sitemap .= ' </news:publication>' . self::LB;
// Time stamp
$date = date('Y-m-d', $entry['date-created']);
if ($timezone !== false) {
$date .= 'T' . date('H:i:s', $entry['date-created']) . $timezone;
}
$sitemap .= ' <news:publication_date>' . $date . '</news:publication_date>' . self::LB;
// Title
$sitemap .= ' <news:title>' . $entry['title'] . '</news:title>' . self::LB;
$sitemap .= ' </news:news>';
$sitemap .= ' </url>' . self::LB;
$this->num_entries++;
}
}
// Append the header and footer to the sitemap body even if sitemap contains no info
$sitemap = '<?xml version="1.0" encoding="UTF-8" ?>' . self::LB
. '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:news="http://www.google.com/schemas/sitemap-news/0.9">' . self::LB
. $sitemap
. '</urlset>' . self::LB;
// Check the number of items and the size of the sitemap file
if ($this->num_entries > self::MAX_NUM_ENTRIES) {
COM_errorLog(__METHOD__ . ': The number of items in the sitemap file must be ' . self::MAX_NUM_ENTRIES . ' or smaller.');
return false;
} else if (strlen($sitemap) > self::MAX_FILE_SIZE) {
COM_errorLog(__METHOD__ . ': The size of the sitemap file must be ' . self::MAX_FILE_SIZE . ' bytes or smaller.');
return false;
}
if (!$this->write($news_filename, $sitemap)) {
return false;
}
}
return true;
}
/**
* Sends a ping to search engines for the main sitemap only
*
* @param array $destinations an array of search engine types.
* Currently supported are 'google' and 'bing'.
* @return int the number of successful pings
*/
public function sendPing(array $destinations)
{
global $_CONF, $_TABLES;
// Checks if arguments are good
$destinations = array_unique($destinations);
list ($sitemap, ) = $this->getFileNames();
if ($sitemap == '') {
COM_errorLog(__METHOD__ . ': sitemap file name is not specified.');
return 0;
} else if (count($destinations) === 0) {
COM_errorLog(__METHOD__ . ': target URL is not specified.');
return 0;
}
// Checks for the record of previous pings
$hasRecord = false;
$sql = "SELECT value FROM {$_TABLES['vars']} WHERE (name = 'xmlsitemap.pings') ";
$result = DB_query($sql);
if (($result !== false) && (DB_numRows($result) == 1)) {
$hasRecord = true;
list ($A) = DB_fetchArray($result);
$records = json_decode($A, true);
} else {
$records = array();
}
$success = 0;
$sitemapUrl = $_CONF['site_url'] . '/' . basename($sitemap);
$sitemapUrl = urlencode($sitemapUrl);
foreach ($destinations as $dest) {
$dest = strtolower($dest);
// Checks if there was a record of a previous ping
if (isset($records[$dest]) &&
($records[$dest] + self::PING_INTERVAL > time())) {
continue;
}
switch ($dest) {
case 'google':
$url = 'http://www.google.com/webmasters/tools/ping?sitemap=' . $sitemapUrl;
break;
case 'bing':
$url = 'http://www.bing.com/ping?sitemap=' . $sitemapUrl;
break;
default:
$url = '';
COM_errorLog(__METHOD__ . ': unknown target "' . $dest . '"is specified.');
break;
}
// Sends a ping to the endpoint of a search engine
if ($url !== '') {
$req = new HTTP_Request2(
$url,
HTTP_Request2::METHOD_GET
);
$req->setHeader('User-Agent', 'Geeklog/' . VERSION);
try {
$response = $req->send();
$status = $response->getStatus();
if ($status == 200) {
$success++;
$records[$dest] = time();
} else {
COM_errorLog(__METHOD__ . ': HTTP status ' . $status);
}
} catch (HTTP_Request2_Exception $e) {
COM_errorLog(__METHOD__ . ': ' . $e->getMessage());
}
}
}
// Writes back a record of pings into database
$records = json_encode($records);
$records = DB_escapeString($records);
if ($hasRecord) {
$sql = "UPDATE {$_TABLES['vars']} SET value = '{$records}' "
. "WHERE (name = 'xmlsitemap.pings') ";
} else {
$sql = "INSERT INTO {$_TABLES['vars']} (name, value) "
. "VALUES ('xmlsitemap.pings', '{$records}') ";
}
if (DB_query($sql) === false) {
COM_errorLog(__METHOD__ . ': cannot save ping records into database');
}
return (count($destinations) === $success);
}
}