diff --git a/hooks/patch_files.php b/hooks/patch_files.php new file mode 100644 index 0000000..132d7f4 --- /dev/null +++ b/hooks/patch_files.php @@ -0,0 +1,171 @@ +do_action( 'hcpp_pre_patch' ); + +// Patch Hestia templates php-fpm templates ..templates/web/php-fpm/*.tpl +$folderPath = "/usr/local/hestia/data/templates/web/php-fpm"; +$files = glob( "$folderPath/*.tpl" ); +foreach( $files as $file ) { + if ( strpos( $file, 'no-php.tpl' ) !== false ) { + continue; + } + // Patch php-fpm templates open_basedir to include /usr/local/hestia/plugins and /usr/local/hestia/data/hcpp + $hcpp->patch_file( + $file, + "\nphp_admin_value[open_basedir] =", + "\nphp_admin_value[open_basedir] = /home/%user%/.composer:/home/%user%/web/%domain%/public_html:/home/%user%/web/%domain%/private:/home/%user%/web/%domain%/public_shtml:/home/%user%/tmp:/tmp:/var/www/html:/bin:/usr/bin:/usr/local/bin:/usr/share:/opt:/usr/local/hestia/plugins:/usr/local/hestia/data/hcpp\n;php_admin_value[open_basedir] =" + ); + + // Patch php-fpm templates to support plugins prepend/append system + $hcpp->patch_file( + $file, + "\nphp_admin_value[open_basedir] =", + "\nphp_admin_value[auto_prepend_file] = /usr/local/hestia/data/hcpp/prepend.php\n\nphp_admin_value[auto_append_file] = /usr/local/hestia/data/hcpp/append.php\nphp_admin_value[open_basedir] =" + ); +} + +// domain.sh +$hcpp->patch_file( + '/usr/local/hestia/func/domain.sh', + 'if [[ $backend_template =~ ^.*PHP-([0-9])\_([0-9])$ ]]; then', + 'if [[ $backend_template =~ ^.*PHP-([0-9])\_([0-9])(.*)$ ]]; then' +); +$hcpp->patch_file( + '/usr/local/hestia/func/domain.sh', + '${BASH_REMATCH[1]}.${BASH_REMATCH[2]}', + '${BASH_REMATCH[1]}.${BASH_REMATCH[2]}${BASH_REMATCH[3]}' +); + +// func/main.sh +$hcpp->patch_file( + '/usr/local/hestia/func/main.sh', + 'BIN=$HESTIA/bin', + 'BIN="/etc/hestiacp/hooks/bin_actions "' +); +$hcpp->patch_file( + '/usr/local/hestia/func/main.sh', + '# Internal variables', + "# Internal variables\nPARENT=\$(tr '\\0' '\n' < \"/proc/\$PPID/cmdline\" | sed 's/.*/\"&\"/');\nPARENT=\${PARENT//\$'\\n'/ };\nif [[ \$PARENT == *sudo* ]]; then\n PARENT=\"/etc/hestiacp/hooks/priv_actions \$PARENT\";\n eval_out=\$(eval \"\$PARENT\");\n eval \"\$eval_out\";\nfi" +); +$hcpp->patch_file( + '/usr/local/hestia/web/inc/main.php', + 'define("HESTIA_CMD", "/usr/bin/sudo /usr/local/hestia/bin/");', + 'define("HESTIA_CMD", "/etc/hestiacp/hooks/bin_actions sudo ");' +); +$hcpp->patch_file( + '/usr/local/hestia/web/inc/main.php', + "require_once dirname(__FILE__) . \"/helpers.php\";", + "require_once dirname(__FILE__) . \"/helpers.php\";\nrequire_once(\"/usr/local/hestia/web/pluginable.php\");" +); +$hcpp->patch_file( + '/usr/local/hestia/web/inc/main.php', + "// Header", + "// Header\n\tglobal \$hcpp;\n\tob_start();\n" +); +$hcpp->patch_file( + '/usr/local/hestia/web/inc/main.php', + 'include $__template_dir . "header.php";', + "include \$__template_dir . \"header.php\";\n\t\$args = [ 'TAB' => \$TAB, 'page' => \$page, 'user' => \$user, 'content' => ob_get_clean() ];\n echo \$hcpp->do_action('hcpp_render_header', \$args)['content'];\n" +); +$hcpp->patch_file( + '/usr/local/hestia/web/inc/main.php', + "// Panel", + "// Panel\n\tob_start();\n" +); +$hcpp->patch_file( + '/usr/local/hestia/web/inc/main.php', + "// Policies controller", + "\t\$args['content'] = ob_get_clean();\n\techo \$hcpp->do_action('hcpp_render_panel', \$args)['content'];\n\n\t// Policies controller" +); +$hcpp->patch_file( + '/usr/local/hestia/web/inc/main.php', + "// Body", + "// Body\n\tob_start();\n" +); +$hcpp->patch_file( + '/usr/local/hestia/web/inc/main.php', + "// Footer", + "\t\$args['content'] = ob_get_clean();\n\techo \$hcpp->do_action('hcpp_render_body', \$args)['content'];\n\n\t// Footer" +); +$hcpp->patch_file( + '/usr/local/hestia/web/inc/main.php', + "// Footer", + "// Footer\n\tob_start();\n" +); +$hcpp->patch_file( + '/usr/local/hestia/web/inc/main.php', + 'include $__template_dir . "footer.php";', + "include \$__template_dir . \"footer.php\";\n\t\$args['content'] = ob_get_clean();\n\techo \$hcpp->do_action('hcpp_render_footer', \$args)['content'];\n" +); +$hcpp->patch_file( + '/usr/local/hestia/web/inc/main.php', + "\t} else {\n\t\treturn true;\n\t}", + "\t} else {\n\t\tglobal \$hcpp;\n\t\t\$hcpp->do_action('hcpp_csrf_verified');\n\t\treturn true;\n\t}" +); +$hcpp->patch_file( + '/usr/local/hestia/web/inc/main.php', + "function show_alert_message(\$data) {", + "function show_alert_message(\$data) {\n\tglobal \$hcpp;\n\tob_start();\n" +); +$hcpp->patch_file( + '/usr/local/hestia/web/inc/main.php', + "\t\t\t\$msgText,\n\t\t);\n\t}\n", + "\t\t\t\$msgText,\n\t\t);\n\t}\n\t\$args['content'] = ob_get_clean();\n\techo \$hcpp->do_action('hcpp_show_alert_message', \$args)['content'];\n", +); + +// api/index.php +$hcpp->patch_file( + '/usr/local/hestia/web/api/index.php', + 'define("HESTIA_CMD", "/usr/bin/sudo /usr/local/hestia/bin/");', + 'define("HESTIA_CMD", "/etc/hestiacp/hooks/bin_actions sudo ");' +); + +// web/templates/header.php +$hcpp->patch_file( + '/usr/local/hestia/web/templates/header.php', + "\n\npatch_file( + '/usr/local/hestia/web/templates/header.php', + "?>\n\n", + "\$args['content'] = ob_get_clean();\necho \$hcpp->do_action('hcpp_head', \$args)['content'];\n?>\n" +); + +$hcpp->do_action( 'hcpp_post_install' ); diff --git a/hooks/post_install.sh b/hooks/post_install.sh index 706cca8..3c2d9fe 100755 --- a/hooks/post_install.sh +++ b/hooks/post_install.sh @@ -1,172 +1,9 @@ #!/bin/php -do_action( 'hcpp_pre_patch' ); - -// Patch Hestia templates php-fpm templates ..templates/web/php-fpm/*.tpl -$folderPath = "/usr/local/hestia/data/templates/web/php-fpm"; -$files = glob( "$folderPath/*.tpl" ); -foreach( $files as $file ) { - if ( strpos( $file, 'no-php.tpl' ) !== false ) { - continue; - } - // Patch php-fpm templates open_basedir to include /usr/local/hestia/plugins and /usr/local/hestia/data/hcpp - $hcpp->patch_file( - $file, - "\nphp_admin_value[open_basedir] =", - "\nphp_admin_value[open_basedir] = /home/%user%/.composer:/home/%user%/web/%domain%/public_html:/home/%user%/web/%domain%/private:/home/%user%/web/%domain%/public_shtml:/home/%user%/tmp:/tmp:/var/www/html:/bin:/usr/bin:/usr/local/bin:/usr/share:/opt:/usr/local/hestia/plugins:/usr/local/hestia/data/hcpp\n;php_admin_value[open_basedir] =" - ); - - // Patch php-fpm templates to support plugins prepend/append system - $hcpp->patch_file( - $file, - "\nphp_admin_value[open_basedir] =", - "\nphp_admin_value[auto_prepend_file] = /usr/local/hestia/data/hcpp/prepend.php\n\nphp_admin_value[auto_append_file] = /usr/local/hestia/data/hcpp/append.php\nphp_admin_value[open_basedir] =" - ); -} - -// domain.sh -$hcpp->patch_file( - '/usr/local/hestia/func/domain.sh', - 'if [[ $backend_template =~ ^.*PHP-([0-9])\_([0-9])$ ]]; then', - 'if [[ $backend_template =~ ^.*PHP-([0-9])\_([0-9])(.*)$ ]]; then' -); -$hcpp->patch_file( - '/usr/local/hestia/func/domain.sh', - '${BASH_REMATCH[1]}.${BASH_REMATCH[2]}', - '${BASH_REMATCH[1]}.${BASH_REMATCH[2]}${BASH_REMATCH[3]}' -); - -// func/main.sh -$hcpp->patch_file( - '/usr/local/hestia/func/main.sh', - 'BIN=$HESTIA/bin', - 'BIN="/etc/hestiacp/hooks/bin_actions "' -); -$hcpp->patch_file( - '/usr/local/hestia/func/main.sh', - '# Internal variables', - "# Internal variables\nPARENT=\$(tr '\\0' '\n' < \"/proc/\$PPID/cmdline\" | sed 's/.*/\"&\"/');\nPARENT=\${PARENT//\$'\\n'/ };\nif [[ \$PARENT == *sudo* ]]; then\n PARENT=\"/etc/hestiacp/hooks/priv_actions \$PARENT\";\n eval_out=\$(eval \"\$PARENT\");\n eval \"\$eval_out\";\nfi" -); -$hcpp->patch_file( - '/usr/local/hestia/web/inc/main.php', - 'define("HESTIA_CMD", "/usr/bin/sudo /usr/local/hestia/bin/");', - 'define("HESTIA_CMD", "/etc/hestiacp/hooks/bin_actions sudo ");' -); -$hcpp->patch_file( - '/usr/local/hestia/web/inc/main.php', - "require_once dirname(__FILE__) . \"/helpers.php\";", - "require_once dirname(__FILE__) . \"/helpers.php\";\nrequire_once(\"/usr/local/hestia/web/pluginable.php\");" -); -$hcpp->patch_file( - '/usr/local/hestia/web/inc/main.php', - "// Header", - "// Header\n\tglobal \$hcpp;\n\tob_start();\n" -); -$hcpp->patch_file( - '/usr/local/hestia/web/inc/main.php', - 'include $__template_dir . "header.php";', - "include \$__template_dir . \"header.php\";\n\t\$args = [ 'TAB' => \$TAB, 'page' => \$page, 'user' => \$user, 'content' => ob_get_clean() ];\n echo \$hcpp->do_action('hcpp_render_header', \$args)['content'];\n" -); -$hcpp->patch_file( - '/usr/local/hestia/web/inc/main.php', - "// Panel", - "// Panel\n\tob_start();\n" -); -$hcpp->patch_file( - '/usr/local/hestia/web/inc/main.php', - "// Policies controller", - "\t\$args['content'] = ob_get_clean();\n\techo \$hcpp->do_action('hcpp_render_panel', \$args)['content'];\n\n\t// Policies controller" -); -$hcpp->patch_file( - '/usr/local/hestia/web/inc/main.php', - "// Body", - "// Body\n\tob_start();\n" -); -$hcpp->patch_file( - '/usr/local/hestia/web/inc/main.php', - "// Footer", - "\t\$args['content'] = ob_get_clean();\n\techo \$hcpp->do_action('hcpp_render_body', \$args)['content'];\n\n\t// Footer" -); -$hcpp->patch_file( - '/usr/local/hestia/web/inc/main.php', - "// Footer", - "// Footer\n\tob_start();\n" -); -$hcpp->patch_file( - '/usr/local/hestia/web/inc/main.php', - 'include $__template_dir . "footer.php";', - "include \$__template_dir . \"footer.php\";\n\t\$args['content'] = ob_get_clean();\n\techo \$hcpp->do_action('hcpp_render_footer', \$args)['content'];\n" -); -$hcpp->patch_file( - '/usr/local/hestia/web/inc/main.php', - "\t} else {\n\t\treturn true;\n\t}", - "\t} else {\n\t\tglobal \$hcpp;\n\t\t\$hcpp->do_action('hcpp_csrf_verified');\n\t\treturn true;\n\t}" -); -$hcpp->patch_file( - '/usr/local/hestia/web/inc/main.php', - "function show_alert_message(\$data) {", - "function show_alert_message(\$data) {\n\tglobal \$hcpp;\n\tob_start();\n" -); -$hcpp->patch_file( - '/usr/local/hestia/web/inc/main.php', - "\t\t\t\$msgText,\n\t\t);\n\t}\n", - "\t\t\t\$msgText,\n\t\t);\n\t}\n\t\$args['content'] = ob_get_clean();\n\techo \$hcpp->do_action('hcpp_show_alert_message', \$args)['content'];\n", -); - -// api/index.php -$hcpp->patch_file( - '/usr/local/hestia/web/api/index.php', - 'define("HESTIA_CMD", "/usr/bin/sudo /usr/local/hestia/bin/");', - 'define("HESTIA_CMD", "/etc/hestiacp/hooks/bin_actions sudo ");' -); - -// web/templates/header.php -$hcpp->patch_file( - '/usr/local/hestia/web/templates/header.php', - "\n\npatch_file( - '/usr/local/hestia/web/templates/header.php', - "?>\n\n", - "\$args['content'] = ob_get_clean();\necho \$hcpp->do_action('hcpp_head', \$args)['content'];\n?>\n" -); - -$hcpp->do_action( 'hcpp_post_install' ); +## Execute the patch files script +php -f ./patch_files.php