File: /home/posscale/.trash/1app.3/Http/Controllers/RegisterTenant.php
<?php
namespace App\Http\Controllers;
use App\Http\Requests\TenantRequest;
use App\Mail\RegistraionSuccessAdminEmail;
use App\Mail\RegistraionSuccessEmail;
use App\Models\Tenant;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Str;
class RegisterTenant extends Controller
{
/**
* Display a listing of the resource.
*/
public function index()
{
return view('printer.register');
}
/**
* Show the form for creating a new resource.
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*/
public function store(TenantRequest $request)
{
$site_name = Str::slug($request->input('site_name'), "-");
$name = $request->input('name');
$user_email = $request->input('user_email');
$password = $request->input('password');
$db_name = 'posscale_' . $site_name;
if ($request->has('has_domain') && $request->input('has_domain') == 1) {
$site_domain = $request->input('site_domain');
} else {
$site_domain = $request->input('sub_domain') . '.' . env('PARENT_DOMAIN');
$sub_domain = $request->input('sub_domain');
$db_name = 'posscale_' . $sub_domain;
if (env('CPANEL_URL')) {
// Create database and add user to the database.
$url = env('CPANEL_URL') . '/execute/Mysql/check_database?name=' . $db_name;
$curl = curl_init();
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$header[0] = 'Authorization: cpanel ' . env('CPANEL_USER') . ':' . env('CPANEL_TOKEN');
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($curl, CURLOPT_URL, $url);
$result = curl_exec($curl);
$http_status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ($http_status == 200) {
$result_array = json_decode($result, true);
if ($result_array['status'] == 1) {
return Redirect::route('printer.index')->with('ct_errors', 'Database already exists with this domain name.')->withInput();
}
} else {
return Redirect::route('printer.index')->with('error', 'Unable to create domain or database.')->withInput();
}
// Create database and add user to the database.
$url = env('CPANEL_URL') . '/execute/SubDomain/addsubdomain?domain=' . $sub_domain . '&rootdomain=' . env('PARENT_DOMAIN') . '&dir=' . env('DOMAIN_ROOT');
$curl = curl_init();
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$header[0] = 'Authorization: cpanel ' . env('CPANEL_USER') . ':' . env('CPANEL_TOKEN');
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($curl, CURLOPT_URL, $url);
$result = curl_exec($curl);
$http_status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ($http_status != 200) {
return Redirect::route('printer.index')->with('error', 'Unable to create domain.')->withInput();
} else {
$result_array = json_decode($result, true);
if ($result_array['status'] != 1) {
return Redirect::route('printer.index')->with('ct_errors', join('__', $result_array['errors']))->withInput();
}
}
curl_close($curl);
}
}
$data_to_check = [
'site_name' => $site_name,
'site_domain' => $site_domain
];
// Set up the form validation
$validator = Validator::make(
$data_to_check,
array(
'site_name' => 'unique:tenants,id',
'site_domain' => 'unique:domains,domain'
)
);
// If validation fails, redirect to the settings page and send the errors
if ($validator->fails()) {
return Redirect::route('printer.index')->withErrors($validator)->withInput();
} else {
if (env('CPANEL_URL')) {
// Create database and add user to the database.
$url = env('CPANEL_URL') . '/execute/Mysql/create_database?name=' . $db_name;
$curl = curl_init();
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$header[0] = 'Authorization: cpanel ' . env('CPANEL_USER') . ':' . env('CPANEL_TOKEN');
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($curl, CURLOPT_URL, $url);
$result = curl_exec($curl);
$http_status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ($http_status != 200) {
return Redirect::route('printer.index')->with('error', 'Get error while creating database.')->withInput();
} else {
$result_array = json_decode($result, true);
if ($result_array['status'] != 1) {
return Redirect::route('printer.index')->with('ct_errors', join('__', $result_array['errors']))->withInput();
}
}
curl_close($curl);
// Add user to database
$url = env('CPANEL_URL') . '/execute/Mysql/set_privileges_on_database?user=' . env('CPANEL_DB_USERNAME') . '&database=' . $db_name . '&privileges=' . urlencode('ALL PRIVILEGES');
$curl = curl_init();
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$header[0] = 'Authorization: cpanel ' . env('CPANEL_USER') . ':' . env('CPANEL_TOKEN');
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($curl, CURLOPT_URL, $url);
$result = curl_exec($curl);
$http_status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ($http_status != 200) {
return Redirect::route('printer.index')->with('error', 'Get error while adding user.')->withInput();
} else {
$result_array = json_decode($result, true);
if ($result_array['status'] != 1) {
return Redirect::route('printer.index')->with('ct_errors', join('__', $result_array['errors']))->withInput();
}
}
curl_close($curl);
}
$tenant = new Tenant();
$tenant->id = $site_name;
$tenant->reseller_name = $name;
$tenant->tenancy_db_name = $db_name;
$tenant->data = [
'tenancy_db_name' => $db_name,
'tenancy_create_database' => false,
];
$tenant->save();
// $tenant = Tenant::create([
// 'id' => $site_name,
// 'reseller_name' => $name,
// 'tenancy_db_name' => $db_name,
// 'tenancy_create_database' => true
// ], []);
$tenant->domains()->create([
'domain' => $site_domain
]);
tenancy()->initialize($tenant);
// Create user associated with the tenant
$user = new User([
'name' => $name,
'email' => $user_email,
'password' => Hash::make($password),
]);
$user->save();
tenancy()->end();
Mail::to(env('ADMIN_EMAIL', 'developer@posscales.com.au'))->send(new RegistraionSuccessAdminEmail([
'name' => $name,
'site_name' => $site_name,
'site_domain' => $site_domain,
'user_email' => $user_email,
]));
Mail::to($user_email)->send(new RegistraionSuccessEmail([
'name' => $name,
]));
return Redirect::route('printer.index')->with('message', 'You application was submited. We will contact you within 24 hours.');
}
}
/**
* Display the specified resource.
*/
public function show(string $id)
{
//
}
/**
* Show the form for editing the specified resource.
*/
public function edit(string $id)
{
//
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, string $id)
{
//
}
/**
* Remove the specified resource from storage.
*/
public function destroy(string $id)
{
//
}
}