<?php
//出错提示
function show_err($str,$url="")
{
	echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />";
	switch($url){
		case "":
			die ("<script>alert('$str');history.back();</script>");
			break;
		case "close":
			die ("<script>alert('$str');window.close();</script>");
			break;
		case "bad_str":
			die ("<script>alert('HMCSS提示，不可以输入以下非法字符：\',\;,\=,\%和union');history.back();</script>");
			break;
		case "reff":
			die ("<script>alert('$str');location.href='" . $_SERVER['HTTP_REFERER'] . "';</script>");
			break;
		default:
			$dir_arr = explode("/", $_SERVER["SCRIPT_NAME"]);
			$ROOT = "/";
			if(count($dir_arr) > 2){
				array_pop($dir_arr);
				$ROOT = implode("/",$dir_arr);
			}
			$url=$ROOT . "/" . $url;
			die ("<script>alert('$str');location.href='$url';</script>");
			break;
	}
}

//检查是否为数字
function check_is_numb($num)
{
	if(strlen(trim($num)) > 0)
		return ereg("^(0|[1-9][0-9]*)$",$num)? true : false;
	else
		return false;
}

//检查输入中是否有非法字符或串
function check_bad_str($str)
{
	//PHP防注入，要过滤的非法字符
	$ArrFiltrate=array("\'\'",";","union","=","%");
	foreach ($ArrFiltrate as $key=>$value){
		if (eregi($value,$str)){
			return true;
		}
	}
	return false;
}

//根据传来的SQL语句返回执行的结果
function get_row_from_sql($sql_name,$my_sql)
{
	$hm_arr=false;
	$query=mysql_query($my_sql)
		or die("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />[$sql_name]传递来的SQL语句有问题");
	while($row=mysql_fetch_row($query))
	{
		$hm_arr[]=$row;
	}
	return $hm_arr;
}
//
function set_config($config_name,$config_content)
{
	$config_arr = array("com_name" => "公司名",
						"main_product" => "主要产品",
						"com_describe" => "公司描述",
						"com_icp" => "ICP",
						"com_count" => "网站计数",
						"site_url" => "URL",
						"com_intro" => "企业简介",
						"com_contact" => "企业联系方式",
						"admin_name" => "管理员名",
						"job_good" => "企业福利待遇"
						);
	mysql_query("update hmcss_config set config_content='" . addslashes($config_content) . "' where config_name='$config_name'")
		or show_err("更新网站配置数据[" . $config_arr[$config_name] . "]出错");
}
//取回网站的相关配置信息
function get_config($str)
{
	$my_sql = "select config_id,config_name,config_content from hmcss_config where config_name = '$str'";
	$hm_arr=get_row_from_sql("读取网站配置信息($str)",$my_sql);
	if(is_array($hm_arr))
		return $hm_arr;
	else
		return false;
}

//改管理员名进行原密码验证
function check_admin_pwd($admin_pwd,$admin_name="")
{
	$my_pwd = get_config("admin_pwd");
	if($admin_name){
		$my_name = get_config("admin_name");
		return $admin_name != $my_name[0][2] || md5(addslashes($admin_pwd)) != $my_pwd[0][2] ? false : true;
	}
	else
		return md5(addslashes($admin_pwd)) != $my_pwd[0][2] ? false : true;
}

//对提交数据的域名进行检查，防止跨域提交
function check_site_url()
{
	if(!$hm_arr = get_config("site_url"))
		show_error("读取网站配置信息出错(site_url)");
	if(strpos($hm_arr[0][2],$_SERVER["HTTP_HOST"]) === false)
		show_err("本站不支持跨域提交信息，请检查您网站配置中的[网站域名配置]");
}

