<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class CustomStyleController extends Controller
{
	public function addStyle()
    {
      $css_get = @file_get_contents('css/custom-style.css');
      $js_get  = @file_get_contents('js/custom-js.js');
      return view('admin.customstyle.add',compact('css_get','js_get'));
    }

    public function storeCSS(Request $request)
    {
        $this->validate($request,array(
          'css' => 'required'
        ));
        // Remove any script tags or expressions from CSS
        $css = $request->css;
        $css = preg_replace('/<script\b[^>]*>(.*?)<\/script>/is', '', $css);
        $css = preg_replace('/expression\s*\(/i', '', $css);
        $css = preg_replace('/javascript\s*:/i', '', $css);
        $css = preg_replace('/url\s*\(\s*["\']?\s*javascript/i', 'url(blocked', $css);
        try {
          file_put_contents("css/custom-style.css",$css.PHP_EOL);
          return redirect()->route('customstyle')->with('added','Added Custom CSS !');
        } catch (\Exception $e) {
          return back()->with('deleted',$e->getMessage());
        }

    }

    public function storeJS(Request $request)
    {
      $this->validate($request,array(
        'js' => 'required'
      ));

      $js = $request->js;
      try{
        file_put_contents("js/custom-js.js",$js.PHP_EOL);
        return redirect()->route('customstyle')->with('added','Added Javascript !');
      }
      catch(\Exception $e){
        return back()->with('deleted',$e->getMessage());
      }

    }


}
