<?php
class Render extends Controller
{
	/*
	Render ÁLLAPOTOK:
	
	0 - feldolgozasra var, semmi nem törént vele
	1 - kész van
	2 - renderelés folyamatban
	4 - projekt feldolgozásra vár
	30 - megszakításra vár
	31 - megszakítás alatt
	32 - megszakítva
	
	
	*/
	
	
	var $irodaip = '84.3.52.156';
	
	var $allapotoktomb = array(
		"0" => "semmi nem törént vele, elkezdendő",
		"1" => "kész van",
		"2" => "renderelés folyamatban",
		"4" => "projekt feldolgozásra vár",
		"30" => "megszakításra vár",
		"31" => "megszakítás alatt",
		"32" => "megszakítva"
	);
	
	var $pw = "dfDF34mkZ6";
	var $ervenydesseg = 3600;
	var $ervenydessegazon = 172800;
	var $valtozasok = "public/render/valtozasok.xml";
	var $valtozasoktemp = "public/render/valtozasoktemp.xml";
	var $temputvonal = "public/render/temp";
	var $elokepmeret = "120x90";
	var $lehetosegek = 5;
	
	
	function Render(){
		parent::Controller();
		$this->load->model('mrender');
		
		define( 'RENDSZER', "o");
		define( 'MEHET', false);
		
	/*	if( $_SERVER['REMOTE_ADDR'] == "84.3.52.156" && strstr($_SERVER['HTTP_USER_AGENT'],"o") ){
			define( 'KUPONKERES', true);
		}else
			define( 'KUPONKERES', false);*/
	}
	
	function index(){
		
	}
	
	function valaszt(){
		$hibavoltekorabban = $this->phpsession->get('feltoltesbelepesihiba');
		$hibavoltekorabbanemail = $this->phpsession->get('feltoltesbelepesihibaemail');
		if( $hibavoltekorabban == "hfvj" ){
			$error = "Hibás név vagy jelszó";
			$data["error"] = $error;
			$data["email"] = $hibavoltekorabbanemail;
			unset($_SESSION['default']['feltoltesbelepesihiba']);
			unset($_SESSION['default']['feltoltesbelepesihibaemail']);
		}
		
		
		
		if ( $this->cookieupdate() || $this->uri->segment(3) > 1 ){
			
			// sajat kep-e ellenorzes
			if( !isset($_SESSION['webrender']['guid']) || $_SESSION['webrender']['guid'] == "" )
				$guid = $this->uri->segment(4);
			else
				$guid = $_SESSION['webrender']['guid'];
			
			$query = " SELECT user_id FROM `cadline2`.`render_img` WHERE guid = '".$guid."' AND `deleted` = '0'";
			$query = $this->db->query($query)->row();
			$query2 = " SELECT login FROM `cadline2`.`forum_user` WHERE user_id = '".$query->user_id."'";
			$query2 = $this->db->query($query2)->row();
			
			$engedelyezve = array('eszter','sztikee@gmail.com','bodnart@cadline.hu','illes');
			
			if( $query2->login != $_COOKIE['azonosito'] && !in_array($_COOKIE['azonosito'],$engedelyezve) ){
				print "nem saját kép ..";//redirect('forum/renderek'); // ???
			}
			
			
			$data['style'] = array('style_2011','fancybox2','forum_2011');
			$data['javascript'] = array('fancybox/jquery-1.3.2.min','fancybox/jquery.easing.1.3','fancybox/jquery.fancybox-1.2.1.pack','fancybox/fancy');
			$data['tartalom'] = 'render/valaszt';
			
			
		//	if( $_SERVER['REMOTE_ADDR'] == $this->irodaip && strstr($_SERVER['HTTP_USER_AGENT'],RENDSZER) ){
				if($this->uri->segment(3) > 1)
					$data['tartalom'] = 'render/lehetosegek';
		//	}
			
			
			$lepes = $this->uri->segment(3);
			switch($lepes){
				case '0':
					$this->valaszt0($data);
					break;
				case '1':
					$this->valaszt1($data);
					break;
				case '2':
					$this->valaszt2($data);
					break;
				case '3':
					$this->valaszt3($data);
					break;
				case '4':
					$this->valaszt4($data);
					break;
				default:
					$this->valaszt0($data);
			}
			
		}
		else{
			$data['tartalom'] = 'collection/login';
			$data['javascript'] = array('objfeltoltes_2');
			$data['style'] = array('temp/style_interior');
			if(isset($_COOKIE['azonosito']))
				$data['email'] = $_COOKIE['azonosito'];
			
			$data['render'] = 'igen';
			
			$this->load->view('site_upload',$data);
		}
	}
	
	
	function valaszt0($data){
		$_SESSION['webrender']['guid'] = $this->uri->segment(4);
		
		$guid = $this->uri->segment(4);
		$lehetosegek = array($guid);
		$data['lehetosegek'] = $lehetosegek;
		$data['kov'] = 1;
		
		$data['style'] = array('style_2011');
		$data['tartalom'] = 'render/valaszt';
		
		$this->load->view('site_upload',$data);
	}
	
	// képváltozatok átmásolása a temp-be, ezek megjelenítése és kiválasztás
	function valaszt1($data){
		include('application/helpers/myimg_helper.php');
		
		$_SESSION['webrender']['guid'] = $this->uri->segment(4);
		$_SESSION['webrender']['aktiv'][0] = $this->uri->segment(4);
		$guid = $this->uri->segment(4);
		
		
		// saját guid-e
		$render = $this->db->query(" SELECT * FROM render_img WHERE guid = '".$guid."'")->row();
		$user = $this->db->query(" SELECT login FROM forum_user WHERE user_id = '".$render->user_id."'")->row();
		if( $user->login != $_COOKIE['azonosito'] ){
			// nem sajat render, vissza a fórumba
			
			$record->osztaly = "render";
			$record->fv = "valaszt1";
			$record->hiba = "Nem sajat render: ".$guid."\n".$_COOKIE['azonosito'];
			$record->datum = date('Y-m-d H:i:s',time());
			$this->db->insert('a_hibak',$record);
			
			redirect("forum/renderek");
		}
		
		
		$lehetosegek = array();
		$mappa = '/home/vendeg/render/eredmeny/';
		$masolnivalok = scandir($mappa);
		foreach($masolnivalok as $fajl){
			if( substr($fajl,0,36) == $guid && substr($fajl,36,4) != ".bmp" ){
				$hovamasolja = 'public/render/temp/'.$fajl;
				if( copy($mappa.$fajl, $hovamasolja) ){
					$t = array($fajl,substr($fajl,37,-4));
					$lehetosegek[] = $t;
					
					$_SESSION['webrender']['spk'][] = substr($fajl,37,-4);
					
					
					if( $render->infok != "" ){
						$adatok = json_decode($render->infok, true);
						
						if( $adatok['v2'] != "" && is_numeric($adatok['v2']) )
							fenyerosh($hovamasolja,$hovamasolja,$adatok['v2'],true);
						
						if( $adatok['v3'] != "" && is_numeric($adatok['v3']) )
							kontrasztsh($hovamasolja,$hovamasolja,$adatok['v3'],true);
					}
					
					$elo = 'public/render/temp/elo_'.$fajl;
					meretezsh('public/render/temp/'.$fajl,$elo,$this->elokepmeret);
				}
			}
		}
		
		$_SESSION['webrender']['lehetosegek'][1] = $lehetosegek;
		unset($_SESSION['webrender']['lehetosegek'][2]);
		unset($_SESSION['webrender']['lehetosegek'][3]);
		usort($_SESSION['webrender']['lehetosegek'][1], 'hasonlito');
		
		if(!isset( $_SESSION['webrender']['v1'] ))
			$_SESSION['webrender']['aktiv'][1] = 250;
		else
			$_SESSION['webrender']['aktiv'][1] = $_SESSION['webrender']['v1'];
		$data['lehetosegek'] = $lehetosegek;
		$data['guid'] = $this->uri->segment(4);
		$data['kov'] = 2;
		
	//	$data['tartalom'] = 'render/valaszt';
		
		$this->load->view('site_upload',$data);
	}
	
	// az 1. lépésnél kiválasztott kép különböző fényerőkkel és kiválasztás
	function valaszt2($data){
		$this->ervenyeskep(&$guid);
		
		$valamiertek = $this->uri->segment(4);
		if( isset($_SESSION['webrender']['v1']) && $_SESSION['webrender']['v1'] != $this->uri->segment(4) ){
			unset($_SESSION['webrender']['v2']);
			unset($_SESSION['webrender']['v3']);
		}
		$_SESSION['webrender']['v1'] = $this->uri->segment(4);
		$_SESSION['webrender']['aktiv'][1] = $_SESSION['webrender']['v1'];
		
		// valamelyik szelso kepet valasztotta-e
		if( $_SESSION['webrender']['v1'] == min($_SESSION['webrender']['spk']) )
			$_SESSION['webrender']['h'] = "a";
		elseif( $_SESSION['webrender']['v1'] == max($_SESSION['webrender']['spk']) )
			$_SESSION['webrender']['h'] = "f";
		else
			$_SESSION['webrender']['h'] = "k";
		
		
		$lehetosegek = array();
		
		// kul. fenyeros kepek
		include('application/helpers/myimg_helper.php');
		$ext = ".bmp";
		$file = $this->temputvonal."/".$guid."_".$valamiertek.$ext;
		for($i=0;$i<$this->lehetosegek;$i++){
			$value = ($i-((int)($this->lehetosegek/2)))*15;
			$newfile = $this->temputvonal."/".$guid."_".$valamiertek."_".$value.$ext;
			$elo = $this->temputvonal."/elo_".$guid."_".$valamiertek."_".$value.$ext;
		//	fenyero($file,$newfile,$value);
			fenyerosh($file,$newfile,$value);
			meretezsh($newfile,$elo,$this->elokepmeret);
			$t = array(basename($newfile),$value);
			$lehetosegek[] = $t;
		}
		
		
		$_SESSION['webrender']['lehetosegek'][2] = $lehetosegek;
		unset($_SESSION['webrender']['lehetosegek'][3]);
		
		
		if(!isset( $_SESSION['webrender']['v2'] ))
			$_SESSION['webrender']['aktiv'][2] = 0;
		else
			$_SESSION['webrender']['aktiv'][2] = $_SESSION['webrender']['v2'];
		$data['lehetosegek'] = $lehetosegek;
		$data['kov'] = 3;
		
	//	$data['tartalom'] = 'render/lehetosegek';
		
	//	if( $_SERVER['REMOTE_ADDR'] == $this->irodaip && strstr($_SERVER['HTTP_USER_AGENT'],RENDSZER) )
		$this->load->view('site_rss',$data);
	//	else
	//	$this->load->view('site_upload',$data);
	}
	
	// az 2. lépésnél kiválasztott kép különböző kontrasztokkal és kiválasztás
	function valaszt3($data){
		$this->ervenyeskep(&$guid);
		
		$fenyero = $this->uri->segment(4);
		
		if( isset($_SESSION['webrender']['v2']) && $_SESSION['webrender']['v2'] != $this->uri->segment(4) ){
			unset($_SESSION['webrender']['v3']);
		}
		
		$_SESSION['webrender']['v2'] = $this->uri->segment(4);
		$_SESSION['webrender']['aktiv'][2] = $_SESSION['webrender']['v2'];
		
		$lehetosegek = array();
		
		// kul. kontrasztos kepek
		include('application/helpers/myimg_helper.php');
		$ext = ".bmp";
		$file = $this->temputvonal."/".$guid."_".$_SESSION['webrender']['v1']."_".$_SESSION['webrender']['v2'].$ext;
		$re1 = $re2 = $re3 = "";
		for($i=0;$i<$this->lehetosegek;$i++){
			$value = ($i-(int)($this->lehetosegek/2))*2;
			$value2 = ($i-(int)($this->lehetosegek/2))*5;
			$value3 = ($i-(int)($this->lehetosegek/2))*4;
			$newfile = $this->temputvonal."/".$guid."_".$_SESSION['webrender']['v1']."_".$_SESSION['webrender']['v2']."_".$value.$ext;
			$elo = $this->temputvonal."/elo_".$guid."_".$_SESSION['webrender']['v1']."_".$_SESSION['webrender']['v2']."_".$value.$ext;
		//	kontraszt($file,$newfile,$value);
			$re1 .= kontrasztsh($file,$newfile,$value);
			meretezsh($newfile,$elo,$this->elokepmeret);
			
			$t = array(basename($newfile),$value);
			$lehetosegek[] = $t;
			
			
			/*
			$elo2 = $this->temputvonal."/elo_".$guid."_".$_SESSION['webrender']['v1']."_".$_SESSION['webrender']['v2']."_".$value.$ext."-v2";
			$re2 .= kontrasztsh2($file,$newfile."-v2",$value2);
			meretezsh($newfile."-v2",$elo2,$this->elokepmeret);
			
			$t2 = array(basename($newfile)."-v2",$value2);
			$lehetosegekv2[] = $t2;
			$data['lehetosegekv2'] = $lehetosegekv2;
			*/
			
			
			/*
			$elo3 = $this->temputvonal."/elo_".$guid."_".$_SESSION['webrender']['v1']."_".$_SESSION['webrender']['v2']."_".$value.$ext."-v3";
			$re3 .= kontrasztsh3($file,$newfile."-v3",$value3);
			meretezsh($newfile."-v3",$elo3,$this->elokepmeret);
			
			$t3 = array(basename($newfile)."-v3",$value3);
			$lehetosegekv3[] = $t3;
			$data['lehetosegekv3'] = $lehetosegekv3;
			*/
		}
		
		$_SESSION['webrender']['lehetosegek'][3] = $lehetosegek;
		
//		print $re1."<br>".$re3;
		
		if(!isset( $_SESSION['webrender']['v3'] ))
			$_SESSION['webrender']['aktiv'][3] = 0;
		else
			$_SESSION['webrender']['aktiv'][3] = $_SESSION['webrender']['v3'];
		$data['lehetosegek'] = $lehetosegek;
		$data['kov'] = 4;
		
	//	if( $_SERVER['REMOTE_ADDR'] == $this->irodaip && strstr($_SERVER['HTTP_USER_AGENT'],RENDSZER) )
		$this->load->view('site_rss',$data);
	//	else
	//	$this->load->view('site_upload',$data);
	}
	
	// kivalasztott kep megerositese
	function valaszt4($data){
		$this->ervenyeskep(&$guid);
		
		$kontraszt = $this->uri->segment(4);
		$_SESSION['webrender']['v3'] = $this->uri->segment(4);
		
		
		$data['valasztott'] = $this->temputvonal."/".$guid."_".$_SESSION['webrender']['v1']."_".$_SESSION['webrender']['v2']."_".$_SESSION['webrender']['v3'].".bmp";
		
		$data['tartalom'] = 'render/valasztas';
		
		$this->load->view('site_upload',$data);
	}
	
	// kivalasztott kep mentese (+takaritas)
	function valasztvege(){
		include('application/helpers/myimg_helper.php');
		
		if( !isset($_SESSION['webrender']['guid']) || $_SESSION['webrender']['guid'] == "" )
			redirect("forum/renderek");
		else
			$guid = $_SESSION['webrender']['guid'];
		
		// adatok elmentése
		$eddigiinfo = $this->db->query(" SELECT * FROM render_img WHERE guid = '".$guid."'")->row();
		if($eddigiinfo->infok == "")
			$_SESSION['webrender']['i'] = 1;
		else{
			$adatok = json_decode($eddigiinfo->infok, true);
			$_SESSION['webrender']['i'] = ((int)$adatok['i']+1);
		}
		
		$valasztott = $this->temputvonal."/".$guid."_".$_SESSION['webrender']['v1']."_".$_SESSION['webrender']['v2']."_".$_SESSION['webrender']['v3'].".bmp";
		
		$eddigikep = $this->mrender->get_rowByguid($guid);
		$elokep = substr($eddigikep->keszkep,0,-1*(strlen(basename($eddigikep->keszkep))))."elo_".substr($eddigikep->keszkep,-1*(strlen(basename($eddigikep->keszkep))));
		
		exec('cp /var/www/hosting/cadline.hu/www/'.$eddigikep->keszkep.'  /var/www/hosting/cadline.hu/www/public/render/torolve/'.$eddigikep->user_id."__".basename($eddigikep->keszkep));
		exec('mv /var/www/hosting/cadline.hu/www/'.$valasztott.'  /var/www/hosting/cadline.hu/www/'.$eddigikep->keszkep);
		
		meretezsh('/var/www/hosting/cadline.hu/www/'.$eddigikep->keszkep,'/var/www/hosting/cadline.hu/www/'.$elokep,'200x150',true);
		
		
		unset($_SESSION['webrender']['guid']);
		unset($_SESSION['webrender']['spk']);
		unset($_SESSION['webrender']['lehetosegek']);
		unset($_SESSION['webrender']['aktiv']);
		$info = json_encode($_SESSION['webrender']);
		$this->db->update('render_img',array('infok'=>$info,/*'allapot'=>4,'datum'=>time()*/),"guid = '".$guid."'");
		
		unset($_SESSION['webrender']);
		
		// takarítás
		$kepek = 'public/render/temp/';
		$handle = opendir($kepek);
		while($file = readdir($handle)){
			if( substr($file,0,36) == $guid || substr($file,4,36) == $guid ){
				chmod($kepek.$file, 777);
				unlink($kepek.$file);
			}
		}
		
		$kepek = '/home/vendeg/render/eredmeny/';
		$handle = opendir($kepek);
		while($file = readdir($handle)){
			if( substr($file,0,36) == $guid ){
				chmod($kepek.$file, 777);
				unlink($kepek.$file);
			}
		}
		
		$record->osztaly = "render";
		$record->fv = "valasztvege";
		$record->hiba = "Kiválasztva: ".$guid."\n".$info;
		$record->datum = date('Y-m-d H:i:s',time());
		$this->db->insert('a_hibak',$record);
		
		$uzenet = array(6);
		$this->phpsession->save('fuzenet',$uzenet);
		redirect('forum/renderek');
	//	print "Elmentve ...<br><a href=\"/forum/renderek\">Vissza a renderekhez.</a>";
	}
	
	function ervenyeskep(&$guid){
		
		if( !isset($_SESSION['webrender']['guid']) || $_SESSION['webrender']['guid'] == "" ){
			//redirect("forum/renderek");
			
			if( $this->uri->segment(3) == 1 )
				redirect("forum/renderek");
			
			print "Nincs belépve!<pre>";//redirect("forum/renderek");
			print_r($_SESSION['webrender']);
			print "</pre>".$this->uri->segment(4);
			
			print "<strong>Lejárt az idő, <a href=\"/forum/render\">Vissza</a></strong>";
		}
		else
			$guid = $_SESSION['webrender']['guid'];
	}
	
	
	function frissit(){
		$this->load->model('mclusers');
		$this->load->model('mforum');
		
		$mit = $this->uri->segment(3);
		$pguid = $this->uri->segment(3);
		$allapot = $this->uri->segment(4);
		$statusz = $this->uri->segment(5);
		
		if(!is_numeric($statusz))
			$statusz = 0;
		
		$user = $this->phpsession->get('fuser');
		$uid = $user['user_id'];
		
		$arender = $this->mrender->get_rowByguid($pguid);
		
		$nUserID = $this->mforum->get_nUserID2($uid);
		
		if( !$nUserID ){
			$emailcim = $this->mforum->get_user_alap($user['name']);
			$emailcim = $emailcim[0]->email;
			$nUserID = $this->mforum->get_nUserID30($emailcim);
		}
		
		
		$infoguid = " guid: ".$arender->guid;
		
		if( true || $_SERVER['REMOTE_ADDR'] == $this->irodaip ){
			$uid = $arender->user_id;
			$nUserID = $this->mforum->get_nUserID2($uid);
			
			if( !$nUserID ){
				$login = $this->mforum->get_login($uid);
				$emailcim = $this->mforum->get_user_alap($login);
				$emailcim = $emailcim[0]->email;
				$nUserID = $this->mforum->get_nUserID30($emailcim);
			}
			
			
			$this->mrender->frissit($arender->render_id,$allapot,$statusz);
			if($allapot == "2" && $arender->allapot != "2"){
				$this->db->update('render_img',array('kezdet'=>time()),"render_id = '".$arender->render_id."'");
			}
			elseif($allapot == 30 && hun() && $nUserID > 0 ){
				$info = date('Y-m-d H:i',time()).": Renderelés megszakítás.".$infoguid;
				$this->mclusers->addEvent($nUserID, 116, $info);
			}elseif($allapot == 1 && hun() && $nUserID > 0 ){
				
				$this->db->update('render_img',array('vege'=>time()),"render_id = '".$arender->render_id."'");
				
			//	$info = date('Y-m-d H:i',time()).": Renderelés kész.".$infoguid;
			//	$this->mclusers->addEvent($nUserID, 116, $info);
			}
		}
		
		
		
		
		$record->osztaly = "render";
		$record->fv = "frissit";
		$record->hiba = "statusz: ".$statusz.", guid: ".$pguid;
		$record->datum = date('Y-m-d H:i:s',time());
		$this->db->insert('a_hibak',$record);
	}
	
	function upload(){
		//működik-e a szerver (volt-e az utóbbi 3 percben üzenet tőle)
		$ftemp = fopen( "public/render/utolso.txt", "r" );
		$utolsoido = fgets( $ftemp, 1024);
		fclose($ftemp);
		if($utolsoido == ""){
			$record->osztaly = "render";
			$record->fv = "upload";
			$record->hiba = "Nem olvastam az utolso.txt-ből!! ";
			$record->datum = date('Y-m-d H:i:s',time());
			$this->db->insert('a_hibak',$record);
			exec('echo "nem volt utolso.txt a rendernel, user: '.$_COOKIE['azonosito'].'" | mail -s "Render feltöltés hiba" sztikee@gmail.com');
		}
		if( strtotime($utolsoido) < (time()-180) ){
			$record->osztaly = "render";
			$record->fv = "upload";
			$record->hiba = "Legalább 3 perce nem megy a szerver, hiba a föltéskor!\n user: ".$_COOKIE['azonosito'];
			$record->datum = date('Y-m-d H:i:s',time());
			$this->db->insert('a_hibak',$record);
			
			// email Tamásnak
		//	exec('echo "Nem megy a render szerver, utolso kapott idopont: '.$utolsoido.'" | mail -s "Render feltöltés hiba" -c bodnart@invitel.hu bodnart@cadline.hu');
			
			$data['tartalom'] = 'forum/kiir';
			$data['style'] = array('temp/style_interior');
			$data['email'] = $_COOKIE['azonosito'];
			
			$data['info'] = "<div style='text-align: center; color: #fff; font-size: 1.3em;'>".lang('render_nemmegy')."</div>";
			
			$this->load->view('site_upload',$data);
			return;
		}
		
		
		// volt-e belepesi hiba
		$hibavoltekorabban = $this->phpsession->get('feltoltesbelepesihiba');
		$hibavoltekorabbanemail = $this->phpsession->get('feltoltesbelepesihibaemail');
		if( $hibavoltekorabban == "hfvj" ){
			$error = "Hibás név vagy jelszó";
			$data["error"] = $error;
			$data["email"] = $hibavoltekorabbanemail;
			unset($_SESSION['default']['feltoltesbelepesihiba']);
			unset($_SESSION['default']['feltoltesbelepesihibaemail']);
		}
		
		if($this->uri->segment(3)!=""){
			$this->load->model('mforum');
			
			$data["fajl"] = $this->uri->segment(3);
			$data["eredeti"] = $this->uri->segment(3);
			$data['csik']=array('home');
			
			
			if( !file_exists('/home/vendeg/render/'.$this->uri->segment(3).".dat") ){
				$data["error2"] = "Hiba a fájl feltöltésekor";
			}
			
			$fajlnev = array(
				0 => $this->uri->segment(3)
			);
			
			if( preg_grep("/^(95|96|97|99)/", $fajlnev) ){
				$data["probaverzio"] = true;
			}
			
			
			if ( $this->cookieupdate() ){
				
				$honnan = '/home/vendeg/render/kiskep/'.$this->uri->segment(3).'.jpg';
				$kepnev = time()."_".$this->uri->segment(3).".jpg";
				$ide= "public/img/render/kiskepek/".$kepnev;
				if( copy($honnan,$ide) ){
					$data["kepalap"] = $ide;
				}else{
					$data["kepalap"] = false;
				}
				
				
				$data['projektnev'] = $this->uri->segment(4);
				
				$data['email'] = $_COOKIE['azonosito'];
				$data['belepve'] = true;
				$belepve = $_COOKIE['azonosito'];
				
				$user_id = $this->mforum->get_userid($_COOKIE['azonosito']);
				$data['user_id'] = $user_id;
				
				$data['javascript'] = array('renderfeltoltes','fancybox/jquery-1.3.2.min','fancybox/jquery.easing.1.3','fancybox/jquery.fancybox-1.2.1.pack','fancybox/fancy');
				$data['style'] = array('temp/style_interior','fancybox2');
				$data['tartalom']='render/upload';
				
				$this->load->view('site_upload',$data);
				
			}else{
				$data['tartalom'] = 'collection/login';
				$data['javascript'] = array('objfeltoltes_2');
				$data['style'] = array('temp/style_interior');
				if(isset($_COOKIE['azonosito']))
					$data['email'] = $_COOKIE['azonosito'];
				
				$data['render'] = 'igen';
				
				$this->load->view('site_upload',$data);
			}
			
		}else{
			redirect('render');
		}
		
	}
	
