<?php
class Muser extends Model
{
	var $user = false;

	var $nincs_elozo = array('settings/lang','lol/próba','settings/langChange');

	var $tomb;



	function Muser()
	{
		parent::Model();

		$user = $this->phpsession->get('user');
		/*if( $_SERVER['REMOTE_ADDR']=="188.36.216.113" )
			print_r($user);*/
		//print "uri2: ".$this->uri->segment(2);
		$langkivetelek  = array("admin","collection2","collection2012","showroom");
		$langkivetelek2 = array("upload");
		$segment1 = $this->uri->segment(1);
		$segment2 = $this->uri->segment(2);
		// ha van uri     vagy    esp..
		if( $this->uri->lang != "" ||
			!$user ||
			$segment1=='view' ||
			( $segment1=='mobile' && $segment2=='login' ) ||
			( $user['lang']=="esp" && $segment1!='admin' ) ||
			( $user['lang']=="ita" && (!in_array($segment1,$langkivetelek) || in_array($segment2,$langkivetelek2)) )  ||
			( $user['lang']=="ger" && (!in_array($segment1,$langkivetelek) || in_array($segment2,$langkivetelek2)) )
		)
		{
			$this->user = $this->setuser();

			// spanyol admin volt-e
			if( $user['admin']===true && ($user['lang']=="esp" || $user['lang']=="ita" || $user['lang']=="ger" ) ){
				$this->user['admin'] = true;
				$this->user['admin_lang'] = $user['lang'];
				$this->phpsession->save('user',$user);
			}
		}
		else
		{
			$this->user = $user;
		}

		$this->elozo = $this->phpsession->get('elozo');

		$this->set_elozo();

		$this->lang->load('site', $this->user['lang']);

		$this->tomb['á']='&aacute;';
		$this->tomb['Á']='&Aacute;';
		$this->tomb['é']='&eacute;';
		$this->tomb['É']='&Eacute;';
		$this->tomb['ó']='&oacute;';
		$this->tomb['Ó']='&Oacute;';
		$this->tomb['ö']='&ouml;';
		$this->tomb['Ö']='&Ouml;';
		$this->tomb['ő']='&#337;';
		$this->tomb['Ő']='&#336;';
		$this->tomb['ú']='&uacute;';
		$this->tomb['Ú']='&Uacute;';
		$this->tomb['ü']='&uuml;';
		$this->tomb['Ü']='&Uuml;';
		$this->tomb['ű']='&#369;';
		$this->tomb['Ű']='&#368;';
		$this->tomb['í']='&iacute;';
		$this->tomb['Í']='&Iacute;';


		// Spanyol url útvonal
		if( $this->user['lang'] == "esp" )
			define('PREURL', 'es/');
		elseif( $this->user['lang'] == "ita" )
			define('PREURL', 'it/');
		elseif( $this->user['lang'] == "ger" )
			define('PREURL', 'de/');
		else
			define('PREURL', '');

		return;

		// ha mobil, es nem volt a mobil oldalon akkor oda iranyitani
		if(
			$this->user['mobil']===true
			&& (!isset($this->user['voltmobiloldal']) || $this->user['voltmobiloldal'] !== true)
			/*&& $this->uri->segment(1) != "mobile"
			&& $this->uri->segment(1) != "view"
			&& $this->uri->segment(1) != "events"*/
			&& !in_array($this->uri->segment(1),array("mobile","view","events"))
			&& !in_array($this->uri->segment(2),array("show2"))
		)
			redirect('mobile');

	}

	function string_to_code($string)
	{
		foreach($this->tomb as $k => $v)
		{
			$string = str_replace($k,$v,$string);
		}

		return $string;

	}

	function get($name)
	{
		if(!isset($this->user[$name]))
		{
			return false;
		}
		else
		{
			return $this->user[$name];
		}
	}

