<?

class user{

var $error_msg;

// l�ptet�
var $split=50; // h�ny t�tel per oldal
var $range=3;  // h�ny szomsz�dos oldal l�tsszon a l�ptet�ben
var $thumb=array(width=>100,height=>100);  // ezt lehet feluliratni projektenket EXT_IMG

// keres�si param�terek
//var $search_fields=array('','type','name','email','phone','timestamp','o'); // els� elem csak ahhoz kell hogy user-keres�s van-e a sessionben elmentve
//var $search_fields_isarray=array(); // mely keres�si param�ter t�mb

var $multi_images; // t�bb k�p per user bekapcsol�sa

function user(){

}

function getLangId($code){
	global $dbi;
	$sql="select id from country where code = '$code'";
	$res=sql_query($sql,$dbi);
	list($id)=sql_fetch_array($res,$dbi);
	return  $id;
}


function getUserTypeData($id){

	global $dbi;

	$sql="select id,type from user_type where id=$id";
	$res=sql_query($sql,$dbi);
	list($id,$name)=sql_fetch_array($res,$dbi);

	$data=array(id=>$id,name=>$name);

	return $data;
}

function getUserTypeList($params=NULL){

	global $dbi;

	if($params){
		$sql_where=" where id > $params "; //ha 5, akkor nem admin és nem adatfelvivo
	}

	$sql="select id, type from user_type $sql_where order by type";
	$res=sql_query($sql,$dbi);
	while(list($id,$type)=sql_fetch_array($res,$dbi)){
		$list[]=array(id=>$id,type=>$type,name=>$type);
	}

	return $list;
}

function getUserTypeByCode($id){

	global $dbi;

	$sql="select type from user_type where id=$id order by type";
	$res=sql_query($sql,$dbi);
	list($type)=sql_fetch_array($res,$dbi);

	return $type;
}

function errorMsg($new_message) {
	// "error_msg"-be f�z�dnek f�l a hiba�zik
	if($this->error_msg) $this->error_msg=$this->error_msg.'<br>'; //hozz�ad sort�r�st az eddigiekhez
	$this->error_msg=$this->error_msg.$new_message; //hozz�adja az �jat
}

function verifyNewUser($data){

	global $dbi;
	$rosszchar=sprintf("'%c+\&",34); // amik nem lehetnek a usern�vben

	//// HIBAELLEN�RZ�SEK ////
	unset($hiba);
	unset($error_msg); //nem lehet m�s v�ltoz�, ezt haszn�lja az errorMsg(), mint glob�lis

	//html karakterek semleges�t�se
	$login=htmlentities($data[profil_login]);
	$email=htmlentities($data[profil_email]);

	//hib�s usern�v
	if(!$login) {
		$hiba=1;
		$this->errorMsg('{$USER_NEW_ERROR_LOGINNAME}');
	}
	else {
		//van m�r ilyen user
		if(sql_fetch_row(sql_query("SELECT id FROM users WHERE login='$login'",$dbi),$dbi)) {
			$hiba=1;
			$this->errorMsg('{$USER_NEW_ERROR_LOGINPRESENT}');
		}
		//usern�v min. 2 kar.
		if(strlen($login)<2) {
			$hiba=1;
			$this->errorMsg('{$USER_NEW_ERROR_LOGINTOOSHORT}');
		}
	}
	//hib�s jelsz�
	if($data[profil_pass1]!=$data[profil_pass2] || !$data[profil_pass1]) {
		$hiba=1;
		$this->errorMsg('{$USER_NEW_ERROR_PASS}');
	}
	//rossz karakterek az usern�vben
	for($i=0;$i<strlen($rosszchar);$i++) if(strpos($login,$rosszchar[$i])) $hiba=2;
	if($hiba==2) {
		$this->errorMsg('{$USER_NEW_ERROR_BADCHARS}:&nbsp; '.$rosszchar);
	}
	//van m�r ilyen emailc�m
	if(sql_fetch_row(sql_query("SELECT id FROM users WHERE email='$email'",$dbi),$dbi)) {
		$hiba=1;
		$this->errorMsg('{$USER_NEW_ERROR_EMAILPRESENT}');
	}
	/*
		hib�s emailc�m ezt jelenti:
		- nincs benne "@"
		- nincs benne "."
		- "." nincs min. 2-vel k�s�bb mint a "@"
	*/
	$kukac=strpos($email,"@");
	$pont=strrpos($email,".");
	if(!($kukac && $pont && $pont>$kukac+1)) {
		$hiba=1;
		$this->errorMsg('{$USER_NEW_ERROR_EMAIL}');
	}
}

function recoverPass($value,$field){

	if(!$field) return false;
	global $dbi,$SITEGLOBALS;

	$sql="select id,login,concat(firstname,' ',lastname),email from users where $field='$value'";
	$res=sql_query($sql,$dbi);
	list($id,$login,$name,$email)=sql_fetch_array($res,$dbi);

	if(!$id) return false;

	$lang=new translator;

	$pass=$this->generatePass();

	$sql="update users set password=md5('$pass') where id=$id";
	$res=sql_query($sql,$dbi);

	$mail=$lang->getSingleTranslation('MAIL_RECOVER');
	$mail=str_replace('\r','',$mail);
	$mail=str_replace('#login#',$login,$mail);
	$mail=str_replace('#name#',$name,$mail);
	$mail=str_replace('#pass#',$pass,$mail);
	$mail=str_replace('#data#',$lang->getSingleTranslation('MAIL_RECOVER_BY_$field'),$mail);

	//print'<span style="color:A0F">(<span style="color:a60">'.$email.'</span>)</span><br>';
	//print'<pre>';print_r($mail);print'</pre>';
	mail($email,$SITEGLOBALS[SELLER_DATA][company]." -".$lang->getSingleTranslation('TEXT_PASS_RECOVERY'),$mail,"From: $SITEGLOBALS[MAIL_FROM]");

	return $email;
}

function generatePass(){

	$chars=array(
		'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','r',
		'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','R','S',
		'1','2','3','4','5','6','7','8','9'
	);

	for($i=0;$i<8;$i++){
		$pass.=$chars[rand(1,count($chars))];
	}

	return $pass;
}

function getActivationUrl($id) {
	$key=md5(CODE.$id);
	$url=WEBPATH."?page=activate&action=$action&user=$id&key=$key";
	return $url;
}

function saveNewUser($post){

	global $dbi,$selected_lang;

	extract($post);
	$login=$p_login;

	if(!$no_email_check) {
		// ha m�r l�tezik az email account n�lk�l, oda vegye fel
		$sql="select id from users where email='$email' and login=''";
		$res=sql_query($sql,$dbi);
		list($id)=sql_fetch_row($res,$dbi);
	}

	if(!($pass=$pass1) && !$post[nopass]) $pass=$this->generatePass();
	if($pass) $pass_md5=md5($pass);

	if(!$type) $type=6;

	$inactive=($set_inactive && !$post[inactive])?0:1;

	if(sql_check_field('users','newsletter_send')) {
		$newsletter_send_field=',newsletter_send';
		$newsletter_send=",'$newsletter_send'";
	}

	$sql="replace into users (id,login,user_type,password,title,firstname,lastname,company,email,phone,country_code,inactive$newsletter_send_field) ".
		"values ('$id','$login',$type,'$pass_md5','$title','$fname','$lname','$company','$email','$phone','$country_code',$inactive$newsletter_send)";
	sql_query($sql,$dbi);

	if(!$id) {
		$sql="select max(id) from users";
		$res=sql_query($sql,$dbi);
		list($id)=sql_fetch_row($res,$dbi);
	}

	// tortenet ha van
	//if(sql_check_table('user_history')){
	//	$sql="";
	//}

	if($address_type) {
		$post[target_type]='u';
		$post[target_id]=$id;
		$addr=new address;
		$addr->saveAddressesGrouped($post);
	}

	// ** MAILEK **
	if($title) $name="$title ";
	$name.="$fname $lname";

	if(($login && $email) || $this->mail_to_admin) {

		$content=new content;
		$translator=new translator($selected_lang,1);

		if($login && $email) {
			if($set_inactive) { // $set_inactive --> ha admin hozta letre
				if(!$inactive)
					$content->sendMail('NEW_USER_BY_ADMIN',compact(login,pass,name,email));
			}else{
				if($post[inactive]==1){
					$link=$this->getActivationUrl($id);
					$link="<a href='$link'>".$translator->getSingleTranslation('TEXT_ACTIVATION')."</a>";
				}else{
					$link=' Fiokja aktivalva van!';
				}
				$content->sendMail('REG_USER',compact(login,pass,name,email,link));
			}
		}
		if($this->mail_to_admin){
			global $SITEGLOBALS;
			$user_email=$email;
			$email=$SITEGLOBALS[MAIL_ADMIN];
			$content->sendMail('REG_ADMIN',compact(email,login,pass,name,company,user_email));
		}
	}

	return $id;
}

function updateUser($post){
	global $dbi;

	extract($post);

	if($p=$p_login) $login="login='$p',";
	if($p=$type) $type="user_type=$p,";
	if($p=$country_code) $country_code="country_code='$p',";
	if($p=$pass) $pass="password=md5('$p'),";
	if($inactive) $inactive="inactive=1,"; else $inactive="inactive=0,";

	// ezt nem ertem miert van itt!!
	//$inactive='inactive='.($inactive?1:0).", ";

	$sql="update users set $login $pass $type $inactive firstname='$fname', lastname='$lname', title='$title', ".
		"phone='$phone', email='$email', company='$company' where id=$uid";
	sql_query($sql,$dbi);

	//k�p
	if($_FILES['image']['name']) {
		$filename=$this->makeUserSingleImageName($uid);
		exit;
		$params=array(dst_file_name=>USER_IMAGES.$filename, x=>$this->thumb[width], y=>$this->thumb[height], dst_ext=>'jpg');
		$gal=new gallery;
		$gal->uploadPicture($params);
	}
}

function makeUserSingleImageName($id) {
	$fn=$id;
	while(strlen($fn)<8) $fn="0$fn";
	$fn="u$fn";
	return $fn;
}

function updateOneField($field,$post){
	global $dbi;
	$sql="update users set $field='$post[$field]' where id=$post[uid]";
	sql_query($sql,$dbi);
}

function updateOneFieldNormal($field,$value,$uid){
	global $dbi;
	$sql="update users set $field='$value' where id=$uid";
	sql_query($sql,$dbi);
}

function saveUser($uid,$country_code,$office,$utype,$fname,$lname){

	global $dbi;
	$country=$this->getCountryName($country_code);
	$sql="update users set country_code='$country_code', office_id='$office', user_type='$utype', firstname='$fname', lastname='$lname' where id=$uid;";
	sql_query($sql,$dbi);
}

function setInactive($uid,$set){
	global $dbi;
	$sql="update users set inactive=$set where id=$uid;";
	sql_query($sql,$dbi);
}

function activate($id,$key) {

	global $dbi;
	$err=true;

	//print"<span style='color:#888'>(<span style='color:#f00'>".$key."</span>)</span><br>";
	//print"<span style='color:#888'>(<span style='color:#f00'>".md5(CODE.$id)."</span>)</span><br>";
	if($key==md5(CODE.$id)) {
		$sql="select inactive from users where id=$id";
		$res=sql_query($sql,$dbi);
		list($inactive)=sql_fetch_array($res,$dbi);
		if(!$inactive) $msg='a1';
		else{
			$sql="update users set inactive=0 where $id=id";
			$msg='ok1';
			$err=false;
		}
		if(!$err && !sql_query($sql,$dbi)) $msg='e2';
	}else $msg='e1';

	return array(text=>$msg,error=>$err);
}

/**
 *
 *
 * @todo Lorand: ez a t�rl�si m�d biztos, hogy nem okoz gondot?
 */
function deleteUser($id){
	global $dbi;
	$sql="delete from users where id=$id";
	sql_query($sql,$dbi);
}

function updatePass($pass){
	global $dbi;
	$sql="update users set password='".md5($pass)."' where id=".$this->uid;
	sql_query($sql,$dbi);
}

function checkLogin($login){

	global $dbi;

	$sql="select id from users where login='$login'";
	$res=sql_query($sql,$dbi);
	if(list($id)=sql_fetch_array($res,$dbi)) return $id;
	else return false;

}


function getUserShortList($types){

	global $dbi;

	$sql_where="where user_type in ($types)";

	if($this->users_to_load){
		foreach($this->users_to_load as $key => $item){
			$tmp.="$item";
			if($key<count($this->users_to_load)-1) $tmp.=',';
		}
		$sql_where.=" and id in ($tmp)";
	}


	$sql="select id,firstname,lastname,login,company,email,user_type,inactive from users $sql_where order by user_type,firstname,lastname";
	$res=sql_query($sql,$dbi);
	while(list($id,$fname,$lname,$login,$company,$email,$utype,$inactive)=sql_fetch_array($res,$dbi)){
		$utype=$this->getUserTypeByCode($utype);
		$users[]=array(id=>$id,fname=>$fname,lname=>$lname,login=>$login,company=>$company,email=>$email,utype=>$utype,inactive=>$inactive);
	}

	return $users;
}

function getUsersByType($type){

	global $dbi;

	$sql="select id from users where user_type in ($type)";
	$res=sql_query($sql,$dbi);
	while(list($id)=sql_fetch_array($res,$dbi)){
		$ids[]=$id;
	}

	$this->users_to_load=$ids;
}

/**
 * @todo Lorand: biztos hogy a users_to_load tagv�ltoz� t�mb?
 */
function getUserList($type='',$w_pre=''){

	global $dbi,$auth;

	if($this->users_to_load)
		$w[]="id in (".implode(',',$this->users_to_load).")";

	if($type) $w[]=" user_type in($type) ";
	if($w_pre) $w=array_merge($w,$w_pre);

	if($w) $where=' where '.implode(' and ',$w);

	$sql="select id,user_type,company,login,title,firstname as fname,lastname as lname,phone,email,last_login_date as last_login,inactive from users $where";

	$res=sql_query($sql,$dbi);
	while($a=sql_fetch_assoc($res,$dbi)){
		$a[pic]=$this->getUserMainImage($id);
		$type=$this->getUserTypeData($a[user_type]);
		$a[user_type_name]=$type[name];
		$users[]=$a;
	}

	return $users;
}


/**
 * adding new user type group
 *
 * @param array $post name of the user type
 */
function saveNewUserType($post) {
		global $dbi;
		$sql = "insert into user_type (type) values ('$post')";
		sql_query ( $sql, $dbi );
	}

/**
 * searching for users by parameters
 *
 * @param array $p search criteria
 * @return array|NULL
 */
function searchUsers($p) {
	global $dbi;

	//l�ptet� hat�s�ra limit
	if($this->split) {
		if(!($o=$p[o]) && !($o=$_GET[o])) $o=0;
		$o_item=$o*$this->split;
		$limit="limit $o_item,$this->split";
	}

	//v�gigmegy a lehets�ges param�tereken, �s felf�zi a WHERE-t
	if($x=$p[user_type]) $w[]="user_type in ($x)";
	if($x=$p[ids]) {
		if(is_array($x)) $x=implode(',',$x);
		$w[]="id in ($x)";
	}
	if($x=$p[name]) {
		//ha space-szel el van v�lasztva, teljes n�vre keressen
		$s="(firstname like '%$x%' or lastname like '%$x%')";
		if(strpos($x,' ')) {
			list($fname,$lname,$xname)=explode(' ',$x);
			if($xname) {
				// ha 3 n�v van
				$s.=" or (firstname like '%$fname $lname%' and lastname like '%$xname%')".
			        " or (firstname like '%$fname%' and lastname like '%$lname $xname%')";
			}else{
				// ha 2 n�v van
				$s.=" or (firstname like '%$fname%' and lastname like '%$lname%')";
			}
		}
		$w[]="($s)";
	}
	if($x=$p[phone]) $w[]="phone like '%$x%'";
	if($x=$p[email]) $w[]="email like '%$x%'";
	if($x=$p[timestamp]) $w[]="timestamp like '%$x%'";
	if($x=$p[keruletresz]) $w[]=" keruletresz like '%$x%' ";
	if($x=$p[city]) $w[]=" city = '$x' ";
	if($x=$p[street]) $w[]=" street like '%$x%' ";
	if($x=$p[comment]) $w[]=" comment like '%$x%' ";
	if($x=$p[owner_id]) $w[]=" owner_id = '$x' ";
	if(!$p[_inactive]) $w[]='inactive=0';

	if($w) $where='where '.implode(' and ',$w);

	$i=$o_item+1;

	$sql="select id, user_type, concat(firstname,' ',lastname) as name, login, email, phone, last_login_date,timestamp,owner_id, inactive from users $where order by user_type,firstname,lastname $limit";
	if($p[debug]) {print'<pre>';print_r($w);print"</pre>$sql<br>";}
	$res=sql_query($sql,$dbi);
	while($a=sql_fetch_assoc($res,$dbi)) {
		$a[no]=$i;
		$a[user_type_name]=$this->getUserTypeData($a[user_type]);
		$a[owner]=$this->getUserData($a[owner_id]);
		$data[]=$a;
		$i++;
	}

	//l�ptet�
	if($this->split) {
		$sql="select count(id) from users $where";
		$res1=sql_query($sql,$dbi);
		list($items)=sql_fetch_row($res1,$dbi);
		$this->makePages($items,$o);
	}

	return $data;
}
/**
 * get search params from session
 *
 * @param string $sid session id
 * @return array|false|NULL|string

function getSearchSql($sid) {

	global $dbi,$auth;

	if(!sql_check_field('session','search_params')) return;

	$sql="select search_params from session where session_id='$sid'";
	$res=sql_query($sql,$dbi);
	extract(sql_fetch_assoc($res,$dbi));
	$sp=explode(';',$search_params);
	// els� elem csak ahhoz kell hogy user-keres�s van-e a sessionben elmentve
	if($sp[0]!='user') return false;
	foreach($this->search_fields as $i=>$f)
		if($i) $p[$f]=$sp[$i];

	return $p;
}
/**
 * save search params into session
 *
 * @param array $p search params
 * @param string $sid session id
 * @todo fel�lvizsg�lni impload az updateben

function saveSearchParams($p,$sid) {
	global $dbi;
	$s=array('user');

	foreach($this->search_fields as $i=>$f)
		if($i)
			if((in_array($f,$this->search_fields_isarray)) && $p[$f])
				$s[]=implode(',',$p[$f]);
			else
				$s[]=$p[$f];

	$sql="update session set search_params='".implode(';',$s)."' where session_id='$sid'";
	sql_query($sql,$dbi);

}*/

/**
 * splitting items to pages (l�ptet�)
 *
 * @param integer $items number of items to be split
 * @param integer $o page offest
 * @return array|NULL
 */
function makePages($items,$o) {
	$pages=ceil($items/$this->split);
	if(!$pages) return;

	$sight_min=$o-$this->range; if($sight_min<0) $sight_min=0;
	$sight_max=$o+$this->range; if($sight_max>=$pages) $sight_max=$pages-1;

	for($i=$sight_min; $i<=$sight_max; $i++) $sight[$i].=$i+1;

	$this->pages=array(items=>$items, pages=>$pages, sight=>$sight, act=>$o);
	//print'<pre>';print_r($this->pages);print'</pre>';
}


function getCountryList(){
	global $dbi;
	$sql="select code,name,charset from country order by name";
	$res=sql_query($sql,$dbi);
	while(list($code,$name,$charset)=sql_fetch_array($res,$dbi)){
		$data[]=array(code=>$code,name=>$name,charset=>$charset);
	}
	return $data;
}

function getUserType($uid){

	global $dbi;

	$sql="select user_type from users where id=$uid";
	$res=sql_query($sql,$dbi);
	list($type)=sql_fetch_array($res,$dbi);
	return $type;
}

function getUserData($uid){
	if(!$uid) return false;
	global $dbi,$auth;

	$sql="select id,user_type,company,login,title,firstname as fname,lastname as lname,country_code,phone,email,last_login_date as last_login,inactive from users where id=$uid";
	$res=sql_query($sql,$dbi);
	if($a=sql_fetch_assoc($res,$dbi)){
		$a[pic]=$this->getUserMainImage($id);
		$type=$this->getUserTypeData($a[user_type]);
		$a[user_type_name]=$type[name];
		$a[loginname]=$a[login];
		$a[name]="$a[fname] $a[lname]";
	}
	return $a;
}


function getUserName($id){
	global $dbi;
	$sql="select login,firstname,lastname from users where id='$id'";
	$res=sql_query($sql,$dbi);
	if($a=sql_fetch_assoc($res,$dbi)) {
		extract($a);
		if(!($name="$firstname $lastname")) $name=$login;
		return $name;
	}
}


function getUserImages($id) {

	global $dbi;

	$sql="select file,main,pos from image_users where user_id=$id order by pos";
	$res=sql_query($sql,$dbi);
	$i=0;
	while($fetch=sql_fetch_array($res,$dbi)) {
		list($images[],$main)=$fetch;
	}
//echo "<pre>";print_r($images);
	return $images;
}

function getUserImagesForAjax($id) {

	global $dbi;

	$sql="select user_id, file,pos from image_users where user_id=$id order by pos";
	$res=sql_query($sql,$dbi);
	$n=sql_num_rows($res,$dbi); $i=0;

	while(list($id1, $image)=sql_fetch_array($res,$dbi)) {
		$size=@GetImageSize(PATH."Image/users/$image");
		$data[]=array(id1=>$id1, pic=>$image, thumb=>str_replace('.','_t.',$image), x=>$size[0], y=>$size[1]);
	}
	return $data;
}


function getUserMainImage($id) {
	if($auth->multi_images) {
		global $dbi;
		$sql="select file from image_users where user=$id and main=1";
		$res=sql_query($sql,$dbi);
		list($image)=sql_fetch_array($res,$dbi);
	}else{
		$image=$this->makeUserSingleImageName($id).'.jpg';
		if(!is_file(USER_IMAGES.$image)) return;
	}
	return $image;
}

function getMegrNyilAccept($id) {
	global $dbi;
	$sql="select megr_nyil_date from users where id='$id'";
	$res=sql_query($sql,$dbi);
	list($date)=sql_fetch_array($res,$dbi);
	return $date;
}
function updateMegrNyilAccept($id) {
	global $dbi;
	$sql="update users set megr_nyil_date='".date("Y-m-d")."' where id='$id'";
	sql_query($sql,$dbi);
}

/**
 * @todo Lorand:biztos hogy $post[cimzett] egy t�mb?
 * @todo Lorand:biztos hogy $users egy t�mb?
 */
function sendNewsletter($post,$debug=false) {

	global $dbi,$SITEGLOBALS;
	$trans=new translator($SITEGLOBALS[LANGUAGE],0);

	extract($post);

	foreach($users as $id){
		$userdata[]=$this->getUserData($id);
	}

	// ha nem j�tt postb�l content,blurb
	if(!$content) {
		$c=new content;
		extract(is_numeric($newsletter) ? $c->getContentData($newsletter) : $c->getContentByCode($newsletter));
	}

	if($frame) {
		$frame_content = is_numeric($frame) ? $c->getContentData($frame) : $c->getContentByCode($frame);
		$content=str_ireplace('#content#',$content,$frame_content[content]);
	}

	$headers="From: $SITEGLOBALS[MAIL_FROM]\nContent-Type: text/html; charset=ISO-8859-2 ";

	foreach($userdata as $u){
		$email=$u[email];
		$unsubscribe=$this->unsubscribeURL($u[id],$email);

		$subst_codes=array('name'=>$u[name], 'unsubscribe'=>$unsubscribe);
		if($subst) $subst_codes=array_merge($subst_codes,$subst);

		$body=$content;
		foreach($subst_codes as $f=> $r) {
			$body=str_ireplace("#$f#",$r?$r:'---',$body);
		}
		$body=str_ireplace('\"','"',$body);

		if($t=$SITEGLOBALS['SELLER_DATA']){
			$body.=	"<br><br><hr><br><h3 style='margin:0px;'>".$t[company]."</h3><br>"
					."<b>".$trans->getSingleTranslation('TEXT_PHONE').":</b> ".$t[phone1]."<br>"
					."<b>".$trans->getSingleTranslation('TEXT_ADDRESS').":</b> ".$t[address1]."<br>"
					."<b>"."www:</b> ".$t[web];
		}

		if($debug)
			print"<font color='#00cc33'><b>".htmlentities($email)."</b></font><br><font color='#bb3300'>".$blurb."</font><br><font color='#aa7733'>$body</font><hr>";
		else
			mail($email,$blurb,$body,$headers);
	}
}
function resendNewsLetter($post,$debug=false){
	global $dbi,$SITEGLOBALS;

	$headers="From: $SITEGLOBALS[MAIL_FROM]\nContent-Type: text/html; charset=ISO-8859-2 ";

	$sql="select email,concat(firstname,' ',lastname) from users where id in ($post[users])";
	$res=sql_query($sql,$dbi);
	while(list($email,$name)=sql_fetch_array($res,$dbi)){
		$body=str_ireplace('#name#',$name,$post[content]);
		$email="$name <$email>";

		if($debug)
			print"<font color='#00cc33'><b>".htmlentities($email)."</b></font><br><font color='#bb3300'>".$blurb."</font><br><font color='#aa7733'>$body</font><hr>";
		else
			mail($email,$post[blurb],$body,$headers);
	}
}
function unsubscribeURL($id,$email){
	return WEBPATH."index.php?page=unsubscribe&id=$id&email=$email";
}
function unsubscribeNewsletter($id,$email) {
	global $dbi;
	$sql="update users set newsletter_send=0 where id='$id' and email='$email'";
	sql_query($sql,$dbi);
	return mysql_affected_rows($dbi);
}

function regByEmailURL($user_id,$email) {
	return WEBPATH."index.php?page=reg_by_email&email=$email&key=".md5(CODE.$user_id.$email);
}
function getUserDataForRegByEmail($email,$key) {
	global $dbi;
	$sql="select id,email,firstname as fname,lastname as lname,phone from users where email='$email'";
	$res=sql_query($sql,$dbi);
	$a=sql_fetch_assoc($res,$dbi);
	if($key==md5(CODE.$a[id].$a[email])) return $a;
}

function urlChars() {
	$chars=array('�','�','�','�','�','�','�','�','�','�','�','�','�','�','�','�','�','�');
	//foreach($chars as $i=>$c) $chars[$i]=iconv("ISO-8859-2", "UTF-8", $c);
	return $chars;
}

function getHistory($user_id){

	global $dbi;

	$sql="select * from user_history where user_id=$user_id order by timestamp desc";
	//echo $sql;
	$res=sql_query($sql,$dbi);
	while($r=sql_fetch_assoc($res,$dbi)){
		switch($r[type]){
			case 'U':
				$r[type]='frissites';
				break;
			case 'I':
				$r[type]='letrehozas';
				break;
			case 'D':
				$r[type]='torles';
				break;
		}

		switch($r[change_table]){
			case 'users':
				$r[change_table]='alapadat';
				break;
			case 'property_values':
				$r[change_table]='tulajdonsag';
				break;
		}

		$data[]=$r;
	}
	//echo "<pre>";print_r($data);
	return $data;
}

} // class v�ge
?>