	function submit(){
		$this->load->model('mforum');
		
		$lang = $this->muser->user['lang'];
		$tempFolder = "/home/vendeg/render/"; //ide másolja a program a fájlt
		$error = "";
		$user_id = $this->uri->segment(3);
		$email = "";
		$render_id = "";
		
		// Tényleges e-mail cím
		if ( $this->cookieupdate() ){
			$emailcim = $_COOKIE['azonosito'];
			
			// ha nem email cim, lekerdezzuk az emailt
			if(!preg_match('/^([a-zA-Z0-9_\.])+([\-]?[a-zA-Z0-9]+)*@([a-zA-Z0-9_\-\.])+(\.[a-zA-Z]{2,4})$/i', $emailcim)){
				$qaf = $this->mforum->get_user_alap($emailcim);
				$emailcim = $qaf[0]->email;
				
				
				$qaf = $this->mforum->get_user_szem($_COOKIE['azonosito']);
				$emailcim = $qaf[0]->email;
				
				if(!$emailcim){
					$qaf = $this->mforum->get_user_alap($_COOKIE['azonosito']);
					$emailcim = $qaf[0]->email;
				}
				
			}
			
		}else{
			$error = "Nincs belejelentkeve!<br />";
			$emailcim = false;
			$user_id = 0;
		}
		
		// kep atmeretezese 200x150-re
		$ujkep33 = $this->input->post('kepalap');
		
		if(file_exists($ujkep33) && !$error){
			$config['image_library'] = 'gd2';
			$config['full_src_path'] = $this->input->post('kepalap');
			$config['wm_overlay_path'] = 'public/img/forum/stilus/watermark.png';
			$config['full_dst_path'] = $config['full_src_path'];
			$config['dynamic_output'] = FALSE;
			$this->load->library('image_lib', $config);
			
			// Get new dimensions
			list($width, $height) = getimagesize($ujkep33);
			$new_width = 200;
			$new_height = 150;
			
			// Resample
			$image_p = imagecreatetruecolor($new_width, $new_height);
			$image = imagecreatefromjpeg($ujkep33);
			imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
			
			// Output1 - átméretezés
			imagejpeg($image_p, $ujkep33, 100);
			
			
			// Output2 - vízjel
			$this->image_lib->overlay_watermark($ujkep33);
			
		}else{
			$error .= " nincs előnézeti kép, vagy mar hiba volt korabban<br />\nhiba: '".$error."'\n";
		}
		
		
		
		// Felvétel az adatbázisba
		if(!$error){
			$ujkep->user_id = $user_id;
			$ujkep->guid = $this->input->post("guid");
			$ujkep->email = $emailcim;
			$ujkep->nyelv = $lang;
			$ujkep->render_name = /*base64_decode(*/$this->input->post('projektnev')/*)*/;
			$ujkep->kiskep = $this->input->post('kepalap');
			$ujkep->allapot = 0;
			$ujkep->datum = time();
			$ujkep->szamla = $this->input->post('kupon');
			$ujkep->teszt = $this->input->post('free');
			
			$this->mrender->newrender($ujkep);
			
			$this->load->model('mclusers');
			$nUserID = $this->mforum->get_nUserID2($user_id);
			//$nUserID = $this->mforum->get_nUserID30($emailcim);
			if($nUserID && hun()){
				$info = date('Y-m-d H:i',time()).": Renderelés indítás, ";
				if( $this->input->post('free') == "2" ){
					$info .= "teljes render.";
					$info .= " Kupon kód: ".$this->input->post('kupon');
				}
				elseif( $this->input->post('free') == "1" )
					$info .= "teszt render.";
				$info .= " guid: ".$ujkep->guid;
				$this->mclusers->addEvent($nUserID, 116, $info);
			}
			
			
			$record->osztaly = "render";
			$record->fv = "submit";
			$record->hiba = "hozza fogok adni: ".$this->input->post("guid");
			$record->datum = date('Y-m-d H:i:s',time());
			$this->db->insert('a_hibak',$record);
			
			$render->guid = $ujkep->guid;
			$render->user_id = $ujkep->user_id;
			$render->allapot = "0";
			$render->teszt = $ujkep->teszt;
		//	$this->hozzaad($render);
		}
		
		if(!$user_id)
			$error .= " nincs felhasznalo<br />";
		if(!$emailcim)
			$error .= " nincs email cim<br />";
		
		
		if($error){
			if(hun())
				$data['page'] = $this->mpages->get_row(567);
			else
				$data['page'] = $this->mpages->get_row(569);
			$data['error'] = $error;
			
			$record->osztaly = "render";
			$record->fv = "submit";
			$record->hiba = "HIBA: ".$error." ,\n guid: ".$this->input->post("guid").", \n nyelv= ".$lang;
			$record->datum = date('Y-m-d H:i:s',time());
			$this->db->insert('a_hibak',$record);
		}
		
		print "email: ".$emailcim.", id: ".$user_id;
		
	}
	
	// teljes render kérés a fórumból
	function submit2(){
		$user = $this->phpsession->get('fuser');
		if(!$user)
			$user = $this->phpsession->get('feltolto');
		
		if(!$user)
			redirect("forum");
		
	//	$render_id = $this->input->post('renderid');
		if( isset($_POST['renderid']) )
			$render_id = $_POST['renderid'];
		else
			$render_id = $this->uri->segment(3);
		
		$render = $this->mrender->get_row($render_id);
		
		if( is_numeric($render_id) && $render->user_id == $user['user_id'] ){
			
			$kupon = $this->input->post('kupon');
			
			// kupon kód ellenőrzése
			if( KUPONKERES === true ){
				$ered = $this->kupon_felhasznalas($kupon,1,1,$render);
				if( $ered !== true ){
					$this->phpsession->save('fhiba',$ered);
					
					redirect("forum/renderek");
				}
			}
			
			/*  ???    előző felülírása  ???  */
			$ujkep->allapot = '4';
			$ujkep->datum = time();
			$ujkep->teszt = '2';
			$ujkep->szamla = $kupon;
			
			$this->db->update('render_img',$ujkep,"render_id='".$render_id."'");
			
			// alternativak torlese a vendegbol
			$kepek = '/home/vendeg/render/eredmeny/';
			$handle = opendir($kepek);
			while($file = readdir($handle)){
				if( substr($file,0,36) == $render->guid ){
					chmod($kepek.$file, 777);
					unlink($kepek.$file);
				}
			}
			
			
			$record->osztaly = "render";
			$record->fv = "submit2";
			$record->hiba = "Teljes render: ".$render->guid;
			$record->datum = date('Y-m-d H:i:s',time());
			$this->db->insert('a_hibak',$record);
			
			$uzenet = array(5);
			$this->phpsession->save('fuzenet',$uzenet);
			
		}else{
			$uzenet = array(15);
			$this->phpsession->save('fhiba',$uzenet);
			
			$record->osztaly = "render";
			$record->fv = "submit2";
			$record->hiba = "Sikertelen teljes render: ".$render->guid."\n user: ".$user['user_id'];
			$record->datum = date('Y-m-d H:i:s',time());
			$this->db->insert('a_hibak',$record);
		}
		
		redirect("forum/renderek");
	}
	
