Skip to content

Instantly share code, notes, and snippets.

@kode54
Last active January 26, 2016 06:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kode54/6cbcf2dc4f5c018be16b to your computer and use it in GitHub Desktop.
Save kode54/6cbcf2dc4f5c018be16b to your computer and use it in GitHub Desktop.
A little script to generate Google site maps for an ElkArte forum.
<?php
// Put the path to your forum install, with a trailing slash.
// This is the on-disk directory where index.php for your board
// lives.
$forum_path = "/var/www/html/forum/";
// Number of posts to show in a given syndication run
$num_posts = 300;
// We need our configuration!
require $forum_path."Settings.php";
$dsns = $db_type . ':dbname=' . $db_name . ';host=' . $db_server . (!empty($db_port) ? ';port=' . $db_port : '');
$db = new PDO($dsns, $db_user, $db_passwd);
$db->exec('SET NAMES utf8');
$tblprefix = $db_prefix;
$whereclause = "id_board <> 0";
$query = 'SELECT * FROM '.$tblprefix.'topics WHERE '.$whereclause.' ORDER BY id_last_msg DESC LIMIT 1';
$result = $db->query($query);
$row = $result->fetch(PDO::FETCH_ASSOC);
$query = 'SELECT poster_time FROM '.$tblprefix.'messages WHERE id_msg = '.$row['id_last_msg'];
$result = $db->query($query);
$row = $result->fetch(PDO::FETCH_ASSOC);
// ...and get a timestamp for it, to use as our
// RSS 2.0 lastBuildDate
$latestpoststamp = $row['poster_time'];
$latestpost = gmdate('D, d M Y H:i:s \G\M\T', $latestpoststamp);
// Build some headers
$xmlheader = "<?xml version='1.0' encoding='UTF-8'?>";
$channelheader = '
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
// Now we grab a list of the forums, in order to get
// names and permissions from them.
$query = 'SELECT * FROM '.$tblprefix.'boards ORDER BY id_board';
$result = $db->query($query);
$forums = array();
$forumperms = array();
while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
$perms = explode(',', $row['member_groups']);
$forums[$row['id_board']] = $row['name'];
$forumperms[$row['id_board']] = $perms;
}
$read_group = "Guests";
// And then we go find user 0, namely the Guest user,
// and get their group permissions for later use.
$query = 'SELECT * FROM '.$tblprefix.'membergroups WHERE group_name=\''.$read_group.'\'';
$result = $db->query($query);
$anongroup = -1;
if ($row = $result->fetch(PDO::FETCH_ASSOC))
{
$anongroup = $row['id_group'];
}
$whereclause = "";
// Now we query for posts! Yay!
$query = 'SELECT t.id_topic, t.id_board, t.id_last_msg, t.num_replies, mf.subject, ml.poster_time FROM '.$tblprefix.'topics AS t INNER JOIN '.$tblprefix.'messages AS mf ON mf.id_msg = id_first_msg INNER JOIN '.$tblprefix.'messages AS ml ON ml.id_msg = id_last_msg '.$whereclause.' ORDER BY id_last_msg DESC LIMIT '.$num_posts;
$result = $db->query($query);
$itembodies = '';
// ...with our posts, we iterate across them.
while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
// We build a little array of permissions for the forum this
// post is in...
$forum_id = 0;
$forum_id = $row['id_board'];
$title = htmlentities($row['subject'], ENT_QUOTES, "UTF-8");
$readgroups = $forumperms[$forum_id];
// ...and check whether the Guest can read in this forum.
if (in_array($anongroup,$readgroups)) {
$lastpage = (intval(($row['num_replies'] + 1) / 25)) * 25;
$postdate = date(DATE_W3C, $row['poster_time']);
if ($lastpage == 0) {
$posturl = $boardurl.
'/index.php/topic,'.$row['id_topic'].'.0.html';
} else {
$posturl = $boardurl.
'/index.php/topic,'.$row['id_topic'].'.'.$lastpage.'.html';
}
$posturl = htmlentities($posturl, ENT_QUOTES, "UTF-8");
// Build the actual RSS text for the item...
$thisitem = "
<url>
<loc>{$posturl}</loc>
<lastmod>{$postdate}</lastmod>
</url>";
// ..and concatenate it onto the item bodies.
$itembodies .= $thisitem;
}
}
$channelclose = '
</urlset>
';
// Now we build an actual RSS feed out of all the pieces we've
// collected.
$rssbody = $xmlheader.
$channelheader.
$itembodies.
$channelclose;
// Look, ma, honoring HTTP headers!
$rsslength = strlen($rssbody);
header("Content-Type: text/xml; charset=utf-8");
header("Content-Length: ".$rsslength);
header("Cache-Control: no-cache");
header("Last-Modified: ".$latestpost);
echo $rssbody;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment