Projet

Général

Profil

Paste
Télécharger (27,2 ko) Statistiques
| Branche: | Révision:

root / htmltest / sites / all / modules / print / print_pdf / print_pdf.module @ dc45a079

1
<?php
2

    
3
/**
4
 * @file
5
 * Displays Printer-friendly versions of Drupal pages.
6
 *
7
 * @ingroup print
8
 */
9

    
10
define('PRINTPDF_PATH', 'printpdf');
11

    
12
// Defined in print.module
13
// define('PRINT_PDF_FORMAT', 'pdf');
14

    
15
define('PRINT_PDF_LIB_PATH', 'sites/all/libraries');
16

    
17
define('PRINT_PDF_LINK_POS_DEFAULT', '{ "link": "link", "block": "block", "help": "help" }');
18
define('PRINT_PDF_LINK_TEASER_DEFAULT', 0);
19
define('PRINT_PDF_SHOW_LINK_DEFAULT', 1);
20
define('PRINT_PDF_NODE_LINK_VISIBILITY_DEFAULT', 0);
21
define('PRINT_PDF_NODE_LINK_PAGES_DEFAULT', '');
22
define('PRINT_PDF_LINK_CLASS_DEFAULT', 'print-pdf');
23
define('PRINT_PDF_SYS_LINK_VISIBILITY_DEFAULT', 1);
24
define('PRINT_PDF_SYS_LINK_PAGES_DEFAULT', '');
25
define('PRINT_PDF_LINK_USE_ALIAS_DEFAULT', 0);
26
define('PRINT_PDF_BOOK_LINK_DEFAULT', 1);
27
define('PRINT_PDF_PDF_TOOL_DEFAULT', 0);
28
define('PRINT_PDF_CONTENT_DISPOSITION_DEFAULT', 2);
29
define('PRINT_PDF_PAPER_SIZE_DEFAULT', 'A4');
30
define('PRINT_PDF_PAGE_ORIENTATION_DEFAULT', 'portrait');
31
define('PRINT_PDF_IMAGES_VIA_FILE_DEFAULT', 0);
32
define('PRINT_PDF_AUTOCONFIG_DEFAULT', 1);
33
define('PRINT_PDF_FONT_FAMILY_DEFAULT', 'dejavusans');
34
define('PRINT_PDF_FONT_SIZE_DEFAULT', 10);
35
define('PRINT_PDF_FONT_SUBSETTING_DEFAULT', FALSE);
36
define('PRINT_PDF_FILENAME_DEFAULT', '[site:name] - [node:title] - [node:changed:custom:Y-m-d]');
37
define('PRINT_PDF_DOMPDF_UNICODE_DEFAULT', 0);
38
define('PRINT_PDF_WKHTMLTOPDF_OPTIONS', "--footer-font-size 7 --footer-right '[page]'");
39
define('PRINT_PDF_DOMPDF_CACHE_DIR_DEFAULT', 'print_pdf/dompdf');
40
define('PRINT_PDF_TCPDF_CACHE_DIR_DEFAULT', 'print_pdf/tcpdf');
41

    
42
/**
43
 * Implements hook_permission().
44
 */
45
function print_pdf_permission() {
46
  return array(
47
    'access PDF version' => array(
48
      'title' => t('Access the PDF version'),
49
      'description' => t('View the PDF versions and the links to them in the original pages.'),
50
    ),
51
  );
52
}
53

    
54
/**
55
 * Implements hook_theme().
56
 */
57
function print_pdf_theme() {
58
  return array(
59
    'print_pdf_format_link' => array(
60
      'variables' => array(),
61
    ),
62
    'print_pdf_dompdf_footer' => array(
63
      'variables' => array('html' => ''),
64
      'file' => 'print_pdf.pages.inc',
65
    ),
66
    'print_pdf_tcpdf_header' => array(
67
      'variables' => array('pdf' => NULL, 'html' => '', 'font' => array()),
68
      'file' => 'print_pdf.pages.inc',
69
    ),
70
    'print_pdf_tcpdf_page' => array(
71
      'variables' => array('pdf' => NULL),
72
      'file' => 'print_pdf.pages.inc',
73
    ),
74
    'print_pdf_tcpdf_content' => array(
75
      'variables' => array('pdf' => NULL, 'html' => '', 'font' => array()),
76
      'file' => 'print_pdf.pages.inc',
77
    ),
78
    'print_pdf_tcpdf_footer' => array(
79
      'variables' => array('pdf' => NULL, 'html' => '', 'font' => array()),
80
      'file' => 'print_pdf.pages.inc',
81
    ),
82
    'print_pdf_tcpdf_footer2' => array(
83
      'variables' => array('pdf' => NULL),
84
      'file' => 'print_pdf.pages.inc',
85
    ),
86
  );
87
}
88

    
89
/**
90
 * Implements hook_init().
91
 */
