Current File : //root/panel/modules/services/code/controller.ext.php

<?php

/**
 * @copyright 2014-2015 Sentora Project (http://www.sentora.org/) 
 * Sentora is a GPL fork of the ZPanel Project whose original header follows:
 *
 * ZPanel - A Cross-Platform Open-Source Web Hosting Control panel.
 *
 * @package ZPanel
 * @version $Id$
 * @author Bobby Allen - ballen@bobbyallen.me
 * @copyright (c) 2008-2014 ZPanel Group - http://www.zpanelcp.com/
 * @license http://opensource.org/licenses/gpl-3.0.html GNU Public License v3
 *
 * This program (ZPanel) is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @changes P.Peyremorte
 * - added display of ports checked (80 may be not used!)
 * - refactored (replacement of duplicate string constructions by a fucntion)
 */
class module_controller extends ctrl_module
{

     static public function getinit()
    {
        return '<script type="text/javascript" src="modules/services/assets/init.js"></script>';
    }
    static private function status_port($PortNum, $iconpath)
    {
        $status = sys_monitoring::LocalPortStatus($PortNum);
        return ($status ?  $iconpath.'up.gif">' : $iconpath.'down.gif">') . ' (port ' . $PortNum .' is ' . ($status ? 'open' : 'closed') . ')';
    }

    static public function getServices()
    {
	global $zdbh;
        global $controller;
        $iconpath = '<img src="modules/' . $controller->GetControllerRequest('URL', 'module') . '/assets/';
        
        $line = "<h2>" . ui_language::translate("Checking status of services...") . "</h2>";
        $line .= "<table>";
        
        $status = fs_director::CheckForEmptyValue(sys_monitoring::PortStatus($PortNum));
	$flag=0;
	$currentuser = ctrl_users::GetUserDetail();
	if($currentuser['userid'] == 1)
	{
	$flag=1;
	} 
       $line .= '<tr><th>HTTP</th><td>'  . module_controller::status_port(80, $iconpath).'</td>';
if($flag==1){	
$line.= '<td><button Id="Restarthttp"  class="btn btn-primary"  style="height:35px;width:140px;" >restart HTTP</button></td>';
$line.='</tr>';
}
$line .= '<tr><th>FTP</th><td>'   . module_controller::status_port(21, $iconpath) .'</td>';
if($flag==1){
$line .='<td><button Id="Restartftp" = class="btn btn-primary"  style="height:35px;width:140px;" >restart FTP</button></td>';
$line .='</tr>';
}
        $line .= '<tr><th>SMTP</th><td>'  . module_controller::status_port(25, $iconpath) .'</td><td><button Id="Restartsmtp"  class="btn btn-primary"  style="height:35px;width:140px;" >restart SMTP</button></td></tr>';
        $line .= '<tr><th>POP3</th><td>'  . module_controller::status_port(110, $iconpath).'</td><td><button Id="Restartpop3"  class="btn btn-primary"  style="height:35px;width:140px;" >restart POP3</button></td></tr>';
        $line .= '<tr><th>IMAP</th><td>'  . module_controller::status_port(143, $iconpath) .'</td><td><button Id="Restartimap"  class="btn btn-primary"  style="height:35px;width:140px;" >restart IMAP</button></td></tr>';
        $line .= '<tr><th>MySQL</th><td>' . module_controller::status_port(3306, $iconpath) .'</td><td><button Id="Restartmysql" class="btn btn-primary"  style="height:35px;width:140px;" >restart MYSQL</button></td></tr>';
        $line .= '<tr><th>DNS</th><td>'   . module_controller::status_port(53, $iconpath)  . '</td><td><button Id="Restardns"    class="btn btn-primary"  style="height:35px;width:140px;" >restart DNS</button></td></tr>';
        $line .= '</table>';
        $line .= '<br><h2>' . ui_language::translate('Server Uptime') . '</h2>';
        $line .= ui_language::translate('Uptime') . ": " . sys_monitoring::ServerUptime();
        return $line;
    }  

