Proyectos de Subversion LeadersLinked - Services

Rev

Rev 319 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
314 www 1
<?php
2
declare(strict_types=1);
3
 
4
namespace LeadersLinked\Controller;
5
 
626 stevensc 6
use Laminas\View\Model\JsonModel;
314 www 7
use Laminas\Mvc\Controller\AbstractActionController;
8
use LeadersLinked\Mapper\UserMapper;
9
use LeadersLinked\Mapper\DeviceMapper;
10
use LeadersLinked\Model\Device;
11
use LeadersLinked\Mapper\DeviceHistoryMapper;
12
use LeadersLinked\Model\DeviceHistory;
13
use LeadersLinked\Mapper\SyncLogMapper;
14
use LeadersLinked\Model\SyncLog;
15
 
16
class ServiceController extends AbstractActionController
17
{
18
    /**
19
     *
20
     * @var \Laminas\Db\Adapter\AdapterInterface
21
     */
22
    private $adapter;
23
 
24
    /**
25
     *
26
     * @var \LeadersLinked\Cache\CacheInterface
27
     */
28
    private $cache;
29
 
30
 
31
    /**
32
     *
33
     * @var \Laminas\Log\LoggerInterface
34
     */
35
    private $logger;
36
 
37
    /**
38
     *
39
     * @var array
40
     */
41
    private $config;
42
 
43
 
44
    /**
45
     *
46
     * @var \Laminas\Mvc\I18n\Translator
47
     */
48
    private $translator;
49
 
50
 
51
    /**
52
     *
53
     * @param \Laminas\Db\Adapter\AdapterInterface $adapter
54
     * @param \LeadersLinked\Cache\CacheInterface $cache
55
     * @param \Laminas\Log\LoggerInterface LoggerInterface $logger
56
     * @param array $config
57
     * @param \Laminas\Mvc\I18n\Translator $translator
58
     */
59
    public function __construct($adapter, $cache, $logger, $config, $translator)
60
    {
61
        $this->adapter      = $adapter;
62
        $this->cache        = $cache;
63
        $this->logger       = $logger;
64
        $this->config       = $config;
65
        $this->translator   = $translator;
66
    }
67
 
68
    public function indexAction()
69
    {
70
        return new JsonModel(['ok' => false]);
71
    }
72
 
73
    public function signinAction()
74
    {
316 www 75
        $rawdata = file_get_contents("php://input");
317 www 76
        error_log('URL = ' . $_SERVER['REQUEST_URI'] );
316 www 77
        error_log('$rawdata = ' . $rawdata );
78
 
314 www 79
        $request = $this->getRequest();
80
 
81
        if($request->isPost()) {
82
 
83
 
84
            $seed   = $this->config['leaderslinked.services.seed'];
85
            $min    = strtotime('-1 day');
86
            $max    = strtotime('+1 day');
87
 
88
 
89
 
90
 
318 www 91
            $device_uuid    = Functions::sanitizeFilterString($this->params()->fromPost('device_uuid', ''));
92
            $user_uuid      = Functions::sanitizeFilterString($this->params()->fromPost('user_uuid', ''));
314 www 93
            $rand           = Functions::sanitizeFilterString($this->params()->fromPost('rand', ''));
94
            $timestamp      = Functions::sanitizeFilterString($this->params()->fromPost('timestamp', ''));
95
            $password       = Functions::sanitizeFilterString($this->params()->fromPost('password', ''));
96
            $sync_id        = Functions::sanitizeFilterString($this->params()->fromPost('sync_id', ''));
97
 
98
 
317 www 99
            $ok = $device_uuid && strlen($device_uuid) == 32;
314 www 100
            $ok = $ok && $rand > 0;
317 www 101
            $ok = $ok && ($timestamp / 1000) >= $min;
102
            $ok = $ok && ($timestamp / 1000) <= $max;
314 www 103
            $ok = $ok && strlen($password) == 32;
104
            $ok = $ok && strlen($user_uuid)  == 36;
105
            $ok = $ok && $sync_id;
106
 
107
            if(!$ok) {
108
                return new JsonModel([
109
                    'success' => false,
110
                    'data' => 'ERROR_PARAMETERS_ARE_INVALID',
111
                ]);
112
            }
113
 
114
            $password_md5 = md5($device_uuid . '-' . $seed . '-' . $rand . '-' . $timestamp . '-' . $user_uuid );
115
            if($password != $password_md5) {
116
                return new JsonModel([
117
                    'success' => false,
118
                    'data' => 'ERROR_WEBSERVICE_PASSWORD',
119
                ]);
120
 
121
 
122
            }
123
 
124
 
125
            $deviceMapper = DeviceMapper::getInstance($this->adapter);
126
            $device = $deviceMapper->fetchOne($device_uuid);
127
            if(!$device) {
128
                return new JsonModel([
129
                    'success' => false,
130
                    'data' => 'ERROR_DEVICE_NOT_FOUND',
131
                ]);
132
            }
133
 
134
            $userMapper = UserMapper::getInstance($this->adapter);
135
            $user = $userMapper->fetchOneByUuid($user_uuid);
136
            if(!$user) {
137
                return new JsonModel([
138
                    'success' => false,
139
                    'data' => 'ERROR_USER_NOT_FOUND',
140
                ]);
141
            }
142
 
143
 
144
            $syncLog = new SyncLog();
145
            $syncLog->data          = json_encode(['user' => $user_uuid]);
146
            $syncLog->type          = 'signin';
147
            $syncLog->device_uuid   = $device->id;
148
            $syncLog->ip            = Functions::getUserIP();
149
 
150
            $syncLogMapper = SyncLogMapper::getInstance($this->adapter);
151
            $syncLogMapper->insert($syncLog);
152
 
153
            $device->user_id        = $user->id;
154
            $device->ip             = Functions::getUserIP();
155
            $result                 = $deviceMapper->update($device);
156
 
157
            if($result) {
158
 
159
                $deviceHistory = new DeviceHistory();
319 www 160
                $deviceHistory->device_id   = $device->id;
314 www 161
                $deviceHistory->user_id     = $user->id;
162
                $deviceHistory->ip          = Functions::getUserIP();
163
 
164
                $deviceHistoryMapper = DeviceHistoryMapper::getInstance($this->adapter);
165
                $deviceHistoryMapper->insert($deviceHistory);
166
 
167
 
168
 
169
                return new JsonModel([
170
                    'success' => true,
171
                    'data' => [
172
                        'sync_id' => $sync_id
173
                    ]
174
                ]);
175
            } else {
176
                return new JsonModel([
177
                    'success' => false,
178
                    'data' => 'ERROR_THERE_WAS_AN_ERROR',
179
                ]);
180
            }
181
 
182
        }
183
 
184
        return new JsonModel([
185
            'success' => false,
186
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
187
        ]);
188
 
189
 
190
    }
191
 
192
    public function fcmAction()
193
    {
317 www 194
 
316 www 195
        $rawdata = file_get_contents("php://input");
317 www 196
        error_log('URL = ' . $_SERVER['REQUEST_URI'] );
316 www 197
        error_log('$rawdata = ' . $rawdata );
198
 
314 www 199
        $request = $this->getRequest();
200
 
316 www 201
 
314 www 202
        if($request->isPost()) {
203
 
204
 
205
            $seed   = $this->config['leaderslinked.services.seed'];
206
            $min    = strtotime('-1 day');
207
            $max    = strtotime('+1 day');
208
 
317 www 209
            $device_uuid    = Functions::sanitizeFilterString($this->params()->fromPost('device_uuid', ''));
314 www 210
            $token          = Functions::sanitizeFilterString($this->params()->fromPost('token', ''));
211
            $rand           = Functions::sanitizeFilterString($this->params()->fromPost('rand', ''));
212
            $timestamp      = Functions::sanitizeFilterString($this->params()->fromPost('timestamp', ''));
213
            $password       = Functions::sanitizeFilterString($this->params()->fromPost('password', ''));
214
            $sync_id        = Functions::sanitizeFilterString($this->params()->fromPost('sync_id', ''));
215
 
317 www 216
            $ok = $device_uuid && strlen($device_uuid) == 32;
314 www 217
            $ok = $ok && $rand > 0;
317 www 218
            $ok = $ok && ($timestamp / 1000) >= $min;
219
            $ok = $ok && ($timestamp / 1000) <= $max;
314 www 220
            $ok = $ok && strlen($password) == 32;
221
            $ok = $ok && strlen($token) <= 512;
222
            $ok = $ok && $sync_id;
223
 
224
            if(!$ok) {
225
                return new JsonModel([
226
                    'success' => false,
227
                    'data' => 'ERROR_PARAMETERS_ARE_INVALID',
228
                ]);
229
            }
230
 
231
            $password_md5 = md5($device_uuid . '-' . $seed . '-' . $rand . '-' . $timestamp . '-' . $token );
317 www 232
 
314 www 233
            if($password != $password_md5) {
234
                return new JsonModel([
235
                    'success' => false,
236
                    'data' => 'ERROR_WEBSERVICE_PASSWORD',
237
                ]);
238
 
239
 
240
            }
241
 
242
 
243
            $deviceMapper = DeviceMapper::getInstance($this->adapter);
244
            $device = $deviceMapper->fetchOne($device_uuid);
245
            if(!$device) {
246
                return new JsonModel([
247
                    'success' => false,
248
                    'data' => 'ERROR_DEVICE_NOT_FOUND',
249
                ]);
250
            }
251
 
252
 
253
            $syncLog = new SyncLog();
254
            $syncLog->data          = json_encode(['token' => $token]);
255
            $syncLog->type          = 'token';
256
            $syncLog->device_uuid   = $device->id;
257
            $syncLog->ip            = Functions::getUserIP();
258
 
259
            $syncLogMapper = SyncLogMapper::getInstance($this->adapter);
260
            $syncLogMapper->insert($syncLog);
261
 
262
            $device->token          = $token;
263
            $device->ip             = Functions::getUserIP();
264
            $result                 = $deviceMapper->update($device);
265
 
266
            if($result) {
267
                return new JsonModel([
268
                    'success' => true,
269
                    'data' => [
270
                        'sync_id' => $sync_id
271
                    ]
272
                ]);
273
            } else {
274
                return new JsonModel([
275
                    'success' => false,
276
                    'data' => 'ERROR_THERE_WAS_AN_ERROR',
277
                ]);
278
            }
279
 
280
        }
281
 
282
        return new JsonModel([
283
            'success' => false,
284
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
285
        ]);
286
    }
287
 
288
 
289
    public function deviceAction()
290
    {
316 www 291
        $rawdata = file_get_contents("php://input");
317 www 292
        error_log('URL = ' . $_SERVER['REQUEST_URI'] );
316 www 293
        error_log('$rawdata = ' . $rawdata );
314 www 294
 
295
        $request = $this->getRequest();
296
 
297
        if($request->isPost()) {
298
 
299
            $seed   = $this->config['leaderslinked.services.seed'];
300
            $min    = strtotime('-1 day');
301
            $max    = strtotime('+1 day');
317 www 302
 
303
 
314 www 304
 
305
 
306
            $device_uuid    = Functions::sanitizeFilterString($this->params()->fromPost('device_uuid', ''));
307
            $manufacturer   = Functions::sanitizeFilterString($this->params()->fromPost('manufacturer', ''));
308
            $platform       = Functions::sanitizeFilterString($this->params()->fromPost('platform', ''));
309
            $brand          = Functions::sanitizeFilterString($this->params()->fromPost('brand', ''));
310
            $version        = Functions::sanitizeFilterString($this->params()->fromPost('version', ''));
311
            $model          = Functions::sanitizeFilterString($this->params()->fromPost('model', ''));
312
            $rand           = Functions::sanitizeFilterString($this->params()->fromPost('rand', ''));
313
            $timestamp      = Functions::sanitizeFilterString($this->params()->fromPost('timestamp', ''));
314
            $password       = Functions::sanitizeFilterString($this->params()->fromPost('password', ''));
315
            $sync_id        = Functions::sanitizeFilterString($this->params()->fromPost('sync_id', ''));
317 www 316
 
314 www 317
 
317 www 318
            $ok = $device_uuid && strlen($device_uuid) == 32;
314 www 319
            $ok = $ok && strlen($manufacturer) <= 250;
320
            $ok = $ok && strlen($brand) <= 250;
321
            $ok = $ok && strlen($version) <= 250;
322
            $ok = $ok && strlen($model) <= 250;
323
            $ok = $ok && $rand > 0;
317 www 324
            $ok = $ok && ($timestamp / 1000) >= $min;
325
            $ok = $ok && ($timestamp / 1000) <= $max;
314 www 326
            $ok = $ok && strlen($password) == 32;
327
            $ok = $ok && $sync_id;
317 www 328
 
314 www 329
 
317 www 330
            $password_md5 = md5($device_uuid . '-' . $seed . '-' . $rand . '-' . $timestamp . '-' . $manufacturer . '-' . $brand . '-' . $version . '-' . $model);
331
 
314 www 332
 
333
            if($password != $password_md5) {
334
                return new JsonModel([
335
                    'success' => false,
336
                    'data' => 'ERROR_WEBSERVICE_PASSWORD',
337
                ]);
338
 
339
 
340
            }
341
 
342
 
343
            if(!$ok) {
344
                return new JsonModel([
345
                    'success' => false,
346
                    'data' => 'ERROR_PARAMETERS_ARE_INVALID',
347
                ]);
348
            }
349
 
350
 
351
 
352
 
353
            $syncLog = new SyncLog();
354
            $syncLog->data = json_encode([
355
                'platform' => $platform,
356
                'manufacturer' => $manufacturer,
357
                'brand' => $brand,
358
                'version' => $version,
359
                'model' => $model,
360
            ]);
361
            $syncLog->type          = 'device';
362
            $syncLog->device_uuid   = $device_uuid;
363
            $syncLog->ip            = Functions::getUserIP();
364
 
365
            $syncLogMapper = SyncLogMapper::getInstance($this->adapter);
366
            $syncLogMapper->insert($syncLog);
367
 
368
 
369
            $deviceMapper = DeviceMapper::getInstance($this->adapter);
370
            $device = $deviceMapper->fetchOne($device_uuid);
371
 
372
 
373
 
374
 
375
            if($device) {
376
                $device->platform       = $platform;
377
                $device->manufacturer   = $manufacturer;
378
                $device->brand          = $brand;
379
                $device->version        = $version;
380
                $device->model          = $model;
381
                $device->ip             = Functions::getUserIP();
382
                $result                 = $deviceMapper->update($device);
383
 
384
            } else {
385
                $device                 = new Device();
386
                $device->id             = $device_uuid;
387
                $device->manufacturer   = $manufacturer;
388
                $device->brand          = $brand;
389
                $device->version        = $version;
390
                $device->model          = $model;
391
                $device->platform       = $platform;
392
                $device->ip             = Functions::getUserIP();
393
                $result                 = $deviceMapper->insert($device);
394
            }
395
 
396
 
397
 
398
            if($result) {
399
                return new JsonModel([
400
                    'success' => true,
401
                    'data' => [
402
                        'sync_id'   => $sync_id,
403
                    ]
404
                ]);
405
            } else {
406
                return new JsonModel([
407
                    'success' => false,
408
                    'data' => 'ERROR_THERE_WAS_AN_ERROR',
409
                ]);
410
            }
411
 
412
        }
413
 
414
        return new JsonModel([
415
            'success' => false,
416
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
417
        ]);
418
 
419
    }
420
 
421
 
422
 
423
    public function signoutAction()
424
    {
316 www 425
        $rawdata = file_get_contents("php://input");
317 www 426
        error_log('URL = ' . $_SERVER['REQUEST_URI'] );
316 www 427
        error_log('$rawdata = ' . $rawdata );
317 www 428
 
314 www 429
        $request = $this->getRequest();
430
 
431
        if($request->isPost()) {
432
 
433
 
434
            $seed   = $this->config['leaderslinked.services.seed'];
435
            $min    = strtotime('-1 day');
436
            $max    = strtotime('+1 day');
437
 
438
 
439
 
440
 
318 www 441
            $device_uuid    = Functions::sanitizeFilterString($this->params()->fromPost('device_uuid', ''));
442
            $user_uuid      = Functions::sanitizeFilterString($this->params()->fromPost('user_uuid', ''));
314 www 443
            $rand           = Functions::sanitizeFilterString($this->params()->fromPost('rand', ''));
444
            $timestamp      = Functions::sanitizeFilterString($this->params()->fromPost('timestamp', ''));
445
            $password       = Functions::sanitizeFilterString($this->params()->fromPost('password', ''));
446
            $sync_id        = Functions::sanitizeFilterString($this->params()->fromPost('sync_id', ''));
447
 
448
 
317 www 449
            $ok = $device_uuid && strlen($device_uuid) == 32;
314 www 450
            $ok = $ok && $rand > 0;
317 www 451
            $ok = $ok && ($timestamp / 1000) >= $min;
452
            $ok = $ok && ($timestamp / 1000) <= $max;
314 www 453
            $ok = $ok && strlen($password) == 32;
454
            $ok = $ok && strlen($user_uuid)  == 36;
455
            $ok = $ok && $sync_id;
456
 
457
            if(!$ok) {
458
                return new JsonModel([
459
                    'success' => false,
460
                    'data' => 'ERROR_PARAMETERS_ARE_INVALID',
461
                ]);
462
            }
463
 
464
            $password_md5 = md5($device_uuid . '-' . $seed . '-' . $rand . '-' . $timestamp . '-' . $user_uuid );
465
            if($password != $password_md5) {
466
                return new JsonModel([
467
                    'success' => false,
468
                    'data' => 'ERROR_WEBSERVICE_PASSWORD',
469
                ]);
470
 
471
 
472
            }
473
 
474
 
475
            $deviceMapper = DeviceMapper::getInstance($this->adapter);
476
            $device = $deviceMapper->fetchOne($device_uuid);
477
            if(!$device) {
478
                return new JsonModel([
479
                    'success' => false,
480
                    'data' => 'ERROR_DEVICE_NOT_FOUND',
481
                ]);
482
            }
483
 
484
            $userMapper = UserMapper::getInstance($this->adapter);
485
            $user = $userMapper->fetchOneByUuid($user_uuid);
486
            if(!$user) {
487
                return new JsonModel([
488
                    'success' => false,
489
                    'data' => 'ERROR_USER_NOT_FOUND',
490
                ]);
491
            }
492
 
493
 
494
            $syncLog = new SyncLog();
495
            $syncLog->data          = json_encode(['user' => $user_uuid]);
496
            $syncLog->type          = 'signin';
497
            $syncLog->device_uuid   = $device->id;
498
            $syncLog->ip            = Functions::getUserIP();
499
 
500
            $syncLogMapper = SyncLogMapper::getInstance($this->adapter);
501
            $syncLogMapper->insert($syncLog);
502
 
503
            $device->user_id        =
504
            $device->ip             = Functions::getUserIP();
505
            $result                 = $deviceMapper->update($device);
506
 
507
            if($result) {
508
                return new JsonModel([
509
                    'success' => true,
510
                    'data' => [
511
                        'sync_id' => $sync_id
512
                    ]
513
                ]);
514
            } else {
515
                return new JsonModel([
516
                    'success' => false,
517
                    'data' => 'ERROR_THERE_WAS_AN_ERROR',
518
                ]);
519
            }
520
 
521
        }
522
 
523
        return new JsonModel([
524
            'success' => false,
525
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
526
        ]);
527
 
528
 
529
    }
530
 
531
 
532
 
533
 
534
 
535
 
536
}
537
 
538