Code coverage for /20081101/includes/menu.inc

Line #Times calledCode
1
<?php
2
// $Id: menu.inc,v 1.299 2008/10/31 02:18:22 dries Exp $
3
4
/**
5
 * @file
6
 * API for the Drupal menu system.
7
 */
8
9
/**
10
 * @defgroup menu Menu system
11
 * @{
12
 * Define the navigation menus, and route page requests to code based on
URLs.
13
 *
14
 * The Drupal menu system drives both the navigation system from a user
15
 * perspective and the callback system that Drupal uses to respond to URLs
16
 * passed from the browser. For this reason, a good understanding of the
17
 * menu system is fundamental to the creation of complex modules.
18
 *
19
 * Drupal's menu system follows a simple hierarchy defined by paths.
20
 * Implementations of hook_menu() define menu items and assign them to
21
 * paths (which should be unique). The menu system aggregates these items
22
 * and determines the menu hierarchy from the paths. For example, if the
23
 * paths defined were a, a/b, e, a/b/c/d, f/g, and a/b/h, the menu system
24
 * would form the structure:
25
 * - a
26
 *   - a/b
27
 *     - a/b/c/d
28
 *     - a/b/h
29
 * - e
30
 * - f/g
31
 * Note that the number of elements in the path does not necessarily
32
 * determine the depth of the menu item in the tree.
33
 *
34
 * When responding to a page request, the menu system looks to see if the
35
 * path requested by the browser is registered as a menu item with a
36
 * callback. If not, the system searches up the menu tree for the most
37
 * complete match with a callback it can find. If the path a/b/i is
38
 * requested in the tree above, the callback for a/b would be used.
39
 *
40
 * The found callback function is called with any arguments specified
41
 * in the "page arguments" attribute of its menu item. The
42
 * attribute must be an array. After these arguments, any remaining
43
 * components of the path are appended as further arguments. In this
44
 * way, the callback for a/b above could respond to a request for
45
 * a/b/i differently than a request for a/b/j.
46
 *
47
 * For an illustration of this process, see page_example.module.
48
 *
49
 * Access to the callback functions is also protected by the menu system.
50
 * The "access callback" with an optional "access arguments" of each menu
51
 * item is called before the page callback proceeds. If this returns TRUE,
52
 * then access is granted; if FALSE, then access is denied. Menu items may
53
 * omit this attribute to use the value provided by an ancestor item.
54
 *
55
 * In the default Drupal interface, you will notice many links rendered as
56
 * tabs. These are known in the menu system as "local tasks", and they are
57
 * rendered as tabs by default, though other presentations are possible.
58
 * Local tasks function just as other menu items in most respects. It is
59
 * convention that the names of these tasks should be short verbs if
60
 * possible. In addition, a "default" local task should be provided for
61
 * each set. When visiting a local task's parent menu item, the default
62
 * local task will be rendered as if it is selected; this provides for a
63
 * normal tab user experience. This default task is special in that it
64
 * links not to its provided path, but to its parent item's path instead.
65
 * The default task's path is only used to place it appropriately in the
66
 * menu hierarchy.
67
 *
68
 * Everything described so far is stored in the menu_router table. The
69
 * menu_links table holds the visible menu links. By default these are
70
 * derived from the same hook_menu definitions, however you are free to
71
 * add more with menu_link_save().
72
 */
73
74
/**
75
 * @name Menu flags
76
 * @{
77
 * Flags for use in the "type" attribute of menu items.
78
 */
79
80
/**
81
 * Internal menu flag -- menu item is the root of the menu tree.
82
 */
832366
define('MENU_IS_ROOT', 0x0001);
84
85
/**
86
 * Internal menu flag -- menu item is visible in the menu tree.
87
 */
882366
define('MENU_VISIBLE_IN_TREE', 0x0002);
89
90
/**
91
 * Internal menu flag -- menu item is visible in the breadcrumb.
92
 */
932366
define('MENU_VISIBLE_IN_BREADCRUMB', 0x0004);
94
95
/**
96
 * Internal menu flag -- menu item links back to its parnet.
97
 */
982366
define('MENU_LINKS_TO_PARENT', 0x0008);
99
100
/**
101
 * Internal menu flag -- menu item can be modified by administrator.
102
 */
1032366
define('MENU_MODIFIED_BY_ADMIN', 0x0020);
104
105
/**
106
 * Internal menu flag -- menu item was created by administrator.
107
 */
1082366
define('MENU_CREATED_BY_ADMIN', 0x0040);
109
110
/**
111
 * Internal menu flag -- menu item is a local task.
112
 */
1132366
define('MENU_IS_LOCAL_TASK', 0x0080);
114
115
/**
116
 * @} End of "Menu flags".
117
 */
118
119
/**
120
 * @name Menu item types
121
 * @{
122
 * Menu item definitions provide one of these constants, which are
shortcuts for
123
 * combinations of the above flags.
124
 */
125
126
/**
127
 * Menu type -- A "normal" menu item that's shown in menu and breadcrumbs.
128
 *
129
 * Normal menu items show up in the menu tree and can be moved/hidden by
130
 * the administrator. Use this for most menu items. It is the default value
if
131
 * no menu item type is specified.
132
 */
1332366
define('MENU_NORMAL_ITEM', MENU_VISIBLE_IN_TREE |
MENU_VISIBLE_IN_BREADCRUMB);
134
135
/**
136
 * Menu type -- A hidden, internal callback, typically used for API calls.
137
 *
138
 * Callbacks simply register a path so that the correct function is fired
139
 * when the URL is accessed. They are not shown in the menu.
140
 */
1412366
define('MENU_CALLBACK', MENU_VISIBLE_IN_BREADCRUMB);
142
143
/**
144
 * Menu type -- A normal menu item, hidden until enabled by an
administrator.
145
 *
146
 * Modules may "suggest" menu items that the administrator may enable. They
act
147
 * just as callbacks do until enabled, at which time they act like normal
items.
148
 * Note for the value: 0x0010 was a flag which is no longer used, but this
way
149
 * the values of MENU_CALLBACK and MENU_SUGGESTED_ITEM are separate.
150
 */
1512366
define('MENU_SUGGESTED_ITEM', MENU_VISIBLE_IN_BREADCRUMB | 0x0010);
152
153
/**
154
 * Menu type -- A task specific to the parent item, usually rendered as a
tab.
155
 *
156
 * Local tasks are menu items that describe actions to be performed on
their
157
 * parent item. An example is the path "node/52/edit", which performs the
158
 * "edit" task on "node/52".
159
 */
1602366
define('MENU_LOCAL_TASK', MENU_IS_LOCAL_TASK);
161
162
/**
163
 * Menu type -- The "default" local task, which is initially active.
164
 *
165
 * Every set of local tasks should provide one "default" task, that links
to the
166
 * same path as its parent when clicked.
167
 */
1682366
define('MENU_DEFAULT_LOCAL_TASK', MENU_IS_LOCAL_TASK |
MENU_LINKS_TO_PARENT);
169
170
/**
171
 * @} End of "Menu item types".
172
 */
173
174
/**
175
 * @name Menu status codes
176
 * @{
177
 * Status codes for menu callbacks.
178
 */
179
180
/**
181
 * Internal menu status code -- Menu item was found.
182
 */
1832366
define('MENU_FOUND', 1);
184
185
/**
186
 * Internal menu status code -- Menu item was not found.
187
 */
1882366
define('MENU_NOT_FOUND', 2);
189
190
/**
191
 * Internal menu status code -- Menu item access is denied.
192
 */
1932366
define('MENU_ACCESS_DENIED', 3);
194
195
/**
196
 * Internal menu status code -- Menu item inaccessible because site is
offline.
197
 */
1982366
define('MENU_SITE_OFFLINE', 4);
199
200
/**
201
 * @} End of "Menu status codes".
202
 */
203
204
/**
205
 * @Name Menu tree parameters
206
 * @{
207
 * Menu tree
208
 */
209
210
 /**
211
 * The maximum number of path elements for a menu callback
212
 */
2132366
define('MENU_MAX_PARTS', 7);
214
215
216
/**
217
 * The maximum depth of a menu links tree - matches the number of p
columns.
218
 */
2192366
define('MENU_MAX_DEPTH', 9);
220
221
222
/**
223
 * @} End of "Menu tree parameters".
224
 */
225
226
/**
227
 * Returns the ancestors (and relevant placeholders) for any given path.
228
 *
229
 * For example, the ancestors of node/12345/edit are:
230
 * - node/12345/edit
231
 * - node/12345/%
232
 * - node/%/edit
233
 * - node/%/%
234
 * - node/12345
235
 * - node/%
236
 * - node
237
 *
238
 * To generate these, we will use binary numbers. Each bit represents a
239
 * part of the path. If the bit is 1, then it represents the original
240
 * value while 0 means wildcard. If the path is node/12/edit/foo
241
 * then the 1011 bitstring represents node/%/edit/foo where % means that
242
 * any argument matches that part.  We limit ourselves to using binary
243
 * numbers that correspond the patterns of wildcards of router items that
244
 * actually exists.  This list of 'masks' is built in menu_rebuild().
245
 *
246
 * @param $parts
247
 *   An array of path parts, for the above example
248
 *   array('node', '12345', 'edit').
249
 * @return
250
 *   An array which contains the ancestors and placeholders. Placeholders
251
 *   simply contain as many '%s' as the ancestors.
252
 */
2532366
function menu_get_ancestors($parts) {
2542476
  $number_parts = count($parts);
2552476
  $placeholders = array();
2562476
  $ancestors = array();
2572476
  $length =  $number_parts - 1;
2582476
  $end = (1 << $number_parts) - 1;
2592476
  $masks = variable_get('menu_masks', array());
260
  // Only examine patterns that actually exist as router items (the masks).
2612476
  foreach ($masks as $i) {
2622476
    if ($i > $end) {
263
      // Only look at masks that are not longer than the path of interest.
2642421
      continue;
2650
    }
2662476
    elseif ($i < (1 << $length)) {
267
      // We have exhausted the masks of a given length, so decrease the
length.
2681630
      --$length;
2691630
    }
2702476
    $current = '';
2712476
    for ($j = $length; $j >= 0; $j--) {
2722476
      if ($i & (1 << $j)) {
2732476
        $current .= $parts[$length - $j];
2742476
      }
275
      else {
2761630
        $current .= '%';
277
      }
2782476
      if ($j) {
2791630
        $current .= '/';
2801630
      }
2812476
    }
2822476
    $placeholders[] = "'%s'";
2832476
    $ancestors[] = $current;
2842476
  }
2852476
  return array($ancestors, $placeholders);
2860
}
287
288
/**
289
 * The menu system uses serialized arrays stored in the database for
290
 * arguments. However, often these need to change according to the
291
 * current path. This function unserializes such an array and does the
292
 * necessary change.
293
 *
294
 * Integer values are mapped according to the $map parameter. For
295
 * example, if unserialize($data) is array('view', 1) and $map is
296
 * array('node', '12345') then 'view' will not be changed because
297
 * it is not an integer, but 1 will as it is an integer. As $map[1]
298
 * is '12345', 1 will be replaced with '12345'. So the result will
299
 * be array('node_load', '12345').
300
 *
301
 * @param @data
302
 *   A serialized array.
303
 * @param @map
304
 *   An array of potential replacements.
305
 * @return
306
 *   The $data array unserialized and mapped.
307
 */
3082366
function menu_unserialize($data, $map) {
3092335
  if ($data = unserialize($data)) {
3102060
    foreach ($data as $k => $v) {
3112060
      if (is_int($v)) {
3121925
        $data[$k] = isset($map[$v]) ? $map[$v] : '';
3131925
      }
3142060
    }
3152060
    return $data;
3160
  }
317
  else {
3182068
    return array();
319
  }
3200
}
321
322
323
324
/**
325
 * Replaces the statically cached item for a given path.
326
 *
327
 * @param $path
328
 *   The path.
329
 * @param $router_item
330
 *   The router item. Usually you take a router entry from menu_get_item
and
331
 *   set it back either modified or to a different path. This lets you
modify the
332
 *   navigation block, the page title, the breadcrumb and the page help in
one
333
 *   call.
334
 */
3352366
function menu_set_item($path, $router_item) {
3360
  menu_get_item($path, $router_item);
3370
}
338
339
/**
340
 * Get a router item.
341
 *
342
 * @param $path
343
 *   The path, for example node/5. The function will find the corresponding
344
 *   node/% item and return that.
345
 * @param $router_item
346
 *   Internal use only.
347
 * @return
348
 *   The router item, an associate array corresponding to one row in the
349
 *   menu_router table. The value of key map holds the loaded objects. The
350
 *   value of key access is TRUE if the current user can access this page.
351
 *   The values for key title, page_arguments, access_arguments will be
352
 *   filled in based on the database values and the objects loaded.
353
 */