 static public function doservice()
    {
	global $zdbh;
        global $controller;
        $iconpath = '<img src="modules/' . $controller->GetControllerRequest('URL', 'module') . '/assets/';
        
        $line = "<h2>" . ui_language::translate("Checking status of services...") . "</h2>";
        $line .= "<table>";
        
        $status = fs_director::CheckForEmptyValue(sys_monitoring::PortStatus($PortNum));
	$flag=0;
	$currentuser = ctrl_users::GetUserDetail();
        if($currentuser['userid'] == 1)
         $flag=1;

$line .= '<tr><th>HTTP</th><td>'  . module_controller::status_port(80, $iconpath) . '</td>';
if($flag==1){
$line.='<td><button onClick="callhttp();" Id="Restarthttp"  class="btn btn-primary"  style="height:35px;width:140px;" >restart HTTP</button></td>';
$line.='</tr>';
}
$line .= '<tr><th>FTP</th><td>'   . module_controller::status_port(21, $iconpath).'</td>';
if($flag==1){
$line.='<td><button onClick="callftp();" Id="Restartftp" = class="btn btn-primary"  style="height:35px;width:140px;" >restart FTP</button></td>';
$line.='</tr>';
}
$line .= '<tr><th>SMTP</th><td>'  . module_controller::status_port(25, $iconpath).'</td>';
if($flag==1){
$line .='<td><button onClick="callsmtp();" Id="Restartsmtp"  class="btn btn-primary"  style="height:35px;width:140px;" >restart SMTP</button></td>';
$line .='</tr>';
}
$line .= '<tr><th>POP3</th><td>'  . module_controller::status_port(110, $iconpath).'</td>';
if($flag==1){
$line .='<td><button onClick="callpop3();"Id="Restartpop3"  class="btn btn-primary"  style="height:35px;width:140px;" >restart POP3</button></td>';
$line .='</tr>';
}
$line .= '<tr><th>IMAP</th><td>'  . module_controller::status_port(143, $iconpath).'</td>';
if($flag==1){
$line .='<td><button onClick="callimap();" Id="Restartimap"  class="btn btn-primary"  style="height:35px;width:140px;" >restart IMAP</button></td>';
$line .='</tr>';
}
$line .= '<tr><th>MySQL</th><td>' . module_controller::status_port(3306, $iconpath).'</td>';
if($flag==1){
$line .='<td><button onClick="callmysql();" Id="Restartmysql" class="btn btn-primary"  style="height:35px;width:140px;" >restart MYSQL</button></td>';
$line .='</tr>';
}
$line .= '<tr><th>DNS</th><td>'   . module_controller::status_port(53, $iconpath). '</td>';
if($flag==1){
$line .= '<td><button onClick="calldns();" Id="Restardns"    class="btn btn-primary"  style="height:35px;width:140px;" >restart DNS</button></td>';
$line .='</tr>';
}
        $line .= '</table>';
        $line .= '<br><h2>' . ui_language::translate('Server Uptime') . '</h2>';
        $line .= ui_language::translate('Uptime') . ": " . sys_monitoring::ServerUptime();
        echo $line;
exit;
    }

    static function getIsWebServerUp()
    {
        return sys_monitoring::PortStatus(80);
    }

    static function getIsMySQLUp()
    {
        return sys_monitoring::PortStatus(3306);
    }

    static function getIsFTPUp()
    {
        return sys_monitoring::PortStatus(21);
    }

    static function getIsSMTPUp()
    {
        return sys_monitoring::PortStatus(25);
    }

    static function getIsPOP3Up()
    {
        return sys_monitoring::PortStatus(110);
    }

    static function getIsIMAPUp()
    {
        return sys_monitoring::PortStatus(143);
    }

    static function getUptime()
    {
        return sys_monitoring::ServerUptime();
    }

    static function getLastRunTime()
    {
        return date(ctrl_options::GetSystemOption('sentora_df'), ctrl_options::GetSystemOption('daemon_lastrun'));
    }