	// renderelt kép elhelyezése
	function uploadrender(){	// kepnev kit. nélkül, guid
		$fajl = $this->uri->segment(3);
		$guid = $this->uri->segment(3);
		
		$render = $this->mrender->get_rowByguid($guid);
		$render_id = $render->render_id;
		
		if( is_numeric($render_id) ){
			if( $this->muser->user['lang'] == "hun" )
				$almappa = "hun";
			else
				$almappa = "eng";
			
			$elotag = substr(time(),-6);
			$fajluj = $elotag . "__" . $fajl . ".bmp";
			
			$uid = $render->user_id;
			
			//$helye = "public/forum-galeria/".$almappa."/".$uid;
			$helye =  "public/render/".$almappa."/".$uid;
			
			if(!is_dir($helye)){
				mkdir($helye,0644);
				chmod($helye,0777);
			}
			
			$honnan = "/home/vendeg/render/eredmeny/".$fajl. ".bmp";
			$hova = $helye."/".$fajluj;
			
			if( copy($honnan,$hova) ){
				
				$info = "atmasoltam<br />honnan: ".$honnan."<br />hova: ".$hova;
				$record->osztaly = "render";
				$record->fv = "uploadrender";
				$record->hiba = $info;
				$record->datum = date('Y-m-d H:i:s',time());
				$this->db->insert('a_hibak',$record);
				
				// renderekhez is pluszban betenni
				copy($honnan,"public/render/".$almappa."/000kozos/".$fajluj);
				
				
				// frissítés
				$this->load->model('mclusers');
				$this->load->model('mforum');
				
				$mit = $render->render_id;
				$allapot = 1;
				
				$user = $this->phpsession->get('fuser');
				$uid = $render->user_id;
				
				$login = $this->mforum->get_login($uid);
				
				$qaf = $this->mforum->get_user_szem($login);
				$emailcim = $qaf[0]->email;
				if(!$emailcim){
					$qaf = $this->mforum->get_user_alap($login);
					$emailcim = $qaf[0]->email;
				}
				
				$nUserID = $this->mforum->get_nUserID2($uid);
				
				if( !$nUserID )
					$nUserID = $this->mforum->get_nUserID30($emailcim);
				
				$ujrender->hely = $render->hely;
				$ujrender->allapot = "1";
				$ujrender->datum = $render->datum;
				$ujrender->keszkep = $hova;
				$ujrender->deleted = $render->deleted;
				$this->mrender->editrender($ujrender,$render->render_id);
				
				$info = date('Y-m-d H:i',time()).": Renderelés kész. guid: ".$render->guid;
				if($render->nyelv == "hun" && $nUserID)
					$this->mclusers->addEvent($nUserID, 116, $info);
				
				$meret = filesize($hova);
				
				/*  ertesites e-mailben  */
				$mail_data = array(
					'nev' => $post['nev'],
					'kepnev' => $hova,
					'ido' => date('Y-m-d H:i',time())
				);
				
				$this->load->library('email');
				$this->email->initialize();
				$this->email->from('info@cadline.hu', 'Cadline Weboldal');
				$this->email->subject('ARCHLine.XP - Render');
				$this->email->message($this->load->view('mail/render_kesz', $mail_data, true));
				
				$this->email->send($emailcim, 'smtp');
				$this->email->clear();
				/*  end ertesites e-mailben  */
			}else{
				$info = "fajl masolas hiba<br />honnan: ".$honnan."<br />hova: ".$hova;
				$record->osztaly = "render";
				$record->fv = "uploadrender";
				$record->hiba = $info;
				$record->datum = date('Y-m-d H:i:s',time());
				$this->db->insert('a_hibak',$record);
			}
		}
	}
	
	// hozzaad vagy atir ha egyezik a guid
	function hozzaad($mit,$hova=""){
		
		if(!$hova)
			$fajlnev = $this->valtozasok;
		else
			$fajlnev = $hova;
		
		// rendes tartalom
		$fnormal = fopen( $fajlnev, "r" );
		while($x = fgets( $fnormal, 1024)){
			$ntartalom .= $x;
		}
		fclose($fnormal);
		
		$xml = simplexml_load_file('http://cadline.hu/'.$hova);
		
		$fajlba = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<renderek>\n";
		
		$volte = false;
		$xml2 = $xml->render;
		if($xml['render'])
			$uresxml = "nem volt ures az xml: ".$ntartalom;
		else
			$uresxml = "ures volt az xml: ".$ntartalom;
		
		foreach($xml as $elem){
			if( $elem['guid'] == $mit->guid ){
				$volte = true;
				foreach($mit as $k=>$v){
					$elem[$k] = $v;
				}
			}
			
			$fajlba .= "\t<render>\n";
			foreach($elem as $k2=>$v2){
				if($k2 == "teszt" && $v2 == "1"){
					$fajlba .= "\t\t<width>800</width>\n\t\t<height>600</height>\n";
					$fajlba .= "\t\t<passlimit>10</passlimit>\n\t\t<timelimit>5</timelimit>\n";
				}elseif($k2 == "teszt" && $v2 == "2"){
					$fajlba .= "\t\t<width>2048</width>\n\t\t<height>1536</height>\n";
					$fajlba .= "\t\t<passlimit>100</passlimit>\n\t\t<timelimit>30</timelimit>\n";
				}elseif( $k2 != "width" && $k2 != "height" && $k2 != "passlimit" && $k2 != "timelimit" )
					$fajlba .= "\t\t<".$k2.">".$v2."</".$k2.">\n";
			}
			$fajlba .= "\t</render>\n";
		}
		if(!$volte){
			$fajlba .= "\t<render>\n";
			foreach($mit as $k=>$v){
				if($k == "teszt" && $v == "1"){
					$fajlba .= "\t\t<width>800</width>\n\t\t<height>600</height>\n";
					$fajlba .= "\t\t<passlimit>10</passlimit>\n\t\t<timelimit>5</timelimit>\n";
				}elseif($k == "teszt" && $v == "2"){
					$fajlba .= "\t\t<width>2048</width>\n\t\t<height>1536</height>\n";
					$fajlba .= "\t\t<passlimit>100</passlimit>\n\t\t<timelimit>30</timelimit>\n";
				}else
					$fajlba .= "\t\t<".$k.">".$v."</".$k.">\n";
			}
			$fajlba .= "\t</render>\n";
		}
		
		$fajlba .= "</renderek>";
		
		
		chmod($fajlnev, 0777);
		$fa = fopen( $fajlnev, "w" ) or die("$fajlnev nem nyithato meg");
		fwrite ( $fa, $fajlba);
		fclose( $fa );
		
		$ujtenyleges = '';
		// rendes tartalom
		$fnormal2 = fopen( $this->valtozasok, "r" );
		while($x = fgets( $fnormal2, 1024)){
			$ujtenyleges .= $x;
		}
		fclose($fnormal2);
		
	}
	
	
	// jelenlegi valtozasok kiirasa + kapott fajlbol a valtozasok elmentese
	function valtozasok(){
		$this->load->model('mclusers');
		$this->load->model('mforum');
		
		// feljegyzi a meghívás idejét
		$utolsotxt = "public/render/utolso.txt";
		$fa = fopen( $utolsotxt, "w" ) or die("$utolsotxt nem nyithato meg");
		fwrite ( $fa, date('Y-m-d H:i:s'));
		fclose( $fa );
		
		
		// uriteskor a temp torlese
		if(  $this->uri->segment(3) == "urit" ){
			$this->urites();
		}elseif( $this->uri->segment(3) == "toroltek" ){
			$torolek = $this->mrender->get_alldeleted();
			foreach($torolek as $elem)
				print $elem->guid.";";
			
			return;
		}
		else{
			$info = "";
			$xxx = "";
			
			
			// az 1-es és 2-es állapotúakon és 1 óránál nem régebbieken kívül mind kiírása
			$renderek = $this->mrender->get_allspec();
			header ( "Content-type: text/xml" );
			$ki = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<renderek>\n";
			if(is_array($renderek)){
			$valttxt = "public/render/valtozasok.txt";
			$fa2 = fopen( $valttxt, "w" ) or die("$valttxt nem nyithato meg");
			
			foreach($renderek as $render){
				
				$ki .= "\t<render>\n";
				$ki .= "\t\t<guid>".$render->guid."</guid>\n";
				$ki .= "\t\t<user_id>".$render->user_id."</user_id>\n";
				$ki .= "\t\t<allapot>".$render->allapot."</allapot>\n";
				
				if($render->teszt == "1"){
					$ki .= "\t\t<width>800</width>\n\t\t<height>600</height>\n";
					$ki .= "\t\t<passlimit>10</passlimit>\n\t\t<timelimit>10</timelimit>\n";
				}elseif($render->teszt == "2"){
			//		$ki .= "\t\t<width>2048</width>\n\t\t<height>1536</height>\n"; // zoli
					$ki .= "\t\t<passlimit>100</passlimit>\n\t\t<timelimit>180</timelimit>\n";
				}
				
				if( $render->infok != "" ){
					
					$adatok = json_decode($render->infok, true);
					
					if( $adatok['v1'] != "" && $adatok['i'] != "" ){
						$o = $adatok['i']*8;
						$x = $adatok['v1'];
						
						if( $adatok['h'] == "f" )
							$ertekek = (int)($x*6/8).";".(int)($x*7/8).";".(int)($x*2).";".(int)($x*4);
						elseif( $adatok['h'] == "a" )
							$ertekek = (int)($x*0.5).";".(int)($x*0.75).";".(int)($x*1.25).";".(int)($x*1.5);
						else
							$ertekek = (int)($x*($o-2)/$o).";".(int)($x*($o-1)/$o).";".(int)($x*($o+2)/$o).";".(int)($x*($o+4)/$o);
						
						$ki .= "\t\t<ShutterSpeeds>".$ertekek."</ShutterSpeeds>\n";
						$ki .= "\t\t<ShutterSpeed>".$adatok['v1']."</ShutterSpeed>\n";
					}
					else{
						$ki .= "\t\t<ShutterSpeeds>60;125;500;1000</ShutterSpeeds>\n";
						$ki .= "\t\t<ShutterSpeed>250</ShutterSpeed>\n";
					}
					
					if( $adatok['v2'] != "" )
						$ki .= "\t\t<Fenyero>".$adatok['v2']."</Fenyero>\n";
					
					if( $adatok['v3'] != "" )
						$ki .= "\t\t<Kontraszt>".$adatok['v3']."</Kontraszt>\n";
					
					
				}else{
					$ki .= "\t\t<ShutterSpeeds>60;125;500;1000</ShutterSpeeds>\n";
					$ki .= "\t\t<ShutterSpeed>250</ShutterSpeed>\n";
				}
				
				if($render->deleted == "1")
					$ki .= "\t\t<deleted>true</deleted>\n";
				else
					$ki .= "\t\t<deleted>false</deleted>\n";
				
				
				$ki .= "\t</render>\n";
			}
			fclose( $fa2 );
			}
			$ki .= "</renderek>";
			
			// tényleges kiírás
			print $ki;
			
			
			// fajlbol frissitesek mentese
			$fajl = $this->uri->segment(3);
			$this->frissitesmentes($fajl);
			
		}
	}
	
