<?php

/**
 * ECSHOP phpBB论坛整合插件(phpbb 2.0.x系列)
 * ============================================================================
 * 版权所有 (C) 2005-2007 北京亿商互动科技发展有限公司，并保留所有权利。
 * 网站地址: http://www.ecshop.com
 * ----------------------------------------------------------------------------
 * 这是一个免费开源的软件；这意味着您可以在不用于商业目的的前提下对程序代码
 * 进行修改、使用和再发布。
 * ============================================================================
 * @author:     liry <liry123@hotmail.com>
 * @version:    v2.1
 * ---------------------------------------------
 * $Author: paulgao $
 * $Date: 2007-05-22 16:06:08 +0800 (星期二, 22 五月 2007) $
 * $Id: phpbb.php 8706 2007-05-22 08:06:08Z paulgao $
 */

if (!defined('IN_ECS'))
{
    die('Hacking attempt');
}

/* 模块的基本信息 */
if (isset($set_modules) && $set_modules == TRUE)
{
    $i = (isset($modules)) ? count($modules) : 0;

    /* 会员数据整合插件的代码必须和文件名保持一致 */
    $modules[$i]['code']    = "phpbb";

    /* 被整合的第三方程序的名称 */
    $modules[$i]['name']    = "phpBB";

    /* 被整合的第三方程序的版本 */
    $modules[$i]['version'] = "2.0.x";

    /* 插件的作者 */
    $modules[$i]['author']  = "ECSHOP R&D TEAM";

    /* 插件作者的官方网站 */
    $modules[$i]['website'] = "http://www.ecshop.com";

    return;
}

class phpbb
{
    /*------------------------------------------------------ */
    //-- PRIVATE ATTRIBUTEs
    /*------------------------------------------------------ */

    /* 整合对象使用的数据库主机 */
    var $db_host        = 'localhost';

    /* 整合对象使用的数据库名 */
    var $db_name        = 'phpbb';

    /* 整合对象使用的数据库用户名 */
    var $db_user       = 'root';

    /* 整合对象使用的数据库密码 */
    var $db_pass       = '';

    /* 整合对象数据库使用字符集 */
    var $db_charset    = 'UTF8';

    /* 字符集转换对象 */
    var $iconv         = '';

    /* 整合对象数据表前缀 */
    var $prefix        = 'phpbb_';

    /* 整合对象使用的cookie的domain */
    var $cookie_domain = '';

    /* 整合对象使用的cookie的path */
    var $cookie_path   = '/';

    /* 连接第三方数据库的MYSQL对象 */
    var $db;

    /* 会员ID的字段名 */
    var $field_id       = 'user_id';

    /* 会员名称的字段名 */
    var $field_name     = 'username';

    /* 会员密码字段名 */
    var $field_pass     = 'user_password';

    /* 会员邮件的字段名 */
    var $field_email    = 'user_email';

    /* 注册日期的字段名 */
    var $field_reg_date = 'user_regdate';

    /* 最后访问的字段名 */
    var $user_lastvisit = 'user_lastvisit';

    /* 保存整合过程中出现的错误 */
    var $error          = 0;

    /*------------------------------------------------------ */
    //-- PUBLIC METHODs
    /*------------------------------------------------------ */

    /**
     * 会员数据整合插件类的构造函数。
     *
     * @access      public
     * @param       string  $db_host        数据库主机
     * @param       string  $db_name        数据库名
     * @param       string  $db_user        数据库用户名
     * @param       string  $db_pass        数据库密码
     * @param       string  $prefix         数据表前缀名
     * @param       string  $cookie_domain  COOKIE域
     * @param       string  $cookie_path    COOKIE路径
     * @param       string  $db_charset     数据库使用的编码
     * @return      void
     */
    function phpbb($db_host='localhost', $db_name='', $db_user='root', $db_pass='', $prefix='phpbb_', $cookie_domain='', $cookie_path='/', $db_charset='UTF8')
    {
        $this->db_host          = $db_host;
        $this->db_name          = $db_name;
        $this->db_charset       = $db_charset;
        $this->db_user          = $db_user;
        $this->db_pass          = $db_pass;
        $this->prefix           = $prefix;
        $this->cookie_domain    = $cookie_domain;
        $this->cookie_path      = $cookie_path;

        /* 开始连接数据库 */
        $this->db = new cls_mysql($db_host, $db_user, $db_pass, $db_name);
        $this->db->set_mysql_charset($this->db_charset);

        /* 如果整合对象使用的字符集不为UTF-8，则初始化转码对象 */
        if ($db_charset != 'UTF8')
        {
            include_once(ROOT_PATH . 'includes/iconv/cls_iconv.php');
            $this->iconv =  new Chinese(ROOT_PATH);
        }
    }

