<?php // $Id: index.php,v 1.49.2.3 2002/05/17 13:40:16 byronmhome Exp $ $Name:  $
// ----------------------------------------------------------------------
// POST-NUKE Content Management System
// Copyright (C) 2001 by the Post-Nuke Development Team.
// http://www.postnuke.com/
// ----------------------------------------------------------------------
// Based on Bjorn Sodergrens MyPHPortal Modified Member List.
// PHP-NUKE Web Portal System - http://phpnuke.org/
// Thatware - http://thatware.org/
// Some code taken from MemberList coded by Paul Joseph Thompson
// of www.slug.okstate.edu
// In memoriam of Members List War ;)
// ----------------------------------------------------------------------
// LICENSE
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License (GPL)
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// To read the license please visit http://www.gnu.org/copyleft/gpl.html
// ----------------------------------------------------------------------
// Original Author of file: Francisco Burzi
// Purpose of file: Displays member listings
// ----------------------------------------------------------------------

if (!defined("LOADED_AS_MODULE")) {
    die ("You can't access this file directly...");
}

/**
 * Credits to Edgar Miller -- http://www.bosna.de/ from his post on PHP-Nuke 
 * (http://phpnuke.org/article.php?sid=2010&mode=nested&order=0&thold=0)
 * Further Credits go to Djordjevic Nebojsa (nesh) for the fix for the fix 
 */

$ModName = basename( dirname( __FILE__ ) );

modules_get_language();

