Proyectos de Subversion LeadersLinked - Services

Rev

Rev 247 | Rev 254 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

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