Proyectos de Subversion LeadersLinked - Services

Rev

Rev 764 | | 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
 
8
use Laminas\Mvc\Controller\AbstractActionController;
283 www 9
 
1 efrain 10
use Laminas\View\Model\JsonModel;
11
use LeadersLinked\Model\Page;
12
use LeadersLinked\Mapper\NotificationMapper;
13
use LeadersLinked\Mapper\PageMapper;
14
use LeadersLinked\Mapper\UserMapper;
15
use LeadersLinked\Mapper\ConnectionMapper;
16
 
17
use LeadersLinked\Mapper\PostMapper;
18
use LeadersLinked\Model\Post;
19
 
20
use LeadersLinked\Mapper\FeedMapper;
21
use LeadersLinked\Model\Feed;
22
use LeadersLinked\Model\User;
23
use LeadersLinked\Model\Connection;
24
use LeadersLinked\Mapper\NetworkMapper;
25
use LeadersLinked\Library\Functions;
26
use LeadersLinked\Model\Network;
27
 
257 efrain 28
use LeadersLinked\Library\ExternalCredentials;
341 www 29
use Nullix\CryptoJsAes\CryptoJsAes;
345 www 30
use LeadersLinked\Library\Storage;
257 efrain 31
 
1 efrain 32
class HomeController extends AbstractActionController
33
{
34
    /**
35
     *
36
     * @var \Laminas\Db\Adapter\AdapterInterface
37
     */
38
    private $adapter;
39
 
40
    /**
41
     *
42
     * @var \LeadersLinked\Cache\CacheInterface
43
     */
44
    private $cache;
45
 
46
 
47
    /**
48
     *
49
     * @var \Laminas\Log\LoggerInterface
50
     */
51
    private $logger;
52
 
53
    /**
54
     *
55
     * @var array
56
     */
57
    private $config;
58
 
59
 
60
    /**
61
     *
62
     * @var \Laminas\Mvc\I18n\Translator
63
     */
64
    private $translator;
65
 
66
 
67
    /**
68
     *
69
     * @param \Laminas\Db\Adapter\AdapterInterface $adapter
70
     * @param \LeadersLinked\Cache\CacheInterface $cache
71
     * @param \Laminas\Log\LoggerInterface LoggerInterface $logger
72
     * @param array $config
73
     * @param \Laminas\Mvc\I18n\Translator $translator
74
     */
75
    public function __construct($adapter, $cache, $logger, $config, $translator)
76
    {
77
        $this->adapter      = $adapter;
78
        $this->cache        = $cache;
79
        $this->logger       = $logger;
80
        $this->config       = $config;
81
        $this->translator   = $translator;
82
    }
341 www 83
 
84
    public function cryptoAction()
85
    {
86
        header('Content-type: text/plain');
87
 
88
 
343 www 89
        $email = '{"ct":"eZd1aD5p3miv+UsN/7CfAst+8U/Tu4de/n5Rmaf/VsicEYlJG3DH4JvRoxCUwcSA","iv":"8abe13978c33e8701857c6377f09864c","s":"cb61b5d25010408b"}';
90
        $password  = '{"ct":"v5maMXocpQT5Jr02vBzXhw==","iv":"a2f8244c89dd690c60ded5f49092d552","s":"522aeee2356f0677"}';
341 www 91
        $aes = 'HP8Rc6vBnr2CJyVG';
92
 
93
        echo "Email = $email \r\n";
94
        echo "Password = $password \r\n";
95
        echo "AES = $aes \r\n\r\n";
96
 
97
        $emailDecrypt  = CryptoJsAes::decrypt($email, $aes);
98
        $passwordDecrypt  = CryptoJsAes::decrypt($password, $aes);
99
 
100
        echo "EmailDecrypt = $emailDecrypt \r\n";
101
        echo "PasswordDecrypt = $passwordDecrypt \r\n";
102
 
103
        exit;
104
    }
1 efrain 105
 
333 www 106
 
107
    public function storageAction()
108
    {
109
        $currentNetworkPlugin = $this->plugin('currentNetworkPlugin');
110
        $currentNetwork = $currentNetworkPlugin->getNetwork();
111
 
112
        $request = $this->getRequest();
113
        if ($request->isGet()) {
114
 
115
            $code = $this->params()->fromRoute('code');
116
            if(empty($code)) {
117
                $data = [
118
                    'success' => false,
119
                    'data' => 'ERROR_STORAGE_CODE_INVALID'
120
                ];
121
 
122
                return new JsonModel($data);
123
            }
124
 
125
            $storageFileMapper = \LeadersLinked\Mapper\StorageFileMapper::getInstance($this->adapter);
126
            $storageFile = $storageFileMapper->fetchOneByCode($code);
127
 
128
 
129
 
130
            if($storageFile) {
131
 
132
 
133
                if(file_exists($storageFile->path)) {
134
 
770 stevensc 135
                    // Check if the path is a directory instead of a file
136
                    if(is_dir($storageFile->path)) {
137
                        $this->logger->err('StorageFile path is a directory instead of a file: ' . $storageFile->path . ' (code: ' . $code . ')');
138
                        return $this->getResponse()->setStatusCode(500);
139
                    }
140
 
333 www 141
                    // Try to open file
142
                    if (!is_readable($storageFile->path)) {
143
                        return $this->getResponse()->setStatusCode(500);
144
                    }
145
 
146
                    // Get file size in bytes.
147
                    $fileSize = filesize($storageFile->path);
148
 
149
                    // Get MIME type of the file.
150
                    $mimeType = mime_content_type($storageFile->path);
151
                    if($mimeType===false) {
152
                        $mimeType = 'application/octet-stream';
153
                    }
154
 
155
                    $fileContent = file_get_contents($storageFile->path);
156
 
157
                    $response = $this->getResponse();
158
                    $headers = $response->getHeaders();
159
                    $headers->addHeaderLine('Content-type: ' . $mimeType);
160
                    $headers->addHeaderLine('Content-length: ' . $fileSize);
161
 
162
                    if($fileContent!==false) {
163
                        $response->setContent($fileContent);
164
                        return $response;
165
                    } else {
166
                        $this->getResponse()->setStatusCode(500);
167
                        return;
168
                    }
169
                } else {
170
                    return $this->getResponse()->setStatusCode(404);
171
                }
172
 
173
            } else {
174
                return $this->getResponse()->setStatusCode(404);
175
            }
176
 
177
        } else {
178
            $data = [
179
                'success' => false,
180
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
181
            ];
182
 
183
            return new JsonModel($data);
184
        }
185
    }
1 efrain 186
 
187
 
764 stevensc 188
    public function dataStorageAction()
189
    {
190
        $request = $this->getRequest();
191
        if ($request->isGet()) {
192
 
193
            $type = $this->params()->fromRoute('type');
194
            $uuid = $this->params()->fromRoute('uuid');
195
            $filename = $this->params()->fromRoute('filename');
196
 
197
            if(empty($type) || empty($uuid) || empty($filename)) {
198
                return $this->getResponse()->setStatusCode(404);
199
            }
200
 
201
            // Map type to config key
202
            $configKey = 'leaderslinked.fullpath.' . $type;
203
            if(!isset($this->config[$configKey])) {
204
                return $this->getResponse()->setStatusCode(404);
205
            }
206
 
207
            // Build the file path using the configured base path
208
            $basePath = $this->config[$configKey];
209
            $filePath = $basePath . $uuid . DIRECTORY_SEPARATOR . $filename;
210
 
211
            // Security check: ensure the path is within the allowed directory
212
            $realFilePath = realpath($filePath);
213
            $realBasePath = realpath($basePath);
214
            if(!$realFilePath || !$realBasePath || strpos($realFilePath, $realBasePath) !== 0) {
215
                return $this->getResponse()->setStatusCode(403);
216
            }
217
 
218
            if(file_exists($realFilePath)) {
219
 
220
                // Try to open file
221
                if (!is_readable($realFilePath)) {
222
                    return $this->getResponse()->setStatusCode(500);
223
                }
224
 
225
                // Get file size in bytes.
226
                $fileSize = filesize($realFilePath);
227
 
228
                // Get MIME type of the file.
229
                $mimeType = mime_content_type($realFilePath);
230
                if($mimeType===false) {
231
                    $mimeType = 'application/octet-stream';
232
                }
233
 
234
                $fileContent = file_get_contents($realFilePath);
235
 
236
                $response = $this->getResponse();
237
                $headers = $response->getHeaders();
238
                $headers->addHeaderLine('Content-type: ' . $mimeType);
239
                $headers->addHeaderLine('Content-length: ' . $fileSize);
240
 
241
                if($fileContent!==false) {
242
                    $response->setContent($fileContent);
243
                    return $response;
244
                } else {
245
                    return $this->getResponse()->setStatusCode(500);
246
                }
247
            } else {
248
                return $this->getResponse()->setStatusCode(404);
249
            }
250
 
251
        } else {
252
            $data = [
253
                'success' => false,
254
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
255
            ];
256
 
257
            return new JsonModel($data);
258
        }
259
    }
260
 
261
 
1 efrain 262
    public function indexAction()
263
    {
264
        /*
265
        if(!empty($this->config['leaderslinked.runmode.autologin'])) {
266
 
267
            $currentNetworkPlugin = $this->plugin('currentNetworkPlugin');
268
            $currentNetwork = $currentNetworkPlugin->getNetwork();
269
 
270
            $authService = new AuthenticationService();
271
            if(!$authService->hasIdentity()) {
272
                $adapter = new AuthEmailAdapter($this->adapter);
273
                $adapter->setData('santiago.olivera@leaderslinked.com',  $currentNetwork->id);
274
 
275
                $authService->authenticate($adapter);
276
            }
277
        }
257 efrain 278
        /*
1 efrain 279
        $currentUserPlugin = $this->plugin('currentUserPlugin');
280
        if ($currentUserPlugin->hasIdentity()) {
281
            return $this->redirect()->toRoute('dashboard');
282
        } else {
283
            return $this->redirect()->toRoute('signin');
284
        }*/
285
 
286
        $currentNetworkPlugin = $this->plugin('currentNetworkPlugin');
287
        $currentNetwork = $currentNetworkPlugin->getNetwork();
288
 
289
        $request = $this->getRequest();
290
        if ($request->isGet()) {
291
 
292
            $currentUserPlugin = $this->plugin('currentUserPlugin');
293
 
294
            if (empty($_SESSION['aes'])) {
295
                $_SESSION['aes'] = Functions::generatePassword(16);
296
            }
297
 
298
            if ($this->config['leaderslinked.runmode.sandbox']) {
299
                $site_key      = $this->config['leaderslinked.google_captcha.sandbox_site_key'];
300
            } else {
301
                $site_key      = $this->config['leaderslinked.google_captcha.production_site_key'];
302
            }
303
 
304
            $access_usign_social_networks = $this->config['leaderslinked.runmode.access_usign_social_networks'];
305
 
306
 
307
 
308
            $sandbox = $this->config['leaderslinked.runmode.sandbox'];
309
            if ($sandbox) {
310
                $google_map_key  = $this->config['leaderslinked.google_map.sandbox_api_key'];
311
            } else {
312
                $google_map_key  = $this->config['leaderslinked.google_map.production_api_key'];
313
            }
257 efrain 314
 
315
 
316
 
1 efrain 317
 
257 efrain 318
            $data = [
1 efrain 319
                'site_key'  => $site_key,
320
                'google_map_key' => $google_map_key,
321
                'aes'       => $_SESSION['aes'],
322
                'defaultNetwork' => $currentNetwork->default,
323
                'is_logged_in' =>  $currentUserPlugin->hasIdentity() ? true : false,
7 efrain 324
                'access_usign_social_networks' => $access_usign_social_networks &&  $currentNetwork && $currentNetwork->default == Network::DEFAULT_YES ? 'y' : 'n',
257 efrain 325
            ];
1 efrain 326
 
257 efrain 327
            if($currentNetwork->default == Network::DEFAULT_YES) {
328
                $currentUserPlugin = $this->plugin('currentUserPlugin');
329
                if ($currentUserPlugin->hasIdentity()) {
330
                  $externalCredentials = ExternalCredentials::getInstancia($this->config, $this->adapter);
331
                  $externalCredentials->getUserBy($currentUserPlugin->getUserId());
332
 
333
                  if($currentNetwork->xmpp_active) {
334
                      $data['xmpp_domain']      = $currentNetwork->xmpp_domain;
335
                      $data['xmpp_hostname']    = $currentNetwork->xmpp_hostname;
336
                      $data['xmpp_port']        = $currentNetwork->xmpp_port;
337
                      $data['xmpp_username']    = $externalCredentials->getUsernameXmpp();
338
                      $data['xmpp_password']    = $externalCredentials->getPasswordXmpp();
283 www 339
                      $data['inmail_username']    = $externalCredentials->getUsernameInmail();
340
                      $data['inmail_password']    = $externalCredentials->getPasswordInmail();
257 efrain 341
                  }
342
                }
343
            }
344
 
345
            return  new JsonModel($data);
346
 
1 efrain 347
 
348
        } else {
349
            $data = [
350
                'success' => false,
351
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
352
            ];
353
 
354
            return new JsonModel($data);
355
        }
356
 
357
    }
358
 
754 stevensc 359
    public function themeAction()
360
    {
361
        $currentNetworkPlugin = $this->plugin('currentNetworkPlugin');
362
        $currentNetwork = $currentNetworkPlugin->getNetwork();
1 efrain 363
 
754 stevensc 364
        $request = $this->getRequest();
365
 
366
        if(!$request->isGet()) {
367
            $data = [
368
                'success' => false,
369
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
370
            ];
371
 
372
            return new JsonModel($data);
373
        }
1 efrain 374
 
754 stevensc 375
        if(!$currentNetwork) {
376
            $data = [
377
                'success' => false,
378
                'data' => 'ERROR_NETWORK_NOT_FOUND'
379
            ];
1 efrain 380
 
754 stevensc 381
            return new JsonModel($data);
382
        }
383
 
384
        $data = [
385
            'success' => true,
386
            'theme' => [
387
                'bg-color' => $currentNetwork->css_bg_color,
388
                'body-bg-color' => $currentNetwork->css_body_bg_color,
389
                'bg-color-secondary' => $currentNetwork->css_bg_color_secondary,
390
                'font-color' => $currentNetwork->css_font_color,
391
                'font-color-hover' => $currentNetwork->css_font_color_hover,
392
                'title-color' => $currentNetwork->css_title_color,
393
                'subtitle-color' => $currentNetwork->css_subtitle_color,
394
                'icons-color' => $currentNetwork->css_icons_color,
395
                'button-bg' => $currentNetwork->css_button_bg,
396
                'button-bg-hover' => $currentNetwork->css_button_bg_hover,
397
                'button-text-color' => $currentNetwork->css_button_text_color,
398
                'button-bg-secondary' => $currentNetwork->css_button_bg_secondary,
399
                'button-bg-secondary-hover' => $currentNetwork->css_button_bg_secondary_hover,
400
                'button-text-color-secondary' => $currentNetwork->css_button_text_color_secondary,
401
                'button-bg-tertiary' => $currentNetwork->css_button_bg_tertiary,
402
                'button-text-color-tertiary' => $currentNetwork->css_button_text_color_tertiary,
403
                'border-primary' => $currentNetwork->css_border_primary,
404
                'border-trasnparent' => $currentNetwork->css_border_trasnparent,
405
                'chat-received' => $currentNetwork->css_chat_received,
406
                'chat-send' => $currentNetwork->css_chat_send,
407
                'chat-color' => $currentNetwork->css_chat_color,
408
                'light-gray' => $currentNetwork->css_light_gray,
409
                'gray' => $currentNetwork->css_gray,
410
                'danger' => $currentNetwork->css_danger,
411
                'danger-hover' => $currentNetwork->css_danger_hover,
412
                'online-green' => $currentNetwork->css_online_green,
413
                'border-gray-primary' => $currentNetwork->css_border_gray_primary,
414
                'white' => $currentNetwork->css_white,
415
                'white-backdrop' => $currentNetwork->css_white_backdrop,
416
                'border-radius' => $currentNetwork->css_border_radius,
417
                'light-shadow' => $currentNetwork->css_light_shadow,
418
                'shadow' => $currentNetwork->css_shadow
419
            ]
420
        ];
421
 
422
        return new JsonModel($data);
423
    }
424
 
1 efrain 425
    public function privacyPolicyAction()
426
    {
427
 
428
 
429
 
430
 
431
        $pageCode = Page::PAGE_CODE_PRIVACY_POLICY;
432
 
433
        $currentNetworkPlugin = $this->plugin('currentNetworkPlugin');
434
        $currentNetwork = $currentNetworkPlugin->getNetwork();
435
 
436
        $currentUserPlugin = $this->plugin('currentUserPlugin');
437
 
438
        $pageMapper = PageMapper::getInstance($this->adapter);
439
        $page = $pageMapper->fetchOneByCodeAndNetworkId($pageCode, $currentNetwork->id);
440
 
441
        if (!$page) {
442
            $networkMapper = NetworkMapper::getInstance($this->adapter);
443
            $network = $networkMapper->fetchOneByDefault();
444
 
445
            $pageDefault = $pageMapper->fetchOneByCodeAndNetworkId($pageCode, $network->id);
446
            if ($pageDefault) {
447
                $page = new Page();
448
                $page->network_id = $currentNetwork->id;
449
                $page->code = $pageDefault->code;
450
                $page->content = $pageDefault->content;
451
                $page->fixed = $pageDefault->fixed;
452
                $page->page_default_id = $pageDefault->id;
453
                $page->title = $pageDefault->title;
454
                $page->status = $pageDefault->status;
455
                $page->type = $pageDefault->type;
456
                $page->url = $pageDefault->url;
457
 
458
                $pageMapper->insert($page);
459
            }
460
        }
461
 
462
        return new JsonModel([
463
            'success' => true,
464
            'data' => [
465
                'title' => $page->title,
466
                'content' => $page->content
467
            ]
468
        ]);
469
 
470
    }
471
 
472
    public function cookiesAction()
473
    {
474
 
475
        $pageCode = Page::PAGE_CODE_COOKIES;
476
 
477
        $currentNetworkPlugin = $this->plugin('currentNetworkPlugin');
478
        $currentNetwork = $currentNetworkPlugin->getNetwork();
479
 
480
        $currentUserPlugin = $this->plugin('currentUserPlugin');
481
 
482
        $pageMapper = PageMapper::getInstance($this->adapter);
483
        $page = $pageMapper->fetchOneByCodeAndNetworkId($pageCode, $currentNetwork->id);
484
 
485
        if (!$page) {
486
            $networkMapper = NetworkMapper::getInstance($this->adapter);
487
            $network = $networkMapper->fetchOneByDefault();
488
 
489
            $pageDefault = $pageMapper->fetchOneByCodeAndNetworkId($pageCode, $network->id);
490
            if ($pageDefault) {
491
                $page = new Page();
492
                $page->network_id = $currentNetwork->id;
493
                $page->code = $pageDefault->code;
494
                $page->content = $pageDefault->content;
495
                $page->fixed = $pageDefault->fixed;
496
                $page->page_default_id = $pageDefault->id;
497
                $page->title = $pageDefault->title;
498
                $page->status = $pageDefault->status;
499
                $page->type = $pageDefault->type;
500
                $page->url = $pageDefault->url;
501
 
502
                $pageMapper->insert($page);
503
            }
504
        }
505
 
506
        return new JsonModel([
507
            'success' => true,
508
            'data' => [
509
                'title' => $page->title,
510
                'content' => $page->content
511
            ]
512
        ]);
513
    }
514
 
515
    public function professionalismPolicyAction()
516
    {
517
        $pageCode = Page::PAGE_CODE_PROFESSIONALISM_POLICY;
518
 
519
        $currentNetworkPlugin = $this->plugin('currentNetworkPlugin');
520
        $currentNetwork = $currentNetworkPlugin->getNetwork();
521
 
522
        $currentUserPlugin = $this->plugin('currentUserPlugin');
523
 
524
 
525
        $pageMapper = PageMapper::getInstance($this->adapter);
526
        $page = $pageMapper->fetchOneByCodeAndNetworkId($pageCode, $currentNetwork->id);
527
 
528
        if (!$page) {
529
            $networkMapper = NetworkMapper::getInstance($this->adapter);
530
            $network = $networkMapper->fetchOneByDefault();
531
 
532
            $pageDefault = $pageMapper->fetchOneByCodeAndNetworkId($pageCode, $network->id);
533
            if ($pageDefault) {
534
                $page = new Page();
535
                $page->network_id = $currentNetwork->id;
536
                $page->code = $pageDefault->code;
537
                $page->content = $pageDefault->content;
538
                $page->fixed = $pageDefault->fixed;
539
                $page->page_default_id = $pageDefault->id;
540
                $page->title = $pageDefault->title;
541
                $page->status = $pageDefault->status;
542
                $page->type = $pageDefault->type;
543
                $page->url = $pageDefault->url;
544
 
545
                $pageMapper->insert($page);
546
            }
547
        }
548
 
549
        return new JsonModel([
550
            'success' => true,
551
            'data' => [
552
                'title' => $page->title,
553
                'content' => $page->content
554
            ]
555
        ]);
556
    }
557
 
558
 
559
    public function termsAndConditionsAction()
560
    {
561
        $pageCode = Page::PAGE_CODE_TERMS_AND_CONDITIONS;
562
 
563
        $currentNetworkPlugin = $this->plugin('currentNetworkPlugin');
564
        $currentNetwork = $currentNetworkPlugin->getNetwork();
565
 
566
        $currentUserPlugin = $this->plugin('currentUserPlugin');
567
 
568
 
569
        $pageMapper = PageMapper::getInstance($this->adapter);
570
        $page = $pageMapper->fetchOneByCodeAndNetworkId($pageCode, $currentNetwork->id);
571
 
572
        if (!$page) {
573
            $networkMapper = NetworkMapper::getInstance($this->adapter);
574
            $network = $networkMapper->fetchOneByDefault();
575
 
576
            $pageDefault = $pageMapper->fetchOneByCodeAndNetworkId($pageCode, $network->id);
577
            if ($pageDefault) {
578
                $page = new Page();
579
                $page->network_id = $currentNetwork->id;
580
                $page->code = $pageDefault->code;
581
                $page->content = $pageDefault->content;
582
                $page->fixed = $pageDefault->fixed;
583
                $page->page_default_id = $pageDefault->id;
584
                $page->title = $pageDefault->title;
585
                $page->status = $pageDefault->status;
586
                $page->type = $pageDefault->type;
587
                $page->url = $pageDefault->url;
588
 
589
                $pageMapper->insert($page);
590
            }
591
        }
592
 
593
        return new JsonModel([
594
            'success' => true,
595
            'data' => [
596
                'title' => $page->title,
597
                'content' => $page->content
598
            ]
599
        ]);
600
    }
601
 
602
    public function checkSessionAction()
603
    {
604
 
605
        $request = $this->getRequest();
606
        if ($request->isGet()) {
607
 
608
            $currentUserPlugin = $this->plugin('currentUserPlugin');
609
            if (!$currentUserPlugin->hasIdentity()) {
610
                //$flashMessenger = $this->plugin('FlashMessenger');
611
                //$flashMessenger->addErrorMessage('ERROR_SESSION_NOT_FOUND');
612
 
613
                $response = [
614
                    'success' => false,
615
                    'data' => [
616
                        'message' =>  '', //ERROR_SESSION_NOT_FOUND',
617
                        'url' => $this->url()->fromRoute('signout')
618
                    ]
619
                ];
620
 
621
                return new JsonModel($response);
622
            }
623
 
624
            $currentUser = $currentUserPlugin->getUser();
625
 
626
 
627
            if ($currentUser->last_activity_on) {
628
                $last_activity_on = strtotime($currentUser->last_activity_on);
629
            } else {
630
                $last_activity_on = strtotime('-1 day');
631
            }
632
 
633
            $expiry_time = $last_activity_on + $this->config['leaderslinked.security.last_activity_expired'];
634
            if (time() > $expiry_time) {
635
                //$flashMessenger = $this->plugin('FlashMessenger');
636
                //$flashMessenger->addErrorMessage('ERROR_SESSION_EXPIRED');
637
 
638
                $response = [
639
                    'success' => false,
640
                    'data' => [
641
                        'message' => 'ERROR_SESSION_EXPIRED',
642
                        'url' => $this->url()->fromRoute('signout')
643
                    ]
644
                ];
645
            } else {
646
                $notificationMapper = NotificationMapper::getInstance($this->adapter);
647
                $total_notifications = $notificationMapper->fetchUnreadNotificationsCount($currentUser->id);
648
 
283 www 649
                /*
1 efrain 650
                $messageMapper = MessageMapper::getInstance($this->adapter);
283 www 651
                $total_messages =  $messageMapper->fetchCountUnreadMessagesReceiverId($currentUser->id);*/
652
 
653
                $total_messages = 0;
1 efrain 654
                $response = [
655
                    'success' => true,
656
                    'data' => [
657
                        'total_notifications' => $total_notifications,
658
                        'total_messages' => $total_messages
659
                    ]
660
                ];
661
            }
662
        } else {
663
            $response = [
664
                'success' => false,
665
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
666
            ];
667
        }
668
 
669
        return new JsonModel($response);
670
    }
671
 
672
 
673
 
674
 
675
 
676
    public function incTotalExternalSharedAction()
677
    {
678
 
679
        $request = $this->getRequest();
680
        if ($request->isPost()) {
681
            $currentUserPlugin = $this->plugin('currentUserPlugin');
682
            $currentUser = $currentUserPlugin->getUser();
683
 
684
            $currentNetworkPlugin = $this->plugin('currentNetworkPlugin');
685
            $currentNetwork = $currentNetworkPlugin->getNetwork();
686
 
687
            $code       = $this->params()->fromRoute('code');
688
            $type       = $this->params()->fromRoute('type');
689
            $user       = $this->params()->fromRoute('user');
690
            $timestamp  = intval($this->params()->fromRoute('timestamp'), 10);
691
            $rand       = intval($this->params()->fromRoute('rand'), 10);
692
            $password   = $this->params()->fromRoute('password');
693
 
694
 
695
 
696
            $checkpassword = '';
697
 
698
 
699
            $userCheck = '';
700
            if ($user && $timestamp > 0 && $rand > 0 && $password) {
701
                $userMapper = UserMapper::getInstance($this->adapter);
702
                $userCheck = $userMapper->fetchOneByUuid($user);
703
                if ($userCheck) {
704
                    $checkpassword  = md5('user-' . $userCheck->uuid . '-' . $type . '-' . $code . '-timestamp-' . $timestamp . '-rand-' . $rand . '-share-key-' . $userCheck->share_key);
705
                }
706
            }
707
 
708
            if (empty($password) || $password != $checkpassword) {
709
                $data = [
710
                    'success' => false,
711
                    'data' => 'ERROR_UNAUTHORIZED'
712
                ];
713
 
714
                return new JsonModel($data);
715
            }
716
 
717
 
718
 
719
 
720
 
721
 
722
 
723
 
724
 
725
            if ($type == 'feed' && $code) {
726
                $feedMapper =  FeedMapper::getInstance($this->adapter);
727
                $feed = $feedMapper->fetchOneByUuidAnyStatus($code);
728
 
729
 
730
                if ($feed) {
731
 
732
                    if ($feed->network_id != $currentNetwork->id) {
733
                        $data = [
734
                            'success' => false,
735
                            'data' =>    'ERROR_UNAUTHORIZED'
736
                        ];
737
 
738
                        return new JsonModel($data);
739
                    }
740
 
741
                    if ($feedMapper->incTotalExternalShared($feed->id)) {
742
                        $total = $feedMapper->fetchTotalExternalShared($feed->id);
743
 
744
                        $this->logger->info('El usuario : ' . trim($currentUser->first_name . ' ' . $currentUser->last_name) . ' (' . $currentUser->email . ') compartio externamente el feed : ' . $feed->title);
745
 
746
                        $data = [
747
                            'success' => true,
748
                            'data' => $total,
749
                        ];
750
 
751
                        return new JsonModel($data);
752
                    } else {
753
                        $data = [
754
                            'success' => false,
755
                            'data' => $feedMapper->getError()
756
                        ];
757
 
758
                        return new JsonModel($data);
759
                    }
760
                } else {
761
                    $data = [
762
                        'success' => false,
763
                        'data' => 'ERROR_FEED_NOT_FOUND',
764
                    ];
765
 
766
                    return new JsonModel($data);
767
                }
768
            } else if ($type == 'post' && $code) {
769
 
770
                $postMapper = PostMapper::getInstance($this->adapter);
771
                $post = $postMapper->fetchOneByUuid($code);
772
 
773
                if ($post && $post->status == Post::STATUS_ACTIVE) {
774
 
775
                    if ($post->network_id != $currentNetwork->id) {
776
                        $data = [
777
                            'success' => false,
778
                            'data' =>    'ERROR_UNAUTHORIZED'
779
                        ];
780
 
781
                        return new JsonModel($data);
782
                    }
783
 
784
 
785
                    if ($postMapper->incTotalExternalShared($post->id)) {
786
                        $total = $postMapper->fetchTotalExternalShared($post->id);
787
 
788
                        $this->logger->info('El usuario : ' . trim($currentUser->first_name . ' ' . $currentUser->last_name) . ' (' . $currentUser->email . ') compartio externamente el post : ' . $post->title);
789
 
790
 
791
 
792
                        $data = [
793
                            'success' => true,
794
                            'data' => $total,
795
                        ];
796
 
797
                        return new JsonModel($data);
798
                    } else {
799
                        $data = [
800
                            'success' => false,
801
                            'data' => $postMapper->getError()
802
                        ];
803
 
804
                        return new JsonModel($data);
805
                    }
806
                } else {
807
                    $data = [
808
                        'success' => false,
809
                        'data' => 'ERROR_POST_NOT_FOUND'
810
                    ];
811
 
812
                    return new JsonModel($data);
813
                }
814
            }
815
        } else {
816
            $response = [
817
                'success' => false,
818
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
819
            ];
820
 
821
            return new JsonModel($response);
822
        }
823
    }
824
 
247 efrain 825
 
826
 
827
    public function languageAction()
828
    {
829
        $request = $this->getRequest();
830
 
831
        if ($request->isGet()) {
832
 
833
 
834
 
835
            $pathname = dirname(__DIR__);
836
            $filename = $pathname . DIRECTORY_SEPARATOR . 'i18n' . DIRECTORY_SEPARATOR . 'spanish.php';
837
 
838
 
839
            if(file_exists($filename)) {
840
 
841
                $arr =  include $filename;
842
                $arr = array_filter($arr, function($a)  {
843
                    return strpos($a, 'LABEL_') !== false;
844
                }, ARRAY_FILTER_USE_KEY);
845
 
846
 
847
                $data = [];
848
                foreach($arr as $key => $value)
849
                {
850
                    $key = str_replace('LABEL_', 'LANG_', $key);
851
 
852
                    $data[ $key ] = $value;
853
 
854
                }
855
 
856
 
857
 
858
                $response = [
859
                    'success' => true,
860
                    'data' => $data
861
                ];
862
 
863
                return new JsonModel($response);
864
 
865
 
866
            } else {
867
                $response = [
868
                    'success' => false,
869
                    'data' => 'ERROR_METHOD_NOT_ALLOWED'
870
                ];
871
 
872
                return new JsonModel($response);
873
            }
874
 
875
 
876
 
877
 
878
 
879
        } else {
880
            $response = [
881
                'success' => false,
882
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
883
            ];
884
 
885
            return new JsonModel($response);
886
        }
887
 
888
 
889
    }
890
 
1 efrain 891
    public function shareAction()
892
    {
893
        $request = $this->getRequest();
894
        if ($request->isGet()) {
895
            $currentUserPlugin = $this->plugin('currentUserPlugin');
896
            $currentUser = $currentUserPlugin->getUser();
247 efrain 897
 
1 efrain 898
            $code       = $this->params()->fromRoute('code');
899
            $type       = $this->params()->fromRoute('type');
900
            $user       = $this->params()->fromRoute('user');
901
            $timestamp  = intval($this->params()->fromRoute('timestamp'), 10);
902
            $rand       = intval($this->params()->fromRoute('rand'), 10);
903
            $password   = $this->params()->fromRoute('password');
247 efrain 904
 
905
 
906
 
1 efrain 907
            $checkpassword = '';
247 efrain 908
 
909
 
1 efrain 910
            $userCheck = '';
911
            if ($user && $timestamp > 0 && $rand > 0 && $password) {
912
                $userMapper = UserMapper::getInstance($this->adapter);
913
                $userCheck = $userMapper->fetchOneByUuid($user);
914
                if ($userCheck) {
915
                    $checkpassword  = md5('user-' . $userCheck->uuid . '-' . $type . '-' . $code . '-timestamp-' . $timestamp . '-rand-' . $rand . '-share-key-' . $userCheck->share_key);
916
                }
917
            }
247 efrain 918
 
1 efrain 919
            if (empty($password) || $password != $checkpassword) {
920
                $data = [
921
                    'success' => false,
922
                    'data' => 'ERROR_UNAUTHORIZED'
923
                ];
247 efrain 924
 
1 efrain 925
                return new JsonModel($data);
926
            }
247 efrain 927
 
1 efrain 928
            /*
247 efrain 929
             $share_params = [
930
             'type' => $type,
931
             'code' => $code,
932
             'user' => $currentUser->uuid,
933
             'timestamp' => $timestamp,
934
             'rand' => $rand,
935
             'password' => $password,
936
             ];
937
 
938
             $share_increment_external_counter_url = $this->url()->fromRoute('share/increment-external-counter', $share_params  , ['force_canonical' => true]);
939
             */
1 efrain 940
 
254 efrain 941
            $hostname = empty($_SERVER['HTTP_HOST']) ?  '' : $_SERVER['HTTP_HOST'];
247 efrain 942
 
254 efrain 943
            $networkMapper = NetworkMapper::getInstance($this->adapter);
944
            $network = $networkMapper->fetchOneByHostnameForFrontend($hostname);
247 efrain 945
 
254 efrain 946
            if(!$network) {
947
                $network = $networkMapper->fetchOneByDefault();
137 efrain 948
            }
247 efrain 949
 
254 efrain 950
            $hostname = trim($network->main_hostname);
951
            $base_share = 'https://' . $hostname;
247 efrain 952
 
1 efrain 953
            $share_url          = $base_share . $_SERVER['REQUEST_URI'];
954
            $share_image        = $base_share . '/images/ll-logo.png';
955
            $share_title        = '';
956
            $share_description  = '';
249 efrain 957
 
247 efrain 958
 
959
 
1 efrain 960
            if ($type == 'feed' && $code) {
961
                $feedMapper =  FeedMapper::getInstance($this->adapter);
962
                $feed = $feedMapper->fetchOneByUuidAnyStatus($code);
247 efrain 963
 
1 efrain 964
                // if($feed && $feed->status == Feed::STATUS_PUBLISHED) {
247 efrain 965
 
1 efrain 966
                if ($feed) {
247 efrain 967
 
1 efrain 968
                    $share_title = $feed->title ? $feed->title : '';
247 efrain 969
 
1 efrain 970
                    if ($feed->posted_or_shared  == Feed::SHARED) {
247 efrain 971
 
972
 
1 efrain 973
                        $feed = $feedMapper->fetchOneAnyStatus($feed->shared_feed_id);
974
                        if ($feed->title) {
247 efrain 975
 
1 efrain 976
                            $share_title = $share_title  .  ' -  ' . $feed->title;
977
                        }
978
                    }
247 efrain 979
 
980
 
1 efrain 981
                    $share_description = $feed->description;
247 efrain 982
 
1 efrain 983
                    $image_name = '';
345 www 984
                    if ($feed->file_type == Storage::FILE_TYPE_IMAGE) {
247 efrain 985
 
1 efrain 986
                        $image_name = $feed->file_name;
987
                    } else  if ($feed->file_image_preview) {
988
                        $image_name = $feed->file_image_preview;
989
                    }
247 efrain 990
 
991
 
992
 
993
                        if ($image_name) {
994
 
995
                            $source = $this->config['leaderslinked.fullpath.feed'] . $feed->uuid . DIRECTORY_SEPARATOR . $image_name;
996
 
257 efrain 997
                            $target_path = $this->config['leaderslinked.frontend.frontend']. DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'feed' . DIRECTORY_SEPARATOR . $feed->uuid;
998
 
247 efrain 999
 
1000
 
257 efrain 1001
                            //$target_path = 'public' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'feed' . DIRECTORY_SEPARATOR . $feed->uuid;
1002
 
247 efrain 1003
                            if (!file_exists($target_path)) {
1004
                                mkdir($target_path, 0755, true);
1005
                            }
1006
 
1007
 
1008
 
1009
                            $target = $target_path . DIRECTORY_SEPARATOR . $image_name;
1010
 
1011
 
1012
 
1013
                            if (!file_exists($target)) {
1014
 
1015
                                copy($source, $target);
1016
                                $share_image =  $base_share . '/images/feed/' . $feed->uuid . '/' . $image_name;
1017
                            } else {
1018
                                $share_image =  $base_share . '/images/feed/' . $feed->uuid . '/' . $image_name;
1019
                            }
1 efrain 1020
                        }
1021
                } else {
247 efrain 1022
 
249 efrain 1023
                    return new JsonModel([
1024
                        'success' => false,
1025
                        'data' => 'ERROR_FEED_OR_POST_SHARED'
1026
                    ]);
1 efrain 1027
                }
1028
            } else if ($type == 'post' && $code) {
247 efrain 1029
 
1 efrain 1030
                $postMapper = PostMapper::getInstance($this->adapter);
1031
                $post = $postMapper->fetchOneByUuid($code);
247 efrain 1032
 
1 efrain 1033
                if ($post && $post->status == Post::STATUS_ACTIVE) {
1034
                    $share_title = $post->title;
1035
                    $share_description = $post->description;
247 efrain 1036
 
1037
 
1 efrain 1038
                    if ($post->image) {
1039
                        $source = $this->config['leaderslinked.fullpath.post'] . $post->uuid . DIRECTORY_SEPARATOR . $post->image;
247 efrain 1040
 
1041
 
257 efrain 1042
                       // $target_path = 'public' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'post' . DIRECTORY_SEPARATOR . $post->uuid;
247 efrain 1043
 
257 efrain 1044
                        $target_path = $this->config['leaderslinked.frontend.frontend'] . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'post' . DIRECTORY_SEPARATOR . $post->uuid;
1045
 
1 efrain 1046
                        if (!file_exists($target_path)) {
1047
                            mkdir($target_path, 0755, true);
1048
                        }
247 efrain 1049
 
1050
 
1051
 
1 efrain 1052
                        $target = $target_path . DIRECTORY_SEPARATOR . $post->image;
247 efrain 1053
 
1054
 
1055
 
1 efrain 1056
                        if (!file_exists($target)) {
247 efrain 1057
 
1 efrain 1058
                            copy($source, $target);
1059
                            $share_image =  $base_share . '/images/post/' . $post->uuid . '/' . $post->image;
1060
                        } else {
1061
                            $share_image =  $base_share . '/images/post/' . $post->uuid . '/' . $post->image;
1062
                        }
1063
                    }
1064
                } else {
247 efrain 1065
 
249 efrain 1066
                    return new JsonModel([
1067
                        'success' => false,
1068
                        'data' => 'ERROR_FEED_OR_POST_SHARED'
1069
                    ]);
1 efrain 1070
                }
1071
            }
247 efrain 1072
 
1 efrain 1073
            if ($currentUserPlugin->hasIdentity()) {
1074
                $currentUser = $currentUserPlugin->getUser();
1075
                if ($userCheck && $userCheck->status == User::STATUS_ACTIVE && $userCheck->id != $currentUser->id) {
247 efrain 1076
 
1 efrain 1077
                    $connectionMapper = ConnectionMapper::getInstance($this->adapter);
1078
                    $connection = $connectionMapper->fetchOneByUserId1AndUserId2($currentUser->id, $userCheck->id);
247 efrain 1079
 
1 efrain 1080
                    if ($connection) {
247 efrain 1081
 
1 efrain 1082
                        if ($connection->status != Connection::STATUS_ACCEPTED) {
1083
                            $connectionMapper->approve($connection);
1084
                        }
1085
                    } else {
1086
                        $connection = new Connection();
1087
                        $connection->request_from = $currentUser->id;
1088
                        $connection->request_to = $userCheck->id;
1089
                        $connection->status = Connection::STATUS_ACCEPTED;
247 efrain 1090
 
1 efrain 1091
                        $connectionMapper->insert($connection);
1092
                    }
1093
                }
1094
 
1095
            } else {
249 efrain 1096
                $this->cache->setItem('user_share_invitation', [
1097
                    'code' => $code,
1098
                    'type' => $type,
1099
                    'user' => $user
1100
                ]);
1 efrain 1101
            }
1102
            return new JsonModel([
249 efrain 1103
                'success' => true,
1104
                'data' => [
1105
                    'image' => $share_image,
1106
                    'url' => $share_url,
1107
                    'title' => strip_tags($share_title),
1108
                    'description' => strip_tags($share_description),
1109
                ]
247 efrain 1110
 
1 efrain 1111
            ]);
1112
 
1113
        } else {
1114
            $response = [
1115
                'success' => false,
1116
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
1117
            ];
1118
 
1119
            return new JsonModel($response);
1120
        }
1121
    }
333 www 1122
 
1123
 
1 efrain 1124
}