function alpha()
{
    // Creates the list of letters and makes them a link.
    global $sortby, $ModName;
        $alphabet = array (_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",_OTHER);
        $num = count($alphabet) - 1;
        echo "<center>[ ";
        $counter = 0;
        while(list(, $ltr) = each($alphabet)) {
            echo "<a class=\"pn-normal\" href=\"modules.php?op=modload&amp;name=$ModName&amp;file=index&amp;letter=$ltr&amp;sortby=$sortby\">".pnVarPrepHTMLDisplay($ltr)."</a>";
            if ($counter == round($num/2)) {
                echo " ]\n<br>\n[ ";
            } elseif($counter != $num) {
                echo "&nbsp;|&nbsp;\n";
            }
            $counter++;
        }
        echo " ]\n</center>\n<br>\n";
}

function SortLinks($letter)
{
    // Makes order by links..
    global $sortby, $ModName;

    if ($letter == "front") {
	$letter = _ALL;
    }
        echo "\n<center>\n";
        echo "<font class=\"pn-normal\"><b>"._SORTBY."&nbsp;[</b> </font>";
        if ($sortby == "uname" OR !$sortby) {
            echo "<font class=\"pn-normal\"><b>"._MNICKNAME."&nbsp;|&nbsp;</b></font>";
        } else {
            echo "<a class=\"pn-normal\" href=\"modules.php?op=modload&amp;name=$ModName&amp;file=index&amp;letter=$letter&amp;sortby=uname\"><b>"._MNICKNAME."</b></a><b>&nbsp;|&nbsp;</b>";
        }
        if ($sortby == "name") {
            echo "<font class=\"pn-normal\"><b>"._MREALNAME."&nbsp;|&nbsp;</b></font>";
        } else {
            echo "<a class=\"pn-normal\" href=\"modules.php?op=modload&amp;name=$ModName&amp;file=index&amp;letter=$letter&amp;sortby=name\"><b>"._MREALNAME."</b></a><b>&nbsp;|&nbsp;</b>";
        }
        if ($sortby == "femail") {
            echo "<font class=\"pn-normal\"><b>"._MEMAIL."&nbsp;|&nbsp;</b></font>";
        } else {
            echo "<a class=\"pn-normal\" href=\"modules.php?op=modload&amp;name=$ModName&amp;file=index&amp;letter=$letter&amp;sortby=femail\"><b>"._MEMAIL."</b></a><b>&nbsp;|&nbsp;</b>";
        }
        if ($sortby == "url") {
            echo "<font class=\"pn-normal\"><b>"._MURL."&nbsp;</b></font>";
        } else {
            echo "<a class=\"pn-normal\" href=\"modules.php?op=modload&amp;name=$ModName&amp;file=index&amp;letter=$letter&amp;sortby=url\"><b>"._MURL."</b></a><b>&nbsp;</b>";
        }
        echo "<b>]</b>\n</center>\n";
}

include 'header.php';
    if (!pnSecAuthAction(0, 'Users::', '::', ACCESS_READ)) {
        echo _MODIFYUSERSNOAUTH;
        include 'footer.php';
        return;
    }

$pagesize = 20;

if (!isset($letter)) {
    $letter = "A";
}
if (!isset($sortby)) {
    $sortby = "uname";
}
if (!isset($page)) {
    $page = 1;
}


 // TODO
 // All of the code from here to around line 125 will be optimized a little later
 // This is the header section that displays the last registered and who's logged in and whatnot
list($dbconn) = pnDBGetConn();
$pntable = pnDBGetTables();

$column = &$pntable['users_column'];
$myquery = buildSimpleQuery ('users', array ('uname'), '', "$column[uid] DESC", 1);
$result = $dbconn->Execute($myquery);
list($lastuser) = $result->fields;

echo "\n\n<!-- MEMBERS LIST -->\n\n";
    OpenTable();
        echo "<center><font class=\"pn-pagetitle\">".pnConfigGetVar('sitename')." "._MEMBERSLIST."</font></center>\n";
    CloseTable();

     echo "<font class=\"pn-normal\"><b>"._GREETINGS."</b></font>
     <center><a class=\"pn-normal\" href=\"user.php?op=userinfo&amp;uname=$lastuser\"><b>".pnVarPrepHTMLDisplay($lastuser)."</b></a>\n</center>\n<br>\n";
        $result = $dbconn->Execute("SELECT COUNT(*) FROM $pntable[users]");
        list($numrows) = $result->fields;
        $result->Close();

        if(pnSecAuthAction(0, 'Users::', '::', ACCESS_COMMENT)){
            $sessioninfocolumn = &$pntable['session_info_column'];
            $sessioninfotable = $pntable['session_info'];

            $activetime = time() - (pnConfigGetVar('secinactivemins') * 60);

            // Get list of users on-line
            $sql = "SELECT DISTINCT $sessioninfocolumn[uid]
                    FROM $sessioninfotable
                    WHERE $sessioninfocolumn[uid] != 0
                    AND $sessioninfocolumn[lastused] > $activetime";
            $result3 = $dbconn->Execute($sql);
            $unames = array();
            for (; !$result3->EOF; $result3->MoveNext()) {
                $unames[] = pnUserGetVar('uname', $result3->fields[0]);
            }
            $result3->Close();
            // Sort by username
            sort($unames);
            reset($unames);
            $numregusers = count($unames);

            if ($numregusers != 0) {
                OpenTable2();
                echo '<font class="pn-normal">'._ONLINEREG.'</font><br /><br /><center>';
                foreach ($unames as $uname) {
                    echo '<a class="pn-normal" href="user.php?op=userinfo&amp;uname=' . $uname . '">' .pnVarPrepForDisplay($uname).'</a><br />';
                }
                echo '</center>';
                CloseTable2();
            }

            echo "<center><font class=\"pn-normal\">"._WEHAVE." ".pnVarPrepHTMLDisplay($numrows)." "._MREGISTERED." ".pnVarPrepHTMLDisplay($numregusers)."</font>\n";
            echo " <font class=\"pn-normal\">"._MREGONLINE."</font></center><br>";
        }

        alpha();
        SortLinks($letter);

// end of top memberlist section thingie
// This starts the beef...

        $min = $pagesize * ($page - 1); // This is where we start our record set from
        $max = $pagesize; // This is how many rows to select

        $column = &$pntable['users_column'];
        $count = "SELECT COUNT($column[uid]) FROM $pntable[users] ";
	// Count all the users in the db..
        if (($letter != _OTHER) AND ($letter != _ALL)) {
	    // are we listing all or "other" ?
            $where = "UPPER($column[uname]) LIKE UPPER('".$letter."%') ";
	    // I guess we are not..
        } else if (($letter == _OTHER) AND ($letter != _ALL)) {
	    // But other is numbers ?
            $where = "($column[uname] LIKE '0%'
                          OR $column[uname] LIKE '1%'
                          OR $column[uname] LIKE '2%'
                          OR $column[uname] LIKE '3%'
                          OR $column[uname] LIKE '4%'
                          OR $column[uname] LIKE '5%'
                          OR $column[uname] LIKE '6%'
                          OR $column[uname] LIKE '7%'
                          OR $column[uname] LIKE '8%'
                          OR $column[uname] LIKE '9%'
                          OR $column[uname] LIKE '-%'
                          OR $column[uname] LIKE '.%'
                          OR $column[uname] LIKE '@%'
                          OR $column[uname] LIKE '$%')";

            // fifers: while this is not the most eloquent solution, it is
            // cross database compatible.  We could do an if dbtype is mysql
            // then do the regexp.  consider for performance enhancement.
            //
            // "WHERE $column[uname] REGEXP \"^\[1-9]\" "
            // REGEX :D, although i think its MySQL only
            // Will have to change this later.
            // if you know a better way to match only the first char
            // to be a number in uname, please change it and email
            // sweede@gallatinriver.net the correction
            // or go to post-nuke project page and post
            // your correction there. Thanks, Bjorn.
        } else { // or we are unknown or all..
            $where = ""; // this is to get rid of anoying "undefinied variable" message
        }
        $sort = "$sortby ASC"; //sorty by .....

	// This is where we get our limit'd result set.
        $myquery = buildSimpleQuery('users', array('uid', 'name', 'uname', 'femail', 'url'), $where, $sort, $max, $min); //select our data

        $result = $dbconn->Execute($myquery);
        if ($result === false) {
            error_log("Error: " . $dbconn->ErrorNo() . ": " . $dbconn->ErrorMsg());
           PN_DBMsgError($dbconn, __FILE__, __LINE__, "Error accesing to the database");
        }
        $num_rows_per_order = $result->PO_RecordCount();
        echo "<br>";
        if ($letter != "front") {
            echo "<table width=\"100%\" border=\"0\" cellpadding=\"3\" cellspacing=\"1\"><tr>\n";
            echo "<td BGCOLOR=\"$bgcolor2\" align=\"center\"><font class=\"pn-normal\"><b>"._MNICKNAME."</b></font></td>\n";
            echo "<td BGCOLOR=\"$bgcolor2\" align=\"center\"><font class=\"pn-normal\"><b>"._REALNAME."</b></font></td>\n";
            echo "<td BGCOLOR=\"$bgcolor2\" align=\"center\"><font class=\"pn-normal\"><b>"._EMAIL."</b></font></td>\n";
            echo "<td BGCOLOR=\"$bgcolor2\" align=\"center\"><font class=\"pn-normal\"><b>"._HOMEPAGE."</b></font></td>\n";
            $cols = 4;
            if (pnSecAuthAction(0, 'Users::', '::', ACCESS_EDIT)){
                $cols = 5;
                echo "<td bgcolor=\"$bgcolor2\" align=\"center\"><font class=\"pn-normal\"><b>"._FUNCTIONS."</b></font></td>\n";
            }
            echo "</tr>";
            $a = 0;

            $dcolor_A = "$bgcolor3";
            $dcolor_B = "$bgcolor1";

            $num_users = $result->PO_RecordCount(); //number of users per sorted and limit query
            if($num_rows_per_order > 0 ) {
                while(!$result->EOF) {
                    $user = $result->GetRowAssoc(false);
                    $result->MoveNext();
                    $dcolor = ($a == 0 ? $dcolor_A : $dcolor_B);
                    echo "<tr><td bgcolor=\"$dcolor\"><a class=\"pn-normal\" href=\"user.php?op=userinfo&amp;uname=$user[uname]\">".pnVarPrepHTMLDisplay($user['uname'])."</a>&nbsp;</td>\n";
                    echo "<td bgcolor=\"$dcolor\"><font class=\"pn-normal\">".pnVarPrepHTMLDisplay($user['name'])."&nbsp;</font></td>\n";
                    echo "<td bgcolor=\"$dcolor\"><font class=\"pn-normal\">".$user['femail']."&nbsp;</font></td>\n";
                    echo "<td bgcolor=\"$dcolor\"><a class=\"pn-normal\" href=\"";

	    	    if(eregi("http://", $user['url']) || (empty($user['url'])))  {
			$user['url'] = '';
	    	    }
	    if (!empty($user['url'])) {
            echo "".$user['url']."\" target=new>".pnVarPrepHTMLDisplay($user['url'])."</a>&nbsp;</td>\n"; 
            }
                    if (pnSecAuthAction(0, 'Users::', '::', ACCESS_EDIT)){
			$authid = pnSecGenAuthKey();
                        echo "<td bgcolor=$dcolor align=center><font class=\"pn-normal\">[ <a class=\"pn-normal\" href=\"admin.php?module=NS-User&amp;op=modifyUser&amp;chng_uid=$user[uid]&amp;authid=$authid\"><font class=\"pn-normal\">"._EDIT."</font></a><font class=\"pn-sub\"> | </font>\n";
                        echo "<a class=\"pn-normal\" href=\"admin.php?module=NS-User&amp;op=delUser&amp;chng_uid=$user[uid]&amp;authid=$authid\"><font class=\"pn-normal\">"._DELETE."</font></a> ]</font></td>\n";
                    }
                    echo "</tr>";
                    $a = ($dcolor == $dcolor_A ? 1 : 0);
                }
		// start of next/prev/row links.
                echo "\n<tr><td colspan='$cols' align='right'>\n";

        OpenTable();
                echo "\t<table width='100%' cellspacing='0' cellpadding='0' border=0><tr>";
                if (!empty($where)) {
                    $where = " WHERE $where";
                } else {
                    $where = '';
                }

                $resultcount = $dbconn->Execute($count . $where);
                list ($numrows) = $resultcount->fields;
                $resultcount->Close();

                if ($numrows > $pagesize) {
                    $total_pages = ceil($numrows / $pagesize); // How many pages are we dealing with here ??
                    $prev_page = $page - 1;

                    if ( $prev_page > 0 ) {
                        echo "<td align='left' width='15%'><a class=\"pn-normal\" href='modules.php?op=modload&amp;name=$ModName&amp;file=index&amp;letter=$letter&amp;sortby=$sortby&amp;page=$prev_page'>";
                        echo "<img src=\"modules/$ModName/images/left.gif\" border=\"0\" Alt=\""._PREVIOUS." (".pnVarPrepHTMLDisplay($prev_page).")\"></a></td>";
                    } else {
                        echo "<td width='15%'>&nbsp;</td>\n";
                    }

                    echo "<td align='center' width='70%'>";
                    echo "<font class=\"pn-sub\">$numrows "._USERSFOUND." $letter ($total_pages "._PAGES.", $num_users "._USERSSHOWN.")</font>";
                    echo "</td>";

                    $next_page = $page + 1;
                    if ( $next_page <= $total_pages ) {
                        echo "<td align='right' width='15%'><a class=\"pn-normal\" href='modules.php?op=modload&amp;name=$ModName&amp;file=index&amp;letter=$letter&amp;sortby=$sortby&amp;page=$next_page'>";
                        echo "<img src=\"modules/$ModName/images/right.gif\" border=\"0\" Alt=\"Next Page ($next_page)\"></a></td>";
                    } else {
                        echo "<td width='15%'>&nbsp;</td></tr>\n";
                    }
			// Added a numbered page list, only shows up to 50 pages.
                        echo "<tr><td colspan=\"3\" align=\"center\">";
                        echo " <font class=\"pn-sub\">[ </font>";

                        for($n=1; $n < $total_pages; $n++) {
                            if ($n == $page) {
                		echo "<font class=\"pn-sub\">$n</font></a>";
                        } else {
                echo "<a class=\"pn-normal\" href='modules.php?op=modload&amp;name=$ModName&amp;file=index&amp;letter=$letter&amp;sortby=$sortby&amp;page=$n'>";
                echo "<font class=\"pn-sub\">".pnVarPrepHTMLDisplay($n)."</font></a>";
                }
                            if($n >= 50) {  // if more than 50 pages are required, break it at 50.
                                $break = true;
                                break;
                            } else {  // guess not.
                                echo "<font class=\"pn-sub\"> | </font>";
                            }
                        }

                        if(!isset($break)) { // are we supposed to break ?
                if($n == $page) {
                    echo "<font class=\"pn-sub\">".pnVarPrepHTMLDisplay($n)."</font></a>";
                } else {
                    echo "<a class=\"pn-normal\" href='modules.php?op=modload&amp;name=$ModName&amp;file=index&amp;letter=$letter&amp;sortby=$sortby&amp;page=$total_pages'>";
                    echo "<font class=\"pn-sub\">".pnVarPrepHTMLDisplay($n)."</font></a>";
                }
                        }
                        echo " <font class=\"pn-sub\">]</font> ";
                        echo "</td></tr>";

// This is where it ends

                } else {  // or we dont have any users..
                    echo "<td align='center'>";
                    echo "<font class=\"pn-sub\">".pnVarPrepHTMLDisplay($num_rows_per_order)." "._USERSFOUND." ".pnVarPrepHTMLDisplay($letter)."</font>";
                    echo "</td></tr>";
                 }
                 echo "</table>\n";
         CloseTable();
                echo "</td></tr>\n";

// end of next/prev/row links

            } else { // no members for this letter.
                echo "<tr><td bgcolor=\"$dcolor_A\" colspan=\"$cols\" align=\"center\"><br>\n";
                echo "<font class=\"pn-normal\">"._NOMEMBERS." ".pnVarPrepHTMLDisplay($letter)."</font>\n";
                echo "<br></td></tr>\n";
            }
            echo "\n</table><br>\n";
        }

        include 'footer.php';
?>
