Преглед изворни кода

Merge pull request #1373 from openNAMU/dev

Dev
잉여개발기 (SPDV) пре 4 година
родитељ
комит
08d8d82e10
9 измењених фајлова са 197 додато и 244 уклоњено
  1. 1 116
      app.py
  2. 2 98
      emergency_tool.py
  3. 6 1
      lang/en-US.json
  4. 6 1
      lang/ko-KR.json
  5. 2 2
      route/main_error_404.py
  6. 22 9
      route/main_func_setting.py
  7. 1 1
      route/recent_history_tool.py
  8. 156 15
      route/tool/func.py
  9. 1 1
      version.json

+ 1 - 116
app.py

@@ -31,122 +31,7 @@ version_list = json.loads(open(
 ).read())
 
 # Init-DB
-if os.path.exists(os.path.join('data', 'set.json')):
-    db_set_list = ['db', 'db_type']
-    set_data = json.loads(open(
-        os.path.join('data', 'set.json'), 
-        encoding = 'utf8'
-    ).read())
-    for i in db_set_list:
-        if not i in set_data:
-            print('Please delete set.json')
-            print('----')
-            raise
-
-    print('DB name : ' + set_data['db'])
-    print('DB type : ' + set_data['db_type'])
-elif os.getenv('NAMU_DB') or os.getenv('NAMU_DB_TYPE'):
-    set_data = {}
-
-    if os.getenv('NAMU_DB'):
-        set_data['db'] = os.getenv('NAMU_DB')
-    else:
-        set_data['db'] = 'data'
-
-    if os.getenv('NAMU_DB_TYPE'):
-        set_data['db'] = os.getenv('NAMU_DB_TYPE')
-    else:
-        set_data['db'] = 'sqlite'
-
-    print('DB name : ' + set_data['db'])
-    print('DB type : ' + set_data['db_type'])
-else:
-    set_data = {}
-    normal_db_type = ['sqlite', 'mysql']
-
-    print('DB type (' + normal_db_type[0] + ') [' + ', '.join(normal_db_type) + '] : ', end = '')
-    data_get = str(input())
-    if data_get == '' or not data_get in normal_db_type:
-        set_data['db_type'] = 'sqlite'
-    else:
-        set_data['db_type'] = data_get
-
-    all_src = []
-    if set_data['db_type'] == 'sqlite':
-        for i_data in os.listdir("."):
-            f_src = re.search(r"(.+)\.db$", i_data)
-            if f_src:
-                all_src += [f_src.group(1)]
-
-    print('DB name (data) [' + ', '.join(all_src) + '] : ', end = '')
-    
-    data_get = str(input())
-    if data_get == '':
-        set_data['db'] = 'data'
-    else:
-        set_data['db'] = data_get
-        
-    with open(os.path.join('data', 'set.json'), 'w', encoding = 'utf8') as f:
-        f.write(json.dumps(set_data))
-
-data_db_set = {}
-data_db_set['name'] = set_data['db']
-data_db_set['type'] = set_data['db_type']
-
-if data_db_set['type'] == 'mysql':
-    if os.path.exists(os.path.join('data', 'mysql.json')):
-        db_set_list = ['user', 'password', 'host', 'port']
-        set_data = json.loads(
-            open(
-                os.path.join('data', 'mysql.json'),
-                encoding = 'utf8'
-            ).read()
-        )
-        for i in db_set_list:
-            if not i in set_data:
-                os.remove(os.path.join('data', 'mysql.json'))
-                
-                break
-                
-        set_data_mysql = set_data
-    
-    if not os.path.exists(os.path.join('data', 'mysql.json')):
-        set_data_mysql = {}
-        
-        print('DB user ID : ', end = '')
-        set_data_mysql['user'] = str(input())
-
-        print('DB password : ', end = '')
-        set_data_mysql['password'] = str(input())
-                
-        print('DB host (localhost) : ', end = '')
-        set_data_mysql['host'] = str(input())
-        if set_data_mysql['host'] == '':
-            set_data_mysql['host'] = 'localhost'
-
-        print('DB port (3306) : ', end = '')
-        set_data_mysql['port'] = str(input())
-        if set_data_mysql['port'] == '':
-            set_data_mysql['port'] = '3306'
-
-        with open(
-            os.path.join('data', 'mysql.json'), 
-            'w', 
-            encoding = 'utf8'
-        ) as f:
-            f.write(json.dumps(set_data_mysql))
-            
-    data_db_set['mysql_user'] = set_data_mysql['user']
-    data_db_set['mysql_pw'] = set_data_mysql['password']
-    if 'host' in set_data_mysql:
-        data_db_set['mysql_host'] = set_data_mysql['host']
-    else:
-        data_db_set['mysql_host'] = 'localhost'
-    
-    if 'port' in set_data_mysql:
-        data_db_set['mysql_port'] = set_data_mysql['port']
-    else:
-        data_db_set['mysql_port'] = '3306'
+data_db_set = class_check_json()
 
 db_data_get(data_db_set['type'])
 do_db_set(data_db_set)

