Proyectos de Subversion LeadersLinked - Services

Rev

Rev 299 | Rev 307 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
 
3
declare(strict_types=1);
4
 
5
namespace LeadersLinked\Controller;
6
 
7
use Laminas\Db\Adapter\AdapterInterface;
8
 
9
use Laminas\Mvc\Controller\AbstractActionController;
10
use Laminas\Log\LoggerInterface;
11
use Laminas\View\Model\ViewModel;
12
 
13
use LeadersLinked\Library\Functions;
14
use LeadersLinked\Mapper\UserMapper;
15
 
16
use LeadersLinked\Model\User;
17
 
18
use LeadersLinked\Mapper\UserBlockedMapper;
19
 
20
use LeadersLinked\Mapper\CompanyMapper;
21
use LeadersLinked\Mapper\CompanyUserMapper;
22
use LeadersLinked\Library\Image;
23
use Laminas\View\Model\JsonModel;
24
use Laminas\Mvc\I18n\Translator;
199 efrain 25
use LeadersLinked\Mapper\AbuseReportMapper;
26
use LeadersLinked\Model\AbuseReport;
261 efrain 27
use LeadersLinked\Library\ExternalCredentials;
28
use LeadersLinked\Library\Storage;
29
use LeadersLinked\Form\InMail\SendForm;
291 www 30
use PHPMailer\PHPMailer\PHPMailer;
295 www 31
use LeadersLinked\Library\S3Files;
1 efrain 32
 
261 efrain 33
 
