Passed
Push — develop ( eb8832...f8f730 )
by Nikolay
05:34
created

LicenseManagementProcessor::getLicenseInfoAction()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 16
rs 9.9
cc 3
nc 2
nop 0
1
<?php
2
/**
3
 * Copyright © MIKO LLC - All Rights Reserved
4
 * Unauthorized copying of this file, via any medium is strictly prohibited
5
 * Proprietary and confidential
6
 * Written by Alexey Portnov, 8 2020
7
 */
8
9
namespace MikoPBX\PBXCoreREST\Lib;
10
11
use MikoPBX\Common\Models\PbxSettings;
12
use MikoPBX\Core\System\MikoPBXConfig;
13
use Phalcon\Di\Injectable;
14
use Phalcon\Text;
15
use SimpleXMLElement;
16
17
18
/**
19
 * Class LicenseManagementProcessor
20
 *
21
 * @package MikoPBX\PBXCoreREST\Lib
22
 * @property \MikoPBX\Common\Providers\LicenseProvider     license
23
 * @property \MikoPBX\Common\Providers\TranslationProvider translation
24
 * @property \Phalcon\Config                               config
25
 */
26
class LicenseManagementProcessor extends Injectable
27
{
28
29
    /**
30
     * Processes requests to licensing system
31
     *
32
     * @param array $request
33
     *
34
     * @return \MikoPBX\PBXCoreREST\Lib\PBXApiResult
35
     *
36
     */
37
    public static function callBack(array $request): PBXApiResult
38
    {
39
        $action         = $request['action'];
40
        $data           = $request['data'];
41
        $res            = new PBXApiResult();
42
        $res->processor = __METHOD__;
43
        switch ($action) {
44
            case 'resetKey':
45
                $proc = new LicenseManagementProcessor();
46
                $res = $proc->resetLicenseAction();
47
                break;
48
            case 'processUserRequest':
49
                $proc = new LicenseManagementProcessor();
50
                $res = $proc->processUserRequestAction($data);
51
                break;
52
            case 'getLicenseInfo':
53
                $proc = new LicenseManagementProcessor();
54
                $res = $proc->getLicenseInfoAction();
55
                break;
56
            case 'getMikoPBXFeatureStatus':
57
                $proc = new LicenseManagementProcessor();
58
                $res = $proc->getMikoPBXFeatureStatusAction();
59
                break;
60
            case 'captureFeatureForProductId':
61
                $proc = new LicenseManagementProcessor();
62
                $res = $proc->captureFeatureForProductIdAction($data);
63
                break;
64
65
            default:
66
                $res->messages[] = "Unknown action - {$action} in licenseCallBack";
67
        }
68
69
        $res->function = $action;
70
71
        return $res;
72
    }
73
74
    /**
75
     * Reset license key
76
     *
77
     * @return \MikoPBX\PBXCoreREST\Lib\PBXApiResult
78
     */
79
    private function resetLicenseAction():PBXApiResult
80
    {
81
        $mikoPBXConfig = new MikoPBXConfig();
82
        $res           = new PBXApiResult();
83
        $res->processor = __METHOD__;
84
        $mikoPBXConfig->deleteGeneralSettings('PBXLicense');
85
        $res->success = true;
86
        $this->license->changeLicenseKey('');
87
        return $res;
88
    }
89
90
91
92
    /**
93
     * Check and update license key on database
94
     *
95
     * @param array $data
96
     *
97
     * @return \MikoPBX\PBXCoreREST\Lib\PBXApiResult
98
     */
99
    private function processUserRequestAction(array $data): PBXApiResult
100
    {
101
        $mikoPBXConfig = new MikoPBXConfig();
102
        $res           = new PBXApiResult();
103
        $res->processor = __METHOD__;
104
        if (strlen($data['licKey']) === 28
105
            && Text::startsWith($data['licKey'], 'MIKO-')
106
        ) {
107
            $oldLicKey = $mikoPBXConfig->getGeneralSettings('PBXLicense');
108
            if ($oldLicKey !== $data['licKey']) {
109
                $licenseInfo = $this->license->getLicenseInfo($data['licKey']);
110
                if ($licenseInfo instanceof SimpleXMLElement) {
0 ignored issues
show
introduced by
$licenseInfo is never a sub-type of SimpleXMLElement.
Loading history...
111
                    $mikoPBXConfig->setGeneralSettings('PBXLicense', $data['licKey']);
112
                    $this->license->changeLicenseKey($data['licKey']);
113
                    $this->license->addTrial('11'); // MikoPBX forever license
114
                    $res->success = true;
115
                } elseif ( ! empty($licenseInfo) && strpos($licenseInfo, '2026') !== false) {
116
                    $res->success    = false;
117
                    $res->messages[] = $this->translation->_('lic_FailedCheckLicense2026');
118
                } elseif ( ! empty($licenseInfo)) {
119
                    $res->messages[] = $licenseInfo;
120
                    $res->success    = false;
121
                } else {
122
                    $res->messages[] = $this->translation->_('lic_FailedCheckLicense');
123
                    $res->success    = false;
124
                }
125
            }
126
            if ( ! empty($data['coupon'])) {
127
                $result = $this->license->activateCoupon($data['coupon']);
128
                if ($result === true) {
129
                    $res->messages[] = $this->translation->_('lic_SuccessfulCouponActivated');
130
                    $res->success    = true;
131
                } else {
132
                    $res->messages[] = $this->license->translateLicenseErrorMessage($result);
0 ignored issues
show
Bug introduced by
$result of type false is incompatible with the type string expected by parameter $message of MikoPBX\Common\Providers...teLicenseErrorMessage(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

132
                    $res->messages[] = $this->license->translateLicenseErrorMessage(/** @scrutinizer ignore-type */ $result);
Loading history...
133
                    $res->success    = false;
134
                }
135
            }
136
        } else { // Only add trial for license key
137
            $newLicenseKey = $this->license->getTrialLicense($data);
138
            if (strlen($newLicenseKey) === 28 && Text::startsWith($newLicenseKey, 'MIKO-')) {
0 ignored issues
show
Bug introduced by
$newLicenseKey of type boolean is incompatible with the type string expected by parameter $text of Phalcon\Text::startsWith(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

138
            if (strlen($newLicenseKey) === 28 && Text::startsWith(/** @scrutinizer ignore-type */ $newLicenseKey, 'MIKO-')) {
Loading history...
139
                $mikoPBXConfig->setGeneralSettings('PBXLicense', $newLicenseKey);
140
                $this->license->changeLicenseKey($newLicenseKey);
141
                $res->success    = true;
142
            } else {
143
                // No internet connection, or wrong data sent to license server, or something else
144
                $res->messages[] = $this->license->translateLicenseErrorMessage($newLicenseKey);
145
                $res->success    = false;
146
            }
147
        }
148
        return $res;
149
    }
150
151
152
    /**
153
     * Returns license info from license server by key
154
     *
155
     * @return \MikoPBX\PBXCoreREST\Lib\PBXApiResult
156
     */
157
    private function getLicenseInfoAction(): PBXApiResult
158
    {
159
        $res = new PBXApiResult();
160
        $res->processor = __METHOD__;
161
        $licenseKey = PbxSettings::getValueByKey('PBXLicense');
162
        if ((strlen($licenseKey) === 28
163
            && Text::startsWith($licenseKey, 'MIKO-')
164
        )) {
165
            $licenseInfo              = $this->license->getLicenseInfo($licenseKey);
166
            $res->success             = true;
167
            $res->data['licenseInfo'] = json_encode($licenseInfo);
168
        } else {
169
            $res->messages[] = 'License key is wrong or empty';
170
        }
171
172
        return $res;
173
    }
174
175
    /**
176
     * Check for free MikoPBX base license
177
     *
178
     * @return \MikoPBX\PBXCoreREST\Lib\PBXApiResult
179
     */
180
    private function getMikoPBXFeatureStatusAction(): PBXApiResult
181
    {
182
        $res = new PBXApiResult();
183
        $res->processor = __METHOD__;
184
        $checkBaseFeature = $this->license->featureAvailable(33);
185
        if ($checkBaseFeature['success'] === false) {
186
            $res->success = false;
187
            $res->messages[] = $this->license->translateLicenseErrorMessage($checkBaseFeature['error']);
188
        } else {
189
            $res->success = true;
190
        }
191
        return $res;
192
    }
193
194
    /**
195
     * Tries to capture feature.
196
     *
197
     * If it fails we try to get trial and then try capture again.
198
     *
199
     * @param array $data
200
     *
201
     * @return \MikoPBX\PBXCoreREST\Lib\PBXApiResult
202
     */
203
    private function captureFeatureForProductIdAction(array $data): PBXApiResult
204
    {
205
        $res = new PBXApiResult();
206
        $res->processor = __METHOD__;
207
        $licFeatureId = $data['licFeatureId'];
208
        $licProductId = $data['licProductId'];
209
210
        if ( ! isset($licFeatureId, $licProductId)) {
211
            $res->messages[]='The feature id or product id is empty.';
212
            return $res;
213
        }
214
        $res->success = true;
215
        if ($licFeatureId > 0) {
216
            // 1.Try to capture feature
217
            $result = $this->license->captureFeature($licFeatureId);
218
            if ($result['success'] === false) {
219
                // Add trial
220
                $this->license->addTrial($licProductId);
221
                // 2.Try to capture feature
222
                $result = $this->license->captureFeature($licFeatureId);
223
                if ($result['success'] === false) {
224
                    $res->messages[] = $this->license->translateLicenseErrorMessage($result['error']);
225
                    $res->success = false;
226
                }
227
            }
228
        }
229
        return $res;
230
    }
231
232
}