<?php

function dbconnect()
    {
        global $pdo;

            try {
                $pdo = new PDO('mysql:host='. DB_HOST .';dbname='. DB_NAME . ';charset=utf8', DB_USER, DB_PASS);
				$pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION ); //Error Handling
            } catch (PDOException $e) {
                die('MySQL connection failed! ' . $e->getMessage());
            }

    }
	
function is_user()
{
	if (isset($_SESSION['user_login']) && isset($_SESSION['user_number']))
		return true;
}


function is_admin()
{
	if (isset($_SESSION['admin_login']))
		return true;
}


function user_attempt($username, $password)
{
	global $pdo;
	$stmt = $pdo->prepare('SELECT * FROM '.USERS_TABLE_NAME.' WHERE user_login = :username AND user_pwd = :password AND user_status = 1 LIMIT 1');
	$stmt->execute(array(':username' => $username, 'password' => md5($password)));
	if ($data = $stmt->fetch( PDO::FETCH_OBJ )) {
		# set session
		$_SESSION['user_login'] = $data->user_login;
		return true;
	} else {
		return false;
	}
}

function user_attemptdob($username, $password, $dob)
{
	global $pdo;
	$stmt = $pdo->prepare('SELECT * FROM '.USERS_TABLE_NAME.' WHERE user_login = :username AND user_pwd = :password AND dob = :dob AND user_status = 1 LIMIT 1');
	$stmt->execute(array(':username' => $username, 'password' => md5($password), ':dob' => $dob));
	if ($data = $stmt->fetch( PDO::FETCH_OBJ )) {
		# set session
		$_SESSION['user_login'] = $data->user_login;
		return true;
	} else {
		return false;
	}
}

function signin_user($username)
{
	global $pdo;
	$stmt = $pdo->prepare('SELECT * FROM '.USERS_TABLE_NAME.' WHERE user_login = :username AND user_status = 1 LIMIT 1');
	$stmt->execute(array(':username' => $username));
	if ($data = $stmt->fetch( PDO::FETCH_OBJ )) {
		# set session
		$_SESSION['user_login'] = $data->user_login;
        $_SESSION['user_number'] = $data->user_number;
		return true;
	} else {
		return false;
	}
}

function security_answer($username, $answer)
{
	global $pdo;
	$stmt = $pdo->prepare('SELECT * FROM '.USERS_TABLE_NAME.' WHERE user_login = :username AND secret_ans = :answer LIMIT 1');
	$stmt->execute(array(':username' => $username, ':answer' => $answer));
	if ($data = $stmt->fetch( PDO::FETCH_OBJ )) {
		return true;
	} else {
		return false;
	}
}

function security_qa($username, $question, $answer)
{
	global $pdo;
	$stmt = $pdo->prepare('SELECT * FROM '.USERS_TABLE_NAME.' WHERE user_login = :username AND secret_qs = :question AND secret_ans = :answer LIMIT 1');
	$stmt->execute(array(':username' => $username, ':question' => $question, ':answer' => $answer));
	if ($data = $stmt->fetch( PDO::FETCH_OBJ )) {
		return true;
	} else {
		return false;
	}
}

function update_seqa($username, $question, $answer)
{
	global $pdo;
	$stmt = $pdo->prepare('UPDATE '.USERS_TABLE_NAME.' SET secret_qs = :question AND secret_ans = :answer WHERE user_login = :username LIMIT 1');
	if ($stmt->execute(array(':username' => $username, ':question' => $question, ':answer' => $answer))) {
	    return true;
    }
}

function user_photo($username)
{
	global $pdo;

	$stmt = $pdo->prepare('SELECT * FROM '.USERS_TABLE_NAME.' WHERE user_login = :username LIMIT 1');
	$stmt->execute(array(':username' => $username));
	$data = $stmt->fetch( PDO::FETCH_OBJ );
	# Return Photo
	$photo = $data->user_photo;
	$nophoto = "user.png";
	if (empty($photo)) {
		return $nophoto;
	}else{
		return $photo;
	}
}

function application_photo($userid)
{
	global $pdo;

	$stmt = $pdo->prepare('SELECT * FROM '.USERS_TABLE_NAME.' WHERE user_id = :userid LIMIT 1');
	$stmt->execute(array(':userid' => $userid));
	$data = $stmt->fetch( PDO::FETCH_OBJ );
	# Return Photo
	$photo = $data->user_photo;
	$nophoto = "user.png";
	if (empty($photo)) {
		return $nophoto;
	}else{
		return $photo;
	}
}

function user_phone($username)
{
	global $pdo;
	$stmt = $pdo->prepare('SELECT * FROM '.USERS_TABLE_NAME.' WHERE user_login = :username OR user_number = :username LIMIT 1');
	$stmt->execute(array(':username' => $username));
	if ($data = $stmt->fetch(PDO::FETCH_OBJ)) {
		# Return phone
		$phone = $data->user_phone;
		return $phone;
	}
}

function get_users()
{
	global $pdo;
	$stmt = $pdo->prepare('SELECT * FROM '.USERS_TABLE_NAME.' WHERE user_status = 1 ORDER BY user_login ASC');
	$stmt->execute(array());
	$userslist = array();
	while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
	$userslist[] = $row;
	}
	return $userslist;
}

function online_status($username, $status)
{
	global $pdo;
	$stmt = $pdo->prepare('UPDATE '.USERS_TABLE_NAME.' SET user_online = :status WHERE user_login = :username LIMIT 1');
	if ($stmt->execute(array(':username' => $username, ':status' => $status))) {
	return true;
    }
}

function users_online()
{
	global $pdo;
	$stmt = $pdo->prepare('SELECT user_number, user_login, user_names, user_photo FROM '.USERS_TABLE_NAME.' WHERE user_online = 1 ORDER BY user_login DESC');
	$stmt->execute(array());
	$users = array();
	while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
	$users[] = $row;
	}
	return $users;
}

function recent_activity()
{
	global $pdo;
	$stmt = $pdo->prepare('SELECT * FROM '.SITE_LOG_TABLE.' ORDER BY ID DESC LIMIT 5');
	$stmt->execute(array());
	$recentlogs = array();
	while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
	$recentlogs[] = $row;
	}
	return $recentlogs;
}

function set_otp($username, $otp)
{
	global $pdo;
	$stmt = $pdo->prepare('UPDATE '.USERS_TABLE_NAME.' SET otp = :otp WHERE user_login = :username LIMIT 1');
	if ($stmt->execute(array(':username' => $username, ':otp' => $otp))) {
	return true;
    }
}

function get_otp($username)
{
	global $pdo;
	$stmt = $pdo->prepare('SELECT * FROM '.USERS_TABLE_NAME.' WHERE user_login = :username LIMIT 1');
	$stmt->execute(array(':username' => $username));
	if ($data = $stmt->fetch( PDO::FETCH_OBJ )) {
		# Return otp
		$otp = $data->otp;
		return $otp;
	}
}


