InstallController.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use URL;
  5. use DB;
  6. use Hash;
  7. use App\Models\BusinessSetting;
  8. use App\Models\User;
  9. use App\Models\Product;
  10. use CoreComponentRepository;
  11. class InstallController extends Controller
  12. {
  13. public function step0() {
  14. $this->writeEnvironmentFile('APP_URL', URL::to('/'));
  15. return view('installation.step0');
  16. }
  17. public function step1() {
  18. $permission['curl_enabled'] = function_exists('curl_version');
  19. $permission['db_file_write_perm'] = is_writable(base_path('.env'));
  20. $permission['routes_file_write_perm'] = is_writable(base_path('app/Providers/RouteServiceProvider.php'));
  21. return view('installation.step1', compact('permission'));
  22. }
  23. public function step2() {
  24. return view('installation.step2');
  25. }
  26. public function step3($error = "") {
  27. CoreComponentRepository::instantiateShopRepository();
  28. if($error == ""){
  29. return view('installation.step3');
  30. }else {
  31. return view('installation.step3', compact('error'));
  32. }
  33. }
  34. public function step4() {
  35. return view('installation.step4');
  36. }
  37. public function step5() {
  38. return view('installation.step5');
  39. }
  40. public function purchase_code(Request $request) {
  41. return redirect('step3');
  42. }
  43. public function system_settings(Request $request) {
  44. $businessSetting = BusinessSetting::where('type', 'system_default_currency')->first();
  45. $businessSetting->value = $request->system_default_currency;
  46. $businessSetting->save();
  47. $businessSetting = BusinessSetting::where('type', 'home_default_currency')->first();
  48. $businessSetting->value = $request->system_default_currency;
  49. $businessSetting->save();
  50. $this->writeEnvironmentFile('APP_NAME', $request->system_name);
  51. $user = new User;
  52. $user->name = $request->admin_name;
  53. $user->email = $request->admin_email;
  54. $user->password = Hash::make($request->admin_password);
  55. $user->user_type = 'admin';
  56. $user->email_verified_at = date('Y-m-d H:m:s');
  57. $user->save();
  58. $previousRouteServiceProvier = base_path('app/Providers/RouteServiceProvider.php');
  59. $newRouteServiceProvier = base_path('app/Providers/RouteServiceProvider.txt');
  60. copy($newRouteServiceProvier, $previousRouteServiceProvier);
  61. //sleep(5);
  62. return view('installation.step6');
  63. // return redirect('step6');
  64. }
  65. public function database_installation(Request $request) {
  66. if(self::check_database_connection($request->DB_HOST, $request->DB_DATABASE, $request->DB_USERNAME, $request->DB_PASSWORD)) {
  67. $path = base_path('.env');
  68. if (file_exists($path)) {
  69. foreach ($request->types as $type) {
  70. $this->writeEnvironmentFile($type, $request[$type]);
  71. }
  72. return redirect('step4');
  73. }else {
  74. return redirect('step3');
  75. }
  76. }else {
  77. return redirect('step3/database_error');
  78. }
  79. }
  80. public function import_sql() {
  81. $sql_path = base_path('shop.sql');
  82. DB::unprepared(file_get_contents($sql_path));
  83. return redirect('step5');
  84. }
  85. function check_database_connection($db_host = "", $db_name = "", $db_user = "", $db_pass = "") {
  86. if(@mysqli_connect($db_host, $db_user, $db_pass, $db_name)) {
  87. return true;
  88. }else {
  89. return false;
  90. }
  91. }
  92. public function writeEnvironmentFile($type, $val) {
  93. $path = base_path('.env');
  94. if (file_exists($path)) {
  95. $val = '"'.trim($val).'"';
  96. file_put_contents($path, str_replace(
  97. $type.'="'.env($type).'"', $type.'='.$val, file_get_contents($path)
  98. ));
  99. }
  100. }
  101. }