<?php
/************************************************************************/
/* DUNE by NPDS                                                         */
/* ===========================                                          */
/*                                                                      */
/* NPDS Copyright (c) 2002-2013 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.       */
/************************************************************************/
if (!function_exists("Mysql_Connexion")) {
   include ("mainfile.php");
}

// Make Member_list Private or not
if (!AutoReg()) { unset($user); }
if (($member_list==1) AND ($user=="") AND ($admin=="")) {
   Header("Location: index.php");
}

if (isset($gr_from_ws) and ($gr_from_ws!=0)) {
   settype($gr_from_ws, 'integer');
   $uid_from_ws="^(";
   $re = sql_query("select uid, groupe from ".$NPDS_Prefix."users_status where groupe regexp '[[:<:]]".$gr_from_ws."[[:>:]]'");
   while (list($ws_uid) = sql_fetch_row($re)) {
      $uid_from_ws.= $ws_uid."|";
   }
   $uid_from_ws=substr($uid_from_ws,0,-1).")\$";
} else
   $uid_from_ws="";

function alpha() {
   global $sortby, $list, $gr_from_ws, $uid_from_ws;

   $alphabet = array (translate("All"), "A","B","C","D","E","F","G","H","I","J","K","L","M",
                     "N","O","P","Q","R","S","T","U","V","W","X","Y","Z",translate("Other"));

   echo "<table width=\"100%\" border=\"0\">\n<tbody><tr>\n<td width=\"50%\" align=\"center\">";
   $num = count($alphabet) - 1;
   $counter = 0;
   while (list(, $ltr) = each($alphabet)) {
      echo "<a href=\"memberslist.php?letter=$ltr&amp;sortby=$sortby&amp;list=$list&amp;gr_from_ws=$gr_from_ws\" class=\"noir\">$ltr</a>";
      if ( $counter == round($num/2) ) {
         echo " ]<br />[ ";
      } elseif ( $counter != $num ) {
         echo "&nbsp;|&nbsp;";
      }
      $counter++;
   }
   echo "</td><td class=\"noir\">";
   echo "<form action=\"memberslist.php\" method=\"post\">";
   echo translate("Search")." : <input id=\"mblst_search\" class=\"textbox_standard\" type=\"input\" name=\"letter\" size=\"40\" maxlenght=\"60\" />";
   echo "<input type=\"hidden\" name=\"list\" value=\"".urldecode($list)."\" />";
   echo "<input type=\"hidden\" name=\"gr_from_ws\" value=\"$gr_from_ws\" />";
   echo "</form>";
   echo "</td>\n</tr>\n</tbody>\n</table>\n";
}

function unique($ibid) {
   while (list(,$to_user) = each($ibid)) {
      settype($Xto_user,'array');
      if (!array_key_exists($to_user,$Xto_user)) {
         $Xto_user[$to_user]=$to_user;
      }
   }
   return ($Xto_user);
}