+ 2 - 98
emergency_tool.py

@@ -8,106 +8,10 @@ while True:
         break
 
 if data_db_load == 'Y':
-    # Init-DB
-    if os.path.exists(os.path.join('data', 'set.json')):
-        db_set_list = ['db', 'db_type']
-        set_data = json.loads(open(
-            os.path.join('data', 'set.json'),
-            encoding='utf8'
-        ).read())
-        for i in db_set_list:
-            if i not in set_data:
-                print('Please delete set.json')
-                print('----')
-                raise KeyError(i)
-
-        print('DB name : ' + set_data['db'])
-        print('DB type : ' + set_data['db_type'])
-    elif os.getenv('NAMU_DB') or os.getenv('NAMU_DB_TYPE'):
-        set_data = {}
-
-        if os.getenv('NAMU_DB'):
-            set_data['db'] = os.getenv('NAMU_DB')
-        else:
-            set_data['db'] = 'data'
-
-        if os.getenv('NAMU_DB_TYPE'):
-            set_data['db'] = os.getenv('NAMU_DB_TYPE')
-        else:
-            set_data['db'] = 'sqlite'
-
-        print('DB name : ' + set_data['db'])
-        print('DB type : ' + set_data['db_type'])
-    else:
-        set_data = {}
-        normal_db_type = ['sqlite', 'mysql']
-
-        data_get = input('DB type (' + normal_db_type[0] + ') [' + ', '.join(normal_db_type) + '] : ')
-        if data_get == '' or not data_get in normal_db_type:
-            set_data['db_type'] = 'sqlite'
-        else:
-            set_data['db_type'] = data_get
-
-        all_src = []
-        if set_data['db_type'] == 'sqlite':
-            for i_data in os.listdir("."):
-                f_src = re.search(r"(.+)\.db$", i_data)
-                if f_src:
-                    all_src += [f_src.group(1)]
-
-        data_get = input('DB name (data) [' + ', '.join(all_src) + '] : ')
-        if data_get == '':
-            set_data['db'] = 'data'
-        else:
-            set_data['db'] = data_get
-
-        with open(os.path.join('data', 'set.json'), 'w', encoding='utf8') as f:
-            f.write(json.dumps(set_data))
-
-    data_db_set = {'name': set_data['db'], 'type': set_data['db_type']}
-
-    if data_db_set['type'] == 'mysql':
-        if os.path.exists(os.path.join('data', 'mysql.json')):
-            with open(os.path.join('data', 'mysql.json'), encoding='utf8') as f:
-                set_data = json.loads(f.read())
-
-            for i in ('user', 'password', 'host', 'port'):
-                if i not in set_data:
-                    print('Please delete mysql.json')
-                    print('----')
-                    raise KeyError(i)
-
-            set_data_mysql = set_data
-        else:
-            set_data_mysql = {
-                'user': input('DB user ID : '),
-                'password': input('DB password : '),
-                'host': input('DB host (localhost) : '),
-                'port': input('DB port (3306) : '),
-            }
-
-            if set_data_mysql['host'] == '':
-                set_data_mysql['host'] = 'localhost'
-
-            if set_data_mysql['port'] == '':
-                set_data_mysql['port'] = '3306'
-
-            with open(os.path.join('data', 'mysql.json'), 'w', encoding='utf8') as f:
-                f.write(json.dumps(set_data_mysql))
-
-        data_db_set['mysql_user'] = set_data_mysql['user']
-        data_db_set['mysql_pw'] = set_data_mysql['password']
-        if 'host' in set_data_mysql:
-            data_db_set['mysql_host'] = set_data_mysql['host']
-        else:
-            data_db_set['mysql_host'] = 'localhost'
-
-        if 'port' in set_data_mysql:
-            data_db_set['mysql_port'] = set_data_mysql['port']
-        else:
-            data_db_set['mysql_port'] = '3306'
+    data_db_set = class_check_json()
 
     db_data_get(data_db_set['type'])
