BaseHomeController::updateLicenseCode()   A
last analyzed

Complexity

Conditions 3
Paths 12

Size

Total Lines 37
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 2 Features 0
Metric Value
eloc 26
dl 0
loc 37
rs 9.504
c 4
b 2
f 0
cc 3
nc 12
nop 1
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Model\Order\Invoice;
6
use App\Model\Order\Order;
7
use App\Model\Product\Subscription;
8
use Illuminate\Http\Request;
9
10
class BaseHomeController extends Controller
11
{
12
    public static function decryptByFaveoPrivateKey($encrypted)
13
    {
14
        $encrypted = json_decode($encrypted);
15
        $sealed_data = $encrypted->seal;
16
        $envelope = $encrypted->envelope;
17
        $input = base64_decode($sealed_data);
18
        $einput = base64_decode($envelope);
19
        $path = storage_path('app'.DIRECTORY_SEPARATOR.'private.key');
20
        $key_content = file_get_contents($path);
21
        $private_key = openssl_get_privatekey($key_content);
22
        $plaintext = null;
23
        openssl_open($input, $plaintext, $einput, $private_key);
24
25
        return $plaintext;
26
    }
27
28
    public function getTotalSales()
29
    {
30
        $invoice = new Invoice();
31
        $total = $invoice->pluck('grand_total')->all();
32
        $grandTotal = array_sum($total);
33
34
        return $grandTotal;
35
    }
36
37
    public function checkDomain($request_url)
38
    {
39
        try {
40
            $order = new Order();
41
            $this_order = $order->where('domain', $request_url)->first();
42
            if (! $this_order) {
43
                return;
44
            } else {
45
                return $this_order->domain;
46
            }
47
        } catch (\Exception $ex) {
48
            throw new \Exception($ex->getMessage());
49
        }
50
    }
51
52
    public function checkSerialKey($faveo_encrypted_key, $order_number)
53
    {
54
        try {
55
            $order = new Order();
56
            //$faveo_decrypted_key = self::decryptByFaveoPrivateKey($faveo_encrypted_key);
57
            $this_order = $order->where('number', $order_number)->first();
58
            if (! $this_order) {
59
                return;
60
            } else {
61
                if ($this_order->serial_key == $faveo_encrypted_key) {
62
                    return $this_order->serial_key;
63
                }
64
            }
65
66
            return;
67
        } catch (\Exception $ex) {
68
            throw new \Exception($ex->getMessage());
69
        }
70
    }
71
72
    public function verifyOrder($order_number, $serial_key)
73
    {
74
        // if (ends_with($domain, '/')) {
75
        //     $domain = substr_replace($domain, '', -1, 1);
76
        // }
77
        //dd($domain);
78
        try {
79
            $order = new Order();
80
            $this_order = $order
81
                    ->where('number', $order_number)
82
                    //->where('serial_key', $serial_key)
83
                    // ->where('domain', $domain)
84
                    ->first();
85
86
            return $this_order;
87
        } catch (\Exception $ex) {
88
            throw new \Exception($ex->getMessage());
89
        }
90
    }
91
92
    public function index()
93
    {
94
        $totalSales = $this->getTotalSales();
95
96
        return view('themes.default1.common.dashboard');
97
    }
98
99
    public function getDomain($url)
100
    {
101
        $pieces = parse_url($url);
102
        $domain = isset($pieces['host']) ? $pieces['host'] : '';
103
        if (preg_match('/(?P<domain>[a-z0-9][a-z0-9\-]{1,63}\.[a-z\.]{2,6})$/i', $domain, $regs)) {
104
            return $regs['domain'];
105
        }
106
107
        return $domain;
108
    }
109
110
    public function verificationResult($order_number, $serial_key)
111
    {
112
        try {
113
            if ($order_number && $serial_key) {
114
                $order = $this->verifyOrder($order_number, $serial_key);
115
                if ($order) {
116
                    return ['status' => 'success', 'message' => 'this-is-a-valid-request',
117
                        'order_number'   => $order_number, 'serial' => $serial_key, ];
118
                } else {
119
                    return ['status' => 'fails', 'message' => 'this-is-an-invalid-request'];
120
                }
121
            } else {
122
                return ['status' => 'fails', 'message' => 'this-is-an-invalid-request'];
123
            }
124
        } catch (\Exception $ex) {
125
            throw new \Exception($ex->getMessage());
126
        }
127
    }
128
129
    public function getEncryptedData(Request $request)
130
    {
131
        $enc = $request->input('en');
132
        $result = self::decryptByFaveoPrivateKey($enc);
133
134
        return response()->json($result);
135
    }
136
137
    public function checkUpdatesExpiry(Request $request)
138
    {
139
        // $v = \Validator::make($request->all(), [
140
        //   'order_number' => 'required',
141
        // ]);
142
        // if ($v->fails()) {
143
        //     $error = $v->errors();
144
145
        //     return response()->json(compact('error'));
146
        // }
147
148
        try {
149
            $order_number = $request->input('order_number');
150
            $licenseCode = $request->input('license_code');
151
            if ($order_number) {
152
                $orderId = Order::where('number', 'LIKE', $order_number)->pluck('id')->first();
153
                if ($orderId) {
154
                    $expiryDate = Subscription::where('order_id', $orderId)->pluck('update_ends_at')->first();
155
                    if (\Carbon\Carbon::now()->toDateTimeString() < $expiryDate) {
156
                        return ['status' => 'success', 'message' => 'allow-auto-update'];
157
                    }
158
                }
159
            } elseif ($licenseCode) {
160
                $orderForLicense = Order::all()->filter(function ($order) use ($licenseCode) {
161
                    if ($order->serial_key == $licenseCode) {
162
                        return $order;
163
                    }
164
                });
165
                if (count($orderForLicense) > 0) {
166
                    $expiryDate = Subscription::where('order_id', $orderForLicense->first()->id)->pluck('update_ends_at')->first();
167
                    if (\Carbon\Carbon::now()->toDateTimeString() < $expiryDate) {
168
                        return ['status' => 'success', 'message' => 'allow-auto-update'];
169
                    }
170
                }
171
            }
172
173
            return ['status' => 'fails', 'message' => 'do-not-allow-auto-update'];
174
        } catch (\Exception $e) {
175
            $result = ['status'=>'fails', 'error' => $e->getMessage()];
176
177
            return $result;
178
        }
179
    }
180
181
    public function updateLatestVersion(Request $request)
182
    {
183
        try {
184
            $orderId = null;
185
            $licenseCode = $request->input('licenseCode');
186
            $orderForLicense = Order::all()->filter(function ($order) use ($licenseCode) {
187
                if ($order->serial_key == $licenseCode) {
188
                    return $order;
189
                }
190
            });
191
            if (count($orderForLicense) > 0) {
192
                $latestVerison = Subscription::where('order_id', $orderForLicense->first()->id)->update(['version'=>$request->input('version')]);
193
194
                return ['status' => 'success', 'message' => 'version-updated-successfully'];
195
            }
196
197
            return ['status' => 'fails', 'message' => 'version-not updated'];
198
        } catch (\Exception $e) {
199
            $result = ['status'=>'fails', 'error' => $e->getMessage()];
200
201
            return $result;
202
        }
203
    }
204
205
    public function updateLicenseCode(Request $request)
206
    {
207
        try {
208
            $licCode = $request->input('licenseCode'); //The license code already existing for older client
209
            $lastFour = $this->getLastFourDigistsOfLicenseCode($request->input('product'));
210
            $existingLicense = Order::select('id', 'client', 'product', 'serial_key')->get()
211
                ->filter(function ($order) use ($licCode) {
212
                    return $order->serial_key == $licCode;
213
                })->first();
214
215
            if ($existingLicense) {//If the license code that is sent in the request exists in billing
216
                $cont = new \App\Http\Controllers\License\LicenseController();
217
                $cont->updateInstalledDomain($licCode, $existingLicense->product); //Delete the installation first for the current license before updating license so that no Faveo installation exists on the user domain/IP path
218
219
                $serial_key = substr($licCode, 0, 12).$lastFour; //The new License Code
220
                //Create new license in license manager with the new license code which has no. of agents in the last 4 digits.
221
                $cont->createNewLicene(
222
                    $existingLicense->id,
223
                    $existingLicense->product,
224
                    $existingLicense->client,
225
                    $this->getLicenseExpiryDate($existingLicense),
226
                    $this->getUpdatesExpiryDate($existingLicense),
227
                    $this->getSupportExpiryDate($existingLicense),
228
                    $serial_key
229
                );
230
                //Update the old license code with new one in billing.
231
                $existingLicense->serial_key = \Crypt::encrypt(substr($licCode, 0, 12).$lastFour);
232
                $existingLicense->save();
233
                //send the newly updated license code in response
234
                $result = ['status'=>'success', 'updatedLicenseCode'=>$existingLicense->serial_key];
235
236
                return response()->json($result);
237
            }
238
        } catch (\Exception $ex) {
239
            $result = ['status'=>'fails', 'error' => $ex->getMessage()];
240
241
            return response()->json($result);
242
        }
243
    }
244
245
    public function getLastFourDigistsOfLicenseCode($productName)
246
    {
247
        switch ($productName) {
248
            case strpos($productName, 'Enterprise') > 0:
249
            case strpos($productName, 'Company') > 0:
250
                return '0000';
251
252
            case strpos($productName, 'Freelancer') > 0:
253
                return '0002';
254
255
            case strpos($productName, 'Startup') > 0:
256
                return '0005';
257
258
            case strpos($productName, 'SME') > 0:
259
                return '0010';
260
261
            default:
262
                throw new \Exception(\Lang::get('message.product_not_found'));
263
        }
264
    }
265
266
    public function getUpdatesExpiryDate($existingLicense)
267
    {
268
        $updatesDate = \Carbon\Carbon::parse(Subscription::where('order_id', $existingLicense->id)->value('update_ends_at'));
269
        if (strtotime($updatesDate) < 0) {
270
            $updatesDate = '';
271
        }
272
273
        return $updatesDate;
274
    }
275
276
    public function getLicenseExpiryDate($existingLicense)
277
    {
278
        $licenseDate = \Carbon\Carbon::parse(Subscription::where('order_id', $existingLicense->id)->value('ends_at'));
279
        if (strtotime($licenseDate) < 0) {
280
            $licenseDate = '';
281
        }
282
283
        return $licenseDate;
284
    }
285
286
    public function getSupportExpiryDate($existingLicense)
287
    {
288
        $supportDate = \Carbon\Carbon::parse(Subscription::where('order_id', $existingLicense->id)->value('support_ends_at'));
289
        if (strtotime($supportDate) < 0) {
290
            $supportDate = '';
291
        }
292
293
        return $supportDate;
294
    }
295
}
296