<?php
/**
* cat-sitemap.inc.php (classe per creare sitemap del sito)
*
* +----------------------------------------------------------------------+
* |                                                                      |
* | web-sytem 2.0rc : Web Site Management System                         |
* |                                                                      |
* +----------------------------------------------------------------------+
* | Copyright (c) davcaffa@gmail.com                                     |
* +----------------------------------------------------------------------+
* | Authors: Davide Caffaratti <davcaffa@gmail.com>                      |
* |                                                                      |
* +----------------------------------------------------------------------+
*
*/
if (defined('CAT_SITEMAP')) { return ; }
define('CAT_SITEMAP'true);

// Versione della classe e suo nome
define('CAT_SITEMAP_GENERATOR''generator="cat-sitemap" sitemap-generator-url="http://davidecaffaratti.com/cat-sitemap" sitemap-generator-version="1.0"');

// settaggi php ini
@ini_set("memory_limit",'32M');
@
set_time_limit(0);

/**
* @desc classe da estendere per creare sitemap del sito
* @package Cat_Sitemap
* @category sitemap
* @author Davide Caffaratti <davcaffa@gmail.com>
* @version 1.0
*/
class Cat_Sitemap
{
    
/**
    * @desc Contiene configurazione gestione messaggi classe
    * @access protected
    * @var array $class_opt
    */
    
var $class_opt = array(
                
// Crea file xml oltre al gz
                
'use_xml'=>true,
                
// Nome del file sitemap
                
'map_name'=>'sitemap',
                
// Mostra errori per debug
                
'debug'=>false,
                
// Cartella scrivibile da php per i file di log se attiva logghiamo eventi qua
                
'log_path'=>false,
                
// Inserire email per ricevere via email notificazioni di errori o informazioni sula creazione sitemap
                
'log_email'=>false,
                
// Permesso dei file se settati esempio '0644'
                
'file_permission'=>false,
                
// Permesso cartelle se settati esempio '0777'
                
'dir_permission'=>false
                
);
    
/**
    * @desc Contiene path alla sitemap con / finale
    * @access protected
    * @var string $root_path
    */
    
var $root_path;
    
/**
    * @desc Contiene url sitemap con / finale
    * @access protected
    * @var string $root_url
    */
    
var $root_url;
    
/**
    * @desc Contiene nome della sitemap .xml.gz
    * @access protected
    * @var string $map_gz_name
    */
    
var $map_gz_name;
    
/**
    * @desc Contiene nome della sitemap .xml
    * @access protected
    * @var string $map_gz_name
    */
    
var $map_xml_name;
    
/**
    * @desc Contiene stringa xml degli elementi della mappa
    * @access protected
    * @var string $flux
    */
    
var $flux '';
    
/**
    * @desc Contiene numero elementi presenti nella mappa
    * @access protected
    * @var integer $item_no
    */
    
var $item_no 0;
    
/**
    * @desc Contiene array dei server da pingare
    * @access static
    * @var string $map_pings
    */
    
var $map_pings = array (
    
'Google' => 'http://www.google.com/webmasters/sitemaps/ping?sitemap=',
    
'Yahoo' => 'http://search.yahooapis.com/SiteExplorerService/V1/updateNotification?appid=SitemapWriter&url=',
    
'Live Search' => 'http://webmaster.live.com/ping.aspx?siteMap=',
    
'Ask.com' => 'http://submissions.ask.com/ping?sitemap=',
    
'MoreOver' => 'http://api.moreover.com/ping?u='
    
);

    
/**
    * @desc Costruttore della classe
    * @param string $root_url
    * @param string $root_path
    * @param array $class_opt Se settato parametro setta nuova configurazione
    */
    
function Cat_Sitemap($root_url$root_path$class_opt=false)
    {
        
// Setto configurazione messaggi
        
if ($class_opt) {
            if (!
is_array($class_opt)) {
                
$vartrace var_export($this->class_opt'log');
                
$this->logger('Class Cat_Sitemap options errors'"Cat_Sitemap('$root_url', '$root_path', $vartrace); - Params \$class_opt is not an array !!");
                exit;
            }
            else {
                
$this->class_opt array_merge($this->class_opt$class_opt);
            }
        }

        
$this->root_url $root_url;
        
$this->root_path $root_path;
        
$this->map_gz_name $this->class_opt['map_name'].'.xml.gz';

        if (
$this->class_opt['debug']) {
            
$vartrace var_export($this->class_opt'log');
            
$this->logger('Construct class Cat_Sitemap'"Cat_Sitemap('$root_url', '$root_path', $vartrace);",'log');
        }

        if (
$this->class_opt['use_xml']) {
            
$this->logger('use xml file flag','$this->class_opt[\'use_xml\'] = true','log');
            
$this->map_xml_name $this->class_opt['map_name'].'.xml';
        }

        
// Se abbiamo la cartella $this->class_opt['log_path'] controllo che sia scrivibile
        
if ($this->class_opt['log_path']) {
            if (!
$this->is_dirWritable($this->class_opt['log_path'])) {
                
$this->logger('Log path not writable',"Cat_Sitemap(); - Error the log dir * ".$this->class_opt['log_path']." * is not writable !!");
                exit;
            }
            if (!
file_exists($this->class_opt['log_path'].'.htaccess')) {
                
$htaccess "<FilesMatch \"^sitemapLog.log\$\">\ndeny from all\n</FilesMatch>";
                if (!
$this->write_file($this->class_opt['log_path'].'.htaccess'$htaccessfalse)) {
                    
$this->logger('Can\'t write file .htaccess''Cat_Sitemap(); - Error writing .htaccess');
                }
            }
            
clearstatcache();
        }
    }
    
/**
    * @desc Distruttore pubblico
    */
    
function __destruct()
    {
        
$this->class_opt null;
        
$this->map_gz_name null;
        
$this->map_xml_name null;
        
$this->root_path null;
        
$this->root_url null;
        
$this->log_path null;
        
$this->flux null;
        
$this->item_no null;
        
$this->map_pings null;
    }
    
/**
    * @desc Aggiunge un nuovo elemento alla mappa
    * @access public
    * @param string $loc location
    * @param string $changefreq (optional)(always,hourly,daily,weekly,monthly,yearly,never)
    * @param string $priority (optional) current link's priority (0.0-1.0)
    * @param string $lastmod date (optional) format in YYYY-MM-DD or in "ISO 8601" format
    * @return void
    */
    
function add_item($loc$changefreq='weekly'$priority=0.5$lastmod='')
    {
        
// registro errore
        
if(empty($loc)) {
            
$this->logger('Failed params'"add_item('$loc', '$changefreq', '$priority', '$lastmod'); - Var \$loc is empty !! Need valid url for put in the sitemap !");
            return 
false;
        }
        else {
            if (!@
parse_url($loc)){
                
//Stop execution with an error message
                
$this->logger('Failed params'"add_item('$loc', '$changefreq', '$priority', '$lastmod'); - Var \$loc=>{$loc} is ot a valid url !! Need valid url for put in the sitemap !");
                return 
false;
            }

            
$this->logger('set map item',"add_item('$loc', '$changefreq', '$priority', '$lastmod');",'log');

            
$this->flux .= "\t<url>\n".
                           
"\t\t<loc>".utf8_encode($this->escape_xml($loc))."</loc>\n";
            if(!empty(
$lastmod)) {
                
$this->flux .= "\t\t<lastmod>".$lastmod."</lastmod>\n";
            }
            if(!empty(
$changefreq)) {
                
$this->flux .= "\t\t<changefreq>".$changefreq."</changefreq>\n";
            }
            
$this->flux .= "\t\t<priority>".$priority."</priority>\n".
                           
"\t</url>\n";
            
$this->item_no++;
        }
    }
    
/**
    * @desc Aggiunge array elementi alla mappa
    * @access public
    * @param array $items
    * @example:
    * $items = array(
    * array('loc'=>$loc,'changefreq'=>$changefreq,'priority'=>$priority,'lastmod'=>$lastmod),
    * array('loc'=>$loc,'changefreq'=>$changefreq,'priority'=>$priority,'lastmod'=>$lastmod),
    * array('loc'=>$loc,'changefreq'=>$changefreq,'priority'=>$priority,'lastmod'=>$lastmod)
    *);
    * (void)add_multiple($items);
    * @return void
    */
    
function add_multiple($items)
    {
        if (!
is_array($items)) {
            
$this->logger('Failed params''add_multiple((array)); - Var $items is not an array !! Need array ...');
            exit;
        }
        if (!
count($items)) {
            
$this->logger('Failed params''add_multiple((array)); - Var $items is an empty array !! Need array ...');
            exit;
        }

        
$this->logger('set add multiple items','add_multiple((array));','log');

        foreach(
$items as $val) {
            
// registro errore
            
if(!isset($val['loc'])){
                
$vartrace var_export($valtrue);
                
$this->logger('Failed params''add_multiple((array)); - Var $loc not exists !! * $val => '.$vartrace.' * Need valid url for put in the sitemap !');
            }
            else {
                
$loc $val['loc'];
                
$changefreq = (isset($val['changefreq'])) ? $val['changefreq'] : 'weekly';
                
$priority = (isset($val['priority'])) ? $val['priority'] : 0.5;
                
$lastmod = (isset($val['lastmod'])) ? $val['lastmod'] : '';
                
$this->add_item($loc$changefreq$priority$lastmod);
            }
        }
    }
    
/**
    * @desc Costruisce file xml.gz della mappa
    * @access public
    * @return boolean
    */
    
function create()
    {
        if (empty(
$this->flux)) {
            
$this->logger('Failed to create sitemap''The sitemap have no url in the urlset !!');
            return 
false;
        }

        
$this->logger('creating sitemap','create();','log');

        
$content =
        
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".
        
"<?xml-stylesheet type=\"text/xsl\" href=\"".$this->root_url."sitemap.xsl\"?>\n".
        
"<!-- ".CAT_SITEMAP_GENERATOR." -->\n".
        
"<urlset xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"".
        
" xsi:schemaLocation=\"http://www.sitemaps.org/schemas/sitemap/0.9".
        
" http://www.sitemaps.org/schemas/sitemap/09/sitemap.xsd\"".
        
" xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">\n".
        
$this->flux.
        
'</urlset>';
        if (!
$this->_write_sitemap($content)) {
            
$this->logger('Create sitemap prolem''create(); - Can\'t create the sitemap !!');
            return 
false;
        }

        
$this->logger('sitemap correctly created','create(); - hend creation of the sitemap','event');

        return 
true;
    }
    
/**
    * @desc Ritorna true se esiste la mappa gz altrimenti false
    * @access public
    * @return boolean false in caso non ci sia una mappa presente
    */
    
function is_sitemap()
    {
        if (!
file_exists($this->root_path.$this->map_gz_name)) {
            return 
false;
        }
        return 
true;
    }
    
/**
    * @desc Prende file xml non compresso
    * @access public
    * @return mixed string|boolean false nel caso il file non sia stato creato
    */
    
function get_xmlFile()
    {
        if (!
$this->class_opt['use_xml']) {
            return 
false;
        }

        
$file $this->root_path.$this->map_xml_name;
        if (
$this->is_sitemap() && file_exists($file)) {
            
$ret file_get_contents($file);
        }
        else {
            
$this->logger('Failed to read xml sitemap','file '.$this->map_xml_name.' not exists !!');
            
$ret false;
        }
        
clearstatcache();
        return 
$ret;
    }
    
/**
    * @desc Prende contenuto di un url se trova le lib curl installate le usa altrimenti usa le soket
    * @access public
    * @param string $url
    * @return array
    */
    
function get_content($url)
    {
        
$user_agent "Cat_Sitemap 1.0 Sitemaps Creator (compatible; sitemapcreatorbot/1.0; +http://davidecaffaratti.com/) ";

        
$this->logger('get contents from url',"get_content($url);",'log');

        if (
function_exists('curl_exec')) {
            
$gacookie $this->get_tempDir().'/cat_sitemap_cookies';

            
$options = array(
                
CURLOPT_RETURNTRANSFER => true,
                
CURLOPT_USERAGENT      => $user_agent,
                
CURLOPT_CONNECTTIMEOUT => 15,
                
CURLOPT_TIMEOUT        => 15,
                
CURLOPT_MAXREDIRS      => 10,
                
CURLOPT_COOKIEJAR      => $gacookie,
                
CURLOPT_COOKIEFILE     => $gacookie
            
);
            if (
ini_get('safe_mode')) {
                
$options[CURLOPT_FOLLOWLOCATION] = true;
            }
            
$ch curl_init($url);
            
curl_setopt_array($ch$options);
            
$resp['content'] = trim(curl_exec($ch));
            
$err             curl_errno($ch);
            
$errmsg          curl_error($ch);
            if(
$err){
                
curl_close($ch);
                
$this->logger('Failed to connect url'"{$errmsg} for URL * {$url} *");
                return 
false;
            }
            else {
                
$header curl_getinfo($ch);
                
curl_close($ch);
            }
        }
        else {
            
$parse_url parse_url($url);
            if ((
$fp = @fsockopen($parse_url['host'], $parse_url['port'], $errno$errstr15)) === false) {
                switch(
$errno) {
                    case -
3:
                        
$this->logger('Failed to connect url''Socket creation failed (-3)');
                    break;
                    case -
4:
                        
$this->logger('Failed to connect url''DNS lookup failure (-4)');
                    break;
                    case -
5:
                        
$this->logger('Failed to connect url''Connection refused or timed out (-5)');
                    break;
                    default:
                        
$this->logger('Failed to connect url''Connection failed ('.$errno.') '.$errstr);
                }
                return 
false;
            }

            if (
version_compare(phpversion(), '4.3.0''>=')) {
                
stream_set_timeout($fp15);
            }
            
$get "GET {$parse_url['path']}{$parse_url['query']} HTTP/1.1\r\n";
            
$get .= "Host: {$parse_url['host']}\r\n";
            
$get .= "User-Agent: {$user_agent})\r\n";
            
$get .= "Referer: {$parse_url['scheme']}://{$parse_url['host']}{$parse_url['path']}\r\n";
            
$get .= "Connection: close\r\n\r\n";
            if (
version_compare(phpversion(), '4.3.0''>=')) {
                
stream_set_blocking($fp1);
            }
            
fwrite($fp$get);

            
$res '';

            while (
'' != ($line=trim(fgets($fp1024)))) {
                if (
false !== ($pos=strpos($line':')))
                    
$header[str_replace('-''_'strtolower(substr($line0$pos)))] = trim(substr($line$pos+1));
                elseif(
preg_match('#\s([0-9]{3})\s#i'$line$code))
                    
$header['http_code'] = intval($code[0]);
            }

            if (!empty(
$header['location'])){
                
$resp get_content($header['location']);
                
$header['http_code'] = 200;
                
$header['url'] = $header['location'];
            }

            if (isset(
$header['transfer_encoding']) && $header['transfer_encoding'] == 'chunked') {
                
$chunk hexdec(fgets($fp1024));
            } else {
                
$chunk = -1;
            }

            
$res '';
            while (
$chunk != && !feof($fp)) {
                if (
$chunk 0){
                     
$part fread($fp$chunk);
                     
$chunk -= strlen($part);
                     
$res .= $part;
                     if (
$chunk == 0){
                         if (
fgets($fp1024) != "\r\n") ;
                         
$chunk hexdec(fgets($fp1024));
                     }
                } else {
                     
$res .= fread($fp1024);
                }
            }
            
fclose($fp);
            
$resp['content'] = $res;

        }

        if (isset(
$header['content_type']) && stripos($header['content_type'], 'text') === false){
            
$this->logger('Failed content type'"Document type is * {$header['content_type']} * for URL * {$url} *");
            return 
false;
        }
        else if (isset(
$header['http_code']) && $header['http_code'] != 200){
            
$code "{$header['http_code']}";
            
$this->logger('Failed http code'"* {$header['http_code']} Error * Found for URL * {$url} *");
            return 
false;
        }
        else {
            
// redirection
            
if (isset($header['url']) && $header['url'] != $url){
                
$url $resp['new_url'] = $header['url'];
            }

            return 
$resp;
        }
    }
    
/**
    * @desc Funzione per scrivere un file ritorna dimensioni file scritto
    * @access public
    * @param string $file
    * @param string $content
    * @param boolean $gzcompress Se settato a true comprime il file con gzip
    * @param string $mode Modalità apertura file
    * @param boolean $printLogger Se settato a false non setta il logger
    * @return mixed integer|boolean false se avviene un errore
    */
    
function write_file($file$content$gzcompress=false$mode='wb'$printLogger=true)
    {
        if (
$printLogger) {
            
$this->logger('write file'"write_file('$file', '\$content', ".(($gzcompress) ? 'true' 'false').", '$mode');",'log');
        }
        
        if (
$gzcompress) {      
            if ((
$fh gzopen($file$mode.'6')) === false) {
                return 
false;
            }
            
flock($fhLOCK_EX);        
            if ((
gzwrite($fh$content)) === false) {
                return 
false;
            }
            
flock($fhLOCK_UN);
            
gzclose($fh);
        }
        else {
            if ((
$fh fopen($file$mode)) === false) {
                return 
false;
            }
            
flock($fhLOCK_EX);        
            if ((
fwrite($fh$content)) === false) {
                return 
false;
            }
            
flock($fhLOCK_UN);
            
fclose($fh);
        }
        if (
$this->class_opt['file_permission']) {
            @
chmod($file$this->class_opt['file_permission']);
        }
        return 
filesize($file);
    }
    
/**
    * @desc Pulisce entità
    * @access public
    * @return string
    */
    
function escape_xml($string)
    {
        return 
str_replace ( array ( '&''"'"'"'<''>'), array ( '&amp;' '&quot;''&apos;' '&lt;' '&gt;'), $string);
    }
    
/**
    * @desc Funzione privata per gestire gli errori
    * @access public
    * @param string $title
    * @param string $mensage
    * @param boolean $logType possibili : 'error', 'log', 'event'
    */
    
function logger($title$mensage$logType='error')
    {
        
// Debug mensage
        
if ($this->class_opt['debug']) {
            
// Debug color
            
if ($logType == 'log') {
                
$style 'color:blue;';
                
$hr 'blue';
            }
            else if (
$logType == 'error') {
                
$style 'background:red;color:white;';
                
$hr 'red';
            }
            else {
                
$style 'background:lime;color:black;';
                
$hr 'lime';
            }
            echo 
"<span style=\"".$style."\"><strong>".$title."</span><br />\n".$mensage."</strong><br /><hr style=\"height:3px;background:".$hr.";\" />\n<br />\n";
        }

        
// Mail webmaster
        
if ($logType != 'log' && $this->class_opt['log_email'] && !empty($this->class_opt['log_email'])) {
            
$msn =
            
"=======================================================================\n".
            
"*Sitemap ".$title."*".
            
"Date : ".gmdate("Y.m.d h:i:s")."\n".
            
"=======================================================================\n\n".
            
$mensage;
            
mail($this->class_opt['log_email'], '[ Sitemap '.$title.' ]'$msg"From: sitemapcreator@".$_SERVER['HTTP_HOST']);
        }

        
// Log file
        
if ($logType != 'log' && $this->class_opt['log_path']) {
            
$content $logType.';'.$title.';'.gmdate("Y.m.d h:i:s").';'.str_replace(array(';',"\n","\t"),array('.',' ',' '),strip_tags($mensage))."\n";
            
// Controllo se abbiamo il file di log presente
            
if (!file_exists($this->class_opt['log_path'].'sitemapLog.log')) {
                if (!
$this->write_file($this->class_opt['log_path'].'sitemapLog.log'$contentfalse'wb'false)) {
                    
// Tento di mandare una email all'amministratoredel sistema
                    
if (!$this->class_opt['debug']) {
                        
mail($this->try_mailAdmin(), $titlestr_replace("\t""\n""Error in logger(); !!\n\n".$mensage), "From: sitemapcreator@".$_SERVER['HTTP_HOST']);
                    }
                    
trigger_error('Error writing sitemapLog.log !!'E_USER_WARNING);
                    exit;
                }
            }
            else {
                if (!
$this->write_file($this->class_opt['log_path'].'sitemapLog.log'$contentfalse'a'false)) {
                    
// Tento di mandare una email all'amministratoredel sistema
                    
if (!$this->class_opt['debug']) {
                        
mail($this->try_mailAdmin(), $titlestr_replace("\t""\n""Error in logger(); !!\n\n".$mensage), "From: sitemapcreator@".$_SERVER['HTTP_HOST']);
                    }
                    
trigger_error('Error writing sitemapLog.log !!'E_USER_WARNING);
                    exit;
                }
            }
            
clearstatcache();
        }

        
// Trigger error se non abbiamo altri metodi di loggare
        
if (!$this->class_opt['debug'] && !$this->class_opt['log_email'] && !$this->class_opt['log_path']) {
            
$e_error = ($logType != 'error') ? E_USER_WARNING E_USER_NOTICE;
            
trigger_error($title.' - '.$mensage$e_error);
        }
        return 
true;
    }
    
/**
    * @desc Visiona il file di log
    * @access public
    * @return string
    */
    
function get_logDatas()
    {
        if (!
$this->class_opt['debug']) {
            return;
        }
        
        if (!
$this->class_opt['log_path']) {
            return 
"<h1 style=\"color:red\">There is no log path setting in the class !!</h1>\n";
        }

        
$content file_get_contents($this->class_opt['log_path'].'sitemapLog.log');
        if (
$content == false) {
            return 
"<h1 style=\"color:red\">Problem extracting the file log content !!</h1>\n";
        }
        else if (empty(
$content)) {
            return 
"<h1 style=\"color:blue\">There is no report in the file log</h1>\n";
        }
        else {
            
$ret "<h1 style=\"width:98%;color:black;margin-bottom:20px;padding-top:10px;padding-bottom:10px;border:2px solid #2580B2;\">Report of the sitemap creation</h1>\n".
                   
"<table style=\"width:98%;border:1px solid #2580B2;background-color:#2580B2;\" cellspacing=\"1\" cellpadding=\"5\">\n".
                   
"  <tr style=\"background-color:#CFEBF7;color:black;\">\n".
                   
"    <th scope=\"col\" width=\"200px\">Title</th>\n".
                   
"    <th scope=\"col\" width=\"60px\">Date</th>\n".
                   
"    <th scope=\"col\">Mensaje</th>\n".
                   
"  </tr>\n";

            
$elements explode("\n"$content);
            
$i=0;
            foreach(
$elements as $lines){
                
$log explode(';'$lines);
                
$logColor = ($log[0] == 'error') ? 'background:red;color:white;' 'background:lime;color:black;';
                if (isset(
$log[0]) && isset($log[1]) && isset($log[2]) && isset($log[3])) {
                    
$color = ($i == 1) ? 'whitesmoke' 'white';
                    
$ret .= "  <tr style=\"background:".$color.";font-weight:bold;vertical-align:top;margin-top:0;\">\n".
                            
"    <td style=\"".$logColor."\">".$log[1]."</td>\n".
                            
"    <td>".$log[2]."</td>\n".
                            
"    <td>".$log[3]."</td>\n".
                            
"  </tr>\n";
                    
$i++;
                }
            }
            
$ret .= "</table>\n";
        }
        return 
$ret;
    }
    
/**
    * @desc Tento di prendere indirizzo amministratore nel caso non possa eseguire il normale logging eventi
    * @access public
    * @return string
    */
    
function try_mailAdmin()
    {
        
// Try to set email to webmaster
        
$to 'webmaster@'.$_SERVER['HTTP_HOST'].', info@'.$_SERVER['HTTP_HOST'];
        if (!empty(
$_SERVER["SERVER_ADMIN"]) && $_SERVER["SERVER_ADMIN"] != 'webmaster@'.$_SERVER['HTTP_HOST'] && $_SERVER["SERVER_ADMIN"] != 'info@'.$_SERVER['HTTP_HOST']) {
            
$to .= ', '.$_SERVER["SERVER_ADMIN"];
        }
        return 
$to;
    }
    
/**
    * @desc Controlla se una cartella è scrivibile e prova a renderlo scrivibile se non lo é.
    * @access private
    * @return bool true if writable
    */
    
function is_dirWritable($dir)
    {
        if (!
is_dir($dir)) {
            
$this->logger('Invalid dir','is_dirWritable('.$dir.'); - The dir passed is not a valid directory !!');
            exit;
        }
        if(!
is_writable($dir)) {
            if (!
$this->class_opt['dir_permission']) {
                return 
false;
            }
            if(!@
chmod($dir$this->class_opt['dir_permission'])) {
                return 
false;
            }
        }
        return 
true;
    }
    
/**
    * @desc Ritorna la directory predefinita del php temp
    * @access public
    * @return mixed string|boolean false se non trova la directory temp
    */
    
function get_tempDir()
    {
        if (
version_compare(phpversion(), '5.2.1''>=')) {
            if (!empty(
$_ENV['TMP'])) {
                return 
realpath($_ENV['TMP']);
            }
            if (!empty(
$_ENV['TMPDIR'])) {
                return 
realpath$_ENV['TMPDIR']);
            }
            if (!empty(
$_ENV['TEMP'])) {
                return 
realpath$_ENV['TEMP']);
            }
            
$tempfile=tempnam(uniqid(rand(),true),'');
            if (
file_exists($tempfile)) {
                
unlink($tempfile);
                return 
realpath(dirname($tempfile));
            }
        }
        else {
            return 
sys_get_temp_dir();
        }
    }
    
