<?php
/*
 * lib_main相关函数库
 * 
============================================================================
 * 版权所有 2012-2020 深圳朝九科技有限公司，并保留所有权利。
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: Yukey  QQ:2787720874
*/
if (!defined('YUKEY'))
{
    die('Hacking attempt');
}
/*
 * 更新用户SESSION,COOKIE及登录时间、登录次数。
 *
*/
function check_user($username, $password)
    {       
            $sql = "SELECT tbID FROM " . $GLOBALS['si']->table('users'). " WHERE tbUserName ='" . $username . "' AND tbPassword ='" . $password . "'";
		    if ($GLOBALS['db']->getOne($sql))
			{
			    $_SESSION['tbID']=$GLOBALS['db']->getOne($sql);
				return true;
			}
			else
			{
			    return false;
			}
}
function update_user_info()
{
    if (!$_SESSION['tbID'])
    {
        return false;
    }
    /* 查询会员信息 */

    $sql = "SELECT u.tbID,u.tbCompanyID,u.tbName,u.tbMail,u.tbRank,u.tbLastTime,c.tbNickName,c.tbCooperation FROM " . $GLOBALS['si']->table('users') . "AS u LEFT JOIN" . $GLOBALS['si']->table('companys') . " AS c ON u.tbCompanyID=c.tbCompanyID WHERE tbID = '" . $_SESSION['tbID'] ."'";

    if ($row = $GLOBALS['db']->getRow($sql))
    {
        /* 更新SESSION */
        $_SESSION['tbCompanyID']  = $row['tbCompanyID'];
        $_SESSION['tbName'] = $row['tbName'];
		$_SESSION['tbMail'] =  $row['tbMail'];
		$_SESSION['tbRank']=$row['tbRank'];
		$_SESSION['tbLastTime'] = $row['tbLastTime'];
        $_SESSION['tbNickName']  = $row['tbNickName'];
		$_SESSION['tbCooperation'] = $row['tbCooperation'];
    }
    /* 更新登录时间，登录次数及登录ip */

    $sql = "UPDATE " .$GLOBALS['si']->table('users'). " SET".
           " tbVisitCount = tbVisitCount + 1, ".
           " tbLastTime = '" . date("Y-m-d H:i:s") . "'".
           " WHERE tbID = '" . $_SESSION['tbID'] . "'";
    $GLOBALS['db']->query($sql);
}
function add_prefix($p,$y,$t,$f,$v,$a,$x,$c){
	$s='';
	if(isset($_SESSION['tbCompanyID']) ){
		  switch ($_SESSION['tbCompanyID']){
			case '2':
				   $s = $p;
			  break;
			case '3':
				   $s = $p;
			  break;
			case '5':
				   $s = $p;
			  break;
			case '9':
				   $s = $c;
			  break;
			case '14':
				   $s = $p;
			  break;
			case '24':
				   $s = $y;
			  break;
			case '25':
				   $s = $p;
			  break;
			case '26':
				   $s = $p;
			  break;
			case '27':
				   $s = $p;
			  break;
			case '28':
				   $s = $p;
			  break;
			case '30':
				   $s = $y;
			  break;
			case '31':
				   $s = $y;
			  break;
			case '32':
				   $s = $p;
			  break;
			case '33':
				   $s = $c;//这里只能用c以方便从邮箱链接登入，不能改成单一客人，新版可以改成单一客人。
			  break;
			case '34':
				   $s = $y;
			  break;
			case '35':
				   $s = $y;
			  break;
			case '36':
				   $s = $y;
			  break;
			case '37':
				   $s = $t;
			  break;
			case '38':
				   $s = $t;
			  break;
			case '40':
				   $s = $p;
			  break;
			case '45':
				   $s = $f;
			  break;
			case '46':
				   $s = $v;
			  break;
			case '47':
				   $s = $v;
			  break;
			case '51':
				   $s = $a;
			  break;
			case '69':
				   $s = $x;
			  break;
			default:
				   $s = $c;
			}
	}
	return $s;
}
function check_login(){

	if(empty($_SESSION['tbRank'])) {
		exit('Access Denied');
	}
}
function check_hrank(){
	if($_SESSION['tbRank']<80) {
		exit('Access Denied');
	}
}
function check_rank(){
	if($_SESSION['tbRank']<70) {
		exit('Access Denied');
	}
}
function listpage($count)
{
  $pages="";
  for($i=1;$i<=$count;$i++)
    { 
      $pages[].=$i;                 
    }
  return $pages;
}
function get_products_list($sql='')
{
	 $sql = "SELECT tbWN,tbClientID,tbProductName,tbVersion,tbModel FROM " . $GLOBALS['prefix']->table('instructs') . " ORDER BY tbProductName asc";
	 if ($sql!=''){
     return $GLOBALS['db']->getAll($sql);
     }else {
     return false;
     }
} 
function leftjoin($s,$field,$l,$r,$k,$v,$table1,$table2,$order)//$table1的表前缀必需非si_开头
{
	  $sql ='';
	  if($order!=''){
	 ($k !='' && $v !='') ? $condition = " WHERE $k='$v' ORDER BY $order ASC" : $condition =" ORDER BY $order ASC";
	 }else{
	 ($k !='' && $v !='') ? $condition = " WHERE $k='$v' LIMIT 1" : $condition =" LIMIT 1";
	 }
	 if($s=='si'){
	 $sql = "SELECT $field FROM " . $GLOBALS['prefix']->table($table1) ." AS a LEFT JOIN " .$GLOBALS['si']->table($table2)." AS b ON a.".$l."=b.".$r. $condition;
	 }else{
	  $sql = "SELECT $field FROM " . $GLOBALS['prefix']->table($table1) ." AS a LEFT JOIN " .$GLOBALS['prefix']->table($table2)." AS b ON a.".$l."=b.".$r. $condition;
	  }
	 if ($sql!=''){
     		if($order!=''){
			return $GLOBALS['db']->getAll($sql);
			}else{
			return $GLOBALS['db']->getRow($sql);
			}
     }else{
     		return false;
     }
}
//与get_rows有区别的地方是AND tbOpen='1'