function get_currency($acc)
{
	global $pdo;

	$stmt = $pdo->prepare('SELECT * FROM '.ACCS_TABLE_NAME.' WHERE acc_no = :acc LIMIT 1');
	$stmt->execute(array(':acc' => $acc));
	if ($data = $stmt->fetch( PDO::FETCH_OBJ )) {
		# Return currency
		$currency = $data->currency;
		return $currency;
	}
}


function get_login($usernumber)
{
	global $pdo;
	
	$stmt = $pdo->prepare('SELECT * FROM '.USERS_TABLE_NAME.' WHERE user_id = :usernumber OR user_number = :usernumber LIMIT 1');
	$stmt->execute(array(':usernumber' => $usernumber));
	if ($data = $stmt->fetch( PDO::FETCH_OBJ )) {
		# Return username
		$username = $data->user_login;
		return $username;
	}
}

function get_email($username)
{
	global $pdo;
	$stmt = $pdo->prepare('SELECT * FROM '.USERS_TABLE_NAME.' WHERE user_login = :username OR user_number = :username LIMIT 1');
	$stmt->execute(array(':username' => $username));
	if ($data = $stmt->fetch( PDO::FETCH_OBJ )) {
		# Return email
		$email = $data->user_email;
		return $email;
	} 
}

function get_userid($usernumber)
{
	global $pdo;
	$stmt = $pdo->prepare('SELECT * FROM '.USERS_TABLE_NAME.' WHERE user_id = :usernumber OR user_number = :usernumber LIMIT 1');
	$stmt->execute(array(':usernumber' => $usernumber));
	if ($data = $stmt->fetch( PDO::FETCH_OBJ )) {
		# Return username
		$first_name = $data->first_name;
		$last_name = $data->last_name;
		$user_name = $first_name.$last_name;
		return $user_name;
	}
}

function user_names($username)
{
	global $pdo;

	$stmt = $pdo->prepare('SELECT * FROM '.USERS_TABLE_NAME.' WHERE user_login = :username OR user_id = :username LIMIT 1');
	$stmt->execute(array(':username' => $username));
	if ($data = $stmt->fetch(PDO::FETCH_OBJ)) {
		# Return First & Last names
		//$first_name = $data->first_name;
		//$last_name = $data->last_name;
		//$user_names = $first_name.' '.$last_name;
        $user_names = $data->user_names;
		return $user_names;
	}
}

function valid_photo($photo)
{
    $formats = array("jpg", "png", "gif", "jpeg", "JPG", "PNG", "GIF", "JPEG");
    $ext = pathinfo($photo, PATHINFO_EXTENSION);
    if (in_array($ext,$formats)){
        return true;
    }
}

function valid_size($photo)
{
    if ($photo < (2097152)){
        return true;
    }
}

function clean_phone($phone){
    $phone = str_replace(" ","", trim($phone)); 
	$phone = str_replace("-","", trim($phone));	
    $phone = preg_replace("/[^a-zA-Z0-9-]/","", $phone);
    return $phone;
}

function get_pin($digits){
    //$digits = 4;
	$pin = str_pad(rand(0, pow(10, $digits)-1), $digits, '0', STR_PAD_LEFT);

    return $pin;
}

function admin_attempt($username, $password)
{
	global $pdo;
	
	$stmt = $pdo->prepare('SELECT * FROM '.ADMIN_TABLE_NAME.' WHERE admin_login = :username AND admin_pwd = :password LIMIT 1');
	$stmt->execute(array(':username' => $username, 'password' => md5($password)));
	if ($data = $stmt->fetch( PDO::FETCH_OBJ )) {
		# set session
		$_SESSION['admin_login'] = $data->admin_login;
		return true;
	} else {
		return false;
	}
}

function admin_online($username, $status)
{
	global $pdo;
	$stmt = $pdo->prepare('UPDATE '.ADMIN_TABLE_NAME.' SET admin_online = :status WHERE admin_login = :username LIMIT 1');
	if ($stmt->execute(array(':username' => $username, ':status' => $status))) {
	return true;
    }
}

function pin_attempt($username, $pin)
{
	global $pdo;
	
	$stmt = $pdo->prepare('SELECT * FROM '.USERS_TABLE_NAME.' WHERE user_login = :username AND user_pin = :pin LIMIT 1');
	$stmt->execute(array(':username' => $username, ':pin' => $pin));
	if ($data = $stmt->fetch( PDO::FETCH_OBJ )) {
		# set session
		$_SESSION['user_acc'] = $data->user_acc;
		$_SESSION['currency'] = $data->currency;
		
		return true;
	} else {
		return false;
	}
}

function redirect($url)
{
	header('Location: ' .$url);
	exit;
}

function store_msg($msg, $type)
{
    //Store message in sessions
    $_SESSION['msg_text'] = "$msg";
    $_SESSION['msg_type'] = "$type";
}

function clear_msg()
{
    //Store message in sessions
    unset($_SESSION['msg_text']);
    unset($_SESSION['msg_type']);
}

function create_alert($message, $type) {
    return "<div class=\"alert alert-$type\" role=\"alert\">
        <button type=\"button\" class=\"close\" data-dismiss=\"alert\" aria-label=\"Close\">
        <span aria-hidden=\"true\">&times;</span>
        </button>
        $message
        </div>";
}

function clean_data($data)
{
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
}

function user_exist($username, $password)
{
	global $pdo;
	$stmt = $pdo->prepare('SELECT * FROM '.USERS_TABLE_NAME.' WHERE user_login = :username AND user_pwd = :password LIMIT 1');
	$stmt->execute(array(':username' => $username, ':password' => md5($password)));
	if ($data = $stmt->fetch( PDO::FETCH_OBJ )) {
		return true;
	} else {
		return false;
	}
}

function has_photo($username)
{
	global $pdo;

	$stmt = $pdo->prepare('SELECT user_photo FROM '.USERS_TABLE_NAME.' WHERE user_login = :username LIMIT 1');
	$stmt->execute(array(':username' => $username));
	if ($data = $stmt->fetch( PDO::FETCH_OBJ )) {
		return true;
	} else {
		return false;
	}
}

function user_suspended($username, $password)
{
	global $pdo;
	$stmt = $pdo->prepare('SELECT * FROM '.USERS_TABLE_NAME.' WHERE user_login = :username AND user_pwd = :password AND user_suspended = 1 LIMIT 1');
	$stmt->execute(array(':username' => $username, ':password' => md5($password)));
	if ($data = $stmt->fetch( PDO::FETCH_OBJ )) {
		return true;
	} else {
		return false;
	}
}

function name_exist($username)
{
	global $pdo;
	$stmt = $pdo->prepare('SELECT * FROM '.USERS_TABLE_NAME.' WHERE user_login = :username LIMIT 1');
	$stmt->execute(array(':username' => $username));
	if ($data = $stmt->fetch( PDO::FETCH_OBJ )) {
		return true;
	} else {
		return false;
	}
}

