import os import sys import platform for i in range(0, 2): try: from diff_match_patch import diff_match_patch import werkzeug.routing import werkzeug.debug import flask_reggie import tornado.ioloop import tornado.httpserver import tornado.wsgi import urllib.request import email.mime.text import email.utils import email.header import requests import sqlite3 import pymysql import hashlib import smtplib import asyncio import zipfile import shutil import threading import PIL import logging import random import flask import json import html import re if sys.version_info < (3, 6): import sha3 from .mark import * except ImportError as e: if i == 0: print(e) print('----') if platform.system() == 'Linux' or platform.system() == 'Windows': sys_pip_ins = os.system( 'python' + ('3' if platform.system() != 'Windows' else '') + ' ' + \ '-m pip install --user -r requirements.txt' ) if sys_pip_ins == 0: print('----') try: os.execl(sys.executable, sys.executable, *sys.argv) except: try: os.execl(sys.executable, '"' + sys.executable + '"', *sys.argv) except: print('Error : restart failed') raise else: print('Error : library install failed') raise print('----') print(e) raise global_lang = {} data_css_ver = '77' data_css = '' conn = '' curs = '' def load_conn(data): global conn global curs conn = data curs = conn.cursor() load_conn2(data) def http_warring(): return '''
''' def send_email(who, title, data): try: curs.execute(db_change('' + \ 'select name, data from other ' + \ 'where name = "smtp_email" or name = "smtp_pass" or name = "smtp_server" or name = "smtp_port" or name = "smtp_security"' + \ '')) rep_data = curs.fetchall() smtp_email = '' smtp_pass = '' smtp_server = '' smtp_security = '' smtp_port = '' smtp = '' for i in rep_data: if i[0] == 'smtp_email': smtp_email = i[1] elif i[0] == 'smtp_pass': smtp_pass = i[1] elif i[0] == 'smtp_server': smtp_server = i[1] elif i[0] == 'smtp_security': smtp_security = i[1] elif i[0] == 'smtp_port': smtp_port = i[1] smtp_port = int(smtp_port) if smtp_security == 'plain': smtp = smtplib.SMTP(smtp_server, smtp_port) elif smtp_security == 'starttls': smtp = smtplib.SMTP(smtp_server, smtp_port) smtp.starttls() else: # if smtp_security == 'tls': smtp = smtplib.SMTP_SSL(smtp_server, smtp_port) smtp.login(smtp_email, smtp_pass) domain = load_domain() wiki_name = wiki_set()[0] msg = email.mime.text.MIMEText(data) msg['Subject'] = title msg['From'] = 'openNAMU ' msg['To'] = who smtp.sendmail('openNAMU@' + domain, who, msg.as_string()) smtp.quit() return 1 except Exception as e: print('----') print('Error : email send error') print(e) return 0 def load_domain(): curs.execute(db_change("select data from other where name = 'domain'")) domain = curs.fetchall() domain = domain[0][0] if domain and domain[0][0] != '' else flask.request.host_url return domain def load_random_key(long = 64): return ''.join(random.choice("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") for i in range(long)) def last_change(data): json_address = re.sub(r"(((?!\.|\/).)+)\.html$", "set.json", skin_check()) try: json_data = json.loads(open(json_address, encoding='utf8').read()) except: json_data = 0 if json_data != 0: for j_data in json_data: if "class" in json_data[j_data]: if "require" in json_data[j_data]: re_data = re.compile("<((?:" + j_data + ")( (?:(?!>).)*)?)>") s_data = re_data.findall(data) for i_data in s_data: e_data = 0 for j_i_data in json_data[j_data]["require"]: re_data_2 = re.compile("( |^)" + j_i_data + " *= *[\'\"]" + json_data[j_data]["require"][j_i_data] + "[\'\"]") if not re_data_2.search(i_data[1]): re_data_2 = re.compile("( |^)" + j_i_data + "=" + json_data[j_data]["require"][j_i_data] + "(?: |$)") if not re_data_2.search(i_data[1]): e_data = 1 break if e_data == 0: re_data_3 = re.compile("<" + i_data[0] + ">") data = re_data_3.sub("<" + i_data[0] + " class=\"" + json_data[j_data]["class"] + "\">", data) else: re_data = re.compile("<(?P" + j_data + "(?: (?:(?!>).)*)?)>") data = re_data.sub("<\g class=\"" + json_data[j_data]["class"] + "\">", data) return data def easy_minify(data, tool = None): return last_change(data) def render_set(doc_name = '', doc_data = '', data_type = 'view', data_in = '', doc_acl = ''): # data_type in ['view', 'raw', 'api_view', 'backlink'] doc_acl = acl_check(doc_name, 'render') if doc_acl == '' else doc_acl doc_data = 0 if not doc_data else doc_data if doc_acl == 1: return 'HTTP Request 401.3' else: if data_type == 'raw': return doc_data else: if doc_data != 0: return render_do(doc_name, doc_data, data_type, data_in) else: return 'HTTP Request 404' def update(ver_num, set_data): print('----') # 업데이트 하위 호환 유지 함수 if ver_num < 3160027: print('Add init set') set_init() if ver_num < 3170002: curs.execute(db_change("select html from html_filter where kind = 'extension'")) if not curs.fetchall(): for i in ['jpg', 'jpeg', 'png', 'gif', 'webp']: curs.execute(db_change("insert into html_filter (html, kind) values (?, 'extension')"), [i]) if ver_num < 3170400: curs.execute(db_change("select title, sub, code from topic where id = '1'")) for i in curs.fetchall(): curs.execute(db_change("update topic set code = ? where title = ? and sub = ?"), [i[2], i[0], i[1]]) curs.execute(db_change("update rd set code = ? where title = ? and sub = ?"), [i[2], i[0], i[1]]) if ver_num < 3171800: curs.execute(db_change("select data from other where name = 'recaptcha'")) change_rec = curs.fetchall() if change_rec and change_rec[0][0] != '': new_rec = re.search(r'data-sitekey="([^"]+)"', change_rec[0][0]) if new_rec: curs.execute(db_change("update other set data = ? where name = 'recaptcha'"), [new_rec.group(1)]) else: curs.execute(db_change("update other set data = '' where name = 'recaptcha'")) curs.execute(db_change("update other set data = '' where name = 'sec_re'")) if ver_num < 3172800 and set_data['db_type'] == 'mysql': get_data_mysql = json.loads(open('data/mysql.json').read()) with open('data/mysql.json', 'w') as f: f.write('{ "user" : "' + get_data_mysql['user'] + '", "password" : "' + get_data_mysql['password'] + '", "host" : "localhost" }') if ver_num < 3183603: curs.execute(db_change("select block from ban where band = 'O'")) for i in curs.fetchall(): curs.execute(db_change("update ban set block = ?, band = 'regex' where block = ? and band = 'O'"), [ '^' + i[0].replace('.', '\\.'), i[0] ]) curs.execute(db_change("select block from rb where band = 'O'")) for i in curs.fetchall(): curs.execute(db_change("update rb set block = ?, band = 'regex' where block = ? and band = 'O'"), [ '^' + i[0].replace('.', '\\.'), i[0] ]) if ver_num < 3190201: today_time = get_time() curs.execute(db_change("select block, end, why, band, login from ban")) for i in curs.fetchall(): curs.execute(db_change("insert into rb (block, end, today, why, band, login, ongoing) values (?, ?, ?, ?, ?, ?, ?)"), [ i[0], i[1], today_time, i[2], i[3], i[4], '1' ]) if ver_num < 3191301: curs.execute(db_change('' + \ 'select id, title, date from history ' + \ 'where not title like "user:%" ' + \ 'order by date desc ' + \ 'limit 50' + \ '')) data_list = curs.fetchall() for get_data in data_list: curs.execute(db_change("insert into rc (id, title, date, type) values (?, ?, ?, 'normal')"), [ get_data[0], get_data[1], get_data[2] ]) if ver_num < 3202400: curs.execute(db_change("select data from other where name = 'update'")) get_data = curs.fetchall() if get_data and get_data[0][0] == 'master': curs.execute(db_change("update other set data = 'beta' where name = 'update'"), []) # 캐시 초기화 if ver_num < 3206000: curs.execute(db_change('delete from cache_data')) if ver_num < 3202600: curs.execute(db_change("select name, regex, sub from filter")) for i in curs.fetchall(): curs.execute(db_change("insert into html_filter (html, kind, plus, plus_t) values (?, 'regex_filter', ?, ?)"), [ i[0], i[1], i[2] ]) curs.execute(db_change("select title, link, icon from inter")) for i in curs.fetchall(): curs.execute(db_change("insert into html_filter (html, kind, plus, plus_t) values (?, 'inter_wiki', ?, ?)"), [ i[0], i[1], i[2] ]) if ver_num < 3203400: curs.execute(db_change("select user, css from custom")) for i in curs.fetchall(): curs.execute(db_change("insert into user_set (name, id, data) values ('custom_css', ?, ?)"), [ re.sub(r' \(head\)$', '', i[0]), i[1] ]) if ver_num < 3205500: curs.execute(db_change("select title, decu, dis, view, why from acl")) for i in curs.fetchall(): curs.execute(db_change("insert into acl (title, data, type) values (?, ?, ?)"), [i[0], i[1], 'decu']) curs.execute(db_change("insert into acl (title, data, type) values (?, ?, ?)"), [i[0], i[2], 'dis']) curs.execute(db_change("insert into acl (title, data, type) values (?, ?, ?)"), [i[0], i[3], 'view']) curs.execute(db_change("insert into acl (title, data, type) values (?, ?, ?)"), [i[0], i[4], 'why']) conn.commit() print('Update completed') def set_init(): # 초기값 설정 함수 curs.execute(db_change("select html from html_filter where kind = 'email'")) if not curs.fetchall(): for i in ['naver.com', 'gmail.com', 'daum.net', 'kakao.com']: curs.execute(db_change("insert into html_filter (html, kind) values (?, 'email')"), [i]) curs.execute(db_change("select html from html_filter where kind = 'extension'")) if not curs.fetchall(): for i in ['jpg', 'jpeg', 'png', 'gif', 'webp']: curs.execute(db_change("insert into html_filter (html, kind) values (?, 'extension')"), [i]) curs.execute(db_change('select data from other where name = "smtp_server" or name = "smtp_port" or name = "smtp_security"')) if not curs.fetchall(): for i in [['smtp_server', 'smtp.gmail.com'], ['smtp_port', '587'], ['smtp_security', 'starttls']]: curs.execute(db_change("insert into other (name, data) values (?, ?)"), [i[0], i[1]]) def pw_encode(data, type_d = ''): if type_d == '': curs.execute(db_change('select data from other where name = "encode"')) set_data = curs.fetchall() type_d = set_data[0][0] if type_d == 'sha256': return hashlib.sha256(bytes(data, 'utf-8')).hexdigest() else: if sys.version_info < (3, 6): return sha3.sha3_256(bytes(data, 'utf-8')).hexdigest() else: return hashlib.sha3_256(bytes(data, 'utf-8')).hexdigest() def pw_check(data, data2, type_d = 'no', id_d = ''): curs.execute(db_change('select data from other where name = "encode"')) db_data = curs.fetchall() if type_d != 'no': if type_d == '': set_data = 'sha3' else: set_data = type_d else: set_data = db_data[0][0] re_data = 1 if pw_encode(data, set_data) == data2 else 0 if db_data[0][0] != set_data and re_data == 1 and id_d != '': curs.execute(db_change("update user set pw = ?, encode = ? where id = ?"), [pw_encode(data), db_data[0][0], id_d]) return re_data def add_alarm(who, context): curs.execute(db_change('insert into alarm (name, data, date) values (?, ?, ?)'), [who, context, get_time()]) def captcha_get(): data = '' if ip_or_user() != 0: curs.execute(db_change('select data from other where name = "recaptcha"')) recaptcha = curs.fetchall() if recaptcha and recaptcha[0][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] != '': curs.execute(db_change('select data from other where name = "recaptcha_ver"')) rec_ver = curs.fetchall() if not rec_ver or rec_ver[0][0] == '': data += '' + \ '' + \ '
' + \ '
' + \ '' else: data += '' + \ '' + \ '' + \ '' + \ '' return data def captcha_post(re_data, num = 1): if num == 1: 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() != '': try: data = urllib.request.urlopen('https://www.google.com/recaptcha/api/siteverify?secret=' + sec_re[0][0] + '&response=' + re_data) except: data = None if data and data.getcode() == 200: json_data = json.loads(data.read().decode(data.headers.get_content_charset())) if json_data['success'] == True: return 0 else: return 1 else: return 0 else: return 0 else: pass def ua_plus(id, ip, ua, time): curs.execute(db_change("select data from other where name = 'ua_get'")) rep_data = curs.fetchall() if rep_data and rep_data[0][0] != '': pass else: curs.execute(db_change("insert into ua_d (name, ip, ua, today, sub) values (?, ?, ?, ?, '')"), [id, ip, ua, time]) def load_lang(data, num = 2, safe = 0): global global_lang for i in range(0, 2): if i == 0: ip = ip_check() if ip_or_user(ip) == 0: curs.execute(db_change('select data from user_set where name = "lang" and id = ?'), [ip]) rep_data = curs.fetchall() else: if 'lang' in flask.session: rep_data = [[flask.session['lang']]] else: continue else: curs.execute(db_change("select data from other where name = 'language'")) rep_data = curs.fetchall() if rep_data and rep_data[0][0] != '' and rep_data[0][0] != 'default': try: if not rep_data[0][0] in global_lang: lang = json.loads(open(os.path.join('language', rep_data[0][0] + '.json'), encoding='utf8').read()) global_lang[rep_data[0][0]] = lang else: lang = global_lang[rep_data[0][0]] except: continue if data in lang: return lang[data] if safe == 1 else html.escape(lang[data]) else: continue else: continue return html.escape(data + ' (' + rep_data[0][0] + ')') def ip_or_user(data = ''): if data == '': data = ip_check() if re.search(r'(\.|:)', data): return 1 else: return 0 def edit_button(): insert_list = [] curs.execute(db_change("select html, plus from html_filter where kind = 'edit_top'")) db_data = curs.fetchall() for get_data in db_data: insert_list += [[get_data[1], get_data[0]]] data = '' for insert_data in insert_list: data += '(' + insert_data[1] + ') ' if admin_check() == 1: data += (' ' if data != '' else '') + '(' + load_lang('add') + ')' return data + '
' def ip_warring(): if ip_or_user() != 0: curs.execute(db_change('select data from other where name = "no_login_warring"')) data = curs.fetchall() if data and data[0][0] != '': text_data = '' + \ '' + data[0][0] + '' + \ '
' + \ '' else: text_data = '' + \ '' + load_lang('no_login_warring') + '' + \ '
' + \ '' else: text_data = '' return text_data def skin_check(set_n = 0): skin_list = load_skin('marisa', 1) skin = skin_list[0] check_list = [] ip = ip_check() if ip_or_user(ip) == 0: curs.execute(db_change('select data from user_set where name = "skin" and id = ?'), [ip]) skin_exist = curs.fetchall() check_list += skin_exist else: if 'skin' in flask.session: check_list += [[flask.session['skin']]] curs.execute(db_change('select data from other where name = "skin"')) skin_exist = curs.fetchall() check_list += skin_exist for i in check_list: if i[0] != '' and i[0] in skin_list: skin = i[0] break return './views/' + skin + '/index.html' if set_n == 0 else skin def next_fix(link, num, page, end = 50): list_data = '' if num == 1: if len(page) == end: list_data += '' + \ '
' + \ '(' + load_lang('next') + ')' + \ '' elif len(page) != end: list_data += '' + \ '
' + \ '(' + load_lang('previous') + ')' + \ '' else: list_data += '' + \ '
' + \ '(' + load_lang('previous') + ') (' + load_lang('next') + ')' + \ '' return list_data def other2(data): global data_css global data_css_ver data += ['' for _ in range(0, 3 - len(data))] if data_css == '': for i_data in os.listdir(os.path.join("views", "main_css", "css")): if i_data != 'sub': data_css += '' for i_data in os.listdir(os.path.join("views", "main_css", "js")): if i_data != 'sub': data_css += '' data = data[0:2] + ['', ''' ''' + data_css + ''] + data[2:] return data def cut_100(data): if re.search(r'^\/w\/', flask.request.path): data = re.sub(r'' else: end = '
  • ' + load_lang('authority_error') + '
' return easy_minify(flask.render_template(skin_check(), imp = [load_lang('error'), wiki_set(1), custom(), other2([0, 0])], data = '

' + load_lang('error') + '

' + end, menu = 0 )), 401 else: num = int(number_check(data.replace('/error/', ''))) if num == 1: data = load_lang('no_login_error') elif num == 2: data = load_lang('no_exist_user_error') elif num == 3: data = load_lang('authority_error') elif num == 4: data = load_lang('no_admin_block_error') elif num == 5: data = load_lang('skin_error') elif num == 6: data = load_lang('same_id_exist_error') elif num == 7: data = load_lang('long_id_error') elif num == 8: data = load_lang('id_char_error') + ' (' + load_lang('id_filter_list') + ')' elif num == 9: data = load_lang('file_exist_error') elif num == 10: data = load_lang('password_error') elif num == 11: data = load_lang('topic_long_error') elif num == 12: data = load_lang('email_error') elif num == 13: data = load_lang('recaptcha_error') elif num == 14: data = load_lang('file_extension_error') + ' (' + load_lang('extension_filter_list') + ')' elif num == 15: data = load_lang('edit_record_error') elif num == 16: data = load_lang('same_file_error') elif num == 17: data = load_lang('file_capacity_error') + wiki_set(3) elif num == 18: data = load_lang('email_send_error') elif num == 19: data = load_lang('decument_exist_error') elif num == 20: data = load_lang('password_diffrent_error') elif num == 21: data = load_lang('edit_filter_error') elif num == 22: data = load_lang('file_name_error') elif num == 23: data = load_lang('regex_error') elif num == 24: curs.execute(db_change("select data from other where name = 'slow_edit'")) data = load_lang('fast_edit_error') + curs.fetchall()[0][0] elif num == 25: data = load_lang('too_many_dec_error') elif num == 26: data = load_lang('application_not_found') elif num == 27: data = load_lang("invalid_password_error") elif num == 28: data = load_lang('watchlist_overflow_error') elif num == 29: data = load_lang('copyright_disagreed') elif num == 30: data = load_lang('ie_wrong_callback') elif num == 33: data = load_lang('restart_fail_error') elif num == 34: data = load_lang("update_error") + ' (Github)' elif num == 35: data = load_lang('same_email_error') elif num == 36: data = load_lang('input_email_error') else: data = '???' if num == 5: get_url = flask.request.path return easy_minify(flask.render_template(skin_check(), imp = [(load_lang('skin_set') if get_url != '/main_skin_set' else load_lang('main_skin_set')), wiki_set(1), custom(), other2([0, 0])], data = '' + \ '
' + \ '

' + load_lang('error') + '

' + \ '' + \ '
' + \ ('' if get_url == '/main_skin_set' else ''), menu = ([['main_skin_set', load_lang('main_skin_set')]] if get_url != '/main_skin_set' else [['skin_set', load_lang('skin_set')]]) )) else: return easy_minify(flask.render_template(skin_check(), imp = [load_lang('error'), wiki_set(1), custom(), other2([0, 0])], data = '

' + load_lang('error') + '

  • ' + data + '
', menu = 0 )), 400