| > | # |---------------|--------|-------------| # | Less Than | < | < | # |---------------|--------|-------------| function googlesitemap_compatible($url) { $url = str_replace("&","&",$url); $url = str_replace("'","'",$url); $url = str_replace('"',""",$url); $url = str_replace(">",">",$url); $url = str_replace("<","<",$url); return $url; } # Gets a URLs path minus the actual filename + query. function getPath($url) { if($GLOBALS['index_dynamic_pages_params'] == true) { $url = explode("?", $url); $url = $url[0]; } $temp = explode("/", $url); $fnsize=strlen($temp[(count($temp) - 1)]); return substr($url, 0, strlen($url) - $fnsize); } # Cleans up a path so that extra / slashes are gone, .. are translated, etc function cleanPath($url) { $new = array(); $url = explode("/", trim($url)); foreach($url as $p) { $p = trim($p); if($p != "" && $p != ".") { if($p == "..") { if(is_array($new)) $new = array_pop($new); } else { $new = array_merge((array) $new, array($p)); } } } $url = $new[0]."/"; for($i=1; $i < count($new); $i++) $url .= "/".$new[$i]; return $url; } # Checks if URL has specified extension, if so returns true function checkExt($url, $ext) { # Strip out parameter info from a script (?) if($GLOBALS['index_dynamic_pages_params'] == true) { $url = explode("?", $url); $url = $url[0]; } $text=substr($url, strlen($url) - (strlen($ext) + 1), strlen($url)); if($text == ".".$ext) return true; else return false; } # Retrieve Site URLs function getUrls($url, $string) { $type = "href"; # Regex to chop out urls preg_match_all("|$type\=\"?'?`?([[:alnum:]:?=&@/._-]+)\"?'?`?|i", $string, $matches); $ret[$type] = $matches[1]; # Make all URLS literal (full path) for($i = 0; $i < count($ret['href']); $i++) { if(! preg_match( '/^(http|https):\/\//i' , $ret['href'][$i])) $ret['href'][$i] = getPath($url)."/".$ret['href'][$i]; $ret['href'][$i] = cleanPath($ret['href'][$i]); } return $ret; } function addUrls($urls) { if(is_array($urls)) for($i=0; $i < count($urls['href']); $i++) { $skip = 0; # Cycle through to make sure url is unique for($x=0; $x < count($GLOBALS['urls']); $x++) if($GLOBALS['urls'][$x] == $urls['href'][$i]) { $skip = 1; break; } # Check extension $extgood = 0; foreach($GLOBALS['extensions_ignore'] as $ext) if(!checkExt($urls['href'][$i], $ext)) $extgood = 1; # And finally make sure its in the current website if(! stristr($urls['href'][$i], $GLOBALS['siteurl'])) $skip = 1; if($skip == 0 && $extgood == 1) $GLOBALS['urls'][] = $urls['href'][$i]; } } function getNextUrl($oldurl) { if($oldurl == "") return $GLOBALS['urls'][0]; for($i=0; $i < count($GLOBALS['urls']); $i++) if($GLOBALS['urls'][$i] == $oldurl) if(isset($GLOBALS['urls'][($i+1)])) return $GLOBALS['urls'][($i+1)]; else return false; return false; } $urls = array($siteurl); #start to generate inline sitemap echo ''."\n".''."\n".''."\n"; # if possible the script will writte on website a copy of generated sitemap # start to writte generated sitemap # Make sure you have writte permission to file sitemap_temp.xml if($fp = fopen("sitemap_temp.xml", "w")) { $open_file = 'sucess'; fputs($fp, ''."\n".''."\n".''."\n"); } # Log 404 Error Page Not Found # Make sure you have writte access to file 404error.txt if($fp_err = fopen("404error.txt", "w")) $open_file_404 = 'sucess'; $turl = ""; # Cycle through tree and build a url list while($turl = getNextUrl($turl)) { # Extend script time limit set_time_limit(3000); # Read html file into memory if($html = file($turl)) { $html = stripslashes(implode($html)); echo ''."\n\t".''.googlesitemap_compatible($turl).''."\n"; if($lastmodification == true) echo "\t".''.date("Y-m-d").''."\n"; echo "\t".''.$frequency.''."\n\t".''.$priority.''."\n".''."\n"; # Get site urls from html and add new unique url to list if needed addUrls(getUrls($turl, $html)); #writte the same thing above on website if you have permission to writte if($open_file == 'sucess') { fputs($fp,''."\n\t".''.googlesitemap_compatible($turl).''."\n"); if($lastmodification == true) fputs($fp,"\t".''.date("Y-m-d").''."\n"); fputs($fp,"\t".''.$frequency.''."\n\t".''.$priority.''."\n".''."\n"); } } else { # check if 404error.txt was sucsefuly opened if($open_file_404 == 'sucess') fputs($fp_err, $turl."\n"); } } echo ''; if($open_file == 'sucess') { fputs($fp, ''); fclose($fp); # Make sure you have writte access to file sitemap_OK.xml # To track evolution of your google sitemap you can replace sitemap_temp.xml with "sitemap_OK_".date(d-m-y).".xml" copy('sitemap_temp.xml','sitemap_OK.xml'); if($open_file_404 == 'sucess') fclose($fp_err); } print_r($GLOBALS['urls']); ?>