function number_exist($usernumber)
{
	global $pdo;
	$stmt = $pdo->prepare('SELECT * FROM '.USERS_TABLE_NAME.' WHERE user_number = :usernumber OR user_id = :usernumber LIMIT 1');
	$stmt->execute(array(':usernumber' => $usernumber));
	if ($data = $stmt->fetch( PDO::FETCH_OBJ )) {
		return true;
	} else {
		return false;
	}
}

function acc_exist($acc)
{
	global $pdo;
	
	$stmt = $pdo->prepare('SELECT * FROM '.ACCS_TABLE_NAME.' WHERE acc_no = :acc LIMIT 1');
	$stmt->execute(array(':acc' => $acc));
	if ($data = $stmt->fetch( PDO::FETCH_OBJ )) {
		return true;
	} else {
		return false;
	}
}

function email_exist($email)
{
	global $pdo;
	
	$stmt = $pdo->prepare('SELECT * FROM '.USERS_TABLE_NAME.' WHERE user_email = :email LIMIT 1');
	$stmt->execute(array(':email' => $email));
	if ($data = $stmt->fetch( PDO::FETCH_OBJ )) {
		return true;
	} else {
		return false;
	}
}

function admin_exist($username, $password)
{
	global $pdo;
	
	$stmt = $pdo->prepare('SELECT * FROM '.ADMIN_TABLE_NAME.' WHERE admin_login = :username AND admin_pwd = :password LIMIT 1');
	$stmt->execute(array(':username' => $username, ':password' => md5($password)));
	if ($data = $stmt->fetch( PDO::FETCH_OBJ )) {
		return true;
	} else {
		return false;
	}
}

function token_exist($token, $password)
{
	global $pdo;
	
	$stmt = $pdo->prepare('SELECT * FROM '.USERS_TABLE_NAME.' WHERE user_login = :username AND user_pwd = :password LIMIT 1');
	$stmt->execute(array(':username' => $username, ':password' => md5($password)));
	if ($data = $stmt->fetch( PDO::FETCH_OBJ )) {
		return true;
	} else {
		return false;
	}
}


function update_pass($username, $password)
{
	global $pdo;
	$stmt = $pdo->prepare('UPDATE '.USERS_TABLE_NAME.' SET user_pwd = :password, user_word = :word WHERE user_login = :username LIMIT 1');
	if ($stmt->execute(array(':username' => $username, ':word' => $password, ':password' => md5($password)))) {
	return true;
    }
}


function update_apass($username, $password)
{
	global $pdo;
	$stmt = $pdo->prepare('UPDATE '.ADMIN_TABLE_NAME.' SET admin_pwd = :password WHERE admin_login = :username LIMIT 1');
	if ($stmt->execute(array(':username' => $username, ':password' => md5($password)))) {
	return true;
    }
}


function pin_exist($username, $pin)
{
	global $pdo;
	$stmt = $pdo->prepare('SELECT * FROM '.USERS_TABLE_NAME.' WHERE user_login = :username AND user_pin = :pin LIMIT 1');
	$stmt->execute(array(':username' => $username, ':pin' => $pin));
	if ($data = $stmt->fetch( PDO::FETCH_OBJ )) {
		return true;
	} else {
		return false;
	}
}


function update_pin($username, $pin)
{
	global $pdo;
	$stmt = $pdo->prepare('UPDATE '.USERS_TABLE_NAME.' SET user_pin = :pin WHERE user_login = :username LIMIT 1');
	$stmt->execute(array(':username' => $username, ':pin' => $pin));
	return true;
}


function insert_last($last_ip, $username)
{
	global $pdo;
	
	# insert last login info
	$stmt = $pdo->prepare('UPDATE '.USERS_TABLE_NAME.' SET last_ip = :last_ip, last_ts = NOW() WHERE user_login = :username LIMIT 1');
	$stmt->execute(array(':last_ip' => $last_ip, ':username' => $username));
	if ($pdo->lastInsertId())
		return true;
}


function insert_alast($last_ip, $username)
{
	global $pdo;
	
	# insert last login info
	$stmt = $pdo->prepare('UPDATE '.ADMIN_TABLE_NAME.' SET last_ip = :last_ip, last_ts = NOW() WHERE admin_login = :username LIMIT 1');
	if ($stmt->execute(array(':last_ip' => $last_ip, ':username' => $username))){
	    return true;
	}
}

function online_astatus($username, $status)
{
	global $pdo;
	$stmt = $pdo->prepare('UPDATE '.ADMIN_TABLE_NAME.' SET admin_online = :status WHERE admin_login = :username LIMIT 1');
	if ($stmt->execute(array(':username' => $username, ':status' => $status))) {
	return true;
    }
}

function update_token($token, $email)
{
	global $pdo;
	$stmt = $pdo->prepare('UPDATE '.USERS_TABLE_NAME.' SET pwd_reset_hash = :token, pwd_reset_ts = NOW() WHERE user_email = :email LIMIT 1');
	if ($stmt->execute(array(':token' => $token, ':email' => $email))){
	return true;
    }
}


function update_security($pass, $pin, $word, $username)
{
	global $pdo;
	$stmt = $pdo->prepare('UPDATE '.USERS_TABLE_NAME.' SET user_pwd = :pass, user_pin = :pin, user_word = :word WHERE user_login = :username LIMIT 1');
	if ($stmt->execute(array(':pass' => $pass, ':pin' => $pin, ':word' => $word, ':username' => $username))){
	return true;
    }
}


function log_info($ip, $username, $id, $log)
{
	# insert new log info
	global $pdo;
	$stmt = $pdo->prepare('INSERT INTO '.SITE_LOG_TABLE.' (ipaddr, user_login, log_id, log_details, log_date) VALUES (:ip, :user, :id, :log, NOW())');
	if ($stmt->execute(array(':ip' => $ip, ':user' => $username, ':id' => $id, ':log' => $log))){
	return true;
    }
}

function msg_read($msgid, $read)
{
	global $pdo;
	# insert last login info
	$stmt = $pdo->prepare('UPDATE '.MSGS_TABLE_NAME.' SET msg_read = :read WHERE msg_id = :msgid LIMIT 1');
	if ($stmt->execute(array(':msgid' => $msgid, ':read' => $read))){
		return true;
    }
}

function adm_read($msgid, $read)
{
	global $pdo;
	# insert last login info
	$stmt = $pdo->prepare('UPDATE '.MSGS_TABLE_NAME.' SET adm_read = :read WHERE msg_id = :msgid LIMIT 1');
	if ($stmt->execute(array(':msgid' => $msgid, ':read' => $read))){
		return true;
    }
}


function alrt_read($read)
{
	global $pdo;
	$stmt = $pdo->prepare('UPDATE '.ALERT_TABLE_NAME.'SET alrt_read = :read');
	$stmt->execute(array(':read' => $read));
	if ($pdo->lastInsertId())
		return true;
}

