<?php
//============================================================================
// Atom Class. PHP Class to create Atom feed file from database
// Version: 0.1b
// Copyright (c) Tomas Kavalek, kavalek.net 2005
// All rights reserved
// 
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
// 
// Copy of GNU Lesser General Public License at: http://www.gnu.org/copyleft/lesser.txt
//
// For support contact support@kavalek.net
//============================================================================
class Atom {
    var 
$atom_mysql_link;       // Database connection ID
    
var $atom_mysql_table;      // Table name for feed reading
    
var $atom_mysql_title;      // Title column in table
    
var $atom_mysql_body;       // Body column in table
    
var $atom_mysql_published;  // Time and date of published in table
    
var $atom_mysql_updated;    // Time and date of updated in table
    
var $atom_time_zone;        // Atom feed time zone
    
var $atom_code_page;        // Atom feed document coding
    
var $atom_language;         // Atom feed document language
    
var $atom_version;          // Atom feed version (default version is v0.3)
    
var $atom_id;               // Atom feed identification
    
var $atom_title;            // Title of Atom feed
    
var $atom_archive;          // Path to archive Atom feed data on your server
    
var $atom_last_modified;    // Last modified of Atom feed
    
var $atom_author;           // Atom feed author name
    
var $atom_idtag;            // Atom feed items identification
    
var $atom_home;             // Atom feed data home
    
var $atom_xsl;              // Atom feed XSL file (style formating)
    
var $atom_output;           // Atom feed file output (filename)
    
var $atom_content;          // Atom feed content
    
    //------------------------------------------------------------------------

   /** 
    * Constructor Create an Atom object.
    *
    * @param version Version of Atom feed.
    * @return null
    */
    
function Atom($version "v0.3")  {
      
register_shutdown_function(array(&$this'_Atom'));
      
$this->atom_version $version;
      
$this->atom_content "";
      
$this->atom_time_zone "+01:00";
      
$this->atom_code_page "iso-8859-2";
      
$this->atom_language "cs-cz";
      
$this->atom_mysql_table "news";
      
$this->atom_mysql_title "news_title";
      
$this->atom_mysql_body "news_body";
      
$this->atom_mysql_published "news_published";
      
$this->atom_mysql_updated "news_updated";
    }

    
//------------------------------------------------------------------------
   
   /** 
    * Set Atom feed MySQL properties.
    *
    * @param table_name Atom feed data table name.
    * @param title_column Atom feed title column name.
    * @param body_column Atom feed body column name.
    * @param published_column Atom feed published date column name.
    * @param updated_column Atom feed updated date column name.
    * @return nothing
    */
    
function set_mysql($table_name$title_column$body_column$published_column$updated_column) {
      
$this->atom_mysql_table $table_name;
      
$this->atom_mysql_title $title_column;
      
$this->atom_mysql_body $body_column;
      
$this->atom_mysql_published $published_column;
      
$this->atom_mysql_updated $updated_column;
    }

    
//------------------------------------------------------------------------
   
   /** 
    * Set Atom feed identification
    *
    * @param id Atom feed identification.
    * @return nothing
    */
    
function set_id($id) {
      
$this->atom_id $id;
    }

    
//------------------------------------------------------------------------
   
   /** 
    * Set Atom feed title
    *
    * @param title Atom feed title.
    * @return nothing
    */
    
function set_title($title) {
      
$this->atom_title $title;
    }

    
//------------------------------------------------------------------------
   
   /** 
    * Set Atom feed time zone
    *
    * @param time_zone Time zone - example: +01:00
    * @return nothing
    */
    
function set_time_zone($time_zone) {
      
$this->atom_time_zone $time_zone;
    }
    
    
//------------------------------------------------------------------------
   
   /** 
    * Set Atom feed document code page
    *
    * @param code_page Code page - example: iso-8859-2
    * @return nothing
    */
    
function set_code_page($code_page) {
      
$this->atom_code_page $code_page;
    }

    
//------------------------------------------------------------------------
   
   /** 
    * Set Atom feed document language
    *
    * @param language Language - example: cs-cz
    * @return nothing
    */
    
function set_language($language) {
      
$this->atom_language $language;
    }

    
//------------------------------------------------------------------------
   
   /** 
    * Set Atom feed data archive location. Etc. News archive or news history.
    *
    * @param url Location of data included in feed.
    * @return nothing
    */
    
function set_archive($url) {
      
$this->atom_archive $url;
    }

    
//------------------------------------------------------------------------
   
   /** 
    * Set Atom feed owner - author.
    *
    * @param author Author name.
    * @return nothing
    */
    
function set_author($author) {
      
$this->atom_author $author;
    }

    
//------------------------------------------------------------------------
   
