Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.63 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Http\Controllers;
  4.  
  5. use App\User;
  6. use Illuminate\Http\Request;
  7. use Illuminate\Support\Facades\Artisan;
  8. use Illuminate\Support\Facades\Storage;
  9.  
  10. class InstallController extends Controller
  11. {
  12.     /**
  13.      * Create a new controller instance.
  14.      *
  15.      * @return void
  16.      */
  17.     public function __construct()
  18.     {
  19.  
  20.     }
  21.  
  22.     protected function changeEnv($data = array()){
  23.         if(count($data) > 0){
  24.  
  25.             // Read .env-file
  26.             $env = file_get_contents(base_path() . '/.env');
  27.  
  28.             // Split string on every " " and write into array
  29.             $env = preg_split('/\s+/', $env);;
  30.  
  31.             // Loop through given data
  32.             foreach((array)$data as $key => $value){
  33.  
  34.                 // Loop through .env-data
  35.                 foreach($env as $env_key => $env_value){
  36.  
  37.                     // Turn the value into an array and stop after the first split
  38.                     // So it's not possible to split e.g. the App-Key by accident
  39.                     $entry = explode("=", $env_value, 2);
  40.  
  41.                     // Check, if new key fits the actual .env-key
  42.                     if($entry[0] == $key){
  43.                         // If yes, overwrite it with the new one
  44.                         $env[$env_key] = $key . "=" . $value;
  45.                     } else {
  46.                         // If not, keep the old one
  47.                         $env[$env_key] = $env_value;
  48.                     }
  49.                 }
  50.             }
  51.  
  52.             // Turn the array back to an String
  53.             $env = implode("\n", $env);
  54.  
  55.             // And overwrite the .env with the new data
  56.             file_put_contents(base_path() . '/.env', $env);
  57.  
  58.             return true;
  59.         } else {
  60.             return false;
  61.         }
  62.     }
  63.  
  64.     /**
  65.      * Show the application dashboard.
  66.      *
  67.      * @return \Illuminate\Http\Response
  68.      */
  69.     public function index()
  70.     {
  71.         return view('install.index');
  72.     }
  73.  
  74.     public function Step1(Request $request){
  75.  
  76.  
  77.             $servername = $request->servername;
  78.             $username = $request->username;
  79.             $password = $request->pass;
  80.             $dbname = $request->dbname;
  81.             $adminUsername = $request->adminUsername;
  82.             $adminPass = $request->adminPass;
  83.         try {
  84.             $conn = new \PDO("mysql:host=$servername", $username, $password);
  85.             $conn->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
  86.             $sql = "CREATE DATABASE IF NOT EXISTS $dbname";
  87.             $conn->exec($sql);
  88.  
  89.             copy(base_path() . '/.env.example', base_path()."/.env");
  90.             $env_update = $this->changeEnv([
  91.                 'DB_DATABASE'   => $dbname,
  92.                 'DB_USERNAME'   => $username,
  93.                 'DB_PASSWORD'   => $password,
  94.                 'DB_HOST'       => $servername
  95.             ]);
  96.             if($env_update){
  97.              Artisan::call("migrate");
  98.              Artisan::call("db:seed");
  99.              User::create([
  100.                  'name' => "admin",
  101.                  'email' => $adminUsername,
  102.                  'password' => bcrypt($adminPass),
  103.              ]);
  104.                 Storage::put('installed', "");
  105.                 return redirect()->route("indexInstall")->with('success', "Installation terminée");
  106.             } else {
  107.                 return redirect()->route("indexInstall")->with('error', "Une erreur s'est produite");
  108.             }
  109.             // more code
  110.  
  111.         }
  112.         catch(\PDOException $e)
  113.         {
  114.             return redirect()->route("indexInstall")->with('error',   $e->getMessage());
  115.  
  116.         }
  117.  
  118.  
  119.     }
  120.    
  121.  
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement