if (CAN_POST && AUTH && TITLE && TEXT) { //the file on disk is a simplified version of the title: $translit = preg_replace(array('/[^_a-z0-9-]/i', '/_{2,}/'), '_', str_replace(array("'", "`", "^", "~", "'", '"'), '', strtolower(iconv('UTF-8', 'US-ASCII//IGNORE//TRANSLIT', TITLE)))); //old iconv versions and certain inputs may cause a nullstring. don't allow a blank filename if (!$translit) { $translit = '_'; } //if a thread already exsits with that name, append a number until an available filename is found $c = 0; do { $file = $translit . ($c++ ? '_' . ($c - 1) : ''); } while (file_exists("{$file}.rss")); //write out the new thread as an RSS file: $rss = new DOMTemplate(FORUM_ROOT . DIRECTORY_SEPARATOR . 'lib' . DIRECTORY_SEPARATOR . 'rss-template.xml'); $rss->set(array('/rss/channel/title' => TITLE, '/rss/channel/link' => FORUM_URL . PATH_URL . $file, '/rss/channel/item/title' => TITLE, '/rss/channel/item/link' => FORUM_URL . PATH_URL . "{$file}#" . base_convert(microtime(), 10, 36), '/rss/channel/item/author' => NAME, '/rss/channel/item/pubDate' => gmdate('r'), '/rss/channel/item/description' => formatText(TEXT)))->remove('//category'); file_put_contents("{$file}.rss", $rss->html()) or die("Failed to save thread. Folder permissions may be incorrect."); //regenerate the folder's RSS file indexRSS(); //redirect to newley created thread header('Location: ' . FORUM_URL . PATH_URL . $file, true, 303); exit; } /* ====================================================================================================================== template the page ====================================================================================================================== */ //first load the list of threads in the forum so that we can determine the number of pages and validate the page number, //the thread list won't be used until further down after templating begins if ($threads = preg_grep('/\\.rss$/', scandir('.'))) { //order by last modified date array_multisort(array_map('filemtime', $threads), SORT_DESC, $threads); //get sticky list, trimming any files that no longer exist
function indexRSS() { /* create an RSS feed -------------------------------------------------------------------------------------------------------------- */ $rss = new DOMTemplate(file_get_contents(FORUM_LIB . 'rss-template.xml')); $rss->set(array('/rss/channel/title' => FORUM_NAME . (PATH ? str_replace('/', ' / ', PATH) : ''), '/rss/channel/link' => FORUM_URL . url('index', PATH_URL)))->remove('/rss/channel/category'); //get list of threads, sort by date; most recently modified first $threads = preg_grep('/\\.rss$/', scandir('.')); array_multisort(array_map('filemtime', $threads), SORT_DESC, $threads); $items = $rss->repeat('/rss/channel/item'); //get the last post made in each thread as an RSS item foreach (array_slice($threads, 0, FORUM_THREADS) as $thread) { if ($xml = @simplexml_load_file($thread)) { //if the RSS feed is valid if ($item = @$xml->channel->item[0]) { //if the feed has any items $items->set(array('./title' => $item->title, './link' => $item->link, './author' => $item->author, './pubDate' => gmdate('r', strtotime($item->pubDate)), './description' => $item->description))->remove(array('./category[.="deleted"]' => !$item->xpath('category[.="deleted"]')))->next(); } } } file_put_contents('index.xml', $rss->html()); /* sitemap -------------------------------------------------------------------------------------------------------------- */ //we’re going to use the RSS files as sitemaps chdir(FORUM_ROOT); //get list of sub-forums and include the root too $folders = array('') + array_filter(preg_grep('/^(\\.|users$|themes$|lib$)/', scandir(FORUM_ROOT . DIRECTORY_SEPARATOR), PREG_GREP_INVERT), 'is_dir'); //start the XML file. this template has an XMLNS, so we have to prefix all our XPath queries :( $xml = new DOMTemplate(file_get_contents(FORUM_LIB . 'sitemap-template.xml'), array('x' => 'http://www.sitemaps.org/schemas/sitemap/0.9')); //generate a sitemap index file, to point to each index RSS file in the forum: //<https://www.google.com/support/webmasters/bin/answer.py?answer=71453> $sitemap = $xml->repeat('//x:sitemap'); foreach ($folders as $folder) { //get the time of the latest item in the RSS feed //(the RSS feed may be missing as they are not generated in new folders until something is posted) if (@($rss = simplexml_load_file(FORUM_ROOT . ($folder ? DIRECTORY_SEPARATOR . $folder : '') . DIRECTORY_SEPARATOR . 'index.xml'))) { //if you delete the last thread in a folder, there won’t be anything in the RSS index file! if (@$rss->channel->item[0]) { $sitemap->set(array('./x:loc' => FORUM_URL . ($folder ? safeURL("/{$folder}", false) : '') . '/index.xml', './x:lastmod' => gmdate('r', strtotime($rss->channel->item[0]->pubDate))))->next(); } } } file_put_contents(FORUM_ROOT . DIRECTORY_SEPARATOR . 'sitemap.xml', $xml->html()); //you saw nothing, right? clearstatcache(); }
$page = (count($thread) + 1) % FORUM_POSTS == 1 ? floor((count($thread) + 1) / FORUM_POSTS) : ceil((count($thread) + 1) / FORUM_POSTS); $url = FORUM_URL . url('thread', PATH_URL, $FILE, $page) . '#' . base_convert(microtime(), 10, 36); //re-template the whole thread: $rss = new DOMTemplate(file_get_contents(FORUM_LIB . 'rss-template.xml')); $rss->set(array('/rss/channel/title' => $xml->channel->title, '/rss/channel/link' => FORUM_URL . url('thread', PATH_URL, $FILE)))->remove(array('/rss/channel/category' => !$xml->channel->xpath('category[.="locked"]'))); //template the new reply first $items = $rss->repeat('/rss/channel/item'); $items->set(array('./title' => sprintf(THEME_RE, count($xml->channel->item), $xml->channel->title), './link' => $url, './author' => NAME, './pubDate' => gmdate('r'), './description' => formatText(TEXT, $xml)))->remove('./category')->next(); //copy the remaining replies across foreach ($xml->channel->item as $item) { $items->set(array('./title' => $item->title, './link' => $item->link, './author' => $item->author, './pubDate' => $item->pubDate, './description' => $item->description))->remove(array('./category' => !$item->xpath('./category')))->next(); } //write the file: first move the write-head to 0, remove the file's contents, and then write new one rewind($f); ftruncate($f, 0); fwrite($f, $rss->html()); } else { //if a double-post, link back to the previous post $url = $xml->channel->item[0]->link; } //close the lock / file flock($f, LOCK_UN); fclose($f); //regenerate the forum / sub-forums's RSS file indexRSS(); //refresh page to see the new post added header("Location: {$url}", true, 303); exit; } /* ====================================================================================================================== template thread
====================================================================================================================== */ //has the user submitted a new thread? //(`AUTH` will be true if username and password submitted and correct, `TITLE` and `TEXT` are checked to not be blank) if (CAN_POST && AUTH && TITLE && TEXT) { //the file on disk is a simplified version of the title; see 'lib/functions.php' for `safeTransliterate` $translit = safeTransliterate(TITLE); //if a thread already exsits with that name, append a number until an available filename is found. //we also check for directories with the same name so as to avoid problematic Apache behaviour $c = 0; do { $file = $translit . ($c++ ? '_' . ($c - 1) : ''); } while (file_exists("{$file}") || file_exists("{$file}.rss")); //write out the new thread as an RSS file: $rss = new DOMTemplate(file_get_contents(FORUM_LIB . 'rss-template.xml')); $rss->set(array('/rss/channel/title' => TITLE, '/rss/channel/link' => FORUM_URL . url('thread', PATH_URL, $file), '/rss/channel/item/title' => TITLE, '/rss/channel/item/link' => FORUM_URL . url('thread', PATH_URL, $file) . '#' . base_convert(microtime(), 10, 36), '/rss/channel/item/author' => NAME, '/rss/channel/item/pubDate' => gmdate('r'), '/rss/channel/item/description' => formatText(TEXT)))->remove('//category'); file_put_contents("{$file}.rss", $rss->html()) or (require FORUM_LIB . 'error_permissions.php'); //regenerate the folder's RSS file indexRSS(); //redirect to newley created thread header('Location: ' . FORUM_URL . url('thread', PATH_URL, $file), true, 303); exit; } /* ====================================================================================================================== template the page ====================================================================================================================== */ //first load the list of threads in the forum so that we can determine the number of pages and validate the page number, //the thread list won't be used until further down after templating begins if ($threads = preg_grep('/\\.rss$/', scandir('.'))) { //order by last modified date array_multisort(array_map('filemtime', $threads), SORT_DESC, $threads); //get sticky list, trimming any files that no longer exist