1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* drush integration for print_pdf module PDF libraries download.
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* The PDF project download URL
|
10
|
*/
|
11
|
|
12
|
// TCPDF is in sourceforge, and nicely provides a link to the latest version
|
13
|
define('TCPDF_DOWNLOAD_URI', 'http://sourceforge.net/projects/tcpdf/files/latest');
|
14
|
|
15
|
// URI to the the latest dompdf version.. Hardcoded version unfortunately
|
16
|
define('DOMPDF_DOWNLOAD_URI', 'http://dompdf.googlecode.com/files/dompdf_0-6-0_beta3.tar.gz');
|
17
|
|
18
|
// wkhtmltopdf is a binary, requiring a different download for each platform
|
19
|
define('WKHTMLTOPDF_AMD64_DOWNLOAD_URI', 'http://wkhtmltopdf.googlecode.com/files/wkhtmltopdf-0.11.0_rc1-static-amd64.tar.bz2');
|
20
|
define('WKHTMLTOPDF_I386_DOWNLOAD_URI', 'http://wkhtmltopdf.googlecode.com/files/wkhtmltopdf-0.11.0_rc1-static-i386.tar.bz2');
|
21
|
define('WKHTMLTOPDF_WIN_DOWNLOAD_URI', 'http://wkhtmltopdf.googlecode.com/files/wkhtmltox-0.11.0_rc1-installer.exe');
|
22
|
define('WKHTMLTOPDF_OSX_DOWNLOAD_URI', 'http://wkhtmltopdf.googlecode.com/files/wkhtmltopdf-OSX-0.10.0_rc2-static.tar.bz2');
|
23
|
|
24
|
/**
|
25
|
* Implements hook_drush_command().
|
26
|
*/
|
27
|
function print_pdf_drush_command() {
|
28
|
$items = array();
|
29
|
|
30
|
$items['print-pdf-download'] = array(
|
31
|
'description' => 'Download a PDF library.',
|
32
|
'arguments' => array(
|
33
|
'library' => dt('The PDF library to download. Either tcpdf, dompdf or wkhtmltopdf.'),
|
34
|
),
|
35
|
'options' => array(
|
36
|
'path' => dt('A path to the download folder. If omitted Drush will use the default location (@path).', array('@path' => 'sites/all/libraries')),
|
37
|
),
|
38
|
'aliases' => array('pdfdl'),
|
39
|
'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_ROOT, // No site or config needed.
|
40
|
);
|
41
|
|
42
|
return $items;
|
43
|
}
|
44
|
|
45
|
/**
|
46
|
* Download and extract PDF archive.
|
47
|
*/
|
48
|
function drush_print_pdf_download($library) {
|
49
|
if (isset($library)) {
|
50
|
$download_url = _drush_print_pdf_download_url($library);
|
51
|
if ($download_url) {
|
52
|
$path = drush_get_option('path');
|
53
|
if (empty($path)) {
|
54
|
$path = drush_get_context('DRUSH_DRUPAL_ROOT') . '/sites/all/libraries';
|
55
|
}
|
56
|
|
57
|
// Create the path if it does not exist.
|
58
|
if (!is_dir($path)) {
|
59
|
drush_op('mkdir', $path);
|
60
|
drush_log(dt('Directory @path was created', array('@path' => $path)), 'notice');
|
61
|
}
|
62
|
|
63
|
// Chdir to the download location.
|
64
|
$olddir = getcwd();
|
65
|
drush_op('chdir', $path);
|
66
|
|
67
|
// Warn about an existing dir
|
68
|
if (is_dir($library)) {
|
69
|
// drush_op('rmdir', $library); // Directory must be empty for the php rmdir to work..
|
70
|
drush_log(dt('An existing @library was overwritten at @path', array('@library' => $library, '@path' => $path . '/' . $library)), 'notice');
|
71
|
}
|
72
|
|
73
|
// Download the archive
|
74
|
$filename = _drush_print_pdf_download_file($download_url);
|
75
|
if ($filename) {
|
76
|
$extract_ret = _drush_print_pdf_download_extract($filename);
|
77
|
if ($extract_ret) {
|
78
|
// Remove the archive
|
79
|
drush_op('unlink', $filename);
|
80
|
drush_log(dt('@file has been downloaded and extracted in @path', array('@file' => $filename, '@path' => $path)), 'success');
|
81
|
}
|
82
|
else {
|
83
|
drush_log(dt('@file has been downloaded to @path, but extract failed. Check that you have the necessary program installed, and if necessary extract it manually.',
|
84
|
array('@file' => $filename, '@path' => $path)), 'warning');
|
85
|
}
|
86
|
}
|
87
|
else {
|
88
|
drush_log(dt('Drush was unable to download @library to @path', array('@library' => $library, '@path' => $path)), 'error');
|
89
|
}
|
90
|
|
91
|
// Set working directory back to the previous working directory.
|
92
|
drush_op('chdir', $olddir);
|
93
|
}
|
94
|
}
|
95
|
else {
|
96
|
drush_log(dt('Please specify a PDF library. Currently supported libraries are dompdf, tcpdf and wkhtmltopdf.'), 'error');
|
97
|
}
|
98
|
}
|
99
|
|
100
|
/**
|
101
|
* Discover the correct URL of the package to download
|
102
|
*/
|
103
|
function _drush_print_pdf_download_url($library) {
|
104
|
$ret = FALSE;
|
105
|
|
106
|
switch (drupal_strtolower($library)) {
|
107
|
case 'dompdf':
|
108
|
$ret = DOMPDF_DOWNLOAD_URI;
|
109
|
break;
|
110
|
case 'tcpdf':
|
111
|
$ret = TCPDF_DOWNLOAD_URI;
|
112
|
break;
|
113
|
case 'wkhtmltopdf':
|
114
|
switch (drupal_substr(php_uname('s'), 0, 3)) {
|
115
|
case 'Lin':
|
116
|
$ret = (php_uname('m') == 'x86_64') ? WKHTMLTOPDF_AMD64_DOWNLOAD_URI : WKHTMLTOPDF_I386_DOWNLOAD_URI;
|
117
|
break;
|
118
|
case 'Win':
|
119
|
$ret = WKHTMLTOPDF_WIN_DOWNLOAD_URI;
|
120
|
break;
|
121
|
case 'Dar':
|
122
|
$ret = WKHTMLTOPDF_OSX_DOWNLOAD_URI;
|
123
|
break;
|
124
|
default:
|
125
|
drush_log(dt('wkhtmltopdf is not supported in this system, please choose another library.'), 'error');
|
126
|
break;
|
127
|
}
|
128
|
break;
|
129
|
default:
|
130
|
drush_log(dt('Unknown PDF library specified, please use one of the supported PDF libraries.'), 'error');
|
131
|
break;
|
132
|
}
|
133
|
|
134
|
return $ret;
|
135
|
}
|
136
|
|
137
|
/**
|
138
|
* Helper to download and extract the zip/tar archive.
|
139
|
*/
|
140
|
function _drush_print_pdf_download_extract($filename) {
|
141
|
$arch_ret = FALSE;
|
142
|
|
143
|
if (drush_op('is_file', $filename)) {
|
144
|
switch (drush_op('mime_content_type', $filename)) {
|
145
|
case 1:
|
146
|
$arch_ret = TRUE;
|
147
|
break;
|
148
|
case 'application/zip':
|
149
|
// Decompress the zip archive
|
150
|
$arch_ret = drush_shell_exec('unzip -qq -o ' . $filename);
|
151
|
// ZIP archives usually get the access rights wrong
|
152
|
drush_log(dt('@filename is a Zip file. Check the access permissions of the extracted files.', array('@filename' => $filename)), 'warning');
|
153
|
break;
|
154
|
case 'application/x-gzip':
|
155
|
// Decompress the tar gz archive
|
156
|
$arch_ret = drush_shell_exec('tar xzf ' . $filename);
|
157
|
break;
|
158
|
case 'application/x-bzip2':
|
159
|
// Decompress the tar bz2 archive
|
160
|
$arch_ret = drush_shell_exec('tar xjf ' . $filename);
|
161
|
break;
|
162
|
}
|
163
|
}
|
164
|
else {
|
165
|
drush_log(dt('@filename not found.', array('@filename' => $filename)), 'error');
|
166
|
}
|
167
|
|
168
|
return $arch_ret;
|
169
|
}
|
170
|
|
171
|
/**
|
172
|
* Download a file using wget or curl
|
173
|
*
|
174
|
* Adapted from a function in drush/includes/drush.inc to support 302 redirects.
|
175
|
*
|
176
|
* @param string $download_url
|
177
|
* The path to the file to download
|
178
|
*
|
179
|
* @return string
|
180
|
* The filename that was downloaded, or NULL if the file could not be
|
181
|
* downloaded.
|
182
|
*/
|
183
|
function _drush_print_pdf_download_file($download_url) {
|
184
|
$wget_ret = drush_shell_exec("wget -nv --trust-server-names %s", $download_url);
|
185
|
|
186
|
if (!drush_get_context('DRUSH_SIMULATE')) {
|
187
|
if ($wget_ret) {
|
188
|
// Get the filename of the saved file from the output
|
189
|
$wget_out = explode('"', array_shift(drush_shell_exec_output()));
|
190
|
$filename = $wget_out[1];
|
191
|
}
|
192
|
else {
|
193
|
$tempnam = uniqid('drush_print_pdf_');
|
194
|
|
195
|
$curl_ret = drush_shell_exec("curl -s -L -o %s %s -w '%%{url_effective}'", $tempnam, $download_url);
|
196
|
if ($curl_ret) {
|
197
|
// File was donwloaded with the tempname
|
198
|
|
199
|
// Find the effective name
|
200
|
$filename = explode('/', array_shift(drush_shell_exec_output()));
|
201
|
$filename = array_pop($filename);
|
202
|
|
203
|
// Rename file from tempname to effective name
|
204
|
if (!drush_op('rename', $tempnam, './' . $filename)) {
|
205
|
$filename = $tempnam;
|
206
|
}
|
207
|
}
|
208
|
else {
|
209
|
$filename = FALSE;
|
210
|
}
|
211
|
}
|
212
|
}
|
213
|
else {
|
214
|
$filename = basename($download_url);
|
215
|
}
|
216
|
|
217
|
return $filename;
|
218
|
}
|