Proyectos de Subversion LeadersLinked - Services

Rev

Rev 295 | Rev 297 | 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
 
599
                                   if($storage->putFile($path, $code, $tmp_file)) {
600
                                       $url = $storage->getGenericFile($path, $code, $tmp_file) ;
601
                                   } else {
602
                                       $url = '';
603
                                   }
604
 
605
                               }
606
 
294 www 607
 
608
 
295 www 609
                               // $this->url()->fromRoute('inmail/message/attachment',[ 'id' => $email['uid'], 'attachment' => $i ], ['force_canonical' => true]),
294 www 610
 
261 efrain 611
                                array_push($attachments, [
295 www 612
                                    'filename' =>  $remoteFilename,
261 efrain 613
                                    'content-type' => $contentType,
295 www 614
                                    'attachment_link' => $url,
261 efrain 615
                                ]);
616
                                $i++;
1 efrain 617
                            }
618
                        }
619
                    }
261 efrain 620
 
1 efrain 621
                    return new JsonModel([
261 efrain 622
                        'success' => true,
623
                        'data' => [
624
                            'uuid' => $user->uuid,
625
                            'name' => $user->first_name . ' ' . $user->last_name,
283 www 626
                            'image' => $storage->getUserImage($user),
261 efrain 627
                            'subject' => $subject,
628
                            'body' => $body,
629
                            'date' => date('Y-m-d h:i a', $email['date']),
630
                            'seen' => $seen,
631
                            'block_link' => $this->url()->fromRoute('inmail/user/block',[ 'id' => $user->uuid ], ['force_canonical' => true]),
632
                            'send_link' => $this->url()->fromRoute('inmail/message/send',[ 'id' => $user->uuid ], ['force_canonical' => true]),
633
                            'delete_link'=>    $this->url()->fromRoute('inmail/message/delete',[ 'id' => $email['uid'] ], ['force_canonical' => true]),
634
                            'attachments' => $attachments,
1 efrain 635
                        ]
636
                    ]);
261 efrain 637
 
638
 
1 efrain 639
                } else {
640
                    return new JsonModel([
261 efrain 641
                        'success' => false,
642
                        'data' => 'ERROR_INMAIL_MESSAGE_NOT_FOUND'
1 efrain 643
                    ]);
644
                }
261 efrain 645
 
646
 
647
            } catch (\Exception $e) {
648
 
1 efrain 649
                return new JsonModel([
261 efrain 650
                    'success' => false,
651
                    'data' => 'ERROR_INMAIL_MESSAGE_LIST_UNAVAILABLE'
1 efrain 652
                ]);
653
            }
261 efrain 654
 
655
 
656
 
1 efrain 657
        } else {
658
            return new JsonModel([
659
                'success' => false,
660
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
661
            ]);
662
        }
663
    }
261 efrain 664
 
665
 
666
    public function deleteMessageAction()
1 efrain 667
    {
261 efrain 668
        $request = $this->getRequest();
669
        if ($request->isPost()) {
670
            $currentUserPlugin = $this->plugin('currentUserPlugin');
671
            $currentUser = $currentUserPlugin->getUser();
672
 
673
            $id = intval($this->params()->fromRoute('id'), 10);
268 efrain 674
 
261 efrain 675
 
676
            $externalCredentials = ExternalCredentials::getInstancia($this->config, $this->adapter);
677
            $externalCredentials->getUserBy($currentUser->id);
678
 
679
            $host       = trim($this->config['leaderslinked.inmail.host']);
680
            $port       = intval($this->config['leaderslinked.inmail.port_imap'], 10);
681
            $mailbox    = trim($this->config['leaderslinked.inmail.mailbox']);
682
 
683
 
684
            $username   = $externalCredentials->getUsernameInmail();
685
            $password   =  $externalCredentials->getPasswordInmail();
686
 
687
 
688
 
689
 
690
            try {
691
                \Eden\Core\Control::i();
692
                $client = eden('mail')->imap(
693
                    $host,
694
                    $username,
695
                    $password,
696
                    $port,
697
                    false);
698
 
699
                $client->setActiveMailbox($mailbox);
700
 
295 www 701
 
702
                $storage = Storage::getInstance($this->config);
703
                $path = $storage->getPathMessage();
704
                $code = $currentUser->uuid;
705
                $storage->deleteDirectory($path, $code);
706
 
707
 
708
 
261 efrain 709
                $response = $client->remove($id, true);
710
                $client->disconnect();
711
 
712
                if($response) {
713
                    return new JsonModel([
714
                        'success' => true,
715
                        'data' => 'LABEL_INMAIL_MESSAGE_DELETED'
716
                    ]);
717
                } else {
718
                    return new JsonModel([
719
                        'success' => false,
720
                        'data' =>  'ERROR_INMAIL_MESSAGE_NOT_DELETE'
721
                    ]);
722
                }
723
 
724
 
725
            } catch (\Exception $e) {
726
 
727
                return new JsonModel([
728
                    'success' => false,
729
                    'data' => 'ERROR_INMAIL_MESSAGE_LIST_UNAVAILABLE'
730
                ]);
731
            }
732
 
733
 
734
 
1 efrain 735
        } else {
261 efrain 736
            return new JsonModel([
737
                'success' => false,
738
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
739
            ]);
1 efrain 740
        }
741
    }
