print.pages.inc 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  1. <?php
  2. /**
  3. * @file
  4. * Contains the functions to generate Printer-friendly pages.
  5. *
  6. * This file is included by the core PF module, and includes all the
  7. * functions necessary to generate a PF version of the original page
  8. * in HTML format.
  9. *
  10. * @ingroup print
  11. */
  12. $_print_urls = PRINT_URLS_DEFAULT;
  13. /**
  14. * Generate an HTML version of the printer-friendly page
  15. *
  16. * @see print_controller()
  17. */
  18. function print_controller_html() {
  19. $args = func_get_args();
  20. $path = filter_xss(implode('/', $args));
  21. $cid = isset($_GET['comment']) ? (int)$_GET['comment'] : NULL;
  22. $link = print_print_link();
  23. $node = print_controller($path, $link['format'], $cid);
  24. if ($node) {
  25. // Handle the query
  26. $query = $_GET;
  27. unset($query['q']);
  28. $html = theme('print', array('node' => $node, 'query' => $query, 'format' => $link['format']));
  29. drupal_add_http_header('Content-Type', 'text/html; charset=utf-8');
  30. drupal_send_headers();
  31. print $html;
  32. $nodepath = (isset($node->nid)) ? 'node/' . $node->nid : drupal_get_normal_path($path);
  33. db_merge('print_page_counter')
  34. ->key(array('path' => $nodepath))
  35. ->fields(array(
  36. 'totalcount' => 1,
  37. 'timestamp' => REQUEST_TIME,
  38. ))
  39. ->expression('totalcount', 'totalcount + 1')
  40. ->execute();
  41. }
  42. }
  43. /**
  44. * Select the print generator function based on the page type
  45. *
  46. * Depending on the type of node, this functions chooses the appropriate
  47. * generator function.
  48. *
  49. * @param string $path
  50. * path of the original page
  51. * @param string $format
  52. * format of the page being generated
  53. * @param int $cid
  54. * comment ID of the individual comment to be rendered
  55. * @param string $view_mode
  56. * (optional) view mode to be used when rendering the content
  57. *
  58. * @return object
  59. * node-like object to be used in the print template
  60. *
  61. * @see _print_generate_node()
  62. * @see _print_generate_path()
  63. * @see _print_generate_book()
  64. * @see print_preprocess_print()
  65. */
  66. function print_controller($path, $format, $cid = NULL, $view_mode = PRINT_VIEW_MODE) {
  67. if (empty($path)) {
  68. // If no path was provided, let's try to generate a page for the referer
  69. global $base_url;
  70. $ref = $_SERVER['HTTP_REFERER'];
  71. $path = preg_replace("!^$base_url/!", '', $ref);
  72. if (($path === $ref) || empty($path)) {
  73. $path = variable_get('site_frontpage', 'node');
  74. }
  75. }
  76. if ($alias = drupal_lookup_path('source', $path)) {
  77. // Indirect call with print/alias
  78. // If there is a path alias with these arguments, generate a printer-friendly version for it
  79. $path = $alias;
  80. }
  81. $parts = explode('/', $path);
  82. if (($parts[0] == 'node') && (count($parts) > 1) && ctype_digit($parts[1])) {
  83. array_shift($parts);
  84. $path = implode('/', $parts);
  85. }
  86. $revision_view = preg_match('!^[\d]*/revisions/[\d]*/view$!', $path);
  87. if (ctype_digit($parts[0]) && ((count($parts) == 1) || $revision_view)) {
  88. $vid = $revision_view ? $parts[2] : NULL;
  89. $node = _print_generate_node($path, $format, $vid, $cid, $view_mode);
  90. }
  91. else {
  92. $ret = preg_match('!^book/export/html/(.*)!i', $path, $matches);
  93. if ($ret == 1) {
  94. // This is a book PF page link, handle trough the book handling functions
  95. $node = _print_generate_book($matches[1], $format);
  96. }
  97. else {
  98. // If no content node was found, handle the page printing with the 'printable' engine
  99. $node = _print_generate_path($path, $format);
  100. }
  101. }
  102. return $node;
  103. }
  104. /**
  105. * Implements hook_preprocess_HOOK().
  106. */
  107. function print_preprocess_print(&$variables) {
  108. global $language;
  109. $node = $variables['node'];
  110. $format = $variables['format'];
  111. $path = drupal_get_path_alias(empty($node->nid) ? $node->path : "node/$node->nid");
  112. static $hooks = NULL;
  113. if (!isset($hooks)) {
  114. drupal_theme_initialize();
  115. $hooks = theme_get_registry();
  116. }
  117. $variables['page']['#show_messages'] = FALSE;
  118. // Stolen from theme() so that ALL preprocess functions are called
  119. $hook = 'page';
  120. $info = $hooks[$hook];
  121. if (isset($info['preprocess functions']) || isset($info['process functions'])) {
  122. $variables['theme_hook_suggestions'] = array();
  123. foreach (array('preprocess functions', 'process functions') as $phase) {
  124. if (!empty($info[$phase])) {
  125. foreach ($info[$phase] as $processor_function) {
  126. if (function_exists($processor_function)) {
  127. // We don't want a poorly behaved process function changing $hook.
  128. $hook_clone = $hook;
  129. $processor_function($variables, $hook_clone);
  130. }
  131. }
  132. }
  133. }
  134. }
  135. $logo_url = FALSE;
  136. switch (variable_get('print_logo_options', PRINT_LOGO_OPTIONS_DEFAULT)) {
  137. case 1: // theme's
  138. $logo_url = theme_get_setting('logo');
  139. break;
  140. case 2: // user-specifed
  141. $logo_url = strip_tags(variable_get('print_logo_url', PRINT_LOGO_URL_DEFAULT));
  142. break;
  143. }
  144. $logo_url = preg_replace('!^' . base_path() . '!', '', $logo_url);
  145. $variables['print_logo'] = $logo_url ? theme('image', array('path' => $logo_url, 'alt' => variable_get('site_name', 'Drupal'), 'attributes' => array('class' => 'print-logo', 'id' => 'logo'))) : NULL;
  146. $variables['print_node'] = $node;
  147. $variables['content'] = $node->content;
  148. $variables['scripts'] = drupal_get_js();
  149. $variables['footer_scripts'] = drupal_get_js('footer');
  150. $variables['sourceurl_enabled'] = variable_get('print_sourceurl_enabled', PRINT_SOURCEURL_ENABLED_DEFAULT);
  151. $variables['url'] = url($path, array('absolute' => TRUE, 'query' => $variables['query']));
  152. $variables['source_url'] = url(variable_get('print_sourceurl_forcenode', PRINT_SOURCEURL_FORCENODE_DEFAULT) ? drupal_get_normal_path($path) : $path, array('alias' => TRUE, 'absolute' => TRUE, 'query' => $variables['query']));
  153. $variables['cid'] = isset($node->cid) ? $node->cid : NULL;
  154. $variables['print_title'] = check_plain($node->title);
  155. $variables['head'] = drupal_get_html_head();
  156. $variables['robots_meta'] = _print_robots_meta_generator();
  157. $variables['css'] = _print_css_generator($variables['expand_css']);
  158. if (variable_get('print_html_sendtoprinter', PRINT_HTML_SENDTOPRINTER_DEFAULT) && ($format == 'html')) {
  159. drupal_add_js('misc/drupal.js', array('weight' => JS_LIBRARY));
  160. $window_close = (variable_get('print_html_new_window', PRINT_HTML_NEW_WINDOW_DEFAULT) && variable_get('print_html_windowclose', PRINT_HTML_WINDOWCLOSE_DEFAULT)) ? 'window.close();' : '';
  161. $variables['sendtoprinter'] = '<script type="text/javascript">(function ($) { Drupal.behaviors.print = {attach: function(context) {$(window).load(function() {window.print();' . $window_close . '})}}})(jQuery);</script>';
  162. }
  163. $type = (isset($node->type)) ? $node->type : '';
  164. $nid = (isset($node->nid)) ? $node->nid : '';
  165. $variables['theme_hook_suggestions'] = array();
  166. $variables['theme_hook_suggestions'][] = "print__node__{$type}";
  167. $variables['theme_hook_suggestions'][] = "print__node__{$type}__{$nid}";
  168. $variables['theme_hook_suggestions'][] = "print__{$format}";
  169. $variables['theme_hook_suggestions'][] = "print__{$format}__node__{$type}";
  170. $variables['theme_hook_suggestions'][] = "print__{$format}__node__{$type}__{$nid}";
  171. }
  172. /**
  173. * Returns HTML for the published line of the print template.
  174. *
  175. * @param array $vars
  176. * An empty associative array
  177. *
  178. * @return string
  179. * HTML text with the published line
  180. *
  181. * @ingroup themeable
  182. * @ingroup print_themeable
  183. */
  184. function theme_print_published($vars) {
  185. global $base_url;
  186. $published_site = variable_get('site_name', 0);
  187. return $published_site ? t('Published on %site_name', array('%site_name' => $published_site)) . ' (' . l($base_url, $base_url) . ')' : '';
  188. }
  189. /**
  190. * Returns HTML for the breadcrumb line of the print template.
  191. *
  192. * @param array $vars
  193. * An associative array containing:
  194. * - $node: the node object
  195. *
  196. * @return string
  197. * HTML text with the breadcrumb
  198. *
  199. * @ingroup themeable
  200. * @ingroup print_themeable
  201. */
  202. function theme_print_breadcrumb($vars) {
  203. $node = $vars['node'];
  204. $old_path = $_GET['q'];
  205. $path = empty($node->nid) ? $node->path : "node/$node->nid";
  206. menu_set_active_item($path);
  207. $breadcrumb = drupal_get_breadcrumb();
  208. if (!empty($breadcrumb)) {
  209. $breadcrumb[] = menu_get_active_title();
  210. menu_set_active_item($old_path);
  211. return filter_xss(implode(' > ', $breadcrumb));
  212. }
  213. else {
  214. menu_set_active_item($old_path);
  215. return '';
  216. }
  217. }
  218. /**
  219. * Returns HTML for the footer of the print template.
  220. *
  221. * @param array $vars
  222. * An empty associative array
  223. *
  224. * @return string
  225. * HTML text with the footer
  226. *
  227. * @ingroup themeable
  228. * @ingroup print_themeable
  229. */
  230. function theme_print_footer($vars) {
  231. $footer = '';
  232. switch (variable_get('print_footer_options', PRINT_FOOTER_OPTIONS_DEFAULT)) {
  233. case 1: // theme's
  234. $footer_blocks = block_get_blocks_by_region('footer');
  235. $footer = variable_get('site_footer', FALSE) . "\n" . drupal_render($footer_blocks);
  236. break;
  237. case 2: // user-specifed
  238. $footer = variable_get('print_footer_user', PRINT_FOOTER_USER_DEFAULT);
  239. break;
  240. }
  241. // Delete the contextual links
  242. $footer = preg_replace('!\s*<div class="contextual-links-wrapper">.*?</div>!sim', '', $footer);
  243. return filter_xss_admin($footer);
  244. }
  245. /**
  246. * Returns HTML for the source URL line of the print template.
  247. *
  248. * @param array $vars
  249. * An associative array containing:
  250. * - $url: the URL to the full node view.
  251. * - $node: the node object.
  252. * - $cid; comment ID of the comment to display.
  253. *
  254. * @return string
  255. * HTML text with the footer
  256. *
  257. * @ingroup themeable
  258. * @ingroup print_themeable
  259. */
  260. function theme_print_sourceurl($vars) {
  261. $sourceurl_date = variable_get('print_sourceurl_date', PRINT_SOURCEURL_DATE_DEFAULT);
  262. $url = is_int($vars['cid']) ? $vars['url'] . '#comment-' . $vars['cid'] : $vars['url'];
  263. $output = '<strong>' . t('Source URL');
  264. if ($sourceurl_date && isset($vars['node'])) {
  265. $output .= ' (';
  266. $date = format_date($vars['node']->changed, 'short');
  267. $output .= empty($vars['node']->nid) ? t('retrieved on !date', array('!date' => $date)) : t('modified on !date', array('!date' => $date));
  268. $output .= ')';
  269. }
  270. $output .= ':</strong> ' . $url;
  271. return $output;
  272. }
  273. /**
  274. * Returns HTML for the URL list of the print template.
  275. *
  276. * @param array $vars
  277. * An empty associative array
  278. *
  279. * @return string
  280. * HTML text with the URL list
  281. *
  282. * @ingroup themeable
  283. * @ingroup print_themeable
  284. */
  285. function theme_print_url_list($vars) {
  286. global $_print_urls;
  287. // Display the collected links at the bottom of the page. Code once taken from Kjartan Mannes' project.module
  288. if (!empty($_print_urls)) {
  289. $urls = _print_friendly_urls();
  290. $max = count($urls);
  291. $url_list = '';
  292. foreach ($urls as $key => $url) {
  293. drupal_alter('print_url_list', $url);
  294. $url_list .= '[' . ($key + 1) . '] ' . check_plain($url) . "<br />\n";
  295. }
  296. if (!empty($url_list)) {
  297. return "<p><strong>" . t('Links') . "</strong><br />$url_list</p>";
  298. }
  299. else {
  300. return '';
  301. }
  302. }
  303. }
  304. /**
  305. * Generates a robots meta tag to tell them what they may index
  306. *
  307. * @return string
  308. * meta robots tag
  309. */
  310. function _print_robots_meta_generator() {
  311. $robots_meta = array();
  312. if (variable_get('print_robots_noindex', PRINT_ROBOTS_NOINDEX_DEFAULT)) {
  313. $robots_meta[] = 'noindex';
  314. }
  315. if (variable_get('print_robots_nofollow', PRINT_ROBOTS_NOFOLLOW_DEFAULT)) {
  316. $robots_meta[] = 'nofollow';
  317. }
  318. if (variable_get('print_robots_noarchive', PRINT_ROBOTS_NOARCHIVE_DEFAULT)) {
  319. $robots_meta[] = 'noarchive';
  320. }
  321. if (count($robots_meta) > 0) {
  322. return '<meta name="robots" content=' . implode(', ', $robots_meta) . ' />';
  323. }
  324. else {
  325. return '';
  326. }
  327. }
  328. /**
  329. * Generates the CSS directive to include in the printer-friendly version
  330. *
  331. * @param bool $expand
  332. * If TRUE, the provided CSS will be expanded, instead of given as a list
  333. * of includes.
  334. *
  335. * @return string
  336. * applicable CSS
  337. */
  338. function _print_css_generator($expand = FALSE) {
  339. $print_css = variable_get('print_css', PRINT_CSS_DEFAULT);
  340. if (!empty($print_css)) {
  341. drupal_add_css(strtr($print_css, array('%t' => drupal_get_path('theme', variable_get('theme_default')))));
  342. }
  343. else {
  344. drupal_add_css(drupal_get_path('module', 'print') . '/css/print.css');
  345. }
  346. $drupal_css = drupal_add_css();
  347. if (!variable_get('print_keep_theme_css', PRINT_KEEP_THEME_CSS_DEFAULT)) {
  348. foreach ($drupal_css as $key => $css_file) {
  349. if ($css_file['group'] == CSS_THEME) {
  350. // Unset the theme's CSS
  351. unset($drupal_css[$key]);
  352. }
  353. }
  354. }
  355. // Expand the CSS if requested
  356. if ($expand) {
  357. $style = '';
  358. $css_files = array_keys($drupal_css);
  359. foreach ($css_files as $filename) {
  360. if (file_exists($filename)) {
  361. $style .= file_get_contents($filename, TRUE);
  362. }
  363. }
  364. return "<style type='text/css' media='all'>$style</style>\n";
  365. }
  366. else {
  367. return drupal_get_css($drupal_css);
  368. }
  369. }
  370. /**
  371. * Callback function for the preg_replace_callback for URL-capable patterns
  372. *
  373. * Manipulate URLs to make them absolute in the URLs list, and add a [n]
  374. * footnote marker.
  375. *
  376. * @param array $matches
  377. * array with the matched tag patterns, usually <a...>+text+</a>
  378. *
  379. * @return string
  380. * tag with re-written URL and, if applicable, the [n] index to the URL list
  381. */
  382. function _print_rewrite_urls($matches) {
  383. global $base_url, $base_root, $_print_urls;
  384. $include_anchors = variable_get('print_urls_anchors', PRINT_URLS_ANCHORS_DEFAULT);
  385. // first, split the html into the different tag attributes
  386. $pattern = '!\s*(\w+\s*=\s*"(?:\\\"|[^"])*")\s*|\s*(\w+\s*=\s*\'(?:\\\\\'|[^\'])*\')\s*|\s*(\w+\s*=\s*\w+)\s*|\s+!';
  387. $attribs = preg_split($pattern, $matches[1], -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
  388. foreach ($attribs as $key => $value) {
  389. $attribs[$key] = preg_replace('!(\w)\s*=\s*(.*)!', '$1=$2', $value);
  390. }
  391. $size = count($attribs);
  392. for ($i=1; $i < $size; $i++) {
  393. // If the attribute is href or src, we may need to rewrite the URL in the value
  394. if (preg_match('!^(?:href|src)\s*?=(.*)!i', $attribs[$i], $urls) > 0) {
  395. $url = trim($urls[1], " \t\n\r\0\x0B\"'");
  396. if (empty($url)) {
  397. // If URL is empty, use current_url
  398. $path = explode('/', $_GET['q']);
  399. unset($path[0]);
  400. $path = implode('/', $path);
  401. if (ctype_digit($path)) {
  402. $path = "node/$path";
  403. }
  404. // Printer-friendly URLs is on, so we need to make it absolute
  405. $newurl = url($path, array('fragment' => drupal_substr($url, 1), 'absolute' => TRUE));
  406. }
  407. elseif (strpos(html_entity_decode($url), '://') || preg_match('!^mailto:.*?@.*?\..*?$!iu', html_entity_decode($url))) {
  408. // URL is absolute, do nothing
  409. $newurl = $url;
  410. }
  411. elseif (strpos(html_entity_decode($url), '//') === 0) {
  412. // URL is 'almost absolute', but it does not contain protocol; replace with base_path protocol
  413. $newurl = (empty($_SERVER['HTTPS']) ? 'http' : 'https') . ":" . $url;
  414. $matches[1] = str_replace($url, $newurl, $matches[1]);
  415. }
  416. else {
  417. if ($url[0] == '#') {
  418. // URL is an anchor tag
  419. if ($include_anchors && (!empty($_print_urls))) {
  420. $path = explode('/', $_GET['q']);
  421. unset($path[0]);
  422. $path = implode('/', $path);
  423. if (ctype_digit($path)) {
  424. $path = "node/$path";
  425. }
  426. // Printer-friendly URLs is on, so we need to make it absolute
  427. $newurl = url($path, array('fragment' => drupal_substr($url, 1), 'absolute' => TRUE));
  428. }
  429. // Because base href is the original page, change the link to
  430. // still be usable inside the print page
  431. $matches[1] = str_replace($url, check_plain(base_path() . $_GET['q'] . $url), $matches[1]);
  432. }
  433. else {
  434. // URL is relative, convert it into absolute URL
  435. if ($url[0] == '/') {
  436. // If it starts with '/' just append it to the server name
  437. $newurl = $base_root . '/' . trim($url, '/');
  438. }
  439. elseif (preg_match('!^(?:index.php)?\?q=!i', $url)) {
  440. // If it starts with ?q=, just prepend with the base URL
  441. $newurl = $base_url . '/' . trim($url, '/');
  442. }
  443. else {
  444. $newurl = url(trim($url, '/'), array('absolute' => TRUE));
  445. }
  446. $matches[1] = str_replace($url, $newurl, $matches[1]);
  447. }
  448. }
  449. }
  450. }
  451. $ret = '<' . $matches[1] . '>';
  452. if (count($matches) == 4) {
  453. $ret .= $matches[2] . $matches[3];
  454. if ((!empty($_print_urls)) && (isset($newurl))) {
  455. $ret .= ' <span class="print-footnote">[' . _print_friendly_urls(trim($newurl)) . ']</span>';
  456. }
  457. }
  458. return filter_xss_admin($ret);
  459. }
  460. /**
  461. * Auxiliary function to store the Printer-friendly URLs list as static.
  462. *
  463. * @param string $url
  464. * absolute URL to be inserted in the list
  465. *
  466. * @return array
  467. * list of URLs previously stored if $url is 0, or the current count
  468. * otherwise.
  469. */
  470. function _print_friendly_urls($url = 0) {
  471. static $urls = array();
  472. if ($url !== 0) {
  473. $url_idx = array_search($url, $urls);
  474. if ($url_idx !== FALSE) {
  475. return ($url_idx + 1);
  476. }
  477. else {
  478. $urls[] = $url;
  479. return count($urls);
  480. }
  481. }
  482. $ret = $urls;
  483. $urls = array();
  484. return $ret;
  485. }
  486. /**
  487. * Check URL list settings for this node
  488. *
  489. * @param object $node
  490. * node object
  491. * @param string $format
  492. * format of the page being generated
  493. *
  494. * @return bool
  495. * TRUE if URL list should be displayed, FALSE otherwise
  496. */
  497. function _print_url_list_enabled($node, $format) {
  498. if (!isset($node->type)) {
  499. $node_urllist = variable_get('print_' . $format . '_display_sys_urllist', PRINT_TYPE_SYS_URLLIST_DEFAULT);
  500. }
  501. else {
  502. $node_urllist = isset($node->{'print_' . $format . '_display_urllist'}) ? $node->{'print_' . $format . '_display_urllist'} : variable_get('print_' . $format . '_display_urllist_' . $node->type, PRINT_TYPE_URLLIST_DEFAULT);
  503. }
  504. // Get value of Printer-friendly URLs setting
  505. return (variable_get('print_urls', PRINT_URLS_DEFAULT) && ($node_urllist));
  506. }
  507. /**
  508. * Prepare a Printer-friendly-ready node body for content nodes
  509. *
  510. * @param int $nid
  511. * node ID of the node to be rendered into a printer-friendly page
  512. * @param string $format
  513. * format of the page being generated
  514. * @param int $vid
  515. * (optional) revision ID of the node to use
  516. * @param int $cid
  517. * (optional) comment ID of the individual comment to be rendered
  518. * @param string $view_mode
  519. * (optional) view mode to be used when rendering the content
  520. *
  521. * @return object
  522. * filled node-like object to be used in the print template
  523. */
  524. function _print_generate_node($nid, $format, $vid = NULL, $cid = NULL, $view_mode = PRINT_VIEW_MODE) {
  525. global $_print_urls;
  526. if (!isset($langcode)) {
  527. $langcode = $GLOBALS['language_content']->language;
  528. }
  529. // We can take a node id
  530. $node = node_load($nid, $vid);
  531. if (!$node) {
  532. // Node not found
  533. drupal_not_found();
  534. return FALSE;
  535. }
  536. elseif (!node_access('view', $node)) {
  537. // Access is denied
  538. drupal_access_denied();
  539. return FALSE;
  540. }
  541. drupal_set_title($node->title);
  542. if ($cid === NULL) {
  543. // Adapted (simplified) version of node_view
  544. // Render the node content
  545. node_build_content($node, $view_mode);
  546. // Disable the links area
  547. unset($node->content['links']);
  548. $build = $node->content;
  549. unset($node->content);
  550. }
  551. if (function_exists('comment_node_page_additions') &&
  552. (($cid != NULL) || (variable_get('print_comments', PRINT_COMMENTS_DEFAULT)))) {
  553. // Print only the requested comment (or if $cid is NULL, all of them)
  554. $comments = comment_node_page_additions($node);
  555. if (!empty($comments)) {
  556. unset($comments['comment_form']);
  557. foreach ($comments['comments'] as $key => &$comment) {
  558. if (is_numeric($key)) {
  559. if (($cid != NULL) && ($key != $cid)) {
  560. unset($comments['comments'][$key]);
  561. }
  562. else {
  563. unset($comment['links']);
  564. }
  565. }
  566. }
  567. $build['comments'] = $comments;
  568. }
  569. }
  570. $build += array(
  571. '#theme' => 'node',
  572. '#node' => $node,
  573. '#view_mode' => $view_mode,
  574. '#language' => $langcode,
  575. '#print_format' => $format,
  576. );
  577. $type = 'node';
  578. drupal_alter(array('node_view', 'entity_view'), $build, $type);
  579. $content = render($build);
  580. // Get rid of any links before the content
  581. $parts = explode('<div class="content', $content, 2);
  582. if (count($parts) == 2) {
  583. $pattern = '!(.*?)<a [^>]*?>(.*?)</a>(.*?)!mis';
  584. $parts[0] = preg_replace($pattern, '$1$2$3', $parts[0]);
  585. $content = implode('<div class="content', $parts);
  586. }
  587. // Check URL list settings
  588. $_print_urls = _print_url_list_enabled($node, $format);
  589. // Convert the a href elements
  590. $pattern = '!<(a\s[^>]*?)>(.*?)(</a>)!is';
  591. $content = preg_replace_callback($pattern, '_print_rewrite_urls', $content);
  592. $node->content = $content;
  593. return $node;
  594. }
  595. /**
  596. * Prepare a Printer-friendly-ready node body for non-content pages
  597. *
  598. * @param string $path
  599. * path of the node to be rendered into a printer-friendly page
  600. * @param string $format
  601. * format of the page being generated
  602. *
  603. * @return object
  604. * filled node-like object to be used in the print template
  605. */
  606. function _print_generate_path($path, $format) {
  607. global $_print_urls;
  608. // Handle node tabs
  609. $parts = explode('/', $path);
  610. if (ctype_digit($parts[0]) && (count($parts) > 1)) {
  611. $path = 'node/' . $path;
  612. }
  613. $path = drupal_get_normal_path($path);
  614. menu_set_active_item($path);
  615. // Adapted from index.php.
  616. $node = new stdClass();
  617. $node->content = menu_execute_active_handler($path, FALSE);
  618. if (is_array($node->content)) {
  619. $node->content = drupal_render($node->content);
  620. }
  621. if (is_int($node->content)) {
  622. switch ($node->content) {
  623. case MENU_NOT_FOUND:
  624. drupal_not_found();
  625. return FALSE;
  626. break;
  627. case MENU_ACCESS_DENIED:
  628. drupal_access_denied();
  629. return FALSE;
  630. break;
  631. }
  632. }
  633. $node->title = drupal_get_title();
  634. $node->path = $path;
  635. $node->changed = REQUEST_TIME;
  636. // Delete any links area
  637. $node->content = preg_replace('!\s*<div class="links">.*?</div>!sim', '', $node->content);
  638. // Delete the contextual links also
  639. $node->content = preg_replace('!\s*<div class="contextual-links-wrapper">.*?</div>!sim', '', $node->content);
  640. // Check URL list settings
  641. $_print_urls = _print_url_list_enabled($node, $format);
  642. // Convert the a href elements
  643. $pattern = '!<(a\s[^>]*?)>(.*?)(</a>)!is';
  644. $node->content = preg_replace_callback($pattern, '_print_rewrite_urls', $node->content);
  645. return $node;
  646. }
  647. /**
  648. * Prepare a Printer-friendly-ready node body for book pages
  649. *
  650. * @param int $nid
  651. * node ID of the node to be rendered into a printer-friendly page
  652. * @param string $format
  653. * format of the page being generated
  654. *
  655. * @return object
  656. * filled node-like object to be used in the print template
  657. */
  658. function _print_generate_book($nid, $format) {
  659. global $_print_urls;
  660. $node = node_load($nid);
  661. if (!$node) {
  662. // Node not found
  663. drupal_not_found();
  664. return FALSE;
  665. }
  666. elseif (!node_access('view', $node) || (!user_access('access printer-friendly version'))) {
  667. // Access is denied
  668. drupal_access_denied();
  669. return FALSE;
  670. }
  671. $tree = book_menu_subtree_data($node->book);
  672. $node->content = book_export_traverse($tree, 'book_node_export');
  673. // Check URL list settings
  674. $_print_urls = _print_url_list_enabled($node, $format);
  675. // Convert the a href elements
  676. $pattern = '!<(a\s[^>]*?)>(.*?)(</a>)!is';
  677. $node->content = preg_replace_callback($pattern, '_print_rewrite_urls', $node->content);
  678. return $node;
  679. }