   /** 
    * Set Atom feed identification tag for items.
    *
    * @param id Identification tag.
    * @return nothing
    */
    
function set_idtag($id) {
      
$this->atom_idtag $id;
    }

    
//------------------------------------------------------------------------
   
   /** 
    * Set Atom feed data home - server.
    *
    * @param url Atom feed data home.
    * @return nothing
    */
    
function set_home($url) {
      
$this->atom_home $url;
    }
    
    
//------------------------------------------------------------------------
   
   /** 
    * Set Atom feed data XSL style file.
    *
    * @param xsl Path to XSL style file.
    * @return nothing
    */
    
function set_xsl($xsl) {
      
$this->atom_xsl $xsl;
    }
    
    
//------------------------------------------------------------------------
   
   /** 
    * Set Atom feed data output file.
    *
    * @param filepath Atom feed data output filename.
    * @return nothing
    */
    
function set_output($filepath) {
      
$this->atom_output $filepath;
    }

    
//------------------------------------------------------------------------

   /** 
    * Make Atom feed output to file.
    * 
    * TODO: File exist check, setting permissions, ...
    *
    * @param null
    * @return nothing
    */
    
function create_file_output() {
      
$fw fopen($this->atom_output"w+");
      
fwrite($fw$this->atom_content);
      
fclose($fw);
    }
  
    
//------------------------------------------------------------------------
  
   /** 
    * Make Atom feed output to browser.
    * 
    * @param null
    * @return nothing
    */
    
function create_screen_output() {
      
printf($this->atom_content);
    }
    
    
//------------------------------------------------------------------------
  
   /** 
    * Connect to MySQL server and set character coding to LATIN2
    * 
    * @param db_conn Asociative array of data for connection.
    *                $db_conn = array(
    *                              "username" => "",
    *                              "password" => "",
    *                              "hostname" => "",
    *                              "dbname"   => "");
    * @return nothing
    */
    
function mysql_connect($db_conn) {
      if(!(
$link = @mysql_connect($db_conn["hostname"],
                                  
$db_conn["username"],
                                  
$db_conn["password"]))) {
        
printf("Chyba spojení s databází: %s | %s<br>\n"$db_conn["username"], $db_conn["hostname"]);
        
printf("Chyba: %s | %s<br>\n", @mysql_errno($link), @mysql_error($link));
        exit();
      }
      if(!@
mysql_select_db($db_conn["dbname"], $link)) {
        
printf("Chyba při výběru databáze: %s<br>\n"$db_conn["dbname"]);
        
printf("Chyba: %s | %s<br>\n", @mysql_errno($link), @mysql_error($link));
        exit();
      }
      
$this->atom_mysql_link $link;
      
$stmt "SET CHARACTER SET latin2;";
      
$this->mysql_query($stmt);
    }

    
//------------------------------------------------------------------------
  
   /** 
    * Create MySQL query.
    * 
    * @param stmt MySQL query.
    * @return result MySQL query result.
    */
    
function mysql_query($stmt) {
      if(!
$result = @mysql_query($stmt$this->atom_mysql_link)) {
        
printf("Chyba vykonávání příkazu: %s<br>\n"$stmt);
        
printf("Chyba: %s | %s<br>\n", @mysql_errno($this->atom_mysql_link), @mysql_error($this->atom_mysql_link));
        exit();
      }
      return 
$result;
    }
    
    
//------------------------------------------------------------------------
  
