Your IP : 216.73.216.86


Current Path : /home/emeraadmin/www/4d695/
Upload File :
Current File : /home/emeraadmin/www/4d695/lve.tar

modlscapi/buildlsphp/WhmMod_LiteSpeed_BuildPHP.php000064400000014375151677542400016243 0ustar00<?php
/********************************************
 * LiteSpeed web server Plugin for WHM
 * @Author:   Lite Speed Technologies, Inc. (http://www.litespeedtech.com)
 * @Copyright: (c) 2013
 *********************************************/

define ('PHP_AP_LOC', '/usr/local/bin/php');
define ('EASYAPACHE_BUILD_DIR_CP', '/home/cpeasyapache/src');
define ('EASYAPACHE_BUILD_DIR_LS', '/home/cpeasyapache/ls.src');

class WhmMod_LiteSpeed_BuildPHP
{
	
	private function get_php_config($php_binary)
	{
		putenv( "ACCEPT_ENCODING" );
		putenv( "HTTP_ACCEPT_ENCODING" );
		putenv( "SCRIPT_FILENAME" );
		putenv( "REDIRECT_STATUS" );
		putenv( "SERVER_SOFTWARE" );
		putenv( "SERVER_NAME" );
		putenv( "GATEWAY_INTERFACE" );
		putenv( "REQUEST_METHOD" );

		$extract_values = array();
		exec("$php_binary -c ./php.ini -i", $output, $returns);
		if ($returns > 10) {
			$extract_values['error'] =  "ERROR: $php_binary -i returns $returns <br>" . implode('<br>', $output);
			return $extract_values;
		}

		foreach($output as $line) {
			//PHP Version 4.4.7
			//<tr><td class="e">Configure Command </td><td class="v"> &#039;./configure&#039; </td></tr>
			//PHP Version => 4.4.8
			//Configure Command =>  './configure' '--enable-bcmath' '--enable-calendar' '--enable-ftp' '--enable-gd

			if (preg_match("/PHP Version [^\d]*([\d.]+)/", $line, $matches)) {
				$extract_values['PHP_VERSION'] = $matches[1];
			}

			if (strpos($line, 'Configure Command') !== FALSE) {
				# echo "\n\nA: $line\n";
				// before 4.4.8
				trim($line);
				$line = str_replace('&#039;', "'", $line);
				$start_pos = strpos($line, "./configure");
				if ($start_pos === FALSE) { // should not happen
					$extract_values['error'] = 'Cannot find configure options!';
					return $extract_values;
				}
				if ($line[$start_pos-1] == "'")
				-- $start_pos; //including the quote
				$line = substr($line, $start_pos);
				if (($end_pos = strpos($line, '</td>')) !== FALSE) {
					$line = substr($line, 0, $end_pos);
				}

				// remove unused params: '--with-apxs2=/usr/local/apache/bin/apxs'
				$pattern_unused = '/\'?--with-apxs[^ \']+\'? ?/';
				$line = preg_replace($pattern_unused, '', $line);

				$pattern_unused = '/\'?--enable-fastcgi\'? ?/';
				$line = preg_replace($pattern_unused, '', $line);

				if (strpos($line, '--with-litespeed') === FALSE) {
					//find last param, see if there's missing quote
					$line = trim($line);
					if (substr($line,-1) != "'" ) {
						$pos0 = strrpos($line, '--');
						if ($pos0 > 1 && (substr($line, $pos0 -1, 1) == "'")) {
							$line .= "'";
						}
					}
					$line .= " '--with-litespeed'";
				}

				$extract_values['PHP_CONF_OPTIONS'] = $line;
				return $extract_values;
			}
		}
		$extract_values['error'] = 'Cannot find configuration options!';
		if (count($output) < 10) {
			$extract_values['error'] .= "ERROR: $php_binary -i returns $returns <br>" . implode('<br>', $output);
		}
		return $extract_values;
	}

	public function GenerateBuildScript($base_php_binary, $path_to_bld = "")
	{
		$result = array();
		//get env
		$php_options = $this->get_php_config($base_php_binary);
		if (isset($php_options['error'])) {
			$result['error'] = $php_options['error'];
			return $result;
		}

		$php_options['PHP_BUILD_DIR'] = EASYAPACHE_BUILD_DIR_LS;
		$php_options['CP_BUILD_DIR'] = $path_to_bld.EASYAPACHE_BUILD_DIR_CP;
		$php_options['LSWS_HOME'] = LSWS_HOME;

		if ( !file_exists(WGET_TEMP) && !mkdir(WGET_TEMP, 0700, true) ) {
			$result['error'] = 'Cannot create temp build directory ' . WGET_TEMP;
			return $result;
		} 
			
		//generate shell script
		$template = './php_build.template';
		$sh_template = file_get_contents($template);
		if ($sh_template === false) {
			$result['error'] = 'failed to read file: ' . $template;
			return $result;
		}

		$search = array('{PHP_BUILD_DIR}',
			'{CP_BUILD_DIR}',
			'{PHP_VERSION}', 
			'{PHP_CONF_OPTIONS}',
			'{LSWS_HOME}');
		$replace = array($php_options['PHP_BUILD_DIR'],
		$php_options['CP_BUILD_DIR'],
		$php_options['PHP_VERSION'],
		$php_options['PHP_CONF_OPTIONS'],
		$php_options['LSWS_HOME']);
		$sh_script = str_replace($search, $replace, $sh_template);

		$file_name = WGET_TEMP . '/build_lsphp_' . $php_options['PHP_VERSION'] . '.sh';
		
		if ( file_put_contents($file_name, $sh_script) === FALSE) {
			$result['error'] = 'Failed to create build script: ' . $file_name;
			return $result;
		}

		if ( chmod($file_name, 0700) == FALSE) {
			$result['error'] = 'Failed to chmod for ' . $file_name;
			return $result;
		}

		$result['file'] = $file_name;
		return $result;
	}

	function PrecheckPHPVersion($PHP_BINARY_LOC)
	{
		// return 0: same, 1 different; 2 error stop;
		$result = array('msg' => '', 'next' => 1);

		$apache_php = $this->get_php_config($PHP_BINARY_LOC);
		if (isset($apache_php['error'])) {
			$result['msg'] .= $apache_php['error'];
			$result['next'] = 2;
			return $result;
		}		
		$result['msg'] .= "Found Apache PHP binary at $PHP_BINARY_LOC  Version is {$apache_php['PHP_VERSION']} \n";
		$main_version = $apache_php['PHP_VERSION'][0];

		$lsphp_binary = LSWS_HOME . '/fcgi-bin/lsphp' . $main_version;
		if (!file_exists($lsphp_binary)) {
			$result['msg'] .= "{$lsphp_binary} does not exist! \n";
			return $result;
		}

		$ls_php = $this->get_php_config($lsphp_binary);
		if (isset($ls_php['error'])) {
			$result['msg'] .= "{$lsphp_binary} exists, but not runnable: {$ls_php['error']} \n";
			return $result;
		}
		$result['msg'] .= "Found LiteSpeed PHP binary at {$lsphp_binary}. Version is {$ls_php['PHP_VERSION']} \n";

		if ( $apache_php['PHP_VERSION'] != $ls_php['PHP_VERSION'] ) {
			$result['msg'] .= "PHP binary has different version! \n";
			return $result;
		}

		//compare conf
		$apache_options = preg_split("/[' ]/", $apache_php['PHP_CONF_OPTIONS'], -1, PREG_SPLIT_NO_EMPTY);
		$ls_options = preg_split("/[' ]/", $ls_php['PHP_CONF_OPTIONS'], -1, PREG_SPLIT_NO_EMPTY);
		$conf_diff = '';
		foreach( $apache_options as $ai) {
			if (!in_array($ai, $ls_options)) {
				$conf_diff .= " $ai";
			}
		}
		if ($conf_diff == '') {
			$result['msg'] .= "Configuration options for lsphp and Apache PHP are matched.\n";
			$result['next'] = 0; // same, no action needed.
			return $result;
		}
		else {
			$result['msg'] .= "Apache PHP configuration has more options that are not available or different in lsphp: $conf_diff \n";
			return $result;
		}

	}


}



modlscapi/buildlsphp/cmd_buildmatchingphp.php000064400000004366151677542400015567 0ustar00<?php

/******************************
 * cmd_buildmatchingphp.php
 *
 * Options:
 * 		-f force build even it is matched
 * 		-v verify version and config options, no actual build
 * 		default no option: build the matching php if there is difference.
 *
 * Command line tool to build Apache matching PHP.
 * This requires that existing php has been successfully built by whm EasyApache tool.
 * You can make this as part of your automated server installation script after EasyApache build done.
 *
 ******************************/

ini_set('max_execution_time', 7200);

require_once('WhmMod_LiteSpeed_BuildPHP.php');

define ('PHP_AP_LOC', '/usr/local/bin/php');	// Apache PHP location
define ('LSWS_HOME', '/usr/local/lsws');

define( 'CGI_DIR', '/usr/local/cpanel/whostmgr/docroot/cgi/lsws' );
define( 'WGET_TEMP', '/usr/src/lsws' );
$path_to_bld = "";

$buildtool = new WhmMod_LiteSpeed_BuildPHP();

$next = $buildtool->PrecheckPHPVersion(PHP_AP_LOC, $msg);
echo "$msg \n";

// return 0: same, 1 different; 2 error stop;
$do_build = FALSE;

if ($next == 2) {
	echo "Please make sure PHP exists, you can specify a different location by editing variable definition PHP_AP_LOC \n";
}
elseif (isset($argv[1]) && $argv[1] == '-v') {
	echo "Verify only - Exit!\n";
}
elseif ($next == 0) {
	if (isset($argv[1]) && $argv[1] == '-f') {
		echo "Config option is same, will do force rebuild matching php";
		$do_build = TRUE;
	}
	else {
		echo "No need to rebuild. Aborted! If you want to do force rebuid, add parameter -f \n";
	}
}
else { // next == 1
	$do_build = TRUE;
}

if ((isset($argv[1])) && ($argv[1] != '-f') && ($argv[1] != '-v')) {
    $path_to_bld = trim($argv[1]);
}

if (!$do_build)
	exit;

echo "Next will build lsphp using the same configuration as Apache's php.
	This will download the source code and build php binary. The whole process may take more than 10 minutes, please be patient.\n";

$curdir = getcwd();
if ($curdir != CGI_DIR) {
	echo "change current directory to " . CGI_DIR . "\n";
	chdir(CGI_DIR);
}

$result = $buildtool->GenerateBuildScript(PHP_AP_LOC, $path_to_bld);
if (isset($result['error'])) {
	echo "{$result['error']} \n Aborted!\n";
	return false;
}

$build_script = $result['file'];

echo "build script generated at $build_script \n";

system($build_script);

modlscapi/buildlsphp/php.ini000064400000112315151677542400012173 0ustar00[PHP]

;;;;;;;;;;;
; WARNING ;
;;;;;;;;;;;
; This is the default settings file for new PHP installations.
; By default, PHP installs itself with a configuration suitable for
; development purposes, and *NOT* for production purposes.
; For several security-oriented considerations that should be taken
; before going online with your site, please consult php.ini-recommended
; and http://php.net/manual/en/security.php.


;;;;;;;;;;;;;;;;;;;
; About this file ;
;;;;;;;;;;;;;;;;;;;
; This file controls many aspects of PHP's behavior.  In order for PHP to
; read it, it must be named 'php.ini'.  PHP looks for it in the current
; working directory, in the path designated by the environment variable
; PHPRC, and in the path that was defined in compile time (in that order).
; Under Windows, the compile-time path is the Windows directory.  The
; path in which the php.ini file is looked for can be overridden using
; the -c argument in command line mode.
;
; The syntax of the file is extremely simple.  Whitespace and Lines
; beginning with a semicolon are silently ignored (as you probably guessed).
; Section headers (e.g. [Foo]) are also silently ignored, even though
; they might mean something in the future.
;
; Directives are specified using the following syntax:
; directive = value
; Directive names are *case sensitive* - foo=bar is different from FOO=bar.
;
; The value can be a string, a number, a PHP constant (e.g. E_ALL or M_PI), one
; of the INI constants (On, Off, True, False, Yes, No and None) or an expression
; (e.g. E_ALL & ~E_NOTICE), or a quoted string ("foo").
;
; Expressions in the INI file are limited to bitwise operators and parentheses:
; |        bitwise OR
; &        bitwise AND
; ~        bitwise NOT
; !        boolean NOT
;
; Boolean flags can be turned on using the values 1, On, True or Yes.
; They can be turned off using the values 0, Off, False or No.
;
; An empty string can be denoted by simply not writing anything after the equal
; sign, or by using the None keyword:
;
;  foo =         ; sets foo to an empty string
;  foo = none    ; sets foo to an empty string
;  foo = "none"  ; sets foo to the string 'none'
;
; If you use constants in your value, and these constants belong to a
; dynamically loaded extension (either a PHP extension or a Zend extension),
; you may only use these constants *after* the line that loads the extension.
;
; All the values in the php.ini-dist file correspond to the builtin
; defaults (that is, if no php.ini is used, or if you delete these lines,
; the builtin defaults will be identical).


;;;;;;;;;;;;;;;;;;;;
; Language Options ;
;;;;;;;;;;;;;;;;;;;;

; Enable the PHP scripting language engine under Apache.
engine = On

; Allow the <? tag.  Otherwise, only <?php and <script> tags are recognized.  
; NOTE: Using short tags should be avoided when developing applications or
; libraries that are meant for redistribution, or deployment on PHP
; servers which are not under your control, because short tags may not
; be supported on the target server. For portable, redistributable code,
; be sure not to use short tags.
short_open_tag = On

; Allow ASP-style <% %> tags.
asp_tags = Off

; The number of significant digits displayed in floating point numbers.
precision    =  12

; Enforce year 2000 compliance (will cause problems with non-compliant browsers)
;y2k_compliance = On

; Output buffering allows you to send header lines (including cookies) even
; after you send body content, at the price of slowing PHP's output layer a
; bit.  You can enable output buffering during runtime by calling the output
; buffering functions.  You can also enable output buffering for all files by
; setting this directive to On.  If you wish to limit the size of the buffer
; to a certain size - you can use a maximum number of bytes instead of 'On', as
; a value for this directive (e.g., output_buffering=4096).
output_buffering = Off

; You can redirect all of the output of your scripts to a function.  For
; example, if you set output_handler to "mb_output_handler", character
; encoding will be transparently converted to the specified encoding.
; Setting any output handler automatically turns on output buffering.
; Note: People who wrote portable scripts should not depend on this ini
;       directive. Instead, explicitly set the output handler using ob_start().
;       Using this ini directive may cause problems unless you know what script 
;       is doing.
; Note: You cannot use both "mb_output_handler" with "ob_iconv_handler"
;       and you cannot use both "ob_gzhandler" and "zlib.output_compression". 
;output_handler =

; Transparent output compression using the zlib library
; Valid values for this option are 'off', 'on', or a specific buffer size
; to be used for compression (default is 4KB)
; Note: Resulting chunk size may vary due to nature of compression. PHP 
;       outputs chunks that are few hundreds bytes each as a result of 
;       compression. If you prefer a larger chunk size for better 
;       performance, enable output_buffering in addition.
; Note: You need to use zlib.output_handler instead of the standard
;       output_handler, or otherwise the output will be corrupted.
zlib.output_compression = Off

; You cannot specify additional output handlers if zlib.output_compression
; is activated here. This setting does the same as output_handler but in
; a different order.
;zlib.output_handler =

; Implicit flush tells PHP to tell the output layer to flush itself
; automatically after every output block.  This is equivalent to calling the
; PHP function flush() after each and every call to print() or echo() and each
; and every HTML block.  Turning this option on has serious performance
; implications and is generally recommended for debugging purposes only.
implicit_flush = Off

; The unserialize callback function will be called (with the undefined class'
; name as parameter), if the unserializer finds an undefined class
; which should be instanciated.
; A warning appears if the specified function is not defined, or if the
; function doesn't include/implement the missing class.
; So only set this entry, if you really want to implement such a 
; callback-function.
unserialize_callback_func=

; When floats & doubles are serialized store serialize_precision significant
; digits after the floating point. The default value ensures that when floats
; are decoded with unserialize, the data will remain the same.
serialize_precision = 100

; Safe Mode
;
safe_mode = Off

; By default, Safe Mode does a UID compare check when
; opening files. If you want to relax this to a GID compare,
; then turn on safe_mode_gid.
safe_mode_gid = Off

; When safe_mode is on, UID/GID checks are bypassed when
; including files from this directory and its subdirectories.
; (directory must also be in include_path or full path must
; be used when including)
;safe_mode_include_dir =								

; When safe_mode is on, only executables located in the safe_mode_exec_dir
; will be allowed to be executed via the exec family of functions.
;safe_mode_exec_dir =

; Setting certain environment variables may be a potential security breach.
; This directive contains a comma-delimited list of prefixes.  In Safe Mode,
; the user may only alter environment variables whose names begin with the
; prefixes supplied here.  By default, users will only be able to set
; environment variables that begin with PHP_ (e.g. PHP_FOO=BAR).
;
; Note:  If this directive is empty, PHP will let the user modify ANY
; environment variable!
;safe_mode_allowed_env_vars = PHP_

; This directive contains a comma-delimited list of environment variables that
; the end user won't be able to change using putenv().  These variables will be
; protected even if safe_mode_allowed_env_vars is set to allow to change them.
;safe_mode_protected_env_vars = LD_LIBRARY_PATH

; open_basedir, if set, limits all file operations to the defined directory
; and below.  This directive makes most sense if used in a per-directory
; or per-virtualhost web server configuration file. This directive is
; *NOT* affected by whether Safe Mode is turned On or Off.
;open_basedir =

; This directive allows you to disable certain functions for security reasons.
; It receives a comma-delimited list of function names. This directive is
; *NOT* affected by whether Safe Mode is turned On or Off.
disable_functions =

; This directive allows you to disable certain classes for security reasons.
; It receives a comma-delimited list of class names. This directive is
; *NOT* affected by whether Safe Mode is turned On or Off.
disable_classes =

; Colors for Syntax Highlighting mode.  Anything that's acceptable in
; <font color="??????"> would work.
;highlight.string  = #DD0000
;highlight.comment = #FF9900
;highlight.keyword = #007700
;highlight.bg      = #FFFFFF
;highlight.default = #0000BB
;highlight.html    = #000000


;
; Misc
;
; Decides whether PHP may expose the fact that it is installed on the server
; (e.g. by adding its signature to the Web server header).  It is no security
; threat in any way, but it makes it possible to determine whether you use PHP
; on your server or not.
expose_php = On


;;;;;;;;;;;;;;;;;;;
; Resource Limits ;
;;;;;;;;;;;;;;;;;;;

max_execution_time = 30     ; Maximum execution time of each script, in seconds
max_input_time = 60	; Maximum amount of time each script may spend parsing request data
max_input_vars = 1000
memory_limit = 32M      ; Maximum amount of memory a script may consume (32MB)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Error handling and logging ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

