<?PHP
/************************************************************************/
/* DUNE by NPDS                                                         */
/* ===========================                                          */
/*                                                                      */
/* NPDS Copyright (c) 2002-2007 by Philippe Brunier                     */
/*                                                                      */
/* This program is free software. You can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 2 of the License.       */
/************************************************************************/
function readnews ($blog_dir, $op, $perpage, $startpage, $action, $adminblog, $page) {
   $content=""; $contentT="!l_new_pages!";
   $blog_file=$blog_dir."news.txt";
   if (!file_exists($blog_file)) {
      $fp=fopen($blog_file,"w");
      fclose($fp);
   }
   $xnews=file($blog_file);
   $xnews=array_reverse($xnews);
   $startpage-=1;
   $ubound=count($xnews);
   if ($startpage<0 || $startpage>=$ubound/$perpage) $startpage=0;
   if ($ubound>$perpage) {
      for ($j=1;$j<=ceil($ubound/$perpage);$j++) {
          if ($j==$startpage+1)
             $contentT.="<b>$j</b>&nbsp;";
          else
             $contentT.="<a href=\"minisite.php?op=$op&amp;page=$page&amp;startpage=$j\" clas=\"BLOG_LIEN\">$j</a>&nbsp;";
      }
   }
   if ($adminblog) {
      // Suppression
      if (substr($action,0,1)=="D") {
         @copy ($blog_file,$blog_file.".bak");
         $index=substr($action,1);
         unset ($xnews[$index]);
         $xnews=array_reverse($xnews);
         $fp=fopen($blog_file,"w");
         for ($j=0;$j<count($xnews);$j++) {
             fwrite($fp,$xnews[$j]);
         }
         fclose($fp);
         redirect_url("minisite.php?op=$op&page=$page");
      }
      // Ajouter - Ecriture
      if (substr($action,0,3)=="AOK") {
         global $title, $story;
         @copy ($blog_file,$blog_file.".bak");
         $fp=fopen($blog_file,"a");
         $formatted=str_replace("\r\n","<br />",$story);
         $formatted=str_replace("\n","<br />",$formatted);
         $newsto=date("d m Y")."!;!".$title."!;!".$formatted;
         fwrite($fp,StripSlashes($newsto)."\n");
         fclose($fp);
         redirect_url("minisite.php?op=$op&page=$page");
      }
      // Ajouter
      if (substr($action,0,1)=="A") {
         $content.="<form name=\"adminForm\" method=\"post\" action=\"minisite.php?op=$op&page=$page&action=AOK\">";
         $content.="<table width=\"100%\" cellspacing=\"2\" cellpadding=\"2\" border=\"0\">";
         $content.="<tr class=\"blog_titre\"><td>".translate("Title")." :</td><td width=\"75%\"><input type=\"text\" name=\"title\" size=\"45\" class=\"blog_textbox\" /></td></tr>";
         $content.="<tr class=\"blog_titre\"><td>".translate("Full Text")." :</td><td width=\"75%\"><textarea name=\"story\" rows=\"25\" cols=\"70\" class=\"blog_textbox\" style=\"width: 100%;\"></textarea>";
         $content.="&nbsp;!blog_editeur!";
         $content.="&nbsp;<input type=\"submit\" name=\"submit\" value=\"".translate("Submit")."\" class=\"blog_new\" />";
         $content.="</td></tr></table></form>\n";
      }
      // Modifier - Ecriture
      if (substr($action,0,3)=="MOK") {
         global $title, $story, $index;
         @copy ($blog_file,$blog_file.".bak");
         $formatted=str_replace("\r\n","<br />",$story);
         $formatted=str_replace("\n","<br />",$formatted);
         $newsto=date("d m Y")."!;!".$title."!;!".$formatted;
         $xnews[$index]=StripSlashes($newsto)."\n";
         $xnews=array_reverse($xnews);
         $fp=fopen($blog_file,"w");
         for ($j=0;$j<count($xnews);$j++) {
             fwrite($fp,$xnews[$j]);
         }
         fclose($fp);
         redirect_url("minisite.php?op=$op&page=$page");
      }
      // Modifier
      if (substr($action,0,1)=="M") {
         $index=substr($action,1);
         $crtsplit=explode("!;!",$xnews[$index]);
         $content.="<form name=\"adminForm\" method=\"post\" action=\"minisite.php?op=$op&page=$page&action=MOK&index=$index\">";
         $content.="<table width=\"100%\" cellspacing=\"2\" cellpadding=\"2\" border=\"0\">";
         $content.="<tr class=\"blog_titre\"><td>".translate("Title")." :</td><td width=\"75%\"><input type=\"text\" name=\"title\" value=\"".$crtsplit[1]."\" size=\"45\" class=\"blog_textbox\" /></td></tr>";
         $content.="<tr class=\"blog_titre\" valign=\"top\"><td>".translate("Full Text")." :</td><td width=\"75%\"><textarea name=\"story\" rows=\"25\" cols=\"70\" class=\"blog_textbox\" style=\"width: 100%;\">".str_replace("\n","",$crtsplit[2])."</textarea>";
         $content.="&nbsp;!blog_editeur!";
         $content.="<br /><input type=\"submit\" name=\"submit\" value=\"".translate("Submit")."\" class=\"blog_new\" />";
         $content.="</td></tr></table></form>\n";
      }
   }
   if ($contentT!="") {$colspan=1;};
   if ($adminblog) {
      $colspan=$colspan+2;
   } else {
      $colspan=$colspan+1;
   }
   // Output
   $template=false;
   $new_page=false;
   $blog_template=$blog_dir."news-tpl.html";
   if (file_exists($blog_template)) {
      $fp=fopen($blog_template,"r");
      if (filesize($blog_template)>0)
         $buffer=fread($fp,filesize($blog_template));
      fclose($fp);
      $template=true;
   }
   if (!$template) {
      $content.="<table width=\"100%\" cellspacing=\"2\" cellpadding=\"2\" border=\"0\">";
      if ($adminblog) {
         $content.="<tr class=\"blog_titre\"><td width=\"95%\" colspan=\"$colspan\" align=\"right\">!l_blog_ajouterOK!</td></tr>";
      }
   }
   for ($i=$startpage*$perpage;$i<$startpage*$perpage+$perpage && $i<$ubound;$i++) {
       $crtsplit="";
       $crtsplit=explode("!;!",$xnews[$i]);
       $actionM="<a href=\"minisite.php?op=$op&amp;page=$page&amp;action=M$i\" class=\"blog_lien\">".translate("Modify")."</a>";
       $actionD="<a href=\"minisite.php?op=$op&amp;page=$page&amp;action=D$i\" class=\"blog_lien\">".translate("Delete")."</a>";
       if (!$template) {
          $content.="<tr class=\"blog_titre\"><td>".aff_langue($crtsplit[1])." - ".$crtsplit[0]."</td>";
          if (substr($contentT,13)!="") {$content.="<td align=\"right\" nowrap=\"nowrap\">".substr($contentT,13)."</td>";};
          if ($adminblog) {
             $content.="<td nowrap=\"nowrap\" width=\"25%\" align=\"center\">$actionM&nbsp;&nbsp;$actionD</td></tr>";
          } else {
             $content.="</tr>";
          }
          $content.="<tr><td colspan=\"$colspan\" class=\"blog_new\">".convert_ressources($crtsplit[2])."</td></tr>";
          $content.="<tr><td colspan=\"$colspan\">&nbsp;</td></tr>";
       } else {
         if (($adminblog) and (strstr($buffer,"!l_new_modif!"))) {
            $new_modif=$actionM;
         } else {
            $new_modif="";
         }
         if (($adminblog) and (strstr($buffer,"!l_new_suppr!"))) {
            $new_suppr=$actionD;
         } else {
            $new_suppr="";
         }
         if (strstr($buffer,"!l_new_pages!")) {
            $new_pages=true;
         }
         $MNS_METALANG_words=array(
         "'!l_new_date!'i"=>"$crtsplit[0]",
         "'!l_new_titre!'i"=>"$crtsplit[1]",
         "'!l_new_pages!'i"=>substr($contentT,13),
         "'!l_new_texte!'i"=>convert_ressources($crtsplit[2]),
         "'!l_new_modif!'i"=>"$new_modif",
         "'!l_new_suppr!'i"=>"$new_suppr"
         );
         $content.=preg_replace(array_keys($MNS_METALANG_words),array_values($MNS_METALANG_words), $buffer);
       }
   }
   if (!$template) {
      $content.="</table>\n";
   }
   if (!$new_pages) {$content.=$contentT;}
   return ($content);
}
?>