-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcloudview.php
More file actions
414 lines (353 loc) · 15.2 KB
/
cloudview.php
File metadata and controls
414 lines (353 loc) · 15.2 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
<?php
/**
* @version $Id$
* Add a button to open attachments online with Zoho or Pixlr web service
* @author Rene Kanzler <rk (at) cosmomill (dot) de>
*/
class cloudview extends rcube_plugin {
public $task = 'mail|settings';
private $aAttachmentData = array();
private $oMessage;
/**
* Plugin initialization
*/
function init() {
// initialize the rcmail class ##
$oRCmail = rcmail::get_instance();
// Add include path for internal classes ##
$include_path = $this->home . '/lib' . PATH_SEPARATOR;
$include_path .= ini_get('include_path');
set_include_path($include_path);
// include javascript files ##
$this->include_script("js/browserWindowSize.js");
if ($oRCmail->action == 'show' || $oRCmail->action == 'preview') {
$this->add_hook('message_load', array($this, 'messageLoad'));
$this->add_hook('template_object_messagebody', array($this, 'htmlOutput'));
}
else if (!$oRCmail->output->framed && (!$oRCmail->action || $oRCmail->action == 'list')) {
$this->include_script('js/openDocument.js');
}
$this->register_action('plugin.cloudview', array($this, 'viewDocument'));
// preference settings hooks ##
if ($oRCmail->task == 'settings') {
$this->add_hook('preferences_list', array($this, 'preferencesList'));
$this->add_hook('preferences_save', array($this, 'preferencesSave'));
}
}
/**
* Handler for user preferences form (preferences_list hook)
*/
function preferencesList($args) {
// add our new preferences to the server settings page ##
if ($args['section'] != 'server') {
return $args;
}
// initialize the rcmail class ##
$oRCmail = rcmail::get_instance();
// load configuration ##
$this->load_config();
// load localization and configuration ##
$this->add_texts('localization/');
// get disabled configuration parameters ##
$aDontOverride = $oRCmail->config->get('dont_override', array());
// add zoho save url input field ##
if (!in_array('cloudview_zoho_save_url', $aDontOverride)) { // check that our configuration is not disabled ##
$sFieldId = '_cloudview_zoho_save_url';
// get the current value ##
$sZohoSaveUrl = $oRCmail->config->get('cloudview_zoho_save_url');
// crate the input field ##
$oInputField = new html_inputfield( array('name' => $sFieldId, 'id' => $sFieldId, 'size' => 50) );
// add the new input filed to the argument list ##
$args['blocks']['main']['options']['cloudview_zoho_save_url'] = array(
'title' => html::label($sFieldId, Q($this->gettext('zohosaveurl'))),
'content' => $oInputField->show($sZohoSaveUrl),
);
}
// add pixlr save url input field ##
if (!in_array('cloudview_pixlr_save_url', $aDontOverride)) { // check that our configuration is not disabled ##
$sFieldId = '_cloudview_pixlr_save_url';
// get the current value ##
$sPixlrSaveUrl = $oRCmail->config->get('cloudview_pixlr_save_url');
// crate the input field ##
$oInputField = new html_inputfield( array('name' => $sFieldId, 'id' => $sFieldId, 'size' => 50) );
// add the new input filed to the argument list ##
$args['blocks']['main']['options']['cloudview_pixlr_save_url'] = array(
'title' => html::label($sFieldId, Q($this->gettext('pixlrsaveurl'))),
'content' => $oInputField->show($sPixlrSaveUrl),
);
}
// add access key input field ##
if (!in_array('cloudview_access_key', $aDontOverride)) { // check that our configuration is not disabled ##
$sFieldId = '_cloudview_access_key';
// get the current value ##
$sAccessKey = $oRCmail->config->get('cloudview_access_key');
// crate the input field ##
$oInputField = new html_inputfield( array('name' => $sFieldId, 'id' => $sFieldId, 'size' => 50) );
// add the new input filed to the argument list ##
$args['blocks']['main']['options']['cloudview_access_key'] = array(
'title' => html::label($sFieldId, Q($this->gettext('accesskey'))),
'content' => $oInputField->show($sAccessKey),
);
}
// add enable editor check box ##
if (!in_array('cloudview_enable_editor', $aDontOverride)) { // check that our configuration is not disabled ##
$sFieldId = '_cloudview_enable_editor';
// get the current value ##
$bEnableEditor = $oRCmail->config->get('cloudview_enable_editor');
// crate the input field ##
$oCheckBox = new html_checkbox( array('name' => $sFieldId, 'id' => $sFieldId, 'value' => 1) );
// add the new input filed to the argument list ##
$args['blocks']['main']['options']['cloudview_enable_editor'] = array(
'title' => html::label($sFieldId, Q($this->gettext('enableeditor'))),
'content' => $oCheckBox->show($bEnableEditor?1:0),
);
}
return $args;
}
/**
* Handler for user preferences save (preferences_save hook)
*/
function preferencesSave($args) {
// add our new preferences to the server settings page ##
if ($args['section'] != 'server') {
return $args;
}
// initialize the rcmail class ##
$oRCmail = rcmail::get_instance();
// load configuration ##
$this->load_config();
// get disabled configuration parameters ##
$aDontOverride = $oRCmail->config->get('dont_override', array());
// zoho save url ##
if (!in_array('cloudview_zoho_save_url', $aDontOverride)) { // check that our configuration is not disabled ##
// get the current value ##
#$sSaveUrl = $oRCmail->config->get('cloudview_zoho_save_url');
// set the new value ##
$args['prefs']['cloudview_zoho_save_url'] = filter_var($_POST['_cloudview_zoho_save_url'], FILTER_SANITIZE_URL);
}
// pixlr save url ##
if (!in_array('cloudview_pixlr_save_url', $aDontOverride)) { // check that our configuration is not disabled ##
// get the current value ##
#$sSaveUrl = $oRCmail->config->get('cloudview_pixlr_save_url');
// set the new value ##
$args['prefs']['cloudview_pixlr_save_url'] = filter_var($_POST['_cloudview_pixlr_save_url'], FILTER_SANITIZE_URL);
}
// access key ##
if (!in_array('cloudview_access_key', $aDontOverride)) { // check that our configuration is not disabled ##
// get the current value ##
#$sAccessSecret = $oRCmail->config->get('cloudview_access_key');
// set the new value ##
$args['prefs']['cloudview_access_key'] = filter_var($_POST['_cloudview_access_key'], FILTER_SANITIZE_STRING);
}
// enable editor ##
if (!in_array('cloudview_enable_editor', $aDontOverride)) { // check that our configuration is not disabled ##
// get the current value ##
#$bEnableEditor = $oRCmail->config->get('cloudview_enable_editor');
// set the new value ##
$args['prefs']['cloudview_enable_editor'] = isset($_POST['_cloudview_enable_editor']) ? true : false;
}
return $args;
}
/**
* Check message bodies and attachments for supported documents
*/
function messageLoad($p)
{
$this->oMessage = $p['object'];
// handle attachments ##
foreach ( (array)$this->oMessage->attachments as $oAttachment ) {
$sMimeType = $this->isSupportedDoc($oAttachment);
if ($sMimeType) { // if $sMimeType isset we support the document ##
$this->aAttachmentData[] = array('mime_id' => $oAttachment->mime_id,
'mimetype' => $sMimeType,
'filename' => $oAttachment->filename
);
}
}
// debug stuff ##
#var_dump($this->oMessage->attachments);
if ($this->aAttachmentData)
$this->add_texts('localization');
}
/**
* This callback function adds a box below the message content
* if there is a supported document available
*/
function htmlOutput($p) {
$bAttachScript = false;
$bAttachPdfScript = false;
foreach ($this->aAttachmentData as $aDocumentInfo) {
$aJsonDocument[document] = $aDocumentInfo;
$style = 'margin:0.5em 1em; padding:0.2em 0.5em; border:1px solid #999; '
.'border-radius:4px; -moz-border-radius:4px; -webkit-border-radius:4px; width: auto';
if (mimeHelper::isMimeTypeText($aDocumentInfo['mimetype'])) {
$icon = 'plugins/cloudview/' .$this->local_skin_path(). '/x-office-document.png';
// add box below message body ##
$p['content'] .= html::p(array('style' => $style),
html::a(array('href' => "#",
'onclick' => "return plugin_cloudview_view_document('".JQ(json_encode($aJsonDocument))."')",
'title' => $this->gettext('opendocument')
),
html::img(array('src' => $icon,
'style' => 'vertical-align:middle'
)
)
) . ' ' . html::span(null, Q($aDocumentInfo['filename']))
);
$bAttachScript = true;
}
if (mimeHelper::isMimeTypeSpreadsheet($aDocumentInfo['mimetype'])) {
$icon = 'plugins/cloudview/' .$this->local_skin_path(). '/x-office-spreadsheet.png';
// add box below message body ##
$p['content'] .= html::p(array('style' => $style),
html::a(array('href' => "#",
'onclick' => "return plugin_cloudview_view_document('".JQ(json_encode($aJsonDocument))."')",
'title' => $this->gettext('opendocument')
),
html::img(array('src' => $icon,
'style' => 'vertical-align:middle'
)
)
) . ' ' . html::span(null, Q($aDocumentInfo['filename']))
);
$bAttachScript = true;
}
if(mimeHelper::isMimeTypePresentation($aDocumentInfo['mimetype'])) {
$icon = 'plugins/cloudview/' .$this->local_skin_path(). '/x-office-presentation.png';
// add box below message body ##
$p['content'] .= html::p(array('style' => $style),
html::a(array('href' => "#",
'onclick' => "return plugin_cloudview_view_document('".JQ(json_encode($aJsonDocument))."')",
'title' => $this->gettext('opendocument')
),
html::img(array('src' => $icon,
'style' => 'vertical-align:middle'
)
)
) . ' ' . html::span(null, Q($aDocumentInfo['filename']))
);
$bAttachScript = true;
}
if(mimeHelper::isMimeTypeImage($aDocumentInfo['mimetype'])) {
$icon = 'plugins/cloudview/' .$this->local_skin_path(). '/image-x-generic.png';
// add box below message body ##
$p['content'] .= html::p(array('style' => $style),
html::a(array('href' => "#",
'onclick' => "return plugin_cloudview_view_document('".JQ(json_encode($aJsonDocument))."')",
'title' => $this->gettext('opendocument')
),
html::img(array('src' => $icon,
'style' => 'vertical-align:middle'
)
)
) . ' ' . html::span(null, Q($aDocumentInfo['filename']))
);
$bAttachScript = true;
}
if(mimeHelper::isMimeTypePdf($aDocumentInfo['mimetype'])) {
$icon = 'plugins/cloudview/' .$this->local_skin_path(). '/x-application-pdf.png';
// show PDF below message body ##
$p['content'] .= html::div(array('class' => 'pdfviewer-container'),
html::p(array('class' => 'pdfviewer-navigation', 'style' => $style),
html::span(array('style' => 'float:right'),
html::a(array('href' => "#",
'class' => 'svgicon-arrowleft prev-page',
'title' => $this->gettext('previouspage')
), ''
) .
html::span(null,
html::span(array('class' => 'page_num'), '') . ' ' . $this->gettext('pagenrof') . ' ' . html::span(array('class' => 'page_count'), '')
) .
html::a(array('href' => "#",
'class' => 'svgicon-arrowright next-page',
'title' => $this->gettext('nextpage')
), ''
)
) .
html::img(array('src' => $icon,
'style' => 'vertical-align:middle'
)
) . ' ' .
html::span(null, Q($aDocumentInfo['filename']))
) .
html::div(array('class' => 'pdfviewer-viewport'),
html::tag('canvas', array('class' => 'pdfviewer-canvas',
'data-id' => $aDocumentInfo['mime_id']
)
)
)
);
$bAttachPdfScript = true;
}
}
if ($bAttachScript) {
$this->include_script('js/openDocument.js');
}
if ($bAttachPdfScript) {
$this->include_script('js/pdf-min.js');
$this->include_script('js/compatibility-min.js');
$this->include_script('js/showPdf.js');
$this->include_script('js/raphael.js');
$this->include_script('js/svgIcons.js');
}
return $p;
}
/**
* Handler for request action
*/
function viewDocument() {
// tell the plugin API where to search for texts ##
$this->add_texts('localization', true);
// get the post values ##
$sUid = get_input_value('_uid', RCUBE_INPUT_POST);
$aJsonDocument = get_input_value('_info', RCUBE_INPUT_POST);
// initialize the rcmail class ##
$oRCmail = rcmail::get_instance();
// get the attachment as string ##
if ($sUid && $aJsonDocument) {
$aDocumentInfo = json_decode($aJsonDocument, true);
$sDocument = $oRCmail->imap->get_message_part($sUid, $aDocumentInfo['document']['mime_id']);
}
// get Zoho API Key from plugin config ##
if (!defined('zohoAPIkey')) {
$this->load_config();
$sZohoAPIkey = $oRCmail->config->get('zoho_api_key');
define('zohoAPIkey', $sZohoAPIkey);
}
// check if editor mode is enabled ##
$bEnableEditor = $oRCmail->config->get('cloudview_enable_editor');
if ($bEnableEditor) {
$sOpenMode = 'edit';
} else {
$sOpenMode = 'view';
}
// open document with Zoho or Pixlr ##
$aJsonResponse = openDocument::loadDocument($sDocument, $aDocumentInfo['document']['filename'], $aDocumentInfo['document']['mimetype'], $sOpenMode);
// check the result ##
$aResult = json_decode($aJsonResponse, true);
if ($aResult['response']['result'] != 'Success') {
$oRCmail->output->command('display_message', $this->gettext($aResult['response']['errorCode']), 'error');
}
$oRCmail->output->command('plugin.cloudview', array('message' => $aJsonResponse));
$oRCmail->output->send();
}
/**
* Check if specified attachment contains a supported document.
* @param oAttachment The attachment object
* @return boolean True if part is a document supported by Zoho
*/
function isSupportedDoc($oAttachment) {
// use file name suffix with hard-coded mime-type map ##
$aMimeExt = @include(RCMAIL_CONFIG_DIR . '/mimetypes.php');
$sFileSuffix = pathinfo($oAttachment->filename, PATHINFO_EXTENSION);
if (is_array($aMimeExt)) {
$sMimeType = $aMimeExt[$sFileSuffix];
}
if (mimeHelper::isSupportedMimeType($oAttachment->mimetype)) {
return mimeHelper::isSupportedMimeType($oAttachment->mimetype);
} else {
return mimeHelper::isSupportedMimeType($sMimeType);
}
}
}
?>