forked from tikiorg/tiki
/
pdflib.php
167 lines (137 loc) · 4.15 KB
/
pdflib.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php
// (c) Copyright 2002-2015 by authors of the Tiki Wiki CMS Groupware Project
//
// All Rights Reserved. See copyright.txt for details and a complete list of authors.
// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
// $Id$
/**
*
*/
class PdfGenerator
{
private $mode;
private $location;
/**
*
*/
function __construct()
{
global $prefs;
$this->mode = 'none';
if ( $prefs['print_pdf_from_url'] == 'webkit' ) {
$path = $prefs['print_pdf_webkit_path'];
if (!empty($path) && is_executable($path)) {
$this->mode = 'webkit';
$this->location = $path;
}
} else if ($prefs['print_pdf_from_url'] == 'weasyprint') {
$path = $prefs['print_pdf_weasyprint_path'];
if (!empty($path) && is_executable($path)) {
$this->mode = 'weasyprint';
$this->location = $path;
}
} elseif ( $prefs['print_pdf_from_url'] == 'webservice' ) {
if ( ! empty( $prefs['print_pdf_webservice_url'] ) ) {
$this->mode = 'webservice';
$this->location = $prefs['print_pdf_webservice_url'];
}
}
}
/**
* @param $file
* @param array $params
* @return mixed
*/
function getPdf( $file, array $params )
{
global $prefs, $base_url, $tikiroot;
if ( $prefs['auth_token_access'] == 'y' ) {
$perms = Perms::get();
require_once 'lib/auth/tokens.php';
$tokenlib = AuthTokens::build($prefs);
$params['TOKEN'] = $tokenlib->createToken(
$tikiroot . $file, $params, $perms->getGroups(),
array('timeout' => 120)
);
}
$url = $base_url . $file . '?' . http_build_query($params, '', '&');
return $this->{$this->mode}( $url );
}
/**
* @param $url
* @return null
*/
private function none( $url )
{
return null;
}
/**
* @param $url
* @return mixed
*/
private function webkit( $url )
{
// Make sure shell_exec is available
if (!function_exists('shell_exec')) {
die(tra('Required function shell_exec is not enabled.'));
}
// escapeshellarg will replace all % characters with spaces on Windows
// So, decode the URL before sending it to the commandline
$urlDecoded = urldecode($url);
$arg = escapeshellarg($urlDecoded);
// Write a temporary file, instead of using stdout
// There seemed to be encoding issues when using stdout (on Windows 7 64 bit).
// Use temp/public. It is cleaned up during a cache clean, in case some files are left
$filename = 'temp/public/out'.rand().'.pdf';
// Run shell_exec command to generate out file
// NOTE: this requires write permissions
$quotedFilename = '"'.$filename.'"';
$quotedCommand = '"'.$this->location.'"';
`$quotedCommand -q $arg $quotedFilename`;
// Read the out file
$pdf = file_get_contents($filename);
// Delete the outfile
unlink($filename);
return $pdf;
}
/**
* @param $url
* @return mixed
*/
private function weasyprint( $url )
{
// Make sure shell_exec is available
if (!function_exists('shell_exec')) {
die(tra('Required function shell_exec is not enabled.'));
}
// escapeshellarg will replace all % characters with spaces on Windows
// So, decode the URL before sending it to the commandline
$urlDecoded = urldecode($url);
$arg = escapeshellarg($urlDecoded);
// Write a temporary file, instead of using stdout
// There seemed to be encoding issues when using stdout (on Windows 7 64 bit).
// Use temp/public. It is cleaned up during a cache clean, in case some files are left
$filename = 'temp/public/out'.rand().'.pdf';
// Run shell_exec command to generate out file
// NOTE: this requires write permissions
$quotedFilename = '"'.$filename.'"';
$quotedCommand = '"'.$this->location.'"';
// redirect STDERR to null with 2>/dev/null becasue it outputs plenty of irrelevant warnings (hopefully nothing critical)
`$quotedCommand $arg $quotedFilename 2>/dev/null`;
// Read the out file
$pdf = file_get_contents($filename);
// Delete the outfile
unlink($filename);
return $pdf;
}
/**
* @param $url
* @return bool
*/
private function webservice( $url )
{
global $tikilib;
$target = $this->location . '?' . $url;
return $tikilib->httprequest($target);
}
}