Miscellaneous Code Snippets / Scripts

Toby
2006-10-01
2013-04-18
  • Toby

    Toby - 2006-10-01

    Hello, everyone! This thread will provide a repository for various code snippets or scripts that don't have anything to do with any of the BibWorm applications as such but may deal with things that came up during development or work on the website. Perhaps there's something useful for someone...

     
    • Toby

      Toby - 2006-10-01

      For the time being this script is redundant for the BibWorm website since I moved my development blog to blogger.com. Maybe it's helpful for someone else.

      <?

      //*******************************************************************************
      //
      //    Blog-to-RSS-2.0
      //   
      //    creates an RSS 2.0 feed of the latest 10 items posted in the
      //    specified blog.
      //
      //    written by tobyhist for The BibWorm Project
      //    This script is licensed under the Apache License 2.0
      //       You can read the terms of this agreement at http://www.apache.org/licenses/LICENSE-2.0.html
      //       By using or modyfiying this script you agree to the terms of this license agreement.
      //
      //*******************************************************************************

      header('Content-type: application/rss+xml');

      // Output the XML headers
      $output = "<?xml version='1.0' ?>\n";

      // define global variables
      $blogsTable = "blogs"        // contains the name of the table listing all blogs in the database
      $blogTable = "";                // the table for the blog we require
      $blogger = "";                    // the blogger's name
      $bloggerEmail = "";            // the blogger's email
      $blogURL = "";                 // the blog URL
      $title = "";                          // blog title
             
          // retrieve meta-information from the blog overview table
          if (isset($_GET["blogid"]))
              {
                  $blogID = $_GET["blogid"];
                 
                  if (isset($_GET["blogstable"])
                  {
                      $blogsTable = $_GET["blogstable"];
                  }
                 
             
                  $link = mysql_connect('host', 'username', 'password') or die('Error: ' .mysql_error());
                  @ mysql_select_db('dbname') or die('Could not select db.');

                  $result = mysql_query("SELECT * FROM $blogsTable WHERE id=$blogID;") or die('Query failed: ' .mysql_error());
             
                  if ($line = mysql_fetch_array($result, MYSQL_ASSOC))
                  {
                          $blogTable = $line["blog_table"];
                          $blogger = htmlspecialchars($line["blogger"]);
                          $bloggerEmail = htmlspecialchars($line["blogger_email"]);
                          $blogURL = htmlspecialchars($line["url"]);
                          $title = htmlspecialchars($line["title"]);
                  }
                 
                  // Free resultset
                  mysql_free_result($result);
         
                  // Closing connection
                  mysql_close($link);
              }
             
             
              // set other variables
              $currentYear = date("Y", time());
              $lastBuildDate = gmdate("r", time());

      $output .= "<rss version=\&quot;2.0\&quot;>\n";
      $output .= "<channel>\n";
      $output .= "<title>$title</title>\n";
      $output .= "<link>$blogURL</link>\n";
      $output .= "<description>This feed supplies the latest 10 entries from $title</description>\n";
      $output .= "<language>en-UK</language>\n";
      $output .= "<copyright>(c) $currentYear $blogger</copyright>\n";
      $output .= "<lastBuildDate>$lastBuildDate</lastBuildDate>\n";
      $output .= "<generator>BibWorm Project's blog-to-RSS-2.0 script</generator>\n";
             
                  // read the blogitems
                  $link = mysql_connect('host', 'username', 'password') or die('Error: ' .mysql_error());
                  @ mysql_select_db('dbname') or die('Could not select db.');
             
                  $result = mysql_query("SELECT * FROM $blogTable ORDER BY created DESC, id DESC;") or die('Query failed: ' .mysql_error());
                 
                  // adds the 10 latest entries to the feed
                  for ($counter = 0; ($line = mysql_fetch_array($result, MYSQL_ASSOC)) && ($counter < 10); $counter++)
                  {
                      $output .= "<item>\n";
                      $output .= "<title>";
                      $output .= htmlspecialchars($line["title"]);
                      $output .= "</title>\n";
                      $output .= "<description>\n";
                      $output .= "<![CDATA[\n";
                      $output .= $line['text'];
                      $output .= "\n]]>\n";
                      $output .= "</description>\n";
                      $output .= "<author>";
                      $output .= htmlspecialchars($bloggerEmail);
                      $output .= "</author>\n";
                      $output .= "<pubDate>";
                      // format 'created' as RFC date
                      $output .= htmlspecialchars(gmdate("r", strtotime($line['created'])));
                      $output .= "</pubDate>\n";
                      $output .= "<guid>";
                      $output .= htmlspecialchars("some URL");
                      $output .= htmlspecialchars($line['id']);
                      $output .= "</guid>\n";
                      $output .= "</item>\n";
                     
                  }
                 
      $output .= "</channel>\n";
      $output .= "</rss>\n";

      // Clear up the resultset
      mysql_free_result($result);

      // And close the database.
      mysql_close($link);

      echo $output;

      ?>

       

Log in to post a comment.