@ -5,8 +5,10 @@ namespace App\Commands;  
														
													
														
															
																use  Illuminate\Console\Scheduling\Schedule ; use  Illuminate\Console\Scheduling\Schedule ;  
														
													
														
															
																use  LaravelZero\Framework\Commands\Command ; use  LaravelZero\Framework\Commands\Command ;  
														
													
														
															
																use  Illuminate\Support\Facades\File ; use  Illuminate\Support\Facades\File ;  
														
													
														
															
																use  Jenssegers\Blade\Blade ;  
														
													
														
															
																
 
  
														
													
														
															
																use  App\Facades\Install ; use  App\Facades\Install ;  
														
													
														
															
																use  App\Facades\NginxVhost ;  
														
													
														
															
																
 
  
														
													
														
															
																use  PhpSchool\CliMenu\Builder\CliMenuBuilder ; use  PhpSchool\CliMenu\Builder\CliMenuBuilder ;  
														
													
														
															
																use  PhpSchool\CliMenu\CliMenu ; use  PhpSchool\CliMenu\CliMenu ;  
														
													
												
													
														
															
																
																	
																	
																	
																		
																			 
																	
																 
																@ -14,11 +16,12 @@ use PhpSchool\CliMenu\MenuItem\CheckboxItem;  
														
													
														
															
																use  PhpSchool\CliMenu\Style\SelectableStyle ; use  PhpSchool\CliMenu\Style\SelectableStyle ;  
														
													
														
															
																
 
  
														
													
														
															
																use  Log ; use  Log ;  
														
													
														
															
																use  Closure ;  
														
													
														
															
																
 
  
														
													
														
															
																class  NginxVhostsCommand  extends  Command class  NginxVhostsCommand  extends  Command  
														
													
														
															
																{ {  
														
													
														
															
																    // path templates
      // path templates
   
														
													
														
															
																    const  TEMPLATES_DIR  =  '/resources/templates/nginx ' ;   
														
													
														
															
																    const  TEMPLATES_DIR  =  '/resources/nginx/ templates' ;   
														
													
														
															
																
 
  
														
													
														
															
																    /**      /**   
														
													
														
															
																     *  The  signature  of  the  command .       *  The  signature  of  the  command .   
														
													
												
													
														
															
																
																	
																	
																	
																		
																			 
																	
																 
																@ -39,138 +42,227 @@ class NginxVhostsCommand extends Command  
														
													
														
															
																     *       *   
														
													
														
															
																     *       *   
														
													
														
															
																     */       */   
														
													
														
															
																    private  function  add ( CliMenuBuilder  $builder )   
														
													
														
															
																    private  function  getTemplates ( $prefix  =  '' )   
														
													
														
															
																    {      {   
														
													
														
															
																        // templates
   
														
													
														
															
																        $templates  =  [];   
														
													
														
															
																
 
  
														
													
														
															
																        // getting templates
   
														
													
														
															
																        $files  =  scandir ( base_path () . self :: TEMPLATES_DIR );   
														
													
														
															
																
  
														
													
														
															
																        foreach ( $files  as  $file )  {   
														
													
														
															
																
  
														
													
														
															
																            // create filepath
   
														
													
														
															
																            $filepath  =  base_path () . self :: TEMPLATES_DIR . '/' . $file ;   
														
													
														
															
																
  
														
													
														
															
																            // getting info
   
														
													
														
															
																            $pathinfo  =  pathinfo ( $filepath );   
														
													
														
															
																
  
														
													
														
															
																            // if extension isset and php
   
														
													
														
															
																            if  ( isset ( $pathinfo [ 'extension' ])  &&  $pathinfo [ 'extension' ]  ===  'php' )  {   
														
													
														
															
																
  
														
													
														
															
																                $name  =  str_replace ( '.blade.php' ,  '' ,  $file );   
														
													
														
															
																
  
														
													
														
															
																                $templates []  =  [   
														
													
														
															
																                    'name'  =>  $name ,   
														
													
														
															
																                    'file'  =>  $file ,   
														
													
														
															
																                    'filepath'  =>  $filepath   
														
													
														
															
																                ];   
														
													
														
															
																            }   
														
													
														
															
																        }   
														
													
														
															
																
  
														
													
														
															
																        return  $templates ;   
														
													
														
															
																    }      }   
														
													
														
															
																
 
  
														
													
														
															
																    /**      /**   
														
													
														
															
																     *  Execute  the  console  command .   
														
													
														
															
																     *       *   
														
													
														
															
																     *  @ return  mixed   
														
													
														
															
																     *   
														
													
														
															
																     *   
														
													
														
															
																     */       */   
														
													
														
															
																    public  function  handle ()   
														
													
														
															
																    private  function  selectTemplat e ()   
														
													
														
															
																    {      {   
														
													
														
															
																        // create menu
   
														
													
														
															
																        $main  =  $this -> menu ( self :: MENU_TITLE )   
														
													
														
															
																            -> addSubMenu ( 'add' ,  function ( CliMenuBuilder  $builder )  {   
														
													
														
															
																
  
														
													
														
															
																                // getting templates
   
														
													
														
															
																                $templates  =  scandir ( base_path () . self :: TEMPLATES_DIR );   
														
													
														
															
																
  
														
													
														
															
																                foreach ( $templates  as  $template )  {   
														
													
														
															
																                    $builder   
														
													
														
															
																                        -> addSubMenu ( $template ,  function ( CliMenuBuilder  $builder )  use  ( $template )  {   
														
													
														
															
																
  
														
													
														
															
																                            $configuration  =  [   
														
													
														
															
																                                'domain'  =>  '' ,   
														
													
														
															
																                                'ssl'  =>  true ,   
														
													
														
															
																                                'redirect_www'  =>  true   
														
													
														
															
																                            ];   
														
													
														
															
																
  
														
													
														
															
																                            $checkboxSSL  =  new  CheckboxItem ( 'ssl' ,  function ( CliMenu  $menu )  use  ( & $configuration )  {   
														
													
														
															
																                                $configuration [ 'ssl' ]  =  $menu -> getSelectedItem () -> getChecked ();   
														
													
														
															
																                            });   
														
													
														
															
																
  
														
													
														
															
																                            $checkboxSSL -> setChecked ( $configuration [ 'ssl' ]);   
														
													
														
															
																
  
														
													
														
															
																                            $checkboxRedirect  =  new  CheckboxItem ( 'redirect www' ,  function ( CliMenu  $menu )  use  ( & $configuration )  {   
														
													
														
															
																                                $configuration [ 'redirect www' ]  =  $menu -> getSelectedItem () -> getChecked ();   
														
													
														
															
																                            });   
														
													
														
															
																
  
														
													
														
															
																                            $checkboxRedirect -> setChecked ( $configuration [ 'redirect_www' ]);   
														
													
														
															
																
  
														
													
														
															
																                            $builder   
														
													
														
															
																                                -> setTitle ( 'Nginx Vhosts > add > ' . $template )   
														
													
														
															
																                                -> addItem ( 'domain' ,  function ( CliMenu  $menu )  use  ( & $configuration )  {   
														
													
														
															
																                                    $result  =  $menu -> askText () -> ask ();   
														
													
														
															
																                                    $configuration [ 'domain' ]  =  $result -> fetch ();   
														
													
														
															
																
  
														
													
														
															
																                                    $menu -> getSelectedItem () -> setText ( 'domain -> ' . $result -> fetch ());   
														
													
														
															
																                                    $menu -> redraw ();   
														
													
														
															
																                                })   
														
													
														
															
																                                -> addLineBreak ( '-' )   
														
													
														
															
																                                -> addMenuItem ( $checkboxSSL )   
														
													
														
															
																                                -> addMenuItem ( $checkboxRedirect )   
														
													
														
															
																                                -> addLineBreak ( '-' )   
														
													
														
															
																                                -> addItem ( 'save' ,  function ()  use  ( & $configuration )  {   
														
													
														
															
																                                    var_dump ( $configuration );   
														
													
														
															
																                                })   
														
													
														
															
																                                -> addLineBreak ( '-' );   
														
													
														
															
																                        });   
														
													
														
															
																                }   
														
													
														
															
																        // getting templates
   
														
													
														
															
																        $templates  =  $this -> getTemplates ();   
														
													
														
															
																
  
														
													
														
															
																        $menu  =  function ( CliMenuBuilder  $builder )  use  ( $templates )   
														
													
														
															
																        {   
														
													
														
															
																            // writing configuration
   
														
													
														
															
																            $blade  =  new  Blade ( base_path () . self :: TEMPLATES_DIR ,  base_path () . '/storage/cache' );   
														
													
														
															
																
  
														
													
														
															
																            $builder   
														
													
														
															
																                -> setTitle ( 'Nginx > add' )   
														
													
														
															
																                -> setGoBackButtonText ( 'Back' );   
														
													
														
															
																
  
														
													
														
															
																            foreach ( $templates  as  $template )  {   
														
													
														
															
																                $submenuCallable  =  $this -> createConfiguration ( $template ,  $blade );   
														
													
														
															
																                $builder   
														
													
														
															
																                    -> addSubMenu ( $template [ 'name' ],  $submenuCallable );   
														
													
														
															
																            }   
														
													
														
															
																
 
  
														
													
														
															
																                //exec('touch /etc/nginx/sites-available/');
   
														
													
														
															
																            })   
														
													
														
															
																            -> addLineBreak ( '-' );   
														
													
														
															
																            $builder -> addLineBreak ( '-' );   
														
													
														
															
																        };   
														
													
														
															
																
 
  
														
													
														
															
																        /***   
														
													
														
															
																        return  $menu ;   
														
													
														
															
																    }   
														
													
														
															
																
 
  
														
													
														
															
																        $itemCallable  =  function ( CliMenu  $menu )  {   
														
													
														
															
																    /**   
														
													
														
															
																     *   
														
													
														
															
																     *   
														
													
														
															
																     *   @ param   [ type ]  $template  [ description ]   
														
													
														
															
																     *   @ param   [ type ]  $blade     [ description ]   
														
													
														
															
																     *   @ return  [ type ]            [ description ]   
														
													
														
															
																     */   
														
													
														
															
																    private  function  createConfiguration ( $template ,  $blade )   
														
													
														
															
																    {   
														
													
														
															
																        $menu  =  function ( CliMenuBuilder  $builder )  use  ( $template ,  $blade )   
														
													
														
															
																        {   
														
													
														
															
																            $configuration  =  [   
														
													
														
															
																                'domain'  =>  '' ,   
														
													
														
															
																                'root'  =>  '' ,   
														
													
														
															
																                'index'  =>  'index.php' ,   
														
													
														
															
																                'ssl'  =>  true ,   
														
													
														
															
																                'redirect_www'  =>  true   
														
													
														
															
																            ];   
														
													
														
															
																
  
														
													
														
															
																            // create checkbox for ssl
   
														
													
														
															
																            $checkboxSSL  =  new  CheckboxItem ( 'ssl' ,  function ( CliMenu  $menu )  use  ( & $configuration )  {   
														
													
														
															
																                $configuration [ 'ssl' ]  =  $menu -> getSelectedItem () -> getChecked ();   
														
													
														
															
																            });   
														
													
														
															
																
  
														
													
														
															
																            $checkboxSSL -> setChecked ( $configuration [ 'ssl' ]);   
														
													
														
															
																
  
														
													
														
															
																            // create checkbox for redirect from www
   
														
													
														
															
																            $checkboxRedirect  =  new  CheckboxItem ( 'redirect www' ,  function ( CliMenu  $menu )  use  ( & $configuration )  {   
														
													
														
															
																                $configuration [ 'redirect_www' ]  =  $menu -> getSelectedItem () -> getChecked ();   
														
													
														
															
																            });   
														
													
														
															
																
  
														
													
														
															
																            $checkboxRedirect -> setChecked ( $configuration [ 'redirect_www' ]);   
														
													
														
															
																
  
														
													
														
															
																            $builder   
														
													
														
															
																                -> setTitle ( 'Nginx Vhosts > add > ' . $template [ 'name' ])   
														
													
														
															
																                -> addItem ( 'domain' ,  function ( CliMenu  $menu )  use  ( & $configuration )  {   
														
													
														
															
																                    $result  =  $menu -> askText () -> ask ();   
														
													
														
															
																                    $configuration [ 'domain' ]  =  $result -> fetch ();   
														
													
														
															
																
  
														
													
														
															
																                    $menu -> getSelectedItem () -> setText ( 'domain -> ' . $result -> fetch ());   
														
													
														
															
																                    $menu -> redraw ();   
														
													
														
															
																                })   
														
													
														
															
																                -> addItem ( 'root' ,  function ( CliMenu  $menu )  use  ( & $configuration )  {   
														
													
														
															
																                    $result  =  $menu -> askText () -> ask ();   
														
													
														
															
																                    $configuration [ 'root' ]  =  $result -> fetch ();   
														
													
														
															
																
  
														
													
														
															
																                    $menu -> getSelectedItem () -> setText ( 'root -> ' . $result -> fetch ());   
														
													
														
															
																                    $menu -> redraw ();   
														
													
														
															
																                })   
														
													
														
															
																                -> addItem ( $configuration [ 'index' ],  function ( CliMenu  $menu )  use  ( & $configuration )  {   
														
													
														
															
																                    $result  =  $menu -> askText () -> ask ();   
														
													
														
															
																                    $configuration [ 'index' ]  =  $result -> fetch ();   
														
													
														
															
																
  
														
													
														
															
																                    $menu -> getSelectedItem () -> setText ( $result -> fetch ());   
														
													
														
															
																                    $menu -> redraw ();   
														
													
														
															
																                })   
														
													
														
															
																                -> addLineBreak ( '-' )   
														
													
														
															
																                -> addMenuItem ( $checkboxSSL )   
														
													
														
															
																                -> addMenuItem ( $checkboxRedirect )   
														
													
														
															
																                -> addLineBreak ( '-' )   
														
													
														
															
																                -> addItem ( 'save' ,  function ()  use  ( & $configuration ,  $template ,  $blade )  {   
														
													
														
															
																                    $content  =  $blade -> render ( $template [ 'name' ],  $configuration );   
														
													
														
															
																                    file_put_contents ( '/etc/nginx/sites-available/' . $configuration [ 'domain' ] . '.conf' ,  $content );   
														
													
														
															
																                })   
														
													
														
															
																                -> addLineBreak ( '-' );   
														
													
														
															
																        };   
														
													
														
															
																
  
														
													
														
															
																        return  $menu ;   
														
													
														
															
																    }   
														
													
														
															
																
 
  
														
													
														
															
																            // getting sites-available
   
														
													
														
															
																            $sitesAvailable  =  scandir ( '/etc/nginx/sites-available' );   
														
													
														
															
																    /**   
														
													
														
															
																     *   create  submenu  for  vhost   
														
													
														
															
																     *   
														
													
														
															
																     *   
														
													
														
															
																     *   @ param   array  $vhost   
														
													
														
															
																     *   @ return  CliMenuBuilder   
														
													
														
															
																     *   
														
													
														
															
																     */   
														
													
														
															
																    private  function  vhost ( $vhost )   
														
													
														
															
																    {   
														
													
														
															
																        $menu  =  function ( CliMenuBuilder  $builder )  use  ( $vhost )   
														
													
														
															
																        {   
														
													
														
															
																            $checkbox  =  new  CheckboxItem ( 'enabled' ,  function ( CliMenu  $menu )  use  ( $vhost )  {   
														
													
														
															
																
  
														
													
														
															
																                // check status
   
														
													
														
															
																                if  ( $menu -> getSelectedItem () -> getChecked ())  {   
														
													
														
															
																                    symlink ( '/etc/nginx/sites-available/' . $vhost [ 'file' ],  '/etc/nginx/sites-enabled/' . $vhost [ 'file' ]);   
														
													
														
															
																                    $status  =  'enabled' ;   
														
													
														
															
																                }  else  {   
														
													
														
															
																                    unlink ( '/etc/nginx/sites-enabled/' . $vhost [ 'file' ]);   
														
													
														
															
																                    $status  =  'disabled' ;   
														
													
														
															
																                }   
														
													
														
															
																
 
  
														
													
														
															
																            // getting sites-endabled
   
														
													
														
															
																            $sitesEnabled  =  scandir ( '/etc/nginx/sites-enabled' );   
														
													
														
															
																                // restart
   
														
													
														
															
																                exec ( 'service nginx restart' );   
														
													
														
															
																                exec ( 'service nginx status' ,  $output );   
														
													
														
															
																
 
  
														
													
														
															
																            foreach ( $sitesAvailable  as  $site )  {   
														
													
														
															
																                if  ( $site  !==  '.'  &&  $site  !==  '..' )  {   
														
													
														
															
																                if  ( strpos ( implode ( ' ' ,  $output ),  'active' )  !==  false )  {   
														
													
														
															
																                    $menu -> confirm ( $vhost [ 'domain' ] . ' is ' . $status . '!' ) -> display ( 'OK!' );   
														
													
														
															
																                }  else  {   
														
													
														
															
																                    $menu -> confirm ( 'Error! Something not working!' ) -> display ( 'OK!' );   
														
													
														
															
																                }   
														
													
														
															
																
 
  
														
													
														
															
																                    // getting title
   
														
													
														
															
																                    $title  =  str_replace ( '.conf' ,  '' ,  $site );   
														
													
														
															
																            });   
														
													
														
															
																
 
  
														
													
														
															
																                    $checkbox  =  new  CheckboxItem ( 'enabled' ,  function ( CliMenu  $menu )  use  ( $site ,  $title )  {   
														
													
														
															
																            // adding checkbox
   
														
													
														
															
																            if  ( $vhost [ 'enabled' ])  {   
														
													
														
															
																                $checkbox -> setChecked ( true );   
														
													
														
															
																            }   
														
													
														
															
																
 
  
														
													
														
															
																                        // check status
   
														
													
														
															
																                        if  ( $menu -> getSelectedItem () -> getChecked ())  {   
														
													
														
															
																                            symlink ( '/etc/nginx/sites-available/' . $site ,  '/etc/nginx/sites-enabled/' . $site );   
														
													
														
															
																                            $status  =  'enabled' ;   
														
													
														
															
																                        }  else  {   
														
													
														
															
																                            unlink ( '/etc/nginx/sites-enabled/' . $site );   
														
													
														
															
																                            $status  =  'disabled' ;   
														
													
														
															
																                        }   
														
													
														
															
																            $builder   
														
													
														
															
																                -> setTitle ( 'Nginx > ' . $vhost [ 'domain' ])   
														
													
														
															
																                -> setGoBackButtonText ( 'Back' )   
														
													
														
															
																                -> addItem ( 'edit' ,  function ( CliMenu  $menu )  use  ( $vhost )  {   
														
													
														
															
																                    system ( 'nano /etc/nginx/sites-available/' . $vhost [ 'file' ] . ' > `tty`' );   
														
													
														
															
																                })   
														
													
														
															
																                -> addItem ( 'delete' ,  function ( CliMenu  $menu )  use  ( $vhost )  {   
														
													
														
															
																                    if  ( file_exists ( '/etc/nginx/sites-enabled/' . $vhost [ 'file' ]))  {   
														
													
														
															
																                        $menu -> confirm ( 'Error! Please disable ' . $vhost [ 'domain' ] . ' first!' ) -> display ( 'OK!' );   
														
													
														
															
																                    }  else  {   
														
													
														
															
																                        unlink ( '/etc/nginx/sites-available/' . $vhost [ 'file' ]);   
														
													
														
															
																                        $menu -> confirm ( $vhost [ 'domain' ] . ' is deleted!' ) -> display ( 'OK!' );   
														
													
														
															
																                    }   
														
													
														
															
																                })   
														
													
														
															
																                -> addLineBreak ( '-' )   
														
													
														
															
																                -> addMenuItem ( $checkbox )   
														
													
														
															
																                -> addLineBreak ( '-' );   
														
													
														
															
																        };   
														
													
														
															
																
 
  
														
													
														
															
																                        // starting
   
														
													
														
															
																                        exec ( 'service nginx restart' );   
														
													
														
															
																                        exec ( 'service nginx status' ,  $output );   
														
													
														
															
																        return  $menu ;   
														
													
														
															
																    }   
														
													
														
															
																
 
  
														
													
														
															
																                        if  ( strpos ( implode ( ' ' ,  $output ),  'active' )  !==  false )  {   
														
													
														
															
																                            $menu -> confirm ( $title . ' is ' . $status . '!' ) -> display ( 'OK!' );   
														
													
														
															
																                        }  else  {   
														
													
														
															
																                            $menu -> confirm ( 'Error! Something not working!' ) -> display ( 'OK!' );   
														
													
														
															
																                        }   
														
													
														
															
																    /**   
														
													
														
															
																     *   let  it  rain   
														
													
														
															
																     *   
														
													
														
															
																     *   
														
													
														
															
																     */   
														
													
														
															
																    public  function  handle ()   
														
													
														
															
																    {   
														
													
														
															
																        $submenuSelectTemplate  =  $this -> selectTemplate ();   
														
													
														
															
																
 
  
														
													
														
															
																                    });   
														
													
														
															
																        // create menu
   
														
													
														
															
																        $main  =  $this -> menu ( 'Nginx' )   
														
													
														
															
																
 
  
														
													
														
															
																                    // adding checkbox
   
														
													
														
															
																                    if  ( in_array ( $site ,  $sitesEnabled ))  {   
														
													
														
															
																                        $checkbox -> setChecked ( true );   
														
													
														
															
																                    }   
														
													
														
															
																            // add Submenu for select templates
   
														
													
														
															
																            -> addSubMenu ( 'add' ,  $submenuSelectTemplate )   
														
													
														
															
																            -> addLineBreak ( '-' );   
														
													
														
															
																
 
  
														
													
														
															
																                    $menuMain   
														
													
														
															
																                        -> addSubmenu ( $title ,  function ( CliMenuBuilder  $builder )  use  ( $site ,  $title ,  $checkbox )  {   
														
													
														
															
																                            $builder -> setTitle ( " Nginx >  $title " )   
														
													
														
															
																                                -> addItem ( 'edit' ,  function ( CliMenu  $menu )  use  ( $site ,  $title )  {   
														
													
														
															
																                                    system ( 'nano /etc/nginx/sites-available/' . $site . ' > `tty`' );   
														
													
														
															
																                                })   
														
													
														
															
																                                -> addItem ( 'delete' ,  function ( CliMenu  $menu )  use  ( $site )  {   
														
													
														
															
																                                    if  ( file_exists ( '/etc/nginx/sites-enabled/' . $site ))  {   
														
													
														
															
																                                        $menu -> confirm ( 'Error! Please disable ' . $title . ' first!' ) -> display ( 'OK!' );   
														
													
														
															
																                                    }  else  {   
														
													
														
															
																                                        unlink ( '/etc/nginx/sites-available/' . $site );   
														
													
														
															
																                                        $menu -> confirm ( " $site  is deleted! " ) -> display ( 'OK!' );   
														
													
														
															
																                                    }   
														
													
														
															
																                                })   
														
													
														
															
																                                -> addLineBreak ( '-' )   
														
													
														
															
																                                -> addMenuItem ( $checkbox )   
														
													
														
															
																                                -> addLineBreak ( '-' );   
														
													
														
															
																                        });   
														
													
														
															
																        // getting vhosts
   
														
													
														
															
																        $vhosts  =  NginxVhost :: find ();   
														
													
														
															
																
 
  
														
													
														
															
																                }   
														
													
														
															
																            }   
														
													
														
															
																        }; */   
														
													
														
															
																        // add submenu for each vhost
   
														
													
														
															
																        foreach ( $vhosts  as  $vhost )  {   
														
													
														
															
																            $submenuEditVhost  =  $this -> vhost ( $vhost );   
														
													
														
															
																            $main -> addSubMenu ( $vhost [ 'domain' ],  $submenuEditVhost );   
														
													
														
															
																        }   
														
													
														
															
																
 
  
														
													
														
															
																        $main -> addLineBreak ( '-' ) -> open ();   
														
													
														
															
																        $main -> addLineBreak ( '-' );   
														
													
														
															
																        $main -> open ();   
														
													
														
															
																    }      }   
														
													
														
															
																} }