<?php
/**
* @author moufer<moufer@163.com>
* @copyright www.modoer.com
* @version $Id 2 2008-08-29 21:06 $
*/
!defined('IN_MUDDER') && exit('Access Denied');
function getMember($uid) {
    global $db,$dbpre;
    $query = $db->query("SELECT * FROM {$dbpre}members WHERE uid='$uid'");
    if($result = $db->fetch_array($query)) {
        return memberToArray($result);
    }
    return array();
}

function updateCredit($uid, $op, $sqlac='sqlexec') {
    global $db, $dbpre, $_expleve;

    $oplist_add = array(
        'reg', 'addshop', 'updateshop', 'addreview', 'addrespond', 'addpicture', 
    );
    $oplist_dec = array(
        'deleteshop', 'deletereview', 'deleterespond', 'deletepicture',
    );
    if(!in_array($op, array_merge($oplist_add, $oplist_dec))) return false;

    $uppoint = intval($_expleve[$op]['point']);
    $upcoin = intval($_expleve[$op]['coin']);
    if(in_array($op, $oplist_dec)) {
        $uppoint = -$uppoint;
        $upcoin = -$upcoin;
        //pr(array($uppoint, $upcoin));
    }
    $set = "`point`=`point`+$uppoint, `coin`=`coin`+$upcoin";
    $sql = "UPDATE {$dbpre}members SET $set WHERE uid='$uid'";
    if($sqlac == 'sqlstr') {
        return $sql;
    } elseif($sqlac == 'sqlset') {
        return $set;
    } elseif($sqlac == 'sqlexec' && !empty($uppoint) && !empty($upcoin)) {
        $db->update($sql);
    }
}

function updateActivity($uid, $username, $op) {
    global $db, $dbpre, $timestamp;

    $year = date('Y', $timestamp);
    $month = date('n', $timestamp);
    $aid = $db->get_value("SELECT aid FROM {$dbpre}activity WHERE uid='$uid' AND `year`='$year' AND `month`='$month'") >= 1;
    $set = array();
    if(is_string($op)) {
        $set[] = $aid ? "`$op`=`$op`+1" : "`$op`=1";
    } elseif(is_array($op)) {
        foreach($op as $val) {
            $set[] = $aid ? "`$val`=`$val`+1" : "`$val`=1";
        }
    }
    if(!$aid) {
        $set[] = "`uid`='$uid'";
        $set[] = "`username`='$username'";
        $set[] = "`year`='$year'";
        $set[] = "`month`='$month'";
    }
    $set = implode(',', $set);
    if($aid) {
        $db->update("UPDATE {$dbpre}activity SET $set WHERE aid='$aid'");
    } else {
        $db->query("INSERT INTO {$dbpre}activity SET $set");
    }
}

// 加好友
function joinFriend($user, $friendid, $content) {
    global $db, $dbpre,$ucenter_enable, $timestamp;

    $content = isset($content) ? unite_textarea($content) : '';
    if(!is_numeric($friendid) || friendid < 0) {
        $error = '未知的会员ID。';
    } elseif(strlen($content) > 300) {
        $error = '您的留言内容不能超过300个字。';
    } elseif($user->uid == $friendid) {
        $error = '您不能添加自己为好友。';
    } elseif($db->get_value("SELECT * FROM {$dbpre}friends WHERE uid='{$user->uid}' AND fuid='$friendid'")) {
        $error = '您已经将该会员加为好友了，无需再次添加。';
    } elseif(!$friend = $db->get_one("SELECT username,groupid FROM {$dbpre}members WHERE uid='$friendid'")) {
        $error = '您添加的好友不存在或已删除，请返回刷新。';
    } elseif(in_array($friend['groupid'], array('1','2','3'))) {
        $error = '无法添加被系统限制会员为好友。';
    } else {
        $subject = $user->username.'将您加为好友';
        $content = $content ? $content : '我已经将您加为了我的好友！';
        $post = array(
            'uid' => $user->uid,
            'fuid' => $friendid,
            'fusername' => $friend['username'],
            'addtime' => $timestamp,
        );
        $db->insert_new($dbpre.'friends', $post);
        if(!class_exists('MessageModel')) {
            if($ucenter_enable) {
                require_once(MUDDER_ROOT."./member/model/message_uc.php");
            } else {
                require_once(MUDDER_ROOT."./member/model/message.php");
            }
        }
        MessageModel::send(0, $friendid, $subject, $content);
    }
    if($error) {
        return '<script type="text/javascript">alert("'.$error.'");</script>';
    } else {
        return '<script type="text/javascript">alert("添加完毕！");window.parent.document.forms["addfriendfrm"].hide.click();</script>';
    }
}

// 修改密码
function changePassword($user, $old_pw, $new_pw, $new_pw2) {
    global $db,$dbpre,$ucenter_enable;

    if(!$old_pw) {
        $error = '未填写原密码，请重新填写。';
    } elseif(!$new_pw || $new_pw != addslashes($new_pw)) {
        $error = '密码空或包含非法字符，请重新填写。';
    } elseif($new_pw != $new_pw2) {
        $error = '2次输入的密码不一致，请重新填写。';
    } else {
        if($ucenter_enable) {
            $result = $user->Modfily_Password($new_pw, $old_pw);
            !$result && $error = $user->errmsg;
        } else {
            $old_md5pw = md5($old_pw);
            $member = $db->get_one("SELECT uid,username FROM {$dbpre}members WHERE uid='{$user->uid}' AND password='$old_md5pw'");
            if(!$member) {
                $error = '原密码不符，请重新确认。';
            } else {
                $new_md5pw = md5($new_pw);
                $s = $db->query("UPDATE {$dbpre}members SET password='$new_md5pw' WHERE uid='{$user->uid}'");
                if(!$s) {
                    $error = '密码修改失败！';
                } else {
                    $life = 3600*24*30; // Cookie保存1个月
                    $hash = formhash($member['uid'], $member['username'], $new_md5pw);
                    set_Cookie("uid", $member['uid'], $life);
                    set_Cookie("hash", $hash, $life);
                    set_Cookie("logintime", $timestamp, $life);
                    set_Cookie("remembertime", $timestamp + $life, $life);
                }
            }
            unset($old_md5pw,$new_md5pw,$old_pw,$new_pw,$new_pw2);
        }
    }
    if($error) {
        return '<script type="text/javascript">alert("'.$error.'");</script>';
    } else {
        return '<script type="text/javascript">alert("修改完毕！");window.parent.document.forms["changepasswordfrm"].hide.click();</script>';
    }
}

// 转换会员信息为显示数组
function memberToArray(& $row) {
    global $_config, $ucenter_enable;
    $row['uid'] = $row['uid'] ? $row['uid'] : 0;
    $row['username'] = $row['username'] ? $row['username'] : '会员已删除';
    if($ucenter_enable) {
        $row['face'] = UC_API.'/avatar.php?uid='.$row['uid'].'&size=small';
    } else {
        $row['face'] = get_face($row['face'] ? $row['face'] : 'noface.gif');
    }
    $row['point'] = intval($row['point']);
    $row['coin'] = intval($row['coin']);
    $row['reviews'] = intval($row['reviews']);
    $row['flowers'] = intval($row['flowers']);
    $row['rank'] = get_userrank(intval($row['point']));
    return $row;
}
?>