LicenseController::getInstallPreference()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace App\Http\Controllers\License;
4
5
use App\ApiKey;
6
use App\Http\Controllers\Controller;
7
use App\Model\Order\Order;
8
use App\Model\Product\Product;
9
use App\User;
10
11
class LicenseController extends Controller
12
{
13
    private $api_key_secret;
14
    private $url;
15
    private $license;
16
17
    public function __construct()
18
    {
19
        $model = new ApiKey();
20
        $this->license = $model->first();
21
22
        $this->api_key_secret = $this->license->license_api_secret;
23
        $this->url = $this->license->license_api_url;
24
    }
25
26
    private function postCurl($post_url, $post_info)
27
    {
28
        $ch = curl_init();
29
        curl_setopt($ch, CURLOPT_URL, $post_url);
30
        curl_setopt($ch, CURLOPT_POST, 1);
31
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_info);
32
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
33
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
34
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
35
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
36
        $result = curl_exec($ch);
37
        curl_close($ch);
38
39
        return $result;
40
    }
41
42
    /*
43
    *  Add New Product
44
    */
45
    public function addNewProduct($product_name, $product_sku)
46
    {
47
        $url = $this->url;
48
        $api_key_secret = $this->api_key_secret;
49
        $addProduct = $this->postCurl($url, "api_key_secret=$api_key_secret&api_function=products_add&product_title=$product_name&product_sku=$product_sku&product_status=1");
50
    }
51
52
    /*
53
   *  Add New User
54
   */
55
    public function addNewUser($first_name, $last_name, $email)
