<?PHP
/************************************************************************/
/* NPDS V : Net Portal Dynamic System .                                 */
/* ===========================                                          */
/*                                                                      */
/* Original Copyright (c) 2001 by Francisco Burzi (fburzi@ncc.org.ve)   */
/* http://phpnuke.org                                                   */
/*                                                                      */
/* This version name NPDS Copyright (c) 2001-2005 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 () {
  if (file_exists("meta/meta.php"))
     include ("meta/meta.php");
  echo "<title>NPDS</title></head><body bgcolor=\"white\"><br /><p align=\"center\"><span style=\"font-size: 14px; font-family: Arial; font-weight: bold; color: red;\">";
  echo "Access Denied / Acc&egrave;s Refus&eacute;";
  if (cur_charset=="utf-8") echo" / &#x901A;&#x5165;&#x88AB;&#x5426;&#x8BA4;";
  echo "</span>";
  echo "<br /><br /><span style=\"font-size: 12px; font-family: Arial; font-weight: bold; color: black;\">NPDS - Portal System";
  echo "</span></p></body></html>";
  die();
}
if (!eregi("admin.php", $PHP_SELF)) { Access_Error(); }
global $pdst; $pdst="0";

global $language;
$hlpfile = "manuels/$language/users.html";
$result = mysql_query("select radminuser, radminsuper from authors where aid='$aid'");
list($radminuser, $radminsuper) = mysql_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() {
   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 = mysql_query("select uid, uname, name, url, email, femail from users where uid!='1' order by uid");
   while($temp_user = mysql_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=mysql_query("select C1, C2, C3, C4, C5, C6, C7, C8, M1, M2, T1, T2 from users_extend where uid='$temp_user[0]'");
      $temp_user2 = mysql_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;
   include("header.php");
   GraphicAdmin($hlpfile);
   OpenTable();
   $result = mysql_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 users where uid='$chng_user' or uname='$chng_user'");
   if (mysql_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) = mysql_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 = mysql_query("select level, open, groupe, attachsig, rank from users_status where uid='$chng_uid'");
      list ($chng_level, $open_user, $groupe, $attach, $chng_rank) = mysql_fetch_row($result);
      $result = mysql_query("SELECT C1, C2, C3, C4, C5, C6, C7, C8, M1, M2, T1, T2, B1 FROM users_extend WHERE uid='$chng_uid'");
      list($C1, $C2, $C3, $C4, $C5, $C6, $C7, $C8, $M1, $M2, $T1, $T2, $B1) = mysql_fetch_array($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) {
   $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) {
      mysql_query("update 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,$cpass);
      } else {
         $cpass=$chng_pass;
      }
      mysql_query("update 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="";}
         }
      }
   }
   mysql_query("update users_status set attachsig='$attach', level='$level', open='$open_user', groupe='$chng_groupe', rank='$chng_rank' where uid='$chng_uid'");
   mysql_query("update 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 = mysql_query("select uid from users where uid='$del_uid' or uname='$del_uid'");
        list($del_uid) = mysql_fetch_row($result);
        if ($del_uid!=1) {
           mysql_query("delete from users where uid='$del_uid'");
           mysql_query("delete from users_status where uid='$del_uid'");
           mysql_query("delete from subscribe where uid='$del_uid'");
           mysql_query("delete from 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 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 = mysql_query($sql);
        list($usr_id) = mysql_fetch_array(mysql_query("select uid from users where uname='$add_uname'"));
        $result = mysql_query("insert into 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 = mysql_query("insert into 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 = mysql_query("select uid from users where uid='$chng_uid' or uname='$chng_uid'");
        list($chng_uid) = mysql_fetch_row($result);
        if ($chng_uid!=1) {
           mysql_query("delete from subscribe where uid='$chng_uid'");
        }
        Header("Location: admin.php?op=mod_users");
        break;

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