Total Complexity | 45 |
Total Lines | 352 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like HomeController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use HomeController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
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() |
||
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 | { |
||
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 | { |
||
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 | { |
||
334 | } |
||
335 | |||
336 | public function latestVersion(Request $request, Product $product) |
||
337 | { |
||
362 | |||
363 | /* |
||
364 | * Check if the Product is valid For Auto Updates |
||
365 | * @params string Serial Key in encrypted |
||
366 | * @return array |
||
369 |