92
function print_pdf_init() {
93
  if (variable_get('print_pdf_autoconfig', PRINT_PDF_AUTOCONFIG_DEFAULT)) {
94
    $pdf_dirs = array();
95
    $print_pdf_pdf_tool = variable_get('print_pdf_pdf_tool', PRINT_PDF_PDF_TOOL_DEFAULT);
96

    
97
    if (basename($print_pdf_pdf_tool) == 'dompdf_config.inc.php') {
98
      $pdf_dirs[] = PRINT_PDF_DOMPDF_CACHE_DIR_DEFAULT . '/fonts';
99
    }
100
    elseif (basename($print_pdf_pdf_tool) == 'tcpdf.php') {
101
      foreach (array('cache', 'images') as $dir) {
102
        $pdf_dirs[] = PRINT_PDF_TCPDF_CACHE_DIR_DEFAULT . '/' . $dir;
103
      }
104
    }
105

    
106
    if (!empty($pdf_dirs)) {
107
      foreach ($pdf_dirs as $pdf_dir) {
108
        $directory = 'public://' . $pdf_dir;
109
        file_prepare_directory($directory, FILE_CREATE_DIRECTORY);
110
      }
111
    }
112
  }
113
}
114

    
115
/**
116
 * Implements hook_menu().
117
 */
118
function print_pdf_menu() {
119
  $items = array();
120

    
121
  $items[PRINTPDF_PATH] = array(
122
    'title' => 'Printer-friendly PDF',
123
    'page callback' => 'print_pdf_controller',
124
    'access arguments' => array('access PDF version'),
125
    'type' => MENU_CALLBACK,
126
    'file' => 'print_pdf.pages.inc',
127
  );
128
  $items[PRINTPDF_PATH . '/' . PRINTPDF_PATH] = array(
129
    'access callback' => FALSE,
130
  );
131
  $items['admin/config/user-interface/print/pdf'] = array(
132
    'title' => 'PDF',
133
    'description' => 'Configure the settings of the PDF generation functionality.',
134
    'page callback' => 'drupal_get_form',
135
    'page arguments' => array('print_pdf_settings'),
136
    'access arguments'  => array('administer print'),
137
    'weight' => 3,
138
    'type' => MENU_LOCAL_TASK,
139
    'file' => 'print_pdf.admin.inc',
140
  );
141
  $items['admin/config/user-interface/print/pdf/options'] = array(
142
    'title' => 'Options',
143
    'weight' => 1,
144
    'type' => MENU_DEFAULT_LOCAL_TASK,
145
  );
146
  $items['admin/config/user-interface/print/pdf/strings'] = array(
147
    'title' => 'Text strings',
148
    'description' => 'Override the user-facing strings used in the PDF version.',
149
    'page callback' => 'drupal_get_form',
150
    'page arguments' => array('print_pdf_strings_settings'),
151
    'access arguments'  => array('administer print'),
152
    'weight' => 2,
153
    'type' => MENU_LOCAL_TASK,
154
    'file' => 'print_pdf.admin.inc',
155
  );
156

    
157
  return $items;
158
}
159

    
160
/**
161
 * Implements hook_block_info().
162
 */
163
function print_pdf_block_info() {
164
      $block['print_pdf-top']['info'] = t('Most PDFd');
165
      $block['print_pdf-top']['cache'] = DRUPAL_CACHE_GLOBAL;
166
      return $block;
167
}
168

    
169
/**
170
 * Implements hook_block_view().
171
 */
172
function print_pdf_block_view($delta = 0) {
173
      switch ($delta) {
174
      case 'print_pdf-top':
175
        $block['subject'] = t('Most PDFd');
176
        $result = db_query_range("SELECT path FROM {print_pdf_page_counter} LEFT JOIN {node} n ON path = CONCAT('node/', n.nid) WHERE status <> 0 OR status IS NULL ORDER BY totalcount DESC", 0, 3)
177
                    ->fetchAll();
178
        if (count($result)) {
179
          $block['content'] = '<div class="item-list"><ul>';
180
          foreach ($result as $obj) {
181
            $block['content'] .= '<li>' . l(_print_get_title($obj->path), $obj->path) . '</li>';
182
          }
183
          $block['content'] .= '</ul></div>';
184
        }
185
        break;
186
      }
187
      return $block;
188
}
189

    
190
/**
191
 * Implements hook_requirements().
192
 */
