Completed
Push — development ( c0a597...4b3f17 )
by Ashutosh
11:26
created

BaseHomeController   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 141
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 25
eloc 72
dl 0
loc 141
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A index() 0 4 1
A getTotalSales() 0 7 1
A getEncryptedData() 0 6 1
A checkDomain() 0 12 3
A decryptByFaveoPrivateKey() 0 14 1
A getDomain() 0 9 3
A verificationResult() 0 15 6
A hook() 0 6 2
A checkSerialKey() 0 17 4
A verifyOrder() 0 17 3
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use Illuminate\Http\Request;
6
7
class BaseHomeController extends Controller
8
{
9
     public static function decryptByFaveoPrivateKey($encrypted)
10
    {
11
        $encrypted = json_decode($encrypted);
12
        $sealed_data = $encrypted->seal;
13
        $envelope = $encrypted->envelope;
14
        $input = base64_decode($sealed_data);
15
        $einput = base64_decode($envelope);
16
        $path = storage_path('app'.DIRECTORY_SEPARATOR.'private.key');
17
        $key_content = file_get_contents($path);
18
        $private_key = openssl_get_privatekey($key_content);
19
        $plaintext = null;
20
        openssl_open($input, $plaintext, $einput, $private_key);
21
22
        return $plaintext;
23
    }
24
25
    public function getTotalSales()
26
    {
27
        $invoice = new Invoice();
28
        $total = $invoice->pluck('grand_total')->all();
29
        $grandTotal = array_sum($total);
30
31
        return $grandTotal;
32
    }
33
34
35
    public function checkDomain($request_url)
36
    {
37
        try {
38
            $order = new Order();
39
            $this_order = $order->where('domain', $request_url)->first();
40
            if (!$this_order) {
41
                return;
42
            } else {
43
                return $this_order->domain;
44
            }
45
        } catch (Exception $ex) {
0 ignored issues
show
Bug introduced by
The type App\Http\Controllers\Exception was not found. Did you mean Exception? If so, make sure to prefix the type with \.
Loading history...
46
            throw new Exception($ex->getMessage());
47
        }
48
    }
49
50
51
    public function checkSerialKey($faveo_encrypted_key, $order_number)
52
    {
53
        try {
54
            $order = new Order();
55
            //$faveo_decrypted_key = self::decryptByFaveoPrivateKey($faveo_encrypted_key);
56
            $this_order = $order->where('number', $order_number)->first();
57
            if (!$this_order) {
58
                return;
59
            } else {
60
                if ($this_order->serial_key == $faveo_encrypted_key) {
61
                    return $this_order->serial_key;
62
                }
63
            }
64
65
            return;
66
        } catch (Exception $ex) {
67
            throw new Exception($ex->getMessage());
68
        }
69
    }
70
71
72
    public function verifyOrder($order_number, $serial_key, $domain)
73
    {
74
        if (ends_with($domain, '/')) {
75
            $domain = substr_replace($value, '', -1, 1);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $value seems to be never defined.
Loading history...
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
93
    public function hook(Request $request)
94
    {
95
        try {
96
            \Log::info('requests', $request->all());
97
        } catch (Exception $ex) {
98
            dd($ex);
99
        }
100
    }
101
102
103
    public function index()
104
    {
105
        $totalSales = $this->getTotalSales();
106
		return view('themes.default1.common.dashboard');
107
    }
108
109
110
    public function getDomain($url)
111
    {
112
    $pieces = parse_url($url);
113
    $domain = isset($pieces['host']) ? $pieces['host'] : '';
114
    if (preg_match('/(?P<domain>[a-z0-9][a-z0-9\-]{1,63}\.[a-z\.]{2,6})$/i', $domain, $regs)) {
115
        return $regs['domain'];
116
    }
117
118
     return $domain;
119
    }
120
121
122
123
    public function verificationResult($order_number, $serial_key, $domain)
124
    {
125
        try {
126
            if ($order_number && $domain && $serial_key) {
127
                $order = $this->verifyOrder($order_number, $serial_key, $domain);
128
                if ($order) {
129
                    return ['status' => 'success', 'message' => 'this-is-a-valid-request', 'order_number' => $order_number, 'serial' => $serial_key];
130
                } else {
131
                    return ['status' => 'fails', 'message' => 'this-is-an-invalid-request'];
132
                }
133
            } else {
134
                return ['status' => 'fails', 'message' => 'this-is-an-invalid-request'];
135
            }
136
        } catch (Exception $ex) {
137
            throw new Exception($ex->getMessage());
138
        }
139
    }
140
141
142
    public function getEncryptedData(Request $request)
143
    {
144
        $enc = $request->input('en');
145
        $result = self::decryptByFaveoPrivateKey($enc);
146
147
        return response()->json($result);
148
    }
149
}
150