//取回网站图片相关信息
//图片中,img_type的类型设置,1:表示企业荣誉图片,2:表示企业实力图片,3:表示首页幻灯图片
function get_hm_imgs($str,$img_id = 0)
{
	if($img_id){
		$my_sql="select img_id,img_pic,img_des,img_order from hmcss_img where img_id = $img_id";
	}
	else{
		switch($str)
		{
			case "honner":
				$my_sql="select img_id,img_pic from hmcss_img where img_type = 1 order by img_order";
				break;
			case "best":
				$my_sql="select img_id,img_pic from hmcss_img where img_type = 2 order by img_order";
				break;
			case "flash":
				$my_sql="select img_id,img_pic,img_des,img_order from hmcss_img where img_type = 3 order by img_order";
				break;
		}
	}
	$hm_arr=get_row_from_sql("读取网站图片信息($str)",$my_sql);
	if(is_array($hm_arr))
		return $hm_arr;
	else
		return false;
}
//重新生成幻灯用的XML文件
function set_flash_xml()
{
	//查FALSH图片
	if(!$hm_arr = get_hm_imgs("flash"))
		show_err("读取幻灯图片数据时出错");
	$f_count = count($hm_arr);
	//修改XML文件
	$x_file = ROOT_PATH . "bcastr.xml";
	$xml = new DOMDocument;
	$i = 0;
	if($xml->load($x_file)){
		$item_arr = $xml->getElementsByTagName("item");
		foreach($item_arr as $x){
			if($i > $f_count - 1){
				$title = $x->getElementsByTagName("link");
				$title->item(0)->nodeValue = "http://www.morewen.com";
				$title = $x->getElementsByTagName("title");
				$title->item(0)->nodeValue = "HMCSS企业通用网站管理系统";
				$title = $x->getElementsByTagName("image");
				$title->item(0)->nodeValue = "flash_pic/3.gif";
			}
			else{
				$title = $x->getElementsByTagName("link");
				$title->item(0)->nodeValue = "http://www.morewen.com";
				if($i < $f_count){
					$title = $x->getElementsByTagName("title");
					$title->item(0)->nodeValue = $hm_arr[$i][2];
					$title = $x->getElementsByTagName("image");
					$title->item(0)->nodeValue = "flash_pic/" . $hm_arr[$i][1];
				}
			}
			$i++;
		}
		$xml->save($x_file);
		//show_err("幻灯图片已成功修改","set_img_flash.php");
	}
	else
		die("加载XML文件失败");
}

//取单个图片
function get_img_from_id($str,$id)
{
	switch($str)
	{
		case "honner":
		case "best":
			$my_sql="select img_pic,img_order from hmcss_img where img_id=$id";
			break;
		case "product":
			$my_sql="select config_com_contact from hmcss_config";
			break;
		case "site_url":
			$my_sql="select config_site_url from hmcss_config";
			break;
	}
	$hm_arr=get_row_from_sql("读取单个图片信息($str)",$my_sql);
	if(is_array($hm_arr))
		return $hm_arr;
	else
		return false;
}

//文件删除
function del_hm_file($path)
{
	//进行安全检查，仅能删除以下扩展名的文件
	$use_ext=array("GIF","JPG","PNG");
	$hm_arr=explode(".",$path);
	if(!in_array(strtoupper($hm_arr[count($hm_arr)-1]),$use_ext))
		show_err("警告：不能删除当前文件");
	else
	{
		if(file_exists($path))
		{
			if(unlink($path))
				return true;
			else
				return false;
		}
		else
			return false;
	}
}

//对文件进行数据库排序
function hm_set_order($type,$this_order,$action,$id=0)
{
	switch($action)
	{
		case "del":
			mysql_query("update hmcss_img set img_order=img_order-1 where img_order > $this_order") or die("文件整理排序出错($action)");
			break;
		case "up":
			if($thsis_order-1===0)
				show_err("已经是第一个了，不能再上移了……");
			else
			{
				mysql_query("update hmcss_img set img_order = img_order+1 where img_order = $this_order-1") or die("文件整理排序出错($action)，前一文件下移");
				mysql_query("update hmcss_img set img_order = img_order-1 where img_id = $id") or die("文件整理排序出错($action)，当前文件上移");
			}
			break;
		case "down":
			$hm_arr = get_all_count("hmcss_img");
			if($this_order-$hm_arr[0][0]===0)
				show_err("已经是最后一个了，不能再下移了……");
			else
			{
				mysql_query("update hmcss_img set img_order = img_order-1 where img_order = $this_order+1") or die("文件整理排序出错($action)，下一文件上移");
				mysql_query("update hmcss_img set img_order = img_order+1 where img_id = $id") or die("文件整理排序出错($action)，当前文件下移");
			}
			break;
	}			
}

