<?php
if ( !defined( "SAXUE_CORE_INCLUDE" ) ) {
		include "../../core.php";
} 
saxue_checkpower( 'newmessage' );
saxue_loadlang( "message" );
if ( !isset( $_REQUEST['action'] ) ) {
		$GLOBALS['_REQUEST']['action'] = "message";
} 
switch ( $_REQUEST['action'] ) {
		case "newmessage" :
				$_POST['content'] = trim( $_POST['content'] );
				if ( strlen( $_POST['content'] ) == 0 ) {
						saxue_printfail( $saxueLang['message']['message_need_content'] );
				} 
				$_POST['title'] = trim( $_POST['title'] );
				if ( strlen( $_POST['title'] ) == 0 ) {
						$_POST['title'] = saxue_substr( strip_tags( $_POST['content'] ), 0, 50 );
				} elseif ( strlen( $_POST['title'] ) > 50 ) {
						$_POST['title'] = saxue_substr( strip_tags( $_POST['title'] ), 0, 50 );
				}
				$_POST['receiver'] = trim( $_POST['receiver'] );
				include_once( SAXUE_ROOT_PATH . "/model/member_users.php" );
				$users_handler = saxuememberusershandler :: getinstance( "saxuememberusershandler" );
				if ( strlen( $_POST['receiver'] ) > 0 ) {
						$touser = $users_handler -> getbyname( $_POST['receiver'] );
						if ( !$touser ) {
								saxue_printfail( $saxueLang['message']['message_no_receiver'], 0 );
						} 
						$toid = $touser -> getvar( "uid", "n" );
						$toname = $touser -> getvar( "uname", "n" );
				} else {
						$toid = 0;
						$toname = '';
				}
				include SAXUE_ROOT_PATH . "/model/member_message.php";
				$message_handler = &saxuemembermessagehandler :: getinstance( "saxuemembermessagehandler" );
				$newMessage = $message_handler -> create();
				$newMessage -> setvar( "postdate", SAXUE_NOW_TIME );
				$newMessage -> setvar( "toid", $toid );
				$newMessage -> setvar( "toname", $toname );
				if ( !$message_handler -> insert( $newMessage ) ) {
						saxue_printfail( $saxueLang['message']['message_send_failure'] );
				} 
				saxue_jumppage( "message.php", LANG_DO_SUCCESS );
				break;
		case "message" :
				include_once( SAXUE_ADMIN_PATH . "/header.php" );
				$message = false;
				if ( !empty( $_REQUEST['reid'] ) || !empty( $_REQUEST['fwid'] ) ) {
						include SAXUE_ROOT_PATH . "/model/member_message.php";
						$message_handler = &saxuemembermessagehandler :: getinstance( "saxuemembermessagehandler" );
						if ( !empty( $_REQUEST['reid'] ) ) {
								$message = $message_handler -> get( $_REQUEST['reid'] );
						} else if ( !empty( $_REQUEST['fwid'] ) ) {
								$message = $message_handler -> get( $_REQUEST['fwid'] );
						} 
				} 
				if ( is_object( $message ) ) {
						$GLOBALS['_REQUEST']['receiver'] = $message -> getvar( "fromname", "e" );
						$GLOBALS['_REQUEST']['title'] = $message -> getvar( "title", "e" );
						if ( !empty( $_REQUEST['reid'] ) ) {
								$GLOBALS['_REQUEST']['title'] = "Re:" . $_REQUEST['title'];
								$GLOBALS['_REQUEST']['content'] = "";
						} else if ( !empty( $_REQUEST['fwid'] ) ) {
								$GLOBALS['_REQUEST']['title'] = "Fw:" . $_REQUEST['title'];
								$GLOBALS['_REQUEST']['content'] = $message -> getvar( "content", "e" );
						} 
				} 
				if ( !isset( $_REQUEST['receiver'] ) ) {
						$GLOBALS['_REQUEST']['receiver'] = "";
				} 
				if ( !isset( $_REQUEST['title'] ) ) {
						$GLOBALS['_REQUEST']['title'] = "";
				} 
				if ( !isset( $_REQUEST['content'] ) ) {
						$GLOBALS['_REQUEST']['content'] = "";
				} 
				$saxueTpl -> assign( "receiver", $_REQUEST['receiver'] );
				$saxueTpl -> assign( "title", $_REQUEST['title'] );
				$saxueTpl -> assign( "content", $_REQUEST['content'] );
				$saxueTpl -> setcaching( 0 );
				$saxueTset['saxue_contents_template'] = SAXUE_WEB_PATH . "/member/templates/newmessage.html";
				include_once( SAXUE_ADMIN_PATH . "/footer.php" );
} 