   /** 
    * Create Atom feed header accord version.
    * 
    * @param null
    * @return nothing
    */
    
function header() {
      
// Get last Atom feed update
      
$stmt "SELECT MAX(" $this->atom_mysql_updated ") AS last_updated FROM " $this->atom_mysql_table;
      
$result $this->mysql_query($stmt);
      
$row mysql_fetch_object($result); 
      
$this->atom_last_modified strftime("%Y-%m-%dT%H:%M:%S"strtotime($row->last_updated));
      
$this->atom_content .= sprintf("<?xml version=\"1.0\" encoding=\"%s\"?>\n"$this->atom_code_page);
      if(isset(
$this->atom_xsl)) $this->atom_content .= sprintf("<?xml-stylesheet type=\"text/xsl\" href=\"%s\"?>\n"$this->atom_xsl);
      
// Check Atom feed version
      
if($this->atom_version == "v1.0") {
      
// ATOM v1.0  
        
$this->atom_content .= sprintf("<feed xmlns=\"http://www.w3.org/2005/Atom\" xml:lang=\"%s\">\n"$this->atom_language);
        
$this->atom_content .= sprintf("<id>%s</id>\n"$this->atom_id);
      } else {
      
// ATOM v0.3
        
$this->atom_content .= sprintf("<feed version=\"0.3\" xmlns=\"http://purl.org/atom/ns#\" xml:lang=\"%s\">\n"$this->atom_language);      
      }
      
$this->atom_content .= sprintf("<title>%s</title>\n"$this->atom_title);
      
// Check Atom feed version
      
if($this->atom_version == "v1.0") {
      
// ATOM v1.0  
        
$this->atom_content .= sprintf("<link rel=\"self\" type=\"application/atom+xml\" href=\"%s\" />\n"$this->atom_id); 
      }
      
$this->atom_content .= sprintf("<link rel=\"alternate\" type=\"application/xhtml+xml\" href=\"%s\" />\n"$this->atom_archive);
      
// Check Atom feed version
      
if($this->atom_version == "v1.0") {
      
// ATOM v1.0  
        
$this->atom_content .= sprintf("<updated>%s%s</updated>\n"$this->atom_last_modified$this->atom_time_zone);
      } else {
      
// ATOM v0.3
        
$this->atom_content .= sprintf("<modified>%s%s</modified>\n"$this->atom_last_modified$this->atom_time_zone);
      }
        
$this->atom_content .= sprintf("<author>\n");
        
$this->atom_content .= sprintf(" <name>%s</name>\n"$this->atom_author);
        
$this->atom_content .= sprintf("</author>\n");
    }
    
    
//------------------------------------------------------------------------
  
   /** 
    * Create Atom feed footer.
    * 
    * @param null
    * @return nothing
    */
    
function footer() {
      
$this->atom_content .= sprintf("</feed>\n");
    }
   
//------------------------------------------------------------------------
  
   /** 
    * Create Atom feed body.
    * 
    * @param null
    * @return nothing
    */
    
function body() {
      
$stmt "SELECT * FROM " $this->atom_mysql_table " ORDER BY " $this->atom_mysql_updated ." DESC";
      
$mysql_title $this->atom_mysql_title;
      
$mysql_body $this->atom_mysql_body;
      
$mysql_published $this->atom_mysql_published;
      
$mysql_updated $this->atom_mysql_updated;
      if(
$result $this->mysql_query($stmt)) {
        while(
$row mysql_fetch_object($result)) {
          
$id_date_time strftime("%Y-%m-%d:%H/%M/%S"strtotime($row->$mysql_published));
          
$this->atom_content .= sprintf("<entry>\n");
          
$this->atom_content .= sprintf(" <id>%s,%s</id>\n"$this->atom_idtag$id_date_time);
          
$this->atom_content .= sprintf(" <title>%s</title>\n"$row->$mysql_title);
          
$this->atom_content .= sprintf(" <link rel=\"alternate\" type=\"application/xhtml+xml\" href=\"%s\" />\n"$this->atom_home);
        
// Check Atom feed version  
          
if($this->atom_version == "v1.0") {
          
// ATOM v1.0
            
$this->atom_content .= sprintf(" <published>%s%s</published>\n"strftime("%Y-%m-%dT%H:%M:%S"strtotime($row->$mysql_published)), $this->atom_time_zone);
            
$this->atom_content .= sprintf(" <updated>%s%s</updated>\n"strftime("%Y-%m-%dT%H:%M:%S"strtotime($row->$mysql_updated)), $this->atom_time_zone);            
          } else {
          
// ATOM v0.3
            
$this->atom_content .= sprintf(" <issued>%s%s</issued>\n"strftime("%Y-%m-%dT%H:%M:%S"strtotime($row->$mysql_published)), $this->atom_time_zone);
            
$this->atom_content .= sprintf(" <modified>%s%s</modified>\n"strftime("%Y-%m-%dT%H:%M:%S"strtotime($row->$mysql_updated)), $this->atom_time_zone);
          }
          
// Check Atom feed version  
          
if($this->atom_version == "v1.0") {
          
// ATOM v1.0 
            
$this->atom_content .= sprintf(" <content type=\"xhtml\">\n");
          } else {
          
// ATOM v0.3
            
$this->atom_content .= sprintf(" <content type=\"application/xhtml+xml\">\n");
          }
          
$this->atom_content .= sprintf("  <div xmlns=\"http://www.w3.org/1999/xhtml\">\n");
          
$this->atom_content .= sprintf("   %s\n"$row->$mysql_body);
          
$this->atom_content .= sprintf("  </div>\n");
          
$this->atom_content .= sprintf(" </content>\n"); 
          
$this->atom_content .= sprintf("</entry>\n");
        }
      }
    }
    
    
//------------------------------------------------------------------------

    /**
    * Descruction of Atom feed
    *
    * @param null
    * @return nothing
    */
    
function _Atom () {
      return;
    }
  }
?>