193
function print_pdf_requirements($phase) {
194
  $requirements = array();
195
  $t = get_t();
196
  switch ($phase) {
197
    // At runtime, make sure that a PDF generation tool is selected
198
    case 'runtime':
199
      $print_pdf_pdf_tool = variable_get('print_pdf_pdf_tool', PRINT_PDF_PDF_TOOL_DEFAULT);
200
      if (empty($print_pdf_pdf_tool)) {
201
        $requirements['print_pdf_tool'] = array(
202
          'title' => $t('Printer, email and PDF versions - PDF generation library'),
203
          'value' => $t('No PDF tool selected'),
204
          'description' => $t('Please configure it in the !url.', array('!url' => l($t('PDF settings page'), 'admin/config/user-interface/print/pdf'))),
205
          'severity' => REQUIREMENT_ERROR,
206
        );
207
      }
208
      else {
209
        if (!is_file($print_pdf_pdf_tool) || !is_readable($print_pdf_pdf_tool)) {
210
          $requirements['print_pdf_tool'] = array(
211
            'title' => $t('Printer, email and PDF versions - PDF generation library'),
212
            'value' => $t('File not found'),
213
            'description' => $t('The currently selected PDF generation library (%file) is no longer accessible.', array('%file' => $print_pdf_pdf_tool)),
214
            'severity' => REQUIREMENT_ERROR,
215
          );
216
        }
217
        elseif (basename($print_pdf_pdf_tool) == 'dompdf_config.inc.php') {
218
          if (variable_get('print_pdf_autoconfig', PRINT_PDF_AUTOCONFIG_DEFAULT)) {
219
            $directory = 'public://' . PRINT_PDF_DOMPDF_CACHE_DIR_DEFAULT . '/fonts';
220
            if (!is_dir($directory) || !is_writable($directory)) {
221
              $requirements['print_pdf_tool'] = array(
222
                'title' => $t('DOMPDF font cache directory'),
223
                'value' => $t('Non-writable permissions'),
224
                'description' => $t('You must change the %fontdir permissions to be writable, as dompdf requires write-access to that directory.', array('%fontdir' => $directory)),
225
                'severity' => REQUIREMENT_ERROR,
226
              );
227
            }
228
          }
229
        }
230
        elseif (basename($print_pdf_pdf_tool) == 'tcpdf.php') {
231
          $version = _print_pdf_tcpdf_version();
232

    
233
          if (version_compare($version, '5.9.001', '<')) {
234
            $requirements['print_pdf_tool'] = array(
235
              'title' => $t('Printer, email and PDF versions - PDF generation library'),
236
              'value' => $t('Unsupported TCPDF version'),
237
              'description' => $t('The currently selected version of TCPDF (@version) is not supported. Please update to a !url.', array('@version' => $version, '!url' => l($t('newer version'), 'http://sourceforge.net/projects/tcpdf/files/latest'))),
238
              'severity' => REQUIREMENT_ERROR,
239
            );
240
          }
241
          else {
242
            $requirements['print_pdf_tool'] = array(
243
              'title' => $t('Printer, email and PDF versions - PDF generation library'),
244
              'value' => $t('TCPDF') . ' ' . $version,
245
            );
246
          }
247

    
248
          if (variable_get('print_pdf_autoconfig', PRINT_PDF_AUTOCONFIG_DEFAULT)) {
249
            foreach (array('cache', 'images') as $dir) {
250
              $directory = 'public://' . PRINT_PDF_TCPDF_CACHE_DIR_DEFAULT . '/' . $dir;
251
              if (!is_dir($directory) || !is_writable($directory)) {
252
                $requirements['print_pdf_tool_' . $dir] = array(
253
                  'title' => $t('TCPDF directory'),
254
                  'value' => $t('Non-writable permissions'),
255
                  'description' => $t('You must change the %fontdir permissions to be writable, as TCPDF requires write-access to that directory.', array('%fontdir' => $directory)),
256
                  'severity' => REQUIREMENT_ERROR,
257
                );
258
              }
259
            }
260
          }
261
        }
262
        elseif (drupal_substr(basename($print_pdf_pdf_tool, '.exe'), 0, 11) == 'wkhtmltopdf') {
263
          if (function_exists('is_executable') && !is_executable($print_pdf_pdf_tool)) {
264
            $requirements['print_pdf_tool'] = array(
265
              'title' => $t('wkhtmltopdf library'),
266
              'value' => $t('Non-executable permissions'),
267
              'description' => $t('You must modify the permissions of the wkhtmltopdf file (%file) to make it executable.', array('%file' => $print_pdf_pdf_tool)),
268
              'severity' => REQUIREMENT_ERROR,
269
            );
270
          }
271
          else {
272
            $version = _print_pdf_wkhtmltopdf_version();
273
            if (version_compare($version, '0.9.6', '<')) {
274
              $requirements['print_pdf_tool'] = array(
275
                'title' => $t('Printer, email and PDF versions - PDF generation library'),
276
                'value' => $t('Unsupported wkhtmltopdf version'),
277
                'description' => $t('The currently selected version of wkhtmltopdf (@version) is not supported. Please update to a !url.', array('@version' => $version, '!url' => l($t('newer version'), 'http://code.google.com/p/wkhtmltopdf/'))),
278
                'severity' => REQUIREMENT_ERROR,
279
              );
280
            }
281
            else {
282
              $requirements['print_pdf_tool'] = array(
283
                'title' => $t('Printer, email and PDF versions - PDF generation library'),
284
                'value' => $t('wkhtmltopdf') . ' ' . $version,
285
              );
286
            }
287
          }
288
        }
289
      }
290
      break;
291
  }
292
  return $requirements;
293
}
294

    
295
/**
296
 * Implements hook_node_view().
297
 */
