|
1
|
|
|
<?php |
|
2
|
|
|
class wkhtmltoimage { |
|
3
|
|
|
protected $config; |
|
4
|
|
|
protected $headers; |
|
5
|
|
|
protected $cookies; |
|
6
|
|
|
protected $options; |
|
7
|
|
|
|
|
8
|
|
|
public function __construct( $config = array() ) { |
|
9
|
|
|
if (!$config['cmd']) { |
|
10
|
|
|
$config['cmd'] = '/usr/local/bin/wkhtmltoimage --disable-local-file-access '; |
|
11
|
|
|
} |
|
12
|
|
|
|
|
13
|
|
View Code Duplication |
if (!$config['temp']) { |
|
14
|
|
|
$context = pobject::getContext(); |
|
15
|
|
|
$me = $context["arCurrentObject"]; |
|
16
|
|
|
$config['temp'] = $me->store->get_config( "files" ) . "temp/"; |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
$this->config = $config; |
|
20
|
|
|
$this->options = array( |
|
21
|
|
|
'format' => 'png' |
|
22
|
|
|
); |
|
23
|
|
|
$this->cookies = array(); |
|
24
|
|
|
$this->headers = array(); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
public function generateFromURL( $url ) { |
|
29
|
|
|
if ( !preg_match( '|^https?://|', $url ) ) { |
|
30
|
|
|
return ar_error::raiseError( "wkhtmltoimage: '$url' is not a valid URL", 201 ); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
$url = escapeshellarg( $url ); |
|
34
|
|
|
$tempFile = tempnam( $this->config['temp'], 'pdf' ); |
|
35
|
|
|
if ( !$tempFile ) { |
|
36
|
|
|
return ar_error::raiseError( "wkhtmltoimage: could not create a temporary file", 202 ); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
$execString = $this->config['cmd']; |
|
40
|
|
View Code Duplication |
foreach ($this->options as $name => $value) { |
|
41
|
|
|
if ( is_bool( $name ) ) { |
|
42
|
|
|
$execString .= " --$name"; |
|
43
|
|
|
} else { |
|
44
|
|
|
$execString .= " --$name " . escapeshellarg( $value ); |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
View Code Duplication |
foreach ($this->cookies as $name => $value) { |
|
49
|
|
|
$execString .= " --cookie " . escapeshellarg( $name ) . " " . escapeshellarg( $value ); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
View Code Duplication |
foreach ($this->headers as $name => $value) { |
|
53
|
|
|
$execString .= " --custom-header " . escapeshellarg( $name ) . " " . escapeshellarg( $value ); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
$execString .= " $url $tempFile"; |
|
57
|
|
|
$execOutput = array(); |
|
58
|
|
|
$execResult = 0; |
|
59
|
|
|
|
|
60
|
|
|
exec( $execString, $execOutput, $execResult ); |
|
61
|
|
View Code Duplication |
if ( $execResult != 0 && $execResult != 2 ) { // code 2 is for 404's encountered |
|
|
|
|
|
|
62
|
|
|
@unlink( $tempFile ); |
|
63
|
|
|
return ar_error::raiseError( "wkhtmltoimage: error ($execResult) while trying to generate image: " . implode( "\n", (array) $execOutput ), 203 ); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
readfile( $tempFile ); |
|
67
|
|
|
unlink( $tempFile ); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function setCookieList( $cookieList = array() ) { |
|
71
|
|
|
if ( is_array($cookieList) ) { |
|
72
|
|
|
foreach( $cookieList as $name => $value) { |
|
73
|
|
|
$this->setOption( $name, $value ); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function setCookie($name, $value = null) { |
|
79
|
|
|
$this->cookies[ $name ] = $value; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function setHeaderList( $headerList = array() ) { |
|
83
|
|
|
if ( is_array($headerList) ) { |
|
84
|
|
|
foreach( $headerList as $name => $value) { |
|
85
|
|
|
$this->setHeader( $name, $value ); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
|
|
91
|
|
|
public function setHeader($name, $value = null) { |
|
92
|
|
|
$this->headers[ $name ] = $value; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
|
|
96
|
|
|
public function setOptionList( $optionList = array() ) { |
|
97
|
|
|
if ( is_array($optionList) ) { |
|
98
|
|
|
foreach( $optionList as $name => $value) { |
|
99
|
|
|
$this->setOption( $name, $value ); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
public function setOption($name, $value = null) { |
|
105
|
|
|
if ($value === null) { |
|
106
|
|
|
unset( $this->options[ $name ] ); |
|
107
|
|
|
return true; |
|
108
|
|
|
} |
|
109
|
|
|
switch ($name) { |
|
110
|
|
|
case 'ignore-load-errors': |
|
111
|
|
|
$this->options[ $name ] = true; |
|
112
|
|
|
break; |
|
113
|
|
|
case 'format': |
|
114
|
|
|
case 'encoding': |
|
115
|
|
|
case 'username': |
|
116
|
|
|
case 'password': |
|
117
|
|
|
case 'title': |
|
118
|
|
|
$this->options[ $name ] = (string) $value; |
|
119
|
|
|
break; |
|
120
|
|
|
case 'zoom': |
|
121
|
|
|
$this->options[ $name ] = (float) $value; |
|
122
|
|
|
break; |
|
123
|
|
|
default: |
|
124
|
|
|
return false; |
|
125
|
|
|
} |
|
126
|
|
|
return true; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
|
|
132
|
|
|
class pinp_wkhtmltoimage { |
|
133
|
|
|
private $instance; |
|
134
|
|
|
|
|
135
|
|
|
public function __construct( $options = array() ) { |
|
136
|
|
|
$this->instance = new wkhtmltoimage(); |
|
137
|
|
|
$this->instance->setOptionList( $options ); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
public function _generateFromURL( $url ) { |
|
141
|
|
|
return $this->instance->generateFromURL( $url ); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
static function _get( $options = array() ) { |
|
145
|
|
|
return new pinp_wkhtmltoimage( $options ); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
|
|
149
|
|
|
|
|
150
|
|
|
} |
|
151
|
|
|
|