<?php
/*
    CREATIVE COMMONS - Attribution-No Derivative Works 3.0 Germany:
    - You may not alter, transform, or build upon this work.
    - Dieses Werk darf nicht bearbeitet oder in anderer Weise verändert werden.
*/
function NewsBackendNavigation ()
  {
    global $AnzahlSeiten;
    if ( $AnzahlSeiten>'1' )
    {
      global $aktuelleSeite;
      global $L_111;

      $temp = '<div class="GigNavigation">'.$L_111.': ';
      $i = '1';
      while ($i <= $AnzahlSeiten)
      {
        if ($aktuelleSeite == $i) { $temp = $temp.'<b>'.$i.'</b> '; }
        else { $temp = $temp.'<a href="index.php?submenu=news&amp;page=backend_showAllNews&amp;page='.$i.'">'.$i.'</a> '; }
        $i++;
      }
     $temp = $temp.'</div>';
    }
    else $temp = '';
    return $temp;
  }
  
function convertString ( $String )
{
  $String = str_replace("<","&lt;",$String);
  $String = str_replace(">","&gt;",$String);
  $String = str_replace("&","&amp;",$String);
  $String = str_replace('"','&quot;',$String);
  $String = str_replace("'","&apos;",$String);
  return $String;
}
  
function getTimeStringRFC3339 ($Timestamp)
{
  // get TimeZone
  $timeZone = date('O', $Timestamp);
  // create first part of AtomTime
  $atomTime[0] = date('Y-m-d\TH:i:s', $Timestamp );
  // build second part of AtomTime
  $atomTime[1] = substr($timeZone,0,3). ':' .substr($timeZone,3,2);
  // merge the 2 parts to one
  $atomTimeString = implode('', $atomTime);
  return $atomTimeString;
}
  
function writeAllFeeds ()
{
  writeAtomFeed();
  writeRSSFeed();
}
  
function writeAtomFeed ()
{
  
  global $DB_PREFIX;
  global $GIGKALENDERPATH;
  global $GigKalenderVersion;
  $SetupNewsChannelURL = getSetup('SetupNewsChannelURL');
  $path = str_replace("\\","/",dirname(__FILE__));
  $path = str_replace("/news","/news",$path);
  $aktuellerEintrag = AnzahlVeroeffentlichterNewsInDatenbank();

  $String = '<?xml version="1.0" encoding="utf-8"?>
  <feed xmlns="http://www.w3.org/2005/Atom">
  <title>'.getSetup('SetupNewsChannelTitle').'</title>
  <subtitle type="text">'.getSetup('SetupNewsChannelDescription').'</subtitle>
  <updated>'.getTimeStringRFC3339(time()).'</updated>
  <link href="'.GIGKALENDERPATH.'news/news.atom" rel="self" />
  <generator>GigKalender '.$GigKalenderVersion.'</generator>
  <rights>'.getSetup('SetupNewsChannelCopyright').'</rights>
  <id>'.getSetup('SetupNewsChannelURL').'</id>';
  

  // NewsFeed schreiben
    $sql = "SELECT * FROM ".DB_PREFIX."news order by Date DESC";
    $result = mysql_query($sql);
    $datei = $path."/news.atom";
    $fp=fopen($datei,"w+");

    while ($row = mysql_fetch_array($result))
    {
      extract($row);
      if ( $Publish=='1' )
      {
        if ( substr_count($SetupNewsChannelURL, '?')) $URL = $SetupNewsChannelURL.'&amp;';
        else $URL = $SetupNewsChannelURL.'?';
        $URL = $URL.'NewsID='.$aktuellerEintrag.'#news'.$aktuellerEintrag;

        $AuthorEmailadress = getUserVar ($AddedFrom, 'UserEmailadress');
        if ( $AuthorEmailadress!='' ) $Email = '<email>'.$AuthorEmailadress.'</email>';
        else $Email = '';
        
        $Title = convertString ( $Title );
        
        $String = $String.'
  <entry>
    <title>'.$Title.'</title>
    <content type="xhtml" xml:lang="'.getSetup('SetupNewsChannelLanguage').'">
      <div xmlns="http://www.w3.org/1999/xhtml">'.$Text.'</div>
    </content>
    <author>
      <name>'.$AddedFrom.'</name>
      <uri>'.getSetup('SetupNewsChannelURL').'</uri>
      '.$Email.'
    </author>
    <id>'.$URL.'</id>
    <published>'.getTimeStringRFC3339($Date).'</published>
    <link rel="alternate" href="'.$URL.'" />';
    if ($DateOfLastEdit!=NULL AND $DateOfLastEdit!=0)
    {
      $String .= '
    <updated>'.getTimeStringRFC3339($DateOfLastEdit).'</updated>';
    }
    else
    {
      $String .= '
    <updated>'.getTimeStringRFC3339($Date).'</updated>';
    }
  $String .= '
  </entry>';
      $aktuellerEintrag--;
      }
    }
    $String = $String.'
</feed>';
    fwrite($fp,$String);
}
  
function writeRSSFeed ()
{
  
  global $DB_PREFIX;
  global $GIGKALENDERPATH;
  global $GigKalenderVersion;
  $SetupNewsChannelURL = getSetup('SetupNewsChannelURL');
  $path = str_replace("\\","/",dirname(__FILE__));
  $path = str_replace("/news","/news",$path);
  $aktuellerEintrag = AnzahlVeroeffentlichterNewsInDatenbank();
  
  $String = '<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
    <channel>
      <title>'.getSetup('SetupNewsChannelTitle').'</title>
      <link>'.getSetup('SetupNewsChannelURL').'</link>
      <atom:link href="'.GIGKALENDERPATH.'news/news.rss" rel="self" type="application/rss+xml" />
      <description>'.getSetup('SetupNewsChannelDescription').'</description>
      <language>'.getSetup('SetupNewsChannelLanguage').'</language>
      <copyright>'.getSetup('SetupNewsChannelCopyright').'</copyright>
      <pubDate>'.gmdate ( "D, d M Y H:i:s" ).' GMT</pubDate>
      <lastBuildDate>'.gmdate ( "D, d M Y H:i:s" ).' GMT</lastBuildDate>
      <generator>GigKalender '.$GigKalenderVersion.'</generator>';
  
  // NewsFeed schreiben
    $sql = "SELECT * FROM ".DB_PREFIX."news order by Date DESC";
    $result = mysql_query($sql);
    $datei = $path."/news.rss";
    $fp=fopen($datei,"w+");

    while ($row = mysql_fetch_array($result))
    {
      extract($row);
      if ( substr_count($SetupNewsChannelURL, '?')) $URL = $SetupNewsChannelURL.'&amp;';
      else $URL = $SetupNewsChannelURL.'?';
      $URL = $URL.'NewsID='.$aktuellerEintrag.'#news'.$aktuellerEintrag;
      
      $AuthorEmailadress = getUserVar ($AddedFrom, 'UserEmailadress');
      if ( $AuthorEmailadress!='' ) $Email = '<author>'.$AuthorEmailadress.' ('.$AddedFrom.')</author>';
      else $Email = '';
      
      $Title = convertString ( $Title );
      
      if ( $Publish=='1' )
      {
        $String = $String.'
        <item>
          <guid>'.$URL.'</guid>
          <title>'.$Title.'</title>
          <link>'.$URL.'</link>
          <description><![CDATA['.$Text.']]></description>
          '.$Email.'
          <pubDate>'.gmdate ( "D, d M Y H:i:s", $Date ).' GMT</pubDate>
        </item>';
        $aktuellerEintrag--;
      }
    }
    $String = $String.'</channel>
    </rss>';
    fwrite($fp,$String);
}
  
function importNewsEntry ($Date, $AddedFrom, $Title, $Text, $DateOfLastEdit, $LastEditor, $Publish)
  {
    global $DB_PREFIX;
    
    
    // HTML-Tags entfernen
      $Title = strip_tags($Title);
    $Text = str_replace("&","&amp;",$Text);
    // Maskieren spezieller Zeichen für die Verwendung in einer SQL-Anweisung
      $Title = mysql_real_escape_string($Title);
      $Text = mysql_real_escape_string($Text);
    // Leere numerische Variablen als NULL definieren
      if ( $Date=='' ) $Date="NULL";
      if ( $DateOfLastEdit=='' ) $DateOfLastEdit="NULL";
      if ( $Publish=='' ) $Publish="NULL";

    $sql = "INSERT INTO ".DB_PREFIX."news (Date, AddedFrom, Title, Text, DateOfLastEdit, LastEditor, Publish)
      VALUES ($Date, '$AddedFrom', '$Title', '$Text', $DateOfLastEdit, '$LastEditor', $Publish)";
    $resultat = mysql_query($sql);
    if ($resultat) { return true; }
    else {return false;}
}   
  
function AnzahlVeroeffentlichterNewsInDatenbank ()
  {
    global $DB_PREFIX;
    

    $sql = "SELECT count(*) as zahl FROM ".DB_PREFIX."news WHERE Publish= '1'";
    $result = mysql_query($sql);
    $row = mysql_fetch_array($result);
    return $row['zahl'];
  }
  