	function urites(){
		// torli a temp-et és a rendesbol kiszedi azt ami a temp-ben is volt
		if( ($_SERVER['REMOTE_ADDR'] == $this->irodaip) && filesize($this->valtozasoktemp) > 0 ){
			
			$temp = $this->valtozasoktemp;
			$ttartalom = "";
			$ntartalom = "";
			$ttomb = array();
			$ntomb = array();
			// temp tartalom
			$ftemp = fopen( $temp, "r" );
			while($x = fgets( $ftemp, 1024)){
				if( (trim($x) != "<?xml version=\"1.0\" encoding=\"UTF-8\"?>") && (trim($x) != "<renderek>") && (trim($x) != "</renderek>") ){
					$ttartalom .= $x;
					$ttomb[] = $x;
					$ntomb[] = " ";
				}
			}
			fclose($ftemp);
			
			// rendes tartalom
			$fnormal = fopen( $fajlnev, "r" );
			while($x = fgets( $fnormal, 1024)){
				$ntartalom .= $x;
			}
			fclose($fnormal);
			
			// rendesbol a tempes resz kiszedese
			$ujrendes = str_replace($ttartalom,"",$ntartalom);
			
			// temp urites
			$ft = fopen( $temp, "w" );
			fwrite ( $ft, "");
			fclose( $ft );
			
			// uj rendes valtozasok
			$fn = fopen( $fajlnev, "w" );
			fwrite ( $fn, $ujrendes);
			fclose( $fn );
		}
	}
	
	
	
