Proyectos de Subversion LeadersLinked - Services

Rev

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