Passed
Push — develop ( 6ea00e...b86149 )
by Портнов
05:04
created

LicensingController::checkInternetConnection()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 14
rs 9.9332
c 0
b 0
f 0
cc 4
nc 6
nop 0
1
<?php
2
/*
3
 * MikoPBX - free phone system for small business
4
 * Copyright (C) 2017-2020 Alexey Portnov and Nikolay Beketov
5
 *
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation; either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License along with this program.
17
 * If not, see <https://www.gnu.org/licenses/>.
18
 */
19
20
namespace MikoPBX\AdminCabinet\Controllers;
21
22
use GuzzleHttp\Client as GuzzleHttpClient;
23
use MikoPBX\AdminCabinet\Forms\LicensingActivateCouponForm;
24
use MikoPBX\AdminCabinet\Forms\LicensingChangeLicenseKeyForm;
25
use MikoPBX\AdminCabinet\Forms\LicensingGetKeyForm;
26
use MikoPBX\Common\Models\PbxSettings;
27
28
/**
29
 * @property \MikoPBX\Service\License license
30
 */
31
class LicensingController extends BaseController
32
{
33
    /**
34
     * License key, get new key, activate coupon form
35
     *
36
     */
37
    public function modifyAction(): void
38
    {
39
        if ($this->language === 'ru') {
40
            $this->view->modulesExampleImgPath = $this->url->get('assets/img/modules-example-ru.png');
41
        } else {
42
            $this->view->modulesExampleImgPath = $this->url->get('assets/img/modules-example-en.png');
43
        }
44
45
        // License key form
46
        $licKey                           = PbxSettings::getValueByKey('PBXLicense');
47
        $changeLicenseKeyForm             = new LicensingChangeLicenseKeyForm(null, ['licKey' => $licKey]);
48
        $this->view->changeLicenseKeyForm = $changeLicenseKeyForm;
49
50
        // Coupon form
51
        $activateCouponForm             = new LicensingActivateCouponForm();
52
        $this->view->activateCouponForm = $activateCouponForm;
53
54
        // Get new license key form
55
        $getKeyForm             = new LicensingGetKeyForm();
56
        $this->view->getKeyForm = $getKeyForm;
57
        $this->view->submitMode = null;
58
        $this->view->internetExists = $this->checkInternetConnection();
59
    }
60
61
    /**
62
     * After some changes on form we will refresh some session cache
63
     */
64
    public function saveAction()
65
    {
66
        $this->session->remove('PBXLicense');
67
    }
68
69
    /**
70
     * Проверка доступа к серверу лицензирования.
71
     * @return bool
72
     */
73
    private function checkInternetConnection():bool
74
    {
75
        $client  = new GuzzleHttpClient();
76
        try {
77
            $res    = $client->request('GET', 'https://lic.miko.ru/protect/v1/ping', ['timeout'     => 2, 'http_errors' => false,]);
78
            $code   = $res->getStatusCode();
79
        }catch (\Throwable $e ){
80
            $code = 0;
81
        }
82
        $body = '';
83
        if($code === 200 && isset($res)){
84
            $body = $res->getBody()->getContents();
85
        }
86
        return strpos($body, "message='pong'") !== false;
87
    }
88
89
}