298
function print_pdf_node_view($node, $view_mode) {
299
  $print_pdf_link_pos = variable_get('print_pdf_link_pos', drupal_json_decode(PRINT_PDF_LINK_POS_DEFAULT));
300
  $print_pdf_link_use_alias = variable_get('print_pdf_link_use_alias', PRINT_PDF_LINK_USE_ALIAS_DEFAULT);
301

    
302
  foreach (array('node', 'comment') as $type) {
303
    $allowed_type = print_pdf_link_allowed(array('type' => $type, 'node' => $node, 'view_mode' => $view_mode));
304
    if (($allowed_type) && !empty($print_pdf_link_pos['link'])) {
305
      drupal_add_css(drupal_get_path('module', 'print') . '/css/printlinks.css');
306
      $links = array();
307
      $format = theme('print_pdf_format_link');
308

    
309
      // Show book link
310
      if ($allowed_type === PRINT_ALLOW_BOOK_LINK) {
311
        $links['book_pdf'] = array(
312
          'href' => PRINTPDF_PATH . '/book/export/html/' . $node->nid,
313
          'title' => $format['text'],
314
          'attributes' => $format['attributes'],
315
          'html' => $format['html'],
316
        );
317
      }
318
      elseif ($allowed_type === PRINT_ALLOW_NORMAL_LINK) {
319
        $path = (($print_pdf_link_use_alias) && ($alias = drupal_lookup_path('alias', 'node/' . $node->nid))) ? $alias : $node->nid;
320

    
321
        $links['print_pdf'] = array(
322
          'href' => PRINTPDF_PATH . '/' . $path,
323
          'title' => $format['text'],
324
          'attributes' => $format['attributes'],
325
          'html' => $format['html'],
326
          'query' => print_query_string_encode($_GET, array('q')),
327
        );
328
      }
329

    
330
      $link_content = array(
331
        '#theme' => 'links',
332
        '#links' => $links,
333
        '#attributes' => array('class' => array('links', 'inline')),
334
      );
335

    
336
      if ($type == 'node') {
337
        $node->content['links']['print_pdf'] = $link_content;
338
      }
339
      elseif (($type == 'comment') && isset($node->content['comments']['comments'])) {
340
        foreach ($node->content['comments']['comments'] as $cid => $comment) {
341
          if (is_numeric($cid)) {
342
            $link_content['#links']['print_pdf']['query']['comment'] = $cid;
343
            $node->content['comments']['comments'][$cid]['links']['print_pdf'] = $link_content;
344
          }
345
        }
346
      }
347
    }
348
  }
349

    
350
  // Insert content corner links
351
  if ((!empty($print_pdf_link_pos['corner'])) && ($view_mode == 'full')) {
352
    $node->content['print_links']['#markup'] .= print_pdf_insert_link(NULL, $node);
353
  }
354
}
355

    
356
/**
357
 * Implements hook_help().
358
 */
