Current File : //etc/zpanel/panel/modules/phpmodule/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/>.
 *
 */
class module_controller extends ctrl_module
{
    static function n_sanitize($data)
    {
        $data = trim($data);
        $data = stripslashes($data);
        $data = htmlspecialchars($data);
        return $data;
    }

  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 dopackdetail()
    {
     	error_reporting(0);
        $currentuser = ctrl_users::GetUserDetail();
        
        
        if(array_key_exists("userid",$currentuser) && is_numeric($currentuser['userid']))
        {
        
            $filePath = stream_resolve_include_path('System.php');
            if ($filePath !== false)
            {
                require_once('System.php');
                if(class_exists('System')===true)
                {
                    if (function_exists('shell_exec'))
	                {
                        $c_ins_pack ="";
                        $c_ins_st = 0;
                        $status_array = array("memcache"=>0,"gender"=>0,"dbx"=>0,"radius"=>0,
                        "Rar"=>0,"runkit"=>0,"geoip"=>0,"gnupg"=>0,"imagick"=>0);
                        
                       $st_array = array("pecl"=>array("memcache","gender","dbx","radius","rar","runkit","geoip","gnupg"),"imagick"=>array("imagick"));
                        //$ext_array = get_loaded_extensions();
                        $nst_array = $st_array['pecl'];
                        $php_m = shell_exec('php -m');
                        $ext_array = explode("\n",$php_m);
                        $ext_array = array_map('strtolower',$ext_array);
                        
                        foreach($nst_array as $key)
                        {
                            if(in_array($key,$ext_array))
                            {
                                $status_array[$key] = 1; 
                            }
                        
                        }
                            if(in_array("imagick",$ext_array))
                            {
                                $status_array["imagick"] = 1; 
                            }
                        if (array_key_exists("pckg7",$_SESSION) &&  array_key_exists("inst7",$_SESSION)&& isset($_SESSION['pckg7'])  && isset($_SESSION["inst7"]) )
                        {
                             if($_SESSION['inst7'] == 2 )
                            {
                                if($status_array[$_SESSION['pckg7']] == 1)
                                {
                                
                                    unset($_SESSION['pckg7']);
                                    unset($_SESSION['inst7']);
                                    $c_ins_pack = $_SESSION['pckg7'];
                                    $c_ins_st = 3;
                                    
                                }
                                else
                                {
                                    $c_ins_pack = $_SESSION['pckg7'];
                                    $c_ins_st = $_SESSION['inst7'];
                                }
                            }
                            else
                            {
                                if($status_array[$_SESSION['pckg7']] == 0)
                                {
                                
                                    unset($_SESSION['pckg7']);
                                    unset($_SESSION['inst7']);
                                    $c_ins_pack = $_SESSION['pckg7'];
                                    $c_ins_st = 3;
                                    
                                }
                                else
                                {
                                    $c_ins_pack = $_SESSION['pckg7'];
                                    $c_ins_st = $_SESSION['inst7'];
                                }
                            
                            
                            }
                        }
                        
	                    $dis_array = array("status"=>1,"darray" =>$status_array,"sarray"=>$st_array,"c_p"=>$c_ins_pack,"c_s"=>$c_ins_st,"std"=>$ext_array);
                        echo json_encode($dis_array);
                    }
                    else
                    {
                       $dis_array = array("status"=>0,"msg" =>"Please tell your system admin to allow system operation" );
                        echo json_encode($dis_array);  
                    }
                
                }
                else
                {
                    $dis_array = array("status"=>0,"msg" =>"Please tell your system admin to install php-pear" );
                    echo json_encode($dis_array);   
                
                
                }
                
            }
            else
            {
                $dis_array = array("status"=>0,"msg" =>"Please tell your system admin to install php-pear" );
                echo json_encode($dis_array);
            }
        }
        else
        {
            $dis_array = array("status"=>2,"msg" =>"Current session expire please login again" );
            echo json_encode($dis_array);
        }
        exit;          
    }
    static function dopackinstall()
    {
        global $controller;
        $currentuser = ctrl_users::GetUserDetail();
        
        $formvars = $controller->GetAllControllerRequests('FORM');
        if(array_key_exists("userid",$currentuser) && is_numeric($currentuser['userid']))
        {
            if(array_key_exists("stv",$formvars)  && array_key_exists("packs",$formvars) && is_numeric($formvars['stv']) && preg_match("/^[a-zA-Z]+$/", $formvars['packs']))
            {
                $status_array = array("memcache","gender","dbx","radius","Rar","runkit","geoip","gnupg","imagick"); 
                $stv =self::n_sanitize($formvars['stv']);
                $packs =self::n_sanitize($formvars['packs']);
                $ins_array = array(1,2);
                if( in_array($stv,$ins_array))
                { 
                    if(array_key_exists($stv,$status_array))
                    {
                        $_SESSION['pckg7'] = $packs;
                        $_SESSION['inst7'] = $stv;
                        if($stv == 2 )
                        {
                        
                            self::call_socket(1,$packs);
                            $dis_array = array("status"=>1,"msg" =>"Please wait installation is going on ..." );
                            echo json_encode($dis_array);
                        }
                        else
                        {
                            self::call_socket(0,$packs); 
                            $dis_array = array("status"=>1,"msg" =>"Please wait uninstallation is going on ..." );
                            echo json_encode($dis_array);                      
                        }               
                    
                    }
                    else
                    {
                        $dis_array = array("status"=>0,"msg" =>"Please Provide proper data " );
                        echo json_encode($dis_array);          
                    
                    }
                }
                else
                {
                    $dis_array = array("status"=>0,"msg" =>"Please Provide proper data " );
                    echo json_encode($dis_array);
                }
                
            
            } 
            else
            {
                $dis_array = array("status"=>0,"msg" =>"Please Provide proper data " );
                echo json_encode($dis_array);
                
            }
        
        }
        else
        {
            $dis_array = array("status"=>2,"msg" =>"Current session expire please login again" );
            echo json_encode($dis_array);
        }  
        exit;
    }
    
    
    
     static function call_socket($inst,$packg)
    {


        $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));
        sleep(3);
            if($inst ==1)
            {
                $in="command phpm install ".$packg;
            }
            else
            {
                $in="command phpm uninstall ".$packg;
            }
        
        $out = '';
        socket_write($socket, $in, strlen($in));
        socket_close($socket);

    }
    
        

}