Completed
Push — development ( eb9524...db4517 )
by Andrij
28:49 queued 02:09
created

Sitemap::_get_lang_prefix()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 3
eloc 4
nc 3
nop 1
1
<?php
2
3
use CMSFactory\Events;
4
5
if (!defined('BASEPATH')) {
6
    exit('No direct script access allowed');
7
}
8
9
/**
10
 * Image CMS
11
 *
12
 * Sitemap Module
13
 * @property Sitemap_model $sitemap_model
14
 * @property Lib_category lib_category
15
 * @property smart_filter smart_filter
16
 */
17
class Sitemap extends MY_Controller
18
{
0 ignored issues
show
introduced by
Opening brace of a class must be on the same line as the definition
Loading history...
19
20
    /**
21
     * Blocked urls array
22
     * @var array
23
     */
24
    public $blocked_urls = [];
25
26
    /**
27
     * Frequency for brands pages
28
     * @var string
29
     */
30
    public $brands_changefreq = 'daily';
31
32
    /**
33
     * Priority for brands pages
34
     * @var int
35
     */
36
    public $brands_priority = '1';
37
38
    /**
39
     * Frequency for categories pages
40
     * @var string
41
     */
42
    public $categories_changefreq = 'daily'; // priority for subcategories pages
43
44
    /**
45
     * Priority for categories
46
     * @var int
47
     */
48
    public $cats_priority = '1';
49
50
    /**
51
     * Default frequency
52
     * @var string
53
     */
54
    public $changefreq = 'daily';
55
56
    /**
57
     * Default lang
58
     * @var array
59
     */
60
    public $default_lang = [];
61
62
    /**
63
     * Gzip level
64
     * @var int
65
     */
66
    public $gzip_level = 0;
67
68
    /**
69
     * Sitemap items
70
     * @var array
71
     */
72
    public $items = [];
73
74
    /**
75
     * Langs array
76
     * @var array
77
     */
78
    public $langs = [];
79
80
    /**
81
     * Frequency for main page
82
     * @var string
83
     */
84
    public $main_page_changefreq = 'daily';
85
86
    /**
87
     * Priority for main page
88
     * @var int
89
     */
90
    public $main_page_priority = '1';
91
92
    /**
93
     * Max url tag count
94
     * @var int
95
     */
96
    private $max_url_count = 30000;
97
98
    /**
99
     * Frequency for pages
100
     * @var string
101
     */
102
    public $pages_changefreq = 'daily';
103
104
    /**
105
     * Priority for pages
106
     * @var int
107
     */
108
    public $pages_priority = '1';
109
110
    /**
111
     * Frequency for products categories pages
112
     * @var string
113
     */
114
    public $products_categories_changefreq = 'daily';
115
116
    /**
117
     * Priority for products categories pages
118
     * @var int
119
     */
120
    public $products_categories_priority = '1';
121
122
    /**
123
     * Frequency for products pages
124
     * @var string
125
     */
126
    public $products_changefreq = 'daily';
127
128
    /**
129
     * Priority for products pages
130
     * @var int
131
     */
132
    public $products_priority = '1';
133
134
    /**
135
     * Frequency for products sub categiries pages
136
     * @var string
137
     */
138
    public $products_sub_categories_changefreq = 'daily';
139
140
    /**
141
     * Priority for products sub categories pages
142
     * @var int
143
     */
144
    public $products_sub_categories_priority = '1';
145
146
    /**
147
     * Sitemap result
148
     * @var string
149
     */
150
    public $result = '';
151
152
    /**
153
     * Path to folder where site_maps files exists
154
     * @var string
155
     */
156
    private $site_map_folder_path = './uploads/sitemaps';
157
158
    /**
159
     * Path to saved sitemap file
160
     * @var string
161
     */
162
    private $sitemap_path = './uploads/sitemaps/sitemap.xml';
163
164
    /**
165
     * Frequency for sub categories pages
166
     * @var string
167
     */
168
    public $sub_categories_changefreq = 'daily';
169
170
    /**
171
     * Priority for subcategories pages
172
     * @var int
173
     */
174
    public $sub_cats_priority = '1';
175
176
    /**
177
     * Sitemap constructor.
178
     */
179
    public function __construct() {
180
181
        parent::__construct();
182
        $lang = new MY_Lang();
183
        $lang->load('sitemap');
184
185
        $this->load->module('core');
186
        $this->load->model('sitemap_model');
187
        $this->langs = $this->core->langs;
188
        $this->default_lang = $this->core->def_lang[0];
189
        if (uri_string() == 'sitemap.xml') {
190
            $this->build_xml_map();
191
            exit();
192
        }
193
194
        if (uri_string() == 'sitemapRegenerate.xml') {
195
            $this->build_xml_map_regenerated();
196
            exit();
197
        }
198
    }
199
200
    /**
201
     * Create and display sitemap xml
202
     */
203
    public function build_xml_map($regenerate = FALSE) {
204
205
        $settings = $this->sitemap_model->load_settings();
206
207
        // Generate new or use saved map
208
        if ((int) $settings['generateXML'] || $regenerate) {
209
            $this->_create_map();
210
        } else {
211
            if (file_exists($this->sitemap_path)) {
212
                $this->result = file_get_contents($this->sitemap_path);
213
            } else {
214
                $this->_create_map();
215
            }
216
        }
217
218
        // Show Site Map
219
        if ($this->result) {
220
            header('content-type: text/xml');
221
            echo $this->result;
222
        }
223
    }
224
225
    /**
226
     * Create map
227
     */
228
    public function _create_map() {
229
230
        $this->initialize();
231
232
        // Add main page
233
        if (!$this->robotsCheck(site_url())) {
234
            $this->items[] = [
235
                'loc' => site_url(),
236
                'changefreq' => $this->main_page_changefreq,
237
                'priority' => $this->main_page_priority,
238
                'lastmod' => date('Y-m-d', time())
239
            ];
240
        }
241
        $this->getPagesCategories();
242
        $this->getAllPages();
243
244
        if (SHOP_INSTALLED) {
245
246
            $this->getShopCategories();
247
            $this->getShopBrands();
248
            $this->getShopProducts();
249
250
            $this->load->module('smart_filter');
251
            if (method_exists($this->smart_filter, 'attachPages')) {
252
                $this->smart_filter->attachPages($this);
253
            }
254
        }
255
        $this->result = $this->generate_xml($this->items);
256
        return $this->result;
257
    }
258
259
    /**
260
     * Initialize module settings
261
     */
262
    public function initialize() {
263
264
        // Get sitemap values
265
        $priorities = $this->sitemap_model->getPriorities();
266
        $changfreq = $this->sitemap_model->getChangefreq();
267
        $blocked_urls = $this->sitemap_model->getBlockedUrls();
268
269
        // Initialize priorities
270
        if ($priorities) {
271
            $this->main_page_priority = $priorities['main_page_priority'];
272
            $this->cats_priority = $priorities['cats_priority'];
273
            $this->pages_priority = $priorities['pages_priority'];
274
            $this->sub_cats_priority = $priorities['sub_cats_priority'];
275
            $this->products_priority = $priorities['products_priority'];
276
            $this->products_categories_priority = $priorities['products_categories_priority'];
277
            $this->products_sub_categories_priority = $priorities['products_sub_categories_priority'];
278
            $this->brands_priority = $priorities['brands_priority'];
279
        }
280
281
        // Initialize changfreq
282
        if ($changfreq) {
283
            $this->main_page_changefreq = $changfreq['main_page_changefreq'];
284
            $this->categories_changefreq = $changfreq['categories_changefreq'];
285
            $this->products_categories_changefreq = $changfreq['products_categories_changefreq'];
286
            $this->products_sub_categories_changefreq = $changfreq['products_sub_categories_changefreq'];
287
            $this->pages_changefreq = $changfreq['pages_changefreq'];
288
            $this->products_changefreq = $changfreq['product_changefreq'];
289
            $this->brands_changefreq = $changfreq['brands_changefreq'];
290
            $this->sub_categories_changefreq = $changfreq['sub_categories_changefreq'];
291
        }
292
293
        // Initialize Blocked urls
294
        if ($blocked_urls) {
295
            foreach ($blocked_urls as $url) {
296
                $this->blocked_urls[] = $url['url'];
297
            }
298
        }
299
300
        return $this;
301
    }
302
303
    /**
304
     * Check robots
305
     * @param string $check
306
     * @return boolean
307
     */
308
    public function robotsCheck($check) {
309
310
        $checkSetting = $this->db->get('settings')
311
            ->row()
312
            ->robots_status;
313
314
        if (!$checkSetting && $checkSetting == '0') {
315
            $array = ['/'];
316
        }
317
318
        foreach ($array as $ar) {
319
            if ($ar == '/') {
320
                return true;
321
            }
322
323
            if (strstr($ar, $check)) {
324
                return true;
325
            }
326
        }
327
        return false;
328
    }
329
330
    /**
331
     * Get pages categories
332
     */
333
    private function getPagesCategories() {
334
335
        // Add categories to sitemap urls.
336
        $categories = $this->lib_category->unsorted();
337
338
        foreach ($categories as $category) {
339
            if (!$this->robotsCheck(site_url($category['path_url']))) {
340
341
                if ((int) $category['parent_id']) {
342
                    $changefreq = $this->sub_categories_changefreq;
343
                    $priority = $this->sub_cats_priority;
344
                } else {
345
                    $changefreq = $this->categories_changefreq;
346
                    $priority = $this->cats_priority;
347
                }
348
349
                // create date
350
                if ($category['updated'] > 0) {
351
                    $date = date('Y-m-d', $category['updated']);
352
                } else {
353
                    $date = date('Y-m-d', $category['created']);
354
                }
355
356
                if ($this->not_blocked_url($category['path_url'])) {
357
358
                    $this->items[] = [
359
                        'loc' => site_url($category['path_url']),
360
                        'changefreq' => $changefreq,
361
                        'priority' => $priority,
362
                        'lastmod' => $date
363
                    ];
364
                }
365
366
                // Add links to categories in all langs.
367
                foreach ($this->langs as $lang_indentif => $lang) {
368
                    if ($lang['id'] != $this->default_lang['id']) {
369
                        $url = $lang_indentif . '/' . $category['path_url'];
370 View Code Duplication
                        if ($this->not_blocked_url($url)) {
371
                            $this->items[] = [
372
                                'loc' => site_url($url),
373
                                'changefreq' => $changefreq,
374
                                'priority' => $priority,
375
                                'lastmod' => $date
376
                            ];
377
                        }
378
                    }
379
                }
380
            }
381
        }
382
    }
383
384
    /**
385
     * Chech is url blocked
386
     * @param string $url
387
     * @return boolean
388
     */
389
    private function not_blocked_url($url) {
390
391
        if (!in_array($url, $this->blocked_urls) && !in_array(substr($url, 0, -1), $this->blocked_urls)) {
392
            foreach ($this->blocked_urls as $blocked_url) {
393
                $url = str_replace(site_url(), '', $url);
394
395
                if (mb_strpos($url, '/') === 0) {
396
                    $url = substr($url, 1);
397
                }
398
399
                if (mb_strrpos($url, '/') === (mb_strlen($url) - 1)) {
400
                    $url = substr($url, 0, -1);
401
                }
402
403
                $url_length = mb_strlen($blocked_url);
404
                $last_symbol = substr($blocked_url, $url_length - 1);
405
                $first_symbol = substr($blocked_url, 0, 1);
406
                $blocked_url_tpm = str_replace('*', '', $blocked_url);
407
408
                if ($last_symbol == '*') {
409
                    $url_position = mb_strpos($url, $blocked_url_tpm);
410
                    if ($url_position === 0) {
411
                        return FALSE;
412
                    }
413
                }
414
415
                if ($first_symbol == '*') {
416
                    $must_be_in_pos = (int) mb_strlen($url) - (int) mb_strlen($blocked_url_tpm);
417
                    $url_position = mb_strrpos($url, $blocked_url_tpm);
418
                    if ($must_be_in_pos == $url_position) {
419
                        return FALSE;
420
                    }
421
                }
422
            }
423
424
            return TRUE;
425
        } else {
426
            return FALSE;
427
        }
428
    }
429
430
    /**
431
     * Get all content pages
432
     */
433
    private function getAllPages() {
434
435
        // Get all pages
436
        $pages = $this->sitemap_model->get_all_pages();
437
438
        foreach ($pages as $page) {
439
440
            if (!$this->robotsCheck($page['full_url'])) {
441
442
                // create page url
443
                if ($page['lang'] == $this->default_lang['id']) {
444
                    $url = site_url($page['full_url']);
445
                    $url_page = $page['full_url'];
446
                } else {
447
                    $prefix = $this->_get_lang_prefix($page['lang']);
448
                    $url_page = $prefix . '/' . $page['full_url'];
449
                    $url = site_url($url_page);
450
                }
451
452
                // create date
453
                if ($page['updated'] > 0) {
454
                    $date = date('Y-m-d', $page['updated']);
455
                } else {
456
                    $date = date('Y-m-d', $page['created']);
457
                }
458
459
                if ($page['cat_url'] == '') {
460
                    $c_priority = $this->cats_priority;
461
                } else {
462
                    $c_priority = $this->pages_priority;
463
                }
464
465 View Code Duplication
                if ($this->not_blocked_url($url_page)) {
466
                    $this->items[] = [
467
                        'loc' => $url,
468
                        'changefreq' => $this->pages_changefreq,
469
                        'priority' => $c_priority,
470
                        'lastmod' => $date
471
                    ];
472
                }
473
            }
474
        }
475
    }
476
477
    /**
478
     * Get language prefix by lang id
479
     * @param int $id
480
     * @return int|string
481
     */
482
    private function _get_lang_prefix($id) {
483
484
        foreach ($this->langs as $k => $v) {
485
            if ($v['id'] === $id) {
486
                return $k;
487
            }
488
        }
489
    }
490
491
    /**
492
     * Get products categories
493
     */
494
    private function getShopCategories() {
495
496
        // Get Shop Categories
497
        $shop_categories = $this->sitemap_model->get_shop_categories();
498
499
        // Add categories to Site Map
500
        foreach ($shop_categories as $shopcat) {
501
502
            if ($this->sitemap_model->categoryIsActive($shopcat['id'])) {
503
504
                $localeSegment = $shopcat['locale'] == self::defaultLocale() ? '' : $shopcat['locale'] . '/';
505
506
                $url = $localeSegment . 'shop/category/' . $shopcat['full_path'];
507
                if ($this->not_blocked_url($url)) {
508
                    if (!$this->robotsCheck(site_url($url))) {
509
510
                        // Check if category is subcategory
511
                        if ((int) $shopcat['parent_id']) {
512
                            $changefreq = $this->products_sub_categories_changefreq;
513
                            $priority = $this->products_sub_categories_priority;
514
                        } else {
515
                            $changefreq = $this->products_categories_changefreq;
516
                            $priority = $this->products_categories_priority;
517
                        }
518
519
                        // create date
520
                        if ($shopcat['updated'] > 0) {
521
                            $date = date('Y-m-d', $shopcat['updated']);
522
                        } else {
523
                            $date = date('Y-m-d', $shopcat['created']);
524
                        }
525
526
                        $this->items[] = [
527
                            'loc' => site_url($url),
528
                            'changefreq' => $changefreq,
529
                            'priority' => $priority,
530
                            'lastmod' => $date,
531
                        ];
532
                    }
533
                }
534
            }
535
        }
536
    }
537
538
    /**
539
     * Get products brands
540
     */
541 View Code Duplication
    private function getShopBrands() {
542
543
        // Get Shop Brands
544
        $shop_brands = $this->sitemap_model->get_shop_brands();
545
546
        // Add Shop Brand to Site Map
547
        foreach ($shop_brands as $shopbr) {
548
549
            $localeSegment = $shopbr['locale'] == self::defaultLocale() ? '' : $shopbr['locale'] . '/';
550
551
            $url = site_url($localeSegment . 'shop/brand/' . $shopbr['url']);
552
            if ($this->not_blocked_url($localeSegment . 'shop/brand/' . $shopbr['url'])) {
553
                if (!$this->robotsCheck($url)) {
554
                    // create date
555
                    if ($shopbr['updated'] > 0) {
556
                        $date = date('Y-m-d', $shopbr['updated']);
557
                    } else {
558
                        $date = date('Y-m-d', $shopbr['created']);
559
                    }
560
                    $this->items[] = [
561
                        'loc' => $url,
562
                        'changefreq' => $this->brands_changefreq,
563
                        'priority' => $this->brands_priority,
564
                        'lastmod' => $date
565
                    ];
566
                }
567
            }
568
        }
569
    }
570
571
    /**
572
     * Get products
573
     */
574 View Code Duplication
    private function getShopProducts() {
575
576
        // Get Shop products
577
        $shop_products = $this->sitemap_model->get_shop_products();
578
579
        // Add Shop products to Site Map
580
        foreach ($shop_products as $shopprod) {
581
            if ($this->sitemap_model->categoryIsActive($shopprod['category_id'])) {
582
583
                $localeSegment = $shopprod['locale'] == self::defaultLocale() ? '' : $shopprod['locale'] . '/';
584
                $url = site_url($localeSegment . 'shop/product/' . $shopprod['url']);
585
                if ($this->not_blocked_url($localeSegment . 'shop/product/' . $shopprod['url'])) {
586
                    if (!$this->robotsCheck($url)) {
587
                        if ($shopprod['updated'] > 0) {
588
                            $date = date('Y-m-d', $shopprod['updated']);
589
                        } else {
590
                            $date = date('Y-m-d', $shopprod['created']);
591
                        }
592
                        $this->items[] = [
593
                            'loc' => $url,
594
                            'changefreq' => $this->products_changefreq,
595
                            'priority' => $this->products_priority,
596
                            'lastmod' => $date
597
                        ];
598
                    }
599
                }
600
            }
601
        }
602
    }
603
604
    /**
605
     * Generate xml
606
     * @param array $items
607
     * @return string
608
     */
609
    private function generate_xml(array $items = []) {
610
611
        $data = '';
612
613
        $site_maps = [];
614
        $url_count = 0;
615
616
        while ($item = current($items)) {
617
            if ($url_count < $this->max_url_count) {
618
                $data .= "<url>\n";
619
                foreach ($item as $k => $v) {
620
                    if ($v != '') {
621
                        $data .= "\t<$k>" . htmlspecialchars($v) . "</$k>\n";
622
                    }
623
                }
624
                $data .= "</url>\n";
625
626
                next($items);
627
            } else {
628
                $site_maps[] = "<\x3Fxml version=\"1.0\" encoding=\"UTF-8\"\x3F>\n<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">\n" . $data . "\t</urlset>";
629
                $url_count = 0;
630
                $data = '';
631
            }
632
            $url_count++;
633
        }
634
635
        if ($data && $site_maps) {
636
            $site_maps[] = "<\x3Fxml version=\"1.0\" encoding=\"UTF-8\"\x3F>\n<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">\n" . $data . "\t</urlset>";
637
        }
638
639
        if ($site_maps) {
640
            $this->saveSiteMaps($site_maps);
641
            $result = $this->createMainSitemap($site_maps);
642
        } else {
643
            $result = "<\x3Fxml version=\"1.0\" encoding=\"UTF-8\"\x3F>\n<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">\n" . $data . "\t</urlset>";
644
        }
645
646
        return $result;
647
    }
648
649
    /**
650
     * Save several sitemaps files
651
     * @param array $site_maps - array of sitemaps data
652
     */
653
    private function saveSiteMaps($site_maps) {
654
655
        if (!is_dir($this->site_map_folder_path)) {
656
            mkdir($this->site_map_folder_path, 0777);
657
        }
658
659
        foreach (glob($this->site_map_folder_path . '/sitemap*') as $site_map_file) {
660
            chmod($site_map_file, 0777);
661
            unlink($site_map_file);
662
        }
663
664
        foreach ($site_maps as $number => $site_map) {
665
            if ($site_map) {
666
                $number++;
667
                $site_map_path = $this->site_map_folder_path . "/sitemap{$number}.xml";
668
                file_put_contents($site_map_path, $site_map);
669
                chmod($site_map_path, 0777);
670
            }
671
        }
672
    }
673
674
    /**
675
     * Create main sitemap file
676
     * @param array $site_maps - array of sitemaps data
677
     * @return string
678
     */
679
    private function createMainSitemap($site_maps) {
680
681
        foreach ($site_maps as $number => $site_map) {
682
            $number++;
683
            $site_map_url = site_url(str_replace('./', '', $this->site_map_folder_path . "/sitemap{$number}.xml"));
684
            $data .= '<sitemap><loc>' . $site_map_url . '</loc></sitemap>';
685
        }
686
687
        $result = '<?xml version="1.0" encoding="UTF-8"?><sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . $data . '</sitemapindex>';
688
689
        file_put_contents($this->site_map_folder_path . '/sitemap.xml', $result);
690
        chmod($this->site_map_folder_path . '/sitemap.xml', 0777);
691
        return $result;
692
    }
693
694
    /**
695
     * Deinstall module
696
     */
697
    public function _deinstall() {
698
699
        return $this->sitemap_model->deinstallModule();
700
    }
701
702
    /**
703
     * Install module
704
     */
705
    public function _install() {
706
707
        $robotsCheck = $this->robotsCheck() ? 1 : 0;
708
        return $this->sitemap_model->installModule($robotsCheck);
709
    }
710
711
    public static function adminAutoload() {
712
713
        parent::adminAutoload();
714
715
        // Set listeners on page pre update to set url
716
        Events::create()->setListener('setUpdatedUrl', 'ShopAdminProducts:preEdit');
717
        Events::create()->onAdminPagePreEdit()->setListener('setUpdatedUrl');
718
        Events::create()->onAdminCategoryPreUpdate()->setListener('setUpdatedUrl');
719
        Events::create()->onShopCategoryPreEdit()->setListener('setUpdatedUrl');
720
        Events::create()->onShopBrandPreEdit()->setListener('setUpdatedUrl');
721
722
        Events::create()->onShopProductCreate()->setListener('ping_google');
723
        Events::create()->onShopProductUpdate()->setListener('ping_google');
724
        Events::create()->onShopProductDelete()->setListener('ping_google');
725
726
        Events::create()->onShopCategoryCreate()->setListener('ping_google');
727
        Events::create()->onShopCategoryEdit()->setListener('ping_google');
728
        Events::create()->onShopCategoryDelete()->setListener('ping_google');
729
730
        Events::create()->onShopBrandCreate()->setListener('ping_google');
731
        Events::create()->onShopBrandEdit()->setListener('ping_google');
732
        Events::create()->onShopBrandDelete()->setListener('ping_google');
733
734
        Events::create()->onAdminPageCreate()->setListener('ping_google');
735
        Events::create()->onAdminPageUpdate()->setListener('ping_google');
736
        Events::create()->onAdminPageDelete()->setListener('ping_google');
737
738
        Events::create()->onAdminCategoryCreate()->setListener('ping_google');
739
        Events::create()->onAdminCategoryUpdate()->setListener('ping_google');
740
    }
741
742
    /**
743
     * Gzip generate
744
     */
745
    public function gzip() {
746
747
        $this->_create_map();
748
        echo gzencode($this->result, $this->gzip_level);
749
    }
750
751
    /**
752
     * Show sitemap for categories
753
     */
754
    public function index() {
755
756
        $categories = $this->lib_category->_build();
757
758
        $pages = $this->db
759
            ->get_where(
760
                'content',
761
                [
762
                    'category' => 0,
763
                    'lang' => $this->config->item('cur_lang'),
764
                    'publish_date <=' => time(),
765
                    'post_status' => 'publish'
766
                ]
767
            )
768
            ->result_array();
769
770
        $this->template->assign('content', $this->sitemap_ul($categories, $pages));
771
        $this->template->show();
772
    }
773
774
    /**
775
     * Display sitemap ul list
776
     * @param array $items - site map items
777
     * @param array $pages_without_category
778
     * @return string
779
     */
780
    public function sitemap_ul($items = [], $pages_without_category = []) {
781
782
        $out = '<ul class="sitemap">';
783
784
        foreach ($items as $item) {
785
            if (isset($item['path_url'])) {
786
                $url = $item['path_url'];
787
            } elseif (isset($item['full_url'])) {
788
                $url = $item['full_url'];
789
            }
790
791
            $out .= '<li>' . anchor($url, $item['name']) . '</li>';
792
793
            // Get category pages
794
            if (isset($item['path_url'])) {
795
                $pages = $this->sitemap_model->get_cateogry_pages($item['id']);
796
797
                if ($pages) {
798
                    $out .= $this->sitemap_ul($pages);
799
                }
800
            }
801
802
            if (count($item['subtree']) > 0) {
803
                $out .= $this->sitemap_ul($item['subtree']);
804
            }
805
        }
806
807
        foreach ($pages_without_category as $page) {
808
            $out .= '<li>' . anchor($page['url'], $page['title']) . '</li>';
809
        }
810
811
        $out .= '</ul>';
812
813
        return $out;
814
    }
815
816
    /**
817
     * Send xml to google
818
     * return $code if send (200 = ok) else 'false'
819
     * @return bool|mixed
820
     */
821
    public static function ping_google() {
822
823
        // Checking is used server is local
824
825
        $ci = &get_instance();
826
        if (strstr($ci->input->server('SERVER_NAME'), '.loc')) {
827
            return FALSE;
828
        }
829
830
        $ci->db->select('settings');
831
        $ci->db->where('name', 'sitemap');
832
        $query = $ci->db->get('components', 1)->row_array();
833
834
        $settings = unserialize($query['settings']);
835
836
        // Check if turn off sending site map
837
        if (!$settings['sendSiteMap']) {
838
            return FALSE;
839
        }
840
841
        // Checking time permission(1 hour passed from last send) to send ping
842
        if ((time() - $settings['lastSend']) / (60 * 60) >= 1) {
843
844
            $ch = curl_init();
845
846
            curl_setopt($ch, CURLOPT_URL, 'http://www.google.com/webmasters/tools/ping?sitemap=' . site_url() . '/sitemap.xml');
847
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
848
849
            curl_exec($ch);
850
            $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
851
852
            curl_close($ch);
853
854
            if ($code == '200') {
855
                // Update settings, set lastSend time
856
                $settings['lastSend'] = time();
857
                $ci->db->limit(1);
858
                $ci->db->where('name', 'sitemap');
859
                $ci->db->update('components', ['settings' => serialize($settings)]);
860
861
            }
862
863
            return $code;
864
        }
865
        return false;
866
    }
867
868
}
869
870
/* End of file sitemap.php */