Proyectos de Subversion LeadersLinked - Services

Rev

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