Projet

Général

Profil

Paste
Télécharger (24,2 ko) Statistiques
| Branche: | Révision:

root / htmltest / sites / all / modules / print / print.pages.inc @ dc45a079

1
<?php
2

    
3
/**
4
 * @file
5
 * Contains the functions to generate Printer-friendly pages.
6
 *
7
 * This file is included by the core PF module, and includes all the
8
 * functions necessary to generate a PF version of the original page
9
 * in HTML format.
10
 *
11
 * @ingroup print
12
 */
13

    
14
$_print_urls = PRINT_URLS_DEFAULT;
15

    
16
/**
17
 * Generate an HTML version of the printer-friendly page
18
 *
19
 * @see print_controller()
20
 */
21
function print_controller_html() {
22
  $args = func_get_args();
23
  $path = filter_xss(implode('/', $args));
24
  $cid = isset($_GET['comment']) ? (int)$_GET['comment'] : NULL;
25

    
26
  // Handle the query
27
  $query = $_GET;
28
  unset($query['q']);
29

    
30
  $print = print_controller($path, $query, $cid, PRINT_HTML_FORMAT);
31
  if ($print !== FALSE) {
32
    $node = $print['node'];
33
    $html = theme('print', array('print' => $print, 'type' => PRINT_HTML_FORMAT, 'node' => $node));
34
    drupal_add_http_header('Content-Type', 'text/html; charset=utf-8');
35
    drupal_send_headers();
36
    print $html;
37

    
38
    $nodepath = (isset($node->path) && is_string($node->path)) ? drupal_get_normal_path($node->path) : 'node/' . $path;
39
    db_merge('print_page_counter')
40
      ->key(array('path' => $nodepath))
41
      ->fields(array(
42
          'totalcount' => 1,
43
          'timestamp' => REQUEST_TIME,
44
      ))
45
      ->expression('totalcount', 'totalcount + 1')
46
      ->execute();
47
  }
48
}
49

    
50
/**
51
 * Select the print generator function based on the page type
52
 *
53
 * Depending on the type of node, this functions chooses the appropriate
54
 * generator function.
55
 *
56
 * @param $path
57
 *   path of the original page
58
 * @param array $query
59
 *   (optional) array of key/value pairs as used in the url() function for the
60
 *   query
61
 * @param $cid
62
 *   comment ID of the individual comment to be rendered
63
 * @param $format
64
 *   format of the page being generated
65
 * @param $teaser
66
 *   if set to TRUE, outputs only the node's teaser
67
 * @param $message
68
 *   optional sender's message (used by the send email module)
69
 * @return
70
 *   array with the fields to be used in the template
71
 * @see _print_generate_node()
72
 * @see _print_generate_path()
73
 * @see _print_generate_book()
74
 */
75
function print_controller($path, $query = NULL, $cid = NULL, $format = PRINT_HTML_FORMAT, $teaser = FALSE, $message = NULL) {
76
  if (empty($path)) {
77
    // If no path was provided, let's try to generate a page for the referer
78
    global $base_url;
79

    
80
    $ref = $_SERVER['HTTP_REFERER'];
81
    $path = preg_replace("!^$base_url/!", '', $ref);
82
    if (($path === $ref) || empty($path)) {
83
      $path = variable_get('site_frontpage', 'node');
84
    }
85
  }
86
  if ($alias = drupal_lookup_path('source', $path)) {
87
    // Indirect call with print/alias
88
    // If there is a path alias with these arguments, generate a printer-friendly version for it
89
    $path = $alias;
90
  }
91
  $parts = explode('/', $path);
92
  if (($parts[0] == 'node') && (count($parts) > 1) && ctype_digit($parts[1])) {
93
    array_shift($parts);
94
    $path = implode('/', $parts);
95
  }
96
  if (ctype_digit($parts[0]) && (count($parts) == 1)) {
97
    $print = _print_generate_node($path, $query, $cid, $format, $teaser, $message);
98
  }
99
  else {
100
    $ret = preg_match('!^book/export/html/(.*)!i', $path, $matches);
101
    if ($ret == 1) {
102
      // This is a book PF page link, handle trough the book handling functions
103
      $print = _print_generate_book($matches[1], $query, $format, $teaser, $message);
104
    }
105
    else {
106
      // If no content node was found, handle the page printing with the 'printable' engine
107
      $print = _print_generate_path($path, $query, $format, $teaser, $message);
108
    }
109
  }
110

    
111
  return $print;
112
}
113

    
114
/**
115
 * Generates a robots meta tag to tell them what they may index
116
 *
117
 * @return
118
 *   string with the meta robots tag
119
 */
