block.module 16.8 KB
Newer Older
Dries Buytaert's avatar
   
Dries Buytaert committed
1
<?php
2
// $Id$
Dries Buytaert's avatar
 
Dries Buytaert committed
3

Dries Buytaert's avatar
   
Dries Buytaert committed
4
5
6
7
8
/**
 * @file
 * Controls the boxes that are displayed around the main content.
 */

Dries Buytaert's avatar
   
Dries Buytaert committed
9
10
11
12
/**
 * Implementation of hook_help().
 */
function block_help($section) {
Dries Buytaert's avatar
   
Dries Buytaert committed
13
  switch ($section) {
Dries Buytaert's avatar
   
Dries Buytaert committed
14
    case 'admin/help#block':
Dries Buytaert's avatar
   
Dries Buytaert committed
15
      return t("
16
<p>Blocks are the boxes visible in the sidebar(s) of your web site. These are usually generated automatically by modules (e.g. recent forum topics), but you can also create your own blocks.</p>
Dries Buytaert's avatar
   
Dries Buytaert committed
17
<p>The sidebar each block appears in depends on both which theme you're using (some are left-only, some right, some both), and on the settings in block management.</p><p>Whether a block is visible in the first place depends on four things:</p><ul><li>It must have its \"enabled\" box checked in block management.</li><li>If it has its \"custom\" box checked in block management, the user must have chosen to display it in their user preferences.</li><li>If the \"path\" field in block management is set, the visitor must be on a page that matches the path specification (more on this later).</li><li>If the block has its throttle box checked, the user will only see the block if the site throttle level is low.</li></ul>
Dries Buytaert's avatar
   
Dries Buytaert committed
18
<p>The block management screen also lets you specify the vertical sort-order of the blocks within a sidebar. You do this by assigning a <strong>weight</strong> to each block. Lighter blocks (smaller weight) \"float up\" towards the top of the sidebar. Heavier ones \"sink down\" towards the bottom of it.</p>
Dries Buytaert's avatar
   
Dries Buytaert committed
19
<p>The path setting lets you define the pages on which a specific block is visible. If you leave the path blank it will appear on all pages. The path uses a regular expression syntax so remember to escape special characters! The path expression is matched against the relative URL of a Drupal page, e.g. <code>book</code>, <code>node/12</code>, <code>admin</code>.</p>
Dries Buytaert's avatar
   
Dries Buytaert committed
20
21
<p>In case you do not know what a regular expression is, you should read about them in the PHP manual. The chapter to look at is the one on <a href=\"%pcre\">Perl-Compatible Regular Expressions (PCRE)</a>.</p>
<p>However, for basic tasks it is sufficient to look at the following examples:</p>
Dries Buytaert's avatar
   
Dries Buytaert committed
22
<p>If the block should only show up on blog pages, use &lt;^blog&gt;.  To display on all node views use &lt;^node&gt;.  The angular brackets are used as delimiters of the regular expression.  To show up on either forum or book pages use &lt;^(forum|book)&gt;.  The round brackets form a group of expressions, divided by the | character. It matches if any of the expressions in it match.  A more complicated example is &lt;^node/add/(story|blog|image)&gt;. Blocks which have their paths set to this expression will show up on story, block, or image composition pages.  If you want to show a block an all pages, but not the search page, use &lt;^(?!search)&gt;.</p>
Dries Buytaert's avatar
   
Dries Buytaert committed
23
<h3>Administrator Defined Blocks</h3>
24
<p>An administrator defined block contains content supplied by you (as opposed to being generated automatically by a module). Each admin-defined block consists of a title, a description, and a body which can be as long as you wish. The Drupal engine will 'render' the content of the block.</p>", array('%pcre' => 'http://php.net/pcre/'));
Dries Buytaert's avatar
   
Dries Buytaert committed
25
    case 'admin/modules#description':
Dries Buytaert's avatar
   
Dries Buytaert committed
26
      return t('Controls the boxes that are displayed around the main content.');
Dries Buytaert's avatar
   
Dries Buytaert committed
27
    case 'admin/block':
Dries Buytaert's avatar
   
Dries Buytaert committed
28
      return t("Blocks are the boxes in the left- and right- side bars of the web site, depending on the chosen theme.  They are made available active modules or created manually. The \"enabled\" checkbox sets the default status of the block. Only enabled blocks are shown. When the \"custom\" checkbox is checked, your users can show or hide the block using their account settings. In that case, the 'enabled' checkbox signifies the block's default status. You can assign the block's placement by giving it a region and a weight. The region specifies which side of the page the block is on, and the weight sorts blocks within a region. Lighter (smaller weight value) blocks \"float up\" towards the top of the page. The path setting lets you define which pages you want a block to be shown on.  Blocks can automatically be temporarily disabled to reduce server load when your site becomes extremely busy by checking throttle.  The auto-throttle functionality must be enabled on the <a href=\"%throttle\">throttle configuration page</a> after having enabled the throttle module.", array('%throttle' => url('admin/settings/throttle')));
Dries Buytaert's avatar
   
Dries Buytaert committed
29
    case 'admin/block/add':
30
      return t("Here you can create a new block. Once you have created this block you must make it active and give it a place on the page using <a href=\"%overview\">blocks</a>. The title is used when displaying the block. The description is used in the \"block\" column on the <a href=\"%overview\">blocks</a> page.", array('%overview' => url('admin/block')));
Dries Buytaert's avatar
   
Dries Buytaert committed
31
  }
Dries Buytaert's avatar
   
Dries Buytaert committed
32
33
}

Dries Buytaert's avatar
   
Dries Buytaert committed
34
/**
35
 * Menu callback; presents the block-specific information from admin/help.
Dries Buytaert's avatar
   
Dries Buytaert committed
36
 */
Dries Buytaert's avatar
   
Dries Buytaert committed
37
function block_help_page() {
Dries Buytaert's avatar
   
Dries Buytaert committed
38
  print theme('page', block_help('admin/help#block'));
Dries Buytaert's avatar
   
Dries Buytaert committed
39
40
}

Dries Buytaert's avatar
   
Dries Buytaert committed
41
42
43
/**
 * Implementation of hook_perm().
 */
Dries Buytaert's avatar
   
Dries Buytaert committed
44
function block_perm() {
Dries Buytaert's avatar
   
Dries Buytaert committed
45
  return array('administer blocks');
Dries Buytaert's avatar
   
Dries Buytaert committed
46
47
}

Dries Buytaert's avatar
   
Dries Buytaert committed
48
/**
Dries Buytaert's avatar
   
Dries Buytaert committed
49
 * Implementation of hook_menu().
Dries Buytaert's avatar
   
Dries Buytaert committed
50
 */
Dries Buytaert's avatar
   
Dries Buytaert committed
51
function block_menu($may_cache) {
Dries Buytaert's avatar
   
Dries Buytaert committed
52
  $items = array();
Dries Buytaert's avatar
   
Dries Buytaert committed
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69

  if ($may_cache) {
    $items[] = array('path' => 'admin/block', 'title' => t('blocks'),
      'access' => user_access('administer blocks'),
      'callback' => 'block_admin');
    $items[] = array('path' => 'admin/block/list', 'title' => t('list'),
      'type' => MENU_DEFAULT_LOCAL_TASK, 'weight' => -10);
    $items[] = array('path' => 'admin/block/edit', 'title' => t('edit block'),
      'access' => user_access('administer blocks'),
      'callback' => 'block_box_edit',
      'type' => MENU_CALLBACK);
    $items[] = array('path' => 'admin/block/add', 'title' => t('add'),
      'access' => user_access('administer blocks'),
      'callback' => 'block_box_edit',
      'type' => MENU_LOCAL_TASK);
  }

Dries Buytaert's avatar
   
Dries Buytaert committed
70
  return $items;
Dries Buytaert's avatar
   
Dries Buytaert committed
71
72
}

Dries Buytaert's avatar
   
Dries Buytaert committed
73
74
75
76
77
78
79
80
/**
 * Implementation of hook_block().
 *
 * Generates the administrator-defined blocks for display.
 */
function block_block($op = 'list', $delta = 0) {
  if ($op == 'list') {
    $result = db_query('SELECT bid, title, info FROM {boxes} ORDER BY title');
Dries Buytaert's avatar
   
Dries Buytaert committed
81
    while ($block = db_fetch_object($result)) {
Dries Buytaert's avatar
   
Dries Buytaert committed
82
      $blocks[$block->bid]['info'] = $block->info;
Dries Buytaert's avatar
   
Dries Buytaert committed
83
84
85
86
    }
    return $blocks;
  }
  else {
Dries Buytaert's avatar
   
Dries Buytaert committed
87
88
    $block = db_fetch_object(db_query('SELECT * FROM {boxes} WHERE bid = %d', $delta));
    $data['subject'] = $block->title;
89
    $data['content'] = check_output($block->body, $block->format);
Dries Buytaert's avatar
   
Dries Buytaert committed
90
    return $data;
91
92
93
  }
}

Dries Buytaert's avatar
 
Dries Buytaert committed
94
function block_admin_save($edit) {
Dries Buytaert's avatar
   
Dries Buytaert committed
95
96
  foreach ($edit as $module => $blocks) {
    foreach ($blocks as $delta => $block) {
Dries Buytaert's avatar
   
Dries Buytaert committed
97
      db_query("UPDATE {blocks} SET region = %d, status = %d, custom = %d, path = '%s', weight = %d, throttle = %d WHERE module = '%s' AND delta = '%s'",
Dries Buytaert's avatar
   
Dries Buytaert committed
98
                $block['region'], $block['status'], $block['custom'], $block['path'], $block['weight'], $block['throttle'], $module, $delta);
Dries Buytaert's avatar
   
Dries Buytaert committed
99
    }
Dries Buytaert's avatar
 
Dries Buytaert committed
100
  }
Dries Buytaert's avatar
   
Dries Buytaert committed
101

Dries Buytaert's avatar
   
Dries Buytaert committed
102
  return t('the block settings have been updated.');
Dries Buytaert's avatar
 
Dries Buytaert committed
103
104
}

Dries Buytaert's avatar
   
Dries Buytaert committed
105
/**
Dries Buytaert's avatar
   
Dries Buytaert committed
106
 * Update the 'blocks' DB table with the blocks currently exported by modules.
Dries Buytaert's avatar
   
Dries Buytaert committed
107
 *
Dries Buytaert's avatar
   
Dries Buytaert committed
108
109
110
 * @param $order_by php <a
 *   href="http://www.php.net/manual/en/function.array-multisort.php">array_multisort()</a>
 *   style sort ordering, eg. "weight", SORT_ASC, SORT_STRING.
111
 *
Dries Buytaert's avatar
   
Dries Buytaert committed
112
113
 * @return
 *   Blocks currently exported by modules, sorted by $order_by.
Dries Buytaert's avatar
   
Dries Buytaert committed
114
 */
Dries Buytaert's avatar
   
Dries Buytaert committed
115
116
function _block_rehash($order_by = array('weight')) {
  $result = db_query('SELECT * FROM {blocks} ');
Dries Buytaert's avatar
   
Dries Buytaert committed
117
118
119
120
  while ($old_block = db_fetch_object($result)) {
    $old_blocks[$old_block->module][$old_block->delta] = $old_block;
  }

Dries Buytaert's avatar
   
Dries Buytaert committed
121
  db_query('DELETE FROM {blocks} ');
Dries Buytaert's avatar
   
Dries Buytaert committed
122
123

  foreach (module_list() as $module) {
Dries Buytaert's avatar
   
Dries Buytaert committed
124
    $module_blocks = module_invoke($module, 'block', 'list');
Dries Buytaert's avatar
   
Dries Buytaert committed
125
126
    if ($module_blocks) {
      foreach ($module_blocks as $delta => $block) {
Dries Buytaert's avatar
   
Dries Buytaert committed
127
128
        $block['module'] = $module;
        $block['delta']  = $delta;
Dries Buytaert's avatar
   
Dries Buytaert committed
129
        if ($old_blocks[$module][$delta]) {
Dries Buytaert's avatar
   
Dries Buytaert committed
130
131
132
133
134
135
          $block['status'] = $old_blocks[$module][$delta]->status;
          $block['weight'] = $old_blocks[$module][$delta]->weight;
          $block['region'] = $old_blocks[$module][$delta]->region;
          $block['path']   = $old_blocks[$module][$delta]->path;
          $block['custom'] = $old_blocks[$module][$delta]->custom;
          $block['throttle'] = $old_blocks[$module][$delta]->throttle;
Dries Buytaert's avatar
   
Dries Buytaert committed
136
137
        }
        else {
Dries Buytaert's avatar
   
Dries Buytaert committed
138
139
          $block['status'] = $block['weight'] = $block['region'] = $block['custom'] = 0;
          $block['path']   = '';
Dries Buytaert's avatar
   
Dries Buytaert committed
140
141
142
        }

        // reinsert blocks into table
Dries Buytaert's avatar
   
Dries Buytaert committed
143
        db_query("INSERT INTO {blocks} (module, delta, status, weight, region, path, custom, throttle) VALUES ('%s', '%s', %d, %d, %d, '%s', %d, %d)",
Dries Buytaert's avatar
   
Dries Buytaert committed
144
                  $block['module'], $block['delta'], $block['status'], $block['weight'], $block['region'], $block['path'], $block['custom'], $block['throttle']);
Dries Buytaert's avatar
   
Dries Buytaert committed
145
146

        $blocks[] = $block;
Dries Buytaert's avatar
   
Dries Buytaert committed
147

Dries Buytaert's avatar
   
Dries Buytaert committed
148
149
        // build array to sort on
        $order[$order_by[0]][] = $block[$order_by[0]];
Dries Buytaert's avatar
   
Dries Buytaert committed
150
151
152
153
      }
    }
  }

Dries Buytaert's avatar
   
Dries Buytaert committed
154
155
156
  // sort
  array_multisort($order[$order_by[0]], $order_by[1] ? $order_by[1] : SORT_ASC, $order_by[2] ? $order_by[2] : SORT_REGULAR, $blocks);

Dries Buytaert's avatar
   
Dries Buytaert committed
157
158
  return $blocks;
}
Dries Buytaert's avatar
   
Dries Buytaert committed
159

Dries Buytaert's avatar
   
Dries Buytaert committed
160
161
162
/**
 * Prepare the main block administration form.
 */
Dries Buytaert's avatar
   
Dries Buytaert committed
163
function block_admin_display() {
Dries Buytaert's avatar
   
Dries Buytaert committed
164

Dries Buytaert's avatar
   
Dries Buytaert committed
165
  $blocks = _block_rehash();
Dries Buytaert's avatar
   
Dries Buytaert committed
166

167
168
169
170
171
172
173
  // Fetch input formats used by admin-defined boxes.
  $formats = array();
  $result = db_query('SELECT bid, format FROM {boxes}');
  while ($box = db_fetch_object($result)) {
    $formats[$box->bid] = $box->format;
  }

174
  $header = array(t('Block'), t('Enabled'), t('Custom'), t('Throttle'), t('Weight'), t('Region'), t('Path'), array('data' => t('Operations')));
Dries Buytaert's avatar
   
Dries Buytaert committed
175

Dries Buytaert's avatar
   
Dries Buytaert committed
176
  foreach ($blocks as $block) {
177
178
    if ($block['module'] == 'block' && filter_access($formats[$block['delta']])) {
      $edit = l(t('edit block'), 'admin/block/edit/'. $block['delta']);
179
    }
Dries Buytaert's avatar
   
Dries Buytaert committed
180
    else {
Dries Buytaert's avatar
   
Dries Buytaert committed
181
      $edit = '';
Dries Buytaert's avatar
   
Dries Buytaert committed
182
    }
Dries Buytaert's avatar
   
Dries Buytaert committed
183

184
    $rows[] = array($block['info'], array('data' => form_checkbox(NULL, $block['module'] .']['. $block['delta'] .'][status', 1, $block['status']), 'align' => 'center'), array('data' => form_checkbox(NULL, $block['module'] .']['. $block['delta'] .'][custom', 1, $block['custom']), 'align' => 'center'), array('data' => form_checkbox(NULL, $block['module'] .']['. $block['delta'] .'][throttle', 1, $block['throttle'], NULL, module_exist('throttle') ? NULL : array('disabled' => 'disabled')), 'align' => 'center'), form_weight(NULL, $block['module'] .']['. $block['delta'] .'][weight', $block['weight']), form_radios(NULL, $block['module'] .']['. $block['delta'] .'][region', $block['region'], array(t('left'), t('right'))), form_textfield(NULL, $block['module'] .']['. $block['delta'] .'][path', $block['path'], 10, 255), $edit);
Dries Buytaert's avatar
 
Dries Buytaert committed
185
  }
Dries Buytaert's avatar
   
Dries Buytaert committed
186

Dries Buytaert's avatar
   
Dries Buytaert committed
187
188
  $output = theme('table', $header, $rows);
  $output .= form_submit(t('Save blocks'));
Dries Buytaert's avatar
 
Dries Buytaert committed
189

Dries Buytaert's avatar
   
Dries Buytaert committed
190
  return form($output, 'post', url('admin/block'));
Dries Buytaert's avatar
 
Dries Buytaert committed
191
192
}

193
function block_box_get($bid) {
Dries Buytaert's avatar
   
Dries Buytaert committed
194
195
196
197
  return db_fetch_array(db_query('SELECT * FROM {boxes} WHERE bid = %d', $bid));
}

/**
198
199
200
 * Menu callback; displays the block editing form.
 *
 * On edit, saves changes and displays the block overview.
Dries Buytaert's avatar
   
Dries Buytaert committed
201
202
203
204
205
 */
function block_box_edit($bid = 0) {
  $edit = $_POST['edit'];
  $op = $_POST['op'];

206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
  switch ($op) {
    case t('Save block'):
      drupal_set_message(block_box_save($edit));
      cache_clear_all();
      drupal_goto('admin/block');

    case t('Delete block'):
      $form = '<p>'. t('Are you sure you want to delete the block %name?', array('%name' => '<em>'. $edit['info'] .'</em>')) ."</p>\n";
      $form .= form_submit(t('Delete'));
      $output = form($form);
      break;

    case t('Delete'):
      db_query('DELETE FROM {boxes} WHERE bid = %d', $bid);
      drupal_set_message(t('The block has been deleted.'));
      cache_clear_all();
      drupal_goto('admin/block');

    case t('Cancel'):
    default:
      if ($bid) {
        $output = block_box_form(block_box_get($bid));
      }
      else {
        $output = block_box_form();
      }
Dries Buytaert's avatar
   
Dries Buytaert committed
232
233
234
  }

  print theme('page', $output);
235
236
237
}

function block_box_form($edit = array()) {
238
239
240
241
  $output = form_textfield(t('Block title'), 'title', $edit['title'], 50, 64, t('The title of the block as shown to the user.'));
  $output .= filter_form('format', $edit['format']);
  $output .= form_textarea(t('Block body'), 'body', $edit['body'], 70, 10, t('The content of the block as shown to the user.'));
  $output .= form_textfield(t('Block description'), 'info', $edit['info'], 50, 64, t('A brief description of your block.  Used on the <a href="%overview">block overview page</a>.', array('%overview' => url('admin/block'))));
242

Dries Buytaert's avatar
   
Dries Buytaert committed
243
  if ($edit['bid']) {
244
    $output .= form_hidden('bid', $edit['bid']);
245
246
  }

247
248
249
250
  $output .= form_submit(t('Save block'));
  if ($edit['bid']) {
    $output .= form_submit(t('Delete block'));
  }
251

252
  return form($output);
253
254
255
}

function block_box_save($edit) {
256
257
  if (!filter_access($edit['format'])) {
    $edit['format'] = FILTER_FORMAT_DEFAULT;
Dries Buytaert's avatar
   
Dries Buytaert committed
258
259
  }

Dries Buytaert's avatar
   
Dries Buytaert committed
260
  if ($edit['bid']) {
261
    db_query("UPDATE {boxes} SET title = '%s', body = '%s', info = '%s', format = %d WHERE bid = %d", $edit['title'], $edit['body'], $edit['info'], $edit['format'], $edit['bid']);
Dries Buytaert's avatar
   
Dries Buytaert committed
262
    return t('The block has been updated.');
263
264
  }
  else {
265
    db_query("INSERT INTO {boxes} (title, body, info, format) VALUES  ('%s', '%s', '%s', %d)", $edit['title'], $edit['body'], $edit['info'], $edit['format']);
Dries Buytaert's avatar
   
Dries Buytaert committed
266
    return t('The new block has been added.');
267
268
269
  }
}

Dries Buytaert's avatar
   
Dries Buytaert committed
270
/**
271
 * Menu callback; displays the block overview page.
Dries Buytaert's avatar
   
Dries Buytaert committed
272
 */
Dries Buytaert's avatar
 
Dries Buytaert committed
273
function block_admin() {
Dries Buytaert's avatar
   
Dries Buytaert committed
274
275
  $edit = $_POST['edit'];
  $op = $_POST['op'];
Dries Buytaert's avatar
   
Dries Buytaert committed
276

Dries Buytaert's avatar
   
Dries Buytaert committed
277
278
279
  if ($op == t('Save blocks')) {
    drupal_set_message(block_admin_save($edit));
    cache_clear_all();
280
    drupal_goto($_GET['q']);
Dries Buytaert's avatar
   
Dries Buytaert committed
281
  }
Dries Buytaert's avatar
   
Dries Buytaert committed
282
  print theme('page', block_admin_display());
Dries Buytaert's avatar
 
Dries Buytaert committed
283
}
Dries Buytaert's avatar
   
Dries Buytaert committed
284

Dries Buytaert's avatar
   
Dries Buytaert committed
285
286
287
288
289
290
/**
 * Implementation of hook_user().
 *
 * Allow users to decide which custom blocks to display when they visit
 * the site.
 */
291
function block_user($type, $edit, &$user, $category = NULL) {
292
  switch ($type) {
293
    case 'form':
294
295
296
297
298
299
300
301
      if ($category == 'account') {
        $result = db_query('SELECT * FROM {blocks} WHERE custom = %d ORDER BY module, delta', 1);

        while ($block = db_fetch_object($result)) {
          $data = module_invoke($block->module, 'block', 'list');
          if ($data[$block->delta]['info']) {
            $form .= form_checkbox($data[$block->delta]['info'], "block][$block->module][$block->delta", 1, isset($user->block[$block->module][$block->delta]) ? $user->block[$block->module][$block->delta] : $block->status);
          }
Kjartan Mannes's avatar
Kjartan Mannes committed
302
303
        }

304
305
306
        if (isset($form)) {
          return array(array('title' => t('Block configuration'), 'data' => $form, 'weight' => 2));
        }
307
      }
Dries Buytaert's avatar
   
Dries Buytaert committed
308
309

      break;
Dries Buytaert's avatar
   
Dries Buytaert committed
310
311
312
    case 'validate':
      if (!$edit['block']) {
        $edit['block'] = array();
313
314
      }
      return $edit;
315
316
317
  }
}

318
319
320
/**
 * Return blocks available for current $user at $region.
 *
Dries Buytaert's avatar
   
Dries Buytaert committed
321
 * @param $region main|left|right
322
 *
Dries Buytaert's avatar
   
Dries Buytaert committed
323
 * @return array of block objects, indexed with <i>module</i>_<i>delta</i>
324
 *
Dries Buytaert's avatar
   
Dries Buytaert committed
325
 * @see <a href="http://drupal.org/node/1042" target="_top">[feature]
326
 *   Generic template design difficult w/o block region "look-ahead"</a>
Dries Buytaert's avatar
   
Dries Buytaert committed
327
 * @todo add a proper primary key (bid) to the blocks table so we don't have
328
329
330
331
 *   to mess around with this <i>module</i>_<i>delta</i> construct. currently,
 *   "blocks" has no primary key defined (bad)!
 */
function block_list($region) {
332
  global $user, $base_url;
333
334
335
336
  static $blocks = array();

  if (!isset($blocks[$region])) {
    $blocks[$region] = array();
Dries Buytaert's avatar
   
Dries Buytaert committed
337
    $result = db_query('SELECT * FROM {blocks} WHERE (status = 1 OR custom = 1) '. ($region != 'all' ? 'AND region = %d ' : '') .'ORDER BY weight, module', $region == 'left' ? 0 : 1);
338
339

    while ($result && ($block = db_fetch_array($result))) {
340
341
342
343
      // When the user's account setting is empty, we use the block's regular 'status' (which is the default)
      if ($block['custom'] && $user->uid && !isset($user->block[$block['module']][$block['delta']])) {
        $user->block[$block['module']][$block['delta']] = $block['status'];
      }
Dries Buytaert's avatar
   
Dries Buytaert committed
344

345
346
347
348
349
350
351
352
      // Determine block visibility
      $enabled = $block['status'] && (!$user->uid || !$block['custom']);
      $custom = $block['custom'] && $user->block[$block['module']][$block['delta']];

      // Match path if necessary
      if ($block['path']) {
        $base = parse_url($base_url);
        $session = session_name() .'='. session_id();
Steven Wittens's avatar
Steven Wittens committed
353
        $url = str_replace(array($base['path'], '?'. $session), '', request_uri());
Dries Buytaert's avatar
   
Dries Buytaert committed
354
        $url = ereg_replace('^/(\?q=)?', '', $url);
Steven Wittens's avatar
Steven Wittens committed
355
        $matched = preg_match($block['path'], $url);
356
357
358
359
360
361
      }
      else {
        $matched = true;
      }

      if (($enabled || $custom) && $matched) {
Dries Buytaert's avatar
   
Dries Buytaert committed
362
        /*
363
364
        ** Check the current throttle status and see if block should be displayed
        ** based on server load.
Dries Buytaert's avatar
   
Dries Buytaert committed
365
        */
Dries Buytaert's avatar
   
Dries Buytaert committed
366
        if (!($block['throttle'] && (module_invoke('throttle', 'status') > 4))) {
367
          $array = module_invoke($block['module'], 'block', 'view', $block['delta']);
Dries Buytaert's avatar
   
Dries Buytaert committed
368
          if (is_array($array)) {
369
370
            $block = array_merge($block, $array);
          }
Dries Buytaert's avatar
   
Dries Buytaert committed
371
        }
Dries Buytaert's avatar
   
Dries Buytaert committed
372
        if (isset($block['content']) && $block['content']) {
373
374
375
376
377
378
379
380
          $blocks[$region]["$block[module]_$block[delta]"] = (object) $block;
        }
      }
    }
  }
  return $blocks[$region];
}

381
?>