Completed
Push — development ( 2fd849...7f23a5 )
by Ashutosh
24:05 queued 14:01
created

BaseHomeController::checkUpdatesExpiry()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 26
rs 9.4222
c 0
b 0
f 0
cc 5
nc 8
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 hook(Request $request)
93
    {
94
        try {
95
            \Log::info('requests', $request->all());
96
        } catch (\Exception $ex) {
97
            dd($ex);
98
        }
99
    }
100
101
    public function index()
102
    {
103
        $totalSales = $this->getTotalSales();
104
105
        return view('themes.default1.common.dashboard');
106
    }
107
108
    public function getDomain($url)
109
    {
110
        $pieces = parse_url($url);
111
        $domain = isset($pieces['host']) ? $pieces['host'] : '';
112
        if (preg_match('/(?P<domain>[a-z0-9][a-z0-9\-]{1,63}\.[a-z\.]{2,6})$/i', $domain, $regs)) {
113
            return $regs['domain'];
114
        }
115
116
        return $domain;
117
    }
118
119
    public function verificationResult($order_number, $serial_key)
120
    {
121
        try {
122
            if ($order_number && $serial_key) {
123
                $order = $this->verifyOrder($order_number, $serial_key);
124
                if ($order) {
125
                    return ['status' => 'success', 'message' => 'this-is-a-valid-request',
126
                    'order_number'   => $order_number, 'serial' => $serial_key, ];
127
                } else {
128
                    return ['status' => 'fails', 'message' => 'this-is-an-invalid-request'];
129
                }
130
            } else {
131
                return ['status' => 'fails', 'message' => 'this-is-an-invalid-request'];
132
            }
133
        } catch (\Exception $ex) {
134
            throw new \Exception($ex->getMessage());
135
        }
136
    }
137
138
    public function getEncryptedData(Request $request)
139
    {
140
        $enc = $request->input('en');
141
        $result = self::decryptByFaveoPrivateKey($enc);
142
143
        return response()->json($result);
144
    }
145
146
    public function checkUpdatesExpiry(Request $request)
147
    {
148
        $v = \Validator::make($request->all(), [
149
          'order_number' => 'required',
150
        ]);
151
        if ($v->fails()) {
152
            $error = $v->errors();
153
154
            return response()->json(compact('error'));
155
        }
156
157
        try {
158
            $order_number = $request->input('order_number');
159
            $orderId = Order::where('number', 'LIKE', $order_number)->pluck('id')->first();
160
            if ($orderId) {
161
                $expiryDate = Subscription::where('order_id', $orderId)->pluck('update_ends_at')->first();
162
                if (\Carbon\Carbon::now()->toDateTimeString() < $expiryDate->toDateTimeString()) {
163
                    return ['status' => 'success', 'message' => 'allow-auto-update'];
164
                }
165
            }
166
167
            return ['status' => 'fails', 'message' => 'do-not-allow-auto-update'];
168
        } catch (\Exception $e) {
169
            $result = ['status'=>'fails', 'error' => $e->getMessage()];
170
171
            return $result;
172
        }
173
    }
174
}
175