    /**
     * 获得在整合过程中出现的最后一个错误信息。
     *
     * @access  public
     * @return  void
     */
    function error_msg()
    {
        return $this->db->ErrorMsg();
    }

    /**
     * 根据用户名、密码验证用户身份的函数
     *
     * @access      public
     * @param       string      username    用户名
     * @param       string      password    登录密码
     * @return bool
     */
    function login($username, $password)
    {
        /* 字符集转换 */
        $uname = $this->convert_to($username);
        $password = $this->compile_password($password);

        /* 根据用户名和密码查找该用户 */
        $sql = "SELECT " .$this->field_id. ", " .$this->field_name. ", " .$this->field_email.", ".$this->user_lastvisit.
               " FROM " .$this->table('users').
               " WHERE " .$this->field_name. " = '$uname' AND " .$this->field_pass. " = '$password'";
        $row = $this->db->getRow($sql);
        if ($row)
        {
            $auto_login_key = $this->dss_rand() . $this->dss_rand();
            $auto_login_key = md5($auto_login_key);

            /* 向整合对象的数据表里写入cookie值 */
            $this->db->query("INSERT INTO " .$this->table('sessions_keys')." (key_id, user_id, last_login) ".
                   "VALUES ('" .$auto_login_key. "', '".$row[$this->field_id]."', '".time()."')");

            $client_ip = ( !empty($HTTP_SERVER_VARS['REMOTE_ADDR']) ) ? $HTTP_SERVER_VARS['REMOTE_ADDR'] : ( ( !empty($HTTP_ENV_VARS['REMOTE_ADDR']) ) ? $HTTP_ENV_VARS['REMOTE_ADDR'] : getenv('REMOTE_ADDR') );
            $sql = "INSERT INTO ".$this->table('sessions')." (session_id, session_user_id, session_start, session_time, session_ip, session_logged_in, session_admin) VALUES('$auto_login_key', '".$row[$this->field_id]."','".time()."','".time()."','".$this->encode_ip($client_ip)."',1, 0)";
            $this->db->query($sql);

            $this->set_cookie($row[$this->field_id],  $auto_login_key);
            $this->set_user_session($row[$this->field_id], $username, $row[$this->field_email], $row[$this->user_lastvisit]);
            $this->user_sync($row[$this->field_id], $username);

            return true;
        }
        else
        {
            return false;
        }
    }

    /**
     * 用户退出
     *
     * @access      public
     * @return      void
     */
    function logout()
    {
        $cookie_key  = addslashes_deep(@unserialize(stripslashes_deep($_COOKIE[$this->cookie_name() . '_data'])));
        $session_key = @$cookie_key['autologinid'];
        $session_id  = isset($_COOKIE[$this->cookie_name() . '_sid']) ? stripslashes_deep($_COOKIE[$this->cookie_name() . '_sid']) : '';

        $sql = "UPDATE ".$this->table('sessions')." SET session_user_id = -1, session_logged_in = 0 WHERE session_id='$session_id'";
        $this->db->query($sql);

        // Remove this auto-login entry (if applicable)
        if (isset($session_key) && $session_key != '' )
        {
            $autologin_key = md5($session_key);
            $sql = "DELETE FROM ". $this->table('sessions_keys')." WHERE user_id = '".$_SESSION['user_id']."' AND key_id = '".$autologin_key."'";
            $this->db->query($sql);
        }

        setcookie($this->cookie_name() . '_data', '', time() - 31536000, $this->cookie_path, $this->cookie_domain);
        setcookie($this->cookie_name() . '_sid', '', time() - 31536000, $this->cookie_path, $this->cookie_domain);

        /* 清除session */
        $GLOBALS['sess']->destroy_session();
    }

