Proyectos de Subversion LeadersLinked - Services

Rev

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