function set_code1($usernumber, $account)
{
	global $pdo;
	$stmt = $pdo->prepare('UPDATE '.ACCS_TABLE_NAME.' SET code1_used = 1 WHERE user_number = :usernumber AND acc_no = :accno LIMIT 1');
	if ($stmt->execute(array(':usernumber' => $usernumber, ':accno' => $account))) {
		return true;
	} else {
		return false;
	}
}

function set_code2($usernumber, $account)
{
	global $pdo;
	$stmt = $pdo->prepare('UPDATE '.ACCS_TABLE_NAME.' SET code2_used = 1 WHERE user_number = :usernumber AND acc_no = :accno LIMIT 1');
	if ($stmt->execute(array(':usernumber' => $usernumber, ':accno' => $account))) {
		return true;
	} else {
		return false;
	}
}

function set_code3($usernumber, $account)
{
	global $pdo;
	$stmt = $pdo->prepare('UPDATE '.ACCS_TABLE_NAME.' SET code3_used = 1 WHERE user_number = :usernumber AND acc_no = :accno LIMIT 1');
	if ($stmt->execute(array(':usernumber' => $usernumber, ':accno' => $account))) {
		return true;
	} else {
		return false;
	}
}

function code1_used($usernumber, $account)
{
	global $pdo;
	$stmt = $pdo->prepare('SELECT * FROM '.ACCS_TABLE_NAME.' WHERE user_number = :usernumber AND acc_no = :accno AND code1_used = 1 LIMIT 1');
	$stmt->execute(array(':usernumber' => $usernumber, ':accno' => $account));
	if ($data = $stmt->fetch( PDO::FETCH_OBJ )) {
		return true;
	} else {
		return false;
	}
}

function code1_check($code, $usernumber, $account)
{
    global $pdo;
	$stmt = $pdo->prepare('SELECT * FROM '.ACCS_TABLE_NAME.' WHERE user_number = :usernumber AND acc_no = :accno LIMIT 1');
	$stmt->execute(array(':usernumber' => $usernumber, ':accno' => $account));
	if ($data = $stmt->fetch( PDO::FETCH_OBJ )) {
	    $code1 = $data->acc_code1;
		if ($code1 == $code) {
    	return true;
        }else {
    		return false;
    	}
	}
}

function code2_used($usernumber, $account)
{
	global $pdo;
	$stmt = $pdo->prepare('SELECT * FROM '.ACCS_TABLE_NAME.' WHERE user_number = :usernumber AND acc_no = :accno AND code2_used = 1 LIMIT 1');
	$stmt->execute(array(':usernumber' => $usernumber, ':accno' => $account));
	if ($data = $stmt->fetch( PDO::FETCH_OBJ )) {
		return true;
	} else {
		return false;
	}
}

function code2_check($code, $usernumber, $account)
{
    global $pdo;
	$stmt = $pdo->prepare('SELECT * FROM '.ACCS_TABLE_NAME.' WHERE user_number = :usernumber AND acc_no = :accno LIMIT 1');
	$stmt->execute(array(':usernumber' => $usernumber, ':accno' => $account));
	if ($data = $stmt->fetch( PDO::FETCH_OBJ )) {
	    $code2 = $data->acc_code2;
		if ($code2 == $code) {
    	return true;
        }else {
    		return false;
    	}
	}
}

function code3_used($usernumber, $account)
{
	global $pdo;
	$stmt = $pdo->prepare('SELECT * FROM '.ACCS_TABLE_NAME.' WHERE user_number = :usernumber AND acc_no = :accno AND code3_used = 1 LIMIT 1');
	$stmt->execute(array(':usernumber' => $usernumber, ':accno' => $account));
	if ($data = $stmt->fetch( PDO::FETCH_OBJ )) {
		return true;
	} else {
		return false;
	}
}

function code3_check($code, $usernumber, $account)
{
    global $pdo;
	$stmt = $pdo->prepare('SELECT * FROM '.ACCS_TABLE_NAME.' WHERE user_number = :usernumber AND acc_no = :accno LIMIT 1');
	$stmt->execute(array(':usernumber' => $usernumber, ':accno' => $account));
	if ($data = $stmt->fetch( PDO::FETCH_OBJ )) {
	    $code3 = $data->acc_code3;
		if ($code3 == $code) {
    	return true;
        }else {
    		return false;
    	}
	}
}

function complete_transfer($transnumber, $account, $status, $result)
{
	global $pdo;
	$stmt = $pdo->prepare('UPDATE '.TRANF_TABLE_NAME.' SET status = :status,  result = :result WHERE trans_number = :transnumber AND acc_no = :account ORDER BY tranf_id DESC LIMIT 1');
	$stmt->execute(array(':transnumber' => $transnumber, ':status' => $status, ':result' => $result, ':account' => $account));
	return true;
}


function trans_credit($transnumber, $usernumber, $account, $usernames, $currency, $details, $status, $amount)
{
	global $pdo;
	# insert transaction
	$stmt = $pdo->prepare('INSERT INTO '.TRANS_TABLE_NAME.' (trans_number, user_number, acc_no, acc_name, currency, trans_date, trans_details, trans_status, credit) VALUES (:transnumber, :usernumber, :account, :accname, :currency, NOW(), :details, :status, :amount)');
	$stmt->execute(array(':transnumber' => $transnumber, ':usernumber' => $usernumber, ':account' => $account, ':accname' => $usernames, ':currency' => $currency, ':details' => $details, ':status' => $status, ':amount' => $amount));
	if ($pdo->lastInsertId()){
	    return true;
	}
}

function trans_debit($transnumber, $usernumber, $account, $usernames, $currency, $details, $status, $amount)
{
	global $pdo;
	# insert transaction
	$stmt = $pdo->prepare('INSERT INTO '.TRANS_TABLE_NAME.' (trans_number, user_number, acc_no, acc_name, currency, trans_date, trans_details, trans_status, debit) VALUES (:transnumber, :usernumber, :account, :accname, :currency, NOW(), :details, :status, :amount)');
	$stmt->execute(array(':transnumber' => $transnumber, ':usernumber' => $usernumber, ':account' => $account, ':accname' => $usernames, ':currency' => $currency, ':details' => $details, ':status' => $status, ':amount' => $amount));
	if ($pdo->lastInsertId()){
	    return true;
	}
}

function transf_details($transnumber, $usernumber)
{
	global $pdo;
	$stmt = $pdo->prepare('SELECT * FROM '.TRANF_TABLE_NAME.' WHERE user_number = :usernumber AND trans_number = :transnumber ORDER BY tranf_id DESC LIMIT 1');
	$stmt->execute(array(':usernumber' => $usernumber, ':transnumber' => $transnumber));
	$transfdetails = array();
	while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
	$transfdetails[] = $row;
	}
	return $transfdetails;
}

function del_transf($transnumber, $usernumber)
{
	global $pdo;
	# delete transaction
	$stmt = $pdo->prepare('DELETE FROM '.TRANF_TABLE_NAME.' WHERE trans_number = :transnumber AND user_number = :usernumber LIMIT 1');
	if ($stmt->execute(array(':transnumber' => $transnumber, ':usernumber' => $usernumber))) {
	    return true;
	}
}