3542366
function menu_get_item($path = NULL, $router_item = NULL) {
3552347
  static $router_items;
3562347
  if (!isset($path)) {
3572347
    $path = $_GET['q'];
3582347
  }
3592347
  if (isset($router_item)) {
3600
    $router_items[$path] = $router_item;
3610
  }
3622347
  if (!isset($router_items[$path])) {
3632346
    $original_map = arg(NULL, $path);
3642346
    $parts = array_slice($original_map, 0, MENU_MAX_PARTS);
3652346
    list($ancestors, $placeholders) = menu_get_ancestors($parts);
366
3672346
    if ($router_item = db_fetch_array(db_query_range('SELECT * FROM
{menu_router} WHERE path IN (' . implode (',', $placeholders) . ') ORDER BY
fit DESC', $ancestors, 0, 1))) {
3682339
      $map = _menu_translate($router_item, $original_map);
3692339
      if ($map === FALSE) {
3706
        $router_items[$path] = FALSE;
3716
        return FALSE;
3720
      }
3732333
      if ($router_item['access']) {
3742284
        $router_item['map'] = $map;
3752284
        $router_item['page_arguments'] =
array_merge(menu_unserialize($router_item['page_arguments'], $map),
array_slice($map, $router_item['number_parts']));
3762284
      }
3772333
    }
3782340
    $router_items[$path] = $router_item;
3792340
  }
3802347
  return $router_items[$path];
3810
}
382
383
/**
384
 * Execute the page callback associated with the current path
385
 */
3862366
function menu_execute_active_handler($path = NULL) {
3872346
  if (_menu_site_is_offline()) {
3880
    return MENU_SITE_OFFLINE;
3890
  }
390
  // Rebuild if we know it's needed, or if the menu masks are missing which
391
  // occurs rarely, likely due to a race condition of multiple rebuilds.
3922346
  if (variable_get('menu_rebuild_needed', FALSE) ||
!variable_get('menu_masks', array())) {
3930
    menu_rebuild();
3940
  }
3952346
  if ($router_item = menu_get_item($path)) {
3962333
    registry_load_path_files();
3972333
    if ($router_item['access']) {
3982284
      if (drupal_function_exists($router_item['page_callback'])) {
3992284
        return call_user_func_array($router_item['page_callback'],
$router_item['page_arguments']);
4000
      }
4010
    }
402
    else {
40351
      return MENU_ACCESS_DENIED;
404
    }
4050
  }
40615
  return MENU_NOT_FOUND;
4070
}
408
409
/**
410
 * Loads objects into the map as defined in the $item['load_functions'].
411
 *
412
 * @param $item
413
 *   A menu router or menu link item
414
 * @param $map
415
 *   An array of path arguments (ex: array('node', '5'))
416
 * @return
417
 *   Returns TRUE for success, FALSE if an object cannot be loaded.
418
 *   Names of object loading functions are placed in
$item['load_functions'].
419
 *   Loaded objects are placed in $map[]; keys are the same as keys in the
420
 *   $item['load_functions'] array.
421
 *   $item['access'] is set to FALSE if an object cannot be loaded.
422
 */
4232366
function _menu_load_objects(&$item, &$map) {
4242341
  if ($load_functions = $item['load_functions']) {
425
    // If someone calls this function twice, then unserialize will fail.
4261887
    if ($load_functions_unserialized = unserialize($load_functions)) {
4271887
      $load_functions = $load_functions_unserialized;
4281887
    }
4291887
    $path_map = $map;
4301887
    foreach ($load_functions as $index => $function) {
4311887
      if ($function) {
4321871
        $value = isset($path_map[$index]) ? $path_map[$index] : '';
4331871
        if (is_array($function)) {
434
          // Set up arguments for the load function. These were pulled from
435
          // 'load arguments' in the hook_menu() entry, but they need
436
          // some processing. In this case the $function is the key to the
437
          // load_function array, and the value is the list of arguments.
438260
          list($function, $args) = each($function);
439260
          $load_functions[$index] = $function;
440
441
          // Some arguments are placeholders for dynamic items to process.
442260
          foreach ($args as $i => $arg) {
443260
            if ($arg === '%index') {
444
              // Pass on argument index to the load function, so multiple
445
              // occurances of the same placeholder can be identified.
446255
              $args[$i] = $index;
447255
            }
448260
            if ($arg === '%map') {
449
              // Pass on menu map by reference. The accepting function must
450
              // also declare this as a reference if it wants to modify
451
              // the map.
452255
              $args[$i] = &$map;
453255
            }
454260
            if (is_int($arg)) {
4555
              $args[$i] = isset($path_map[$arg]) ? $path_map[$arg] : '';
4565
            }
457260
          }
458260
          array_unshift($args, $value);
459260
          $return = call_user_func_array($function, $args);
460260
        }
461
        else {
4621855
          $return = $function($value);
463
        }
464
        // If callback returned an error or there is no callback, trigger
404.
4651871
        if ($return === FALSE) {
46613
          $item['access'] = FALSE;
46713
          $map = FALSE;
46813
          return FALSE;
4690
        }
4701865
        $map[$index] = $return;
4711865
      }
4721881
    }
4731881
    $item['load_functions'] = $load_functions;
4741881
  }
4752335
  return TRUE;
4760
}
477
478
/**
479
 * Check access to a menu item using the access callback
480
 *
481
 * @param $item
482
 *   A menu router or menu link item
483
 * @param $map
484
 *   An array of path arguments (ex: array('node', '5'))
485
 * @return
486
 *   $item['access'] becomes TRUE if the item is accessible, FALSE
otherwise.
487
 */
4882366
function _menu_check_access(&$item, $map) {
489
  // Determine access callback, which will decide whether or not the
current
490
  // user has access to this path.
4912335
  $callback = empty($item['access_callback']) ? 0 :
trim($item['access_callback']);
492
  // Check for a TRUE or FALSE value.
4932335
  if (is_numeric($callback)) {
4941929
    $item['access'] = (bool)$callback;
4951929
  }
496
  else {
4972154
    $arguments = menu_unserialize($item['access_arguments'], $map);
498
    // As call_user_func_array is quite slow and user_access is a very
common
499
    // callback, it is worth making a special case for it.
5002154
    if ($callback == 'user_access') {
5011934
      $item['access'] = (count($arguments) == 1) ?
user_access($arguments[0]) : user_access($arguments[0], $arguments[1]);
5021934
    }
503
    else {
5041975
      $item['access'] = call_user_func_array($callback, $arguments);
505
    }
506
  }
5072335
}
508
509
/**
510
 * Localize the router item title using t() or another callback.
511
 *
512
 * Translate the title and description to allow storage of English title
513
 * strings in the database, yet display of them in the language required
514
 * by the current user.
515
 *
516
 * @param $item
517
 *   A menu router item or a menu link item.
518
 * @param $map
519
 *   The path as an array with objects already replaced. E.g., for path
520
 *   node/123 $map would be array('node', $node) where $node is the node
521
 *   object for node 123.
522
 * @param $link_translate
523
 *   TRUE if we are translating a menu link item; FALSE if we are
524
 *   translating a menu router item.
525
 * @return
526
 *   No return value.
527
 *   $item['title'] is localized according to $item['title_callback'].
528
 *   If an item's callback is check_plain(), $item['options']['html']
becomes
529
 *   TRUE.
530
 *   $item['description'] is translated using t().
531
 *   When doing link translation and the
$item['options']['attributes']['title']
532
 *   (link title attribute) matches the description, it is translated as
well.
533
 */
5342366
function _menu_item_localize(&$item, $map, $link_translate = FALSE) {
5352335
  $callback = $item['title_callback'];
5362335
  $item['localized_options'] = $item['options'];
537
  // If we are not doing link translation or if the title matches the
538
  // link title of its router item, localize it.
5392335
  if (!$link_translate || (!empty($item['title']) && ($item['title'] ==
$item['link_title']))) {
540
    // t() is a special case. Since it is used very close to all the time,
541
    // we handle it directly instead of using indirect, slower methods.
5422335
    if ($callback == 't') {
5432280
      if (empty($item['title_arguments'])) {
5442280
        $item['title'] = t($item['title']);
5452280
      }
546
      else {
5470
        $item['title'] = t($item['title'],
menu_unserialize($item['title_arguments'], $map));
548
      }
5492280
    }
5501266
    elseif ($callback) {
5511266
      if (empty($item['title_arguments'])) {
552188
        $item['title'] = $callback($item['title']);
553188
      }
554
      else {
5551226
        $item['title'] = call_user_func_array($callback,
menu_unserialize($item['title_arguments'], $map));
556
      }
557
      // Avoid calling check_plain again on l() function.
5581266
      if ($callback == 'check_plain') {
559243
        $item['localized_options']['html'] = TRUE;
560243
      }
5611266
    }
5622335
  }
5631760
  elseif ($link_translate) {
5641760
    $item['title'] = $item['link_title'];
5651760
  }
566
567
  // Translate description, see the motivation above.
5682335
  if (!empty($item['description'])) {
569601
    $original_description = $item['description'];
570601
    $item['description'] = t($item['description']);
571601
    if ($link_translate && isset($item['options']['attributes']['title'])
&& $item['options']['attributes']['title'] == $original_description) {
572226
      $item['localized_options']['attributes']['title'] =
$item['description'];
573226
    }
574601
  }
5752335
}
576
577
/**
578
 * Handles dynamic path translation and menu access control.
579
 *
580
 * When a user arrives on a page such as node/5, this function determines
581
 * what "5" corresponds to, by inspecting the page's menu path definition,
582
 * node/%node. This will call node_load(5) to load the corresponding node
583
 * object.
584
 *
585
 * It also works in reverse, to allow the display of tabs and menu items
which
586
 * contain these dynamic arguments, translating node/%node to node/5.
587
 *
588
 * Translation of menu item titles and descriptions are done here to
589
 * allow for storage of English strings in the database, and translation
590
 * to the language required to generate the current page
591
 *
592
 * @param $router_item
593
 *   A menu router item
594
 * @param $map
595
 *   An array of path arguments (ex: array('node', '5'))
596
 * @param $to_arg
597
 *   Execute $item['to_arg_functions'] or not. Use only if you want to
render a
598
 *   path from the menu table, for example tabs.
599
 * @return
600
 *   Returns the map with objects loaded as defined in the
601
 *   $item['load_functions. $item['access'] becomes TRUE if the item is
602
 *   accessible, FALSE otherwise. $item['href'] is set according to the
map.
603
 *   If an error occurs during calling the load_functions (like trying to
load
604
 *   a non existing node) then this function return FALSE.
605
 */
6062366
function _menu_translate(&$router_item, $map, $to_arg = FALSE) {
6072339
  $path_map = $map;
6082339
  if (!_menu_load_objects($router_item, $map)) {
609
    // An error occurred loading an object.
61013
    $router_item['access'] = FALSE;
61113
    return FALSE;
6120
  }
6132333
  if ($to_arg) {
6141678
    _menu_link_map_translate($path_map, $router_item['to_arg_functions']);
6151678
  }
616
617
  // Generate the link path for the page request or local tasks.
6182333
  $link_map = explode('/', $router_item['path']);
6192333
  for ($i = 0; $i < $router_item['number_parts']; $i++) {
6202333
    if ($link_map[$i] == '%') {
621815
      $link_map[$i] = $path_map[$i];
622815
    }
6232333
  }
6242333
  $router_item['href'] = implode('/', $link_map);
6252333
  $router_item['options'] = array();
6262333
  _menu_check_access($router_item, $map);
627
628
  // For performance, don't localize an item the user can't access.
6292333
  if ($router_item['access']) {
6302284
    _menu_item_localize($router_item, $map);
6312284
  }
632
6332333
  return $map;
6340
}
635
636
/**
637
 * This function translates the path elements in the map using any to_arg
638
 * helper function. These functions take an argument and return an object.
639
 * See http://drupal.org/node/109153 for more information.
640
 *
641
 * @param map
642
 *   An array of path arguments (ex: array('node', '5'))
643
 * @param $to_arg_functions
644
 *   An array of helper function (ex: array(2 => 'menu_tail_to_arg'))
645
 */
6462366
function _menu_link_map_translate(&$map, $to_arg_functions) {
6471760
  if ($to_arg_functions) {
6481748
    $to_arg_functions = unserialize($to_arg_functions);
6491748
    foreach ($to_arg_functions as $index => $function) {
650
      // Translate place-holders into real values.
6511748
      $arg = $function(!empty($map[$index]) ? $map[$index] : '', $map,
$index);
6521748
      if (!empty($map[$index]) || isset($arg)) {
6531748
        $map[$index] = $arg;
6541748
      }
655
      else {
6560
        unset($map[$index]);
657
      }
6581748
    }
6591748
  }
6601760
}
661
6622366
function menu_tail_to_arg($arg, $map, $index) {
66311
  return implode('/', array_slice($map, $index));
6640
}
665
666
/**
667
 * This function is similar to _menu_translate() but does link-specific
668
 * preparation such as always calling to_arg functions
669
 *
670
 * @param $item
671
 *   A menu link
672
 * @return
673
 *   Returns the map of path arguments with objects loaded as defined in
the
674
 *   $item['load_functions'].
675
 *   $item['access'] becomes TRUE if the item is accessible, FALSE
otherwise.
676
 *   $item['href'] is generated from link_path, possibly by to_arg
functions.
677
 *   $item['title'] is generated from link_title, and may be localized.
678
 *   $item['options'] is unserialized; it is also changed within the call
here
679
 *   to $item['localized_options'] by _menu_item_localize().
680
 */
6812366
function _menu_link_translate(&$item) {
6821760
  $item['options'] = unserialize($item['options']);
6831760
  if ($item['external']) {
6840
    $item['access'] = 1;
6850
    $map = array();
6860
    $item['href'] = $item['link_path'];
6870
    $item['title'] = $item['link_title'];
6880
    $item['localized_options'] = $item['options'];
6890
  }
690
  else {
6911760
    $map = explode('/', $item['link_path']);
6921760
    _menu_link_map_translate($map, $item['to_arg_functions']);
6931760
    $item['href'] = implode('/', $map);
694
695
    // Note - skip callbacks without real values for their arguments.
6961760
    if (strpos($item['href'], '%') !== FALSE) {
6971748
      $item['access'] = FALSE;
6981748
      return FALSE;
6990
    }
700
    // menu_tree_check_access() may set this ahead of time for links to
nodes.
7011760
    if (!isset($item['access'])) {
7021754
      if (!_menu_load_objects($item, $map)) {
703
        // An error occurred loading an object.
7040
        $item['access'] = FALSE;
7050
        return FALSE;
7060
      }
7071754
      _menu_check_access($item, $map);
7081754
    }
709
    // For performance, don't localize a link the user can't access.
7101760
    if ($item['access']) {
7111760
      _menu_item_localize($item, $map, TRUE);
7121760
    }
713
  }
714
715
  // Allow other customizations - e.g. adding a page-specific query string
to the
716
  // options array. For performance reasons we only invoke this hook if the
link
717
  // has the 'alter' flag set in the options array.
7181760
  if (!empty($item['options']['alter'])) {
7190
    drupal_alter('translated_menu_link', $item, $map);
7200
  }
721
7221760
  return $map;
7230
}
724
725
/**
726
 * Get a loaded object from a router item.
727
 *
728
 * menu_get_object() will provide you the current node on paths like
node/5,
729
 * node/5/revisions/48 etc. menu_get_object('user') will give you the user
730
 * account on user/5 etc. Note - this function should never be called
within a
731
 * _to_arg function (like user_current_to_arg()) since this may result in
an
732
 * infinite recursion.
733
 *
734
 * @param $type
735
 *   Type of the object. These appear in hook_menu definitons as %type.
Core
736
 *   provides aggregator_feed, aggregator_category, contact, filter_format,
737
 *   forum_term, menu, menu_link, node, taxonomy_vocabulary, user. See the
738
 *   relevant {$type}_load function for more on each. Defaults to node.
739
 * @param $position
740
 *   The expected position for $type object. For node/%node this is 1, for
741
 *   comment/reply/%node this is 2. Defaults to 1.
742
 * @param $path
743
 *   See menu_get_item() for more on this. Defaults to the current path.
744
 */
7452366
function menu_get_object($type = 'node', $position = 1, $path = NULL) {
7461740
  $router_item = menu_get_item($path);
7471740
  if (isset($router_item['load_functions'][$position]) &&
!empty($router_item['map'][$position]) &&
$router_item['load_functions'][$position] == $type . '_load') {
748241
    return $router_item['map'][$position];
7490
  }
7501499
}
751
752
/**
753
 * Render a menu tree based on the current path.
754
 *
755
 * The tree is expanded based on the current path and dynamic paths are
also
756
 * changed according to the defined to_arg functions (for example the 'My
account'
757
 * link is changed from user/% to a link with the current user's uid).
758
 *
759
 * @param $menu_name
760
 *   The name of the menu.
761
 * @return
762
 *   The rendered HTML of that menu on the current page.
763
 */
7642366
function menu_tree($menu_name = 'navigation') {
7651717
  static $menu_output = array();
766
7671717
  if (!isset($menu_output[$menu_name])) {
7681717
    $tree = menu_tree_page_data($menu_name);
7691717
    $menu_output[$menu_name] = menu_tree_output($tree);
7701717
  }
7711717
  return $menu_output[$menu_name];
7720
}
773
774
/**
775
 * Returns a rendered menu tree.
776
 *
777
 * @param $tree
778
 *   A data structure representing the tree as returned from
menu_tree_data.
779
 * @return
780
 *   The rendered HTML of that data structure.
781
 */
7822366
function menu_tree_output($tree) {
7831717
  $output = '';
7841717
  $items = array();
785
786
  // Pull out just the menu items we are going to render so that we
787
  // get an accurate count for the first/last classes.
7881717
  foreach ($tree as $data) {
7891717
    if (!$data['link']['hidden']) {
7901205
      $items[] = $data;
7911205
    }
7921717
  }
793
7941717
  $num_items = count($items);
7951717
  foreach ($items as $i => $data) {
7961205
    $extra_class = NULL;
7971205
    if ($i == 0) {
7981205
      $extra_class = 'first';
7991205
    }
8001205
    if ($i == $num_items - 1) {
8011205
      $extra_class = 'last';
8021205
    }
8031205
    $link = theme('menu_item_link', $data['link']);
8041205
    if ($data['below']) {
805218
      $output .= theme('menu_item', $link, $data['link']['has_children'],
menu_tree_output($data['below']), $data['link']['in_active_trail'],
$extra_class);
806218
    }
807
    else {
8081205
      $output .= theme('menu_item', $link, $data['link']['has_children'],
'', $data['link']['in_active_trail'], $extra_class);
809
    }
8101205
  }
8111717
  return $output ? theme('menu_tree', $output) : '';
8120
}
813
814
/**
815
 * Get the data structure representing a named menu tree.
816
 *
817
 * Since this can be the full tree including hidden items, the data
returned
818
 * may be used for generating an an admin interface or a select.
819
 *
820
 * @param $menu_name
821
 *   The named menu links to return
822
 * @param $item
823
 *   A fully loaded menu link, or NULL.  If a link is supplied, only the
824
 *   path to root will be included in the returned tree- as if this link
825
 *   represented the current page in a visible menu.
826
 * @return
827
 *   An tree of menu links in an array, in the order they should be
rendered.
828
 */
8292366
function menu_tree_all_data($menu_name = 'navigation', $item = NULL) {
830147
  static $tree = array();
831
832
  // Use $mlid as a flag for whether the data being loaded is for the whole
tree.
833147
  $mlid = isset($item['mlid']) ? $item['mlid'] : 0;
834
  // Generate a cache ID (cid) specific for this $menu_name and $item.
835147
  $cid = 'links:' . $menu_name . ':all-cid:' . $mlid;
836
837147
  if (!isset($tree[$cid])) {
838
    // If the static variable doesn't have the data, check {cache_menu}.
839147
    $cache = cache_get($cid, 'cache_menu');
840147
    if ($cache && isset($cache->data)) {
841
      // If the cache entry exists, it will just be the cid for the actual
data.
842
      // This avoids duplication of large amounts of data.
843110
      $cache = cache_get($cache->data, 'cache_menu');
844110
      if ($cache && isset($cache->data)) {
845110
        $data = $cache->data;
846110
      }
847110
    }
848
    // If the tree data was not in the cache, $data will be NULL.
849147
    if (!isset($data)) {
850
      // Build and run the query, and build the tree.
85156
      if ($mlid) {
852
        // The tree is for a single item, so we need to match the values in
its
853
        // p columns and 0 (the top level) with the plid values of other
links.
85411
        $args = array(0);
85511
        for ($i = 1; $i < MENU_MAX_DEPTH; $i++) {
85611
          $args[] = $item["p$i"];
85711
        }
85811
        $args = array_unique($args);
85911
        $placeholders = implode(', ', array_fill(0, count($args), '%d'));
86011
        $where = ' AND ml.plid IN (' . $placeholders . ')';
86111
        $parents = $args;
86211
        $parents[] = $item['mlid'];
86311
      }
864
      else {
865
        // Get all links in this menu.
86645
        $where = '';
86745
        $args = array();
86845
        $parents = array();
869
      }
87056
      array_unshift($args, $menu_name);
871
      // Select the links from the table, and recursively build the tree. 
We
872
      // LEFT JOIN since there is no match in {menu_router} for an external
873
      // link.
87456
      $data['tree'] = menu_tree_data(db_query("
875
        SELECT m.load_functions, m.to_arg_functions, m.access_callback,
m.access_arguments, m.page_callback, m.page_arguments, m.title,
m.title_callback, m.title_arguments, m.type, m.description, ml.*
876
        FROM {menu_links} ml LEFT JOIN {menu_router} m ON m.path =
ml.router_path
87756
        WHERE ml.menu_name = '%s'" . $where . "
87856
        ORDER BY p1 ASC, p2 ASC, p3 ASC, p4 ASC, p5 ASC, p6 ASC, p7 ASC, p8
ASC, p9 ASC", $args), $parents);
87956
      $data['node_links'] = array();
88056
      menu_tree_collect_node_links($data['tree'], $data['node_links']);
881
      // Cache the data, if it is not already in the cache.
88256
      $tree_cid = _menu_tree_cid($menu_name, $data);
88356
      if (!cache_get($tree_cid, 'cache_menu')) {
88449
        cache_set($tree_cid, $data, 'cache_menu');
88549
      }
886
      // Cache the cid of the (shared) data using the menu and
item-specific cid.
88756
      cache_set($cid, $tree_cid, 'cache_menu');
88856
    }
889
    // Check access for the current user to each item in the tree.
890147
    menu_tree_check_access($data['tree'], $data['node_links']);
891147
    $tree[$cid] = $data['tree'];
892147
  }
893
894147
  return $tree[$cid];
8950
}
896
897
/**
898
 * Get the data structure representing a named menu tree, based on the
current page.
899
 *
900
 * The tree order is maintained by storing each parent in an individual
901
 * field, see http://drupal.org/node/141866 for more.
902
 *
903
 * @param $menu_name
904
 *   The named menu links to return
905
 * @return
906
 *   An array of menu links, in the order they should be rendered. The
array
907
 *   is a list of associative arrays -- these have two keys, link and
below.
908
 *   link is a menu item, ready for theming as a link. Below represents the
909
 *   submenu below the link if there is one, and it is a subtree that has
the
910
 *   same structure described for the top-level array.
911
 */
9122366
function menu_tree_page_data($menu_name = 'navigation') {
9131743
  static $tree = array();
914
915
  // Load the menu item corresponding to the current page.
9161743
  if ($item = menu_get_item()) {
917
    // Generate a cache ID (cid) specific for this page.
9181730
    $cid = 'links:' . $menu_name . ':page-cid:' . $item['href'] . ':' .
(int)$item['access'];
919
9201730
    if (!isset($tree[$cid])) {
921
      // If the static variable doesn't have the data, check {cache_menu}.
9221730
      $cache = cache_get($cid, 'cache_menu');
9231730
      if ($cache && isset($cache->data)) {
924
        // If the cache entry exists, it will just be the cid for the
actual data.
925
        // This avoids duplication of large amounts of data.
926952
        $cache = cache_get($cache->data, 'cache_menu');
927952
        if ($cache && isset($cache->data)) {
928952
          $data = $cache->data;
929952
        }
930952
      }
931
      // If the tree data was not in the cache, $data will be NULL.
9321730
      if (!isset($data)) {
933
        // Build and run the query, and build the tree.
934835
        if ($item['access']) {
935
          // Check whether a menu link exists that corresponds to the
current path.
936799
          $args = array($menu_name, $item['href']);
937799
          $placeholders = "'%s'";
938799
          if (drupal_is_front_page()) {
939114
            $args[] = '<front>';
940114
            $placeholders .= ", '%s'";
941114
          }
942799
          $parents = db_fetch_array(db_query("SELECT p1, p2, p3, p4, p5,
p6, p7, p8 FROM {menu_links} WHERE menu_name = '%s' AND link_path IN (" .
$placeholders . ")", $args));
943
944799
          if (empty($parents)) {
945
            // If no link exists, we may be on a local task that's not in
the links.
946
            // TODO: Handle the case like a local task on a specific node
in the menu.
947777
            $parents = db_fetch_array(db_query("SELECT p1, p2, p3, p4, p5,
p6, p7, p8 FROM {menu_links} WHERE menu_name = '%s' AND link_path = '%s'",
$menu_name, $item['tab_root']));
948777
          }
949
          // We always want all the top-level links with plid == 0.
950799
          $parents[] = '0';
951
952
          // Use array_values() so that the indices are numeric for
array_merge().
953799
          $args = $parents = array_unique(array_values($parents));
954799
          $placeholders = implode(', ', array_fill(0, count($args), '%d'));
955799
          $expanded = variable_get('menu_expanded', array());
956
          // Check whether the current menu has any links set to be
expanded.
957799
          if (in_array($menu_name, $expanded)) {
958
            // Collect all the links set to be expanded, and then add all
of
959
            // their children to the list as well.
960
            do {
96166
              $result = db_query("SELECT mlid FROM {menu_links} WHERE
menu_name = '%s' AND expanded = 1 AND has_children = 1 AND plid IN (" .
$placeholders . ') AND mlid NOT IN (' . $placeholders . ')',
array_merge(array($menu_name), $args, $args));
96266
              $num_rows = FALSE;
96366
              while ($item = db_fetch_array($result)) {
96448
                $args[] = $item['mlid'];
96548
                $num_rows = TRUE;
96648
              }
96766
              $placeholders = implode(', ', array_fill(0, count($args),
'%d'));
9680
            } while ($num_rows);
96966
          }
970799
          array_unshift($args, $menu_name);
971799
        }
972
        else {
973
          // Show only the top-level menu items when access is denied.
97436
          $args = array($menu_name, '0');
97536
          $placeholders = '%d';
97636
          $parents = array();
977
        }
978
        // Select the links from the table, and recursively build the tree.
We
979
        // LEFT JOIN since there is no match in {menu_router} for an
external
980
        // link.
981835
        $data['tree'] = menu_tree_data(db_query("
982
          SELECT m.load_functions, m.to_arg_functions, m.access_callback,
m.access_arguments, m.page_callback, m.page_arguments, m.title,
m.title_callback, m.title_arguments, m.type, m.description, ml.*
983
          FROM {menu_links} ml LEFT JOIN {menu_router} m ON m.path =
ml.router_path
984835
          WHERE ml.menu_name = '%s' AND ml.plid IN (" . $placeholders . ")
985835
          ORDER BY p1 ASC, p2 ASC, p3 ASC, p4 ASC, p5 ASC, p6 ASC, p7 ASC,
p8 ASC, p9 ASC", $args), $parents);
986835
        $data['node_links'] = array();
987835
        menu_tree_collect_node_links($data['tree'], $data['node_links']);
988
        // Cache the data, if it is not already in the cache.
989835
        $tree_cid = _menu_tree_cid($menu_name, $data);
990835
        if (!cache_get($tree_cid, 'cache_menu')) {
991614
          cache_set($tree_cid, $data, 'cache_menu');
992614
        }
993
        // Cache the cid of the (shared) data using the page-specific cid.
994835
        cache_set($cid, $tree_cid, 'cache_menu');
995835
      }
996
      // Check access for the current user to each item in the tree.
9971730
      menu_tree_check_access($data['tree'], $data['node_links']);
9981730
      $tree[$cid] = $data['tree'];
9991730
    }
10001730
    return $tree[$cid];
10010
  }
1002
100313
  return array();
10040
}
1005
1006
/**
1007
 * Helper function - compute the real cache ID for menu tree data.
1008
 */
10092366
function _menu_tree_cid($menu_name, $data) {
1010842
  return 'links:' . $menu_name . ':tree-data:' . md5(serialize($data));
10110
}
1012
1013
/**
1014
 * Recursive helper function - collect node links.
1015
 */
10162366
function menu_tree_collect_node_links(&$tree, &$node_links) {
1017854
  foreach ($tree as $key => $v) {
1018851
    if ($tree[$key]['link']['router_path'] == 'node/%') {
1019851
      $nid = substr($tree[$key]['link']['link_path'], 5);
1020851
      if (is_numeric($nid)) {
102195
        $node_links[$nid][$tree[$key]['link']['mlid']] =
&$tree[$key]['link'];
102295
        $tree[$key]['link']['access'] = FALSE;
102395
      }
1024851
    }
1025851
    if ($tree[$key]['below']) {
1026476
      menu_tree_collect_node_links($tree[$key]['below'], $node_links);
1027476
    }
1028851
  }
1029854
}
1030
1031
/**
1032
 * Check access and perform other dynamic operations for each link in the
tree.
1033
 */
10342366
function menu_tree_check_access(&$tree, $node_links = array()) {
1035
10361754
  if ($node_links) {
1037
    // Use db_rewrite_sql to evaluate view access without loading each full
node.
1038137
    $nids = array_keys($node_links);
1039137
    $placeholders = '%d' . str_repeat(', %d', count($nids) - 1);
1040137
    $result = db_query(db_rewrite_sql("SELECT n.nid FROM {node} n WHERE
n.status = 1 AND n.nid IN (" . $placeholders . ")"), $nids);
1041137
    while ($node = db_fetch_array($result)) {
1042137
      $nid = $node['nid'];
1043137
      foreach ($node_links[$nid] as $mlid => $link) {
1044137
        $node_links[$nid][$mlid]['access'] = TRUE;
1045137
      }
1046137
    }
1047137
  }
10481754
  _menu_tree_check_access($tree);
10491754
  return;
10500
}
1051
1052
/**
1053
 * Recursive helper function for menu_tree_check_access()
1054
 */
10552366
function _menu_tree_check_access(&$tree) {
10561754
  $new_tree = array();
10571754
  foreach ($tree as $key => $v) {
10581754
    $item = &$tree[$key]['link'];
10591754
    _menu_link_translate($item);
10601754
    if ($item['access']) {
10611754
      if ($tree[$key]['below']) {
1062548
        _menu_tree_check_access($tree[$key]['below']);
1063548
      }
1064
      // The weights are made a uniform 5 digits by adding 50000 as an
offset.
1065
      // After _menu_link_translate(), $item['title'] has the localized
link title.
1066
      // Adding the mlid to the end of the index insures that it is unique.
10671754
      $new_tree[(50000 + $item['weight']) . ' ' . $item['title'] . ' ' .
$item['mlid']] = $tree[$key];
10681754
    }
10691754
  }
1070
  // Sort siblings in the tree based on the weights and localized titles.
10711754
  ksort($new_tree);
10721754
  $tree = $new_tree;
10731754
}
1074
1075
/**
1076
 * Build the data representing a menu tree.
1077
 *
1078
 * @param $result
1079
 *   The database result.
1080
 * @param $parents
1081
 *   An array of the plid values that represent the path from the current
page
1082
 *   to the root of the menu tree.
1083
 * @param $depth
1084
 *   The depth of the current menu tree.
1085
 * @return
1086
 *   See menu_tree_page_data for a description of the data structure.
1087
 */
10882366
function menu_tree_data($result = NULL, $parents = array(), $depth = 1) {
1089854
  list(, $tree) = _menu_tree_data($result, $parents, $depth);
1090854
  return $tree;
10910
}
1092
1093
/**
1094
 * Recursive helper function to build the data representing a menu tree.
1095
 *
1096
 * The function is a bit complex because the rendering of an item depends
on
1097
 * the next menu item. So we are always rendering the element previously
1098
 * processed not the current one.
1099
 */
11002366
function _menu_tree_data($result, $parents, $depth, $previous_element = '')
{
1101854
  $remnant = NULL;
1102854
  $tree = array();
1103854
  while ($item = db_fetch_array($result)) {
1104
    // We need to determine if we're on the path to root so we can later
build
1105
    // the correct active trail and breadcrumb.
1106851
    $item['in_active_trail'] = in_array($item['mlid'], $parents);
1107
    // The current item is the first in a new submenu.
1108851
    if ($item['depth'] > $depth) {
1109
      // _menu_tree returns an item and the menu tree structure.
1110476
      list($item, $below) = _menu_tree_data($result, $parents,
$item['depth'], $item);
1111476
      if ($previous_element) {
1112476
        $tree[$previous_element['mlid']] = array(
1113476
          'link' => $previous_element,
1114476
          'below' => $below,
1115
        );
1116476
      }
1117
      else {
11180
        $tree = $below;
1119
      }
1120
      // We need to fall back one level.
1121476
      if (!isset($item) || $item['depth'] < $depth) {
1122162
        return array($item, $tree);
11230
      }
1124
      // This will be the link to be output in the next iteration.
1125435
      $previous_element = $item;
1126435
    }
1127
    // We are at the same depth, so we use the previous element.
1128851
    elseif ($item['depth'] == $depth) {
1129851
      if ($previous_element) {
1130
        // Only the first time.
1131822
        $tree[$previous_element['mlid']] = array(
1132822
          'link' => $previous_element,
1133822
          'below' => FALSE,
1134
        );
1135822
      }
1136
      // This will be the link to be output in the next iteration.
1137851
      $previous_element = $item;
1138851
    }
1139
    // The submenu ended with the previous item, so pass back the current
item.
1140
    else {
1141435
      $remnant = $item;
1142435
      break;
1143
    }
1144851
  }
1145854
  if ($previous_element) {
1146
    // We have one more link dangling.
1147851
    $tree[$previous_element['mlid']] = array(
1148851
      'link' => $previous_element,
1149851
      'below' => FALSE,
1150
    );
1151851
  }
1152854
  return array($remnant, $tree);
11530
}
1154
1155
/**
1156
 * Generate the HTML output for a single menu link.
1157
 *
1158
 * @ingroup themeable
1159
 */
11602366
function theme_menu_item_link($link) {
11611472
  if (empty($link['localized_options'])) {
11621470
    $link['localized_options'] = array();
11631470
  }
1164
11651472
  return l($link['title'], $link['href'], $link['localized_options']);
11660
}
1167
1168
/**
1169
 * Generate the HTML output for a menu tree
1170
 *
1171
 * @ingroup themeable
1172
 */
11732366
function theme_menu_tree($tree) {
11741205
  return '<ul class="menu">' . $tree . '</ul>';
11750
}
1176
1177
/**
1178
 * Generate the HTML output for a menu item and submenu.
1179
 *
1180
 * @ingroup themeable
1181
 */
11822366
function theme_menu_item($link, $has_children, $menu = '', $in_active_trail
= FALSE, $extra_class = NULL) {
11831205
  $class = ($menu ? 'expanded' : ($has_children ? 'collapsed' : 'leaf'));
11841205
  if (!empty($extra_class)) {
11851205
    $class .= ' ' . $extra_class;
11861205
  }
11871205
  if ($in_active_trail) {
1188405
    $class .= ' active-trail';
1189405
  }
11901205
  return '<li class="' . $class . '">' . $link . $menu . "</li>\n";
11910
}
1192
1193
/**
1194
 * Generate the HTML output for a single local task link.
1195
 *
1196
 * @ingroup themeable
1197
 */
11982366
function theme_menu_local_task($link, $active = FALSE) {
11991068
  return '<li ' . ($active ? 'class="active" ' : '') . '>' . $link .
"</li>\n";
12000
}
1201
1202
/**
1203
 * Generates elements for the $arg array in the help hook.
1204
 */
12052366
function drupal_help_arg($arg = array()) {
1206
  // Note - the number of empty elements should be > MENU_MAX_PARTS.
12071680
  return $arg + array('', '', '', '', '', '', '', '', '', '', '', '');
12080
}
1209
1210
/**
1211
 * Returns the help associated with the active menu item.
1212
 */
12132366
function menu_get_active_help() {
12141740
  $output = '';
12151740
  $router_path = menu_tab_root_path();
1216
  // We will always have a path unless we are on a 403 or 404.
12171740
  if (!$router_path) {
121862
    return '';
12190
  }
1220
12211678
  $arg = drupal_help_arg(arg(NULL));
12221678
  $empty_arg = drupal_help_arg();
1223
12241678
  foreach (module_list() as $name) {
12251676
    if (module_hook($name, 'help')) {
1226
      // Lookup help for this path.
12271676
      if ($help = module_invoke($name, 'help', $router_path, $arg)) {
1228328
        $output .= $help . "\n";
1229328
      }
1230
      // Add "more help" link on admin pages if the module provides a
1231
      // standalone help page.
12321676
      if ($arg[0] == "admin" && module_exists('help') &&
module_invoke($name, 'help', 'admin/help#' . $arg[2], $empty_arg) && $help)
{
1233191
        $output .= theme("more_help_link", url('admin/help/' . $arg[2]));
1234191
      }
12351676
    }
12361676
  }
12371678
  return $output;
12380
}
1239
1240
/**
1241
 * Build a list of named menus.
1242
 */
12432366
function menu_get_names($reset = FALSE) {
12440
  static $names;
1245
12460
  if ($reset || empty($names)) {
12470
    $names = array();
12480
    $result = db_query("SELECT DISTINCT(menu_name) FROM {menu_links} ORDER
BY menu_name");
12490
    while ($name = db_fetch_array($result)) {
12500
      $names[] = $name['menu_name'];
12510
    }
12520
  }
12530
  return $names;
12540
}
1255
1256
/**
1257
 * Return an array containing the names of system-defined (default) menus.
1258
 */
12592366
function menu_list_system_menus() {
1260219
  return array('navigation', 'main-menu', 'secondary-menu');
12610
}
1262
1263
/**
1264
 * Return an array of links to be rendered as the Main menu.
1265
 */
12662366
function menu_main_menu() {
12671740
  return menu_navigation_links(variable_get('menu_main_links_source',
'main-menu'));
12680
}
1269
1270
/**
1271
 * Return an array of links to be rendered as the Secondary links.
1272
 */
12732366
function menu_secondary_menu() {
1274
1275
  // If the secondary menu source is set as the primary menu, we display
the
1276
  // second level of the primary menu.
12771740
  if (variable_get('menu_secondary_links_source', 'secondary-menu') ==
variable_get('menu_main_links_source', 'main-menu')) {
12780
    return menu_navigation_links(variable_get('menu_main_links_source',
'main-menu'), 1);
12790
  }
1280
  else {
12811740
    return
menu_navigation_links(variable_get('menu_secondary_links_source',
'secondary-menu'), 0);
1282
  }
12830
}
1284
1285
/**
1286
 * Return an array of links for a navigation menu.
1287
 *
1288
 * @param $menu_name
1289
 *   The name of the menu.
1290
 * @param $level
1291
 *   Optional, the depth of the menu to be returned.
1292
 * @return
1293
 *   An array of links of the specified menu and level.
1294
 */
12952366
function menu_navigation_links($menu_name, $level = 0) {
1296
  // Don't even bother querying the menu table if no menu is specified.
12971740
  if (empty($menu_name)) {
12980
    return array();
12990
  }
1300
1301
  // Get the menu hierarchy for the current page.
13021740
  $tree = menu_tree_page_data($menu_name);
1303
1304
  // Go down the active trail until the right level is reached.
13051740
  while ($level-- > 0 && $tree) {
1306
    // Loop through the current level's items until we find one that is in
trail.
13070
    while ($item = array_shift($tree)) {
13080
      if ($item['link']['in_active_trail']) {
1309
        // If the item is in the active trail, we continue in the subtree.
13100
        $tree = empty($item['below']) ? array() : $item['below'];
13110
        break;
13120
      }
13130
    }
13140
  }
1315
1316
  // Create a single level of links.
13171740
  $links = array();
13181740
  foreach ($tree as $item) {
1319125
    if (!$item['link']['hidden']) {
1320125
      $class = '';
1321125
      $l = $item['link']['localized_options'];
1322125
      $l['href'] = $item['link']['href'];
1323125
      $l['title'] = $item['link']['title'];
1324125
      if ($item['link']['in_active_trail']) {
13250
        $class = ' active-trail';
13260
      }
1327
      // Keyed with the unique mlid to generate classes in theme_links().
1328125
      $links['menu-' . $item['link']['mlid'] . $class] = $l;
1329125
    }
1330125
  }
13311740
  return $links;
13320
}
1333
1334
/**
1335
 * Collects the local tasks (tabs) for a given level.
1336
 *
1337
 * @param $level
1338
 *   The level of tasks you ask for. Primary tasks are 0, secondary are 1.
1339
 * @param $return_root
1340
 *   Whether to return the root path for the current page.
1341
 * @return
1342
 *   Themed output corresponding to the tabs of the requested level, or
1343
 *   router path if $return_root == TRUE. This router path corresponds to
1344
 *   a parent tab, if the current page is a default local task.
1345
 */
13462366
function menu_local_tasks($level = 0, $return_root = FALSE) {
13471740
  static $tabs;
13481740
  static $root_path;
1349
13501740
  if (!isset($tabs)) {
13511740
    $tabs = array();
1352
13531740
    $router_item = menu_get_item();
13541740
    if (!$router_item || !$router_item['access']) {
135562
      return '';
13560
    }
1357
    // Get all tabs and the root page.
13581678
    $result = db_query("SELECT * FROM {menu_router} WHERE tab_root = '%s'
ORDER BY weight, title", $router_item['tab_root']);
13591678
    $map = arg();
13601678
    $children = array();
13611678
    $tasks = array();
13621678
    $root_path = $router_item['path'];
1363
13641678
    while ($item = db_fetch_array($result)) {
13651678
      _menu_translate($item, $map, TRUE);
13661678
      if ($item['tab_parent']) {
1367
        // All tabs, but not the root page.
13681068
        $children[$item['tab_parent']][$item['path']] = $item;
13691068
      }
1370
      // Store the translated item for later use.
13711678
      $tasks[$item['path']] = $item;
13721678
    }
1373
1374
    // Find all tabs below the current path.
13751678
    $path = $router_item['path'];
1376
    // Tab parenting may skip levels, so the number of parts in the path
may not
1377
    // equal the depth. Thus we use the $depth counter (offset by 1000 for
ksort).
13781678
    $depth = 1001;
13791678
    while (isset($children[$path])) {
1380812
      $tabs_current = '';
1381812
      $next_path = '';
1382812
      $count = 0;
1383812
      foreach ($children[$path] as $item) {
1384812
        if ($item['access']) {
1385812
          $count++;
1386
          // The default task is always active.
1387812
          if ($item['type'] == MENU_DEFAULT_LOCAL_TASK) {
1388
            // Find the first parent which is not a default local task.
1389807
            for ($p = $item['tab_parent']; $tasks[$p]['type'] ==
MENU_DEFAULT_LOCAL_TASK; $p = $tasks[$p]['tab_parent']);
1390807
            $link = theme('menu_item_link', array('href' =>
$tasks[$p]['href']) + $item);
1391807
            $tabs_current .= theme('menu_local_task', $link, TRUE);
1392807
            $next_path = $item['path'];
1393807
          }
1394
          else {
1395674
            $link = theme('menu_item_link', $item);
1396674
            $tabs_current .= theme('menu_local_task', $link);
1397
          }
1398812
        }
1399812
      }
1400812
      $path = $next_path;
1401812
      $tabs[$depth]['count'] = $count;
1402812
      $tabs[$depth]['output'] = $tabs_current;
1403812
      $depth++;
1404812
    }
1405
1406
    // Find all tabs at the same level or above the current one.
14071678
    $parent = $router_item['tab_parent'];
14081678
    $path = $router_item['path'];
14091678
    $current = $router_item;
14101678
    $depth = 1000;
14111678
    while (isset($children[$parent])) {
1412268
      $tabs_current = '';
1413268
      $next_path = '';
1414268
      $next_parent = '';
1415268
      $count = 0;
1416268
      foreach ($children[$parent] as $item) {
1417268
        if ($item['access']) {
1418268
          $count++;
1419268
          if ($item['type'] == MENU_DEFAULT_LOCAL_TASK) {
1420
            // Find the first parent which is not a default local task.
1421215
            for ($p = $item['tab_parent']; $tasks[$p]['type'] ==
MENU_DEFAULT_LOCAL_TASK; $p = $tasks[$p]['tab_parent']);
1422215
            $link = theme('menu_item_link', array('href' =>
$tasks[$p]['href']) + $item);
1423215
            if ($item['path'] == $router_item['path']) {
14245
              $root_path = $tasks[$p]['path'];
14255
            }
1426215
          }
1427
          else {
1428263
            $link = theme('menu_item_link', $item);
1429
          }
1430
          // We check for the active tab.
1431268
          if ($item['path'] == $path) {
1432268
            $tabs_current .= theme('menu_local_task', $link, TRUE);
1433268
            $next_path = $item['tab_parent'];
1434268
            if (isset($tasks[$next_path])) {
1435268
              $next_parent = $tasks[$next_path]['tab_parent'];
1436268
            }
1437268
          }
1438
          else {
1439252
            $tabs_current .= theme('menu_local_task', $link);
1440
          }
1441268
        }
1442268
      }
1443268
      $path = $next_path;
1444268
      $parent = $next_parent;
1445268
      $tabs[$depth]['count'] = $count;
1446268
      $tabs[$depth]['output'] = $tabs_current;
1447268
      $depth--;
1448268
    }
1449
    // Sort by depth.
14501678
    ksort($tabs);
1451
    // Remove the depth, we are interested only in their relative
placement.
14521678
    $tabs = array_values($tabs);
14531678
  }
1454
14551740
  if ($return_root) {
14561678
    return $root_path;
14570
  }
1458
  else {
1459
    // We do not display single tabs.
14601740
    return (isset($tabs[$level]) && $tabs[$level]['count'] > 1) ?
$tabs[$level]['output'] : '';
1461
  }
14620
}
1463
1464
/**
1465
 * Returns the rendered local tasks at the top level.
1466
 */
14672366
function menu_primary_local_tasks() {
14681740
  return menu_local_tasks(0);
14690
}
1470
1471
/**
1472
 * Returns the rendered local tasks at the second level.
1473
 */
14742366
function menu_secondary_local_tasks() {
14751740
  return menu_local_tasks(1);
14760
}
1477
1478
/**
1479
 * Returns the router path, or the path of the parent tab of a default
local task.
1480
 */
14812366
function menu_tab_root_path() {
14821740
  return menu_local_tasks(0, TRUE);
14830
}
1484
1485
/**
1486
 * Returns the rendered local tasks. The default implementation renders
them as tabs.
1487
 *
1488
 * @ingroup themeable
1489
 */
14902366
function theme_menu_local_tasks() {
14911
  $output = '';
1492
14931
  if ($primary = menu_primary_local_tasks()) {
14941
    $output .= "<ul class=\"tabs primary\">\n" . $primary . "</ul>\n";
14951
  }
14961
  if ($secondary = menu_secondary_local_tasks()) {
14971
    $output .= "<ul class=\"tabs secondary\">\n" . $secondary . "</ul>\n";
14981
  }
1499
15001
  return $output;
15010
}
1502
1503
/**
1504
 * Set (or get) the active menu for the current page - determines the
active trail.
1505
 */
15062366
function menu_set_active_menu_name($menu_name = NULL) {
15071348
  static $active;
1508
15091348
  if (isset($menu_name)) {
151012
    $active = $menu_name;
151112
  }
15121336
  elseif (!isset($active)) {
15131336
    $active = 'navigation';
15141336
  }
15151348
  return $active;
15160
}
1517
1518
/**
1519
 * Get the active menu for the current page - determines the active trail.
1520
 */
15212366
function menu_get_active_menu_name() {
15221336
  return menu_set_active_menu_name();
15230
}
1524
1525
/**
1526
 * Set the active path, which determines which page is loaded.
1527
 *
1528
 * @param $path
1529
 *   A Drupal path - not a path alias.
1530
 *
1531
 * Note that this may not have the desired effect unless invoked very early
1532
 * in the page load, such as during hook_boot, or unless you call
1533
 * menu_execute_active_handler() to generate your page output.
1534
 */
15352366
function menu_set_active_item($path) {
15365
  $_GET['q'] = $path;
15375
}
1538
1539
/**
1540
 * Set (or get) the active trail for the current page - the path to root in
the menu tree.
1541
 */
15422366
function menu_set_active_trail($new_trail = NULL) {
15431348
  static $trail;
1544
15451348
  if (isset($new_trail)) {
154612
    $trail = $new_trail;
154712
  }
15481348
  elseif (!isset($trail)) {
15491336
    $trail = array();
15501336
    $trail[] = array('title' => t('Home'), 'href' => '<front>',
'localized_options' => array(), 'type' => 0);
15511336
    $item = menu_get_item();
1552
1553
    // Check whether the current item is a local task (displayed as a tab).
15541336
    if ($item['tab_parent']) {
1555
      // The title of a local task is used for the tab, never the page
title.
1556
      // Thus, replace it with the item corresponding to the root path to
get
1557
      // the relevant href and title.  For example, the menu item
corresponding
1558
      // to 'admin' is used when on the 'By module' tab at
'admin/by-module'.
1559268
      $parts = explode('/', $item['tab_root']);
1560268
      $args = arg();
1561
      // Replace wildcards in the root path using the current path.
1562268
      foreach ($parts as $index => $part) {
1563268
        if ($part == '%') {
1564127
          $parts[$index] = $args[$index];
1565127
        }
1566268
      }
1567
      // Retrieve the menu item using the root path after wildcard
replacement.
1568268
      $root_item = menu_get_item(implode('/', $parts));
1569268
      if ($root_item && $root_item['access']) {
1570263
        $item = $root_item;
1571263
      }
1572268
    }
1573
15741336
    $tree = menu_tree_page_data(menu_get_active_menu_name());
15751336
    list($key, $curr) = each($tree);
1576
15771336
    while ($curr) {
1578
      // Terminate the loop when we find the current path in the active
trail.
15791336
      if ($curr['link']['href'] == $item['href']) {
1580684
        $trail[] = $curr['link'];
1581684
        $curr = FALSE;
1582684
      }
1583
      else {
1584
        // Add the link if it's in the active trail, then move to the link
below.
15851331
        if ($curr['link']['in_active_trail']) {
1586175
          $trail[] = $curr['link'];
1587175
          $tree = $curr['below'] ? $curr['below'] : array();
1588175
        }
15891331
        list($key, $curr) = each($tree);
1590
      }
15911336
    }
1592
    // Make sure the current page is in the trail (needed for the page
title),
1593
    // but exclude tabs and the front page.
15941336
    $last = count($trail) - 1;
15951336
    if ($trail[$last]['href'] != $item['href'] && !(bool)($item['type'] &
MENU_IS_LOCAL_TASK) && !drupal_is_front_page()) {
1596647
      $trail[] = $item;
1597647
    }
15981336
  }
15991348
  return $trail;
16000
}
1601
1602
/**
1603
 * Get the active trail for the current page - the path to root in the menu
tree.
1604
 */
16052366
function menu_get_active_trail() {
16061348
  return menu_set_active_trail();
16070
}
1608
1609
/**
1610
 * Get the breadcrumb for the current page, as determined by the active
trail.
1611
 */
16122366
function menu_get_active_breadcrumb() {
16131633
  $breadcrumb = array();
1614
1615
  // No breadcrumb for the front page.
16161633
  if (drupal_is_front_page()) {
1617244
    return $breadcrumb;
16180
  }
1619
16201389
  $item = menu_get_item();
16211389
  if ($item && $item['access']) {
16221327
    $active_trail = menu_get_active_trail();
1623
16241327
    foreach ($active_trail as $parent) {
16251327
      $breadcrumb[] = l($parent['title'], $parent['href'],
$parent['localized_options']);
16261327
    }
16271327
    $end = end($active_trail);
1628
1629
    // Don't show a link to the current page in the breadcrumb trail.
16301327
    if ($item['href'] == $end['href'] || ($item['type'] ==
MENU_DEFAULT_LOCAL_TASK && $end['href'] != '<front>')) {
16311064
      array_pop($breadcrumb);
16321064
    }
16331327
  }
16341389
  return $breadcrumb;
16350
}
1636
1637
/**
1638
 * Get the title of the current page, as determined by the active trail.
1639
 */
16402366
function menu_get_active_title() {
1641779
  $active_trail = menu_get_active_trail();
1642
1643779
  foreach (array_reverse($active_trail) as $item) {
1644779
    if (!(bool)($item['type'] & MENU_IS_LOCAL_TASK)) {
1645779
      return $item['title'];
16460
    }
16470
  }
16480
}
1649
1650
/**
1651
 * Get a menu link by its mlid, access checked and link translated for
rendering.
1652
 *
1653
 * This function should never be called from within node_load() or any
other
1654
 * function used as a menu object load function since an infinite recursion
may
1655
 * occur.
1656
 *
1657
 * @param $mlid
1658
 *   The mlid of the menu item.
1659
 * @return
1660
 *   A menu link, with $item['access'] filled and link translated for
1661
 *   rendering.
1662
 */
16632366
function menu_link_load($mlid) {
166438
  if (is_numeric($mlid) && $item = db_fetch_array(db_query("SELECT m.*,
ml.* FROM {menu_links} ml LEFT JOIN {menu_router} m ON m.path =
ml.router_path WHERE ml.mlid = %d", $mlid))) {
166538
    _menu_link_translate($item);
166638
    return $item;
16670
  }
16680
  return FALSE;
16690
}
1670
1671
/**
1672
 * Clears the cached cached data for a single named menu.
1673
 */
16742366
function menu_cache_clear($menu_name = 'navigation') {
1675170
  static $cache_cleared = array();
1676
1677170
  if (empty($cache_cleared[$menu_name])) {
1678170
    cache_clear_all('links:' . $menu_name . ':', 'cache_menu', TRUE);
1679170
    $cache_cleared[$menu_name] = 1;
1680170
  }
1681139
  elseif ($cache_cleared[$menu_name] == 1) {
1682139
    register_shutdown_function('cache_clear_all', 'links:' . $menu_name .
':', 'cache_menu', TRUE);
1683139
    $cache_cleared[$menu_name] = 2;
1684139
  }
1685170
}
1686
1687
/**
1688
 * Clears all cached menu data.  This should be called any time broad
changes
1689
 * might have been made to the router items or menu links.
1690
 */
16912366
function menu_cache_clear_all() {
1692158
  cache_clear_all('*', 'cache_menu', TRUE);
1693158
}
1694
1695
/**
1696
 * (Re)populate the database tables used by various menu functions.
1697
 *
1698
 * This function will clear and populate the {menu_router} table, add
entries
1699
 * to {menu_links} for new router items, then remove stale items from
1700
 * {menu_links}. If called from update.php or install.php, it will also
1701
 * schedule a call to itself on the first real page load from
1702
 * menu_execute_active_handler(), because the maintenance page environment
1703
 * is different and leaves stale data in the menu tables.
1704
 */
17052366
function menu_rebuild() {
1706157
  variable_del('menu_rebuild_needed');
1707157
  menu_cache_clear_all();
1708157
  $menu = menu_router_build(TRUE);
1709157
  _menu_navigation_links_rebuild($menu);
1710
  // Clear the page and block caches.
1711157
  _menu_clear_page_cache();
1712157
  if (defined('MAINTENANCE_MODE')) {
17130
    variable_set('menu_rebuild_needed', TRUE);
17140
  }
1715157
}
1716
1717
/**
1718
 * Collect, alter and store the menu definitions.
1719
 */
17202366
function menu_router_build($reset = FALSE) {
1721186
  static $menu;
1722
1723186
  if (!isset($menu) || $reset) {
1724186
    if (!$reset && ($cache = cache_get('router:', 'cache_menu')) &&
isset($cache->data)) {
172528
      $menu = $cache->data;
172628
    }
1727
    else {
1728
      // We need to manually call each module so that we can know which
module
1729
      // a given item came from.
1730158
      $callbacks = array();
1731158
      foreach (module_implements('menu', TRUE) as $module) {
1732158
        $router_items = call_user_func($module . '_menu');
1733158
        if (isset($router_items) && is_array($router_items)) {
1734158
          foreach (array_keys($router_items) as $path) {
1735158
            $router_items[$path]['module'] = $module;
1736158
          }
1737158
          $callbacks = array_merge($callbacks, $router_items);
1738158
        }
1739158
      }
1740
      // Alter the menu as defined in modules, keys are like user/%user.
1741158
      drupal_alter('menu', $callbacks);
1742158
      $menu = _menu_router_build($callbacks);
1743
    }
1744186
  }
1745186
  return $menu;
17460
}
1747
1748
/**
1749
 * Builds a link from a router item.
1750
 */
17512366
function _menu_link_build($item) {
1752158
  if ($item['type'] == MENU_CALLBACK) {
1753157
    $item['hidden'] = -1;
1754157
  }
1755158
  elseif ($item['type'] == MENU_SUGGESTED_ITEM) {
1756157
    $item['hidden'] = 1;
1757157
  }
1758
  // Note, we set this as 'system', so that we can be sure to distinguish
all
1759
  // the menu links generated automatically from entries in {menu_router}.
1760158
  $item['module'] = 'system';
1761
  $item += array(
1762158
    'menu_name' => 'navigation',
1763158
    'link_title' => $item['title'],
1764158
    'link_path' => $item['path'],
1765158
    'hidden' => 0,
1766158
    'options' => empty($item['description']) ? array() : array('attributes'
=> array('title' => $item['description'])),
17670
  );
1768158
  return $item;
17690
}
1770
1771
/**
1772
 * Helper function to build menu links for the items in the menu router.
1773
 */
17742366
function _menu_navigation_links_rebuild($menu) {
1775
  // Add normal and suggested items as links.
1776157
  $menu_links = array();
1777157
  foreach ($menu as $path => $item) {
1778157
    if ($item['_visible']) {
1779157
      $item = _menu_link_build($item);
1780157
      $menu_links[$path] = $item;
1781157
      $sort[$path] = $item['_number_parts'];
1782157
    }
1783157
  }
1784157
  if ($menu_links) {
1785
    // Make sure no child comes before its parent.
1786157
    array_multisort($sort, SORT_NUMERIC, $menu_links);
1787
1788157
    foreach ($menu_links as $item) {
1789157
      $existing_item = db_fetch_array(db_query("SELECT mlid, menu_name,
plid, customized, has_children, updated FROM {menu_links} WHERE link_path =
'%s' AND module = '%s'", $item['link_path'], 'system'));
1790157
      if ($existing_item) {
1791157
        $item['mlid'] = $existing_item['mlid'];
1792
        // A change in hook_menu may move the link to a different menu
1793157
        if (empty($item['menu_name']) || ($item['menu_name'] ==
$existing_item['menu_name'])) {
1794157
          $item['menu_name'] = $existing_item['menu_name'];
1795157
          $item['plid'] = $existing_item['plid'];
1796157
        }
1797157
        $item['has_children'] = $existing_item['has_children'];
1798157
        $item['updated'] = $existing_item['updated'];
1799157
      }
1800157
      if (!$existing_item || !$existing_item['customized']) {
1801157
        menu_link_save($item);
1802157
      }
1803157
    }
1804157
  }
1805157
  $placeholders = db_placeholders($menu, 'varchar');
1806157
  $paths = array_keys($menu);
1807
  // Updated and customized items whose router paths are gone need new
ones.
1808157
  $result = db_query("SELECT ml.link_path, ml.mlid, ml.router_path,
ml.updated FROM {menu_links} ml WHERE ml.updated = 1 OR (router_path NOT IN
($placeholders) AND external = 0 AND customized = 1)", $paths);
1809157
  while ($item = db_fetch_array($result)) {
18100
    $router_path = _menu_find_router_path($menu, $item['link_path']);
18110
    if (!empty($router_path) && ($router_path != $item['router_path'] ||
$item['updated'])) {
1812
      // If the router path and the link path matches, it's surely a
working
1813
      // item, so we clear the updated flag.
18140
      $updated = $item['updated'] && $router_path != $item['link_path'];
18150
      db_query("UPDATE {menu_links} SET router_path = '%s', updated = %d
WHERE mlid = %d", $router_path, $updated, $item['mlid']);
18160
    }
18170
  }
1818
  // Find any item whose router path does not exist any more.
1819157
  $result = db_query("SELECT * FROM {menu_links} WHERE router_path NOT IN
($placeholders) AND external = 0 AND updated = 0 AND customized = 0 ORDER
BY depth DESC", $paths);
1820
  // Remove all such items. Starting from those with the greatest depth
will
1821
  // minimize the amount of re-parenting done by menu_link_delete().
1822157
  while ($item = db_fetch_array($result)) {
18231
    _menu_delete_item($item, TRUE);
18241
  }
1825157
}
1826
1827
/**
1828
 * Delete one or several menu links.
1829
 *
1830
 * @param $mlid
1831
 *   A valid menu link mlid or NULL. If NULL, $path is used.
1832
 * @param $path
1833
 *   The path to the menu items to be deleted. $mlid must be NULL.
1834
 */
18352366
function menu_link_delete($mlid, $path = NULL) {
18365
  if (isset($mlid)) {
18375
    _menu_delete_item(db_fetch_array(db_query("SELECT * FROM {menu_links}
WHERE mlid = %d", $mlid)));
18385
  }
1839
  else {
18400
    $result = db_query("SELECT * FROM {menu_links} WHERE link_path = '%s'",
$path);
18410
    while ($link = db_fetch_array($result)) {
18420
      _menu_delete_item($link);
18430
    }
1844
  }
18455
}
1846
1847
/**
1848
 * Helper function for menu_link_delete; deletes a single menu link.
1849
 *
1850
 * @param $item
1851
 *   Item to be deleted.
1852
 * @param $force
1853
 *   Forces deletion. Internal use only, setting to TRUE is discouraged.
1854
 */
18552366
function _menu_delete_item($item, $force = FALSE) {
18566
  if ($item && ($item['module'] != 'system' || $item['updated'] || $force))
{
1857
    // Children get re-attached to the item's parent.
18586
    if ($item['has_children']) {
18593
      $result = db_query("SELECT mlid FROM {menu_links} WHERE plid = %d",
$item['mlid']);
18603
      while ($m = db_fetch_array($result)) {
18612
        $child = menu_link_load($m['mlid']);
18622
        $child['plid'] = $item['plid'];
18632
        menu_link_save($child);
18642
      }
18653
    }
18666
    db_query('DELETE FROM {menu_links} WHERE mlid = %d', $item['mlid']);
1867
1868
    // Update the has_children status of the parent.
18696
    _menu_update_parental_status($item);
18706
    menu_cache_clear($item['menu_name']);
18716
    _menu_clear_page_cache();
18726
  }
18736
}
1874
1875
/**
1876
 * Save a menu link.
1877
 *
1878
 * @param $item
1879
 *   An array representing a menu link item. The only mandatory keys are
1880
 *   link_path and link_title. Possible keys are:
1881
 *   - menu_name   default is navigation
1882
 *   - weight      default is 0
1883
 *   - expanded    whether the item is expanded.
1884
 *   - options     An array of options, @see l for more.
1885
 *   - mlid        Set to an existing value, or 0 or NULL to insert a new
link.
1886
 *   - plid        The mlid of the parent.
1887
 *   - router_path The path of the relevant router item.
1888
 * @return
1889
 *   The mlid of the saved menu link, or FALSE if the menu link could not
be 
1890
 *   saved.
1891
 */
18922366
function menu_link_save(&$item) {
1893186
  $menu = menu_router_build();
1894
1895186
  drupal_alter('menu_link', $item, $menu);
1896
1897
  // This is the easiest way to handle the unique internal path '<front>',
1898
  // since a path marked as external does not need to match a router path.
1899186
  $item['external'] = (menu_path_is_external($item['link_path'])  ||
$item['link_path'] == '<front>') ? 1 : 0;
1900
  // Load defaults.
1901
  $item += array(
1902186
    'menu_name' => 'navigation',
1903186
    'weight' => 0,
1904186
    'link_title' => '',
1905186
    'hidden' => 0,
1906186
    'has_children' => 0,
1907186
    'expanded' => 0,
1908186
    'options' => array(),
1909186
    'module' => 'menu',
1910186
    'customized' => 0,
1911186
    'updated' => 0,
19120
  );
1913186
  $existing_item = FALSE;
1914186
  if (isset($item['mlid'])) {
1915178
    $existing_item = db_fetch_array(db_query("SELECT * FROM {menu_links}
WHERE mlid = %d", $item['mlid']));
1916178
  }
1917
1918186
  if (isset($item['plid'])) {
1919184
    if ($item['plid']) {
1920171
      $parent = db_fetch_array(db_query("SELECT * FROM {menu_links} WHERE
mlid = %d", $item['plid']));
1921171
    }
1922
    else {
1923
      // Don't bother with the query - mlid can never equal zero..
1924170
      $parent = FALSE;
1925
    }
1926184
  }
1927
  else {
1928
    // Find the parent - it must be unique.
1929139
    $parent_path = $item['link_path'];
1930139
    $where = "WHERE link_path = '%s'";
1931
    // Only links derived from router items should have module == 'system',
and
1932
    // we want to find the parent even if it's in a different menu.
1933139
    if ($item['module'] == 'system') {
1934138
      $where .= " AND module = '%s'";
1935138
      $arg2 = 'system';
1936138
    }
1937
    else {
1938
      // If not derived from a router item, we respect the specified menu
name.
1939135
      $where .= " AND menu_name = '%s'";
1940135
      $arg2 = $item['menu_name'];
1941
    }
1942
    do {
1943139
      $parent = FALSE;
1944139
      $parent_path = substr($parent_path, 0, strrpos($parent_path, '/'));
1945139
      $result = db_query("SELECT COUNT(*) FROM {menu_links} " . $where,
$parent_path, $arg2);
1946
      // Only valid if we get a unique result.
1947139
      if (db_result($result) == 1) {
1948137
        $parent = db_fetch_array(db_query("SELECT * FROM {menu_links} " .
$where, $parent_path, $arg2));
1949137
      }
1950139
    } while ($parent === FALSE && $parent_path);
1951
  }
1952186
  if ($parent !== FALSE) {
1953172
    $item['menu_name'] = $parent['menu_name'];
1954172
  }
1955186
  $menu_name = $item['menu_name'];
1956
  // Menu callbacks need to be in the links table for breadcrumbs, but
can't
1957
  // be parents if they are generated directly from a router item.
1958186
  if (empty($parent['mlid']) || $parent['hidden'] < 0) {
1959171
    $item['plid'] =  0;
1960171
  }
1961
  else {
1962172
    $item['plid'] = $parent['mlid'];
1963
  }
1964
1965186
  if (!$existing_item) {
1966148
    db_query("INSERT INTO {menu_links} (
1967
       menu_name, plid, link_path,
1968
      hidden, external, has_children,
1969
      expanded, weight,
1970
      module, link_title, options,
1971
      customized, updated) VALUES (
1972
      '%s', %d, '%s',
1973
      %d, %d, %d,
1974
      %d, %d,
1975148
      '%s', '%s', '%s', %d, %d)",
1976148
      $item['menu_name'], $item['plid'], $item['link_path'],
1977148
      $item['hidden'], $item['external'], $item['has_children'],
1978148
      $item['expanded'], $item['weight'],
1979148
      $item['module'],  $item['link_title'], serialize($item['options']),
1980148
      $item['customized'], $item['updated']);
1981148
    $item['mlid'] = db_last_insert_id('menu_links', 'mlid');
1982148
  }
1983
1984186
  if (!$item['plid']) {
1985171
    $item['p1'] = $item['mlid'];
1986171
    for ($i = 2; $i <= MENU_MAX_DEPTH; $i++) {
1987171
      $item["p$i"] = 0;
1988171
    }
1989171
    $item['depth'] = 1;
1990171
  }
1991
  else {
1992
    // Cannot add beyond the maximum depth.
1993172
    if ($item['has_children'] && $existing_item) {
1994157
      $limit = MENU_MAX_DEPTH -
menu_link_children_relative_depth($existing_item) - 1;
1995157
    }
1996
    else {
1997172
      $limit = MENU_MAX_DEPTH - 1;
1998
    }
1999172
    if ($parent['depth'] > $limit) {
20000
      return FALSE;
20010
    }
2002172
    $item['depth'] = $parent['depth'] + 1;
2003172
    _menu_link_parents_set($item, $parent);
2004
  }
2005
  // Need to check both plid and menu_name, since plid can be 0 in any
menu.
2006186
  if ($existing_item && ($item['plid'] != $existing_item['plid'] ||
$menu_name != $existing_item['menu_name'])) {
20073
    _menu_link_move_children($item, $existing_item);
20083
  }
2009
  // Find the router_path.
2010186
  if (empty($item['router_path'])  || !$existing_item ||
($existing_item['link_path'] != $item['link_path'])) {
2011183
    if ($item['external']) {
20120
      $item['router_path'] = '';
20130
    }
2014
    else {
2015
      // Find the router path which will serve this path.
2016183
      $item['parts'] = explode('/', $item['link_path'], MENU_MAX_PARTS);
2017183
      $item['router_path'] = _menu_find_router_path($menu,
$item['link_path']);
2018
    }
2019183
  }
2020186
  $item['options'] = serialize($item['options']);
2021
  // If every value in $existing_item is the same in the $item, there is no
2022
  // reason to run the update queries or clear the caches. We use
2023
  // array_diff_assoc() with the $existing_item as the first parameter
2024
  // because $item has additional keys left over from the process of
building
2025
  // the router item.
2026186
  if (!$existing_item || array_diff_assoc($existing_item, $item)) {
2027166
    db_query("UPDATE {menu_links} SET menu_name = '%s', plid = %d,
link_path = '%s',
2028
      router_path = '%s', hidden = %d, external = %d, has_children = %d,
2029
      expanded = %d, weight = %d, depth = %d,
2030
      p1 = %d, p2 = %d, p3 = %d, p4 = %d, p5 = %d, p6 = %d, p7 = %d, p8 =
%d, p9 = %d,
2031166
      module = '%s', link_title = '%s', options = '%s', customized = %d
WHERE mlid = %d",
2032166
      $item['menu_name'], $item['plid'], $item['link_path'],
2033166
      $item['router_path'], $item['hidden'], $item['external'],
$item['has_children'],
2034166
      $item['expanded'], $item['weight'],  $item['depth'],
2035166
      $item['p1'], $item['p2'], $item['p3'], $item['p4'], $item['p5'],
$item['p6'], $item['p7'], $item['p8'], $item['p9'],
2036166
      $item['module'],  $item['link_title'], $item['options'],
$item['customized'], $item['mlid']);
2037
    // Check the has_children status of the parent.
2038166
    _menu_update_parental_status($item);
2039166
    menu_cache_clear($menu_name);
2040166
    if ($existing_item && $menu_name != $existing_item['menu_name']) {
20411
      menu_cache_clear($existing_item['menu_name']);
20421
    }
2043
  
2044166
    _menu_clear_page_cache();
2045166
  }
2046186
  return $item['mlid'];
20470
}
2048
2049
/**
2050
 * Helper function to clear the page and block caches at most twice per
page load.
2051
 */
20522366
function _menu_clear_page_cache() {
2053189
  static $cache_cleared = 0;
2054
2055
  // Clear the page and block caches, but at most twice, including at
2056
  //  the end of the page load when there are multple links saved or
deleted.
2057189
  if (empty($cache_cleared)) {
2058189
    cache_clear_all();
2059
    // Keep track of which menus have expanded items.
2060189
    _menu_set_expanded_menus();
2061189
    $cache_cleared = 1;
2062189
  }
2063141
  elseif ($cache_cleared == 1) {
2064141
    register_shutdown_function('cache_clear_all');
2065
    // Keep track of which menus have expanded items.
2066141
    register_shutdown_function('_menu_set_expanded_menus');
2067141
    $cache_cleared = 2;
2068141
  }
2069189
}
2070
2071
/**
2072
 * Helper function to update a list of menus with expanded items
2073
 */
20742366
function _menu_set_expanded_menus() {
2075189
  $names = array();
2076189
  $result = db_query("SELECT menu_name FROM {menu_links} WHERE expanded <>
0 GROUP BY menu_name");
2077189
  while ($n = db_fetch_array($result)) {
207820
    $names[] = $n['menu_name'];
207920
  }
2080189
  variable_set('menu_expanded', $names);
2081189
}
2082
2083
/**
2084
 * Find the router path which will serve this path.
2085
 *
2086
 * @param $menu
2087
 *  The full built menu.
2088
 * @param $link_path
2089
 *  The path for we are looking up its router path.
2090
 * @return
2091
 *  A path from $menu keys or empty if $link_path points to a nonexisting
2092
 *  place.
2093
 */
20942366
function _menu_find_router_path($menu, $link_path) {
2095183
  $parts = explode('/', $link_path, MENU_MAX_PARTS);
2096183
  $router_path = $link_path;
2097183
  if (!isset($menu[$router_path])) {
2098158
    list($ancestors) = menu_get_ancestors($parts);
2099158
    $ancestors[] = '';
2100158
    foreach ($ancestors as $key => $router_path) {
2101158
      if (isset($menu[$router_path])) {
2102158
        break;
21030
      }
2104158
    }
2105158
  }
2106183
  return $router_path;
21070
}
2108
2109
/**
2110
 * Insert, update or delete an uncustomized menu link related to a module.
2111
 *
2112
 * @param $module
2113
 *   The name of the module.
2114
 * @param $op
2115
 *   Operation to perform: insert, update or delete.
2116
 * @param $link_path
2117
 *   The path this link points to.
2118
 * @param $link_title
2119
 *   Title of the link to insert or new title to update the link to.
2120
 *   Unused for delete.
2121
 * @return
2122
 *   The insert op returns the mlid of the new item. Others op return NULL.
2123
 */
21242366
function menu_link_maintain($module, $op, $link_path, $link_title) {
2125
  switch ($op) {
21261
    case 'insert':
2127
      $menu_link = array(
21281
        'link_title' => $link_title,
21291
        'link_path' => $link_path,
21301
        'module' => $module,
21311
      );
21321
      return menu_link_save($menu_link);
21330
      break;
21340
    case 'update':
21350
      db_query("UPDATE {menu_links} SET link_title = '%s' WHERE link_path =
'%s' AND customized = 0 AND module = '%s'", $link_title, $link_path,
$module);
21360
      $result = db_query("SELECT menu_name FROM {menu_links} WHERE
link_path = '%s' AND customized = 0 AND module = '%s'", $link_path,
$module);
21370
      while ($item = db_fetch_array($result)) {
21380
        menu_cache_clear($item['menu_name']);
21390
      }
21400
      break;
21410
    case 'delete':
21420
      menu_link_delete(NULL, $link_path);
21430
      break;
21440
  }
21450
}
2146
2147
/**
2148
 * Find the depth of an item's children relative to its depth.
2149
 *
2150
 * For example, if the item has a depth of 2, and the maximum of any child
in
2151
 * the menu link tree is 5, the relative depth is 3.
2152
 *
2153
 * @param $item
2154
 *   An array representing a menu link item.
2155
 * @return
2156
 *   The relative depth, or zero.
2157
 *
2158
 */
21592366
function menu_link_children_relative_depth($item) {
2160169
  $i = 1;
2161169
  $match = '';
2162169
  $args[] = $item['menu_name'];
2163169
  $p = 'p1';
2164169
  while ($i <= MENU_MAX_DEPTH && $item[$p]) {
2165169
    $match .= " AND $p = %d";
2166169
    $args[] = $item[$p];
2167169
    $p = 'p' . ++$i;
2168169
  }
2169
2170169
  $max_depth = db_result(db_query_range("SELECT depth FROM {menu_links}
WHERE menu_name = '%s'" . $match . " ORDER BY depth DESC", $args, 0, 1));
2171
2172169
  return ($max_depth > $item['depth']) ? $max_depth - $item['depth'] : 0;
21730
}
2174
2175
/**
2176
 * Update the children of a menu link that's being moved.
2177
 *
2178
 * The menu name, parents (p1 - p6), and depth are updated for all children
of
2179
 * the link, and the has_children status of the previous parent is updated.
2180
 */
21812366
function _menu_link_move_children($item, $existing_item) {
2182
21833
  $args[] = $item['menu_name'];
21843
  $set[] = "menu_name = '%s'";
2185
21863
  $i = 1;
21873
  while ($i <= $item['depth']) {
21883
    $p = 'p' . $i++;
21893
    $set[] = "$p = %d";
21903
    $args[] = $item[$p];
21913
  }
21923
  $j = $existing_item['depth'] + 1;
21933
  while ($i <= MENU_MAX_DEPTH && $j <= MENU_MAX_DEPTH) {
21943
    $set[] = 'p' . $i++ . ' = p' . $j++;
21953
  }
21963
  while ($i <= MENU_MAX_DEPTH) {
21972
    $set[] = 'p' . $i++ . ' = 0';
21982
  }
2199
22003
  $shift = $item['depth'] - $existing_item['depth'];
22013
  if ($shift < 0) {
22022
    $args[] = -$shift;
22032
    $set[] = 'depth = depth - %d';
22042
  }
22051
  elseif ($shift > 0) {
2206
    // The order of $set must be reversed so the new values don't overwrite
the
2207
    // old ones before they can be used because "Single-table UPDATE
2208
    // assignments are generally evaluated from left to right"
2209
    // see: http://dev.mysql.com/doc/refman/5.0/en/update.html
22100
    $set = array_reverse($set);
22110
    $args = array_reverse($args);
2212
22130
    $args[] = $shift;
22140
    $set[] = 'depth = depth + %d';
22150
  }
22163
  $where[] = "menu_name = '%s'";
22173
  $args[] = $existing_item['menu_name'];
22183
  $p = 'p1';
22193
  for ($i = 1; $i <= MENU_MAX_DEPTH && $existing_item[$p]; $p = 'p' . ++$i)
{
22203
    $where[] = "$p = %d";
22213
    $args[] = $existing_item[$p];
22223
  }
2223
22243
  db_query("UPDATE {menu_links} SET " . implode(', ', $set) . " WHERE " .
implode(' AND ', $where), $args);
2225
  // Check the has_children status of the parent, while excluding this
item.
22263
  _menu_update_parental_status($existing_item, TRUE);
22273
}
2228
2229
/**
2230
 * Check and update the has_children status for the parent of a link.
2231
 */
22322366
function _menu_update_parental_status($item, $exclude = FALSE) {
2233
  // If plid == 0, there is nothing to update.
2234170
  if ($item['plid']) {
2235
    // Check if at least one visible child exists in the table.
2236155
    $query = db_select('menu_links', 'm');
2237155
    $query->addField('m', 'mlid');
2238155
    $query->condition('menu_name', $item['menu_name']);
2239155
    $query->condition('hidden', 0);
2240155
    $query->condition('plid', $item['plid']);
2241155
    $query->range(0, 1);
2242
2243155
    if ($exclude) {
22442
      $query->condition('mlid', $item['mlid'], '!=');
22452
    }
2246
2247155
    $parent_has_children = ((bool) $query->execute()->fetchField()) ? 1 :
0;
2248155
    db_query("UPDATE {menu_links} SET has_children = %d WHERE mlid = %d",
$parent_has_children, $item['plid']);
2249
2250155
  }
2251170
}
2252
2253
/**
2254
 * Helper function that sets the p1..p9 values for a menu link being saved.
2255
 */
22562366
function _menu_link_parents_set(&$item, $parent) {
2257172
  $i = 1;
2258172
  while ($i < $item['depth']) {
2259172
    $p = 'p' . $i++;
2260172
    $item[$p] = $parent[$p];
2261172
  }
2262172
  $p = 'p' . $i++;
2263
  // The parent (p1 - p9) corresponding to the depth always equals the
mlid.
2264172
  $item[$p] = $item['mlid'];
2265172
  while ($i <= MENU_MAX_DEPTH) {
2266172
    $p = 'p' . $i++;
2267172
    $item[$p] = 0;
2268172
  }
2269172
}
2270
2271
/**
2272
 * Helper function to build the router table based on the data from
hook_menu.
2273
 */
22742366
function _menu_router_build($callbacks) {
2275
  // First pass: separate callbacks from paths, making paths ready for
2276
  // matching. Calculate fitness, and fill some default values.
2277158
  $menu = array();
2278158
  foreach ($callbacks as $path => $item) {
2279158
    $load_functions = array();
2280158
    $to_arg_functions = array();
2281158
    $fit = 0;
2282158
    $move = FALSE;
2283
2284158
    $parts = explode('/', $path, MENU_MAX_PARTS);
2285158
    $number_parts = count($parts);
2286
    // We store the highest index of parts here to save some work in the
fit
2287
    // calculation loop.
2288158
    $slashes = $number_parts - 1;
2289
    // Extract load and to_arg functions.
2290158
    foreach ($parts as $k => $part) {
2291158
      $match = FALSE;
2292
      // Look for wildcards in the form allowed to be used in PHP
functions,
2293
      // because we are using these to construct the load function names.
2294
      // See http://php.net/manual/en/language.functions.php for reference.
2295158
      if (preg_match('/^%(|[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)$/',
$part, $matches)) {
2296158
        if (empty($matches[1])) {
2297158
          $match = TRUE;
2298158
          $load_functions[$k] = NULL;
2299158
        }
2300
        else {
2301158
          if (drupal_function_exists($matches[1] . '_to_arg')) {
2302158
            $to_arg_functions[$k] = $matches[1] . '_to_arg';
2303158
            $load_functions[$k] = NULL;
2304158
            $match = TRUE;
2305158
          }
2306158
          if (drupal_function_exists($matches[1] . '_load')) {
2307158
            $function = $matches[1] . '_load';
2308
            // Create an array of arguments that will be passed to the
_load
2309
            // function when this menu path is checked, if 'load arguments'
2310
            // exists.
2311158
            $load_functions[$k] = isset($item['load arguments']) ?
array($function => $item['load arguments']) : $function;
2312158
            $match = TRUE;
2313158
          }
2314
        }
2315158
      }
2316158
      if ($match) {
2317158
        $parts[$k] = '%';
2318158
      }
2319
      else {
2320158
        $fit |=  1 << ($slashes - $k);
2321
      }
2322158
    }
2323158
    if ($fit) {
2324158
      $move = TRUE;
2325158
    }
2326
    else {
2327
      // If there is no %, it fits maximally.
23280
      $fit = (1 << $number_parts) - 1;
2329
    }
2330158
    $masks[$fit] = 1;
2331158
    $item['load_functions'] = empty($load_functions) ? '' :
serialize($load_functions);
2332158
    $item['to_arg_functions'] = empty($to_arg_functions) ? '' :
serialize($to_arg_functions);
2333
    $item += array(
2334158
      'title' => '',
2335158
      'weight' => 0,
2336158
      'type' => MENU_NORMAL_ITEM,
2337158
      '_number_parts' => $number_parts,
2338158
      '_parts' => $parts,
2339158
      '_fit' => $fit,
23400
    );
2341
    $item += array(
2342158
      '_visible' => (bool)($item['type'] & MENU_VISIBLE_IN_BREADCRUMB),
2343158
      '_tab' => (bool)($item['type'] & MENU_IS_LOCAL_TASK),
23440
    );
2345158
    if ($move) {
2346158
      $new_path = implode('/', $item['_parts']);
2347158
      $menu[$new_path] = $item;
2348158
      $sort[$new_path] = $number_parts;
2349158
    }
2350
    else {
23510
      $menu[$path] = $item;
23520
      $sort[$path] = $number_parts;
2353
    }
2354158
  }
2355158
  array_multisort($sort, SORT_NUMERIC, $menu);
2356
2357158
  if (!$menu) {
23580
    return array();
23590
  }
2360
  // Delete the existing router since we have some data to replace it.
2361158
  db_query('DELETE FROM {menu_router}');
2362
  // Apply inheritance rules.
2363158
  foreach ($menu as $path => $v) {
2364158
    $item = &$menu[$path];
2365158
    if (!$item['_tab']) {
2366
      // Non-tab items.
2367158
      $item['tab_parent'] = '';
2368158
      $item['tab_root'] = $path;
2369158
    }
2370158
    for ($i = $item['_number_parts'] - 1; $i; $i--) {
2371158
      $parent_path = implode('/', array_slice($item['_parts'], 0, $i));
2372158
      if (isset($menu[$parent_path])) {
2373
2374158
        $parent = $menu[$parent_path];
2375
2376158
        if (!isset($item['tab_parent'])) {
2377
          // Parent stores the parent of the path.
2378158
          $item['tab_parent'] = $parent_path;
2379158
        }
2380158
        if (!isset($item['tab_root']) && !$parent['_tab']) {
2381158
          $item['tab_root'] = $parent_path;
2382158
        }
2383
        // If an access callback is not found for a default local task we
use
2384
        // the callback from the parent, since we expect them to be
identical.
2385
        // In all other cases, the access parameters must be specified.
2386158
        if (($item['type'] == MENU_DEFAULT_LOCAL_TASK) &&
!isset($item['access callback']) && isset($parent['access callback'])) {
2387158
          $item['access callback'] = $parent['access callback'];
2388158
          if (!isset($item['access arguments']) && isset($parent['access
arguments'])) {
2389158
            $item['access arguments'] = $parent['access arguments'];
2390158
          }
2391158
        }
2392
        // Same for page callbacks.
2393158
        if (!isset($item['page callback']) && isset($parent['page
callback'])) {
2394158
          $item['page callback'] = $parent['page callback'];
2395158
          if (!isset($item['page arguments']) && isset($parent['page
arguments'])) {
2396158
            $item['page arguments'] = $parent['page arguments'];
2397158
          }
2398158
        }
2399158
      }
2400158
    }
2401158
    if (!isset($item['access callback']) && isset($item['access
arguments'])) {
2402
      // Default callback.
2403158
      $item['access callback'] = 'user_access';
2404158
    }
2405158
    if (!isset($item['access callback']) || empty($item['page callback']))
{
24062
      $item['access callback'] = 0;
24072
    }
2408158
    if (is_bool($item['access callback'])) {
2409158
      $item['access callback'] = intval($item['access callback']);
2410158
    }
2411
2412
    $item += array(
2413158
      'access arguments' => array(),
2414158
      'access callback' => '',
2415158
      'page arguments' => array(),
2416158
      'page callback' => '',
2417158
      'block callback' => '',
2418158
      'title arguments' => array(),
2419158
      'title callback' => 't',
2420158
      'description' => '',
2421158
      'position' => '',
2422158
      'tab_parent' => '',
2423158
      'tab_root' => $path,
2424158
      'path' => $path,
24250
    );
2426
2427158
    $title_arguments = $item['title arguments'] ? serialize($item['title
arguments']) : '';
2428158
    db_query("INSERT INTO {menu_router}
2429
      (path, load_functions, to_arg_functions, access_callback,
2430
      access_arguments, page_callback, page_arguments, fit,
2431
      number_parts, tab_parent, tab_root,
2432
      title, title_callback, title_arguments,
2433
      type, block_callback, description, position, weight)
2434
      VALUES ('%s', '%s', '%s', '%s',
2435
      '%s', '%s', '%s', %d,
2436
      %d, '%s', '%s',
2437
      '%s', '%s', '%s',
2438158
      %d, '%s', '%s', '%s', %d)",
2439158
      $path, $item['load_functions'], $item['to_arg_functions'],
$item['access callback'],
2440158
      serialize($item['access arguments']), $item['page callback'],
serialize($item['page arguments']), $item['_fit'],
2441158
      $item['_number_parts'], $item['tab_parent'], $item['tab_root'],
2442158
      $item['title'], $item['title callback'], $title_arguments,
2443158
      $item['type'], $item['block callback'], $item['description'],
$item['position'], $item['weight']);
2444158
  }
2445
  // Sort the masks so they are in order of descending fit, and store them.
2446158
  $masks = array_keys($masks);
2447158
  rsort($masks);
2448158
  variable_set('menu_masks', $masks);
2449158
  cache_set('router:', $menu, 'cache_menu');
2450158
  return $menu;
24510
}
2452
2453
/**
2454
 * Returns TRUE if a path is external (e.g. http://example.com).
2455
 */
24562366
function menu_path_is_external($path) {
2457215
  $colonpos = strpos($path, ':');
2458215
  return $colonpos !== FALSE && !preg_match('![/?#]!', substr($path, 0,
$colonpos)) && filter_xss_bad_protocol($path, FALSE) == check_plain($path);
24590
}
2460
2461
/**
2462
 * Checks whether the site is offline for maintenance.
2463
 *
2464
 * This function will log the current user out and redirect to front page
2465
 * if the current user has no 'administer site configuration' permission.
2466
 *
2467
 * @return
2468
 *   FALSE if the site is not offline or its the login page or the user has
2469
 *     'administer site configuration' permission.
2470
 *   TRUE for anonymous users not on the login page if the site is offline.
2471
 */
24722366
function _menu_site_is_offline() {
2473
  // Check if site is set to offline mode.
24742346
  if (variable_get('site_offline', 0)) {
2475
    // Check if the user has administration privileges.
24760
    if (user_access('administer site configuration')) {
2477
      // Ensure that the offline message is displayed only once [allowing
for
2478
      // page redirects], and specifically suppress its display on the site
2479
      // maintenance page.
24800
      if (drupal_get_normal_path($_GET['q']) !=
'admin/settings/site-maintenance') {
24810
        drupal_set_message(t('Operating in offline mode. <a href="@url">Go
online.</a>', array('@url' => url('admin/settings/site-maintenance'))),
'status', FALSE);
24820
      }
24830
    }
2484
    else {
2485
      // Anonymous users get a FALSE at the login prompt, TRUE otherwise.
24860
      if (user_is_anonymous()) {
24870
        return $_GET['q'] != 'user' && $_GET['q'] != 'user/login';
24880
      }
2489
      // Logged in users are unprivileged here, so they are logged out.
24900
      require_once DRUPAL_ROOT . '/' . drupal_get_path('module', 'user') .
'/user.pages.inc';
24910
      user_logout();
2492
    }
24930
  }
24942346
  return FALSE;
24950
}
2496
2497
/**
2498
 * Validates the path of a menu link being created or edited.
2499
 *
2500
 * @return
2501
 *   TRUE if it is a valid path AND the current user has access permission,
2502
 *   FALSE otherwise.
2503
 */
25042366
function menu_valid_path($form_item) {
250546
  global $menu_admin;
250646
  $item = array();
250746
  $path = $form_item['link_path'];
2508
  // We indicate that a menu administrator is running the menu access
check.
250946
  $menu_admin = TRUE;
251046
  if ($path == '<front>' || menu_path_is_external($path)) {
25110
    $item = array('access' => TRUE);
25120
  }
251346
  elseif (preg_match('/\/\%/', $path)) {
2514
    // Path is dynamic (ie 'user/%'), so check directly against menu_router
table.
25150
    if ($item = db_fetch_array(db_query("SELECT * FROM {menu_router} where
path = '%s' ", $path))) {
25160
      $item['link_path']  = $form_item['link_path'];
25170
      $item['link_title'] = $form_item['link_title'];
25180
      $item['external']   = FALSE;
25190
      $item['options'] = '';
25200
      _menu_link_translate($item);
25210
    }
25220
  }
2523
  else {
252446
    $item = menu_get_item($path);
2525
  }
252646
  $menu_admin = FALSE;
252746
  return $item && $item['access'];
25280
}
2529
2530
/**
2531
 * @} End of "defgroup menu".
2532
 */
25332366