print_epub.pages.inc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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
  21. // headers from the cache.
  22. $GLOBALS['conf']['cache'] = FALSE;
  23. $args = func_get_args();
  24. $path = filter_xss(implode('/', $args));
  25. $cid = isset($_GET['comment']) ? (int) $_GET['comment'] : NULL;
  26. // Handle the query.
  27. $query = $_GET;
  28. unset($query['q']);
  29. $node = NULL;
  30. if (!empty($path)) {
  31. if ($alias = drupal_lookup_path('source', $path)) {
  32. // Alias.
  33. $path_arr = explode('/', $alias);
  34. $node = node_load($path_arr[1]);
  35. }
  36. elseif (ctype_digit($args[0])) {
  37. // Normal nid.
  38. $node = node_load($args[0]);
  39. }
  40. $epub_filename = variable_get('print_epub_filename', PRINT_EPUB_FILENAME_DEFAULT);
  41. if (!empty($epub_filename) && !empty($node)) {
  42. $epub_filename = token_replace($epub_filename, array('node' => $node), array('clear' => TRUE));
  43. }
  44. else {
  45. $epub_filename = token_replace($epub_filename, array('site'), array('clear' => TRUE));
  46. if (empty($epub_filename)) {
  47. // If empty, use a fallback solution.
  48. $epub_filename = str_replace('/', '_', $path);
  49. }
  50. }
  51. }
  52. else {
  53. $epub_filename = 'page';
  54. }
  55. if (function_exists('transliteration_clean_filename')) {
  56. $epub_filename = transliteration_clean_filename($epub_filename, language_default('language'));
  57. }
  58. drupal_alter('print_epub_filename', $epub_filename, $path);
  59. $epub = print_epub_generate_path($path, $query, $cid, $epub_filename . '.epub');
  60. if ($epub == NULL) {
  61. drupal_goto($path);
  62. exit;
  63. }
  64. $nodepath = (isset($node->nid)) ? 'node/' . $node->nid : drupal_get_normal_path($path);
  65. db_merge('print_epub_page_counter')
  66. ->key(array('path' => substr($nodepath, 0, 255)))
  67. ->fields(array(
  68. 'totalcount' => 1,
  69. 'timestamp' => REQUEST_TIME,
  70. ))
  71. ->expression('totalcount', 'totalcount + 1')
  72. ->execute();
  73. drupal_exit();
  74. }
  75. /**
  76. * Gennerate a EPUB for a given Drupal path.
  77. *
  78. * @param string $path
  79. * path of the page to convert to EPUB.
  80. * @param array $query
  81. * (Optional) array of key/value pairs as used in the url() function for the
  82. * query.
  83. * @param int $cid
  84. * (Optional) comment ID of the comment to render.
  85. * @param string $epub_filename
  86. * (Optional) filename of the generated EPUB.
  87. * @param string $view_mode
  88. * (Optional) view mode to be used when rendering the content.
  89. *
  90. * @return string|null
  91. * generated EPUB page, or NULL in case of error
  92. *
  93. * @see print_epub_controller()
  94. */
  95. function print_epub_generate_path($path, $query = NULL, $cid = NULL, $epub_filename = NULL, $view_mode = PRINT_VIEW_MODE) {
  96. $link = print_epub_print_link();
  97. $node = print_controller($path, $link['format'], $cid, $view_mode);
  98. if ($node) {
  99. $html = theme('print', array(
  100. 'node' => $node,
  101. 'query' => $query,
  102. 'expand_css' => TRUE,
  103. 'format' => $link['format'],
  104. ));
  105. $meta = array(
  106. 'node' => $node,
  107. 'url' => url(drupal_get_path_alias(empty($node->nid) ? $node->path : "node/$node->nid"), array('absolute' => TRUE)),
  108. );
  109. if (isset($node->name)) {
  110. $meta['name'] = $node->name;
  111. }
  112. if (isset($node->title)) {
  113. $meta['title'] = $node->title;
  114. }
  115. return print_epub_generate_html($html, $meta, $epub_filename);
  116. }
  117. else {
  118. return NULL;
  119. }
  120. }