<?php

  
// MySQL data
  
$mysql_user ''// The MySQL user name
  
$mysql_host 'localehost'// The MySQL server, normally localehost
  
$mysql_password '';  // the MySQL password
  
$mysql_database ''// the MySQL database
  
$mysql_table 'feed'// The MySQL table.

  
$user '';  // User Name for the admin panel
  
$password '';  // Password for the admin panel

  
$title 'News';  // the title of the feed 
  
$description 'Update and News'// the feed description
  
  
$author ''// The name of the person that writes the feed
  
$author_email '';  // their email address
  
  
$script_location ''//The location of the feed script when views in a web browser
  
  
  // ** only edit the HTML after this point ** //
  
  // function that makes the GUIDs needed by the feeds.
  
function generate_GUID($base) {
        
$uuid md5($base);
    
$guid substr($uuid,0,8)."-".
        
substr($uuid,8,4)."-".
        
substr($uuid,12,4)."-".
        
substr($uuid,16,4)."-".
        
substr($uuid,20,12);
    return 
$guid;
  }

  
// add paragraphs and link breaks in at the needed points
  
function htmlParagrahs($text){
   
$text preg_replace("/\r\n/","\n",$text);
   
$text preg_replace("/\r/","\n",$text);
   if(!
preg_match("/^\n/",$text)){
    
$text "\n" $text;
   }
   if(!
preg_match("/\n$/",$text)){
    
$text .= "\n";
   }
   
$text preg_replace("/\n\s+\n/","\n\n",$text);
   
$text preg_replace("/\n([^\n]+)\n/","<p>$1</p>",$text);
   return 
preg_replace("/\n/","<br>",$text);
  }
  
  
// conect to the data base
  
$con mysql_connect($mysql_host,$mysql_user,$mysql_password) or die(mysql_error());
  
mysql_select_db($mysql_database$con) or die(mysql_error());
  
  
// to clear the data base completely uncomment this line.  Note once this is done you can't get the posts back
  // mysql_query("DROP TABLE `$mysql_table`", $con) or die(mysql_error());

 // ?admin has been set, so we shoe the admin panel not the feed
 
if(isset($_GET['admin'])){
  
?>
  <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
  <html>
  <head>
   <title>Admin News Feed Panel</title>
   <link rel="alternate" type="application/atom+xml" href="<?php echo $script_location?>">
   <style type="text/css">
    body {
     margin:10px auto;
     width:800px;
     border:1px solid #000;
     padding:0 15px 15px 15px;
    }
    h1 {
     font-size:1em;
     color:#fff;
     background:#000;
     margin:0;
     padding:5px;
     text-align:center;
    }
    h2 {
     font-size:1em;
     text-align:center;
    }
    #login {
     margin:20px;
     text-align:center;
    }
    table {
     width:100%;
     margin:10px 0;
     border-collapse:collapse;
    }
    td {
     border:1px solid #000;
    }
   </style>
  </head>
  <body>
   <h1>Admin News Feed Panel</h1>
  <?php
  session_start
();
  
// check the user name and password to see if the person should be logged in
  
