Proyectos de Subversion LeadersLinked - Services

Rev

Rev 241 | Rev 249 | 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;
241 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
 
248 efrain 87
        return new JsonModel([
88
            'success' => true,
89
            'data' => $shorter->url
90
        ]);
1 efrain 91
 
92
    }
93
 
94
    public function generateAction()
95
    {
96
        $currentUserPlugin = $this->plugin('currentUserPlugin');
97
        $currentUser = $currentUserPlugin->getUser();
98
 
99
 
100
        $code = $this->params()->fromRoute('code');
101
        $type = $this->params()->fromRoute('type');
102
 
103
        $timestamp = time();
104
 
105
        list($usec, $sec) = explode(' ', microtime());
106
        $seed = intval($sec + ((float) $usec * 100000));
107
        mt_srand($seed, MT_RAND_MT19937);
108
        $rand =  mt_rand();
109
 
110
 
111
        if($type == 'feed') {
112
 
113
            $feedMapper = FeedMapper::getInstance($this->adapter);
114
            $feed = $feedMapper->fetchOneByUuidAnyStatus($code);
115
            if(!$feed) {
116
                return new JsonModel([
117
                    'success' => false,
118
                    'data' => 'ERROR_FEED_NOT_FOUND'
119
                ]);
120
            }
121
 
122
            $target =  $currentUser->uuid . '-feed-' . $feed->uuid;
123
            $password  = md5('user-' . $currentUser->uuid . '-feed-' . $feed->uuid . '-timestamp-' . $timestamp . '-rand-' . $rand . '-share-key-' . $currentUser->share_key) ;
124
 
125
        } else {
126
 
127
            $postMapper = PostMapper::getInstance($this->adapter);
128
            $post = $postMapper->fetchOneByUuidAnyStatus($code);
129
            if(!$post) {
130
                return new JsonModel([
131
                    'success' => false,
132
                    'data' => 'ERROR_POST_NOT_FOUND'
133
                ]);
134
 
135
            }
136
 
137
            $target =  $currentUser->uuid . '-post-' . $post->uuid;
138
            $password  = md5('user-' . $currentUser->uuid . '-post-' . $post->uuid . '-timestamp-' . $timestamp . '-rand-' . $rand . '-share-key-' . $currentUser->share_key) ;
139
        }
140
 
141
 
142
        $shorterMapper = ShorterMapper::getInstance($this->adapter);
143
        $shorter = $shorterMapper->fetchOneByTarget($target);
144
        if(!$shorter) {
145
 
146
 
147
            if($type == 'feed') {
148
                $share_params = [
149
                    'type' => 'feed',
150
                    'code' => $feed->uuid,
151
                    'user' => $currentUser->uuid,
152
                    'timestamp' => $timestamp,
153
                    'rand' => $rand,
154
                    'password' => $password,
155
                ];
156
            } else {
157
                $share_params = [
158
                    'type' => 'post',
159
                    'code' => $post->uuid,
160
                    'user' => $currentUser->uuid,
161
                    'timestamp' => $timestamp,
162
                    'rand' => $rand,
163
                    'password' => $password,
164
                ];
165
            }
166
 
167
 
248 efrain 168
 
1 efrain 169
 
248 efrain 170
            $url = $this->url()->fromRoute('share', $share_params, ['force_canonical' => true]);
241 efrain 171
 
172
 
1 efrain 173
 
174
            $shorter = new Shorter();
175
            $shorter->status = Shorter::STATUS_ACTIVE;
176
            $shorter->target = $target;
177
            $shorter->url = $url;
178
 
179
            if(!$shorterMapper->insert($shorter)) {
180
                return new JsonModel([
181
                    'success' => false,
182
                    'data' => $shorterMapper->getError()
183
                ]);
184
            }
185
 
186
            $shorter = $shorterMapper->fetchOne($shorter->id);
187
 
188
        }
189
 
248 efrain 190
        $url = $this->url()->fromRoute('shorter', ['code' => $shorter->uuid]);
241 efrain 191
 
192
        $hostname = empty($_SERVER['HTTP_HOST']) ?  '' : $_SERVER['HTTP_HOST'];
1 efrain 193
 
241 efrain 194
        $networkMapper = NetworkMapper::getInstance($this->adapter);
195
        $network = $networkMapper->fetchOneByHostnameForFrontend($hostname);
196
 
197
        if(!$network) {
198
            $network = $networkMapper->fetchOneByDefault();
199
        }
200
 
201
        $hostname = trim($network->main_hostname);
248 efrain 202
        $url = 'https://' . $hostname . $url;
203
 
204
 
1 efrain 205
        return new JsonModel([
206
            'success' => true,
248 efrain 207
            'data' => $url
1 efrain 208
        ]);
209
    }
248 efrain 210
 
211
 
1 efrain 212
 
213
 
214
 
215
 
216
}