<?php

session_start();
error_reporting(0);

include("adminheader.php");

//dictionnaire specifique au module
  include("../langages/$SESSION_LANGSKIN/admin_download.php");

// donnees titre et liens modules
//-------------------------------
$imgmodule="artiphpdownload.gif";
$titremodule="$LG_MODUDOWN";
$descriptionmodule="$LG_MODUDOWNDES";

include("../outils/foncpost2.php");


/*****************************************************************************************
                          Artiphp Velocity 3 - ArtiDownload
               copyright : Ronald Guérin - webmaster@artiloo.com
                            http://www.artiloo.com
                      Dernière mise à jour : 20 octobre 2004
******************************************************************************************/

/*****************************************************************************************
 Artiphp, portail CMS pour la création de sites dynamiques 

 Copyright (C) 2003 Ronald Guérin - webmaster@artiloo.com 

 Ce programme est un logiciel libre ; vous pouvez le redistribuer et/ou le modifier 
 conformément aux dispositions de la Licence Publique Générale GNU, telle que publiée 
 par la Free Software Foundation ; version 2 de la licence, ou encore (à votre choix) 
 toute version ultérieure. 

 Ce programme est distribué dans l'espoir qu'il sera utile, mais SANS AUCUNE GARANTIE ; 
 sans même la garantie implicite de COMMERCIALISATION ou D'ADAPTATION A UN OBJET PARTICULIER. 
 Pour plus de détail, voir la Licence Publique Générale GNU . 

 Vous devez avoir reçu un exemplaire de la Licence Publique Générale GNU en même temps que 
 ce programme ; si ce n'est pas le cas, écrivez à la Free Software Foundation Inc., 
 675 Mass Ave, Cambridge, MA 02139, Etats-Unis. 

 Pour plus d'informations sur Artiphp :
 http://www.artiloo.com - webmaster@artiloo.com
******************************************************************************************/

// autorisation d'ouverture de cette page uniquement aux status autorisés
//----------------------------------------------------------------------
if ($_SESSION['SESSION_STATUT']!="artadmin") { 
 header("Location: ../outils/erreur.php?ec=2");
 exit();
}



   $modif="$LG_NEWDOWN";

   $datedossier= date ("y-m-d H:i:s");
   $datevisible= date ("d/m/Y");



if ($_GET['id_download']) {
  $id_download=$_GET['id_download'];


  $requete="select * from ".$extension."download where id_download='$id_download'";
  $result=mysql_query($requete, $link);
  $dossier=mysql_fetch_object($result);

   $dossier->titreDownload = stripslashes($dossier->titreDownload);
   $dossier->chapoDownload = stripslashes($dossier->chapoDownload);
   $modif="$LG_MODIFDOWN <font style=\"color:red\">".$dossier->titreDownload."</font>";


    /****suivant le traitement de texte (Ie ou NS)***********/
    $nav=$_GET['$nav'];
    if($nav=='2') {
     $dossier->chapoDownload=eregi_replace('<br[[:space:]]*/?[[:space:]]*>', "", $dossier->chapoDownload);
    }
    /********************************************************/


     if ($dossier->imgDownload !="") {
     $formphoto="$LG_IMGNOW <br>";
     $formphoto .="<img src=\"../images/".$dossier->imgDownload."\"></p>"; 
     $formphoto .="<input type=\"checkbox\" name=\"suppimage\" value=\"ok\"> $LG_SUPPRES<br><br>";
     $formphoto .="$LG_IMGCHANG<br>";
     }
}

// ouverture FTP et changement de chmod du repertoire bibliotheque
   if ($_FILES['fichier']['type']){
	if($ftp=='1') {
	 $ftpcon = ftp_connect($ftp_host) or die('Erreur');
	 $loginresult = ftp_login($ftpcon, $ftp_login, $ftp_mdp);

	 $repertoire="".$ftp_repertoire."download";

	 //emplacement du fichier que vous voulez modifier
	 ftp_chdir($ftpcon, $repertoire) or die('Erreur dossier');

	 //changement de chmod via FTP
	 $chmod_cmd="CHMOD 0777 down"; 
	 ftp_site($ftpcon, $chmod_cmd);
	}
    }
