<?php

function menu_render($menuarray) {
    ksort($menuarray);
	$current_page = $_SERVER['REQUEST_URI'];
    ?>
    
            <?php foreach ($menuarray as $key => $value) { 
			
			
			?>
            <li <?php echo (has_sub($value) ? 'class="nav-item ' : 'class="nav-item '); echo ($current_page == $value["url"] ? ' open ' : ' '); ?>"><a href="<?php echo $value["url"]; ?>" class="nav-link <?php echo (has_sub($value) ? ' nav-toggle ' : ''); ?>"><i class="<?php echo $value["icon"]; ?>"></i> <span class="title"><?php echo $value["label"]; ?></span><?php echo (has_sub($value) ? ' <span class="arrow"></span> ' : ''); ?></a>
            <?php if (has_sub($value)) { ?>
                    <ul class="sub-menu">
            <?php the_sub($value["sub"]); ?>
                    </ul>
        <?php } ?>
            </li>
    <?php } ?>
    <?php
}

function the_head() {
    require_once __DIR__ . '/../tpl/head.php';
}
function the_footer() {
    require_once __DIR__ . '/../tpl/footer.php';
}
function is_login() {
    if (isset($_SESSION['username']) && $_SESSION['username'] != "") {
        return true;
    } else {
        return false;
    }
}
function action_log($user_id,$action,$status){
    global $db;
	$ip_address = get_client_ip();
	$db->bind("user_id", $user_id);
	$db->bind("action", $action);
    $db->bind("ip_address", $ip_address);
    $db->bind("status", $status);
    // Execute
    $db->query("INSERT INTO action_log(user_id,action,ip_address,status) VALUES(:user_id,:action,:ip_address,:status)");
}
function getAllSubdirectories($base) {
    $dir_array = array();
    if (!is_dir($base)) {
        return $dir_array;
    }

    if ($dh = opendir($base)) {
        while (($file = readdir($dh)) !== false) {
            if ($file == '.' || $file == '..')
                continue;

            if (is_dir($base . '/' . $file)) {
                $dir_array[] = $file;
            } else {
                array_merge($dir_array, getAllSubdirectories($base . '/' . $file));
            }
        }
        closedir($dh);
        return $dir_array;
        
    }
}
function get_title($request){
	global $hooks;
	global $menu_array;
	$current_page = $_SERVER['REQUEST_URI'];
	if($_SESSION['role'] == 0){
		$menuarray = $menu_array; 
	}else{
		$menuarray = json_decode(getActiveGroup($_SESSION["user_id"], "menu_json"),true); 
	}
	foreach ($menuarray as $key => $value) { 
		if($current_page == $value["url"]){
			return $value[$request];
		}elseif(isset($value["sub"])){
			foreach ($value["sub"] as $key => $sub) {
				if($current_page == $sub["url"]){
					if($sub[$request] == ""){
						return $hooks->do_action("the_title");
					}else{
						return $sub[$request];
					}
					
				}
			}
		}
	}
	if($request == 'label'){
		return $hooks->do_action("the_title");
	}else{
		return $_SERVER['REQUEST_URI'];
	}
}
function has_sub($array) {
    if (array_key_exists("sub", $array)) {
        return true;
    } else {
        return false;
    }
}

function the_sub($array) {
    ksort($array);
	$current_page = $_SERVER['REQUEST_URI'];
    foreach ($array as $key => $value) {
        ?>
        <li class="<?php echo ($current_page == $value["url"] ? 'start active open"' : '"'); ?>><a  class="nav-link" href="<?php echo $value["url"]; ?>"><i class="<?php echo $value["icon"]; ?>"></i> <span class="title"><?php echo $value["label"]; ?></span></a></li>
    <?php
    }
}
function xToken($length = 8) {
    $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()_-=+;:,.?";
    $password = substr(str_shuffle($chars), 0, $length);
    return $password;
}
function ismscURL($msg, $number){
	$ASCII = 1;
	$UNICODE = 2;
	
	$destination = urlencode($number);
	$message = $msg;
	$message = html_entity_decode($message, ENT_QUOTES, 'utf-8'); 
	$message = urlencode($message);

	$username = urlencode("afiqsyed");
	$password = urlencode("596092as");
	$sender_id = urlencode("66300");
	$type = 2;

	$link = "http://www.isms.com.my/isms_send.php";
	$link .= "?un=$username&pwd=$password&dstno=$destination&msg=$message&type=$type&sendid=$sender_id";

	$http = curl_init($link);

	curl_setopt($http, CURLOPT_RETURNTRANSFER, TRUE);
	$http_result = curl_exec($http);
	$http_status = curl_getinfo($http, CURLINFO_HTTP_CODE);
	curl_close($http);
	
	return $http_result;
}
