Beispiel #1
0
 /**
 		Parse each URL recursively and generate sitemap
 			@param $_url string
 			@public
 	**/
 public static function sitemap($_url = '/')
 {
     $_map =& F3::$global['SITEMAP'];
     if (array_key_exists($_url, $_map) && $_map[$_url]['status'] !== NULL) {
         // Already crawled
         return;
     }
     preg_match('/^http[s]*:\\/\\/([^\\/$]+)/', $_url, $_host);
     if (!empty($_host) && $_host[1] != $_SERVER['SERVER_NAME']) {
         // Remote URL
         $_map[$_url]['status'] = FALSE;
         return;
     }
     F3::$global['QUIET'] = TRUE;
     F3::mock('GET ' . $_url);
     F3::run();
     // Check if an error occurred or no HTTP response
     if (F3::$global['ERROR'] || !F3::$global['RESPONSE']) {
         $_map[$_url]['status'] = FALSE;
         // Reset error flag for next page
         unset(F3::$global['ERROR']);
         return;
     }
     $_doc = new XMLTree('1.0', F3::$global['ENCODING']);
     if ($_doc->loadHTML(F3::$global['RESPONSE'])) {
         // Valid HTML; add to sitemap
         if (!$_map[$_url]['level']) {
             // Web root
             $_map[$_url]['level'] = 0;
         }
         $_map[$_url]['status'] = TRUE;
         $_map[$_url]['mod'] = time();
         $_map[$_url]['freq'] = 0;
         // Cached page
         $_hash = 'url.' . F3::hashCode('GET ' . $_url);
         $_cached = Cache::cached($_hash);
         if ($_cached) {
             $_map[$_url]['mod'] = $_cached['time'];
             $_map[$_url]['freq'] = $_SERVER['REQUEST_TTL'];
         }
         // Parse all links
         $_links = $_doc->getElementsByTagName('a');
         foreach ($_links as $_link) {
             $_ref = $_link->getAttribute('href');
             $_rel = $_link->getAttribute('rel');
             if (!$_ref || $_rel && preg_match('/nofollow/', $_rel)) {
                 // Don't crawl this link!
                 continue;
             }
             if (!array_key_exists($_ref, $_map)) {
                 $_map[$_ref] = array('level' => $_map[$_url]['level'] + 1, 'status' => NULL);
             }
         }
         // Parse each link
         array_walk(array_keys($_map), 'self::sitemap');
     }
     unset($_doc);
     if (!$_map[$_url]['level']) {
         // Finalize sitemap
         $_depth = 1;
         while ($_ref = current($_map)) {
             // Find depest level while iterating
             if (!$_ref['status']) {
                 // Remove remote URLs and pages with errors
                 unset($_map[key($_map)]);
             } else {
                 $_depth = max($_depth, $_ref['level'] + 1);
                 next($_map);
             }
         }
         // Create XML document
         $_xml = simplexml_load_string('<?xml version="1.0" encoding="' . F3::$global['ENCODING'] . '"?>' . '<urlset xmlns="' . 'http://www.sitemaps.org/schemas/sitemap/0.9' . '"/>');
         $_host = 'http://' . $_SERVER['SERVER_NAME'];
         foreach ($_map as $_key => $_ref) {
             // Add new URL
             $_item = $_xml->addChild('url');
             // Add URL elements
             $_item->addChild('loc', $_host . $_key);
             $_item->addChild('lastMod', date('c', $_ref['mod']));
             $_item->addChild('changefreq', self::frequency($_ref['freq']));
             $_item->addChild('priority', sprintf('%1.1f', 1 - $_ref['level'] / $_depth));
         }
         // Send output
         F3::$global['QUIET'] = FALSE;
         if (PHP_SAPI != 'cli' && !headers_sent()) {
             header(F3::HTTP_Content . ': application/xhtml+xml; ' . 'charset=' . F3::$global['ENCODING']);
         }
         $_xml = dom_import_simplexml($_xml)->ownerDocument;
         $_xml->formatOutput = TRUE;
         echo $_xml->saveXML();
     }
 }