359
function print_pdf_help($path, $arg) {
360
  $print_pdf_link_pos = variable_get('print_pdf_link_pos', drupal_json_decode(PRINT_PDF_LINK_POS_DEFAULT));
361
  if (($path !== 'node/%') && !(empty($print_pdf_link_pos['help']))) {
362
    static $output = FALSE;
363

    
364
    if ($output === FALSE) {
365
      $output = TRUE;
366

    
367
      $link = print_pdf_insert_link();
368
      if ($link) {
369
        return "<span class='print-syslink'>$link</span>";
370
      }
371
    }
372
  }
373
}
374

    
375
/**
376
 * Implements hook_node_load().
377
 */
378
function print_pdf_node_load($nodes, $types) {
379
  $ids = array();
380
  foreach ($nodes as $node) {
381
    $ids[] = $node->nid;
382
  }
383

    
384
  $result = db_query('SELECT nid, link, comments, url_list FROM {print_pdf_node_conf} WHERE nid IN (:nids)', array(':nids' => $ids))->fetchAllAssoc('nid');
385

    
386
  foreach ($nodes as $node) {
387
    $node->print_pdf_display = isset($result[$node->nid]) ? intval($result[$node->nid]->link) : variable_get('print_pdf_display_' . $node->type, PRINT_TYPE_SHOW_LINK_DEFAULT);
388
    $node->print_pdf_display_comment = isset($result[$node->nid]) ? intval($result[$node->nid]->comments) : variable_get('print_pdf_display_comment_' . $node->type, PRINT_TYPE_COMMENT_LINK_DEFAULT);
389
    $node->print_pdf_display_urllist = isset($result[$node->nid]) ? intval($result[$node->nid]->url_list) : variable_get('print_pdf_display_urllist_' . $node->type, PRINT_TYPE_URLLIST_DEFAULT);
390
  }
391
}
392

    
393
/**
394
 * Implements hook_node_insert().
395
 */
396
function print_pdf_node_insert($node) {
397
  if (user_access('administer print') || user_access('node-specific print configuration')) {
398
    if (!isset($node->print_pdf_display)) $node->print_pdf_display = variable_get('print_pdf_display_' . $node->type, PRINT_TYPE_SHOW_LINK_DEFAULT);
399
    if (!isset($node->print_pdf_display_comment)) $node->print_pdf_display_comment = variable_get('print_pdf_display_comment_' . $node->type, PRINT_TYPE_COMMENT_LINK_DEFAULT);
400
    if (!isset($node->print_pdf_display_urllist)) $node->print_pdf_display_urllist = variable_get('print_pdf_display_urllist_' . $node->type, PRINT_TYPE_URLLIST_DEFAULT);
401

    
402
    _print_pdf_node_conf_modify($node->nid, $node->print_pdf_display, $node->print_pdf_display_comment, $node->print_pdf_display_urllist);
403
  }
404
}
405

    
406
/**
407
 * Implements hook_node_update().
408
 */
409
function print_pdf_node_update($node) {
410
  if (user_access('administer print') || user_access('node-specific print configuration')) {
411
    if (!isset($node->print_pdf_display)) $node->print_pdf_display = variable_get('print_pdf_display_' . $node->type, PRINT_TYPE_SHOW_LINK_DEFAULT);
412
    if (!isset($node->print_pdf_display_comment)) $node->print_pdf_display_comment = variable_get('print_pdf_display_comment_' . $node->type, PRINT_TYPE_COMMENT_LINK_DEFAULT);
413
    if (!isset($node->print_pdf_display_urllist)) $node->print_pdf_display_urllist = variable_get('print_pdf_display_urllist_' . $node->type, PRINT_TYPE_URLLIST_DEFAULT);
414

    
415
    _print_pdf_node_conf_modify($node->nid, $node->print_pdf_display, $node->print_pdf_display_comment, $node->print_pdf_display_urllist);
416
  }
417
}
418

    
419
/**
420
 * Implements hook_node_delete().
421
 */
422
function print_pdf_node_delete($node) {
423
  db_delete('print_pdf_node_conf')
424
    ->condition('nid', $node->nid)
425
    ->execute();
426
  db_delete('print_pdf_page_counter')
427
    ->condition('path', 'node/' . $node->nid)
428
    ->execute();
429
}
430

    
431
/**
432
 * Implements hook_form_alter().
433
 */
