<?php
/**
 * $Source: /home/cvs/nukescripts/kernel/users.php,v $
 * $Revision: 1.1.0a $
 * $Author: cyril $
 * $Date: 2006/06/19 21:03:12 $
 *
 */
/************************************************************************/
/* Maximus CMS: Web Portal System                                       */
/* ==============================                                       */
/*                                                                      */
/* Copyright (c) 2002 - 2006                                            */
/* http://www.php-maximus.fr                                            */
/* dev@php-maximus.org                                                  */
/*                                                                      */
/* 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.       */
/************************************************************************/
/* Based on PHP-NUKE: Web Portal System                                 */
/************************************************************************/
defined('MAXIMUS') or die ('Access denied users MAXIMUS directly...');

define('NSNGROUPS_IS_LOADED', TRUE);

function is_user($user) {
    if (empty($user)) { return NULL; }
    if (isset($userSave)) return $userSave;
    if (!is_array($user)) {
        $user = base64_decode($user);
        $user = addslashes($user);
        $user = explode(":", $user);
    }
    $uid = $user[0];
    $pwd = $user[2];
    $uid = $GLOBALS['db']->XMFnum($uid);
    if (!empty($uid) AND !empty($pwd)) {
        $sql = "SELECT `user_password` FROM `".TABLE_USERS."` WHERE (`user_id`='$uid' AND `user_active`='1' AND `user_level`>='1' ) ";
        if ( !($result = $GLOBALS['db']->sql_query($sql)) )
{ query_die(CORE_ERROR, 'Could not obtain user_password _users', '', __LINE__, __FILE__, $sql);}
        $row = $GLOBALS['db']->sql_fetchrow($result);
        $GLOBALS['db']->sql_freeresult($result);
        if ($row[0] == $pwd && !empty($row[0])) {
            static $userSave;
            return $userSave = 1;
        }
    }
    static $userSave;
    return $userSave = 0;
}
if (is_user($user)) { define('USER_ALLOW', true); }


function in_group($gid) {
    if (empty($gid)) { return NULL; }
    global $user, $cookie;
    /*if (is_admin($admin)) {
        return 1;
    } else*/if (defined('USER_ALLOW')) {
        cookiedecode($user);
        $guid     = $cookie[0];
        $currdate = time();
        $ingroup  = $GLOBALS['db']->sql_numrows($GLOBALS['db']->sql_query("SELECT `gid` FROM `".TABLE_NSNGR_USERS."` WHERE `gid`='$gid' AND `uid`='$guid' AND (edate>'$currdate' OR edate='0')"));
        if ($ingroup > 0) { return 1; }
    }
    return 0;
}

function in_groups($gids) {
    if (empty($gids)) { return NULL; }
    global $user, $cookie;
    if(!is_array($gids)) { $gids = explode("-",$gids); }
    /*if (is_admin($admin)) {
        return 1;
    } else*/if (defined('USER_ALLOW')) {
        cookiedecode($user);
        $guid     = $cookie[0];
        $currdate = time();
        for ($i=0; $i < count($gids); $i++) {
            $ingroup = $GLOBALS['db']->sql_numrows($GLOBALS['db']->sql_query("SELECT `gid` FROM `".TABLE_NSNGR_USERS."` WHERE `gid`='".$gids[$i]."' AND `uid`='$guid' AND (edate>'$currdate' OR edate='0')"));
            if ($ingroup > 0) { return 1; }
        }
    }
    return 0;
}

function is_ingroup($guid,$gid) {
    if ((empty($gid)) OR (empty($guid))) { return NULL; }
    $isgroup = $GLOBALS['db']->sql_numrows($GLOBALS['db']->sql_query("SELECT `gid` FROM `".TABLE_NSNGR_USERS."` WHERE `gid`='$gid' AND `uid`='$guid'"));
    if ($isgroup > 0) { return 1; }
    return 0;
}

function grget_config($config_name){
    if (empty($config_name)) { return NULL; }
    $configresult = $GLOBALS['db']->sql_query("SELECT `config_value` FROM `".TABLE_NSNGR_CONFIG."` WHERE `config_name`='$config_name'");
    $raw          = $GLOBALS['db']->sql_fetchrow($configresult);
    $config_value = stripslashes($raw['config_value']);
    return $config_value;
}

function grget_configs(){
    $configresult = $GLOBALS['db']->sql_query("SELECT `config_name`, `config_value` FROM `".TABLE_NSNGR_CONFIG."`");
    while ($raw1  = $GLOBALS['db']->sql_fetchrow($configresult)) {
    $config_name  = stripslashes($raw1['config_name']);
    $config_value = stripslashes($raw1['config_value']);
        $config[$config_name] = $config_value;
    }
    return $config;
}

// compatibility phpnuke
function is_group($user, $name) {
}

function cookiedecode($user) {
   if (empty($user)) { return NULL; }
   global $cookie;
    static $pass;
    if(!is_array($user)) {
        $user   = base64_decode($user);
        $user   = addslashes($user);
        $cookie = explode(":", $user);
    } else {
        $cookie = $user;
    }
    if (!isset($pass)) {
       $result = $GLOBALS['db']->sql_query("SELECT `user_password` FROM `".TABLE_USERS."` WHERE ( `username`='$cookie[1]'  AND `user_active`='1' AND `user_level`>='1' ) ");
       $row    = $GLOBALS['db']->sql_fetchrow($result);
       $pass   = stripslashes($row['user_password']);
       $GLOBALS['db']->sql_freeresult($result);
    }
    if ($cookie[2] == $pass && !empty($pass)) { return $cookie; }
}

function public_message(){
}

function getusrinfo($user) {
    if (empty($user)) { return NULL; }
    global $userinfo, $cookie;
    cookiedecode($user);
    $user = $cookie;
    if (isset($userrow) AND is_array($userrow)) {
        if ($userrow['username'] == $user[1] && $userrow['user_password'] == $user[2]) {
            return $userrow;
        }
    }
    $sql = "SELECT * FROM `".TABLE_USERS."` WHERE `username`='$user[1]' AND `user_password`='$user[2]'";
    $result = $GLOBALS['db']->sql_query($sql);
    if ($GLOBALS['db']->sql_numrows($result) == 1) {
        static $userrow;
        $userrow = $GLOBALS['db']->sql_fetchrow($result);
        return $userinfo = $userrow;
    }
    $GLOBALS['db']->sql_freeresult($result);
    unset($userinfo);
}

if (isset($livemembers)) {
if (is_file('kernel/addons/livemembers.php')) {
include_once (INCLUDE_PATH.'kernel/addons/livemembers.php');}
die();
}