//查当前表中的数量
function get_all_count($table)
{
	$my_sql="select count(*) from $table";
	$hm_arr=get_row_from_sql("计算表信息数量出错",$my_sql);
	if(is_array($hm_arr))
		return $hm_arr;
	else
		return false;
}

//查询最大序号
function get_img_order($type)
{
	switch($type)
	{
		case "honner":
			$img_type = 1;
			break;
		case "best":
			$img_type = 2;
			break;
	}
	$my_sql="select max(img_order) as max_order from hmcss_img where img_type = $img_type";
	$hm_arr=get_row_from_sql("查询最大序号出错($type)",$my_sql);
	if(is_array($hm_arr))
		return $hm_arr;
	else
		return false;
}
//取通用产品分类
function get_class_type($type = "big" , $big_id = 0)
{
	if($type == "big")
		$my_sql="select big_id,big_name from hmcss_bigclass";
	else
		$my_sql="select small_id,small_name from hmcss_smallclass where big_id = $big_id";
	$hm_arr=get_row_from_sql("取产品分类信息($type)出错",$my_sql);
	if(is_array($hm_arr))
		return $hm_arr;
	else
		return false;
}
//取本地分类
function get_product_t($filter_arr="")
{
	$where_str = "";
	if(is_array($filter_arr)){
		foreach($filter_arr as $key => $val){
			switch($val[0]){
				case "0":
					$filter_str = "t_id = $val[1]";
					break;
				case "1":
					$filter_str = "t_name = '$val[1]'";
					break;
				case "2":
					$filter_str = "f_id = $val[1]";
					break;
				//case "2":
				//	$val[2] ? $blear_str = "like '%" . $val[1] . "%'" : $blear_str = "= '$val[1]'";
				//	$filter_str = "product_remark $blear_str";
				//	break;
			}
			$where_str === "" ? $where_str = "where $filter_str" : $where_str = $where_str . " and $filter_str";
		}
	}
	$my_sql="select t_id,f_id,t_name,t_order from hmcss_product_type $where_str order by t_order";
	$hm_arr=get_row_from_sql("查产品分类",$my_sql);
	if(is_array($hm_arr))
		return $hm_arr;
	else
		return false;
}
//取产品数据
function get_product($filter_arr="")
{
	$where_str = "";
	$limit_str = "";
	$filter_str = "";
	if(is_array($filter_arr)){
		foreach($filter_arr as $key => $val){
			switch($val[0]){
				case "0":
					$filter_str = $val[2] ? "p_id <> $val[1]" : "p_id = $val[1]";
					break;
				case "1":
					$filter_str = "t_id = $val[1]";
					break;
				case "2":
					$filter_str = "f_id = $val[1]";
					break;
				case "3":
					$filter_str = "p_name = '$val[1]'";
					break;
				case "4":
					$limit_str = " limit $val[1]";
					break;
				//case "2":
				//	$val[2] ? $blear_str = "like '%" . $val[1] . "%'" : $blear_str = "= '$val[1]'";
				//	$filter_str = "product_remark $blear_str";
				//	break;
			}
			if($filter_str)
				$where_str === "" ? $where_str = "where $filter_str" : $where_str = $where_str . " and $filter_str";
			$filter_str = "";
		}
	}
	$my_sql="select p_id,t_id,f_id,p_name,p_model,p_standar,p_content,p_main_pic,p_pics,p_order from hmcss_product $where_str order by p_order $limit_str";
	//die($my_sql);
	$hm_arr=get_row_from_sql("查产品数据",$my_sql);
	if(is_array($hm_arr))
		return $hm_arr;
	else
		return false;
}

