Completed
Push — master ( fde4fb...b78da7 )
by vijay
111:36 queued 59:13
created

HomeController::decryptByFaveoPrivateKey()   B

Complexity

Conditions 5
Paths 13

Size

Total Lines 33
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 21
c 1
b 0
f 0
nc 13
nop 1
dl 0
loc 33
rs 8.439
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Model\Order\Order;
6
use App\Model\Product\Product;
7
use Illuminate\Http\Request;
8
use Exception;
9
10
class HomeController extends Controller
11
{
12
    /*
13
      |--------------------------------------------------------------------------
14
      | Home Controller
15
      |--------------------------------------------------------------------------
16
      |
17
      | This controller renders your application's "dashboard" for users that
18
      | are authenticated. Of course, you are free to change or remove the
19
      | controller as you wish. It is just here to get your app started!
20
      |
21
     */
22
23
    /**
24
     * Create a new controller instance.
25
     *
26
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
27
     */
28
    public function __construct()
29
    {
30
        $this->middleware('auth', ['only' => ['index']]);
31
        $this->middleware('admin', ['only' => ['index']]);
32
    }
33
34
    /**
35
     * Show the application dashboard to the user.
36
     *
37
     * @return Response
38
     */
39
    public function index()
40
    {
41
        return view('themes.default1.layouts.master');
42
    }
43
44
    public function version(Request $request, Product $product)
45
    {
46
        $url = $request->input('response_url');
47
48
        $title = $request->input('title');
49
        //dd($title);
50
        $id = $request->input('id');
51
        if ($id) {
52
            $product = $product->where('id', $id)->first();
53
        } else {
54
            $product = $product->where('name', $title)->first();
55
        }
56
57
        if ($product) {
58
            $version = str_replace('v', '', $product->version);
59
        } else {
60
            $version = 'Not-Available';
61
        }
62
63
        echo "<form action=$url method=post name=redirect >";
64
        echo '<input type=hidden name=_token value='.csrf_token().'>';
65
        echo "<input type=hidden name=value value=$version />";
66
        echo '</form>';
67
        echo"<script language='javascript'>document.redirect.submit();</script>";
68
    }
69
70
    public function getVersion(Request $request, Product $product)
71
    {
72
        $this->validate($request, [
73
            'title' => 'required',
74
        ]);
75
        $title = $request->input('title');
76
        $product = $product->where('name', $title)->first();
77
        if ($product) {
78
            $version = $product->version;
0 ignored issues
show
Unused Code introduced by
$version is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
79
        } else {
80
            return 0;
81
        }
82
        return str_replace('v', '', $product->version);
83
    }
84
85
    public function versionTest() {
86
        $s = "eyJpdiI6ImFIVDByR29vVzNpcEExM2UyNDVaWXc9PSIsInZhbHVlIjoiODNJS0MxWXFyVEtrYjhZYXFmUFlvOTJYY09NUHhGYTZBemN2eFMzckZCST0iLCJtYWMiOiI2MDdmZTU5YmRjMjQxOWRlZjE3ODUyMWI0OTk5NDM5ZmQxMWE5ZTUyNzQ3YTMyOGQyYmRmNGVkYWQyNDM5ZTNkIn0=";
87
        dd(decrypt($s));
88
        $url = "http://localhost/billings/agorainvoicing/agorainvoicing/public/version";
0 ignored issues
show
Unused Code introduced by
$url is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
89
        $response = "http://localhost/billings/agorainvoicing/agorainvoicing/public/version-result";
0 ignored issues
show
Unused Code introduced by
$response is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
90
        $name = "faveo helpdesk community";
0 ignored issues
show
Unused Code introduced by
$name is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
91
        $version = $product->version;
0 ignored issues
show
Unused Code introduced by
$version is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
Bug introduced by
The variable $product does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
92
93
        return str_replace('v', '', $product->version);
94
    }
95
96
    public function versionTest()
97
    {
98
        $url = 'http://localhost/billings/agorainvoicing/agorainvoicing/public/version';
99
        $response = 'http://localhost/billings/agorainvoicing/agorainvoicing/public/version-result';
100
        $name = 'faveo helpdesk community';
0 ignored issues
show
Unused Code introduced by
$name is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
101
        echo "<form action=$url method=post name=redirect >";
102
        echo '<input type=hidden name=_token value=csrf_token() />';
103
        echo "<input type=hidden name=response_url value=$response />";
104
        echo "<input type=hidden name=title value='faveo helpdesk community' />";
105
        echo '</form>';
106
        echo"<script language='javascript'>document.redirect.submit();</script>";
107
    }
108
109
    public function versionResult(Request $request)
110
    {
111
        dd($request->all());
112
    }
113
114
    public function serial(Request $request, Order $order)
115
    {
116
        $ul = $request->input('url');
117
        $url = str_replace('serial', 'CheckSerial', $ul);
118
        $domain = $request->input('domain');
119
        $first = $request->input('first');
120
        $second = $request->input('second');
121
        $third = $request->input('third');
122
        $forth = $request->input('forth');
123
        $serial = $first.$second.$third.$forth;
124
        //dd($serial);
125
        $order_no = $request->input('order_no');
126
        $order = $order->where('number', $order_no)->first();
127
        if ($order) {
128
            if ($domain === $order->domain) {
129
                $key = $order->serial_key;
130
                if ($key === $serial) {
131
                    $id1 = 'true';
132
                    echo "<form action=$url/$id1 method=post name=redirect>";
133
                    echo '<input type=hidden name=_token value=csrf_token()/>';
134
                    echo '</form>';
135
                    echo"<script language='javascript'>document.redirect.submit();</script>";
136
                } else {
137
                    $id = 'false1';
138
                    echo "<form action=$url/$id method=post name=redirect>";
139
                    echo '<input type=hidden name=_token value=csrf_token()/>';
140
                    echo '</form>';
141
                    echo"<script language='javascript'>document.redirect.submit();</script>";
142
                }
143
            } else {
144
                $id = 'false3';
145
                echo "<form action=$url/$id method=post name=redirect>";
146
                echo '<input type=hidden name=_token value=csrf_token()/>';
147
                echo '</form>';
148
                echo"<script language='javascript'>document.redirect.submit();</script>";
149
            }
150
        } else {
151
            $id = 'false2';
152
            echo "<form action=$url/$id method=post name=redirect>";
153
            echo '<input type=hidden name=_token value=csrf_token()/>';
154
            echo '</form>';
155
            echo"<script language='javascript'>document.redirect.submit();</script>";
156
        }
157
    }
158
159
    public static function decryptByFaveoPrivateKey($encrypted) {
160
        try {
161
            // Get the private Key
162
            $path = storage_path('app/faveo-private.key');
163
            $key_content = file_get_contents($path);
164
            if (!$privateKey = openssl_pkey_get_private($key_content)) {
165
                throw new \Exception('Private Key failed');
166
            }
167
            $a_key = openssl_pkey_get_details($privateKey);
168
169
            // Decrypt the data in the small chunks
170
            $chunkSize = ceil($a_key['bits'] / 8);
171
            $output = '';
172
173
            while ($encrypted) {
174
                $chunk = substr($encrypted, 0, $chunkSize);
175
                $encrypted = substr($encrypted, $chunkSize);
176
                $decrypted = '';
177
                if (!openssl_private_decrypt($chunk, $decrypted, $privateKey)) {
178
                    die('Failed to decrypt data');
179
                }
180
                $output .= $decrypted;
181
            }
182
            openssl_free_key($privateKey);
183
184
            // Uncompress the unencrypted data.
185
            $output = gzuncompress($output);
186
187
            return $output;
188
        } catch (\Exception $ex) {
189
            dd($ex);
190
        }
191
    }
192
193
    public function getEncryptedData(Request $request) {
194
        $enc = $request->input('en');
195
        $result = self::decryptByFaveoPrivateKey($enc);
196
        return response()->json($result);
197
    }
198
199
    public function createEncryptionKeys() {
200
        try {
201
            $privateKey = openssl_pkey_new(array(
202
                'private_key_bits' => 2048, // Size of Key.
203
                'private_key_type' => OPENSSL_KEYTYPE_RSA,
204
            ));
205
            //dd($privateKey);
206
            // Save the private key to private.key file. Never share this file with anyone.
207
            openssl_pkey_export_to_file($privateKey, 'faveo-private-new.key');
208
209
            // Generate the public key for the private key
210
            $a_key = openssl_pkey_get_details($privateKey);
211
            //dd($a_key);
212
            // Save the public key in public.key file. Send this file to anyone who want to send you the encrypted data.
213
            file_put_contents('faveo-public-new.key', $a_key['key']);
214
215
            // Free the private Key.
216
            openssl_free_key($privateKey);
217
        } catch (\Exception $ex) {
218
            dd($ex);
219
        }
220
    }
221
222
    public function checkSerialKey($faveo_encrypted_key,$order_number) {
223
        try {
224
            $order = new Order();
225
            $faveo_decrypted_key = self::decryptByFaveoPrivateKey($faveo_encrypted_key);
226
            $this_order = $order->where('number', $order_number)->first();
227
            if (!$this_order) {
228
                return null;
229
            } else {
230
               if($this_order->serial_key == $faveo_decrypted_key){
231
                   return $this_order->serial_key;
232
               }
233
            }
234
            return null;
235
        } catch (Exception $ex) {
236
            throw new Exception($ex->getMessage());
237
        }
238
    }
239
240
    public function checkOrder($faveo_encrypted_order_number) {
241
        try {
242
            $order = new Order();
243
            $faveo_decrypted_order = self::decryptByFaveoPrivateKey($faveo_encrypted_order_number);
244
            $this_order = $order->where('number', $faveo_decrypted_order)->first();
245
            if (!$this_order) {
246
                return null;
247
            } else {
248
                return $this_order->number;
249
            }
250
        } catch (Exception $ex) {
251
            throw new Exception($ex->getMessage());
252
        }
253
    }
254
255 View Code Duplication
    public function checkDomain($request_url) {
256
        try {
257
//            echo $request_url;
258
//            exit();
259
            $order = new Order();
260
            $this_order = $order->where('domain', $request_url)->first();
261
            if (!$this_order) {
262
                return null;
263
            } else {
264
                return $this_order->domain;
265
            }
266
        } catch (Exception $ex) {
267
            throw new Exception($ex->getMessage());
268
        }
269
    }
270
271 View Code Duplication
    public function verifyOrder($order_number, $serial_key, $domain) {
272
        try {
273
            
274
            $order = new Order();
275
            $this_order = $order
276
                    ->where('number', $order_number)
277
                   // ->where('serial_key', $serial_key)
278
                    ->where('domain', $domain)
279
                    ->first();
280
            return $this_order;
281
        } catch (Exception $ex) {
282
            throw new Exception($ex->getMessage());
283
        }
284
    }
285
286
    public function faveoVerification(Request $request) {
287
        try {
288
            $url = $request->input('url');
289
            $faveo_encrypted_order_number = $request->input('order_number');
290
            $faveo_encrypted_key = $request->input('serial_key');
291
            $request_type = $request->input('request_type');
292
            $faveo_name = $request->input('name');
293
            $faveo_version = $request->input('version');
294
            $order_number = $this->checkOrder($faveo_encrypted_order_number);
295
            $domain = $this->checkDomain($url);
296
            $serial_key = $this->checkSerialKey($faveo_encrypted_key,$order_number);
297
            //return $serial_key;
298
            $result = [];
299
            if ($request_type == 'install') {
300
                $result = $this->verificationResult($order_number, $serial_key, $domain);
301
            }
302
            if ($request_type == 'check_update') {
303
                $result = $this->checkUpdate($order_number, $serial_key, $domain, $faveo_name, $faveo_version);
304
            }
305
           
306
            return response()->json($result);
0 ignored issues
show
Bug introduced by
It seems like $result can also be of type null; however, Illuminate\Contracts\Rou...ResponseFactory::json() does only seem to accept string|array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
307
        } catch (Exception $ex) {
308
            $result = ['status' => 'error', 'message' => $ex->getMessage()];
309
            return response()->json($result);
310
        }
311
    }
312
313
    public function verificationResult($order_number, $serial_key, $domain) {
314
        try {
315
316
            if ($order_number && $domain && $serial_key) {
317
                $order = $this->verifyOrder($order_number, $serial_key, $domain);
318
                if ($order) {
319
                    return ['status' => 'success', 'message' => 'This is a valid request'];
320
                }
321
            } else {
322
                return ['status' => 'fails', 'message' => 'This is an invalid request'];
323
            }
324
            
325
        } catch (Exception $ex) {
326
            throw new Exception($ex->getMessage());
327
        }
328
    }
329
330
    public function checkUpdate($order_number, $serial_key, $domain, $faveo_name, $faveo_version) {
331
        try {
332
            if ($order_number && $domain && $serial_key) {
333
                $order = $this->verifyOrder($order_number, $serial_key, $domain);
334
                //var_dump($order);
335
                if ($order) {
336
                    return $this->checkFaveoDetails($order_number, $faveo_name, $faveo_version);
337
                }
338
            } else {
339
                return ['status' => 'fails', 'message' => 'This is an invalid request'];
340
            }
341
        } catch (Exception $ex) {
342
            throw new Exception($ex->getMessage());
343
        }
344
    }
345
346
    public function checkFaveoDetails($order_number, $faveo_name, $faveo_version) {
347
        try {
348
            $order = new Order();
349
            $product = new Product();
350
            $this_order = $order->where('number', $order_number)->first();
351
            if ($this_order) {
352
                $product_id = $this_order->product;
353
                if($product_id){
354
                    $this_product = $product->where('id',$product_id)->first();
355
                    if($this_product){
356
                        $version = str_replace('v', '', $this_product->version);
357
                        return ['status' => 'success', 'message' => 'This is a valid request','version'=>$version];
358
                    }
359
                }
360
            }
361
            return ['status' => 'fails', 'message' => 'This is an invalid request'];
362
            
363
        } catch (Exception $ex) {
364
            throw new Exception($ex->getMessage());
365
        }
366
    }
367
368
}
369