<?php
defined('WikyBlog') or die("Not an entry point...");

class sessionConfirm{
	
	function sessionConfirm(){
		global $wbTables, $page;
		
		wbLang::getFile('register');
		
		$code = sessionConfirm::getCode($_GET['u']);
		
		if( $code != $_GET['c'] ){
			message('INVALID_CODE');
			return;
		}
		
		$query = 'UPDATE '.$wbTables['users'];
		$query .= ' SET ';
		$query .= ' `flags` = "" ';
		$query .= ' WHERE ';
		$query .= ' `username` = "'.wbDB::escape($_GET['u']).'" ';
		$query .= ' LIMIT 1 ';
		$num = wbDB::runQuery($query,true);
		
		if( $num < 1 ){
			//a user might follow the activation link twice in which case nothing will happen
			return;
		}
		
		//log user in
		includeFile('tool/sessionActions.php');
		sessLog::set($_GET['u'],false);
		message('ACTIVATED_FROM_EMAIL');
		//$page->status = 'registered'; //show registered template..
	}
	
	function getCode($username){
		global $wbUniq, $wbTables;
		
		
		$query = 'SELECT `user_id`, `password`, `created` ';
		$query .= ' FROM '.$wbTables['users'];
		$query .= ' WHERE ';
		$query .= ' `username` = "'.wbDB::escape($username).'" ';
		$query .= ' AND `flags` = "pending" ';
		$result = wbDB::runQuery($query);
		if( $row = mysql_num_rows($result) ){
			return md5($wbUniq . $row['created'] . $row['password']);
		}
		return false;
	}
}

global $initiateFileClass;

if( $initiateFileClass ){
	new sessionConfirm();
}