//取各表的总数据
function get_all($t_name)
{
	switch($t_name){
		case "product":
			$my_sql = "select count(p_id) as all_count from hmcss_product";
			$sql_str = "产品";
			break;
	}
	$hm_arr=get_row_from_sql("查$sql_str总数据",$my_sql);
	if(is_array($hm_arr))
		return $hm_arr;
	else
		return false;
}

//取各SEO页面信息
function get_seo($seo_id=0)
{
	if($seo_id)
		$my_sql = "select seo_id,seo_file,seo_title,seo_key,seo_des from hmcss_seo where seo_id = $seo_id order by seo_id";
	else
		$my_sql = "select seo_id,seo_file,seo_title,seo_key,seo_des from hmcss_seo order by seo_id";
	$hm_arr=get_row_from_sql("查SEO数据",$my_sql);
	if(is_array($hm_arr))
		return $hm_arr;
	else
		return false;
}

//取职位信息
function get_job($filter_arr="")
{
	$where_str = "";
	$limit_str = "";
	$filter_str = "";
	if(is_array($filter_arr)){
		foreach($filter_arr as $key => $val){
			switch($val[0]){
				case "0":
					$filter_str = "job_id = $val[1]";
					break;
				case "1":
					$limit_str = " limit $val[1]";
					break;
			}
			if($filter_str)
				$where_str === "" ? $where_str = "where $filter_str" : $where_str = $where_str . " and $filter_str";
			$filter_str = "";
		}
	}
	$my_sql="select job_id,job_name,job_address,job_xl,job_zy,job_pay,job_available,job_require,job_do,job_time from hmcss_job_main $where_str order by job_id $limit_str";
	$hm_arr=get_row_from_sql("查招聘信息",$my_sql);
	if(is_array($hm_arr))
		return $hm_arr;
	else
		return false;
}

//取应聘信息
function get_job_d($filter_arr="")
{
	$where_str = "";
	$limit_str = "";
	$filter_str = "";
	if(is_array($filter_arr)){
		foreach($filter_arr as $key => $val){
			switch($val[0]){
				case "0":
					$filter_str = "job_d_id = $val[1]";
					break;
				case "1":
					$filter_str = "job_id = $val[1]";
					break;
				case "2":
					$limit_str = " limit $val[1]";
					break;
			}
			if($filter_str)
				$where_str === "" ? $where_str = "where $filter_str" : $where_str = $where_str . " and $filter_str";
			$filter_str = "";
		}
	}
	$my_sql="select job_d_id,job_id,user_name,user_xl,user_zy,user_address,user_contact,job_content,user_time from hmcss_job_detail $where_str order by job_d_id $limit_str";
	$hm_arr=get_row_from_sql("查应聘信息",$my_sql);
	if(is_array($hm_arr))
		return $hm_arr;
	else
		return false;
}

//取新闻信息
function get_news($filter_arr="")
{
	$where_str = "";
	$limit_str = "";
	$filter_str = "";
	if(is_array($filter_arr)){
		foreach($filter_arr as $key => $val){
			switch($val[0]){
				case "0":
					$filter_str = "news_id = $val[1]";
					break;
				case "1":
					$limit_str = " limit $val[1]";
					break;
				case "2":
					$filter_str = "news_type = $val[1]";
					break;
			}
			if($filter_str)
				$where_str === "" ? $where_str = "where $filter_str" : $where_str = $where_str . " and $filter_str";
			$filter_str = "";
		}
	}
	$my_sql="select news_id,news_title,news_content,news_top,news_time,news_type,news_hits from hmcss_news $where_str order by news_top,news_time desc $limit_str";
	$hm_arr=get_row_from_sql("查新闻",$my_sql);
	if(is_array($hm_arr))
		return $hm_arr;
	else
		return false;
}
//文章类型
function get_news_type($type_id = 0,$set_type = false)
{
	$type_arr = array("网站新闻","网站公告");
	if($type_id && !$set_type)
		return $type_arr[$type_id-1];
	else{
		$type_str = "<select name=\"news_type\" id=\"news_type\">\n";
		if($type_arr){
			foreach($type_arr as $key => $val){
				if($set_type && $type_id-1-$key == 0)
					$type_str .= " <option value=\"" . ($key+1) . "\" selected=\"selected\">$val</option>\n";
				else
					$type_str .= " <option value=\"" . ($key+1) . "\">$val</option>\n";
			}
		}
		else
			$type_str .= " <option value=\"0\">请配置文章类型</option>\n";
		$type_str .="</select>";
		return $type_str;
	}
}

