View source
<?php
$_print_urls = PRINT_URLS_DEFAULT;
function print_controller_html() {
$args = func_get_args();
$path = implode('/', $args);
$cid = isset($_GET['comment']) ? (int) $_GET['comment'] : NULL;
$print = print_controller($path, $cid, PRINT_HTML_FORMAT);
if ($print !== FALSE) {
$node = $print['node'];
ob_start();
include_once _print_get_template(PRINT_HTML_FORMAT, $print['type']);
$html = ob_get_contents();
ob_end_clean();
$html = drupal_final_markup($html);
print $html;
$nodepath = isset($node->path) ? drupal_get_normal_path($node->path) : $path;
db_query("UPDATE {print_page_counter} SET totalcount = totalcount + 1, timestamp = %d WHERE path = '%s'", time(), $nodepath);
if (!db_affected_rows()) {
db_query("INSERT INTO {print_page_counter} (path, totalcount, timestamp) VALUES ('%s', 1, %d)", $nodepath, time());
}
}
}
function print_controller($path, $cid = NULL, $format = PRINT_HTML_FORMAT, $teaser = FALSE, $message = NULL) {
if (empty($path)) {
global $base_url;
$ref = referer_uri();
$path = preg_replace("!^{$base_url}/!", '', $ref);
if ($path === $ref) {
$path = '';
}
}
if (!is_numeric($path)) {
$path = drupal_get_normal_path($path);
$ret = preg_match('!^node/(.*)!i', $path, $matches);
if ($ret == 1) {
$path = $matches[1];
}
}
$parts = explode('/', $path);
if (is_numeric($parts[0]) && count($parts) == 1) {
$print = _print_generate_node($path, $cid, $format, $teaser, $message);
}
else {
$ret = preg_match('!^book/export/html/(.*)!i', $path, $matches);
if ($ret == 1) {
$print = _print_generate_book($matches[1], $format, $teaser, $message);
}
else {
$print = _print_generate_path($path, $format, $teaser, $message);
}
}
return $print;
}
function _print_robots_meta_generator() {
$print_robots_noindex = variable_get('print_robots_noindex', PRINT_ROBOTS_NOINDEX_DEFAULT);
$print_robots_nofollow = variable_get('print_robots_nofollow', PRINT_ROBOTS_NOFOLLOW_DEFAULT);
$print_robots_noarchive = variable_get('print_robots_noarchive', PRINT_ROBOTS_NOARCHIVE_DEFAULT);
$robots_meta = array();
if (!empty($print_robots_noindex)) {
$robots_meta[] = 'noindex';
}
if (!empty($print_robots_nofollow)) {
$robots_meta[] = 'nofollow';
}
if (!empty($print_robots_noarchive)) {
$robots_meta[] = 'noarchive';
}
if (count($robots_meta) > 0) {
$robots_meta = implode(', ', $robots_meta);
$robots_meta = "<meta name='robots' content='{$robots_meta}' />\n";
}
else {
$robots_meta = '';
}
return $robots_meta;
}
function _print_var_generator($node, $message = NULL, $cid = NULL) {
global $base_url, $_print_urls;
$path = empty($node->nid) ? $node->path : "node/{$node->nid}";
init_theme();
$print_css = variable_get('print_css', PRINT_CSS_DEFAULT);
$print_logo_options = variable_get('print_logo_options', PRINT_LOGO_OPTIONS_DEFAULT);
$print_logo_url = variable_get('print_logo_url', PRINT_LOGO_URL_DEFAULT);
$print_html_new_window = variable_get('print_html_new_window', PRINT_HTML_NEW_WINDOW_DEFAULT);
$print_html_sendtoprinter = variable_get('print_html_sendtoprinter', PRINT_HTML_SENDTOPRINTER_DEFAULT);
$print_html_windowclose = variable_get('print_html_windowclose', PRINT_HTML_WINDOWCLOSE_DEFAULT);
$print_sourceurl_enabled = variable_get('print_sourceurl_enabled', PRINT_SOURCEURL_ENABLED_DEFAULT);
$print_sourceurl_forcenode = variable_get('print_sourceurl_forcenode', PRINT_SOURCEURL_FORCENODE_DEFAULT);
$print_sourceurl_date = variable_get('print_sourceurl_date', PRINT_SOURCEURL_DATE_DEFAULT);
$print_footer_options = variable_get('print_footer_options', PRINT_FOOTER_OPTIONS_DEFAULT);
$print_footer_user = variable_get('print_footer_user', PRINT_FOOTER_USER_DEFAULT);
$print['language'] = $GLOBALS['locale'];
$print['title'] = check_plain($node->title);
$print['head'] = drupal_get_html_head();
if ($print_html_sendtoprinter) {
drupal_add_js('misc/drupal.js', 'core');
}
$print['scripts'] = drupal_get_js();
$print['robots_meta'] = _print_robots_meta_generator();
$print['url'] = url($path, NULL, NULL, TRUE);
$print['base_href'] = "<base href='" . $print['url'] . "' />\n";
$print['favicon'] = theme_get_setting('toggle_favicon') ? "<link rel='shortcut icon' href='" . theme_get_setting('favicon') . "' type='image/x-icon' />\n" : '';
if (!empty($print_css)) {
drupal_add_css(strtr($print_css, array(
'%t' => path_to_theme(),
)));
}
else {
drupal_add_css(drupal_get_path('module', 'print') . '/css/print.css');
}
$drupal_css = drupal_add_css();
foreach ($drupal_css as $key => $types) {
$drupal_css[$key]['theme'] = array();
}
if (!empty($message)) {
$style = '';
$css_files = array();
foreach ($drupal_css as $types) {
foreach ($types as $values) {
$css_files = array_merge($css_files, array_keys($values));
}
}
foreach ($css_files as $filename) {
$res = file_exists($filename) ? file_get_contents($filename, TRUE) : FALSE;
if ($res != FALSE) {
$style .= $res;
}
}
$print['css'] = "<style type='text/css' media='all'>{$style}</style>\n";
}
else {
$print['css'] = drupal_get_css($drupal_css);
}
$window_close = $print_html_new_window && $print_html_windowclose ? 'window.close();' : '';
$print['sendtoprinter'] = $print_html_sendtoprinter ? "<script type='text/javascript'>\$(document).ready(function() {window.print();{$window_close}});</script>" : '';
switch ($print_logo_options) {
case 0:
$logo_url = 0;
break;
case 1:
$logo_url = theme_get_setting('logo');
break;
case 2:
$logo_url = strip_tags($print_logo_url);
break;
}
$site_name = variable_get('site_name', 'Drupal');
$print['logo'] = $logo_url ? "<img class='print-logo' src='" . check_url($logo_url) . "' alt='{$site_name}' id='logo' />\n" : '';
switch ($print_footer_options) {
case 0:
$footer = '';
break;
case 1:
$footer = variable_get('site_footer', FALSE) . "\n" . theme('blocks', 'footer');
break;
case 2:
$footer = $print_footer_user;
break;
}
$print['footer_message'] = filter_xss_admin($footer);
$published_site = variable_get('site_name', 0);
if ($published_site) {
$print_text_published = filter_xss(variable_get('print_text_published', t('Published on %site_name')));
$published = t($print_text_published, array(
'%site_name' => $published_site,
));
$print['site_name'] = $published . ' (' . l($base_url, $base_url) . ')';
}
else {
$print['site_name'] = '';
}
if ($print_sourceurl_enabled == 1) {
if (empty($print_sourceurl_forcenode)) {
$url = $print['url'];
}
else {
$url = $base_url . '/' . ((bool) variable_get('clean_url', '0') ? '' : '?q=') . $path;
}
if (is_int($cid)) {
$url .= '#comment-$cid';
}
$retrieved_date = format_date(time(), 'small');
$print_text_retrieved = filter_xss(variable_get('print_text_retrieved', t('retrieved on %date')));
$retrieved = t($print_text_retrieved, array(
'%date' => $retrieved_date,
));
$print['printdate'] = $print_sourceurl_date ? " ({$retrieved})" : '';
$source_url = filter_xss(variable_get('print_text_source_url', t('Source URL')));
$print['source_url'] = '<strong>' . $source_url . $print['printdate'] . ':</strong> ' . l($url, $url);
}
else {
$print['source_url'] = '';
}
if (isset($node->type)) {
$node_type = $node->type;
if (theme_get_setting("toggle_node_info_{$node_type}")) {
$print_text_by = filter_xss(variable_get('print_text_by', t('By %author')));
$by_author = $node->name ? $node->name : variable_get('anonymous', t('Anonymous'));
$print['submitted'] = t($print_text_by, array(
'%author' => $by_author,
));
$print_text_created = filter_xss(variable_get('print_text_created', t('Created %date')));
$created_datetime = format_date($node->created, 'small');
$print['created'] = t($print_text_created, array(
'%date' => $created_datetime,
));
}
else {
$print['submitted'] = '';
$print['created'] = '';
}
$print['type'] = $node->type;
}
else {
$print['submitted'] = '';
$print['created'] = '';
$print['type'] = '';
}
menu_set_active_item($path);
$breadcrumb = drupal_get_breadcrumb();
if (!empty($breadcrumb)) {
$breadcrumb[] = menu_get_active_title();
$print['breadcrumb'] = filter_xss(implode(' > ', $breadcrumb));
}
else {
$print['breadcrumb'] = '';
}
$print['pfp_links'] = '';
if (!empty($_print_urls)) {
$urls = _print_friendly_urls();
$max = count($urls);
$pfp_links = '';
if ($max) {
for ($i = 0; $i < $max; $i++) {
$pfp_links .= '[' . ($i + 1) . '] ' . check_plain($urls[$i]) . "<br />\n";
}
$links = filter_xss(variable_get('print_text_links', t('Links')));
$print['pfp_links'] = "<p><strong>{$links}:</strong><br />{$pfp_links}</p>";
}
}
if (module_exists('taxonomy')) {
$terms = taxonomy_link('taxonomy terms', $node);
$print['taxonomy'] = theme('links', $terms);
}
$print['node'] = $node;
$print['message'] = $message;
return $print;
}
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 (strpos(html_entity_decode($url), '://') || preg_match('!^mailto:.*?@.*?\\..*?$!iu', html_entity_decode($url))) {
$newurl = $url;
}
else {
if ($url[0] == '#') {
if ($include_anchors && !empty($_print_urls)) {
$path = explode('/', $_GET['q']);
unset($path[0]);
$path = implode('/', $path);
if (is_numeric($path)) {
$path = "node/{$path}";
}
$newurl = url($path, NULL, substr($url, 1), TRUE);
}
$matches[1] = str_replace($url, 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, '/'), NULL, NULL, 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 $ret;
}
function _print_friendly_urls($url = 0) {
static $urls = array();
if ($url) {
$url_idx = array_search($url, $urls);
if ($url_idx !== FALSE) {
return $url_idx + 1;
}
else {
$urls[] = $url;
return count($urls);
}
}
$ret = $urls;
$urls = array();
return $ret;
}
function _print_get_template($format = NULL, $type = NULL) {
$filenames = array();
if (!empty($format) && !empty($type)) {
$filenames[] = "print_{$format}.node-{$type}.tpl.php";
}
if (!empty($format)) {
$filenames[] = "print_{$format}.tpl.php";
}
if (!empty($type)) {
$filenames[] = "print.node-{$type}.tpl.php";
$filenames[] = "print.{$type}.tpl.php";
}
$filenames[] = 'print.tpl.php';
foreach ($filenames as $value) {
$file = drupal_get_path('theme', $GLOBALS['theme_key']) . '/' . $value;
if (file_exists($file)) {
return $file;
}
$file = drupal_get_path('module', 'print') . '/' . $value;
if (file_exists($file)) {
return $file;
}
}
}
function _print_url_list_enabled($node, $format = PRINT_HTML_FORMAT) {
if (!isset($node->type)) {
return TRUE;
}
switch ($format) {
case PRINT_HTML_FORMAT:
$node_urllist = isset($node->print_display_urllist) ? $node->print_display_urllist : variable_get('print_display_urllist_' . $node->type, PRINT_TYPE_URLLIST_DEFAULT);
$fmt = '';
break;
case PRINT_MAIL_FORMAT:
$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);
$fmt = $format . '_';
break;
case PRINT_PDF_FORMAT:
$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);
$fmt = $format . '_';
break;
}
if (!isset($node_urllist)) {
$node_urllist = PRINT_TYPE_URLLIST_DEFAULT;
}
return variable_get('print_urls', PRINT_URLS_DEFAULT) && $node_urllist;
}
function _print_generate_node($nid, $cid = NULL, $format = PRINT_HTML_FORMAT, $teaser = FALSE, $message = NULL) {
global $_print_urls;
$node = node_load($nid);
if (!$node) {
drupal_not_found();
return FALSE;
}
elseif (!node_access('view', $node)) {
drupal_access_denied();
return FALSE;
}
drupal_set_title(check_plain($node->title));
$node->printing = TRUE;
unset($node->pages);
unset($node->pages_count);
unset($node->page_count);
if (function_exists('og_set_group_context') && isset($node->og_groups)) {
og_set_group_context($node->og_groups);
}
if ($cid === NULL) {
$node = node_build_content($node, $teaser, TRUE);
unset($node->content['fivestar_widget']);
unset($node->content['service_links']);
$content = drupal_render($node->content);
$content = preg_replace('!<div class=[\'"]adsense[\'"].*?</div>!sim', '', $content);
if ($teaser) {
$node->teaser = $content;
unset($node->body);
}
else {
$node->body = $content;
unset($node->teaser);
}
}
$print_comments = variable_get('print_comments', PRINT_COMMENTS_DEFAULT);
if (function_exists('comment_render') && ($cid != NULL || $print_comments)) {
$comments = comment_render($node, $cid);
$comments = preg_replace('!<form.*?id="comment-.*?">.*?</form>!sim', '', $comments);
$comments = preg_replace('!<h2.*?>' . t('Post new comment') . '</h2>!', '', $comments);
$comments = preg_replace('!(<h3.*?>.*?)<a.*?>(.*?)</a>(.*?</h3>)!i', '$1$2$3', $comments);
$pattern = '!(<(?:span|div) class="submitted">.*?)<a.*?>(.*?)</a>(.*?</(?:span|div)>)!sim';
if (preg_match($pattern, $comments)) {
$comments = preg_replace($pattern, '$1$2$3', $comments);
}
$comments = preg_replace('!\\s*<ul class="links">.*?</ul>!sim', '', $comments);
if ($cid != NULL) {
unset($node->body);
}
$node->body .= $comments;
}
node_invoke_nodeapi($node, 'alter', $teaser, TRUE);
if ($teaser) {
$node->body = $node->teaser;
}
$_print_urls = _print_url_list_enabled($node, $format);
$pattern = '!<(a\\s[^>]*?)>(.*?)(</a>)!is';
$node->body = preg_replace_callback($pattern, '_print_rewrite_urls', $node->body);
$print = _print_var_generator($node, $message, $cid);
$print['content'] = $node->body;
return $print;
}
function _print_generate_path($path, $format = PRINT_HTML_FORMAT, $teaser = FALSE, $message = NULL) {
global $_print_urls;
$parts = explode('/', $path);
if (is_numeric($parts[0]) && count($parts) > 1) {
$path = 'node/' . $path;
}
$path = drupal_get_normal_path($path);
$_GET['q'] = $path;
_menu_append_contextual_items();
menu_set_active_item($path);
$node = new stdClass();
$node->body = menu_execute_active_handler();
if (preg_match('/404 Not Found/', drupal_get_headers()) == 1) {
return FALSE;
}
if (is_int($node->body)) {
switch ($node->body) {
case MENU_NOT_FOUND:
drupal_not_found();
return FALSE;
break;
case MENU_ACCESS_DENIED:
drupal_access_denied();
return FALSE;
break;
}
}
$node->title = drupal_get_title();
$node->path = $path;
$node->body = preg_replace('!\\s*<div class="links">.*?</div>!sim', '', $node->body);
$_print_urls = _print_url_list_enabled($node, $format);
$pattern = '!<(a\\s[^>]*?)>(.*?)(</a>)!is';
$node->body = preg_replace_callback($pattern, '_print_rewrite_urls', $node->body);
$print = _print_var_generator($node, $message);
$print['content'] = $node->body;
return $print;
}
function _print_generate_book($nid, $format = PRINT_HTML_FORMAT, $teaser = FALSE, $message = NULL) {
global $_print_urls;
$node = node_load($nid);
if (!$node) {
drupal_not_found();
return FALSE;
}
elseif (!node_access('view', $node) || !user_access('see printer-friendly version')) {
drupal_access_denied();
return FALSE;
}
$depth = count(book_location($node)) + 1;
$node->body = book_recurse($nid, $depth, '_print_node_visitor_html_pre', 'book_node_visitor_html_post');
$_print_urls = _print_url_list_enabled($node, $format);
$pattern = '!<(a\\s[^>]*?)>(.*?)(</a>)!is';
$node->body = preg_replace_callback($pattern, '_print_rewrite_urls', $node->body);
$print = _print_var_generator($node, $message);
$print['content'] = $node->body;
$print['title'] = '';
return $print;
}
function _print_node_visitor_html_pre($node, $depth, $nid) {
$node = node_build_content($node, FALSE, TRUE);
unset($node->content['book_navigation']);
$output .= "<div id='node-{$node->nid}' class='section-{$depth}'>\n";
$output .= "<h1 class='book-heading'>" . check_plain($node->title) . "</h1>\n";
$output .= drupal_render($node->content);
return $output;
}