Fixes
This commit is contained in:
155
usr/local/mgr5/addon/nginx_form_rewrite_plugin.py
Normal file → Executable file
155
usr/local/mgr5/addon/nginx_form_rewrite_plugin.py
Normal file → Executable file
@@ -3,32 +3,163 @@
|
||||
import os
|
||||
import xml.etree.ElementTree as etree
|
||||
from sys import stdin
|
||||
|
||||
import re
|
||||
import datetime
|
||||
|
||||
CONFIG_PATH = "/usr/share/nginx/modules/ngx_http_apache_rewrite_module.conf"
|
||||
MODULE_LINE = 'load_module "/usr/lib64/nginx/modules/ngx_http_apache_rewrite_module.so";'
|
||||
MODULE_PATTERN = re.compile(r'^\s*load_module\s+"\/usr\/lib64\/nginx\/modules\/ngx_http_apache_rewrite_module.so";\s*$')
|
||||
LOG_PATH = "/usr/local/mgr5/var/nginx-mod-rewrite-plugin-addon.log"
|
||||
|
||||
def _log(msg: str) -> None:
|
||||
try:
|
||||
with open(LOG_PATH, 'a') as log_file:
|
||||
log_file.write(f"{datetime.datetime.now().isoformat()} - {msg}\n")
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
def nginx_restart():
|
||||
_log("nginx_restart called")
|
||||
try:
|
||||
result = os.system("systemctl restart nginx")
|
||||
if result != 0:
|
||||
_log(f"systemctl restart nginx returned non-zero exit status {result}")
|
||||
else:
|
||||
_log("Nginx restarted successfully")
|
||||
except Exception as e:
|
||||
_log(f"Error restarting nginx: {e}")
|
||||
|
||||
def check_nginx_rewrite_module():
|
||||
config_path = "/usr/share/nginx/modules/ngx_http_apache_rewrite_module.conf"
|
||||
if not os.path.exists(config_path):
|
||||
_log("check_nginx_rewrite_module called")
|
||||
if not os.path.exists(CONFIG_PATH):
|
||||
_log(f"Config file {CONFIG_PATH} does not exist")
|
||||
return False
|
||||
pattern = re.compile(r'^\s*load_module\s+"\/usr\/lib64\/nginx\/modules\/ngx_http_apache_rewrite_module.so";\s*$')
|
||||
with open(config_path, 'r') as f:
|
||||
try:
|
||||
with open(CONFIG_PATH, 'r') as f:
|
||||
for line in f:
|
||||
stripped = line.lstrip()
|
||||
if stripped.startswith('#'):
|
||||
continue
|
||||
if pattern.match(line):
|
||||
if MODULE_PATTERN.match(line):
|
||||
_log("Module found and enabled in config")
|
||||
return True
|
||||
_log("Module not found in config")
|
||||
return False
|
||||
except Exception as e:
|
||||
_log(f"Error reading config file {CONFIG_PATH}: {e}")
|
||||
return False
|
||||
|
||||
def set_nginx_rewrite_module(enable: bool):
|
||||
_log(f"set_nginx_rewrite_module called with enable={enable}")
|
||||
if enable:
|
||||
if not os.path.exists(CONFIG_PATH):
|
||||
try:
|
||||
with open(CONFIG_PATH, 'w') as f:
|
||||
f.write(MODULE_LINE + "\n")
|
||||
_log(f"Created config file {CONFIG_PATH} and added module line")
|
||||
except Exception as e:
|
||||
_log(f"Failed to create config file {CONFIG_PATH}: {e}")
|
||||
else:
|
||||
try:
|
||||
with open(CONFIG_PATH, 'r') as f:
|
||||
lines = f.readlines()
|
||||
found_uncommented = False
|
||||
found_commented = False
|
||||
idx_commented = -1
|
||||
for idx, line in enumerate(lines):
|
||||
stripped = line.lstrip()
|
||||
if stripped.startswith('#'):
|
||||
stripped_no_hash = stripped.lstrip('#').lstrip()
|
||||
if MODULE_PATTERN.match(stripped_no_hash):
|
||||
found_commented = True
|
||||
idx_commented = idx
|
||||
else:
|
||||
if MODULE_PATTERN.match(stripped):
|
||||
found_uncommented = True
|
||||
if not found_uncommented:
|
||||
if found_commented:
|
||||
lines[idx_commented] = MODULE_LINE + "\n"
|
||||
_log(f"Uncommented module line at line {idx_commented+1}")
|
||||
else:
|
||||
lines.append(MODULE_LINE + "\n")
|
||||
_log("Appended module line to config")
|
||||
with open(CONFIG_PATH, 'w') as f:
|
||||
f.writelines(lines)
|
||||
_log(f"Wrote changes to config file {CONFIG_PATH}")
|
||||
except Exception as e:
|
||||
_log(f"Error processing config file {CONFIG_PATH}: {e}")
|
||||
else:
|
||||
if not os.path.exists(CONFIG_PATH):
|
||||
_log(f"Config file {CONFIG_PATH} does not exist; nothing to disable")
|
||||
return
|
||||
try:
|
||||
with open(CONFIG_PATH, 'r') as f:
|
||||
lines = f.readlines()
|
||||
changed = False
|
||||
for idx, line in enumerate(lines):
|
||||
stripped = line.lstrip()
|
||||
if stripped.startswith('#'):
|
||||
continue
|
||||
if MODULE_PATTERN.match(stripped):
|
||||
lines[idx] = '#' + line
|
||||
changed = True
|
||||
_log(f"Commented out module line at line {idx+1}")
|
||||
if changed:
|
||||
with open(CONFIG_PATH, 'w') as f:
|
||||
f.writelines(lines)
|
||||
_log(f"Wrote changes to config file {CONFIG_PATH} after disabling module")
|
||||
else:
|
||||
_log("No uncommented module line found to disable")
|
||||
except Exception as e:
|
||||
_log(f"Error disabling module in config file {CONFIG_PATH}: {e}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
root = etree.parse(stdin).getroot()
|
||||
|
||||
func = os.getenv('PARAM_func')
|
||||
user_level = os.getenv('AUTH_LEVEL', '2')
|
||||
|
||||
if func == 'nginx_mod_rewrite_plugin_menu_settings':
|
||||
nginx_mod_rewrite_plugin_menu_module_enable = os.getenv("PARAM_nginx_mod_rewrite_plugin_menu_module_enable")
|
||||
|
||||
|
||||
|
||||
_log(f"Function called: {func}, User level: {user_level}")
|
||||
if int(user_level) < 16:
|
||||
_log("User level below user; skipping actions at all")
|
||||
etree.dump(root)
|
||||
quit(0)
|
||||
|
||||
if func == 'nginx_mod_rewrite_plugin_menu_settings':
|
||||
# Проверка уровня доступа - действия только для admin и выше (уровень 29+)
|
||||
if int(user_level) < 29:
|
||||
_log("User level below admin; skipping actions")
|
||||
etree.dump(root)
|
||||
quit(0)
|
||||
|
||||
nginx_mod_rewrite_plugin_menu_module_enable = os.getenv("PARAM_nginx_mod_rewrite_plugin_menu_module_enable")
|
||||
mod_enabled = check_nginx_rewrite_module()
|
||||
_log(f"Current module state: {'enabled' if mod_enabled else 'disabled'}")
|
||||
|
||||
if os.getenv('PARAM_clicked_button') == 'ok' and nginx_mod_rewrite_plugin_menu_module_enable is not None:
|
||||
if nginx_mod_rewrite_plugin_menu_module_enable == "on" and not mod_enabled:
|
||||
set_nginx_rewrite_module(True)
|
||||
nginx_restart()
|
||||
new_element = etree.Element("nginx_mod_rewrite_plugin_menu_module_enable")
|
||||
new_element.text = "on"
|
||||
root.append(new_element)
|
||||
elif nginx_mod_rewrite_plugin_menu_module_enable == "off" and mod_enabled:
|
||||
set_nginx_rewrite_module(False)
|
||||
nginx_restart()
|
||||
new_element = etree.Element("nginx_mod_rewrite_plugin_menu_module_enable")
|
||||
new_element.text = "off"
|
||||
root.append(new_element)
|
||||
else:
|
||||
new_element = etree.Element("nginx_mod_rewrite_plugin_menu_module_enable")
|
||||
new_element.text = "on" if mod_enabled else "off"
|
||||
root.append(new_element)
|
||||
else:
|
||||
new_element = etree.Element("nginx_mod_rewrite_plugin_menu_module_enable")
|
||||
new_element.text = "on" if mod_enabled else "off"
|
||||
root.append(new_element)
|
||||
|
||||
elif func == "webdomain.edit":
|
||||
_log("webdomain.edit handler")
|
||||
elif func == "site.edit":
|
||||
_log("site.edit handler")
|
||||
etree.dump(root)
|
||||
quit(0)
|
||||
|
||||
@@ -73,6 +73,9 @@
|
||||
<messages name="site.edit">
|
||||
<msg name="site_nginx_mod_rewrite_plugin_site_edit_enable">Включить для сайта поддержку mod_rewrite для nginx</msg>
|
||||
</messages>
|
||||
<messages name="webdomain.edit">
|
||||
<msg name="nginx_mod_rewrite_plugin_site_edit_enable">Включить для сайта поддержку mod_rewrite для nginx</msg>
|
||||
</messages>
|
||||
<messages name="desktop">
|
||||
<msg name="modernmenu_nginx_mod_rewrite_plugin_menu">mod_rewrite для nginx</msg>
|
||||
<msg name="modernmenu_nginx_mod_rewrite_plugin_menu_settings">Настройка модуля nginx</msg>
|
||||
|
||||
Reference in New Issue
Block a user