<?php

define('IN_PHPMPS', true);
require dirname(__FILE__) . '/include/common.php';
require dirname(__FILE__) . '/include/com.fun.php';
require PHPMPS_ROOT . 'include/json.class.php';

ini_set('display_errors', 'on');  
$PNG_TEMP_DIR = dirname(__FILE__).'/phpqrcode/'.DIRECTORY_SEPARATOR.'temp'.DIRECTORY_SEPARATOR;  
$PNG_WEB_DIR = 'temp/';  
require dirname(__FILE__) . '/phpqrcode/qrlib.php';    // QRcode lib  

$_REQUEST['act'] = $_REQUEST['act'] ? trim($_REQUEST['act']) : 'select' ;

if(!$_userid) {
	showmsg('你还没有登录', 'member.php?act=login&refer='.$PHP_URL);
} else {
	$member = member_info($_userid);
	if($member['status']!=1) showmsg('您尚未通过审核或被锁定！');
}
   $sql = "SELECT COUNT(*) FROM {$table}com WHERE userid='$_userid'";
   $count = $db->getOne($sql);
   if($count>0)showmsg('你已添加过店铺，为你转向店铺页','member.php?act=com');
$ip = get_ip();

if($_REQUEST['act'] == 'select')
{
	$seo['title'] = '选择商家分类';
	$seo['keywords'] = $CFG['keywords'];
	$seo['description'] = $CFG['description'];

	$here = array('name'=>'选择商家分类','url'=>'postcom.php');
	$cats = get_com_cat_list();
	include template('com_select','com');
}
elseif($_REQUEST['act'] == 'post')
{
	$mappoint = $CFG['map'] ? explode(',', $CFG['map']) : '';
	$catid = intval($_REQUEST['id']);
	if(empty($catid)) {
		showmsg('没有选择分类');
	}
	$com_catinfo = get_com_cat_info($catid);
	if(empty($com_catinfo))showmsg('不存在此商家分类');

	$seo['title'] = '发布商家信息';
	$seo['keywords'] = $CFG['keywords'];
	$seo['description'] = $CFG['description'];

	include template('com_post','com');
}
elseif($_REQUEST['act'] == 'postok')
{
	$catid     = $_POST['catid'] ? intval($_POST['catid']) : '';
	$comname   = $_POST['comname'] ? htmlspecialchars(trim($_POST['comname'])) : '';
	$areaid    = $_POST['areaid'] ? intval($_POST['areaid']) : '';
	$postdate  = time();
	$introduce = $_POST['content'] ? htmlspecialchars(trim($_POST['content'])) : '';;
	$hours     = htmlspecialchars(trim($_POST['starhour'])).':00,'.htmlspecialchars(trim($_POST['endhour'])).':00';
	$phone     = $_POST['phone'] ? htmlspecialchars($_POST['phone']) : '';
	$address   = $_POST['address'] ? htmlspecialchars($_POST['address']) : '';
	$mappoint  = $_POST['mappoint'] ? htmlspecialchars($_POST['mappoint']) : '';
	$is_check  = $CFG['post_check'] == '1' ?  '0' : '1';

    if(empty($comname))showmsg("店铺名称不能为空");
    if(empty($phone))showmsg("联系电话必须填写");
	if(!preg_match("/^1[34578]\d{9}$|^(0\d{2,4}-)?[2-9]\d{6,7}(-\d{2,5})?$|^[48]00(-?\d){7,10}$/",$phone))showmsg("联系电话格式错误");
	if($areaid<=0) showmsg('请选择地区');
	if(strlen($content)>500)showmsg('店铺介绍太长了！');
	check_words(array($comname, $introduce));
	
    $sql = "insert into {$table}com (userid,catid,areaid,comname,phone,postdate,mappoint,address,hours,is_check,introduce,jf_num,sms_num,keycode) 
	values ('$_userid','$catid','$areaid','$comname','$phone','$postdate','$mappoint','$address','$hours','$is_check','$introduce','".$CFG['com_jf_num']."','".$CFG['com_sms_num']."','$keycode')";
    $res = $db->query($sql);
	$id = $db->insert_id();
	
	$datavalue = $CFG['weburl'].'/url/m.php?id='.$id; // data  
    $ecc = 'H'; // L-smallest, M, Q, H-best  
    $size = 4; // 1-50 
	
	$filename = $PNG_TEMP_DIR.$id.'.png';  
    QRcode::png($datavalue, $filename, $ecc, $size, 2);  
    chmod($filename, 0777);
	$sql = "insert into {$table}com_config (comid,userid) values ('$id','$_userid')";
    $res = $db->query($sql);
	
	$alled = array('png','jpg','gif','jpeg');
	$exname = strtolower(trim(substr(strrchr($_FILES['thumb']['name'], '.'), 1)));
	if(checkupfile($_FILES['thumb']['tmp_name']) && $_FILES['thumb']['tmp_name'] != 'none' && $_FILES['thumb']['tmp_name'] && $_FILES['thumb']['name'] && $_FILES['thumb']['size']<'523298' && in_array($exname,$alled)) {

		$thumb_name = $id.'_thumb'. '.' . end(explode('.', $_FILES['thumb']['name']));
		$dir = PHPMPS_ROOT . 'data/com/thumb';
		if(!is_dir($dir)) {
			if(@mkdir(rtrim($dir,'/'), 0777))@chmod($dir, 0777);
		}
		$to = $dir.'/'. $thumb_name;
		CreateSmallImage($_FILES['thumb']['tmp_name'], $to, $CFG['com_thumbwidth'], $CFG['com_thumbheight']);
		$image = 'data/com/thumb/'. $thumb_name;
		$db->query("update {$table}com set thumb='$image' where comid='$id' ");
	}
	$count = count($_FILES)-1;
	for($i=1;$i<=$count;$i++)
	{
		$exname = strtolower(trim(substr(strrchr($_FILES['file'. $i]['name'], '.'), 1)));
		if(!checkupfile($_FILES['file'. $i]['tmp_name']) || !($_FILES['file'. $i]['tmp_name'] != 'none' && $_FILES['file'. $i]['tmp_name'] && $_FILES['file'. $i]['name']) || $_FILES['file'. $i]['size']>'523298' || !in_array($exname,$alled)) {
			continue;
		}
		$name = date('ymdhis');
		for($a = 0;$a < 6;$a++){ $name .= chr(mt_rand(97, 122));}
		$name .= '.' . end(explode('.', $_FILES['file'. $i]['name']));
		$dir = PHPMPS_ROOT . 'data/com/' . date('ymd');
		if(!is_dir($dir)) {
			if(@mkdir(rtrim($dir,'/'), 0777))@chmod($dir, 0777);
		}
		$to = $dir.'/'. $name;
		if(move_uploaded_file($_FILES['file'. $i]['tmp_name'], $to)) {
			$image = 'data/com/' . date('ymd').'/'. $name;
			$db->query("INSERT INTO {$table}com_image (comid,path) VALUES ('$id','$image')");
		}
	}
	//$url = url_rewrite('com',array('act'=>'view', 'comid'=>$id));
	showmsg("恭喜您，店铺信息已经提交成功，我们会在24小时内审核您的店铺信息！", "member.php?act=com_index");
}
?>