<?php
defined('PHP168_PATH') or die();

/**
* 添加模型内容入口文件
**/

$this_controller->check_action($ACTION) or message('no_privilege');
$mid = isset($_REQUEST['mid'])? intval($_REQUEST['mid']) : '';
if($mid){
	
	$this_controller->check_model_action($ACTION, $mid) or message('no_model_privilege');
	
	$this_module->set_model($mid) or message('no_such_model');
	if(!$this_model['enabled']){
		message('this_model_unable');
	}
}else{
	$my_addible_forms = $this_controller->get_acl('my_forms_post');
		$models = $this_module->core->CACHE->read('core/modules', 'forms', 'models');
		if(!isset($my_addible_forms[0]) && !$IS_FOUNDER){
			foreach($models as $mname => $mdata){
				if(!array_key_exists($mdata['id'],$my_addible_forms))
				unset($models[$mname]);
			}
		}
		$SEO_KEYWORDS = $SEO_DESCRIPTION = '';
		$TITLE = 'chose';	
		include template($this_module, 'selectforms');
	
	exit;
}
if(REQUEST_METHOD == 'GET' || defined('P8_GENERATE_HTML')){
	$SEO_KEYWORDS = $SEO_DESCRIPTION = '';
	$TITLE = $this_model['alias'];	
	//$this_module->CONFIG['template']='media';
	
	//模型自定义脚本
	include $this_model['path'] .'post.php';
	
	$template = empty($this_model['post_template']) ?
		'edit' :
		'tpl/'.$this_model['post_template'];
	$attachment_hash = unique_id(16);
	if($core->ismobile){
		$template = empty($this_model['post_template_mobile']) ?
		'edit' :
		'tpl/'.$this_model['post_template_mobile'];
	}
	
	include template($this_module, $template);

}else if(REQUEST_METHOD == 'POST'){
	
	//如果魔法引号开启strip掉
	$_POST = p8_stripslashes2($_POST);
	
	
	$_POST['status'] = $this_controller->check_model_action('authmanage',$mid)? 9 : 0;
	if($this_model['CONFIG']['captcha'] && !captcha($_POST['captcha']))message('captcha_incorrect', HTTP_REFERER, 10);
	//模型自定义脚本
	include $this_model['path'] .'post.php';
	
	$id = $this_controller->add($_POST) or message('fail');
	//email
	
	if($id && $this_model['CONFIG']['email_enabled'] && $this_model['CONFIG']['email']){
		$email = &$core->load_module('mail');
		$sendto = explode(',', $this_model['CONFIG']['email']);
		
		$SEO_KEYWORDS = $SEO_DESCRIPTION = $TITLE = '';	
		
		$data = $this_module->get_data($id,$this_model['name']);
		$data or message('no_such_item');
		$this_module->format_data($data);
		$this_module->format_view($data);
		$status = $this_module->CONFIG['status'];
		//模型自定义脚本
		include $this_model['path'] .'view.php';		
		$template = empty($this_model['view_template'])? 'view' : 'tpl/'.$this_model['view_template'];
		$core->get_cache('role_group');
		$member = $core->load_module('member');
		$member->set_model($ROLE_GROUP);
		$member_info = $member->get_member_info($UID);	
		$status_json = p8_json($this_module->CONFIG['status']);
		ob_start();
		include template($this_module, $template);
		$content = ob_get_clean();
		//取CSS
		$preg = '/href=[\"|\']?(.*?)[\"|\']?\s.*?>/i';//匹配img标签的正则表达式
		preg_match_all($preg, $content, $allImg);//这里匹配所有的img
		$cssstring = '<style type="text/css">';
		foreach($allImg[1] as $css){
			if(strpos($css,'.css') || strpos($css,'.CSS')){
				$cssstring .= file_get_contents($css);//将整个文件内容读入到一个字符串中
			}
		}		
		$cssstring .= '</style>';
		$cssstring = str_replace(array("\r\n", "\r", "\n"), "", $cssstring);		
		//取主体
		if(stristr($content,"<body>",0)){
			$content = substr(stristr($content,"<body>",0),6);
		}else{
			$len = strlen('<body style="background:#fff;background-image:none;">');
			$content = substr(stristr($content,'<body style="background:#fff;background-image:none;">',0),$len);
		}
		if($content) $content = stristr($content,"</body>",1);
		//view
		$viewlink = $this_module->controller .'-view-id-'.$id;
		$viewlinks = '<h1>查看详情：<a href="'.$viewlink.'" target="_blank">'.$viewlink.'</a><h1><br>';
		$content = $cssstring.$viewlinks.$content;		
		//发送邮件		
		foreach($sendto as $send){
			$email->set(array(
					'subject' => $this_model['alias'].'有新信息（'.date('Y-m-d H:i:s',P8_TIME).'）',
					'message' => $content,
					'send_to' => $send
				));
			$status3 = $email->send();
		}		
	}
	if($_POST['status']<1){
		message('verifing', $this_url.'?mid='.$mid);
	}else{
		message(
			array(
				array('forms_to_edit', $this_module->controller .'-update?id='.$id.'&mid='.$mid),
				array('forms_to_list', $this_module->controller .'-list-mid-'.$mid),
				array('forms_to_view', $this_module->controller .'-view-id-'.$id),
				array('forms_to_add', $this_url.'?mid='.$mid)
			),
			$this_module->controller .'-post?cid='.$_POST['cid'].'&mid='.$mid,
			10000
		);
	}	
}
