Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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

Rev Autor Línea Nro. Línea
1 www 1
<?php
2466 stevensc 2
 
1 www 3
declare(strict_types=1);
4
 
5
namespace LeadersLinked\Controller;
6
 
7
use Laminas\Db\Adapter\AdapterInterface;
8
use Laminas\Cache\Storage\Adapter\AbstractAdapter;
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\CompanyMapper;
16
use LeadersLinked\Mapper\CompanyUserMapper;
17
use LeadersLinked\Model\Company;
18
use LeadersLinked\Mapper\PageMapper;
19
use LeadersLinked\Mapper\MessageMapper;
2466 stevensc 20
use LeadersLinked\Mapper\UserMapper;
21
use LeadersLinked\Mapper\UserProfileMapper;
1 www 22
use LeadersLinked\Mapper\CompanyUserRoleMapper;
23
use LeadersLinked\Model\Role;
24
use LeadersLinked\Library\Functions;
2466 stevensc 25
use LeadersLinked\Mapper\ConnectionMapper;
26
use LeadersLinked\Mapper\LocationMapper;
1 www 27
use LeadersLinked\Mapper\PostMapper;
2466 stevensc 28
use LeadersLinked\Mapper\ProfileVisitMapper;
1 www 29
use LeadersLinked\Model\Post;
3138 efrain 30
use LeadersLinked\Mapper\UtilMapper;
3298 efrain 31
use LeadersLinked\Mapper\FeedMapper;
32
use LeadersLinked\Model\Feed;
33
use LeadersLinked\Model\User;
34
use LeadersLinked\Model\Connection;
1 www 35
 