function SortLinks($letter) {
   global $sortby, $list, $admin, $gr_from_ws;

   if ($letter == "front") {
      $letter = translate("All");
   }
   $sort=false;
   echo "<table width=\"100%\" cellspacing=\"2\" cellpadding=\"2\" border=\"0\">\n<tr>\n<td align=\"center\">\n";
   echo translate("Sort by:")." ";
   if ($sortby == "uname ASC" OR !$sortby) {
      echo translate("nickname")."&nbsp;|&nbsp;";
      $sort=true;
   } else {
      echo "<a href=\"memberslist.php?letter=$letter&amp;sortby=uname%20ASC&amp;list=$list&amp;gr_from_ws=$gr_from_ws\" class=\"noir\">".translate("nickname")."</a>&nbsp;|&nbsp;";
   }
   if ($sortby == "name ASC") {
      echo translate("real name")."&nbsp;|&nbsp;";
      $sort=true;
   } else {
      echo "<a href=\"memberslist.php?letter=$letter&amp;sortby=name%20ASC&amp;list=$list&amp;gr_from_ws=$gr_from_ws\" class=\"noir\">".translate("real name")."</a>&nbsp;|&nbsp;";
   }
   if ($sortby == "user_avatar ASC") {
      echo translate("Avatar")."&nbsp;|&nbsp;";
      $sort=true;
   } else {
      echo "<a href=\"memberslist.php?letter=$letter&amp;sortby=user_avatar%20ASC&amp;list=$list&amp;gr_from_ws=$gr_from_ws\" class=\"noir\">".translate("Avatar")."</a>&nbsp;|&nbsp;";
   }
   if (($sortby == "femail ASC") or ($sortby == "email ASC")) {
      echo translate("Email")."&nbsp;|&nbsp;";
      $sort=true;
   } else {
      if ($admin) {
         echo "<a href=\"memberslist.php?letter=$letter&amp;sortby=email%20ASC&amp;list=$list&amp;gr_from_ws=$gr_from_ws\" class=\"noir\">".translate("Email")."</a>&nbsp;|&nbsp;";
      } else {
         echo "<a href=\"memberslist.php?letter=$letter&amp;sortby=femail%20ASC&amp;list=$list&amp;gr_from_ws=$gr_from_ws\" class=\"noir\">".translate("Email")."</a>&nbsp;|&nbsp;";
      }
   }
   if ($sortby == "user_from ASC") {
      echo translate("Location")."&nbsp;|&nbsp;";
      $sort=true;
   } else {
      echo "<a href=\"memberslist.php?letter=$letter&amp;sortby=user_from%20ASC&amp;list=$list&amp;gr_from_ws=$gr_from_ws\" class=\"noir\">".translate("Location")."</a>&nbsp;|&nbsp;";
   }
   if ($sortby == "url DESC") {
      echo translate("url")."&nbsp;|&nbsp;";
      $sort=true;
   } else {
      echo "<a href=\"memberslist.php?letter=$letter&amp;sortby=url%20DESC&amp;list=$list&amp;gr_from_ws=$gr_from_ws\" class=\"noir\">".translate("url")."</a>&nbsp;|&nbsp;";
   }
   if ($sortby == "mns DESC") {
      echo translate("Mini-Web site")."&nbsp;|&nbsp;";
      $sort=true;
   } else {
      echo "<a href=\"memberslist.php?letter=$letter&amp;sortby=mns%20DESC&amp;list=$list&amp;gr_from_ws=$gr_from_ws\" class=\"noir\">".translate("Mini-Web site")."</a>&nbsp;|&nbsp;";
   }
   if ($sortby == "uid DESC") {
      echo "I.D";
      $sort=true;
   } else {
      echo "<a href=\"memberslist.php?letter=$letter&amp;sortby=uid%20DESC&amp;list=$list&amp;gr_from_ws=$gr_from_ws\" class=\"noir\">I.D</a>";
   }
   if (!$sort) {$sortby="uname ASC";}

   echo "</td>\n</tr>\n</table>\n";
}

