Projet

Général

Profil

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

root / drupal7 / sites / all / modules / print / print_pdf / print_pdf.module @ a2baadd1

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
  $block = array();
174

    
175
      switch ($delta) {
176
      case 'print_pdf-top':
177
        $block['subject'] = t('Most PDFd');
178
        $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)
179
                    ->fetchAll();
180
        if (count($result)) {
181
          $block['content'] = '<div class="item-list"><ul>';
182
          foreach ($result as $obj) {
183
            $block['content'] .= '<li>' . l(_print_get_title($obj->path), $obj->path) . '</li>';
184
          }
185
          $block['content'] .= '</ul></div>';
186
        }
187
        break;
188
      }
189
      return $block;
190
}
191

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

    
235
          if (version_compare($version, '5.9.001', '<')) {
236
            $requirements['print_pdf_tool'] = array(
237
              'title' => $t('Printer, email and PDF versions - PDF generation library'),
238
              'value' => $t('Unsupported TCPDF version'),
239
              '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'))),
240
              'severity' => REQUIREMENT_ERROR,
241
            );
242
          }
243
          else {
244
            $requirements['print_pdf_tool'] = array(
245
              'title' => $t('Printer, email and PDF versions - PDF generation library'),
246
              'value' => $t('TCPDF') . ' ' . $version,
247
            );
248
          }
249

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

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

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

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

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

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

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

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

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

    
366
    if ($output === FALSE) {
367
      $output = TRUE;
368

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

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

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

    
388
  foreach ($nodes as $node) {
389
    $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);
390
    $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);
391
    $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);
392
  }
393
}
394

    
395
/**
396
 * Implements hook_node_insert().
397
 */
398
function print_pdf_node_insert($node) {
399
  if (user_access('administer print') || user_access('node-specific print configuration')) {
400
    if (!isset($node->print_pdf_display)) $node->print_pdf_display = variable_get('print_pdf_display_' . $node->type, PRINT_TYPE_SHOW_LINK_DEFAULT);
401
    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);
402
    if (!isset($node->print_pdf_display_urllist)) $node->print_pdf_display_urllist = variable_get('print_pdf_display_urllist_' . $node->type, PRINT_TYPE_URLLIST_DEFAULT);
403

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

    
408
/**
409
 * Implements hook_node_update().
410
 */
411
function print_pdf_node_update($node) {
412
  if (user_access('administer print') || user_access('node-specific print configuration')) {
413
    if (!isset($node->print_pdf_display) || $node->print_pdf_display === NULL) {
414
      $node->print_pdf_display = variable_get('print_pdf_display_' . $node->type, PRINT_TYPE_SHOW_LINK_DEFAULT);
415
    }
416
    if (!isset($node->print_pdf_display_comment) || $node->print_pdf_display_comment === NULL) {
417
      $node->print_pdf_display_comment = variable_get('print_pdf_display_comment_' . $node->type, PRINT_TYPE_COMMENT_LINK_DEFAULT);
418
    }
419
    if (!isset($node->print_pdf_display_urllist) || $node->print_pdf_display_urllist === NULL) {
420
      $node->print_pdf_display_urllist = variable_get('print_pdf_display_urllist_' . $node->type, PRINT_TYPE_URLLIST_DEFAULT);
421
    }
422

    
423
    _print_pdf_node_conf_modify($node->nid, $node->print_pdf_display, $node->print_pdf_display_comment, $node->print_pdf_display_urllist);
424
  }
425
}
426

    
427
/**
428
 * Implements hook_node_delete().
429
 */
430
function print_pdf_node_delete($node) {
431
  db_delete('print_pdf_node_conf')
432
    ->condition('nid', $node->nid)
433
    ->execute();
434
  db_delete('print_pdf_page_counter')
435
    ->condition('path', 'node/' . $node->nid)
436
    ->execute();
437
}
438

    
439
/**
440
 * Implements hook_form_alter().
441
 */
442
function print_pdf_form_alter(&$form, &$form_state, $form_id) {
443
  // Add the node-type settings option to activate the PDF version link
444
  if ((user_access('administer print') || user_access('node-specific print configuration')) &&
445
      (($form_id == 'node_type_form') || !empty($form['#node_edit_form']))) {
446
    $form['print']['pdf_label'] = array(
447
      '#type' => 'markup',
448
      '#markup' => '<p><strong>' . t('PDF version') . '</strong></p>',
449
    );
450

    
451
    $form['print']['print_pdf_display'] = array(
452
      '#type' => 'checkbox',
453
      '#title' => t('Show link'),
454
    );
455
    $form['print']['print_pdf_display_comment'] = array(
456
      '#type' => 'checkbox',
457
      '#title' => t('Show link in individual comments'),
458
    );
459
    $form['print']['print_pdf_display_urllist'] = array(
460
      '#type' => 'checkbox',
461
      '#title' => t('Show Printer-friendly URLs list'),
462
    );
463

    
464
    if ($form_id == 'node_type_form') {
465
      $form['print']['print_pdf_display']['#default_value'] = variable_get('print_pdf_display_' . $form['#node_type']->type, PRINT_TYPE_SHOW_LINK_DEFAULT);
466
      $form['print']['print_pdf_display_comment']['#default_value'] = variable_get('print_pdf_display_comment_' . $form['#node_type']->type, PRINT_TYPE_COMMENT_LINK_DEFAULT);
467
      $form['print']['print_pdf_display_urllist']['#default_value'] = variable_get('print_pdf_display_urllist_' . $form['#node_type']->type, PRINT_TYPE_URLLIST_DEFAULT);
468
    }
469
    else {
470
      $node = $form['#node'];
471
      $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);
472
      $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);
473
      $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);
474
    }