434
function print_pdf_form_alter(&$form, &$form_state, $form_id) {
435
  // Add the node-type settings option to activate the PDF version link
436
  if ((user_access('administer print') || user_access('node-specific print configuration')) &&
437
      (($form_id == 'node_type_form') || !empty($form['#node_edit_form']))) {
438
    $form['print']['pdf_label'] = array(
439
      '#type' => 'markup',
440
      '#markup' => '<p><strong>' . t('PDF version') . '</strong></p>',
441
    );
442

    
443
    $form['print']['print_pdf_display'] = array(
444
      '#type' => 'checkbox',
445
      '#title' => t('Show link'),
446
    );
447
    $form['print']['print_pdf_display_comment'] = array(
448
      '#type' => 'checkbox',
449
      '#title' => t('Show link in individual comments'),
450
    );
451
    $form['print']['print_pdf_display_urllist'] = array(
452
      '#type' => 'checkbox',
453
      '#title' => t('Show Printer-friendly URLs list'),
454
    );
455

    
456
    if ($form_id == 'node_type_form') {
457
      $form['print']['print_pdf_display']['#default_value'] = variable_get('print_pdf_display_' . $form['#node_type']->type, PRINT_TYPE_SHOW_LINK_DEFAULT);
458
      $form['print']['print_pdf_display_comment']['#default_value'] = variable_get('print_pdf_display_comment_' . $form['#node_type']->type, PRINT_TYPE_COMMENT_LINK_DEFAULT);
459
      $form['print']['print_pdf_display_urllist']['#default_value'] = variable_get('print_pdf_display_urllist_' . $form['#node_type']->type, PRINT_TYPE_URLLIST_DEFAULT);
460
    }
461
    else {
462
      $node = $form['#node'];
463
      $form['print']['print_pdf_display']['#default_value'] = isset($node->print_pdf_display) ? $node->print_pdf_display : variable_get('print_pdf_display_' . $node->type, PRINT_TYPE_SHOW_LINK_DEFAULT);
464
      $form['print']['print_pdf_display_comment']['#default_value'] = isset($node->print_pdf_display_comment) ? $node->print_pdf_display_comment : variable_get('print_pdf_display_comment_' . $node->type, PRINT_TYPE_COMMENT_LINK_DEFAULT);
465
      $form['print']['print_pdf_display_urllist']['#default_value'] = isset($node->print_pdf_display_urllist) ? $node->print_pdf_display_urllist : variable_get('print_pdf_display_urllist_' . $node->type, PRINT_TYPE_URLLIST_DEFAULT);
466
    }
467
  }
468
}
469

    
470
/**
471
 * Update the print_pdf_node_conf table to reflect the given attributes
472
 *
473
 * If updating to the default values, delete the record.
474
 *
475
 * @param $nid
476
 *   value of the nid field (primary key)
477
 * @param $link
478
 *   value of the link field (0 or 1)
479
 * @param $comments
480
 *   value of the comments field (0 or 1)
481
 * @param $url_list
482
 *   value of the url_list field (0 or 1)
483
 */
484
function _print_pdf_node_conf_modify($nid, $link, $comments, $url_list) {
485
    db_merge('print_pdf_node_conf')
486
      ->key(array('nid' => $nid))
487
      ->fields(array(
488
        'link' => $link,
489
        'comments' => $comments,
490
        'url_list' => $url_list,
491
      ))
492
      ->execute();
493
}
494

    
495
/**
496
 * Format the PDF version link
497
 *
498
 * @return
499
 *   array of formatted attributes
500
 * @ingroup themeable
501
 */
502
function theme_print_pdf_format_link() {
503
  $print_pdf_link_class  = variable_get('print_pdf_link_class', PRINT_PDF_LINK_CLASS_DEFAULT);
504
  $print_pdf_content_disposition = variable_get('print_pdf_content_disposition', PRINT_PDF_CONTENT_DISPOSITION_DEFAULT);
505
  $print_pdf_show_link = variable_get('print_pdf_show_link', PRINT_PDF_SHOW_LINK_DEFAULT);
506
  $print_pdf_link_text = filter_xss(variable_get('print_pdf_link_text', t('PDF version')));
507

    
508
  $img = drupal_get_path('module', 'print') . '/icons/pdf_icon.gif';
509
  $title = t('Display a PDF version of this page.');
510
  $class = strip_tags($print_pdf_link_class);
511
  $new_window = ($print_pdf_content_disposition == 1);
512
  $format = _print_format_link_aux($print_pdf_show_link, $print_pdf_link_text, $img);
513

    
514
  return array('text' => $format['text'],
515
               'html' => $format['html'],
516
               'attributes' => print_fill_attributes($title, $class, $new_window),
517
              );
518
}
519

    
520
/**
521
 * Auxiliary function to display a formatted PDF version link
522
 *
523
 * Function made available so that developers may call this function from
524
 * their defined pages/blocks.
525
 *
526
 * @param $path
527
 *   path of the original page (optional). If not specified, the current URL
528
 *   is used
529
 * @param $node
530
 *   an optional node object, to be used in defining the path, if used, the
531
 *   path argument is irrelevant
532
 * @return
533
 *   string with the HTML link to the printer-friendly page
534
 */
