<?php
include("include/config.php");
include("include/protect.class.php");
include("include/jdf.php");
include('include/functions.php');

$prot=new protect();
if ($prot->showPage) {
$db=new DB($globalConfig['dbase'], $globalConfig['dbhost'],$globalConfig['dbuser'], $globalConfig['dbpass']);
mysql_query("SET NAMES utf8");
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title> ارسال پیام </title>
<?php include("maintemplate/mainheader.php");?>
<script type="text/javascript" src="ckeditor/ckeditor.js"></script>
<script type="text/javascript" src="js/js_body.js"></script>
<link href="css/style.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" type="text/css" href="fancybox/jquery.fancybox-1.3.1.css" media="screen" />
<script src="js/jquery-1.4.2.min.js" type="text/javascript"></script>
<script type="text/javascript" src="fancybox/jquery.fancybox-1.3.1.js"></script>
</head>
<body>
<div id="wpwrap">
<div id="wpcontent">
	<?php include("include/menutop.php");?> 
<div id="wpbody">
	<?php include("include/header.php");?>
    <div style="overflow:hidden;" id="wpbody-content">
        <div id="screen-meta">
            <div class="wrap">
                <div id="icon-index" class="icon32"><br></div>
                <h2> ارسال پیام </h2>
                <div id="dashboard-widgets-wrap">
                    <div id="dashboard-widgets" class="metabox-holder">
                        <div class="postbox-container" style="width:100%;">
                            <div id="normal-sortables" class="meta-box-sortables ui-sortable"><div id="dashboard_recent_comments" class="postbox ">
                    <div class="handlediv" title="برای جانشینی کلیک کنید"><br></div><h3 class="hndle"><span> ارسال پیام</span></h3>
<?php
	switch ($_GET['act']){
		case 'send_message':
			if(isset($_POST['submit'])){
				if(isset($_POST['allUser'])){
					$from = 'pishsakhteh-Group';
					$email = 'info@acixco.com';
					$header  = "MIME-Version: 1.0\r\n";
					$header .= "Content-type: text/html; charset=utf-8\r\n";
					$header .= "To: ".$to."\r\n";
					$header .= "From: ".$from." <".$email.">\r\n";
					$header .= "Cc: \r\n";
					$subject = $_POST['title'];
					$message = '<table dir="rtl">
									<tbody><tr style="background-color: rgb(21, 111, 0);">
											<td style="color: rgb(255, 255, 255); font-weight: 900; font-size: 16px;">'.returnConfigPortal('title').'</td>
										</tr>
										<tr>
											<td style="text-align: justify;">
												'.$_POST['description'] . '<br><span style="font-family:Tahoma, Arial; font-size:12px;">'.date("l d F Y").'</span>
											</td>
										</tr>
									</tbody>
								</table>';
					$sql = $db->query("SELECT email FROM u_users");
					while($res = $db->fetchNextArray($sql)){
						$to = $res['email'];
						mail($to, $subject, $message, $header);
					}
					echo 'پیام شما با موفقيت ارسال شد.';
					echo $message;
				}else{
					$db->execute("INSERT INTO messages_user SET s_m_user = '1',
																r_m_user = '".(int)$_POST['uid']."',
																title = '".$db->safe_string($_POST['title'])."',
																message = '".$_POST['description']."',
																date = now()");
					$from = 'pishsakhteh-Group';
					$email = 'info@acixco.com';
					$to = $_POST['email'];
					$header  = "MIME-Version: 1.0\r\n";
					$header .= "Content-type: text/html; charset=utf-8\r\n";
					$header .= "To: ".$to."\r\n";
					$header .= "From: ".$from." <".$email.">\r\n";
					$header .= "Cc: \r\n";
					$subject = $_POST['title'];
					$message = '<table dir="rtl">
									<tbody><tr style="background-color: rgb(21, 111, 0);">
											<td style="color: rgb(255, 255, 255); font-weight: 900; font-size: 16px;">'.returnConfigPortal('title').' </td>
										</tr>
										<tr>
											<td style="text-align: justify;">
												'.$_POST['description'] . '<br><span style="font-family:Tahoma, Arial; font-size:12px;">'.date("l d F Y").'</span>
											</td>
										</tr>
									</tbody>
								</table>';
					mail($to, $subject, $message, $header);
					
					echo 'پیام شما با موفقيت ارسال شد.';
				
				}
			}
		
		break;
		
		default:
		
		if(isset($_GET['uid']) || isset($_GET['all'])){
			$sql = $db->query("SELECT * FROM u_users WHERE ID = '".(int)$_GET['uid']."'");
			$res = $db->fetchNextArray($sql);
			
			?>
			<form action="sendMessage.php?act=send_message" method="post">
			<table width="100%" border="0">
            <?php
				if(isset($_GET['all'])){
			?>
			  <tr>
				<td colspan="2" align="center"> <b> ارسال ایميل گروهی </b><input type="hidden" name="allUser" value="1" /></td>
			  </tr>
            <?php
				}else{
			?>
			  <tr>
				<td> نام كاربری : </td>
				<td><input type="text" name="uid" value="<?=$res['userName']?>" disabled="disabled" /><input type="hidden" name="uid" value="<?=(int)$_GET['uid']?>" /></td>
			  </tr>
			  <tr>
				<td> ايمیل : </td>
				<td><input type="text" name="email" value="<?=$res['email']?>" disabled="disabled" /></td>
			  </tr>
            <?php
				}
			?>
			  <tr>
				<td>عنوان :‌</td>
				<td><input type="text" name="title"  /></td>
			  </tr>
			  <tr>
				<td>متن :‌ </td>
				<td>
                <textarea cols="80" id="description" name="description" rows="10"></textarea>
                <script type="text/javascript">
                //<![CDATA[
    
                    CKEDITOR.replace( 'description',
                        {
                            fullPage : true,
                            extraPlugins : 'docprops'
                        });
    
                //]]>
                </script>                
                </td>
			  </tr>
			  <tr>
				<td>&nbsp;</td>
				<td><input type="submit" name="submit" value="ارسال" /></td>
			  </tr>
			</table>
			</form>
			<?php
		}else{
			
		}
		break;
	}
	
?>
                            </div>
                            </div>
                        </div>                        
                    </div>
                    <div class="clear"></div>
                </div><!-- dashboard-widgets-wrap -->
            </div><!-- wrap -->
			<div class="clear"></div></div><!-- wpbody-content -->
			<div class="clear"></div></div><!-- wpbody -->
			<div class="clear"></div></div><!-- wpcontent -->
        </div>
    </div><!-- wpcontent -->
</div><!-- wpwrap -->
<?php include("include/footer.php");?>
</div>
</div>
</body>
</html><?php } ?>