<?php
class Mobile extends Controller
{
	var $pw = "dfDF34mkZ6";
	var $ervenydesseg = 3600;
	var $kepek_utvonal = "public/img/collection/users/";
	var $nagykepneve = '1__big.jpg';
	var $kiskepneve  = '1__small.jpg';

	function Mobile()
	{
		parent::Controller();
		$user = $this->phpsession->get('user');
		$user['voltmobiloldal'] = true;
		$this->phpsession->save('user',$user);
	}

	function index(){
		$data['title'] = hun() ? 'ARCHLine.XP építész és belsőépítész szoftver' : 'ARCHLine.XP architect and interior design software';
		$data['tartalom']='mobile/index';
		$this->load->view('site_mobile',$data);
	}

	function page(){
		$page_id = $this->uri->rsegment(3);
		if( !is_numeric($page_id) ){
			$page_id = mysql_real_escape_string($page_id);
			$page = $this->db->query("SELECT * FROM `cadline2`.`pages` WHERE page_url = '".$page_id."'")->row();
		}
		else{
			$page=$this->mpages->get_row($page_id);
		}

		$data['page']=$page;
		$data['tartalom']='mobile/show';
		$this->load->view('site_mobile',$data);

	}

	function contact(){
		$data['title']      = hun() ? 'ARCHLine.XP - Kapcsolat' : 'ARCHLine.XP - Contact us';
		$data['tartalom']   = 'mobile/contact';
		$data['javascript'] = array('regformcheck');
		$data['canonical']  = "/contact";
		$this->load->view('site_mobile',$data);
	}

	function interior(){
		$data['title'] = hun() ? 'ARCHLine.XP Interior Belsőépítész program' : 'ARCHLine.XP Interior Design Software';
		$data['page'] = hun() ? $this->mpages->get_row(508) : $this->mpages->get_row(502);

		$data['page']->page_body = $this->mytags($data['page']->page_body);

		$data['page']->page_title = hun() ? 'ARCHLine.XP belsőépítészet' : 'ARCHLine.XP Interior';

		$data['elozooldal'] = "/mobile";
		$data['fullpage']  = '/interior';
		$data['canonical'] = "/interior";

		$data['tartalom']='mobile/show';
		$this->load->view('site_mobile',$data);
	}

