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