	function setuser()
	{

		$angoldomainek = array(
			'archlinexp.com',
			'www.archlinexp.com',
			'archxp.com',
			'www.archxp.com',
			'archlinexp.co.uk',
			'www.archlinexp.co.uk',
			'archlinexp.eu',
			'www.archlinexp.eu'
		);

		if(!isset($_SERVER['HTTP_ACCEPT_LANGUAGE']))
		{
			$accept = false;
		}
		else
		{
			$accept = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
		}

		/*Bővítésem kezdete*/
		/*if($_SERVER['HTTP_POST']=='archlinexp.com' || $_SERVER['HTTP_POST']=='www.archlinexp.com')
		{
		    $lang='eng';
		}
		elseif($_SERVER['HTTP_POST']=='archline.hu' || $_SERVER['HTTP_POST']=='www.archline.hu')
		{
		    $lang='hun';
		}
		elseif($_SERVER['HTTP_POST']=='archline.esp' || $_SERVER['HTTP_POST']=='www.archline.esp')
		{
		    $lang='esp';
		}
		elseif()$_SERVER['HTTP_POST']=='archline.jp' || $_SERVER['HTTP_POST']=='www.archline.jp'
		{
		    $lang='jap';
		}*/


		if(  $this->uri->lang == "es" ){
			$lang = 'esp';
		}
		elseif(  $this->uri->lang == "it" ){
			$lang = 'ita';
		}
		elseif(  $this->uri->lang == "de" ){
			$lang = 'ger';
		}
		elseif($_SERVER['HTTP_HOST']=='archline.jp' || $_SERVER['HTTP_HOST']=='www.archline.jp')
		{
			$lang = 'jap';
		}
		elseif( $this->uri->lang == "hu" || $this->uri->lang != "en" && ($_SERVER['HTTP_HOST']=='cadline.hu' || ($_SERVER['HTTP_HOST']=='www.cadline.hu' && $this->uri->segment(1)!="crash") ||  $_SERVER['HTTP_HOST']=='archline.hu' || $_SERVER['HTTP_HOST']=='www.archline.hu' || $this->uri->lang == "hu") && $this->uri->segment(1)!="view" && !($this->uri->segment(1)=='mobile' && $this->uri->segment(2)=='login') )
		{
			$lang = 'hun';
		}
		elseif( in_array($_SERVER['HTTP_HOST'],$angoldomainek) || $this->uri->lang == "en" || $this->uri->segment(1)=="view" || ($this->uri->segment(1)=='mobile' && $this->uri->segment(2)=='login') || ( $_SERVER['HTTP_HOST']=='www.cadline.hu' && $this->uri->segment(1)=="crash" ) || ( $_SERVER['HTTP_HOST']=='www.archline.hu' && $this->uri->segment(1)=="crash" ))
		{
		    $lang='eng';
		}/*Kibővítettem a sort ezzel*/

		else
		{
			if(!$accept)
			{
				if( $this->uri->segments[0] != "en" && ($_SERVER['HTTP_HOST']=='cadline.hu' || $_SERVER['HTTP_HOST']=='www.cadline.hu' ||  $_SERVER['HTTP_HOST']=='archline.hu' || $_SERVER['HTTP_HOST']=='www.archline.hu' || $this->uri->segments[0] == "hu") )
				{
					$lang = 'hun';
				}
				else if($_SERVER['HTTP_HOST']=='archline.jp' ||$_SERVER['HTTP_HOST']=='www.archline.jp')
				{
					$lang = 'jap';
				}
				else
				{
					$lang = 'eng';
				}
			}
			else
			{
				if(substr($accept,0,2)=='hu')
				{
					$lang = 'hun';
				}
			/*	else if(substr($accept,0,2)=='jp')
				{
					$lang = 'jap';
				}
				else if(substr($accept,0,2)=='gr')
				{
					$lang = 'gr';
				}
				else if(substr($accept,0,2)=='cr')
				{
					$lang = 'cr';
				}*/
				else
				{
					$lang = 'eng';
				}
			}
		}
		if( $this->uri->segment(1)=="crash" ){
			$lang = 'eng';
		}
		// mobil vagy nem
	/*	$useragent=$_SERVER['HTTP_USER_AGENT'];
		if(preg_match('/android.+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|meego.+mobile|midp|mmp|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows (ce|phone)|xda|xiino/i',$useragent)||preg_match('/1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(di|rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas\-|your|zeto|zte\-/i',substr($useragent,0,4)))*/

		$useragent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : "xxxxxxxxxxxxxxxxxxxxxxxx";
		// archline agent: ArchLINE.XP 2006
		if(
		!stristr($useragent,"ArchLINE.XP")
		&& (preg_match('/android|avantgo|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|symbian|treo|up\.(browser|link)|vodafone|wap|windows (ce|phone)|xda|xiino/i',$useragent)
		|| preg_match('/1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|e\-|e\/|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(di|rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|xda(\-|2|g)|yas\-|your|zeto|zte\-/i',substr($useragent,0,4)))
		)
			$mobil = true;
		else
			$mobil = false;


		$user['lang']=$lang;
		$user['mobil']=$mobil;
		$this->phpsession->save('user',$user);
		return $user;
	}