1 efrain 34
class InMailController extends AbstractActionController
35
{
261 efrain 36
    const _SIZE_PAGE = 10;
283 www 37
 
261 efrain 38
 
1 efrain 39
    /**
40
     *
41
     * @var \Laminas\Db\Adapter\AdapterInterface
42
     */
43
    private $adapter;
44
 
45
    /**
46
     *
47
     * @var \LeadersLinked\Cache\CacheInterface
48
     */
49
    private $cache;
50
 
51
 
52
    /**
53
     *
54
     * @var \Laminas\Log\LoggerInterface
55
     */
56
    private $logger;
57
 
58
    /**
59
     *
60
     * @var array
61
     */
62
    private $config;
63
 
64
 
65
    /**
66
     *
67
     * @var \Laminas\Mvc\I18n\Translator
68
     */
69
    private $translator;
70
 
71
 
72
    /**
73
     *
74
     * @param \Laminas\Db\Adapter\AdapterInterface $adapter
75
     * @param \LeadersLinked\Cache\CacheInterface $cache
76
     * @param \Laminas\Log\LoggerInterface LoggerInterface $logger
77
     * @param array $config
78
     * @param \Laminas\Mvc\I18n\Translator $translator
79
     */
80
    public function __construct($adapter, $cache, $logger, $config, $translator)
81
    {
82
        $this->adapter      = $adapter;
83
        $this->cache        = $cache;
84
        $this->logger       = $logger;
85
        $this->config       = $config;
86
        $this->translator   = $translator;
87
    }
88
 
89
    /**
90
     *
91
     * Generación del listado de conversationes
92
     *
93
     * [
94
     *  success: true,
95
     *  data:[
261 efrain 96
     *    messages :
1 efrain 97
     *    [
98
     *       uuid: uuid con quien se tiene la conversación,
99
     *       name: nombre de con quien se tiene la conversacion,
100
     *       image: imagen de con quien se tiene la conversación,
261 efrain 101
     *       subject: asunto del mensaje
102
     *       date: fecha del ultimo mensaje,
103
     *       seen: true|false,
1 efrain 104
     *       send_link: url para enviar el mensaje,
261 efrain 105
     *       open_link : url para leer el mensaje,
106
     *       delete_link : url para leer el mensaje,
107
     *     ],
108
     *     'total_messages' : total de mensajes,
109
     *     'total_pages' : total de páginas 10 en 10,
110
     *     'current_page' : página actual
1 efrain 111
     *  ]
112
     *
113
     *
114
     * @return \Laminas\View\Model\JsonModel
115
     */
116
    public function indexAction()
117
    {
118
        $request = $this->getRequest();
119
        if ($request->isGet()) {
120
            $currentUserPlugin = $this->plugin('currentUserPlugin');
121
            $currentUser = $currentUserPlugin->getUser();
261 efrain 122
 
123
            $page = intval($this->params()->fromQuery('page', 0), 10);
124
 
283 www 125
            $storage = Storage::getInstance($this->config);
126
 
261 efrain 127
            $externalCredentials = ExternalCredentials::getInstancia($this->config, $this->adapter);
128
            $externalCredentials->getUserBy($currentUser->id);
1 efrain 129
 
261 efrain 130
            $host       = trim($this->config['leaderslinked.inmail.host']);
131
            $port       = intval($this->config['leaderslinked.inmail.port_imap'], 10);
132
            $mailbox    = trim($this->config['leaderslinked.inmail.mailbox']);
199 efrain 133
 
1 efrain 134
 
261 efrain 135
            $username   = $externalCredentials->getUsernameInmail();
136
            $password   =  $externalCredentials->getPasswordInmail();
137
 
138
            $userMapper = \LeadersLinked\Mapper\UserMapper::getInstance($this->adapter);
199 efrain 139
 
261 efrain 140
            try {
141
                \Eden\Core\Control::i();
142
                $client = eden('mail')->imap(
143
                    $host,
144
                    $username,
145
                    $password,
146
                    $port,
147
                    false);
1 efrain 148
 
261 efrain 149
                $client->setActiveMailbox($mailbox);
150
 
151
                //$emails = $imap->getEmails(0, 3);
152
                $maxMessages = $client->getEmailTotal();
1 efrain 153
 
261 efrain 154
                if($maxMessages > self::_SIZE_PAGE) {
155
 
156
 
157
                    $maxPages = (int)  $maxMessages / self::_SIZE_PAGE;
158
                    if(($maxPages * self::_SIZE_PAGE) <  $maxMessages) {
159
                        $maxPages++;
160
                    }
161
                } else {
162
                    $maxPages = 1;
163
                    $page = 1;
164
                }
165
 
166
                $page = $page > 0 ? $page : 1;
167
                $page = $page > $maxPages ? $maxPages : $page;
168
 
169
                if($page == 1) {
170
                    $low = 0;
171
                    $high = self::_SIZE_PAGE - 1;
172
                } else {
173
                    $low = ($page - 1) * self::_SIZE_PAGE;
174
                    $high = $low + ( self::_SIZE_PAGE - 1);
175
                }
176
 
177
                $users = [];
178
                $messages = [];
179
                if( $maxMessages ) {
180
                    $emails = $client->getEmails($low, $high);
181
                    foreach($emails as $email)
182
                    {
199 efrain 183
 
261 efrain 184
 
185
 
186
                        $from = $email['from']['email'];
187
 
188
                        if(isset($users[ $from ])) {
189
                            $user = $users[ $from ];
190
                        } else {
191
                            $user = $userMapper->fetchOneByUsernameInmailAndNetworkId($from, $currentUser->network_id);
192
                            $users[ $from ] = $user;
193
                        }
194
 
195
 
196
                        $seen = false;
197
                        if(is_array($email['flags'])) {
198
                            foreach($email['flags'] as $flag)
199
                            {
200
                                if($flag == 'seen') {
201
                                    $seen = 1;
202
                                    break;
1 efrain 203
                                }
204
                            }
205
                        }
199 efrain 206
 
261 efrain 207
 
199 efrain 208
 
261 efrain 209
                        $message =  [
210
                            'uuid' => $user->uuid,
211
                            'name' => $user->first_name . ' ' . $user->last_name,
283 www 212
                            'image' => $storage->getUserImage($user),
261 efrain 213
                            'subject' => $email['subject'],
214
                            'date' => date('Y-m-d h:i a', $email['date']),
215
                            'seen' => $seen,
283 www 216
                            'email_id' => $email['uid'],
261 efrain 217
                            'block_link' => $this->url()->fromRoute('inmail/user/block',[ 'id' => $user->uuid ], ['force_canonical' => true]),
218
                            'send_link' => $this->url()->fromRoute('inmail/message/send',[ 'id' => $user->uuid ], ['force_canonical' => true]),
219
                            'get_link' =>  $this->url()->fromRoute('inmail/message/get',[ 'id' => $email['uid'] ], ['force_canonical' => true]),
220
                            'delete_link'=>    $this->url()->fromRoute('inmail/message/delete',[ 'id' => $email['uid'] ], ['force_canonical' => true]),
221
                       ];
222
 
223
 
224
                        array_push($messages, $message);
225
 
1 efrain 226
                    }
261 efrain 227
 
1 efrain 228
                }
261 efrain 229
 
230
                $client->disconnect();
231
 
232
                $data = [
233
                    'success' => true,
234
                    'data' => [
235
                        'messages'          => $messages,
236
                        'total_messages'    => $maxMessages,
237
                        'total_pages'       => $maxPages,
238
                        'current_page'      => $page,
239
                    ]
240
                ];
241
 
242
 
243
                return new JsonModel($data);
244
 
245
            } catch (\Exception $e) {
246
 
247
                return new JsonModel([
248
                    'success' => false,
249
                    'data' => 'ERROR_INMAIL_MESSAGE_LIST_UNAVAILABLE'
250
                ]);
251
            }
252
 
253
           /*
254
            *
255
            * (
256
    [0] => Array
257
        (
258
            [id] => <6f27ed6a-d554-4f0b-b9ee-7c708cca8b33@inmail.leaderslinked.com>
259
            [parent] =>
260
            [topic] => Test 1
261
            [mailbox] => INBOX
262
            [uid] => 8
263
            [date] => 1721906994
264
            [subject] => Test 1
265
            [from] => Array
266
                (
267
                    [name] => Efrain Yanez
268
                    [email] => efrain.yanez@inmail.leaderslinked.com
269
                )
1 efrain 270
 
261 efrain 271
            [flags] => Array
272
                (
273
                    [0] => seen
274
                )
1 efrain 275
 
261 efrain 276
            [to] => Array
277
                (
278
                    [0] => Array
279
                        (
280
                            [name] =>
281
                            [email] => santiago.olivera@inmail.leaderslinked.com
282
                        )
1 efrain 283
 
261 efrain 284
                )
1 efrain 285
 
261 efrain 286
            [cc] => Array
287
                (
288
                )
1 efrain 289
 
261 efrain 290
            [bcc] => Array
291
                (
292
                )
1 efrain 293
 
261 efrain 294
            [attachment] => 1
295
 
296
        )
297
        (
298
    [id] => <6f27ed6a-d554-4f0b-b9ee-7c708cca8b33@inmail.leaderslinked.com>
299
    [parent] =>
300
    [topic] => Test 1
301
    [mailbox] => INBOX
302
    [uid] => 8
303
    [date] => 1721906994
304
    [subject] => Test 1
305
    [from] => Array
306
        (
307
            [name] => Efrain Yanez
308
            [email] => efrain.yanez@inmail.leaderslinked.com
309
        )
1 efrain 310
 
261 efrain 311
    [flags] => Array
312
        (
313
            [0] => seen
314
        )
1 efrain 315
 
261 efrain 316
    [to] => Array
317
        (
318
            [0] => Array
319
                (
320
                    [name] =>
321
                    [email] => santiago.olivera@inmail.leaderslinked.com
322
                )
1 efrain 323
 
261 efrain 324
        )
1 efrain 325
 
261 efrain 326
    [cc] => Array
327
        (
328
        )
1 efrain 329
 
261 efrain 330
    [bcc] => Array
331
        (
332
        )
333
[body] => 'esto es una pryeba',
334
    [attachment] => Array
335
        (
336
            [POST 1080X1080-03-37.png] => Array
337
                (
338
                    [image/png] => �PNG
1 efrain 339
 
261 efrain 340
 
341
 
342
 
343
 
344
 
345
        [futbol-720p-230714 - Copy (1).mp4] => Array
346
                (
347
                    [video/mp4]
348
 
349
            *
350
            *
351
            Array
352
(
353
    [0] => __construct
354
    [1] => connect
355
    [2] => disconnect
356
    [3] => getActiveMailbox
357
    [4] => getEmails
358
    [5] => getEmailTotal
359
    [6] => getNextUid
360
    [7] => getMailboxes
361
    [8] => getUniqueEmails
362
    [9] => move
363
    [10] => copy
364
    [11] => remove
365
    [12] => expunge
366
    [13] => search
367
    [14] => searchTotal
368
    [15] => setActiveMailbox
369
    [16] => i
370
    [17] => __call
371
    [18] => __invoke
372
    [19] => __toString
373
    [20] => addMethod
374
    [21] => callArray
375
    [22] => inspect
376
    [23] => loadState
377
    [24] => loop
378
    [25] => off
379
    [26] => on
380
    [27] => saveState
381
    [28] => trigger
382
    [29] => when
383
)
384
            */
385
 
1 efrain 386
        } else {
387
            return new JsonModel([
388
                'success' => false,
389
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
390
            ]);
391
        }
392
    }
261 efrain 393
 
394
    public function getAttachmentAction()
1 efrain 395
    {
396
        $request = $this->getRequest();
397
        if ($request->isGet()) {
398
            $currentUserPlugin = $this->plugin('currentUserPlugin');
399
            $currentUser = $currentUserPlugin->getUser();
400
 
261 efrain 401
            $id = intval($this->params()->fromRoute('id'), 10);
402
            $attachment = intval($this->params()->fromRoute('attachment'), 10);
199 efrain 403
 
261 efrain 404
            $externalCredentials = ExternalCredentials::getInstancia($this->config, $this->adapter);
405
            $externalCredentials->getUserBy($currentUser->id);
199 efrain 406
 
261 efrain 407
            $host       = trim($this->config['leaderslinked.inmail.host']);
408
            $port       = intval($this->config['leaderslinked.inmail.port_imap'], 10);
409
            $mailbox    = trim($this->config['leaderslinked.inmail.mailbox']);
410
 
411
 
412
            $username   = $externalCredentials->getUsernameInmail();
413
            $password   =  $externalCredentials->getPasswordInmail();
414
 
415
 
416
 
417
 
418
            try {
419
                \Eden\Core\Control::i();
420
                $client = eden('mail')->imap(
421
                    $host,
422
                    $username,
423
                    $password,
424
                    $port,
425
                    false);
426
 
427
                $client->setActiveMailbox($mailbox);
428
 
429
                $email = $client->getUniqueEmails($id, true);
430
                $client->disconnect();
431
 
432
                if($email['uid']) {
433
 
434
 
435
                    $base64         = '';
436
                    $contentType    = '';
437
                    $filename       = '';
438
 
439
                    if(!empty($email['attachment']) &&  is_array($email['attachment'])) {
199 efrain 440
 
261 efrain 441
 
199 efrain 442
 
261 efrain 443
                        foreach($email['attachment'] as $filename => $data)
444
                        {
445
 
446
                            $i = 0;
447
                            foreach($data as $contentType => $value)
448
                            {
449
                                if($i == $attachment) {
450
                                    $base64 = base64_encode($value);
451
                                    break;
452
                                }
453
                                $i++;
454
                            }
199 efrain 455
                        }
261 efrain 456
                    }
457
 
458
                    if($base64) {
459
                        return new JsonModel([
460
                            'success' => true,
461
                            'data' => [
462
                                'filename' => $filename,
463
                                'mime-type' => $contentType,
464
                                'base64' =>  $base64,
465
                            ]
466
                        ]);
467
                    } else {
468
                        return new JsonModel([
469
                            'success' => false,
470
                            'data' =>  'ERROR_INMAIL_MESSAGE_ATTACHMENT_NOT_FOUND'
471
                        ]);
472
                    }
473
 
474
 
475
 
476
 
477
 
1 efrain 478
 
261 efrain 479
 
480
                } else {
481
                    return new JsonModel([
482
                        'success' => false,
483
                        'data' => 'ERROR_INMAIL_MESSAGE_NOT_FOUND'
484
                    ]);
1 efrain 485
                }
261 efrain 486
 
487
 
488
            } catch (\Exception $e) {
489
 
1 efrain 490
                return new JsonModel([
261 efrain 491
                    'success' => false,
492
                    'data' => 'ERROR_INMAIL_MESSAGE_LIST_UNAVAILABLE'
1 efrain 493
                ]);
494
            }
261 efrain 495
 
496
 
497
 
1 efrain 498
        } else {
499
            return new JsonModel([
500
                'success' => false,
501
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
502
            ]);
503
        }
504
    }
261 efrain 505
 
506
    public function getMessageAction()
1 efrain 507
    {
508
        $request = $this->getRequest();
261 efrain 509
        if ($request->isGet()) {
1 efrain 510
            $currentUserPlugin = $this->plugin('currentUserPlugin');
511
            $currentUser = $currentUserPlugin->getUser();
512
 
261 efrain 513
            $id = intval($this->params()->fromRoute('id'), 10);
514
 
283 www 515
            $storage = Storage::getInstance($this->config);
1 efrain 516
 
261 efrain 517
            $externalCredentials = ExternalCredentials::getInstancia($this->config, $this->adapter);
518
            $externalCredentials->getUserBy($currentUser->id);
519
 
520
            $host       = trim($this->config['leaderslinked.inmail.host']);
521
            $port       = intval($this->config['leaderslinked.inmail.port_imap'], 10);
522
            $mailbox    = trim($this->config['leaderslinked.inmail.mailbox']);
523
 
524
 
525
            $username   = $externalCredentials->getUsernameInmail();
526
            $password   =  $externalCredentials->getPasswordInmail();
527
 
528
 
529
 
530
 
531
            try {
532
                \Eden\Core\Control::i();
533
                $client = eden('mail')->imap(
534
                    $host,
535
                    $username,
536
                    $password,
537
                    $port,
538
                    false);
539
 
540
                $client->setActiveMailbox($mailbox);
541
 
542
                $email = $client->getUniqueEmails($id, true);
543
                $client->disconnect();
544
 
545
                if($email['uid']) {
546
                    $from = $email['from']['email'];
547
                    $subject = $email['subject'];
548
                    $body   = empty($email['body']['text/html']) ?  $email['body']['text/plain'] : $email['body']['text/html'];
549
 
550
 
551
                    $userMapper = \LeadersLinked\Mapper\UserMapper::getInstance($this->adapter);
552
                    $user = $userMapper->fetchOneByUsernameInmailAndNetworkId($from, $currentUser->network_id);
553
 
554
 
555
                    $seen = false;
556
                    if(is_array($email['flags'])) {
557
                        foreach($email['flags'] as $flag)
558
                        {
559
                            if($flag == 'seen') {
560
                                $seen = 1;
561
                                break;
1 efrain 562
                            }
563
                        }
564
                    }
261 efrain 565
 
294 www 566
                    $extensions = ['mov', 'webm','mp4','mpeg','jpg','jpeg','png', 'pdf'];
567
                    $contentTypes =  ['video/quicktime', 'video/webm', 'video/mp4', 'video/mpeg', 'image/jpg', 'image/jpeg', 'image/png', 'application/pdf'];
261 efrain 568
 
295 www 569
                    $storage = Storage::getInstance($this->config);
570
                    $path = $storage->getPathMessage();
571
                    $code = $currentUser->uuid;
261 efrain 572
 
573
                    $attachments = [];
574
                    if(!empty($email['attachment']) &&  is_array($email['attachment'])) {
575
                        foreach($email['attachment'] as $filename => $attachment)
576
                        {
577
                            $i = 0;
578
                            foreach($attachment as $contentType => $value)
579
                            {
294 www 580
                                $pos = strrpos($filename, '.');
581
                                $extension = trim(substr($filename, $pos + 1));
582
 
583
                               for($j = 0; $max = count($extensions), $j < $max; $j++)
584
                               {
585
                                   if($extension == $extensions[$j]) {
586
                                       $contentType = $contentTypes[$j];
587
                                   }
588
                               }
295 www 589
 
296 www 590
                               $remoteFilename = trim($email['uid'] . '-' . $filename);
295 www 591
 
592
 
593
                               $url = $storage->getGenericFile($path, $code,  $remoteFilename) ;
594
                               if(!$url) {
595
 
596
                                   $tmp_file = sys_get_temp_dir() . '/' .  $remoteFilename;
597
                                   file_put_contents($tmp_file, $value);
598
 
298 www 599
                                   if($storage->putFile($path, $code, $tmp_file)) {
600
 
299 www 601
                                       $url = $storage->getGenericFile($path, $code,  $remoteFilename) ;
298 www 602
                                   } else {
603
                                       $url = '';
604
                                   }
295 www 605
 
606
                               }
607
 
294 www 608
 
609
 
295 www 610
                               // $this->url()->fromRoute('inmail/message/attachment',[ 'id' => $email['uid'], 'attachment' => $i ], ['force_canonical' => true]),
294 www 611
 
261 efrain 612
                                array_push($attachments, [
295 www 613
                                    'filename' =>  $remoteFilename,
261 efrain 614
                                    'content-type' => $contentType,
295 www 615
                                    'attachment_link' => $url,
261 efrain 616
                                ]);
617
                                $i++;
1 efrain 618
                            }
619
                        }
620
                    }
261 efrain 621
 
1 efrain 622
                    return new JsonModel([
261 efrain 623
                        'success' => true,
624
                        'data' => [
625
                            'uuid' => $user->uuid,
626
                            'name' => $user->first_name . ' ' . $user->last_name,
283 www 627
                            'image' => $storage->getUserImage($user),
261 efrain 628
                            'subject' => $subject,
629
                            'body' => $body,
630
                            'date' => date('Y-m-d h:i a', $email['date']),
631
                            'seen' => $seen,
632
                            'block_link' => $this->url()->fromRoute('inmail/user/block',[ 'id' => $user->uuid ], ['force_canonical' => true]),
633
                            'send_link' => $this->url()->fromRoute('inmail/message/send',[ 'id' => $user->uuid ], ['force_canonical' => true]),
634
                            'delete_link'=>    $this->url()->fromRoute('inmail/message/delete',[ 'id' => $email['uid'] ], ['force_canonical' => true]),
635
                            'attachments' => $attachments,
1 efrain 636
                        ]
637
                    ]);
261 efrain 638
 
639
 
1 efrain 640
                } else {
641
                    return new JsonModel([
261 efrain 642
                        'success' => false,
643
                        'data' => 'ERROR_INMAIL_MESSAGE_NOT_FOUND'
1 efrain 644
                    ]);
645
                }
261 efrain 646
 
647
 
648
            } catch (\Exception $e) {
649
 
1 efrain 650
                return new JsonModel([
261 efrain 651
                    'success' => false,
652
                    'data' => 'ERROR_INMAIL_MESSAGE_LIST_UNAVAILABLE'
1 efrain 653
                ]);
654
            }
261 efrain 655
 
656
 
657
 
1 efrain 658
        } else {
659
            return new JsonModel([
660
                'success' => false,
661
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
662
            ]);
663
        }
664
    }
261 efrain 665
 
666
 
667
    public function deleteMessageAction()
1 efrain 668
    {
261 efrain 669
        $request = $this->getRequest();
670
        if ($request->isPost()) {
671
            $currentUserPlugin = $this->plugin('currentUserPlugin');
672
            $currentUser = $currentUserPlugin->getUser();
673
 
674
            $id = intval($this->params()->fromRoute('id'), 10);
268 efrain 675
 
261 efrain 676
 
677
            $externalCredentials = ExternalCredentials::getInstancia($this->config, $this->adapter);
678
            $externalCredentials->getUserBy($currentUser->id);
679
 
680
            $host       = trim($this->config['leaderslinked.inmail.host']);
681
            $port       = intval($this->config['leaderslinked.inmail.port_imap'], 10);
682
            $mailbox    = trim($this->config['leaderslinked.inmail.mailbox']);
683
 
684
 
685
            $username   = $externalCredentials->getUsernameInmail();
686
            $password   =  $externalCredentials->getPasswordInmail();
687
 
688
 
689
 
690
 
691
            try {
692
                \Eden\Core\Control::i();
693
                $client = eden('mail')->imap(
694
                    $host,
695
                    $username,
696
                    $password,
697
                    $port,
698
                    false);
699
 
700
                $client->setActiveMailbox($mailbox);
701
 
295 www 702
 
703
                $storage = Storage::getInstance($this->config);
704
                $path = $storage->getPathMessage();
705
                $code = $currentUser->uuid;
706
                $storage->deleteDirectory($path, $code);
707
 
300 www 708
                $email = $client->getUniqueEmails($id, true);
709
 
295 www 710
 
300 www 711
                if($email['uid']) {
712
 
713
 
714
                    if(!empty($email['attachment']) &&  is_array($email['attachment'])) {
715
 
716
                        $filenames = array_keys($email['attachment']);
717
                        foreach($filenames as $filename)
718
                        {
719
                            $remoteFilename = trim($email['uid'] . '-' . $filename);
720
                            $storage->deleteFile($path, $code, $remoteFilename);
721
 
722
                        }
723
                    }
724
                }
725
 
295 www 726
 
300 www 727
 
728
 
261 efrain 729
                $response = $client->remove($id, true);
730
                $client->disconnect();
731
 
732
                if($response) {
733
                    return new JsonModel([
734
                        'success' => true,
735
                        'data' => 'LABEL_INMAIL_MESSAGE_DELETED'
736
                    ]);
737
                } else {
738
                    return new JsonModel([
739
                        'success' => false,
740
                        'data' =>  'ERROR_INMAIL_MESSAGE_NOT_DELETE'
741
                    ]);
742
                }
743
 
744
 
745
            } catch (\Exception $e) {
746
 
747
                return new JsonModel([
748
                    'success' => false,
749
                    'data' => 'ERROR_INMAIL_MESSAGE_LIST_UNAVAILABLE'
750
                ]);
751
            }
752
 
753
 
754
 
1 efrain 755
        } else {
261 efrain 756
            return new JsonModel([
757
                'success' => false,
758
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
759
            ]);
1 efrain 760
        }
761
    }
261 efrain 762
 
763
 
764
    public function sendMessageAction()
1 efrain 765
    {
766
        $request = $this->getRequest();
767
        if ($request->isPost()) {
768
            $currentUserPlugin = $this->plugin('currentUserPlugin');
769
            $currentUser = $currentUserPlugin->getUser();
770
 
261 efrain 771
            $userMapper = UserMapper::getInstance($this->adapter);
772
 
1 efrain 773
            $id = $this->params()->fromRoute('id');
774
            if (!$id) {
775
                return new JsonModel([
776
                    'success' => false,
777
                    'data' => 'ERROR_PARAMETERS_ARE_INVALID'
778
                ]);
779
            }
780
 
261 efrain 781
 
782
            $user = $userMapper->fetchOneByUuidAndNetworkId($id, $currentUser->network_id);
1 efrain 783
            if (!$user) {
784
                return new JsonModel([
785
                    'success' => false,
786
                    'data' => 'ERROR_REQUEST_IS_INVALID'
787
                ]);
788
            }
789
 
261 efrain 790
            $externalCredentials = ExternalCredentials::getInstancia($this->config, $this->adapter);
791
            $externalCredentials->getUserBy($currentUser->id);
1 efrain 792
 
261 efrain 793
            $host       = trim($this->config['leaderslinked.inmail.host']);
794
            $port       = intval($this->config['leaderslinked.inmail.port_smtp'], 10);
795
 
1 efrain 796
 
261 efrain 797
            $username   = $externalCredentials->getUsernameInmail();
798
            $password   =  $externalCredentials->getPasswordInmail();
799
 
800
 
801
 
802
            $data = array_merge($request->getPost()->toArray(), $request->getFiles()->toArray());
803
 
804
 
805
            $form = new SendForm();
806
            $form->setData($data);
807
 
808
            if ($form->isValid()) {
1 efrain 809
 
261 efrain 810
                $dataPost = (array) $form->getData();
811
 
812
                try {
291 www 813
 
814
                    $subject = $dataPost['subject'];
815
                    $message = $dataPost['body'];
816
 
817
                    $encoding = mb_detect_encoding($subject);
818
                    if($encoding != 'UTF-8') {
819
                        $subject = mb_convert_encoding($subject, 'UTF-8', $encoding);
820
                    }
821
 
822
                    $encoding = mb_detect_encoding($message);
823
                    if($encoding != 'UTF-8') {
824
                        $message = mb_convert_encoding($message, 'UTF-8', $encoding);
825
                    }
826
 
827
 
828
 
829
 
830
 
831
 
832
                    $phpMailer = new PHPMailer();
833
                    $phpMailer->isSMTP();
834
 
835
                    $phpMailer->addAddress($user->username_inmail, ($user->first_name . ' ' . $user->last_name));
836
                   /*
837
                    if($cc) {
838
                        foreach($cc as $address => $name) {
839
                            $phpMailer->addCC($address, $name);
840
                        }
841
                    }
842
                    if($bcc) {
843
                        foreach($bcc as $address => $name) {
844
                            $phpMailer->addBCC($address, $name);
845
                        }
846
                    }*/
847
 
848
                    $phpMailer->setFrom($currentUser->username_inmail, ($currentUser->first_name . ' ' . $currentUser->last_name));
293 www 849
                    $phpMailer->SMTPDebug    = false;
291 www 850
                    $phpMailer->Host         = $host;
851
                    $phpMailer->Port         = $port;
852
                    $phpMailer->IsHTML(true);
853
                    $phpMailer->SMTPAuth    = true;
854
                    //$phpMailer->SMTPSecure   = 'tls';
293 www 855
                    $phpMailer->SMTPAutoTLS = false;
856
                    $phpMailer->SMTPSecure = ''; // PHPMailer::ENCRYPTION_SMTPS;
291 www 857
                    $phpMailer->Username     = $username;
858
                    $phpMailer->Password     = $password;
859
                    $phpMailer->Subject      = $subject;
860
                    $phpMailer->Body         = $message;
861
                    $phpMailer->AltBody      = $message;
862
                    $phpMailer->CharSet      = 'UTF-8';
863
 
864
                    $files  = $this->getRequest()->getFiles()->toArray();
865
                    if (isset($files['filename']) && empty($files['filename']['error'])) {
866
                        $phpMailer->addAttachment($files['filename']['tmp_name'], $files['filename']['name'] );
867
 
868
                    }
869
 
870
                    if($phpMailer->send()) {
871
 
872
                        return new JsonModel([
873
                            'success'   => true,
874
                            'data'   => 'LABEL_INMAIL_MESSAGE_SENT'
875
                        ]);
876
                    } else {
877
                        return new JsonModel([
878
                            'success'   => false,
879
                            'data'   => 'ERROR_INMAIL_MESSAGE_NOT_SENT'
880
                        ]);
292 www 881
                    }
291 www 882
 
883
 
884
                    /*
885
 
261 efrain 886
                    \Eden\Core\Control::i();
887
                    $smtp = eden('mail')->smtp(
888
                        $host,
889
                        $username,
890
                        $password,
891
                        $port,
892
                        false
893
                    );
894
 
895
                    $smtp->setSubject( $dataPost['subject']);
896
                    $smtp->setBody( $dataPost['body'], true);
897
                    $smtp->setBody($dataPost['body']);
898
                    $smtp->addTo($user->username_inmail);
899
 
900
                    $files  = $this->getRequest()->getFiles()->toArray();
283 www 901
                    if (isset($files['filename']) && empty($files['filename']['error'])) {
902
                        $smtp->addAttachment($files['filename']['name'], $files['filename']['tmp_name']);
1 efrain 903
 
904
                    }
261 efrain 905
 
906
 
907
 
908
                    $smtp->send();
909
 
291 www 910
                    $smtp->disconnect(); */
261 efrain 911
 
912
                    return new JsonModel([
913
                        'success'   => true,
914
                        'data'   => 'LABEL_INMAIL_MESSAGE_SENT'
915
                    ]);
916
 
917
                } catch (\Exception $e) {
918
                    return new JsonModel([
919
                        'success'   => false,
920
                        'data'   => 'ERROR_INMAIL_MESSAGE_NOT_SENT'
921
                    ]);
1 efrain 922
                }
923
 
261 efrain 924
 
1 efrain 925
            } else {
261 efrain 926
 
927
 
928
                $messages = [];
929
                $form_messages = (array) $form->getMessages();
930
                foreach ($form_messages  as $fieldname => $field_messages) {
931
                    $messages[$fieldname] = array_values($field_messages);
932
                }
933
 
934
                return new JsonModel([
935
                    'success'   => false,
936
                    'data'   => $messages
937
                ]);
1 efrain 938
            }
939
        } else {
261 efrain 940
            return new JsonModel([
1 efrain 941
                'success' => false,
942
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
261 efrain 943
            ]);
1 efrain 944
        }
945
    }
261 efrain 946
 
947
 
1 efrain 948
}
261 efrain 949