36
class HomeController extends AbstractActionController
37
{
38
    /**
39
     *
40
     * @var AdapterInterface
41
     */
42
    private $adapter;
2466 stevensc 43
 
44
 
1 www 45
    /**
46
     *
47
     * @var AbstractAdapter
48
     */
49
    private $cache;
2466 stevensc 50
 
1 www 51
    /**
52
     *
53
     * @var  LoggerInterface
54
     */
55
    private $logger;
56
 
2466 stevensc 57
 
1 www 58
    /**
59
     *
60
     * @var array
61
     */
62
    private $config;
2466 stevensc 63
 
1 www 64
    /**
65
     *
66
     * @param AdapterInterface $adapter
67
     * @param AbstractAdapter $cache
68
     * @param LoggerInterface $logger
69
     * @param array $config
70
     */
2466 stevensc 71
    public function __construct($adapter, $cache, $logger,  $config)
1 www 72
    {
73
        $this->adapter      = $adapter;
74
        $this->cache        = $cache;
75
        $this->logger       = $logger;
76
        $this->config       = $config;
77
    }
2466 stevensc 78
 
79
 
80
 
1 www 81
    public function indexAction()
82
    {
83
 
3298 efrain 84
        $currentUserPlugin = $this->plugin('currentUserPlugin');
3302 efrain 85
        if ($currentUserPlugin->hasIdentity()) {
2466 stevensc 86
            return $this->redirect()->toRoute('dashboard');
1 www 87
        } else {
2466 stevensc 88
            return $this->redirect()->toRoute('signin');
1 www 89
        }
90
    }
2466 stevensc 91
 
92
 
93
 
94
 
1 www 95
    public function privacyPolicyAction()
96
    {
97
        $pageMapper = PageMapper::getInstance($this->adapter);
98
        $page = $pageMapper->fetchOne(Page::PAGE_ID_PRIVACY_POLICY);
2466 stevensc 99
 
1 www 100
        $this->layout()->setTemplate('layout/layout.phtml');
101
        $viewModel = new ViewModel();
102
        $viewModel->setTemplate('leaders-linked/home/privacy-policy.phtml');
103
        $viewModel->setVariable('page', $page);
2466 stevensc 104
        return $viewModel;
1 www 105
    }
2466 stevensc 106
 
1 www 107
    public function cookiesAction()
108
    {
109
        $pageMapper = PageMapper::getInstance($this->adapter);
110
        $page = $pageMapper->fetchOne(Page::PAGE_ID_COOKIES);
2466 stevensc 111
 
1 www 112
        $this->layout()->setTemplate('layout/layout.phtml');
113
        $viewModel = new ViewModel();
114
        $viewModel->setTemplate('leaders-linked/home/cookies.phtml');
115
        $viewModel->setVariable('page', $page);
2466 stevensc 116
        return $viewModel;
1 www 117
    }
2466 stevensc 118
 
1 www 119
    public function professionalismPolicyAction()
120
    {
121
        //
2466 stevensc 122
 
1 www 123
        $pageMapper = PageMapper::getInstance($this->adapter);
124
        $page = $pageMapper->fetchOne(Page::PAGE_ID_PROFESSIONALISM_POLICY);
2466 stevensc 125
 
1 www 126
        $this->layout()->setTemplate('layout/layout.phtml');
127
        $viewModel = new ViewModel();
128
        $viewModel->setTemplate('leaders-linked/home/professionalism-policy');
129
        $viewModel->setVariable('page', $page);
2466 stevensc 130
        return $viewModel;
1 www 131
    }
2466 stevensc 132
 
133
 
1 www 134
    public function termsAndConditionsAction()
135
    {
136
        $pageMapper = PageMapper::getInstance($this->adapter);
137
        $page = $pageMapper->fetchOne(Page::PAGE_ID_TERMS_AND_CONDITIONS);
2466 stevensc 138
 
1 www 139
        $this->layout()->setTemplate('layout/layout.phtml');
140
        $viewModel = new ViewModel();
141
        $viewModel->setTemplate('leaders-linked/home/terms-and-conditions.phtml');
142
        $viewModel->setVariable('page', $page);
2466 stevensc 143
        return $viewModel;
1 www 144
    }
2466 stevensc 145
 
1 www 146
    public function checkSessionAction()
147
    {
148
 
149
        $request = $this->getRequest();
2466 stevensc 150
        if ($request->isGet()) {
151
 
1 www 152
            $currentUserPlugin = $this->plugin('currentUserPlugin');
2466 stevensc 153
            if (!$currentUserPlugin->hasIdentity()) {
1 www 154
                $flashMessenger = $this->plugin('FlashMessenger');
155
                $flashMessenger->addErrorMessage('ERROR_SESSION_NOT_FOUND');
2466 stevensc 156
 
1 www 157
                $response = [
158
                    'success' => false,
159
                    'data' => [
160
                        'message' =>  'ERROR_SESSION_NOT_FOUND',
161
                        'url' => $this->url()->fromRoute('signout')
162
                    ]
163
                ];
2466 stevensc 164
 
1 www 165
                return new JsonModel($response);
166
            }
2466 stevensc 167
 
1 www 168
            $currentUser = $currentUserPlugin->getUser();
2466 stevensc 169
 
170
 
171
            if ($currentUser->last_activity_on) {
1 www 172
                $last_activity_on = strtotime($currentUser->last_activity_on);
173
            } else {
174
                $last_activity_on = strtotime('-1 day');
175
            }
176
 
177
            $expiry_time = $last_activity_on + $this->config['leaderslinked.security.last_activity_expired'];
178
            if (time() > $expiry_time) {
2466 stevensc 179
                //$flashMessenger = $this->plugin('FlashMessenger');
180
                //$flashMessenger->addErrorMessage('ERROR_SESSION_EXPIRED');
1 www 181
 
182
                $response = [
183
                    'success' => false,
184
                    'data' => [
185
                        'message' => 'ERROR_SESSION_EXPIRED',
186
                        'url' => $this->url()->fromRoute('signout')
187
                    ]
188
                ];
189
            } else {
190
                $notificationMapper = NotificationMapper::getInstance($this->adapter);
191
                $total_notifications = $notificationMapper->fetchUnreadNotificationsCount($currentUser->id);
2466 stevensc 192
 
193
                $messageMapper = MessageMapper::getInstance($this->adapter);
1 www 194
                $total_messages =  $messageMapper->fetchCountUnreadMessagesReceiverId($currentUser->id);
195
                $response = [
196
                    'success' => true,
197
                    'data' => [
198
                        'total_notifications' => $total_notifications,
199
                        'total_messages' => $total_messages
200
                    ]
201
                ];
202
            }
203
        } else {
204
            $response = [
205
                'success' => false,
206
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
207
            ];
208
        }
2466 stevensc 209
 
1 www 210
        return new JsonModel($response);
211
    }
2466 stevensc 212
 
3262 efrain 213
 
1 www 214
    public function postAction()
215
    {
3364 efrain 216
        $request = $this->getRequest();
217
        if ($request->isGet()) {
218
            $currentUserPlugin = $this->plugin('currentUserPlugin');
219
            $currentUser = $currentUserPlugin->getUser();
220
 
221
 
222
            $id = $this->params()->fromRoute('id');
223
 
224
            $postMapper = PostMapper::getInstance($this->adapter);
225
            $post = $postMapper->fetchOneByUuid($id);
226
 
227
            if (!$post || $post->status != Post::STATUS_ACTIVE) {
228
                $flashMessenger = $this->plugin('FlashMessenger');
229
 
230
                if (!$id) {
231
                    $flashMessenger->addErrorMessage('ERROR_POST_NOT_AVAILABLE');
232
                    return $this->redirect()->toRoute('dashboard');
233
                }
1 www 234
            }
3364 efrain 235
 
236
 
237
 
238
            $timestamp = time();
239
 
240
            list($usec, $sec) = explode(' ', microtime());
241
            $seed = intval($sec + ((float) $usec * 100000));
242
            mt_srand($seed, MT_RAND_MT19937);
243
            $rand =  mt_rand();
244
 
245
 
2466 stevensc 246
 
3366 efrain 247
            $password  = md5('user-' . $currentUser->uuid . '-post-' . $post->uuid . '-timestamp-' . $timestamp . '-rand-' . $rand . '-share-key-' . $currentUser->share_key) ;
3298 efrain 248
 
3364 efrain 249
 
3380 efrain 250
            $share_params = [
3379 efrain 251
                'type' => 'post',
252
                'code' => $post->uuid,
253
                'user' => $currentUser->uuid,
254
                'timestamp' => $timestamp,
255
                'rand' => $rand,
256
                'password' => $password,
257
            ];
3364 efrain 258
 
3379 efrain 259
 
3380 efrain 260
            $share_external_url = $this->url()->fromRoute('share',$share_params  , ['force_canonical' => true]);
3379 efrain 261
 
3364 efrain 262
            $this->layout()->setTemplate('layout/layout.phtml');
263
            $viewModel = new ViewModel();
264
            $viewModel->setTemplate('leaders-linked/home/post.phtml');
265
            $viewModel->setVariables([
266
                'post' => $post,
267
                'id' => $post->id,
268
                'uuid' => $post->uuid,
269
                'title' => $post->title,
270
                'description' => $post->description,
271
                'url' => $post->url,
272
                'date' => $post->date,
273
                'status' => $post->status,
274
                'image' => $post->image,
275
                'file' => $post->file,
276
                'added_on' => $post->added_on,
3379 efrain 277
                'share_external_url' =>  $share_external_url,
3364 efrain 278
 
279
            ]);
280
            return $viewModel;
281
 
282
        } else {
283
            $response = [
284
                'success' => false,
285
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
286
            ];
287
 
288
            return new JsonModel($response);
289
        }
1 www 290
    }
3298 efrain 291
 
292
    public function shareAction()
293
    {
294
        $request = $this->getRequest();
295
        if ($request->isGet()) {
296
            $currentUserPlugin = $this->plugin('currentUserPlugin');
297
            $currentUser = $currentUserPlugin->getUser();
298
 
3364 efrain 299
            $code       = $this->params()->fromRoute('code');
300
            $type       = $this->params()->fromRoute('type');
3379 efrain 301
            $user       = $this->params()->fromRoute('user');
302
            $timestamp  = intval($this->params()->fromRoute('timestamp'), 10);
303
            $rand       = intval($this->params()->fromRoute('rand'), 10);
304
            $password   = $this->params()->fromRoute('password');
3377 efrain 305
 
3379 efrain 306
 
307
 
3364 efrain 308
            $checkpassword = '';
309
 
3383 efrain 310
 
3364 efrain 311
            $userCheck = '';
312
            if($user && $timestamp > 0 && $rand > 0 && $password) {
313
                $userMapper = UserMapper::getInstance($this->adapter);
314
                $userCheck = $userMapper->fetchOneByUuid($user);
315
                if($userCheck) {
3366 efrain 316
                    $checkpassword  = md5('user-' . $userCheck->uuid . '-'.$type.'-' . $code . '-timestamp-' . $timestamp . '-rand-' . $rand . '-share-key-' . $userCheck->share_key) ;
3364 efrain 317
                }
318
            }
3349 efrain 319
 
3364 efrain 320
            if(empty($password) || $password != $checkpassword) {
321
                $data = [
3349 efrain 322
                    'success' => false,
323
                    'data' => 'ERROR_UNAUTHORIZED'
324
                ];
325
 
3364 efrain 326
                return new JsonModel($data);
3383 efrain 327
            }
3371 efrain 328
 
3364 efrain 329
 
330
 
331
 
332
 
3357 efrain 333
 
3298 efrain 334
 
3335 efrain 335
 
336
            if(strpos(strtolower($_SERVER['REQUEST_SCHEME']), 'https') === false) {
3355 efrain 337
                $base_share = 'http://' . $_SERVER['HTTP_HOST'];
3298 efrain 338
            } else {
3355 efrain 339
                $base_share = 'https://' . $_SERVER['HTTP_HOST'];
3298 efrain 340
            }
341
 
342
 
3374 efrain 343
            /*
3372 efrain 344
            echo '<pre>';
345
            print_r($_SERVER);
346
            echo '</pre>';
3374 efrain 347
            */
3298 efrain 348
 
3372 efrain 349
 
3355 efrain 350
            $share_url          = $base_share . $_SERVER['REQUEST_URI'];
351
            $share_image        = $base_share . '/images/ll-logo.png';
3298 efrain 352
            $share_title        = '';
353
            $share_description  = '';
3364 efrain 354
 
3298 efrain 355
 
356
            if($type == 'feed' && $code ) {
357
                $feedMapper =  FeedMapper::getInstance($this->adapter);
358
                $feed = $feedMapper->fetchOneByUuid($code);
359
 
360
                if($feed && $feed->status == Feed::STATUS_PUBLISHED) {
3453 efrain 361
 
3298 efrain 362
                    $share_title = $feed->title ? $feed->title : $feed->description;
3453 efrain 363
 
364
                    if($feed->posted_or_shared  == Feed::SHARED) {
365
 
366
 
367
                        $feed = $feedMapper->fetchOneAnyStatus($feed->shared_feed_id);
368
                        if($feed->title) {
369
 
370
                            $share_title = $share_title  .  ' -  ' .$feed->title;
371
                        }
372
 
373
 
374
                    }
375
 
376
 
3298 efrain 377
                    $share_description = $feed->description;
378
 
379
                    $image_name = '';
380
                    if($feed->file_type == Feed::FILE_TYPE_IMAGE) {
381
 
382
                        $image_name = $feed->file_name;
383
 
384
                    } else  if($feed->file_image_preview) {
385
                        $image_name = $feed->file_image_preview;
386
                    }
387
 
388
 
389
 
390
                    if( $image_name ) {
391
 
392
                        $source = $this->config['leaderslinked.fullpath.feed'] . $feed->uuid . DIRECTORY_SEPARATOR . $image_name;
393
 
394
 
395
                        $target_path = 'public' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'feed'. DIRECTORY_SEPARATOR . $feed->uuid;
396
 
397
                        if(!file_exists($target_path)) {
398
                            mkdir($target_path, 0755, true);
399
                        }
400
 
401
 
402
 
403
                        $target = $target_path . DIRECTORY_SEPARATOR . $image_name;
404
 
405
 
406
 
407
                        if(!file_exists($target)) {
408
 
409
                            copy($source, $target);
3355 efrain 410
                            $share_image =  $base_share . '/images/feed/' . $feed->uuid . '/' . $image_name;
3298 efrain 411
 
412
                        } else {
3355 efrain 413
                            $share_image =  $base_share . '/images/feed/' . $feed->uuid . '/' . $image_name;
3298 efrain 414
 
415
                        }
416
 
417
 
418
 
419
 
420
                    }
421
 
3349 efrain 422
                } else {
423
 
424
                    if($currentUserPlugin->hasIdentity()) {
425
                        $this->layout()->setTemplate('layout/layout.phtml');
426
                    } else {
427
                        $this->layout()->setTemplate('layout/share.phtml');
428
                    }
429
                    $viewModel = new ViewModel();
430
                    $viewModel->setTemplate('leaders-linked/error/404.phtml');
431
                    return $viewModel;
3298 efrain 432
                }
433
 
434
 
435
            } else if ($type == 'post' && $code) {
436
 
437
                $postMapper = PostMapper::getInstance($this->adapter);
438
                $post = $postMapper->fetchOneByUuid($code);
439
 
440
                if($post && $post->status == Post::STATUS_ACTIVE) {
441
                    $share_title = $post->title;
442
                    $share_description = $post->description;
443
 
444
 
445
                    if($post->image) {
446
                        $source = $this->config['leaderslinked.fullpath.post'] . $post->uuid . DIRECTORY_SEPARATOR . $post->image;
447
 
448
 
449
                        $target_path = 'public' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'post'. DIRECTORY_SEPARATOR . $post->uuid;
450
 
451
                        if(!file_exists($target_path)) {
452
                            mkdir($target_path, 0755, true);
453
                        }
454
 
455
 
456
 
457
                        $target = $target_path . DIRECTORY_SEPARATOR . $post->image;
458
 
459
 
460
 
461
                        if(!file_exists($target)) {
462
 
463
                            copy($source, $target);
3355 efrain 464
                            $share_image =  $base_share . '/images/post/' . $post->uuid . '/' . $post->image;
3298 efrain 465
 
466
                        } else {
3355 efrain 467
                            $share_image =  $base_share . '/images/post/' . $post->uuid . '/' . $post->image;
3298 efrain 468
 
469
                        }
470
                    }
3349 efrain 471
                } else {
472
 
473
                    if($currentUserPlugin->hasIdentity()) {
474
                        $this->layout()->setTemplate('layout/layout.phtml');
475
                    } else {
476
                        $this->layout()->setTemplate('layout/share.phtml');
477
                    }
478
                    $viewModel = new ViewModel();
479
                    $viewModel->setTemplate('leaders-linked/error/404.phtml');
480
                    return $viewModel;
3298 efrain 481
                }
482
            }
3374 efrain 483
 
3382 efrain 484
            /*
3372 efrain 485
            echo '<pre>';
3371 efrain 486
            print_r([ 'share_image' => $share_image,
487
                'share_url' => $share_url,
488
                'share_title' => strip_tags($share_title),
3372 efrain 489
                'share_description' => strip_tags($share_description)]);  echo '</pre>';
490
            exit;
3382 efrain 491
            */
3298 efrain 492
 
3349 efrain 493
            if($currentUserPlugin->hasIdentity()) {
3364 efrain 494
                $currentUser = $currentUserPlugin->getUser();
495
                if($userCheck && $userCheck->status == User::STATUS_ACTIVE && $userCheck->id != $currentUser->id ) {
496
 
497
                    $connectionMapper = ConnectionMapper::getInstance($this->adapter);
498
                    $connection = $connectionMapper->fetchOneByUserId1AndUserId2($currentUser->id, $userCheck->id);
499
 
500
                    if($connection) {
501
 
502
                        if($connection->status != Connection::STATUS_ACCEPTED) {
503
                            $connectionMapper->approve($connection);
504
                        }
505
 
506
                    } else {
507
                        $connection = new Connection();
508
                        $connection->request_from = $currentUser->id;
509
                        $connection->request_to = $userCheck->id;
510
                        $connection->status = Connection::STATUS_ACCEPTED;
511
 
512
                        $connectionMapper->insert($connection);
513
                    }
514
                }
515
 
516
 
3383 efrain 517
 
3364 efrain 518
 
3349 efrain 519
                $this->layout()->setTemplate('layout/layout.phtml');
520
            } else {
3383 efrain 521
                $this->cache->addItem('user_share_invitation', $user);
3364 efrain 522
 
3349 efrain 523
                $this->layout()->setTemplate('layout/share.phtml');
524
            }
3298 efrain 525
            $viewModel = new ViewModel();
526
            $viewModel->setTemplate('leaders-linked/home/share.phtml');
527
            $viewModel->setVariables([
3352 efrain 528
                'share_image' => $share_image,
529
                'share_url' => $share_url,
3356 efrain 530
                'share_title' => strip_tags($share_title),
531
                'share_description' => strip_tags($share_description),
3349 efrain 532
 
3298 efrain 533
            ]);
534
 
535
 
536
            return $viewModel;
537
 
538
 
539
        } else {
540
            $response = [
541
                'success' => false,
542
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
543
            ];
544
 
545
            return new JsonModel($response);
546
        }
547
 
548
 
549
    }
550
 
3364 efrain 551
 
1 www 552
}