535
function print_pdf_insert_link($path = NULL, $node = NULL) {
536
  if ($node !== NULL) {
537
    $nid = $node->nid;
538
    $path = 'node/' . $nid;
539
    $allowed_type = print_pdf_link_allowed(array('node' => $node));
540
  }
541
  else {
542
    if ($path === NULL) {
543
      $nid = preg_replace('!^node/([\d]+)!', '$1', $_GET['q']);
544
      $path = $_GET['q'];
545
    }
546
    else {
547
      $nid = NULL;
548
    }
549
    $allowed_type = print_pdf_link_allowed(array('path' => $path));
550
  }
551

    
552
  if ($allowed_type) {
553
    if ($nid !== NULL) {
554
      if ($allowed_type === PRINT_ALLOW_BOOK_LINK) {
555
        $path = 'book/export/html/' . $nid;
556
      }
557
      else {
558
        if (variable_get('print_pdf_link_use_alias', PRINT_PDF_LINK_USE_ALIAS_DEFAULT) && ($alias = drupal_lookup_path('alias', $path))) {
559
          $path = $alias;
560
        }
561
        else {
562
          $path = $nid;
563
        }
564
      }
565
      $path = PRINTPDF_PATH . '/' . $path;
566
      $query = print_query_string_encode($_GET, array('q'));
567
    }
568
    else {
569
      $query = NULL;
570
    }
571
    drupal_add_css(drupal_get_path('module', 'print') . '/css/printlinks.css');
572
    $format = theme('print_pdf_format_link');
573
    return '<span class="print_pdf">' . l($format['text'], $path, array('attributes' => $format['attributes'], 'query' => $query, 'absolute' => TRUE, 'html' => $format['html'])) . '</span>';
574
  }
575
  else {
576
    return FALSE;
577
  }
578
}
579

    
580
/**
581
 * Check if the link to the PDF version is allowed depending on the settings
582
 *
583
 * @param $args
584
 *   array containing the possible parameters:
585
 *    teaser, node, type, path
586
 * @return
587
 *   FALSE if not allowed
588
 *   PRINT_ALLOW_NORMAL_LINK if a normal link is allowed
589
 *   PRINT_ALLOW_BOOK_LINK if a link is allowed in a book node
590
 */