120
function _print_robots_meta_generator() {
121
  $print_robots_noindex = variable_get('print_robots_noindex', PRINT_ROBOTS_NOINDEX_DEFAULT);
122
  $print_robots_nofollow = variable_get('print_robots_nofollow', PRINT_ROBOTS_NOFOLLOW_DEFAULT);
123
  $print_robots_noarchive = variable_get('print_robots_noarchive', PRINT_ROBOTS_NOARCHIVE_DEFAULT);
124
  $robots_meta = array();
125

    
126
  if (!empty($print_robots_noindex)) {
127
    $robots_meta[] = 'noindex';
128
  }
129
  if (!empty($print_robots_nofollow)) {
130
    $robots_meta[] = 'nofollow';
131
  }
132
  if (!empty($print_robots_noarchive)) {
133
    $robots_meta[] = 'noarchive';
134
  }
135

    
136
  if (count($robots_meta) > 0) {
137
    $robots_meta = implode(', ', $robots_meta);
138
    $robots_meta = "<meta name='robots' content='$robots_meta' />\n";
139
  }
140
  else {
141
    $robots_meta = '';
142
  }
143

    
144
  return $robots_meta;
145
}
146

    
147
/**
148
 * Post-processor that fills the array for the template with common details
149
 *
150
 * @param $node
151
 *   generated node with a printer-friendly node body
152
 * @param array $query
153
 *   (optional) array of key/value pairs as used in the url() function for the
154
 *   query
155
 * @param $message
156
 *   optional sender's message (used by the send email module)
157
 * @param $cid
158
 *   id of current comment being generated (NULL when not generating
159
 *   an individual comment)
160
 * @return
161
 *   array with the fields to be used in the template
162
 */