+    do_db_set(data_db_set)
     load_db = get_db_connect_old(data_db_set)
 
     conn = load_db.db_load()

+ 6 - 1
lang/en-US.json

@@ -105,6 +105,7 @@
         "empty" : "Empty",
         "domain" : "Domain",
         "result" : "Result",
+        "incomplete" : "Incomplete",
         "_comment_1.1_" : "Time",
             "second" : "Second(s)",
             "hour" : "Hour(s)",
@@ -298,8 +299,11 @@
                 "edit_help" : "Editing textarea phrase",
                 "upload_help" : "File upload phrase",
                 "upload_default" : "File upload other Default",
+                "topic_text" : "Discussion textarea phrase",
             "_comment_2.2.4_" : "Ext_API",
                 "recaptcha" : "reCAPTCHA",
+                "hcaptcha" : "hCAPTCHA",
+                "captcha" : "CAPTCHA",
                 
                 "email_setting" : "Email settings",
                 "_comment_2.2.4.1_" : "SMTP",
@@ -339,7 +343,8 @@
             "_comment_2.3.3_" : "Record",
                 "edit_record" : "Edit record",
                 "discussion_record" : "Discussion record",
-        "_comment_2.4_" : "Topic tool",
+        "_comment_2.4_" : "Topic",
+            "make_new_topic" : "Make new discussion",
             "topic_tool" : "Discussion management tools",
             "topic_state" : "Discussion status",
             "topic_delete" : "Delete this thread",

+ 6 - 1
lang/ko-KR.json

@@ -414,5 +414,10 @@
     "password_instead_key": "대체 인증키",
     "key_change": "키 변경",
     "key_delete": "키 삭제",
-    "email_delete": "이메일 삭제"
+    "email_delete": "이메일 삭제",
+    "incomplete": "미완성",
+    "topic_text": "토론 기본 문구",
+    "hcaptcha": "hCAPTCHA",
+    "captcha": "CAPTCHA",
+    "make_new_topic": "새 토론 생성"
 }

+ 2 - 2
route/main_error_404.py

@@ -1,7 +1,7 @@
 from .tool.func import *
 
-def main_error_404(e):
+def main_error_404(e = ''):
     if os.path.exists('404.html') and flask.request.path != '/':
-        return open('404.html', encoding='utf8').read(), 404
+        return open('404.html', encoding = 'utf8').read(), 404
     else:
         return redirect('/w/' + wiki_set(2))

+ 22 - 9
route/main_func_setting.py

@@ -536,20 +536,29 @@ def main_func_setting(db_set, num = 0):
 
                 security_radios = ''
                 for i in ['tls', 'starttls', 'plain']:
