Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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