Proyectos de Subversion LeadersLinked - Services

Rev

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