	// renderelt kép elhelyezése
	function uploadrender2($guid){	// kepnev kit. nélkül, guid
		
		$fajl = $guid;
		
		$render = $this->mrender->get_rowByguid($guid);
		$render_id = $render->render_id;
		
		if( is_numeric($render_id) ){
			if( $render->nyelv == "hun" )
				$almappa = "hun";
			else
				$almappa = "eng";
			
			$elotag = substr(time(),-6);
			$fajluj = $elotag . "__" . $fajl . ".bmp";
			
			$uid = $render->user_id;
			
			$helye =  "public/render/".$almappa."/".$uid;
			
			if(!is_dir($helye)){
				mkdir($helye,0644);
				chmod($helye,0777);
			}
			
			
			if( $render->infok != "" ){
				$adatok = json_decode($render->infok, true);
				if( $adatok['v1'] != "" && is_numeric($adatok['v1']) ){
					$sp = $adatok['v1'];
				}
				else
					$sp = 250;
			}else
				$sp = 250;
			
			if( file_exists("/home/vendeg/render/eredmeny/".$fajl."_".$sp.".bmp") )
				$honnan = "/home/vendeg/render/eredmeny/".$fajl."_".$sp.".bmp";
			else
				$honnan = "/home/vendeg/render/eredmeny/".$fajl.".bmp";
			
		//	$honnan = "/home/vendeg/render/eredmeny/".$fajl. ".bmp";
			$hova = $helye."/".$fajluj;
			
			if( copy($honnan,$hova) ){
				include('application/helpers/myimg_helper.php');
				
				// biztonsgági mentésekhez is pluszban betenni
				copy($honnan,"public/render/".$almappa."/000kozos/".$uid."__".$fajluj);
				
				// frissítés
				$this->load->model('mclusers');
				$this->load->model('mforum');
				
				
				if( file_exists($render->keszkep) && $render->keszkep != "" ){
					$honnan = '/var/www/hosting/cadline.hu/www/';
					$ide = '/var/www/hosting/cadline.hu/www/public/render/torolve/';
					exec('mv '.$honnan.$render->keszkep.' ' . $ide . $render->user_id . '__' . basename($render->keszkep));
				//	exec('mv '.$honnan.$render->kiskep.' '. $ide . $render->user_id . '__' . basename($render->kiskep));
				}
				
				// utolso beallitasokbol fenyero es kontraszt
				if( $render->infok != "" ){
					$adatok = json_decode($render->infok, true);
					
					if( $adatok['v2'] != "" && is_numeric($adatok['v2']) )
						fenyerosh($hova,$hova,$adatok['v2'],true);
					
					if( $adatok['v3'] != "" && is_numeric($adatok['v3']) )
						kontrasztsh($hova,$hova,$adatok['v3'],true);
				}
				
				$mit = $render->render_id;
				$allapot = 1;
				
				$uid = $render->user_id;
				
				$login = $this->mforum->get_login($uid);
				
				$qaf = $this->mforum->get_user_szem($login);
				$emailcim = $qaf[0]->email;
				if(!$emailcim){
					$qaf = $this->mforum->get_user_alap($login);
					$emailcim = $qaf[0]->email;
				}
				
				$ered22 = $this->mforum->get_row($uid);
				$nUserID = $ered22->nUserID;
				
				if( !$nUserID || !is_numeric($nUserID) )
					$nUserID = $this->mforum->get_nUserID2($uid);
				
				if( !$nUserID || !is_numeric($nUserID) )
					$nUserID = $this->mforum->get_nUserID30($emailcim);
				
				$ujrender->hely = $render->render_name;
				$ujrender->allapot = "1";
				$ujrender->datum = $render->datum;
				$ujrender->keszkep = $hova;
				$ujrender->deleted = $render->deleted;
				
				$this->db->update('render_img',array('allapot'=>"1",'keszkep'=>$hova),"render_id = '".$render->render_id."'");
				
				// Kupon felhasznalas ha volt
				if( $render->teszt == 2 && $render->szamla != "" && $render->szamla != "0" ){
					$ered = $this->kupon_felhasznalas($render->szamla, 2, 1, $render);
					if( $ered !== true ){
						$record->osztaly = "render";
						$record->fv = "uploadrender2";
						$record->hiba = "Hiba a kupon felhasznalasakor: ".$ered."\n".$render->szamla.", ".$render->guid;
						$record->datum = date('Y-m-d H:i:s',time());
						$this->db->insert('a_hibak',$record);
					}
				}
				
				$info = date('Y-m-d H:i',time()).": Renderelés kész. guid: ".$render->guid;
				if( $render->teszt == "2" )
					$info .= " , kupon: ".$render->szamla;
				
				if($render->nyelv == "hun" && $nUserID)
					$this->mclusers->addEvent($nUserID, 116, $info);
				if($render->nyelv == "eng" && $nUserID)
					$this->mclusers->addEvent($nUserID, 146, $info, 'eng');
				
				$meret = filesize($hova);
				
				
				/*  ertesites e-mailben  */
				$mail_data = array(
				//	'nev' => $post['nev'],
					'kepnev' => $hova,
					'guid' => $render->guid,
					'ido' => date('Y-m-d H:i',time())
				);
				
				$this->load->library('email');
				$this->email->initialize();
				$this->email->from('info@cadline.hu', 'Cadline Weboldal');
				$this->email->subject('ARCHLine.XP - Render');
				$this->email->message($this->load->view('mail/render_kesz', $mail_data, true));
				
				$mit = array("/public/");
				$mire = array("..");
				$hova2 =  preg_replace($mit, $mire, $hova);
		//		$this->email->addImage($hova2);
				
				$this->email->send($emailcim, 'smtp');
				$this->email->clear();
				/*  end ertesites e-mailben  */
				
				
				// elonezeti kep elkeszitese 200x150-ben
				kepmeretezesvagas($hova,$helye,"elo_".$fajluj);
				
				$record->osztaly = "render";
				$record->fv = "uploadrender2";
				$record->hiba = "kepmeretezesvagas UTAN, guid: ".$guid;
				$record->datum = date('Y-m-d H:i:s',time());
			//	$this->db->insert('a_hibak',$record);
			}else{
				$info = "fajl masolas hiba\nhonnan: ".$honnan."\nhova: ".$hova."\nguid: ".$guid." ,\n fajl: ".$fajl;
				$record->osztaly = "render";
				$record->fv = "uploadrender2";
				$record->hiba = $info;
				$record->datum = date('Y-m-d H:i:s',time());
				$this->db->insert('a_hibak',$record);
			}
		}else{
			$record->osztaly = "render";
			$record->fv = "uploadrender2";
			$record->hiba = "NINCS meg a render, guid: ".$guid;
			$record->datum = date('Y-m-d H:i:s',time());
			$this->db->insert('a_hibak',$record);
		}
		
		// takaritas
		$parancs = 'find /home/vendeg/render -type f -maxdepth 1 -exec rm {} \; ';
		@exec($parancs);
	}
	
	
	function frissitesmentes($fajl){
		$this->load->model('mclusers');
		$this->load->model('mforum');
		
		if( $fajl != "" && file_exists("/home/vendeg/render/".$fajl) /*&& ($_SERVER['REMOTE_ADDR'] == $this->irodaip)*/ ){
			
			$xml = simplexml_load_file("/home/vendeg/render/".$fajl);
			
			$info = "";
			$guid = "";
			$allapot = "";
			$statusz = "0";
			
			foreach($xml as $elem){
				$renderido = $elem->ElapsedSeconds;
				$guid = $elem->ProjectID;
				$allapot = $elem->WebStatus;
				
				$arender = $this->mrender->get_rowByguid($guid);
				if(!is_numeric($arender->render_id)){
					$record->osztaly = "render";
					$record->fv = "frissitesmentes";
					$record->hiba = "NINCS render id, GUID: ".$guid;
					$record->datum = date('Y-m-d H:i:s',time());
					$this->db->insert('a_hibak',$record);
				}else
					$this->mrender->frissit($arender->render_id,$allapot,$statusz);
				
				
				$nUserID = $this->mforum->get_nUserID2($arender->user_id);
				$infoguid = " guid: ".$arender->guid;
				
				
				if(!is_numeric($arender->render_id)){
				}
				elseif($allapot == "2" && $arender->allapot != "2"){
					$this->db->update('render_img',array('kezdet'=>time()),"render_id = '".$arender->render_id."'");
				}
				elseif($allapot == "30" && $arender->nyelv=="hun" && $nUserID > 0 ){
					$info = date('Y-m-d H:i',time()).": Renderelés megszakítás.".$infoguid;
					$this->mclusers->addEvent($nUserID, 116, $info);
				}
				
				// level ertesites
				$vvvv = "mail";
				if($arender->$vvvv == "0"){
					$this->emailkuldes($arender->guid);
				}
				
				
				// atmasolas
				if(!is_numeric($arender->render_id)){
				}
				elseif($allapot == "1"){
					$this->db->update('render_img',array('vege'=>$renderido),"render_id = '".$arender->render_id."'");
					$this->uploadrender2($guid);
					
					$record->osztaly = "render";
					$record->fv = "frissitesmentes";
					$record->hiba = "VÉGE: ".$guid."\n".$renderido."\nvaltozo: \n".$elem->ElapsedSeconds;
					$record->datum = date('Y-m-d H:i:s',time());
					$this->db->insert('a_hibak',$record);
				}
				
			}
			
		}
		
		if( $fajl != "" && file_exists("/home/vendeg/render/".$fajl) && $_SERVER['REMOTE_ADDR'] != $this->irodaip ){
			$record->osztaly = "render";
			$record->fv = "frissitesmentes";
			$record->hiba = "IP változás, régi: ".$this->irodaip."\núj:".$_SERVER['REMOTE_ADDR'];
			$record->datum = date('Y-m-d H:i:s',time());
		//	$this->db->insert('a_hibak',$record);
			
		//	if(substr($fajl,0,1) == "0")
		//		exec('echo "Megvaltozott az IP: '.$_SERVER['REMOTE_ADDR'].'" | mail -s "Render frissítés hiba" sztikee@gmail.com');
		}
	}
	
	function server(){
		$fajlnev = 'public/render/szerver.txt';
		$fa = fopen( $fajlnev, "r" ) or die("Open error");
		$ol = fgets( $fa, 1024);
		
		print $ol;
		
		fclose($fa);
	}
	
	function delrender(){
		$guid = $this->uri->segment(3);
		$render = $this->mrender->get_rowByguid($guid);
		
		if( /*$this->irodaip == $_SERVER['REMOTE_ADDR'] &&*/ is_numeric($render->render_id) ){
			$this->mrender->del($render->render_id);
			
			
			if( file_exists($render->keszkep) && $render->keszkep != "" ){
				$honnan = '/var/www/hosting/cadline.hu/www/';
				$ide = '/var/www/hosting/cadline.hu/www/public/render/torolve/';
				exec('mv '.$honnan.$render->keszkep.' ' . $ide . $render->user_id . '__' . basename($render->keszkep));
				exec('mv '.$honnan.$render->kiskep.' '. $ide . $render->user_id . '__' . basename($render->kiskep));
				exec('mv '.$honnan.'public/render/'.$render->nyelv.'/'.$render->user_id.'/elo_'.basename($render->keszkep).' '. $ide . $render->user_id . '__elo_' . basename($render->keszkep));
			}
			
			$record->osztaly = "render";
			$record->fv = "delrender";
			$record->hiba = "töröltem: ".$guid."\n render_id: ".$render->render_id;
			$record->datum = date('Y-m-d H:i:s',time());
			$this->db->insert('a_hibak',$record);
		}else{
			$record->osztaly = "render";
			$record->fv = "delrender";
			$record->hiba = "törlés sikertelen: ".$guid."\n render_id: ".$render->render_id."\n honnan: ".$_SERVER["HTTP_REFERER"];
			$record->datum = date('Y-m-d H:i:s',time());
			$this->db->insert('a_hibak',$record);
			
			print "Sikertelen torles";
		}
		
		redirect('admin/render');
	}
	
