<?php
class datainput
{
	private $db;
	private $modelobj;
	private $field;
	private $attchtable;

	function __construct()
	{
		global $db,$modelobj;
		$this->db=$db;
		$this->modelobj=$modelobj;
		$this->attchtable=LA_PRE.'attchment';
		$this->fields=array();
	}

	function setmodel($modelid=array())
	{
		$fields=array();

		if($modelid)
		{
			foreach($modelid as $m)
			{
				if(intval($m))
				{
					$fields=array_merge($fields,$this->modelobj->getmodelfields(intval($m)));
				}
			}
		}

		if($fields)
		{
			foreach($fields as $key => $val)
			{
				$this->fields[$val['enname']]=$val;
			}
		}
	}

	function filter($data)
	{
		global $_roleid,$_gradeid,$sitelang,$_userid,$LA,$MEM;

		$fields=$this->fields;

		if(!$data)
		{
			return $data;
		}

		foreach($data as $key => $value)
		{
			if(!isset($fields[$key]))
			{
				continue;
			}

			
			
	
			if($_gradeid!=-1 && !$_roleid && !in_array($_gradeid,explode(',',$fields[$key]['membergrade'])))
			{
				showjsmsg($sitelang['err-field-accss']);
			}

	
			if($fields[$key]['form']=='fckeditor' || $fields[$key]['form']=='content')
			{
				$data[$key]=la_stripslashes(la_batsafehtml($value,4));
			}
			else
			{
				$data[$key]=la_htmlspecialchars($value);
			}
	
			
			if($fields[$key]['form']=='datetime')
			{
				$data[$key]=strtotime($value);
			}

			if($fields[$key]['form']=='expire')
			{
				$data[$key]=intval($value);
			}
	
			if($fields[$key]['form']=='password')
			{
				if($value)
				{
					$data[$key]=md5($value);
				}
				else
				{
					unset($data[$key]);
				}
			}

			if($fields[$key]['form']=='number')
			{
				$data[$key]=floatval($value);
			}

			if($fields[$key]['form']=='checkbox')
			{
				$data[$key]=implode(',', $value);
			}


			if($fields[$key]['notnull'] && !$value && $fields[$key]['form']!='password')
			{
					showjsmsg($fields[$key]['name'].'输入不能为空！');
			}

			
			if($fields[$key]['regex']=='limit')
			{
				if(strlen($value)>=$fields[$key]['length'])
				{
					showjsmsg($fields[$key]['name'].'的长度不要超过'.$fields[$key]['length'].'个字符');
				}
			}
			
			if($fields[$key]['regex']=='limit3_50')
			{
				if(strlen($value)>50)
				{
					showjsmsg($fields[$key]['name'].$sitelang['err-max-50']);
				}

				if(strlen($value)<3)
				{
					showjsmsg($fields[$key]['name'].$sitelang['err-min-3']);
				}
			}
			
			if($fields[$key]['regex']=='userName')
			{
				if(!preg_match('/^[\x80-\xff_a-zA-Z0-9]{1,64}$/i',$value))
				{
					showjsmsg($sitelang['err-username']);
				}
			}

			if($fields[$key]['regex']=='usePsw')
			{
				if(preg_match('/[\x80-\xff]/i',$value))
				{
					showjsmsg($sitelang['err-pwd']);
				}
			}
			
			if($fields[$key]['regex']=='number')
			{
				if(!is_numeric($value) && $value)
				{
					showjsmsg($fields[$key]['name'].$sitelang['err-float-0-9']);
				}
			}

			if($fields[$key]['regex']=='integer')
			{
				if(!preg_match('/^[0-9]+$/',$value) && $value)
				{
					showjsmsg($fields[$key]['name'].$sitelang['err-integer-0-9']);
				}
			}

			if($fields[$key]['regex']=='english')
			{
				if(!preg_match('/^[a-z0-9_]+$/',$value) && $value)
				{
					showjsmsg($fields[$key]['name'].$sitelang['err-a0_']);
				}
			}

			if($fields[$key]['regex']=='chinese')
			{
				if(!preg_match('/[\x80-\xff]+/i',$value) && $value)
				{
					showjsmsg($fields[$key]['name'].$sitelang['err-chinese']);
				}
			}

			if($fields[$key]['regex']=='email')
			{
				if(!preg_match('/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/i',$value) && $value)
				{
					showjsmsg($sitelang['err-email']);
				}
			}

			if($fields[$key]['regex']=='qq')
			{
				if(!preg_match('/^[0-9]{3,15}$/i',$value) && $value)
				{
					showjsmsg($sitelang['err-qq']);
				}
			}

			if($fields[$key]['regex']=='url')
			{
				if(!preg_match('/^http:\/\/[A-Za-z0-9]+\.?[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/i',$value) && $value)
				{
					showjsmsg($fields[$key]['name'].$sitelang['err-url']);
				}
			}

			if($fields[$key]['regex']=='mobile')
			{
				if(!preg_match('/^[0-9\-\(\)\.]+$/i',$value) && !preg_match('/^[01][0-9_]{10,14}$/i',$value) && $value)
				{
					showjsmsg($sitelang['err-mobile']);
				}
			}
			
			if($fields[$key]['regex']=='phone')
			{
				if(!preg_match('/^[0-9\-\(\)\.]+$/i',$value) && !preg_match('/^[01][0-9_]{10,14}$/i',$value) && $value)
				{
					showjsmsg($sitelang['err-phone']);
				}
			}

			if($fields[$key]['regex']=='ip')
			{
				if(!preg_match('/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/i',$value) && $value)
				{
					showjsmsg($sitelang['err-ip']);
				}
			}

			$attch=$attch1=$attch2=array();
			$attch1=get_images(strip_tags($value,'<img>'));
			$attch2=get_attchments(strip_tags($value,'<a>'));
			$attch=array_unique(array_merge($attch1,$attch2));

			if($attch)
			{
				foreach($attch as $fn)
				{
					if(is_image($fn) && !is_image($data['thumb']))
					{
						$data['thumb']=$fn;
					}

					$_attch=array();
					$_attch['filepath']=substr($fn,strlen($LA['siteurl']));
					if((is_image($fn) || is_attchment($fn)) && is_file(SYSTEM_ROOT.$_attch['filepath']))
					{
						$_attch['modelid']=intval($data['modelid']);
						$_attch['filename']=get_basename($fn);
						$_attch['userid']=intval($_userid);
						$_attch['datetime']=TIME;
						$_attch['siteid']=$_SESSION['siteid'];
						$this->db->insert($this->attchtable,$_attch,true);
					}
				}
			}

			if($fields[$key]['form']=='image')
			{
				$im=imageupload($fields[$key]['enname'].'_file');
				$data[$key]=is_image($im)?$im:(is_image($value)?$value:'');
				
				if(!is_image($data['thumb']))
				{
					$data['thumb']=$data[$key];
				}

			
				$_attch=array();
				if(is_file(SYSTEM_ROOT.$data[$key]))
				{
					$_attch['filepath']=$data[$key];
					$_attch['modelid']=intval($data['modelid']);
					$_attch['filename']=get_basename($data[$key]);
					$_attch['userid']=intval($_userid);
					$_attch['datetime']=TIME;
					$_attch['siteid']=$_SESSION['siteid'];
					$this->db->insert($this->attchtable,$_attch,true);
				}
			}

			else if($fields[$key]['form']=='images' && isset($_POST['urlimgs_'.$fields[$key]['enname']]))
			{
				$urlimgs=$_POST['urlimgs_'.$fields[$key]['enname']];
				$deleteimgs=$_POST['deleteimgs_'.$fields[$key]['enname']];
				$nameimgs=$_POST['nameimgs_'.$fields[$key]['enname']];
				$t=array();
					
				$maxlimit=$_roleid?127:$MEM['gradeinfo']['images'];

				if($urlimgs)
				{
					foreach($urlimgs as $keyimgs => $url)
					{
						$url=substr($url,0,14)=='/upload/image/'?$url:'/upload/image/'.$url;
						if(!empty($url) &&  is_image($url))
						{
							if(sizeof($urlimgs)<=$maxlimit)
							{
								if(!isset($deleteimgs[$keyimgs]) || $deleteimgs[$keyimgs])
								{
									@unlink(SYSTEM_ROOT.$url);
								}
								else
								{
									$t[]=str_replace(array(','),'',$url.'`'.$nameimgs[$keyimgs]);
								}
								
								if(!is_image($data['thumb']))
								{
									$data['thumb']=$url;
								}

								$_attch=array();
								if(is_file(SYSTEM_ROOT.$url))
								{
									$_attch['filepath']=$url;
									$_attch['modelid']=intval($data['modelid']);
									$_attch['filename']=$nameimgs[$keyimgs];
									$_attch['userid']=intval($_userid);
									$_attch['datetime']=TIME;
									$_attch['siteid']=$_SESSION['siteid'];
									$this->db->insert($this->attchtable,$_attch,true);
								}
							}
							else
							{
								@unlink(SYSTEM_ROOT.$url);
							}
						}
					}

					if(sizeof($urlimgs)>$maxlimit)
					{
						showjsmsg('您上传的图片数量超过限制！');
					}
					$data[$key]=implode(',',$t);
				}
				continue;
			}
			
		
			else if($fields[$key]['form']=='attachment')
			{
				$att=attachmentupload($fields[$key]['enname'].'_file');
				$data[$key]=trim($att)?$att:(trim($value)?$value:'');

				$atts=explode(',',$data[$key]);
				foreach($atts as $att)
				{
					$att=explode('`',$att);
					$_attch=array();
					if(is_file(SYSTEM_ROOT.$att[0]))
					{
						$_attch['filepath']=$att[0];
						$_attch['modelid']=intval($data['modelid']);
						$_attch['filename']=$att[1];
						$_attch['userid']=intval($_userid);
						$_attch['datetime']=TIME;
						$_attch['siteid']=$_SESSION['siteid'];
						$this->db->insert($this->attchtable,$_attch,true);
					}
				}
			}
		
			else if($fields[$key]['form']=='video')
			{
				$video=videoupload($fields[$key]['enname'].'_file');
				$data[$key]=trim($video)?$video:(trim($value)?$value:'');

		
				$_attch=array();
				if(is_file(SYSTEM_ROOT.$data[$key]))
				{
					$_attch['filepath']=$data[$key];
					$_attch['modelid']=intval($data['modelid']);
					$_attch['filename']=get_basename($data[$key]);
					$_attch['userid']=intval($_userid);
					$_attch['datetime']=TIME;
					$_attch['siteid']=$_SESSION['siteid'];
					$this->db->insert($this->attchtable,$_attch,true);
				}
			}
		}

		if(is_image($data['thumb']))
		{
			$isdel=false;
			$fn=strtolower($data['thumb']);
			if(substr($fn,0,7)=='http://')
			{
				$file=@file_get_contents($fn,true);
				if($file)
				{
					$fn=SYSTEM_ROOT.'upload/image/'.date('Y-m-d',TIME).'/'.substr(md5($fn),8,16).'.'.get_fileext($fn);;
					$fp=@fopen($fn,'a');
					@fwrite($fp,$file);
					@fclose($fp);
					$isdel=true;
				}
			}
			else
			{
				$fn=SYSTEM_ROOT.$fn;
			}

			$fninfo=getimagesize($fn);
			if($fninfo[0]<120 && $fninfo[1]<120)
			{
				unset($data['thumb']);
				if($isdel==true)
				{
					unlink($fn);
				}
			}
		}
		return $data;
	}
}