Proyectos de Subversion LeadersLinked - Services

Rev

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