261 efrain 742
 
743
 
744
    public function sendMessageAction()
1 efrain 745
    {
746
        $request = $this->getRequest();
747
        if ($request->isPost()) {
748
            $currentUserPlugin = $this->plugin('currentUserPlugin');
749
            $currentUser = $currentUserPlugin->getUser();
750
 
261 efrain 751
            $userMapper = UserMapper::getInstance($this->adapter);
752
 
1 efrain 753
            $id = $this->params()->fromRoute('id');
754
            if (!$id) {
755
                return new JsonModel([
756
                    'success' => false,
757
                    'data' => 'ERROR_PARAMETERS_ARE_INVALID'
758
                ]);
759
            }
760
 
261 efrain 761
 
762
            $user = $userMapper->fetchOneByUuidAndNetworkId($id, $currentUser->network_id);
1 efrain 763
            if (!$user) {
764
                return new JsonModel([
765
                    'success' => false,
766
                    'data' => 'ERROR_REQUEST_IS_INVALID'
767
                ]);
768
            }
769
 
261 efrain 770
            $externalCredentials = ExternalCredentials::getInstancia($this->config, $this->adapter);
771
            $externalCredentials->getUserBy($currentUser->id);
1 efrain 772
 
261 efrain 773
            $host       = trim($this->config['leaderslinked.inmail.host']);
774
            $port       = intval($this->config['leaderslinked.inmail.port_smtp'], 10);
775
 
1 efrain 776
 
261 efrain 777
            $username   = $externalCredentials->getUsernameInmail();
778
            $password   =  $externalCredentials->getPasswordInmail();
779
 
780
 
781
 
782
            $data = array_merge($request->getPost()->toArray(), $request->getFiles()->toArray());
783
 
784
 
785
            $form = new SendForm();
786
            $form->setData($data);
787
 
788
            if ($form->isValid()) {
1 efrain 789
 
261 efrain 790
                $dataPost = (array) $form->getData();
791
 
792
                try {
291 www 793
 
794
                    $subject = $dataPost['subject'];
795
                    $message = $dataPost['body'];
796
 
797
                    $encoding = mb_detect_encoding($subject);
798
                    if($encoding != 'UTF-8') {
799
                        $subject = mb_convert_encoding($subject, 'UTF-8', $encoding);
800
                    }
801
 
802
                    $encoding = mb_detect_encoding($message);
803
                    if($encoding != 'UTF-8') {
804
                        $message = mb_convert_encoding($message, 'UTF-8', $encoding);
805
                    }
806
 
807
 
808
 
809
 
810
 
811
 
812
                    $phpMailer = new PHPMailer();
813
                    $phpMailer->isSMTP();
814
 
815
                    $phpMailer->addAddress($user->username_inmail, ($user->first_name . ' ' . $user->last_name));
816
                   /*
817
                    if($cc) {
818
                        foreach($cc as $address => $name) {
819
                            $phpMailer->addCC($address, $name);
820
                        }
821
                    }
822
                    if($bcc) {
823
                        foreach($bcc as $address => $name) {
824
                            $phpMailer->addBCC($address, $name);
825
                        }
826
                    }*/
827
 
828
                    $phpMailer->setFrom($currentUser->username_inmail, ($currentUser->first_name . ' ' . $currentUser->last_name));
293 www 829
                    $phpMailer->SMTPDebug    = false;
291 www 830
                    $phpMailer->Host         = $host;
831
                    $phpMailer->Port         = $port;
832
                    $phpMailer->IsHTML(true);
833
                    $phpMailer->SMTPAuth    = true;
834
                    //$phpMailer->SMTPSecure   = 'tls';
293 www 835
                    $phpMailer->SMTPAutoTLS = false;
836
                    $phpMailer->SMTPSecure = ''; // PHPMailer::ENCRYPTION_SMTPS;
291 www 837
                    $phpMailer->Username     = $username;
838
                    $phpMailer->Password     = $password;
839
                    $phpMailer->Subject      = $subject;
840
                    $phpMailer->Body         = $message;
841
                    $phpMailer->AltBody      = $message;
842
                    $phpMailer->CharSet      = 'UTF-8';
843
 
844
                    $files  = $this->getRequest()->getFiles()->toArray();
845
                    if (isset($files['filename']) && empty($files['filename']['error'])) {
846
                        $phpMailer->addAttachment($files['filename']['tmp_name'], $files['filename']['name'] );
847
 
848
                    }
849
 
850
                    if($phpMailer->send()) {
851
 
852
                        return new JsonModel([
853
                            'success'   => true,
854
                            'data'   => 'LABEL_INMAIL_MESSAGE_SENT'
855
                        ]);
856
                    } else {
857
                        return new JsonModel([
858
                            'success'   => false,
859
                            'data'   => 'ERROR_INMAIL_MESSAGE_NOT_SENT'
860
                        ]);
292 www 861
                    }
291 www 862
 
863
 
864
                    /*
865
 
261 efrain 866
                    \Eden\Core\Control::i();
867
                    $smtp = eden('mail')->smtp(
868
                        $host,
869
                        $username,
870
                        $password,
871
                        $port,
872
                        false
873
                    );
874
 
875
                    $smtp->setSubject( $dataPost['subject']);
876
                    $smtp->setBody( $dataPost['body'], true);
877
                    $smtp->setBody($dataPost['body']);
878
                    $smtp->addTo($user->username_inmail);
879
 
880
                    $files  = $this->getRequest()->getFiles()->toArray();
283 www 881
                    if (isset($files['filename']) && empty($files['filename']['error'])) {
882
                        $smtp->addAttachment($files['filename']['name'], $files['filename']['tmp_name']);
1 efrain 883
 
884
                    }
261 efrain 885
 
886
 
887
 
888
                    $smtp->send();
889
 
291 www 890
                    $smtp->disconnect(); */
261 efrain 891
 
892
                    return new JsonModel([
893
                        'success'   => true,
894
                        'data'   => 'LABEL_INMAIL_MESSAGE_SENT'
895
                    ]);
896
 
897
                } catch (\Exception $e) {
898
                    return new JsonModel([
899
                        'success'   => false,
900
                        'data'   => 'ERROR_INMAIL_MESSAGE_NOT_SENT'
901
                    ]);
1 efrain 902
                }
903
 
261 efrain 904
 
1 efrain 905
            } else {
261 efrain 906
 
907
 
908
                $messages = [];
909
                $form_messages = (array) $form->getMessages();
910
                foreach ($form_messages  as $fieldname => $field_messages) {
911
                    $messages[$fieldname] = array_values($field_messages);
912
                }
913
 
914
                return new JsonModel([
915
                    'success'   => false,
916
                    'data'   => $messages
917
                ]);
1 efrain 918
            }
919
        } else {
261 efrain 920
            return new JsonModel([
1 efrain 921
                'success' => false,
922
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
261 efrain 923
            ]);
1 efrain 924
        }
925
    }
261 efrain 926
 
927
 
1 efrain 928
}
261 efrain 929