475
  }
476
}
477

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

    
503
/**
504
 * Format the PDF version link
505
 *
506
 * @return
507
 *   array of formatted attributes
508
 * @ingroup themeable
509
 */
510
function theme_print_pdf_format_link() {
511
  $print_pdf_link_class  = variable_get('print_pdf_link_class', PRINT_PDF_LINK_CLASS_DEFAULT);
512
  $print_pdf_content_disposition = variable_get('print_pdf_content_disposition', PRINT_PDF_CONTENT_DISPOSITION_DEFAULT);
513
  $print_pdf_show_link = variable_get('print_pdf_show_link', PRINT_PDF_SHOW_LINK_DEFAULT);
514
  $print_pdf_link_text = filter_xss(variable_get('print_pdf_link_text', t('PDF version')));
515

    
516
  $img = drupal_get_path('module', 'print') . '/icons/pdf_icon.gif';
517
  $title = t('Display a PDF version of this page.');
518
  $class = strip_tags($print_pdf_link_class);
519
  $new_window = ($print_pdf_content_disposition == 1);
520
  $format = _print_format_link_aux($print_pdf_show_link, $print_pdf_link_text, $img);
521

    
522
  return array('text' => $format['text'],
523
               'html' => $format['html'],
524
               'attributes' => print_fill_attributes($title, $class, $new_window),
525
              );
526
}
527

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

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

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

    
616
    $node = $args['node'];
617
    if (isset($node->type)) {
618
      $node_type = $node->type;
619
    }
620
    // Node
621
    $print_pdf_node_link_visibility = variable_get('print_pdf_node_link_visibility', PRINT_PDF_NODE_LINK_VISIBILITY_DEFAULT);
622
    $print_pdf_node_link_pages = variable_get('print_pdf_node_link_pages', PRINT_PDF_NODE_LINK_PAGES_DEFAULT);
623

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

    
669
    return _print_page_match($print_pdf_sys_link_visibility, $_GET['q'], $print_pdf_sys_link_pages);
670
  }
671
  return FALSE;
672
}
673

    
674
/**
675
 * Find out the version of the TCPDF library
676
 */
677
function _print_pdf_tcpdf_version() {
678
  $print_pdf_pdf_tool = variable_get('print_pdf_pdf_tool', PRINT_PDF_PDF_TOOL_DEFAULT);
679
  if (variable_get('print_pdf_autoconfig', PRINT_PDF_AUTOCONFIG_DEFAULT)) {
680
    // prevent TCPDF default configs
681
    define('K_TCPDF_EXTERNAL_CONFIG', TRUE);
682
  }
683
  require_once(DRUPAL_ROOT . '/' . $print_pdf_pdf_tool);
684

    
685
  // Hide warnings, as some TCPDF constants may still be undefined
686
  if (class_exists('TCPDF')) {
687
    @$pdf = new TCPDF();
688

    
689
    if (class_exists('TCPDF_STATIC')) {
690
      return TCPDF_STATIC::getTCPDFVersion();
691
    }
692
    elseif (method_exists($pdf, 'getTCPDFVersion')) {
693
      return $pdf->getTCPDFVersion();
694
    }
695
    elseif (defined('PDF_PRODUCER')) {
696
      sscanf(PDF_PRODUCER, "TCPDF %s", $version);
697

    
698
      return $version;
699
    }
700
  }
701
  return 'unknown';
702
}
703

    
704
/**
705
 * Find out the version of the wkhtmltopdf library
706
 */
707
function _print_pdf_wkhtmltopdf_version() {
708
  $print_pdf_pdf_tool = variable_get('print_pdf_pdf_tool', PRINT_PDF_PDF_TOOL_DEFAULT);
709
  $descriptor = array(0 => array('pipe', 'r'), 1 => array('pipe', 'w'), 2 => array('pipe', 'w'));
710

    
711
  $cmd = '"' . realpath($print_pdf_pdf_tool) . '" --version';
712
  $process = proc_open($cmd, $descriptor, $pipes, NULL, NULL);
713
  if (is_resource($process)) {
714
    $content = stream_get_contents($pipes[1]);
715
    $out = preg_match('!.*?(\d+\.\d+\.\d+).*$!m', $content, $matches);
716
    fclose($pipes[0]);
717
    fclose($pipes[1]);
718
    fclose($pipes[2]);
719
    $retval = proc_close($process);
720
  }
721

    
722
  return ($matches[1]);
723
}
724

    
725
/**
726
 * Implements hook_views_api().
727
 */
728
function print_pdf_views_api() {
729
  return array(
730
    'api' => 2.0,
731
    'path' => drupal_get_path('module', 'print_pdf'),
732
  );
733
}