function avatar($user_avatar) {
   if (!$user_avatar) {
      $imgtmp="images/forum/avatar/blank.gif";
   } else if (stristr($user_avatar,"users_private")) {
      $imgtmp=$user_avatar;
   } else {
      if ($ibid=theme_image("forum/avatar/$user_avatar")) {$imgtmp=$ibid;} else {$imgtmp="images/forum/avatar/$user_avatar";}
      if (!file_exists($imgtmp)) {$imgtmp="images/forum/avatar/blank.gif";}
   }
   return ($imgtmp);
}

   include("header.php");
   $pagesize = $show_user;

   if (!isset($letter) or ($letter=="")) { $letter = translate("All"); }
   $letter=removeHack(stripslashes(htmlspecialchars($letter,ENT_QUOTES,cur_charset)));
   if (!isset($sortby)) { $sortby = "uid DESC"; }
   $sortby=removeHack($sortby);
   if (!isset($page)) { $page = 1; }

   if (isset($list)) {
      $tempo=unique(explode(",",$list));
      $list=urlencode(implode(",",$tempo));
   }

   $result = sql_query("select uname from ".$NPDS_Prefix."users order by uid DESC limit 0,1");
   list($lastuser) = sql_fetch_row($result);
   
   echo'<h2><img src="images/admin/users.png" border="0" alt="'.translate("Members List").'" />'.translate("Members List");
   if (isset ($uid_from_ws) and ($uid_from_ws!="")) echo " ".translate("for group")." ".$gr_from_ws;
   echo '</h2>';

   if (!isset($gr_from_ws))
      echo "<p align=\"center\">".translate("Greetings to our latest registered user:")." <a href=\"user.php?op=userinfo&amp;uname=$lastuser\" class=\"noir\">$lastuser</a></p>";

   opentable();
      alpha();
      echo "<br />";
      SortLinks($letter);

      $min = $pagesize * ($page - 1);
      $max = $pagesize;
      $ws_req="";
      if (isset($uid_from_ws) and ($uid_from_ws!="")) $ws_req= 'where uid regexp \''.$uid_from_ws.'\' ';
      $count = "SELECT COUNT(uid) AS total FROM ".$NPDS_Prefix."users ";
      $select = "select uid, name, uname, femail, url, user_regdate, user_from, email, is_visible, user_viewemail, user_avatar, mns, user_lastvisit from ".$NPDS_Prefix."users ";
      if (($letter != translate("Other")) AND ($letter != translate("All"))) {
         if ($admin and (preg_match('#^[_\.0-9a-z-]+@[0-9a-z-\.]+\.+[a-z]{2,4}$#i',$letter))) 
            $where = "where uname like '".$letter."%' or email like '%".strtolower($letter)."%'".str_replace ( 'where',' and',$ws_req );
         else
            $where = "where uname like '".$letter."%'".str_replace ( 'where',' and',$ws_req );         
      } else if (($letter == translate("Other")) AND ($letter != translate("All"))) {
         $where = "where uname REGEXP \"^\[1-9]\" ".str_replace ( 'where',' and',$ws_req );
      } else {
         $where = "$ws_req";
      }
      global $member_invisible;
      if ($member_invisible) {
         if ($admin)
            $and="";
         else {
            if ($where)
               $and="and is_visible=1 ";
            else
               $and="where is_visible=1 ";
         }
      } else {
         $and="";
      }      
      $sort = "order by $sortby";
      $limit = " LIMIT ".$min.", ".$max;
      $count_result = sql_query($count.$where);
      list($num_rows_per_order) = sql_fetch_row($count_result);
      $result = sql_query($select.$where.$and.$sort.$limit);
      echo "<br />";
      if ( $letter != "front" ) {
         echo "<table width=\"100%\" cellspacing=\"0\" cellpadding=\"1\" border=\"0\">\n<tbody>\n<tr class=\"header\">\n";
         echo "<td class=\"box\">&nbsp;</td>\n";
         echo "<td class=\"box\"><strong>".translate("Nickname")."</strong></td>\n";
         echo "<td class=\"box\" align=\"center\">&nbsp;</td>";
         echo "<td class=\"box\"><strong>".translate("Real Name")."</strong></td>\n";
         if ($sortby!="user_from ASC") {
            echo "<td class=\"box\"><strong>".translate("Email")."</strong></td>\n";
         } else {
            echo "<td class=\"box\"><strong>".translate("Location")."</strong></td>\n";
         }
         echo "<td class=\"box\"><strong>".translate("URL")."</strong></td>\n";
         $cols = 6;
         if ($admin) {
            $cols = 7;
            echo "<td class=\"box\" align=\"center\" width=\"15%\"><strong>".translate("Functions")."</strong></td>\n";
         }
         echo "</tr>\n";
         $a = 0;
         $num_users = sql_num_rows($result);
         if ( $num_rows_per_order > 0  ) {
            global $anonymous, $user;
            while($temp_user = sql_fetch_assoc($result) ) {
               $rowcolor = tablos();
               if ($temp_user['mns']) {$mns="<a href=\"minisite.php?op=".$temp_user['uname']."\" title=\"".translate("Mini-Web site")."\" target=\"_blank\" class=\"heada\"><img src=\"images/admin/ws/application_user.gif\" class=\"vam\" alt=\"".translate("Visit the Mini Web Site !")."\" title=\"".translate("Visit the Mini Web Site !")."\" /></a>";} else {$mns="<img src=\"images/admin/ws/blank.gif\" class=\"vam\" border=\"0\" />";}
               echo "<tr $rowcolor>\n";
               echo "<td style=\"vertical-align: middle;\">";
               if ($ibid_avatar=avatar($temp_user['user_avatar']))
                  echo "<img src=\"".$ibid_avatar."\" width=\"20\" height=\"20\" border=\"0\" alt=\"Avatar\" />";
               else
                  echo "&nbsp;";
               echo "</td>\n";
               echo "<td><a href=\"user.php?op=userinfo&amp;uname=".$temp_user['uname']."\" title=\"".date(translate("dateinternal"),$temp_user['user_regdate']);
               if ($admin) 
                  echo " => ".date(translate("dateinternal"),$temp_user['user_lastvisit']);
               echo "\" class=\"heada\">".$temp_user['uname']."</a></td>\n";

               if ($temp_user['uname']!=$anonymous) {
                  if ($user) {
                     echo "<td style=\"white-space:nowrap;\">$mns&nbsp;<a href=\"replypmsg.php?send=".urlencode($temp_user['uname'])."\"><img src=\"images/admin/ws/envelope.gif\" class=\"vam\" border=\"0\" alt=\"".translate("Send internal Message")."\" title=\"".translate("Send internal Message")."\" /></a>";
                     echo "<a href=\"memberslist.php?letter=$letter&amp;sortby=$sortby&amp;list=$list".urlencode($temp_user['uname']).",&amp;page=$page&amp;gr_from_ws=$gr_from_ws\" >";
                     echo "<img src=\"images/admin/ws/modifier_add.gif\" class=\"vam\" border=\"0\" alt=\"".translate("Add to mailing list")."\" title=\"".translate("Add to mailing list")."\" /></a></td>\n";
                  } else {
                     echo "<td>$mns</td>\n";
                  }
               } else {
                  echo "<td>&nbsp;</td>\n";
               }
               echo "<td>".$temp_user['name']."&nbsp;</td>\n";
               if ($sortby!="user_from ASC") {
                  if ($admin) {
                     echo "<td>".preg_anti_spam($temp_user['email'])."&nbsp;</td>\n";
                  } else {
                     if ($temp_user['user_viewemail']) {
                        echo "<td>".preg_anti_spam($temp_user['email'])."&nbsp;</td>\n";
                     } else {
                        echo "<td>".substr($temp_user['femail'],0,strpos($temp_user['femail'],"@"))."&nbsp;</td>\n";
                     }
                  }
               } else {
                  echo "<td>".$temp_user['user_from']."&nbsp;</td>\n";
               }
               echo "<td><a href=\"".$temp_user['url']."\" target=\"_blank\" class=\"noir\">".$temp_user['url']."</a>&nbsp;</td>\n";
               if ($admin) {
                  echo "<td align=\"right\"><a href=\"admin.php?chng_uid=".$temp_user['uid']."&amp;op=modifyUser\" class=\"noir\"><img src=\"images/admin/ws/pencil.gif\" class=\"vam\" border=\"0\" alt=\"".translate("Edit")."\" title=\"".translate("Edit")." \" /></a>";
                  echo "&nbsp;<a href=\"admin.php?op=delUser&amp;chng_uid=".$temp_user['uid']."\" class=\"rouge\"><img src=\"images/admin/ws/trash.gif\" class=\"vam\" border=\"0\" alt=\"".translate("Delete")."\" title=\"".translate("Delete")."\" /></a>";
                  $op_result = sql_query("select open from ".$NPDS_Prefix."users_status where uid='".$temp_user['uid']."'");
                  list($open_user) = sql_fetch_row($op_result);
                  if ($open_user==1) {
                     echo "<img src=\"images/admin/ws/connect.gif\" class=\"vam\" border=\"0\" alt=\"".translate("Connection allowed")."\" title=\"".translate("Connection allowed")." \" />";
                  } else {
                     echo "<img src=\"images/admin/ws/disconnect.gif\" class=\"vam\" border=\"0\" alt=\"".translate("Connection not allowed")."\" title=\"".translate("Connection not allowed")." \" />";
                  }
                  if (!$temp_user['is_visible']) {
                     echo "<img src=\"images/admin/ws/user_invisible.gif\" class=\"vam\" border=\"0\" alt=\"".translate("Invisible' member")."\" title=\"".translate("Invisible' member")." \" /></td>\n";
                  } else {
                     echo "<img src=\"images/admin/ws/blank.gif\" class=\"vam\" border=\"0\" /></td>\n";
                  }
               }
               echo "</tr>\n";
            }

            if ($user) {
               echo "<tr>\n<td colspan=\"$cols\"><br /></td>\n</tr>\n";
               $rowcolor = tablos();
               echo "<tr $rowcolor>\n<td colspan=\"".($cols-1)."\" align=\"left\">\n";
               echo translate("Mailing list")." : ".urldecode($list)."</td>\n";
               echo "<td align=\"right\"><a href=\"memberslist.php?letter=$letter&amp;sortby=$sortby&amp;page=$page&amp;gr_from_ws=$gr_from_ws\"><img src=\"images/admin/ws/trash.gif\" class=\"vam\" border=\"0\" alt=\"".translate("RAZ member's list")."\" title=\"".translate("RAZ member's list")."\" /></a>";
               if ($list) {
                  echo "<a href=\"replypmsg.php?send=".substr($list,0,strlen($list)-3)."\"><img src=\"images/admin/ws/envelope.gif\" class=\"vam\" border=\"0\" alt=\"".translate("Write to the list")."\" title=\"".translate("Write to the list")."\" /></a>";
               }
            }
            echo "</td>\n</tr>\n<tr>\n<td colspan=\"$cols\" align=\"right\">\n";
            echo "<br />";
            echo "<table width=\"100%\" cellspacing=\"0\" cellpadding=\"4\" border=\"0\">\n<tr>\n";
            if ( $num_rows_per_order > $pagesize ) {
               $total_pages = ceil($num_rows_per_order / $pagesize); // How many pages are we dealing with here ??
               $prev_page = $page - 1;
               if ( $prev_page > 0 ) {
                  echo "<td width=\"15%\" class=\"header\" style=\"text-align: right;\"><a href=\"memberslist.php?letter=$letter&amp;sortby=$sortby&amp;list=$list&amp;page=$prev_page&amp;gr_from_ws=$gr_from_ws\">";
                  if ($ibid=theme_image("download/left.gif")) {$imgtmp=$ibid;} else {$imgtmp="images/download/left.gif";}
                  echo "<img src=\"$imgtmp\" border=\"0\" alt=\"".translate("Previous Page")." ($prev_page)\" /></a></td>";
               } else {
                  echo "<td width=\"15%\" class=\"header\">&nbsp;</td>\n";
               }
               echo "<td width=\"70%\" class=\"header\" style=\"text-align: center;\">";
               echo "$num_rows_per_order ".translate("users found for")." <strong>$letter</strong> ($total_pages ".translate("pages").", $num_users ".translate("users shown").").";
               echo "</td>";
               $next_page = $page + 1;
               if ( $next_page <= $total_pages ) {
                  echo "<td align=\"left\" width=\"15%\" class=\"header\"><a href=\"memberslist.php?letter=$letter&amp;sortby=$sortby&amp;list=$list&amp;page=$next_page&amp;gr_from_ws=$gr_from_ws\">";
                  if ($ibid=theme_image("download/right.gif")) {$imgtmp=$ibid;} else {$imgtmp="images/download/right.gif";}
                  echo "<img src=\"$imgtmp\" border=\"0\" alt=\"".translate("Next Page")." ($next_page)\" /></a></td>";
               } else {
                  echo "<td width=\"15%\" class=\"header\">&nbsp;</td></tr>\n";
               }
               echo "<tr><td colspan=\"3\" class=\"header\" style=\"text-align: center;\">";
               echo "[ ";
               for($n=1; $n < $total_pages; $n++) {
                  if ($n == $page) {
                     echo "<span class=\"rouge\">$n</span>";
                  } else {
                     echo "<a href=\"memberslist.php?letter=$letter&amp;sortby=$sortby&amp;list=$list&amp;page=$n&amp;gr_from_ws=$gr_from_ws\" class=\"box\">$n</a>";
                  }
                  if ($n >= 22) {  // if more than 20 pages are required, break it at 22.
                     $break = true;
                     break;
                  } else {
                     echo " | ";
                  }
               }
               if (!isset($break)) {
                  if ($n == $page) {
                     echo "<span class=\"rouge\">$n</span>";
                  } else {
                     echo "<a href=\"memberslist.php?letter=$letter&amp;sortby=$sortby&amp;list=$list&amp;page=$total_pages&amp;gr_from_ws=$gr_from_ws\" class=\"box\">$n</a>";
                  }
               }
               echo " ] ";
               echo "</td></tr>";
            } else {
               echo "<td align=\"center\">";
               echo "$num_rows_per_order ".translate("users found").".";
               echo "</td>\n</tr>\n";
            }
            echo "</table>\n";
            echo "</td>\n</tr>\n";
         } else {
            echo "<tr>\n<td colspan=\"$cols\" align=\"center\"><br />\n";
            echo "<strong>".translate("No Members Found for")." $letter</strong>\n";
            echo "<br /></td>\n</tr>\n";
         }
         echo "</tbody>\n</table>\n";
      }

   closetable();
   include("footer.php");
?>