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\ORM\DataObject; |
9
|
|
|
use ZipArchive; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Utility class hiding the specifics of the document conversion process. |
13
|
|
|
*/ |
14
|
|
|
class DocumentConverter { |
15
|
|
|
|
16
|
|
|
use Configurable; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var array Docvert connection details |
20
|
|
|
* @config |
21
|
|
|
*/ |
22
|
|
|
private static $docvert_details = [ |
|
|
|
|
23
|
|
|
'username' => '', |
24
|
|
|
'password' => '', |
25
|
|
|
'url' => '' |
26
|
|
|
]; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Associative array of: |
30
|
|
|
* - name: the full name of the file including the extension. |
31
|
|
|
* - path: the path to the file on the local filesystem. |
32
|
|
|
* - mimeType |
33
|
|
|
*/ |
34
|
|
|
protected $fileDescriptor; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var int |
38
|
|
|
* ID of a SilverStripe\Assets\Folder |
39
|
|
|
*/ |
40
|
|
|
protected $chosenFolderID; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var array instance specific connection details |
44
|
|
|
* initially filled with the config settings |
45
|
|
|
*/ |
46
|
|
|
protected $docvertDetails = [ |
47
|
|
|
'username' => '', |
48
|
|
|
'password' => '', |
49
|
|
|
'url' => '' |
50
|
|
|
]; |
51
|
|
|
|
52
|
|
|
public function __construct($fileDescriptor, $chosenFolderID = null) { |
53
|
|
|
$this->fileDescriptor = $fileDescriptor; |
54
|
|
|
$this->chosenFolderID = $chosenFolderID; |
55
|
|
|
array_merge($this->docvertDetails, (array)$this->config()->get('docvert_details')); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function setDocvertUsername($username = null) { |
59
|
|
|
$this->docvertDetails['username'] = $username; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function getDocvertUsername() { |
63
|
|
|
return $this->docvertDetails['username']; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function setDocvertPassword($password = null) { |
67
|
|
|
$this->docvertDetails['password'] = $password; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function getDocvertPassword() { |
71
|
|
|
return $this->docvertDetails['password']; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function setDocvertUrl($url = null) { |
75
|
|
|
$this->docvertDetails['url'] = $url; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function getDocvertUrl() { |
79
|
|
|
return $this->docvertDetails['url']; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function import() { |
83
|
|
|
$ch = curl_init(); |
84
|
|
|
|
85
|
|
|
// PHP 5.5+ introduced CURLFile which makes the '@/path/to/file' syntax deprecated. |
86
|
|
|
if(class_exists('CURLFile')) { |
87
|
|
|
$file = new CURLFile( |
88
|
|
|
$this->fileDescriptor['path'], |
89
|
|
|
$this->fileDescriptor['mimeType'], |
90
|
|
|
$this->fileDescriptor['name'] |
91
|
|
|
); |
92
|
|
|
} else { |
93
|
|
|
$file = '@' . $this->fileDescriptor['path']; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
curl_setopt_array($ch, array( |
97
|
|
|
CURLOPT_URL => $this->getDocvertUrl(), |
98
|
|
|
CURLOPT_USERPWD => sprintf('%s:%s', $this->getDocvertUsername(), $this->getDocvertPassword()), |
99
|
|
|
CURLOPT_POST => 1, |
100
|
|
|
CURLOPT_POSTFIELDS => array('file' => $file), |
101
|
|
|
CURLOPT_CONNECTTIMEOUT => 25, |
102
|
|
|
CURLOPT_TIMEOUT => 100, |
103
|
|
|
)); |
104
|
|
|
|
105
|
|
|
$chosenFolder = ($this->chosenFolderID) ? DataObject::get_by_id(Folder::class, $this->chosenFolderID) : null; |
106
|
|
|
$folderName = ($chosenFolder) ? '/' . $chosenFolder->Name : ''; |
107
|
|
|
$outname = tempnam(ASSETS_PATH, 'convert'); |
108
|
|
|
$outzip = $outname . '.zip'; |
109
|
|
|
|
110
|
|
|
$out = fopen($outzip, 'w'); |
111
|
|
|
curl_setopt($ch, CURLOPT_FILE, $out); |
112
|
|
|
$returnValue = curl_exec($ch); |
113
|
|
|
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
114
|
|
|
curl_close($ch); |
115
|
|
|
fclose($out); |
|
|
|
|
116
|
|
|
chmod($outzip, 0666); |
117
|
|
|
|
118
|
|
|
if (!$returnValue || ($status != 200)) { |
119
|
|
|
return array('error' => _t( |
120
|
|
|
__CLASS__ . '.SERVERUNREACHABLE', |
121
|
|
|
'Could not contact document conversion server. Please try again later or contact your system administrator.', |
122
|
|
|
'Document Converter process Word documents into HTML.' |
123
|
|
|
)); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
// extract the converted document into assets |
127
|
|
|
// you need php zip, i.e. port install php5-zip |
128
|
|
|
$zip = new ZipArchive(); |
129
|
|
|
|
130
|
|
|
if($zip->open($outzip)) { |
131
|
|
|
$zip->extractTo(ASSETS_PATH .$folderName); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
// remove temporary files |
135
|
|
|
unlink($outname); |
136
|
|
|
unlink($outzip); |
137
|
|
|
|
138
|
|
|
if (!file_exists(ASSETS_PATH . $folderName . '/index.html')) { |
139
|
|
|
return array('error' => _t( |
140
|
|
|
__CLASS__ . '.PROCESSFAILED', |
141
|
|
|
'Could not process document, please double-check you uploaded a .doc or .docx format.', |
142
|
|
|
'Document Converter processes Word documents into HTML.' |
143
|
|
|
)); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
$content = file_get_contents(ASSETS_PATH . $folderName . '/index.html'); |
147
|
|
|
|
148
|
|
|
unlink(ASSETS_PATH . $folderName . '/index.html'); |
149
|
|
|
|
150
|
|
|
return $content; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
} |
154
|
|
|
|