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