    /**
     * 根据cookie来验证用户身份的函数
     *
     * @access      public
     * @return      void
     */
    function get_cookie()
    {
        if (isset($_COOKIE[$this->cookie_name() . '_data']) || isset($_COOKIE[$this->cookie_name() . '_sid']))
        {
            /* 序列化cookie,取得用户信息 */
            $cookie_data = addslashes_deep(@unserialize(stripslashes_deep($_COOKIE[$this->cookie_name() . '_data'])));

            /* 如果没有设置$cookie_data['autologinid'] 将不能验证用户是否合法，忽略该cookie */
            if (empty($cookie_data['autologinid']))
            {
                return false;
            }

            /* 验证用户合法性 */
            $sql = "SELECT COUNT(*) FROM ".$this->table("sessions_keys")." WHERE user_id='".$cookie_data['userid']."' AND key_id='".md5($cookie_data['autologinid'])."'";

            if ($this->db->GetOne($sql) != 1)
            {
                return false;
            }

            /* 查询登录用户的信息 */
            $sql = "SELECT " .$this->field_id. ", " .$this->field_name. ", " .$this->field_email. ", " .$this->field_pass.", ".$this->user_lastvisit.
                    " FROM ".$this->table('users').
                    " WHERE " .$this->field_id. " = '".$cookie_data['userid']."'";
            $row=$this->db->getRow($sql);

            if ($row)
            {
                $autologin_key = md5($cookie_data['autologinid']);
                $this->set_cookie($row[$this->field_id],  $auto_login_key);

                $row[$this->field_name] = $this->convert($row[$this->field_name]);
                $this->set_user_session($row[$this->field_id], $row[$this->field_name], $row[$this->field_email], $row[$this->user_lastvisit]);
                $this->user_sync($row[$this->field_id], $row[$this->field_name]);

                return true;
            }
            else
            {
                return false;
            }
        }

        return false;
    }

    /**
     * 添加新用户的函数
     *
     * @access      public
     * @param       string      username    用户名
     * @param       string      password    登录密码
     * @param       string      email       邮件地址
     * @return      int         返回最新的ID
     */
    function add_user($username, $password, $email)
    {
        $username = $this->convert_to($username);

        /* 检查用户名是否已经存在 */
        $sql = "SELECT COUNT(*)+1 FROM " .$this->table('users'). " WHERE " .$this->field_name. " ='$username'";
        if ($this->db->getOne($sql) == 2)
        {
            $this->error = ERR_USERNAME_EXISTS;
            return false;
        }

        /* 检查邮件地址是否重复 */
        $sql = "SELECT COUNT(*)+1 FROM " .$this->table('users'). " WHERE " .$this->field_email. " = '$email'";
        if ($this->db->getOne($sql) == 2)
        {
            $this->error = ERR_EMAIL_EXISTS;
            return false;
        }

        /* 编译密码 */
        $password = $this->compile_password($password);

        /* 获取用户id */
        $user_id = $this->db->getOne("SELECT MAX(user_id)+1 AS total FROM ".$this->table('users'));

        /* 插入数据库 */
        $sql = "INSERT INTO ".$this->table('users')." (".$this->field_id.",".$this->field_name.", ".$this->field_pass.", ".$this->field_email.", user_regdate) VALUES ('$user_id', '$username', '$password', '$email', '".time()."')";
        $result = $this->db->query($sql);
        if ($result)
        {
            return $user_id;
        }
        else
        {
            $this->error = ERR_USERNAME_EXISTS;
            return false;
        }
    }

    /**
     * 编辑用户帐号信息的函数
     *
     * @access      public
     * @param       int         $user_id        用户编号
     * @param       string      $username       用户名
     * @param       string      $email          邮件地址
     * @return      bool
     */
    function edit_user($user_id, $username='', $email='')
    {
        $fields = '';
        if (!empty($username))
        {
            $username = $this->convert_to($username);

            /* 检查用户名是否重复 */
            $sql = "SELECT COUNT(*)+1 FROM " .$this->table('users'). " ".
                   "WHERE " .$this->field_name. " = '$username' AND " .$this->field_id. " <> $user_id";
            $num = $this->db->getOne($sql);

            if ($num > 1)
            {
                $this->error = ERR_USERNAME_EXISTS;

                return false;
            }
            else
            {
                $fields .= $this->field_name . "='$username'";
            }
        }

        if (!empty($email))
        {
            /* 检查邮件地址是否重复 */
            $sql = "SELECT COUNT(*)+1 FROM ".$this->table('users')." ".
                   "WHERE " .$this->field_email. " = '$email' AND " .$this->field_id. " <> '$user_id'";
            $num = $this->db->getOne($sql);

            if ($num > 1)
            {
                $this->error = ERR_EMAIL_EXISTS;
                return false;
            }
            else
            {
                $fields .= (empty($fields)) ? '' : ',';
                $fields .= $this->field_email. " = '$email'";
            }
        }

        $sql = "UPDATE ".$this->table('users')." SET $fields WHERE " .$this->field_id. " = '$user_id'";

        $this->db->query($sql);

        return true;
    }

