<?php
class Sitemap extends Controller
{
	var $base_url = '/home/cadline/public_html/website/application/';
	var $sites = array(
        'products' = array(
            'url' => 'products',
            'mod_contr' => exec("date '+%Y-%m-%d' -u -r ".$this->base_url."controllers/products.php"),
            'mod_view' => exec("date '+%Y-%m-%d' -u -r ".$this->base_url."view/products/index.php"),
            'change_freq' => 'monthly',
            'importance' => '1.0'
		);
	);

    function __construct()
    {
        parent::Controller();
        $this->load->helper(array('text','url'));
        $this->load->plugin('google_sitemap'); //Load Plugin
    }

    function index()
    {
        $sitemap = new google_sitemap; //Create a new Sitemap Object
        
        foreach($sites as $k => $s)
        {
            $item = new google_sitemap_item('http://www.archlinexp.com/'.$k, date("Y-m-d"), $s, '0.8' );
        	$sitemap->add_item($item);
        }
        
        $sitemap->build('/public/sitemap/sitemap.xml'); //Build it...

         //Let's compress it to gz
        $data = implode("", file("/public/sitemap/sitemap.xml"));
        $gzdata = gzencode($data, 9);
        $fp = fopen("/public/sitemap/sitemap.xml.gz", "w");
        fwrite($fp, $gzdata);
        fclose($fp);

        //Let's Ping google
        $this->_pingGoogleSitemaps('/public/sitemap/sitemap.xml.gz');
    }

    function _pingGoogleSitemaps( $url_xml )
    {
       $status = 0;
       $google = 'www.google.com';
       if( $fp=@fsockopen($google, 80) )
       {
          $req =  'GET /webmasters/sitemaps/ping?sitemap=' .
                  urlencode( $url_xml ) . " HTTP/1.1\r\n" .
                  "Host: $google\r\n" .
                  "User-Agent: Mozilla/5.0 (compatible; " .
                  PHP_OS . ") PHP/" . PHP_VERSION . "\r\n" .
                  "Connection: Close\r\n\r\n";
          fwrite( $fp, $req );
          while( !feof($fp) )
          {
             if( @preg_match('~^HTTP/\d\.\d (\d+)~i', fgets($fp, 128), $m) )
             {
                $status = intval( $m[1] );
                break;
             }
          }
          fclose( $fp );
       }
       return( $status );
    }

}