diff --git a/usr/local/mgr5/addon/nginx_form_rewrite_plugin.py b/usr/local/mgr5/addon/nginx_form_rewrite_plugin.py index 6c7f0f0..f1f5cb0 100755 --- a/usr/local/mgr5/addon/nginx_form_rewrite_plugin.py +++ b/usr/local/mgr5/addon/nginx_form_rewrite_plugin.py @@ -53,6 +53,69 @@ def dump_all_to_log(root): except Exception: pass +def site_state(param): + """ + Get or set the state of a site in sites_settings.cfg. + :param param: either a string key or a dict {key: 'on'/'off'}. + :return: For string key, returns value string or False if not found. + """ + config_path = "/usr/local/ispmanager-mod_rewrite/settings/sites_settings.cfg" + + if isinstance(param, str): + param_name = param + if not os.path.exists(config_path): + return "off" + try: + with open(config_path, 'r', encoding='utf-8') as f: + for line in f: + stripped = line.strip() + if stripped.startswith('#'): + continue + pattern = rf'^\s*{re.escape(param_name)}\s*=\s*(.*)$' + m = re.match(pattern, line) + if m: + return m.group(1).strip() + except Exception: + return "off" + return "off" + + elif isinstance(param, dict): + # Write mode + dir_path = os.path.dirname(config_path) + try: + os.makedirs(dir_path, exist_ok=True) + except Exception: + return None + lines = [] + if os.path.exists(config_path): + try: + with open(config_path, 'r', encoding='utf-8') as f: + lines = f.readlines() + except Exception: + return None + for key, val in param.items(): + actual_value = 'on' if val == 'on' else 'off' + found = False + for idx, line in enumerate(lines): + stripped = line.strip() + if stripped.startswith('#'): + continue + pattern = rf'^\s*{re.escape(key)}\s*=\s*.*$' + if re.match(pattern, line): + lines[idx] = f"{key}={actual_value}\n" + found = True + break + if not found: + lines.append(f"{key}={actual_value}\n") + try: + with open(config_path, 'w', encoding='utf-8') as f: + f.writelines(lines) + except Exception: + return None + return None + else: + return None + def read_global_config(params): """ Reads or writes a parameter in the global settings configuration file. @@ -270,22 +333,33 @@ if __name__ == "__main__": out_error_xml("Access denied") sok = os.environ.get('PARAM_sok', '') type = os.getenv("EVENT_TYPE") + domain = os.environ.get('PARAM_elid', '') if sok == 'ok' and type == 'after': - domain = os.environ.get('PARAM_elid', '') path_to_global_config = f'/etc/nginx/vhosts-resources/{domain}' nginx_mod_rewrite_plugin_site_edit_enable = os.getenv("PARAM_nginx_mod_rewrite_plugin_site_edit_enable") mod_rewrite_global = read_global_config({'param_name': 'global_mod_rewrite_enable'}) if nginx_mod_rewrite_plugin_site_edit_enable == "on": if mod_rewrite_global in ("1", "on"): - new_element = etree.Element("nginx_mod_rewrite_plugin_site_edit_enable") - new_element.text = "on" - root.append(new_element) + _log("Global state enabled") + new_element = etree.Element("nginx_mod_rewrite_plugin_site_edit_enable") + new_element.text = "on" + root.append(new_element) + site_state({ domain: "on"}) else: _log("mod_rewrite should be disbled") + if mod_rewrite_global in ("1", "on"): + _log("Global state enabled") new_element = etree.Element("nginx_mod_rewrite_plugin_site_edit_enable") new_element.text = "off" root.append(new_element) - dump_all_to_log(root) + site_state({ domain: "off"}) + else: + if domain != "": + domain_state = site_state(domain) + if domain_state == "on": + new_element = etree.Element("nginx_mod_rewrite_plugin_site_edit_enable") + new_element.text = "on" + root.append(new_element) elif func == "site.edit": _log("site.edit handler") etree.dump(root)