1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\DocumentConverter; |
4
|
|
|
|
5
|
|
|
use CURLFile; |
6
|
|
|
use SilverStripe\Assets\Folder; |
7
|
|
|
use SilverStripe\Core\Config\Configurable; |
8
|
|
|
use SilverStripe\Core\Environment; |
9
|
|
|
use SilverStripe\Core\Injector\Injectable; |
10
|
|
|
use SilverStripe\ORM\DataObject; |
11
|
|
|
use ZipArchive; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Utility class hiding the specifics of the document conversion process. |
15
|
|
|
*/ |
16
|
|
|
class ServiceConnector |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
use Configurable; |
20
|
|
|
use Injectable; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @config |
24
|
|
|
* @var array Docvert connection username |
25
|
|
|
*/ |
26
|
|
|
private static $username = null; |
|
|
|
|
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @config |
30
|
|
|
* @var array Docvert connection password |
31
|
|
|
*/ |
32
|
|
|
private static $password = null; |
|
|
|
|
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @config |
36
|
|
|
* @var array Docvert service URL |
37
|
|
|
*/ |
38
|
|
|
private static $url = null; |
|
|
|
|
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Associative array of: |
42
|
|
|
* - name: the full name of the file including the extension. |
43
|
|
|
* - path: the path to the file on the local filesystem. |
44
|
|
|
* - mimeType |
45
|
|
|
*/ |
46
|
|
|
protected $fileDescriptor; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var int |
50
|
|
|
* ID of a SilverStripe\Assets\Folder |
51
|
|
|
*/ |
52
|
|
|
protected $chosenFolderID; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var array instance specific connection details |
56
|
|
|
*/ |
57
|
|
|
protected $docvertDetails = [ |
58
|
|
|
'username' => null, |
59
|
|
|
'password' => null, |
60
|
|
|
'url' => null |
61
|
|
|
]; |
62
|
|
|
|
63
|
|
|
public function __construct($fileDescriptor, $chosenFolderID = null) |
64
|
|
|
{ |
65
|
|
|
$this->fileDescriptor = $fileDescriptor; |
66
|
|
|
$this->chosenFolderID = $chosenFolderID; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function setUsername($username = null) |
70
|
|
|
{ |
71
|
|
|
$this->docvertDetails['username'] = $username; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
View Code Duplication |
public function getUsername() |
|
|
|
|
75
|
|
|
{ |
76
|
|
|
$username = $this->docvertDetails['username']; |
77
|
|
|
if ($username) { |
78
|
|
|
return $username; |
79
|
|
|
} |
80
|
|
|
$username = $this->config()->get('username'); |
81
|
|
|
if ($username) { |
82
|
|
|
return $username; |
83
|
|
|
} |
84
|
|
|
$username = Environment::getEnv('DOCVERT_USERNAME'); |
85
|
|
|
if ($username) { |
86
|
|
|
return $username; |
87
|
|
|
} |
88
|
|
|
return null; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function setPassword($password = null) |
92
|
|
|
{ |
93
|
|
|
$this->docvertDetails['password'] = $password; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
View Code Duplication |
public function getPassword() |
|
|
|
|
97
|
|
|
{ |
98
|
|
|
$username = $this->docvertDetails['password']; |
99
|
|
|
if ($username) { |
100
|
|
|
return $username; |
101
|
|
|
} |
102
|
|
|
$username = $this->config()->get('password'); |
103
|
|
|
if ($username) { |
104
|
|
|
return $username; |
105
|
|
|
} |
106
|
|
|
$username = Environment::getEnv('DOCVERT_PASSWORD'); |
107
|
|
|
if ($username) { |
108
|
|
|
return $username; |
109
|
|
|
} |
110
|
|
|
return null; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function setUrl($url = null) |
114
|
|
|
{ |
115
|
|
|
$this->docvertDetails['url'] = $url; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
View Code Duplication |
public function getUrl() |
|
|
|
|
119
|
|
|
{ |
120
|
|
|
$username = $this->docvertDetails['url']; |
121
|
|
|
if ($username) { |
122
|
|
|
return $username; |
123
|
|
|
} |
124
|
|
|
$username = $this->config()->get('url'); |
125
|
|
|
if ($username) { |
126
|
|
|
return $username; |
127
|
|
|
} |
128
|
|
|
$username = Environment::getEnv('DOCVERT_URL'); |
129
|
|
|
if ($username) { |
130
|
|
|
return $username; |
131
|
|
|
} |
132
|
|
|
return null; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function import() |
136
|
|
|
{ |
137
|
|
|
$ch = curl_init(); |
138
|
|
|
|
139
|
|
|
// PHP 5.5+ introduced CURLFile which makes the '@/path/to/file' syntax deprecated. |
140
|
|
|
if (class_exists('CURLFile')) { |
141
|
|
|
$file = new CURLFile( |
142
|
|
|
$this->fileDescriptor['path'], |
143
|
|
|
$this->fileDescriptor['mimeType'], |
144
|
|
|
$this->fileDescriptor['name'] |
145
|
|
|
); |
146
|
|
|
} else { |
147
|
|
|
$file = '@' . $this->fileDescriptor['path']; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
curl_setopt_array($ch, [ |
151
|
|
|
CURLOPT_URL => $this->getUrl(), |
152
|
|
|
CURLOPT_USERPWD => sprintf('%s:%s', $this->getUsername(), $this->getPassword()), |
|
|
|
|
153
|
|
|
CURLOPT_POST => 1, |
154
|
|
|
CURLOPT_POSTFIELDS => ['file' => $file], |
155
|
|
|
CURLOPT_CONNECTTIMEOUT => 25, |
156
|
|
|
CURLOPT_TIMEOUT => 100, |
157
|
|
|
]); |
158
|
|
|
|
159
|
|
|
$chosenFolder = ($this->chosenFolderID) ? DataObject::get_by_id(Folder::class, $this->chosenFolderID) : null; |
160
|
|
|
$folderName = ($chosenFolder) ? '/' . $chosenFolder->Name : ''; |
161
|
|
|
$outname = tempnam(ASSETS_PATH, 'convert'); |
162
|
|
|
$outzip = $outname . '.zip'; |
163
|
|
|
$out = fopen($outzip, 'w'); |
164
|
|
|
curl_setopt($ch, CURLOPT_FILE, $out); |
165
|
|
|
$returnValue = curl_exec($ch); |
166
|
|
|
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
167
|
|
|
curl_close($ch); |
168
|
|
|
fclose($out); |
|
|
|
|
169
|
|
|
chmod($outzip, 0666); |
170
|
|
|
|
171
|
|
|
if (!$returnValue || ($status != 200)) { |
172
|
|
|
return ['error' => _t( |
173
|
|
|
__CLASS__ . '.SERVERUNREACHABLE', |
174
|
|
|
'Could not contact document conversion server. Please try again later ' . |
175
|
|
|
'or contact your system administrator.', |
176
|
|
|
'Document Converter process Word documents into HTML.' |
177
|
|
|
)]; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
// extract the converted document into assets |
181
|
|
|
// you need php zip, e.g. apt-get install php-zip |
182
|
|
|
$zip = new ZipArchive(); |
183
|
|
|
|
184
|
|
|
if ($zip->open($outzip)) { |
185
|
|
|
$zip->extractTo(ASSETS_PATH .$folderName); |
186
|
|
|
$zip->close(); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
// remove temporary files |
190
|
|
|
unlink($outname); |
191
|
|
|
unlink($outzip); |
192
|
|
|
|
193
|
|
|
if (!file_exists(ASSETS_PATH . $folderName . '/index.html')) { |
194
|
|
|
return ['error' => _t( |
195
|
|
|
__CLASS__ . '.PROCESSFAILED', |
196
|
|
|
'Could not process document, please double-check you uploaded a .doc or .docx format.', |
197
|
|
|
'Document Converter processes Word documents into HTML.' |
198
|
|
|
)]; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
$content = file_get_contents(ASSETS_PATH . $folderName . '/index.html'); |
202
|
|
|
|
203
|
|
|
unlink(ASSETS_PATH . $folderName . '/index.html'); |
204
|
|
|
|
205
|
|
|
return $content; |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
|