Mini Shell

Direktori : /usr/share/l.v.e-manager/ispmanager/liblve0.8/addon/lvemanager/
Upload File :
Current File : //usr/share/l.v.e-manager/ispmanager/liblve0.8/addon/lvemanager/preset.class.php

<?php

/**
 * Copyright © Cloud Linux GmbH & Cloud Linux Software, Inc 2010-2019 All Rights Reserved
 *
 * Licensed under CLOUD LINUX LICENSE AGREEMENT
 * http://cloudlinux.com/docs/LICENSE.TXT
 */
    
class LVEPreset {
    private $XML = array();
    
    public function __construct($XML = null) {
        // if simplexml extension dos not present, add it
        if ( !function_exists ( 'simplexml_load_string' ) )
        {
            dl ('simplexml.so');
        } // if
        $this->XML = @simplexml_load_string($XML);
    }
    
    public function PrintXml() {
//        global $file;
//        fwrite($file,PHP_EOL.'****'.PHP_EOL.$this->XML->asXML());
        echo $this->XML->asXML();
    }
    
    private function CommandExecute($command,$result = true) {
        if ($result) {
            ob_start();
            passthru($command);
            $return = ob_get_contents();
            ob_end_clean();
            return $return;
//            return explode(PHP_EOL,$return);
        } else {
            passthru($command);
            return true;
        }
    }

    public function get_cpu_num() {
        $numCpus = 1;
        if (is_file('/proc/cpuinfo')) {
            $cpuinfo = file_get_contents('/proc/cpuinfo');
            preg_match_all('/^processor/m', $cpuinfo, $matches);
            $numCpus = count($matches[0]);
        }
    return $numCpus;
    }

    
    // Return Memory select
    private function ShowMemList(&$XML, $default) {
        $return = '';
        $default = str_replace(' MB',' ',$default);
        $default = str_replace('*',' ',$default);
        $default = str_replace('M',' ',$default);
        $XML->addChild('msg',$default.' MB');
        $XML->addChild('msg',' Disabled');
        for ($i = 128; $i <= 16256; $i+=128) {
            if ($i != $default) {
                $XML->addChild('msg',$i.' MB');
            }
        }
    }
    
    // Get Default settings
    private function GetDefaultLVE() {
        $default = array();
        if (file_exists('/etc/container/ve.cfg')) {
            $xml = simplexml_load_file('/etc/container/ve.cfg');
                        
            $default['CPU'] = trim((string)$xml->defaults->cpu['limit']);
            if (is_numeric($default['CPU'])){
                $SYS_NCPU = $this->get_cpu_num();
                $default['CPU'] = ((string)((int)$default['CPU'] * $SYS_NCPU)).'%';
            }
            $default['NCPU'] = trim((string)$xml->defaults->ncpu['limit']);
            $default['MEM'] = trim((string)(round(((float)$xml->defaults->mem['limit']*4/1024),2).' MB'));
            $default['MEP'] = trim((string)($xml->defaults->other['maxentryprocs']));
        } else {
            return false;
        }
        return $default;
    }
    
    // Fill missed fields of Package settings with defaults
    private function FillPackageWithDefaults($package = array(), $defaults = array()) {
        foreach ($package as $key => $value) {
            if (trim($value) == '') {
                $package[$key] = $defaults[$key];
            }
        }
        return $package;
    }
    
    // Get Package LVE settings
    private function GetPackageLVE($Package) {
        $settings = array();
        if (file_exists('/etc/container/ve.cfg')) {
            $xml = simplexml_load_file('/etc/container/ve.cfg');
            foreach ($xml->package as $item) {
                if ($item['id'] == trim($Package)) {
                    $defaults = LVEPreset::GetDefaultLVE();
                    $settings['CPU'] = (string)$item->cpu['limit'];
                    if (is_numeric($settings['CPU'])){
                        $SYS_NCPU = $this->get_cpu_num();
                        $settings['CPU'] = ((string)((int)$settings['CPU'] * $SYS_NCPU)).'%';
                    }
                    $settings['NCPU'] = (string)$item->ncpu['limit'];
                    $settings['MEM'] = (string)$item->mem['limit'];
                    $settings['MEP'] = (string)$item->other['maxentryprocs'];
                    $settings['MEM'] = $settings['MEM']*4/1024;
                    $settings = LVEPreset::FillPackageWithDefaults($settings, $defaults);
                }
            }
            if (empty($settings)) {
                $settings = LVEPreset::GetDefaultLVE();
            }
        } else {
            $settings = LVEPreset::GetDefaultLVE();
        }
        return $settings;
    }
    
