print.inc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. /**
  3. * @file
  4. * Common functions used by several of the print modules
  5. *
  6. * @ingroup print
  7. */
  8. /**
  9. * Auxiliary function to scan all module directories for a given library.
  10. *
  11. * @param string $lib
  12. * The machine name of a library to return the path for.
  13. * @param string $mask
  14. * The preg_match() regular expression of the files to find.
  15. *
  16. * @return array
  17. * An array of the filenames matching the provided mask.
  18. */
  19. function _print_scan_libs($lib, $mask) {
  20. $tools = array_keys(file_scan_directory(drupal_get_path('module', 'print'), $mask));
  21. $tools = array_merge($tools, array_keys(file_scan_directory(PRINT_LIB_PATH, $mask)));
  22. if (module_exists('libraries')) {
  23. $tools = array_merge($tools, array_keys(file_scan_directory(libraries_get_path($lib), $mask)));
  24. }
  25. return array_unique($tools);
  26. }
  27. /**
  28. * Callback function for the preg_replace_callback replacing spaces with %20
  29. *
  30. * Replace spaces in URLs with %20
  31. *
  32. * @param array $matches
  33. * array with the matched tag patterns, usually <a...>+text+</a>
  34. *
  35. * @return string
  36. * tag with re-written URL
  37. */
  38. function _print_replace_spaces($matches) {
  39. // first, split the html into the different tag attributes
  40. $pattern = '!\s*(\w+\s*=\s*"(?:\\\"|[^"])*")\s*|\s*(\w+\s*=\s*\'(?:\\\\\'|[^\'])*\')\s*|\s*(\w+\s*=\s*\w+)\s*|\s+!';
  41. $attribs = preg_split($pattern, $matches[1], -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
  42. foreach ($attribs as $key => $value) {
  43. $attribs[$key] = preg_replace('!(\w)\s*=\s*(.*)!', '$1=$2', $value);
  44. }
  45. $size = count($attribs);
  46. for ($i=1; $i < $size; $i++) {
  47. // If the attribute is href or src, we may need to rewrite the URL in the value
  48. if (preg_match('!^(?:href|src)\s*?=(.*)!i', $attribs[$i], $urls) > 0) {
  49. $url = trim($urls[1], " \t\n\r\0\x0B\"'");
  50. $new_url = str_replace(' ', '%20', $url);
  51. $matches[1] = str_replace($url, $new_url, $matches[1]);
  52. }
  53. }
  54. $ret = '<' . $matches[1] . '>';
  55. if (count($matches) == 4) {
  56. $ret .= $matches[2] . $matches[3];
  57. }
  58. return $ret;
  59. }
  60. /**
  61. * Convert image paths to the file:// protocol
  62. *
  63. * In some Drupal setups, the use of the 'private' filesystem or Apache's
  64. * configuration prevent access to the images of the page. This function
  65. * tries to circumnvent those problems by accessing files in the local
  66. * filesystem.
  67. *
  68. * @param string $html
  69. * contents of the post-processed template already with the node data
  70. * @param bool $images_via_file
  71. * if TRUE, convert also files in the 'public' filesystem to local paths
  72. *
  73. * @return string
  74. * converted file names
  75. */
  76. function _print_access_images_via_file($html, $images_via_file) {
  77. global $base_url, $language;
  78. $lang = (function_exists('language_negotiation_get_any') && language_negotiation_get_any('locale-url')) ? $language->language : '';
  79. // Always convert private to local paths
  80. $pattern = "!(<img\s[^>]*?src\s*?=\s*?['\"]?)${base_url}/(?:(?:index.php)?\?q=)?(?:${lang}/)?system/files/([^>]*?>)!is";
  81. $replacement = '$1file://' . realpath(variable_get('file_private_path', '')) . '/$2';
  82. $html = preg_replace($pattern, $replacement, $html);
  83. if ($images_via_file) {
  84. $pattern = "!(<img\s[^>]*?src\s*?=\s*?['\"]?)${base_url}/(?:(?:index.php)?\?q=)?(?:${lang}/)?([^>]*?>)!is";
  85. $replacement = '$1file://' . dirname($_SERVER['SCRIPT_FILENAME']) . '/$2';
  86. $html = preg_replace($pattern, $replacement, $html);
  87. }
  88. return $html;
  89. }