//----------------------------------------------------------------

   //upload du fichier
   //------------------

   if ($_FILES['fichier']['type']){
   $fich=$_FILES['fichier']['type'];
   $nomfich=$_FILES['fichier']['name'];
   $tmp_file = $_FILES['fichier']['tmp_name'];

    if( !is_uploaded_file($tmp_file) ) {
       $meserreur2 ="<br><font color=red>$LG_UPNOFILE</font><br><br>";
    }

     $tabTypeImage = split ("/", $fich);
     $typeFichier = $tabTypeImage[1];

//     if ($typeFichier != "x-zip-compressed" && $typeFichier != "octet-stream" && $typeFichier != "pdf" && $typeFichier != "doc") {
     if (0) {
         $meserreur2 ="<br><font color=red>$LG_UPNOFORMDOWN</font><br><br>";
     } else {

         $destination = "../download/down/".$nomfich."";

         if (! move_uploaded_file ($tmp_file, $destination)) {
            $meserreur2 ="<br><font color=red>$LG_UPNOTRANS</font><br><br>";
         } else {
            header("Location: downloadsaisie.php");
            exit();
         }
     }

   }
   //-------------------------

//-------------------------
// Fermeture et remise du chmod FTP
   if ($_FILES['fichier']['type']){
	if($ftp=='1') {
	  $chmod_cmd="CHMOD 0755 down"; 
	  ftp_site($ftpcon, $chmod_cmd);
	  ftp_close($ftpcon);
	}
   }
//----------------------------------



   //visualisation des fichiers du dossier down
   //-----------------------------------------------------
   if ($dossier->lienDownload) {
   $selected="selected";
   $auteurselect[$dossier->lienDownload]=$selected;
   }
     $page.="<select name=\"lien\" size=\"1\">\n";

/*  modified by Marco to sort documents in alphabetical order

        $handle=opendir("../download/down/");
        while (false!==($file = readdir($handle))) {
         if(($file != "..") && ($file != "."))
         {
         $page.="<option value=\"".$file."\"  ".$auteurselect[$file].">".$file."</option>\n";
         }
        }

*/

	$handle=opendir("../download/down/");
        $alphafiles = array();
        while (false!==($file = readdir($handle))) {
         if (($file != "..") && ($file != "."))
         {
          $alphafiles[] = $file;
         }
        }
        natcasesort($alphafiles);
        foreach ($alphafiles as $alphafile) {
         $page.="<option value=\"".$alphafile."\"  ".$auteurselect[$alphafile].">".$alphafile."</option>\n";
        }


     $page.="</select>\n";
   //-----------------------------------------------------



//--------------------
//formulaire de saisie
//--------------------

// ouverture FTP et changement de chmod du repertoire bibliotheque
   if ($_FILES['image']['type']){
	if($ftp=='1') {
	 $ftpcon = ftp_connect($ftp_host) or die('Erreur');
	 $loginresult = ftp_login($ftpcon, $ftp_login, $ftp_mdp);

	 $repertoire="".$ftp_repertoire."";

	 //emplacement du fichier que vous voulez modifier
	 ftp_chdir($ftpcon, $repertoire) or die('Erreur dossier');

	 //changement de chmod via FTP
	 $chmod_cmd="CHMOD 0777 images"; 
	 ftp_site($ftpcon, $chmod_cmd);
	}
    }
//----------------------------------------------------------------

   //upload de la photo
   //------------------

   if ($_FILES['image']['type']){
   $img=$_FILES['image']['type'];
   $nomimage=$_FILES['image']['name'];
   $tmp_file = $_FILES['image']['tmp_name'];

    if( !is_uploaded_file($tmp_file) ) {
       $meserreur2 ="<br><font color=red>$LG_UPNOFILE</font><br><br>";
    }

     $tabTypeImage = split ("/", $img);
     $typeImage = $tabTypeImage[1];

     if ($typeImage != "pjpeg" && $typeImage != "x-jpeg" && $typeImage != "jpeg" && $typeImage != "gif" && $typeImage != "x-png") {
         $meserreur2 ="<br><font color=red>$LG_UPNOFORM</font><br><br>";
     } else {

         $destination = "../images/".$nomimage."";

         if (! move_uploaded_file ($tmp_file, $destination)) {
            $meserreur2 ="<br><font color=red>$LG_UPNOTRANS</font><br><br>";
            $insertimage=="";
         } else {
            $insertimage=", imgDownload='$nomimage' ";
         }
     }

   }

   //-------------------------

//-------------------------
// Fermeture et remise du chmod FTP
   if ($_FILES['image']['type']){
	if($ftp=='1') {
	  $chmod_cmd="CHMOD 0755 images"; 
	  ftp_site($ftpcon, $chmod_cmd);
	  ftp_close($ftpcon);
	}
   }