56
    {
57
        $url = $this->url;
58
        $api_key_secret = $this->api_key_secret;
59
        $addProduct = $this->postCurl($url, "api_key_secret=$api_key_secret&api_function=clients_add
60
      &client_fname=$first_name&client_lname=$last_name&client_email=$email&client_status=1");
61
    }
62
63
    /*
64
   *  Edit Product
65
   */
66
    public function editProduct($product_name, $product_sku)
67
    {
68
        $productId = $this->searchProductId($product_sku);
69
        $url = $this->url;
70
        $api_key_secret = $this->api_key_secret;
71
        $addProduct = $this->postCurl($url, "api_key_secret=$api_key_secret&api_function=products_edit
72
      &product_id=$productId&product_title=$product_name&product_sku=$product_sku&product_status=1");
73
    }
74
75
    /*
76
   *  Search for product id while updating client
77
   */
78
    public function searchProductId($product_sku)
79
    {
80
        try {
81
            $productId = '';
82
            $url = $this->url;
83
            $api_key_secret = $this->api_key_secret;
84
            $getProductId = $this->postCurl($url, "api_key_secret=$api_key_secret&api_function=search
85
            &search_type=product&search_keyword=$product_sku");
86
            $details = json_decode($getProductId);
87
            if ($details->api_error_detected == 0 && is_array($details->page_message)) {//This is not true if Product_sku is updated
88
                $productId = $details->page_message[0]->product_id;
89
            }
90
91
            return $productId;
92
        } catch (\Exception $ex) {
93
            $result = [$ex->getMessage()];
94
95
            return response()->json(compact('result'), 500);
96
        }
97
    }
98
99
    public function deleteProductFromAPL($product)
100
    {
101
        $url = $this->url;
102
        $api_key_secret = $this->api_key_secret;
103
        $productId = $this->searchProductId($product->product_sku);
104
        $productTitle = $product->name;
105
        $productSku = $product->sku;
106
        $delProduct = $this->postCurl($url, "api_key_secret=$api_key_secret&api_function=products_edit
107
         &product_id=$productId&product_title=$productTitle&product_sku=$productSku&product_status=1&delete_record=1");
108
    }
109
110
    /*
111
   *  Edit User
112
   */
113
    public function editUserInLicensing($first_name, $last_name, $email)
114
    {
115
        $userId = $this->searchForUserId($email);
116
        $url = $this->url;
117
        $api_key_secret = $this->api_key_secret;
118
        $addProduct = $this->postCurl($url, "api_key_secret=$api_key_secret&api_function=clients_edit&client_id=$userId
119
      &client_fname=$first_name&client_lname=$last_name&client_email=$email&client_status=1");
120
    }
121
122
    /*
123
   *  Search for user id while updating client
124
   */
125
    public function searchForUserId($email)
126
    {
127
        $userId = '';
128
        $url = $this->url;
129
        $api_key_secret = $this->api_key_secret;
130
        $getUserId = $this->postCurl($url, "api_key_secret=$api_key_secret&api_function=search
131
      &search_type=client&search_keyword=$email");
132
133
        $details = json_decode($getUserId);
134
        if ($details->api_error_detected == 0 && is_array($details->page_message)) {//This is not true if email is updated
135
            $userId = $details->page_message[0]->client_id;
136
        }
137
138
        return $userId;
139
    }
140
141
    /*
142
    *  Create New License For User
143
    */
144
    public function createNewLicene($orderid, $product, $user_id,
145
        $licenseExpiry, $updatesExpiry, $supportExpiry, $serial_key)
146
    {
147
        $url = $this->url;
148
        $api_key_secret = $this->api_key_secret;
149
        $sku = Product::where('id', $product)->first()->product_sku;
150
        $licenseExpiry = ($licenseExpiry != '') ? $licenseExpiry->toDateString() : '';
151
        $updatesExpiry = ($updatesExpiry != '') ? $updatesExpiry->toDateString() : '';
152
        $supportExpiry = ($supportExpiry != '') ? $supportExpiry->toDateString() : '';
153
        $order = Order::where('id', $orderid)->first();
154
        $orderNo = $order->number;
155
        $domain = $order->domain;
156
        $ipAndDomain = $this->getIpAndDomain($domain);
157
        $ip = $ipAndDomain['ip'];
158
        $domain = $ipAndDomain['domain'];
159
        $requireDomain = $ipAndDomain['requireDomain'];
160
        $productId = $this->searchProductId($sku);
161
        $addLicense = $this->postCurl($url, "api_key_secret=$api_key_secret&api_function=licenses_add&product_id=$productId&license_code=$serial_key&license_require_domain=1&license_status=1&license_order_number=$orderNo&license_domain=$domain&&license_ip=$ip&license_require_domain=$requireDomain&license_limit=6&license_expire_date=$licenseExpiry&license_updates_date=$updatesExpiry&license_support_date=$supportExpiry&license_disable_ip_verification=0&license_limit=2");
162
    }
163
164
    /*
165
    *  Edit Existing License
166
    */
167
    public function updateLicensedDomain($licenseCode, $domain, $productId, $licenseExpiry, $updatesExpiry, $supportExpiry, $orderNo, $license_limit = 2, $requiredomain = 1)
168
    {
169
        $l_expiry = '';
170
        $s_expiry = '';
171
        $u_expiry = '';
172
        if (strtotime($licenseExpiry) > 1) {
173
            $l_expiry = date('Y-m-d', strtotime($licenseExpiry));
174
        }
175
        if (strtotime($updatesExpiry) > 1) {
176
            $u_expiry = date('Y-m-d', strtotime($updatesExpiry));
177
        }
178
        if (strtotime($supportExpiry) > 1) {
179
            $s_expiry = date('Y-m-d', strtotime($supportExpiry));
180
        }
181
        $url = $this->url;
182
        $ipAndDomain = $this->getIpAndDomain($domain);
183
        $ip = $ipAndDomain['ip'];
184
        $domain = $ipAndDomain['domain'];
185
        $requireDomain = $ipAndDomain['requireDomain'];
186
        $api_key_secret = $this->api_key_secret;
187
        $searchLicense = $this->searchLicenseId($licenseCode, $productId);
188
        $licenseId = $searchLicense['licenseId'];
189
        $productId = $searchLicense['productId'];
190
        $licenseCode = $searchLicense['code'];
191
        $updateLicense = $this->postCurl($url, "api_key_secret=$api_key_secret&api_function=licenses_edit&product_id=$productId&license_code=$licenseCode&license_id=$licenseId&license_order_number=$orderNo&license_require_domain=$requireDomain&license_status=1&license_expire_date=$l_expiry&license_updates_date=$u_expiry&license_support_date=$s_expiry&license_domain=$domain&license_ip=$ip&license_limit=$license_limit");
192
    }
193
194
    /**
195
     * Get the Ip and domain that is to be entered in License Manager.
196
     *
197
     * @author Ashutosh Pathak <[email protected]>
198
     *
199
     * @date   2019-05-11T11:31:07+0530
200
     *
201
     * @param string $domain
202
     *
203
     * @return array
204
     */
205
    protected function getIpAndDomain($domain)
206
    {
207
        if ($domain != '') {
208
            if (ip2long($domain)) {
209
                return ['ip'=>$domain, 'domain'=>'', 'requireDomain'=>0];
210
            } else {
211
                return ['ip'=>'', 'domain'=>$domain, 'requireDomain'=>1];
212
            }
213
        } else {
214
            return ['ip'=>'', 'domain'=>'', 'requireDomain'=>0];
215
        }
216
    }
217
218
    public function searchLicenseId($licenseCode, $productId)
219
    {
220
        $license = '';
221
        $product = '';
222
        $code = '';
223
        $limit = '';
224
        $ipOrDomain = '';
225
        $url = $this->url;
226
        $api_key_secret = $this->api_key_secret;
227
        $getLicenseId = $this->postCurl($url, "api_key_secret=$api_key_secret&api_function=search
228
        &search_type=license&search_keyword=$licenseCode");
229
        $details = json_decode($getLicenseId);
230
        if ($details->api_error_detected == 0 && is_array($details->page_message)) {
231
            foreach ($details->page_message as $detail) {
232
                if ($detail->product_id == $productId) {
233
                    $license = $detail->license_id;
234
                    $product = $detail->product_id;
235
                    $code = $detail->license_code;
236
                    $limit = $detail->license_limit;
237
                    $ipOrDomain = $detail->license_require_domain;
238
                }
239
            }
240
        }
241
242
        return ['productId'=>$product, 'code'=>$code, 'licenseId'=>$license, 'allowedInstalltion'=>$ipOrDomain, 'installationLimit'=>$limit];
243
    }
244
245
    //Update the Installation status as Inactive after Licensed Domain Is Chnaged
246
    public function updateInstalledDomain($licenseCode, $productId)
247
    {
248
        $url = $this->url;
249
        $api_key_secret = $this->api_key_secret;
250
        //Search for the Installation Id
251
        $searchInstallationId = $this->searchInstallationId($licenseCode);
252
        $details = json_decode($searchInstallationId);
253
        if ($details->api_error_detected == 0 && is_array($details->page_message)) {
254
            foreach ($details->page_message as $detail) {
255
                if ($detail->product_id == $productId) {
256
                    $installation_id = $detail->installation_id;
257
                    $installation_ip = $detail->installation_ip;
258
                    //delete all existing installation
259
                    $updateInstallation = $this->postCurl($url, "api_key_secret=$api_key_secret&api_function=installations_edit&installation_id=$installation_id&installation_ip=$installation_ip&installation_status=0&delete_record=1");
260
                }
261
            }
262
        }
263
    }
264
265
    public function searchInstallationId($licenseCode)
266
    {
267
        $url = $this->url;
268
        $api_key_secret = $this->api_key_secret;
269
        $getInstallId = $this->postCurl($url, "api_key_secret=$api_key_secret&api_function=search
270
      &search_type=installation&search_keyword=$licenseCode");
271
272
        return $getInstallId;
273
    }
274
275
    public function searchInstallationPath($licenseCode, $productId)
276
    {
277
        $installation_domain = [];
278
        $installation_ip = [];
279
        $details = json_decode($this->searchInstallationId($licenseCode));
280
        if ($details->api_error_detected == 0 && is_array($details->page_message)) {
281
            foreach ($details->page_message as $detail) {
282
                if ($detail->product_id == $productId) {
283
                    $installation_domain[] = "<a href=https://$detail->installation_domain target = '_blank'>  "."$detail->installation_domain</a>".' | '.$detail->installation_ip;
284
                    $installation_ip[] = $detail->installation_ip;
285
                }
286
            }
287
        }
288
289
        return ['installed_path' => $installation_domain, 'installed_ip' => $installation_ip];
290
    }
291
292
    //Update  Expiration Date After Renewal
293
    public function updateExpirationDate($licenseCode, $expiryDate, $productId, $domain, $orderNo, $licenseExpiry, $supportExpiry, $license_limit = 2, $requiredomain = 1)
294
    {
295
        $url = $this->url;
296
        $ipAndDomain = $this->getIpAndDomain($domain);
297
        $ip = $ipAndDomain['ip'];
298
        $domain = $ipAndDomain['domain'];
299
        $requireDomain = $ipAndDomain['requireDomain'];
300
301
        $api_key_secret = $this->api_key_secret;
302
        $searchLicense = $this->searchLicenseId($licenseCode, $productId);
303
        $licenseId = $searchLicense['licenseId'];
304
        $productId = $searchLicense['productId'];
305
        $code = $searchLicense['code'];
306
        $updateLicense = $this->postCurl($url, "api_key_secret=$api_key_secret&api_function=licenses_edit&product_id=$productId&license_code=$code&license_id=$licenseId&license_order_number=$orderNo&license_domain=$domain&license_ip=$ip&license_require_domain=$requireDomain&license_status=1&license_expire_date=$licenseExpiry&license_updates_date=$expiryDate&license_support_date=$supportExpiry&license_limit=$license_limit");
307
    }
308
309
    public function getNoOfAllowedInstallation($licenseCode, $productId)
310
    {
311
        $api_key_secret = $this->api_key_secret;
312
        $searchLicense = $this->searchLicenseId($licenseCode, $productId);
313
314
        return $searchLicense['installationLimit'];
315
    }
316
317
    public function getInstallPreference($licenseCode, $productId)
318
    {
319
        $api_key_secret = $this->api_key_secret;
320
        $searchLicense = $this->searchLicenseId($licenseCode, $productId);
321
322
        return $searchLicense['allowedInstalltion'];
323
    }
324
}
325