<?php
/**
* @author moufer<moufer@163.com>
* @copyright www.modoer.com
* @version $Id 3 2008-08-25 18:35 $
*/
!defined('IN_MUDDER') && exit('Access Denied');
if($ucenter_enable) {
    require_once(MUDDER_ROOT."./member/model/message_uc.php");
} else {
    require_once(MUDDER_ROOT."./member/model/message.php");
}

if($in_ajax) {

    if($op == 'read') {
        $caption = '阅读短信息';
        if(!$user->isLogin) dialog($caption, '', 'login');

        $pmid = trim($pmid);
        $pmid = isset($pmid) && is_numeric($pmid) && $pmid > 0 ? intval($pmid) : 0;
        if(!$pmid) {
            dialog($caption,'短信息不存在。');
        }

        $msg = MessageModel::read($user->uid,$pmid);
        if(!$msg) {
            dialog($caption,'短信息不存在或已删除。');
        }
        $folder = $msg['recvuid'] == $user->uid ? 'inbox' : 'outbox';
        require_once template('pm_readmessage_table','member','./templates/member');
        output();

    } elseif($op == 'write') {
        $caption == '写短信息';
        if(!$user->isLogin) dialog($caption, '', 'login');

        if($charset != 'utf-8') {
            if(!class_exists('Chinese')) {
                require_once(MUDDER_ROOT."./include/chinese.class.php");
            }
            $chs = new Chinese('utf-8', $charset);
            $subject = isset($subject) ? $chs->Convert($subject) : '';
            $subject = html_special_chars_ex($subject);
        }

        $recvuid = is_numeric($recvuid) && $recvuid>0 ? $recvuid : '';
        $recvuid && $recv_username = $db->get_value("SELECT username FROM {$dbpre}members WHERE uid='$recvuid'");

        require_once template('member_dlg_writemessage');
        output();
    } else {
        dialog('系统提示','未定义的操作。');
    }

} elseif(check_submit('sendmessagesubmit')) {
    $recv_users = isset($recv_users) ? html_special_chars_ex($recv_users) : '';
    $subject = isset($subject) ? html_special_chars_ex($subject) : '';
    $content = isset($content) ? unite_textarea($content) : '';

    if(!$recv_users) {
        $error = '未填写发送对象。';
    } elseif(!$subject) {
        $error = '未填写短信主题。';
    } elseif(!$content) {
        $error = '未填写短信内容。';
    } elseif(strlen($subject) > 60) {
        $error = '短信主题不能超多60个字符。';
    } elseif(strlen($content) > 500) {
        $error = '短信内容不能超多500个字符。';
    } else {
        $unames = array_unique(explode(",", $recv_users));
        if(count($unames)>5) {
            $error = '一次发送对象不能大于5个。';
        } else {
            $result = MessageModel::send($user->uid, implode(',',$unames), $subject, $content, true);
            if(!$result) $error = '发送失败！'.implode(',', $unames);
        }
    }
    if($error) {
        echo '<script type="text/javascript">alert("'.$error.'");</script>';
    } else {
        echo '<script type="text/javascript">alert("发送完毕！");window.parent.document.forms["pmsgfrm"].hide.click();</script>';
    }
    output();

} elseif(!check_submit('mysubmit')) {

    $folder = $folder == 'outbox' ? 'outbox' : 'inbox';
    $total = MessageModel::getMessagesTotal($user->uid,$folder);
    $msgs = array();
    if($total > 0) {
        $offset = 20;
        $limit = get_limit($page,$offset);
        $msgs = MessageModel::getMessages($user->uid,$folder,$limit);
    }
    $sub_param = array('inbox'=>'收件箱', 'outbox'=>'发件箱');
    $total > 0 && $multipage = multi($total, $offset, $page, "index.php?ac=$ac&folder=$folder");

} else {

    if(!is_array($pmids) || count($pmids) <= 0) {
        redirect('未选择，请返回修改，至少选择一项。');
    } else {
        MessageModel::delete($user->uid,$folder,$pmids);
        redirect('删除完毕！',"index.php?ac=$ac");
    }
}
?>