<?PHP
/************************************************************************/
/* DUNE by NPDS                                                         */
/* ===========================                                          */
/*                                                                      */
/* Manage the EDITO (static/edito.txt) of your web site                 */
/*                                                                      */
/* NPDS Copyright (c) 2002-2008 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 Access_Error () {
  include("admin/die.php");
}
if (!eregi("admin.php", $PHP_SELF)) { Access_Error(); }

global $language, $NPDS_Prefix;
$hlpfile = "manuels/$language/edito.html";
$result = sql_query("select radminarticle, radminsuper from ".$NPDS_Prefix."authors where aid='$aid'");
list($radminarticle, $radminsuper) = sql_fetch_row($result);
if (($radminarticle!=1) and ($radminsuper!=1)) {
   Access_Error();
}

function edito($edito_type, $contents, $Xaff_jours, $Xaff_jour, $Xaff_nuit) {
    global $hlpfile, $language;
    include ("header.php");
    GraphicAdmin($hlpfile);
    opentable();
    echo "<table width=\"100%\" cellspacing=\"2\" cellpadding=\"2\" border=\"0\"><tr><td class=\"header\">\n";
    echo adm_translate("Edito");
    echo "</td></tr></table>\n";
    echo "<br />\n";
    if ($contents=="") {
       echo "</td></tr><tr><td align=\"center\">";
       echo "<form action=\"admin.php?op=Edito_load\" method=\"post\">";
       echo "<select class=\"textbox\" name=\"edito_type\" onChange='submit()'>";
       echo "<option value=\"0\">".adm_translate("Modifier l'Editorial")." ...</option>\n";
       echo "<option value=\"G\">".adm_translate("Anonyme")."</option>\n";
       echo "<option value=\"M\">".adm_translate("Membre")."</option>\n";
       echo "</select></form>";
    } else {
       if ($edito_type=="G") {
          $edito_typeL=" ".adm_translate("Anonyme");
       } elseif ($edito_type=="M") {
          $edito_typeL=" ".adm_translate("Membre");;
       }
       if (strpos($contents,"[/jour]")>0) {
          $contentJ=substr($contents,strpos($contents,"[jour]")+6,strpos($contents,"[/jour]")-6);
          $contentN=substr($contents,strpos($contents,"[nuit]")+6,strpos($contents,"[/nuit]")-19-strlen($contentJ));
       }
       if (!$contentJ and !$contentN and !strpos($contents,"[/jour]")) $contentJ=$contents;
       echo "<b>".adm_translate("Edito")." :$edito_typeL</b>\n";
       echo "<p align=\"center\">";
       echo "<form action=\"admin.php\" method=\"post\" name=\"adminForm\">";
       echo adm_translate("Le jour")." :: ";
       echo aff_editeur("XeditoJ","false");
       echo "<br />";
       echo "<textarea class=\"textbox\" name=\"XeditoJ\" rows=\"20\" cols=\"70\" style=\"width: 100%;\">\n";
       echo htmlspecialchars($contentJ);
       echo "</textarea><br /><br />\n";
       echo adm_translate("La nuit")." :: ";
       echo aff_editeur("XeditoN","false");
       echo "<br />";
       echo "<textarea class=\"textbox\" name=\"XeditoN\" rows=\"20\" cols=\"70\" style=\"width: 100%;\">\n";
       echo htmlspecialchars($contentN);
       echo "</textarea><br /><br />\n";
       echo adm_translate("Afficher pendant")." : <input class=\"textbox_standard\" type=\"text\" name=\"aff_jours\" maxlength=\"3\" size=\"4\" value=\"$Xaff_jours\"> ".adm_translate("jour(s)")." :: ";
       echo adm_translate("Le jour")." : <input type=\"checkbox\" name=\"aff_jour\" value=\"checked\" $Xaff_jour> / ".adm_translate("La nuit")." : <input type=\"checkbox\" name=\"aff_nuit\" value=\"checked\" $Xaff_nuit> - ";

       echo "<input type=\"hidden\" name=\"op\" value=\"Edito_save\">";
       echo "<input type=\"hidden\" name=\"edito_type\" value=\"$edito_type\">";
       echo "<input class=\"bouton_standard\" type=\"submit\" name=\"edito_confirm\" value=\"".adm_translate("Sauver les modifications")."\"> - \n";
       echo "<a href=\"admin.php?op=Edito\" class=\"noir\">".adm_translate("Abandonner")."</a></p>\n";
       echo "</form>\n";
    }
    closetable();
    include ("footer.php");
}

function edito_mod_save($edito_type, $XeditoJ, $XeditoN, $aff_jours, $aff_jour, $aff_nuit) {
    if ($aff_jours<=0) {$aff_jours="999";}
    if ($edito_type=="G") {
       $fp=fopen("static/edito.txt","w");
       fputs($fp,"[jour]".str_replace("&quot;","\"",stripslashes($XeditoJ))."[/jour][nuit]".str_replace("&quot;","\"",stripslashes($XeditoN))."[/nuit]");
       fputs($fp,"aff_jours=".$aff_jours);
       fputs($fp,"&aff_jour=".$aff_jour);
       fputs($fp,"&aff_nuit=".$aff_nuit);
       fputs($fp,"&aff_date=".time());
       fclose($fp);
    }  elseif ($edito_type=="M") {
       $fp=fopen("static/edito_membres.txt","w");
       fputs($fp,"[jour]".str_replace("&quot;","\"",stripslashes($XeditoJ))."[/jour][nuit]".str_replace("&quot;","\"",stripslashes($XeditoN))."[/nuit]");
       fputs($fp,"aff_jours=".$aff_jours);
       fputs($fp,"&aff_jour=".$aff_jour);
       fputs($fp,"&aff_nuit=".$aff_nuit);
       fputs($fp,"&aff_date=".time());
       fclose($fp);
    }
    redirect_url("admin.php?op=Edito");
}

switch ($op) {

    case "Edito_save":
        edito_mod_save($edito_type, $XeditoJ, $XeditoN, $aff_jours, $aff_jour, $aff_nuit);
        break;

    case "Edito_load":
        if ($edito_type=="G") {
           if (file_exists("static/edito.txt")) {
              $fp=fopen("static/edito.txt","r");
              if (filesize("static/edito.txt")>0)
                 $Xcontents=fread($fp,filesize("static/edito.txt"));
              fclose($fp);
           }
        } elseif ($edito_type=="M") {
           if (file_exists("static/edito_membres.txt")) {
              $fp=fopen("static/edito_membres.txt","r");
              if (filesize("static/edito_membres.txt")>0)
                 $Xcontents=fread($fp,filesize("static/edito_membres.txt"));
              fclose($fp);
           }
        }
        $Xcontents=ereg_replace("<!--","",$Xcontents);
        $Xcontents=ereg_replace("/-->","",$Xcontents);
        if ($Xcontents=="") {
           $Xcontents="Edito ...";
        } else {
           $ibid=strstr($Xcontents,"aff_jours");
           parse_str($ibid);
        }
        if ($aff_jours) {
           $Xcontents=substr($Xcontents,0,strpos($Xcontents,"aff_jours"));
        } else {
           $aff_jours=20;
           $aff_jour="checked";
           $aff_nuit="checked";
        }
        edito($edito_type, $Xcontents, $aff_jours, $aff_jour, $aff_nuit);
        break;

    default:
        edito("","","","","");
        break;
}
?>