function get_users($k,$v)
{
	 ($k !='' && $v !='') ? $condition = " WHERE $k='$v' AND tbOpen='1' ORDER BY tbName asc" : $condition ='';
	 $sql = "SELECT tbID,tbCompanyID,tbOpen,tbName,tbCnName FROM " . $GLOBALS['si']->table('users') . $condition;
	 if ($condition != '' && $sql != ''){
     return $GLOBALS['db']->getAll($sql);
     }else{
     return false;
     }
}
function get_mail_list($cy_id,$client_id,$factory_id)
{
     $sql = "SELECT tbID,tbCompanyID,tbOpen,tbName,tbMail,tbSentMail FROM " . $GLOBALS['si']->table('users') . " WHERE tbCompanyID in ($cy_id,$client_id,$factory_id) AND tbOpen='1' AND tbSentMail='1' ORDER BY tbID asc";
	 if ($sql!=''){
     return $GLOBALS['db']->getAll($sql);
     }else{
     return false;
     }
}
/**
 * 用于替换发送邮件时的各个用户名
 *
 * @access  public
 * @return  string
 */
function dear_name($begin,$end,$name,$str)
{
		 $b=$l='';
         $b = mb_strpos($str,$begin) + mb_strlen($begin);
         $l = mb_strpos($str,$end) - $b;
         $str=str_replace(mb_substr($str,$b,$l),$name,$str);
		 return $str;
}

