'print', 'title' => t('Printer friendly'), 'callback' => 'print_controller', 'access' => user_access('access content'), 'type' => MENU_CALLBACK ); $items[] = array( 'path' => 'admin/settings/print', 'title' => t('Printer friendly'), 'description' => t('Adds a printer-friendly version link to node pages.'), 'callback' => 'drupal_get_form', 'callback arguments' => array('print_main_settings') ); } else { $nid = arg(1); if (is_numeric($nid)) { $items[] = array( 'path' => 'print/'. $nid, 'title' => t('Printer friendly'), 'callback' => 'print_node_controller', 'callback arguments' => array($nid), 'type' => MENU_CALLBACK ); } } return $items; } /******************************************************************** * Drupal Hooks :: Core ********************************************************************/ /** * Implementation of hook_link(). */ function print_link($type, $node = 0, $main) { $links = array(); if ($node->type == 'book' && function_exists('book_link')) { return; } if ($type == 'node' && variable_get('print_show_link', 1) && $main == 0) { $links['print'] = theme('print_link', $node); } return $links; } function print_main_settings() { $form['print_show_link'] = array( '#type' => 'radios', '#title' => t('Printer friendly page link'), '#default_value' => variable_get('print_show_link', 1), '#options' => array(t("Disabled"), t("Enabled")), '#description' => t("Enable or disable the printer friendly page link for each node. Even if the link is disabled, you can still view the print version of a node by going to print/nid where nid is the numeric id of the node."), ); $print_settings = variable_get('print_settings', NULL); $form['print_settings'] = array( '#type' => 'fieldset', '#title' => t('Print settings'), '#tree' => TRUE, ); $form['print_settings']['logo_url'] = array( '#type' => 'textfield', '#title' => t('Logo URL'), '#default_value' => !isset($print_settings['logo_url']) ? '' : $print_settings['logo_url'], '#size' => 60, '#maxlength' => 250, '#description' => t('An alternative logo to display on the printer friendly version'), ); $form['print_settings']['css'] = array( '#type' => 'textfield', '#title' => t('Stylesheet URL'), '#default_value' => !isset($print_settings['css']) ? 'misc/print.css' : $print_settings['css'], '#size' => 60, '#maxlength' => 64, '#description' => t('The URL to your print cascading stylesheet.'), ); $form['print_settings']['urls'] = array( '#type' => 'checkbox', '#title' => t('Printer friendly URLs'), '#return_value' => 1, '#default_value' => !isset($print_settings['urls']) ? 1 : $print_settings['urls'], ); $print_robot_settings = variable_get('print_robot_settings', NULL); $form['print_robot_settings'] = array( '#type' => 'fieldset', '#title' => t('Robots META tags'), '#tree' => TRUE, ); $form['print_robot_settings']['noindex'] = array( '#type' => 'checkbox', '#title' => t('Add noindex'), '#return_value' => 1, '#default_value' => empty($print_robot_settings['noindex']) ? 0 : 1, '#description' => t('Instruct robots to not index printer friendly pages') ); $form['print_robot_settings']['nofollow'] = array( '#type' => 'checkbox', '#title' => t('Add nofollow'), '#return_value' => 1, '#default_value' => empty($print_robot_settings['nofollow']) ? 0 : 1, '#description' => t('Instruct robots to not follow outgoing links on printer friendly pages') ); $form['print_robot_settings']['noarchive'] = array( '#type' => 'checkbox', '#title' => t('Add noarchive'), '#return_value' => 1, '#default_value' => empty($print_robot_settings['noarchive']) ? 0 : 1, '#description' => t('Non-standard tag to instruct search engines to not show a "Cached" link for your printer friendly pages. Recognized by Googlebot.') ); $form['print_robot_settings']['nocache'] = array( '#type' => 'checkbox', '#title' => t('Add nocache'), '#return_value' => 1, '#default_value' => empty($print_robot_settings['nocache']) ? 0 : 1, '#description' => t('Non-standard tag to instruct search engines to not show a "Cached" link for your printer friendly pages') ); return system_settings_form($form); } /******************************************************************** * Module Functions :: Controllers ********************************************************************/ function print_node_controller($nid) { print_generate_node($nid); } function print_controller($module) { $f = 'print_generate_'. $module; if (function_exists($f)) { $f(); } } /******************************************************************** * Module Functions ********************************************************************/ /** * Generates a meta tag to tell robots what they may index based on module settings * * @return string */ function _print_robots_meta_generator() { $robots_settings = variable_get('print_robot_settings', NULL); $robots_meta = array(); if(!empty($robots_settings['noindex'])) { $robots_meta[] = 'noindex'; } if(!empty($robots_settings['nofollow'])) { $robots_meta[] = 'nofollow'; } if(!empty($robots_settings['noarchive'])) { $robots_meta[] = 'noarchive'; } if(!empty($robots_settings['nocache'])) { $robots_meta[] = 'nocache'; } if(sizeof($robots_meta) > 0) { $robots_meta = isset($robots_meta[1]) ? implode(', ', $robots_meta) : $robots_meta[0]; $robots_meta = '\n"; } else { $robots_meta = ''; } return $robots_meta; } function print_convert_abspath($matches) { $path = $matches[2]; if (strpos($path, '://') || preg_match("/^mailto:.*?@.*?\..*?$/iu", $path) || ((substr($path, 0, 1) == "#")) ) { // URL is OK, do not touch } else { $path = url(trim($path, "/"), NULL, NULL, TRUE); } return $matches[1].$path.$matches[3]; } /** * Outputs a printer friendly page. */ function print_generate_node($title) { global $base_url; /* We can take a node id or a node title */ $node = (is_numeric($title)) ? node_load(array('nid' => $title)) : node_load(array('title' => $title)); if (!$node->title) return false; $teaser = false; $page = true; // To work with other node types, use drupal's render engine instead of the old node_view method $node->body = drupal_render(node_build_content($node)->content); // Relative links need to be changed to absolute paths since we the printer friendly page is not in the original URL // First the href links $pattern ="@(]*?href=[\']?[\"]?)([^\"|^\'|^|^>]*)(.+?)@is"; $node->body = preg_replace_callback($pattern, "print_convert_abspath", $node->body); // Now the src links $pattern ="@(<[^>]*?src=[\']?[\"]?)([^\"|^\'|^|^>]*)(.+?>)@is"; $node->body = preg_replace_callback($pattern, "print_convert_abspath", $node->body); // associative array settings $print_settings = variable_get('print_settings', NULL); if (!isset($print_settings['urls']) || !empty($print_settings['urls'])) { /* Collect links and display them at the bottom of the page. Code once taken from Kjartan Mannes' project.module */ $pattern = "@]*?href=([\']?[\"]?)([^\"|^\'|^|^>]*)([^>]*)>(.+?)@ise"; $node->body = preg_replace($pattern, "''.stripslashes('\\4').' ['. print_friendly_urls(trim(stripslashes('\\2')), $node->nid) .']'", $node->body); $urls = print_friendly_urls(); if (count($urls)) { $node->pfp_links = ''; $max = count($urls); for ($i = 0; $i < $max; $i++) { $node->pfp_links .= '['. ($i + 1) .'] '. $urls[$i] ."
\n"; } } } init_theme(); $node->logo = !empty($print_settings['logo_url']) ? $print_settings['logo_url'] : theme_get_setting('logo'); /* Grab and format the src URL */ $node->source_url = url("node/$node->nid", NULL, NULL, TRUE); $node->language = $GLOBALS['locale']; $node->printcss = empty($print_settings['css']) ? 'misc/print.css' : $print_settings['css']; $robots_meta = _print_robots_meta_generator(); include_once('print.node.tpl.php'); } function print_friendly_urls($url = 0, $nid) { static $urls = array(); if ($url) { if(strpos($url, '://') || preg_match("/^mailto:.*?@.*?\..*?$/iu", $url)) { $urls[] = $url; } else { // Convert into absolute URL, removing extra beginning and end '/' if (substr($url, 0, 1) == "#") { // Handle tags $urls[] = url("node/" . $nid, NULL, substr($url,1), TRUE); } else { $urls[] = url(trim($url,"/"), NULL, NULL, TRUE); } } return count($urls); } return $urls; } /******************************************************************** * Module Functions :: Themeable Functions ********************************************************************/ function theme_print_link($node) { return array('title' => t('Printer friendly version'), 'href' => "print/$node->nid", 'attributes' => array('title' => t('Display a printer friendly version of this page.')), ); }