| Commit | Line | Data |
|---|---|---|
| b09f08f7 | 1 | <?php |
| 1e156b5a | 2 | // $Id$ |
| b09f08f7 | 3 | |
| 1e156b5a JV |
4 | /** |
| 5 | * @file | |
| 6 | * Contains the functions to generate Printer-friendly pages. | |
| 7 | * | |
| 8 | * This file is included by the core PF module, and includes all the | |
| 9 | * functions necessary to generate a PF version of the original page | |
| 10 | * in HTML format. | |
| 11 | */ | |
| b09f08f7 | 12 | |
| 855b243a JV |
13 | $_print_urls = PRINT_URLS_DEFAULT; |
| 14 | ||
| 1e156b5a JV |
15 | /** |
| 16 | * Generate an HTML version of the printer-friendly page | |
| 17 | * | |
| 18 | * @see print_controller() | |
| 19 | * @see _print_get_template() | |
| 20 | */ | |
| b09f08f7 | 21 | function print_controller_html() { |
| 5b1b9562 | 22 | $args = func_get_args(); |
| 0706a465 | 23 | // Remove the print/ prefix |
| 5b1b9562 | 24 | $path = implode('/', $args); |
| 20ab24e6 | 25 | $cid = isset($_GET['comment']) ? (int)$_GET['comment'] : NULL; |
| 0706a465 | 26 | |
| 855b243a | 27 | $print = print_controller($path, $cid, PRINT_HTML_FORMAT); |
| 5c9469df JV |
28 | if ($print !== FALSE) { |
| 29 | $node = $print['node']; | |
| 855b243a JV |
30 | include_once(DRUPAL_ROOT .'/'. _print_get_template(PRINT_HTML_FORMAT, $print['type'])); |
| 31 | ||
| 32 | $nodepath = drupal_get_normal_path($node->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 + :inc', array(':inc' => 1)) | |
| 40 | ->execute(); | |
| 5c9469df | 41 | } |
| b09f08f7 JV |
42 | } |
| 43 | ||
| 44 | /** | |
| 0706a465 | 45 | * Select the print generator function based on the page type |
| 1e156b5a JV |
46 | * |
| 47 | * Depending on the type of node, this functions chooses the appropriate | |
| 48 | * generator function. | |
| 49 | * | |
| 0706a465 JV |
50 | * @param $path |
| 51 | * path of the original page | |
| 52 | * @param $cid | |
| 53 | * comment ID of the individual comment to be rendered | |
| 855b243a JV |
54 | * @param $format |
| 55 | * format of the page being generated | |
| 0706a465 JV |
56 | * @param $teaser |
| 57 | * if set to TRUE, outputs only the node's teaser | |
| fb70cd39 JV |
58 | * @param $message |
| 59 | * optional sender's message (used by the send e-mail module) | |
| 1e156b5a JV |
60 | * @return |
| 61 | * array with the fields to be used in the template | |
| 62 | * @see _print_generate_node() | |
| 63 | * @see _print_generate_path() | |
| 64 | * @see _print_generate_book() | |
| b09f08f7 | 65 | */ |
| 855b243a | 66 | function print_controller($path, $cid = NULL, $format = PRINT_HTML_FORMAT, $teaser = FALSE, $message = NULL) { |
| 4e52b093 JV |
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 | ||
| 71 | $ref = $_SERVER['HTTP_REFERER']; | |
| 72 | $path = preg_replace("!^$base_url/!", '', $ref); | |
| 73 | if ($path === $ref) { | |
| 74 | $path = ''; | |
| 75 | } | |
| 76 | } | |
| 0706a465 | 77 | if (!is_numeric($path)) { |
| b09f08f7 JV |
78 | // Indirect call with print/alias |
| 79 | // If there is a path alias with these arguments, generate a printer-friendly version for it | |
| 0706a465 | 80 | $path = drupal_get_normal_path($path); |
| 7d62e7c4 | 81 | $ret = preg_match('!^node/(.*)!i', $path, $matches); |
| b09f08f7 | 82 | if ($ret == 1) { |
| 0706a465 | 83 | $path = $matches[1]; |
| b09f08f7 JV |
84 | } |
| 85 | } | |
| 7d62e7c4 | 86 | $parts = explode('/', $path); |
| 9490b60e | 87 | if (is_numeric($parts[0])) { |
| 855b243a | 88 | $print = _print_generate_node($path, $cid, $format, $teaser, $message); |
| b09f08f7 JV |
89 | } |
| 90 | else { | |
| 7d62e7c4 | 91 | $ret = preg_match('!^book/export/html/(.*)!i', $path, $matches); |
| b09f08f7 JV |
92 | if ($ret == 1) { |
| 93 | // This is a book PF page link, handle trough the book handling functions | |
| 855b243a | 94 | $print = _print_generate_book($matches[1], $format, $teaser, $message); |
| b09f08f7 JV |
95 | } |
| 96 | else { | |
| 97 | // If no content node was found, handle the page printing with the 'printable' engine | |
| 855b243a | 98 | $print = _print_generate_path($path, $format, $teaser, $message); |
| b09f08f7 JV |
99 | } |
| 100 | } | |
| 101 | ||
| 102 | return $print; | |
| 103 | } | |
| 104 | ||
| b09f08f7 | 105 | /** |
| 1e156b5a | 106 | * Generates a robots meta tag to tell them what they may index |
| b09f08f7 | 107 | * |
| 1e156b5a | 108 | * @return |
| 3483245d | 109 | * string with the meta robots tag |
| b09f08f7 JV |
110 | */ |
| 111 | function _print_robots_meta_generator() { | |
| 7d62e7c4 JV |
112 | $print_robots_noindex = variable_get('print_robots_noindex', PRINT_ROBOTS_NOINDEX_DEFAULT); |
| 113 | $print_robots_nofollow = variable_get('print_robots_nofollow', PRINT_ROBOTS_NOFOLLOW_DEFAULT); | |
| 114 | $print_robots_noarchive = variable_get('print_robots_noarchive', PRINT_ROBOTS_NOARCHIVE_DEFAULT); | |
| b09f08f7 JV |
115 | $robots_meta = array(); |
| 116 | ||
| 7d62e7c4 | 117 | if (!empty($print_robots_noindex)) { |
| b09f08f7 JV |
118 | $robots_meta[] = 'noindex'; |
| 119 | } | |
| 7d62e7c4 | 120 | if (!empty($print_robots_nofollow)) { |
| b09f08f7 JV |
121 | $robots_meta[] = 'nofollow'; |
| 122 | } | |
| 7d62e7c4 | 123 | if (!empty($print_robots_noarchive)) { |
| b09f08f7 JV |
124 | $robots_meta[] = 'noarchive'; |
| 125 | } | |
| b09f08f7 | 126 | |
| 7e748d74 JV |
127 | if (count($robots_meta) > 0) { |
| 128 | $robots_meta = implode(', ', $robots_meta); | |
| 7d62e7c4 | 129 | $robots_meta = "<meta name='robots' content='$robots_meta' />\n"; |
| b09f08f7 JV |
130 | } |
| 131 | else { | |
| 132 | $robots_meta = ''; | |
| 133 | } | |
| 134 | ||
| 135 | return $robots_meta; | |
| 136 | } | |
| 137 | ||
| 138 | /** | |
| 1e156b5a | 139 | * Post-processor that fills the array for the template with common details |
| b09f08f7 | 140 | * |
| 1e156b5a JV |
141 | * @param $node |
| 142 | * generated node with a printer-friendly node body | |
| fb70cd39 JV |
143 | * @param $message |
| 144 | * optional sender's message (used by the send e-mail module) | |
| 1e156b5a | 145 | * @param $cid |
| 3483245d | 146 | * id of current comment being generated (NULL when not generating |
| 1e156b5a JV |
147 | * an individual comment) |
| 148 | * @return | |
| 149 | * array with the fields to be used in the template | |
| b09f08f7 | 150 | */ |
| 27dc0ea1 | 151 | function _print_var_generator($node, $message = NULL, $cid = NULL) { |
| 855b243a | 152 | global $base_url, $language, $_print_urls; |
| b09f08f7 JV |
153 | |
| 154 | $path = empty($node->nid) ? $node->path : "node/$node->nid"; | |
| 155 | ||
| b09f08f7 | 156 | // print module settings |
| 7d62e7c4 | 157 | $print_css = variable_get('print_css', PRINT_CSS_DEFAULT); |
| ee8a10b8 | 158 | $print_logo_options = variable_get('print_logo_options', PRINT_LOGO_OPTIONS_DEFAULT); |
| 7d62e7c4 JV |
159 | $print_logo_url = variable_get('print_logo_url', PRINT_LOGO_URL_DEFAULT); |
| 160 | $print_html_sendtoprinter = variable_get('print_html_sendtoprinter', PRINT_HTML_SENDTOPRINTER_DEFAULT); | |
| 161 | $print_sourceurl_enabled = variable_get('print_sourceurl_enabled', PRINT_SOURCEURL_ENABLED_DEFAULT); | |
| 162 | $print_sourceurl_forcenode = variable_get('print_sourceurl_forcenode', PRINT_SOURCEURL_FORCENODE_DEFAULT); | |
| 163 | $print_sourceurl_date = variable_get('print_sourceurl_date', PRINT_SOURCEURL_DATE_DEFAULT); | |
| b97f7b85 JV |
164 | $print_footer_options = variable_get('print_footer_options', PRINT_FOOTER_OPTIONS_DEFAULT); |
| 165 | $print_footer_user = variable_get('print_footer_user', PRINT_FOOTER_USER_DEFAULT); | |
| 7d62e7c4 JV |
166 | |
| 167 | $print['language'] = $language->language; | |
| 168 | $print['title'] = $node->title; | |
| 169 | $print['head'] = drupal_get_html_head(); | |
| 170 | $print['scripts'] = drupal_get_js(); | |
| 171 | $print['robots_meta'] = _print_robots_meta_generator(); | |
| 172 | $print['url'] = url($path, array('absolute' => TRUE)); | |
| 173 | $print['base_href'] = "<base href='". $print['url'] ."' />\n"; | |
| 174 | $print['favicon'] = theme_get_setting('toggle_favicon') ? "<link rel='shortcut icon' href='". theme_get_setting('favicon') ."' type='image/x-icon' />\n" : ''; | |
| b09f08f7 | 175 | |
| 7d62e7c4 | 176 | if (!empty($print_css)) { |
| 6f6bfb3e | 177 | $replace_pairs = array('%b' => base_path(), '%t' => path_to_theme()); |
| 24332e75 | 178 | $user_css = strip_tags(strtr($print_css, $replace_pairs)); |
| b09f08f7 JV |
179 | } |
| 180 | else { | |
| 24332e75 | 181 | drupal_add_css(drupal_get_path('module', 'print') .'/css/print.css'); |
| b09f08f7 | 182 | } |
| 24332e75 JV |
183 | $drupal_css = drupal_add_css(); |
| 184 | foreach ($drupal_css as $key => $types) { | |
| 185 | // Unset the theme's CSS | |
| 186 | $drupal_css[$key]['theme'] = array(); | |
| 27dc0ea1 JV |
187 | } |
| 188 | ||
| 189 | // If we are sending a message via e-mail, the CSS must be embedded | |
| 190 | if (!empty($message)) { | |
| 7d62e7c4 | 191 | $style = ''; |
| 24332e75 JV |
192 | $css_files = array(); |
| 193 | foreach ($drupal_css as $types) { | |
| 194 | foreach ($types as $values) { | |
| 195 | $css_files = array_merge($css_files, array_keys($values)); | |
| 196 | } | |
| 197 | } | |
| 198 | if (!empty($print_css)) { | |
| 0f1f957b | 199 | // Convert to a local path, by removing the base_path |
| 24332e75 JV |
200 | $pattern = '!^'. base_path() .'!'; |
| 201 | $css_files[] = preg_replace($pattern, '', $user_css); | |
| 202 | } | |
| 203 | foreach ($css_files as $filename) { | |
| 0f1f957b JV |
204 | $res = file_get_contents($filename, TRUE); |
| 205 | if ($res != FALSE) { | |
| 206 | $style .= $res; | |
| 207 | } | |
| 27dc0ea1 | 208 | } |
| 7d62e7c4 | 209 | $print['css'] = "<style type='text/css' media='all'>$style</style>\n"; |
| 27dc0ea1 JV |
210 | } |
| 211 | else { | |
| 24332e75 JV |
212 | $print['css'] = drupal_get_css($drupal_css); |
| 213 | if (!empty($print_css)) { | |
| 214 | $print['css'] .= "<link type='text/css' rel='stylesheet' media='all' href='$user_css' />\n"; | |
| 27dc0ea1 | 215 | } |
| 5d9fa914 | 216 | } |
| b09f08f7 | 217 | |
| 7d62e7c4 | 218 | $print['sendtoprinter'] = $print_html_sendtoprinter ? ' onload="window.print();"' : ''; |
| 3483245d | 219 | |
| ee8a10b8 JV |
220 | switch ($print_logo_options) { |
| 221 | case 0: // none | |
| 222 | $logo_url = 0; | |
| 223 | break; | |
| 224 | case 1: // theme's | |
| 225 | $logo_url = theme_get_setting('logo'); | |
| 226 | break; | |
| 227 | case 2: // user-specifed | |
| 228 | $logo_url = strip_tags($print_logo_url); | |
| 229 | break; | |
| 230 | } | |
| 7d62e7c4 | 231 | $print['logo'] = $logo_url ? "<img class='print-logo' src='$logo_url' alt='' />\n" : ''; |
| 1424b51c | 232 | |
| b97f7b85 JV |
233 | switch ($print_footer_options) { |
| 234 | case 0: // none | |
| 235 | $footer = ''; | |
| 236 | break; | |
| 237 | case 1: // theme's | |
| 238 | $footer = filter_xss_admin(variable_get('site_footer', FALSE)) ."\n". theme('blocks', 'footer'); | |
| 239 | $logo_url = theme_get_setting('logo'); | |
| 240 | break; | |
| 241 | case 2: // user-specifed | |
| 242 | $footer = $print_footer_user; | |
| 243 | break; | |
| 244 | } | |
| 245 | $print['footer_message'] = $footer; | |
| 246 | ||
| 1424b51c | 247 | $published_site = variable_get('site_name', 0); |
| eaac64a9 | 248 | if ($published_site) { |
| 855b243a JV |
249 | $print_text_published = variable_get('print_text_published', t('Published on %site_name')); |
| 250 | $published = t($print_text_published, array('%site_name' => $published_site)); | |
| 7d62e7c4 | 251 | $print['site_name'] = $published .' ('. l($base_url, $base_url) .')'; |
| 1424b51c JV |
252 | } |
| 253 | else { | |
| 7d62e7c4 | 254 | $print['site_name'] = ''; |
| 1424b51c | 255 | } |
| b09f08f7 | 256 | |
| 7d62e7c4 | 257 | if ($print_sourceurl_enabled == 1) { |
| dad7232c | 258 | /* Grab and format the src URL */ |
| 7d62e7c4 JV |
259 | if (empty($print_sourceurl_forcenode)) { |
| 260 | $url = $print['url']; | |
| dad7232c JV |
261 | } |
| 262 | else { | |
| eaac64a9 | 263 | $url = $base_url .'/'. (((bool)variable_get('clean_url', '0')) ? '' : '?q=') . $path; |
| dad7232c | 264 | } |
| 20ab24e6 | 265 | if (is_int($cid)) { |
| 7d62e7c4 | 266 | $url .= '#comment-$cid'; |
| dad7232c | 267 | } |
| 6fea4d6b | 268 | $retrieved_date = format_date(REQUEST_TIME, 'small'); |
| 855b243a JV |
269 | $print_text_retrieved = variable_get('print_text_retrieved', t('retrieved on %date')); |
| 270 | $retrieved = t($print_text_retrieved, array('%date' => $retrieved_date)); | |
| 7d62e7c4 | 271 | $print['printdate'] = $print_sourceurl_date ? " ($retrieved)" : ''; |
| 1424b51c | 272 | |
| 855b243a | 273 | $source_url = variable_get('print_text_source_url', t('Source URL')); |
| 7d62e7c4 | 274 | $print['source_url'] = '<strong>'. $source_url . $print['printdate'] .':</strong> '. l($url, $url); |
| b09f08f7 JV |
275 | } |
| 276 | else { | |
| 7d62e7c4 | 277 | $print['source_url'] = ''; |
| b09f08f7 JV |
278 | } |
| 279 | ||
| 7e748d74 JV |
280 | if (isset($node->type)) { |
| 281 | $node_type = $node->type; | |
| 1424b51c | 282 | |
| 3da84173 | 283 | if (theme_get_setting("toggle_node_info_$node_type")) { |
| 855b243a | 284 | $print_text_by = variable_get('print_text_by', t('By %author')); |
| eaac64a9 | 285 | $by_author = ($node->name ? $node->name : variable_get('anonymous', t('Anonymous'))); |
| 855b243a | 286 | $print['submitted'] = t($print_text_by, array('%author' => $by_author)); |
| 1424b51c | 287 | |
| 855b243a | 288 | $print_text_created = variable_get('print_text_created', t('Created %date')); |
| eaac64a9 | 289 | $created_datetime = format_date($node->created, 'small'); |
| 855b243a | 290 | $print['created'] = t($print_text_created, array('%date' => $created_datetime)); |
| eaac64a9 JV |
291 | } |
| 292 | else { | |
| 7d62e7c4 JV |
293 | $print['submitted'] = ''; |
| 294 | $print['created'] = ''; | |
| eaac64a9 | 295 | } |
| 1424b51c | 296 | |
| 7d62e7c4 | 297 | $print['type'] = $node->type; |
| 7e748d74 JV |
298 | } |
| 299 | else { | |
| 7d62e7c4 JV |
300 | $print['submitted'] = ''; |
| 301 | $print['created'] = ''; | |
| 302 | $print['type'] = ''; | |
| 7e748d74 | 303 | } |
| ca01bdd2 | 304 | |
| b09f08f7 JV |
305 | menu_set_active_item($path); |
| 306 | $breadcrumb = drupal_get_breadcrumb(); | |
| 307 | if (!empty($breadcrumb)) { | |
| 308 | $breadcrumb[] = menu_get_active_title(); | |
| 7d62e7c4 | 309 | $print['breadcrumb'] = implode(' > ', $breadcrumb); |
| b09f08f7 JV |
310 | } |
| 311 | else { | |
| 7d62e7c4 | 312 | $print['breadcrumb'] = ''; |
| b09f08f7 JV |
313 | } |
| 314 | ||
| 315 | // Display the collected links at the bottom of the page. Code once taken from Kjartan Mannes' project.module | |
| 1a92e513 | 316 | $print['pfp_links'] = ''; |
| 855b243a | 317 | if (!empty($_print_urls)) { |
| b09f08f7 JV |
318 | $urls = _print_friendly_urls(); |
| 319 | $max = count($urls); | |
| 1424b51c | 320 | $pfp_links = ''; |
| b09f08f7 | 321 | if ($max) { |
| b09f08f7 | 322 | for ($i = 0; $i < $max; $i++) { |
| 1424b51c | 323 | $pfp_links .= '['. ($i + 1) .'] '. $urls[$i] ."<br />\n"; |
| b09f08f7 | 324 | } |
| 855b243a | 325 | $links = variable_get('print_text_links', t('Links')); |
| 7d62e7c4 | 326 | $print['pfp_links'] = "<p><strong>$links:</strong><br />$pfp_links</p>"; |
| b09f08f7 JV |
327 | } |
| 328 | } | |
| 329 | ||
| b09f08f7 JV |
330 | if (module_exists('taxonomy')) { |
| 331 | $terms = taxonomy_link('taxonomy terms', $node); | |
| 7d62e7c4 | 332 | $print['taxonomy'] = theme('links', $terms); |
| b09f08f7 JV |
333 | } |
| 334 | ||
| 7d62e7c4 JV |
335 | $print['content'] = $node->body; |
| 336 | $print['node'] = $node; | |
| 337 | $print['message'] = $message; | |
| b09f08f7 JV |
338 | |
| 339 | return $print; | |
| 340 | } | |
| 341 | ||
| 342 | /** | |
| 1e156b5a | 343 | * Callback function for the preg_replace_callback for URL-capable patterns |
| b09f08f7 | 344 | * |
| 3483245d | 345 | * Manipulate URLs to make them absolute in the URLs list, and to add a |
| 1e156b5a | 346 | * [n] footnote marker. |
| 3483245d | 347 | * |
| 1e156b5a JV |
348 | * @param $matches |
| 349 | * array with the matched tag patterns, usually <a...>+text+</a> | |
| 350 | * @return | |
| 3483245d | 351 | * tag with re-written URL and when appropriate the [n] index to the |
| 1e156b5a | 352 | * URL list |
| b09f08f7 JV |
353 | */ |
| 354 | function _print_rewrite_urls($matches) { | |
| 855b243a | 355 | global $base_url, $base_root, $_print_urls; |
| b09f08f7 JV |
356 | |
| 357 | // first, split the html into the different tag attributes | |
| 3da84173 | 358 | $pattern = '!\s*(\w+\s*=\s*"(?:\\\"|[^"])*")\s*|\s*(\w+\s*=\s*\'(?:\\\\\'|[^\'])*\')\s*|\s*(\w+\s*=\s*\w+)\s*|\s+!'; |
| 6ca5f5a4 JV |
359 | $attribs = preg_split($pattern, $matches[1], -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE); |
| 360 | foreach ($attribs as $key => $value) { | |
| 1c9c68be | 361 | $attribs[$key] = preg_replace('!(\w)\s*=\s*(.*)!', '$1=$2', $value); |
| 6ca5f5a4 | 362 | } |
| b09f08f7 | 363 | |
| 7d62e7c4 JV |
364 | $size = count($attribs); |
| 365 | for ($i=1; $i < $size; $i++) { | |
| b09f08f7 | 366 | // If the attribute is href or src, we may need to rewrite the URL in the value |
| 1c9c68be | 367 | if (preg_match('!^(?:href|src)\s*?=(.*)!i', $attribs[$i], $urls) > 0) { |
| 400943b8 | 368 | $url = trim($urls[1], " \t\n\r\0\x0B\"'"); |
| b09f08f7 | 369 | |
| 1c9c68be | 370 | if (strpos($url, '://') || preg_match('!^mailto:.*?@.*?\..*?$!iu', $url)) { |
| b09f08f7 JV |
371 | // URL is absolute, do nothing |
| 372 | $newurl = urldecode($url); | |
| 373 | } | |
| 374 | else { | |
| bb0645f8 | 375 | if ($url[0] == '#') { |
| b09f08f7 | 376 | // URL is an anchor tag |
| 855b243a | 377 | if (!empty($_print_urls)) { |
| bb0645f8 JV |
378 | $path = explode('/', $_GET['q']); |
| 379 | unset($path[0]); | |
| 380 | $path = implode('/', $path); | |
| b09f08f7 JV |
381 | if (is_numeric($path)) { |
| 382 | $path = "node/$path"; | |
| 383 | } | |
| 384 | // Printer-friendly URLs is on, so we need to make it absolute | |
| 385 | $newurl = url($path, array('fragment' => substr(urldecode($url), 1), 'absolute' => TRUE)); | |
| 386 | } | |
| 387 | // Because base href is the original page, change the link to | |
| 388 | // still be usable inside the print page | |
| 389 | $matches[1] = str_replace($url, $_GET['q'] . $url, $matches[1]); | |
| 390 | } | |
| 391 | else { | |
| 392 | // URL is relative, convert it into absolute URL | |
| bb0645f8 | 393 | if ($url[0] == '/') { |
| b09f08f7 | 394 | // If it starts with '/' just append it to the server name |
| 7d62e7c4 | 395 | $newurl = $base_root .'/'. trim(urldecode($url), '/'); |
| b09f08f7 | 396 | } |
| 1c9c68be JV |
397 | elseif (preg_match('!^(?:index.php)?\?q=!i', $url)) { |
| 398 | // If it starts with ?q=, just prepend with the base URL | |
| 7d62e7c4 | 399 | $newurl = $base_url .'/'. trim(urldecode($url), '/'); |
| b09f08f7 JV |
400 | } |
| 401 | else { | |
| 7d62e7c4 | 402 | $newurl = url(trim(urldecode($url), '/'), array('absolute' => TRUE)); |
| b09f08f7 | 403 | } |
| 3da84173 | 404 | $matches[1] = str_replace($url, $newurl, $matches[1]); |
| b09f08f7 JV |
405 | } |
| 406 | } | |
| 407 | } | |
| 408 | } | |
| 409 | ||
| b09f08f7 JV |
410 | $ret = '<'. $matches[1] .'>'; |
| 411 | if (count($matches) == 4) { | |
| 412 | $ret .= $matches[2] . $matches[3]; | |
| 855b243a | 413 | if ((!empty($_print_urls)) && (isset($newurl))) { |
| b09f08f7 JV |
414 | $ret .= ' <span class="print-footnote">['. _print_friendly_urls(trim(stripslashes($newurl))) .']</span>'; |
| 415 | } | |
| 416 | } | |
| 417 | ||
| 418 | return $ret; | |
| 419 | } | |
| 420 | ||
| 421 | /** | |
| 422 | * Auxiliary function to store the Printer-friendly URLs list as static. | |
| 423 | * | |
| 1e156b5a JV |
424 | * @param $url |
| 425 | * absolute URL to be inserted in the list | |
| 3483245d JV |
426 | * @return |
| 427 | * list of URLs previously stored if $url is 0, or the current count | |
| 1e156b5a | 428 | * otherwise. |
| b09f08f7 JV |
429 | */ |
| 430 | function _print_friendly_urls($url = 0) { | |
| 431 | static $urls = array(); | |
| 432 | if ($url) { | |
| 433 | $url_idx = array_search($url, $urls); | |
| 434 | if ($url_idx !== FALSE) { | |
| 435 | return ($url_idx + 1); | |
| 436 | } | |
| 437 | else { | |
| 438 | $urls[] = $url; | |
| 439 | return count($urls); | |
| 440 | } | |
| 441 | } | |
| 21d8ff1e JV |
442 | $ret = $urls; |
| 443 | $urls = array(); | |
| 444 | return $ret; | |
| b09f08f7 JV |
445 | } |
| 446 | ||
| 447 | /** | |
| 1e156b5a | 448 | * Choose most appropriate template |
| 3483245d | 449 | * |
| b09f08f7 JV |
450 | * Auxiliary function to resolve the most appropriate template trying to find |
| 451 | * a content specific template in the theme or module dir before falling back | |
| 452 | * on a generic template also in those dirs. | |
| 453 | * | |
| 0d7894a0 JV |
454 | * @param format |
| 455 | * format of the PF page being rendered (html, pdf, etc.) | |
| 1e156b5a JV |
456 | * @param $type |
| 457 | * name of the node type being rendered in a PF page | |
| 458 | * @return | |
| 459 | * filename of the most suitable template | |
| b09f08f7 | 460 | */ |
| 0d7894a0 JV |
461 | function _print_get_template($format = NULL, $type = NULL) { |
| 462 | $filenames = array(); | |
| 463 | // First try to find a template defined both for the format and then the type | |
| 464 | if (!empty($format) && !empty($type)) { | |
| 465 | $filenames[] = "print_$format.node-$type.tpl.php"; | |
| 466 | } | |
| 467 | // Then only for the format | |
| 468 | if (!empty($format)) { | |
| 469 | $filenames[] = "print_$format.tpl.php"; | |
| 470 | } | |
| 471 | // If the node type is known, then try to find that type's template file | |
| 472 | if (!empty($type)) { | |
| 473 | $filenames[] = "print.node-$type.tpl.php"; | |
| 474 | } | |
| 475 | // Finally search for a generic template file | |
| 7d62e7c4 | 476 | $filenames[] = 'print.tpl.php'; |
| 0d7894a0 JV |
477 | |
| 478 | foreach ($filenames as $value) { | |
| b09f08f7 | 479 | // First in the theme directory |
| 7d62e7c4 | 480 | $file = drupal_get_path('theme', $GLOBALS['theme_key']) .'/'. $value; |
| 0d7894a0 JV |
481 | if (file_exists($file)) { |
| 482 | return $file; | |
| b09f08f7 JV |
483 | } |
| 484 | // Then in the module directory | |
| 7d62e7c4 | 485 | $file = drupal_get_path('module', 'print') .'/'. $value; |
| 0d7894a0 JV |
486 | if (file_exists($file)) { |
| 487 | return $file; | |
| b09f08f7 JV |
488 | } |
| 489 | } | |
| b09f08f7 JV |
490 | } |
| 491 | ||
| b09f08f7 | 492 | /** |
| 855b243a JV |
493 | * Check URL list settings for this node |
| 494 | * | |
| 495 | * @param node | |
| 496 | * node object | |
| 497 | * @param $format | |
| 498 | * format of the page being generated | |
| 499 | * @return | |
| 500 | * TRUE if URL list should be displayed, FALSE otherwise | |
| 501 | */ | |
| 502 | function _print_url_list_enabled($node, $format = PRINT_HTML_FORMAT) { | |
| 503 | switch ($format) { | |
| 504 | case PRINT_HTML_FORMAT: | |
| 505 | $node_urllist = isset($node->print_display_urllist) ? $node->print_display_urllist : TRUE; | |
| 506 | $fmt = ''; | |
| 507 | break; | |
| 508 | case PRINT_MAIL_FORMAT: | |
| 509 | $node_urllist = isset($node->print_mail_display_urllist) ? $node->print_mail_display_urllist : TRUE; | |
| 510 | $fmt = $format .'_'; | |
| 511 | break; | |
| 512 | case PRINT_PDF_FORMAT: | |
| 513 | $node_urllist = isset($node->print_pdf_display_urllist) ? $node->print_pdf_display_urllist : TRUE; | |
| 514 | $fmt = $format .'_'; | |
| 515 | break; | |
| 516 | } | |
| 517 | if (!isset($node_urllist)) $node_urllist = TRUE; | |
| 518 | ||
| 519 | // Get value of Printer-friendly URLs setting | |
| 520 | return (variable_get('print_urls', PRINT_URLS_DEFAULT) && ($node_urllist) && | |
| 521 | variable_get('print_'. $fmt .'display_urllist_'. $node->type, PRINT_TYPE_URLLIST_DEFAULT)); | |
| 522 | } | |
| 523 | ||
| 524 | /** | |
| 1e156b5a | 525 | * Prepare a Printer-friendly-ready node body for content nodes |
| 3483245d | 526 | * |
| 1e156b5a JV |
527 | * @param $nid |
| 528 | * node ID of the node to be rendered into a printer-friendly page | |
| 529 | * @param $cid | |
| 530 | * comment ID of the individual comment to be rendered | |
| 855b243a JV |
531 | * @param $format |
| 532 | * format of the page being generated | |
| 0706a465 JV |
533 | * @param $teaser |
| 534 | * if set to TRUE, outputs only the node's teaser | |
| fb70cd39 JV |
535 | * @param $message |
| 536 | * optional sender's message (used by the send e-mail module) | |
| 1e156b5a JV |
537 | * @return |
| 538 | * filled array ready to be used in the template | |
| b09f08f7 | 539 | */ |
| 855b243a JV |
540 | function _print_generate_node($nid, $cid = NULL, $format = PRINT_HTML_FORMAT, $teaser = FALSE, $message = NULL) { |
| 541 | global $_print_urls; | |
| 542 | ||
| b09f08f7 | 543 | // We can take a node id |
| f0e49fdd | 544 | $node = node_load($nid); |
| 7853dd07 JV |
545 | if (!$node) { |
| 546 | // Node not found | |
| 547 | drupal_not_found(); | |
| 5c9469df | 548 | return FALSE; |
| 7853dd07 JV |
549 | } |
| 550 | elseif (!node_access('view', $node)) { | |
| b09f08f7 | 551 | // Access is denied |
| 7853dd07 | 552 | drupal_access_denied(); |
| 5c9469df | 553 | return FALSE; |
| b09f08f7 | 554 | } |
| 878e1ed0 | 555 | drupal_set_title($node->title, PASS_THROUGH); |
| b09f08f7 JV |
556 | |
| 557 | //alert other modules that we are generating a printer-friendly page, so they can choose to show/hide info | |
| 7e748d74 | 558 | $node->printing = TRUE; |
| b09f08f7 JV |
559 | // Turn off Pagination by the Paging module |
| 560 | unset($node->pages); | |
| 561 | unset($node->page_count); | |
| 562 | ||
| b09f08f7 | 563 | if ($cid === NULL) { |
| 7853dd07 | 564 | // Adapted (simplified) version of node_view |
| b09f08f7 | 565 | //Render the node content |
| 0706a465 | 566 | $node = node_build_content($node, $teaser, TRUE); |
| b09f08f7 | 567 | // Disable fivestar widget output |
| 7d62e7c4 | 568 | unset($node->content['fivestar_widget']); |
| b09f08f7 | 569 | // Disable service links module output |
| 7d62e7c4 | 570 | unset($node->content['service_links']); |
| 3483245d | 571 | |
| 7853dd07 | 572 | $content = drupal_render($node->content); |
| 8efba45c JV |
573 | |
| 574 | // Disable the AdSense module ads | |
| 575 | $content = preg_replace('!<div class=[\'"]adsense[\'"].*?</div>!sim', '', $content); | |
| 576 | ||
| 7853dd07 JV |
577 | if ($teaser) { |
| 578 | $node->teaser = $content; | |
| 579 | unset($node->body); | |
| 580 | } | |
| 581 | else { | |
| 582 | $node->body = $content; | |
| 583 | unset($node->teaser); | |
| 584 | } | |
| 585 | ||
| 9490b60e JV |
586 | //TODO the following was part of the fix for http://drupal.org/node/254863 |
| 587 | //check if it is reproducible and find the exact condition which | |
| 588 | //triggered it | |
| 589 | //$node->body = html_entity_decode($node->body); | |
| b09f08f7 JV |
590 | } |
| 591 | ||
| 7d62e7c4 | 592 | $print_comments = variable_get('print_comments', PRINT_COMMENTS_DEFAULT); |
| b09f08f7 | 593 | |
| 7d62e7c4 | 594 | if (function_exists('comment_render') && (($cid != NULL) || ($print_comments))) { |
| b09f08f7 JV |
595 | //Print only the requested comment (or if $cid is NULL, all of them) |
| 596 | $comments = comment_render($node, $cid); | |
| 3483245d | 597 | |
| b09f08f7 | 598 | //Remove the comment forms |
| 7d62e7c4 | 599 | $comments = preg_replace('!<form.*?id="comment-.*?">.*?</form>!sim', '', $comments); |
| b09f08f7 | 600 | //Remove the 'Post new comment' title |
| cc053834 | 601 | $comments = preg_replace('!<h2.*?>'. t('Post new comment') .'</h2>!', '', $comments); |
| b09f08f7 | 602 | //Remove the comment title hyperlink |
| cc053834 | 603 | $comments = preg_replace('!(<h3.*?>.*?)<a.*?>(.*?)</a>(.*?</h3>)!i', '$1$2$3', $comments); |
| e458482f | 604 | //Remove the comment author link |
| cc053834 | 605 | $pattern = '!(<(?:span|div) class="submitted">.*?)<a.*?>(.*?)</a>(.*?</(?:span|div)>)!sim'; |
| c42f715c | 606 | if (preg_match($pattern, $comments)) { |
| cc053834 | 607 | $comments = preg_replace($pattern , '$1$2$3', $comments); |
| c42f715c | 608 | } |
| b09f08f7 | 609 | //Remove the comment links |
| 7d62e7c4 | 610 | $comments = preg_replace('!\s*<ul class="links">.*?</ul>!sim', '', $comments); |
| b09f08f7 JV |
611 | if ($cid != NULL) { |
| 612 | // Single comment requested, output only the comment | |
| 613 | unset($node->body); | |
| 614 | } | |
| 615 | $node->body .= $comments; | |
| 616 | } | |
| 617 | ||
| 7853dd07 | 618 | node_invoke_nodeapi($node, 'alter', $teaser, TRUE); |
| b09f08f7 | 619 | |
| 855b243a JV |
620 | if ($teaser) { |
| 621 | $node->body = $node->teaser; | |
| 622 | unset($node->teaser); | |
| 623 | } | |
| 624 | ||
| 625 | //Check URL list settings | |
| 626 | $_print_urls = _print_url_list_enabled($node, $format); | |
| 627 | ||
| b09f08f7 | 628 | // Convert the a href elements |
| 7d62e7c4 JV |
629 | $pattern = '!<(a\s[^>]*?)>(.*?)(</a>)!is'; |
| 630 | $node->body = preg_replace_callback($pattern, '_print_rewrite_urls', $node->body); | |
| b09f08f7 JV |
631 | |
| 632 | init_theme(); | |
| 633 | ||
| 27dc0ea1 | 634 | $print = _print_var_generator($node, $message, $cid); |
| b09f08f7 JV |
635 | |
| 636 | return $print; | |
| 637 | } | |
| 638 | ||
| 639 | /** | |
| 1e156b5a | 640 | * Prepare a Printer-friendly-ready node body for non-content pages |
| 3483245d | 641 | * |
| 1e156b5a JV |
642 | * @param $path |
| 643 | * path of the node to be rendered into a printer-friendly page | |
| 855b243a JV |
644 | * @param $format |
| 645 | * format of the page being generated | |
| fb70cd39 JV |
646 | * @param $teaser |
| 647 | * if set to TRUE, outputs only the node's teaser | |
| 648 | * @param $message | |
| 649 | * optional sender's message (used by the send e-mail module) | |
| 1e156b5a JV |
650 | * @return |
| 651 | * filled array ready to be used in the template | |
| b09f08f7 | 652 | */ |
| 855b243a JV |
653 | function _print_generate_path($path, $format = PRINT_HTML_FORMAT, $teaser = FALSE, $message = NULL) { |
| 654 | global $_print_urls; | |
| 655 | ||
| b09f08f7 JV |
656 | $path = drupal_get_normal_path($path); |
| 657 | ||
| 658 | menu_set_active_item($path); | |
| 659 | // Adapted from index.php. | |
| 660 | $node = new stdClass(); | |
| 661 | $node->body = menu_execute_active_handler($path); | |
| 00590736 | 662 | $node->title = drupal_get_title(); |
| b09f08f7 JV |
663 | $node->path = $path; |
| 664 | ||
| 665 | // It may happen that a drupal_not_found is called in the above call | |
| 666 | if (preg_match('/404 Not Found/', drupal_get_headers()) == 1) { | |
| 5c9469df | 667 | return FALSE; |
| b09f08f7 JV |
668 | } |
| 669 | ||
| 9eaec26d JV |
670 | if (is_int($node->body)) { |
| 671 | switch ($node->body) { | |
| 672 | case MENU_NOT_FOUND: | |
| 7853dd07 | 673 | drupal_not_found(); |
| 5c9469df | 674 | return FALSE; |
| 9eaec26d JV |
675 | break; |
| 676 | case MENU_ACCESS_DENIED: | |
| 7853dd07 | 677 | drupal_access_denied(); |
| 5c9469df | 678 | return FALSE; |
| 9eaec26d JV |
679 | break; |
| 680 | } | |
| b09f08f7 JV |
681 | } |
| 682 | ||
| 683 | // Delete any links area | |
| 7d62e7c4 | 684 | $node->body = preg_replace('!\s*<div class="links">.*?</div>!sim', '', $node->body); |
| b09f08f7 | 685 | |
| 855b243a JV |
686 | //Check URL list settings |
| 687 | $_print_urls = _print_url_list_enabled($node, $format); | |
| 688 | ||
| b09f08f7 | 689 | // Convert the a href elements |
| 7d62e7c4 JV |
690 | $pattern = '!<(a\s[^>]*?)>(.*?)(</a>)!is'; |
| 691 | $node->body = preg_replace_callback($pattern, '_print_rewrite_urls', $node->body); | |
| b09f08f7 JV |
692 | |
| 693 | init_theme(); | |
| 694 | ||
| 27dc0ea1 | 695 | $print = _print_var_generator($node, $message); |
| b09f08f7 JV |
696 | |
| 697 | return $print; | |
| 698 | } | |
| 699 | ||
| 700 | ||
| 701 | /** | |
| 1e156b5a | 702 | * Prepare a Printer-friendly-ready node body for book pages |
| 3483245d | 703 | * |
| 1e156b5a JV |
704 | * @param $nid |
| 705 | * node ID of the node to be rendered into a printer-friendly page | |
| 855b243a JV |
706 | * @param $format |
| 707 | * format of the page being generated | |
| fb70cd39 JV |
708 | * @param $teaser |
| 709 | * if set to TRUE, outputs only the node's teaser | |
| 710 | * @param $message | |
| 711 | * optional sender's message (used by the send e-mail module) | |
| 1e156b5a JV |
712 | * @return |
| 713 | * filled array ready to be used in the template | |
| b09f08f7 | 714 | */ |
| 855b243a JV |
715 | function _print_generate_book($nid, $format = PRINT_HTML_FORMAT, $teaser = FALSE, $message = NULL) { |
| 716 | global $_print_urls; | |
| 717 | ||
| f0e49fdd | 718 | $node = node_load($nid); |
| 7853dd07 JV |
719 | if (!$node) { |
| 720 | // Node not found | |
| 721 | drupal_not_found(); | |
| 5c9469df | 722 | return FALSE; |
| 7853dd07 JV |
723 | } |
| 724 | elseif (!node_access('view', $node) || (!user_access('access printer-friendly version'))) { | |
| b09f08f7 | 725 | // Access is denied |
| 7853dd07 | 726 | drupal_access_denied(); |
| 5c9469df | 727 | return FALSE; |
| b09f08f7 JV |
728 | } |
| 729 | ||
| 730 | $tree = book_menu_subtree_data($node->book); | |
| 731 | $node->body = book_export_traverse($tree, 'book_node_export'); | |
| 732 | ||
| 855b243a JV |
733 | //Check URL list settings |
| 734 | $_print_urls = _print_url_list_enabled($node, $format); | |
| 735 | ||
| b09f08f7 | 736 | // Convert the a href elements |
| 7d62e7c4 JV |
737 | $pattern = '!<(a\s[^>]*?)>(.*?)(</a>)!is'; |
| 738 | $node->body = preg_replace_callback($pattern, '_print_rewrite_urls', $node->body); | |
| b09f08f7 JV |
739 | |
| 740 | init_theme(); | |
| 741 | ||
| 27dc0ea1 | 742 | $print = _print_var_generator($node, $message); |
| b09f08f7 | 743 | // The title is already displayed by the book_recurse, so avoid duplication |
| 7d62e7c4 | 744 | $print['title'] = ''; |
| b09f08f7 JV |
745 | |
| 746 | return $print; | |
| 747 | } |