if(($_REQUEST['password'] == $password && $_REQUEST['user'] == $user) || ($_SESSION['usr'] == $user && $_SESSION['psw'] == $password)){
   
$_SESSION['usr'] = $user;
   
$_SESSION['psw'] = $password;
   
// make things safe query wise
   
foreach($_GET as $key => $value){
    if(
is_string($value)){
     
$_GET[$key] = mysql_real_escape_string($value,$con);
    }
   }
   foreach(
$_POST as $key => $value){
    if(
is_string($value)){
     
$_POST[$key] = mysql_real_escape_string($value,$con);
    }
   }
   
   
// make the table if it does not exits
   
$sql "
    CREATE TABLE IF NOT EXISTS `$mysql_table` (
     `id` INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
     `posted` INT( 11 ) NOT NULL ,
     `updated` INT( 11 ) NOT NULL ,
     `content` TEXT NOT NULL ,
     `title` TINYTEXT NOT NULL ,
     `link` TINYTEXT NOT NULL ,
     `type` BOOL NOT NULL DEFAULT '1',
     `deleted` BOOL NOT NULL DEFAULT '0'
       )
   "
;
   
mysql_query($sql$con) or die(mysql_error());
   
   
// process the input when the user makes a post
   
if(isset($_POST['title'])){
    
$type = ($_POST['format'] == 'html') ? 0;
    
$time time();
    if(isset(
$_POST['edit']) && $_POST['edit'] != ''){
     
$sql "UPDATE `$mysql_table` SET `updated` = '$time', `content` = '{$_POST['content']}', `title` = '{$_POST['title']}', `link` = '{$_POST['link']}', `type` = '$type' WHERE `id` = '{$_POST['edit']}'";   
     echo 
"<h2>Post Updateed</h2> <p> The post has been updated. </p>";
    } else {
     
$sql "INSERT INTO `$mysql_table` (posted,updated,content,title,link,type) VALUES ('$time','$time','{$_POST['content']}','{$_POST['title']}','{$_POST['link']}','$type')";
     echo 
"<h2>Post Added</h2> <p> New post inserted. </p>";
    }
    
mysql_query($sql$con) or die(mysql_error());
   }

   
// delete/hide the feed item
   
if(isset($_GET['delete'])){
    
$sql "UPDATE `$mysql_table` SET `deleted` = '1' WHERE id = '{$_GET['delete']}'";
    
mysql_query($sql$con) or die(mysql_error());
    
$sql "SELECT title FROM `$mysql_table` WHERE `id` = '{$_GET['delete']}' LIMIT 0,1";
    
$result mysql_query($sql$con) or die(mysql_error());
    while(
$row mysql_fetch_assoc($result)){    
      echo 
"<p>The item <strong>{$row['title']}</strong> has been deleted.  It is possible to recover it from the admin window. Return to <a href=\"$script_location?admin\">Admin Panel</a></p></p>";
    }
   
// recover/unhide the feed item
   
} else if(isset($_GET['recover'])) {
    
$sql "UPDATE `$mysql_table` SET `deleted` = '0'";
    
mysql_query($sql$con) or die(mysql_error());
    
$sql "SELECT title FROM `$mysql_table` WHERE `id` = '{$_GET['recover']}' LIMIT 0,1";
    
$result mysql_query($sql$con) or die(mysql_error());
    while(
$row mysql_fetch_assoc($result)){    
      echo 
"<p>The item {$row['title']} has been recovered.  Return to <a href=\"$script_location?admin\">Admin Panel</a></p>";
    }     
   } else {
    
// get the info out of the data base if the person wants to edit a post
    
if(isset($_GET['edit'])){
      
$sql "SELECT * FROM `$mysql_table` WHERE `id` = '{$_GET['edit']}' LIMIT 0,1";
      
$result mysql_query($sql$con) or die(mysql_error());
      while(
$row mysql_fetch_assoc($result)){    
    
$data $row;
      }
    }
    
// the main form for the admin window
   
?>
    <h2><?php echo (isset($data['id'])) ? 'Update Current Feed' 'Post New Feed'?></h2>
    <form method="post" action="?admin">
     <div>
      <p>The title for the feed entry</p>
      <label for="title">
       Title: <input type="text" name="title" id="title" size="40" value="<?php echo (isset($data['title'])) ? htmlentities($data['title']) : ''?>">
      </label>
      <p>The page the feed links to.  Leave blank to use the <a href="<?php echo $script_location?>?list">feed listing</a> (html version of the feed)</p>
      <label for="link">
       Link: <input type="text" name="link" id="link" size="40" value="<?php echo (isset($data['link'])) ? htmlentities($data['link']) : ''?>">
      </label>
      <div>
       <p>
         Using HTML allows for formatting the text and adding things like images.  If you need a tool to help you with it try the <a href="http://codefisher.org">Text Formatting Toolbar</a> for <a href="http://www.spreadfirefox.com/?q=affiliates&amp;id=0&amp;t=218">Firefox</a>
       </p>
       <label for="html">
        <input type="radio" name="format" value="html" id="html" <?php echo (!isset($data['type']) || $data['type'] == 1) ? 'checked' ''?> >HTML
       </label>
       <label for="text">
        <input type="radio" name="format" value="text" id="text" <?php echo (isset($data['type']) && $data['type'] == 0) ? 'checked' ''?> >Plan Text
       </label>
      </div>
      <textarea name="content" id="content" oninput="updateContent()" rows="10" cols="40"><?php echo (isset($data['content'])) ? htmlentities($data['content']) : ''?></textarea>
      <div>
    <?php if(isset($data['id'])){ ?>
        <input type="hidden" name="edit" value="<?php echo $data['id'] ; ?>">
        <?php ?>
        <input type="submit" value="<?php echo (isset($data['id'])) ? 'Update' 'Submit'?>">
      </div>
     </div>
    </form>
    <script type="text/javascript"><!--
    function updateContent() {
      var feild = document.getElementById('feild');
      var input = document.getElementById('content').value;
      var title = document.getElementById('title').value;
      var link = document.getElementById('link').value;
      var str = '<h2><a href="'+link+'">'+title+"</a></h2>" + input;
      feild.innerHTML = str;
    }
    //--></script>
    <div id="feild"></div>
   <?php
    
// list all the feed items so they can be deleted or edited
    
$sql "SELECT `title`,`id`,`deleted` FROM `$mysql_table`";
    
$result mysql_query($sql$con) or die(mysql_error());
    
$results = array();
    while(
$row mysql_fetch_assoc($result)){    
     
$results[] = $row;
    }
    
// we only print the table if something was returned
    
if($results){
     
?>
     <h2>Edit Feeds</h2>
    <table>
     <tbody>     
     <?php
     
foreach($results as $row){
     echo 
"<tr>\n    <td>{$row['title']}</td>\n    <td><a href=\"$script_location?admin&amp;edit={$row['id']}\">Edit</a></td>\n";
    echo (
$row['deleted'] == 1) ? "<td><a href=\"$script_location?admin&amp;recover={$row['id']}\">Recover</a></td>\n" :
        
"<td><a href=\"$script_location?admin&amp;delete={$row['id']}\">Delete</a></td>\n";
    echo 
"</tr>";    
     }
   
?>
   </tbody>
   </table>
   <?php
    
}
   }
   
// they need to log in
  
} else { ?>
    <form action="?admin" method="post" id="login">
     <label for="user">
      User: <input type="text" name="user" id="user">
     </label>
     <label for="password">
      Password:<input type="password" name="password" id="password">
     </label>
     <input type="submit" value="Log In">
   </form><?php
  
}
  
?>
     <!-- you may not removed this line from this file -->
     <div style="text-align:center">A <a href="http://codefisher.org/">Codefisher.org Script</a></div>
   </body>
  </html>
  <?php
 
} else if(isset($_GET['list'])){
 
?>
   <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
  <html>
  <head>
   <title><?php  $title?></title>
   <link rel="alternate" type="application/atom+xml" href="<?php echo $script_location?>">
   <style type="text/css">
    body {
     margin:10px auto;
     width:800px;
     border:1px solid #000;
     padding:0 15px 15px 15px;
    }
    h1 {
     font-size:1em;
     color:#fff;
     background:#000;
     margin:0;
     padding:5px;
     text-align:center;
    }
    h2 {
     font-size:1em;
     text-align:center;
    }
    div {
     font-size:0.75em;
     text-align:right;
     color:#333;
    }
   </style>
  </head>
  <body>
   <h1><?php echo $title?></h1>
   <?php
    $sql 
"SELECT * FROM `$mysql_table` WHERE `deleted` = '0' ORDER BY `posted` DESC";
    
$result mysql_query($sql$con) or die(mysql_error());
    
$results = array();
    while(
$row mysql_fetch_assoc($result)){
     
$date date('D, jS \of M Y h:i a',$row['posted']);
     
$content = ($row['type'] == 0) ? htmlParagrahs($row['content']) : $row['content'];
     echo <<<FEED
      <h2><a name="post{$row['id']}">{$row['title']}</a></h2>
       $content
      <div style="text-align:right">$date</div>
FEED;
    }
    
?>
     <!-- you may not removed this line from this file -->
     <div style="text-align:center">A <a href="http://codefisher.org/">Codefisher.org Script</a></div>
     </body>
    </html> 
    <?php
 
} else {
  
header('Content-Type: application/xml');
  
// load the feed into an array read for use.
   
$sql "SELECT * FROM `$mysql_table` WHERE `deleted` = '0' ORDER BY `posted` DESC";
   
$result mysql_query($sql$con) or die(mysql_error());
   
$results = array();
   while(
$row mysql_fetch_assoc($result)){    
    
$results[] = $row;
   }
   
// short tag compatibility
   
echo '<';
   
// out put the feed !!
   
?>?xml version="1.0" encoding="utf-8" ?>
  <feed xmlns="http://www.w3.org/2005/Atom">
   <!--
     This feed was created with Codefisher.org Lite Atom Feed Creator
     http://codefisher.org/projects/atom_feed
   
     The script is listed under the
     Attribution-ShareAlike 3.0 Unported.

     Furthermore;
     You may not claim the works as your own.
     You must provide a link back to http://codefisher.org/ when using on a website.
     You may not redistribute this package as-is.
     You must seek approval for commercial purposes.

     Please don't remove this from this file.
     if you want to contact me please use my contact page
     http://codefisher.org/email
  
     http://codefisher.org/ is not connected to any site this script is used on
     opinions and views expressed in this feed belong to the author.
   
     You may not remove this comment block with out permission of the creator
     of this said script and this will not be given unless a small donation is made
   -->
   <title><?php echo $title?></title>
   <subtitle><?php echo $description?></subtitle>
   <link rel="self" href="<?php echo $script_location?>"/>
   <link rel="alternate" type="text/html" href="<?php echo $script_location?>?list"/>
   <updated><?php echo date('Y-m-d\TH:i:s-04:00',$results[0]['updated']); ?></updated>
   <author>
    <name><?php echo $author?></name>
    <email><?php echo $author_email?></email>
   </author>
  <id><?php echo $script_location?></id>
  <?php
  
   
foreach($results as $row){
    
$euuid generate_GUID($row['id']);
    
$date date('Y-m-d\TH:i:s-04:00',$row['posted']);
    
$type = ($row['type'] == 1) ? 'html' 'text';
    
$link = ($row['link'] == '') ? $script_location.'?list#post'.$row['id'] : $row['link'];
    
$content = ($row['type'] == 1) ? '<![CDATA[ '.$row['content'].' ]]>' $row['content'];
    echo <<<FEED
    <entry>
      <title>
{$row['title']}</title>
      <link href="$link"/>
      <id>urn:uuid:$euuid</id>
      <updated>$date</updated>
      <summary type="$type">$content</summary>
    </entry>
FEED;
   }  
  
?>
  
  </feed>
 <?php
 
}
?>