Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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

Rev Autor Línea Nro. Línea
3775 efrain 1
<?php
2
 
3
declare(strict_types=1);
4
 
5
namespace LeadersLinked\Controller;
6
 
7
use Laminas\Db\Adapter\AdapterInterface;
6849 efrain 8
 
3775 efrain 9
use Laminas\Mvc\Controller\AbstractActionController;
10
use Laminas\Log\LoggerInterface;
11
use Laminas\View\Model\JsonModel;
6388 efrain 12
 
3775 efrain 13
use LeadersLinked\Mapper\FeedMapper;
6388 efrain 14
 
3775 efrain 15
use LeadersLinked\Mapper\ShorterMapper;
16
use LeadersLinked\Model\Shorter;
6388 efrain 17
use LeadersLinked\Mapper\PostMapper;
3775 efrain 18
 
19
class ShorterController extends AbstractActionController
20
{
21
    /**
22
     *
23
     * @var AdapterInterface
24
     */
25
    private $adapter;
26
 
27
 
28
    /**
29
     *
30
     * @var  LoggerInterface
31
     */
32
    private $logger;
33
 
34
 
35
    /**
36
     *
37
     * @var array
38
     */
39
    private $config;
40
 
41
    /**
42
     *
43
     * @param AdapterInterface $adapter
44
     * @param LoggerInterface $logger
45
     * @param array $config
46
     */
47
    public function __construct($adapter, $cache, $logger,  $config)
48
    {
49
        $this->adapter      = $adapter;
50
        $this->logger       = $logger;
51
        $this->config       = $config;
52
    }
53
 
54
 
55
 
56
    public function indexAction()
57
    {
58
 
59
        $code = $this->params()->fromRoute('code');
60
        $shorterMapper = ShorterMapper::getInstance($this->adapter);
61
        $shorter = $shorterMapper->fetchOneByUuid($code);
62
 
63
        if(!$shorter || $shorter->status == Shorter::STATUS_INACTIVE) {
64
            $response = $this->getResponse();
65
            $response->setStatusCode(404);
66
            return $response;
67
        }
68
 
69
        return $this->redirect()->toUrl($shorter->url);
70
 
71
    }
72
 
73
    public function generateAction()
74
    {
75
        $currentUserPlugin = $this->plugin('currentUserPlugin');
76
        $currentUser = $currentUserPlugin->getUser();
77
 
78
 
79
        $code = $this->params()->fromRoute('code');
80
        $type = $this->params()->fromRoute('type');
81
 
82
        $timestamp = time();
83
 
84
        list($usec, $sec) = explode(' ', microtime());
85
        $seed = intval($sec + ((float) $usec * 100000));
86
        mt_srand($seed, MT_RAND_MT19937);
87
        $rand =  mt_rand();
88
 
89
 
90
        if($type == 'feed') {
91
 
92
            $feedMapper = FeedMapper::getInstance($this->adapter);
93
            $feed = $feedMapper->fetchOneByUuidAnyStatus($code);
94
            if(!$feed) {
95
                return new JsonModel([
96
                    'success' => false,
97
                    'data' => 'ERROR_FEED_NOT_FOUND'
98
                ]);
99
            }
100
 
101
            $target =  $currentUser->uuid . '-feed-' . $feed->uuid;
102
            $password  = md5('user-' . $currentUser->uuid . '-feed-' . $feed->uuid . '-timestamp-' . $timestamp . '-rand-' . $rand . '-share-key-' . $currentUser->share_key) ;
103
 
104
        } else {
105
 
106
            $postMapper = PostMapper::getInstance($this->adapter);
107
            $post = $postMapper->fetchOneByUuidAnyStatus($code);
108
            if(!$post) {
109
                return new JsonModel([
110
                    'success' => false,
111
                    'data' => 'ERROR_POST_NOT_FOUND'
112
                ]);
113
 
114
            }
115
 
116
            $target =  $currentUser->uuid . '-post-' . $post->uuid;
117
            $password  = md5('user-' . $currentUser->uuid . '-post-' . $post->uuid . '-timestamp-' . $timestamp . '-rand-' . $rand . '-share-key-' . $currentUser->share_key) ;
118
        }
119
 
120
 
121
        $shorterMapper = ShorterMapper::getInstance($this->adapter);
122
        $shorter = $shorterMapper->fetchOneByTarget($target);
123
        if(!$shorter) {
124
 
125
 
126
            if($type == 'feed') {
127
                $share_params = [
128
                    'type' => 'feed',
129
                    'code' => $feed->uuid,
130
                    'user' => $currentUser->uuid,
131
                    'timestamp' => $timestamp,
132
                    'rand' => $rand,
133
                    'password' => $password,
134
                ];
135
            } else {
136
                $share_params = [
137
                    'type' => 'post',
138
                    'code' => $post->uuid,
139
                    'user' => $currentUser->uuid,
140
                    'timestamp' => $timestamp,
141
                    'rand' => $rand,
142
                    'password' => $password,
143
                ];
144
            }
145
 
146
 
147
 
148
 
149
            $url = $this->url()->fromRoute('share', $share_params  , ['force_canonical' => true]);
150
 
151
 
152
            $shorter = new Shorter();
153
            $shorter->status = Shorter::STATUS_ACTIVE;
154
            $shorter->target = $target;
155
            $shorter->url = $url;
156
 
157
            if(!$shorterMapper->insert($shorter)) {
158
                return new JsonModel([
159
                    'success' => false,
160
                    'data' => $shorterMapper->getError()
161
                ]);
162
            }
163
 
164
            $shorter = $shorterMapper->fetchOne($shorter->id);
165
 
166
        }
167
 
168
 
169
        return new JsonModel([
170
            'success' => true,
171
            'data' => $url = $this->url()->fromRoute('shorter', ['code' => $shorter->uuid ]  , ['force_canonical' => true]),
172
        ]);
173
    }
174
 
175
 
176
 
177
 
178
}