OTPController.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use App\Models\OtpConfiguration;
  5. class OTPController extends Controller
  6. {
  7. /**
  8. * Display a listing of the resource.
  9. *
  10. * @return \Illuminate\Http\Response
  11. */
  12. public function configure_index()
  13. {
  14. return view('otp_systems.configurations.activation');
  15. }
  16. /**
  17. * Display a listing of the resource.
  18. *
  19. * @return \Illuminate\Http\Response
  20. */
  21. public function credentials_index()
  22. {
  23. return view('otp_systems.configurations.index');
  24. }
  25. /**
  26. * Update the specified resource in storage.
  27. *
  28. * @param \Illuminate\Http\Request $request
  29. * @return \Illuminate\Http\Response
  30. */
  31. public function updateActivationSettings(Request $request)
  32. {
  33. $business_settings = OtpConfiguration::where('type', $request->type)->first();
  34. if($business_settings!=null){
  35. $business_settings->value = $request->value;
  36. $business_settings->save();
  37. }
  38. else{
  39. $business_settings = new OtpConfiguration;
  40. $business_settings->type = $request->type;
  41. $business_settings->value = $request->value;
  42. $business_settings->save();
  43. }
  44. return '1';
  45. }
  46. /**
  47. * Update the specified resource in .env
  48. *
  49. * @param \Illuminate\Http\Request $request
  50. * @return \Illuminate\Http\Response
  51. */
  52. public function update_credentials(Request $request)
  53. {
  54. foreach ($request->types as $key => $type) {
  55. $this->overWriteEnvFile($type, $request[$type]);
  56. }
  57. flash("Settings updated successfully")->success();
  58. return back();
  59. }
  60. /**
  61. *.env file overwrite
  62. */
  63. public function overWriteEnvFile($type, $val)
  64. {
  65. $path = base_path('.env');
  66. if (file_exists($path)) {
  67. $val = '"'.trim($val).'"';
  68. if(is_numeric(strpos(file_get_contents($path), $type)) && strpos(file_get_contents($path), $type) >= 0){
  69. file_put_contents($path, str_replace(
  70. $type.'="'.env($type).'"', $type.'='.$val, file_get_contents($path)
  71. ));
  72. }
  73. else{
  74. file_put_contents($path, file_get_contents($path)."\r\n".$type.'='.$val);
  75. }
  76. }
  77. }
  78. }