//前台相关函数+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//取留言
function get_ly($filter_arr="")
{
	$where_str = "";
	$limit_str = "";
	$filter_str = "";
	if(is_array($filter_arr)){
		foreach($filter_arr as $key => $val){
			switch($val[0]){
				case "0":
					$filter_str = "ly_id = $val[1]";
					break;
				case "1":
					$limit_str = " limit $val[1]";
					break;
			}
			if($filter_str)
				$where_str === "" ? $where_str = "where $filter_str" : $where_str = $where_str . " and $filter_str";
			$filter_str = "";
		}
	}
	$my_sql="select ly_id,ly_user,ly_contact,ly_content,ly_reply,ly_time,ly_reply_time from hmcss_ly_main $where_str order by ly_id desc $limit_str";
	$hm_arr=get_row_from_sql("查留言",$my_sql);
	if(is_array($hm_arr))
		return $hm_arr;
	else
		return false;
}
//网站页脚显示
function hmcss_foot()
{
	//取网站备案信息
	$hm_arr = get_config("com_icp");
	if(!trim($hm_arr[0][2]))
		$icp_str = "";
	else
		$icp_str = "　｜　<a href=\"http://www.miibeian.gov.cn\" target=\"_blank\">" . $hm_arr[0][2] . "</a>";
	//取网站页脚信息
	if(!$hm_arr = get_config("foot"))
		$foot_str = "";
	else
		$foot_str = substr(trim($hm_arr[0][2]),3,-4);
	//取网站的统计信息
	if(!$hm_arr = get_config("com_count"))
		$count_str = "<br>网站统计";
	else
		$count_str = trim($hm_arr[0][2]);
	echo $foot_str,$icp_str,"　｜　<a href=\"admin\" target=\"_blank\">管理入口</a>",$count_str;
}

