<?php
/********************************************************************
 *                         DirCMS会员模型数据输入验证类
 *                            ----------------------
 *   begin                : 2010-5-20
 *   copyright            : (C) 2009 DirCMS.cn
 *   email                : uuturn@126.com
 *
 *   $Id: member.input.class.php,v 1.0.0 2010-5-20 M Exp $
 ********************************************************************/

class member_input
{
	public $modelid;
	private $db;
	private $fields;

	function __construct($modelid)
	{
		global $db;
		$this->db=$db;
		$this->modelid=$modelid;
		$r=cache_read("cache_model_m".$this->modelid.".fields.cache.php",DIRCMS_ROOT.'data/cache_model/');
		if($r)foreach($r as $_r)
		{
			$t[$_r['name']]=$_r;
		}
		$this->fields=$t;
	}

	function content_input($modelid)
	{
		$this->__construct($modelid);
	}

	function check($data)
	{
		$r=$this->fields;
		foreach($data as $key => $value)
		{
			$data[$key]=($r[$key]['formtype']=='editor'|| $r[$key]['formtype']=='copyfrom')?$value:dircms_htmlspecialchars($value);
			$data[$key]=$r[$key]['formtype']=='checkbox'?implode('/',$data[$key]):$data[$key];
			if($r[$key]['formtype']=='images')
			{
				$data[$key]='';

				$old_images_file='old_images_file'.$key;
				$old_images_name='old_images_name'.$key;
				$old_images_file=$_POST[$old_images_file];
				$old_images_name=$_POST[$old_images_name];

				if($old_images_file && $old_images_name)
				{
					foreach($old_images_name as $k => $v)
					{
						if($v)
						{
							$data[$key].=str_replace('|','',$v).'|'.$old_images_file[$k]."\n";
						}
						else
						{
							@unlink(DIRCMS_ROOT.$old_images_file[$k]);
						}
					}
				}

				$images_name='images_name'.$key;
				$images_file='images_file'.$key;
				$images_name=$_POST[$images_name];
			//	$data[$key]='';
				@set_time_limit(0);
				require_once(DIRCMS_ROOT.'include/upload.class.php');
				$upload=new upload($images_file,'upload/image/'.date('Ymd'),'',array('jpg','jpeg','gif','png','bmp'));
				$imagearr=$upload->files;
				require_once(DIRCMS_ROOT.'include/image.class.php');
				$image=new image();

				if(WATERMARK_ENABLED==1) //加水印
				{
					foreach($imagearr as $val)
					{
						$image->watermark($val,WATERMARK_POS,WATERMARK_FILE,WATERMARK_PCT,WATERMARK_WORDS,WATERMARK_COLOR);
					}
				}

				if($imagearr && $images_name)
				{
					foreach($images_name as $_k =>$_v)
					{
						if($imagearr[$_k] && $_v)
						{	
							$data[$key].=str_replace('|','',$_v).'|'.$imagearr[$_k]."\n";
						}	
					}
				}
			}

			if($r[$key]['formtype']=='videourl')
			{
				$video_from='video_from_'.$key;
				$video_url='video_url_'.$key;
				$video_from=$_POST[$video_from];
				$video_url=$_POST[$video_url];
				$data[$key]='';
				if($video_from && $video_url)
				{
					foreach($video_url as $_k =>$_v)
					{
						if($video_from[$_k] && $_v)
						{	
							$data[$key].=str_replace('|','',$video_from[$_k]).'|'.$_v."\n";
						}	
					}
				}
			}
			if(!isset($r[$key]['name']))continue;
			if($r[$key]['formtype']=='editor')continue;
			if($r[$key]['preg']=='limit')
			{
				$len=strlen($value);
				$mixlength=$r[$key]['minsize'];
				$maxlength=$r[$key]['maxsize'];
				if($len>$maxlength)showmsg($r[$key]['name'].'不能超过'.$maxlength.'个字符!');
				if($len<$mixlength)showmsg($r[$key]['name'].'不能少于'.$mixlength.'个字符!');
			}
			elseif($r[$key]['preg']=='email' && !is_email($value) &&$value)
			{
				showmsg('请输入正确的邮箱!');
			}
			elseif($r[$key]['preg']=='url' && !is_url($value) &&$value)
			{
				showmsg('请输入正确的链接地址!');
			}
			elseif($r[$key]['preg']=='phone' && !is_phone_fax($value) &&$value)
			{
				showmsg('数据格式不正确!');
			}
			elseif($r[$key]['preg']=='number' && !preg_match('/^[0-9\.]+$/',$value) &&$value)
			{
				showmsg($r[$key]['name'].'只能是0-9的数字以及小数点!');
			}
			elseif(strtolower($r[$key]['preg'])=='username' && !is_username($value))
			{
				showmsg('用户名由1-30位中文，数字，字母，下划线组成!');
			}
			elseif(strtolower($r[$key]['preg'])=='usepsw' && !is_pwd($value))
			{
				showmsg('用户密码由3-30位数字，字母，下划线组成!');
			}
			elseif(strtolower($r[$key]['preg'])=='integer' && !preg_match('/^[0-9]+$/',$value) &&$value)
			{
				showmsg($r[$key]['name'].'只能是0-9的整数!');
			}
			elseif(strtolower($r[$key]['preg'])=='english' && !preg_match('/^[a-z0-9_]+$/i',$value) &&$value)
			{
				showmsg($r[$key]['name'].'只能由数字，字母，下划线组成!');
			}
			elseif(strtolower($r[$key]['preg'])=='qq' && !preg_match('/^[0-9]{3,15}$/i',$value) &&$value)
			{
				showmsg('QQ号码只能由3-15位的数字组成!');
			}
			elseif(strtolower($r[$key]['preg'])=='mobile' && !preg_match('/^[01][0-9_]{10,14}$/i',$value) &&$value)
			{
				showmsg('手机号码格式不正确!');
			}
			elseif(strtolower($r[$key]['preg'])=='ip' && !preg_match('/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/i',$value) &&$value)
			{
				showmsg('IP地址格式不正确!');
			}
			elseif(strtolower($r[$key]['preg'])=='chinese' && !preg_match('/^[\x80-\xff_a-zA-Z0-9]+$/i',$value) &&$value)
			{
				showmsg($r[$key]['name'].'只能由数字，字母，中文，下划线组成!');
			}
		}
		return $data;
	}
}
?>