Passed
Push — develop ( aa4282...8b6a2b )
by Nikolay
05:16 queued 10s
created

getMikoPBXFeatureStatusAction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
c 0
b 0
f 0
dl 0
loc 11
rs 10
cc 2
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 licenseCallBack(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
        $mikoPBXConfig->deleteGeneralSettings('PBXLicense');
84
        $res->success = true;
85
        $this->license->changeLicenseKey('');
86
        return $res;
87
    }
88
89
90
91
    /**
92
     * Check and update license key on database
93
     *
94
     * @param array $data
95
     *
96
     * @return \MikoPBX\PBXCoreREST\Lib\PBXApiResult
97
     */
98
    private function processUserRequestAction(array $data): PBXApiResult
99
    {
100
        $mikoPBXConfig = new MikoPBXConfig();
101
        $res           = new PBXApiResult();
102
        if (strlen($data['licKey']) === 28
103
            && Text::startsWith($data['licKey'], 'MIKO-')
104
        ) {
105
            $oldLicKey = $mikoPBXConfig->getGeneralSettings('PBXLicense');
106
            if ($oldLicKey !== $data['licKey']) {
107
                $licenseInfo = $this->license->getLicenseInfo($data['licKey']);
108
                if ($licenseInfo instanceof SimpleXMLElement) {
0 ignored issues
show
introduced by
$licenseInfo is never a sub-type of SimpleXMLElement.
Loading history...
109
                    $mikoPBXConfig->setGeneralSettings('PBXLicense', $data['licKey']);
110
                    $this->license->changeLicenseKey($data['licKey']);
111
                    $this->license->addTrial('11'); // MikoPBX forever license
112
                    $res->success = true;
113
                } elseif ( ! empty($licenseInfo) && strpos($licenseInfo, '2026') !== false) {
114
                    $res->success    = false;
115
                    $res->messages[] = $this->translation->_('lic_FailedCheckLicense2026');
116
                } elseif ( ! empty($licenseInfo)) {
117
                    $res->messages[] = $licenseInfo;
118
                    $res->success    = false;
119
                } else {
120
                    $res->messages[] = $this->translation->_('lic_FailedCheckLicense');
121
                    $res->success    = false;
122
                }
123
            }
124
            if ( ! empty($data['coupon'])) {
125
                $result = $this->license->activateCoupon($data['coupon']);
126
                if ($result === true) {
127
                    $res->messages[] = $this->translation->_('lic_SuccessfulCouponActivated');
128
                    $res->success    = true;
129
                } else {
130
                    $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

130
                    $res->messages[] = $this->license->translateLicenseErrorMessage(/** @scrutinizer ignore-type */ $result);
Loading history...
131
                    $res->success    = false;
132
                }
133
            }
134
        } else { // Only add trial for license key
135
            $newLicenseKey = $this->license->getTrialLicense($data);
136
            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

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