function  NewsNavigation ( $Locator )
  {
    global $AnzahlSeiten;
    global $aktuelleSeite;
    global $L_111;
    global $L_112;
    global $NewsID;

    $temp = '<div class="NewsNavigation">'.$L_111.': ';
    $Seitenzahl = $AnzahlSeiten;

    // $FormActionURL = str_replace("?NewsID=".$NewsID, "", $_SERVER['REQUEST_URI']);
    // $FormActionURL = getSetup('SetupNewsChannelURL');
    $URL = getSetup('SetupNewsChannelURL');
    $URL = str_replace("&","&amp;",$URL);
    if ( preg_match ( '|\?|', $URL ) ) $URL = $URL.'&amp;';
      else $URL = $URL.'?';
    
    while ($Seitenzahl >= 1)
    { 
      $FormActionURL = $URL.'page='.$Seitenzahl;
      if ($aktuelleSeite == $Seitenzahl) { $temp = $temp.'<b>'.$Seitenzahl.'</b> '; }
      else { $temp = $temp.'<form style="display:inline" id="form'.$Seitenzahl.$Locator.'" method="post" action="'.$FormActionURL.'" accept-charset="UTF-8">
                              <p style="display:inline">
                                <input type="hidden" name="page" value="'.$Seitenzahl.'" />
                                <a href="'.$FormActionURL.'" onclick="javascript:document.forms[\'form'.$Seitenzahl.$Locator.'\'].submit()">'.$Seitenzahl.'</a>
                              </p>
                            </form>&nbsp;'; }
      $Seitenzahl--;
    }
    $temp = $temp.'</div>';
    $temp = preg_replace ('/(\r\n|\n|\r)/', '', $temp);
    $temp = str_replace("  ", "", $temp);
    return $temp;
  }
  
function editNews ($NewsID, $Title, $Text, $Publish)
{
  global $DB_PREFIX;
  
  
  // HTML-Tags entfernen
    $Title = strip_tags($Title);
  $DateOfLastEdit = date("U");
  // Maskieren spezieller Zeichen für die Verwendung in einer SQL-Anweisung
    $Title = mysql_real_escape_string($Title);
    $Text = mysql_real_escape_string($Text);
  
  $sql = "UPDATE ".DB_PREFIX."news SET
        Title = '".$Title."',
        Text = '".$Text."',
        Publish = '".$Publish."',
        DateOfLastEdit = '".$DateOfLastEdit."',
        LastEditor = '".getCurrentUserVar('UserName')."'
        WHERE NewsID = '".$NewsID."'
        LIMIT 1";
      $resultat = mysql_query($sql);
      if ($resultat) { writeAllFeeds(); return true; }
      else {return false;}
}

function  changePublishing ( $Date )
{
  global $DB_PREFIX;
  
  
  // aktuellen Status abfragen
  $sql = "SELECT Publish
    FROM ".DB_PREFIX."news
    WHERE Date='".$Date."'
    LIMIT 1";
  $result = mysql_query($sql);
  if ( mysql_num_rows($result)==1)
  {
    $temp=mysql_fetch_assoc($result);
    $aktuellerStatus = $temp['Publish'];
  }
  // neuen Status setzen
  if ( $aktuellerStatus=='0' ) $neuerStatus = '1';
  if ( $aktuellerStatus=='1' ) $neuerStatus = '0';
  $DateOfLastEdit = date("U");
  // Leere numerische Variablen als NULL definieren
    if ( $DateOfLastEdit=='' ) $DateOfLastEdit="NULL";
    if ( $Publish=='' ) $Publish="NULL";
  $sql = "UPDATE ".DB_PREFIX."news SET
    Publish = ".$neuerStatus.",
    DateOfLastEdit = ".$DateOfLastEdit.",
    LastEditor = '".getCurrentUserVar('UserName')."'
    WHERE Date = '".$Date."' LIMIT 1";
  $resultat = mysql_query($sql);
  if ($resultat) { writeAllFeeds(); return true; }
    else {return false;}
}  
  
function addNews ($Title, $Text, $Publish)
  {
    global $DB_PREFIX;
    // HTML-Tags entfernen
      $Title = strip_tags($Title);
    // Sonderzeichen umwandeln
      //$Title = htmlspecialchars($Title, ENT_QUOTES);
      //$Text = htmlspecialchars($Text, ENT_QUOTES);
    $Text = str_replace("&","&amp;",$Text);
    $Date = date("U");
    // Maskieren spezieller Zeichen für die Verwendung in einer SQL-Anweisung
      $Title = mysql_real_escape_string($Title);
      $Text = mysql_real_escape_string($Text);
    // Leere numerische Variablen als NULL definieren
      if ( $Date=='' ) $Date="NULL";
      if ( $Publish=='' ) $Publish="NULL";
    $sql = "INSERT INTO ".DB_PREFIX."news (Date, AddedFrom, Title, Text, DateOfLastEdit, Publish)
      VALUES ($Date, '".getCurrentUserVar('UserName')."', '$Title', '$Text', '', $Publish)";
    $resultat = mysql_query($sql);
    if ($resultat) { writeAllFeeds(); return true; }
    else {return false;}
}  

function AnzahlAllerNewsInDatenbank ()
  {
    global $DB_PREFIX;
    $sql = "SELECT count(*) as zahl FROM ".DB_PREFIX."news";
    $result = mysql_query($sql);
    $row = mysql_fetch_array($result);
    return $row['zahl'];
  }
?>
