<?

// number of entries in the XML feed
$entries 10;

// timestamp for this action
$timestamp gmdate("Y-m-d\TH:i:s\Z");

// construct the file name
// username+user_id
// compact the username first so no spaces in filename
$compact_un str_replace(" """$authenticatedUser);
$filename $compact_un ".xml";

// open the file
// we're overwriting here to limit the number of entries
// otherwise it'd be a constantly growing file

$fh fopen("./atom/$filename""w");

// write the common 'feed' level lines to the xml file

fwrite($fh"<?xml version='1.0' encoding='utf-8'?>\n");
fwrite($fh"<feed version='0.3' xmlns='http://purl.org/atom/ns#'>\n");

// db connection should already be open

// construct and run query to get blog data (name, url, etc)
$query "SELECT blogname, url, password FROM users WHERE username = \"$authenticatedUser\"";
$result mysql_query($query$connection) or die("query failed");
while (
$row = @ mysql_fetch_array($result)) {
$blogname $row["blogname"];
$url $row["url"];
$password $row["password"];
}

// write the blog-specific "feed" level xml

fwrite($fh"\t<title>$blogname" " - Sideblog</title>\n");
fwrite($fh"\t<link rel='alternate' type='text/html' href='$url'/>\n");
fwrite($fh"\t<modified>$timestamp</modified>\n");
fwrite($fh"\t<author>\n");
fwrite($fh"\t\t<name>$authenticatedUser</name>\n");
fwrite($fh"\t</author>\n");

// query to get entries

$query2 "select title, date_format(datetime, '%Y-%m-%dT%H:%i:%s'), content, post_id, date_format(date_add(datetime, interval 5 hour), '%Y-%m-%dT%H:%i:%sZ') from posts where user_id='$user_id' order by post_id desc limit $entries";
$result2 mysql_query($query2) or die("query failed " mysql_error());
$num_of_rows mysql_num_rows($result2);

// loop through the results set, using data to write
// the "entry" level lines in the xml file

for ($i=0$i<$num_of_rows$i++) {
      
$row mysql_fetch_array($result2);
    
fwrite($fh"\t<entry>\n");
    
// check to see if this blogger is using titles
    // if not, we need some character to link the permalink to
        
$content_length_title strlen($row[0]);
        if (
$content_length_title == || $content_length_title == "") {
                
$row[0] = "#";
        }
    
fwrite($fh"\t\t<title>$row[0]</title>\n");
    
fwrite($fh"\t\t<link rel='alternate' type='text/html' href='http://www.sideblog.com/prem/archive.php?user_id=$user_id&auth=$password'/>\n");
    
// construct the globally unique post identifier
    
$id "tag:sideblog.com," $user_id "." $row[3];
    
fwrite($fh"\t\t<id>$id</id>\n");
    
fwrite($fh"\t\t<issued>$row[1]</issued>\n");
    
fwrite($fh"\t\t<modified>$row[4]</modified>\n");
    
// rtrim the content field
    
$row[2] = rtrim($row[2]);
    
fwrite($fh"\t\t<content>$row[2]</content>\n");
    
fwrite($fh"\t</entry>\n");

}

// write the closing "feed" tag

fwrite($fh"</feed>\n");

// close the file

fclose($fh);

// end