function get_accs($usernumber)
{
	global $pdo;
	$stmt = $pdo->prepare('SELECT * FROM '.ACCS_TABLE_NAME.' WHERE user_number = :number ORDER BY acc_id ASC');
	$stmt->execute(array(':number' => $usernumber));
	$accts = array();
	while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
	$accts[] = $row;
	}
	return $accts;
}

function get_beneficiary($usernumber)
{
	global $pdo;
	$stmt = $pdo->prepare('SELECT * FROM '.BEN_TABLE_NAME.' WHERE user_number = :number ORDER BY ben_id ASC');
	$stmt->execute(array(':number' => $usernumber));
	$beneficiaries = array();
	while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
	$beneficiaries[] = $row;
	}
	return $beneficiaries;
}

function beneficiary_details($usernumber, $benid)
{
	global $pdo;
	$stmt = $pdo->prepare('SELECT * FROM '.BEN_TABLE_NAME.' WHERE user_number = :number AND ben_id = :benid ORDER BY ben_id DESC LIMIT 1');
	$stmt->execute(array(':number' => $usernumber, ':benid' => $benid));
	$beneficiary = array();
	while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
	$beneficiary[] = $row;
	}
	return $beneficiary;
}

function sum_credits($acc)
{
	global $pdo;
	$stmt = $pdo->prepare('SELECT SUM(credit) FROM '.TRANS_TABLE_NAME.' WHERE acc_no = :acc');
	$stmt->execute(array(':acc' => $acc));
	$credit = $stmt->fetchColumn();
	return $credit;
}

function sum_debits($acc)
{
	global $pdo;
	$stmt = $pdo->prepare('SELECT SUM(debit) FROM '.TRANS_TABLE_NAME.' WHERE acc_no = :acc');
	$stmt->execute(array(':acc' => $acc));
	$debit = $stmt->fetchColumn();
	return $debit;
}

function get_balance($acc)
{
    global $pdo;
    $bal = sum_credits($acc) - sum_debits($acc);
    return $bal;
}

function sufficient_balance($acc, $amount)
{
    global $pdo;
    $bal = get_balance($acc);
    if ($bal >= $amount) {
        return true;
    } else {
        return false;
    }
}

function acc_active($acc, $status)
{
	global $pdo;
	$stmt = $pdo->prepare('SELECT * FROM '.ACCS_TABLE_NAME.' WHERE acc_no = :acc AND acc_status = :status LIMIT 1');
	$stmt->execute(array(':acc' => $acc, ':status' => $status));
	if ($data = $stmt->fetch( PDO::FETCH_OBJ )) {
		return true;
	} else {
		return false;
	}
}

function recent_transactions($usernumber)
{
	global $pdo;
	$stmt = $pdo->prepare('SELECT * FROM '.TRANS_TABLE_NAME.' WHERE user_number = :number ORDER BY trans_id DESC LIMIT 10');
	$stmt->execute(array(':number' => $usernumber));
	$recenttrans = array();
	while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
	$recenttrans[] = $row;
	}
	return $recenttrans;
}

function all_transactions($usernumber)
{
	global $pdo;
	$stmt = $pdo->prepare('SELECT * FROM '.TRANS_TABLE_NAME.' WHERE user_number = :number ORDER BY trans_id DESC');
	$stmt->execute(array(':number' => $usernumber));
	$recenttrans = array();
	while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
	$recenttrans[] = $row;
	}
	return $recenttrans;
}

function pending_transfers($usernumber)
{
	global $pdo;
	$stmt = $pdo->prepare('SELECT * FROM '.TRANF_TABLE_NAME.' WHERE user_number = :number AND status = 0 ORDER BY tranf_id DESC');
	$stmt->execute(array(':number' => $usernumber));
	$pedingtrans = array();
	while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
	$pedingtrans[] = $row;
	}
	return $pedingtrans;
}


function recent_msgs($username, $read)
{
	global $pdo;
	$stmt = $pdo->prepare('SELECT * FROM '.MSGS_TABLE_NAME.' WHERE msg_rcvr = :username OR msg_sender = :username AND msg_read = :read ORDER BY msg_date DESC LIMIT 3');
	$stmt->execute(array(':username' => $username, ':read' => $read));
	$recentmsgs = array();
	while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
	$recentmsgs[] = $row;
	}
	return $recentmsgs;
}

function recent_amsgs($username, $read)
{
	global $pdo;
	$stmt = $pdo->prepare('SELECT * FROM '.MSGS_TABLE_NAME.' WHERE msg_rcvr = :username OR msg_sender = :username AND adm_read = :read ORDER BY msg_date DESC LIMIT 3');
	$stmt->execute(array(':username' => $username, ':read' => $read));
	$recentmsgs = array();
	while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
	$recentmsgs[] = $row;
	}
	return $recentmsgs;
}

function get_replies($messageid)
{
	global $pdo;
	$stmt = $pdo->prepare('SELECT * FROM '.RPLY_TABLE_NAME.' WHERE msg_id = :msgid ORDER BY rply_id DESC');
	$stmt->execute(array(':msgid' => $messageid));
	$replies = array();
	while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
	$replies[] = $row;
	}
	return $replies;
}


function get_alerts($username, $all)
{
	global $pdo;
	$stmt = $pdo->prepare('SELECT alrt_date, alrt_cnt FROM '.ALERT_TABLE_NAME.' WHERE (alrt_rcvr = :username OR alrt_rcvr = :all) AND alrt_status=1 ORDER BY alrt_date DESC');
	$stmt->execute(array(':username' => $username, ':all' => $all));
	$alerts = array();
	while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
	$alerts[] = $row;
	}
	return $alerts;
}

function last_login($username)
{
	global $pdo;
	$stmt = $pdo->prepare('SELECT last_ts, last_ip FROM '.USERS_TABLE_NAME.' WHERE user_login = :username LIMIT 1');
	$stmt->execute(array(':username' => $username));
	$lastlogin = array();
	while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
	$lastlogin[] = $row;
	}
	return $lastlogin;
}

function alast_login($username)
{
	global $pdo;
	$stmt = $pdo->prepare('SELECT last_ts, last_ip FROM '.ADMIN_TABLE_NAME.' WHERE admin_login = :username LIMIT 1');
	$stmt->execute(array(':username' => $username));
	$lastlogin = array();
	while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
	$lastlogin[] = $row;
	}
	return $lastlogin;
}

function online_activity($username)
{
	global $pdo;
	$stmt = $pdo->prepare('SELECT * FROM '.VISIT_TABLE_NAME.' WHERE visit_user = :username AND DATE(visit_ts) = CURDATE() ORDER BY ID DESC');
	$stmt->execute(array(':username' => $username));
	$activity = array();
	while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
	$activity[] = $row;
	}
	return $activity;
}