	function generate_csik($tomb)
	{
		$config = $this->config->item('csik');

		if(!$tomb || !is_array($tomb))
		{
			return false;
		}
		else
		{
			$return ='';

			foreach($tomb as $elem)
			{
				if(!isset($config[$elem]))
				{
					$return.=anchor($this->uri->uri_string(),$elem);
				}
				else
				{
					// teszt a servicesnél
					if( $this->uri->segment(1) == "services" ){
						$return.="<div itemscope=\"itemscope\" itemtype=\"http://data-vocabulary.org/Breadcrumb\" style=\"float: left; width: auto;\">";
						$return.="<a href=\"/".$config[$elem]['uri']."\" itemprop=\"url\">";
						$return.="<span itemprop=\"title\">".$config[$elem][$this->user['lang']]."</span>";
						$return.="</a>";
						$return.="</div>";
					}
					else
						$return.=anchor($config[$elem]['uri'],$config[$elem][$this->user['lang']]);
				}

			}
			// teszt a servicesnél
			if( $this->uri->segment(1) == "services" )
				$return .= "<div class=\"clear\"></div>";
		}

		return $return;

	}


	function get_setting($name,$lang=false)
	{
		if(!$lang)
		{
			$value = $this->db->select('value')->where('name',$name)->get('settings');
		}
		else
		{
			$value = $this->db->select('value')->where('name',$name)->where('lang',$lang)->get('settings');
		}

		if($value->num_rows()==0)
		{
			return null;
		}
		else
		{
			return $value->row()->value;
		}
	}


	function set_setting($name,$value,$lang=false)
	{
		if(!$lang)
		{
			$query = "update settings set value='$value' where name='$name'";
		}
		else
		{
			$query = "update settings set value='$value' where name='$name' and lang='$lang'";
		}

		$this->db->query($query);

	return true;

	}

	function get_welcome_img(){
		if(hun())
			$lang = "hun";
		else
			$lang = "eng";

		$ered = $this->db->where('nyelv',$lang)->where('sorszam > 0')->where('img_id != 36')->where('img_id != 44')->orderby('sorszam','asc')->get('welcome_images')->result();
		return $ered;
	}

	function get_welcome_video(){
		if(hun())
			$lang = "hun";
		else
			$lang = "eng";

		$ered = $this->db->where('nyelv',$lang)->where('sorszam > 0')->orderby('sorszam','asc')->get('welcome_videos')->result();
		return $ered;
	}

	function get_welcome_fokep(){
		if(hun()){
			$lang = "hun";
			$ered = $this->db->where('nyelv',$lang)->where('img_id','36')->get('welcome_images')->row();
		}
		else{
			$lang = "eng";
			$ered = $this->db->where('nyelv',$lang)->where('img_id','44')->get('welcome_images')->row();
		}

		return $ered;
	}

	function set_settings_img($img_id, $img_url, $kepcim, $img_nagy, $kulsourl, $kepnagyitas, $tipus){
		$query = " UPDATE `cadline2`.`welcome_images`";
		$query .= " SET `img_url` = '$img_url', `img_cim` = '$kepcim', `img_nagy` = '$img_nagy', `url` = '$kulsourl', `nagyit` = '$kepnagyitas', `tipus` = '$tipus' ";
		$query .= " WHERE img_id = ".$img_id;


		//print "<br />".$query;

		$this->db->query($query);
	}

	function set_settings_video($welvid_id, $sorszam, $youtubeid, $kep, $cim, $leiras){
		$query = " UPDATE `cadline2`.`welcome_videos`";
		$query .= " SET `sorszam` = '$sorszam', `youtubeid` = '$youtubeid',";

		if($kep)
			$query .= " `kep` = '$kep',";

		$query .= " `cim` = '$cim', `leiras` = '$leiras' ";
		$query .= " WHERE welvid_id = ".$welvid_id;


		//print "<br />".$query;

		$this->db->query($query);
	}