591
function print_pdf_link_allowed($args) {
592
  $view_mode = isset($args['view_mode']) ? $args['view_mode'] : '';
593
  $print_pdf_pdf_tool = variable_get('print_pdf_pdf_tool', PRINT_PDF_PDF_TOOL_DEFAULT);
594
  if ((($view_mode == 'teaser') && !variable_get('print_pdf_link_teaser', PRINT_PDF_LINK_TEASER_DEFAULT))
595
      || !in_array($view_mode, array('full', 'teaser', '')) || !user_access('access PDF version') || (empty($print_pdf_pdf_tool))) {
596
    // If the teaser link is disabled or the user is not allowed
597
    return FALSE;
598
  }
599
  if (!empty($args['path'])) {
600
    $nid = preg_replace('!^node/!', '', drupal_get_normal_path($args['path']));
601
    if (ctype_digit($nid)) {
602
      $args['node'] = node_load($nid);
603
    }
604
  }
605
  if (!empty($args['node'])) {
606
    static $node_type = FALSE;
607

    
608
    $node = $args['node'];
609
    if (isset($node->type)) {
610
      $node_type = $node->type;
611
    }
612
    // Node
613
    $print_pdf_node_link_visibility = variable_get('print_pdf_node_link_visibility', PRINT_PDF_NODE_LINK_VISIBILITY_DEFAULT);
614
    $print_pdf_node_link_pages = variable_get('print_pdf_node_link_pages', PRINT_PDF_NODE_LINK_PAGES_DEFAULT);
615

    
616
    if (!_print_page_match($print_pdf_node_link_visibility, "node/" . $node->nid, $print_pdf_node_link_pages)) {
617
      // Page not in visibility list
618
      return FALSE;
619
    }
620
    elseif (isset($args['type']) && ($args['type'] == 'comment') && isset($node_type)) {
621
      // Link is for a comment, return the configured setting
622
      // Cache this statically to avoid duplicate queries for every comment.
623
      static $res = array();
624
      if (!isset($res[$node->nid])) {
625
        $res[$node->nid] = db_query("SELECT comments FROM {print_pdf_node_conf} WHERE nid = :nid", array(':nid' => $node->nid))->fetchField();
626
      }
627
      $print_display_comment = ($res && ($res[$node->nid] !== FALSE)) ? $res[$node->nid] : variable_get('print_pdf_display_comment_' . $node_type, PRINT_TYPE_COMMENT_LINK_DEFAULT);
628
      if ($print_display_comment) {
629
        return PRINT_ALLOW_NORMAL_LINK;
630
      }
631
    }
632
    else {
633
      // Node link
634
      if (isset($node->print_pdf_display) && !$node->print_pdf_display) {
635
        // Link for this node is disabled
636
        return FALSE;
637
      }
638
      elseif (isset($node->book)) {
639
        // Node is a book;
640
        $print_pdf_book_link = variable_get('print_pdf_book_link', PRINT_PDF_BOOK_LINK_DEFAULT);
641
        switch ($print_pdf_book_link) {
642
          case 1:
643
            if (user_access('access printer-friendly version')) {
644
              return PRINT_ALLOW_BOOK_LINK;
645
            }
646
            break;
647
          case 2:
648
            return PRINT_ALLOW_NORMAL_LINK;
649
        }
650
      }
651
      else {
652
        return PRINT_ALLOW_NORMAL_LINK;
653
      }
654
    }
655
  }
656
  else {
657
    // 'System' page
658
    $print_pdf_sys_link_visibility = variable_get('print_pdf_sys_link_visibility', PRINT_PDF_SYS_LINK_VISIBILITY_DEFAULT);
659
    $print_pdf_sys_link_pages = variable_get('print_pdf_sys_link_pages', PRINT_PDF_SYS_LINK_PAGES_DEFAULT);
660

    
661
    return _print_page_match($print_pdf_sys_link_visibility, $_GET['q'], $print_pdf_sys_link_pages);
662
  }
663
  return FALSE;
664
}
665

    
666
/**
667
 * Find out the version of the TCPDF library
668
 */
669
function _print_pdf_tcpdf_version() {
670
  $print_pdf_pdf_tool = variable_get('print_pdf_pdf_tool', PRINT_PDF_PDF_TOOL_DEFAULT);
671
  if (variable_get('print_pdf_autoconfig', PRINT_PDF_AUTOCONFIG_DEFAULT)) {
672
    // prevent TCPDF default configs
673
    define('K_TCPDF_EXTERNAL_CONFIG', TRUE);
674
  }
675
  require_once(DRUPAL_ROOT . '/' . $print_pdf_pdf_tool);
676

    
677
  // Hide warnings, as some TCPDF constants may still be undefined
678
  @$pdf = new TCPDF();
679

    
680
  if (method_exists($pdf, 'getTCPDFVersion')) {
681
    return $pdf->getTCPDFVersion();
682
  }
683
  elseif (defined('PDF_PRODUCER')) {
684
    sscanf(PDF_PRODUCER, "TCPDF %s", $version);
685

    
686
    return $version;
687
  }
688
  else {
689
    return 'unknown';
690
  }
691
}
692

    
693
/**
694
 * Find out the version of the wkhtmltopdf library
695
 */
696
function _print_pdf_wkhtmltopdf_version() {
697
  $print_pdf_pdf_tool = variable_get('print_pdf_pdf_tool', PRINT_PDF_PDF_TOOL_DEFAULT);
698
  $descriptor = array(0 => array('pipe', 'r'), 1 => array('pipe', 'w'), 2 => array('pipe', 'w'));
699

    
700
  $cmd = '"' . realpath($print_pdf_pdf_tool) . '" --version';
701
  $process = proc_open($cmd, $descriptor, $pipes, NULL, NULL);
702
  if (is_resource($process)) {
703
    $content = stream_get_contents($pipes[1]);
704
    $out = preg_match('!.*?(\d+\.\d+\.\d+).*$!m', $content, $matches);
705
    fclose($pipes[0]);
706
    fclose($pipes[1]);
707
    fclose($pipes[2]);
708
    $retval = proc_close($process);
709
  }
710

    
711
  return ($matches[1]);
712
}
713

    
714
/**
715
 * Implements hook_views_api().
716
 */
717
function print_pdf_views_api() {
718
  return array(
719
    'api' => 2.0,
720
    'path' => drupal_get_path('module', 'print_pdf'),
721
  );
722
}