print.pages.inc 24 KB

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