function user_permissions_get($type) { log_debug("inc_user", "Executing user_permissions_get({$type})"); $obj_user = new user_auth(); return $obj_user->permissions_get($type); }
function load_data() { log_debug("menu_main", "Executing load_data()"); /* Fetch an array of all the user permissions. */ log_debug("menu_main", "Fetching array of all the permissions the user has for displaying the menu"); $user_permissions = array(); if (user_online()) { // it's probably the first time we're checking for permissions // we should pre-load them all if needed if (!isset($GLOBALS["cache"]["user"]["perms"])) { $obj_user_auth = new user_auth(); $obj_user_auth->permissions_init(); } // fetch ID of all permissions $sql_obj = new sql_query(); $sql_obj->string = "SELECT id, value FROM permissions"; $sql_obj->execute(); $sql_obj->fetch_array(); // build array of all permissions IDs for the groups the user belongs to. foreach (array_keys($GLOBALS["cache"]["user"]["perms"]) as $type) { if ($GLOBALS["cache"]["user"]["perms"][$type] == 1) { foreach ($sql_obj->data as $data_permids) { if ($data_permids["value"] == $type) { $user_permissions[] = $data_permids["id"]; } } } } // (legacy) For system without a public permissions group, add the ID of 0 $user_permissions[] = "0"; } else { // user is not logged in - select public menu entries only $sql_obj = new sql_query(); $sql_obj->string = "SELECT id FROM `permissions` WHERE value='public' LIMIT 1"; $sql_obj->execute(); if ($sql_obj->num_rows()) { $sql_obj->fetch_array(); $user_permissions[] = $sql_obj->data[0]["id"]; } else { // (legacy) For system without a public permissions group, add the ID of 0 $user_permissionsp[] = "0"; } } // unable to display a menu if there are no permissions if (!$user_permissions) { log_write("debug", "main_menu", "User has no permissions public or private so menu options can not be queried"); return 0; } /* Fetch data for the entire menu from the database We fetch all the data at once, then run though it following the parent value as we run though all the items to determine what menu items need to be shown and in what order. We know that the single loop will match all the menu items correctly, since the menu items are ordered so we run though the order in the same direction. This saves us from having to do heaps of unnessacary loops. :-) */ log_debug("menu_main", "Loading menu from SQL database..."); $sql_menu_obj = new sql_query(); $sql_menu_obj->string = "SELECT link, topic, parent, config FROM menu WHERE permid IN (" . format_arraytocommastring($user_permissions) . ") ORDER BY priority DESC"; $sql_menu_obj->execute(); if (!$sql_menu_obj->num_rows()) { log_debug("menu_main", "No menu entires exist for the current user that they have permission to access"); return 0; } // fetch menu entires $sql_menu_obj->fetch_array(); // array to store the order of the menu items $this->menu_order = array(); // keep track of the topic we are looking for $target_topic = ""; /* Apply config filtering Some applications have the need to be able to enable/disable specific features using boolean options in the config table - by setting the name of the value in the config column on the menu entries, a check will be made, and if the menu entry config option is unset, the menu options will not be displayed. This is typically used for hiding disabled features where for whatever reason, the feature can not be disabled using permissions groups. */ for ($i = 0; $i < $sql_menu_obj->data_num_rows; $i++) { // check feature option (if set) if (!empty($sql_menu_obj->data[$i]["config"])) { @(list($config_name, $config_value) = explode('=', $sql_menu_obj->data[$i]["config"], 2)); if (!$GLOBALS["config"][$config_name]) { // config is disabled for this feature unset($sql_menu_obj->data[$i]); } else { if ($config_value) { // do value matching if ($GLOBALS["config"][$config_name] != $config_value) { // non match, failed unset($sql_menu_obj->data[$i]); } } // default is that menu item is enabled since config option exists } } } /* If the remember option is enabled, check if the page provided exists, if not then we will select the last known good page (from session variables). If it does exist, we set the session variable to the new page. */ if ($this->option_remember) { // check page foreach ($sql_menu_obj->data as $data) { if ($data["link"] == $this->page) { $_SESSION["amberphplib"]["menu"]["page"] = $this->page; } } // set page $this->page = $_SESSION["amberphplib"]["menu"]["page"]; } // loop though the menu items foreach ($sql_menu_obj->data as $data) { // add each item to menu array if ($target_topic != "top") { if (!$target_topic) { // use the page link to find the first target if ($data["link"] == $this->page) { $target_topic = $data["parent"]; $this->menu_order[] = $data["parent"]; } } else { // check the topic type if ($data["topic"] == $target_topic) { $target_topic = $data["parent"]; $this->menu_order[] = $data["parent"]; } } } } // now we reverse the order array, so we can // render the menus in the correct order if ($this->menu_order) { $this->menu_order = array_reverse($this->menu_order); } else { // if we have no sub-menu information, just set // to display the top menu only $this->menu_order = array("top"); } // sort the menu data in the opposite direction for correct rendering $this->menu_structure = array_reverse($sql_menu_obj->data); // return success return 1; }