163
function _print_var_generator($node, $query = NULL, $message = NULL, $cid = NULL) {
164
  global $base_url, $language, $_print_urls;
165

    
166
  $path = empty($node->nid) ? $node->path : "node/$node->nid";
167

    
168
  // print module settings
169
  $print_css = variable_get('print_css', PRINT_CSS_DEFAULT);
170
  $print_keep_theme_css = variable_get('print_keep_theme_css', PRINT_KEEP_THEME_CSS_DEFAULT);
171
  $print_logo_options = variable_get('print_logo_options', PRINT_LOGO_OPTIONS_DEFAULT);
172
  $print_logo_url = variable_get('print_logo_url', PRINT_LOGO_URL_DEFAULT);
173
  $print_html_new_window = variable_get('print_html_new_window', PRINT_HTML_NEW_WINDOW_DEFAULT);
174
  $print_html_sendtoprinter = variable_get('print_html_sendtoprinter', PRINT_HTML_SENDTOPRINTER_DEFAULT);
175
  $print_html_windowclose = variable_get('print_html_windowclose', PRINT_HTML_WINDOWCLOSE_DEFAULT);
176
  $print_sourceurl_enabled = variable_get('print_sourceurl_enabled', PRINT_SOURCEURL_ENABLED_DEFAULT);
177
  $print_sourceurl_forcenode = variable_get('print_sourceurl_forcenode', PRINT_SOURCEURL_FORCENODE_DEFAULT);
178
  $print_sourceurl_date = variable_get('print_sourceurl_date', PRINT_SOURCEURL_DATE_DEFAULT);
179
  $print_footer_options = variable_get('print_footer_options', PRINT_FOOTER_OPTIONS_DEFAULT);
180
  $print_footer_user = variable_get('print_footer_user', PRINT_FOOTER_USER_DEFAULT);
181

    
182
  $print['language'] = $language->language;
183
  $print['title'] = check_plain($node->title);
184
  $print['head'] = drupal_get_html_head();
185
  if ($print_html_sendtoprinter) {
186
    drupal_add_js('misc/drupal.js', array('weight' => JS_LIBRARY));
187
  }
188
  $print['scripts'] = drupal_get_js();
189
  $print['footer_scripts'] = drupal_get_js('footer');
190
  $print['robots_meta'] = _print_robots_meta_generator();
191
  $print['url'] = url($path, array('absolute' => TRUE, 'query' => $query));
192
  $print['base_href'] = "<base href='" . $print['url'] . "' />\n";
193
  $print['favicon'] = theme_get_setting('toggle_favicon') ? "<link rel='shortcut icon' href='" . theme_get_setting('favicon') . "' type='image/x-icon' />\n" : '';
194

    
195
  if (!empty($print_css)) {
196
    drupal_add_css(strtr($print_css, array('%t' => path_to_theme())));
197
  }
198
  else {
199
    drupal_add_css(drupal_get_path('module', 'print') . '/css/print.css');
200
  }
201
  $drupal_css = drupal_add_css();
202
  if (!$print_keep_theme_css) {
203
    foreach ($drupal_css as $key => $css_file) {
204
      if ($css_file['group'] == CSS_THEME) {
205
      // Unset the theme's CSS
206
        unset($drupal_css[$key]);
207
      }
208
    }
209
  }
210

    
211
  // If we are sending a message via email, the CSS must be embedded
212
  if (!empty($message)) {
213
    $style = '';
214
    $css_files = array_keys($drupal_css);
215
    foreach ($css_files as $filename) {
216
      $res = file_exists($filename) ? file_get_contents($filename, TRUE) : FALSE;
217
      if ($res != FALSE) {
218
        $style .= $res;
219
      }
220
    }
221
    $print['css'] = "<style type='text/css' media='all'>$style</style>\n";
222
  }
223
  else {
224
    $print['css'] = drupal_get_css($drupal_css);
225
  }
226

    
227
  $window_close = ($print_html_new_window && $print_html_windowclose) ? 'window.close();' : '';
228
  $print['sendtoprinter'] = $print_html_sendtoprinter ? '<script type="text/javascript">(function ($) { Drupal.behaviors.print = {attach: function(context) {$(window).load(function() {window.print();' . $window_close . '})}}})(jQuery);</script>' : '';
229

    
230
  switch ($print_logo_options) {
231
    case 0: // none
232
      $logo_url = 0;
233
      break;
234
    case 1: // theme's
235
      $logo_url = theme_get_setting('logo');
236
      break;
237
    case 2: // user-specifed
238
      $logo_url = strip_tags($print_logo_url);
239
      break;
240
  }
241
  $logo_url = preg_replace('!^' . base_path() . '!', '', $logo_url);
242
  $site_name = variable_get('site_name', 'Drupal');
243
  $print['logo'] = $logo_url ? theme('image', array('path' => $logo_url, 'alt' => $site_name, 'attributes' => array('class' => 'print-logo', 'id' => 'logo'))) : '';
244

    
245
  switch ($print_footer_options) {
246
    case 0: // none
247
      $footer = '';
248
      break;
249
    case 1: // theme's
250
      $footer_blocks = block_get_blocks_by_region('footer');
251
      $footer = variable_get('site_footer', FALSE) . "\n" . drupal_render($footer_blocks);
252
      break;
253
    case 2: // user-specifed
254
      $footer = $print_footer_user;
255
      break;
256
  }
257
  $print['footer_message'] = filter_xss_admin($footer);
258

    
259
  $published_site = variable_get('site_name', 0);
260
  if ($published_site) {
261
    $print_text_published = filter_xss(variable_get('print_text_published', t('Published on %site_name')));
262
    $published = t($print_text_published, array('%site_name' => $published_site));
263
    $print['site_name'] = $published . ' (' . l($base_url, $base_url) . ')';
264
  }
265
  else {
266
    $print['site_name'] = '';
267
  }
268

    
269
  if ($print_sourceurl_enabled == 1) {
270
    /* Grab and format the src URL */
271
    if (empty($print_sourceurl_forcenode)) {
272
      $url = $print['url'];
273
    }
274
    else {
275
      $url = $base_url . '/' . (((bool)variable_get('clean_url', '0')) ? '' : '?q=') . $path;
276
    }
277
    if (is_int($cid)) {
278
      $url .= "#comment-$cid";
279
    }
280
    $retrieved_date = format_date(REQUEST_TIME, 'short');
281
    $print_text_retrieved = filter_xss(variable_get('print_text_retrieved', t('retrieved on %date')));
282
    $retrieved = t($print_text_retrieved, array('%date' => $retrieved_date));
283
    $print['printdate'] = $print_sourceurl_date ? " ($retrieved)" : '';
284

    
285
    $source_url = filter_xss(variable_get('print_text_source_url', t('Source URL')));
286
    $print['source_url'] = '<strong>' . $source_url . $print['printdate'] . ':</strong> ' . l($url, $url);
287
  }
288
  else {
289
    $print['source_url'] = '';
290
  }
291

    
292
  $print['type'] = (isset($node->type)) ? $node->type : '';
293

    
294
  menu_set_active_item($path);
295
  $breadcrumb = drupal_get_breadcrumb();
296
  if (!empty($breadcrumb)) {
297
    $breadcrumb[] = menu_get_active_title();
298
    $print['breadcrumb'] = filter_xss(implode(' > ', $breadcrumb));
299
  }
300
  else {
301
    $print['breadcrumb'] = '';
302
  }
303

    
304
  // Display the collected links at the bottom of the page. Code once taken from Kjartan Mannes' project.module
305
  $print['pfp_links'] = '';
306
  if (!empty($_print_urls)) {
307
    $urls = _print_friendly_urls();
308
    $max = count($urls);
309
    $pfp_links = '';
310
    if ($max) {
311
      for ($i = 0; $i < $max; $i++) {
312
        $pfp_links .= '[' . ($i + 1) . '] ' . check_plain($urls[$i]) . "<br />\n";
313
      }
314
      $links = filter_xss(variable_get('print_text_links', t('Links')));
315
      $print['pfp_links'] = "<p><strong>$links:</strong><br />$pfp_links</p>";
316
    }
317
  }
318

    
319
  $print['node'] = $node;
320
  $print['message'] = $message;
321

    
322
  return $print;
323
}
324

    
325
/**
326
 * Callback function for the preg_replace_callback for URL-capable patterns
327
 *
328
 * Manipulate URLs to make them absolute in the URLs list, and to add a
329
 * [n] footnote marker.
330
 *
331
 * @param $matches
332
 *   array with the matched tag patterns, usually <a...>+text+</a>
333
 * @return
334
 *   tag with re-written URL and when appropriate the [n] index to the
335
 *   URL list
336
 */
337
function _print_rewrite_urls($matches) {
338
  global $base_url, $base_root, $_print_urls;
339

    
340
  $include_anchors = variable_get('print_urls_anchors', PRINT_URLS_ANCHORS_DEFAULT);
341

    
342
  // first, split the html into the different tag attributes
343
  $pattern = '!\s*(\w+\s*=\s*"(?:\\\"|[^"])*")\s*|\s*(\w+\s*=\s*\'(?:\\\\\'|[^\'])*\')\s*|\s*(\w+\s*=\s*\w+)\s*|\s+!';
344
  $attribs = preg_split($pattern, $matches[1], -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
345
  foreach ($attribs as $key => $value) {
346
    $attribs[$key] = preg_replace('!(\w)\s*=\s*(.*)!', '$1=$2', $value);
347
  }
348

    
349
  $size = count($attribs);
350
  for ($i=1; $i < $size; $i++) {
351
    // If the attribute is href or src, we may need to rewrite the URL in the value
352
    if (preg_match('!^(?:href|src)\s*?=(.*)!i', $attribs[$i], $urls) > 0) {
353
      $url = trim($urls[1], " \t\n\r\0\x0B\"'");
354

    
355
      if (empty($url)) {
356
        // If URL is empty, use current_url
357
        $path = explode('/', $_GET['q']);
358
        unset($path[0]);
359
        $path = implode('/', $path);
360
        if (ctype_digit($path)) {
361
          $path = "node/$path";
362
        }
363
        // Printer-friendly URLs is on, so we need to make it absolute
364
        $newurl = url($path, array('fragment' => drupal_substr($url, 1), 'absolute' => TRUE));
365
      }
366
      elseif (strpos(html_entity_decode($url), '://') || preg_match('!^mailto:.*?@.*?\..*?$!iu', html_entity_decode($url))) {
367
        // URL is absolute, do nothing
368
        $newurl = $url;
369
      }
370
      elseif (strpos(html_entity_decode($url), '//') === 0) {
371
        // URL is 'almost absolute', but it does not contain protocol; replace with base_path protocol
372
        $newurl = (empty($_SERVER['HTTPS']) ? 'http' : 'https') . ":" . $url;
373
        $matches[1] = str_replace($url, $newurl, $matches[1]);
374
      }
375
      else {
376
        if ($url[0] == '#') {
377
          // URL is an anchor tag
378
          if ($include_anchors && (!empty($_print_urls))) {
379
            $path = explode('/', $_GET['q']);
380
            unset($path[0]);
381
            $path = implode('/', $path);
382
            if (ctype_digit($path)) {
383
              $path = "node/$path";
384
            }
385
            // Printer-friendly URLs is on, so we need to make it absolute
386
            $newurl = url($path, array('fragment' => drupal_substr($url, 1), 'absolute' => TRUE));
387
          }
388
          // Because base href is the original page, change the link to
389
          // still be usable inside the print page
390
          $matches[1] = str_replace($url, base_path() . $_GET['q'] . $url, $matches[1]);
391
        }
392
        else {
393
          // URL is relative, convert it into absolute URL
394
          if ($url[0] == '/') {
395
            // If it starts with '/' just append it to the server name
396
            $newurl = $base_root . '/' . trim($url, '/');
397
          }
398
          elseif (preg_match('!^(?:index.php)?\?q=!i', $url)) {
399
            // If it starts with ?q=, just prepend with the base URL
400
            $newurl = $base_url . '/' . trim($url, '/');
401
          }
402
          else {
403
            $newurl = url(trim($url, '/'), array('absolute' => TRUE));
404
          }
405
          $matches[1] = str_replace($url, $newurl, $matches[1]);
406
        }
407
      }
408
    }
409
  }
410

    
411
  $ret = '<' . $matches[1] . '>';
412
  if (count($matches) == 4) {
413
    $ret .= $matches[2] . $matches[3];
414
    if ((!empty($_print_urls)) && (isset($newurl))) {
415
      $ret .= ' <span class="print-footnote">[' . _print_friendly_urls(trim($newurl)) . ']</span>';
416
    }
417
  }
418

    
419
  return $ret;
420
}
421

    
422
/**
423
 * Auxiliary function to store the Printer-friendly URLs list as static.
424
 *
425
 * @param $url
426
 *   absolute URL to be inserted in the list
427
 * @return
428
 *   list of URLs previously stored if $url is 0, or the current count
429
 *   otherwise.
430
 */
431
function _print_friendly_urls($url = 0) {
432
  static $urls = array();
433
  if ($url !== 0) {
434
    $url_idx = array_search($url, $urls);
435
    if ($url_idx !== FALSE) {
436
      return ($url_idx + 1);
437
    }
438
    else {
439
      $urls[] = $url;
440
      return count($urls);
441
    }
442
  }
443
  $ret = $urls;
444
  $urls = array();
445
  return $ret;
446
}
447

    
448
/**
449
 * Check URL list settings for this node
450
 *
451
 * @param node
452
 *   node object
453
 * @param $format
454
 *   format of the page being generated
455
 * @return
456
 *   TRUE if URL list should be displayed, FALSE otherwise
457
 */
458
function _print_url_list_enabled($node, $format = PRINT_HTML_FORMAT) {
459
  if (!isset($node->type)) {
460
    switch ($format) {
461
      case PRINT_HTML_FORMAT:
462
        $node_urllist = variable_get('print_display_sys_urllist', PRINT_TYPE_SYS_URLLIST_DEFAULT);
463
        break;
464
      case PRINT_MAIL_FORMAT:
465
        $node_urllist = variable_get('print_mail_display_sys_urllist', PRINT_TYPE_SYS_URLLIST_DEFAULT);
466
        break;
467
      case PRINT_PDF_FORMAT:
468
        $node_urllist = variable_get('print_pdf_display_sys_urllist', PRINT_TYPE_SYS_URLLIST_DEFAULT);
469
        break;
470
      default:
471
        $node_urllist = PRINT_TYPE_SYS_URLLIST_DEFAULT;
472
    }
473
  }
474
  else {
475
    switch ($format) {
476
      case PRINT_HTML_FORMAT:
477
        $node_urllist = isset($node->print_display_urllist) ? $node->print_display_urllist : variable_get('print_display_urllist_' . $node->type, PRINT_TYPE_URLLIST_DEFAULT);
478
        break;
479
      case PRINT_MAIL_FORMAT:
480
        $node_urllist = isset($node->print_mail_display_urllist) ? $node->print_mail_display_urllist : variable_get('print_mail_display_urllist_' . $node->type, PRINT_TYPE_URLLIST_DEFAULT);
481
        break;
482
      case PRINT_PDF_FORMAT:
483
        $node_urllist = isset($node->print_pdf_display_urllist) ? $node->print_pdf_display_urllist : variable_get('print_pdf_display_urllist_' . $node->type, PRINT_TYPE_URLLIST_DEFAULT);
484
        break;
485
      default:
486
        $node_urllist = PRINT_TYPE_URLLIST_DEFAULT;
487
    }
488
  }
489

    
490
  // Get value of Printer-friendly URLs setting
491
  return (variable_get('print_urls', PRINT_URLS_DEFAULT) && ($node_urllist));
492
}
493

    
494
/**
495
 * Prepare a Printer-friendly-ready node body for content nodes
496
 *
497
 * @param $nid
498
 *   node ID of the node to be rendered into a printer-friendly page
499
 * @param array $query
500
 *   (optional) array of key/value pairs as used in the url() function for the
501
 *   query
502
 * @param $cid
503
 *   comment ID of the individual comment to be rendered
504
 * @param $format
505
 *   format of the page being generated
506
 * @param $teaser
507
 *   if set to TRUE, outputs only the node's teaser
508
 * @param $message
509
 *   optional sender's message (used by the send email module)
510
 * @return
511
 *   filled array ready to be used in the template
512
 */
513
function _print_generate_node($nid, $query = NULL, $cid = NULL, $format = PRINT_HTML_FORMAT, $teaser = FALSE, $message = NULL) {
514
  global $_print_urls;
515

    
516
  if (!isset($langcode)) {
517
    $langcode = $GLOBALS['language_content']->language;
518
  }
519

    
520
  // We can take a node id
521
  $node = node_load($nid);
522
  if (!$node) {
523
    // Node not found
524
    drupal_not_found();
525
    return FALSE;
526
  }
527
  elseif (!node_access('view', $node)) {
528
    // Access is denied
529
    drupal_access_denied();
530
    return FALSE;
531
  }
532
  drupal_set_title($node->title);
533

    
534
  $view_mode = $teaser ? 'teaser' : 'print';
535

    
536
  // Turn off Pagination by the Paging module
537
  unset($node->pages);
538
  unset($node->page_count);
539

    
540
  // Make this page a member of the original page's organic group
541
  if (function_exists('og_set_group_context') && isset($node->og_groups)) {
542
    og_set_group_context($node->og_groups);
543
  }
544

    
545
  if ($cid === NULL) {
546
    // Adapted (simplified) version of node_view
547
    // Render the node content
548
    node_build_content($node, $view_mode);
549

    
550
    // Disable the links area
551
    unset($node->content['links']);
552
    // Disable fivestar widget output
553
    unset($node->content['fivestar_widget']);
554
    // Disable service links module output
555
    unset($node->content['service_links']);
556

    
557
    $build = $node->content;
558
    unset($node->content);
559
  }
560

    
561
  $print_comments = variable_get('print_comments', PRINT_COMMENTS_DEFAULT);
562

    
563
  if (function_exists('comment_node_page_additions') && (($cid != NULL) || ($print_comments))) {
564
    // Print only the requested comment (or if $cid is NULL, all of them)
565

    
566
    $comments = comment_node_page_additions($node);
567
    if (!empty($comments)) {
568
      unset($comments['comment_form']);
569
      foreach ($comments['comments'] as $key => &$comment) {
570
        if (is_numeric($key)) {
571
          if (($cid != NULL) && ($key != $cid)) {
572
            unset($comments['comments'][$key]);
573
          }
574
          else {
575
            unset($comment['links']);
576
          }
577
        }
578
      }
579

    
580
      $build['comments'] = $comments;
581
    }
582
  }
583

    
584
  $build += array(
585
    '#theme' => 'node',
586
    '#node' => $node,
587
    '#view_mode' => $view_mode,
588
    '#language' => $langcode,
589
    '#print_format' => $format,
590
  );
591

    
592
  $type = 'node';
593
  drupal_alter(array('node_view', 'entity_view'), $build, $type);
594

    
595
  $content = render($build);
596

    
597
  // Get rid of any links before the content
598
  $parts = explode('<div class="content', $content, 2);
599
  if (count($parts) == 2) {
600
    $pattern = '!(.*?)<a [^>]*?>(.*?)</a>(.*?)!mis';
601
    $parts[0] = preg_replace($pattern, '$1$2$3', $parts[0]);
602
    $content = implode('<div class="content', $parts);
603
  }
604

    
605
  // Check URL list settings
606
  $_print_urls = _print_url_list_enabled($node, $format);
607

    
608
  // Convert the a href elements
609
  $pattern = '!<(a\s[^>]*?)>(.*?)(</a>)!is';
610
  $content = preg_replace_callback($pattern, '_print_rewrite_urls', $content);
611

    
612
  $print = _print_var_generator($node, $query, $message, $cid);
613
  $print['content'] = $content;
614

    
615
  return $print;
616
}
617

    
618
/**
619
 * Prepare a Printer-friendly-ready node body for non-content pages
620
 *
621
 * @param $path
622
 *   path of the node to be rendered into a printer-friendly page
623
 * @param array $query
624
 *   (optional) array of key/value pairs as used in the url() function for the
625
 *   query
626
 * @param $format
627
 *   format of the page being generated
628
 * @param $teaser
629
 *   if set to TRUE, outputs only the node's teaser
630
 * @param $message
631
 *   optional sender's message (used by the send email module)
632
 * @return
633
 *   filled array ready to be used in the template
634
 */
635
function _print_generate_path($path, $query = NULL, $format = PRINT_HTML_FORMAT, $teaser = FALSE, $message = NULL) {
636
  global $_print_urls;
637

    
638
  // Handle node tabs
639
  $parts = explode('/', $path);
640
  if (ctype_digit($parts[0]) && (count($parts) > 1)) {
641
    $path = 'node/' . $path;
642
  }
643

    
644
  $path = drupal_get_normal_path($path);
645

    
646
  menu_set_active_item($path);
647
  // Adapted from index.php.
648
  $node = new stdClass();
649
  $node->body = menu_execute_active_handler($path, FALSE);
650
  if (is_array($node->body)) {
651
    $node->body = drupal_render($node->body);
652
  }
653

    
654
  if (is_int($node->body)) {
655
    switch ($node->body) {
656
      case MENU_NOT_FOUND:
657
        drupal_not_found();
658
        return FALSE;
659
        break;
660
      case MENU_ACCESS_DENIED:
661
        drupal_access_denied();
662
        return FALSE;
663
        break;
664
    }
665
  }
666

    
667
  $node->title = drupal_get_title();
668
  $node->path = $path;
669
  $node->changed = 0;
670

    
671
  // Delete any links area
672
  $node->body = preg_replace('!\s*<div class="links">.*?</div>!sim', '', $node->body);
673

    
674
  // Check URL list settings
675
  $_print_urls = _print_url_list_enabled($node, $format);
676

    
677
  // Convert the a href elements
678
  $pattern = '!<(a\s[^>]*?)>(.*?)(</a>)!is';
679
  $node->body = preg_replace_callback($pattern, '_print_rewrite_urls', $node->body);
680

    
681
  $print = _print_var_generator($node, $query, $message);
682
  $print['content'] = $node->body;
683

    
684
  return $print;
685
}
686

    
687

    
688
/**
689
 * Prepare a Printer-friendly-ready node body for book pages
690
 *
691
 * @param $nid
692
 *   node ID of the node to be rendered into a printer-friendly page
693
 * @param array $query
694
 *   (optional) array of key/value pairs as used in the url() function for the
695
 *   query
696
 * @param $format
697
 *   format of the page being generated
698
 * @param $teaser
699
 *   if set to TRUE, outputs only the node's teaser
700
 * @param $message
701
 *   optional sender's message (used by the send email module)
702
 * @return
703
 *   filled array ready to be used in the template
704
 */
705
function _print_generate_book($nid, $query = NULL, $format = PRINT_HTML_FORMAT, $teaser = FALSE, $message = NULL) {
706
  global $_print_urls;
707

    
708
  $node = node_load($nid);
709
  if (!$node) {
710
    // Node not found
711
    drupal_not_found();
712
    return FALSE;
713
  }
714
  elseif (!node_access('view', $node) || (!user_access('access printer-friendly version'))) {
715
    // Access is denied
716
    drupal_access_denied();
717
    return FALSE;
718
  }
719

    
720
  $tree = book_menu_subtree_data($node->book);
721
  $node->body = book_export_traverse($tree, 'book_node_export');
722

    
723
  // Check URL list settings
724
  $_print_urls = _print_url_list_enabled($node, $format);
725

    
726
  // Convert the a href elements
727
  $pattern = '!<(a\s[^>]*?)>(.*?)(</a>)!is';
728
  $node->body = preg_replace_callback($pattern, '_print_rewrite_urls', $node->body);
729

    
730
  $print = _print_var_generator($node, $query, $message);
731
  $print['content'] = $node->body;
732

    
733
  // The title is already displayed by the book_recurse, so avoid duplication
734
  $print['title'] = '';
735

    
736
  return $print;
737
}