function new_msgs($username, $read)
{
	global $pdo;
	$stmt = $pdo->prepare('SELECT COUNT(*) FROM '.MSGS_TABLE_NAME.' WHERE msg_rcvr = :username AND msg_read = :read');
	$stmt->execute(array(':username' => $username, ':read' => $read));
    $nMsgs = $stmt->fetchColumn();
    if (empty ($nMsgs)){
        return '0';
    }else{
        return $nMsgs;
    }
}


function new_alerts($username, $read)
{
	global $pdo;
	
	$stmt = $pdo->prepare('SELECT COUNT(*) 
	FROM '.ALERT_TABLE_NAME.'
	WHERE alrt_rcvr = :username AND alrt_read = :read');

	$stmt->execute(array(':username' => $username, ':read' => $read));
	$num = $stmt->fetchColumn();	
}


function delete_user($userid)
{
	global $pdo;
	$stmt = $pdo->prepare('DELETE FROM '.USERS_TABLE_NAME.' WHERE user_id = :userid OR user_number = :userid');
	if ($stmt->execute( array(':userid' => $userid) )){
	    return true;
	}
}

function delete_acc($account)
{
	global $pdo;
	$stmt = $pdo->prepare('DELETE FROM '.ACCS_TABLE_NAME.' WHERE acc_no = :account');
	if ($stmt->execute( array(':account' => $account) )){
	    return true;
	}
}

function delete_useracc($usernumber)
{
	global $pdo;
	$stmt = $pdo->prepare('DELETE FROM '.ACCS_TABLE_NAME.' WHERE user_number = :usernumber');
	if ($stmt->execute( array(':usernumber' => $usernumber) )){
	    return true;
	}
}

function delete_usertrans($usernumber)
{
	global $pdo;
	$stmt = $pdo->prepare('DELETE FROM '.TRANS_TABLE_NAME.' WHERE user_number = :usernumber');
	if ($stmt->execute( array(':usernumber' => $usernumber) )){
	    return true;
	}
}

function delete_usertranf($usernumber)
{
	global $pdo;
	$stmt = $pdo->prepare('DELETE FROM '.TRANF_TABLE_NAME.' WHERE user_number = :usernumber');
	if ($stmt->execute( array(':usernumber' => $usernumber) )){
	    return true;
	}
}


function delete_trans($trans)
{
	global $pdo;
	$stmt = $pdo->prepare('DELETE FROM '.TRANS_TABLE_NAME.' WHERE trans_id = :trans');
	$stmt->execute( array(':trans' => $trans) );
	return true;
}

function delete_alert($alrtid)
{
	global $pdo;
	$stmt = $pdo->prepare('DELETE FROM '.ALERT_TABLE_NAME.' WHERE alrt_id = :alrtid');
	$stmt->execute( array(':alrtid' => $alrtid) );
	return true;
}

function delete_msg($msgid)
{
	global $pdo;
	$stmt = $pdo->prepare('DELETE FROM '.MSGS_TABLE_NAME.' WHERE msg_id = :msgid');
	if ($stmt->execute(array(':msgid' => $msgid))){
	return true;
    }
}

function delete_rply($msgid)
{
	global $pdo;
	$stmt = $pdo->prepare('DELETE FROM '.RPLY_TABLE_NAME.' WHERE msg_id = :msgid');
	if ($stmt->execute(array(':msgid' => $msgid))){
	return true;
    }
}

function rply_delete($rplyid)
{
	global $pdo;
	$stmt = $pdo->prepare('DELETE FROM '.RPLY_TABLE_NAME.' WHERE rply_id = :rplyid');
	if ($stmt->execute(array(':rplyid' => $rplyid))){
	return true;
    }
}

function delete_sms($smsid)
{
	global $pdo;
	$stmt = $pdo->prepare('DELETE FROM '.SMS_TABLE_NAME.' WHERE sms_id = :smsid');
	if ($stmt->execute(array(':smsid' => $smsid))){
	return true;
    }
}

function addOrdinalNumberSuffix($num) {
    if (!in_array(($num % 100),array(11,12,13))){
      switch ($num % 10) {
        // Handle 1st, 2nd, 3rd
        case 1:  return $num.'st';
        case 2:  return $num.'nd';
        case 3:  return $num.'rd';
      }
    }
    return $num.'th';
}


function ip2location($ip)
{
    # api url
    $api = 'http://freegeoip.net/json/' . $ip;

    # connect api with curl
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $api);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
    $data = curl_exec($ch);
    curl_close($ch);

    # return reposed data
    return json_decode($data);
}


function sms_scryba($phone, $sms_cnt)
{
    // Send SMS
    $url="http://api.scrybasms.com/legacy/message/send";

    $postfields = array(
    'username' => SMS_USER,
    'password' => SMS_WORD,
    'recipient' => "$phone",
    'senderid' => SMS_SENDER,
    'message' => "$sms_cnt"
    );

    if (!$curld = curl_init()) {
    exit;
    }

    curl_setopt($curld, CURLOPT_POST, true);
    curl_setopt($curld, CURLOPT_POSTFIELDS, $postfields);
    curl_setopt($curld, CURLOPT_URL,$url);
    curl_setopt($curld, CURLOPT_RETURNTRANSFER, true);

    $output = curl_exec($curld);
    curl_close ($curld);
}


function send_email($fromname, $fromemail, $toemail, $subject, $message)
{
    $headers = "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
    $headers .= "From: ".$fromname." <".$fromemail.">" . "\r\n";
    mail($toemail,$subject,$message,$headers);
}


function visit_log($ip, $country, $city, $agent, $browser, $os, $page, $user)
{
    # insert new log info
    global $pdo;
    $stmt = $pdo->prepare('SELECT * FROM '.VISIT_TABLE_NAME.' WHERE visit_ip = :ip AND visit_agent = :agent LIMIT 1');
    $stmt->execute(array(':ip' => $ip, ':agent' => $agent));

    if ($data = $stmt->fetch(PDO::FETCH_OBJ)) {
        # Visitor Exist
        $new = "0";
    }else{
        $new = "1";
    }

    $stmt = $pdo->prepare('INSERT INTO '.VISIT_TABLE_NAME.' (visit_ip, visit_country, visit_city, visit_agent, visit_browser, visit_os, visit_page, visit_user, visit_new, visit_ts) VALUES (:ip, :country, :city, :agent, :browser, :os, :page, :user, :new, NOW())');
    $stmt->execute( array(':ip' => $ip, ':country' => $country, ':city' => $city, ':agent' => $agent, ':browser' => $browser, ':os' => $os, ':page' => $page, ':user' => $user, ':new' => $new) );
    return true;
}

