<?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')=='menu'){
	$id=getGP('id','G','int');
	if($id!=''){
		echo '<div class="second-panel">';
			echo '<h4>'.$_CACHE[DB_JT.'menu'][$id]['menuname'].'</h4>';
			echo '<ul class="second-menu clearfix">';
			_Nmenu($id);
			echo '</ul>';
		echo '</div>';
	}
	exit;
}
if(getGP('do','G')=='topmenu'){
	global $_CACHE;
	get_cache(DB_JT.'menu');
	foreach ( $_CACHE[DB_JT.'menu'] as $blog) {
		if($blog["menuid"]==getGP('menuid','G')){
			if($blog["fatherid"]!='0'){
				foreach ( $_CACHE[DB_JT.'menu'] as $blog1) {
					if($blog1["menuid"]==$blog["fatherid"]){
						if($blog1["fatherid"]!='0'){
							$html2="<li class='active'>".crm_topmenu_2($blog1["fatherid"])."</li>";
							$html1="<li class='active'>".menujavascript($blog1["menuid"],$blog1["menuname"],$blog1["menuurl"])."</li>";
						}else{
							$html1="<li class='active'>".menujavascript($blog1["menuid"],$blog1["menuname"],$blog1["menuurl"])."</li>";
						}
					}
				}
				$html="<li class='active'>".menujavascript($blog["menuid"],$blog["menuname"],$blog["menuurl"])."</li>";
			}else{
				$html="<li class='active'>".menujavascript($blog["menuid"],$blog["menuname"],$blog["menuurl"])."</a></li>";
			}
		}
	}
	echo '<li><i class="fa fa-home"></i><a href="';
	echo "javascript:_M(0,'home.php?file=new')";
	echo '">桌面</a></li>'.$html2.$html1.$html;
	exit;
}
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 online='0'
	and receiveuser='".$_USER->id."'");
	while ($row = $db->fetch_array($query)) {
		echo '<li>';
		echo '<a href="javascript:_M(214,';
		echo "'admin.php?ac=receive&fileurl=sms');";
		echo '">';
		if($row['pic']!=''){
			echo '<img src="'.$row['pic'].'" ';
		}else{
			echo '<img src="template/default/images/u.jpg" ';
		}
		echo 'class="message-avatar">';
		echo '<div class="message">';
		echo '<span class="message-sender">';
		echo get_realname($row['senduser']);
		echo '</span>';
		echo ' <span class="message-time">';
		echo Sms_Date(strtotime($row['senddate']));
		echo '</span>';
		echo '<span class="message-body">';
		$c=unserialize($row['content']);
		echo SMS::ContentShow($c['name'],$c['content'],$c['url'],$c['type']);
		//echo Annex::Show('16',$row['sendid'],'2');
		echo '</span>';
		echo '</div>';
		echo '</a>';
		echo '</li>';
	}
	exit;
}
if(getGP('do','G')=='Mail'){
	$query = $db->query("SELECT a.*,b.pic FROM 
	".DB_TABLEPRE.DB_JT."recemail a,".DB_TABLEPRE."user_view b  where 
	a.receuser=b.uid and  a.type='0'
	and a.receuser='".$_USER->id."' order by a.date desc");
	while ($row = $db->fetch_array($query)) {
		echo '<li>';
		echo '<a href="javascript:_M(303,';
		echo "'admin.php?ac=email&do=view&fileurl=email&id=".$row['id']."');";
		echo '">';
		if($row['pic']!=''){
			echo '<img src="'.$row['pic'].'" ';
		}else{
			echo '<img src="template/default/images/u.jpg" ';
		}
		echo 'class="message-avatar">';
		echo '<div class="message">';
		echo '<span class="message-sender">';
		echo get_realname($row['user']);
		echo '</span>';
		echo ' <span class="message-time">';
		echo Sms_Date(strtotime($row['date']));
		echo '</span>';
		echo '<span class="message-subject">';
        echo $row['subject'];
        echo '</span>';
		echo '<span class="message-body">';
		$content=preg_replace("/(<a.*?>)(.*)(<\/a>)/","", trim($row['content']));  
		//$content=preg_replace("<a([^>]*)>(.*)</a>", " ",trim($row['content']));
		echo $content;
		echo '</span>';
		echo '</div>';
		echo '</a>';
		echo '</li>';
	}
	exit;
}
if(getGP('do','G')=='SmsStart'){
	$user=SMS::UserList($_USER->id);
	if($user){
		$user=explode(',',$user); 
		for($i=0;$i<sizeof($user);$i++){
			if($user[$i]){
				SMS::new_add($_USER->id,$user[$i],get_date('Y-m-d H:i:s',PHP_TIME),'',0);
			}
		}
	
	}
	echo $user;
	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 '<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="contact-avatar">';
	echo '<input type="hidden" name="sid" value="'.$row['sid'].'" />';
	echo '<input type="hidden" name="Sms_User" value="'.$row['user'].'" />';
	if($row['pic']==''){
		$row['pic']= 'template/default/images/u.jpg';
	}
    echo '<img src="'.$row['pic'].'" />';
    echo '</div>';
    echo '<div class="contact-info">';
    echo '<div class="contact-name">'.$row['name'].'</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 '</div>';
    echo '<div class="back" onClick="_back();">';
    echo '<i class="fa fa-arrow-circle-left"></i>';
    echo '</div>';
    echo '</div>';
	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']."'");
		//$content=preg_replace("/(<a.*?)(.*)(<\/a>)/","", trim($row['content'])); 
		$c=unserialize($row['content']);
		if(trim($row['senduser'])==trim($s['user'])){
			echo '<li class="message">';
			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 trim($content);
			echo '</div>';
			echo '</li>';
		}else{
			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;
}

$usersql = "SELECT pic,hometype FROM ".DB_TABLEPRE."user_view  WHERE uid='".$_USER->id."'";
$user = $db->fetch_one_array($usersql);
if($user['pic']!=''){
	$user['pic']=$user['pic'];
}else{
	$user['pic']='template/default/images/u.jpg';
}
 
include_once template.'newstart.php';
function _Nico($id){
	switch ($id)
	{
		case 3:
		  return "fa-user";
		  break;
		case 4:
		  return "fa-retweet";
		  break;
		case 7:
		  return "fa-group";
		  break;
		case 10:
		  return "fa-briefcase";
		  break;
		case 11:
		  return "fa-sitemap";
		  break;
		case 410:
		  return "fa-folder-open";
		  break;
		case 6:
		  return "fa-folder";
		  break;
		case 8:
		  return "fa-list-alt";
		  break;
		case 9:
		  return "fa-comments";
		  break;
		case 58:
		  return "fa-gears";
		  break;
		case 600:
		  return "fa-qrcode";
		  break;
		case 31:
		  return "fa-road";
		  break;
		case 304:
		  return "fa-volume-up";
		  break;
		case 404:
		  return "fa-truck";
		  break;
		case 6:
		  return "fa-road";
		  break;
		case 615:
		  return "fa-code";
		  break;
		default:
		  return "fa-desktop";
	}
}
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="expand">';
								echo '<a class="second-menu-item" href="javascript:;" >';
								echo $row[menuname].'</a>';
								echo '<ul class="third-menu">';
								_Nmenu($row[menuid]);
								echo '</ul>';
								echo '</li> ';
							}
						}else{
							echo '<li class="expand">';
							echo '<a class="second-menu-item" href="javascript:;" >';
							echo $row[menuname].'</a>';
							echo '<ul class="third-menu">';
							_Nmenu($row[menuid]);
							echo '</ul>';
							echo '</li> ';
						}
					}
				}else{
					if($row[menutype]!='1'){
						if($row[keytable]!=''){
							if(is_superadmin() || check_purview($row[keytable])){
								echo '<li>';
								echo '<a class="second-menu-item" href="';
								echo 'javascript:_M('.$row[menuid].',';
								echo "'".$row[menuurl]."')";
								echo '" >';
								echo $row[menuname].'</a>';
								echo '</li> ';
							}
						}else{
							echo '<li>';
							echo '<a class="second-menu-item" href="';
							echo 'javascript:_M('.$row[menuid].',';
							echo "'".$row[menuurl]."')";
							echo '" >';
							echo $row[menuname].'</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;
}
?>