//----------------------------------



 /*si un champ n'est pas renseigné, message d'erreur*/

  if (! $_POST['titre'] || ! $_POST['texte'] || ! $_POST['categorie'] || ! $_POST['choix'] || (! $_POST['lien'] && ! $_POST['lien2'])) {
   $meserreur="<font style=\"color:red\"><br>$LG_FORMERROR</font>";

     $id_download = $_POST['id_download'];
     $titre = $_POST['titre'];
     $texte=$_POST['texte'];
     $choix = $_POST['choix'];
     $lien = $_POST['lien'];
     $lien2 = $_POST['lien2'];
     $categorie = $_POST['categorie'];
  
 /*si on recupere id_download, c'est une mise à jour*/

  } elseif ($_POST['id_download']) {  

     $id_download = $_POST['id_download'];
     $titre = $_POST['titre'];
     $texte=$_POST['texte'];
     $choix = $_POST['choix'];
     $lien = $_POST['lien'];
     $lien2 = $_POST['lien2'];
     $categorie = $_POST['categorie'];

   /*******************************************/
   $nav=$_POST['$nav'];
   if ($nav=='2') {
     $texte = nl2br($texte);
   }
   /*******************************************/
     $texte = doscode($texte);
     $titre = addslashes($titre);
     $texte = addslashes($texte);

   if ($_POST['suppimage']){
    $insertimage=", imgDownload='' ";
   }


  if ($choix=="1") {

// dirty patch by Marco, to avoid tinyMCE path error when including images
     $texte=str_replace("www.cilss.bf..","www.cilss.bf/fondsitalie",$texte);
//

   $requete="update ".$extension."download set titreDownload='$titre', dateDownload='$datedossier', chapoDownload='$texte', lienDownload='$lien', id_cat='$categorie', dateDownvis='$datevisible' $insertimage where id_download='$id_download'";
  } else {

// dirty patch by Marco, to avoid tinyMCE path error when including images
     $texte=str_replace("www.cilss.bf..","www.cilss.bf/fondsitalie",$texte);
//

   $requete="update ".$extension."download set titreDownload='$titre', dateDownload='$datedossier', chapoDownload='$texte', lienDownload='$lien2', id_cat='$categorie', dateDownvis='$datevisible' $insertimage where id_download='$id_download'";
  }

   $result=mysql_query($requete, $link);

	//mise a jour de l'index
	include("download_donnees.php");

     header ("Location:downloadconfirm.php?categorie=$categorie");
     exit();

 /*sinon, c'est un ajout*/
  
  } else {

     $titre = $_POST['titre'];
     $texte=$_POST['texte'];
     $choix = $_POST['choix'];
     $lien = $_POST['lien'];
     $lien2 = $_POST['lien2'];
     $categorie = $_POST['categorie'];

   /*******************************************/
   $nav=$_POST['$nav'];
   if ($nav=='2') {
     $texte = nl2br($texte);
   }
   /*******************************************/
     $texte = doscode($texte);
     $titre = addslashes($titre);
     $texte = addslashes($texte);



  if ($choix=="1") {

// dirty patch by Marco, to avoid tinyMCE path error when including images
     $texte=str_replace("www.cilss.bf..","www.cilss.bf/fondsitalie",$texte);
//

   $requete="INSERT into ".$extension."download set titreDownload='$titre', dateDownload='$datedossier', dateDownvis='$datevisible', chapoDownload='$texte', lienDownload='$lien', id_cat='$categorie'$insertimage";
  } else {

// dirty patch by Marco, to avoid tinyMCE path error when including images
     $texte=str_replace("www.cilss.bf..","www.cilss.bf/fondsitalie",$texte);
//

   $requete="INSERT into ".$extension."download set titreDownload='$titre', dateDownload='$datedossier', dateDownvis='$datevisible', chapoDownload='$texte', lienDownload='$lien2', id_cat='$categorie'$insertimage";
  }

   $result=mysql_query($requete, $link);

	//mise a jour de l'index
	include("download_donnees.php");

     header ("Location:downloadconfirm.php?categorie=$categorie");
     exit();

  } 



mysql_free_result($result);
//-------------------------