/**
    * @desc Funzione privata per pingare la sitemap
    * @access private
    * @return boolean
    */
    
function _ping()
    {
        
$this->logger('start make ping','true','log');

        foreach(
$this->map_pings as $domain=>$val) {
            
$pingError null;
            
$vartrace null;
            switch(
$domain) {
                
//Ask.com returns 200 OK even if there was an error, so we need to check the content.
                
case 'Ask.com':
                    if(!
$response $this->get_content($val.$this->root_url.$this->map_gz_name)) {
                        
$pingError var_export($responsetrue);
                        
$this->logger('Failed to ping '.$domain"Failed to ping * ".$val.$this->root_url.$this->map_gz_name." * response is {$pingError} *");
                    }
                    else if (
strpos($response['content'], "successfully received and added")===false) {
                        
$pingError var_export($responsetrue);
                        
$this->logger('Failed to ping Ask.com'"Failed to ping * ".$val.$this->root_url.$this->map_gz_name." * response is {$pingError} *");
                    }
                break;
                case 
'Yahoo':
                    if(!
$response $this->get_content($val.$this->root_url.$this->map_gz_name)) {
                        
$pingError var_export($responsetrue);
                        
$this->logger('Failed to ping '.$domain"Failed to ping * ".$val.$this->root_url.$this->map_gz_name." * response is {$pingError} *");
                    }
                    else if (
strpos(strtolower($response['content']), "success")===false) {
                        
$pingError var_export($responsetrue);
                        
$this->logger('Failed to ping'"Failed to ping * ".$val.$this->root_url.$this->map_gz_name." * response is {$pingError} *");
                    }
                break;
                case 
'Live Search':
                    if(!
$response $this->get_content($val.$this->root_url.$this->map_gz_name)) {
                        
$pingError var_export($responsetrue);
                        
$this->logger('Failed to ping '.$domain"Failed to ping * ".$val.$this->root_url.$this->map_gz_name." * response is {$pingError} *");
                    }
                    else if (
strpos(strtolower($response['content']), "thanks for submitting your sitemap.")===false) {
                        
$pingError var_export($responsetrue);
                        
$this->logger('Failed to ping Live Search'"Failed to ping * ".$val.$this->root_url.$this->map_gz_name." * response is {$pingError} *");
                    }
                break;
                default:
                    if(!
$response $this->get_content($val.$this->root_url.$this->map_gz_name)) {
                        
$pingError var_export($responsetrue);
                        
$this->logger('Failed to ping '.$domain"Failed to ping * ".$val.$this->root_url.$this->map_gz_name." * response is {$pingError} *");
                    }
            }
            if (!
$pingError) {
                
$this->logger('Correctly pinged '.$domain$domain.' - Correctly pinged and added the sitemap to server.','event');
            }
        }
        return 
true;
    }
    