//在段落中显示彩色字体
function show_color($begin,$end,$color,$str){

                $b = mb_strpos($str,$begin) + mb_strlen($begin);

                $e = mb_strpos($str,$end) - $b;



                return str_replace( mb_substr($str,$b,$e),"<font class='f_".$color."'>" . mb_substr($str,$b,$e). "</font>",$str );

}
function get_count($k,$v,$table,$date,$begin,$end)
{

	($k !='' && $v !='') ? $condition = " WHERE $k='$v' " : $condition ='';

	($begin !='' && $end !='') ? $condition1 = " AND $date>='$begin' AND $date<='$end'" : $condition1 ='';

	$str="SELECT COUNT(*) FROM " . $GLOBALS['prefix']->table($table) . $condition . $condition1;	

    if ($GLOBALS['db']->getOne($str)){

     	return $GLOBALS['db']->getOne($str);

    }else{

     	return false;

    }

}
function count_item($k,$v,$table,$condition='')
{
	if($_SESSION['tbRank']>='50'){

        ($k !='') ? $condition="WHERE $k='$v'" : $condition='';

    }elseif($_SESSION['tbRank']<='40' && $_SESSION['tbRank']>'20'){

	    ($k !='') ? $condition="WHERE tbFactoryID='" . $_SESSION['tbCompanyID'] . "' AND $k='$v'" : $condition="WHERE tbFactoryID='" . $_SESSION['tbCompanyID'] . "'";

	}elseif($_SESSION['tbRank']<='20'){

	    ($k !='') ? $condition="WHERE tbClientID='" . $_SESSION['tbCompanyID'] . "' AND $k='$v'" : $condition="WHERE tbClientID='" . $_SESSION['tbCompanyID'] . "'";

	}  //($k !='' && $v !='')使用该条件时在搜索页面搜不到结果时出现所有结果，所以取消&& $v !=''，但取消后会不会有新的bug出现？

		 $sql="SELECT COUNT(*) FROM " . $GLOBALS['prefix']->table($table) . $condition;	

     if ($sql!=''){
     return $GLOBALS['db']->getOne($sql);
     }else{
     return false;
     }
}
function get_book_pury($po_no)
{
      $sql='';
	  $sql = "SELECT a.tbSN,a.tbBookingDate,a.tbPONumber,a.tbCheckone,a.tbClientID,a.tbFactoryID,a.tbInspectionDate,a.tbFactoryBookNO,a.tbPM,a.tbStatus,a.tbTeam,a.tbFrom,a.tbRevisedCount,b.tbCompanyName,b.tbNickName,c.tbNickName AS client FROM " . $GLOBALS['prefix']->table('projects'). "AS a LEFT JOIN" . $GLOBALS['si']->table('companys') . "AS b ON a.tbFactoryID = b.tbCompanyID LEFT JOIN" . $GLOBALS['si']->table('companys') . " AS c ON a.tbClientID = c.tbCompanyID WHERE a.tbPONumber='". $po_no ."'";
	 if ($sql!=''){
     return $GLOBALS['db']->getRow($sql);
     }else{
     return false;
     }
} 
function get_rows($s,$field,$seek,$table,$order)
{
	  $sql ='';
	 ($seek !='') ? $condition = " WHERE $seek ORDER BY $order ASC" : $condition =" ORDER BY $order ASC";

	 if($s=='si'){
	 $sql = "SELECT $field FROM " . $GLOBALS['si']->table($table) . $condition;
	 }else{
	  $sql = "SELECT $field FROM " . $GLOBALS['prefix']->table($table) . $condition;
	  }
	 if ($sql!=''){

     		return $GLOBALS['db']->getAll($sql);
     }else{
     		return false;
     }
}
function get_row($s,$field,$k,$v,$table)
{
	 ($k !='' && $v !='') ? $condition = " WHERE $k='$v' LIMIT 1" : $condition =" LIMIT 1";

	  if($s=='si'){
	  $sql = "SELECT $field FROM " . $GLOBALS['si']->table($table) . $condition;
	  }else{
	  $sql = "SELECT $field FROM " . $GLOBALS['prefix']->table($table) . $condition;
	  }
	 if ($sql!=''){
     		return $GLOBALS['db']->getRow($sql);
     }else{
     		return false;
     }
}

function sum_defect($rep)
{
	if($_SESSION['tbRank'])
	{
	     $sql = "SELECT SUM(tbCritical) AS CR,SUM(tbMajor) AS MA,SUM(tbMinor) AS MI FROM " . $GLOBALS['prefix']->table('find_defects') . " WHERE tbReportNumber='".$rep."'";	 
		 if ($sql!=''){
		 return $GLOBALS['db']->getRow($sql);
		 }else{
		 return false;
		 }
	}else{
	return false;
	}
} 

function getcontent($url)
{
	if(!is_readable($url))
	{
	echo "<div align=center><font color=red>".$url."777检测不通过</font><a href=install.php>返回</a><br></div>";

	}else{
	 $f=file($url);
	 $count = count($f);
	 for ($i=0;$i<$count;$i++)
	     {
		 @$content.= $f[$i];
		 }
	 }
	return $content;
}

function less($str){
	$ss='';
	$i=0;
	if(!empty($str)){
		if(explode(PHP_EOL,$str)){
			$s=explode(PHP_EOL,$str);
			foreach($s as $v){
			$i++;
			$ss.=" $i. ".$v."<br>";
			}
		return $ss;
		}else{
		return $str;
		}
	}else{
	return '';
	}
}
?>