| Server IP : 138.197.107.151 / Your IP : 216.73.217.7 Web Server : Apache/2.4.58 (Ubuntu) System : Linux BloxBy-Builder 6.8.0-71-generic #71-Ubuntu SMP PREEMPT_DYNAMIC Tue Jul 22 16:52:38 UTC 2025 x86_64 User : wpbetasites_mrakzqskir ( 1022) PHP Version : 8.3.6 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : OFF Directory : /var/www/silversharkdev/app/Http/Controllers/ |
Upload File : |
<?php
// app/Http/Controllers/WPController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Symfony\Component\Process\Process;
use Symfony\Component\Process\Exception\ProcessFailedException;
use App\Models\Website;
use App\Models\UserSetting;
use App\Models\Prompt;
use App\Models\User;
use App\Models\WebsitesArchiveRequest;
use Stripe\Stripe;
use App\Helpers\EncryptionHelper;
use Auth;
use Mail;
use DB;
class WebsiteController extends Controller
{
public function pageDivision(Request $request, $website_id)
{
$website = Website::where("id", $website_id)->first();
$websiteMeta = json_decode($website->meta, true);
// dd($websiteMeta);
foreach($websiteMeta['pages'] as $key => $page){
if($key <= 1){
$pagedefalt[] = $page;
}else{
$pageother[] = $page;
}
}
$websitedefalt = $websiteother = $websiteMeta;
$websitedefalt['pages'] = $pagedefalt;
$websiteother['pages'] = $pageother;
$websitedefaltjson = base64_encode(serialize($websitedefalt));
$websiteotherjson = base64_encode(serialize($websiteother));
dd($websitedefaltjson, $websiteotherjson);
}
public function index(Request $request)
{
$websites = Website::where("user_id", auth()->user()->id)->count();
$website_limit = 3;
$setting = UserSetting::where("user_id", auth()->user()->id)->first();
if($setting){
$website_limit = $setting->websites_limit;
}
$user = auth()->user();
if($user->email_verified_at==null && time() >= strtotime($user->trial_ending_at)){
session()->forget('website_json');
return redirect()->route("dashboard")->with(["error"=>"You will not be able to create new websites, unless you verify your email."]);
}
if($websites >= $website_limit){
session()->forget('website_json');
return redirect()->route("dashboard")->with(["error"=>"Your maximum limit of creating websites has been reached."]);
} else {
$this->saveWebsite();
}
return redirect()->route("dashboard");
//$data = session('website_json');
/*$website = Website::where("user_id", auth()->user()->id )->where("is_temp",1)->first();
if(!$data && !$website){
return redirect('/dashboard');
}
//print_r($data);
return view('generate-site');*/
}
public function saveWebsite(){
$data = session('website_json');
if($data){
$website = Website::create([
"user_id" => auth()->user()->id,
"title" => $data["website_title"],
"path" => "/bloxby_wp",
"status" => "pending",
]);
$website->meta = json_encode($data);
$website->save();
if($data["prompt_id"]){
$website->prompt_id = $data["prompt_id"];
if(Auth::check()){
if(auth()->user()->email_verified_at == null){
$website->trial_mode = 1;
}
}
$website->save();
$prompt = Prompt::where("id", $data["prompt_id"])->first();
if($prompt){
if(!$prompt->user_id){
$prompt->user_id = auth()->user()->id;
$prompt->meta = json_encode($data);
$prompt->prompt = json_encode($data["user_prompt"]);
$prompt->save();
} else {
$prompt->meta = json_encode($data);
$prompt->prompt = json_encode($data["user_prompt"]);
$prompt->save();
}
$prompt->website_id = $website->id;
$prompt->save();
}
}
session()->forget('website_json');
}
}
function generateSecureUniqueId($length = 10) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyz';
$charactersLength = strlen($characters);
$uniqueId = '';
for ($i = 0; $i < $length; $i++) {
$uniqueId .= $characters[random_int(0, $charactersLength - 1)];
}
return $uniqueId;
}
function generateStrongPassword($length = 12, $username = '') {
$lower = 'abcdefghijklmnopqrstuvwxyz';
$upper = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$numbers = '0123456789';
$special = '!@#$%^&*()-_=+[]{}|;:,.<>?';
$all = $lower . $upper . $numbers . $special;
do {
$password = '';
// Ensure each required character type is included
$password .= $lower[random_int(0, strlen($lower) - 1)];
$password .= $upper[random_int(0, strlen($upper) - 1)];
$password .= $numbers[random_int(0, strlen($numbers) - 1)];
$password .= $special[random_int(0, strlen($special) - 1)];
for ($i = 4; $i < $length; $i++) {
$password .= $all[random_int(0, strlen($all) - 1)];
}
// Shuffle to avoid predictable order
$password = str_shuffle($password);
} while (
stripos($password, $username) !== false // must NOT contain username
);
return $password;
}
public function run(Request $request)
{
// Run the WP-CLI command
$website = Website::where("user_id", auth()->user()->id )->where("is_temp",1)->first();
if(!$website){
$data = session('website_json');
$website = Website::create([
"user_id" => auth()->user()->id,
"title" => $data["website_title"],
"path" => "/bloxby_wp",
"is_temp" => 1
]);
$json = base64_encode(serialize($data));
$test_mode = 1;
//site_bloxy_wp_".$website->id." site_bloxy_db_".$website->id." '".$json."' ".$website->id. " ".$test_mode
$server_meta = [];
$server_meta["folder"] = env('WEBSITES_DIR');
$site_chars = $this->generateSecureUniqueId(10);
$server_meta["site"] = $site_chars;
$server_meta["domain"] = $site_chars.env("DOMAIN_PREFIX");
$server_meta["slug_environment"] = env('SLUG_ENV');
$server_meta["sftp_user"] = $server_meta["slug_environment"]."_".$site_chars ;
$server_meta["sftp_pass"] = $this->generateSecureUniqueId(9);
$server_meta["db_pass"] = $this->generateStrongPassword(12,"db_user_".$server_meta["slug_environment"]."_".$server_meta["site"]);
$server_meta["db_name"] = "db_".$server_meta["slug_environment"]."_".$site_chars ;
$server_meta["db_user"] = "db_user_".$server_meta["slug_environment"]."_".$site_chars ;
$server_meta["slug_environment"] = env('SLUG_ENV');
$server_meta["sftp_group"] = "sftpusers";
//response()->json(['status' => 'success', 'output' => env("SITEGENPATH")."--folder ".$server_meta["folder"]." --site ".$server_meta["site"]." --sftp-pass \"".$server_meta["sftp_pass"]."\" --db-pass \"".$server_meta["db_pass"]."\" --wpsite ".$server_meta["slug_environment"]." --sftp-group ".$server_meta["sftp_group"]." --site-id $website->id --json-data $json"]); ;
$process = new Process([
'sudo',
'/var/bloxbycommands/create_site.sh',
'--folder', $server_meta["folder"],
'--site', $server_meta["site"],
'--sftp-pass', $server_meta["sftp_pass"],
'--db-pass', $server_meta["db_pass"],
'--wpsite', $server_meta["slug_environment"],
'--sftp-group', $server_meta["sftp_group"],
'--site-id', $website->id,
'--json-data', $json,
'--domain', $server_meta["domain"]
]);
//$process->run();
//$process = Process::fromShellCommandline(env("SITEGENPATH")." site_bloxy_wp_".$website->id." site_bloxy_db_".$website->id." '".$json."' ".$website->id. " ".$test_mode); // change to your command
$process->setTimeout(900); // Increase for long tasks
$process->run();
if (!$process->isSuccessful()) {
session()->forget('website_json');
$website->is_temp = 2;
$website->save();
return response()->json(['status' => 'error', 'message' => $process->getErrorOutput()], 500);
} else {
session()->forget('website_json');
$website->path = $server_meta["slug_environment"]."_".$server_meta["site"];
$website->dbname = $server_meta["db_user"];
$website->server_meta = json_encode($server_meta);
$website->meta = json_encode($data);
$website->is_temp = 0;
$website->save();
}
return response()->json(['status' => 'success', 'output' => $process->getOutput()]);
}
return response()->json(['status' => 'success', 'output' => ""]);
}
public function remove(Request $request, $id){
$website = Website::where("id",$id);
if(Auth::check()){
$website = $website->where("user_id", auth()->user()->id);
}
$website = $website->first();
if($website){
if($website->domain!=null){
return redirect()->back()->withError( "Please remove the domain attached before removing the website.");
}
if($website->plan){
return redirect()->back()->withError( "You are not authorized to remove the website");
}
$server_meta = json_decode($website->server_meta, true);
// dd($server_meta);
$process = new Process([
'sudo',
'/var/bloxbycommands/remove_site.sh',
'--folder', $server_meta["folder"],
'--site', $server_meta["site"],
'--wpsite', $server_meta["slug_environment"],
'--domain', $server_meta["domain"]
]);
// ./remove_site.sh --folder websites_beta --site esodfjvno4 --wpsite wpbetasites --domain esodfjvno4beta
//$process = Process::fromShellCommandline(env("SITEREMPATH")." site_bloxy_wp_".$website->id." site_bloxy_db_".$website->id); // change to your command
$process->setTimeout(300); // Increase for long tasks
$process->run();
if (!$process->isSuccessful()) {
return response()->json(['status' => 'error', 'message' => $process->getErrorOutput()], 500);
} else {
@unlink(public_path("websites/thumbs/".$website->thumbnail));
Prompt::where("website_id", $website->id)->delete();
$website->forceDelete();
return redirect()->route("dashboard")->with([ 'success' => "Website has been removed successfully."]);
}
}
return redirect()->back();
}
public function storeSession(Request $request){
}
public function downloadZip(Request $request, $id){
$archive = new WebsitesArchiveRequest;
$archive->website_id = $id;
$archive->status = "pending";
$archive->save();
$website = Website::where("id",$id);
if(Auth::check()){
$website = $website->where("user_id", auth()->user()->id);
}
/*$website = $website->first();
if($website){
$server_meta = json_decode($website->server_meta, true);
$process = new Process([
'sudo',
'/var/bloxbycommands/changeinrole.sh',
'/var/www/'.$server_meta["folder"].'/'.$server_meta["sftp_user"]."/public_html",
'info@site.com',
'subscriber'
]);
//$process = Process::fromShellCommandline(env("SITEREMPATH")." site_bloxy_wp_".$website->id." site_bloxy_db_".$website->id); // change to your command
$process->setTimeout(300); // Increase for long tasks
$process->run();
if (!$process->isSuccessful()) {
return response()->json(['status' => 'error', 'message' => $process->getErrorOutput()], 500);
}
}*/
return redirect()->back()->with(["success"=>"Your download request has started. We'll email you the link once completed."]);
}
public function cancelPlan(Request $request, $id){
try{
Stripe::setApiKey(env('STRIPE_SECRET'));
$website= Website::where("id", $id)->first();
if($website && $website->is_cancellation_requested ==0){
$subscription = \Stripe\Subscription::retrieve(
$website->stripe_subscription_id,
['cancel_at_period_end' => true]
);
$subscription = \Stripe\Subscription::update(
$website->stripe_subscription_id,
['cancel_at_period_end' => true]
);
if($subscription->cancel_at_period_end){
$website->is_cancellation_requested = true;
if ($subscription->cancel_at_period_end) {
$end = $subscription->current_period_end
? date('Y-m-d H:i:s', $stripeSub->current_period_end)
: null;
if (!$subscription->current_period_end && isset($subscription->items->data[0])) {
$end = date('Y-m-d H:i:s', $subscription->items->data[0]->current_period_end);
}
$website->subscription_end = $end ;
}
$website->save();
//$user = $website->user();
$user = User::where("id", $website->user_id)->first();
//dd($user);
Mail::send('emails.website_cancelation_requested', [
'user' => $user,
'website' => $website
], function ($message) use ($user) {
$message->to($user->email);
$message->subject('Cancellation Requested');
});
}
}
return redirect()->back()->with(["success"=>"The website has been successfully marked for cancellation at the end of the current billing plan"]);
} catch (Exception $e){
return redirect()->back()->with(["error",$e->getMessage()]);
}
}
public function undoCancelPlan(Request $request, $id){
try{
Stripe::setApiKey(env('STRIPE_SECRET'));
$website= Website::where("id", $id)->first();
if($website && $website->is_cancellation_requested ==1){
$subscription = \Stripe\Subscription::update(
$website->stripe_subscription_id,
['cancel_at_period_end' => false]
);
if(!$subscription->cancel_at_period_end){
$website->is_cancellation_requested = 0;
$website->save();
}
}
return redirect()->back()->with(["success"=>"Website has been successfully un-marked for cancellation at end of the current billing plan."]);
} catch (Exception $e){
return redirect()->back()->with(["error"=>$e->getMessage()]);
}
}
function getClientIp() {
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
// IP from shared internet
return $_SERVER['HTTP_CLIENT_IP'];
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
// IP passed from proxy
return $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
// Default: remote address
return $_SERVER['REMOTE_ADDR'];
}
}
public function getSFTPCredentials(Request $request){
$website = $website = Website::where("user_id", auth()->user()->id )->where("id",$request->website_id)->first();
$sftp_ip = env("SERVER_IP");
$sftp_user = null;
$sftp_pass = null;
if($website){
$server_meta = @json_decode($website->server_meta, true);
$sftp_user = $server_meta["sftp_user"];
$sftp_pass = EncryptionHelper::secret_decrypt($server_meta["sftp_pass"],env("SODIUM_KEY"));
}
return view("websites.sftp_credentials")->with(compact(['sftp_ip','sftp_user','sftp_pass']));
}
public function addDomain(Request $request, $id){
$website = Website::where("domain", $request->domain)->first();
if($website){
return redirect()->back()->with('error', "The domain is already attached to another website");
}
$website = Website::where("id", $id)->where("user_id", auth()->user()->id)->first();
$website->domain = $request->domain;
$website->domain_status = "pending";
$website->save();
return redirect()->back()->with(["success"=>"Your domain has been added successfully."]);
}
public function attachDomain(Request $request, $id){
$website = Website::where("id", $id)->where("user_id", auth()->user()->id)->first();
if($website){
$ip = gethostbyname($request->domain);
if ($ip && $ip === env("SERVER_IP")) {
$server_meta = json_decode($website->server_meta, true);
$process = new Process([
'sudo',
'/var/bloxbycommands/attach_domain.sh',
$server_meta["domain"].".". \config('app.SITE_DOMAIN'),
$request->domain,
"/var/www/".$server_meta["folder"]."/".$server_meta["sftp_user"]."/public_html"
]);
$process->setTimeout(300); // Increase for long tasks
$process->run();
$output = trim($process->getOutput()); // single-line message from script
$exitCode = $process->getExitCode();
// Determine message type
if ($exitCode === 0) {
$website->domain = $request->domain;
$website->save();
return redirect()->back()->with('success', $output);
} else {
// Optionally include stderr if needed
$errorOutput = trim($process->getErrorOutput());
$message = $output ?: $errorOutput ?: 'Unknown error';
return redirect()->back()->with('error', $message);
}
} else {
return redirect()->back()->with('error', "Your domain is either not pointed or still in transition.");
}
}
return redirect()->back()->with(["success"=>"Your domain has been pointed successfully."]);
}
public function removeDomain(Request $request, $id){
$website = Website::where("id", $id)->first();
if($website){
$server_meta = json_decode($website->server_meta, true);
$process = new Process([
'sudo',
'/var/bloxbycommands/remove_domain.sh',
$website->domain,
$server_meta["domain"].".". \config('app.SITE_DOMAIN'),
"/var/www/".$server_meta["folder"]."/".$server_meta["sftp_user"]."/public_html"
]);
$process->setTimeout(300); // Increase for long tasks
$process->run();
if (!$process->isSuccessful()) {
return redirect()->back()->with([ 'errors' =>[ $process->getErrorOutput()]]);
}
$website->domain= null;
$website->domain_status= 'pending';
$website->domain_error= null;
$website->save();
}
return redirect()->back()->with(["success"=>"Your domain has been removed successfully."]);
}
public function deleteDomain(Request $request, $id){
try{
DB::BeginTransaction();
$website = Website::where("id", $id)->first();
if($website){
if($website->domain_status=="pending"){
$website->domain= null;
$website->domain_status= 'pending';
$website->domain_error= null;
$website->save();
}
DB::Commit();
}
} catch(\Exception $e){
DB::Rollback();
return redirect()->back()->with([ 'errors' =>[ $e->getMessage()]]);
}
return redirect()->back()->with(["success"=>"Your domain has been deleted successfully."]);
}
public function websiteDetails(Request $request, $id){
$website = Website::where("id", $id)->first();
if(!$website){
return redirect("/dashboard");
}
$server_meta = json_decode($website->server_meta, true);
$sftp_ip = env("SERVER_IP");
$sftp_user = null;
$sftp_pass = null;
if($website){
$sftp_user = $server_meta["sftp_user"];
$sftp_pass = EncryptionHelper::secret_decrypt($server_meta["sftp_pass"],env("SODIUM_KEY"));
}
$dns_status = false;
if($website->domain){
$ip = gethostbyname($website->domain);
if ($ip && $ip === env("SERVER_IP")){
$dns_status = true;
}
}
return view("websites.overview_new")->with(compact(['website','server_meta','sftp_ip','sftp_user','sftp_pass','dns_status']));
}
}