/**
    * @desc Scrive la sitemap ed effettua il ping ai server
    * @access private
    * @param string $content
    * @return boolean
    */
    
function _write_sitemap($content)
    {
        
// Controllo che il file della sitemap.xml.gz esista e sia scrivibile
        
if (!is_writable($this->root_path.$this->map_gz_name)) {
            if (!
file_exists($this->root_path.$this->map_gz_name)) {
                if (!
$this->write_file($this->root_path.$this->map_gz_name$contenttrue'w')) {
                    
$this->logger('write sitemap error',"_write_sitemap(); - Error the sitemap * ".$this->root_path.$this->map_gz_name." * not exists !! Please create it using the ftp");
                    return 
false;
                }
            }
            else { 
                if (!
$this->class_opt['file_permission']) {
                    
$this->logger('write sitemap error',"_write_sitemap(); - Error the sitemap * ".$this->root_path.$this->map_gz_name." * is not writable and can't chmod it !! Please chmod it using the ftp");
                    return 
false;
                }
                else if (!@
chmod($this->root_path.$this->map_gz_name$this->class_opt['file_permission'])) {
                    
$this->logger('write sitemap error',"_write_sitemap(); - Error the sitemap * ".$this->root_path.$this->map_gz_name." * is not writable and can't chmod it !! Please chmod it using the ftp");
                    return 
false;
                }
                else {
                    if (!
is_writable($this->root_path.$this->map_gz_name)) {
                        
$this->logger('write sitemap error',"_write_sitemap(); - Error the sitemap * ".$this->root_path.$this->map_gz_name." * is not writable after the chmod !! Please create it using the ftp or change the current option on \$this->class_opt['file_permission']");
                        return 
false;
                    }
                    else if (!
$this->write_file($this->root_path.$this->map_gz_name$contenttrue'w')) {
                        
$this->logger('Class errors',"_write_sitemap(); - Error the sitemap * ".$this->root_path.$this->map_gz_name." * not exists !! Please create it using the ftp");
                        return 
false;
                    }
                }
            }
        }
        else {
            if (!
$this->write_file($this->root_path.$this->map_gz_name$contenttrue'w')) {
                
$this->logger('write sitemap error',"_write_sitemap(); - Error the sitemap * ".$this->root_path.$this->map_gz_name." * not exists !! Please create it using the ftp");
                return 
false;
            }
        }
        
clearstatcache();
        
// Controllo che il file della sitemap.xml esista e sia scrivibile se usiamo la sitemap xml
        
if ($this->class_opt['use_xml'] && !is_writable($this->root_path.$this->map_xml_name)) {
            if (!
file_exists($this->root_path.$this->map_xml_name)) {
                if (!
$this->write_file($this->root_path.$this->map_xml_name$contentfalse)) {
                    
$this->logger('write sitemap error',"_write_sitemap(); - Error the sitemap * ".$this->root_path.$this->map_xml_name." * not exists !! Please create it using the ftp or change the current option on \$this->class_opt['file_permission']");
                }
            }
            else { 
                if (!
$this->class_opt['file_permission']) {
                    
$this->logger('write sitemap error',"_write_sitemap(); - Error the sitemap * ".$this->root_path.$this->map_xml_name." * is not writable and can't chmod it !! Please create it using the ftp or change the current option on \$this->class_opt['file_permission']");
                }
                else if (!@
chmod($this->root_path.$this->map_xml_name$this->class_opt['file_permission'])) {
                    
$this->logger('write sitemap error',"_write_sitemap(); - Error the sitemap * ".$this->root_path.$this->map_xml_name." * is not writable and can't chmod it !! Please create it using the ftp or change the current option on \$this->class_opt['file_permission']");
                }
                else {
                    if (!
is_writable($this->root_path.$this->map_xml_name)) {
                        
$this->logger('write sitemap error',"_write_sitemap(); - Error the sitemap * ".$this->root_path.$this->map_xml_name." * is not writable after the chmod !! Please create it using the ftp or change the current option on \$this->class_opt['file_permission']");
                    }
                    else if (!
$this->write_file($this->root_path.$this->map_xml_name$contentfalse)) {
                        
$this->logger('write sitemap error',"_write_sitemap(); - Error the sitemap * ".$this->root_path.$this->map_xml_name." * not exists !! Please create it using the ftp or change the current option on \$this->class_opt['file_permission']");
                    }
                }
            }
        }
        else {
            if (!
$this->write_file($this->root_path.$this->map_xml_name$contentfalse)) {
                
$this->logger('write sitemap error',"_write_sitemap(); - Error the sitemap * ".$this->root_path.$this->map_xml_name." * not exists !! Please create it using the ftp or change the current option on \$this->class_opt['file_permission']");
            }
        }
        
clearstatcache();

        
$robotsContent "Sitemap : ".$this->root_url.$this->map_gz_name;
        
// Cotrollo se file non é scrivibile o non esiste
        
if (!is_writable($this->root_path.'robot.txt')) {
            
// Controllo che il file robot.txt esista
            
if (!file_exists($this->root_path.'robot.txt')) {
                if (!
$this->write_file($this->root_path.'robot.txt'$robotsContentfalse)) {
                    
$this->logger('write sitemap error',"_write_sitemap(); - Error the robot.txt not exists and I can' create it !! Please create it using the ftp or change the current option on \$this->class_opt['file_permission']");
                }
            }
            else { 
                if (!
$this->class_opt['file_permission']) {
                    
$this->logger('write sitemap error',"_write_sitemap(); - Error the robot.txt is not writable and can't chmod it !! Please create it using the ftp or change the current option on \$this->class_opt['file_permission']");
                }
                else if (!@
chmod($this->root_path.'robot.txt'$this->class_opt['file_permission'])) {
                    
$this->logger('write sitemap error',"_write_sitemap(); - Error the robot.txt is not writable and can't chmod it !! Please create it using the ftp or change the current option on \$this->class_opt['file_permission']");
                }
                else {
                    if (!
is_writable($this->root_path.'robot.txt')) {
                        
$this->logger('write sitemap error',"_write_sitemap(); - Error the robot.txt is not writable after the chmod !! Please create it using the ftp or change teh current option on \$this->class_opt['file_permission']");
                    }
                    else if (!
$this->write_file($this->root_path.'robot.txt'$robotsContentfalse)) {
                        
$this->logger('write sitemap error',"_write_sitemap(); - Error the robot.txt not exists and I can' create it !! Please create it using the ftp or change the current option on \$this->class_opt['file_permission']");
                    }
                }
            }
        }
        else {
            
$robots file_get_contents($this->root_path.'robot.txt');
            if (
strpos($robots'Sitemap :') === false) {
                if (!
$this->write_file($this->root_path.'robot.txt'$robotsContentfalse)) {
                    
$this->logger('write sitemap error',"_write_sitemap(); - Error the robot.txt exists and haven't the sitemap url, but Y can't I write on it !! Please create it using the ftp or change the current option on \$this->class_opt['file_permission']");
                }
            }
        }
        
clearstatcache();

        
// Ping the services for new map
        
$this->_ping();
        return 
true;
    }
}
/**
* @desc classe estesa per amministrare le sitemap del sito
* @package Cat_Sitemap_Manage
* @uses Cat_Sitemap
* @category sitemap
* @author Davide Caffaratti <davcaffa@gmail.com>
* @version 1.0
*/
class Cat_Sitemap_Manage extends Cat_Sitemap
{
    
/**
    * @desc Costruttore della classe
    * @param string $root_url
    * @param string $root_path
    * @param array $class_opt Se settato parametro setta nuova configurazione
    */
    
function Cat_Sitemap_Manage($root_url$root_path$class_opt=false)
    {
        
parent::Cat_Sitemap($root_url$root_path$class_opt);

        if (
$this->class_opt['debug']) {
            
$vartrace var_export($this->class_opttrue);
            
$this->logger('Construct class Cat_Sitemap_Manage'"Cat_Sitemap_Manage('$root_url', '$root_path', $vartrace);",'log');
        }
    }
    
/**
    * @desc Distruttore pubblico
    */
    
function __destruct()
    {
        
parent::__destruct();
    }
    
/**
    * @desc Inserisce una uova riga del sitemap
    * @access public
    * @param string $loc location
    * @param string $changefreq (optional)(always,hourly,daily,weekly,monthly,yearly,never)
    * @param string $priority (optional) current link's priority (0.0-1.0)
    * @param string $lastmod date (optional) format in YYYY-MM-DD or in "ISO 8601" format
    * @return boolean
    */
    
function insert_row($loc$changefreq='weekly'$priority=0.5$lastmod='')
    {
        
$this->logger('insert new item row',"insert_row('$loc','$changefreq','$priority','$lastmod');",'log');

        if (!
$content $this->get_xmlFile()) {
            
$this->logger('Insert Row error','insert_row(); - Can\'t insert row !! Need to activate the xml creation in the class in the option $this->class_opt[\'use_xml\']');
            return 
false;
        }
        
$this->flux ='';
        
$this->add_item($loc$changefreq$priority$lastmod);
        
$newContent str_replace('</urlset>'$this->flux.'</urlset>'$content);
        return 
$this->_write_sitemap($newContent);
    }
    
/**
    * @desc Aggiunge array elementi alla mappa
    * @access public
    * @param array $items
    * @return boolean
    */
    
function insert_multipleRows($items)
    {
        
$this->logger('insert multiple rows','insert_multipleRows((array));','log');

        if (!
$content $this->get_xmlFile()) {
            
$this->logger('Insert multiple row error','insert_multiple((array)); - Can\'t insert row !! Need to activate the xml creation in the class in the option $this->class_opt[\'use_xml\']');
            return 
false;
        }
        
$this->flux ='';
        
$this->add_multiple($items);
        
$newContent str_replace('</urlset>'$this->flux.'</urlset>'$content);
        return 
$this->_write_sitemap($newContent);
    }
    
/**
    * @desc Aggiorna una riga della sitemap
    * @access public
    * @param string $hold_loc hold location
    * @param string $loc new location
    * @param string $changefreq (optional)(always,hourly,daily,weekly,monthly,yearly,never)
    * @param string $priority (optional) current link's priority (0.0-1.0)
    * @param string $lastmod date (optional) format in YYYY-MM-DD or in "ISO 8601" format
    * @return boolean
    */
    
function update_row($hold_loc$loc$changefreq='weekly'$priority=0.5$lastmod='')
    {
        
$this->logger('update row',"update_row('$hold_loc','$loc','$changefreq','$priority','$lastmod');",'log');

        if (!
$presentRow $this->get_matchedLoc($hold_loc)) {
            return 
false;
        }
        
$this->flux ='';
        
$this->add_item($loc$changefreq$priority$lastmod);
        
$newContent str_replace($presentRow$this->flux$content);
        return 
$this->_write_sitemap($newContent);
    }
    
/**
    * @desc Cancella una riga dalla sitemap
    * @access public
    * @param string $loc location
    * @return boolean
    */
    
function delete_row($loc)
    {
        
$this->logger('delete row'"delete_row('$loc');",'log');

        if (!
$presentRow $this->get_matchedLoc($hold_loc)) {
            return 
false;
        }
        
$newContent str_replace($presentRow''$content);
        return 
$this->_write_sitemap($newContent);
    }
    
/**
    * @desc Ritorna stringa in base al locale se esiste o false se non trova corrispondenze
    * @access public
    * @param string $loc
    * @return mixed string|boolean false in caso non incontri la stringa
    */
    
function get_matchedLoc($loc)
    {
        if (!
$content $this->get_xmlFile()) {
            
$this->logger('MatchedLoc error',"get_matchedLoc('$loc'); - Can't insert row !! Need to activate the xml creation in the class in the option \$this->class_opt['use_xml']");
            return 
false;
        }
        if (
preg_match("@(<url>)\n\t\t(<loc>$loc</loc>)\n\t\t(<lastmod>.*</lastmod>)\n\t\t(<changefreq>.*</changefreq>)\n\t\t(<priority>.*</priority>)\n\t(</url>)@i"$content$match)) {
            return 
$match[0];
        }
        return 
false;
    }
    
/**
    * @desc Cancella contenuti dalla sitemap
    * @access public
    * @return boolean
    */
    
function delete_sitemapContent()
    {
        
$this->logger('delete sitemap','delete_sitemap();','log');

        if (
file_exists($this->root_path.$this->map_gz_name)) {
            if (!
$this->write_file($this->root_path.$this->map_gz_name''true)) {
                
$this->logger('Delete errors',"delete_sitemap(); - Error can\'t empty the sitemap * ".$this->root_path.$this->map_gz_name." *");
                return 
false;
            }
        }
        if (
$this->class_opt['use_xml'] && file_exists($this->root_path.$this->map_xml_name)) {
            if (!
$this->write_file($this->root_path.$this->map_xml_name''true)) {
                
$this->logger('Delete errors',"delete_sitemap(); - Error can\'t empty the sitemap * ".$this->root_path.$this->map_xml_name." *");
                return 
false;
            }
        }
        
clearstatcache();
        return 
true;
    }
}
/**
* @desc classe estesa per creare le sitemap del sito usando un crontab
* @package Cat_Sitemap_Crontab
* @uses Cat_Sitemap
* @category sitemap
* @author Davide Caffaratti <davcaffa@gmail.com>
* @version 1.0
*/
class Cat_Sitemap_Crontab extends Cat_Sitemap
{
    
/**
    * @desc Costruttore della classe
    * @param string $root_url
    * @param string $root_path
    * @param array $class_opt Se settato parametro setta nuova configurazione
    */
    
function Cat_Sitemap_Crontab($root_url$root_path$class_opt=false)
    {
        
parent::Cat_Sitemap($root_url$root_path$class_opt);

        if (
$this->class_opt['debug']) {
            
$vartrace var_export($this->class_opttrue);
            
$this->logger('Construct class Cat_Sitemap_Crontab'"Cat_Sitemap_Crontab('$root_url', '$root_path', $vartrace);"'log');
        }

        
$curDir dirname(__FILE__).'/';
        
$curFile basename($_SERVER['PHP_SELF']);
        
$htaccess "<FilesMatch \"^".$curFile."\$\">\ndeny from all\n</FilesMatch>";
        
// Controllo se un file .htaccess esista o lo creo
        
if (!file_exists($curDir.'.htaccess')) {
            if (!
$this->write_file($curDir.'.htaccess'$htaccessfalse)) {
                
$this->logger('Error writing file',"Cat_Sitemap_Crontab(); - Error writing .htaccess file !!");
            }
        }
        else {
            if (!
$this->write_file($curDir.'.htaccess'"\n".$htaccessfalse'a')) {
                
$this->logger('Error writing file',"Cat_Sitemap_Crontab(); - Error writing .htaccess file !!");
            }
        }
        
clearstatcache();
    }
}
?>