; error_reporting is a bit-field.  Or each number up to get desired error
; reporting level
; E_ALL             - All errors and warnings
; E_ERROR           - fatal run-time errors
; E_WARNING         - run-time warnings (non-fatal errors)
; E_PARSE           - compile-time parse errors
; E_NOTICE          - run-time notices (these are warnings which often result
;                     from a bug in your code, but it's possible that it was
;                     intentional (e.g., using an uninitialized variable and
;                     relying on the fact it's automatically initialized to an
;                     empty string)
; E_CORE_ERROR      - fatal errors that occur during PHP's initial startup
; E_CORE_WARNING    - warnings (non-fatal errors) that occur during PHP's
;                     initial startup
; E_COMPILE_ERROR   - fatal compile-time errors
; E_COMPILE_WARNING - compile-time warnings (non-fatal errors)
; E_USER_ERROR      - user-generated error message
; E_USER_WARNING    - user-generated warning message
; E_USER_NOTICE     - user-generated notice message
;
; Examples:
;
;   - Show all errors, except for notices
;
;error_reporting = E_ALL & ~E_NOTICE
;
;   - Show only errors
;
;error_reporting = E_COMPILE_ERROR|E_ERROR|E_CORE_ERROR
;
;   - Show all errors except for notices
;
error_reporting = E_ALL & ~E_NOTICE & ~E_DEPRECATED

; Print out errors (as a part of the output).  For production web sites,
; you're strongly encouraged to turn this feature off, and use error logging
; instead (see below).  Keeping display_errors enabled on a production web site
; may reveal security information to end users, such as file paths on your Web
; server, your database schema or other information.
display_errors = On

; Even when display_errors is on, errors that occur during PHP's startup
; sequence are not displayed.  It's strongly recommended to keep
; display_startup_errors off, except for when debugging.
display_startup_errors = Off

; Log errors into a log file (server-specific log, stderr, or error_log (below))
; As stated above, you're strongly advised to use error logging in place of
; error displaying on production web sites.
log_errors = On

; Set maximum length of log_errors. In error_log information about the source is
; added. The default is 1024 and 0 allows to not apply any maximum length at all.
log_errors_max_len = 1024

; Do not log repeated messages. Repeated errors must occur in same file on same
; line until ignore_repeated_source is set true.
ignore_repeated_errors = Off

; Ignore source of message when ignoring repeated messages. When this setting 
; is On you will not log errors with repeated messages from different files or
; sourcelines.
ignore_repeated_source = Off

; If this parameter is set to Off, then memory leaks will not be shown (on
; stdout or in the log). This has only effect in a debug compile, and if 
; error reporting includes E_WARNING in the allowed list
report_memleaks = On

; Store the last error/warning message in $php_errormsg (boolean).
track_errors = Off

; Disable the inclusion of HTML tags in error messages.
;html_errors = Off
  
; If html_errors is set On PHP produces clickable error messages that direct 
; to a page describing the error or function causing the error in detail.
; You can download a copy of the PHP manual from http://www.php.net/docs.php 
; and change docref_root to the base URL of your local copy including the
; leading '/'. You must also specify the file extension being used including 
; the dot.
;docref_root = "/phpmanual/"
;docref_ext = .html
  
; String to output before an error message.
;error_prepend_string = "<font color=ff0000>"

; String to output after an error message.
;error_append_string = "</font>"

; Log errors to specified file.
error_log = error_log

; Log errors to syslog (Event Log on NT, not valid in Windows 95).
;error_log = syslog


;;;;;;;;;;;;;;;;;
; Data Handling ;
;;;;;;;;;;;;;;;;;
;
; Note - track_vars is ALWAYS enabled as of PHP 4.0.3

; The separator used in PHP generated URLs to separate arguments.
; Default is "&". 
;arg_separator.output = "&amp;"

; List of separator(s) used by PHP to parse input URLs into variables.
; Default is "&". 
; NOTE: Every character in this directive is considered as separator!
;arg_separator.input = ";&"

; This directive describes the order in which PHP registers GET, POST, Cookie,
; Environment and Built-in variables (G, P, C, E & S respectively, often
; referred to as EGPCS or GPC).  Registration is done from left to right, newer
; values override older values.
variables_order = "EGPCS"

; Whether or not to register the EGPCS variables as global variables.  You may
; want to turn this off if you don't want to clutter your scripts' global scope
; with user data.  This makes most sense when coupled with track_vars - in which
; case you can access all of the GPC variables through the $HTTP_*_VARS[],
; variables.
;
; You should do your best to write your scripts so that they do not require
; register_globals to be on;  Using form variables as globals can easily lead
; to possible security problems, if the code is not very well thought of.
;register_globals = Off

; This directive tells PHP whether to declare the argv&argc variables (that
; would contain the GET information).  If you don't use these variables, you
; should turn it off for increased performance.
register_argc_argv = On

; Maximum size of POST data that PHP will accept.
post_max_size = 8M

; This directive is deprecated.  Use variables_order instead.
gpc_order = "GPC"

; Magic quotes
;

; Magic quotes for incoming GET/POST/Cookie data.
;magic_quotes_gpc = Off

; Magic quotes for runtime-generated data, e.g. data from SQL, from exec(), etc.
;magic_quotes_runtime = Off    

; Use Sybase-style magic quotes (escape ' with '' instead of \').
;magic_quotes_sybase = Off

; Automatically add files before or after any PHP document.
auto_prepend_file =
auto_append_file =

; As of 4.0b4, PHP always outputs a character encoding by default in
; the Content-type: header.  To disable sending of the charset, simply
; set it to be empty.
;
; PHP's built-in default is text/html
default_mimetype = "text/html"
;default_charset = "iso-8859-1"

; Always populate the $HTTP_RAW_POST_DATA variable.
;always_populate_raw_post_data = On


;;;;;;;;;;;;;;;;;;;;;;;;;
; Paths and Directories ;
;;;;;;;;;;;;;;;;;;;;;;;;;

; UNIX: "/path1:/path2"  
include_path = ".:/usr/lib/php:/usr/local/lib/php"
;
; Windows: "\path1;\path2"
;include_path = ".;c:\php\includes"

; The root of the PHP pages, used only if nonempty.
; if PHP was not compiled with FORCE_REDIRECT, you SHOULD set doc_root
; if you are running php as a CGI under any web server (other than IIS)
; see documentation for security issues.  The alternate is to use the
; cgi.force_redirect configuration below
doc_root =

; The directory under which PHP opens the script using /~username used only
; if nonempty.
user_dir =

; Directory in which the loadable extensions (modules) reside.
;extension_dir = "/usr/local/lib/php/extensions/no-debug-non-zts-20100525"

; Whether or not to enable the dl() function.  The dl() function does NOT work
; properly in multithreaded servers, such as IIS or Zeus, and is automatically
; disabled on them.
enable_dl = On

; cgi.force_redirect is necessary to provide security running PHP as a CGI under
; most web servers.  Left undefined, PHP turns this on by default.  You can
; turn it off here AT YOUR OWN RISK
; **You CAN safely turn this off for IIS, in fact, you MUST.**
; cgi.force_redirect = 1

; if cgi.nph is enabled it will force cgi to always sent Status: 200 with
; every request.
; cgi.nph = 1

; if cgi.force_redirect is turned on, and you are not running under Apache or Netscape 
; (iPlanet) web servers, you MAY need to set an environment variable name that PHP
; will look for to know it is OK to continue execution.  Setting this variable MAY
; cause security issues, KNOW WHAT YOU ARE DOING FIRST.
; cgi.redirect_status_env = ;

; cgi.fix_pathinfo provides *real* PATH_INFO/PATH_TRANSLATED support for CGI.  PHP's
; previous behaviour was to set PATH_TRANSLATED to SCRIPT_FILENAME, and to not grok
; what PATH_INFO is.  For more information on PATH_INFO, see the cgi specs.  Setting
; this to 1 will cause PHP CGI to fix it's paths to conform to the spec.  A setting
; of zero causes PHP to behave as before.  Default is zero.  You should fix your scripts
; to use SCRIPT_FILENAME rather than PATH_TRANSLATED.
; cgi.fix_pathinfo=0

; FastCGI under IIS (on WINNT based OS) supports the ability to impersonate
; security tokens of the calling client.  This allows IIS to define the
; security context that the request runs under.  mod_fastcgi under Apache
; does not currently support this feature (03/17/2002)
; Set to 1 if running under IIS.  Default is zero.
; fastcgi.impersonate = 1;

; cgi.rfc2616_headers configuration option tells PHP what type of headers to
; use when sending HTTP response code. If it's set 0 PHP sends Status: header that
; is supported by Apache. When this option is set to 1 PHP will send
; RFC2616 compliant header.
; Default is zero.
;cgi.rfc2616_headers = 0 
 

;;;;;;;;;;;;;;;;
; File Uploads ;
;;;;;;;;;;;;;;;;

; Whether to allow HTTP file uploads.
file_uploads = On

; Temporary directory for HTTP uploaded files (will use system default if not
; specified).
;upload_tmp_dir =

; Maximum allowed size for uploaded files.
upload_max_filesize = 2M


;;;;;;;;;;;;;;;;;;
; Fopen wrappers ;
;;;;;;;;;;;;;;;;;;

; Whether to allow the treatment of URLs (like http:// or ftp://) as files.
allow_url_fopen = On

; Define the anonymous ftp password (your email address)
;from="john@doe.com"

; Define the User-Agent string
; user_agent="PHP"

; Default timeout for socket based streams (seconds)
default_socket_timeout = 60

; If your scripts have to deal with files from Macintosh systems,
; or you are running on a Mac and need to deal with files from
; unix or win32 systems, setting this flag will cause PHP to
; automatically detect the EOL character in those files so that
; fgets() and file() will work regardless of the source of the file.
; auto_detect_line_endings = Off


;;;;;;;;;;;;;;;;;;;;;;
; Dynamic Extensions ;
;;;;;;;;;;;;;;;;;;;;;;
;
; If you wish to have an extension loaded automatically, use the following
; syntax:
;
;   extension=modulename.extension
;
; For example, on Windows:
;
;   extension=msql.dll
;
; ... or under UNIX:
;
;   extension=msql.so
;
; Note that it should be the name of the module only; no directory information 
; needs to go here.  Specify the location of the extension with the
; extension_dir directive above.


;Windows Extensions
;Note that MySQL and ODBC support is now built in, so no dll is needed for it.
;
;extension=php_bz2.dll
;extension=php_cpdf.dll
;extension=php_crack.dll
;extension=php_curl.dll
;extension=php_db.dll
;extension=php_dba.dll
;extension=php_dbase.dll
;extension=php_dbx.dll
;extension=php_domxml.dll
;extension=php_exif.dll
;extension=php_fdf.dll
;extension=php_filepro.dll
;extension=php_gd2.dll
;extension=php_gettext.dll
;extension=php_hyperwave.dll
;extension=php_iconv.dll
;extension=php_ifx.dll
;extension=php_iisfunc.dll
;extension=php_imap.dll
;extension=php_interbase.dll
;extension=php_java.dll
;extension=php_ldap.dll
;extension=php_mbstring.dll
;extension=php_mcrypt.dll
;extension=php_mhash.dll
;extension=php_mime_magic.dll
;extension=php_ming.dll
;extension=php_mssql.dll
;extension=php_msql.dll
;extension=php_oci8.dll
;extension=php_openssl.dll
;extension=php_oracle.dll
;extension=php_pdf.dll
;extension=php_pgsql.dll
;extension=php_printer.dll
;extension=php_shmop.dll
;extension=php_snmp.dll
;extension=php_sockets.dll
;extension=php_sybase_ct.dll
;extension=php_w32api.dll
;extension=php_xmlrpc.dll
;extension=php_xslt.dll
;extension=php_yaz.dll
;extension=php_zip.dll


;;;;;;;;;;;;;;;;;;;
; Module Settings ;
;;;;;;;;;;;;;;;;;;;

[Syslog]
; Whether or not to define the various syslog variables (e.g. $LOG_PID,
; $LOG_CRON, etc.).  Turning it off is a good idea performance-wise.  In
; runtime, you can define these variables by calling define_syslog_variables().
;define_syslog_variables  = Off

[mail function]
; For Win32 only.
;SMTP = localhost
smtp_port = 25

; For Win32 only.
;sendmail_from = me@localhost.com

; For Unix only.  You may supply arguments as well (default: "sendmail -t -i").
sendmail_path = "/usr/sbin/sendmail -t -i"

[Java]
;java.class.path = .\php_java.jar
;java.home = c:\jdk
;java.library = c:\jdk\jre\bin\hotspot\jvm.dll 
;java.library.path = .\

[SQL]
sql.safe_mode = Off

[ODBC]
;odbc.default_db    =  Not yet implemented
;odbc.default_user  =  Not yet implemented
;odbc.default_pw    =  Not yet implemented

; Allow or prevent persistent links.
odbc.allow_persistent = On

; Check that a connection is still valid before reuse.
odbc.check_persistent = On

; Maximum number of persistent links.  -1 means no limit.
odbc.max_persistent = -1

; Maximum number of links (persistent + non-persistent).  -1 means no limit.
odbc.max_links = -1  

; Handling of LONG fields.  Returns number of bytes to variables.  0 means
; passthru.
odbc.defaultlrl = 4096  

; Handling of binary data.  0 means passthru, 1 return as is, 2 convert to char.
; See the documentation on odbc_binmode and odbc_longreadlen for an explanation
; of uodbc.defaultlrl and uodbc.defaultbinmode
odbc.defaultbinmode = 1  

[MySQL]
; Allow or prevent persistent links.
mysql.allow_persistent = On

; Maximum number of persistent links.  -1 means no limit.
mysql.max_persistent = -1

; Maximum number of links (persistent + non-persistent).  -1 means no limit.
mysql.max_links = -1

; Default port number for mysql_connect().  If unset, mysql_connect() will use
; the $MYSQL_TCP_PORT or the mysql-tcp entry in /etc/services or the
; compile-time value defined MYSQL_PORT (in that order).  Win32 will only look
; at MYSQL_PORT.
mysql.default_port =

; Default socket name for local MySQL connects.  If empty, uses the built-in
; MySQL defaults.
mysql.default_socket =

; Default host for mysql_connect() (doesn't apply in safe mode).
mysql.default_host =

; Default user for mysql_connect() (doesn't apply in safe mode).
mysql.default_user =

; Default password for mysql_connect() (doesn't apply in safe mode).
; Note that this is generally a *bad* idea to store passwords in this file.
; *Any* user with PHP access can run 'echo get_cfg_var("mysql.default_password")
; and reveal this password!  And of course, any users with read access to this
; file will be able to reveal the password as well.
mysql.default_password =

; Maximum time (in seconds) for connect timeout. -1 means no limit
mysql.connect_timeout = 60

; Trace mode. When trace_mode is active (=On), warnings for table/index scans and
; SQL-Errors will be displayed.
mysql.trace_mode = Off

[mSQL]
; Allow or prevent persistent links.
msql.allow_persistent = On

; Maximum number of persistent links.  -1 means no limit.
msql.max_persistent = -1

; Maximum number of links (persistent+non persistent).  -1 means no limit.
msql.max_links = -1

[PostgresSQL]
; Allow or prevent persistent links.
pgsql.allow_persistent = On

; Detect broken persistent links always with pg_pconnect(). Need a little overhead.
pgsql.auto_reset_persistent = Off 

; Maximum number of persistent links.  -1 means no limit.
pgsql.max_persistent = -1

; Maximum number of links (persistent+non persistent).  -1 means no limit.
pgsql.max_links = -1

; Ignore PostgreSQL backends Notice message or not.
pgsql.ignore_notice = 0

; Log PostgreSQL backends Noitce message or not.
; Unless pgsql.ignore_notice=0, module cannot log notice message.
pgsql.log_notice = 0

[Sybase]
; Allow or prevent persistent links.
sybase.allow_persistent = On

; Maximum number of persistent links.  -1 means no limit.
sybase.max_persistent = -1

; Maximum number of links (persistent + non-persistent).  -1 means no limit.
sybase.max_links = -1

;sybase.interface_file = "/usr/sybase/interfaces"

; Minimum error severity to display.
sybase.min_error_severity = 10

; Minimum message severity to display.
sybase.min_message_severity = 10

; Compatability mode with old versions of PHP 3.0.
; If on, this will cause PHP to automatically assign types to results according
; to their Sybase type, instead of treating them all as strings.  This
; compatibility mode will probably not stay around forever, so try applying
; whatever necessary changes to your code, and turn it off.
sybase.compatability_mode = Off

[Sybase-CT]
; Allow or prevent persistent links.
sybct.allow_persistent = On

; Maximum number of persistent links.  -1 means no limit.
sybct.max_persistent = -1

; Maximum number of links (persistent + non-persistent).  -1 means no limit.
sybct.max_links = -1

; Minimum server message severity to display.
sybct.min_server_severity = 10

; Minimum client message severity to display.
sybct.min_client_severity = 10

[dbx]
; returned column names can be converted for compatibility reasons
; possible values for dbx.colnames_case are
; "unchanged" (default, if not set)
; "lowercase"
; "uppercase"
; the recommended default is either upper- or lowercase, but
; unchanged is currently set for backwards compatibility
dbx.colnames_case = "unchanged"

[bcmath]
; Number of decimal digits for all bcmath functions.
bcmath.scale = 0

[browscap]
;browscap = extra/browscap.ini

[Informix]
; Default host for ifx_connect() (doesn't apply in safe mode).
ifx.default_host =

; Default user for ifx_connect() (doesn't apply in safe mode).
ifx.default_user =

; Default password for ifx_connect() (doesn't apply in safe mode).
ifx.default_password =

; Allow or prevent persistent links.
ifx.allow_persistent = On

; Maximum number of persistent links.  -1 means no limit.
ifx.max_persistent = -1

; Maximum number of links (persistent + non-persistent).  -1 means no limit.
ifx.max_links = -1

; If on, select statements return the contents of a text blob instead of its id.
ifx.textasvarchar = 0

; If on, select statements return the contents of a byte blob instead of its id.
ifx.byteasvarchar = 0

; Trailing blanks are stripped from fixed-length char columns.  May help the
; life of Informix SE users.
ifx.charasvarchar = 0

; If on, the contents of text and byte blobs are dumped to a file instead of
; keeping them in memory.
ifx.blobinfile = 0

; NULL's are returned as empty strings, unless this is set to 1.  In that case,
; NULL's are returned as string 'NULL'.
ifx.nullformat = 0

[Session]
; Handler used to store/retrieve data.
session.save_handler = files

; Argument passed to save_handler.  In the case of files, this is the path
; where data files are stored. Note: Windows users have to change this 
; variable in order to use PHP's session functions.
; As of PHP 4.0.1, you can define the path as: session.save_path = "N;/path"
; where N is an integer.  Instead of storing all the session files in 
; /path, what this will do is use subdirectories N-levels deep, and 
; store the session data in those directories.  This is useful if you 
; or your OS have problems with lots of files in one directory, and is 
; a more efficient layout for servers that handle lots of sessions.
; NOTE 1: PHP will not create this directory structure automatically.
;         You can use the script in the ext/session dir for that purpose.
; NOTE 2: See the section on garbage collection below if you choose to
;         use subdirectories for session storage

session.save_path = /tmp

; Whether to use cookies.
session.use_cookies = 1

; This option enables administrators to make their users invulnerable to
; attacks which involve passing session ids in URLs; defaults to 0.
; session.use_only_cookies = 1

; Name of the session (used as cookie name).
session.name = PHPSESSID

; Initialize session on request startup.
session.auto_start = 0

; Lifetime in seconds of cookie or, if 0, until browser is restarted.
session.cookie_lifetime = 0

; The path for which the cookie is valid.
session.cookie_path = /

; The domain for which the cookie is valid.
session.cookie_domain =

; Handler used to serialize data.  php is the standard serializer of PHP.
session.serialize_handler = php

; Define the probability that the 'garbage collection' process is started
; on every session initialization.
; The probability is calculated by using gc_probability/gc_divisor,
; e.g. 1/100 means there is a 1% chance that the GC process starts
; on each request.

session.gc_probability = 1
session.gc_divisor     = 100

; After this number of seconds, stored data will be seen as 'garbage' and
; cleaned up by the garbage collection process.
session.gc_maxlifetime = 1440

; NOTE: If you are using the subdirectory option for storing session files
;       (see session.save_path above), then garbage collection does *not*
;       happen automatically.  You will need to do your own garbage 
;       collection through a shell script, cron entry, or some other method. 
;       For example, the following script would is the equivalent of
;       setting session.gc_maxlifetime to 1440 (1440 seconds = 24 minutes):
;          cd /path/to/sessions; find -cmin +24 | xargs rm

; PHP 4.2 and less have an undocumented feature/bug that allows you to
; to initialize a session variable in the global scope, albeit register_globals
; is disabled.  PHP 4.3 and later will warn you, if this feature is used.
; You can disable the feature and the warning separately. At this time,
; the warning is only displayed, if bug_compat_42 is enabled.

;session.bug_compat_42 = 1
;session.bug_compat_warn = 1

; Check HTTP Referer to invalidate externally stored URLs containing ids.
; HTTP_REFERER has to contain this substring for the session to be
; considered as valid.
session.referer_check =

; How many bytes to read from the file.
session.entropy_length = 0

; Specified here to create the session id.
session.entropy_file =

;session.entropy_length = 16

;session.entropy_file = /dev/urandom

; Set to {nocache,private,public,} to determine HTTP caching aspects
; or leave this empty to avoid sending anti-caching headers.
session.cache_limiter = nocache

; Document expires after n minutes.
session.cache_expire = 180

; trans sid support is disabled by default.
; Use of trans sid may risk your users security. 
; Use this option with caution.
; - User may send URL contains active session ID
;   to other person via. email/irc/etc.
; - URL that contains active session ID may be stored
;   in publically accessible computer.
; - User may access your site with the same session ID
;   always using URL stored in browser's history or bookmarks.
session.use_trans_sid = 0

; The URL rewriter will look for URLs in a defined set of HTML tags.
; form/fieldset are special; if you include them here, the rewriter will
; add a hidden <input> field with the info which is otherwise appended
; to URLs.  If you want XHTML conformity, remove the form entry.
; Note that all valid entries require a "=", even if no value follows.
url_rewriter.tags = "a=href,area=href,frame=src,input=src,form=,fieldset="

[MSSQL]
; Allow or prevent persistent links.
mssql.allow_persistent = On

; Maximum number of persistent links.  -1 means no limit.
mssql.max_persistent = -1

; Maximum number of links (persistent+non persistent).  -1 means no limit.
mssql.max_links = -1

; Minimum error severity to display.
mssql.min_error_severity = 10

; Minimum message severity to display.
mssql.min_message_severity = 10

; Compatability mode with old versions of PHP 3.0.
mssql.compatability_mode = Off

; Connect timeout
;mssql.connect_timeout = 5

; Query timeout
;mssql.timeout = 60

; Valid range 0 - 2147483647.  Default = 4096.
;mssql.textlimit = 4096

; Valid range 0 - 2147483647.  Default = 4096.
;mssql.textsize = 4096

; Limits the number of records in each batch.  0 = all records in one batch.
;mssql.batchsize = 0

; Specify how datetime and datetim4 columns are returned
; On => Returns data converted to SQL server settings
; Off => Returns values as YYYY-MM-DD hh:mm:ss
;mssql.datetimeconvert = On

; Use NT authentication when connecting to the server
mssql.secure_connection = Off

; Specify max number of processes. Default = 25
;mssql.max_procs = 25

[Assertion]
; Assert(expr); active by default.
;assert.active = On

; Issue a PHP warning for each failed assertion.
;assert.warning = On

; Don't bail out by default.
;assert.bail = Off

; User-function to be called if an assertion fails.
;assert.callback = 0

; Eval the expression with current error_reporting().  Set to true if you want
; error_reporting(0) around the eval().
;assert.quiet_eval = 0

[Ingres II]
; Allow or prevent persistent links.
ingres.allow_persistent = On

; Maximum number of persistent links.  -1 means no limit.
ingres.max_persistent = -1

; Maximum number of links, including persistents.  -1 means no limit.
ingres.max_links = -1

; Default database (format: [node_id::]dbname[/srv_class]).
ingres.default_database =

; Default user.
ingres.default_user =

; Default password.
ingres.default_password =

[Verisign Payflow Pro]
; Default Payflow Pro server.
pfpro.defaulthost = "test-payflow.verisign.com"

; Default port to connect to.
pfpro.defaultport = 443

; Default timeout in seconds.
pfpro.defaulttimeout = 30

; Default proxy IP address (if required).
;pfpro.proxyaddress =

; Default proxy port.
;pfpro.proxyport =

; Default proxy logon.
;pfpro.proxylogon =

; Default proxy password.
;pfpro.proxypassword =

[com]
; path to a file containing GUIDs, IIDs or filenames of files with TypeLibs
;com.typelib_file = 
; allow Distributed-COM calls
;com.allow_dcom = true
; autoregister constants of a components typlib on com_load()
;com.autoregister_typelib = true
; register constants casesensitive
;com.autoregister_casesensitive = false
; show warnings on duplicate constat registrations
;com.autoregister_verbose = true

[Printer]
;printer.default_printer = ""

[mbstring]
; language for internal character representation.
;mbstring.language = Japanese

; internal/script encoding.
; Some encoding cannot work as internal encoding.
; (e.g. SJIS, BIG5, ISO-2022-*)
;mbstring.internal_encoding = EUC-JP

; http input encoding.
;mbstring.http_input = auto

; http output encoding. mb_output_handler must be
; registered as output buffer to function
;mbstring.http_output = SJIS

; enable automatic encoding translation accoding to 
; mbstring.internal_encoding setting. Input chars are
; converted to internal encoding by setting this to On.
; Note: Do _not_ use automatic encoding translation for
;       portable libs/applications.
;mbstring.encoding_translation = Off

; automatic encoding detection order.
; auto means 
;mbstring.detect_order = auto

; substitute_character used when character cannot be converted
; one from another
;mbstring.substitute_character = none;

; overload(replace) single byte functions by mbstring functions.
; mail(), ereg(), etc are overloaded by mb_send_mail(), mb_ereg(),
; etc. Possible values are 0,1,2,4 or combination of them.
; For example, 7 for overload everything.
; 0: No overload
; 1: Overload mail() function
; 2: Overload str*() functions
; 4: Overload ereg*() functions
;mbstring.func_overload = 0

[FrontBase]
;fbsql.allow_persistent = On
;fbsql.autocommit = On
;fbsql.default_database = 
;fbsql.default_database_password =
;fbsql.default_host =
;fbsql.default_password =
;fbsql.default_user = "_SYSTEM"
;fbsql.generate_warnings = Off
;fbsql.max_connections = 128
;fbsql.max_links = 128
;fbsql.max_persistent = -1
;fbsql.max_results = 128
;fbsql.batchSize = 1000

[Crack]
; Modify the setting below to match the directory location of the cracklib
; dictionary files.  Include the base filename, but not the file extension.
; crack.default_dictionary = "c:\php\lib\cracklib_dict"

[exif]
; Exif UNICODE user comments are handled as UCS-2BE/UCS-2LE and JIS as JIS. 
; With mbstring support this will automatically be converted into the encoding
; given by corresponding encode setting. When empty mbstring.internal_encoding 
; is used. For the decode settings you can distinguish between motorola and 
; intel byte order. A decode setting cannot be empty.
;exif.encode_unicode = ISO-8859-15
;exif.decode_unicode_motorola = UCS-2BE
;exif.decode_unicode_intel    = UCS-2LE
;exif.encode_jis = 
;exif.decode_jis_motorola = JIS
;exif.decode_jis_intel    = JIS

; Local Variables:
; tab-width: 4
; End:
date.timezone="UTC"
modlscapi/buildlsphp/php_build.template000075500000016057151677542400014417 0ustar00#!/bin/sh

###########
# need to replace the following
# 	PHP_BUILD_DIR = {PHP_BUILD_DIR}
#	CP_BUILD_DIR = {CP_BUILD_DIR}
# 	PHP_VERSION = {PHP_VERSION}
# 	PHP_CONF_OPTIONS = {PHP_CONF_OPTIONS}
#	LSWS_HOME = {LSWS_HOME}
############

check_errs()
{
  if [ "${1}" -ne "0" ] ; then
    echo "**ERROR** ${2}" 
    exit ${1}
  fi
}

test_phpsrc_ok()
{
	echo "Extracting PHP source archive: tar -zxf ${1}" 
	tar -zxf ${1}
	if [ "$?" -ne "0" ] ; then
		## remove bad copy
		rm -f ${1}
		echo "Could not extract PHP source archive"
		return 1
	fi
	return 0
}

echo ""
echo "=============================================="
echo "Start building PHP {PHP_VERSION} with LSAPI"
echo "=============================================="
echo `date`
echo ""

ulimit -m unlimited
ulimit -v unlimited

if [ ! -e "{PHP_BUILD_DIR}" ] ; then
	mkdir -p {PHP_BUILD_DIR}
	check_errs $? "Could not create build directory {PHP_BUILD_DIR}"
fi

echo "Changing to build directory {PHP_BUILD_DIR}" 
cd {PHP_BUILD_DIR}
check_errs $? "Could not get into build directory"

if [ -e php-{PHP_VERSION} ] ; then
	rm -rf php-{PHP_VERSION}
	check_errs $? "Could not delete old php directory"
fi

if [ -e "{CP_BUILD_DIR}/php-{PHP_VERSION}" ] ; then
	echo "Detected php build directory for cPanel {CP_BUILD_DIR}/php-{PHP_VERSION}"
	echo "Same source code will be copied over"
	cp -rp "{CP_BUILD_DIR}/php-{PHP_VERSION}" php-{PHP_VERSION} 
	check_errs $? "Failed to copy over php source directory"
	
else
	check_errs 1 "Cannot find php build directory for cPanel, please run EasyApache first to build PHP with correct options for Apache."
fi

echo ""


DOWNLOAD_URL="http://update.litespeedtech.com/ws/latest.php"
LSAPI_VERSION=`wget -q --output-document=- $DOWNLOAD_URL`
LSAPI_VERSION=`expr "$LSAPI_VERSION" : '.*PHP-LSAPI=\([0-9\.]*\)'`
echo "Query lastest LSAPI version: $LSAPI_VERSION"

if [ -e "php-litespeed-$LSAPI_VERSION.tgz" ] ; then
	echo "php-litespeed-$LSAPI_VERSION.tgz already downloaded, use the saved copy"
else
	DOWNLOAD_URL="https://www.litespeedtech.com/packages/lsapi/php-litespeed-$LSAPI_VERSION.tgz"
	echo "Retrieving LSAPI archive"
	wget -nv -O "php-litespeed-$LSAPI_VERSION.tgz" $DOWNLOAD_URL
	check_errs $? "Could not retrieve LSAPI archive"
fi

cd php-{PHP_VERSION}/sapi
check_errs $? "Could not get into php/sapi directory"

if [ -e litespeed/Makefile.frag ] ; then
	mv -f litespeed/Makefile.frag litespeed/Makefile.frag.package 
fi

echo "Extracting LSAPI archive: tar -xzf php-litespeed-$LSAPI_VERSION.tgz" 
tar -xzf "../../php-litespeed-$LSAPI_VERSION.tgz"
check_errs $? "Could not extract LSAPI archive"

if [ -e litespeed/Makefile.frag.package ] ; then
	mv -f litespeed/Makefile.frag.package litespeed/Makefile.frag 
fi

echo ""

echo "Finished gathering all the source code"
echo ""

echo "Generating configuration script"
cd ..
check_errs $? "Could not get into php directory"

sleep 1
touch ac*
check_errs $? "Could not touch ac*"

rm -rf autom4te.*

PHP_MAIN_VER=`expr "{PHP_VERSION}" : '\([0-9]*\.[0-9]*\)'`

PHP_MAIN_VER1=`expr "$PHP_MAIN_VER" : '\([0-9]*\)\.'`
PHP_MAIN_VER2=`expr "$PHP_MAIN_VER" : '[0-9]*\.\([0-9]*\)'`

BUILDCONF_FORCE=N
if [ "$PHP_MAIN_VER1" -lt "5" ] ; then
    BUILDCONF_FORCE=Y
elif [ "$PHP_MAIN_VER1" = "5" ] && [ "$PHP_MAIN_VER2" -lt "3" ] ; then
    BUILDCONF_FORCE=Y
fi

if [ "$BUILDCONF_FORCE" = "Y" ] ; then
	./buildconf --force
	check_errs $? "Could not generate configuration script for version prior to 5.3"
fi

rm config.cache

echo "Configuring PHP build (2-3 minutes)" 
echo "{PHP_CONF_OPTIONS}"

OS=`uname -s`

if [ "x$OS" = "xFreeBSD" ]; then
     CFLAGS='-O3' LDFLAGS='-L/usr/local/lib' {PHP_CONF_OPTIONS}
else
     CFLAGS='-O3' {PHP_CONF_OPTIONS}
fi

check_errs $? "Could not configure PHP build"

cp -fp "{CP_BUILD_DIR}/php-{PHP_VERSION}/libtool" . 
PLF=`uname -p`
if [ "x$PLF" = "xx86_64" ] ; then
	# work around for libtool problem for linux
	DLSCH=`grep 'sys_lib_dlsearch_path_spec="/lib /usr/lib ' libtool`
	if [ "x$DLSCH" != "x" ] ; then
		echo "  .. work around for libtool problem: sys_lib_dlsearch_path_spec should use lib64"
		cp libtool libtool.orig
		sed -e 's/sys_lib_dlsearch_path_spec=\"\/lib \/usr\/lib /sys_lib_dlsearch_path_spec=\"\/lib64 \/usr\/lib64 /' libtool.orig > libtool
	    if [ "$?" -ne "0" ] ; then
			echo "   sed command error, please try to modify libtool manually using lib64 for line: sys_lib_dlsearch_path_spec=\"/lib /usr/lib\" "
	    fi	
	fi
fi

make clean

echo "Compiling PHP (5-10 minutes)" 
echo `date`
make
check_errs $? "Could not compile PHP"

#make install

PHP_INI=/usr/local/lib/php.ini

ext=`grep '^zend_extension=".*\/ioncube' $PHP_INI`
if [ $? -eq 0 ]; then
    cd {CP_BUILD_DIR}/ioncube*
    if [ $? -eq 0 ]; then
		echo "Build IonCube ..."
		./cpanel-install
		cd ..
    fi
fi

ext=`grep '^extension="eaccelerator' $PHP_INI`
if [ $? -eq 0 ]; then
    cd {CP_BUILD_DIR}/eaccelerator*
    if [ $? -eq 0 ]; then
		echo "Build eaccelerator ..."
		echo "  .. Special handling for eAccelerator: do not use spin lock, buggy"
		echo "     set mm_sem_spinlock=no and mm_sem_ipc=no in config.m4"
		
		found=`find . -name "config.m4"`

		for f in $found
		do
		    cp $f $f.orig
		    sed -e 's/mm_sem_spinlock=yes/mm_sem_spinlock=no/' -e 's/mm_sem_ipc=yes/mm_sem_ipc=no/' $f.orig > $f
		    if [ "$?" -ne "0" ] ; then
				echo "   sed command error, please try to modify $f manually to avoid using spinlock"
		    fi
		done
		
		./cpanel-install
		cd ..
    fi
fi

ext=`grep '^extension="suhosin' $PHP_INI`
if [ $? -eq 0 ]; then
    cd {CP_BUILD_DIR}/suhosin*
    if [ $? -eq 0 ]; then
		echo "Build suhosin ..."
		./cpanel-install
		cd ..
    fi
fi

ext=`grep '^extension="ixed\.' $PHP_INI`
if [ $? -eq 0 ]; then
    cd {CP_BUILD_DIR}/sourceguardian*
    if [ $? -eq 0 ]; then
		echo "Build sourceguardian ..."
		./cpanel-install
		cd ..
    fi
fi

ext=`grep '^extension=".*homeloader\.' $PHP_INI`
if [ $? -eq 0 ]; then
    cd {CP_BUILD_DIR}/userphp
    if [ $? -eq 0 ]; then
		echo "Build homeloader ..."
		./configure
		make install
		cd ..
    fi
fi

ext=`grep '^zend_extension=".*ZendOptimizer\.' $PHP_INI`
if [ $? -eq 0 ]; then
    cd {CP_BUILD_DIR}/zendopt
    if [ $? -eq 0 ]; then
		echo "Build Zend Optimizer ..."
		./cpanel-install
		cd ..
    fi
fi

cp -p /usr/local/lib/php.ini /usr/local/lib/php.ini-lsbak
sed -e "s/^extension_dir/;extension_dir/" /usr/local/lib/php.ini-lsbak > /usr/local/lib/php.ini

chmod a+r /usr/local/lib/php.ini

echo "copy compiled php binary to litespeed directory"

echo "cd /usr/local/bin/"
cd /usr/local/bin/
check_errs $? "cannot cd to /usr/local/bin/"

if [ -e "lsphp" ] ; then
	mv lsphp lsphp.bak
fi

cp {PHP_BUILD_DIR}/php-{PHP_VERSION}/sapi/litespeed/php lsphp
check_errs $? "fail to copy lsphp from {PHP_BUILD_DIR}/php-{PHP_VERSION}/sapi/litespeed/php"

chmod a+rx lsphp

PHP_MAIN_VERSION=`expr "{PHP_VERSION}" : '\([0-9]*\)\.'`

#echo "ln -sf lsphp-{PHP_VERSION} lsphp$PHP_MAIN_VERSION"
#ln -sf lsphp-{PHP_VERSION} "lsphp$PHP_MAIN_VERSION" 
#check_errs $? "fail to creat symbolic link"
 
echo ""
echo "=============================================="
echo "Finished building PHP {PHP_VERSION} with LSAPI"
echo "=============================================="
echo `date`
echo "**COMPLETE**"
modlscapi/hooks/buildlsphp.hook000075500000001507151677542400012713 0ustar00#!/bin/bash

# Copyright (c) Cloud Linux GmbH & Cloud Linux Software, Inc 2010-2018 All Rights Reserved
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

#Cloudlinux rpm hook. mod_lsapi
. /usr/share/lve/modlscapi/include/cpanel-common-lve

/usr/bin/switch_mod_lsapi --build-native-lsphp

writeToLog "[modlsapi] hook was successfully executed"
modlscapi/hooks/clearconf.hook000075500000001633151677542400012501 0ustar00#!/bin/bash

# Copyright (c) Cloud Linux GmbH & Cloud Linux Software, Inc 2010-2018 All Rights Reserved
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

#Clear old conf files. Need it if apache type change

. /usr/share/lve/modlscapi/include/cpanel-common-lve

#if [ -e "/usr/local/apache/conf/conf.d" ]; then
#    rm -f /usr/local/apache/conf/conf.d/lsapi.conf
#fi

writeToLog "[clearconf] hook was successfully executed"

 
modlscapi/hooks/cpanel_check_handlers_hook.sh000075500000002466151677542400015523 0ustar00#!/bin/bash

CMD="$1"

HLOG="/usr/share/lve/modlscapi/logs/last.pre.upcp.handlers"

function get_current_handlers() {
    /usr/sbin/whmapi1 php_get_handlers | while read -r line;
    do
        echo -n "$line" | sed -n '/current_handler:/p' >> "$LOG"
        echo " $line" | sed -n '/version: [ea:alt]/p' >> "$LOG"
    done
}

function set_handlers_back() {
    cat "$HLOG" | while read -r line;
    do
        HANDLER=$(echo "$line" | cut -d ' ' -f2)
        PHPVERS=$(echo "$line" | cut -d ' ' -f4)
        echo "Setting $PHPVERS to $HANDLER handler" >> "$LOG"
        /usr/sbin/whmapi1 php_set_handler version="$PHPVERS" handler="$HANDLER" 2>/dev/null >/dev/null
    done
}

if [ "$CMD" == "--pre" ]; then
    LOG="/usr/share/lve/modlscapi/logs/Pre.upcp.$(date +%Y-%m-%d_%H:%M).log"
    get_current_handlers
    truncate -s 0 "$HLOG"
    cat "$LOG" >> "$HLOG"
    exit 0
fi

if [ "$CMD" == "--post" ]; then
    LOG="/usr/share/lve/modlscapi/logs/Post.upcp.$(date +%Y-%m-%d_%H:%M).log"
    get_current_handlers
    if diff "$LOG" "$HLOG" >/dev/null ; then
        echo "Handlers matched after upcp" >> "$LOG"
    else
        echo "Error: Handlers not matched after upcp" >> "$LOG"
        # Uncomment on the server where the lsapi handler is automatically turned off after the upcp
        set_handlers_back
    fi
    exit 0
fi


modlscapi/hooks/cpanel_post_upcp_check_handlers_hook.py000075500000002543151677542400017631 0ustar00#!/usr/bin/python3.6

# Copyright (c) Cloud Linux GmbH & Cloud Linux Software, Inc 2010-2018 All Rights Reserved
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""
Post upcp check php handlers hook
"""
import subprocess
import simplejson as json
import sys

def describe():
    """
    Description of the current hook for Cpanel
    """
    descr = [
        {
            'category': 'System',
            'event'   : 'upcp',
            'stage'   : 'post',
            'hook'    : '/usr/share/lve/modlscapi/hooks/cpanel_post_upcp_check_handlers_hook.py',
            'exectype': 'script',
        }
    ]
    return json.dumps(descr)


if "__main__" == __name__:
    argv = sys.argv[1:]
    if '--describe' in argv:
        print(describe())
        sys.exit(0)
    ret = subprocess.call(["/usr/share/lve/modlscapi/hooks/cpanel_check_handlers_hook.sh", "--post"])
    exit(ret)
modlscapi/hooks/cpanel_post_upcp_lsapi_hook.py000075500000002522151677542400016001 0ustar00#!/usr/bin/python3.6

# Copyright (c) Cloud Linux GmbH & Cloud Linux Software, Inc 2010-2018 All Rights Reserved
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""
Mod_lsapi patch Cpanel post upcp hook
"""
import subprocess
import simplejson as json
import sys


def describe():
    """
    Description of the current hook for Cpanel
    """
    descr = [
        {
            'category': 'System',
            'event'   : 'upcp',
            'stage'   : 'post',
            'hook'    : '/usr/share/lve/modlscapi/hooks/cpanel_post_upcp_lsapi_hook.py',
            'exectype': 'script',
        }
    ]
    return json.dumps(descr)


if "__main__" == __name__:
    argv = sys.argv[1:]
    if '--describe' in argv:
        print(describe())
        sys.exit(0)
    ret = subprocess.call(['/usr/share/lve/modlscapi/hooks/lsapi_patch_cpanel_hook.sh'])
    exit(ret)
modlscapi/hooks/cpanel_pre_upcp_check_handlers_hook.py000075500000002541151677542400017430 0ustar00#!/usr/bin/python3.6

# Copyright (c) Cloud Linux GmbH & Cloud Linux Software, Inc 2010-2018 All Rights Reserved
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""
Pre upcp check php handlers hook
"""
import subprocess
import simplejson as json
import sys

def describe():
    """
    Description of the current hook for Cpanel
    """
    descr = [
        {
            'category': 'System',
            'event'   : 'upcp',
            'stage'   : 'pre',
            'hook'    : '/usr/share/lve/modlscapi/hooks/cpanel_pre_upcp_check_handlers_hook.py',
            'exectype': 'script',
        }
    ]
    return json.dumps(descr)


if "__main__" == __name__:
    argv = sys.argv[1:]
    if '--describe' in argv:
        print(describe())
        sys.exit(0)
    ret = subprocess.call(["/usr/share/lve/modlscapi/hooks/cpanel_check_handlers_hook.sh", "--pre"])
    exit(ret)


modlscapi/hooks/cpanelpostupcp.hook000075500000004034151677542400013603 0ustar00#!/bin/bash

# Copyright (c) Cloud Linux GmbH & Cloud Linux Software, Inc 2010-2018 All Rights Reserved
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

#CloudLinux POSTUPCP INSTALL
. /usr/share/lve/modlscapi/include/cpanel-common-lve

function getEasyApacheVer(){
    cat /var/cpanel/easy/apache/profile/_main.yaml | grep version | cut -d":" -f2 | tr -d ' ' | tr -d "'"
}

log=$(getLogFile)

VER=$(getEasyApacheVer)

if [ -e "/var/cpanel/conf/apache/main" ]; then
    if [ ! -e "/var/cpanel/conf/apache/main.bak" ]; then
        cp /var/cpanel/conf/apache/main /var/cpanel/conf/apache/main.bak
    fi

    cat /var/cpanel/conf/apache/main | sed -n '/mod_ruid2\.so/{N;s/.*//;x;d;};x;p;${x;p;}' | sed '/^$/d' > /var/cpanel/conf/apache/main.tmp
    cat /var/cpanel/conf/apache/main.tmp | sed -n '/mod_ruid2\.conf/{s/.*//;x;d;};x;p;${x;p;}' | sed '/^$/d' > /var/cpanel/conf/apache/main
    rm -f /var/cpanel/conf/apache/main.tmp
fi

"$common_path_of_cpanel/utils/checkconfdutil.pm"

rebuildscript="/scripts/rebuildhttpdconf"
if [ -e "$rebuildscript" ]; then
 "$rebuildscript" 1>>"$log" 2>>"$log"
 echo "Rebuilding conf ok..." >> "$log"
else
 echo "Rubuilding failed..." >> "$log"
fi

restarthttpd="/scripts/restartsrv_httpd"
if [ -e "$restarthttpd" ]; then
 "$restarthttpd" 1>>"$log" 2>>"$log"
 echo "Restarting apache ok..." >> "$log"
else
 echo "Restarting apache failed..." >> "$log"
fi
                                            
writeToLog "[cpanelpostupcp] hook was successfully executed"
                                                       



                          
modlscapi/hooks/lsapi.hook000075500000002060151677542400011650 0ustar00#!/bin/bash

# Copyright (c) Cloud Linux GmbH & Cloud Linux Software, Inc 2010-2018 All Rights Reserved
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

#Cloudlinux rpm hook. mod_lsapi
. /usr/share/lve/modlscapi/include/cpanel-common-lve

installModule_lsapi

writeToLog "[modlsapi] hook was successfully executed"

if [ -e "/usr/share/lve/modlscapi/global.enabled" ]; then
    /usr/bin/switch_mod_lsapi --enable-global
    writeToLog "[modlsapi] global config successfully restored"
else
    writeToLog "[modlsapi] gloval config not enabled"
fi


                          
modlscapi/hooks/lsapi_patch_cpanel_hook.sh000075500000002006151677542400015043 0ustar00#!/bin/bash

LOG="/usr/share/lve/modlscapi/logs/lsapi_patch.log"
RES=$(cat "/usr/local/cpanel/Cpanel/WebServer/Supported/apache.pm" | grep "lsapi")

if [ -z "$RES" ]; then

    current_path=$(pwd)
    common_path_of_cpanel="/usr/share/lve/modlscapi/tars/"
    cd "$common_path_of_cpanel" || exit

    if [ -e "apache_pm.patch" ]; then

        cd /usr/local/cpanel/Cpanel/WebServer/Supported || exit
        patch -N -i $common_path_of_cpanel/apache_pm.patch

        if [ $? != 0 ]; then
            echo "$(date +%Y-%m-%d_%H:%M) - Error applying patch..." >> "$LOG"
            cd "$current_path" || exit
            exit 1
        else
            echo "$(date +%Y-%m-%d_%H:%M) - Patch was applied correctly..." >> "$LOG"
            cd "$current_path" || exit
            exit 0
        fi
    else
        echo "$(date +%Y-%m-%d_%H:%M) - Cannot find apache_pm.patch..." >> "$LOG"
        cd "$current_path" || exit
        exit 1
    fi
else
    echo "$(date +%Y-%m-%d_%H:%M) - Already patched..." >> "$LOG"
    exit 0
fi



modlscapi/include/cpanel-common-lve000075500000020562151677542400013424 0ustar00#!/bin/bash

# Copyright (c) Cloud Linux GmbH & Cloud Linux Software, Inc 2010-2018 All Rights Reserved
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

##################################################
# Common fucntions                               #
##################################################
VERSION="0.1beta"

common_path_of_cpanel="/usr/share/lve/modlscapi"
common_current_date=$(date +%Y-%m-%d)
common_tmp_path="$common_path_of_cpanel/tmp"

function getLogFile(){
    if [ ! -e "$common_path_of_cpanel/logs" ];then
	mkdir -p "$common_path_of_cpanel/logs"
    fi
    current_date_time=$(date +"%Y-%m-%d %k:%M:%S")
    echo "$common_path_of_cpanel/logs/$common_current_date.log"
}

function writeToLog(){
    if [ ! -e "$common_path_of_cpanel/logs" ];then
	mkdir -p "$common_path_of_cpanel/logs"
    fi
    current_date_time=$(date +"%Y-%m-%d %k:%M:%S")
    prg=$(basename "$0")
    echo "[$current_date_time from $prg] $1" >> "$common_path_of_cpanel/logs/$common_current_date.log"
}

function writeFileToLog(){
    if [ ! -e "$common_path_of_cpanel/logs" ];then
	mkdir -p $common_path_of_cpanel/logs
    fi
    current_date_time=$(date +"%Y-%m-%d %k:%M:%S")
    prg=$(basename "$0")
    echo "[$current_date_time from $prg] ----------------File Content $1 BEG---------------" >> "$common_path_of_cpanel/logs/$common_current_date.log"
    if [ -e "$1" ];then
	cat "$1" >> "$common_path_of_cpanel/logs/$common_current_date.log"
    fi
    echo "[$current_date_time from $prg] ----------------File Content $1 End---------------" >> "$common_path_of_cpanel/logs/$common_current_date.log"
}

function checkForAppNameSyntax(){
    isApache2_0syntax=$(echo "$1" | grep [,\;@])
    if [ -n "$isApache2_0syntax" ];then
	echo "$1" | cut -d',' -f 3 | tr '[:lower:]' '[:lower:]'
    else
	echo "$1"
    fi
}

function removeEmptyStringsFromFile(){
    filename="$1"
    res=$(sed -e '/^$/d' "$filename")
    echo "$res" > "$filename"
}

function deleteAllExcept(){
    #1 - hook
    #2 - tmp name
    #3 - pattern
    if [ ! -e "$common_tmp_path" ]; then
        mkdir -p "$common_tmp_path"
    fi
    if [ -e "$1" ];then
        cat "$1" | sed -n "$3" > "$common_tmp_path/$2.tmp.$$"
        echo "#!/bin/bash" > "$1"
        cat "$common_tmp_path/$2.tmp.$$" >> "$1"
        rm -f "$common_tmp_path/$2.tmp.$$"
    fi
}

function deleteAllInclude(){
    #1 - hook
    #2 - tmp name
    #3 - pattern
    if [ ! -e "$common_tmp_path" ]; then
        mkdir -p "$common_tmp_path"
    fi
    if [ -e "$1" ];then
        cat "$1" | sed "$3" > "$common_tmp_path/$2.tmp.$$"
        cat "$common_tmp_path/$2.tmp.$$" > "$1"
        rm -f "$common_tmp_path/$2.tmp.$$"
    fi
}


function showBar {
 nmb=$(cat "$0" | grep showBar | wc -l)
 let "nmb = $nmb"
 let "prct = $1 * 30 / $nmb"
 let "prct_n = $1 * 100 / $nmb"
 prg=$(basename "$0")
 echo -n "$prg: [" >&2
 for bar in {1..30}
 do
  if [ $bar -le $prct ];then
    echo -n "#" >&2
  else
    echo -n " " >&2
  fi
 done
 echo -ne "] ($prct_n%)\r" >&2
}

function get_command(){
    command=$(which "$1")                                                                                                                                      
    if [ $? != 0 ]; then                                                                                                                                     
        writeToLog "Can't execute command $1..."                                                                                                                   
        exit 1;                                                                                                                                              
    fi                                                                                                                                                       
    echo "$command"                                                                                                                                            
} 

function cmakeSorce(){          
 CL7=$(uname -r | grep '\.el7')
 PARAMS=""
 if [ -n "$CL7" ]; then
   PARAMS="-DWITH_CRIU:BOOLEAN=TRUE"
 fi
 cur=$(pwd)                                                                                                                              
 log=$(getLogFile)
 iscmake=$(get_command "cmake")                                                                                                                              
 CMAKE_SRC=$common_tmp_path/tmpcmakesrc                                                                                                                                       
 if [ -e "$CMAKE_SRC" ];then
    rm -rf "$CMAKE_SRC"
 fi
 mkdir -p $CMAKE_SRC                                                                                                                                         
 tar -zxvf "$1" -C $CMAKE_SRC >>$log                                                                                                                                  
 (cd "$CMAKE_SRC/$2"                                                                                                                                           
            "$iscmake" "$PARAMS" . 1>>$log 2>&1 && make 1>>$log 2>&1 && make install 1>>$log 2>&1)                                                                                                                                                                                                                                                                         
 makeproc=$?                                                                                                                                                 
 rm -rf $CMAKE_SRC                                                                                                                                           
 cd "$cur" 
 return $makeproc                                                                                                                                            
} 

function installModule_lsapi(){                                                                                                                            
 
        pathtosrc="$common_path_of_cpanel/tars/mod_lsapi.tar.gz"                                                                                                               
        cmakeSorce $pathtosrc ""                                                                                                                         
        if [[ $? != 0 ]]; then                                                                                                                           
            writeToLog "Linking error mod_lsapi.tar.gz"                                                                                                     
            exit 1
        else                                                                                                                                             
            writeToLog "Module mod_lsapi compiled... Ok"                                                                                                                                                                                                                                                     
        fi
        if [ ! -e "/usr/local/apache/conf/conf.d" ];then
            mkdir -p /usr/local/apache/conf/conf.d/
        fi
        if [ ! -e "/usr/local/apache/conf/conf.d/lsapi.conf" ];then
            cp -f "$common_path_of_cpanel/confs/lsapi.conf" /usr/local/apache/conf/conf.d/lsapi.conf                                                                                                                                                    
        fi
        return 0                                                                                                                                             
                                                                                                                                                             
}


modlscapi/logs/INFO000064400000000013151677542400010146 0ustar00CloudLinux
modlscapi/tars/apache2_2.patch000064400000000746151677542400012257 0ustar00--- vhost.default.old	2014-05-29 15:16:55.836980636 +0300
+++ vhost.default	2014-06-02 16:12:50.047978949 +0300
@@ -48,6 +48,12 @@
 [% END -%]
 [% END -%]
 [% END -%]
+[% IF vhost.modlsapi -%]
+ <IfModule mod_lsapi.c>
+    AddType application/x-httpd-lsphp [% vhost.modlsapi %]
+    AddHandler application/x-httpd-lsphp [% vhost.modlsapi %]
+ </IfModule>
+[% END -%]
 [% IF supported.mod_suphp -%]
     <IfModule mod_suphp.c>
         suPHP_UserGroup [% vhost.user %] [% vhost.group %]
modlscapi/tars/apache2_4.patch000064400000000753151677542400012257 0ustar00--- vhost.default.old	2014-05-29 14:26:39.895982152 +0300
+++ vhost.default	2014-06-02 16:14:54.143978886 +0300
@@ -59,6 +59,12 @@
         </Directory>
     </IfModule>
 
+[% IF vhost.modlsapi -%]
+ <IfModule mod_lsapi.c>
+    AddType application/x-httpd-lsphp [% vhost.modlsapi %]
+    AddHandler application/x-httpd-lsphp [% vhost.modlsapi %]
+ </IfModule>
+[% END -%]
 [% IF supported.mod_suphp -%]
     <IfModule mod_suphp.c>
         suPHP_UserGroup [% vhost.user %] [% vhost.group %]
modlscapi/tars/apache2_4_1.patch000064400000000735151677542400012477 0ustar00--- ssl_vhost.default.orig	2015-06-04 14:52:57.000000000 +0300
+++ ssl_vhost.default	2015-06-05 14:36:25.160000011 +0300
@@ -67,6 +67,14 @@
     </Directory>
   </IfModule>
 
+[% IF vhost.modlsapi -%]
+ <IfModule mod_lsapi.c>
+    AddType application/x-httpd-lsphp [% vhost.modlsapi %]
+    AddHandler application/x-httpd-lsphp [% vhost.modlsapi %]
+ </IfModule>
+[% END -%]
+
+
   <IfModule suphp_module>
     suPHP_UserGroup [% vhost.user %] [% vhost.group %]
   </IfModule>
modlscapi/tars/apache_pm.patch000064400000001224151677542400012440 0ustar00--- apache.pm.old	2017-02-09 05:46:30.904000000 -0500
+++ apache.pm	2017-02-09 05:47:59.566000000 -0500
@@ -815,7 +815,7 @@ sub get_available_handlers {
     # Only return handlers that support the language's SAPI interfaces.
     my $lang_obj = $lang->get_package( package => $package );
 
-    for my $sapi ( $lang_obj->get_sapi_list(), 'suphp', 'none' ) {
+    for my $sapi ( $lang_obj->get_sapi_list(), 'suphp', 'lsapi', 'none' ) {
         $sapi = 'dso' if $sapi eq 'apache2';
         my $handler = try { $self->make_handler( type => $sapi, lang => $lang, package => $package, lang_obj => $lang_obj ) };
         $handlers{$sapi} = $handler if $handler;
modlscapi/tars/lsapi.pm000064400000005040151677542400011150 0ustar00package Cpanel::WebServer::Supported::apache::Handler::lsapi;

# cpanel - Cpanel/WebServer/Supported/apache/Handler/lsapi.pm
#                                                    Copyright 2017 cPanel, Inc.
#                                                           All rights Reserved.
# copyright@cpanel.net                                         http://cpanel.net
# This code is subject to the cPanel license. Unauthorized copying is prohibited

=head1 NAME

Cpanel::WebServer::Supported::apache::Handler::lsapi

=head1 DESCRIPTION

An Apache handler module which supports the mod_lsapi module.  mod_lsapi
performs security checks on scripts and directories, and sets its
execution user/group IDs to that of the script owner.

A clearer documentation suite for the base class and an implemented
handler can be found in the I<SEE ALSO> section below.

=head1 SEE ALSO

L<Cpanel::WebServer::Supported::apache::Handler::base>,
L<Cpanel::WebServer::Supported::apache::Handler::cgi>

=head1 LICENSE AND COPYRIGHT

Copyright 2017, cPanel, Inc. All rights reserved. This code is
subject to the cPanel license. Unauthorized copying is prohibited.

=cut

use parent 'Cpanel::WebServer::Supported::apache::Handler::base';
use strict;
use warnings;

sub new {
    my ( $class, %args ) = @_;

    my $self = bless( {}, $class );
    $self->init( \%args );
    $self->module_check_and( ['mod_lsapi'] );
    $self->sapi_check('cgi');
    return $self;
}

sub type {
    return 'lsapi';
}

sub get_config_string {
    my ($self) = @_;

    my $package = $self->get_package();
    my $type    = $self->get_mime_type();
    my @exts    = sort $self->get_lang_obj()->get_file_extensions();
    my $str     = <<"EOF";
# lsapi configuration for $package
<IfModule lsapi_module>
    lsapi_engine On
    AddType $type @exts
</IfModule>
EOF
    return $str;
}

sub get_mime_type {
    my ($self) = @_;

    # NOTE: We're overriding the mime header because the lsapi EA4 package
    #       deploys a hard-coded /etc/container/php.handler.
    #       This conf hard-codes the names of the PHP package mime headers
    #       THIS IS BAD, especially if we add a new PHP version.
    #
    #       So, if you want to use base::get_mime_type (instead of overriding
    #       here), then we'd need to add a set_lang_handler() which updates
    #       /etc/container/php.handler with the correct [handlers] section.
    return 'application/x-httpd-' . $self->{'lang_obj'}->get_package_name() . "-lsphp";    # Look for '3 underscores' at https://cpanel.wiki/display/EA/Adding+a+PHP+Handler for more details
}

1;
modlscapi/tars/lsapi_modfcgid.conf000064400000000262151677542400013316 0ustar00<IfModule mod_lsapi.c>
AddType application/x-httpd-lsphp .php5 .php4 .php .php3 .php2 .phtml
AddHandler application/x-httpd-lsphp .php5 .php4 .php .php3 .php2 .phtml
</IfModule>
modlscapi/tmp/INFO000064400000000013151677542400010002 0ustar00CloudLinux
modlscapi/user/dashboard_malfunctions.py000064400000002557151677542400014604 0ustar00# Copyright (c) Cloud Linux GmbH & Cloud Linux Software, Inc 2010-2018 All Rights Reserved
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

criu_settings_malfunctions = {
    'on_stopped': {
        'reason': 'Criu service stopped',
        'description': "Run 'service criu start' because lsapi_criu enabled through Apache configs"
    },
    'off_running': {
        'reason': 'lsapi_criu disabled',
        'description': "Set 'lsapi_criu On' through Apache configs because criu service is running"
    }
}
lsapi_settings_malfunctions = {
    'disabled': {
        'reason': 'Apache module lsapi_module is not loaded',
        'description': "Enable mod_lsapi through Apache configs with 'LoadModule lsapi_module modules/mod_lsapi.so' directive"
    }
}
liblsapi_malfunctions = {
    None: {
        'reason': 'No liblsapi found',
        'description': "Install liblsapi package"
    }
}


modlscapi/user/exec_command.py000064400000003176151677542400012513 0ustar00#%copyright%

import os
import subprocess


def first_quot(line, isquotf):
    if line[0] == "\"" and isquotf == 0:
        return 1
    return 0


def last_quot(line, isquotf):
    if line[len(line) - 1] == '\"' and isquotf == 1:
        return 1
    return 0


def parse_command(command):
    command = command.split(" ")
    isquot = 0
    res = ""
    result = []
    for i in range(len(command)):
        if command[i] != "":
            if first_quot(command[i], isquot) == 1:
                isquot = 1
                res = command[i]
                continue
            if last_quot(command[i], isquot) == 1:
                isquot = 0
                res += " " + command[i]
                result.append(res)
                continue
            result.append(command[i])
    print(result)


def exec_command(command):
    result = []
    try:
        p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        while 1:
            output = p.stdout.readline()
            if not output:
                break
            if output.strip() != "":
                result.append(output.decode().strip())
    except Exception as inst:
        print("Call process error: " + str(inst))
    return result


def exec_command_out(command):
    """
    Run command via os.system and return its exit status

    :param command: Command to execute in a subshell
    :return: Exit status of the command process
    """
    return os.system(command)


def exec_command_find_substring(command, substring):
    result = exec_command(command)
    for i in result:
        if substring in i:
            return i
    return -1
modlscapi/user/lsphpchecker.ini000064400000003067151677542400012672 0ustar00bool:eq,suhosin.simulation = Off
bool:eq,suhosin.mail.protect = 1
bool:eq,suhosin.cookie.disallow_nul = no value
suhosin.cookie.max_array_depth = 1000
suhosin.cookie.max_array_index_length = 500
suhosin.cookie.max_name_length = 500
suhosin.cookie.max_totalname_length = 500
suhosin.cookie.max_value_length = 200000
suhosin.cookie.max_vars = 16384
bool:eq,suhosin.get.disallow_nul = no value
suhosin.get.max_array_depth = 1000
suhosin.get.max_array_index_length = 500
suhosin.get.max_name_length = 500
suhosin.get.max_totalname_length = 500
suhosin.get.max_value_length = 1000000
suhosin.get.max_vars = 16384
bool:eq,suhosin.post.disallow_nul = no value
suhosin.post.max_array_depth = 1000
suhosin.post.max_array_index_length = 500
suhosin.post.max_name_length = 500
suhosin.post.max_totalname_length = 500
suhosin.post.max_value_length = 1000000
suhosin.post.max_vars = 16384
bool:eq,suhosin.request.disallow_nul = no value
suhosin.request.max_array_depth = 1000
suhosin.request.max_array_index_length = 500
suhosin.request.max_totalname_length = 500
suhosin.request.max_value_length = 1000000
suhosin.request.max_vars = 16384
suhosin.request.max_varname_length = 524288
suhosin.upload.max_uploads = 300
bool:eq,suhosin.upload.disallow_elf = no value
bool:eq,suhosin.session.cryptua = Off
bool:eq,suhosin.session.encrypt = Off
suhosin.session.max_id_length = 1024
bool:eq,suhosin.executor.allow_symlink = Off
bool:eq,suhosin.executor.disable_eval = Off
bool:eq,suhosin.executor.disable_emodifier = Off
suhosin.executor.include.max_traversal = 8
apc.shm_segments = 1
mb:gte,apc.shm_size = 32
modlscapi/user/lsphpchecker.py000075500000026027151677542400012547 0ustar00#!/usr/bin/python3.6

# Copyright (c) Cloud Linux GmbH & Cloud Linux Software, Inc 2010-2018 All Rights Reserved
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

#coding:utf-8
import glob
import os
import pwd
import sys
from subprocess import Popen, PIPE


CHECK = {
    "gt": lambda x, y: x > y,
    "gte": lambda x, y: x >= y,
    "lt": lambda x, y: x < y,
    "lte": lambda x, y: x <= y,
    "eq": lambda x, y: x == y,
}
CHECK_KEYS = {"gt": "more", "gte": "more or equal", "lt": "less",
              "lte": "less or equal", "eq": "equal"}
BYTES_CONVERSION_TABLE = {'M': 1, 'G': 1024, 'T': 1024 * 1024}

quick = 0


def log_error(msg):
    """
    Wrapper for logging errors.
    Simple logging to stderr.
    """
    print(msg, file=sys.stderr)


class Cast:
    """
    Class with functions for cast to any type
    """
    @staticmethod
    def to_bool(_v):
        return {"Off": 0, "On": 1}.get(_v, Cast.to_number(_v))

    @staticmethod
    def to_mb(_v):
        if not len(_v):
            return None

        if _v[-1].isdigit():
            _v = "%sM" % _v

        _num = Cast.to_number(_v[:-1])
        if _num is None:
            return None

        return _num * BYTES_CONVERSION_TABLE.get(_v[-1].upper(), 1)

    @staticmethod
    def to_number(_v):
        try:
            return int(_v)
        except (ValueError, TypeError):
            return None


class PhpChecker:
    """
    docstring for PhpChecker
    """
    SAMPLE_CONFIG_PATH = "/usr/share/lve/modlscapi/user/lsphpchecker.ini"
    __php_binary = None
    __sample = None

    def __init__(self):
        """
        Initialize php versions list
        """
        global quick
        super(PhpChecker, self).__init__()
        php_list = glob.glob("/usr/local/bin/lsphp")
        if quick != 1:
            php_list.extend(glob.glob("/opt/alt/php*/usr/bin/lsphp"))
            php_list.extend(glob.glob("/opt/cpanel/ea-php*/root/usr/bin/lsphp"))
            php_list.extend(glob.glob("/usr/bin/lsphp"))
            php_list = sorted(php_list)
        self.__php_binary = php_list
        
        self._load_sample_options()

    def check_user(self, user):
        """
        Check configurations for user
        """
        for php_path in self.__php_binary:
            if os.path.exists(php_path):
                check_result = self._check_php_options(php_path, user)
                if check_result:
                    for message in check_result:
                        print("%s: %s: %s" % (user, php_path, message))

    def _check_php_options(self, php_path, user):
        """
        Load and check specified php version options
        @param `php_path` str: path to php binary
        @param `user` str: username
        """
        warnings = []
        options = self._load_php_options(php_path, user)
        modules = self._detect_danger_modules(php_path, user)

        warnings += self._check_options(options, "apc")
        warnings += self._check_options(options, "suhosin")
        #if "zend_guard_loader" in modules:
        #    warnings.append("Unstable module Zend Guard detected. Please disable module")

        #if "ioncube_loader" in modules:
        #    warnings.append("Unstable module ionCube detected. Please disable module")

        return warnings

    def _load_php_options(self, php_path, user):
        """
        Load php options from CLI phpinfo output
        """
        #print "%s: %s" % (user, php_path)
        if user == "":
            p = Popen([php_path,  "-i"], stdout=PIPE,
                  stderr=PIPE)
        else:
            p = Popen(["/bin/su", user, "-c", "[ ! -f %s ] || %s -i" % (php_path, php_path)], stdout=PIPE,
                  stderr=PIPE)
        out, err = p.communicate()

        #if p.returncode != 0:
        #    log_error(err)

        options = {}
        option_value = False
        for line in out.decode().split("\n"):
            if "Directive => Local Value => Master Value" == line:
                option_value = True
                continue

            if option_value:
                if not line:
                    option_value = False
                    continue

                values = line.split(" => ")
                if len(values) < 3:
                    log_error("Invalid option line - %s" % line)
                    continue

                if "." not in values[0]:
                    module = "__common__"
                    key = values[0]
                else:
                    module, key = values[0].split(".", 1)

                if module not in options:
                    options[module] = {}

                options[module][values[0]] = values[1]

        return options

    def _detect_danger_modules(self, php_path, user):
        """
        Detect unstable and potential danger php modules from php version output
        """
        if user == "":
            p = Popen([php_path, "-i"], stdout=PIPE,
                  stderr=PIPE)
        else:
            p = Popen(["/bin/su", user, "-c", "[ ! -f %s ] || %s -i" % (php_path, php_path)], stdout=PIPE,
                  stderr=PIPE)
        out, err = p.communicate()

        #if p.returncode != 0:
        #    log_error(err)

        modules = {}
        for line in out.decode().split("\n"):
            line = line.strip()
            if line.startswith("with the ionCube PHP Loader"):
                modules["ioncube_loader"] = True
            elif line.startswith("with Zend Guard Loader"):
                modules["zend_guard_loader"] = True

        return modules

    def _check_options(self, config, module):
        """
        Check php options based on sample config
        """
        if not config or not isinstance(config, dict) or \
                not isinstance(module, str) or \
                not isinstance(config.get(module), dict):
            return []

        if module not in self.__sample:
            return []

        result = []
        options = config[module]
        for key, check_info in self.__sample[module].items():
            if key in options and not self._validate_value(options[key], check_info):
                result.append("%s must be %s %s (current value: %s) (no value means Off)" %
                    (key, CHECK_KEYS.get(check_info["check"], ""),
                        check_info["value"], options[key]))

        if len(result):
            result.insert(0, "[%s]" % module)
            result.insert(0, "change %s options to default" % module)

        return result

    def _load_sample_options(self):
        """
        Load sample options
        """
        self.__sample = {}
        try:
            f = open(self.SAMPLE_CONFIG_PATH, "r")
            for line in f:
                line = line.strip()
                value_type = "number"
                check_type = "gte"
                try:
                    key, value = line.split("=", 1)
                    if "," in key:
                        # get additional information about value type and check method
                        type_info, key = key.split(",")
                        value_type, check_type = type_info.split(":")
                    module = key.split(".", 1)[0]
                except (ValueError, IndexError):
                    print("Invalid sample string %s" % line)
                    continue
    
                if module not in self.__sample:
                    self.__sample[module] = {}
                self.__sample[module][key.strip()] = {"value": value.strip(),
                    "type": value_type, "check": check_type}
            f.close()
        except (OSError, IOError):
            log_error("Error read %s" % self.SAMPLE_CONFIG_PATH)

    def _validate_value(self, value, rule):
        """
        Validate option value.
        @param value_type `str`|default:"number" : value type
        @param value1 `str|int`: value1 for compare
        @param value2 `str|int`: value2 for compare
        @return int : -1, 0, 1
        """
        if not isinstance(rule, dict):
            return False

        value_type = rule.get("type", "number")
        check_type = rule.get("check", "gte")
        check_value = rule["value"]

        cast_func = "to_%s" % value_type
        if hasattr(Cast, cast_func):
            value = getattr(Cast, cast_func)(value)
            check_value = getattr(Cast, cast_func)(check_value)

        if check_type not in CHECK:
            return False

        return CHECK[check_type](value, check_value)


def load_min_max_uid():
    """
    Load min and max UID from /etc/login.defs config
    """
    min_uid = max_uid = None
    try:
        f = open("/etc/login.defs", "r")
        for line in f:
            if not line.startswith("UID_MIN") and not line.startswith("UID_MAX"):
                continue

            data = line.split()
            if not data:
                continue

            try:
                if data[0] == "UID_MIN":
                    min_uid = int(data[1])
                elif data[0] == "UID_MAX":
                    max_uid = int(data[1])
            except (ValueError, IndexError):
                log_error("Invalid UID_MIN/UID_MAX values")
                break

        f.close()
    except (IOError, OSError):
        print("Can`t read UID_MIN and UID_MAX from /etc/login.defs file", file=sys.stderr)

    return min_uid, max_uid


def main(users_list):
    """
    Run check
    """
    global quick
    
    if len(users_list)>0 and users_list[0] == "help":
        print("%s [help] [quick] [users list...]" % sys.argv[0])
        print("   help       - show this help")
        print("   fast       - check all lsphp without switching to user")
        print("   medium     - check all users but only /usr/local/bin/lsphp config")
        print("   users list - list of needed users or empty. i this case users list will be taken from passwd")
        return
    if len(users_list)>0 and (users_list[0] == "fast" or users_list[0] == "medium"):
        if users_list[0] == "fast":
            quick = 2
        else:
            quick = 1
        users_list = users_list[1:]
    checker = PhpChecker()
    if quick == 2:
        checker.check_user("")
    elif users_list:
        for username in users_list:
            try:
                user = pwd.getpwnam(username)
                checker.check_user(username)
            except KeyError:
                log_error("User %s doesn`t exists" % username)
    else:
        UID_MIN, UID_MAX = load_min_max_uid()
        # get username list
        for user in pwd.getpwall():
            if  user.pw_uid >= UID_MIN and user.pw_uid <= UID_MAX:
                # for each user run check_user
                checker.check_user(user.pw_name)


if "__main__" == __name__:
    main(sys.argv[1:])
modlscapi/user/lve_diagnostic.py000064400000032657151677542400013071 0ustar00# Copyright (c) Cloud Linux GmbH & Cloud Linux Software, Inc 2010-2018 All Rights Reserved
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os
from distutils import version


def myExec(str):
    handle = os.popen(str)
    return handle.read()


class LiteSpeed:
    exist = False
    version = 'Unknown'
    suexec = False
    lve = False
    correctArch = False

    def __init__(self):
        self.exist = os.path.isfile('/usr/local/lsws')
        if os.path.isfile('/usr/local/lsws/bin/litespeed') and len(myExec('ps aux|grep litespeed').split('\n')) > 1:
            self.exist = True
            self.lve = '1' in myExec('grep enableLVE /usr/local/lsws/conf/httpd_config.xml')
            self.suexec = not ('0' in myExec('grep -i phpSuexec\> httpd_config.xml'))
            arch = myExec('file -L /usr/local/lsws/bin/litespeed')


class ApacheInfo:
    exist = False
    version = 'Unknown'
    mpm = 'Unknown'
    hostinglimits = 'Unknown'
    fcgid = 'Unknown'
    cgi = 'Unknown'
    php_dso = 'Unknown'
    cgid = 'Unknown'
    suPHP = 'Unknown'
    fastcgi = 'Unknown'
    disable_suexec = 'Unknown'
    suexec = 'Unknown'
    moduleInfo = False
    modules = 'Unknown'

    def __init__(self, path):
        self.detect(path)

    def check_version(self):
        if version.LooseVersion(self.version) >= version.LooseVersion("2.2.0"):
            return True
        else:
            print_warning(3001, "Unable to determine list of loaded modules, apache version %s", self.apache.version)
            return False

    def isModule(self, name):
        return str(self.modules.find(" "+name+"_module") != -1)

    def parseModules(self):
        if self.moduleInfo:
            self.hostinglimits = self.isModule('hostinglimits')
            self.fcgid = self.isModule('fcgid')
            self.php_dso = self.isModule('php5')
            self.cgi = self.isModule('cgi')
            self.cgid = self.isModule('cgid')
            self.suPHP = self.isModule('suPHP')
            self.fastcgi = self.isModule('fastcgi')
            self.disable_suexec = self.isModule('disable_suexec')
            self.suexec = self.isModule('suexec')

    def detect(self, path):
        self.exist = os.path.isfile(path)
        if self.exist:
            tmp = myExec(path + " -V 2>&1")
            if tmp.find('Syntax error') >= 0:
                lsapi_conf = None
                for s in tmp.split(' '):
                    if s.find('lsapi.conf') >= 0:
                        lsapi_conf = s.replace(':', '')
                        break
                if lsapi_conf is not None:
                    myExec('mv ' + lsapi_conf + ' ' + lsapi_conf + '.SAVE')
                tmp = myExec(path + " -V 2>&1")
                if lsapi_conf is not None:
                    myExec('mv ' + lsapi_conf + '.SAVE ' + lsapi_conf)
            lines = tmp.split('\n')
            for line in lines:
                if line.find('Server version:') != -1:
                    self.version = line[line.find('/')+1:]
                if line.find('Server MPM:') != -1:
                    self.mpm = line[line.rfind(' ')+1:]
            if version.LooseVersion(self.version) > version.LooseVersion('2.2'):
                self.moduleInfo = True
                self.modules = myExec(path + " -M 2>&1")
                self.parseModules()

    def str(self):
        if self.exist:
            str = "Apache verion: "+self.version+", mpm="+self.mpm+ \
                  ", hostinglimits="+self.hostinglimits+ \
                  ", cgi="+self.cgi+", cgid="+self.cgid+ \
                  ", fcgi="+self.fcgid+ ", fastcgi="+self.fastcgi+ \
                  ", php DSO="+self.php_dso+", suphp="+self.suPHP+ \
                  ", suexec="+self.suexec+", disable_suexec="+self.disable_suexec

            return str

        else:
            return None


def print_error(code, error_str, error_list, solution):
    print("CODE: ", code)
    print(error_str % error_list)
    print("Solution: ", solution)


def print_warning(code, error_str, error_list):
    print("WARNING: ", code)
    print(error_str % error_list)


class Kernel:
    version = None
    isLVEKernel = False
    isVZKernel = False
    isLVEEnabled = False

    def __init__(self):
        self.kernelName = myExec('/bin/uname -r').rstrip('\n')
        self.isLVEKernel = self.kernelName.find('lve')
        if self.isLVEKernel != -1:
            self.version = self.kernelName[self.isLVEKernel+3:]
            self.isLVEEnabled = os.path.isfile('/proc/lve/list')
        else:
            self.isVZKernel = 'stab' in self.kernelName

    def check(self):
        if self.isLVEEnabled:
            if self.isLVEEnabled:
                if version.LooseVersion(self.version) > version.LooseVersion('0.8.28'):
                    return True
                elif version.LooseVersion(self.version) > version.LooseVersion('0.8.0'):
                    print_error(1001, "You are running bugy kernel LVE version %s", self.version,
                                "Upgrade Kernel")
                elif version.LooseVersion(self.version) > version.LooseVersion('0.7.0'):
                    print_error(1002, "You are running old kernel LVE version %s\n That version doesn't support multiple cores per LVE or memory limits", self.version, "Upgrade Kernel")
                else:
                    print_error(1003, "You are running very old, bugy kernel, LVE version %s",
                                self.version, "Upgrade Kernel")
            else:
                print_error(1004, "LVE is not enabled", (),
                            "Check /etc/sysconfig/lve file, and make sure lve rpm is installed")
        elif self.isVZKernel:
            print_error(1101, "You are running VZ or OpenVZ", (),
                        "CloudLinux is not compatible, see http://www.cloudlinux.com/vz-compat.php for more info")
        else:
            print_error(1201, "You are not running CloudLinux kernel. Your kernel is: %s",
                        self.version, "Check /boot/grub/grub.conf")

    def str(self):
        result = "Kernel: "
        if self.isLVEEnabled:
            result += "OK (" + self.version + ")"
        elif self.isVZKernel:
            result += "VZ (" + self.kernelName + ")"
        else:
            result += "Unknown (" + self.kernelName + ")"
        return result


class CP:
    name = "Unknown CP"
    version = "Unknown"
    kernel = None

    def __init__(self):
        self.apache = ApacheInfo('/usr/sbin/apachectl')
        self.apache_path = '/etc/httpd'
        self.kernel = Kernel()

    def str(self):
        return self.name + " " + self.version + " " + self.kernel.str()

    def __str__(self):
        return self.name

    def check(self):
        self.kernel.check()


class CPanel(CP):
    name = "cPanel"
    def __init__(self):
        super(CPanel, self).__init__()
        self.version = myExec('/usr/local/cpanel/cpanel -V')
        self.apache = ApacheInfo('/usr/local/bin/apachectl')
        if os.path.exists('/etc/cpanel/ea4/is_ea4'):
            self.ea = 'EA4'
            self.apache_path = '/etc/apache2'
        else:
            self.ea = 'EA3'
            self.apache_path = '/usr/local/apache'

    def __str__(self):
        return self.ea


class Plesk(CP):
    name = "Plesk"
    def __init__(self):
        super(Plesk, self).__init__()
        tmp = myExec('/bin/cat /usr/local/psa/version')
        self.version = tmp.split(' ')[0]
        self.apache = ApacheInfo('/usr/sbin/apachectl')


class DirectAdmin(CP):
    name = "DirectAdmin"
    def __init__(self):
        super(DirectAdmin, self).__init__()
        tmp = myExec('/usr/local/directadmin/custombuild/build versions')
        tmp = tmp.split('\n')
        self.version = 'Unknown'
        self.apache = ApacheInfo('/usr/sbin/apachectl')
        for item in tmp:
            if item.find('Installed version of DirectAdmin:') != -1:
                self.version = item.split(':')[1].strip()
                break


class HSphere(CP):
    name = "H-Sphere"
    def __init__(self):
        super(HSphere, self).__init__()
        tmp = myExec('/bin/cat /hsphere/local/home/cpanel/shiva/psoft_config/HS_VERSION')
        self.version = tmp.split('\n')[1].strip()
        self.apache = self.get_apache_type()

    def get_apache_type(self):
        if os.path.isfile('/hsphere/shared/scripts/scripts.cfg'):
            f = open('/hsphere/shared/scripts/scripts.cfg')
            lines = f.readlines()
            f.close()
            for item in lines:
                key = item.split('=')[0].strip()
                value = item.split('=')[1].strip()
                if key == 'apache_version':
                    if value == '1':
                        return ApacheInfo('/hsphere/shared/apache/bin/httpd')
                    else:
                        return ApacheInfo('/hsphere/shared/apache2/bin/apachectl')
        return ApacheInfo('')


class iWorx(CP):
    name = "InterWorx"
    def __init__(self):
        super(iWorx, self).__init__()
        self.ini_file = '/usr/local/interworx/iworx.ini'
        self.version = self.find_version()
        self.apache = ApacheInfo('/usr/sbin/apachectl')

    def find_version(self):
        try:
            with open(self.ini_file, 'r') as ini_info:
                out = ini_info.read()
            return out.split('version')[4].replace('\n', '').replace('=', '').replace('"', '').split('[')[0]
        except Exception:
            return None


class ISPMgr(CP):
    name = "ISPManager"
    def __init__(self):
        super(ISPMgr, self).__init__()
        self.version = "unk"
        self.apache = ApacheInfo('/usr/sbin/apachectl')


class CustomPanel(CP):
    name = "CustomPanel"
    def __init__(self):
        super(CustomPanel, self).__init__()
        self.config_reader()
        self.root_dir = '/usr/share/lve/modlscapi/custom/'
        self.apache = None

    def config_reader(self):
        """Read all configurations related to Custom Panel from config.ini in the 'root_dir' folder.
        config.ini file must include GLOBAL section.
        """
        import configparser
        config = configparser.ConfigParser()
        config.read(self.root_dir + 'config.ini')
        cp_config = config['GLOBAL']
        self.check_config_ini(cp_config)

        self.version = cp_config.get('VERSION')
        self.apachectl= cp_config.get('APACHECTL_BIN_LOCATION')
        self.doc_url = cp_config.get('DOC_URL')
        self.executable= cp_config.get('EXECUTABLE_BIN')
        self.panel_name = cp_config.get('PANEL_NAME')

        if self.apachectl:
            self.apache = ApacheInfo(self.apachectl)

    def check_config_ini(self, cp_config):
        # If we add some directive to config.ini as a new feature or delete one of them, we have update this list too.
        # With this list we check if user supplied all required keys.
        required_keys = ['version', 'apachectl_bin_location', 'doc_url', 'executable_bin', 'panel_name']
        missing_keys = []

        for key in required_keys:
            if cp_config.get(key) is None:
                missing_keys.append(key)

        if missing_keys:
            print(f'Config file is missing required keys: {missing_keys}')
            exit(1)

def _get_cp_class():
    if os.path.isfile('/usr/local/cpanel/cpanel'):
        return CPanel
    if os.path.isfile('/usr/local/psa/version'):
        return Plesk
    if os.path.isdir('/usr/local/directadmin') and os.path.isfile('/usr/local/directadmin/custombuild/build'):
        return DirectAdmin
    if os.path.isfile('/hsphere/local/home/cpanel/shiva/psoft_config/HS_VERSION'):
        return HSphere
    if os.path.isdir("/usr/local/ispmgr"):
        return ISPMgr
    if os.path.isfile('/usr/local/interworx/iworx.ini'):
        return iWorx
    if os.path.isfile('/usr/share/lve/modlscapi/custom/config.ini'):
        return CustomPanel
    return CP

def get_cp_name():
    """
    Call this when you only need to detect Control Panel type, and don't need its complete properties.

    The function avoids creating an instance of CP successor class.
    This is safer for calling from installation scriptlets, when the system state can be inconsistent -
    because instance initialization tries to collect much more info than _get_cp_class().
    For example, 'apachectl' utility, used to gather server attributes into 'apache' instance variable,
    can fail half-way 'mod_lsapi' post-installation. Specifically:
    when the upgrade brings a non-backward compatible change of 'mod_lsapi.so'<->'liblscapi.so' binary interface,
    at the moment when we have 'liblscapi.so' already updated,
    but the new 'mod_lsapi.so' not yet copied into the Apache modules directory,
    'apachectl' would fail due to unloadable modules, print errors instead of Apache properties,
    and eventually produce installation failure.
    See CLOS-3184.
    """
    return _get_cp_class().name

def get_cp():
    """
    Call this when you want full Control Panel properties, not only its type.

    Properties can fail to be obtained, or be invalid, in intermediate machine state, like half-installed 'mod_lsapi'.
    See CLOS-3184.
    """
    return _get_cp_class()()
modlscapi/user/mod_lsapi_stat.py000064400000035310151677542400013066 0ustar00# Copyright (c) Cloud Linux GmbH & Cloud Linux Software, Inc 2010-2018 All Rights Reserved
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import json
import traceback
import subprocess
import configparser
import exec_command
from lve_diagnostic import get_cp
from dashboard_malfunctions import criu_settings_malfunctions
from dashboard_malfunctions import lsapi_settings_malfunctions
from dashboard_malfunctions import liblsapi_malfunctions
from stat_utils import cpanel_whmapi, plesk_bin_php_handler, dump_loaded_modules, dump_lsapi, query_strings
from stat_utils import get_da_domains, get_da_php_options, read_da_config, liblsapi_path, pretty_version_keys, count_domains, StatUtilsException
import selector_usage_lib


class ModLsapiStatException(Exception):
    pass


def get(as_json=False):
    """
    Return statistics data
    :param as_json: return json string if True, dict otherwise
    :return: statistics data dict(
                `controlPanel`: EA3/EA4/Plesk/DirectAdmin/Unknown CP,
                `criu`: dict(
                        `status`: running/stopped,
                        `version`: version
                    ),
                `domainStat`: dict(
                        `version`: `domains_num`,
                        ...
                    ) or dict(`error`: description) if some error occurred,
                `lsapiConf`: dict(
                        lsapi_with_connection_pool: on/off,
                        lsapi_criu: on/off
                    )
            ) or its json-packed version
    """
    error = None
    stats = {}
    control_panel = get_cp()
    try:
        if control_panel.name == 'cPanel':
            if os.path.exists('/etc/cpanel/ea4/is_ea4'):
                stats['domainStat'] = get_cpanel_ea4_stat()
            else:
                error = 'mod_lsapi domains stat is currently unsupported for EA3'
        elif control_panel.name == 'Plesk':
            stats['domainStat'] = get_plesk_stat()
        elif control_panel.name == 'DirectAdmin':
            stats['domainStat'] = get_da_stat()
        else:
            error = 'mod_lsapi domains stat is currently unsupported for {0}'.format(control_panel.name)
    except ModLsapiStatException as e:
        error = str(e)

    if error is not None:
        stats['domainStat'] = dict()
        stats['domainStatError'] = error

    stats['controlPanel'] = str(control_panel)
    stats.update(get_lsapi_conf())
    stats.update(get_crui_stat())

    stats.update({'totalDomain': sum(stats['domainStat'].values())})
    analyze_malfunctions(stats)

    if as_json:
        return json.dumps(stats, sort_keys=True)
    else:
        return stats


def get_cpanel_ea4_stat(lsapi_only=True, with_selector=True):
    """
    Collect mod_lsapi statistics for cPanel EA4 through WHM API
    :param lsapi_only: return only lsapi domains statistics if True, or full statistics if False
    :param with_selector: take into account the statistics of php selector
    :return: if lsapi_only return lsapi domains per version
                stat dict(
                    `version`: `domains_num`,
                    ...
                )
            else return full statistics per handler
                stat dict(
                    `handler`: {`version`: `domains_num`, ... }
                    ...
                )
    """
    domains_per_version = dict()  # to store `version`: `domains_list`
    handlers_stat = dict()  # to store `handler`: {`version`: `domains_num`, ...}
    domain_users = dict()  # to store `domain`: `user` correspondence
    try:
        # get all vhosts along with versions
        vhosts_data = cpanel_whmapi('php_get_vhost_versions').get('versions')
        for vhost in vhosts_data:
            domains_per_version.setdefault(vhost.get('version'), set()).add(vhost.get('vhost'))
            domain_users[vhost.get('vhost')] = vhost.get('account')
        # get handlers for versions
        handlers_data = cpanel_whmapi('php_get_handlers').get('version_handlers')
        version_handlers = dict([(h.get('version'), h.get('current_handler')) for h in handlers_data])
        all_versions = list(version_handlers.keys())

        # map {version: domains_list} to handlers, domains count in place
        for ver, handler in version_handlers.items():
            handlers_stat.setdefault(handler, dict()).update({ver: domains_per_version.get(ver, set())})

        # reinspect handlers stat against selector
        if with_selector:
            s_checked_version_handlers = selector_usage_lib.ea4_selector_check(domains_per_version, domain_users, handlers_stat)
            handlers_stat = s_checked_version_handlers

        # update structure with versions, which are not used by one handler, e.g. `ver: 0 domains`
        for h, v in handlers_stat.items():
            v.update(dict.fromkeys(set(all_versions).difference(list(v.keys())), set()))
    except (KeyError, TypeError, StatUtilsException):
        raise ModLsapiStatException(''.join(traceback.format_exc().split('\n')))

    # return only number of domains
    return count_domains(handlers_stat, all_versions, lsapi_only)


def get_plesk_stat(lsapi_only=True, with_selector=True):
    """
    Collect mod_lsapi statistics for Plesk
    Collects lsapi domains ONLY
    :param lsapi_only: return only lsapi domains statistics if True, or full statistics if False
    :param with_selector: take into account the statistics of php selector
    :return: if lsapi_only return lsapi domains per version dict(
                    `version`: `domains_num`
                    ...
                )
            else return stats with handler dict(
                    `lsapi`: {`version`: `domains_num`, ... }
                )
    """
    handler_tmpl = 'alt-php{v}'
    custom_version = 'alt-php56'
    domain_version_stat = dict()
    try:
        all_handlers = plesk_bin_php_handler('list')
        # on Plesk mod_lsapi is used only through handlers x-httpd-lsphp-*, which are added by --setup
        lsphp_handlers = [h for h in all_handlers if 'lsphp' in h.get('id')]
        for handler in lsphp_handlers:
            handler_id = handler.get('id')
            php_version = ''.join(handler.get('fullVersion').split('.')[:-1])
            domains = set([d.get('domain') for d in plesk_bin_php_handler('get-usage', id=handler_id)])
            # x-httpd-lsphp-custom domains are to be checked withon selector
            if 'custom' in handler_id:
                version_id = 'custom'
                custom_version = handler_tmpl.format(v=php_version)
            else:
                version_id = handler_tmpl.format(v=php_version)
            domain_version_stat[version_id] = domains
        # PLACE SELECTOR CHECK HERE (should be done for custom handler)
        if with_selector:
            domain_version_stat = selector_usage_lib.plesk_selector_check(domain_version_stat, custom_version)
    except (KeyError, TypeError, AttributeError, StatUtilsException, selector_usage_lib.SelectorStatException):
        raise ModLsapiStatException(''.join(traceback.format_exc().split('\n')))
    # return only number of domains
    result_stat = {
        'lsapi': {k: len(v) for k, v in domain_version_stat.items()}
    }
    return result_stat['lsapi'] if lsapi_only else result_stat


def get_da_stat(lsapi_only=True, with_selector=True):
    """
    Collect lsapi domains statistics fro DirectAdmin
    :param lsapi_only: return only lsapi domains statistics if True, or full statistics if False
    :param with_selector: take into account the statistics of php selector
    :return: if lsapi_only return lsapi domains per version
                stat dict(
                    `version`: `domains_num`,
                    ...
                )
            else return full statistics per handler
                stat dict(
                    `handler`: {`version`: `domains_num`, ... }
                    ...
                )
    """
    domain_conf_path = '/usr/local/directadmin/data/users/{user}/domains/{domain}.conf'
    handler_stat = dict()
    try:
        # get php settings from option.conf
        php_options = get_da_php_options()
        php1_ver = php_options[1]['version']
        php2_ver = php_options[2]['version']
        php1_handler = php_options[1]['handler']
        php2_handler = php_options[2]['handler']

        php1_label = pretty_version_keys(php1_ver)
        php2_label = pretty_version_keys(php2_ver)

        # get user: domains correspondence
        user_domains = get_da_domains()
        joined = set()
        [joined.update(v) for v in user_domains.values()]
        # analyze options.conf settings for versions
        if php2_ver == 'no':
            # no secondary php set, assume all domains use primary
            version_stat = {php1_label: joined}
            handler_stat[php1_handler] = {php1_label: joined}
        elif php1_ver == 'no':
            # no primary php set, assume all domains use secondary
            version_stat = {php2_label: joined}
            handler_stat[php2_handler] = {php2_label: joined}
        else:
            version_stat = {php1_label: set(), php2_label: set()}
            # if both php releases in options.conf are set, that means that DA PHP selector is enabled
            for user, domains in user_domains.items():
                # find php release settings for each domain
                for domain in domains:
                    config_path = domain_conf_path.format(user=user, domain=domain)
                    try:
                        # try to find which version domain uses as primary
                        conf_parser, global_section = read_da_config(config_path)
                        php_setting = conf_parser.getint(global_section, 'php1_select')
                        version = pretty_version_keys(php_options[php_setting]['version'])
                        version_stat.get(version).add(domain)
                    except configparser.NoOptionError:
                        # means that domain do not use DA PHP selector and uses primary php version from options.conf
                        version_stat.get(php1_label).add(domain)
            # if both php releases in options.conf are set, that means that DA PHP selector is enabled
            # create per-handler statistics
            if php1_handler == php2_handler:
                handler_stat[php1_handler] = {php1_label: version_stat[php1_label],
                                              php2_label: version_stat[php2_label]}
            else:
                handler_stat[php1_handler] = {php1_label: version_stat[php1_label],
                                              php2_label: set()}
                handler_stat[php2_handler] = {php2_label: version_stat[php2_label],
                                              php1_label: set()}

        if with_selector:
            selector_updated = selector_usage_lib.da_selector_check(version_stat.get(php1_label), user_domains, php1_label)
            handler_stat.get(php1_handler).update(selector_updated)
    except (KeyError, TypeError, AttributeError, StatUtilsException):
        raise ModLsapiStatException(''.join(traceback.format_exc().split('\n')))

    # return only number of domains
    return count_domains(handler_stat, [php1_label, php2_label], lsapi_only)


def get_crui_stat():
    """
    Get criu service info
    :return: dict(
                `criu`: dict(
                    `status`: running/stopped,
                    `version`: version
                )
            )
    """
    criu_version = ''.join(exec_command.exec_command('/usr/sbin/criu -V'))

    try:
        subprocess.check_call(['/sbin/service', 'criu', 'status'], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
        criu_service_status = 'running'
    except subprocess.CalledProcessError:
        criu_service_status = 'stopped'

    return {
        'criu': {
            'status': criu_service_status,
            'version': criu_version.split(' ')[1] if criu_version else 'not installed'
        }
    }


def get_lsapi_conf():
    """
    Retrieve lsapi configuration:
    - crui
    - connection pool
    and versions of module and library
    :return: dict(
                lsapiConf: dict(
                    lsapi_with_connection_pool: on/off,
                    lsapi_criu: on/off
                ),
                modVersion: version,
                libVersion: version
            )
    """
    # Plesk carries upstream apache 2.4.6, which says
    # `Passing arguments to httpd using apachectl is no longer supported.`
    # have to query with httpd instead of apachectl
    apache_conf = dump_lsapi()
    apache_modules = dump_loaded_modules()
    mod_status = apache_modules.get('lsapi_module', None)
    return {
        'lsapiConf': {
            'lsapi_criu': apache_conf.get('lsapi_criu', 'off'),
            'lsapi_with_connection_pool': apache_conf.get('lsapi_with_connection_pool', 'off')
        },
        'modVersion': apache_conf.get('version', None),
        'libVersion': query_strings(liblsapi_path(), 'liblsapi_version'),
        'modStatus': 'disabled' if not mod_status else 'enabled'
    }


def analyze_malfunctions(stats_dict):
    """
    Detect configuration malfunctions and update resulting statistics dict accordingly
    For now only criu malfunctions are presented
    See malfunctions in dashboard_malfunctions.py module
    :param stats_dict: resulting statistics dict
    """
    malfunctions = list()

    def add_malfunction(malfunc_dict, key):
        try:
            # try to detect one malfunction
            malfunctions.append(malfunc_dict[key])
        except KeyError:
            # no malfunction found
            pass

    criu_settings = '{opt}_{serv}'.format(opt=stats_dict['lsapiConf']['lsapi_criu'],
                                          serv=stats_dict['criu']['status'])

    lsapi_settings = stats_dict['modStatus']
    # for further extension of similar malfunctions:
    # malfuncs = tuple of malfunctions
    # keys = tuple of according keys
    # for malfunc, k in zip(malfuncs, keys):
    #     add_malfunction(malfunc, k)
    add_malfunction(liblsapi_malfunctions, stats_dict['libVersion'])
    add_malfunction(lsapi_settings_malfunctions, lsapi_settings)
    add_malfunction(criu_settings_malfunctions, criu_settings)

    # no need in `malfunctions` field if there are no malfunctions
    if not malfunctions:
        return
    else:
        stats_dict.update({'malfunctions': malfunctions})
modlscapi/user/selector_usage_lib.py000064400000031577151677542400013731 0ustar00# Copyright (c) Cloud Linux GmbH & Cloud Linux Software, Inc 2010-2018 All Rights Reserved
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import json
# from lxml import etree
from copy import deepcopy
import exec_command
from stat_utils import pretty_version_keys, cpanel_whmapi, dump_loaded_modules


class SelectorStatException(Exception):
    """
    Inner exception class
    """
    pass


def cagefs_enabled():
    """
    Check status of cagefs - installed & enabled
    If not - selector is not applied, no need to query its settings
    :return: True is cagefs is installed and enabled, False otherwise
    """
    cagefs_status = ''.join(exec_command.exec_command("/usr/sbin/cagefsctl --cagefs-status")).strip()
    return cagefs_status == 'Enabled'


def get_selector_versions():
    """
    Retrieve versions from selector, EXCEPT native
    :return: selector versions iterator
    """
    # query selector versions
    selector_data = ''.join(exec_command.exec_command("/usr/bin/selectorctl --summary --json"))
    try:
        selector_versions = json.loads(selector_data).get("PHPConfiguration")
    except ValueError:
        # no json could be decoded (may be selector is not installed)
        selector_versions = ()
    for item in selector_versions:
        ver = item['version']
        if ver != 'native':
            yield ver


def get_selector_users(version):
    """
    Retrieve selector users for given version
    :param version: selector version, e.g 7.2
    :return: dict user: version, e.g. {user: alt-php72, user1: alt-php72}
    """
    users = ''.join(exec_command.exec_command("/usr/bin/selectorctl --list-users --version={v}".format(v=version)))
    return dict((u, pretty_version_keys(version, 'alt-php')) for u in users.split(',') if u)


def get_selector_usage():
    """
    Retrieve versions set for users in selector
    :return: dict { user: version }
    """
    if not cagefs_enabled():
        return dict()

    # retrieve users of each selector version
    users_ver = dict()
    for ver in get_selector_versions():
        users_ver.update(get_selector_users(ver))
    return users_ver


def ea4_selector_check(ver_domains_list, domain_user_dict, handlers_mapping):
    """
    Check EA4 selector settings. Statistics per handler is inspected.
    Selector on EA4 does not change handler, it changes version only.
    Selector version is applied only in case of all the following circumstances:
     - suexec_module is enabled
     - system default version is not alt-php
     - selector setting is not native
    :param ver_domains_list: set of domains per version
    :param domain_user_dict: domain--user correspondence
    :param handlers_mapping: per handler statistics
    :return: set of domains per version checked against selector settings
    """
    # ## 1.check if selector is appicable
    default_ver = cpanel_whmapi('php_get_system_default_version').get('version')
    if ver_domains_list.get(default_ver) is None:
        # there are no domains using default version - this means there are no domains to apply selector settings to
        return handlers_mapping
    # system default version is not alt-php and suexec is installed
    if 'alt-php' not in default_ver and 'suexec_module' in dump_loaded_modules():
        # ## 1 finish

        # ## 2. get api_ver: dom_list
        # this is ver_domains_list input, which should be inverted
        user_domains = ea4_revert_domains_user_struct(domain_user_dict)
        # ## 2 finish

        # ## 3. get sel_ver: dom_list
        s_ver_domains = get_selector_domains(user_domains)
        # ## 3 finish

        # ## 4. aggregation: need to inspect system default domains and selector domains against each other
        api_ver_list = deepcopy(ver_domains_list)  # dicts and sets are mutable, so it's better not to modify original structure
        default_domains_matched, s_ver_matched = matcher(api_ver_list.get(default_ver, set()), s_ver_domains)
        api_ver_list[default_ver] = default_domains_matched
        # ## 4  finish

        # ## 5. merge; need to update stat per handler according to matched structures: selector do not change handler!
        handlers = deepcopy(handlers_mapping)
        for h, v in handlers.items():
            if default_ver in v:
                # default version belongs to handler, update whole structure for handler according to matched data
                handlers[h].update({default_ver: default_domains_matched})
                handlers[h].update(merger(v, s_ver_matched))
        return handlers
        # ## 5 finish
    else:
        return handlers_mapping


def ea4_revert_domains_user_struct(domains_user_dict):
    """
    Revert structure `domain`: `user` to structure `user`: set of domains
    :param domains_user_dict: dict of domains per user correspondence
    :return: dict user: set_of_domains
    """
    user_domains = dict()
    for domain, user in domains_user_dict.items():
        user_domains.setdefault(user, set()).add(domain)
    return user_domains


def get_selector_domains(domains_of_user):
    """
    Retrieve domains set for selector versions
    :param domains_of_user: `user`: set of domains structure
    :return: dict selector_ver: set of domains
    """
    selector_users = get_selector_usage()  # user: version
    selector_versions = dict()
    for user, s_ver in selector_users.items():
        selector_versions.setdefault(s_ver, set()).update(domains_of_user.get(user, set()))
    return selector_versions


def matcher(system_default_api_domains, selector_versions):
    """
    Match domains sets between system default version and selector versions:
    - for system default version domains get difference with selector domains
    - for selector domains get intersection with system default version
    Common for cPanel and Plesk
    :param system_default_api_domains: set of domains using system default version
    :param selector_versions: dict selector_ver: set of domains
    :return: tuple(clean_default_domains, clean_selector_domains)
    """
    for ver, dom_list in selector_versions.items():
        if ver == 'native':
            continue
        selector_versions[ver].intersection_update(system_default_api_domains)
        system_default_api_domains.difference_update(dom_list)
    return system_default_api_domains, selector_versions


def merger(clean_api_versions, clean_selector_versions):
    """
    Merge matched structures of domains (common for cPanel and Plesk)
    :param clean_api_versions: API_version: set of domains structure after match
    :param clean_selector_versions: selector_version: set of domains structure after match
    :return: unified structure version: domains_set
    """
    # get the union of two dicts -- there might be different keys in either
    merged = dict(clean_api_versions, **clean_selector_versions)
    # union values (sets of domains) for each version
    for ver, dom_list in merged.items():
        try:
            merged[ver] = dom_list.union(clean_api_versions[ver])
        except KeyError:
            pass
    return merged


def plesk_selector_check(ver_domains_list, custom_ver):
    """
    Check Plesk selector settings.
    Selector version is applied only to LSPHP by vendor OS handlers, e.g. x-httpd-lsphp-custom
    :param ver_domains_list: list of domains per version, including `custom` handler version
    :param custom_ver: real version, to which Plesk resolves `custom` handler
    :return: set of domains per version checked against selector settings
    """
    # ## 1.check if selector is appicable
    if ver_domains_list.get('custom'):
        # ## 2. get api_ver: dom_list
        user_domains = plesk_get_user_domains()
        # ## 2 finish

        # ## 3. get sel_ver: dom_list
        s_ver_domains = get_selector_domains(user_domains)
        # ## 3 finish

        # ## 4. aggregation: need to inspect custom domains and selector domains against each other
        api_ver_list = deepcopy(ver_domains_list)  # dicts and sets are mutable, so it's better not to modify original structure
        custom_domains_matched, s_ver_matched = matcher(api_ver_list.get('custom', set()), s_ver_domains)
        api_ver_list['custom'] = custom_domains_matched
        # ## 4  finish
        # ## 5. merge; need to get one whole structure from two different, previously inspected
        merged = merger(api_ver_list, s_ver_matched)
        # ## 5 finish
    else:
        # there are no domains using custom handler - this means there are no domains to apply selector settings to
        merged = ver_domains_list
    # clean `custom` field
    return plesk_manage_custom(merged, custom_ver)


def plesk_get_user_domains():
    """
    Find `user`: `domains` correspondence for Plesk
    :return: dict(`user`: set of domains)
    """
    return merger(plesk_get_domains_under_new_subsciption(),
                  plesk_get_domains_under_existing_subsciption())


def plesk_get_domains_under_existing_subsciption():
    """
    Get users' domains, added under existing subscription
    :return: dict(user: set_of_domains)
    """
    return plesk_query_db('select login,name from domains inner join Subscriptions as sc on domains.webspace_id=sc.object_id inner join sys_users as u on sc.id=u.id where domains.webspace_id!=0 and sc.object_type="domain"')


def plesk_get_domains_under_new_subsciption():
    """
    Get users' domains, added under new subscription
    :return: dict(user: set_of_domains)
    """
    return plesk_query_db('select login,name from domains inner join Subscriptions as sc on domains.id=sc.object_id inner join sys_users as u on sc.id=u.id where domains.webspace_id=0 and sc.object_type="domain"')


def plesk_query_db(query):
    """
    Query Plesk database for user:domain correspondence and get result in XML
    :return: dict(user: set_of_domains)
    """
    from lxml import etree
    user_domains = dict()
    xml_result = exec_command.exec_command("plesk db '{q}' --xml".format(q=query))
    try:
        root = etree.fromstring(''.join(xml_result))
        for row in root.iter('row'):
            domain_name = row.xpath("field[@name='name']")[0].text
            user_name = row.xpath("field[@name='login']")[0].text
            user_domains.setdefault(user_name, set()).add(domain_name)
    except (etree.XMLSyntaxError, etree.ParseError):
        raise SelectorStatException("Failed to parse XML from plesk db output: {0}".format(xml_result))
    return user_domains


def plesk_manage_custom(versions_mapping, real_version):
    """
    Unify `custom` domains with `real_version` domains,
    clear `custom` key from resulting `version`: `set of domains` mapping
    :param versions_mapping: final merged structure of domains
    :param real_version: version, to which Plesk resolves custom
    :return: final mapping with `custom` domains unified with `real_version` domains
    """
    if versions_mapping:
        real_version_domains = versions_mapping.get(real_version, set())
        real_version_domains.update(versions_mapping.get('custom', set()))
        versions_mapping.update({real_version: real_version_domains})
        try:
            del versions_mapping['custom']
        except KeyError:
            pass
    return versions_mapping


def da_selector_check(primary_ver_domains, domains_per_user, primary_php):
    """
    Check DA selector settings
    :param primary_ver_domains: set of domains using primary version, e.g. php1_release
    :param domains_per_user: user--domain correspondence
    :param primary_php: php version set in options.conf as php1_ver, only this version is affected by selector
    :return: set of domains per primary version checked against selector settings
    """
    # ## 1.check if selector is appicable
    if primary_ver_domains:
        # ## 2. get api_ver: dom_list
        # already have it as domains_per_user
        # ## 2 finish

        # ## 3. get sel_ver: dom_list
        s_ver_domains = get_selector_domains(domains_per_user)
        # ## 3 finish

        # ## 4. aggregation: need to inspect primary php domains and selector domains against each other
        api_ver_list = deepcopy(primary_ver_domains)  # dicts and sets are mutable, so it's better not to modify original structure
        default_domains_matched, s_ver_matched = matcher(api_ver_list, s_ver_domains)
        # ## 4  finish
        # ## 5. merge; need to get one whole structure from two different, previously inspected
        merged = merger({primary_php: default_domains_matched}, s_ver_matched)
        # ## 5 finish
        return merged
    else:
        return {primary_php: primary_ver_domains}
modlscapi/user/stat_utils.py000064400000022255151677542400012263 0ustar00# Copyright (c) Cloud Linux GmbH & Cloud Linux Software, Inc 2010-2018 All Rights Reserved
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import string
import sys
import os
import traceback
import json
import configparser
from io import StringIO
from pipes import quote
import exec_command


class StatUtilsException(Exception):
    pass


def cpanel_whmapi(cmd, **kwargs):
    """
    Perform cPanel WHM API console request and return data from result
    :param cmd: whm api command
    :return: data dict from result
    """
    joined_kwargs = ' '.join([quote('{0}={1}'.format(k, v)) for k, v in kwargs.items()])
    result = exec_command.exec_command("/usr/sbin/whmapi1 {cmd} {kw} --output json".format(cmd=cmd, kw=joined_kwargs))
    try:
        dict_result = json.loads(''.join(result))
    except ValueError:
        # if result is not JSON
        raise StatUtilsException(
            "Failed to get JSON from this API request: {0} {1}; output: {2}".format(cmd,
                                                                                    joined_kwargs,
                                                                                    ''.join(result)))

    try:
        return dict_result['data']
    except KeyError:
        # if there are no data field in received JSON
        raise StatUtilsException("Failed to get data from this API result: {0}".format(dict_result))


def plesk_bin_php_handler(cmd, **kwargs):
    """
    Perform Plesk php_handler utility console request and return result
    :param cmd: php_handler command
    :return: dict result
    """
    # need to quote only value (-k 'v'), quoting full params ('-k v') causes API failure
    joined_kwargs = ' '.join(['-{0} {1}'.format(k, quote(v)) for k, v in kwargs.items()])
    result = exec_command.exec_command("/usr/local/psa/bin/php_handler --{cmd} {kw} -json true".format(cmd=cmd,
                                                                                                       kw=joined_kwargs))
    try:
        return json.loads(''.join(result))
    except ValueError:
        raise StatUtilsException(
            "Failed to get JSON from this API request: php_handler {0} {1}; output: {2}".format(cmd,
                                                                                                joined_kwargs,
                                                                                                ''.join(result)))


def get_da_domains():
    """
    Get domains per user
    :return: dict(
                user: list of domains
            )
    """
    da_users_path = '/usr/local/directadmin/data/users'
    da_domains_path = '/usr/local/directadmin/data/users/{user}/domains.list'

    da_users = os.listdir(da_users_path)
    domains = dict.fromkeys(da_users)
    try:
        for user in da_users:
            with open(da_domains_path.format(user=user), 'r') as domains_list_file:
                domains[user] = set([l.strip() for l in domains_list_file.readlines()])
    except (OSError, IOError):
        raise StatUtilsException(''.join(traceback.format_exc().split('\n')))
    return domains


def get_da_php_options():
    """
    Get php settings from options.conf
    :return: dict(
                first php setting: {version, mode},
                second php setting: {version, mode},
            )
    """
    options_path = '/usr/local/directadmin/custombuild/options.conf'

    try:
        config_parser, global_section = read_da_config(options_path)
        php1_ver = config_parser.get(global_section, 'php1_release')
        php2_ver = config_parser.get(global_section, 'php2_release')
        php1_handler = config_parser.get(global_section, 'php1_mode')
        php2_handler = config_parser.get(global_section, 'php2_mode')
    except configparser.NoOptionError:
        raise StatUtilsException('No option found: {0}'.format(''.join(traceback.format_exc().split('\n'))))

    return {
        1: {
            'version': php1_ver,
            'handler': 'lsapi' if php1_handler == 'lsphp' else php1_handler
        },
        2: {
            'version': php2_ver,
            'handler': 'lsapi' if php2_handler == 'lsphp' else php2_handler
        }
    }


def read_da_config(conf_file, append_section_name='dummy_section'):
    """
    Read DA config file with ConfigParser.
    Need to add dummy section for success
    :param conf_file: config file name
    :param append_section_name: name of section to place in the beginning of file
    :return: RawConfigParser instance
    """
    try:
        with open(conf_file) as f:
            file_content = StringIO('[{s}]\n'.format(s=append_section_name) + f.read())
        config_parser = configparser.RawConfigParser(strict=False)
        config_parser.read_file(file_content)
    except (OSError, IOError):
        raise StatUtilsException(''.join(traceback.format_exc().split('\n')))
    return config_parser, append_section_name


def pretty_version_keys(php_ver, pre='php'):
    """
    Convert simple php versions to pretty format
    :param php_ver: {major}.{minor} version
    :param pre: desired key start
    :return: alt-php{major}{minor} or desired `pre`{major}{minor}
    """
    template = '{0}%s%s'.format(pre)
    try:
        return template % tuple(php_ver.split('.'))
    except Exception:
        return php_ver


def dump_lsapi(ctl_path='/usr/sbin/httpd'):
    """
    Get `httpd -t -D DUMP_RUN_LSAPI` info
    For httpd24 this default path is `/opt/rh/httpd24/root/usr/sbin/httpd`, generated in make_from_templates.sh script
    :param ctl_path: path to httpd (also apachectl may be used)
    :return: dict(
                lsapi_option: value
            )
    """
    apache_conf_data = exec_command.exec_command('{ctl} -t -D DUMP_RUN_LSAPI'.format(ctl=ctl_path))
    try:
        return dict([l.lower().split(' ') for l in apache_conf_data])
    except:
        return dict()


def dump_loaded_modules(ctl_path='/usr/sbin/httpd'):
    """
    Get `httpd -M`
    For httpd24 this default path is `/opt/rh/httpd24/root/usr/sbin/httpd`, generated in make_from_templates.sh script
    :param ctl_path: path to httpd (also apachectl may be used)
    :return: dict(
                apache_module: value
            )
    """
    apache_modules = exec_command.exec_command('{ctl} -M'.format(ctl=ctl_path))
    try:
        return dict([l.lower().split(' ') for l in apache_modules])
    except:
        return dict()


def liblsapi_path():
    """
    Retrieve path to liblsapi, depends on arch
    :return: path to liblsapi
    """
    is_64bits = sys.maxsize > 2**32
    return '/usr/lib{a}/liblscapi.so'.format(a='64' if is_64bits else '')


def rpm_query(pkg):
    """
    Get version-release from rpm -q `pkg`
    :param pkg: package name to query
    :return: version-release
    """
    try:
        ver, rel = ''.join(exec_command.exec_command('/bin/rpm -q ' + pkg + ' --qf %{v}-%{r}')).split('-')
        return '{ver}-{rel}'.format(ver=ver, rel=rel.split('.')[0])
    except ValueError:
        return None


def query_strings(fname, template):
    """
    Filter strings by given template
    Also split string upon given template
    :param fname: path to file
    :param template: template to find in string
    :return: first template occurrence splitted by template
    """
    try:
        return [l for l in strings(fname) if template in l][0].split(template)[1].strip()
    except (IndexError, IOError, OSError):
        return None


def strings(fname, n=6):
    """
    Strings utility analog.
    Finds printable strings in executable
    :param fname: path to file
    :param n: minimum string length
    :return: generator, yeilds string
    """
    with open(fname, errors='ignore') as f:
        result = ""
        for c in f.read():
            if c in string.printable:
                result += c
                continue
            if len(result) >= n:
                yield result
            result = ""
        if len(result) >= n:  # catch result at EOF
            yield result


def count_domains(handler_struct, default_keys, only_lsapi=True):
    """
    Count domains
    :param handler_struct: handler: version: set_of_domains structure
    :param default_keys: sequence of keys to add as default if no `lsapi` found
    :param only_lsapi: return only lsapi statistics
    :return: statistics - number of lsapi domains per version if only_lsapi=True
                          number of lsapi domains per version per handler otherwise
    """
    result_stat = dict()
    for h, data in handler_struct.items():
        result_stat[h] = dict((k, len(v)) for k, v in data.items())

    # if there are no lsapi handler domains, return 0 per each installed version
    try:
        return result_stat['lsapi'] if only_lsapi else result_stat
    except KeyError:
        return dict.fromkeys([x for x in default_keys if x != 'no'], 0)
modlscapi/user/switch_lsapi000075500000067054151677542400012143 0ustar00#!/bin/bash

#%copyright%

VERBOSE=NO
FORCE=NO

for item in "$@"
do
  if [ "$item" == "--verbose" ]; then
    VERBOSE=YES
    echo "Switching verbose level on... done"
    shift
  fi

  if [ "$item" == "--force" ]; then
    FORCE=YES
    echo "Using force-yes option..."
    shift
  fi
done

CMD="$1"

function addString(){
    HANDLR="$1"
    BINARY="$2"
    FL="$3"
    touch "$FL"
    if [ -e "$FL" ] && [ -e "$BINARY" ]; then
        RES=$(cat "$FL" | grep "$HANDLR")
        if [ -z "$RES" ]; then
            echo "$HANDLR $BINARY" >> "$FL"
        fi
    fi
}

function set_handler() {
    PHP="$1"
    HANDLER="$2"
    if [ -z "$PHP" ]; then
        return 1
    fi
    if [ -z "$HANDLER" ]; then
        return 1
    fi
    echo "Setting $PHP to $HANDLER handler..."
    /usr/sbin/whmapi1 php_set_handler version="$PHP" handler="$HANDLER" 2>/dev/null >/dev/null
    return 0
}

function confirm() {
    if [ "$FORCE" == "YES" ]; then
        return 0
    fi

    read -r -p "Do you want to proceed? [y/N] " response </dev/tty
    if [[ "$response" =~ ^([yY][eE][sS]|[yY])+$ ]]; then
        return 0
    else
        return 1
    fi
}

function check_enable_global() {
    YES="$1"
    if [ -e /usr/share/lve/modlscapi/global.enabled -o -n "$YES" ]; then

        echo "mod_lsapi switched to turning on and off through the MultiPHP Manager(/Home/Software/MultiPHP Manager)"
        echo "You are using enabled globally mod_lsapi. Do you want to enable mod_lsapi through MultiPHP Manager?"
        echo "Current PHP will be switched to lsapi handler:"

        /usr/local/cpanel/bin/rebuild_phpconf --current | while read -r line;
        do
            echo "$line" | sed -n '/ cgi/p; /suphp/p'
        done
        echo "If you type N then mod_lsapi will be disabled and you can enable it again from MultiPHP Manager."

        if confirm; then
            /usr/local/cpanel/bin/rebuild_phpconf --current | while read -r line;
            do
                if [[ "$line" == *"suphp"* ]]; then
                    set_handler "$(echo "$line" | cut -d ' ' -f1)" "lsapi"
                fi
                if [[ "$line" == *" cgi"* ]]; then
                    set_handler "$(echo "$line" | cut -d ' ' -f1)" "lsapi"
                fi
            done
        fi
    fi

    if [ -e /etc/apache2/conf.d/lsapi.conf ]; then
        [ "$VERBOSE" = "YES" ] && echo -n "Switching old lsapi configuration off..."
        sed -i '/LoadModule/d'   "/etc/apache2/conf.d/lsapi.conf"
        sed -i '/lsapi_engine/d' "/etc/apache2/conf.d/lsapi.conf"
        sed -i '/AddHandler/d'   "/etc/apache2/conf.d/lsapi.conf"
        sed -i '/AddType/d'      "/etc/apache2/conf.d/lsapi.conf"
        sed -i '/FilesMatch/d'   "/etc/apache2/conf.d/lsapi.conf"
        [ "$VERBOSE" = "YES" ] && echo " done"
    fi

#    [ "$VERBOSE" = "YES" ] && echo "Enabling module..."
#    addString "# Enable mod_lsapi" "" "/etc/apache2/conf.modules.d/001_mod_lsapi.conf"
#    addString "LoadModule lsapi_module modules/mod_lsapi.so" "" "/etc/apache2/conf.modules.d/001_mod_lsapi.conf"
#    [ "$VERBOSE" = "YES" ] && echo " done"
}

function uninstall_lsapi_handler() {

    if [ -e /etc/apache2/conf.modules.d/90-suphp.conf ]; then
        HANDLER="suphp"
    elif [ -e /etc/apache2/conf.modules.d/005_mod_cgid.conf -o \
           -e /etc/apache2/conf.modules.d/005_mod_cgi.conf ]; then
        HANDLER="cgi"
    else
	echo "[WARNING]: Cannot detect a valid handler"
        HANDLER="cgi"
    fi

    echo "You started the uninstallation of the mod_lsapi."
    echo "PHP with enabled lsapi handler will be switched to $HANDLER handler."
    /usr/local/cpanel/bin/rebuild_phpconf --current | while read -r line;
    do
        echo "$line" | sed -n '/lsapi/p'
    done

    /usr/local/cpanel/bin/rebuild_phpconf --current | while read -r line;
    do
        if [[ "$line" == *"lsapi"* ]]; then
            set_handler "$(echo "$line" | cut -d ' ' -f1)" "$HANDLER"
        fi
    done
}

function get_user_config() {
    DOMAIN="$1"
    CLEAN="$2"
    if [ -z "$DOMAIN" ]; then
        return 1
    fi

    UN=$(/scripts/whoowns "$1" | tr -d '\n' | tr -d ' ')
    if [ -z "$UN" ]; then
        return 1
    fi

    HD=$(/usr/share/lve/modlscapi/utils/getdocroot.pm "$UN" "$DOMAIN")
    if [ -z "$HD" ]; then
        return 1
    fi

    if [ -e "$HD/.htaccess" ]; then
        if grep -E '[[:space:]]*lsapi_engine*[[:space:]]On' "$HD/.htaccess" | grep -vq "#"; then
            if grep -E '[[:space:]]*application/x-httpd-lsphp' "$HD/.htaccess" | grep -vq "#"; then
                if [ "$CLEAN" == "Yes" ]; then
                    sed -i '/lsphp -- BEGIN/,/lsphp -- END/d' "$HD/.htaccess"
                    sed -i '/mod_lsapi/d'     "$HD/.htaccess"
                    sed -i '/x-httpd-lsphp/d' "$HD/.htaccess"
                    sed -i '/lsapi_engine/d'  "$HD/.htaccess"
                fi
                return 0
            fi
        fi
    fi
    return 1
}

function count_lsapi_user() {
    ARRAY=( "$@" )
    COUNTER=0
    for vhost in "${ARRAY[@]}";
    do
        if get_user_config "$vhost" "No"; then
            let COUNTER=COUNTER+1
        fi
    done
    if [ $COUNTER -gt 0 ]; then
        return 0
    else
        return 1
    fi
}

function clean_user_conf() {
    ARRAY=( "$@" )
    for vhost in "${ARRAY[@]}";
    do
        get_user_config "$vhost" "Yes"
    done
}

function get_vhosts_by_version() {
    PHP="$1"
    INHERITED=$(/usr/local/cpanel/bin/rebuild_phpconf --current | grep DEFAULT | cut -d ' ' -f3)
    /usr/sbin/whmapi1 php_get_vhosts_by_version version="$PHP" | while read -r line;
    do
        echo "$line" | grep -e '- ' | sed 's/^[ \t]*- //'
    done
    if [ "$PHP" == "$INHERITED" ]; then
        /usr/sbin/whmapi1 php_get_vhosts_by_version version="inherit" | while read -r line;
        do
            echo "$line" | grep -e '- ' | sed 's/^[ \t]*- //'
        done
    fi
}

function check_enable_user() {
    /usr/local/cpanel/bin/rebuild_phpconf --current | while read -r line;
    do
        PHP=$(echo "$line" | cut -d ' ' -f1)
        HANDLER=$(echo "$line" | cut -d ' ' -f3)
        declare -a Array
        Array+=$(get_vhosts_by_version "$PHP")
        if count_lsapi_user ${Array[@]}; then
            echo "Domains that handled by $PHP:"
            for vhost in ${Array[@]};
            do
                if get_user_config "$vhost" "No"; then
                    echo "$vhost - lsapi"
                else
                    echo "$vhost - $HANDLER"
                fi
            done
            echo "There are domains which are using mod_lsapi through --enable-domain option."
            echo "This option is deprecated for EA4 and mod_lsapi switched to turning on and off through"
            echo "the MultiPHP Manager(/Home/Software/MultiPHP Manager)"
            echo "Do you want to enable mod_lsapi through MultiPHP Manager for $PHP?"
            echo "Domains which are using $HANDLER will be switched to lsapi handler too."
            echo "If you type N then mod_lsapi will remain enabled on these domains."
            echo "However, enabling mod_lsapi for new domains is now possible only through MultiPHP Manager."
            if confirm; then
                clean_user_conf ${Array[@]}
                set_handler "$PHP" "lsapi"
            fi
        fi
        unset Array
    done
}



function oldUninstall() {
    [ "$VERBOSE" = "YES" ] && echo "Old mod_lsapi uninstall started"

    if [ -e /etc/apache2/conf.modules.d/90-suphp.conf.cpanel ]; then
        [ "$VERBOSE" = "YES" ] && echo -n "Restoring /etc/apache2/conf.modules.d/90-suphp.conf..."
        mv /etc/apache2/conf.modules.d/90-suphp.conf.cpanel /etc/apache2/conf.modules.d/90-suphp.conf
        [ "$VERBOSE" = "YES" ] && echo " done"
    fi

    if [ -e /etc/container/php.handler ]; then
        [ "$VERBOSE" = "YES" ] && echo -n "Cleaning /etc/container/php.handler"
        sed -i '/x-httpd-ea-php/d' "/etc/container/php.handler"
        [ "$VERBOSE" = "YES" ] && echo " done"
    fi
}

function add_handlers() {
    if [ ! -e /etc/container ]; then
        mkdir -p /etc/container
    fi
    addString "application/x-httpd-ea-php44-lsphp" "/opt/cpanel/ea-php44/root/usr/bin/lsphp" "/etc/container/php.handler"
    addString "application/x-httpd-ea-php51-lsphp" "/opt/cpanel/ea-php51/root/usr/bin/lsphp" "/etc/container/php.handler"
    addString "application/x-httpd-ea-php52-lsphp" "/opt/cpanel/ea-php52/root/usr/bin/lsphp" "/etc/container/php.handler"
    addString "application/x-httpd-ea-php53-lsphp" "/opt/cpanel/ea-php53/root/usr/bin/lsphp" "/etc/container/php.handler"
    addString "application/x-httpd-ea-php54-lsphp" "/opt/cpanel/ea-php54/root/usr/bin/lsphp" "/etc/container/php.handler"
    addString "application/x-httpd-ea-php55-lsphp" "/opt/cpanel/ea-php55/root/usr/bin/lsphp" "/etc/container/php.handler"
    addString "application/x-httpd-ea-php56-lsphp" "/opt/cpanel/ea-php56/root/usr/bin/lsphp" "/etc/container/php.handler"
    addString "application/x-httpd-ea-php70-lsphp" "/opt/cpanel/ea-php70/root/usr/bin/lsphp" "/etc/container/php.handler"
    addString "application/x-httpd-ea-php71-lsphp" "/opt/cpanel/ea-php71/root/usr/bin/lsphp" "/etc/container/php.handler"
    addString "application/x-httpd-ea-php72-lsphp" "/opt/cpanel/ea-php72/root/usr/bin/lsphp" "/etc/container/php.handler"
    addString "application/x-httpd-ea-php73-lsphp" "/opt/cpanel/ea-php73/root/usr/bin/lsphp" "/etc/container/php.handler"
    addString "application/x-httpd-ea-php74-lsphp" "/opt/cpanel/ea-php74/root/usr/bin/lsphp" "/etc/container/php.handler"
    addString "application/x-httpd-ea-php80-lsphp" "/opt/cpanel/ea-php80/root/usr/bin/lsphp" "/etc/container/php.handler"
    addString "application/x-httpd-ea-php81-lsphp" "/opt/cpanel/ea-php81/root/usr/bin/lsphp" "/etc/container/php.handler"
    addString "application/x-httpd-ea-php82-lsphp" "/opt/cpanel/ea-php82/root/usr/bin/lsphp" "/etc/container/php.handler"
    addString "application/x-httpd-ea-php83-lsphp" "/opt/cpanel/ea-php83/root/usr/bin/lsphp" "/etc/container/php.handler"
    addString "application/x-httpd-ea-php84-lsphp" "/opt/cpanel/ea-php84/root/usr/bin/lsphp" "/etc/container/php.handler"

    addString "application/x-httpd-ea-php44___lsphp" "/opt/cpanel/ea-php44/root/usr/bin/lsphp" "/etc/container/php.handler"
    addString "application/x-httpd-ea-php51___lsphp" "/opt/cpanel/ea-php51/root/usr/bin/lsphp" "/etc/container/php.handler"
    addString "application/x-httpd-ea-php52___lsphp" "/opt/cpanel/ea-php52/root/usr/bin/lsphp" "/etc/container/php.handler"
    addString "application/x-httpd-ea-php53___lsphp" "/opt/cpanel/ea-php53/root/usr/bin/lsphp" "/etc/container/php.handler"
    addString "application/x-httpd-ea-php54___lsphp" "/opt/cpanel/ea-php54/root/usr/bin/lsphp" "/etc/container/php.handler"
    addString "application/x-httpd-ea-php55___lsphp" "/opt/cpanel/ea-php55/root/usr/bin/lsphp" "/etc/container/php.handler"
    addString "application/x-httpd-ea-php56___lsphp" "/opt/cpanel/ea-php56/root/usr/bin/lsphp" "/etc/container/php.handler"
    addString "application/x-httpd-ea-php70___lsphp" "/opt/cpanel/ea-php70/root/usr/bin/lsphp" "/etc/container/php.handler"
    addString "application/x-httpd-ea-php71___lsphp" "/opt/cpanel/ea-php71/root/usr/bin/lsphp" "/etc/container/php.handler"
    addString "application/x-httpd-ea-php72___lsphp" "/opt/cpanel/ea-php72/root/usr/bin/lsphp" "/etc/container/php.handler"
    addString "application/x-httpd-ea-php73___lsphp" "/opt/cpanel/ea-php73/root/usr/bin/lsphp" "/etc/container/php.handler"
    addString "application/x-httpd-ea-php74___lsphp" "/opt/cpanel/ea-php74/root/usr/bin/lsphp" "/etc/container/php.handler"
    addString "application/x-httpd-ea-php80___lsphp" "/opt/cpanel/ea-php80/root/usr/bin/lsphp" "/etc/container/php.handler"
    addString "application/x-httpd-ea-php81___lsphp" "/opt/cpanel/ea-php81/root/usr/bin/lsphp" "/etc/container/php.handler"
    addString "application/x-httpd-ea-php82___lsphp" "/opt/cpanel/ea-php82/root/usr/bin/lsphp" "/etc/container/php.handler"
    addString "application/x-httpd-ea-php83___lsphp" "/opt/cpanel/ea-php83/root/usr/bin/lsphp" "/etc/container/php.handler"
    addString "application/x-httpd-ea-php84___lsphp" "/opt/cpanel/ea-php84/root/usr/bin/lsphp" "/etc/container/php.handler"

    addString "application/x-httpd-alt-php44___lsphp" "/opt/cloudlinux/alt-php44/root/usr/bin/lsphp" "/etc/container/php.handler"
    addString "application/x-httpd-alt-php51___lsphp" "/opt/cloudlinux/alt-php51/root/usr/bin/lsphp" "/etc/container/php.handler"
    addString "application/x-httpd-alt-php52___lsphp" "/opt/cloudlinux/alt-php52/root/usr/bin/lsphp" "/etc/container/php.handler"
    addString "application/x-httpd-alt-php53___lsphp" "/opt/cloudlinux/alt-php53/root/usr/bin/lsphp" "/etc/container/php.handler"
    addString "application/x-httpd-alt-php54___lsphp" "/opt/cloudlinux/alt-php54/root/usr/bin/lsphp" "/etc/container/php.handler"
    addString "application/x-httpd-alt-php55___lsphp" "/opt/cloudlinux/alt-php55/root/usr/bin/lsphp" "/etc/container/php.handler"
    addString "application/x-httpd-alt-php56___lsphp" "/opt/cloudlinux/alt-php56/root/usr/bin/lsphp" "/etc/container/php.handler"
    addString "application/x-httpd-alt-php70___lsphp" "/opt/cloudlinux/alt-php70/root/usr/bin/lsphp" "/etc/container/php.handler"
    addString "application/x-httpd-alt-php71___lsphp" "/opt/cloudlinux/alt-php71/root/usr/bin/lsphp" "/etc/container/php.handler"
    addString "application/x-httpd-alt-php72___lsphp" "/opt/cloudlinux/alt-php72/root/usr/bin/lsphp" "/etc/container/php.handler"
    addString "application/x-httpd-alt-php73___lsphp" "/opt/cloudlinux/alt-php73/root/usr/bin/lsphp" "/etc/container/php.handler"
    addString "application/x-httpd-alt-php74___lsphp" "/opt/cloudlinux/alt-php74/root/usr/bin/lsphp" "/etc/container/php.handler"
    addString "application/x-httpd-alt-php80___lsphp" "/opt/cloudlinux/alt-php80/root/usr/bin/lsphp" "/etc/container/php.handler"
    addString "application/x-httpd-alt-php81___lsphp" "/opt/cloudlinux/alt-php81/root/usr/bin/lsphp" "/etc/container/php.handler"
    addString "application/x-httpd-alt-php82___lsphp" "/opt/cloudlinux/alt-php82/root/usr/bin/lsphp" "/etc/container/php.handler"
    addString "application/x-httpd-alt-php83___lsphp" "/opt/cloudlinux/alt-php83/root/usr/bin/lsphp" "/etc/container/php.handler"
    addString "application/x-httpd-alt-php84___lsphp" "/opt/cloudlinux/alt-php84/root/usr/bin/lsphp" "/etc/container/php.handler"

    # Let's add version which still don't exist, for future
    addString "application/x-httpd-ea-php85-lsphp" "/opt/cpanel/ea-php85/root/usr/bin/lsphp" "/etc/container/php.handler"
    addString "application/x-httpd-ea-php86-lsphp" "/opt/cpanel/ea-php86/root/usr/bin/lsphp" "/etc/container/php.handler"
    addString "application/x-httpd-ea-php87-lsphp" "/opt/cpanel/ea-php87/root/usr/bin/lsphp" "/etc/container/php.handler"
    addString "application/x-httpd-ea-php88-lsphp" "/opt/cpanel/ea-php88/root/usr/bin/lsphp" "/etc/container/php.handler"
    addString "application/x-httpd-ea-php89-lsphp" "/opt/cpanel/ea-php89/root/usr/bin/lsphp" "/etc/container/php.handler"
    addString "application/x-httpd-ea-php90-lsphp" "/opt/cpanel/ea-php90/root/usr/bin/lsphp" "/etc/container/php.handler"
    addString "application/x-httpd-ea-php91-lsphp" "/opt/cpanel/ea-php91/root/usr/bin/lsphp" "/etc/container/php.handler"
    addString "application/x-httpd-ea-php92-lsphp" "/opt/cpanel/ea-php92/root/usr/bin/lsphp" "/etc/container/php.handler"
    addString "application/x-httpd-ea-php93-lsphp" "/opt/cpanel/ea-php93/root/usr/bin/lsphp" "/etc/container/php.handler"
    addString "application/x-httpd-ea-php94-lsphp" "/opt/cpanel/ea-php94/root/usr/bin/lsphp" "/etc/container/php.handler"
    addString "application/x-httpd-ea-php95-lsphp" "/opt/cpanel/ea-php95/root/usr/bin/lsphp" "/etc/container/php.handler"
    addString "application/x-httpd-ea-php96-lsphp" "/opt/cpanel/ea-php96/root/usr/bin/lsphp" "/etc/container/php.handler"
    addString "application/x-httpd-ea-php97-lsphp" "/opt/cpanel/ea-php97/root/usr/bin/lsphp" "/etc/container/php.handler"
    addString "application/x-httpd-ea-php98-lsphp" "/opt/cpanel/ea-php98/root/usr/bin/lsphp" "/etc/container/php.handler"
    addString "application/x-httpd-ea-php99-lsphp" "/opt/cpanel/ea-php99/root/usr/bin/lsphp" "/etc/container/php.handler"

    addString "application/x-httpd-ea-php85___lsphp" "/opt/cpanel/ea-php85/root/usr/bin/lsphp" "/etc/container/php.handler"
    addString "application/x-httpd-ea-php86___lsphp" "/opt/cpanel/ea-php86/root/usr/bin/lsphp" "/etc/container/php.handler"
    addString "application/x-httpd-ea-php87___lsphp" "/opt/cpanel/ea-php87/root/usr/bin/lsphp" "/etc/container/php.handler"
    addString "application/x-httpd-ea-php88___lsphp" "/opt/cpanel/ea-php88/root/usr/bin/lsphp" "/etc/container/php.handler"
    addString "application/x-httpd-ea-php89___lsphp" "/opt/cpanel/ea-php89/root/usr/bin/lsphp" "/etc/container/php.handler"
    addString "application/x-httpd-ea-php90___lsphp" "/opt/cpanel/ea-php90/root/usr/bin/lsphp" "/etc/container/php.handler"
    addString "application/x-httpd-ea-php91___lsphp" "/opt/cpanel/ea-php91/root/usr/bin/lsphp" "/etc/container/php.handler"
    addString "application/x-httpd-ea-php92___lsphp" "/opt/cpanel/ea-php92/root/usr/bin/lsphp" "/etc/container/php.handler"
    addString "application/x-httpd-ea-php93___lsphp" "/opt/cpanel/ea-php93/root/usr/bin/lsphp" "/etc/container/php.handler"
    addString "application/x-httpd-ea-php94___lsphp" "/opt/cpanel/ea-php94/root/usr/bin/lsphp" "/etc/container/php.handler"
    addString "application/x-httpd-ea-php95___lsphp" "/opt/cpanel/ea-php95/root/usr/bin/lsphp" "/etc/container/php.handler"
    addString "application/x-httpd-ea-php96___lsphp" "/opt/cpanel/ea-php96/root/usr/bin/lsphp" "/etc/container/php.handler"
    addString "application/x-httpd-ea-php97___lsphp" "/opt/cpanel/ea-php97/root/usr/bin/lsphp" "/etc/container/php.handler"
    addString "application/x-httpd-ea-php98___lsphp" "/opt/cpanel/ea-php98/root/usr/bin/lsphp" "/etc/container/php.handler"
    addString "application/x-httpd-ea-php99___lsphp" "/opt/cpanel/ea-php99/root/usr/bin/lsphp" "/etc/container/php.handler"

    addString "application/x-httpd-alt-php85___lsphp" "/opt/cloudlinux/alt-php85/root/usr/bin/lsphp" "/etc/container/php.handler"
    addString "application/x-httpd-alt-php86___lsphp" "/opt/cloudlinux/alt-php86/root/usr/bin/lsphp" "/etc/container/php.handler"
    addString "application/x-httpd-alt-php87___lsphp" "/opt/cloudlinux/alt-php87/root/usr/bin/lsphp" "/etc/container/php.handler"
    addString "application/x-httpd-alt-php88___lsphp" "/opt/cloudlinux/alt-php88/root/usr/bin/lsphp" "/etc/container/php.handler"
    addString "application/x-httpd-alt-php89___lsphp" "/opt/cloudlinux/alt-php89/root/usr/bin/lsphp" "/etc/container/php.handler"
    addString "application/x-httpd-alt-php90___lsphp" "/opt/cloudlinux/alt-php90/root/usr/bin/lsphp" "/etc/container/php.handler"
    addString "application/x-httpd-alt-php91___lsphp" "/opt/cloudlinux/alt-php91/root/usr/bin/lsphp" "/etc/container/php.handler"
    addString "application/x-httpd-alt-php92___lsphp" "/opt/cloudlinux/alt-php92/root/usr/bin/lsphp" "/etc/container/php.handler"
    addString "application/x-httpd-alt-php93___lsphp" "/opt/cloudlinux/alt-php93/root/usr/bin/lsphp" "/etc/container/php.handler"
    addString "application/x-httpd-alt-php94___lsphp" "/opt/cloudlinux/alt-php94/root/usr/bin/lsphp" "/etc/container/php.handler"
    addString "application/x-httpd-alt-php95___lsphp" "/opt/cloudlinux/alt-php95/root/usr/bin/lsphp" "/etc/container/php.handler"
    addString "application/x-httpd-alt-php96___lsphp" "/opt/cloudlinux/alt-php96/root/usr/bin/lsphp" "/etc/container/php.handler"
    addString "application/x-httpd-alt-php97___lsphp" "/opt/cloudlinux/alt-php97/root/usr/bin/lsphp" "/etc/container/php.handler"
    addString "application/x-httpd-alt-php98___lsphp" "/opt/cloudlinux/alt-php98/root/usr/bin/lsphp" "/etc/container/php.handler"
    addString "application/x-httpd-alt-php99___lsphp" "/opt/cloudlinux/alt-php99/root/usr/bin/lsphp" "/etc/container/php.handler"


}

if [ "$CMD" == "--uninstall" ]; then
    [ "$VERBOSE" = "YES" ] && echo "mod_lsapi uninstall started"

#    if [ -e /etc/apache2/conf.modules.d/001_mod_lsapi.conf ]; then
#        [ "$VERBOSE" = "YES" ] && echo -n "Removing /etc/apache2/conf.modules.d/001_mod_lsapi.conf..."
#        rm -f /etc/apache2/conf.modules.d/001_mod_lsapi.conf
#        [ "$VERBOSE" = "YES" ] && echo " done"
#    fi

    [ "$VERBOSE" = "YES" ] && echo -n "Changing enabled lsapi handler to default handler"
    uninstall_lsapi_handler
    [ "$VERBOSE" = "YES" ] && echo " done"

    #Disable all domains if exists in htaccess
    /usr/share/lve/modlscapi/utils/getdocroots.pm | while read line;
    do
    if [ -e "$line/.htaccess" ]; then
        [ "$VERBOSE" = "YES" ] && echo -n "Disabling mod_lsapi in $line..."
        sed -i '/lsphp -- BEGIN/,/lsphp -- END/d' "$line/.htaccess"
        sed -i '/x-httpd-lsphp/d' "$line/.htaccess"
        sed -i '/lsapi_engine/d' "$line/.htaccess"
        sed -i '/mod_lsapi/d' "$line/.htaccess"
        [ "$VERBOSE" = "YES" ] && echo " done"
    fi
    done

############ REMOVE PATCH FOR CPANEL, this part should be removed when cpanel will be provided with our patch
#    [ "$VERBOSE" = "YES" ] && echo -n "Restoring /usr/local/cpanel/Cpanel/WebServer/Supported/apache.pm..."
#    /usr/share/lve/modlscapi/utils/cpanel_hooks_manage.py -d
#    [ "$VERBOSE" = "YES" ] && echo " done"
######################

    if [ -e /etc/container/php.handler ]; then
        [ "$VERBOSE" = "YES" ] && echo -n "Cleaning /etc/container/php.handler"
        sed -i '/x-httpd-ea-php/d' "/etc/container/php.handler"
        [ "$VERBOSE" = "YES" ] && echo " done"
    fi

    [ "$VERBOSE" = "YES" ] && echo "Rebuilding httpd config..."
    /scripts/rebuildhttpdconf
    [ "$VERBOSE" = "YES" ] && echo "Rebuilding httpd config done"

    echo "Reconfiguration completed"
    exit
fi

if [ "$CMD" == "--setup" ]; then

    oldUninstall

    [ "$VERBOSE" = "YES" ] && echo "mod_lsapi setup started"

    [ "$VERBOSE" = "YES" ] && echo -n "Adding php handlers..."
    add_handlers
    [ "$VERBOSE" = "YES" ] && echo " done"

############ INSTALL PATCH FOR CPANEL, this part should be removed when cpanel will be provided with our patch
#    if [ ! -e /usr/local/cpanel/Cpanel/WebServer/Supported/apache/Handler/lsapi.pm ]; then
#        [ "$VERBOSE" = "YES" ] && echo -n "Adding lsapi.pm to Cpanel..."
#        cp /usr/share/lve/modlscapi/tars/lsapi.pm /usr/local/cpanel/Cpanel/WebServer/Supported/apache/Handler/lsapi.pm
#        [ "$VERBOSE" = "YES" ] && echo " done"
#    fi

#    [ "$VERBOSE" = "YES" ] && echo -n "Patching Cpanel for lsapi handler..."
#    cp /usr/local/cpanel/Cpanel/WebServer/Supported/apache.pm /usr/local/cpanel/Cpanel/WebServer/Supported/apache.pm.bak
#    /usr/share/lve/modlscapi/hooks/lsapi_patch_cpanel_hook.sh
#    /usr/share/lve/modlscapi/utils/cpanel_hooks_manage.py -i
#    [ "$VERBOSE" = "YES" ] && echo " done"
########################

    [ "$VERBOSE" = "YES" ] && echo "Checking for enabled global module..."
    check_enable_global
    [ "$VERBOSE" = "YES" ] && echo " done"

    [ "$VERBOSE" = "YES" ] && echo "Checking for enabled domain module..."
    check_enable_user
    [ "$VERBOSE" = "YES" ] && echo " done"

    [ "$VERBOSE" = "YES" ] && echo "Rebuilding httpd config..."
    /scripts/rebuildhttpdconf
    [ "$VERBOSE" = "YES" ] && echo "Rebuilding httpd config done"

    if [ ! -e /usr/local/bin/lsphp ]; then
        [ "$VERBOSE" = "YES" ] && echo "Setting up cl-selector for lsphp..."
        cp /opt/alt/php56/usr/bin/lsphp /usr/local/bin/
        ln -s /usr/local/bin/lsphp /etc/cl.selector/lsphp
        if [ -e /usr/sbin/cagefsctl ]; then
            /usr/sbin/cagefsctl --update-etc
        fi
        RES=""
        if [ -e /etc/cl.selector/native.conf ]; then
            RES=`cat /etc/cl.selector/native.conf | grep lsphp=/usr/local/bin/lsphp `
        fi
        if [ -z "$RES" ]; then
            echo "lsphp=/usr/local/bin/lsphp" >> /etc/cl.selector/native.conf
        fi
        if [ -e /usr/sbin/cagefsctl ]; then
            /usr/sbin/cagefsctl --setup-cl-selector
        fi
        [ "$VERBOSE" = "YES" ] && echo "Setting up cl-selector for lsphp done"
    fi

    CSF_CONFIG="/etc/csf/csf.pignore"
    CSF_MAIN_RULE="exe:/usr/local/bin/lsphp"
    CSF_ALT_RULE="pexe:/opt/alt/php.*/usr/bin/lsphp"
    CSF_SELECTOR_RULE="exe:/usr/selector/lsphp"
    CSF_EA_RULE="pexe:/opt/cpanel/ea-php*/root/usr/bin/lsphp"

    if [ -f "${CSF_CONFIG}" ]; then
        [ "$VERBOSE" = "YES" ] && echo "Setting up lfd service for lsphp..."
        [ -e "${CSF_CONFIG}.bkp" ] || cp "${CSF_CONFIG}" "${CSF_CONFIG}.bkp"
        grep -v '^[[:space:]]*#' "${CSF_CONFIG}" | grep -Fq "${CSF_MAIN_RULE}" || echo "${CSF_MAIN_RULE}" >> "$CSF_CONFIG"
        grep -v '^[[:space:]]*#' "${CSF_CONFIG}" | grep -Fq "${CSF_SELECTOR_RULE}" || echo "${CSF_SELECTOR_RULE}" >> "$CSF_CONFIG"
        grep -v '^[[:space:]]*#' "${CSF_CONFIG}" | grep -Fq "${CSF_EA_RULE}" || echo "${CSF_EA_RULE}" >> "$CSF_CONFIG"
        grep -v '^[[:space:]]*#' "${CSF_CONFIG}" | grep -Fq "${CSF_ALT_RULE}" || echo "${CSF_ALT_RULE}" >> "$CSF_CONFIG"
        service lfd restart 2>/dev/null >/dev/null
        [ "$VERBOSE" = "YES" ] && echo "Setting up lfd service for lsphp done"
    fi

    echo "Reconfiguration completed"
    exit
fi

if [ "$CMD" == "--setup-light" ]; then

    [ "$VERBOSE" = "YES" ] && echo -n "Adding php handlers..."
    add_handlers
    [ "$VERBOSE" = "YES" ] && echo " done"

    /usr/share/lve/modlscapi/utils/getdocroots.pm | while read HD;
    do
    if [ -e "$HD/.htaccess" ]; then
        [ "$VERBOSE" = "YES" ] && echo -n "Removing old mod_lsapi configuration (lsapi_engine off) in $HD..."
        if grep -qE '[[:space:]]*lsapi_engine*[[:space:]]*off' "$HD/.htaccess"; then
            sed -i '/[[:space:]]*lsapi_engine*[[:space:]]*off/d' "$HD/.htaccess"
        fi
        if grep -qE '[[:space:]]*lsapi_engine*[[:space:]]*Off' "$HD/.htaccess"; then
            sed -i '/[[:space:]]*lsapi_engine*[[:space:]]*Off/d' "$HD/.htaccess"
        fi
    fi
        [ "$VERBOSE" = "YES" ] && echo " done"
    done

#    [ "$VERBOSE" = "YES" ] && echo -n "Patching Cpanel for lsapi handler..."
#    cp /usr/local/cpanel/Cpanel/WebServer/Supported/apache.pm /usr/local/cpanel/Cpanel/WebServer/Supported/apache.pm.bak
#    /usr/share/lve/modlscapi/hooks/lsapi_patch_cpanel_hook.sh
#    /usr/share/lve/modlscapi/utils/cpanel_hooks_manage.py -i
#    [ "$VERBOSE" = "YES" ] && echo " done"

    exit
fi

if [ "$CMD" == "--enable-domain" ]; then
    echo "Deprecated for EA4, use MultiPHP Manager."
    exit
fi

if [ "$CMD" == "--disable-domain" ]; then
    echo "Deprecated for EA4, use MultiPHP Manager."
    exit
fi

if [ "$CMD" == "--enable-global" ]; then
    check_enable_global "YES"
    exit
fi

if [ "$CMD" == "--disable-global" ]; then
    echo "Deprecated for EA4, use MultiPHP Manager."
    exit
fi

if [ "$CMD" == "--build-native-lsphp" ]; then
    echo "Deprecated for EA4"
    exit
fi

echo "$0 [OPTION]"
echo "utility for switching mod_lsapi on cPanel on or off (without mod_ruid2):"
echo "--setup - setup mod_lsapi configurations for Apache"
echo "--setup-light - setup only the EasyApache 4 feature"
echo "--uninstall - uninstall mod_lsapi from Apache"
echo "--enable-domain - enable mod_lsapi for an individual domain"
echo "--disable-domain - disable mod_lsapi for an individual domain"
echo "--enable-global - sets up mod_lsapi as a default way to serve PHP, making it enabled for all domains. Once that mode is enabled, you cannot disable mod_lsapi for individual domains"
echo "--disable-global - disable mod_lsapi as a default way to serve PHP, disabling mod_lsapi for all domains, including those selected previously using --enable-domain"
echo "--build-native-lsphp - build native lsphp for cPanel"
echo "--verbose - switch verbose logging on"
echo "--force - only with setup option (EA4)"
echo "--help - show this help message"
modlscapi/user/__pycache__/mod_lsapi_stat.cpython-36.pyc000064400000024167151677542400017362 0ustar003

�7�i�:�@s�ddlZddlZddlZddlZddlZddlZddlmZddlm	Z	ddlm
Z
ddlmZddlm
Z
mZmZmZmZddlmZmZmZmZmZmZmZddlZGdd	�d	e�Zddd�Zddd�Zddd�Zddd�Zdd�Z dd�Z!dd�Z"dS)�N)�get_cp)�criu_settings_malfunctions)�lsapi_settings_malfunctions)�liblsapi_malfunctions)�
cpanel_whmapi�plesk_bin_php_handler�dump_loaded_modules�
dump_lsapi�
query_strings)�get_da_domains�get_da_php_options�read_da_config�
liblsapi_path�pretty_version_keys�
count_domains�StatUtilsExceptionc@seZdZdS)�ModLsapiStatExceptionN)�__name__�
__module__�__qualname__�rr�//usr/share/lve/modlscapi/user/mod_lsapi_stat.pyrsrFcCsd}i}t�}yd|jdkr8tjjd�r2t�|d<qpd}n8|jdkrNt�|d<n"|jdkrdt�|d<ndj|j�}Wn*t	k
r�}zt
|�}WYdd}~XnX|dk	r�t�|d<||d	<t
|�|d
<|jt
��|jt��|jdt|dj��i�t|�|�rtj|dd
�S|SdS)a�
    Return statistics data
    :param as_json: return json string if True, dict otherwise
    :return: statistics data dict(
                `controlPanel`: EA3/EA4/Plesk/DirectAdmin/Unknown CP,
                `criu`: dict(
                        `status`: running/stopped,
                        `version`: version
                    ),
                `domainStat`: dict(
                        `version`: `domains_num`,
                        ...
                    ) or dict(`error`: description) if some error occurred,
                `lsapiConf`: dict(
                        lsapi_with_connection_pool: on/off,
                        lsapi_criu: on/off
                    )
            ) or its json-packed version
    N�cPanelz/etc/cpanel/ea4/is_ea4Z
domainStatz7mod_lsapi domains stat is currently unsupported for EA3�Plesk�DirectAdminz7mod_lsapi domains stat is currently unsupported for {0}ZdomainStatErrorZcontrolPanelZtotalDomainT)Z	sort_keys)r�name�os�path�exists�get_cpanel_ea4_stat�get_plesk_stat�get_da_stat�formatr�str�dict�update�get_lsapi_conf�
get_crui_stat�sum�values�analyze_malfunctions�json�dumps)�as_json�errorZstatsZ
control_panel�errr�get"s4



r0Tc
Cs`t�}t�}t�}�y
td�jd�}x@|D]8}|j|jd�t��j|jd��|jd�||jd�<q*Wtd�jd�}tdd	�|D��}t|j��}	x6|j�D]*\}
}|j|t��j	|
|j|
t��i�q�W|r�t
j|||�}|}x:|j�D].\}
}|j	tjt|	�j
t|j���t���q�WWn4tttfk
�rRtd
jtj�jd����YnXt||	|�S)aY
    Collect mod_lsapi statistics for cPanel EA4 through WHM API
    :param lsapi_only: return only lsapi domains statistics if True, or full statistics if False
    :param with_selector: take into account the statistics of php selector
    :return: if lsapi_only return lsapi domains per version
                stat dict(
                    `version`: `domains_num`,
                    ...
                )
            else return full statistics per handler
                stat dict(
                    `handler`: {`version`: `domains_num`, ... }
                    ...
                )
    Zphp_get_vhost_versionsZversions�version�vhostZaccountZphp_get_handlers�version_handlerscSs g|]}|jd�|jd�f�qS)r1Zcurrent_handler)r0)�.0�hrrr�
<listcomp>tsz'get_cpanel_ea4_stat.<locals>.<listcomp>��
)r$rr0�
setdefault�set�add�list�keys�itemsr%�selector_usage_libZea4_selector_check�fromkeys�
difference�KeyError�	TypeErrorrr�join�	traceback�
format_exc�splitr)�
lsapi_only�
with_selectorZdomains_per_versionZ
handlers_statZdomain_usersZvhosts_datar2Z
handlers_datar3Zall_versionsZver�handlerZs_checked_version_handlersr5�vrrrrYs*
 &.rc
Csd}d}t�}y�td�}dd�|D�}x||D]t}|jd�}dj|jd�jd	�d
d��}	tdd�td
|d�D��}
d|kr�d}|j|	d�}n|j|	d�}|
||<q,W|r�tj||�}Wn8t	t
tttj
fk
r�tdjtj�jd����YnXddd�|j�D�i}|�r|dS|S)a
    Collect mod_lsapi statistics for Plesk
    Collects lsapi domains ONLY
    :param lsapi_only: return only lsapi domains statistics if True, or full statistics if False
    :param with_selector: take into account the statistics of php selector
    :return: if lsapi_only return lsapi domains per version dict(
                    `version`: `domains_num`
                    ...
                )
            else return stats with handler dict(
                    `lsapi`: {`version`: `domains_num`, ... }
                )
    z
alt-php{v}z	alt-php56r<cSsg|]}d|jd�kr|�qS)Zlsphp�id)r0)r4r5rrrr6�sz"get_plesk_stat.<locals>.<listcomp>rLr7ZfullVersion�.N�cSsg|]}|jd��qS)�domain)r0)r4�drrrr6�sz	get-usage)rLZcustom)rKr8ZlsapicSsi|]\}}t|�|�qSr)�len)r4�krKrrr�
<dictcomp>�sz"get_plesk_stat.<locals>.<dictcomp>���)r$rr0rDrGr:r"r?Zplesk_selector_checkrBrC�AttributeErrorrZSelectorStatExceptionrrErFr>)
rHrIZhandler_tmplZcustom_versionZdomain_version_statZall_handlersZlsphp_handlersrJZ
handler_idZphp_version�domainsZ
version_idZresult_statrrrr �s*

r cs d}t�}�y�t�}|dd}|dd}|dd}|dd}t|�}	t|�}
t�}t���fdd�|j�D�|dkr�|	�i}|	�i||<�n|dkr�|
�i}|
�i||<n�|	t�|
t�i}x�|j�D]�\}
}x�|D]x}|j|
|d	�}y<t|�\}}|j	|d
�}t||d�}|j
|�j|�Wq�tj
k
�rR|j
|	�j|�Yq�Xq�Wq�W||k�r�|	||	|
||
i||<n,|	||	|
t�i||<|
||
|	t�i||<|�r�tj|j
|	�||	�}|j
|�j|�Wn6ttttfk
�rtdjtj�jd����YnXt||	|
g|�S)
aN
    Collect lsapi domains statistics fro DirectAdmin
    :param lsapi_only: return only lsapi domains statistics if True, or full statistics if False
    :param with_selector: take into account the statistics of php selector
    :return: if lsapi_only return lsapi domains per version
                stat dict(
                    `version`: `domains_num`,
                    ...
                )
            else return full statistics per handler
                stat dict(
                    `handler`: {`version`: `domains_num`, ... }
                    ...
                )
    z>/usr/local/directadmin/data/users/{user}/domains/{domain}.confrNr1�rJcsg|]}�j|��qSr)r%)r4rK)�joinedrrr6�szget_da_stat.<locals>.<listcomp>�no)�userrOZphp1_selectr7r8)r$rrrr:r)r>r"r
Zgetintr0r;�configparserZ
NoOptionErrorr?Zda_selector_checkr%rBrCrUrrrDrErFrGr)rHrIZdomain_conf_pathZhandler_statZphp_optionsZphp1_verZphp2_verZphp1_handlerZphp2_handlerZ
php1_labelZ
php2_labelZuser_domainsZversion_statrZrVrOZconfig_pathZconf_parserZglobal_sectionZphp_settingr1Zselector_updatedr)rXrr!�sV

r!cCsndjtjd��}y"tjdddgtjtjd�d}Wntjk
rLd}YnXd||rd|jd	�d
ndd�iS)
z�
    Get criu service info
    :return: dict(
                `criu`: dict(
                    `status`: running/stopped,
                    `version`: version
                )
            )
    r7z/usr/sbin/criu -Vz
/sbin/service�criu�status)�stdout�stderrZrunning�stopped� rNz
not installed)r]r1)rD�exec_command�
subprocessZ
check_callZDEVNULLZCalledProcessErrorrG)Zcriu_versionZcriu_service_statusrrrr's

r'cCsTt�}t�}|jdd�}|jdd�|jdd�d�|jdd�tt�d�|sLd	nd
d�S)al
    Retrieve lsapi configuration:
    - crui
    - connection pool
    and versions of module and library
    :return: dict(
                lsapiConf: dict(
                    lsapi_with_connection_pool: on/off,
                    lsapi_criu: on/off
                ),
                modVersion: version,
                libVersion: version
            )
    Zlsapi_moduleN�
lsapi_criuZoff�lsapi_with_connection_pool)rdrer1Zliblsapi_versionZdisabledZenabled)�	lsapiConfZ
modVersion�
libVersion�	modStatus)r	rr0r
r)Zapache_confZapache_modulesZ
mod_statusrrrr&s


r&cstt���fdd�}dj|dd|ddd�}|d	}|t|d
�|t|�|t|��sbdS|jd�i�dS)
z�
    Detect configuration malfunctions and update resulting statistics dict accordingly
    For now only criu malfunctions are presented
    See malfunctions in dashboard_malfunctions.py module
    :param stats_dict: resulting statistics dict
    cs,y�j||�Wntk
r&YnXdS)N)�appendrB)Zmalfunc_dict�key)�malfunctionsrr�add_malfunctionHsz-analyze_malfunctions.<locals>.add_malfunctionz{opt}_{serv}rfrdr\r])�optZservrhrgNrk)r<r"rrrr%)Z
stats_dictrlZ
criu_settingsZlsapi_settingsr)rkrr*?s

r*)F)TT)TT)TT)#rr+rErcr[rb�lve_diagnosticrZdashboard_malfunctionsrrr�
stat_utilsrrrr	r
rrr
rrrrr?�	Exceptionrr0rr r!r'r&r*rrrr�<module>s($
7
1
,
O modlscapi/user/__pycache__/exec_command.cpython-36.pyc000064400000003330151677542400016767 0ustar003

�7�i~�@sDddlZddlZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�ZdS)�NcCs|ddkr|dkrdSdS)Nr�"��)�line�isquotfrr�-/usr/share/lve/modlscapi/user/exec_command.py�
first_quotsrcCs$|t|�ddkr |dkr dSdS)Nrrr)�len)rrrrr�	last_quot
sr
cCs�|jd�}d}d}g}x�tt|��D]p}||dkr$t|||�dkrTd}||}q$t|||�dkr�d}|d||7}|j|�q$|j||�q$Wt|�dS)N� r�r)�split�ranger	rr
�append�print)�commandZisquot�res�result�irrr�
parse_commands"

rcCs�g}yNtj|dtjtjd�}x2|jj�}|s.P|j�dkr|j|j�j��qWWn2tk
r�}zt	dt
|��WYdd}~XnX|S)NT)�shell�stdout�stderrrzCall process error: )�
subprocess�Popen�PIPEr�readline�stripr�decode�	Exceptionr�str)rr�p�output�instrrr�exec_command's
"r$cCs
tj|�S)z�
    Run command via os.system and return its exit status

    :param command: Command to execute in a subshell
    :return: Exit status of the command process
    )�os�system)rrrr�exec_command_out6sr'cCs&t|�}x|D]}||kr|SqWdS)Nr���)r$)rZ	substringrrrrr�exec_command_find_substring@s

r))r%rrr
rr$r'r)rrrr�<module>s
modlscapi/user/__pycache__/lve_diagnostic.cpython-36.pyc000064400000032163151677542400017345 0ustar003

�7�i�5�@s�ddlZddlmZdd�ZGdd�d�ZGdd�d�Zd	d
�Zdd�ZGd
d�d�ZGdd�d�Z	Gdd�de	�Z
Gdd�de	�ZGdd�de	�ZGdd�de	�Z
Gdd�de	�ZGdd�de	�ZGdd�de	�Zdd �Zd!d"�Zd#d$�ZdS)%�N)�versioncCstj|�}|j�S)N)�os�popen�read)�strZhandle�r�//usr/share/lve/modlscapi/user/lve_diagnostic.py�myExecs
r	c@s(eZdZdZdZdZdZdZdd�ZdS)�	LiteSpeedF�UnknowncCs^tjjd�|_tjjd�rZttd�jd��dkrZd|_dtd�k|_d	td
�k|_td�}dS)Nz/usr/local/lswsz/usr/local/lsws/bin/litespeedzps aux|grep litespeed�
�T�1z4grep enableLVE /usr/local/lsws/conf/httpd_config.xml�0z$grep -i phpSuexec\> httpd_config.xmlz%file -L /usr/local/lsws/bin/litespeed)	r�path�isfile�exist�lenr	�split�lve�suexec)�selfZarchrrr�__init__s"zLiteSpeed.__init__N)	�__name__�
__module__�__qualname__rrrrZcorrectArchrrrrrr
sr
c@steZdZdZdZdZdZdZdZdZ	dZ
dZdZdZ
dZdZdZdd�Zdd�Zdd�Zd	d
�Zdd�Zd
d�ZdS)�
ApacheInfoFrcCs|j|�dS)N)�detect)rrrrrr8szApacheInfo.__init__cCs2tj|j�tjd�krdStdd|jj�dSdS)Nz2.2.0Ti�z=Unable to determine list of loaded modules, apache version %sF)r�LooseVersion�
print_warning�apache)rrrr�
check_version;szApacheInfo.check_versioncCst|jjd|d�dk�S)N� �_moduler
���)r�modules�find)r�namerrr�isModuleBszApacheInfo.isModulecCsv|jrr|jd�|_|jd�|_|jd�|_|jd�|_|jd�|_|jd�|_|jd�|_|jd�|_	|jd	�|_
dS)
N�
hostinglimits�fcgidZphp5�cgi�cgid�suPHP�fastcgi�disable_suexecr)�
moduleInfor(r)r*�php_dsor+r,r-r.r/r)rrrr�parseModulesEszApacheInfo.parseModulescCsHtjj|�|_|j�rDt|d�}|jd�dkr�d}x.|jd�D] }|jd�dkr@|jdd�}Pq@W|dk	r�td|d|d	�t|d�}|dk	r�td|d
|�|jd�}xX|D]P}|jd�dkr�||jd�d
d�|_|jd�dkr�||j	d�d
d�|_
q�Wtj|j�tjd�k�rDd|_t|d�|_
|j�dS)Nz -V 2>&1zSyntax errorrr"z
lsapi.conf�:�zmv z.SAVEz.SAVE rzServer version:r
�/zServer MPM:z2.2Tz -M 2>&1r$r$)rrrrr	r&r�replacer�rfind�mpmrr0r%r2)rr�tmpZ
lsapi_conf�s�lines�linerrrrQs0

zApacheInfo.detectcCs�|jrxd|jd|jd|jd|jd|jd|jd|jd|jd	|j	d
|j
d|j}|SdSdS)NzApache verion: z, mpm=z, hostinglimits=z, cgi=z, cgid=z, fcgi=z
, fastcgi=z
, php DSO=z, suphp=z	, suexec=z, disable_suexec=)rrr8r)r+r,r*r.r1r-rr/)rrrrrrksnzApacheInfo.strN)rrrrrr8r)r*r+r1r,r-r.r/rr0r%rr!r(r2rrrrrrr(s(rcCs$td|�t||�td|�dS)NzCODE: z
Solution: )�print)�code�	error_str�
error_listZsolutionrrr�print_errorzs
rAcCstd|�t||�dS)Nz	WARNING: )r=)r>r?r@rrrr�s
rc@s4eZdZdZdZdZdZdd�Zdd�Zdd�Z	dS)	�KernelNFcCs^td�jd�|_|jjd�|_|jdkrN|j|jdd�|_tjjd�|_	nd|jk|_
dS)	Nz
/bin/uname -rrrr
�z/proc/lve/listZstabr$)r	�rstrip�
kernelNamer&�isLVEKernelrrrr�isLVEEnabled�
isVZKernel)rrrrr�s
zKernel.__init__cCs�|jr�|jr�tj|j�tjd�kr&dStj|j�tjd�krNtdd|jd�q�tj|j�tjd�krvtdd	|jd�q�td
d|jd�q�tdd
fd�n&|jr�tddfd�ntdd|jd�dS)Nz0.8.28Tz0.8.0i�z*You are running bugy kernel LVE version %szUpgrade Kernelz0.7.0i�zoYou are running old kernel LVE version %s
 That version doesn't support multiple cores per LVE or memory limitsi�z5You are running very old, bugy kernel, LVE version %si�zLVE is not enabledzACheck /etc/sysconfig/lve file, and make sure lve rpm is installediMzYou are running VZ or OpenVZzWCloudLinux is not compatible, see http://www.cloudlinux.com/vz-compat.php for more infoi�z9You are not running CloudLinux kernel. Your kernel is: %szCheck /boot/grub/grub.conf)rGrrrArH)rrrr�check�s$
zKernel.checkcCsNd}|jr|d|jd7}n,|jr8|d|jd7}n|d|jd7}|S)NzKernel: zOK (�)zVZ (z	Unknown ()rGrrHrE)r�resultrrrr�sz
Kernel.str)
rrrrrFrHrGrrIrrrrrrB�s	rBc@s8eZdZdZdZdZdd�Zdd�Zdd	�Zd
d�Z	dS)�CPz
Unknown CPrNcCstd�|_d|_t�|_dS)Nz/usr/sbin/apachectlz
/etc/httpd)rr �apache_pathrB�kernel)rrrrr�s
zCP.__init__cCs|jd|jd|jj�S)Nr")r'rrNr)rrrrr�szCP.strcCs|jS)N)r')rrrr�__str__�sz
CP.__str__cCs|jj�dS)N)rNrI)rrrrrI�szCP.check)
rrrr'rrNrrrOrIrrrrrL�srLcs(eZdZdZ�fdd�Zdd�Z�ZS)�CPanel�cPanelcsLtt|�j�td�|_td�|_tjj	d�r<d|_
d|_nd|_
d|_dS)Nz/usr/local/cpanel/cpanel -Vz/usr/local/bin/apachectlz/etc/cpanel/ea4/is_ea4ZEA4z/etc/apache2ZEA3z/usr/local/apache)�superrPrr	rrr rr�exists�earM)r)�	__class__rrr�s

zCPanel.__init__cCs|jS)N)rT)rrrrrO�szCPanel.__str__)rrrr'rrO�
__classcell__rr)rUrrP�srPcs eZdZdZ�fdd�Z�ZS)�Pleskcs4tt|�j�td�}|jd�d|_td�|_dS)Nz/bin/cat /usr/local/psa/versionr"rz/usr/sbin/apachectl)rRrWrr	rrrr )rr9)rUrrr�szPlesk.__init__)rrrr'rrVrr)rUrrW�srWcs eZdZdZ�fdd�Z�ZS)�DirectAdmincsftt|�j�td�}|jd�}d|_td�|_x0|D](}|jd�dkr6|jd�dj	�|_Pq6WdS)	Nz1/usr/local/directadmin/custombuild/build versionsrrz/usr/sbin/apachectlz!Installed version of DirectAdmin:r
r3r$)
rRrXrr	rrrr r&�strip)rr9�item)rUrrr�s


zDirectAdmin.__init__)rrrr'rrVrr)rUrrX�srXcs(eZdZdZ�fdd�Zdd�Z�ZS)�HSpherezH-Spherecs8tt|�j�td�}|jd�dj�|_|j�|_dS)NzA/bin/cat /hsphere/local/home/cpanel/shiva/psoft_config/HS_VERSIONrr
)	rRr[rr	rrYr�get_apache_typer )rr9)rUrrr�szHSphere.__init__cCs~tjjd�rvtd�}|j�}|j�xP|D]H}|jd�dj�}|jd�dj�}|dkr*|dkrjtd�Std�Sq*Wtd	�S)
Nz#/hsphere/shared/scripts/scripts.cfg�=rr
Zapache_versionrz /hsphere/shared/apache/bin/httpdz%/hsphere/shared/apache2/bin/apachectlr4)	rrr�open�	readlines�closerrYr)r�fr;rZ�key�valuerrrr\�s
zHSphere.get_apache_type)rrrr'rr\rVrr)rUrr[�sr[cs(eZdZdZ�fdd�Zdd�Z�ZS)�iWorx�	InterWorxcs,tt|�j�d|_|j�|_td�|_dS)Nz/usr/local/interworx/iworx.iniz/usr/sbin/apachectl)rRrdr�ini_file�find_versionrrr )r)rUrrrs
ziWorx.__init__cCsjyPt|jd��}|j�}WdQRX|jd�djdd�jdd�jdd�jd�d	Stk
rddSXdS)
N�rr�rr4r]�"�[r)r^rfrrr6�	Exception)rZini_info�outrrrrgs0ziWorx.find_version)rrrr'rrgrVrr)rUrrd
srdcs eZdZdZ�fdd�Z�ZS)�ISPMgr�
ISPManagercs"tt|�j�d|_td�|_dS)NZunkz/usr/sbin/apachectl)rRrnrrrr )r)rUrrrszISPMgr.__init__)rrrr'rrVrr)rUrrnsrncs0eZdZdZ�fdd�Zdd�Zdd�Z�ZS)�CustomPanelcs&tt|�j�|j�d|_d|_dS)Nz /usr/share/lve/modlscapi/custom/)rRrpr�
config_reader�root_dirr )r)rUrrr%szCustomPanel.__init__cCs�ddl}|j�}|j|jd�|d}|j|�|jd�|_|jd�|_|jd�|_|jd�|_	|jd	�|_
|jr�t|j�|_dS)
z�Read all configurations related to Custom Panel from config.ini in the 'root_dir' folder.
        config.ini file must include GLOBAL section.
        rNz
config.iniZGLOBAL�VERSIONZAPACHECTL_BIN_LOCATIONZDOC_URLZEXECUTABLE_BINZ
PANEL_NAME)
�configparser�ConfigParserrrr�check_config_ini�getrZ	apachectl�doc_url�
executable�
panel_namerr )rrt�config�	cp_configrrrrq+s
zCustomPanel.config_readercCsVdddddg}g}x$|D]}|j|�dkr|j|�qW|rRtd|���td�dS)NrZapachectl_bin_locationrxZexecutable_binrzz&Config file is missing required keys: r
)rw�appendr=�exit)rr|Z
required_keysZmissing_keysrbrrrrv>s
zCustomPanel.check_config_ini)rrrr'rrqrvrVrr)rUrrp#srpcCs�tjjd�rtStjjd�r tStjjd�r<tjjd�r<tStjjd�rLtStjjd�r\tStjjd�rlt	Stjjd�r|t
StS)	Nz/usr/local/cpanel/cpanelz/usr/local/psa/versionz/usr/local/directadminz(/usr/local/directadmin/custombuild/buildz8/hsphere/local/home/cpanel/shiva/psoft_config/HS_VERSIONz/usr/local/ispmgrz/usr/local/interworx/iworx.iniz*/usr/share/lve/modlscapi/custom/config.ini)rrrrPrW�isdirrXr[rnrdrprLrrrr�
_get_cp_classLsr�cCst�jS)a�
    Call this when you only need to detect Control Panel type, and don't need its complete properties.

    The function avoids creating an instance of CP successor class.
    This is safer for calling from installation scriptlets, when the system state can be inconsistent -
    because instance initialization tries to collect much more info than _get_cp_class().
    For example, 'apachectl' utility, used to gather server attributes into 'apache' instance variable,
    can fail half-way 'mod_lsapi' post-installation. Specifically:
    when the upgrade brings a non-backward compatible change of 'mod_lsapi.so'<->'liblscapi.so' binary interface,
    at the moment when we have 'liblscapi.so' already updated,
    but the new 'mod_lsapi.so' not yet copied into the Apache modules directory,
    'apachectl' would fail due to unloadable modules, print errors instead of Apache properties,
    and eventually produce installation failure.
    See CLOS-3184.
    )r�r'rrrr�get_cp_name]sr�cCst��S)z�
    Call this when you want full Control Panel properties, not only its type.

    Properties can fail to be obtained, or be invalid, in intermediate machine state, like half-installed 'mod_lsapi'.
    See CLOS-3184.
    )r�rrrr�get_cposr�)rZ	distutilsrr	r
rrArrBrLrPrWrXr[rdrnrpr�r�r�rrrr�<module>s$R1	)modlscapi/user/__pycache__/dashboard_malfunctions.cpython-36.pyc000064400000001330151677542400021054 0ustar003

�7�io�@s6ddd�ddd�d�Zdddd�iZd	d
dd�iZd	S)zCriu service stoppedzJRun 'service criu start' because lsapi_criu enabled through Apache configs)�reason�descriptionzlsapi_criu disabledzJSet 'lsapi_criu On' through Apache configs because criu service is running)Z
on_stoppedZoff_running�disabledz(Apache module lsapi_module is not loadedzeEnable mod_lsapi through Apache configs with 'LoadModule lsapi_module modules/mod_lsapi.so' directiveNzNo liblsapi foundzInstall liblsapi package)�criu_settings_malfunctions�lsapi_settings_malfunctions�liblsapi_malfunctions�rr�7/usr/share/lve/modlscapi/user/dashboard_malfunctions.py�<module>s
modlscapi/user/__pycache__/stat_utils.cpython-36.pyc000064400000021521151677542400016542 0ustar003

�7�i�$�@s�ddlZddlZddlZddlZddlZddlZddlmZddlm	Z	ddl
Z
Gdd�de�Zdd�Z
dd	�Zd
d�Zdd
�Zd%dd�Zd&dd�Zd'dd�Zd(dd�Zdd�Zdd�Zdd�Zd)d d!�Zd*d#d$�ZdS)+�N)�StringIO)�quotec@seZdZdS)�StatUtilsExceptionN)�__name__�
__module__�__qualname__�rr�+/usr/share/lve/modlscapi/user/stat_utils.pyrsrcKs�djdd�|j�D��}tjdj||d��}ytjdj|��}Wn,tk
rltdj||dj|����YnXy|dStk
r�td	j|���YnXd
S)z�
    Perform cPanel WHM API console request and return data from result
    :param cmd: whm api command
    :return: data dict from result
    � cSs g|]\}}tdj||���qS)z{0}={1})r�format)�.0�k�vrrr	�
<listcomp>$sz!cpanel_whmapi.<locals>.<listcomp>z*/usr/sbin/whmapi1 {cmd} {kw} --output json)�cmd�kw�z>Failed to get JSON from this API request: {0} {1}; output: {2}�dataz,Failed to get data from this API result: {0}N)	�join�items�exec_commandr�json�loads�
ValueErrorr�KeyError)r�kwargs�
joined_kwargs�resultZdict_resultrrr	�
cpanel_whmapisrcKsndjdd�|j�D��}tjdj||d��}ytjdj|��Stk
rhtdj||dj|����YnXdS)	z�
    Perform Plesk php_handler utility console request and return result
    :param cmd: php_handler command
    :return: dict result
    r
cSs g|]\}}dj|t|���qS)z-{0} {1})rr)rr
rrrr	r=sz)plesk_bin_php_handler.<locals>.<listcomp>z6/usr/local/psa/bin/php_handler --{cmd} {kw} -json true)rrrzJFailed to get JSON from this API request: php_handler {0} {1}; output: {2}N)rrrrrrrr)rrrrrrr	�plesk_bin_php_handler6s

rcCs�d}d}tj|�}tj|�}yJxD|D]<}t|j|d�d�� }tdd�|j�D��||<WdQRXq$WWn0tt	fk
r�t
djtj
�jd	����YnX|S)
ze
    Get domains per user
    :return: dict(
                user: list of domains
            )
    z!/usr/local/directadmin/data/usersz5/usr/local/directadmin/data/users/{user}/domains.list)�user�rcSsg|]}|j��qSr)�strip)r�lrrr	rXsz"get_da_domains.<locals>.<listcomp>Nr�
)�os�listdir�dict�fromkeys�openr�set�	readlines�OSError�IOErrorrr�	traceback�
format_exc�split)Z
da_users_pathZda_domains_pathZda_users�domainsr Zdomains_list_filerrr	�get_da_domainsIs


,r2cCs�d}y@t|�\}}|j|d�}|j|d�}|j|d�}|j|d�}Wn4tjk
rxtdjdjtj�j	d�����YnX||d	kr�d
n|d�||d	kr�d
n|d�d�S)
z�
    Get php settings from options.conf
    :return: dict(
                first php setting: {version, mode},
                second php setting: {version, mode},
            )
    z//usr/local/directadmin/custombuild/options.confZphp1_releaseZphp2_releaseZ	php1_modeZ	php2_modezNo option found: {0}rr$�lsphp�lsapi)�version�handler)��)
�read_da_config�get�configparser�
NoOptionErrorrrrr.r/r0)Zoptions_path�
config_parser�global_section�php1_ver�php2_ver�php1_handler�php2_handlerrrr	�get_da_php_options^s$rC�
dummy_sectioncCs�yFt|��}tdj|d�|j��}WdQRXtjdd�}|j|�Wn0ttfk
rvt	dj
tj�j
d����YnX||fS)z�
    Read DA config file with ConfigParser.
    Need to add dummy section for success
    :param conf_file: config file name
    :param append_section_name: name of section to place in the beginning of file
    :return: RawConfigParser instance
    z[{s}]
)�sNF)�strictrr$)r)rr�readr;�RawConfigParser�	read_filer,r-rrr.r/r0)Z	conf_fileZappend_section_name�fZfile_contentr=rrr	r9}s
"r9�phpcCs6dj|�}y|t|jd��Stk
r0|SXdS)z�
    Convert simple php versions to pretty format
    :param php_ver: {major}.{minor} version
    :param pre: desired key start
    :return: alt-php{major}{minor} or desired `pre`{major}{minor}
    z{0}%s%s�.N)r�tupler0�	Exception)Zphp_ver�pre�templaterrr	�pretty_version_keys�s

rQ�/usr/sbin/httpdc	Cs6tjdj|d��}ytdd�|D��St�SdS)a+
    Get `httpd -t -D DUMP_RUN_LSAPI` info
    For httpd24 this default path is `/opt/rh/httpd24/root/usr/sbin/httpd`, generated in make_from_templates.sh script
    :param ctl_path: path to httpd (also apachectl may be used)
    :return: dict(
                lsapi_option: value
            )
    z{ctl} -t -D DUMP_RUN_LSAPI)�ctlcSsg|]}|j�jd��qS)r
)�lowerr0)rr#rrr	r�szdump_lsapi.<locals>.<listcomp>N)rrr')�ctl_pathZapache_conf_datarrr	�
dump_lsapi�s
	rVc	Cs6tjdj|d��}ytdd�|D��St�SdS)a
    Get `httpd -M`
    For httpd24 this default path is `/opt/rh/httpd24/root/usr/sbin/httpd`, generated in make_from_templates.sh script
    :param ctl_path: path to httpd (also apachectl may be used)
    :return: dict(
                apache_module: value
            )
    z{ctl} -M)rScSsg|]}|j�jd��qS)r
)rTr0)rr#rrr	r�sz'dump_loaded_modules.<locals>.<listcomp>N)rrr')rU�apache_modulesrrr	�dump_loaded_modules�s
	rXcCstjdk}dj|rdndd�S)zR
    Retrieve path to liblsapi, depends on arch
    :return: path to liblsapi
    r8� z/usr/lib{a}/liblscapi.soZ64r)�al)�sys�maxsizer)Z	is_64bitsrrr	�
liblsapi_path�s
r]cCsTy:djtjd|d��jd�\}}dj||jd�dd�Stk
rNd	SXd	S)
zr
    Get version-release from rpm -q `pkg`
    :param pkg: package name to query
    :return: version-release
    rz/bin/rpm -q z --qf %{v}-%{r}�-z{ver}-{rel}rLr)�ver�relN)rrr0rr)Zpkgr_r`rrr	�	rpm_query�s
"rac
sHy(�fdd�t|�D�dj��dj�Stttfk
rBdSXdS)z�
    Filter strings by given template
    Also split string upon given template
    :param fname: path to file
    :param template: template to find in string
    :return: first template occurrence splitted by template
    csg|]}�|kr|�qSrr)rr#)rPrr	r�sz!query_strings.<locals>.<listcomp>rr7N)�stringsr0r"�
IndexErrorr-r,)�fnamerPr)rPr	�
query_strings�s(re�c
csnt|dd��X}d}x:|j�D].}|tjkr4||7}qt|�|krF|Vd}qWt|�|kr`|VWdQRXdS)z�
    Strings utility analog.
    Finds printable strings in executable
    :param fname: path to file
    :param n: minimum string length
    :return: generator, yeilds string
    �ignore)�errorsrN)r)rG�stringZ	printable�len)rd�nrJr�crrr	rb�s
rbTcCsrt�}x.|j�D]"\}}tdd�|j�D��||<qWy|rD|dS|Stk
rltjdd�|D�d�SXdS)a�
    Count domains
    :param handler_struct: handler: version: set_of_domains structure
    :param default_keys: sequence of keys to add as default if no `lsapi` found
    :param only_lsapi: return only lsapi statistics
    :return: statistics - number of lsapi domains per version if only_lsapi=True
                          number of lsapi domains per version per handler otherwise
    css|]\}}|t|�fVqdS)N)rj)rr
rrrr	�	<genexpr>sz count_domains.<locals>.<genexpr>r4cSsg|]}|dkr|�qS)�nor)r�xrrr	rsz!count_domains.<locals>.<listcomp>rN)r'rrr()Zhandler_structZdefault_keysZ
only_lsapi�result_stat�hrrrr	�
count_domains�s	rr)rD)rK)rR)rR)rf)T)rir[r%r.rr;�iorZpipesrrrNrrrr2rCr9rQrVrXr]rarerbrrrrrr	�<module>s,



	

modlscapi/user/__pycache__/selector_usage_lib.cpython-36.pyc000064400000022430151677542400020201 0ustar003

�7�i3�@s�ddlZddlmZddlZddlmZmZmZGdd�de�Z	dd�Z
dd	�Zd
d�Zdd
�Z
dd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zd d!�Zd"d#�Zd$d%�ZdS)&�N)�deepcopy)�pretty_version_keys�
cpanel_whmapi�dump_loaded_modulesc@seZdZdZdS)�SelectorStatExceptionz
    Inner exception class
    N)�__name__�
__module__�__qualname__�__doc__�rr�3/usr/share/lve/modlscapi/user/selector_usage_lib.pyrsrcCsdjtjd��j�}|dkS)z�
    Check status of cagefs - installed & enabled
    If not - selector is not applied, no need to query its settings
    :return: True is cagefs is installed and enabled, False otherwise
    �z#/usr/sbin/cagefsctl --cagefs-statusZEnabled)�join�exec_command�strip)Z
cagefs_statusrrr�cagefs_enabledsrccsfdjtjd��}ytj|�jd�}Wntk
r<f}YnXx"|D]}|d}|dkrD|VqDWdS)z`
    Retrieve versions from selector, EXCEPT native
    :return: selector versions iterator
    r
z%/usr/bin/selectorctl --summary --jsonZPHPConfiguration�version�nativeN)rr�json�loads�get�
ValueError)Z
selector_data�selector_versions�item�verrrr�get_selector_versions's

rcs4djtjdj�d���}t�fdd�|jd�D��S)z�
    Retrieve selector users for given version
    :param version: selector version, e.g 7.2
    :return: dict user: version, e.g. {user: alt-php72, user1: alt-php72}
    r
z//usr/bin/selectorctl --list-users --version={v})�vc3s |]}|r|t�d�fVqdS)zalt-phpN)r)�.0�u)rrr�	<genexpr>@sz%get_selector_users.<locals>.<genexpr>�,)rr�format�dict�split)rZusersr)rr�get_selector_users9sr$cCs4t�st�St�}xt�D]}|jt|��qW|S)zY
    Retrieve versions set for users in selector
    :return: dict { user: version }
    )rr"r�updater$)Z	users_verrrrr�get_selector_usageCsr&cCs�td�jd�}|j|�dkr |Sd|kr�dt�kr�t|�}t|�}t|�}t|j|t��|�\}}|||<t|�}	xB|	j�D]6\}
}||kr||	|
j	||i�|	|
j	t
||��q|W|	S|SdS)aD
    Check EA4 selector settings. Statistics per handler is inspected.
    Selector on EA4 does not change handler, it changes version only.
    Selector version is applied only in case of all the following circumstances:
     - suexec_module is enabled
     - system default version is not alt-php
     - selector setting is not native
    :param ver_domains_list: set of domains per version
    :param domain_user_dict: domain--user correspondence
    :param handlers_mapping: per handler statistics
    :return: set of domains per version checked against selector settings
    Zphp_get_system_default_versionrNzalt-phpZ
suexec_module)rrr�ea4_revert_domains_user_struct�get_selector_domainsr�matcher�set�itemsr%�merger)�ver_domains_listZdomain_user_dictZhandlers_mappingZdefault_ver�user_domains�
s_ver_domains�api_ver_list�default_domains_matched�
s_ver_matchedZhandlers�hrrrr�ea4_selector_checkRs r4cCs4t�}x(|j�D]\}}|j|t��j|�qW|S)z�
    Revert structure `domain`: `user` to structure `user`: set of domains
    :param domains_user_dict: dict of domains per user correspondence
    :return: dict user: set_of_domains
    )r"r+�
setdefaultr*�add)Zdomains_user_dictr.�domain�userrrrr'�sr'cCsDt�}t�}x2|j�D]&\}}|j|t��j|j|t���qW|S)z�
    Retrieve domains set for selector versions
    :param domains_of_user: `user`: set of domains structure
    :return: dict selector_ver: set of domains
    )r&r"r+r5r*r%r)Zdomains_of_userZselector_usersrr8Zs_verrrrr(�s
"r(cCs@x6|j�D]*\}}|dkrq
||j|�|j|�q
W||fS)a�
    Match domains sets between system default version and selector versions:
    - for system default version domains get difference with selector domains
    - for selector domains get intersection with system default version
    Common for cPanel and Plesk
    :param system_default_api_domains: set of domains using system default version
    :param selector_versions: dict selector_ver: set of domains
    :return: tuple(clean_default_domains, clean_selector_domains)
    r)r+�intersection_update�difference_update)Zsystem_default_api_domainsrr�dom_listrrrr)�s
r)cCsRt|f|�}x@|j�D]4\}}y|j||�||<Wqtk
rHYqXqW|S)a+
    Merge matched structures of domains (common for cPanel and Plesk)
    :param clean_api_versions: API_version: set of domains structure after match
    :param clean_selector_versions: selector_version: set of domains structure after match
    :return: unified structure version: domains_set
    )r"r+�union�KeyError)Zclean_api_versionsZclean_selector_versions�mergedrr;rrrr,�s
r,cCsZ|jd�rLt�}t|�}t|�}t|jdt��|�\}}||d<t||�}n|}t||�S)a|
    Check Plesk selector settings.
    Selector version is applied only to LSPHP by vendor OS handlers, e.g. x-httpd-lsphp-custom
    :param ver_domains_list: list of domains per version, including `custom` handler version
    :param custom_ver: real version, to which Plesk resolves `custom` handler
    :return: set of domains per version checked against selector settings
    �custom)r�plesk_get_user_domainsr(rr)r*r,�plesk_manage_custom)r-Z
custom_verr.r/r0Zcustom_domains_matchedr2r>rrr�plesk_selector_check�s	
rBcCstt�t��S)zc
    Find `user`: `domains` correspondence for Plesk
    :return: dict(`user`: set of domains)
    )r,�'plesk_get_domains_under_new_subsciption�,plesk_get_domains_under_existing_subsciptionrrrrr@�sr@cCstd�S)zg
    Get users' domains, added under existing subscription
    :return: dict(user: set_of_domains)
    z�select login,name from domains inner join Subscriptions as sc on domains.webspace_id=sc.object_id inner join sys_users as u on sc.id=u.id where domains.webspace_id!=0 and sc.object_type="domain")�plesk_query_dbrrrrrD�srDcCstd�S)zb
    Get users' domains, added under new subscription
    :return: dict(user: set_of_domains)
    z�select login,name from domains inner join Subscriptions as sc on domains.id=sc.object_id inner join sys_users as u on sc.id=u.id where domains.webspace_id=0 and sc.object_type="domain")rErrrrrC�srCcCs�ddlm}t�}tjdj|d��}y\|jdj|��}xF|jd�D]8}|jd�dj	}|jd�dj	}|j
|t��j|�qBWWn*|j
|jfk
r�td	j|���YnX|S)
z{
    Query Plesk database for user:domain correspondence and get result in XML
    :return: dict(user: set_of_domains)
    r)�etreezplesk db '{q}' --xml)�qr
�rowzfield[@name='name']zfield[@name='login']z-Failed to parse XML from plesk db output: {0})ZlxmlrFr"rr!Z
fromstringr�iterZxpath�textr5r*r6ZXMLSyntaxErrorZ
ParseErrorr)ZqueryrFr.Z
xml_result�rootrHZdomain_nameZ	user_namerrrrE�srEcCsX|rT|j|t��}|j|jdt���|j||i�y
|d=Wntk
rRYnX|S)a\
    Unify `custom` domains with `real_version` domains,
    clear `custom` key from resulting `version`: `set of domains` mapping
    :param versions_mapping: final merged structure of domains
    :param real_version: version, to which Plesk resolves custom
    :return: final mapping with `custom` domains unified with `real_version` domains
    r?)rr*r%r=)Zversions_mappingZreal_versionZreal_version_domainsrrrrAs
rAcCs@|r4t|�}t|�}t||�\}}t||i|�}|S||iSdS)av
    Check DA selector settings
    :param primary_ver_domains: set of domains using primary version, e.g. php1_release
    :param domains_per_user: user--domain correspondence
    :param primary_php: php version set in options.conf as php1_ver, only this version is affected by selector
    :return: set of domains per primary version checked against selector settings
    N)r(rr)r,)Zprimary_ver_domainsZdomains_per_userZprimary_phpr/r0r1r2r>rrr�da_selector_check!s	rL)r�copyrr�
stat_utilsrrr�	Exceptionrrrr$r&r4r'r(r)r,rBr@rDrCrErArLrrrr�<module>s(

2
!	modlscapi/utils/checkconfdutil.pm000075500000005045151677542400013224 0ustar00#!/usr/local/cpanel/3rdparty/bin/perl

# Copyright (c) Cloud Linux GmbH & Cloud Linux Software, Inc 2010-2018 All Rights Reserved
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

BEGIN { unshift @INC, '/usr/local/cpanel', '/scripts'; }

my $pckg_name = 'Cpanel';
use strict;

eval "use " .$pckg_name . "::CPAN::Hash::Merge          ();";
if ($@){
    $pckg_name = 'cPScript';
    eval "use " .$pckg_name . "::CPAN::Hash::Merge       ();";
}

use strict;
use warnings;

#eval "use " .$pckg_name . "::CPAN::Hash::Merge              ();";
eval "use  " .$pckg_name . "::DataStore          ();";


no warnings 'uninitialized';

my $main_conf_ref = Cpanel::DataStore::fetch_ref('/var/cpanel/conf/apache/main');
my $ref_type=ref($main_conf_ref->{'main'}{'include'}{'items'});

my $etalon_ref={"main"=>{"include"=>{"directive"=>"include","items"=>[{include=>"\"/usr/local/apache/conf/conf.d/*\""}]}}};

if ($ref_type eq 'ARRAY'){
    my $res=$main_conf_ref->{'main'}{'include'}{'items'};
    my $find=0;
    my $empty_find=0;
    for(my $index=0;$index<@$res;$index++)
    {
       my $item_ref=@$res[$index];
       if ( !defined($item_ref->{'include'})) {
            $empty_find = 1;
       } else { 
           if ( index($item_ref->{'include'},"/usr/local/apache/conf/conf.d/*.conf")!=-1 ){
    	       $find=1;
           }
       }
    }
    if ( $find==0 ){
	for(my $index=0;$index<@$res;$index++)
	{
    	    my $item_ref=@$res[$index];
    	    if ( index($item_ref->{'include'},"/usr/local/apache/conf/conf.d/*")!=-1 ){
    		delete @$res[$index];
    	    }
	}
	push(@$res,{ "include"=>"\"/usr/local/apache/conf/conf.d/*.conf\"" });
	Cpanel::DataStore::store_ref( '/var/cpanel/conf/apache/main', $main_conf_ref );
    }
    if( $empty_find==1 ) {
	@$res = grep { defined $_ } @$res;
	Cpanel::DataStore::store_ref( '/var/cpanel/conf/apache/main', $main_conf_ref );
    }
} else {
    $main_conf_ref->{'main'} = Cpanel::CPAN::Hash::Merge::merge($main_conf_ref->{'main'}, $etalon_ref->{'main'});
    Cpanel::DataStore::store_ref( '/var/cpanel/conf/apache/main', $main_conf_ref );
}

















modlscapi/utils/cpanel-delete-lve000075500000003666151677542400013121 0ustar00#!/bin/bash

# Copyright (c) Cloud Linux GmbH & Cloud Linux Software, Inc 2010-2018 All Rights Reserved
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

. /usr/share/lve/modlscapi/include/cpanel-common-lve

before_apache_make="/scripts/before_apache_make"                                                                                                             
before_httpd_restart_tests="/scripts/before_httpd_restart_tests"                                                                                             
postupcp="/scripts/postupcp" 
after_apache_make_install="/scripts/after_apache_make_install"
posteasyapache="/scripts/posteasyapache"

showBar 1
rm -f /usr/local/apache/conf/conf.d/lsapi.conf
writeToLog "LVE Config files deleted"

showBar 2
deleteAllInclude "$before_httpd_restart_tests" "before_httpd_restart_tests" "/#mod_lsapi/d"                                                                    
showBar 3
deleteAllInclude "$before_apache_make" "before_apache_make" "/#mod_lsapi/d"
showBar 4
deleteAllInclude "$postupcp" "postupcp" "/#mod_lsapi/d"
showBar 5
deleteAllInclude "$after_apache_make_install" "postupcp" "/#mod_lsapi/d"
showBar 6
deleteAllInclude "$posteasyapache" "posteasyapache" "/#mod_lsapi/d"
writeToLog "Delete hooks"

pathto="/usr/local/apache/modules"                                                                                                                          
pathbak="/usr/share/lve/modlscapi/tmp"

showBar 7

echo
modlscapi/utils/cpanel-hooks-lve000075500000010745151677542400012776 0ustar00#!/bin/bash

# Copyright (c) Cloud Linux GmbH & Cloud Linux Software, Inc 2010-2018 All Rights Reserved
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

###################################################
# Utils for hooks installation or removing        #
###################################################
. /usr/share/lve/modlscapi/include/cpanel-common-lve

function createHookHeader(){
    #1 - hook name
    if [ ! -e "$common_path_of_cpanel/tmp" ]; then
        mkdir -p "$common_path_of_cpanel/tmp"
    fi
    if [ ! -e "$1" ];then
	touch "$1"
    fi
    if [ -e "$1" ];then
	chmod 755 "$1"
	tmp=$(cat "$1" | grep "#!/bin/bash")
	if [ -z "$tmp" ];then
	    echo "#!/bin/bash" > "$common_path_of_cpanel/tmp/future_hook.tmp.$$"
	    cat "$1" >> "$common_path_of_cpanel/tmp/future_hook.tmp.$$"
	    cat "$common_path_of_cpanel/tmp/future_hook.tmp.$$" > "$1"
	    rm -f "$common_path_of_cpanel/tmp/future_hook.tmp.$$"
	fi
    fi
}

function checkHookString(){
    #1 - hook name
    #2 - pattern string
    #3 - comment srting
    if [ -e "$1" ];then
	tmp=$(cat "$1" | grep "$2")
	if [ -z "$tmp" ];then
	    echo "$2   #$3" >> "$1"
	fi
    fi
}

function addHookStringFirst(){
    #1 - hook name
    #2 - pattern string
    #3 - comment srting
    #4 - short hook name
    if [ -e "$1" ];then
	cat "$1" | sed /$4/d | sed 1a"$2   #$3" > "$common_path_of_cpanel/tmp/future_hook.tmp.$$"
	cat "$common_path_of_cpanel/tmp/future_hook.tmp.$$" > "$1"
	rm -f "$common_path_of_cpanel/tmp/future_hook.tmp.$$"
    fi
}



####################################################
# EasyApache hooks                                 #
####################################################

before_apache_make="/scripts/before_apache_make"
before_httpd_restart_tests="/scripts/before_httpd_restart_tests"
postupcp="/scripts/postupcp"
after_apache_make_install="/scripts/after_apache_make_install"
posteasyapache="/scripts/posteasyapache"

####################################################
# Hooks list                                       #
####################################################

lsapi_HOOK="$common_path_of_cpanel/hooks/lsapi.hook"
cpanelpostupcp_HOOK="$common_path_of_cpanel/hooks/cpanelpostupcp.hook"
clearconf_HOOK="$common_path_of_cpanel/hooks/clearconf.hook"
posteasyapache_HOOK="$common_path_of_cpanel/hooks/buildlsphp.hook"


writeToLog "Install cPanel hooks"

showBar 1
#Install new hooks
createHookHeader "$before_apache_make"
createHookHeader "$before_httpd_restart_tests"
createHookHeader "$postupcp"
createHookHeader "$after_apache_make_install"
createHookHeader "$posteasyapache"
showBar 2
writeToLog "Headers checked"
addHookStringFirst "$before_apache_make" "$clearconf_HOOK" "mod_lsapi Version $VERSION. Required for LVE module recompilation" "clearconf.hook"
removeEmptyStringsFromFile "$before_apache_make"
showBar 3
writeToLog "before_apache_make checked"
checkHookString "$before_httpd_restart_tests" "$lsapi_HOOK" "mod_lsapi Version $VERSION mod_lsapi module installation"
checkHookString "$before_httpd_restart_tests" "$cpanelpostupcp_HOOK" "mod_lsapi Version $VERSION Rebuild Apache config hook"
removeEmptyStringsFromFile "$before_httpd_restart_tests"
showBar 4
writeToLog "before_httpd_restart_tests checked"
checkHookString "$postupcp" "$cpanelpostupcp_HOOK" "mod_lsapi Version $VERSION Rebuild Apache config hook"
removeEmptyStringsFromFile "$postupcp"
showBar 5
writeToLog "postupcp checked"
showBar 6
writeToLog "after_apache_make_install checked"
checkHookString "$after_apache_make_install" "$lsapi_HOOK" "mod_lsapi Version $VERSION mod_lsapi module installation"
removeEmptyStringsFromFile "$after_apache_make_install"
showBar 7
writeToLog "after_apache_make_install checked"
showBar 8
checkHookString "$posteasyapache" "$posteasyapache_HOOK" "mod_lsapi Version $VERSION mod_lsapi module installation"
checkHookString "$posteasyapache" "$cpanelpostupcp_HOOK" "mod_lsapi Version $VERSION Rebuild Apache config hook"
removeEmptyStringsFromFile "$posteasyapache"
writeToLog "posteasyapache checked"
showBar 9
writeToLog "Done installing hooks"

echo
modlscapi/utils/cpanel-make-lve000075500000001730151677542400012562 0ustar00#!/bin/bash

# Copyright (c) Cloud Linux GmbH & Cloud Linux Software, Inc 2010-2018 All Rights Reserved
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

#####################################################
# Util for building modules by hands. Without hooks #
#####################################################

. /usr/share/lve/modlscapi/include/cpanel-common-lve

showBar 1
writeToLog "mod_lsapi module installation"

installModule_lsapi

showBar 2
writeToLog "End operation"
echo
modlscapi/utils/cpanel_hooks_manage.py000075500000006776151677542400014244 0ustar00#!/usr/bin/python3.6

# Copyright (c) Cloud Linux GmbH & Cloud Linux Software, Inc 2010-2018 All Rights Reserved
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

#coding:utf-8
"""
Mod_lsapi hooks manager for Cpanel EA4
-i - installs hooks
-d - removes hooks
"""
import os
import sys
import syslog

import yaml


YAML_HOOK_BASE = "/var/cpanel/hooks.yaml"
MANAGE_HOOK = "/usr/local/cpanel/bin/manage_hooks"


def log_error(msg):
    """
    Write message to log
    """
    syslog.syslog("%s: %s" % (__file__, msg))


def is_hook_installed(category, event, hook_script_path):
    """
    Check is hook installed in current cpanel
    """
    try:
        stream = open(YAML_HOOK_BASE, "r")
    except (IOError, OSError):
        return False

    hook_base = yaml.load(stream)
    try:
        for i in hook_base[category][event]:
            if i["hook"] == hook_script_path:
                return True
    except KeyError:
        return False
    return False


def install_hook(hook_script_path, category, event, stage):
    """
    Install event hook for cpanel
    """
    if not is_hook_installed(category, event, hook_script_path):
        cmd = "%s add script %s --category=%s --event=%s --stage=%s --manual" \
              % (MANAGE_HOOK, hook_script_path, category, event, stage)

        if os.system(cmd):
            log_error("Can`t add hook to category:%s event:%s" % (category, event))
            return 1

    return 0


def remove_hook(hook_script_path, category, event, stage):
    """
    Remove cpanel event hook
    """
    cmd = "%s delete script %s --category=%s --event=%s --stage=%s --manual" \
          % (MANAGE_HOOK, hook_script_path, category, event, stage)

    if os.system(cmd):
        log_error("Can't delete hook to category:%s event:%s" % (category, event))
        return 1

    return 0


def main(argv):
    """
    Install or delete cpanel event hook
    """
    result = 0
    if "-i" in argv:
        # install hooks
        result = install_hook("/usr/share/lve/modlscapi/hooks/cpanel_post_upcp_lsapi_hook.py",
                              "System", "upcp", "post") or result
        result = install_hook("/usr/share/lve/modlscapi/hooks/cpanel_post_upcp_check_handlers_hook.py",
                              "System", "upcp", "post") or result
        result = install_hook("/usr/share/lve/modlscapi/hooks/cpanel_pre_upcp_check_handlers_hook.py",
                              "System", "upcp", "pre") or result

    elif "-d" in argv:
        # remove hooks
        result = remove_hook("/usr/share/lve/modlscapi/hooks/cpanel_post_upcp_lsapi_hook.py",
                             "System", "upcp", "post") or result
        result = remove_hook("/usr/share/lve/modlscapi/hooks/cpanel_post_upcp_check_handlers_hook.py",
                             "System", "upcp", "post") or result
        result = remove_hook("/usr/share/lve/modlscapi/hooks/cpanel_pre_upcp_check_handlers_hook.py",
                             "System", "upcp", "pre") or result

    return result


if "__main__" == __name__:
    sys.exit(main(sys.argv[1:]))


modlscapi/utils/getdocroot.pm000075500000005512151677542400012407 0ustar00#!/bin/sh
eval 'if [ -x /usr/local/cpanel/3rdparty/bin/perl ]; then exec /usr/local/cpanel/3rdparty/bin/perl -x -- $0 ${1+"$@"}; else exec /usr/bin/perl -x $0 ${1+"$@"}; fi;'
if 0;

#!/usr/bin/perl

# Copyright (c) Cloud Linux GmbH & Cloud Linux Software, Inc 2010-2018 All Rights Reserved
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

BEGIN { unshift @INC, '/usr/local/cpanel', '/scripts'; }

use strict;

use Cpanel::Config::userdata::Load  ();
use Cpanel::Config::userdata::Cache ();
use Cpanel::Logger                  ();
use Cpanel::PwCache                 ();
use Carp                            ();

my ( $SubDomains, $MULTIPARKED, %DOCROOTS, %domainCache );

my $PERMIT_MEMORY_CACHE = 1;

sub getmultiparked_my {
    my $user = $_[0];
    if ( !$MULTIPARKED ) {
        $MULTIPARKED = {};
        my $cache = Cpanel::Config::userdata::Cache::load_cache( $user, $PERMIT_MEMORY_CACHE );

        # $cache is a HASHREF
        # $cache keys are domains
        # $cache values are a ARRAYREF with ( 0 $owner, 1 $reseller, 2 $type, 3 $parent, 4 $docroot )
        if ( $cache && %$cache ) {
            for my $domain ( grep { $cache->{$_}->[2] eq 'parked' || $cache->{$_}->[2] eq 'addon' } keys %$cache ) {
                $MULTIPARKED->{ $cache->{$domain}->[3] }->{$domain} = $cache->{$domain}->[4];
            }
        }
    }
    return %$MULTIPARKED;
}


sub getdocroot_my {
    my $domain = $_[0];
    my $user = $_[1];
    my $docroot;

    my $ud = Cpanel::Config::userdata::Load::load_userdata( $user, $domain );
    if ( $ud && keys %$ud ) {
        $docroot = $ud->{'documentroot'};
    }
    else {
        if ( !$MULTIPARKED ) {
            getmultiparked_my();
        }
        for my $sub_domain ( keys %$MULTIPARKED ) {
            if ( exists $MULTIPARKED->{$sub_domain}->{$domain} ) {
                $docroot = $MULTIPARKED->{$sub_domain}->{$domain};
                last;
            }
        }
    }

    if ( !$docroot ) {
        $docroot = $Cpanel::homedir . '/public_html';
    }
    if (wantarray) {
        ( my $reldocroot = $docroot ) =~ s{^$Cpanel::homedir/}{};
        return ( $docroot, $reldocroot );
    }
    return $docroot;
}


my $num_args = $#ARGV + 1;
if ($num_args != 2) {
  print "";
  exit;
}

my $name = $ARGV[0];
my $dm = $ARGV[1];
my $path = getdocroot_my($dm, $name);
if ($path eq "/public_html") {
print "";
} else {
print $path;
}

modlscapi/utils/getdocroots.pm000075500000002412151677542400012566 0ustar00#!/bin/sh
eval 'if [ -x /usr/local/cpanel/3rdparty/bin/perl ]; then exec /usr/local/cpanel/3rdparty/bin/perl -x -- $0 ${1+"$@"}; else exec /usr/bin/perl -x $0 ${1+"$@"}; fi;'
if 0;

#!/usr/bin/perl

# Copyright (c) Cloud Linux GmbH & Cloud Linux Software, Inc 2010-2018 All Rights Reserved
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

BEGIN { unshift @INC, '/usr/local/cpanel', '/scripts'; }

use strict;
use Data::Dumper;
use Cpanel::DomainLookup          ();
use Cpanel::Config::Users         ();

my @users =Cpanel::Config::Users::getcpusers();
foreach my $user (@users) {
    my @pathes = Cpanel::DomainLookup::getdocrootlist($user);
    foreach my $path (@pathes) {
        my @k = keys %{$path};
        foreach my $path_i (@k) {
            print $path_i , "\n";
        }
    }
}

modlscapi/utils/getuserdomains.pm000075500000002553151677542400013271 0ustar00#!/bin/sh
eval 'if [ -x /usr/local/cpanel/3rdparty/bin/perl ]; then exec /usr/local/cpanel/3rdparty/bin/perl -x -- $0 ${1+"$@"}; else exec /usr/bin/perl -x $0 ${1+"$@"}; fi;'
if 0;

#!/usr/bin/perl

# Copyright (c) Cloud Linux GmbH & Cloud Linux Software, Inc 2010-2018 All Rights Reserved
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

BEGIN { unshift @INC, '/usr/local/cpanel', '/scripts'; }

use strict;
use Cpanel::DomainLookup             ();
use Data::Dumper;
use Cpanel::Config::Users         ();

sub getdocrootlist_my {
    my $user         = shift;                # optional
    my %DOCROOTS     = ();
    my $docroots_ref = Cpanel::DomainLookup::getdocroots($user);
    foreach my $domain ( keys %{$docroots_ref} ) {
        print $user, "\t",$domain, "\n";
    }
}


my @users =Cpanel::Config::Users::getcpusers();
foreach my $user (@users) {
    getdocrootlist_my($user);
}

modlscapi/utils/inserphpconf.pm000075500000005043151677542400012733 0ustar00#!/usr/local/cpanel/3rdparty/bin/perl

# Copyright (c) Cloud Linux GmbH & Cloud Linux Software, Inc 2010-2018 All Rights Reserved
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

BEGIN { unshift @INC, '/usr/local/cpanel', '/scripts'; }

my $pckg_name = 'Cpanel';
use strict;

eval "use " .$pckg_name . "::CPAN::Hash::Merge          ();";
if ($@){
    $pckg_name = 'cPScript';
    eval "use " .$pckg_name . "::CPAN::Hash::Merge       ();";
}


use strict;
use warnings;

#eval "use " .$pckg_name . "::CPAN::Hash::Merge              ();";
eval "use  " .$pckg_name . "::DataStore          ();";


no warnings 'uninitialized';

my $main_conf_ref = Cpanel::DataStore::fetch_ref('/var/cpanel/conf/apache/main');
my $ref_type=ref($main_conf_ref->{'main'}{'include'}{'items'});

my $etalon_ref={"main"=>{"include"=>{"directive"=>"include","items"=>[{include=>"\"/usr/local/apache/conf/php.conf\""}]}}};

if ($ref_type eq 'ARRAY'){
    my $res=$main_conf_ref->{'main'}{'include'}{'items'};
    my $find=0;
    my $empty_find=0;
    for(my $index=0;$index<@$res;$index++)
    {
       my $item_ref=@$res[$index];
       if ( !defined($item_ref->{'include'})) {
            $empty_find = 1;
       } else {
           if ( index($item_ref->{'include'},"php.conf")!=-1 ){
               $find=1;
           }
       }
    }
    if ( $find==0 ){
        for(my $index=0;$index<@$res;$index++)
        {
            my $item_ref=@$res[$index];
            if ( index($item_ref->{'include'},"php.conf")!=-1 ){
                delete @$res[$index];
            }
        }
        push(@$res,{ "include"=>"\"/usr/local/apache/conf/php.conf\"" });
        Cpanel::DataStore::store_ref( '/var/cpanel/conf/apache/main', $main_conf_ref );
    }
    if( $empty_find==1 ) {
        @$res = grep { defined $_ } @$res;
        Cpanel::DataStore::store_ref( '/var/cpanel/conf/apache/main', $main_conf_ref );
    }
} else {
    $main_conf_ref->{'main'} = Cpanel::CPAN::Hash::Merge::merge($main_conf_ref->{'main'}, $etalon_ref->{'main'});
    Cpanel::DataStore::store_ref( '/var/cpanel/conf/apache/main', $main_conf_ref );
}
modlscapi/utils/lsapi-cache000075500000002052151677542400011770 0ustar00#!/bin/bash

# Copyright © Cloud Linux GmbH & Cloud Linux Software, Inc 2010-2021 All Rights Reserved
#
# Licensed under CLOUD LINUX LICENSE AGREEMENT
# http://cloudlinux.com/docs/LICENSE.TXT
#

[ -z "$LSAPICACHE_LOAD_DIRS" ] && exit 0

[ -z "$LSAPICACHE_LOGFILE" ] && LSAPICACHE_LOGFILE=/usr/share/lve/modlscapi/logs/lsapi-cache.log

D=`date`; echo "$D: the following dirs will be processed: $LSAPICACHE_LOAD_DIRS" >>$LSAPICACHE_LOGFILE

OIFS="$IFS"
IFS=":"

for d in $LSAPICACHE_LOAD_DIRS
do
    [ -d "$d" ] || continue
    D=`date`; echo "$D: loading shared libraries from ${d}..." >>$LSAPICACHE_LOGFILE
    /usr/bin/find "$d" -type f -executable -exec /usr/bin/ldd {} \; >/dev/null 2>&1
    D=`date`; echo "$D: ${d} - done." >>$LSAPICACHE_LOGFILE
done

for d in $LSAPICACHE_LOAD_DIRS
do
    [ -d "$d" ] || continue
    D=`date`; echo "$D: loading lsphp from ${d}..." >>$LSAPICACHE_LOGFILE
    /usr/bin/find "$d" -type f -executable -name lsphp -exec {} -i \; >/dev/null 2>&1
    D=`date`; echo "$D: ${d} - done." >>$LSAPICACHE_LOGFILE
done


IFS="$OIFS"
exit 0