Proyectos de Subversion LeadersLinked - Services

Rev

Rev 253 | | 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
    {
249 efrain 76
        $currentUserPlugin = $this->plugin('currentUserPlugin');
77
        $currentUser = $currentUserPlugin->getUser();
1 efrain 78
 
249 efrain 79
        //$currentCompany = $currentUserPlugin->getCompany();
80
 
81
        $currentNetworkPlugin = $this->plugin('currentNetworkPlugin');
82
        $currentNetwork = $currentNetworkPlugin->getNetwork();
83
 
84
 
1 efrain 85
        $code = $this->params()->fromRoute('code');
86
        $shorterMapper = ShorterMapper::getInstance($this->adapter);
87
        $shorter = $shorterMapper->fetchOneByUuid($code);
302 www 88
 
1 efrain 89
        if(!$shorter || $shorter->status == Shorter::STATUS_INACTIVE) {
249 efrain 90
           /// $response = $this->getResponse();
91
            //$response->setStatusCode(404);
92
            //return $response;
93
 
94
            return new JsonModel([
95
                'success' => false,
96
                'data' => 'ERROR_FEED_OR_POST_SHARED'
97
            ]);
1 efrain 98
        }
249 efrain 99
 
251 efrain 100
        if( $currentUserPlugin->hasIdentity()) {
249 efrain 101
 
102
            $target = $shorter->target;
103
            if(strpos($target, '-feed-') !== -1) {
104
 
105
                $s = explode('-feed-', $target);
106
                $url = $this->url()->fromRoute('dashboard', ['feed' => trim($s[1]) ]);
107
 
108
            }
109
            else if(strpos($target, '-post-') !== -1) {
110
                $s = explode('-post-', $target);
111
                $url = $this->url()->fromRoute('post', ['id' => trim($s[1]) ]);
112
            }
113
            else {
114
                $url = $this->url()->fromRoute('dashboard');
115
            }
116
 
117
            $hostname = empty($_SERVER['HTTP_HOST']) ?  '' : $_SERVER['HTTP_HOST'];
118
 
119
            $networkMapper = NetworkMapper::getInstance($this->adapter);
120
            $network = $networkMapper->fetchOneByHostnameForFrontend($hostname);
121
 
122
            if(!$network) {
123
                $network = $networkMapper->fetchOneByDefault();
124
            }
125
 
126
            $hostname = trim($network->main_hostname);
127
            $url = 'https://' . $hostname . $url;
128
 
129
            return new JsonModel([
130
                'success' => true,
131
                'data' => [
132
                    'redirect' => true,
133
                    'url' => $url
134
                ]
135
            ]);
136
 
137
 
138
        } else {
139
 
140
            return new JsonModel([
141
                'success' => true,
142
                'data' => [
143
                    'redirect' => false,
144
                    'url' => $shorter->url
145
                ]
146
            ]);
147
        }
1 efrain 148
 
149
    }
150
 
151
    public function generateAction()
152
    {
153
        $currentUserPlugin = $this->plugin('currentUserPlugin');
154
        $currentUser = $currentUserPlugin->getUser();
155
 
156
 
157
        $code = $this->params()->fromRoute('code');
158
        $type = $this->params()->fromRoute('type');
159
 
160
        $timestamp = time();
161
 
162
        list($usec, $sec) = explode(' ', microtime());
163
        $seed = intval($sec + ((float) $usec * 100000));
164
        mt_srand($seed, MT_RAND_MT19937);
165
        $rand =  mt_rand();
166
 
167
 
168
        if($type == 'feed') {
169
 
170
            $feedMapper = FeedMapper::getInstance($this->adapter);
171
            $feed = $feedMapper->fetchOneByUuidAnyStatus($code);
172
            if(!$feed) {
173
                return new JsonModel([
174
                    'success' => false,
175
                    'data' => 'ERROR_FEED_NOT_FOUND'
176
                ]);
177
            }
178
 
179
            $target =  $currentUser->uuid . '-feed-' . $feed->uuid;
180
            $password  = md5('user-' . $currentUser->uuid . '-feed-' . $feed->uuid . '-timestamp-' . $timestamp . '-rand-' . $rand . '-share-key-' . $currentUser->share_key) ;
181
 
182
        } else {
183
 
184
            $postMapper = PostMapper::getInstance($this->adapter);
185
            $post = $postMapper->fetchOneByUuidAnyStatus($code);
186
            if(!$post) {
187
                return new JsonModel([
188
                    'success' => false,
189
                    'data' => 'ERROR_POST_NOT_FOUND'
190
                ]);
191
 
192
            }
193
 
194
            $target =  $currentUser->uuid . '-post-' . $post->uuid;
195
            $password  = md5('user-' . $currentUser->uuid . '-post-' . $post->uuid . '-timestamp-' . $timestamp . '-rand-' . $rand . '-share-key-' . $currentUser->share_key) ;
196
        }
197
 
198
 
199
        $shorterMapper = ShorterMapper::getInstance($this->adapter);
200
        $shorter = $shorterMapper->fetchOneByTarget($target);
201
        if(!$shorter) {
202
 
203
 
204
            if($type == 'feed') {
205
                $share_params = [
206
                    'type' => 'feed',
207
                    'code' => $feed->uuid,
208
                    'user' => $currentUser->uuid,
209
                    'timestamp' => $timestamp,
210
                    'rand' => $rand,
211
                    'password' => $password,
212
                ];
213
            } else {
214
                $share_params = [
215
                    'type' => 'post',
216
                    'code' => $post->uuid,
217
                    'user' => $currentUser->uuid,
218
                    'timestamp' => $timestamp,
219
                    'rand' => $rand,
220
                    'password' => $password,
221
                ];
222
            }
223
 
224
 
248 efrain 225
 
1 efrain 226
 
248 efrain 227
            $url = $this->url()->fromRoute('share', $share_params, ['force_canonical' => true]);
241 efrain 228
 
229
 
1 efrain 230
 
231
            $shorter = new Shorter();
232
            $shorter->status = Shorter::STATUS_ACTIVE;
233
            $shorter->target = $target;
234
            $shorter->url = $url;
235
 
236
            if(!$shorterMapper->insert($shorter)) {
237
                return new JsonModel([
238
                    'success' => false,
239
                    'data' => $shorterMapper->getError()
240
                ]);
241
            }
242
 
243
            $shorter = $shorterMapper->fetchOne($shorter->id);
244
 
245
        }
246
 
253 efrain 247
 
248 efrain 248
        $url = $this->url()->fromRoute('shorter', ['code' => $shorter->uuid]);
241 efrain 249
 
250
        $hostname = empty($_SERVER['HTTP_HOST']) ?  '' : $_SERVER['HTTP_HOST'];
1 efrain 251
 
241 efrain 252
        $networkMapper = NetworkMapper::getInstance($this->adapter);
253
        $network = $networkMapper->fetchOneByHostnameForFrontend($hostname);
254
 
255
        if(!$network) {
256
            $network = $networkMapper->fetchOneByDefault();
257
        }
258
 
259
        $hostname = trim($network->main_hostname);
253 efrain 260
        $url = 'https://' . $hostname . $url;
248 efrain 261
 
262
 
1 efrain 263
        return new JsonModel([
264
            'success' => true,
248 efrain 265
            'data' => $url
1 efrain 266
        ]);
267
    }
248 efrain 268
 
269
 
1 efrain 270
 
271
 
272
 
273
 
274
}