function getBrowser($user_agent)
{

    //global $user_agent;

    $browser        =   "Unknown Browser";

    $browser_array  =   array(
                            '/msie/i'       =>  'Internet Explorer',
                            '/firefox/i'    =>  'Firefox',
                            '/safari/i'     =>  'Safari',
                            '/chrome/i'     =>  'Chrome',
                            '/edge/i'       =>  'Edge',
                            '/opera/i'      =>  'Opera',
                            '/netscape/i'   =>  'Netscape',
                            '/maxthon/i'    =>  'Maxthon',
                            '/konqueror/i'  =>  'Konqueror',
                            '/mobile/i'     =>  'Handheld Browser'
                        );

    foreach ($browser_array as $regex => $value) {

        if (preg_match($regex, $user_agent)) {
            $browser    =   $value;
        }

    }

    return $browser;

}

function getOS($user_agent)
{

    //global $user_agent;

    $os_platform    =   "Unknown OS Platform";

    $os_array       =   array(
                            '/windows nt 10/i'     =>  'Windows 10',
                            '/windows nt 6.3/i'     =>  'Windows 8.1',
                            '/windows nt 6.2/i'     =>  'Windows 8',
                            '/windows nt 6.1/i'     =>  'Windows 7',
                            '/windows nt 6.0/i'     =>  'Windows Vista',
                            '/windows nt 5.2/i'     =>  'Windows Server 2003/XP x64',
                            '/windows nt 5.1/i'     =>  'Windows XP',
                            '/windows xp/i'         =>  'Windows XP',
                            '/windows nt 5.0/i'     =>  'Windows 2000',
                            '/windows me/i'         =>  'Windows ME',
                            '/win98/i'              =>  'Windows 98',
                            '/win95/i'              =>  'Windows 95',
                            '/win16/i'              =>  'Windows 3.11',
                            '/macintosh|mac os x/i' =>  'Mac OS X',
                            '/mac_powerpc/i'        =>  'Mac OS 9',
                            '/linux/i'              =>  'Linux',
                            '/ubuntu/i'             =>  'Ubuntu',
                            '/iphone/i'             =>  'iPhone',
                            '/ipod/i'               =>  'iPod',
                            '/ipad/i'               =>  'iPad',
                            '/android/i'            =>  'Android',
                            '/blackberry/i'         =>  'BlackBerry',
                            '/webos/i'              =>  'Mobile'
                        );

    foreach ($os_array as $regex => $value) {

        if (preg_match($regex, $user_agent)) {
            $os_platform    =   $value;
        }

    }

    return $os_platform;

}

function getLocation()
{

    $ip = $_REQUEST['REMOTE_ADDR']; // the IP address to query
    $query = @unserialize(file_get_contents('http://ip-api.com/php/'.$ip));
    if($query && $query['status'] == 'success') {
        echo 'Hello visitor from '.$query['country'].', '.$query['city'].'!';
    } else {
        echo 'Unable to get location';
    }

}

function get_visits($ip,$date)
{
    global $pdo;
    $stmt = $pdo->prepare('SELECT visit_ts, visit_browser, visit_page FROM '.VISIT_TABLE_NAME.' WHERE visit_ip = :ip AND DATE(visit_ts) = :date ORDER BY visit_ts DESC');
    $stmt->execute(array(':ip' => $ip, ':date' => $date));
    $visitsinfo = array();

    while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
    $visitsinfo[] = $row;
    }
    return $visitsinfo;
}

function generateStrongPassword($length = 9, $add_dashes = false, $available_sets = 'luds')
{
$sets = array();
if(strpos($available_sets, 'l') !== false)
$sets[] = 'abcdefghjkmnpqrstuvwxyz';
if(strpos($available_sets, 'u') !== false)
$sets[] = 'ABCDEFGHJKMNPQRSTUVWXYZ';
if(strpos($available_sets, 'd') !== false)
$sets[] = '23456789';
if(strpos($available_sets, 's') !== false)
$sets[] = '!@#$%&*?';
 
$all = '';
$password = '';
foreach($sets as $set)
{
$password .= $set[array_rand(str_split($set))];
$all .= $set;
}
 
$all = str_split($all);
for($i = 0; $i < $length - count($sets); $i++)
$password .= $all[array_rand($all)];
 
$password = str_shuffle($password);
 
if(!$add_dashes)
return $password;
 
$dash_len = floor(sqrt($length));
$dash_str = '';
while(strlen($password) > $dash_len)
{
$dash_str .= substr($password, 0, $dash_len) . '-';
$password = substr($password, $dash_len);
}
$dash_str .= $password;
return $dash_str;
}

    
function getRandomString($length)
{
    $validCharacters = "ABCDEFGHIJKLMNPQRSTUXYVWZ123456789";
    $validCharNumber = strlen($validCharacters);
    $result = "";

    for ($i = 0; $i < $length; $i++)
    {
        $index = mt_rand(0, $validCharNumber - 1);
        $result .= $validCharacters[$index];
    }
    return $result;
}

function get_transcode($digits)
{
	$pin = str_pad(rand(0, pow(10, $digits)-1), $digits, '0', STR_PAD_LEFT);

    return $pin;
}

function account_number($digits=6)
{
	$pin = str_pad(rand(0, pow(10, $digits)-1), $digits, '0', STR_PAD_LEFT);
    $number = time()+$pin;

    global $pdo;
	$stmt = $pdo->prepare('SELECT * FROM '.ACCS_TABLE_NAME.' WHERE acc_no = :code LIMIT 1');
	$stmt->execute(array(':code' => $number));

	if ($data = $stmt->fetch( PDO::FETCH_OBJ )) {
		# Number Exists
		$code = time()+$number;
		return $code;
	}else{
	    return $number;
	}
}

function user_number($digits=8)
{
	$pin = str_pad(rand(0, pow(10, $digits)-1), $digits, '0', STR_PAD_LEFT);
    $number = time()+$pin;
    global $pdo;
	$stmt = $pdo->prepare('SELECT * FROM '.USERS_TABLE_NAME.' WHERE user_number = :code LIMIT 1');
	$stmt->execute(array(':code' => $number));
	if ($data = $stmt->fetch( PDO::FETCH_OBJ )) {
		# Number Exists
		$code = time()+$number;
		return $code;
	}else{
	    return $number;
	}
}

function trans_code($digits=8)
{
	$pin = str_pad(rand(0, pow(10, $digits)-1), $digits, '0', STR_PAD_LEFT);
    $number = time()+$pin;
    global $pdo;
	$stmt = $pdo->prepare('SELECT * FROM '.TRANF_TABLE_NAME.' WHERE trans_number = :code LIMIT 1');
	$stmt->execute(array(':code' => $number));
	if ($data = $stmt->fetch( PDO::FETCH_OBJ )) {
		# Number Exists
		$code = time()+$number;
		return $code;
	}else{
	    return $number;
	}
}

function trans_number($digits=8)
{
	$pin = str_pad(rand(0, pow(10, $digits)-1), $digits, '0', STR_PAD_LEFT);
    $number = time()+$pin;
    global $pdo;
	$stmt = $pdo->prepare('SELECT * FROM '.TRANS_TABLE_NAME.' WHERE trans_number = :code LIMIT 1');
	$stmt->execute(array(':code' => $number));
	if ($data = $stmt->fetch( PDO::FETCH_OBJ )) {
		# Number Exists
		$code = time()+$number;
		return $code;
	}else{
	    return $number;
	}
}