//网站的默认配置
function hmcss_default_config($df_type)
{
	$hm_seo = "河马工作室：网站建设,网站设计,WEB项目,B/S架构管理软件开发www.morewen.com";
	$hm_intro = "<p>河马工作室成立于2007年5月，专注于网站解决方案，熟练制作各类企业网站，可以开发基于B/S架构的各类企业管理软件，如企业ERP，OA(自动办公)，售后服务系统，CRM(客户关系管理)系统等。</p><p>大型B/S架构WEB系统定制开发，已掌握DISCUZ的核心代码，可熟练制作基于该平台的任意WEB系统</p><p>所有代码工作室自已编写，效率高，成本相对低，质量价格绝对保你满意</p><p>价格参考：一般企业网站：500元起，大型网站：1000元起，企业管理软件：3000元起</p>";
	
	$hm_news[0] = 0;
	$hm_news[1] = "HMCSS通用企业网站平台已全面完成";
	$hm_news[2] = "HMCSS企业通用网站系统简介<br><br>
HMCSS是由河马工作室全新开发的通用的企业网站系统，是PHP+MYSQL的架构，采用DIV+CSS的方式进行网页布局，网站的功能包括有：企业简介，图片展示幻灯，产品图片滚动，企业荣誉，实力展示，产品分类及展示，网上招聘，在线留言，联系我们等内容，另外还带有完整的管理后台，如网站SEO优化关键词等都可以自由设定。目前发布的是1.0版本，就是上述的这些内容。后面我们还要加上产品上传模块，就是企业的所有产品根据系统中固有的产品分类(系统中的分类有两种，一种是本网站自已用的，用户可以定义的分类，另一种是为了上传产品到我们建立的大平台上，由河马工作室内定的分类)，上传到我们后期将要制作的类似\"阿里巴巴\" 那样的集中式的产品宣传和销售平台上，进一步推广您的产品，也就是您现在将要使用的不单单是一套企业网站平台，也是一套集网站宣传，产品推广销售和展示为一体的管理系统。";
	$hm_news[4] = "2011-06-29";
	
	$hm_contact = "<p>
	<span style=\"font-size: 14px\">工作室联系方式：</span></p>
<p>
	<span style=\"font-size: 14px\">QQ：79796070(河马爱上网)</span></p>
<p>
	<span style=\"font-size: 14px\">手机：13913448665(张先生)</span></p>
<p>
	<span style=\"font-size: 14px\">网址：<a href=\"http://www.morewen.com/\">http://www.morewen.com/</a></span></p>
";
	$job_good = "公司实行以岗位为基础的绩效工资制度<br />
员工的工资和个人绩效、部门绩效相关<br />
公司的工资水平根据企业经济效益做出调整<br />
公司的薪酬战略行业领先化战略<br />
公司为员工提供住房<br />
公司为员工提供三餐补助<br />
公司为员工提供有效地培训服务<br />
员工每年享受带薪假期有婚假、产假、探亲假、丧假年休假等<br />
公司为每位员工缴纳规定的各项包括养老、失业、医疗等在内的社会保险<br />
公司不定期举行体育比赛<br />
公司每年组织春、秋游活动<br />
公司每年度结合年终总结会，组织全体员工联谊活动<br />
公司为员工提供各种娱乐场所及设备。<br />
公司每年为员工进行身体健康检查，并为每位员工建立一整套个人健康档案。<br />
出以上固定的福利外公司为每位员工提供个性化、弹性化的福利方案。";
	switch($df_type){
		case "1":
			return $hm_seo;
			break;
		case "2":
			return $hm_intro;
			break;
		case "3":
			return $hm_news;
			break;
		case "4":
			return $hm_contact;
			break;
		case "5":
			return $job_good;
			break;
	}
}
//配置前台公告显示
function hmcss_notice()
{
	$filter[0] = array("2",2,false);
	$filter[1] = array("1","0,5",false);
	if(!$hm_arr = get_news($filter)){
		$hm_arr[0] = hmcss_default_config(3);
	}
	foreach($hm_arr as $key => $val)
		echo "<a href=\"news.php?news_id=$val[0]\" target=\"_blank\" class=\"link1\">$val[1]</a>&nbsp;&nbsp;&nbsp;&nbsp;";
}

//学历类型
function get_user_xl($type_id = 0,$set_type = false)
{
	return hmcss_type(array("初中、高中","大学(大专)","本科","硕士及以上"),"学历","job_xl",$type_id,$set_type);
}

function hmcss_type($type_arr,$type_name,$hmcss_id,$type_id = 0,$set_type = false)
{
	if($type_id && !$set_type)
		return $type_arr[$type_id-1];
	else{
		$type_str = "<select name=\"$hmcss_id\" id=\"$hmcss_id\">\n";
		if($type_arr){
			foreach($type_arr as $key => $val){
				if($set_type && $type_id-1-$key == 0)
					$type_str .= " <option value=\"" . ($key+1) . "\" selected=\"selected\">$val</option>\n";
				else
					$type_str .= " <option value=\"" . ($key+1) . "\">$val</option>\n";
			}
		}
		else
			$type_str .= " <option value=\"0\">请配置" . $type_name . "类型</option>\n";
		$type_str .="</select>";
		return $type_str;
	}
}
?>