-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathddnotes.php
More file actions
589 lines (489 loc) · 18.3 KB
/
Copy pathddnotes.php
File metadata and controls
589 lines (489 loc) · 18.3 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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
<?php
declare(strict_types=1);
/**
* DonDominio Notes
*
* @version 1.0
* @author Soluciones Corporativas IP S.L.
* @copyright Copyright (c) 2021, SCIP
* @license Apache-2
*/
class ddnotes extends rcube_plugin
{
public $task = ".*";
private $config = [];
private $user_id;
public static $default_date_format = "d/m/Y H:i";
/**
* The corresponding usable skin path for this plugin.
*
* @var string like "skins/larry"
*/
private $skinPath = '';
/**
* The corresponding usable skin name for this plugin.
*
* @var string like "larry"
*/
private $skinName = '';
/**
* Plugin initialization
*/
public function init(): void
{
$rcmail = \rcmail::get_instance();
$this->user_id = (int) $rcmail->user->ID;
$this->skinPath = $this->local_skin_path();
// Include CSS for taskbar icon
$this->include_stylesheet($this->local_skin_path() . "/css/main.css");
// remove prefixed "skins/"
$this->skinName = substr($this->skinPath, 6);
// Main task /?task=ddnotes
$this->register_task("ddnotes");
// include localization
$this->add_texts('/localization/', true);
$this->add_button([
"command" => "ddnotes",
"type" => "link",
"id" => "ddnotes",
"label" => "ddnotes.taskbar_button",
"class" => "button-notes",
"classsel" => "button-notes button-selected",
"innerclass" => "button-inner",
], "taskbar");
if ($rcmail->task === "ddnotes") {
// Init plugin configuration and includes
$this->init_config();
$this->init_includes();
// Loads the main page of the plugin
$this->register_action("index", [$this, "render"]);
/**
* Ajax actions
*/
// Action to load an attachment /?task=ddnotes&_action=view&_uid=ATTACHMENTID
$this->register_action("view", [$this, "view"]);
// Action to upload a file note (.pdf, .txt or images files)
$this->register_action("upload_file", [$this, "upload"]);
// Image insertion via editor
$this->register_action("embed", [$this, "embed"]);
// Retrieve notes list
$this->register_action("refresh_list", [$this, "refresh_list"]);
/**
* Register <roundcube:objects /> from html
*/
$rcmail->output->add_handlers([
"ddnotes_creation_submenu" => [$this, "creation_submenu"],
]);
/**
* Notes ajax actions (CRUD)
*/
$this->register_action("new", [$this, "new"]);
$this->register_action("list", [$this, "list"]);
$this->register_action("show", [$this, "show"]);
$this->register_action("update", [$this, "update"]);
$this->register_action("delete", [$this, "delete"]);
/**
* CSS includes
*/
$this->include_stylesheet("includes/fontawesome/css/all.min.css");
$this->include_stylesheet("includes/tinymde/tiny-mde.min.css");
/**
* JS includes
*/
$this->include_script("includes/tinymde/tiny-mde.min.js");
$this->include_script("includes/marked/marked.min.js");
$this->include_script("includes/DOMPurify/purify.js");
$this->include_script($this->local_skin_path() . "/js/main.js");
}
/**
* Send note as attachment via mail/compose hook
*/
if ($rcmail->task === "mail" && $rcmail->action === "compose") {
// Init plugin configuration and includes
$this->init_config();
$this->init_includes();
$this->add_hook("message_compose", [$this, "message_compose"]);
}
}
/**
* Include /includes/ folder to includes path
*
* @return void
*/
public function init_includes(): void
{
// Add include path for classes
$include_path = $this->home . '/includes' . PATH_SEPARATOR;
$include_path .= ini_get('include_path');
set_include_path($include_path);
}
public function init_config(): void
{
$rcmail = \rcmail::get_instance();
// load the plugin configuration
if (!$this->load_config("defaults.inc.php")) {
\rcube::raise_error(
[
"code" => 500,
"type" => "config",
"file" => "DDNotes plugin. File defaults.inc.php not loaded correctly",
"line" => __LINE__ - 5,
"message" => $rcmail->gettext("no_config", "ddnotes")
],
true,
true
);
}
// Loads local config in case it exists
$this->load_config("config.inc.php");
$this->config = \rcmail::get_instance()->config->get("ddnotes_config");
// Server upload_max_filesize VS DDNotes upload_max_filesize
$ddnotes_size = $this->config["upload_max_filesize"] ?? 0;
$app_size = \rcube_utils::max_upload_size();
$app_size = \rcube_utils::max_upload_size();
if ($ddnotes_size && $ddnotes_size < $app_size) {
$app_size = $ddnotes_size;
}
$this->config["upload_max_filesize"] = $app_size;
\rcmail::get_instance()->output->set_env("ddnotes_config", $this->config);
}
/**
* Check if the size is greater than the supported size from the app
*
* @param Int $file_size
* @return boolean
*/
public function check_max_upload($file_size): bool
{
return $this->config["upload_max_filesize"] >= $file_size;
}
/**
* Check for note max_size
*
* @param Int $note_content
* @return boolean
*/
public function check_max_note_size($content_size): bool
{
$note_size = $this->config["note_max_filesize"];
if (is_null($note_size)) {
return $this->check_max_upload($content_size);
}
return $note_size >= $content_size;
}
/**
* Generate creation submenu html and commands
*
* @return string
*/
public function creation_submenu($attrib): string
{
$rcmail = \rcmail::get_instance();
$text_formats = $this->config["extensions"]["text"];
$items = [];
$list_class = "menu listing";
$items_class = "create active";
if ($this->skinName === "larry") {
$list_class = "toolbarmenu";
$items_class = "active";
}
// $rcmail->output->button() already creates <li> wrapper for the button (link-menuitem)
foreach ($text_formats as $format) {
$items[] = $rcmail->output->button([
"command" => "new_note",
"label" => "ddnotes.$format",
"class" => $items_class,
"type" => "link-menuitem",
"prop" => "text/" . $format,
]);
}
return \html::div(
["id" => $attrib["name"], "class" => "popupmenu", "aria-hidden" => "true", "popup-pos" => "down"],
\html::tag("ul", ["role" => "menu", "class" => $list_class], implode("", $items))
);
}
/**
* Load template index
*
* @return void
*/
public function render(): void
{
$rcmail = \rcmail::get_instance();
$rcmail->output->set_pagetitle($this->gettext("browser_title"));
$rcmail->output->send("ddnotes.index");
}
/**
* Loads and attachment
*
* @return void
*/
public function view(): void
{
$rcmail = \rcmail::get_instance();
$id = (int) \rcube_utils::get_input_value("_uid", \rcube_utils::INPUT_GP);
$note = \ddnotes_model::findOneById($id, (int) $this->user_id);
// If note not found, send a 404 headers
if (is_null($note)) {
header("HTTP/1.0 404 Not Found");
exit(0);
}
header("Content-Description: Show Attachment");
header("Content-Disposition: inline; filename=\"" . $note->getTitle() . "." . $note->getExtension() . "\"");
header("Content-Type: " . $note->getMimeType() . "; charset=" . $rcmail->output->get_charset());
header("Content-Length: " . $note->getFileSize());
header("Last-Modified: " . $note->getUpdated()->setTimezone(new DateTimeZone('UTC'))->format("D, d M Y H:i:s \G\M\T"));
if ($note->isText()) {
echo $note->getContent();
} else {
echo base64_decode($note->getContent());
}
exit(0);
}
/**
* Generate a new Note
*
* @return void
*/
public function new(): void
{
$rcmail = \rcmail::get_instance();
$type = \rcube_utils::get_input_value("type", \rcube_utils::INPUT_POST);
$note = new \ddnotes_model();
$response = new \ddnotes_response();
if (is_null($type) || empty($type)) {
$type = "text/" . $this->config["extensions"]["text"][0];
}
$note->setTitle($rcmail->gettext("new_note", "ddnotes"));
$note->setMimeType($type);
$note = $note->save();
$response->setFromNote($note);
$rcmail->output->command("plugin.new", $response);
}
/**
* List all notes from a user
* If you pass a Note ID, it will be send back
* to the frontend in order to select that
* note directly on the list
*
* @return void
*/
public function list(): void
{
$rcmail = \rcmail::get_instance();
$id = (int) \rcube_utils::get_input_value("id", \rcube_utils::INPUT_POST);
$response = new \ddnotes_response();
$notes = \ddnotes_model::find((int) $this->user_id);
if (empty($notes)) {
// $response->setResult(false);
$rcmail->output->command("plugin.list", $response);
return;
}
if ($id === 0) {
$response->setId($notes[0]->getId());
} else {
$response->setId($id);
}
foreach ($notes as $note) {
$response->addFromNote($note);
}
$rcmail->output->command("plugin.list", $response);
}
/**
* Retrieve a note from the database
*
* @return void
*/
public function show(): void
{
$rcmail = \rcmail::get_instance();
$id = (int) \rcube_utils::get_input_value("id", \rcube_utils::INPUT_POST);
$response = new \ddnotes_response();
$note = \ddnotes_model::findOneById($id, (int) $this->user_id);
if (is_null($note)) {
$rcmail->output->show_message("ddnotes.no_note", "error");
return;
}
$response->setFromNote($note);
$rcmail->output->command("plugin.show", $response);
}
/**
* Update note information
*
* @return void
*/
public function update(): void
{
$rcmail = \rcmail::get_instance();
$id = (int) \rcube_utils::get_input_value("id", \rcube_utils::INPUT_POST);
$title = \rcube_utils::get_input_value("name", \rcube_utils::INPUT_POST);
$content = \rcube_utils::get_input_value("content", \rcube_utils::INPUT_POST, true);
$response = new \ddnotes_response();
$note = \ddnotes_model::findOneById($id, (int) $this->user_id);
// Check for note
if (is_null($note)) {
$response->setResult(false);
$response->setId($id);
$rcmail->output->show_message("ddnotes.no_note", "error");
$rcmail->output->command("plugin.update", $response);
return;
}
// Check note size
if (!$this->check_max_note_size(strlen($content))) {
$response->setResult(false);
$response->setId($id);
$rcmail->output->show_message("ddnotes.note_size_error", "error");
$rcmail->output->command("plugin.update", $response);
return;
}
$note->setTitle($title);
$note->setContent($content);
/**
* if it's not a file, size is not defined correctly
*/
if ($note->isText()) {
$note->setFileSize(strlen($content));
}
$note = $note->save();
$response->setFromNote($note);
$rcmail->output->show_message("ddnotes.update_confirmation", "confirmation");
$rcmail->output->command("plugin.update", $response);
}
/**
* Delete a note
*
* @return void
*/
public function delete(): void
{
$rcmail = \rcmail::get_instance();
$id = (int) \rcube_utils::get_input_value("id", \rcube_utils::INPUT_POST);
$response = new \ddnotes_response();
$note = \ddnotes_model::findOneById($id, (int) $this->user_id);
if (is_null($note)) {
$rcmail->output->show_message("ddnotes.no_note", "error");
$rcmail->output->command("plugin.delete", $response);
return;
}
$note->delete();
$rcmail->output->show_message("ddnotes.delete_confirmation", "confirmation");
$rcmail->output->command("plugin.delete", $response);
}
public function upload(): void
{
$rcmail = \rcmail::get_instance();
$title = \rcube_utils::get_input_value("title", \rcube_utils::INPUT_POST);
$mimetype = \rcube_utils::get_input_value("mime", \rcube_utils::INPUT_POST);
$content = \rcube_utils::get_input_value("content", \rcube_utils::INPUT_POST);
$size = (int) \rcube_utils::get_input_value("size", \rcube_utils::INPUT_POST);
$response = new \ddnotes_response();
$note = new \ddnotes_model();
// Check mimetype
if (!\ddnotes_model::isValidMime($mimetype)) {
$response->setResult(false);
$rcmail->output->show_message("ddnotes.invalid_type", "error");
$rcmail->output->command("plugin.uploaded", $response);
return;
}
// Check for file size
if (!$this->check_max_upload($size)) {
$response->setResult(false);
$rcmail->output->show_message("ddnotes.file_size_error", "error");
$rcmail->output->command("plugin.uploaded", $response);
return;
}
// Check for content size
if (!$this->check_max_note_size(strlen($content))) {
$response->setResult(false);
$rcmail->output->show_message("ddnotes.note_size_error", "error");
$rcmail->output->command("plugin.uploaded", $response);
return;
}
$note->setTitle($title);
$note->setMimeType($mimetype);
$note->setFileSize($size);
$note->setContent($content);
$note = $note->save();
$response->setFromNote($note);
$rcmail->output->show_message("ddnotes.create_confirmation", "confirmation");
$rcmail->output->command("plugin.uploaded", $response);
}
public function embed(): void
{
$rcmail = \rcmail::get_instance();
header("Content-Type: application/json; charset=" . $rcmail->output->get_charset());
$id = (int) \rcube_utils::get_input_value("id", \rcube_utils::INPUT_POST);
$title = \rcube_utils::get_input_value("title", \rcube_utils::INPUT_POST);
$mimetype = \rcube_utils::get_input_value("mime", \rcube_utils::INPUT_POST);
$content = \rcube_utils::get_input_value("content", \rcube_utils::INPUT_POST);
$size = (int) \rcube_utils::get_input_value("size", \rcube_utils::INPUT_POST);
$response = new \ddnotes_response();
// Check mimetype
if (!\ddnotes_model::isValidMime($mimetype)) {
$response->setResult(false);
$response->setError("invalid_type");
echo json_encode($response);
exit;
}
// Check for file size
if (!$this->check_max_upload($size)) {
$response->setResult(false);
$response->setError("file_size_error");
echo json_encode($response);
exit;
}
// Check for content size
if (!$this->check_max_note_size($content)) {
$response->setResult(false);
$response->setError("note_size_error");
echo json_encode($response);
exit;
}
$embed = new \ddnotes_model();
$embed->setParentId($id);
$embed->setTitle($title);
$embed->setMimeType($mimetype);
$embed->setFileSize($size);
$embed->setContent($content);
$embed = $embed->save();
$response->setFromNote($embed);
echo json_encode(array_merge($response->toArray(), ["link" => $rcmail->url(["_action" => "view", "_uid" => $embed->getId()])]));
exit;
}
public function refresh_list(): void
{
$rcmail = \rcmail::get_instance();
$response = new \ddnotes_response();
$notes = \ddnotes_model::find((int) $this->user_id);
foreach ($notes as $note) {
$response->addFromNote($note);
}
header("Content-Type: application/json; charset=" . $rcmail->output->get_charset());
echo json_encode($response->toArray());
exit;
}
public function message_compose($args): array
{
$id = (int) ($args["param"]["ddnotes_id"] ?? 0);
if ($id <= 0) {
return $args;
}
$subject = $this->gettext("compose_subject");
$note = \ddnotes_model::findOneById($id, (int) $this->user_id);
if ($note instanceof \ddnotes_model) {
if ($note->isText()) {
$content = $note->getContent();
} else {
$content = base64_decode($note->getContent());
}
$args["attachments"][] = [
"name" => $note->getTitle() . "." . $note->getExtension(),
"mimetype" => $note->getMimeType(),
"data" => $content,
"size" => $note->getFileSize()
];
$args["param"]["subject"] = $subject;
}
return $args;
}
}