    static function getNextRunTime()
    {
        $new_time = ctrl_options::GetSystemOption('daemon_lastrun') + ctrl_options::GetSystemOption('daemon_run_interval');
        return date(ctrl_options::GetSystemOption('sentora_df'), $new_time);
    }
    static function doRestartdns()
    {
               
                $service_port = 4444 ;
                $address = gethostbyname('localhost');
                $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
                if ($socket === false) {
                // echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
                self::$tryagain=true;
                return false;
                } 


                //  echo "Attempting to connect to '$address' on port '$service_port'...";
                $result = socket_connect($socket, $address, $service_port);
                if ($result === false) {
                //            echo "socket_connect() failed.\nReason: ($result) " . socket_strerror(socket_last_error($socket)) . "\n";
                self::$tryagain=true;
                return false;

                }
                socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array("sec" => 1, "usec" =>0));
				$in="command dnsrestart";
                 $out = '';
                socket_write($socket, $in, strlen($in));
                $output = "" ;
               
                 while(true)
                {
                sleep(5);
                $out = socket_read($socket, 2048);
                $output .= $out ;
                // echo "Output=".$out."\n\n" ;
                if ($out)
                break ;
                } 
                echo $output; 
	            socket_close($socket);
                exit;
                

	 self::$Ok=true;
	 return true ;  


    }
 static function gethelpicon()
{
    global $zdbh;
    global $controller;
    $temp=$controller->GetControllerRequest('URL','module') ;
    $val = '/assets/one.txt';
    $val1 = '/modules/';
   $name=file_get_contents("modules/$temp/assets/helpicon.txt");
   return $name;
}

 static function doRestarthttp()
    {
               
                $service_port = 4444 ;
                $address = gethostbyname('localhost');
                $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
                if ($socket === false) {
                // echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
                self::$tryagain=true;
                return false;
                } 


                //  echo "Attempting to connect to '$address' on port '$service_port'...";
                $result = socket_connect($socket, $address, $service_port);
                if ($result === false) {
                //            echo "socket_connect() failed.\nReason: ($result) " . socket_strerror(socket_last_error($socket)) . "\n";
                self::$tryagain=true;
                return false;

                }
                socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array("sec" => 1, "usec" =>0));
				$in="command httprestart";
                 $out = '';
                socket_write($socket, $in, strlen($in));
                $output = "" ;

                 while(true)
                {
                sleep(5);
                $out = socket_read($socket, 2048);
                $output .= $out ;
                // echo "Output=".$out."\n\n" ;
                if ($out)
                break ;
                } 
                echo $output; 
	            socket_close($socket);
                exit;
                

	 self::$Ok=true;
	 return true ;  


    }
 static function doRestartftp()
   {
            
                $service_port = 4444 ;
                $address = gethostbyname('localhost');
                $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
                if ($socket === false) {
                // echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
                self::$tryagain=true;
                return false;
                } 


                //  echo "Attempting to connect to '$address' on port '$service_port'...";
                $result = socket_connect($socket, $address, $service_port);
                if ($result === false) {
                //            echo "socket_connect() failed.\nReason: ($result) " . socket_strerror(socket_last_error($socket)) . "\n";
                self::$tryagain=true;
                return false;

                }
                socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array("sec" => 1, "usec" =>0));
				$in="command ftprestart";
                 $out = '';
                socket_write($socket, $in, strlen($in));
                $output = "" ;

                 while(true)
                {
                sleep(5);
                $out = socket_read($socket, 2048);
                $output .= $out ;
                // echo "Output=".$out."\n\n" ;
                if ($out)
                break ;
                } 
                echo $output; 
	            socket_close($socket);
                exit;
                

	 self::$Ok=true;
	 return true ;  


    }
 static function doRestartpop3()
    {
                
                $service_port = 4444 ;
                $address = gethostbyname('localhost');
                $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
                if ($socket === false) {
                // echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
                self::$tryagain=true;
                return false;
                } 


                //  echo "Attempting to connect to '$address' on port '$service_port'...";
                $result = socket_connect($socket, $address, $service_port);
                if ($result === false) {
                //            echo "socket_connect() failed.\nReason: ($result) " . socket_strerror(socket_last_error($socket)) . "\n";
                self::$tryagain=true;
                return false;

                }
                socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array("sec" => 1, "usec" =>0));
				$in="command pop3restart";
                 $out = '';
                socket_write($socket, $in, strlen($in));
                $output = "" ;
                while(true)
                {
                sleep(5);
                $out = socket_read($socket, 2048);
                $output .= $out ;
                // echo "Output=".$out."\n\n" ;
                if ($out)
                break ;
                } 
                echo $output; 
	            socket_close($socket);
                exit;
                

	 self::$Ok=true;
	 return true ;  


    }
 static function doRestartimap()
     {
             
                $service_port = 4444 ;
                $address = gethostbyname('localhost');
                $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
                if ($socket === false) {
                // echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
                self::$tryagain=true;
                return false;
                } 


                //  echo "Attempting to connect to '$address' on port '$service_port'...";
                $result = socket_connect($socket, $address, $service_port);
                if ($result === false) {
                //            echo "socket_connect() failed.\nReason: ($result) " . socket_strerror(socket_last_error($socket)) . "\n";
                self::$tryagain=true;
                return false;

                }
                socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array("sec" => 1, "usec" =>0));
				$in="command imaprestart";
                 $out = '';
                socket_write($socket, $in, strlen($in));
                $output = "" ;
                while(true)
                {
                sleep(5);
                $out = socket_read($socket, 2048);
                $output .= $out ;
                // echo "Output=".$out."\n\n" ;
                if ($out)
                break ;
                } 
                echo $output; 
	            socket_close($socket);
                exit;
                
    
	 self::$Ok=true;
	 return true ;  
     getIsIMAPUp();
     

    }
 static function doRestartmysql()
        {
             
                $service_port = 4444 ;
                $address = gethostbyname('localhost');
                $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
                if ($socket === false) {
                // echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
                self::$tryagain=true;
                return false;
                } 


                //  echo "Attempting to connect to '$address' on port '$service_port'...";
                $result = socket_connect($socket, $address, $service_port);
                if ($result === false) {
                //            echo "socket_connect() failed.\nReason: ($result) " . socket_strerror(socket_last_error($socket)) . "\n";
                self::$tryagain=true;
                return false;

                }
                socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array("sec" => 1, "usec" =>0));
				$in="command mysqlrestart";
                 $out = '';
                socket_write($socket, $in, strlen($in));
                $output = "" ;

                 while(true)
                {
                sleep(5);
                $out = socket_read($socket, 2048);
                $output .= $out ;
                // echo "Output=".$out."\n\n" ;
                if ($out)
                break ;
                } 
                echo $output; 
	            socket_close($socket);
                exit;
                

	 self::$Ok=true;
	 return true ;  


    }
 static function doRestartsmtp()
    {
               
                $service_port = 4444 ;
                $address = gethostbyname('localhost');
                $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
                if ($socket === false) {
                // echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
                self::$tryagain=true;
                return false;
                } 
            

                //  echo "Attempting to connect to '$address' on port '$service_port'...";
                $result = socket_connect($socket, $address, $service_port);
                if ($result === false) {
                //            echo "socket_connect() failed.\nReason: ($result) " . socket_strerror(socket_last_error($socket)) . "\n";
                self::$tryagain=true;
                return false;

                }
                socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array("sec" => 1, "usec" =>0));
				$in="command smtprestart";
                 $out = '';
                socket_write($socket, $in, strlen($in));
                $output = "" ;
                
                 while(true)
                {
                sleep(5);
                $out = socket_read($socket, 2048);
                $output .= $out ;
                // echo "Output=".$out."\n\n" ;
                if ($out)
                break ;
                } 
                echo $output; 
	            socket_close($socket);
                exit;
                

	 self::$Ok=true;
	 return true ;  


    }



function GetOutput()
{
//echo $output;
//echo "restarted successfully";
}
}