	function set_elozo()
	{
		$mentes = true;

		$uri = $this->uri->uri_string();

		foreach($this->nincs_elozo as $nincs)
		{
			if(strstr($uri,$nincs)!==false)
			{
				$mentes = false;
			}
		}

		if($mentes)
		{
			$this->phpsession->save('elozo',$uri);
		}
	}

	function get_kategoriak($mit){

		$query .= " ( SELECT dl_id, tipus,";

		if(hun())
			$query .= " nev_hun nev";
		else
			$query .= " nev_eng nev";

		$query .= ", deleted FROM `cadline2`.`dl_kategoriak` k WHERE deleted = 0";

		if(hun())
			$query .= " AND nev_hun != ''";
		else
			$query .= " AND nev_eng != ''";

		if($mit != "mind")
			$query .= " AND k.tipus = '".$mit."'";



		$query .= ") UNION ( SELECT dl_id, tipus,";

		if(hun())
			$query .= " nev_eng nev";
		else
			$query .= " nev_hun nev";

		$query .= ", deleted FROM `cadline2`.`dl_kategoriak` k WHERE deleted = 0 ";

		if(hun())
			$query .= " AND nev_hun = ''";
		else
			$query .= " AND nev_eng = ''";

		if($mit != "mind")
			$query .= " AND k.tipus = '".$mit."'";

		$query .= ") ";


		$query .= " ORDER BY tipus";

		if(hun())
			$query .= ", nev";
		else
			$query .= ", nev";

		$ered = $this->db->query($query)->result();
		return $ered;
	}

	function get_kategoria($dlid){
		$query = " SELECT * FROM `cadline2`.`dl_kategoriak` WHERE dl_id = ".$dlid;

		$ered = $this->db->query($query)->row();
		return $ered;
	}

	function letezokategoria($katid,$tipus){
		//$lang = $this->muser->user['lang'];
		$query = $this->db->from('dl_kategoriak')->where('dl_id',$katid)->where('deleted','0')->where("tipus",$tipus);

		$query = $query->get('dl_kategoriak');

		if($query->num_rows()==0){
			return false;
		}else{
			return true;
		}
	}

	function set_kategoria($dl_id, $tipus, $nev_hun, $nev_eng){

		if($dl_id == "uj")
			$query = " INSERT INTO `cadline2`.`dl_kategoriak` VALUES (NULL,'".$tipus."','".$nev_hun."','".$nev_eng."', '0')";
		else
			$query = " UPDATE `cadline2`.`dl_kategoriak` SET tipus = '".$tipus."', nev_hun = '".$nev_hun."', nev_eng = '".$nev_eng."' WHERE dl_id = ".$dl_id;

		$this->db->query($query);
	}

	function new_kategoria($tipus,$nevhun,$neveng){
		$query = " INSERT INTO `cadline2`.`dl_kategoriak` (`dl_id` ,`tipus` ,`nev_hun` ,`nev_eng` ,`deleted`)";
		$query .= " VALUES(NULL , '".$tipus."', '".$nevhun."' , '".$neveng."' , '0')";

		$this->db->query($query);

		$id = mysql_insert_id();

		return $id;
	}

	function del_kategoria($dl_id){
		$query = " UPDATE `cadline2`.`dl_kategoriak` SET deleted = '1' WHERE dl_id =  ".$dl_id;

		$this->db->query($query);
	}

	function get_katName($kat_id){
		if(hun())
			$query = " SELECT nev_hun nev";
		else
			$query = " SELECT nev_eng nev";

		$query .= " FROM `cadline2`.`dl_kategoriak` WHERE dl_id = ".$kat_id;

		$query = $this->db->query($query)->row();

		return $query->nev;
	}

	function lastnewsletter($lang){
		$query = " SELECT nl_id, url FROM `cadline2`.`newsletter` WHERE lang = '".$lang."' AND deleted=0 ORDER BY nl_id DESC LIMIT 0, 1";

		$query = $this->db->query($query)->row();

		if( trim($query->url) != "" )
			return $query->url;
		else
			return $query->nl_id;
	}

	function lasttipoftheday($lang){
		$query=$this->db->query("SELECT nl_id, url FROM `cadline2`.`tipoftheday` WHERE lang = '".$lang."' AND deleted=0 ORDER BY nl_id DESC LIMIT 0, 1")->row();
		$ret=(trim($query->url)>"" ? $query->url : $query->nl_id);
		return ($ret);
	}
}
?>
