print_epub.pages.inc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. /**
  3. * @file
  4. * Generates the EPUB versions of the pages
  5. *
  6. * This file is included by the print_epub module and includes the
  7. * functions that interface with the EPUB generation packages.
  8. *
  9. * @ingroup print
  10. */
  11. module_load_include('inc', 'print', 'print.pages');
  12. /**
  13. * Generate a EPUB version of the printer-friendly page
  14. *
  15. * @see print_controller()
  16. * @see _print_epub_domepub()
  17. * @see _print_epub_tcepub()
  18. */
  19. function print_epub_controller() {
  20. // Disable caching for generated EPUBs, as Drupal doesn't ouput the proper headers from the cache
  21. $GLOBALS['conf']['cache'] = FALSE;
  22. $args = func_get_args();
  23. $path = filter_xss(implode('/', $args));
  24. $cid = isset($_GET['comment']) ? (int)$_GET['comment'] : NULL;
  25. // Handle the query
  26. $query = $_GET;
  27. unset($query['q']);
  28. if (!empty($path)) {
  29. if ($alias = drupal_lookup_path('source', $path)) {
  30. // Alias
  31. $path_arr = explode('/', $alias);
  32. $node = node_load($path_arr[1]);
  33. }
  34. elseif (ctype_digit($args[0])) {
  35. // normal nid
  36. $node = node_load($args[0]);
  37. }
  38. $epub_filename = variable_get('print_epub_filename', PRINT_EPUB_FILENAME_DEFAULT);
  39. if (!empty($epub_filename) && !empty($node)) {
  40. $epub_filename = token_replace($epub_filename, array('node' => $node), array('clear' => TRUE));
  41. }
  42. else {
  43. $epub_filename = token_replace($epub_filename, array('site'), array('clear' => TRUE));
  44. if (empty($epub_filename)) {
  45. // If empty, use a fallback solution
  46. $epub_filename = str_replace('/', '_', $path);
  47. }
  48. }
  49. }
  50. else {
  51. $epub_filename = 'page';
  52. }
  53. if (function_exists('transliteration_clean_filename')) {
  54. $epub_filename = transliteration_clean_filename($epub_filename, language_default('language'));
  55. }
  56. drupal_alter('print_epub_filename', $epub_filename, $path);
  57. $epub = print_epub_generate_path($path, $query, $cid, $epub_filename . '.epub');
  58. if ($epub == NULL) {
  59. drupal_goto($path);
  60. exit;
  61. }
  62. $nodepath = (isset($node->nid)) ? 'node/' . $node->nid : drupal_get_normal_path($path);
  63. db_merge('print_epub_page_counter')
  64. ->key(array('path' => $nodepath))
  65. ->fields(array(
  66. 'totalcount' => 1,
  67. 'timestamp' => REQUEST_TIME,
  68. ))
  69. ->expression('totalcount', 'totalcount + 1')
  70. ->execute();
  71. drupal_exit();
  72. }
  73. /**
  74. * Gennerate a EPUB for a given Drupal path.
  75. *
  76. * @param string $path
  77. * path of the page to convert to EPUB
  78. * @param array $query
  79. * (optional) array of key/value pairs as used in the url() function for the
  80. * query
  81. * @param int $cid
  82. * (optional) comment ID of the comment to render.
  83. * @param string $epub_filename
  84. * (optional) filename of the generated EPUB
  85. * @param string $view_mode
  86. * (optional) view mode to be used when rendering the content
  87. *
  88. * @return
  89. * generated EPUB page, or NULL in case of error
  90. *
  91. * @see print_epub_controller()
  92. */
  93. function print_epub_generate_path($path, $query = NULL, $cid = NULL, $epub_filename = NULL, $view_mode = PRINT_VIEW_MODE) {
  94. global $base_url;
  95. $link = print_epub_print_link();
  96. $node = print_controller($path, $link['format'], $cid, $view_mode);
  97. if ($node) {
  98. $html = theme('print', array('node' => $node, 'query' => $query, 'expand_css' => TRUE, 'format' => $link['format']));
  99. $meta = array(
  100. 'node' => $node,
  101. 'url' => url(drupal_get_path_alias(empty($node->nid) ? $node->path : "node/$node->nid"), array('absolute' => TRUE)),
  102. );
  103. if (isset($node->name)) $meta['name'] = $node->name;
  104. if (isset($node->title)) $meta['title'] = $node->title;
  105. return print_epub_generate_html($html, $meta, $epub_filename);
  106. }
  107. else {
  108. return NULL;
  109. }
  110. }