File size: 3,841 Bytes
a757bd3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import os
import re
import shutil

import public

class NginxExtension:
    _EXTENSION_DIR = "{}/vhost/nginx/extension".format(public.get_panel_path())

    @classmethod
    def set_extension(cls, site_name: str, config_path: str) -> str:
        config_data = public.readFile(config_path)
        if not config_data:
            return ""
        return cls.set_extension_by_config(site_name, config_data)

    @classmethod
    def set_extension_by_config(cls, site_name: str, config_data: str) -> str:
        ext_path = "{}/{}".format(cls._EXTENSION_DIR, site_name)
        if os.path.exists(ext_path):
            if not os.path.isdir(ext_path):
                os.remove(ext_path)
                os.makedirs(ext_path)
        else:
            os.makedirs(ext_path)

        rep_exp_list=[
            re.compile(r"(?<!#)server\s*{(([^{\n]*\n)|(\s*#.*\n)){0,20}\s*root\s+/[^\n]*\n"),
            re.compile(r"(?<!#)server\s*{(([^{\n]*\n)|(\s*#.*\n)){0,20}\s*index\s+[^\n]*\n"),
            re.compile(r"(?<!#)server\s*{(([^{\n]*\n)|(\s*#.*\n)){0,20}\s*server_name\s+[^\n]*\n"),
        ]

        insert_ext = "    include {}/*.conf;\n".format(ext_path)
        for rep_exp in rep_exp_list:
            find_list = list(re.finditer(rep_exp, config_data))
            if not find_list:
                continue
            for tmp in find_list[::-1]:
                config_data = config_data[:tmp.end()] + insert_ext + config_data[tmp.end():]
            break

        return config_data

    @classmethod
    def remove_extension(cls, site_name: str, config_path: str):
        ext_path = "{}/{}".format(cls._EXTENSION_DIR, site_name)
        if os.path.isdir(ext_path):
            shutil.rmtree(ext_path)

        config_data= public.readFile(config_path)
        if not config_data:
            return
        return cls.remove_extension_from_config(site_name, config_data)

    @staticmethod
    def remove_extension_from_config(site_name: str, config_data: str):
        regexp = re.compile(r"\s*include\s+/.*extension/.*/\*\.conf;[^\n]*\n")
        return re.sub(regexp, "\n", config_data)

    @staticmethod
    def has_extension(conf_data: str) -> bool:
        regexp = re.compile(r"\s*include\s+/.*extension/.*/\*\.conf;[^\n]*\n")
        return bool(re.search(regexp, conf_data))


class ApacheExtension(NginxExtension):
    _EXTENSION_DIR = "{}/vhost/apache/extension".format(public.get_panel_path())

    @classmethod
    def set_extension_by_config(cls, site_name: str, config_data: str) -> str:
        ext_path = "{}/{}".format(cls._EXTENSION_DIR, site_name)
        if not os.path.exists(ext_path):
            os.makedirs(ext_path)
        else:
            if not os.path.isdir(ext_path):
                os.remove(ext_path)
                os.makedirs(ext_path)

        rep_exp_list=[
            re.compile(r"<VirtualHost\s+\S+:\d+>\s(.*\n){0,8}\s*ServerAlias\s+[^\n]*\n"),
            re.compile(r"<VirtualHost\s+\S+:\d+>\s(.*\n){0,6}\s*DocumentRoot\s+[^\n]*\n"),
        ]

        insert_ext = "    IncludeOptional {}/*.conf\n".format(ext_path)
        for rep_exp in rep_exp_list:
            find_list = list(re.finditer(rep_exp, config_data))
            if not find_list:
                continue
            for tmp in find_list[::-1]:
                config_data = config_data[:tmp.end()] + insert_ext + config_data[tmp.end():]
            break

        return config_data

    @staticmethod
    def remove_extension_from_config(site_name: str, config_data: str):
        regexp = re.compile(r"\s*IncludeOptional\s+/.*extension/.*/\*\.conf[^\n]*\n")
        return re.sub(regexp, "\n", config_data)

    @staticmethod
    def has_extension(conf_data: str) -> bool:
        regexp = re.compile(r"\s*IncludeOptional\s+/.*extension/.*/\*\.conf[^\n]*\n")
        return bool(re.search(regexp, conf_data))