From 1e63dd1ae61c1e70e33c6cfa5b269c661f3122f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A1bor=20Hojtsy?= Date: Thu, 20 Dec 2007 09:20:41 +0000 Subject: #201536 by chx: centralizing permission checking code in node revision handling, removing lots of duplicate code --- includes/menu.inc | 39 ++++++++++++++++++++++++++++++++++++--- includes/theme.inc | 4 ++-- 2 files changed, 38 insertions(+), 5 deletions(-) (limited to 'includes') diff --git a/includes/menu.inc b/includes/menu.inc index f1b375f52..50352dbcb 100644 --- a/includes/menu.inc +++ b/includes/menu.inc @@ -360,9 +360,12 @@ function menu_execute_active_handler($path = NULL) { * @return * Returns TRUE for success, FALSE if an object cannot be loaded */ -function _menu_load_objects($item, &$map) { - if ($item['load_functions']) { - $load_functions = unserialize($item['load_functions']); +function _menu_load_objects(&$item, &$map) { + if ($load_functions = $item['load_functions']) { + // If someone calls this function twice, then unserialize will fail. + if ($load_functions_unserialized = unserialize($load_functions)) { + $load_functions = $load_functions_unserialized; + } $path_map = $map; foreach ($load_functions as $index => $function) { if ($function) { @@ -373,6 +376,7 @@ function _menu_load_objects($item, &$map) { // some processing. In this case the $function is the key to the // load_function array, and the value is the list of arguments. list($function, $args) = each($function); + $load_functions[$index] = $function; // Some arguments are placeholders for dynamic items to process. foreach ($args as $i => $arg) { @@ -387,6 +391,9 @@ function _menu_load_objects($item, &$map) { // the map. $args[$i] = &$map; } + if (is_int($arg)) { + $args[$i] = isset($path_map[$arg]) ? $path_map[$arg] : ''; + } } array_unshift($args, $value); $return = call_user_func_array($function, $args); @@ -403,6 +410,7 @@ function _menu_load_objects($item, &$map) { $map[$index] = $return; } } + $item['load_functions'] = $load_functions; } return TRUE; } @@ -609,6 +617,31 @@ function _menu_link_translate(&$item) { return $map; } +/** + * Get a loaded object from a router item. + * + * menu_get_object() will provide you the current node on paths like node/5, + * node/5/revisions/48 etc. menu_get_object('user') will give you the user + * account on user/5 etc. + * + * @param $type + * Type of the object. These appear in hook_menu definitons as %type. Core + * provides aggregator_feed, aggregator_category, contact, filter_format, + * forum_term, menu, menu_link, node, taxonomy_vocabulary, user. See the + * relevant {$type}_load function for more on each. Defaults to node. + * @param $position + * The expected position for $type object. For node/%node this is 1, for + * comment/reply/%node this is 2. Defaults to 1. + * @param $path + * See @menu_get_item for more on this. Defaults to the current path. + */ +function menu_get_object($type = 'node', $position = 1, $path = NULL) { + $router_item = menu_get_item($path); + if (isset($router_item['load_functions'][$position]) && !empty($router_item['map'][$position]) && $router_item['load_functions'][$position] == $type .'_load') { + return $router_item['map'][$position]; + } +} + /** * Render a menu tree based on the current path. * diff --git a/includes/theme.inc b/includes/theme.inc index 45655c58a..42c54783f 100644 --- a/includes/theme.inc +++ b/includes/theme.inc @@ -1736,8 +1736,8 @@ function template_preprocess_page(&$variables) { // Closure should be filled last. $variables['closure'] = theme('closure'); - if ((arg(0) == 'node') && is_numeric(arg(1))) { - $variables['node'] = node_load(arg(1)); + if ($node = menu_get_object()) { + $variables['node'] = $node; } // Compile a list of classes that are going to be applied to the body element. -- cgit v1.2.3