	function delrenderforum(){
		$render_id = $this->uri->segment(3);
		$user = $this->phpsession->get('fuser');
		if(!$user)
			$user = $this->phpsession->get('feltolto');
		
		if(!$user)
			redirect('forum');
		
		$render = $this->mrender->get_row($render_id);
		
		if( is_numeric($this->uri->segment(3)) && $render->user_id == $user['user_id'] ){
			$this->mrender->del($render_id);
			
			$this->db->update('render_img',array('deleted'=>'1'),"render_id = '".$render_id."'");
			
			if( file_exists($render->keszkep) && $render->keszkep != "" ){
				$honnan = '/var/www/hosting/cadline.hu/www/';
				$ide = '/var/www/hosting/cadline.hu/www/public/render/torolve/';
				exec('mv '.$honnan.$render->keszkep.' ' . $ide . $render->user_id . '__' . basename($render->keszkep));
				exec('mv '.$honnan.$render->kiskep.' '. $ide . $render->user_id . '__' . basename($render->kiskep));
				exec('mv '.$honnan.'public/render/'.$render->nyelv.'/'.$render->user_id.'/elo_'.basename($render->keszkep).' '. $ide . $render->user_id . '__elo_' . basename($render->keszkep));
			}
			
			$record->osztaly = "render";
			$record->fv = "delrenderforum";
			$record->hiba = "Törölve: ".$render->user_id."\n".$render->guid;
			$record->datum = date('Y-m-d H:i:s',time());
			$this->db->insert('a_hibak',$record);
			
		}else{
			$record->osztaly = "render";
			$record->fv = "delrenderforum";
			$record->hiba = "törlés sikertelen: ".$mit->guid;
			$record->datum = date('Y-m-d H:i:s',time());
			$this->db->insert('a_hibak',$record);
		}
		
		header("Location: ".$_SERVER["HTTP_REFERER"]);
	}
	
	function emailkuldes($guid){
		$this->load->model('mforum');
		
		if(!$guid)
			$guid = $this->uri->segment(3);
		
		$render = $this->mrender->get_rowByguid($guid);
		
		$info = "guid: ".$guid."\n userId: ".$render->user_id;
		
		if( isset($render->user_id) && is_numeric($render->user_id) && is_numeric($render->render_id) ){
			
			$user_id = $render->user_id;
			
			$qaf = $this->mforum->get_userUid($user_id);
			$emailcim = $qaf->email;
			
			
			$qaf2 = $this->mforum->get_user_szem($qaf->login);
			$emailcim = $qaf2[0]->email;
			
			if(!$emailcim){
				$qaf3 = $this->mforum->get_user_alap($qaf->login);
				$emailcim = $qaf3[0]->email;
			}
			
			if(!$emailcim)
				$emailcim = $qaf->login;
			
			if( $emailcim && eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $emailcim) ){
				$info .= "email: ".$emailcim;
				
				$mail_data = array(
					'render_id' => $render->render_id
				);
				
				$this->load->library('email');
				$this->email->initialize();
				$this->email->from('info@cadline.hu', 'Cadline Weboldal');
				$this->email->subject(' Render');
				if($qaf->nyelv == "hun")
					$this->email->message($this->load->view('mail/ujrender_hun', $mail_data, true));
				else
					$this->email->message($this->load->view('mail/ujrender_eng', $mail_data, true));
				
				$this->email->send($emailcim, 'smtp');
				
				$this->db->update('render_img',array('mail'=>"1"),"render_id = '".$render->render_id."'");
			}else{
				$info .= "\nÜres vagy hibás email cim: ".$emailcim."\nguid: ".$render->guid;
			}
			
		}else{
			$info .= "\nNincs render!";
		}
		
		$record->osztaly = "render";
		$record->fv = "emailkuldes";
		$record->hiba = "email: ".$emailcim."\n".$info;
		$record->datum = date('Y-m-d H:i:s',time());
		$this->db->insert('a_hibak',$record);
	}
	
	function session(){
		$this->cookieupdate();
		
		$data['ujratolt'] = '120';
		$this->load->view('site_upload',$data);
	}
	
	function kupon_felhasznalas($kupon, $op=1, $db=1, $render=false){
		/*	op: 1 - lefoglal
			op: 2 - felhasznal
			op: 3 - vissza
		*/
		
		$kod = $this->db->query(" SELECT * FROM `cadline2`.`render_kodok` WHERE kod = '".$kupon."'")->row();
		
		$user = $this->phpsession->get('fuser');
		if(!$user)
			$user = $this->phpsession->get('feltolto');
		
		// hibak
		if( !isset($kod->ervenyes) ){
			$info = lang('render_hibaskupon');
			
			$record->osztaly = "render";
			$record->fv = "kupon_felhasznalas";
			$record->hiba = "Érvénytelen, user: ".$user['user_id']."\n".$kupon;
			$record->datum = date('Y-m-d H:i:s',time());
			$this->db->insert('a_hibak',$record);
			
			return $info;
			
		}
		elseif( ($op==1 && ($kod->ervenyes - $kod->foglalva) < 1) || ($op==2 && ($kod->ervenyes + $kod->foglalva) < 1) ){
			$info = lang('render_hasznaltkupon');
			
			$record->osztaly = "render";
			$record->fv = "kupon_felhasznalas";
			$record->hiba = "Elhasznált, user: ".$user['user_id']."\n".$kupon."\nrender: ".$render-render_id;
			$record->datum = date('Y-m-d H:i:s',time());
			$this->db->insert('a_hibak',$record);
			
			return $info;
			
		}
		elseif( $kod->user != "" && $user['user_id'] != $kod->user && $kupon != "1000000000" ){
			$info = lang('render_nemsajatkupon');
			
			$record->osztaly = "render";
			$record->fv = "kupon_felhasznalas";
			$record->hiba = "Nem saját kupon, user: ".$user['user_id']."\n".$kupon;
			$record->datum = date('Y-m-d H:i:s',time());
			$this->db->insert('a_hibak',$record);
			
			return $info;
		}
		elseif( ($op == "3" || $op == "2") && $db > $kod->foglalva ){
			$info = lang('render_nemvisszaadhato');
			
			$record->osztaly = "render";
			$record->fv = "kupon_felhasznalas";
			$record->hiba = "Visszavonási hiba, user: ".$user['user_id']."\n".$kupon;
			$record->datum = date('Y-m-d H:i:s',time());
			$this->db->insert('a_hibak',$record);
			
			return $info;
		}
		
		// muvelet
		if( $op == "1" ){
			$tomb = array(
					'ervenyes' => ($kod->ervenyes-$db),
					'foglalva' => ($kod->foglalva+$db),
					'elhasznalva' => date('Y-m-d H:i:s',time()),
					'user' => $user['user_id'],
					'azon' => $render->guid,
					'info' => $kod->info.$kod->ervenyes." (".$db."). Foglal, render: ".$render->guid."\n(".date('Y-m-d H:i:s',time()).")\n"
			);
			$this->db->update('render_kodok',$tomb,"kod = '".$kupon."'");
			$info = true;
			
		}
		elseif( $op == "2" ){
			$tomb = array(
					'foglalva' => ($kod->foglalva-$db),
					'elhasznalva' => date('Y-m-d H:i:s',time()),
					'info' => $kod->info.$kod->ervenyes." (".$db."). Felhasznal, render: ".$render->guid."\n(".date('Y-m-d H:i:s',time()).")\n"
			);
			$this->db->update('render_kodok',$tomb,"kod = '".$kupon."'");
			$info = true;
		}
		elseif( $op == "3" ){
			$tomb = array(
					'ervenyes' => ($kod->ervenyes+$db),
					'foglalva' => ($kod->foglalva-$db),
					'elhasznalva' => date('Y-m-d H:i:s',time()),
					'info' => $kod->info.$kod->ervenyes." (".$db."). Visszavonva, render: ".$render->guid."\n(".date('Y-m-d H:i:s',time()).")\n"
			);
			$this->db->update('render_kodok',$tomb,"kod = '".$kupon."'");
			$info = true;
		}
		
		return $info;
	}
	
	
	function cookieupdate(){
		if( isset($_COOKIE['azonosito'],$_COOKIE['sessid']) )
			$ell = md5($_COOKIE['azonosito'].$this->pw.$_COOKIE['sessid']);
		else
			return false;
		
		if ( !empty($_COOKIE['hash']) && $_COOKIE['hash']== $ell ){
			setcookie("sessid",$_COOKIE['sessid'],time()+$this->ervenydesseg,"/");
			setcookie("azonosito", $_COOKIE['azonosito'] , time()+$this->ervenydessegazon,"/");
			setcookie("hash",$_COOKIE['hash'] ,time()+$this->ervenydesseg,"/");
			
			return true;
			
		}else{
			return false;
		}
	}
	
}


function hasonlito( $a, $b ){
	if ( $a[1] == $b[1] )
		return 0;
	if ( $a[1] < $b[1] )
		return -1;
	return 1;
}

?>
