Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\Http\Controllers;
- use App\User;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Artisan;
- use Illuminate\Support\Facades\Storage;
- class InstallController extends Controller
- {
- /**
- * Create a new controller instance.
- *
- * @return void
- */
- public function __construct()
- {
- }
- protected function changeEnv($data = array()){
- if(count($data) > 0){
- // Read .env-file
- $env = file_get_contents(base_path() . '/.env');
- // Split string on every " " and write into array
- $env = preg_split('/\s+/', $env);;
- // Loop through given data
- foreach((array)$data as $key => $value){
- // Loop through .env-data
- foreach($env as $env_key => $env_value){
- // Turn the value into an array and stop after the first split
- // So it's not possible to split e.g. the App-Key by accident
- $entry = explode("=", $env_value, 2);
- // Check, if new key fits the actual .env-key
- if($entry[0] == $key){
- // If yes, overwrite it with the new one
- $env[$env_key] = $key . "=" . $value;
- } else {
- // If not, keep the old one
- $env[$env_key] = $env_value;
- }
- }
- }
- // Turn the array back to an String
- $env = implode("\n", $env);
- // And overwrite the .env with the new data
- file_put_contents(base_path() . '/.env', $env);
- return true;
- } else {
- return false;
- }
- }
- /**
- * Show the application dashboard.
- *
- * @return \Illuminate\Http\Response
- */
- public function index()
- {
- return view('install.index');
- }
- public function Step1(Request $request){
- $servername = $request->servername;
- $username = $request->username;
- $password = $request->pass;
- $dbname = $request->dbname;
- $adminUsername = $request->adminUsername;
- $adminPass = $request->adminPass;
- try {
- $conn = new \PDO("mysql:host=$servername", $username, $password);
- $conn->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
- $sql = "CREATE DATABASE IF NOT EXISTS $dbname";
- $conn->exec($sql);
- copy(base_path() . '/.env.example', base_path()."/.env");
- $env_update = $this->changeEnv([
- 'DB_DATABASE' => $dbname,
- 'DB_USERNAME' => $username,
- 'DB_PASSWORD' => $password,
- 'DB_HOST' => $servername
- ]);
- if($env_update){
- Artisan::call("migrate");
- Artisan::call("db:seed");
- User::create([
- 'name' => "admin",
- 'email' => $adminUsername,
- 'password' => bcrypt($adminPass),
- ]);
- Storage::put('installed', "");
- return redirect()->route("indexInstall")->with('success', "Installation terminée");
- } else {
- return redirect()->route("indexInstall")->with('error', "Une erreur s'est produite");
- }
- // more code
- }
- catch(\PDOException $e)
- {
- return redirect()->route("indexInstall")->with('error', $e->getMessage());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement