1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use App\Model\Order\Order; |
6
|
|
|
use App\Model\Product\Product; |
7
|
|
|
use Exception; |
8
|
|
|
use Illuminate\Http\Request; |
9
|
|
|
|
10
|
|
|
class HomeController extends BaseHomeController |
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 |
27
|
|
|
*/ |
28
|
|
|
public function __construct() |
29
|
|
|
{ |
30
|
|
|
$this->middleware('auth', ['only' => ['index']]); |
31
|
|
|
$this->middleware('admin', ['only' => ['index']]); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function getVersion(Request $request, Product $product) |
35
|
|
|
{ |
36
|
|
|
$this->validate($request, [ |
37
|
|
|
'title' => 'required', |
38
|
|
|
]); |
39
|
|
|
$title = $request->input('title'); |
40
|
|
|
$product = $product->where('name', $title)->first(); |
41
|
|
|
if ($product) { |
42
|
|
|
$version = $product->version; |
43
|
|
|
} else { |
44
|
|
|
return json_encode(['message'=>'Product not found']); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
return str_replace('v', '', $product->version); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function serialV2(Request $request, Order $order) |
51
|
|
|
{ |
52
|
|
|
try { |
53
|
|
|
$faveo_encrypted_order_number = self::decryptByFaveoPrivateKey($request->input('order_number')); |
54
|
|
|
$faveo_encrypted_key = self::decryptByFaveoPrivateKey($request->input('serial_key')); |
55
|
|
|
\Log::emergency(json_encode(['domain' => $request |
56
|
|
|
->input('domain'), 'enc_serial' => $faveo_encrypted_key, |
57
|
|
|
'enc_order' => $faveo_encrypted_order_number, ])); |
58
|
|
|
$request_type = $request->input('request_type'); |
59
|
|
|
$faveo_name = $request->input('name'); |
60
|
|
|
$faveo_version = $request->input('version'); |
61
|
|
|
$order_number = $this->checkOrder($faveo_encrypted_order_number); |
62
|
|
|
$domain = $request->input('domain'); |
63
|
|
|
$domain = $this->checkDomain($domain); |
64
|
|
|
$serial_key = $this->checkSerialKey($faveo_encrypted_key, $order_number); |
65
|
|
|
|
66
|
|
|
\Log::emergency(json_encode(['domain' => $request->input('domain'), |
67
|
|
|
'serial' => $serial_key, 'order' => $order_number, ])); |
68
|
|
|
$result = []; |
69
|
|
|
if ($request_type == 'install') { |
70
|
|
|
$result = $this->verificationResult($order_number, $serial_key); |
71
|
|
|
} |
72
|
|
|
if ($request_type == 'check_update') { |
73
|
|
|
$result = $this->checkUpdate($order_number, $serial_key, $domain, $faveo_name, $faveo_version); |
74
|
|
|
} |
75
|
|
|
$result = self::encryptByPublicKey(json_encode($result)); |
76
|
|
|
|
77
|
|
|
return $result; |
78
|
|
|
} catch (Exception $ex) { |
79
|
|
|
$result = ['status' => 'error', 'message' => $ex->getMessage()]; |
80
|
|
|
$result = self::encryptByPublicKey(json_encode($result)); |
81
|
|
|
|
82
|
|
|
return $result; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function serial(Request $request, Order $order) |
87
|
|
|
{ |
88
|
|
|
try { |
89
|
|
|
$url = $request->input('url'); |
90
|
|
|
$faveo_encrypted_order_number = self::decryptByFaveoPrivateKey($request->input('order_number')); |
91
|
|
|
$domain = $this->getDomain($request->input('domain')); |
92
|
|
|
|
93
|
|
|
//return $domain; |
94
|
|
|
$faveo_encrypted_key = self::decryptByFaveoPrivateKey($request->input('serial_key')); |
95
|
|
|
$request_type = $request->input('request_type'); |
96
|
|
|
$faveo_name = $request->input('name'); |
97
|
|
|
$faveo_version = $request->input('version'); |
98
|
|
|
$order_number = $this->checkOrder($faveo_encrypted_order_number); |
99
|
|
|
|
100
|
|
|
$domain = $this->checkDomain($domain); |
101
|
|
|
$serial_key = $this->checkSerialKey($faveo_encrypted_key, $order_number); |
102
|
|
|
//dd($serial_key); |
103
|
|
|
//return $serial_key; |
104
|
|
|
$result = []; |
105
|
|
|
if ($request_type == 'install') { |
106
|
|
|
$result = $this->verificationResult($order_number, $serial_key); |
107
|
|
|
} |
108
|
|
|
if ($request_type == 'check_update') { |
109
|
|
|
$result = $this->checkUpdate($order_number, $serial_key, $domain, $faveo_name, $faveo_version); |
110
|
|
|
} |
111
|
|
|
$result = self::encryptByPublicKey(json_encode($result)); |
112
|
|
|
$this->submit($result, $url); |
113
|
|
|
} catch (Exception $ex) { |
114
|
|
|
$result = ['status' => 'error', 'message' => $ex->getMessage()]; |
115
|
|
|
$result = self::encryptByPublicKey(json_encode($result)); |
116
|
|
|
$this->submit($result, $url); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public static function decryptByFaveoPrivateKeyold($encrypted) |
121
|
|
|
{ |
122
|
|
|
try { |
123
|
|
|
// Get the private Key |
124
|
|
|
$path = storage_path('app'.DIRECTORY_SEPARATOR.'private.key'); |
125
|
|
|
$key_content = file_get_contents($path); |
126
|
|
|
if (! $privateKey = openssl_pkey_get_private($key_content)) { |
127
|
|
|
dd('Private Key failed'); |
128
|
|
|
} |
129
|
|
|
$a_key = openssl_pkey_get_details($privateKey); |
130
|
|
|
|
131
|
|
|
// Decrypt the data in the small chunks |
132
|
|
|
$chunkSize = ceil($a_key['bits'] / 8); |
133
|
|
|
$output = ''; |
134
|
|
|
|
135
|
|
|
while ("¥IM‰``ì‡Á›LVP›†>¯öóŽÌ3(¢z#¿î1¾:±Zï©Pqʴ›7×:F௦ à•…Ä'öESW±ÉŸLÃvÈñÔs•ÍU)ÍL 8¬š‰A©·Å $}Œ•lA9™¡”¸èÅØv‘ÂOÈ6„_y5¤ì§—ÿíà(ow‰È&’v&T/FLƒigjÒZ eæaa”{©ªUBFÓ’Ga*ÀŒ×?£}-jÏùh¾Q/Ž“1YFq[͉¬òÚ‚œ½Éº5ah¶vZ#,ó@‚rOƱíVåèÜÖšU¦ÚmSΓMý„ùP") { |
136
|
|
|
$chunk = substr($encrypted, 0, $chunkSize); |
137
|
|
|
$encrypted = substr($encrypted, $chunkSize); |
138
|
|
|
$decrypted = ''; |
139
|
|
|
if (! openssl_private_decrypt($chunk, $decrypted, $privateKey)) { |
140
|
|
|
dd('Failed to decrypt data'); |
141
|
|
|
} |
142
|
|
|
$output .= $decrypted; |
143
|
|
|
} |
144
|
|
|
openssl_free_key($privateKey); |
145
|
|
|
|
146
|
|
|
// Uncompress the unencrypted data. |
147
|
|
|
$output = gzuncompress($output); |
148
|
|
|
dd($output); |
149
|
|
|
echo '<br /><br /> Unencrypted Data: '.$output; |
150
|
|
|
} catch (Exception $ex) { |
151
|
|
|
dd($ex); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
public function createEncryptionKeys() |
156
|
|
|
{ |
157
|
|
|
try { |
158
|
|
|
$privateKey = openssl_pkey_new([ |
159
|
|
|
'private_key_bits' => 2048, // Size of Key. |
160
|
|
|
'private_key_type' => OPENSSL_KEYTYPE_RSA, |
161
|
|
|
]); |
162
|
|
|
//dd($privateKey); |
163
|
|
|
// Save the private key to private.key file. Never share this file with anyone. |
164
|
|
|
openssl_pkey_export_to_file($privateKey, 'private.key'); |
165
|
|
|
|
166
|
|
|
// Generate the public key for the private key |
167
|
|
|
$a_key = openssl_pkey_get_details($privateKey); |
168
|
|
|
//dd($a_key); |
169
|
|
|
// Save the public key in public.key file. Send this file to anyone who want to send you the encrypted data. |
170
|
|
|
file_put_contents('public.key', $a_key['key']); |
171
|
|
|
|
172
|
|
|
// Free the private Key. |
173
|
|
|
openssl_free_key($privateKey); |
174
|
|
|
} catch (\Exception $ex) { |
175
|
|
|
dd($ex); |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
public function checkOrder($faveo_decrypted_order) |
180
|
|
|
{ |
181
|
|
|
try { |
182
|
|
|
$order = new Order(); |
183
|
|
|
// $faveo_decrypted_order = self::decryptByFaveoPrivateKey($faveo_encrypted_order_number); |
184
|
|
|
|
185
|
|
|
$this_order = $order->where('number', 'LIKE', $faveo_decrypted_order)->first(); |
186
|
|
|
if (! $this_order) { |
187
|
|
|
return; |
188
|
|
|
} else { |
189
|
|
|
return $this_order->number; |
190
|
|
|
} |
191
|
|
|
} catch (Exception $ex) { |
192
|
|
|
throw new Exception($ex->getMessage()); |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
public function faveoVerification(Request $request) |
197
|
|
|
{ |
198
|
|
|
try { |
199
|
|
|
$data = $request->input('data'); |
200
|
|
|
$json = self::decryptByFaveoPrivateKey($data); |
201
|
|
|
$data = json_decode($json); |
202
|
|
|
//return $data->url; |
203
|
|
|
|
204
|
|
|
$domain = $data->url; |
205
|
|
|
|
206
|
|
|
$faveo_encrypted_order_number = $data->order_number; |
207
|
|
|
|
208
|
|
|
//$domain = $data->domain; |
209
|
|
|
|
210
|
|
|
$faveo_encrypted_key = $data->serial_key; |
211
|
|
|
|
212
|
|
|
$request_type = $data->request_type; |
213
|
|
|
|
214
|
|
|
$faveo_name = $data->name; |
215
|
|
|
|
216
|
|
|
$faveo_version = $data->version; |
217
|
|
|
|
218
|
|
|
$order_number = $this->checkOrder($faveo_encrypted_order_number); |
219
|
|
|
|
220
|
|
|
$domain = $this->checkDomain($domain); |
221
|
|
|
|
222
|
|
|
$serial_key = $this->checkSerialKey($faveo_encrypted_key, $order_number); |
223
|
|
|
//dd($serial_key); |
224
|
|
|
//return $serial_key; |
225
|
|
|
$result = []; |
226
|
|
|
if ($request_type == 'install') { |
227
|
|
|
$result = $this->verificationResult($order_number, $serial_key, $domain); |
228
|
|
|
} |
229
|
|
|
if ($request_type == 'check_update') { |
230
|
|
|
$result = $this->checkUpdate($order_number, $serial_key, $domain, $faveo_name, $faveo_version); |
231
|
|
|
} |
232
|
|
|
$result = self::encryptByPublicKey(json_encode($result)); |
233
|
|
|
|
234
|
|
|
return $result; |
235
|
|
|
} catch (Exception $ex) { |
236
|
|
|
$result = ['status' => 'error', 'message' => $ex->getMessage().' |
237
|
|
|
file=> '.$ex->getFile().' Line=>'.$ex->getLine()]; |
238
|
|
|
$result = self::encryptByPublicKey(json_encode($result)); |
239
|
|
|
|
240
|
|
|
return $result; |
241
|
|
|
} |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
public function submit($result, $url) |
245
|
|
|
{ |
246
|
|
|
echo "<form action=$url method=post name=redirect>"; |
247
|
|
|
echo '<input type=hidden name=_token value=csrf_token()/>'; |
248
|
|
|
echo '<input type=hidden name=result value='.$result.'/>'; |
249
|
|
|
echo '</form>'; |
250
|
|
|
echo"<script language='javascript'>document.redirect.submit();</script>"; |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
public function checkUpdate($order_number, $serial_key, $domain, $faveo_name, $faveo_version) |
254
|
|
|
{ |
255
|
|
|
try { |
256
|
|
|
if ($order_number && $domain && $serial_key) { |
257
|
|
|
$order = $this->verifyOrder($order_number, $serial_key, $domain); |
258
|
|
|
//var_dump($order); |
259
|
|
|
if ($order) { |
260
|
|
|
return $this->checkFaveoDetails($order_number, $faveo_name, $faveo_version); |
261
|
|
|
} else { |
262
|
|
|
return ['status' => 'fails', 'message' => 'this-is-an-invalid-request']; |
263
|
|
|
} |
264
|
|
|
} else { |
265
|
|
|
return ['status' => 'fails', 'message' => 'this-is-an-invalid-request']; |
266
|
|
|
} |
267
|
|
|
} catch (Exception $ex) { |
268
|
|
|
throw new Exception($ex->getMessage()); |
269
|
|
|
} |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
public function checkFaveoDetails($order_number, $faveo_name, $faveo_version) |
273
|
|
|
{ |
274
|
|
|
try { |
275
|
|
|
$order = new Order(); |
276
|
|
|
$product = new Product(); |
277
|
|
|
$this_order = $order->where('number', $order_number)->first(); |
278
|
|
|
if ($this_order) { |
279
|
|
|
$product_id = $this_order->product; |
280
|
|
|
$this_product = $product->where('id', $product_id)->first(); |
281
|
|
|
if ($this_product) { |
282
|
|
|
$version = str_replace('v', '', $this_product->version); |
283
|
|
|
|
284
|
|
|
return ['status' => 'success', 'message' => 'this-is-a-valid-request', 'version' => $version]; |
285
|
|
|
} |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
return ['status' => 'fails', 'message' => 'this-is-an-invalid-request']; |
289
|
|
|
} catch (Exception $ex) { |
290
|
|
|
throw new Exception($ex->getMessage()); |
291
|
|
|
} |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
public static function encryptByPublicKey($data) |
295
|
|
|
{ |
296
|
|
|
$path = storage_path().DIRECTORY_SEPARATOR.'app'.DIRECTORY_SEPARATOR.'public.key'; |
297
|
|
|
//dd($path); |
298
|
|
|
$key_content = file_get_contents($path); |
299
|
|
|
$public_key = openssl_get_publickey($key_content); |
300
|
|
|
|
301
|
|
|
$encrypted = $e = null; |
302
|
|
|
openssl_seal($data, $encrypted, $e, [$public_key]); |
303
|
|
|
|
304
|
|
|
$sealed_data = base64_encode($encrypted); |
305
|
|
|
$envelope = base64_encode($e[0]); |
306
|
|
|
|
307
|
|
|
$result = ['seal' => $sealed_data, 'envelope' => $envelope]; |
308
|
|
|
|
309
|
|
|
return json_encode($result); |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
public function downloadForFaveo(Request $request, Order $order) |
313
|
|
|
{ |
314
|
|
|
try { |
315
|
|
|
$faveo_encrypted_order_number = $request->input('order_number'); |
316
|
|
|
$faveo_serial_key = $request->input('serial_key'); |
317
|
|
|
$orderSerialKey = $order->where('number', $faveo_encrypted_order_number) |
318
|
|
|
->value('serial_key'); |
319
|
|
|
|
320
|
|
|
$this_order = $order |
321
|
|
|
->where('number', $faveo_encrypted_order_number) |
322
|
|
|
->first(); |
323
|
|
|
if ($this_order && $orderSerialKey == $faveo_serial_key) { |
324
|
|
|
$product_id = $this_order->product; |
325
|
|
|
$product_controller = new \App\Http\Controllers\Product\ProductController(); |
326
|
|
|
|
327
|
|
|
return $product_controller->adminDownload($product_id, '', true); |
328
|
|
|
} else { |
329
|
|
|
return response()->json(['Invalid Credentials']); |
330
|
|
|
} |
331
|
|
|
} catch (\Exception $e) { |
332
|
|
|
return response()->json(['error' => $e->getMessage(), 'line' => $e->getFile()], 500); |
333
|
|
|
} |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
public function latestVersion(Request $request, Product $product) |
337
|
|
|
{ |
338
|
|
|
$v = \Validator::make($request->all(), [ |
339
|
|
|
'title' => 'required', |
340
|
|
|
]); |
341
|
|
|
if ($v->fails()) { |
342
|
|
|
$error = $v->errors(); |
343
|
|
|
|
344
|
|
|
return response()->json(compact('error')); |
345
|
|
|
} |
346
|
|
|
|
347
|
|
|
try { |
348
|
|
|
$title = $request->input('title'); |
349
|
|
|
$product = $product->where('name', $title)->first(); |
350
|
|
|
if ($product) { |
351
|
|
|
$message = ['version' => str_replace('v', '', $product->version)]; |
352
|
|
|
} else { |
353
|
|
|
$message = ['error' => 'product_not_found']; |
354
|
|
|
} |
355
|
|
|
$message = ['version' => str_replace('v', '', $product->version)]; |
356
|
|
|
} catch (\Exception $e) { |
357
|
|
|
$message = ['error' => $e->getMessage()]; |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
return response()->json($message); |
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
/* |
364
|
|
|
* Check if the Product is valid For Auto Updates |
365
|
|
|
* @params string Serial Key in encrypted |
366
|
|
|
* @return array |
367
|
|
|
*/ |
368
|
|
|
} |
369
|
|
|
|