-                    security_radios += '<input name="smtp_security" type="radio" value="' + i + '" ' + ('checked' if d_list[4] == i else '') + '>' + i + '<hr class="main_hr">'
+                    if d_list[4] == i:
+                        security_radios = '<option value="' + i + '">' + i + '</option>' + security_radios
+                    else:
+                        security_radios += '<option value="' + i + '">' + i + '</option>'
 
+                re_ver_list = {
+                    '' : 'reCAPTCHA v2',
+                    'v3' : 'reCAPTCHA v3',
+                    'h' : 'hCAPTCHA'
+                }
                 re_ver = ''
-                if d_list[7] == '':
-                    re_ver += '<option value="">v2</option><option value="v3">v3</option>'
-                else:
-                    re_ver += '<option value="v3">v3</option><option value="">v2</option>'
+                for i in re_ver_list:
+                    if d_list[7] == i:
+                        re_ver = '<option value="' + i + '">' + re_ver_list[i] + '</option>' + re_ver
+                    else:
+                        re_ver += '<option value="' + i + '">' + re_ver_list[i] + '</option>'
 
                 return easy_minify(flask.render_template(skin_check(),
                     imp = [load_lang('ext_api_req_set'), wiki_set(), wiki_custom(), wiki_css([0, 0])],
                     data = '''
                         <form method="post" id="main_set_data">
-                            <h2>1. ''' + load_lang('recaptcha') + '''</h2>
-                            <a href="https://www.google.com/recaptcha/admin">(Google)</a>
+                            <h2>1. ''' + load_lang('captcha') + '''</h2>
+                            <a href="https://www.google.com/recaptcha/">(''' + load_lang('recaptcha') + ''')</a> <a href="https://www.hcaptcha.com/">(''' + load_lang('hcaptcha') + ''')</a>
                             <hr class="main_hr">
 
                             <span>''' + load_lang('public_key') + '''</span>
@@ -562,6 +571,8 @@ def main_func_setting(db_set, num = 0):
                             <input name="sec_re" value="''' + html.escape(d_list[1]) + '''">
                             <hr class="main_hr">
 
+                            <span>''' + load_lang('version') + '''</span>
+                            <hr class="main_hr">
                             <select name="recaptcha_ver">
                                 ''' + re_ver + '''
                             </select>
@@ -586,7 +597,9 @@ def main_func_setting(db_set, num = 0):
 
                             <span>''' + load_lang('smtp_security') + '''</span>
                             <hr class="main_hr">
-                            ''' + security_radios + '''
+                            <select name="recaptcha_ver">
+                                ''' + security_radios + '''
+                            </select>
                             <hr class="main_hr">
 
                             <span>''' + load_lang('smtp_username') + '''</span>
@@ -598,7 +611,7 @@ def main_func_setting(db_set, num = 0):
                             <hr class="main_hr">
                             <input type="password" name="smtp_pass" value="''' + html.escape(d_list[6]) + '''">
 
-                            <h2>3. ''' + load_lang('oauth') + '''</h2>
+                            <h2>3. ''' + load_lang('oauth') + ''' (''' + load_lang('incomplete') + ''')</h2>
                             <a href="https://developers.google.com/identity/protocols/oauth2">(Google)</a>
                             <hr class="main_hr">
 

+ 1 - 1
route/recent_history_tool.py

@@ -34,7 +34,7 @@ def recent_history_tool_2(conn, name, rev):
         else:
             data += load_lang('hide')
             
-        data += '</li>'
+        data += '</a></li>'
         data += '</ul>'
 
     if admin_check() == 1:

+ 156 - 15
route/tool/func.py

@@ -78,7 +78,7 @@ import pymysql
 
 if sys.version_info < (3, 6):
     import sha3
-   
+
 # Init-Global
 global_lang = {}
 global_wiki_set = {}
@@ -196,6 +196,130 @@ class get_db_connect:
         load_conn(self.conn_sub)
         self.conn.close()
 
+class class_check_json:
+    def do_check_set_json():
+        if os.getenv('NAMU_DB') or os.getenv('NAMU_DB_TYPE'):
+            set_data = {}
+            set_data['db'] = os.getenv('NAMU_DB') if os.getenv('NAMU_DB') else 'data'
+            set_data['db'] = os.getenv('NAMU_DB_TYPE') if os.getenv('NAMU_DB_TYPE') else 'sqlite'
+        else:
+            if os.path.exists(os.path.join('data', 'set.json')):
+                db_set_list = ['db', 'db_type']
+                set_data = json.loads(open(
+                    os.path.join('data', 'set.json'), 
+                    encoding = 'utf8'
+                ).read())
+                for i in db_set_list:
+                    if not i in set_data:
+                        os.remove(os.path.join('data', 'set.json'))
+                        
+                        break
+            
+            if not os.path.exists(os.path.join('data', 'set.json')):
+                set_data = {}
+                normal_db_type = ['sqlite', 'mysql']
+
+                print('DB type (' + normal_db_type[0] + ') [' + ', '.join(normal_db_type) + '] : ', end = '')
+                data_get = str(input())
+                if data_get == '' or not data_get in normal_db_type:
+                    set_data['db_type'] = 'sqlite'
+                else:
+                    set_data['db_type'] = data_get
+
+                all_src = []
+                if set_data['db_type'] == 'sqlite':
+                    for i_data in os.listdir("."):
+                        f_src = re.search(r"(.+)\.db$", i_data)
+                        if f_src:
+                            all_src += [f_src.group(1)]
+
+                print('DB name (data) [' + ', '.join(all_src) + '] : ', end = '')
+
+                data_get = str(input())
+                if data_get == '':
+                    set_data['db'] = 'data'
+                else:
+                    set_data['db'] = data_get
+
+                with open(os.path.join('data', 'set.json'), 'w', encoding = 'utf8') as f:
+                    f.write(json.dumps(set_data))
+
+        print('DB name : ' + set_data['db'])
+        print('DB type : ' + set_data['db_type'])
+        
+        data_db_set = {}
+        data_db_set['name'] = set_data['db']
+        data_db_set['type'] = set_data['db_type']
+
+        return data_db_set
+
+    def do_check_mysql_json(data_db_set):
+        if os.path.exists(os.path.join('data', 'mysql.json')):
+            db_set_list = ['user', 'password', 'host', 'port']
+            set_data = json.loads(
+                open(
+                    os.path.join('data', 'mysql.json'),
+                    encoding = 'utf8'
+                ).read()
+            )
+            for i in db_set_list:
+                if not i in set_data:
+                    os.remove(os.path.join('data', 'mysql.json'))
+                    
+                    break
+
+            set_data_mysql = set_data
+
+        if not os.path.exists(os.path.join('data', 'mysql.json')):
+            set_data_mysql = {}
+
+            print('DB user ID : ', end = '')
+            set_data_mysql['user'] = str(input())
+
+            print('DB password : ', end = '')
+            set_data_mysql['password'] = str(input())
+
+            print('DB host (localhost) : ', end = '')
+            set_data_mysql['host'] = str(input())
+            if set_data_mysql['host'] == '':
+                set_data_mysql['host'] = 'localhost'
+
+            print('DB port (3306) : ', end = '')
+            set_data_mysql['port'] = str(input())
+            if set_data_mysql['port'] == '':
+                set_data_mysql['port'] = '3306'
+
+            with open(
+                os.path.join('data', 'mysql.json'), 
+                'w', 
+                encoding = 'utf8'
+            ) as f:
+                f.write(json.dumps(set_data_mysql))
+
+        data_db_set['mysql_user'] = set_data_mysql['user']
+        data_db_set['mysql_pw'] = set_data_mysql['password']
+        if 'host' in set_data_mysql:
+            data_db_set['mysql_host'] = set_data_mysql['host']
+        else:
+            data_db_set['mysql_host'] = 'localhost'
+
+        if 'port' in set_data_mysql:
+            data_db_set['mysql_port'] = set_data_mysql['port']
+        else:
+            data_db_set['mysql_port'] = '3306'
+            
+        return data_db_set
+    
+    def __init__(self):
+        self.data_db_set = {}
+            
+    def __new__(self):
+        self.data_db_set = self.do_check_set_json()
+        if self.data_db_set['type'] == 'mysql':
+            self.data_db_set = self.do_check_mysql_json(self.data_db_set)
+        
+        return self.data_db_set
+
 def update(ver_num, set_data):
     curs = conn.cursor()
 
@@ -1040,15 +1164,14 @@ def captcha_get():
         
         curs.execute(db_change('select data from other where name = "recaptcha_ver"'))
         rec_ver = curs.fetchall()
-        if  recaptcha and recaptcha[0][0] != '' and \
-            sec_re and sec_re[0][0] != '':
+        if recaptcha and recaptcha[0][0] != '' and sec_re and sec_re[0][0] != '':
             if not rec_ver or rec_ver[0][0] == '':
                 data += '' + \
                     '<script src="https://www.google.com/recaptcha/api.js" async defer></script>' + \
                     '<div class="g-recaptcha" data-sitekey="' + recaptcha[0][0] + '"></div>' + \
                     '<hr class="main_hr">' + \
                 ''
-            else:
+            elif rec_ver[0][0] == 'v3':
                 data += '' + \
                     '<script src="https://www.google.com/recaptcha/api.js?render=' + recaptcha[0][0] + '"></script>' + \
                     '<input type="hidden" id="g-recaptcha" name="g-recaptcha">' + \
@@ -1060,25 +1183,43 @@ def captcha_get():
                         '});' + \
                     '</script>' + \
                 ''
+            else:
+                data += '''
+                    <script src="https://js.hcaptcha.com/1/api.js" async defer></script>
+                    <div class="h-captcha" data-sitekey="''' + recaptcha[0][0] + '''"></div>
+                    <hr class="main_hr">
+                '''
 
     return data
 
 def captcha_post(re_data, num = 1):
     curs = conn.cursor()
 
-    if num == 1:
+    if num == 1 and ip_or_user() != 0:
         curs.execute(db_change('select data from other where name = "sec_re"'))
         sec_re = curs.fetchall()
-        if  sec_re and sec_re[0][0] != '' and \
-            ip_or_user() != 0 and captcha_get() != '':
-            data = requests.get(
-                'https://www.google.com/recaptcha/api/siteverify' + \
-                '?secret=' + sec_re[0][0] + '&response=' + re_data
-            )
-            if data.status_code == 200:
-                json_data = json.loads(data.text)
-                if json_data['success'] != True:
-                    return 1
+        
+        curs.execute(db_change('select data from other where name = "recaptcha_ver"'))
+        rec_ver = curs.fetchall()
+        if captcha_get() != '':
+            if not rec_ver or rec_ver[0][0] in ('', 'v3'):
+                data = requests.get(
+                    'https://www.google.com/recaptcha/api/siteverify' + \
+                    '?secret=' + sec_re[0][0] + '&response=' + re_data
+                )
+                if data.status_code == 200:
+                    json_data = json.loads(data.text)
+                    if json_data['success'] != True:
+                        return 1
+            else:
+                data = requests.get(
+                    'https://hcaptcha.com/siteverify' + \
+                    '?secret=' + sec_re[0][0] + '&response=' + re_data
+                )
+                if data.status_code == 200:
+                    json_data = json.loads(data.text)
+                    if json_data['success'] != True:
+                        return 1
 
         return 0
 

+ 1 - 1
version.json

@@ -1,6 +1,6 @@
 {
     "beta" : {
-        "r_ver" : "v3.4.3 (stable2) (beta20) (dev36)",
+        "r_ver" : "v3.4.3 (stable3) (beta21) (dev38)",
         "c_ver" : "3500101",
         "s_ver" : "3500110"
     }