baseUrl = strtolower($baseUrl); $this->baseUrlLen = strlen($this->baseUrl); // cycle saver } /** * addUrl * * Adds a URL to the sitemap. All data is optional, except for the actual * URL. The URL can include the domain info, but if it doesn't then the * $pathOnly parameter should be set to true. The last modified timestamp * can be either a date and time, or a date only. * * @access public * @param string $url The URL to add. The URL should not be escaped. * @param bool $pathOnly Set to true if the URL contains only a path, * or leave at false if it contains the domain * @param int $lastModTs The Unix/Epoch timestamp since the doc was modified. * Set this to null to not include this parameter. * @param bool $lastModTsDateOnly If $lastModTs is specified, then setting * this to true means the timestamp date will * only be output, not the time. * @param string $changeFreq The frequency this URL is changed. Must be a valid * value from class $changeFreqs, otherwise ignored. * @param float $priority The priority of this URL relative to other URLs * in the sitemap. Must be between $priorityMin and * $priorityMax inclusive, and a multiple of $priorityStep * @return bool True if URL was added, false if not (e.g. if didn't match $baseUrl) */ function addUrl($url, $pathOnly = false, $lastModTs = null, $lastModTsDateOnly = false, $changeFreq = null, $priority = null) { $this->errorMsg = ''; if (count($this->urls) >= $this->maxURLs) { $this->errorMsg = "Only ".$this->maxURLs . " urls are allowed within a Google Sitemaps file."; return false; } if ($pathOnly) { $url = $this->baseUrl . $url; $url = substr($url, 0, $this->baseUrlLen-1).preg_replace('|/+|', '/', substr($url, $this->baseUrlLen -1)); // replace double slashes with a single slash } else if ($this->baseUrlLen > 0) { // check if the added URL matches the baseUrl if ($this->baseUrl != strtolower(substr($url, 0, $this->baseUrlLen))) { $this->errorMsg = 'The following url does not match the base url ('.$this->baseUrl.'): ' . $url . '!'; return false; } } $data = array('url' => $url); if (($lastModTs != '') && !is_null($lastModTs) && is_numeric($lastModTs)) { $data['lastmod'] = (int) $lastModTs; $data['lastmod_dateonly'] = (bool) $lastModTsDateOnly; } elseif(is_string($lastModTs)) { // ts could be a preformated string if ($lastModTs != '') { $data['lastmod'] = $lastModTs; $data['lastmod_dateonly'] = false; } } if (!is_null($changeFreq) && in_array($changeFreq, $this->changeFreqs)) { $data['changefreq'] = $changeFreq; } if (!is_null($priority) && $priority != '') { $priority = (float) $priority; // ensure it's between the valid range, else ignore it if ($priority >= $this->priorityMin && $priority <= $this->priorityMax) { // ok it's valid, now normalize the value $tmp = floor($priority / $this->priorityStep); $tmp = $priority - $tmp * $this->priorityStep; $priority -= $tmp; $data['priority'] = $priority; } } $this->urls[] = $data; } /** * output * * Output the generated XML. The data can either be returned or output * directly. If it is not being returned (i.e. being output directly * then you can optionally output the HTTP headers for the data * * @access public * @param bool $return Optional. True to return the XML, false to * output it directly. If this is true, then * the $sendHeaders parameter is ignored * @param bool $compress Optional. True to compress the data using gzip * @param bool $sendHeaders Optional. True to send HTTP headers. This * parameter is only used if $return is true * @return mixed void is $return is false, XML string if $return * true and $compress false, gzip binary data * if $return true and $compress true */ function output($return = true, $compress = false, $sendHeaders = false) { $xml = $this->generateXml(); if ($compress) $compress = function_exists($this->compressFunc); if ($compress) $xml = $this->compress($xml); if ($return) { return $xml; } else { if ($sendHeaders) { if ($compress) $mime = 'application/x-gzip'; else $mime = 'text/xml'; header('Content-type: ' . $mime); header('Content-length: ' . strlen($xml)); } echo $xml; } } /** * generateXml * * Builds the sitemap XML from all the added URLs * * @access public * @return string The generated XML */ function generateXml() { $ret = array(); $ret[] = sprintf('', $this->xmlEncoding); $ret[] = sprintf(''); $ret[] = sprintf('', PSNG_VERSION); $ret[] = sprintf('', date($this->lastModDateTime).substr(date("O"),0,3).":".substr(date("O"),3)); foreach ($this->urls as $url) { $ret[] = ''; $ret[] = sprintf('%s', $this->xmlEscape($url['url'])); if (isset($url['lastmod'])) { if (is_numeric($url['lastmod'])) { $ret[] = sprintf('%s', $url['lastmod_dateonly'] ? date($this->lastModDate, $url['lastmod']) : date($this->lastModDateTime, $url['lastmod']). substr(date("O", $url['lastmod']),0,3) . ":" . substr(date("O",$url['lastmod']),3)); } elseif (is_string($url['lastmod'])) { $ret[] = sprintf('%s',$url['lastmod']); } } if (isset($url['changefreq'])) { $ret[] = sprintf('%s', $this->xmlEscape($url['changefreq'])); } if (isset($url['priority'])) { $priorityStr = sprintf('%s', $this->priorityFormat); $ret[] = sprintf($priorityStr, $url['priority']); } $ret[] = ''; } $ret[] = ''; return join("\n", $ret); } /** * compress * * Compresses a text string with GZIP, and returns the compressed data * * @access public * @param string The string to compress * @return The compressed gzip data, or null if the compression callback is not found */ function compress($string) { $func = $this->compressFunc; if (strlen($func) == 0 || !function_exists($func)) return null; return $func($string); } /** * xmlEscape * * Escapes a string to be used as XML cdata. Borrowed from PHP * manual comments on htmlentities() * * @see http://www.php.net/htmlentities * * @param string $str The string to escape * @return string The escaped string */ function xmlEscape($str) { static $trans; if (!isset($trans)) { $trans = get_html_translation_table(HTML_ENTITIES, ENT_QUOTES); foreach ($trans as $key => $value) $trans[$key] = '&#'.ord($key).';'; // dont translate the '&' in case it is part of &xxx; $trans[chr(38)] = '&'; } return preg_replace("/&(?![A-Za-z]{0,4}\w{2,3};|#[0-9]{2,4};)/","&" , strtr($str, $trans)); } } ?>