File: /var/www/ilya/data/www/kamforum.ru/sources/tasks/googlesitemap.php
<?php
/*
+--------------------------------------------------------------------------
| Google Sitemap Generator for IPB 2.1
| Task Module
| v1.3.1
| Coded by: CheetahShrk
+--------------------------------------------------------------------------
*/
if ( ! defined( 'IN_IPB' ) )
{
print "<h1>Incorrect access</h1>You cannot access this file directly. If you have recently upgraded, make sure you upgraded all the relevant files.";
exit();
}
class task_item
{
var $class = "";
var $root_path = "";
var $task = "";
/**
* Did topicsitemap get created or not
*
* @var int
*/
var $topicsitemap = 0;
/**
* Did forumsitemap get created or not
*
* @var int
*/
var $forumsitemap = 0;
/**
* Was the whole operation successful
*
* @var int
*/
var $successful = 0;
/**
* Is gzipping even possible?!
*
* @var int
*/
var $gzippossible = 0;
/**
* The returned status from pinging google
*
* @var int
*/
var $pingstatus = 0;
/**
* Auto Run function, makes this thing work ^^
*
*/
function run_task()
{
if( $this->ipsclass->vars['sitemap_gzip'] )
{
$phpver = phpversion();
if($phpver >= "4.0")
{
if(extension_loaded("zlib"))
{
$this->gzippossible=1;
}
}
}
if( ($this->ipsclass->vars['sitemap_topicbuild']) && (!$this->ipsclass->vars['sitemap_forumids'] == '') )
{
$this->build_topicsitemap();
}
if( ($this->ipsclass->vars['sitemap_forumbuild']) && (!$this->ipsclass->vars['sitemap_forumids'] == '') )
{
$this->build_forumsitemap();
}
//-----------------------------------------
//Fail Safe if statement, if the other 2 functions fail even if they are called
//then disable the indexsitemap build
//-----------------------------------------
if( ($this->topicsitemap == 1) OR ($this->forumsitemap == 1) )
{
$this->build_indexsitemap();
}
//-----------------------------------------
//Make sure the building of the sitemaps went ok to log it
//-----------------------------------------
if( $this->successful == 1 )
{
if( $this->ipsclass->vars['sitemap_ping'] )
{
if( $this->gzippossible != 0 )
{
$this->pingit($this->ipsclass->vars['board_url']."/indexsitemap.xml.gz");
}
else
{
$this->pingit($this->ipsclass->vars['board_url']."/indexsitemap.xml");
}
if( $this->pingstatus == 200 )
{
$this->class->append_task_log( $this->task, "Sitemap for Google rebuilt and successfully pinged Google!" );
}
else
{
$this->class->append_task_log( $this->task, "Sitemap sucessfully built but failed to ping Google." );
}
}
else
{
$this->class->append_task_log( $this->task, "Sitemap for Google rebuilt!" );
}
}
else
{
$this->class->append_task_log( $this->task, "Sitemap for Google failed" );
}
//-----------------------------------------
// Unlock Task: DO NOT MODIFY!
//-----------------------------------------
$this->class->unlock_task( $this->task );
}
/**
* Sitemap for topics ^^
*
*/
function build_topicsitemap()
{
//-----------------------------------------
//Select the data from mysql
//-----------------------------------------
$this->ipsclass->DB->query("SELECT * FROM ibf_topics WHERE forum_id IN ({$this->ipsclass->vars['sitemap_forumids']}) ORDER BY tid DESC LIMIT {$this->ipsclass->vars['sitemap_topicamount']}");
//-----------------------------------------
//Get the xml class
//-----------------------------------------
require_once( KERNEL_PATH.'class_xml.php' );
$xml = new class_xml();
//-----------------------------------------
//Need to change the xml header to UTF instead of IPB default ISO...
//-----------------------------------------
$xml->doc_type = 'UTF-8';
//-----------------------------------------
//Set the root tag..
//-----------------------------------------
$xml->xml_set_root( 'urlset', array( 'xmlns' => 'http://www.google.com/schemas/sitemap/0.84' ) );
while ( $r = $this->ipsclass->DB->fetch_row() )
{
$content = array();
//-----------------------------------------
//Make a ISO 8601 format date
//-----------------------------------------
$date = date('Y-m-d\TH:i:s+00:00', $r['last_post'] );
//-----------------------------------------
//Location tag..
//-----------------------------------------
if( $this->ipsclass->vars['sitemap_topiclofi'] )
{
$content[] = $xml->xml_build_simple_tag( 'loc', $this->lofibuild($r['tid'], 0) );
}
else
{
$content[] = $xml->xml_build_simple_tag( 'loc', "{$this->ipsclass->vars['board_url']}/index.php?showtopic={$r['tid']}" );
}
//-----------------------------------------
//Last modification tag but for this it's really start date
//-----------------------------------------
if( $this->ipsclass->vars['sitemap_topiclastmod'] )
{
$content[] = $xml->xml_build_simple_tag( 'lastmod', $date );
}
//-----------------------------------------
//Might as well make it daily
//-----------------------------------------
if( $this->ipsclass->vars['sitemap_topicchangefreq'] )
{
$changefreq = $this->changefreq($r['last_post']);
$content[] = $xml->xml_build_simple_tag( 'changefreq', $changefreq );
}
//-----------------------------------------
//If pinned make the priority level a bit higher
//-----------------------------------------
if( $this->ipsclass->vars['sitemap_topicpriority'] )
{
if( $r['pinned'] )
{
$content[] = $xml->xml_build_simple_tag( 'priority', "0.7" );
}
else
{
$content[] = $xml->xml_build_simple_tag( 'priority', "0.5" );
}
}
//-----------------------------------------
//It does what it says
//-----------------------------------------
$entry[] = $xml->xml_build_entry( 'url', $content );
}
//-----------------------------------------
// Custom code to use instead
// of one of the mental functions
// in the xml class
//-----------------------------------------
if ( is_array( $entry ) and count( $entry ) )
{
foreach( $entry as $e )
{
$xml->tmp_doc .= "\n\t\t".$e."\n";
}
}
//-----------------------------------------
//So close to finish..
//-----------------------------------------
$xml->xml_format_document();
$doc = $xml->xml_document;
if( $this->gzippossible != 0 )
{
$handle = gzopen( ROOT_PATH . "topicsitemap.xml.gz", "w9" );
gzwrite( $handle, $doc );
gzclose( $handle );
}
else
{
$handle = fopen( ROOT_PATH . "topicsitemap.xml", "w");
fwrite($handle, $doc);
fclose($handle);
}
$this->topicsitemap = 1;
}
/**
* Sitemap for cats & forums ^^
*
*/
function build_forumsitemap()
{
//-----------------------------------------
//Get the xml class
//-----------------------------------------
require_once( KERNEL_PATH.'class_xml.php' );
$xml = new class_xml();
//-----------------------------------------
//Need to change the xml header to UTF instead of IPB default ISO...
//-----------------------------------------
$xml->doc_type = 'UTF-8';
//-----------------------------------------
//Set the root tag..
//-----------------------------------------
$xml->xml_set_root( 'urlset', array( 'xmlns' => 'http://www.google.com/schemas/sitemap/0.84' ) );
//-----------------------------------------
//Select the data from mysql
//-----------------------------------------
$this->ipsclass->DB->query("SELECT * FROM ibf_forums WHERE id IN ({$this->ipsclass->vars['sitemap_forumids']}) ORDER BY id DESC LIMIT 50000");
while ( $r = $this->ipsclass->DB->fetch_row() )
{
$content = array();
//-----------------------------------------
//Make a ISO 8601 format date
//-----------------------------------------
$date = date('Y-m-d\TH:i:s+00:00', $r['last_post'] );
//-----------------------------------------
//Location tag..
//-----------------------------------------
if( $this->ipsclass->vars['sitemap_forumlofi'] )
{
$content[] = $xml->xml_build_simple_tag( 'loc', $this->lofibuild($r['id'], 1) );
}
else
{
$content[] = $xml->xml_build_simple_tag( 'loc', "{$this->ipsclass->vars['board_url']}/index.php?showforum={$r['id']}" );
}
//-----------------------------------------
//Last modification tag but for this it's really start date
//-----------------------------------------
if( $this->ipsclass->vars['sitemap_forumlastmod'] )
{
if( $r['parent_id'] > 0 )
{
$content[] = $xml->xml_build_simple_tag( 'lastmod', $date );
}
}
//-----------------------------------------
//Might as well make it daily
//-----------------------------------------
if( $this->ipsclass->vars['sitemap_forumchangefreq'] )
{
$changefreq = $this->changefreq($r['last_post']);
$content[] = $xml->xml_build_simple_tag( 'changefreq', $changefreq );
}
//-----------------------------------------
//If pinned make the priority level a bit higher
//-----------------------------------------
if( $this->ipsclass->vars['sitemap_forumpriority'] )
{
$content[] = $xml->xml_build_simple_tag( 'priority', "0.5" );
}
//-----------------------------------------
//It does what it says
//-----------------------------------------
$entry[] = $xml->xml_build_entry( 'url', $content );
}
//-----------------------------------------
// Custom code to use instead
// of one of the mental functions
// in the xml class
//-----------------------------------------
if ( is_array( $entry ) and count( $entry ) )
{
foreach( $entry as $e )
{
$xml->tmp_doc .= "\n\t\t".$e."\n";
}
}
//-----------------------------------------
//So close to finish..
//-----------------------------------------
$xml->xml_format_document();
$doc = $xml->xml_document;
//-----------------------------------------
//Now to write the xml file on the server...
//-----------------------------------------
if( $this->gzippossible != 0 )
{
$handle = gzopen( ROOT_PATH . "forumsitemap.xml.gz", "w9" );
gzwrite( $handle, $doc );
gzclose( $handle );
}
else
{
$handle = fopen( ROOT_PATH . "forumsitemap.xml", "w");
fwrite( $handle, $doc );
fclose( $handle );
}
$this->forumsitemap = 1;
}
/**
* Builds the index sitemap
*
*/
function build_indexsitemap()
{
global $DB, $ibforums, $std, $xml;
//-----------------------------------------
//Get the xml class
//-----------------------------------------
require_once( KERNEL_PATH.'class_xml.php' );
$xml = new class_xml();
//-----------------------------------------
//Need to change the xml header to UTF instead of IPB default ISO...
//-----------------------------------------
$xml->doc_type = 'UTF-8';
//-----------------------------------------
//Set the root tag..
//-----------------------------------------
$xml->xml_set_root( 'sitemapindex', array( 'xmlns' => 'http://www.google.com/schemas/sitemap/0.84' ) );
$date = date('Y-m-d\TH:i:s+00:00', time() );
$entry = array();
if( $this->topicsitemap == 1 )
{
$content = array();
//-----------------------------------------
//Location tag..
//-----------------------------------------
if( $this->gzippossible != 0 )
{
$content[] = $xml->xml_build_simple_tag( 'loc', "{$this->ipsclass->vars['board_url']}/topicsitemap.xml.gz" );
}
else
{
$content[] = $xml->xml_build_simple_tag( 'loc', "{$this->ipsclass->vars['board_url']}/topicsitemap.xml" );
}
//-----------------------------------------
//Last modification tag but for this it's really start date
//-----------------------------------------
$content[] = $xml->xml_build_simple_tag( 'lastmod', $date );
//-----------------------------------------
//It does what it says
//-----------------------------------------
$entry[] = $xml->xml_build_entry( 'sitemap', $content );
}
if( $this->forumsitemap == 1 )
{
$content2 = array();
//-----------------------------------------
//Location tag..
//-----------------------------------------
if( $this->gzippossible != 0 )
{
$content2[] = $xml->xml_build_simple_tag( 'loc', "{$this->ipsclass->vars['board_url']}/forumsitemap.xml.gz" );
}
else
{
$content2[] = $xml->xml_build_simple_tag( 'loc', "{$this->ipsclass->vars['board_url']}/forumsitemap.xml" );
}
//-----------------------------------------
//Last modification tag but for this it's really start date
$content2[] = $xml->xml_build_simple_tag( 'lastmod', "{$date}" );
//It does what it says
$entry[] = $xml->xml_build_entry( 'sitemap', $content2 );
}
//-----------------------------------------
//Custom code to use instead
// of one of the mental functions
// in the xml class
//-----------------------------------------
if ( is_array( $entry ) and count( $entry ) )
{
foreach( $entry as $e )
{
$xml->tmp_doc .= "\n\t\t".$e."\n";
}
}
//-----------------------------------------
//So close to finish..
//-----------------------------------------
$xml->xml_format_document();
$doc = $xml->xml_document;
//-----------------------------------------
//Now to write the xml file on the server...
//-----------------------------------------
if( $this->gzippossible != 0 )
{
$handle = gzopen( ROOT_PATH . "indexsitemap.xml.gz", "w9" );
gzwrite( $handle, $doc );
gzclose( $handle );
}
else
{
$handle = fopen(ROOT_PATH . "indexsitemap.xml", "w");
fwrite($handle, $doc);
fclose($handle);
}
$this->successful = 1;
}
/*-------------------------------------------------------------------------*/
// Modified XML Build simple tag function
// NOT USED ATM
/*-------------------------------------------------------------------------*/
/*
function xml_build_simple_tag( $tag, $description="", $attributes=array() )
{
return "<" . $tag . $xml->_xml_build_attribute_string($attributes) . ">" . $description . "</" . $tag . ">";
}
*/
/**
* generates the lofi links, need this to match up lofi's own lofi O/S
* dependent link code
*
* @param string $id
* @param string $tof
* @return string
*/
function lofibuild( $id, $tof = 0 )
{
if ( substr(PHP_OS, 0, 3) == 'WIN' OR strstr( php_sapi_name(), 'cgi') OR php_sapi_name() == 'apache2filter' )
{
if( $tof == 0 )
{
$url="{$this->ipsclass->vars['board_url']}/lofiversion/index.php?t{$id}.html";
}
else
{
$url="{$this->ipsclass->vars['board_url']}/lofiversion/index.php?f{$id}.html";
}
}
else
{
if( $tof == 0 )
{
$url="{$this->ipsclass->vars['board_url']}/lofiversion/index.php/t{$id}.html";
}
else
{
$url="{$this->ipsclass->vars['board_url']}/lofiversion/index.php/f{$id}.html";
}
}
return $url;
}
/**
* Function to ping goggle fo sitemap update
*
* @param string $url_xml
*/
function pingit( $url_xml )
{
if( $fp=@fsockopen('www.google.com', 80) )
{
$req = 'GET /webmasters/sitemaps/ping?sitemap=' .
urlencode( $url_xml ) . " HTTP/1.1\r\n" .
"Host: http://www.google.com\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) )
{
$this->pingstatus = intval( $m[1] );
break;
}
}
fclose( $fp );
}
}
/**
* Generates a "smart" changefrequency tag value
*
* @param string $lastpost
* @return string
*/
function changefreq($lastpost)
{
$time = time();
if( $time <= $lastpost + 3600 )
{
$changefreq = "hourly";
}
elseif( $time <= $lastpost + 86400 )
{
$changefreq = "daily";
}
elseif( $time <= $lastpost + 604800 )
{
$changefreq = "weekly";
}
elseif( $time <= $lastpost + 2629744 )
{
$changefreq = "monthly";
}
elseif( $time <= $lastpost + 31556926 )
{
$changefreq = "yearly";
}
else
{
$changefreq = "never";
}
return $changefreq;
}
/*-------------------------------------------------------------------------*/
// register_class
// LEAVE ALONE
/*-------------------------------------------------------------------------*/
function register_class(&$class)
{
$this->class = $class;
$this->ipsclass =& $class->ipsclass;
$this->root_path = $this->class->root_path;
}
/*-------------------------------------------------------------------------*/
// pass_task
// LEAVE ALONE
/*-------------------------------------------------------------------------*/
function pass_task( $this_task )
{
$this->task = $this_task;
}
}
?>