<?php
/*
	[Office 515158] (C) 2009-2016 天生创想 Inc.
	$Id: newstart.php 1209087 2016-09-20
*/
define('IN_ADMIN',True);
require_once('include/common.php');
function UserAgent(){   
    $user_agent = ( !isset($_SERVER['HTTP_USER_AGENT'])) ? FALSE : $_SERVER['HTTP_USER_AGENT'];   
    return $user_agent;   
} 
//Mobile   
if ((preg_match("/(iphone|ipod|android)/i", strtolower(UserAgent()))) AND strstr(strtolower(UserAgent()), 'webkit')){   
    header('Location: m/index.php');   
    exit;   
}else if(trim(UserAgent()) == '' OR preg_match("/(nokia|sony|ericsson|mot|htc|samsung|sgh|lg|philips|lenovo|ucweb|opera mobi|windows mobile|blackberry)/i", strtolower(UserAgent()))){   
    header('Location: m/index.php');   
    exit;   
}
get_login($_USER->id);
global $_CACHE;
get_cache(DB_JT.'menu');

if(getGP('do','G')=='sms'){
	$query = $db->query("SELECT a.senduser,a.sendid,a.senddate,a.content,a.receiveid,b.pic FROM 
	".DB_TABLEPRE."sms_receive a,".DB_TABLEPRE."user_view b  where a.receiveuser=b.uid and  a.smskey='1'
	and a.receiveuser='".$_USER->id."' order by a.senddate desc");
	$db->query("UPDATE ".DB_TABLEPRE."sms_receive set online=1 WHERE  receiveuser='".$_USER->id."'");
	while ($row = $db->fetch_array($query)) {
		echo '<li>';
		echo '<div class="media">';
		if($row['pic']!=''){
			$pic= $row['pic'];
		}else{
			$pic= 'template/default/images/u.jpg';
		}
		echo '<img class="d-flex align-self-center img-radius" src="'.$pic.'" alt="Generic placeholder image">';
		echo '<div class="media-body">';
		echo '<h5 class="notification-user">'.get_realname($row['senduser']).'</h5>';
		$c=unserialize($row['content']);
		echo '<p class="notification-msg">'.SMS::ContentShow($c['name'],$c['content'],$c['url'],$c['type']).'</p>';
		echo '<span class="notification-time">'.Sms_Date(strtotime($row['senddate'])).'</span></div>';
		echo '</div>';
		echo '</li>';
	}
	exit;
}
if(getGP('do','G')=='SmsShow'){
	$name=unescape(getGP('name','G'));
	if($name){
		$where=" and b.name like'%".$name."%'";
	}
	$num = $db->result("SELECT COUNT(*) AS num FROM ".DB_TABLEPRE."sms_service a,".DB_TABLEPRE."user_view b where a.user=b.uid and a.uid='".$_USER->id."' ".$where." order by a.online desc,a.updatedate desc");
	$query = $db->query("SELECT a.*,b.pic,b.name FROM ".DB_TABLEPRE."sms_service a,".DB_TABLEPRE."user_view b where a.user=b.uid and a.uid='".$_USER->id."' ".$where." order by a.online desc,a.updatedate desc");
	if($num>0){
		while ($row = $db->fetch_array($query)) {
			if($row['pic']==''){
				$row['pic']= 'template/default/images/u.jpg';
			}
			
			echo '<div class="media userlist-box" data-id="1" data-status="online" data-username="'.$row['name'].'" data-toggle="tooltip" data-placement="left" title="'.$row['name'].'" onClick="_SmsContact('.$row['sid'].');">';
			echo '<a class="media-left">';
			echo '<img class="media-object img-radius img-radius" src="'.$row['pic'].'">';
			echo '<div class="live-status bg-success"></div>';
			echo '</a>';
			echo '<div class="media-body">';
			echo '<div class="f-13 chat-header">'.$row['name'].'</div>';
			echo '</div>';
			echo '</div>';
			
			/*
			
			echo '<li class="contact" onClick="_SmsContact('.$row['sid'].');" style="border-bottom:1px #F0F0F0 dashed;">';
			echo '<div class="contact-avatar">';
			echo '<img src="'.$row['pic'].'" />';
			echo '</div>';
			echo '<div class="contact-info">';
			echo '<div class="contact-name">'.$row['name'].'</div>';
			echo '<div class="contact-c">&nbsp;';
			$c=unserialize($row['content']);
			echo SMS::ContentShow($c['name'],$c['content'],$c['url'],$c['type']);
			//echo '<br>'.Annex::Show('16',$row['sendid'],'2');
			echo '</div>';
			echo '<div class="contact-status">';
			if($row['online']==1){
				echo '<div class="online"></div>';
				echo '<div class="status">在线</div>';
			}else{
				echo '<div class="offline"></div>';
				echo '<div class="status">离线</div>';
			}
			echo '</div>';
			echo '<div class="last-chat-time">';
			echo Sms_Date(strtotime($row['updatedate']));
			echo '</div>';
			echo '</div>';
			echo '</li>';*/
		}
	}else{
		echo '<div class="well bordered-top bordered-bottom bordered-pink" ';
		echo 'style="margin-left:10px; margin-right:10px; margin-top:50px;">';
		echo '<h4 class="block">还没有添加联系人？</h4>';
		//echo '<p> <a href="javascript:;" onClick="_SmsStart();" ';
		//echo 'class="btn btn-blue">一键更新联系人</a>';
		//echo '</p>';
		echo '</div>';
	}
	exit;
}
if(getGP('do','G')=='SmsBack'){
	$sid=getGP('sid','G','int');
	$sql = "SELECT a.*,b.pic,b.name FROM ".DB_TABLEPRE."sms_service a,".DB_TABLEPRE."user_view b where a.user=b.uid and a.sid='".$sid."'";
	$row = $db->fetch_one_array($sql);
	echo '<div class="media chat-inner-header">';
	echo '<a class="back_chatBox">';
	echo '<i class="ti-angle-left" onClick="_Sms_hide();"></i>'.$row['name'];
	echo '</a>';
	echo '</div>';
	echo '<input type="hidden" name="sid" value="'.$row['sid'].'" />';
	echo '<input type="hidden" name="Sms_User" value="'.$row['user'].'" />';
	exit;
}

if(getGP('do','G')=='SmsMsg'){
	$sid=getGP('sid','G','int');
	$ssql = "SELECT uid,user FROM ".DB_TABLEPRE."sms_service where sid='".$sid."'";
	$s = $db->fetch_one_array($ssql);
	$num = $db->result("SELECT COUNT(*) AS num FROM ".DB_TABLEPRE."sms_receive where (senduser='".$s['uid']."' and receiveuser='".$s['user']."') or (receiveuser='".$s['uid']."' and senduser='".$s['user']."') ORDER BY receiveid asc");
	if(($num-30)>=0){
		$nums=$num-30;
	}else{
		$nums=0;
	}
	$sql = "SELECT * FROM ".DB_TABLEPRE."sms_receive where (senduser='".$s['uid']."' and receiveuser='".$s['user']."') or (receiveuser='".$s['uid']."' and senduser='".$s['user']."')  ORDER BY receiveid asc LIMIT ".$nums.", ".$num."";
	$result = $db->fetch_all($sql);
	foreach ($result as $row) {
		$db->query("UPDATE ".DB_TABLEPRE."sms_receive set online=1 WHERE receiveid='".$row['id']."'");
		$usersql = "SELECT pic FROM ".DB_TABLEPRE."user_view  WHERE uid='".$row['senduser']."'";
		$user = $db->fetch_one_array($usersql);
		if($user['pic']==''){
			$user['pic']= 'template/default/images/u.jpg';
		}
		//$content=preg_replace("/(<a.*?)(.*)(<\/a>)/","", trim($row['content'])); 
		$c=unserialize($row['content']);
		if(trim($row['senduser'])==trim($s['user'])){
			echo '<div class="media chat-messages">';
			echo '<a class="media-left photo-table" href="#!">';
			echo '<img class="media-object img-radius img-radius m-t-5" src="'.$user['pic'].'"><br>'.trim(get_realname($row['senduser'])).'';
			echo '</a>';
			echo '<div class="media-body chat-menu-content">';
			echo '<div class="">';
			echo '<p class="chat-cont">';
			echo SMS::ContentShow($c['name'],$c['content'],$c['url'],$c['type']);
			echo '<br>'.Annex::Show('16',$row['sendid'],'2');
			echo '</p>';
			echo '<p class="chat-time">'.Sms_Date(strtotime($row['senddate'])).'</p>';
			echo '</div>';
			echo '</div>';
			echo '</div>';
			
			
			
		}else{
			echo '<div class="media chat-messages">';
			echo '<div class="media-body chat-menu-reply">';
			echo '<div class="">';
			echo '<p class="chat-cont">';
			echo SMS::ContentShow($c['name'],$c['content'],$c['url'],$c['type']);
			echo '<br>'.Annex::Show('16',$row['sendid'],'2');
			echo '</p>';
			echo '<p class="chat-time">'.Sms_Date(strtotime($row['senddate'])).'</p>';
			echo '</div>';
			echo '</div>';
			echo '<div class="media-right photo-table">';
			echo '<a href="#!">';
			echo '<img class="media-object img-radius img-radius m-t-5" src="'.$user['pic'].'"><br>'.trim(get_realname($row['senduser'])).'';
			echo '</a>';
			echo '</div>';
			echo '</div>';
		
		
		/*
		
			echo '<li class="message reply">';
			echo '<div class="message-info">';
			echo '<div class="bullet"></div>';
			echo '<div class="contact-name">'.trim(get_realname($row['senduser'])).'</div>';
			echo '<div class="message-time">'.Sms_Date(strtotime($row['senddate'])).'</div>';
			echo '</div>';
			echo '<div class="message-body" style="margin-top:12px;">';
			echo SMS::ContentShow($c['name'],$c['content'],$c['url'],$c['type']);
			echo '<br>'.Annex::Show('16',$row['sendid'],'2');
			echo '</div>';
			echo '</li>';*/
		}
	}
	exit;
}
if(getGP('do','G')=='SmsAdd'){
	$sid=getGP('sid','G','int');
	$Sms_User=get_realname(getGP('Sms_User','G','int'));
	$SMS_CONTENT=unescape(getGP('SMS_CONTENT','G'));
	$sms_content = array();
	$sms_content['name']='短消息';
	$sms_content['content']=$SMS_CONTENT;
	$sms_content['url']='';
	$sms_content['type']='7';
	$sendid=sms::sms_add($Sms_User,serialize($sms_content));
	echo $sid;
	exit;
}
if(getGP('do','G')=='styleadd'){
	$Layouttype=getGP('Layouttype','G');
	$handlemenutype=getGP('handlemenutype','G');
	$vertical=getGP('vertical','G');
	$verticalico=getGP('verticalico','G');
	$verticalsubitem=getGP('verticalsubitem','G');
	$themecolor=getGP('themecolor','G');
	$navbgpattern=getGP('navbgpattern','G');
	$type=getGP('type','G');
	$homemana=$Layouttype.'/'.$handlemenutype.'/'.$vertical.'/'.$verticalico.'/'.$verticalsubitem.'/'.$themecolor.'/'.$navbgpattern;
	if($type==1){
		$db->query("UPDATE ".DB_TABLEPRE."user_view set homemana='' WHERE uid='".$_USER->id."'");
	}else{
		$db->query("UPDATE ".DB_TABLEPRE."user_view set homemana='".$homemana."' WHERE uid='".$_USER->id."'");
	}
	echo '风格数据保存成功！';
	exit;
}



$usersql = "SELECT pic,homemana,hometype FROM ".DB_TABLEPRE."user_view  WHERE uid='".$_USER->id."'";
$user = $db->fetch_one_array($usersql);
if($user['hometype']=='1'){
	header('Location: newstart.php?'.get_date('YmdHis',PHP_TIME));   
    exit;
}
if($user['pic']!=''){
	$user['pic']=$user['pic'];
}else{
	$user['pic']='template/default/images/u.jpg';
}
if($user['homemana']){
	$homemana=explode('/',$user['homemana']);
	$Layouttype=$homemana[0];
	$handlemenutype=$homemana[1];
	$vertical=$homemana[2];
	$verticalico=$homemana[3];
	$verticalsubitem=$homemana[4];
	$themecolor=$homemana[5];
	$navbgpattern=$homemana[6];
}
include_once template.'desktop.php';
function _Nico($id,$type){
	if($type){
		switch ($id)
		{
			case 3://个人办公
			  return "4099ff";
			  break;
			case 4://流程审批
			  return "f794a9";
			  break;
			case 7://CRM系统
			  return "f3cc2f";
			  break;
			case 10://行政办公
			  return "b38675";
			  break;
			case 11://人力资源
			  return "5dbcc2";
			  break;
			case 410://公文管理
			  return "6f3ef8";
			  break;
			case 8://项目管理
			  return "f78ea4";
			  break;
			case 9://知识交流
			  return "55dfc4";
			  break;
			case 58://系统设置
			  return "f3cc2f";
			  break;
			case 600://集团审批
			  return "2ed8b6";
			  break;
			case 404://用车管理
			  return "ff5370";
			  break;
			case 6://档案管理
			  return "4099ff";
			  break;
			default:
			  return "4099ff";
		}
	}else{
		switch ($id)
		{
			case 3://个人办公
			  return "ti-user";
			  break;
			case 4://流程审批
			  return "ti-share-alt";
			  break;
			case 7://CRM系统
			  return "ti-bar-chart";
			  break;
			case 10://行政办公
			  return "ti-briefcase";
			  break;
			case 11://人力资源
			  return "ti-book";
			  break;
			case 410://公文管理
			  return "ti-wallet";
			  break;
			case 8://项目管理
			  return "ti-receipt";
			  break;
			case 9://知识交流
			  return "ti-layers-alt";
			  break;
			case 58://系统设置
			  return "ti-settings";
			  break;
			case 600://集团审批
			  return "ti-pencil-alt";
			  break;
			case 404://用车管理
			  return "ti-truck";
			  break;
			case 6://档案管理
			  return "ti-layout-accordion-merged";
			  break;
			case 31://1
			  return "ti-pencil-alt";
			  break;
			case 304://2
			  return "ti-bar-chart";
			  break;
			default:
			  return "ti-microsoft-alt";
		}
	}
}

function _Nmenu($muid,$type=0){
	global $_CACHE;
	get_cache(DB_JT.'menu');
	foreach ( $_CACHE[DB_JT.'menu'] as $row) {
		if($row['fatherid']==$muid){
				if(crm_menu_view($row["menuid"])){
					if($row[menutype]!='1'){
						if($row[keytable]!=''){
							if(is_superadmin() || check_purview($row[keytable])){
							echo '<li class="pcoded-hasmenu">';
							echo '<a href="javascript:void(0)">';
							echo '<span class="pcoded-micon"><i class="ti-star"></i><b>I</b></span>';
							echo '<span class="pcoded-mtext">'.$row[menuname].'</span>';
							echo '<span class="pcoded-mcaret"></span></a>';
							echo '<ul class="pcoded-submenu">';
							_Nmenu($row[menuid]);
							
							echo '</ul>';
							echo '</li>';
								
							}
						}else{
							echo '<li class="pcoded-hasmenu">';
							echo '<a href="javascript:void(0)">';
							echo '<span class="pcoded-micon"><i class="ti-star"></i><b>I</b></span>';
							echo '<span class="pcoded-mtext">'.$row[menuname].'</span>';
							echo '<span class="pcoded-mcaret"></span></a>';
							echo '<ul class="pcoded-submenu">';
							_Nmenu($row[menuid]);
							
							echo '</ul>';
							echo '</li>';
							
						}
					}
				}else{
					if($row[menutype]!='1'){
						if($row[keytable]!=''){
							if(is_superadmin() || check_purview($row[keytable])){
								echo '<li class=" ">';
								echo '<a href="';
								echo 'javascript:_M('.$row[menuid].',';
								echo "'".$row[menuurl]."')";
								echo '" ><span class="pcoded-micon"><i class="ti-reload rotate-refresh"></i><b>A</b></span>
								<span class="pcoded-mtext">';
								echo $row[menuname].'</span><span class="pcoded-mcaret"></span></a>';
								echo '</li> ';
							}
						}else{
							echo '<li class=" ">';
								echo '<a href="';
								echo 'javascript:_M('.$row[menuid].',';
								echo "'".$row[menuurl]."')";
								echo '" ><span class="pcoded-micon"><i class="ti-reload rotate-refresh"></i><b>A</b></span>
								<span class="pcoded-mtext">';
								echo $row[menuname].'</span><span class="pcoded-mcaret"></span></a>';
								echo '</li> ';
						}
					}
				}
		}
	}
}
function crm_topmenu_2($fid=0){
	global $_CACHE;
	get_cache(DB_JT.'menu');
	foreach ( $_CACHE[DB_JT.'menu'] as $rowuser) {
		if($rowuser['menuid']==$fid){
			return menujavascript($rowuser["menuid"],$rowuser["menuname"],$rowuser["menuurl"]);
		}
	}
}
function menujavascript($id,$name,$url){
	$html='';
	$html.='<a href="javascript:_M('.$id.',';
	$html.="'".$url."'";
	$html.=')">'.$name.'</a>';
	return $html;
}
?>