	function architect(){
		$data['title'] = hun() ? 'ARCHLine.XP Építész program' : 'ARCHLine.XP Architect Software';
		$data['page'] = hun() ? $this->mpages->get_row(765) : $this->mpages->get_row(68);

		$allow = '<p><div><span><ul><li><b><strong><i><em><style><img><h1><h2><h3><table><tr><td><th><a>';
		$regex = '#\s*<(/?\w+)\s+(?:on\w+\s*=\s*(["\'\s])?.+?
\(\1?.+?\1?\);?\1?|style=["\'].+?["\'])\s*>#is';
		$data['page']->page_body = preg_replace($regex, '<${1}>',strip_tags($data['page']->page_body, $allow));

		$data['elozooldal'] = "/mobile";
		$data['fullpage']  = "/products";
		$data['canonical'] = "/products";

		$data['tartalom']='mobile/show';
		$this->load->view('site_mobile',$data);
	}

	function energy(){
		$data['title'] = hun() ? 'ARCHLine.XP Energetika' : 'ARCHLine.XP Energy';
		$data['page'] = hun() ? $this->mpages->get_row(21) : $this->mpages->get_row(68);

		$data['page']->page_body = $this->mytags($data['page']->page_body);
		if(hun()) $data['canonical'] = "/energetika";

		$data['tartalom']='mobile/show';
		$this->load->view('site_mobile',$data);
	}

	function services(){
		$data['tartalom'] = "mobile/services";
		$service = $this->uri->segment(3);
		if(!$service){
			//redirect('mobile#services');
			$data['tartalom'] = "mobile/services_main";
			$this->load->view('site_mobile',$data);
			return;
		}

		switch($service){
			case "virtuallyfurnish":
				$data['urlservice'] = 'virtuallyfurnish';
				$data['infix'] = 'vf';
				break;
			case "rendering":
				$data['urlservice'] = 'rendering';
				$data['infix'] = 're';
				break;
			case "sketch":
				$data['urlservice'] = 'sketch';
				$data['infix'] = 'sk';
				break;
		}

		if($this->uri->segment(4)=="order"){
			$this->load->view('mobile/services_order',$data);
			return;
		}else{
			$data['canonical'] = "/services/{$service}/pricing";
		}

		$this->load->view('site_mobile',$data);
    }

    function ws(){
        $this->load->model('mevents');
		$this->load->library('dateclass');

		$kezdo = array('nappali','konyhatervezes','furdoszoba_tervezes','virtual_staging');
		$lang = hun() ? 1 : 2;

		$urlveg = $this->uri->segment(3);
		$data['tema'] = $urlveg;
		$data['event'] = $this->db->query("SELECT * FROM events_new_main WHERE urlname = '".$urlveg."' AND language_id = ".$lang)->row();

		$data['dates'] = $this->db->query("SELECT * FROM events_new WHERE event_main_id = '".$data['event']->event_main_id."' AND start_ts > ".time()." AND (online IS NULL OR online = '0') AND active = 1 AND deleted = 0 ORDER BY start_ts")->result();
		$results = $this->mevents->getEventDatesByDay($data['event']->event_main_id, true);
		$data['dates']->events = $results['events'];
		foreach($data['dates'] as $date){
			$date->times->able_to_reg = $this->mevents->checkEventLimit($date->event_id);
		}

        if($this->uri->segment(4)=="register"){
			$data['canonical']  = "/interior/workshop/{$urlveg}/register/".$this->uri->segment(5);
			$data['esemid']     = $this->uri->segment(5);
			$data['javascript'] = array('regformcheck');
			$data['tartalom']   = "mobile/wsregister";
		}
		else{
			$data['canonical'] = "/interior/workshop/".$urlveg;
			$data['esemid'] = false;
			$data['tartalom'] = "mobile/workshop";
		}

    	$this->load->view('site_mobile',$data);
    }

	function wsregisztracio(){
		$this->load->model('mevents');
		$this->load->model('mclusers');
		$data['lang'] = $lang = $this->muser->user['lang'];
		$data['tartalom'] = "mobile/wsregister";
		$data['error'] = false;

		$post = array();
		$countries = array();
		$orszagok = $this->db->query('SELECT * FROM `'.CRMADATBAZIS.'`.`countries`')->result_array();
		foreach( $orszagok as $orszag )
			$countries[$orszag['ctrID']] = hun() ? strtolower($orszag['ctrName']) : strtolower($orszag['ctrNameEng']);

		$post['nev']     = $this->input->post('strName1')." ".$this->input->post('strName2');
		$post['tel']     = $this->input->post('strTel');
		$post['email']   = $this->input->post('strEMail');
		$post['cegnev']  = $this->input->post('strCompany');
		$post['varos']   = $this->input->post('strCity');
		$post['irsz']    = $this->input->post('strZip');
		$post['cim']     = $this->input->post('strStreet');
		$post['foglalkozas'] = $this->input->post('nUserProf');
		$post['nctrid']   = array_search(strtolower($this->input->post('ctrName')), $countries);
		$post['comment']  = $this->input->post('comment');
		$post['event_id'] = $this->input->post('event_id');
		$post['event_main_id'] = $this->input->post('event_main_id');

		if( $post['event_id'] != "personal" && (($error = $this->mevents->eventFormCheck($post, $lang)) || $_POST['policy']!='on') ){
			$error2 = implode('<br />', $error);
			if($_POST['policy']!='on')
				$error2 .= '<br />'.lang('event_elkellfog');
			$this->phpsession->save('wshiba',$error2);
			$this->phpsession->save('wsadatok',$post);
			header("Location: ".$_SERVER["HTTP_REFERER"]);
			return;
		}

		if( $post['event_id'] != "personal" )
			$result = $this->mevents->EventRegistration($post);
		else
			$result = 'success';

		$main_event_details = $this->mevents->getMainEventDetails($post['event_main_id']);

		if($result == 'limit') {
			//get the content of the limit page
			$data['page'] = hun() ? $this->mpages->get_row(489) : $this->mpages->get_row(481);
		} elseif($result == 'multiple') {
			//get the content of the multiple registration page
			$data['page'] = hun() ? $this->mpages->get_row(488) : $this->mpages->get_row(482);
		}elseif( $result == 'success' ){
			$event_details = new stdClass();

			if( $post['event_id'] != "personal" ){
				$event_details = $this->mevents->getEventDetailsByID($post['event_id']);
				$start      = date('Y-m-d H:i',$event_details->start_ts);
				$end        = date('Y-m-d H:i',$event_details->end_ts);
				if( $main_event_details->event_main_id == 56 )	// ARCHLine Klub
					$topicidHun = 161;
				else
					$topicidHun = 164;
				$topicidEng = 137;
				$info       = "web, ".mysql_escape_string($post['comment']);

				if( hun() && $main_event_details->reg_success_mail_page==578 )
						$emailtext = $this->mpages->get_row(735);
				else
					$emailtext  = $this->mpages->get_row($main_event_details->reg_success_mail_page);


			}else{
				$event_details->name = $main_event_details->name." - ".$post['event_id'];
				$start      = "On request";
				$end        = "";
				$topicidHun = 164;
				$topicidEng = 118;
				$info       = "web, WS kérés: ".$event_details->name." ".mysql_escape_string($post['comment']);
				$emailtext  = hun() ? $this->mpages->get_row(613) : $this->mpages->get_row(612);
			}

			//collect the event's and user's data for the emails
			$mail_data = array(
				'nev' => $post['nev'],
				'tel' => $post['tel'],
				'email' => $post['email'],
				'comment' => $post['comment'],
				'start' => $start,
				'end' => $end,
				'event_details' => $event_details,
				'main_event_details' => $this->mevents->getMainEventDetails($post['event_main_id'])
			);


			$page = $this->mpages->get_row($main_event_details->reg_success_mail_page);
			$datum = ($start!="On request") ? date('Y-m-d', $event_details->start_ts) : date('Y-m-d', time());

			if(hun()){
				$nUserID = $this->mclusers->get_nUserIDByEmail($post['email'],'hun');

				if(!$nUserID){
					$tomb['strName']    = $post['nev'];
					$tomb['strTel']     = $post['tel'];
					$tomb['strCompany'] = $post['cegnev'];
					$tomb['strCity']    = $post['varos'];
					$tomb['strZip']     = $post['irsz'];
					$tomb['strStreet']  = $post['cim'];
					$tomb['nUserProf']  = $post['foglalkozas'];
					$tomb['nCtrID']     = $post['nctrid'];
					$nUserID = $this->mclusers->addUserHun($tomb);

					$tomb2['nUserID'] = $nUserID;
					$tomb2['email']   = $post['email'];
					$this->mclusers->addUserEmailHun($tomb2);
				}

				//$query .= "INSERT INTO `clusers`.`cl_history` VALUES (NULL, {$nUserID}, {$topicidHun}, '{$datum}', '{$event_details->name}', '{$info}', 0)";
				$query .= "INSERT INTO `".CRMADATBAZIS."`.`u_history` VALUES (NULL,{$nUserID},{$topicidHun},'{$datum}','{$event_details->name}','{$info}', 36, '', '', NULL, NULL, NULL)";

				$this->db->query($query);
			}else{
				$nUserID = $this->mclusers->get_nUserIDByEmail($post['email'],'eng');

				if(!$nUserID){
					$tomb['strName']    = $post['nev'];
					$tomb['strTel']     = $post['tel'];
					$tomb['strCompany'] = $post['cegnev'];
					$tomb['strCity']    = $post['varos'];
					$tomb['strZip']     = $post['irsz'];
					$tomb['strStreet']  = $post['cim'];
					$tomb['nUserProf']  = $post['foglalkozas'];
					$tomb['nCtrID']     = $post['nctrid'];
					$nUserID = $this->mclusers->addUserEng($tomb);

					$tomb2['nUserID'] = $nUserID;
					$tomb2['email']   = $post['email'];
					$this->mclusers->addUserEmailEng($tomb2);
				}

				//$query .= "INSERT INTO `clusers_eng`.`cl_history` VALUES (NULL, {$nUserID}, {$topicidEng}, '{$datum}', '{$event_details->name}', '{$info}', 0)";
				$query .= "INSERT INTO `".CRMADATBAZIS."`.`u_history` VALUES (NULL,{$nUserID},{$topicidEng},'{$datum}','{$event_details->name}','{$info}', 36, '', '', NULL, NULL, NULL)";

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


			$emailtext->page_body = str_ireplace('#reg_name#', $mail_data['nev'], $emailtext->page_body);

			// construmásnál időpont
			$hetnapok = array("hétfő", "kedd", "szerda", "csütörtök", "péntek", "szombat", "vasárnap");
			$ennyiedik = date('N',$event_details->start_ts);
			$hetnap = $hetnapok[($ennyiedik-1)];

			$idopontkimenolevelbe = date('Y. m. d. (', $event_details->start_ts);
			$idopontkimenolevelbe .= $hetnap;
			$idopontkimenolevelbe .= date(') - H', $event_details->start_ts);
			$idopontkimenolevelbe .= " óra";

			$idopontkimenolevelbe2 = date('Y. m. d. (', $event_details->start_ts);
			$idopontkimenolevelbe2 .= $hetnap;
			$idopontkimenolevelbe2 .= date(')  H:i - ', $event_details->start_ts);
			$idopontkimenolevelbe2 .= date('H:i', $event_details->end_ts);

			$esemnevkimenolevelbe = $event_details->name;

			$emailtext->page_body = str_ireplace('#idopont#', $idopontkimenolevelbe, $emailtext->page_body);
			$emailtext->page_body = str_ireplace('#idopont2#', $idopontkimenolevelbe2, $emailtext->page_body);
			$emailtext->page_body = str_ireplace('#esemenyneve#',$esemnevkimenolevelbe,$emailtext->page_body);




			//send an email to cadline
			$this->load->library('email');
			$this->email->initialize();
			$this->email->from('info@cadline.hu', 'Cadline Weboldal');
			$this->email->subject($main_event_details->name.' Registration (mobile)');
			$this->email->message($this->load->view('events/mail/reg_success_to_cadline', $mail_data, true));

			if(hun())
				$this->email->send('info@cadline.hu', 'smtp');
			else
				$this->email->send('info@archlinexp.com', 'smtp');
		//		$this->email->send('sztikee@gmail.com', 'smtp');
			$this->email->clear();


			$this->email->initialize();
			if(hun())
				$this->email->from('info@cadline.hu', 'ARCHLine.hu');
			else
				$this->email->from('info@archlinexp.com', 'ARCHLineXP.com');
			$this->email->subject($emailtext->page_title);
			$this->email->message($emailtext->page_body);

			$this->email->send($post['email'], 'smtp');



			$lapid = $main_event_details->reg_success_page;
			//$data['page'] = $this->mpages->get_row($lapid);
			//$data['page']->page_body = str_ireplace('#reg_name#',$post['nev'],$data['page']->page_body);

			// csak hogy tudjak rola
			$record->osztaly = "mobile";
			$record->fv = "wsregisztracio";
			$record->hiba = "WS reg mobilról: ".print_r($_POST,true);
			$record->datum = date('Y-m-d H:i:s',time());
			$this->db->insert('a_hibak',$record);

			redirect('mobile/page/'.$lapid);

		}

		$data['tartalom']='mobile/show';
		$this->load->view('site_mobile',$data);
	}

	function op(){
		$data['page']       = $this->mpages->get_row(640);
		$data['tartalom']   = "mobile/op";
		$data['javascript'] = array('regformcheck');
		$this->load->view('site_mobile',$data);
	}

    function galleryarchitect(){
		$this->load->model('mgallery');
		$data['title'] = hun() ? 'ARCHLine.XP - Építészet képgaléria' : 'ARCHLine.XP - Architect gallery';
		$data['tartalom']='mobile/gallery2';
		if(hun())
			$nyelv = "hun";
		else
			$nyelv = "eng";
		$data['galeriak']  = $this->mgallery->get_all($nyelv,'architect');
		$data['fullpage']  = '/multimedia/images';
		$data['canonical'] = '/multimedia/images';

		$this->load->view('site_mobile',$data);
	}

	function galleryinterior(){
		$this->load->model('mgallery');
		$data['title'] = hun() ? 'ARCHLine.XP - Belsőépítészet képgaléria' : 'ARCHLine.XP - Interior Design gallery';
		$data['tartalom']='mobile/gallery2';
		if(hun())
			$nyelv = "hun";
		else
			$nyelv = "eng";
		$data['galeriak']  = $this->mgallery->get_all($nyelv,'interior');
		$data['fullpage']  = '/interior/gallery';
		$data['canonical'] = "/interior/gallery";

		$this->load->view('site_mobile',$data);
	}

	function gallerydetail(){
		$this->load->model('mgallery');
		$folder = $this->uri->segment(3);
		if($folder && $this->mgallery->checkFolder($folder)){
			$nyelv = hun() ? "hun" : "eng";
			$sql = "SELECT * FROM gallerys WHERE mappa='gallery/{$folder}' AND nyelv='{$nyelv}' AND deleted=0";
			$adatok = $this->db->query($sql)->row();
			$metak = array();
			if($adatok){
				$metak['galeriacim'] = $metak['title'] = $adatok->cim.' - ARCHLine.XP';
				if($adatok->terulet=="interior" || $adatok->terulet=="architect")
					$metak['canonical'] = "/interior/gallery/".$folder;
			}
			$this->touchgallery(base64_encode('public/gallery/'.$folder.'/'),$metak);

		}
		else{
			redirect('mobile/galleryinterior');
		}
	}

	function gallerymonth(){
		$this->load->model('mgallery');

		$honap = $this->uri->segment(3);

		if( !is_numeric($honap) ){
			redirect('mobile/gallerymonth/'.(date('m',time())-1+24));
		}else{
			$data['galeria'] = $this->mgallery->get_rowHonapSorszam($honap, 'hun');

			if( date('m',time()) <= ($honap-24) && date('j',time()) <= 26 )
				redirect('mobile/gallerymonth/'.(date('m',time())-1+24));
		}

		$honapnevek = lang('honapok');
		$kivhonap = (($honap-1)%24+1);

		$data['canonical'] = hun() ? "/palyazat/gallery/".$honap : "/monthpicture/gallery/".$honap;

        $data['javascript'] = array('responsiveimagesc/responsiveimgs');
		$data['title'] = hun() ? 'A hónap képe: ' : 'Picture of the month: ';
		$data['title'] .= $honapnevek[$kivhonap];
		$data['tartalom']='mobile/gallerymonth';
		$this->load->view('site_mobile',$data);
	}


	function news(){
		$hir_id = $this->uri->segment(3);
		if(is_numeric($hir_id)){
			$hir = $this->db->query("SELECT * FROM news WHERE news_id={$hir_id}")->row();
			$hir->news_text = $this->mytags($hir->news_text);
			if( $hir->news_m<10 ) $hir->news_m = '0'.$hir->news_m;
			if( $hir->news_d<10 ) $hir->news_d = '0'.$hir->news_d;
			$hir->datum1 = $hir->news_y.'-'.$hir->news_m.'-'.$hir->news_d;
			if(hun())
				$hir->datum2 = $hir->news_y.'. '.$hir->news_m.'. '.$hir->news_d.'.';
			else
				$hir->datum2 = $hir->news_d.'. '.$hir->news_m.'. '.$hir->news_y.'.';
			$data['hir'] = $hir;
			$data['tartalom'] = "mobile/news_show";
			$data['canonical'] = ($hir->url) ? "/news/show/".$hir->url : "/news/show/".$hir->news_id;
		}
		else{
			$hirek_list = $this->db->query("SELECT news_id, CAST(news_m AS UNSIGNED) news_mm, CAST(news_d AS UNSIGNED) news_dd, news_title FROM news WHERE news_lang='".$this->muser->user['lang']."' ORDER BY news_arch ASC, news_y DESC, news_mm DESC, news_dd DESC LIMIT 0,15")->result();
			$data['hirek_list'] = $hirek_list;
			$data['tartalom'] = "mobile/news";
		}

    	$this->load->view('site_mobile',$data);
	}

	function newsletter(){
		$hirl_azon = $this->uri->segment(3);
		if($hirl_azon){

			if($hirl_azon == "milora-anyagok"){
				Header("HTTP/1.1 301 Moved Permanently");
				Header("Location: /mobile/newsletter/anyagmintak");
			}


			if(is_numeric($hirl_azon))
				$hirlevel = $this->db->where('nl_id', $hirl_azon)->get('newsletter')->row();
			else
				$hirlevel = $this->db->where('url', $hirl_azon)->get('newsletter')->row();

			$hirlevel->text    = $this->mytags($hirlevel->text);
			$data['hirlevel']  = $hirlevel;
			$data['canonical'] = "/newsletter/show/".$hirlevel->url;
			$data['tartalom']  = "mobile/newsletter_show";
		}
		else{
			$hirlevelek = $this->db->query("SELECT * FROM newsletter WHERE lang='".$this->muser->user['lang']."' AND deleted=0 ORDER BY nl_id DESC LIMIT 0,20")->result();
			$data['hirlevelek'] = $hirlevelek;
			$data['tartalom'] = "mobile/newsletter";
		}

    	$this->load->view('site_mobile',$data);
	}

	function newfeatures(){
		if(true || hun()){
			$this->newfeatures2013();
			return;
		}

		$data['title'] = hun() ? 'ARCHLine.XP 2012 Újdonságok' : 'ARCHLine.XP 2012 new features';
		$data['page'] = hun() ? $this->mpages->get_row(555) : $this->mpages->get_row(556);

		$data['page']->page_body = $this->mytags($data['page']->page_body);
		$csere = array(
			'<img alt="" src="/public/upload/eng/images/News_2012/Roof_tiles_in_3D_2012_TN.jpg" style="width: 480px; height: 390px; " />',
			'<img alt="" src="/public/upload/eng/images/News_2012/Advanced_Template_management_2012_TN.jpg" style="width: 480px; height: 390px; " />',
			'<img alt="" src="/public/upload/eng/images/News_2012/Worktop_wizard_2012_TN.jpg" style="width: 480px; height: 286px; " />'
		);
		$mire  = array(
			'<img alt="" src="/public/upload/eng/images/News_2012/Roof_tiles_in_3D_2012_TN.jpg?full=/public/upload/eng/images/News_2012/Roof_tiles_in_3D_2012_TN.jpg" />',
			'<img alt="" src="/public/upload/eng/images/News_2012/Advanced_Template_management_2012_TN.jpg?full=/public/upload/eng/images/News_2012/Advanced_Template_management_2012_TN.jpg" />',
			'<img alt="" src="/public/upload/eng/images/News_2012/Worktop_wizard_2012_TN.jpg?full=/public/upload/eng/images/News_2012/Worktop_wizard_2012_TN.jpg" />'
		);
		if(!hun())
			$data['page']->page_body = str_replace($csere,$mire,$data['page']->page_body);
		$data['elozooldal'] = "/mobile#news";
		$data['canonical']  = hun() ? "/ujdonsagok-2012" : "/new-features-2012";
		$data['tartalom']   = "mobile/show";
        $data['javascript'] = array('responsiveimagesc/responsiveimgs');
    	$this->load->view('site_mobile',$data);
	}

	function newfeatures2013(){
		$data['title'] = hun() ? 'ARCHLine.XP 2013 Újdonságok' : 'ARCHLine.XP 2013 new features';
		$data['page'] = hun() ? $this->mpages->get_row(752) : $this->mpages->get_row(753);

		$data['page']->page_body = $this->mytags($data['page']->page_body);
		$csere = array(
			'<img alt="" src="/public/upload/eng/images/2013_beta_released_HUN.jpg" style="width: 720px; height: 250px;" />',
			'<img alt="" src="/public/upload/eng/images/Handrail_2013.png" style="width: 488px; height: 460px;" />',
			'<img alt="" src="/public/upload/eng/images/Roof_gutter.png" style="width: 500px; height: 500px;" />',
			'<img height="113" src="/public/img/ujdonsagok/2013/thumbs/image007.png" width="940" />'
		);
		$mire  = array(
			'<img alt="" src="/public/upload/eng/images/2013_beta_released_HUN.jpg?full=/public/upload/eng/images/2013_beta_released_HUN.jpg" />',
			'<img alt="" src="/public/upload/eng/images/Handrail_2013.png?full=/public/upload/eng/images/Handrail_2013.png" />',
			'<img alt="" src="/public/upload/eng/images/Roof_gutter.png?full=/public/upload/eng/images/Roof_gutter.png" />',
			'<img alt="" src="/public/img/ujdonsagok/2013/thumbs/image007.png?full=/public/img/ujdonsagok/2013/image007.png" />'
		);
		$data['page']->page_body = str_replace($csere,$mire,$data['page']->page_body);
		$data['elozooldal'] = "/mobile#news";
		$data['canonical']  = hun() ? "/ujdonsagok-2013" : "/new-features-2013";
		$data['tartalom']   = "mobile/show";
        $data['javascript'] = array('responsiveimagesc/responsiveimgs');
    	$this->load->view('site_mobile',$data);
	}


    function shbrands(){
    	$this->load->model('mcollection2');
        $gyartok = $this->mcollection2->get_gyartok();

        $data['brands'] = $gyartok;

		$data['canonical'] = "/showroom";

        $data['title'] = hun() ? 'ARCHLine.XP Bemutatóterem, Gyártók' : 'ARCHLine.XP Showroom, Brands';

		$data['tartalom']='mobile/sr_brands';
		$this->load->view('site_mobile',$data);
    }

    function shcategories(){
        $this->load->model('mcollection2');
    //    $kategoriak = $this->mcollection2->get_kategoriak($this->uri->segment(3));
        $gyarto     = $this->mcollection2->get_manBydir($this->uri->segment(3));

		$parancs = "SELECT *, (SELECT COUNT(o_id) FROM collection2 WHERE collection2.c_id = coll2_cat.c_id AND deleted=0) elemszam FROM coll2_cat WHERE m_id = {$gyarto->id} AND deleted=0";
		if(hun())
			$parancs .= ' ORDER BY nev_hun ASC';
		else
			$parancs .= ' ORDER BY nev_eng ASC';
		$kategoriak = $this->db->query($parancs)->result();

		if(hun()){
			$data['meta']['desc'] = 'Tekintse meg a(z) '.$gyarto->name.' elemeit az ARCHLine.XP Bemutatótermében.';
		}else{
			$data['meta']['desc'] = 'ARCHLine.XP Showroom - categories of '.$gyarto->name;
		}

        $data['categories'] = $kategoriak;
        $data['gyarto']     = $gyarto;
		$data['canonical']  = "/showroom/manufacturer/".$this->uri->segment(3);
		$data['title']      = lang('collec2_showroom').": ".$gyarto->name;

		$data['tartalom']='mobile/sr_categories';
		$this->load->view('site_mobile',$data);
    }

    function shobjects(){
        $this->load->model('mcollection2');
        $kat = $this->uri->segment(3);
        $kepek = array();

        $elemek    = $this->mcollection2->get_objektumok($kat);
        $kategoria = $this->mcollection2->get_kategoria($kat);
        $gyarto     = $this->mcollection2->get_manById($kategoria->m_id);
        $utvonalinf = 'public/img/collection/'.$gyarto->dir.'/';
		$nyelv     = hun() ? "hun" : "eng";
        foreach($elemek as $k=>$elem){
		//	$thumbkep = $utvonalinf.$elem->o_id.'/thumb/'.end(scandir($utvonalinf.$elem->o_id.'/thumb/'));
        	$kepelem = new stdClass();
            $nagykepek = scandir($utvonalinf.$elem->o_id.'/big/');
            $kepelem->kep = $utvonalinf.$elem->o_id.'/big/'.$nagykepek[2];
		/*	if( $thumbkep!=".." && $thumbkep!="." && !is_dir($thumbkep) && file_exists($thumbkep) )
				$kepelem->kiskep = $thumbkep;
			else
				$kepelem->kiskep = $kepelem->kep;*/
			$kepelem->kiskep = $utvonalinf.$elem->o_id.'/zz_thumb.jpg';
            $kepalt = $this->db->query("SELECT title FROM coll2_desc WHERE o_id={$elem->o_id} AND lang='{$nyelv}'")->row();
            $kepelem->kepalt = $kepalt->title;
            $kepek[] = $kepelem;
        }

		if(hun()){
			$data['meta']['desc'] = "ARCHLine.XP Bemutatóterem - {$gyarto->name}: {$kategoria->nev_hun} kategória elemei";
		}else{
			$data['meta']['desc'] = "ARCHLine.XP Showroom - {$gyarto->name}: {$kategoria->nev_eng} items";
		}

        $data['kepek']   = $kepek;
		$data['canonical'] = "/showroom/category/".$kat;

		$data['title'] = hun() ? $kategoria->nev_hun.' Galéria' : $kategoria->nev_eng.' Gallery';
		$data['mgtitle'] = $data['title'];
		$this->load->view('mobile/photoswipegallery',$data);
    }




    function swipe(){
		$this->touchgallery();
	}

	function touchgallery($param1,$pdata = array()){
		if($param1)
			$p = $param1;
		else
			$p = $this->uri->segment(3);
		$utvonal = "";

		if( $p ){
			$p = base64_decode($p);
			$utvonal = $p;
		}else{
			$utvonal = 'public/img/gallery2012/';
		}

		$kepek       = scandir($utvonal);

		unset($kepek[0],$kepek[1]);

		if( is_dir($utvonal."thumbs/") ){
			$kiskepek = scandir($utvonal."thumbs/");
			unset($kiskepek[0],$kiskepek[1]);
		}

		foreach($kepek as $kep){
			if( !is_dir( $utvonal.$kep ) ){
					$data['elsokep'] = $utvonal.$kep;

				break;
			}else{
				unset($kep);		// ???
			}
		}

		$data['utvonal'] = $utvonal;
		$data['kepek']   = $kepek;

		if(strstr($utvonal,'gallery2012architect'))
			$data['galeriacim'] = hun() ? 'ARCHLine.XP Építészet' : 'ARCHLine.XP Architect';

		$data['title'] = hun() ? ' Galéria' : ' Gallery';

		foreach($pdata as $k=>$v) $data[$k]=$v;

		$this->load->view('mobile/photoswipe',$data);
	}

	function usergallery(){
		$uid = $this->uri->segment(3);
		$kepekutvonal = $this->kepek_utvonal.$uid."/";
		if( !is_numeric($uid) && $uid!="guest" ){
			redirect("mobile");
		}

		if( !is_dir($kepekutvonal) ){
			$data['projektek'] = false;
			$this->load->view("mobile/usergallery",$data);
			return;
		}

		$login = $this->_getlogineduser();
		//if( !$login || !is_numeric($login->user_id) || $login->user_id!=$uid )
		if( (!$login && $uid!="guest") ||
			(!is_numeric($login->user_id) && $uid!="guest") ||
			($login->user_id!=$uid && $uid!="guest")
		){
			redirect('mobile/login');
		}

		$project = $this->uri->segment(4);
		if( $project != "" && is_dir($kepekutvonal.$project) ){
			if( $uid=="guest" ){
				$project .= "/".$this->uri->segment(5);
				$uid = "guest";
			}
			$this->usergalleryproject($uid, $project, $login->mutat);
			return;
		}

		if($uid=="guest"){
			redirect('mobile/guests');
		}

		$sqlprojektek = "SELECT * FROM collection_projects WHERE userid='{$uid}' AND deleted=0 AND cp_id IN (SELECT DISTINCT(cp_id) FROM collection_user WHERE userid='{$uid}' AND deleted=0";
		// minden projekt
		if($login->mutat === true){
		}
		// jelszavas projektek
		elseif( gettype($login->mutat)=="string"  &&  strlen($login->mutat)>0 ){
			$sqlprojektek .= " AND guestname='{$login->mutat}'";
			$sqlprojektek .= " AND shared=1";
		}
		// publikus projektek
		else{
			$sqlprojektek .= " AND (guestname='' OR guestname IS NULL)";
			$sqlprojektek .= " AND shared=1";
		}
		$sqlprojektek .= ") ORDER BY nev ASC";

		$projektek = $this->db->query($sqlprojektek)->result();

		$data['projektek'] = $projektek;
		$data['megosztas'] = $login->mutat;
		$data['uid']       = $login->user_id;

		$this->load->view("mobile/usergallery",$data);
	}

	function usergalleryproject($uid, $project, $shared=false){
		//print "elozo: ".$_SERVER['HTTP_REFERER'];
		if($_SERVER['HTTP_REFERER'] == "" || strstr($_SERVER['HTTP_REFERER'],'mobile/login') ){
			/*if($uid=="guest")
				redirect('mobile/guests/'.current(explode('/',$project)));
			else
				redirect('mobile/usergallery/'.$uid);*/
		}

		$kepekutvonal = $this->kepek_utvonal.$uid."/";

		if( $project == "" || !is_dir($kepekutvonal.$project) ){
			$data['projekt'] = false;
			$this->load->view("mobile/usergalleryproject",$data);
			return;
		}


		//$project = $this->db->query("SELECT * FROM collection_projects WHERE userid='{$uid}' AND mappa='{$project}' AND deleted=0")->row();
		$sqlprojekt = "SELECT * FROM collection_projects WHERE userid='{$uid}' AND mappa='{$project}' AND deleted=0";
		if($uid!="guest"){
			if( $shared===true ){
			}elseif( gettype($shared)=="string"  &&  strlen($shared)>0 ){
				$sqlprojekt .= " AND guestname='{$shared}'";
			}else{
				$sqlprojekt .= " AND (guestname='' OR guestname IS NULL)";
			}
		}

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

		$kepekutvonal = $this->kepek_utvonal.$uid."/".$project->mappa."/";
		$elemek = array();
		$sql = "SELECT * FROM `collection_user` WHERE userid='{$uid}' AND cp_id='".$project->cp_id."' AND deleted=0 ";
		if( $shared!==true )
			$sql .= " AND shared=1 ";
		$elemek = $this->db->query($sql)->result();
		$kiskepek = array();
		foreach($elemek as $elem){
			// csak azok amikhez van nagy is
			if( file_exists($kepekutvonal.$elem->cu_id."/".$this->kiskepneve) && file_exists($kepekutvonal.$elem->cu_id."/".$this->nagykepneve) ){
				$ujelem       = new stdClass();
				$ujelem->cu_id = $elem->cu_id;
				$ujelem->title = $elem->title;
				$kiskepek[]   = $ujelem;
			}
		}

		$project->utvonal = $kepekutvonal;
		$project->kepek = $kiskepek;

		$data['uid']     = $uid;
		$data['projekt'] = $project;

		$this->load->view("mobile/usergalleryproject_full",$data);
	}

	function guests(){
		if( $this->uri->segment(3) != "" ){
			$this->guestuser($this->uri->segment(3));
			return;
		}

		$sql = "SELECT * FROM collection_projects WHERE userid='guest' AND deleted=0 GROUP BY guestname ORDER BY guestname ASC";
		$vendegek = $this->db->query($sql)->result();

		$data['vendegek'] = $vendegek;

		$this->load->view("mobile/guests",$data);
	}

	function guestuser($guestdir){
		$sqlprojektek = "SELECT * FROM collection_projects WHERE userid='guest' AND mappa LIKE '{$guestdir}/%' AND deleted=0 AND cp_id IN (SELECT DISTINCT(cp_id) FROM collection_user WHERE userid='guest' AND deleted=0) ORDER BY nev ASC";

		$projektek = $this->db->query($sqlprojektek)->result();

		$data['projektek'] = $projektek;
		$data['megosztas'] = false;
		$data['uid']       = "guest";

		$this->load->view("mobile/usergallery",$data);
	}

	function login(){
		// ha be van lepve akkor menjen a projektjeihez
		$login = $this->_getlogineduser();
		if( is_numeric($login->user_id) && $login->user_id>0 ){
			redirect('mobile/usergallery/'.$login->user_id);
		}

		$data['info']      = hun() ? $this->mpages->get_row(726) : $this->mpages->get_row(725);
		$data['canonical'] = "/view";
		$data['title']     = hun() ? ' Bejelentkezés az ARCHLine View-ba' : ' Login in ARCHLine View';
		$data['tartalom']  = 'mobile/login';

		$this->load->view('site_mobile',$data);
	}

	function loginin(){
		if(isset($_SESSION['belepesiprobak']) && $_SESSION['belepesiprobak'] > 15){
			$this->phpsession->save('feltoltesbelepesihiba',"hfvj");
			$this->phpsession->save('feltoltesbelepesihibaemail',$email);
			//print "hiba, vissza: ".$link;
			//header("Location: ".$_SERVER["HTTP_REFERER"]);
			print "Too many attempts .. 20 minutes break";
			exit;
		}


		$this->load->model('mforum');
		$email = $_POST["email"];
		$jelszo = $_POST["jelszo"];
		$nyelv = $_POST["lang"];
		$link  = $_SERVER["HTTP_REFERER"];

		$this->muser->user['lang'] = $nyelv;
		$slang = hun() ? "hu" : "en";

		$error = "";


		// ha nincs user vagy guest akkor a nyilvanosra iranyitani
		if( $email == "" || $email == "guest" )
			redirect('mobile/guests');

		// ha nincs jelszó akkor csak a publikus elemek látszódjanak
		if( !$jelszo && $email!="guest" ){
			$user_id = $this->mforum->get_userid($email);

			if(!$user_id){
				if(!isset($_SESSION['belepesiprobak'])) $_SESSION['belepesiprobak'] = 0;
				else $_SESSION['belepesiprobak']++;
				$this->phpsession->save('feltoltesbelepesihiba',"hfvj");
				$this->phpsession->save('feltoltesbelepesihibaemail',$email);
				//print "hiba, vissza: ".$link;
				header("Location: ".$link); exit;
			}

			$sessid   = md5($email.time());
			$hash     = md5($email . $this->pw . $sessid);
			$mutathash= " ";
			setcookie("sessid",$sessid,time()+$this->ervenydesseg,"/");
			setcookie("azonosito", $email, time()+$this->ervenydesseg,"/");
			setcookie("hash",$hash,time()+$this->ervenydesseg,"/");
			setcookie("mutat","public",time()+$this->ervenydesseg,"/");
			setcookie("mutathash",$mutathash,time()+$this->ervenydesseg,"/");
			redirect($slang."/mobile/login");
		}

		// ha nem stimmel a név+jelszó páros
		if( !$this->mforum->belep($email,$jelszo) ){

			// projekthez kapott jelszót
			$user_id = $this->mforum->get_userid($email);

			if(!$user_id){
				if(!isset($_SESSION['belepesiprobak'])) $_SESSION['belepesiprobak'] = 0;
				else $_SESSION['belepesiprobak']++;
				$this->phpsession->save('feltoltesbelepesihiba',"hfvj");
				$this->phpsession->save('feltoltesbelepesihibaemail',$email);
				//print "hiba, vissza: ".$link;
				header("Location: ".$link); exit;
			}

			// van-e olyan projekt amit ezzel a jelszóval meg lehet nézni
			$jelszavasprojektek = "SELECT count(cp_id) darab FROM collection_projects WHERE userid={$user_id} AND guestname='{$jelszo}' AND deleted=0";
			$projektek = $this->db->query($jelszavasprojektek)->row();

			if($projektek->darab > 0){
				$sessid   = md5($email.time());
				$hash     = md5($email . $this->pw . $sessid);
				$mutathash= md5($hash  . $this->pw . $jelszo);
				setcookie("sessid",$sessid,time()+$this->ervenydesseg,"/");
				setcookie("azonosito", $email, time()+$this->ervenydesseg,"/");
				setcookie("hash",$hash,time()+$this->ervenydesseg,"/");
				setcookie("mutat",$jelszo,time()+$this->ervenydesseg,"/");
				setcookie("mutathash",$mutathash,time()+$this->ervenydesseg,"/");
				redirect($slang."/mobile/login");

			}else{
				$this->phpsession->save('feltoltesbelepesihiba',"hfvj");
				$this->phpsession->save('feltoltesbelepesihibaemail',$email);
				//print "hiba, vissza: ".$link;
				header("Location: ".$link); exit;
			}
		}
		// ha érvényes loginnal és jelszóval lép be
		else{
			$_SESSION['belepesiprobak'] = 0;
			$sessid   = md5($email.time());
			$hash     = md5($email . $this->pw . $sessid);
			$mutathash= md5($hash  . $this->pw . "minden");
			setcookie("sessid",$sessid,time()+$this->ervenydesseg,"/");
			setcookie("azonosito", $email, time()+$this->ervenydesseg,"/");
			setcookie("hash",$hash,time()+$this->ervenydesseg,"/");
			setcookie("mutat","minden",time()+$this->ervenydesseg,"/");
			setcookie("mutathash",$mutathash,time()+$this->ervenydesseg,"/");

			$user_id = $this->mforum->get_userid($email);
			$tomb = array();
			$tomb['feltolto_id'] = $user_id;
			$tomb['email'] = $email;
			$this->phpsession->save('feltolo',$tomb);
			redirect($slang."/mobile/login");
		}
	}

	function logout(){
		$link = $_SERVER["HTTP_REFERER"];
		unset($_SESSION['default']['feltolo']);

		setcookie("sessid","",0,"/");
		setcookie("azonosito","",0,"/");
		setcookie("hash","",0,"/");

		redirect("mobile/login");
	}

	function mytags($input){
		// amik maradhatnak az inputban
		$allow = '<p><div><span><ul><li><b><strong><i><em><style><img><h1><h2><h3><br>';
		$regex = '#\s*<(/?\w+)\s+(?:on\w+\s*=\s*(["\'\s])?.+?
\(\1?.+?\1?\);?\1?|style=["\'].+?["\'])\s*>#is';
		$res = preg_replace($regex, '<${1}>',strip_tags($input, $allow));

		return $res;
	}

	// projekt szerkesztés, elemekkel együtt
	function editusergalleryproject(){
		$project = $this->uri->segment(3);

		$login = $this->_getlogineduser();
		if( !$login || !is_numeric($login->user_id) || $login->mutat !== true )
			redirect('mobile/login');

		$uid = $login->user_id;
		$kepekutvonal = $this->kepek_utvonal.$uid."/";
		if( $project == "" || !is_dir($kepekutvonal.$project) ){
			redirect("mobile/usergallery/".$uid."#edit");
		}


		$project = $this->db->query("SELECT * FROM collection_projects WHERE userid={$uid} AND mappa='{$project}' AND deleted=0")->row();

		$kepekutvonal = $this->kepek_utvonal.$uid."/".$project->mappa."/";
		$elemek = array();
		$sql = "SELECT * FROM `collection_user` WHERE userid={$uid} AND cp_id='".$project->cp_id."' AND deleted=0";
		$elemek = $this->db->query($sql)->result();
		$kiskepek = array();
		foreach($elemek as $elem){
			// csak azok amikhez van nagy is
			if( file_exists($kepekutvonal.$elem->cu_id."/".$this->kiskepneve) && file_exists($kepekutvonal.$elem->cu_id."/".$this->nagykepneve) ){
				$ujelem        = new stdClass();
				$ujelem->cu_id = $elem->cu_id;
				$ujelem->name  = $elem->title;
				$ujelem->desc  = $elem->desc;
				$kiskepek[]    = $ujelem;
			}
		}

		$project->utvonal = $kepekutvonal;
		$project->kepek = $kiskepek;

		$data['uid']     = $uid;
		$data['projekt'] = $project;
		$data['tartalom'] = "mobile/editusergalleryproject";

		$this->load->view("site_mobile",$data);

	}

	// projekt szerkesztés
	function edituserprojectprop(){
		$cp_id = $this->uri->segment(3);

		$login = $this->_getlogineduser();
		if( !$login || !is_numeric($login->user_id) || $login->mutat !== true )
			redirect('mobile/login');

		$uid = $login->user_id;
		$kepekutvonal = $this->kepek_utvonal.$uid."/";
		$project = $this->db->query("SELECT * FROM collection_projects WHERE userid={$uid} AND cp_id='{$cp_id}' AND deleted=0")->row();

		if( $project->mappa == "" || !is_dir($kepekutvonal.$project->mappa) ){
			redirect("mobile/usergallery/".$uid);
		}

		if( isset( $_POST['do'] ) ){
			$pr2->nev        = $this->input->post('nev');
			$pr2->guestname  = $this->input->post('jelszo');
			$this->db->update('collection_projects',$pr2,"cp_id={$cp_id}");
			redirect('mobile/usergallery/'.$project->userid);

		}elseif( $this->uri->segment(4) == "delete" ){
			$pr2->deleted = 1;
			$this->db->update('collection_projects',$pr2,"cp_id={$cp_id}");

			/*mappa törlése*/
			$trash = 'public/img/collection/users/trash/';
			$userprojektekpath = "public/img/collection/users/{$uid}/";
			mkdir("{$trash}{$uid}");
			mkdir("{$trash}{$uid}/".$project->mappa);
			exec("chmod 777 -R {$trash}{$uid}");
			exec("cp -R {$userprojektekpath}{$project->mappa} {$trash}{$uid}");
			exec("rm -R {$userprojektekpath}{$project->mappa}");
			// log
			$log = "mappa torolve ".date('Y-m-d H:i',time())." ".$project->mappa;
			exec("echo '{$log}' >> public/img/collection/users/userlog.txt");

			//redirect('mobile/editusergalleryproject/'.$project->mappa);
			redirect('mobile/usergallery/'.$project->userid);

		}else{
			$data['project'] = $project;
			$this->load->view("mobile/edituserprojectprop",$data);
		}

	}

	function editusergalleryitem(){
		$cu_id = $this->uri->segment(3);

		$login = $this->_getlogineduser();
		if( !$login || !is_numeric($login->user_id) || $login->mutat !== true )
			redirect('mobile/login');

		$uid = $login->user_id;
		$cu_id = $this->uri->segment(3);

		$elem = $this->db->query("SELECT * FROM collection_user WHERE cu_id={$cu_id}")->row();

		if( $elem->userid != $uid ){
			redirect('mobile/login');
		}

		if( isset( $_POST['do'] ) ){
			$elem2->title  = $this->input->post('title');
			$elem2->desc   = $this->input->post('desc');
			$elem2->shared = ($this->input->post('shared')=="on") ? 1 : 0;

			$this->db->update('collection_user',$elem2,"cu_id={$cu_id}");

			$project = $this->db->query("SELECT mappa FROM collection_projects WHERE cp_id=".$elem->cp_id)->row();

			redirect('mobile/editusergalleryproject/'.$project->mappa);
		}elseif( $this->uri->segment(4) == "delete" ){
			$elem2->deleted = 1;
			$this->db->update('collection_user',$elem2,"cu_id={$cu_id}");
			$project = $this->db->query("SELECT mappa FROM collection_projects WHERE cp_id=".$elem->cp_id)->row();

			redirect('mobile/editusergalleryproject/'.$project->mappa);

		}else{

			$data['elem'] = $elem;

			$this->load->view("mobile/editusergalleryitem",$data);
		}
	}

	private function _getlogineduser(){
		if( !isset($_COOKIE['azonosito'],$_COOKIE['sessid']) )
			return false;

		$ell = md5($_COOKIE['azonosito'].$this->pw.$_COOKIE['sessid']);

		if ( !empty($_COOKIE['hash']) && $_COOKIE['hash']== $ell ){
			setcookie("sessid",$_COOKIE['sessid'],time()+$this->ervenydesseg,"/");
			setcookie("azonosito", $_COOKIE['azonosito'] , time()+$this->ervenydesseg,"/");
			setcookie("hash",$_COOKIE['hash'] ,time()+$this->ervenydesseg,"/");
			setcookie("mutathash",$_COOKIE['mutathash'] ,time()+$this->ervenydesseg,"/");

			$lang  = $this->muser->user['lang'];
			$query = "SELECT * FROM forum_user WHERE login = '".$_COOKIE['azonosito']."' AND nyelv='".$lang."' AND active=1 AND deleted=0";

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

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

				$mutatell = md5($_COOKIE['hash'] . $this->pw . "minden");
				$mutatprell = md5($_COOKIE['hash'] . $this->pw .$_COOKIE['mutat']);
				if( $_COOKIE['mutathash'] == $mutatell ){
					$user->mutat = true;
				}elseif( $_COOKIE['mutathash'] == $mutatprell ){
					$user->mutat = $_COOKIE['mutat'];
				}
				else{
					$user->mutat = false;
				}

				return $user;
			}
		}else{
			return false;
		}
	}

}
?>
