<?php

/**
 * ECSHOP 会员数据处理类(IPB)
 * ============================================================================
 * 版权所有 (C) 2005-2007 北京亿商互动科技发展有限公司，并保留所有权利。
 * 网站地址: http://www.ecshop.com
 * ----------------------------------------------------------------------------
 * 这是一个免费开源的软件；这意味着您可以在不用于商业目的的前提下对程序代码
 * 进行修改、使用和再发布。
 * ============================================================================
 * @author:     Xu Chaoqian <xuchaoqian@163.com>
 * @version:    v2.1
 * ---------------------------------------------
 * $Author: xcq $
 * $Date: 2006-11-03 11:31:18 +0800 (星期五, 03 十一月 2006) $
 * $Id: ipb.php 2219 2006-11-03 03:31:18Z xcq $
 */

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

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

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

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

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

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

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

    return;
}

class ipb
{
    /*------------------------------------------------------ */
    //-- PUBLIC ATTRIBUTEs
    /*------------------------------------------------------ */

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

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

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

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

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

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

    /*------------------------------------------------------ */
    //-- PRIVATE ATTRIBUTEs
    /*------------------------------------------------------ */

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

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

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

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

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

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

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

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

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

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

    /*------------------------------------------------------ */
    //-- 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 ipb($db_host='localhost', $db_name='', $db_user='root', $db_pass='', $prefix='ibf_', $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();
    }

    /**
     * 用户登录商城时，程序会根据会员的用户名和密码两个参数，到第三方系统所在的数据库验证密码是否合法。
     * 如果验证通过，需要设置第三方系统所使用的COOKIE以及商城的SESSION，并返回true；否则返回false。
     * 商城使用的SESSION有$_SESSION[‘user_id’], $_SESSION[‘user_name’],$_SESSION[‘email’]。
     * 它们的值分别为会员ID，会员名，会员email。
     *
     * @access      public
     * @param       string      username    用户名
     * @param       string      password    登录密码
     *
     * @return bool
     */
    function login($username, $password)
    {
        /* 字符集转换 */
        $uname = $this->convert_to($username);

        /* 获得第三方系统中指定用户的信息 */
        $sql = "SELECT m.id, m.name, m.email, m.member_login_key, mc.converge_pass_hash, mc.converge_pass_salt".
               " FROM ".$this->table('members')." AS m, ".$this->table('members_converge')." AS mc".
               " WHERE m.name = '$uname' AND m.email = mc.converge_email";
        $row = $this->db->getRow($sql);

        /* 如果该用户名存在 */
        if ($row)
        {
            /* 规格化用户输入的密码 */
            $converge_pass_hash_probe = $this->comiple_password($row['converge_pass_salt'], $password);

            /* 检查密码是否匹配 */
            if ($converge_pass_hash_probe != $row['converge_pass_hash'])
            {
                return false;
            }
        }
        else
        {
            return false;
        }

        /* 设置第三方系统所使用的SESSION和COOKIE */
        $this->set_cookie($row['id'], $row['member_login_key']);
        /* 设置商城的SESSION */
        $this->set_user_session($row['id'], $username, $row['email']);
        /* 如果该用户不在商城数据表中，则新加一条记录 */
        $this->user_sync($row['id'], $username);

        return true;
    }

    /**
     * 销毁第三方系统所使用的SESSION和COOKIE，同时销毁商城使用的SESSION。
     *
     * @access      public
     * @return      void
     */
    function logout()
    {
        setcookie('session_id', '', time()-3600, $this->cookie_path, $this->cookie_domain);
        setcookie('member_id', '', time()-3600*24*30, $this->cookie_path, $this->cookie_domain);
        setcookie('pass_hash', '', time()-3600*24*30, $this->cookie_path, $this->cookie_domain);

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

    /**
     * 访问商城时，程序将解析第三方系统所使用的COOKIE，并到第三方系统数据库验证COOKIE信息是否合法。
     * 如果验证通过，需要设置第三方系统所使用的COOKIE以及商城的SESSION。
     *
     * @access      public
     * @return      void
     */
    function get_cookie()
    {
        if (isset($_COOKIE['member_id']) && isset($_COOKIE['pass_hash']))
        {
            $user_id = $_COOKIE['member_id'];
            $auto_login_key = $_COOKIE['pass_hash'];

            /* 查询第三方系统的数据库 */
            $sql = "SELECT `id`, `name`, `email`, `member_login_key` FROM ".$this->table('members').
                   " WHERE `id` = '$user_id' AND `member_login_key` = '$auto_login_key'";
            $row = $this->db->getRow($sql);

            /* 如果有记录，就把用户在商城的状态设置为登录 */
            if ($row)
            {
                $this->set_cookie($row['id'], $row['member_login_key']);
                $row['name'] = $this->convert($row['name']);
                $this->set_user_session($row['id'], $row['name'], $row['email']);
                $this->user_sync($row['id'], $row['name']);

                return true;
            }
            else
            {
                return false;
            }

        }
        else
        {
            return false;
        }
    }

    /**
     * 检查会员用户名和会员email是否已经存在。如果用户名有重复，
     * 那么$error =ERR_USERNAME_EXISTS，函数返回0，如果email重复，
     * 那么$error =ERR_EMAIL_EXISTS，函数返回0。否则将用户名、
     * 加密后的密码、会员的email、会员的注册时间添加到第三方系统用户数据表中。
     *
     * @access      public
     * @param       string      username    用户名
     * @param       string      password    登录密码
     * @param       string      email       邮件地址
     * @param       string      question    密码提示问题
     * @param       string      answer      密码提示问题答案
     * @return      int         返回最新的ID
     */
    function add_user($username, $password, $email, $question='', $answer='')
    {
        $username = $this->convert_to($username);

        /* 检查用户名是否已经存在 */
        $sql = "SELECT COUNT(*)+1 FROM ".$this->table('members').
               " WHERE `name` = '$username'";
        $num = $this->db->GetOne($sql);

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

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

        /* 生成随机串 */
        $salt = $this->generate_password_salt(5);

        /* 生成加密密码 */
        $converge_pass_hash = $this->generate_compiled_passhash($salt, md5($password));

        /* 规格化随机串 */
        $converge_pass_salt = str_replace( '\\', "\\\\", $salt);

        /* 插入数据到members_converge表 */
        $sql = "INSERT INTO ".$this->table('members_converge')." (`converge_id`, `converge_email`,`converge_joined`, `converge_pass_hash`, `converge_pass_salt`) VALUES (null, '$email', " . time() . ", '$converge_pass_hash', '$converge_pass_salt')";
        $this->db->query($sql);

        /* 得到新加用户的UID */
        $uid = $this->db->Insert_ID();

        /* 获得默认的用户组 */
        $grp = 3;

        /* 生成自动登录密钥，存于COOKIE中 */
        $auto_login_key = $this->generate_auto_log_in_key();

        /* 插入数据到members表 */
        $sql = "INSERT INTO ".$this->table('members')." (`id`, `name`, `mgroup`, `email`, `joined`, `ip_address`, `member_login_key`, `members_display_name`)
                VALUES ($uid, '$username', $grp, '$email', " . time() . ", '" . real_ip() . "', '$auto_login_key', '$username')";
        $result = $this->db->query($sql);

        return $uid;
    }

    /**
     * 编辑第三方系统用户帐号信息的函数
     *
     * @access      public
     * @param       int         $user_id        用户编号
     * @param       string      $username       用户名
     * @param       string      $email          邮件地址
     * @return      bool
     */
    function edit_user($user_id, $username = '', $email = '', $bday = 0, $gender = 0)
    {
        $fields = '';
        if (empty($user_id) || empty($email))
        {
            return false;
        }

        $username = $this->convert_to($username);

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

        if ($num > 1)
        {
            $this->error = ERR_USERNAME_EXISTS;
            return false;
        }
        else
        {
            $fields .= "`$this->field_name` = '$username'";
        }

        /* 检查邮件地址是否重复 */
        $sql = "SELECT COUNT(*)+1 FROM " .$this->table('members').
               " WHERE `email` = '$email' AND `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'";
        }

        if (!empty($bday))
        {
            $fields .= (empty($fields)) ? '' : ',';
            $bday_day   = substr($bday, 8, 2);
            $bday_month = substr($bday, 5, 2);
            $bday_year  = substr($bday, 0, 4);
            $fields .= "`bday_day`   = '$bday_day', ";
            $fields .= "`bday_month` = '$bday_month', ";
            $fields .= "`bday_year`  = '$bday_year' ";
        }
        $sql = "UPDATE ".$this->table('members')." SET $fields WHERE `$this->field_id` = '$user_id'";
        $this->db->query($sql);

        $sql = "UPDATE ".$this->table('members_converge')." SET `converge_email` = '$email' ".
               "WHERE `converge_id` = '$user_id'";
        $this->db->query($sql);

        return true;
    }

    /**
     * 删除第三方系统用户帐号的函数
     *
     * @access      public
     * @param       mix     users   用户编号
     * @return      bool
     */
    function remove_user($users)
    {
        if (is_array($users))
        {
            $where1 = '`id`' . db_create_in($users);
            $where2 = '`converge_id`' . db_create_in($users);
        }
        else
        {
            $where1 = "`id` = '$users'";
            $where2 = "`converge_id` = '$users'";
        }

        $sql = "DELETE FROM ".$this->table('members')." WHERE $where1";
        $this->db->query($sql);

        $sql = "DELETE FROM ".$this->table('members_converge')." WHERE $where2";
        $this->db->query($sql);

        return true;
    }

    /**
     * 编辑第三方系统用户密码的函数
     * 在这个函数里判断用户输入的原来的密码是否正确，如果不正确则返回FALSE
     *
     * @access      public
     * @param       int         user_id         用户编号
     * @param       string      password        原来的登录密码
     * @param       string      new_password    新的登录密码
     * @return      bool
     */
    function edit_password($user_id, $password, $new_password)
    {
       $sql = "SELECT `converge_pass_hash`, `converge_pass_salt` FROM ".$this->table('members_converge').
              " WHERE `converge_id` = '$user_id'";
       $row = $this->db->getRow($sql);

       if ($row)
       {
           $converge_pass_hash = $row['converge_pass_hash'];
           $converge_pass_salt = $row['converge_pass_salt'];
           $converge_pass_hash_probe = $this->comiple_password($converge_pass_salt, $password);

           if ($converge_pass_hash_probe != $converge_pass_hash)
           {
               return false;
           }

           if (!$this->update_password($user_id, $new_password, $converge_pass_salt))

           {
               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 converge_pass_hash, converge_pass_salt".
               " FROM " .$this->table('members_converge')
.
               " WHERE converge_id = '$user_id'";
        $row = $this->db->getRow($sql);
        if (!$row)

        {
            return false;

        }

        $password = $row['converge_pass_hash'];
        $salt     = $row['converge_pass_salt'];

        $md5_password = md5($user_id . $password);
        if ($md5_password <> $code)
        {
            return false;
        }
        if (!$this->update_password($user_id, $new_password, $salt))

        {
            return false;

        }
        else
        {
            return true;
        }
    }
    /**
 更新用户密码的函数

     *
 @access      private

     * @param       int         user_id         用户编号

     * @param       string      new_password    新的登录密码
     * @return      void

     */
    function update_password($user_id, $new_password, $salt)
    {

        if (empty($user_id) || empty($new_password) || empty($salt))
        {
           return false;
        }

        /* 更新密码 */
        $sql = "UPDATE " .$this->table('members_converge') .
              " SET converge_pass_hash = '".$this->generate_compiled_passhash($salt, md5($new_password))."'".
              " WHERE converge_id = '$user_id'";
        $result = $this->db->query($sql);
        if ($result)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    /**
     * 获得第三方系统的用户总数
     *
     * @access      public
     * @param       string      查询的关键字     $keyword
     * @param       string      查询条件         $ex_where
     * @return      int         用户总数
     */
    function get_user_count($keyword='', $ex_where='')
    {
        $sql = "SELECT COUNT(*) FROM " .$this->table('members'). " WHERE 1";

        if (!empty($keyword))
        {
            $keyword = $this->convert_to($keyword);
            $sql .= " AND `name` LIKE '%$keyword%'";
        }
        if (!empty($ex_where))
        {
            $sql .= " AND `id` $ex_where";
        }

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

        return $num;
    }

    /**
     * 获得指定用户在整个系统（即商城和第三方系统）中的帐号信息
     *
     * @access: public
     * @param:  int     $user_id     会员编号
     *
     * @return  array
     */
    function get_user_info($user_id)
    {

        $sql = "SELECT name, email, ".$this->field_id." FROM " .$this->table('members').
               " WHERE `id` = '$user_id'";
        $row = $this->db->GetRow($sql);
        if (empty($row))
        {
            return false;
        }

        $row['name'] = $this->convert($row['name']);
        $arr['user_id']     = $user_id;
        $arr['username']    = $row ? htmlspecialchars($row['name'])  : '';
        $arr['email']       = $row ? htmlspecialchars($row['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 `name` LIKE '%$keywords%'";
        }
        if (!empty($ex_where))
        {
            $where .= " AND `id` $ex_where";
        }

        $sql = "SELECT * FROM " .$this->table('members').
                $where.
               " ORDER BY `$sort` $order, `joined` DESC";
        $result = $this->db->SelectLimit($sql, $limit, $start);

        $list = array();

        /* 转换用户名 */
        while ($row = $this->db->fetchRow($result))
        {
            $list[] = array('id'        =>  $row['id'],
                            'name'      =>  $this->convert($row['name']),
                            'email'     =>  $row['email'],
                            'joined'    =>  $row['joined']
                      );

        }

        return $list;
    }

    /*------------------------------------------------------ */
    //-- PRIVATE METHODs
    /*------------------------------------------------------ */
    /**
     * 把数据从第三方数据库所使用的编码转成UTF8编码。可以转换数组。
     *
     * @access  private
     * @param   mix     $data
     * @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;
    }

    /**
     * 把数据从UTF8编码转成第三方数据库所使用的编码，可以转换数组。
     *
     * @access  private
     * @param   mix    $data
     * @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;
    }

    /**
     * 设置第三方系统所使用的SESSION和COOKIE。
     *
     * @access      private
     * @param       int         $user_id        用户编号
     * @param       string      $auto_login_key   登录密码
     * @return      void
     */
    function set_cookie($user_id, $auto_login_key)
    {
        /* 强制第三方系统开始一个新会话 */
        setcookie('session_id', '', time() - 3600 * 24 * 30, $this->cookie_path, $this->cookie_domain);

        /* 设置cookie */
        setcookie('member_id', $user_id, time() + 3600 * 24 * 30, $this->cookie_path, $this->cookie_domain);
        setcookie('pass_hash', $auto_login_key, time() + 3600 * 24 * 30, $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)
    {
        $_SESSION['user_id']        = $user_id;
        $_SESSION['user_name']      = $user_name;
        $_SESSION['email']          = $email;
    }

    /**
     * 实现单个用户同步，如果商城不存在第三方系统中某用户的id，就在商城插入一条记录。否则什么也不做。
     *
     * @access  private
     * @param   int     $user_id    用户编号
     *
     * @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'";
        $num = $this->db->GetOne($sql);

        if ($num == 1)
        {
            $sql = "SELECT m.id, m.name, m.email, m.joined, mc.converge_pass_hash, mc.converge_pass_salt".
                   " FROM ".$this->table('members')." AS m, ".$this->table('members_converge')." AS mc".
                   " WHERE m.id = '$user_id' AND m.email = mc.converge_email";
            $result = $this->db->GetRow($sql);

            if ($result)
            {
                $result['name'] = $this->convert($result['name']);
                $sql = "INSERT INTO ".$GLOBALS['ecs']->table('users')." (`user_id`, `user_name`, `email`, `password`, `reg_time`, `salt`) VALUES ('$user_id', '".$result['name']."', '".$result['email']."', '".$result['converge_pass_hash']."', '".$result['joined']."', '".$result['converge_pass_salt']."')";
                $GLOBALS['db']->query($sql);

                return true;
            }
        }
        return false;
    }

    /**
     * 登录密码的加密算法
     *
     * @access      private
     * @param       string      $salt           随机种子
     * @param       string      $password       原始密码
     * @return      string
     */
    function comiple_password($salt, $password)
    {
        $compiled = md5(md5($salt) . md5($password));
        return $compiled;
    }

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

   /**
    * Generates a compiled passhash
    *
    * Returns a new MD5 hash of the supplied salt and MD5 hash of the password
    *
    * @access   private
    * @param    string  User's salt (5 random chars)
    * @param    string  User's MD5 hash of their password
    * @return   string  MD5 hash of compiled salted password
    */
    function generate_compiled_passhash($salt, $md5_once_password)
    {
        return md5(md5($salt) . $md5_once_password);
    }

   /**
    * Generates a password salt
    *
    * Returns n length string of any char except backslash
    *
    * @access   private
    * @param    integer Length of desired salt, 5 by default
    * @return   string  n character random string
    */
    function generate_password_salt($len=5)
    {
        $salt = '';

        //srand( (double)microtime() * 1000000 );
        // PHP 4.3 is now required ^ not needed

        for ( $i = 0; $i < $len; $i++ )
        {
            $num   = rand(33, 126);

            if ( $num == '92' )
            {
                $num = 93;
            }

            $salt .= chr($num);
        }

        return $salt;
    }

   /**
    * Generates a log in key
    *
    * @access   private
    * @param    integer Length of desired random chars to MD5
    * @return   string  MD5 hash of random characters
    */
    function generate_auto_log_in_key($len=60)
    {
        $pass = $this->generate_password_salt(60);

        return md5($pass);
    }
    /**
     * 检验在找回密码输入的用户名和email是否匹配
     *
     * @access      public
     * @param       int         user_name        用户名
     * @param       string      email            email
     * @return      array
     */
    function check_pwd_info($user_name, $email)
    {
        $sql = "SELECT m.id, mc.converge_pass_hash, m.name, m.email".
               " FROM ".$this->table('members')." AS m, ".$this->table('members_converge')." AS mc".
               " WHERE m.name = '$user_name' AND m.email = '$email' AND m.email = mc.converge_email";
        $res = $this->db->getRow($sql);
        if ($res)
        {
            $res[0] = $res['id'];
            $res[1] = $res['converge_pass_hash'];

            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 converge_pass_hash FROM " .$this->table('members_converge').
               " WHERE converge_id = '$user_id'";
        $password = $this->db->getOne($sql);

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

?>