    // Get MEM select & package LVE limits
    public function GetMEMSelect($new = false) {
        // Default LVE settings
        if ($new) {
            $default = LVEPreset::GetDefaultLVE();
            $MEM = $this->XML->addChild('slist','');
            $MEM->addAttribute('name','MMEM');
            LVEPreset::ShowMemList($MEM,$default['MEM']);
        } else {
            // Package LVE Settings
            global $package;
            $PData = LVEPreset::GetPackageLVE($package);
            $MEM = $this->XML->addChild('slist','');
            $MEM->addAttribute('name','MMEM');
            LVEPreset::ShowMemList($MEM,$PData['MEM']);
            $this->XML->addChild('MCPU',$PData['CPU']);
            $this->XML->addChild('MNCPU',$PData['NCPU']);
            $this->XML->addChild('MEP',$PData['MEP']);
        }
    }
    
    //Create new package
    private function SetLVEPackage($data) {
        $default = LVEPreset::GetDefaultLVE();
        $request = '/usr/sbin/lvectl package-set "'.$data['name'].'" ';
        foreach ($data as $key => $value) {
            if ($value == 'default') {
                $data[$key] = $default[$key];
            }
        }    
        if ($data['CPU'] != 'default')
            $request .= ' --speed='.urldecode($data['CPU']);
        if ($data['NCPU'] != 'default')
            $request .= ' --ncpu='.$data['NCPU'];
        if ($data['MEP'] != 'default')
            $request .= ' --maxEntryProcs='.$data['MEP'];
        if (trim($data['MEM']) == 'Disabled')
            $data['MEM'] = 0;
        $request .= ' --vmem='.trim(str_replace('MB','',$data['MEM'])).'M';
        $file2 = fopen('/usr/local/ispmgr/addon/lvemanager/mini_kron.php',"w+");
        $request = str_replace("'","\'",$request);
        fwrite($file2,'<?php passthru(\''.$request.'\');');
        fclose($file2);
    }
    
    // Delete LVE package
    private function DeleteLVEPackage($Package) {
        LVEPreset::CommandExecute('/usr/sbin/lvectl package-delete '.$Package);
    }
    
    // Edit|Create LVE package limits
    public function EditLVEPackage() {
        if ((string)$this->XML->params->func == 'preset.delete') {
            LVEPreset::DeleteLVEPackage((string)$this->XML->params->elid);
        } else {
            $data = array();
            $data['elid'] = (string)$this->XML->params->elid;
            $data['name'] = (string)$this->XML->params->name;
            $data['CPU'] = (string)$this->XML->params->MCPU;
            $data['MEP'] = (string)$this->XML->params->MEP;
            $data['MEM'] = (string)$this->XML->params->MMEM;
            $data['NCPU'] = (string)$this->XML->params->MNCPU;
            
            LVEPreset::SetLVEPackage($data);
        }
    }
    
    // Add New User or modify exist
    public function AddUser() {
        if ((string)$this->XML->params->func == 'user.delete') {
            $uid = LVEPreset::CommandExecute('id '.(string)$this->XML->params->elid.' -u');
            LVEPreset::CommandExecute('/usr/sbin/lvectl destroy '.$uid);
            LVEPreset::CommandExecute('/usr/sbin/lvectl delete '.$uid);
        } else {
            // New User Create
            if ((string)$this->XML->params->elid == '') {
                $user = (string)$this->XML->params->name;
                // If not custom preset
                if ((string)$this->XML->params->preset != 'custom') {
                    $file2 = fopen('/usr/local/ispmgr/addon/lvemanager/mini_kron.php',"w+");
                    fwrite($file2,'<?php LVEPreset::ApplyUser(\''.$user.'\');');
                    fclose($file2);
                }
            // Exist User modify    
            } else {
               $user = (string)$this->XML->params->elid;
               // If not custom preset
               if ((string)$this->XML->params->preset != 'custom') {
                   $file2 = fopen('/usr/local/ispmgr/addon/lvemanager/mini_kron.php',"w+");
                   fwrite($file2,'<?php LVEPreset::ApplyUser(\''.$user.'\');');
                   fclose($file2);
               }
            }
//            fwrite($file,PHP_EOL.$this->XML->asXML());
        }
    }
    
    // Static void to apply user settings
    public static function ApplyUser($user) {
        $uid = LVEPreset::CommandExecute('id '.trim($user).' -u');
        LVEPreset::CommandExecute('/usr/sbin/lvectl apply '.trim($uid));
    }
}

Zerion Mini Shell 1.0