<?PHP
/************************************************************************/
/* DUNE by NPDS                                                         */
/* ===========================                                          */
/*                                                                      */
/* Based on PhpNuke 4.x source code                                     */
/*                                                                      */
/* Major changes from ALAT 2004-2005                                    */
/* 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;
$hlpfile = "manuels/$language/users.html";
$result =sql_query("select radminuser, radminsuper from ".$NPDS_Prefix."authors where aid='$aid'");
list($radminuser, $radminsuper) = sql_fetch_row($result);
if (($radminuser!=1) and ($radminsuper!=1)) {
   Access_Error();
}

function displayUsers() {
   global $hlpfile;
   include("header.php");
   GraphicAdmin($hlpfile);
   opentable();
   echo "<table width=\"100%\" cellspacing=\"2\" cellpadding=\"2\" border=\"0\"><tr><td class=\"header\">\n";
   echo adm_translate("Edition des Utilisateurs");
   echo "</td></tr></table>\n";
   echo "<form method=\"post\" action=\"admin.php\">";
   echo adm_translate("Extraire l'annuaire")." : ";
   echo "<select class=\"textbox_standard\" name=\"op\">";
   echo "<option value=\"extractUserCSV\">".adm_translate("Au format CSV")."</option>";
   echo "</select>";
   echo "<input class=\"bouton_standard\" type=\"submit\" value=\"".adm_translate("Ok")."\"></form>";
   echo "<hr noshade class=\"ongl\">";

   echo "<form method=\"post\" action=\"admin.php\">";
   echo "<b>".adm_translate("Identifiant Utilisateur")." :</b> <input class=\"textbox_standard\" type=\"text\" name=\"chng_uid\" size=\"20\" max=\"10\"> - ";
   echo "<select class=\"textbox_standard\" name=\"op\">";
   echo "<option value=\"modifyUser\">".adm_translate("Modifier un utilisateur")."</option>";
   echo "<option value=\"unsubUser\">".adm_translate("Désabonner un utilisateur")."</option>";
   echo "<option value=\"delUser\">".adm_translate("Supprimer un utilisateur")."</option></select> - ";
   echo "<input class=\"bouton_standard\" type=\"submit\" value=\"".adm_translate("Ok")."\"></form>";
   $chng_is_visible=1;
   $op="displayUsers";
   include ("modules/sform/extend-user/adm_extend-user.php");
   closetable();
   include("footer.php");
}

function extractUserCSV() {
   global $NPDS_Prefix;

   include("lib/archive.php");
   $MSos=get_os();
   if ($MSos=="Win") {$crlf="\r\n";} else {$crlf="\n";}
   $deliminator = ', ';
   $line = "UID, UNAME, NAME, URL, EMAIL, FEMAIL, C1, C2, C3, C4, C5, C6, C7, C8, M1, M2, T1, T2".$crlf;
   $result = sql_query("select uid, uname, name, url, email, femail from ".$NPDS_Prefix."users where uid!='1' order by uid");
   while($temp_user = sql_fetch_row($result) ) {
      foreach($temp_user as $val) {
        $val = str_replace("\r\n", "\n", $val);
        if (ereg("[$deliminator\"\n\r]", $val)) {
           $val = '"'.str_replace('"', '""', $val).'"';
        }
        $line .= $val.$deliminator;
      }
      $result2=sql_query("select C1, C2, C3, C4, C5, C6, C7, C8, M1, M2, T1, T2 from ".$NPDS_Prefix."users_extend where uid='$temp_user[0]'");
      $temp_user2 = sql_fetch_row($result2);
      if ($temp_user2) {
         foreach($temp_user2 as $val2) {
           $val2 = str_replace("\r\n", "\n", $val2);
           if (ereg("[$deliminator\"\n\r]", $val2)) {
              $val2 = '"'.str_replace('"', '""', $val2).'"';
           }
           $line .= $val2.$deliminator;
          }
      }
      $line = substr($line, 0, (strlen($deliminator) * -1));
      $line .= $crlf;
   }
   send_file($line,"annuaire","csv",$MSos);
}

function modifyUser($chng_user) {
   global $hlpfile;
   global $NPDS_Prefix;

   include("header.php");
   GraphicAdmin($hlpfile);
   opentable();
   $result = sql_query("select uid, uname, name, url, email, femail, user_icq, user_aim, user_yim, user_msnm, user_from, user_occ, user_intrest, user_viewemail, user_avatar, user_sig, bio, pass, send_email, is_visible, mns, user_lnl from ".$NPDS_Prefix."users where uid='$chng_user' or uname='$chng_user'");
   if (sql_num_rows($result) > 0) {
      list($chng_uid, $chng_uname, $chng_name, $chng_url, $chng_email, $chng_femail, $chng_user_icq, $chng_user_aim, $chng_user_yim, $chng_user_msnm, $chng_user_from, $chng_user_occ, $chng_user_intrest, $chng_user_viewemail, $chng_avatar, $chng_user_sig, $chng_bio, $chng_pass, $chng_send_email, $chng_is_visible, $mns, $user_lnl) = sql_fetch_row($result);
      echo "<table width=\"100%\" cellspacing=\"2\" cellpadding=\"2\" border=\"0\"><tr><td class=\"header\">\n";
      echo adm_translate("Modifier un utilisateur")." : $chng_uname / $chng_uid";
      echo "</td></tr></table>\n";
      $op="ModifyUser";
      $result = sql_query("select level, open, groupe, attachsig, rank from ".$NPDS_Prefix."users_status where uid='$chng_uid'");
      list ($chng_level, $open_user, $groupe, $attach, $chng_rank) = sql_fetch_row($result);
      $result = sql_query("SELECT C1, C2, C3, C4, C5, C6, C7, C8, M1, M2, T1, T2, B1 FROM ".$NPDS_Prefix."users_extend WHERE uid='$chng_uid'");
      list($C1, $C2, $C3, $C4, $C5, $C6, $C7, $C8, $M1, $M2, $T1, $T2, $B1) = sql_fetch_row($result);
      include ("modules/sform/extend-user/adm_extend-user.php");
   } else {
      error_handler("Utilisateur inexistant !"."<br />");
   }
   closetable();
   include("footer.php");
}

function error_handler($ibid) {
   opentable();
   echo "<p align=\"center\">".adm_translate("Merci d'entrer l'information en fonction des spécifications")."<br /><br />";
   echo "$ibid<br /><a href=\"admin.php?op=mod_users\" class=\"noir\">".adm_translate("Retour en arrière")."</a></p>";
   closetable();
}

function Minisites($chng_mns,$chng_uname) {
   // Création de la structure pour les MiniSites dans users_private/$chng_uname
   if ($chng_mns) {
      include ("modules/upload/upload.conf.php");
      if ($DOCUMENTROOT=="") {
         global $DOCUMENT_ROOT;
         if ($DOCUMENT_ROOT) {
            $DOCUMENTROOT=$DOCUMENT_ROOT;
         } else {
            $DOCUMENTROOT=$_SERVER['DOCUMENT_ROOT'];
         }
      }
      $user_dir=$DOCUMENTROOT.$racine."/users_private/".$chng_uname;
      $repertoire=$user_dir."/mns";
      if (!is_dir($user_dir)) {
         @umask("0000");
         if (@mkdir($user_dir,0777)) {
            $fp = fopen($user_dir."/index.html", 'w');
            fclose($fp);
            @umask("0000");
            if (@mkdir($repertoire,0777)) {
               $fp = fopen($repertoire."/index.html", 'w');
               fclose($fp);
               $fp = fopen($repertoire."/.htaccess", 'w');
               @fputs($fp, "Deny from All");
               fclose($fp);
            }
         }
      } else {
         @umask("0000");
         if (@mkdir($repertoire,0777)) {
            $fp = fopen($repertoire."/index.html", 'w');
            fclose($fp);
            $fp = fopen($repertoire."/.htaccess", 'w');
            @fputs($fp, "Deny from All");
            fclose($fp);
         }
      }
      @copy($DOCUMENTROOT.$racine."/modules/upload/include_minisite/readme.".$language.".txt", $repertoire."/readme.".$language.".txt");
   }
}

function updateUser($chng_uid, $chng_uname, $chng_name, $chng_url, $chng_email, $chng_femail, $chng_user_icq, $chng_user_aim, $chng_user_yim, $chng_user_msnm, $chng_user_from, $chng_user_occ, $chng_user_intrest, $chng_user_viewemail, $chng_avatar, $chng_user_sig, $chng_bio, $chng_pass, $chng_pass2, $level, $open_user, $chng_groupe, $chng_send_email, $chng_is_visible, $chng_mns, $C1,$C2,$C3,$C4,$C5,$C6,$C7,$C8,$M1,$M2,$T1,$T2,$B1,$raz_avatar, $chng_rank, $chng_lnl) {
   global $NPDS_Prefix;

   $tmp = 0;
   if ($chng_pass2 != "") {
      if ($chng_pass != $chng_pass2) {
         global $hlpfile;
         include("header.php");
         GraphicAdmin($hlpfile);
         echo error_handler(adm_translate("Désolé, les nouveaux Mots de Passe ne correspondent pas. Cliquez sur retour et recommencez")."<br />");
         include("footer.php");
         return;
      }
      $tmp=1;
   }
   if ($chng_send_email=="") {$chng_send_email='0';}
   if ($chng_is_visible=="") {
      $chng_is_visible='1';
   } else {
      $chng_is_visible='0';
   }
   if ($raz_avatar) {$chng_avatar="blank.gif";}
   if ($tmp==0) {
      sql_query("update ".$NPDS_Prefix."users set uname='$chng_uname', name='$chng_name', email='$chng_email', femail='$chng_femail', url='$chng_url', user_icq='$chng_user_icq', user_aim='$chng_user_aim', user_yim='$chng_user_yim', user_msnm='$chng_user_msnm', user_from='$chng_user_from', user_occ='$chng_user_occ', user_intrest='$chng_user_intrest', user_viewemail='$chng_user_viewemail', user_avatar='$chng_avatar', user_sig='$chng_user_sig', bio='$chng_bio', send_email='$chng_send_email', is_visible='$chng_is_visible', mns='$chng_mns', user_lnl='$chng_lnl' where uid='$chng_uid'");
   }
   if ($tmp==1) {
      global $system;
      if (!$system) {
         $cpass = crypt($chng_pass,$chng_pass);
      } else {
         $cpass=$chng_pass;
      }
      sql_query("update ".$NPDS_Prefix."users set uname='$chng_uname', name='$chng_name', email='$chng_email', femail='$chng_femail', url='$chng_url', user_icq='$chng_user_icq', user_aim='$chng_user_aim', user_yim='$chng_user_yim', user_msnm='$chng_user_msnm', user_from='$chng_user_from', user_occ='$chng_user_occ', user_intrest='$chng_user_intrest', user_viewemail='$chng_user_viewemail', user_avatar='$chng_avatar', user_sig='$chng_user_sig', bio='$chng_bio', send_email='$chng_send_email', is_visible='$chng_is_visible', mns='$chng_mns', pass='$cpass', user_lnl='$chng_lnl' where uid='$chng_uid'");
   }
   if ($chng_user_viewemail) {
      $attach = 1;
   } else {
     $attach = 0;
   }
   if ($open_user=="") {$open_user=0;}
   if (ereg("[a-zA-Z_]",$chng_groupe)) {$chng_groupe="";}
   if ($chng_groupe!="") {
      $tab_groupe=explode(",",$chng_groupe);
      if ($tab_groupe) {
         foreach($tab_groupe as $groupevalue) {
           if ( ($groupevalue=="0") and ($groupevalue!="") ) {$chng_groupe="";}
           if ($groupevalue=="1") {$chng_groupe="";}
           if ($groupevalue>"127") {$chng_groupe="";}
         }
      }
   }
   sql_query("update ".$NPDS_Prefix."users_status set attachsig='$attach', level='$level', open='$open_user', groupe='$chng_groupe', rank='$chng_rank' where uid='$chng_uid'");
   sql_query("update ".$NPDS_Prefix."users_extend set C1='$C1', C2='$C2', C3='$C3', C4='$C4', C5='$C5', C6='$C6', C7='$C7', C8='$C8', M1='$M1', M2='$M2', T1='$T1', T2='$T2', B1='$B1' where uid='$chng_uid'");

   Minisites($chng_mns,$chng_uname);

   Header("Location: admin.php?op=mod_users");
}

switch($op) {
   case "extractUserCSV":
        extractUserCSV();
        break;

   case "modifyUser":
        modifyUser($chng_uid);
        break;

   case "updateUser":
        if ($add_group) {$add_group=implode(",",$add_group);}
        updateUser($chng_uid, $add_uname, $add_name, $add_url, $add_email, $add_femail, $add_user_icq, $add_user_aim, $add_user_yim, $add_user_msnm, $add_user_from, $add_user_occ, $add_user_intrest, $add_user_viewemail, $add_avatar, $add_user_sig, $add_bio, $add_pass, $add_pass2, $add_level, $add_open_user, $add_group, $add_send_email, $add_is_visible, $add_mns, $C1,$C2,$C3,$C4,$C5,$C6,$C7,$C8,$M1,$M2,$T1,$T2,$B1,$raz_avatar,$chng_rank,$user_lnl);
        break;

   case "delUser":
        global $hlpfile;
        include("header.php");
        GraphicAdmin($hlpfile);
        opentable();
        echo "<p align=\"center\"><b>".adm_translate("Supprimer un utilisateur")."</b> : ";
        echo "<span class=\"rouge\">".adm_translate("Etes-vous sûr de vouloir effacer") . " " . adm_translate("Utilisateur") . " $chng_uid ? </span><br /><br />";
        echo "[ <a href=\"admin.php?op=delUserConf&amp;del_uid=$chng_uid\" class=\"rouge\">".adm_translate("Oui")."</a> | <a href=\"admin.php?op=mod_users\" class=\"noir\">".adm_translate("Non")."</a> ]<br />";
        closetable();
        include("footer.php");
        break;

   case "delUserConf":
        $result = sql_query("select uid from ".$NPDS_Prefix."users where uid='$del_uid' or uname='$del_uid'");
        list($del_uid) = sql_fetch_row($result);
        if ($del_uid!=1) {
           sql_query("delete from ".$NPDS_Prefix."users where uid='$del_uid'");
           sql_query("delete from ".$NPDS_Prefix."users_status where uid='$del_uid'");
           sql_query("delete from ".$NPDS_Prefix."subscribe where uid='$del_uid'");
           sql_query("delete from ".$NPDS_Prefix."users_extend where uid='$del_uid'");
        }
        Header("Location: admin.php?op=mod_users");
        break;

   case "addUser":
        if (!($add_uname && $add_email && $add_pass) or (ereg("[^a-zA-Z0-9_-]",$add_uname))) {
           global $hlpfile;
           include("header.php");
           GraphicAdmin($hlpfile);
           echo error_handler(adm_translate("Vous devez remplir tous les Champs")."<br />");
           include("footer.php");
           return;
        }
        if (!$system) {
           $add_pass = crypt($add_pass,$add_pass);
        }
        if ($add_is_visible=="") {
           $add_is_visible='1';
        } else {
           $add_is_visible='0';
        }
        $user_regdate = time()+$gmt*3600;
        $sql= "insert into ".$NPDS_Prefix."users ";
        $sql.= "(uid,name,uname,email,femail,url,user_regdate,user_icq,user_aim,user_yim,user_msnm,user_from,user_occ,user_intrest,user_viewemail,user_avatar,user_sig,bio,pass,send_email,is_visible,mns) ";
        $sql.= "values (NULL,'$add_name','$add_uname','$add_email','$add_femail','$add_url','$user_regdate','$add_user_icq','$add_user_aim','$add_user_yim','$add_user_msnm','$add_user_from','$add_user_occ','$add_user_intrest','$add_user_viewemail','$add_avatar','$add_user_sig','$add_bio','$add_pass','$add_send_email','$add_is_visible','$add_mns')";
        $result = sql_query($sql);
        list($usr_id) = sql_fetch_row(sql_query("select uid from ".$NPDS_Prefix."users where uname='$add_uname'"));
        $result = sql_query("insert into ".$NPDS_Prefix."users_extend values ('$usr_id','$C1','$C2','$C3','$C4','$C5','$C6','$C7','$C8','$M1','$M2','$T1','$T2', '$B1')");
        if ($add_user_viewemail) {
           $attach = 1;
        } else {
           $attach = 0;
        }
        if ($add_group) {$add_group=implode(",",$add_group);}
        $result = sql_query("insert into ".$NPDS_Prefix."users_status values ('$usr_id','0','$attach','$chng_rank','$add_level','1','$add_group')");

        Minisites($add_mns,$add_uname);
        Header("Location: admin.php?op=mod_users");
        break;

   case "unsubUser":
        $result = sql_query("select uid from ".$NPDS_Prefix."users where uid='$chng_uid' or uname='$chng_uid'");
        list($chng_uid) = sql_fetch_row($result);
        if ($chng_uid!=1) {
           sql_query("delete from ".$NPDS_Prefix."subscribe where uid='$chng_uid'");
        }
        Header("Location: admin.php?op=mod_users");
        break;

   case "mod_users":
   default:
        displayUsers();
        break;
}
?>