    /**
     * 删除用户帐号
     *
     * @access      public
     * @param       mix     users   用户编号
     * @return      bool
     */
    function remove_user($users)
    {
        $where = is_array($users) ? $this->field_id . db_create_in($users) : $this->field_id . " = '$users'";

        /* 删除相应数据表的信息 */
        $sql = "DELETE FROM ".$this->table('users')." WHERE $where";
        $this->db->query($sql);

        return true;
    }

    /**
     * 编辑用户密码的函数
     * 在这个函数里判断用户输入的原来的密码是否正确，如果不正确则返回FALSE
     *
     * @access      public
     * @param       int         user_id         用户编号
     * @param       string      password        原来的登录密码
     * @param       string      new_password    新的登录密码
     * @return      void
     */
    function edit_password($user_id, $password, $new_password)
    {
        $sql = "SELECT COUNT(*) FROM ".$this->table('users').
               " WHERE " .$this->field_id. " = '$user_id' AND ".$this->field_pass." = '".$this->compile_password($password)."'";
        if ($this->db->getOne($sql) == 1)
        {
            if (!$this->update_password($user_id, $new_password))
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        else
        {
            return false;
        }
    }

    /**
     * 找回用户密码的函数
     *
     * @access      public
     * @param       int         user_id         用户编号
     * @param       string      new_password    新的登录密码
     * @param       string      code            验证串
     * @return      void
     */
    function fetch_password($user_id, $code, $new_password)
    {
        /* 比较code是否合法 */
        $sql = "SELECT " .$this->field_pass. " FROM " .$this->table('users')
            . " WHERE " .$this->field_id. " = '$user_id'";
        $row = $this->db->getRow($sql);

        if (empty($row))
        {
            return false;
        }

        $password = $row['user_password'];

        $md5_password = md5($user_id . $password);

        if ($md5_password <> $code)
        {
            return false;
        }

        if (!$this->update_password($user_id, $new_password))
        {
            return false;
        }
        else
        {
            return true;
        }
    }

    /**
     * 更新用户密码的函数
     *
     * @access      private
     * @param       int         user_id         用户编号
     * @param       string      new_password    新的登录密码
     * @return      void
     */
    function update_password($user_id, $new_password)
    {
        if (empty($user_id) || empty($new_password))
        {
            return false;
        }

        /* 更新密码 */
        $sql = "UPDATE " .$this->table('users')
            . " SET " .$this->field_pass. " = '" .$this->compile_password($new_password)
            . "' WHERE " .$this->field_id. " = '$user_id'";
        $result = $this->db->query($sql);
        if ($result)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    /**
     * 检验在找回密码输入的用户名和email是否匹配
     *
     * @access      public
     * @param       int         user_name        用户名
     * @param       string      email            email
     * @return      array
     */
    function check_pwd_info($user_name, $email)
    {
        $username = $this->convert_to($user_name);

        $sql = "SELECT ".$this->field_id.", ".$this->field_pass.
                ' FROM ' . $this->table('users') .
                " WHERE ".$this->field_name." = '$username' AND ".$this->field_email." = '$email'";
        $res = $this->db->getRow($sql);
        if ($res)
        {
            $res[0] = $res[$this->field_id];
            $res[1] = $res[$this->field_pass];

            return $res;
        }
        else
        {
            return false;
        }
    }

    /**
     * 检验从邮件地址链接过来的code是否合法
     *
     * @access      public
     * @param       int         user_id        用户ID
     * @param       string      code           code值
     * @return      array
     */
    function check_param($user_id, $code)
    {
        /* 比较code是否合法 */
        $sql = "SELECT " .$this->field_pass. " FROM " .$this->table('users').
               " WHERE " .$this->field_id. " = '$user_id'";
        $password = $this->db->getOne($sql);

        $md5_password = md5($user_id . $password);
        if ($md5_password <> $code)
        {
            return false;
        }
        else
        {
            return true;
        }
    }

    /**
     * 获得用户总数
     *
     * @access      public
     * @param       string      查询的关键字
     * @param       string      查询条件
     * @return      int
     */
    function get_user_count($keyword = '', $ex_where = '')
    {
        $sql = "SELECT COUNT(*) FROM " . $this->table('users') . " WHERE 1";
        if (!empty($keyword))
        {
            $keyword = $this->convert_to($keyword);
            $sql .= " AND " .$this->field_name. " LIKE '%$keyword%'";
        }
        if (!empty($ex_where))
        {
            $sql .= " AND " .$this->field_id . $ex_where;
        }

        $num = $this->db->getOne($sql);

        return $num;
    }

    /**
     * 获得指定用户的帐号信息
     *
     * @access: public
     * @param:  int     $id     会员编号
     *
     * @return  array
     */
    function get_user_info($user_id)
    {
        $sql = "SELECT " .$this->field_name. ", " .$this->field_email. " FROM ".$this->table('users')." ".
               "WHERE " .$this->field_id. " = '$user_id'";
        $row = $this->db->getRow($sql);

        $row[$this->field_name] = $this->convert($row[$this->field_name]);

        $arr = array();
        $arr['user_id']     = $user_id;
        $arr['username']    = $row ? htmlspecialchars($row[$this->field_name]) : '';
        $arr['email']       = $row ? htmlspecialchars($row[$this->field_email]) : '';

        return $arr;
    }

    /**
     * 返回会员列表
     *
     * @access      public
     * @param       int     page      当前的页数
     * @param       int     limit     记录总数
     * @return      array
     */
    function &get_users($page=1, $limit=15, $sort='uid', $order='DESC', $keywords='', $ex_where='')
    {
        $start  = ($page - 1) * $limit;

        $where = ' WHERE 1 ';
        if (!empty($keywords))
        {
            $keywords = $this->convert_to($keywords);
            $where .= " AND ". $this->field_name . " LIKE '%$keywords%' ";
        }
        if (!empty($ex_where))
        {
            $where .= " AND ". $this->field_id . $ex_where;
        }

        $sql    = "SELECT * FROM ". $this->table('users') ."  $where ".
                    "ORDER BY $sort $order, " .$this->field_reg_date. " DESC";
        $res  = $this->db->SelectLimit($sql, $limit, $start);
        $list = array();

        /* 转换用户名 */
        while ($row=$this->db->fetchRow($res))
        {
            $list[] = array($this->field_id =>$row[$this->field_id],
                           $this->field_name =>$this->convert($row[$this->field_name]),
                           $this->field_email =>$row[$this->field_email],
                           $this->field_reg_date =>$row[$this->field_reg_date]
                );

        }

        return $list;
    }

    /*------------------------------------------------------ */
    //-- PRIVATE METHODs
    /*------------------------------------------------------ */

    /**
     * 将整合论字符集转换成UTF-8编码
     * 可以转换数组
     *
     * @access  private
     * @param   mix     $arr
     * @return  mix
     */
    function convert($data)
    {
        if (!empty($this->iconv) && $this->db_charset != 'UTF8')
        {
            if (is_array($data))
            {
                foreach ($data as $key => $value)
                {
                    if (is_array($value))
                    {
                        $data[$key] = $this->convert($value);
                    }
                    else
                    {
                        $data[$key] = $this->iconv->convert($this->db_charset,'UTF8',$value);
                    }
                }
            }
            else
            {
                $data = $this->iconv->convert($this->db_charset,'UTF8',$data);
            }
        }

        return $data;
    }

    /**
     * 将UTF-8字符转换整合对象字符集
     * 可以转换数组
     *
     * @access  private
     * @param   mix     $arr
     * @return  mix
     */
    function convert_to($data)
    {
        if (!empty($this->iconv) && $this->db_charset != 'UTF8')
        {
            if (is_array($data))
            {
                foreach ($data as $key => $value)
                {
                    if (is_array($value))
                    {
                        $data[$key] = $this->convert_to($value);
                    }
                    else
                    {
                        $data[$key] = $this->iconv->convert('UTF8',$this->db_charset,$value);
                    }
                }
            }
            else
            {
                $data = $this->iconv->convert('UTF8',$this->db_charset,$data);
            }
        }

        return $data;
    }

    /**
     * 在给定的表名前加上数据库名以及前缀
     *
     * @access  private
     * @param   string      $str    表名
     *
     * @return void
     */
    function table($str)
    {
        return '`' .$this->db_name. '`.`'.$this->prefix.$str.'`';
    }

    /**
     * 实现单个用户同步，如果商城不存在某用户id就在商城插入一条记录，记录用户id保持一致。
     * 否则什么也不做。
     *
     * @access  private
     * @param
     *
     * @return void
     */
    function user_sync($user_id, $user_name)
    {
        $sql = "SELECT COUNT(*)+1 FROM " .$GLOBALS['ecs']->table('users').
               " WHERE user_id = '$user_id' AND user_name='$user_name'";
        if ($GLOBALS['db']->getOne($sql) == 1)
        {
            $sql = "SELECT username, user_password, user_lastvisit, user_regdate, user_email".
                   " FROM " . $this->table('users') . " WHERE user_id = '$user_id'";
            $result = $this->db->getRow($sql);
            if ($result)
            {
                $result['username'] = $this->convert($result['username']);
                $user_lastvisit = date($GLOBALS['_CFG']['time_format'], $result['user_lastvisit']);

                $sql = "INSERT INTO " .$GLOBALS['ecs']->table('users'). " (user_id, user_name, email, password, reg_time, last_time) VALUES ('$user_id', '$result[username]', '$result[user_email]', '$result[user_password]', '$result[user_regdate]', '$user_lastvisit')";

                return $GLOBALS['db']->query($sql);
            }
        }
    }

    /**
     * 设置整合对象的cookie
     *
     * @access      private
     * @param       int         user_id     用户编号
     * @param       string      password    登录密码
     * @return      void
     */
    function set_cookie($user_id, $auto_login_key)
    {
        $sessiondata    = array();

        $sessiondata['autologinid'] = $auto_login_key;
        $sessiondata['userid']      = $user_id;

        setcookie($this->cookie_name() . '_data', serialize($sessiondata), time() + 31536000, $this->cookie_path, $this->cookie_domain);
        setcookie($this->cookie_name() . '_sid', $auto_login_key, time() + 31536000, $this->cookie_path, $this->cookie_domain);
    }

    /**
     * 设置用户在商城的SESSION。
     *
     * @access  private
     * @param   $user_id    用户编号
     * @param   $user_name  用户名
     * @param   $email      用户EMAIL
     *
     * @return void
     */
    function set_user_session($user_id, $user_name, $email, $user_lastvisit)
    {
        $_SESSION['user_id']        = $user_id;
        $_SESSION['user_name']      = $user_name;
        $_SESSION['email']          = $email;
    }

    /**
     * 获得默认的用户组ID
     *
     * @access  private
     * @return  int         默认的用户组
     */
    function get_member_group()
    {
        $sql = "SELECT group_id FROM " .$this->table('groups').
               " WHERE group_single_user = 1 AND group_name = 'Anonymous'";

        return $this->db->getOne($sql);
    }

    /**
     * 登录密码的加密算法
     *
     * @access      private
     * @param       string      password        用户的原始密码
     * @return      string
     */
    function compile_password($password)
    {
        $compiled = md5($password);
        return $compiled;
    }

    /* 取得论坛的cookie名称 */
    function cookie_name()
    {
        $cookie_name = $this->db->getOne("SELECT config_value FROM " .$this->table('config'). " WHERE config_name='cookie_name'");

        return $cookie_name;
    }

    /**
     * Our own generator of random values
     * This uses a constantly changing value as the base for generating the values
     * The board wide setting is updated once per page if this code is called
     * With thanks to Anthrax101 for the inspiration on this one
     * Added in phpBB 2.0.20
     */
    function dss_rand()
    {
        $dss_seeded = false;
        $rand_seed = $this->db->getOne("SELECT config_value FROM " .$this->table('config'). " WHERE config_name='rand_seed'");

        $val = $rand_seed . microtime();
        $val = md5($val);
        $rand_seed = md5($rand_seed . $val . 'a');

        if ($dss_seeded !== true)
        {
            $sql = "UPDATE ".$this->table('config')." SET config_value = '".$rand_seed."' WHERE config_name = 'rand_seed'";
            if (!$this->db->query($sql))
            {
                die('error');
            }

            $dss_seeded = true;
        }

        return substr($val, 16);
    }

    function encode_ip($dotquad_ip)
    {
        $ip_sep = explode('.', $dotquad_ip);

        return sprintf('%02x%02x%02x%02x', $ip_sep[0], $ip_sep[1], $ip_sep[2], $ip_sep[3]);
    }
}

?>