<commit_before><?php

namespace Larasoft\LaravelRemote\Http\Controllers;


use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Log;

class LaravelRemoteController extends Controller
{
    protected $laravel_remote_key;
    protected $backup_dir_name;

    public function __construct(Request $request)
    {
        $this->laravel_remote_key = config('remote.key');
        $this->backup_dir_name = preg_replace("/[^A-Za-z0-9 ]/", '-', config('laravel-backup.backup.name'));
    }

    public function getStatus(Request $request)
    {
        if ( $this->checkLaravelRemoteKey($request) )
        {
            if ( app()->isDownForMaintenance() )
            {
                return \Response::json(['status' => 'down', 'success' => 1]);
            }
            else
            {
                return \Response::json(['status' => 'up', 'success' => 1]);
            }
        }
        else
        {
            return \Response::json(['success' => 0, 'message' => 'Invalid Laravel Remote Key!']);
        }

    }

    public function executeCommand($command, Request $request)
    {
        if ( $this->checkLaravelRemoteKey($request) )
        {
            if ( $command == 'up' )
            {
                Artisan::call('up');

                return \Response::json(['status' => 'up', 'success' => 1]);
            }
            else if( $command == 'down' )
            {
                Artisan::call('down');

                return \Response::json(['status' => 'down', 'success' => 1]);
            }
            else
            {
                Artisan::call($command);
            }

            return response(200);
        }
        else{
            return response()->json(['success' => 0, 'message' => 'Invalid Laravel Remote Key!']);
        }
    }

    /*
     * Get all Backups
     */
    public function backups(Request $request){
        if ( $this->checkLaravelRemoteKey($request) )
        {
            $links = [];

            $files = File::allFiles(storage_path("app/{$this->backup_dir_name}"));

            foreach ($files as $file){
                array_push($links, [
                    'name' => basename($file),
                    'download_path' => url('laravel-remote/backups/'.basename($file).'/download'),
                    'delete_path' => url('laravel-remote/backups/'.basename($file).'/delete')
                ]);
            }

            return response()->json(['success' => 1, 'backups' => $links]);
        }
        else{
            return response()->json(['success' => 0, 'message' => 'Invalid Laravel Remote Key!']);
        }
    }

    /*
     * Download a specific Backup
     */
    public function downloadBackup($name){
        if ( $this->checkLaravelRemoteKey(request()) )
        {
            $file = storage_path("app/{$this->backup_dir_name}/{$name}");

            return response()->download($file);
        }
        else{
            return 'Unauthorised Access! Please contact administrator.';
        }
    }

    /*
     * Delete a specific Backup
     */
    public function deleteBackup($name){
        if ( $this->checkLaravelRemoteKey(request()) )
        {
            $file = storage_path("app/{$this->backup_dir_name}/{$name}");

            if(File::exists($file)){
                File::delete($file);
            }

            return response()->json(['success' => 1]);
        }
        else{
            return response()->json(['success' => 0, 'message' => 'Invalid Laravel Remote Key!']);
        }
    }

    public function getEnvVariables(){
        $vars = $this->envToArray(base_path('.env'));

        $data = [];

        foreach ($vars as $key => $var){
            array_push($data, ['name' => $key, 'value' => $var]);
        }

        return $data;
    }

    public function getRawEnvFile(){
        return file_get_contents(base_path('.env'));
    }

    public function saveRawEnvFile(Request $request){
        if ( $this->checkLaravelRemoteKey(request()) )<commit_msg>Security added to env access<commit_after><?php

namespace Larasoft\LaravelRemote\Http\Controllers;


use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Log;

class LaravelRemoteController extends Controller
{
    protected $laravel_remote_key;
    protected $backup_dir_name;

    public function __construct(Request $request)
    {
        $this->laravel_remote_key = config('remote.key');
        $this->backup_dir_name = preg_replace("/[^A-Za-z0-9 ]/", '-', config('laravel-backup.backup.name'));
    }

    public function getStatus(Request $request)
    {
        if ( $this->checkLaravelRemoteKey($request) )
        {
            if ( app()->isDownForMaintenance() )
            {
                return \Response::json(['status' => 'down', 'success' => 1]);
            }
            else
            {
                return \Response::json(['status' => 'up', 'success' => 1]);
            }
        }
        else
        {
            return \Response::json(['success' => 0, 'message' => 'Invalid Laravel Remote Key!']);
        }

    }

    public function executeCommand($command, Request $request)
    {
        if ( $this->checkLaravelRemoteKey($request) )
        {
            if ( $command == 'up' )
            {
                Artisan::call('up');

                return \Response::json(['status' => 'up', 'success' => 1]);
            }
            else if( $command == 'down' )
            {
                Artisan::call('down');

                return \Response::json(['status' => 'down', 'success' => 1]);
            }
            else
            {
                Artisan::call($command);
            }

            return response(200);
        }
        else{
            return response()->json(['success' => 0, 'message' => 'Invalid Laravel Remote Key!']);
        }
    }

    /*
     * Get all Backups
     */
    public function backups(Request $request){
        if ( $this->checkLaravelRemoteKey($request) )
        {
            $links = [];

            $files = File::allFiles(storage_path("app/{$this->backup_dir_name}"));

            foreach ($files as $file){
                array_push($links, [
                    'name' => basename($file),
                    'download_path' => url('laravel-remote/backups/'.basename($file).'/download'),
                    'delete_path' => url('laravel-remote/backups/'.basename($file).'/delete')
                ]);
            }

            return response()->json(['success' => 1, 'backups' => $links]);
        }
        else{
            return response()->json(['success' => 0, 'message' => 'Invalid Laravel Remote Key!']);
        }
    }

    /*
     * Download a specific Backup
     */
    public function downloadBackup($name){
        if ( $this->checkLaravelRemoteKey(request()) )
        {
            $file = storage_path("app/{$this->backup_dir_name}/{$name}");

            return response()->download($file);
        }
        else{
            return 'Unauthorised Access! Please contact administrator.';
        }
    }

    /*
     * Delete a specific Backup
     */
    public function deleteBackup($name){
        if ( $this->checkLaravelRemoteKey(request()) )
        {
            $file = storage_path("app/{$this->backup_dir_name}/{$name}");

            if(File::exists($file)){
                File::delete($file);
            }

            return response()->json(['success' => 1]);
        }
        else{
            return response()->json(['success' => 0, 'message' => 'Invalid Laravel Remote Key!']);
        }
    }

    public function getEnvVariables(){
        if ( $this->checkLaravelRemoteKey(request()) )
        {
            $vars = $this->envToArray(base_path('.env'));

            $data = [];

            foreach ($vars as $key => $var){
                array_push($data, ['name' => $key, 'value' => $var]);
            }

            return response()->json(['success' => 1, 'variables' => $data]);
        }
        else{
            return response()->json(['success' => 0, 'message' => 'Invalid Laravel Remote Key!']);
        }
    }

    public function getRawEnvFile(){
        if ( $this->checkLaravelRemoteKey(request()) )
        {
            return response()->json(['success' => 1, 'raw' => file_get_contents(base_path('.env'))]);
        }
        else{
            return response()->json(['success' => 0, 'message' => 'Invalid Laravel Remote Key!']);
        }
    }

    public function saveRawEnvFile(Request $request){
        if ( $this->checkLaravelRemoteKey(request()) )
{'fraction_non_alphanumeric': 0.15916087105233154, 'fraction_numerical': 0.003876410899555353, 'mean_word_length': 1.850828729281768, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 105, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1453748', 'n_tokens_mistral': 2483, 'n_tokens_neox': 2387, 'n_words': 448}