View source
<?php
$_print_urls = PRINT_URLS_DEFAULT;
function print_controller_html() {
$args = func_get_args();
$path = filter_xss(implode('/', $args));
$cid = isset($_GET['comment']) ? (int) $_GET['comment'] : NULL;
$link = print_print_link();
$node = print_controller($path, $link['format'], $cid);
if ($node) {
$query = $_GET;
unset($query['q']);
$html = theme('print', array(
'node' => $node,
'query' => $query,
'format' => $link['format'],
));
drupal_add_http_header('Content-Type', 'text/html; charset=utf-8');
drupal_send_headers();
print $html;
$nodepath = isset($node->nid) ? 'node/' . $node->nid : drupal_get_normal_path($path);
db_merge('print_page_counter')
->key(array(
'path' => substr($nodepath, 0, 255),
))
->fields(array(
'totalcount' => 1,
'timestamp' => REQUEST_TIME,
))
->expression('totalcount', 'totalcount + 1')
->execute();
}
else {
drupal_exit();
}
}
function print_controller($path, $format, $cid = NULL, $view_mode = PRINT_VIEW_MODE) {
if (empty($path)) {
global $base_url;
$ref = $_SERVER['HTTP_REFERER'];
$path = preg_replace("!^{$base_url}/!", '', $ref);
if ($path === $ref || empty($path)) {
$path = variable_get('site_frontpage', 'node');
}
}
if ($alias = drupal_lookup_path('source', $path)) {
$path = $alias;
}
$parts = explode('/', $path);
$node_router = variable_get('print_node_router', FALSE);
if ($parts[0] == 'node' && count($parts) > 1 && ctype_digit($parts[1]) && !$node_router) {
array_shift($parts);
$path = implode('/', $parts);
}
$revision_view = preg_match('!^[\\d]*/revisions/[\\d]*/view$!', $path);
if (ctype_digit($parts[0]) && (count($parts) == 1 || $revision_view) && !$node_router) {
$vid = $revision_view ? $parts[2] : NULL;
$node = _print_generate_node($path, $format, $vid, $cid, $view_mode);
}
else {
$ret = preg_match('!^book/export/html/(.*)!i', $path, $matches);
if ($ret == 1) {
$node = _print_generate_book($matches[1], $format);
}
else {
$node = _print_generate_path($path, $format);
}
}
return $node;
}
function print_preprocess_print(&$variables) {
$node = $variables['node'];
$format = $variables['format'];
$path = drupal_get_path_alias(empty($node->nid) ? $node->path : "node/{$node->nid}");
static $hooks = NULL;
if (!isset($hooks)) {
drupal_theme_initialize();
$hooks = theme_get_registry();
}
$variables['page']['#show_messages'] = FALSE;
$variables['theme_hook_suggestions'] = array();
$hook = 'page';
$info = $hooks[$hook];
if (isset($info['preprocess functions']) || isset($info['process functions'])) {
foreach (array(
'preprocess functions',
'process functions',
) as $phase) {
if (!empty($info[$phase])) {
foreach ($info[$phase] as $processor_function) {
if (function_exists($processor_function)) {
$hook_clone = $hook;
$processor_function($variables, $hook_clone);
}
}
}
}
}
$logo_url = FALSE;
switch (variable_get('print_logo_options', PRINT_LOGO_OPTIONS_DEFAULT)) {
case 1:
$logo_url = theme_get_setting('logo');
break;
case 2:
$logo_url = strip_tags(variable_get('print_logo_url', PRINT_LOGO_URL_DEFAULT));
break;
}
$logo_url = preg_replace('!^' . base_path() . '!', '', $logo_url);
$variables['print_logo'] = $logo_url ? theme('image', array(
'path' => $logo_url,
'alt' => variable_get('site_name', 'Drupal'),
'attributes' => array(
'class' => array(
'print-logo',
),
'id' => 'logo',
),
)) : NULL;
$variables['print_node'] = $node;
$variables['content'] = $node->content;
$variables['scripts'] = drupal_get_js();
$variables['footer_scripts'] = drupal_get_js('footer');
$variables['sourceurl_enabled'] = variable_get('print_sourceurl_enabled', PRINT_SOURCEURL_ENABLED_DEFAULT);
$variables['url'] = url($path, array(
'absolute' => TRUE,
'query' => $variables['query'],
));
$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'],
));
$variables['cid'] = isset($node->cid) ? $node->cid : NULL;
$variables['print_title'] = html_entity_decode(check_plain($node->title));
$variables['head'] = drupal_get_html_head();
$variables['robots_meta'] = _print_robots_meta_generator();
$variables['css'] = _print_css_generator($variables['expand_css']);
if (variable_get('print_html_sendtoprinter', PRINT_HTML_SENDTOPRINTER_DEFAULT) && $format == 'html') {
drupal_add_js('misc/drupal.js', array(
'group' => JS_LIBRARY,
));
$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);' : '';
$variables['sendtoprinter'] = '<script type="text/javascript">(function ($) { Drupal.behaviors.print = {attach: function() {$(window).load(function() {window.print();' . $window_close . '})}}})(jQuery);</script>';
}
$type = isset($node->type) ? $node->type : '';
$nid = isset($node->nid) ? $node->nid : '';
$variables['theme_hook_suggestions'][] = "print__node__{$type}";
$variables['theme_hook_suggestions'][] = "print__node__{$type}__{$nid}";
$variables['theme_hook_suggestions'][] = "print__{$format}";
$variables['theme_hook_suggestions'][] = "print__{$format}__node__{$type}";
$variables['theme_hook_suggestions'][] = "print__{$format}__node__{$type}__{$nid}";
}
function theme_print_published($vars) {
global $base_url;
$published_site = variable_get('site_name', 0);
return $published_site ? t('Published on %site_name', array(
'%site_name' => $published_site,
)) . ' (' . l($base_url, $base_url) . ')' : '';
}
function theme_print_breadcrumb($vars) {
$node = $vars['node'];
$old_path = $_GET['q'];
$path = empty($node->nid) ? $node->path : "node/{$node->nid}";
menu_set_active_item($path);
$breadcrumb = drupal_get_breadcrumb();
if (!empty($breadcrumb)) {
$breadcrumb[] = menu_get_active_title();
menu_set_active_item($old_path);
return filter_xss(implode(' > ', $breadcrumb));
}
else {
menu_set_active_item($old_path);
return '';
}
}
function theme_print_footer($vars) {
$footer = '';
switch (variable_get('print_footer_options', PRINT_FOOTER_OPTIONS_DEFAULT)) {
case 1:
$footer_blocks = block_get_blocks_by_region('footer');
$footer = variable_get('site_footer', FALSE) . "\n" . drupal_render($footer_blocks);
break;
case 2:
$footer = variable_get('print_footer_user', PRINT_FOOTER_USER_DEFAULT);
break;
}
$footer = preg_replace('!\\s*<div class="contextual-links-wrapper">.*?</div>!sim', '', $footer);
return filter_xss_admin($footer);
}
function theme_print_sourceurl($vars) {
global $base_url;
$sourceurl_date = variable_get('print_sourceurl_date', PRINT_SOURCEURL_DATE_DEFAULT);
$url = is_int($vars['cid']) ? $vars['url'] . '#comment-' . $vars['cid'] : $vars['url'];
$output = '<strong>' . t('Source URL');
if ($sourceurl_date && isset($vars['node'])) {
$output .= ' (';
$date = format_date($vars['node']->changed, 'short');
$output .= empty($vars['node']->nid) ? t('retrieved on !date', array(
'!date' => $date,
)) : t('modified on !date', array(
'!date' => $date,
));
$output .= ')';
}
$new_url = explode($base_url, $url);
$output .= ':</strong>' . "<a href=" . $new_url[1] . ">" . $url . '</a>';
return $output;
}
function theme_print_url_list($vars) {
global $_print_urls;
global $base_url;
if (!empty($_print_urls)) {
$urls = _print_friendly_urls();
$url_list = '';
foreach ($urls as $key => $url) {
drupal_alter('print_url_list', $url);
$new_url = explode($base_url, $url);
$url_list .= "<a href=" . $new_url[1] . " target='_blank'>" . '[' . ($key + 1) . '] ' . check_plain($url) . "</a>\n";
}
if (!empty($url_list)) {
return "<p><strong>" . t('Links') . "</strong><br />{$url_list}</p>";
}
}
return '';
}
function _print_robots_meta_generator() {
$robots_meta = array();
if (variable_get('print_robots_noindex', PRINT_ROBOTS_NOINDEX_DEFAULT)) {
$robots_meta[] = 'noindex';
}
if (variable_get('print_robots_nofollow', PRINT_ROBOTS_NOFOLLOW_DEFAULT)) {
$robots_meta[] = 'nofollow';
}
if (variable_get('print_robots_noarchive', PRINT_ROBOTS_NOARCHIVE_DEFAULT)) {
$robots_meta[] = 'noarchive';
}
if (count($robots_meta) > 0) {
return '<meta name="robots" content="' . implode(', ', $robots_meta) . '" />';
}
else {
return '';
}
}
function _print_css_generator($expand = FALSE) {
$print_css = variable_get('print_css', PRINT_CSS_DEFAULT);
if (!empty($print_css)) {
drupal_add_css(strtr($print_css, array(
'%t' => drupal_get_path('theme', variable_get('theme_default')),
)));
}
else {
drupal_add_css(drupal_get_path('module', 'print') . '/css/print.css');
}
$drupal_css = drupal_add_css();
if (!variable_get('print_keep_theme_css', PRINT_KEEP_THEME_CSS_DEFAULT)) {
foreach ($drupal_css as $key => $css_file) {
if ($css_file['group'] == CSS_THEME) {
unset($drupal_css[$key]);
}
}
}
if ($expand) {
$style = '';
$css_files = array_keys($drupal_css);
foreach ($css_files as $filename) {
if (file_exists($filename)) {
$style .= file_get_contents($filename, TRUE);
}
}
return "<style type='text/css' media='all'>{$style}</style>\n";
}
else {
return drupal_get_css($drupal_css);
}
}
function _print_rewrite_urls($matches) {
global $base_url, $base_root, $_print_urls;
$include_anchors = variable_get('print_urls_anchors', PRINT_URLS_ANCHORS_DEFAULT);
$pattern = '!\\s*(\\w+\\s*=\\s*"(?:\\\\"|[^"])*")\\s*|\\s*(\\w+\\s*=\\s*\'(?:\\\\\'|[^\'])*\')\\s*|\\s*(\\w+\\s*=\\s*\\w+)\\s*|\\s+!';
$attribs = preg_split($pattern, $matches[1], -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
foreach ($attribs as $key => $value) {
$attribs[$key] = preg_replace('!(\\w)\\s*=\\s*(.*)!', '$1=$2', $value);
}
$size = count($attribs);
for ($i = 1; $i < $size; $i++) {
if (preg_match('!^(?:href|src)\\s*?=(.*)!i', $attribs[$i], $urls) > 0) {
$url = trim($urls[1], " \t\n\r\0\v\"'");
if (empty($url)) {
$path = explode('/', $_GET['q']);
unset($path[0]);
$path = implode('/', $path);
if (ctype_digit($path)) {
$path = "node/{$path}";
}
$newurl = url($path, array(
'fragment' => drupal_substr($url, 1),
'absolute' => TRUE,
));
}
elseif (strpos(html_entity_decode($url), '://') || preg_match('!^mailto:.*?@.*?\\..*?$!iu', html_entity_decode($url))) {
$newurl = $url;
}
elseif (strpos(html_entity_decode($url), '//') === 0) {
$newurl = (empty($_SERVER['HTTPS']) ? 'http' : 'https') . ":" . $url;
$matches[1] = str_replace($url, $newurl, $matches[1]);
}
else {
if ($url[0] == '#') {
if ($include_anchors && !empty($_print_urls)) {
$path = explode('/', $_GET['q']);
unset($path[0]);
$path = implode('/', $path);
if (ctype_digit($path)) {
$path = "node/{$path}";
}
$newurl = url($path, array(
'fragment' => drupal_substr($url, 1),
'absolute' => TRUE,
));
}
$matches[1] = str_replace($url, check_plain(base_path() . $_GET['q'] . $url), $matches[1]);
}
else {
if ($url[0] == '/') {
$newurl = $base_root . '/' . trim($url, '/');
}
elseif (preg_match('!^(?:index.php)?\\?q=!i', $url)) {
$newurl = $base_url . '/' . trim($url, '/');
}
else {
$newurl = url(trim($url, '/'), array(
'absolute' => TRUE,
));
}
$matches[1] = str_replace($url, $newurl, $matches[1]);
}
}
}
}
$ret = '<' . $matches[1] . '>';
if (count($matches) == 4) {
$ret .= $matches[2] . $matches[3];
if (!empty($_print_urls) && isset($newurl)) {
$ret .= ' <span class="print-footnote">[' . _print_friendly_urls(trim($newurl)) . ']</span>';
}
}
return filter_xss_admin($ret);
}
function _print_friendly_urls($url = 0) {
$urls =& drupal_static(__FUNCTION__, array());
if ($url !== 0) {
$url_idx = array_search($url, $urls);
if ($url_idx !== FALSE) {
return $url_idx + 1;
}
else {
$urls[] = $url;
return count($urls);
}
}
else {
$ret = $urls;
$urls = array();
return $ret;
}
}
function _print_url_list_enabled($node, $format) {
if (!isset($node->type)) {
$node_urllist = variable_get('print_' . $format . '_display_sys_urllist', PRINT_TYPE_SYS_URLLIST_DEFAULT);
}
else {
$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);
}
return variable_get('print_urls', PRINT_URLS_DEFAULT) && $node_urllist;
}
function _print_generate_node($nid, $format, $vid = NULL, $cid = NULL, $view_mode = PRINT_VIEW_MODE) {
global $_print_urls;
if (!isset($langcode)) {
$langcode = $GLOBALS['language_content']->language;
}
$node = node_load($nid, $vid);
if (!$node) {
drupal_not_found();
drupal_exit();
}
elseif (!node_access('view', $node)) {
drupal_access_denied();
drupal_exit();
}
drupal_set_title($node->title);
$build = array();
if ($cid === NULL) {
node_build_content($node, $view_mode);
unset($node->content['links']);
$build = $node->content;
unset($node->content);
}
if (function_exists('comment_node_page_additions') && ($cid != NULL || variable_get('print_comments', PRINT_COMMENTS_DEFAULT))) {
$comments = comment_node_page_additions($node);
if (!empty($comments)) {
unset($comments['comment_form']);
foreach ($comments['comments'] as $key => &$comment) {
if (is_numeric($key)) {
if ($cid != NULL && $key != $cid) {
unset($comments['comments'][$key]);
}
else {
unset($comment['links']);
}
}
}
$build['comments'] = $comments;
}
}
$build += array(
'#theme' => 'node',
'#node' => $node,
'#view_mode' => $view_mode,
'#language' => $langcode,
'#print_format' => $format,
);
$type = 'node';
drupal_alter(array(
'node_view',
'entity_view',
), $build, $type);
$content = render($build);
$parts = explode('<div class="content', $content, 2);
if (count($parts) == 2) {
$pattern = '!(.*?)<a [^>]*?>(.*?)</a>(.*?)!mis';
$parts[0] = preg_replace($pattern, '$1$2$3', $parts[0]);
$content = implode('<div class="content', $parts);
}
$_print_urls = _print_url_list_enabled($node, $format);
$pattern = '!<(a\\s[^>]*?)>(.*?)(</a>)!is';
$content = preg_replace_callback($pattern, '_print_rewrite_urls', $content);
$node->content = $content;
return $node;
}
function _print_generate_path($path, $format) {
global $_print_urls;
$parts = explode('/', $path);
if (ctype_digit($parts[0]) && (count($parts) > 1 || variable_get('print_node_router', FALSE))) {
$path = 'node/' . $path;
}
$path = drupal_get_normal_path($path);
menu_set_active_item($path);
$node = new stdClass();
$node->content = menu_execute_active_handler($path, FALSE);
if (is_array($node->content)) {
$node->content = drupal_render($node->content);
}
if (is_int($node->content)) {
switch ($node->content) {
case MENU_NOT_FOUND:
drupal_not_found();
drupal_exit();
break;
case MENU_ACCESS_DENIED:
drupal_access_denied();
drupal_exit();
break;
}
}
$node->title = drupal_get_title();
$node->path = $path;
$node->changed = REQUEST_TIME;
$node->type = '';
$node->content = preg_replace('!\\s*<div class="links">.*?</div>!sim', '', $node->content);
$node->content = preg_replace('!\\s*<div class="contextual-links-wrapper">.*?</div>!sim', '', $node->content);
$_print_urls = _print_url_list_enabled($node, $format);
$pattern = '!<(a\\s[^>]*?)>(.*?)(</a>)!is';
$node->content = preg_replace_callback($pattern, '_print_rewrite_urls', $node->content);
return $node;
}
function _print_generate_book($nid, $format) {
global $_print_urls;
$node = node_load($nid);
if (!$node) {
drupal_not_found();
drupal_exit();
}
elseif (!node_access('view', $node) || !user_access('access printer-friendly version')) {
drupal_access_denied();
drupal_exit();
}
$tree = book_menu_subtree_data($node->book);
$node->content = book_export_traverse($tree, 'book_node_export');
$_print_urls = _print_url_list_enabled($node, $format);
$pattern = '!<(a\\s[^>]*?)>(.*?)(</a>)!is';
$node->content = preg_replace_callback($pattern, '_print_rewrite_urls', $node->content);
return $node;
}