Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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