//---------------
//champ categorie
//---------------
  if ($dossier->id_cat) {
  $selected="selected";
  $lieuselect[$dossier->id_cat]=$selected;
  }
   $listecat.="<select name=\"categorie\" size=\"1\">\n";

   $requete="select nomCategorie, id_categorie, id_parentcat from ".$extension."categoriedown where id_parentcat='0'";
   $result=mysql_query($requete, $link);
   while ($cat=mysql_fetch_object($result)) {
    $cat->nomCategorie = stripslashes($cat->nomCategorie);

    $listecat.="<option value=\"".$cat->id_categorie."\"   ".$lieuselect[$cat->id_categorie].">".$cat->nomCategorie."</option>\n";
    $sscat=$cat->id_categorie;
      $requete="select * from ".$extension."categoriedown where id_parentcat=$sscat order by id_parentcat desc";
      $result2=mysql_query($requete, $link);
      while ($cat=mysql_fetch_object($result2)) {
      $cat->nomCategorie = stripslashes($cat->nomCategorie);

       $listecat.="<option value=\"".$cat->id_categorie."\"  ".$lieuselect[$cat->id_categorie]."> - ".$cat->nomCategorie."</option>\n";
      }

//---------------------------------------------------------------------------
mysql_free_result($result2);
//---------------------------------------------------------------------------

   }
   $listecat.="</select></p>\n";



//------------
//mise en page
//------------

 include("header.php");
 include("menumod.php");


?>

<div align="center"><center>
<table width="95%" border="0" cellspacing="5" cellpadding="5">
<tr>
<form NAME="form" method="POST" action="downloadsaisie.php" enctype="multipart/form-data">
<td>
<p><img src="imgadmin/ptecrire.gif" align="absmiddle" border="0">&nbsp;
<strong><?php echo $LG_UPLOADFICH; ?></strong>
<?php echo $meserreur10; ?>

</td></tr>
<tr><td valign="top" bgcolor="#CCCCFF" style="BORDER:#000000 1px dashed">
<input name="fichier" type="file" size="50"> 
<input type="submit" value="<?php echo $LG_ENVOI; ?>" name="B1">
</td></form></tr></table>
</center></div>
<p></p>



<div align="center"><center>
<table width="95%" border="0" cellspacing="5" cellpadding="5">
<tr>
<form NAME="form" method="POST" action="downloadsaisie.php" enctype="multipart/form-data">
<td>
<p><img src="imgadmin/ptecrire.gif" align="absmiddle" border="0">&nbsp;
<strong><?php echo $modif; ?></strong>
<?php echo $meserreur; echo $meserreur2; ?>

</td></tr>
<tr><td valign="top" bgcolor="#CCCCFF" style="BORDER:#000000 1px dashed">



<b><?php echo $LG_CAT; ?>*</b><br>
<?php echo $listecat; ?>
<br>

<b><?php echo $LG_NOMDOWN; ?>*</b><br>
<input type="text" name="titre" value="<?php echo $dossier->titreDownload; echo $titre; ?>" size="50"></p><p>

<?php
$texte = $_POST['texte'];
/** zone de texte **********************************/
  $nomtxt="<b>".$LG_DESCRIPTION."*</b>"; // nom de la textearea
  $texte="".$dossier->chapoDownload."".$texte.""; // affichage de texte dans le textearea
  $lienNS="downloadsaisie.php?id_download=$dossier->id_download&nav=2";  // lien vers editeur netscape
  $lienIE="downloadsaisie.php?id_download=$dossier->id_download";  // lien vers editeur internet explorer

  include("../outils/foncpost10.php");
?>

<br>
<?php echo $menu; ?>

<input type="radio" name="choix" value="1" checked> 
<b><?php echo $LG_DOWNSITE; ?></b><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<?php echo $page; ?>
<p>

<input type="radio" name="choix" value="2"> 
<b><?php echo $LG_DOWNEXTERN; ?></b> <br><?php echo $LG_DOWNEXEMPLE; ?><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="text" name="lien2" value="<?php echo $dossier->lienDownload; echo $lien2; ?>" size="50"></p><p>

<b><?php // echo $LG_IMGPRES; ?></b><br>
<?php  echo $formphoto; ?>
<input type="hidden" name="MAX_FILE_SIZE" value="150000">
<!-- <input name="image" type="file" size="50" value="" ><br> -->

<input type="hidden" name="id_download" value="<?php echo $dossier->id_download;  echo $_POST['id_download']; ?>">
<input type="hidden" name="datedownload" value="<?php echo $dossier->dateDownload; ?>">
<input type="hidden" name="nav" value="$nav">
<input type="submit" value="<?php echo $LG_ENVOI; ?>" name="B1">


</td></form></tr></table>
</center></div>
<p></p>

<?php


 include("menumod2.php");
 include("footer.php");
 mysql_close();
?>