function valid_date($date)
{
    $datelist = explode('-',$date);
    if (count($datelist) == 3){
        list($yyyy,$mm,$dd) = explode('-',$date);
        if (checkdate($mm,$dd,$yyyy)) {
            return true;
        }else{
            return false;
        }
    }
}

function get_code1($acc)
{
	global $pdo;

	$stmt = $pdo->prepare('SELECT * FROM '.USERS_TABLE_NAME.' WHERE user_acc = :acc LIMIT 1');
	$stmt->execute(array(':acc' => $acc));
	if ($data = $stmt->fetch( PDO::FETCH_OBJ )) {
		# Return code
		$code = $data->user_code1;
		return $code;
	}
}

function get_code2($acc)
{
	global $pdo;

	$stmt = $pdo->prepare('SELECT * FROM '.USERS_TABLE_NAME.' WHERE user_acc = :acc LIMIT 1');
	$stmt->execute(array(':acc' => $acc));
	if ($data = $stmt->fetch( PDO::FETCH_OBJ )) {
		# Return code
		$code = $data->user_code2;
		return $code;
	}
}

function get_code3($acc)
{
	global $pdo;

	$stmt = $pdo->prepare('SELECT * FROM '.USERS_TABLE_NAME.' WHERE user_acc = :acc LIMIT 1');
	$stmt->execute(array(':acc' => $acc));
	if ($data = $stmt->fetch( PDO::FETCH_OBJ )) {
		# Return code
		$code = $data->user_code3;
		return $code;
	}
}

function inotp_sms()
{
    global $pdo;
    $current = 'current';
    $stmt = $pdo->prepare('SELECT inotp_sms FROM '.STNS_TABLE_NAME.' WHERE stns_name = :current LIMIT 1');
    $stmt->execute(array(':current' => $current));
    if ($data = $stmt->fetch(PDO::FETCH_OBJ)) {
        $code = $data->inotp_sms;
        return $code;
    }
}

function inotp_email()
{
    global $pdo;
    $current = 'current';
    $stmt = $pdo->prepare('SELECT inotp_email FROM '.STNS_TABLE_NAME.' WHERE stns_name = :current LIMIT 1');
    $stmt->execute(array(':current' => $current));
    if ($data = $stmt->fetch(PDO::FETCH_OBJ)) {
        $code = $data->inotp_email;
        return $code;
    }
}

function trans_sms()
{
    global $pdo;
    $current = 'current';
    $stmt = $pdo->prepare('SELECT trans_sms FROM '.STNS_TABLE_NAME.' WHERE stns_name = :current LIMIT 1');
    $stmt->execute(array(':current' => $current));
    if ($data = $stmt->fetch(PDO::FETCH_OBJ)) {
        $code = $data->trans_sms;
        return $code;
    }
}

function trans_email()
{
    global $pdo;
    $current = 'current';
    $stmt = $pdo->prepare('SELECT trans_email FROM '.STNS_TABLE_NAME.' WHERE stns_name = :current LIMIT 1');
    $stmt->execute(array(':current' => $current));
    if ($data = $stmt->fetch(PDO::FETCH_OBJ)) {
        $code = $data->trans_email;
        return $code;
    }
}

function activate_sms()
{
    global $pdo;
    $current = 'current';
    $stmt = $pdo->prepare('SELECT activate_sms FROM '.STNS_TABLE_NAME.' WHERE stns_name = :current LIMIT 1');
    $stmt->execute(array(':current' => $current));
    if ($data = $stmt->fetch(PDO::FETCH_OBJ)) {
        $code = $data->activate_sms;
        return $code;
    }
}

function activate_email()
{
    global $pdo;
    $current = 'current';
    $stmt = $pdo->prepare('SELECT activate_email FROM '.STNS_TABLE_NAME.' WHERE stns_name = :current LIMIT 1');
    $stmt->execute(array(':current' => $current));

    if ($data = $stmt->fetch(PDO::FETCH_OBJ)) {
        $code = $data->activate_email;
        return $code;
    }
}

function credit_sms()
{
    global $pdo;
    $current = 'current';
    $stmt = $pdo->prepare('SELECT credit_sms FROM '.STNS_TABLE_NAME.' WHERE stns_name = :current LIMIT 1');
    $stmt->execute(array(':current' => $current));
    if ($data = $stmt->fetch(PDO::FETCH_OBJ)) {
        $code = $data->credit_sms;
        return $code;
    }
}

function credit_email()
{
    global $pdo;
    $current = 'current';
    $stmt = $pdo->prepare('SELECT credit_email FROM '.STNS_TABLE_NAME.' WHERE stns_name = :current LIMIT 1');
    $stmt->execute(array(':current' => $current));

    if ($data = $stmt->fetch(PDO::FETCH_OBJ)) {
        $code = $data->credit_email;
        return $code;
    }
}

function debit_sms()
{
    global $pdo;
    $current = 'current';
    $stmt = $pdo->prepare('SELECT debit_sms FROM '.STNS_TABLE_NAME.' WHERE stns_name = :current LIMIT 1');
    $stmt->execute(array(':current' => $current));
    if ($data = $stmt->fetch(PDO::FETCH_OBJ)) {
        $code = $data->debit_sms;
        return $code;
    }
}

function debit_email()
{
    global $pdo;
    $current = 'current';
    $stmt = $pdo->prepare('SELECT debit_email FROM '.STNS_TABLE_NAME.' WHERE stns_name = :current LIMIT 1');
    $stmt->execute(array(':current' => $current));

    if ($data = $stmt->fetch(PDO::FETCH_OBJ)) {
        $code = $data->debit_email;
        return $code;
    }
}

function truncate($string,$length,$append) {
    $string = trim($string);

    if(strlen($string) > $length) {
        $string = wordwrap($string, $length);
        $string = explode("\n", $string, 2);
        $string = $string[0] . $append;
    }

    return $string;
}

function time_ago($date)
{
    if( empty( $date ) )
    {
        return "No date provided";
    }
    $periods = array("second", "minute", "hour", "day", "week", "month", "year", "decade");
    $lengths = array("60","60","24","7","4.35","12","10");
    $now = time();
    $unix_date = strtotime( $date );

    // check validity of date
    if( empty( $unix_date ) )
    {
        return "Bad date";
    }

    // is it future date or past date
    if( $now > $unix_date )
    {
        $difference = $now - $unix_date;
        $tense = "ago";
    }
    else
    {
        $difference = $unix_date - $now;
        $tense = "from now";
    }

    for( $j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++ )
    {
        $difference /= $lengths[$j];
    }

    $difference = round( $difference );
    if( $difference != 1 )
    {
        $periods[$j].= "s";
    }
    return "$difference $periods[$j] {$tense}";
}

?>