func.py 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245
  1. import os
  2. import sys
  3. import platform
  4. for i in range(0, 2):
  5. try:
  6. from diff_match_patch import diff_match_patch
  7. import werkzeug.routing
  8. import werkzeug.debug
  9. import flask_compress
  10. import flask_reggie
  11. import tornado.ioloop
  12. import tornado.httpserver
  13. import tornado.wsgi
  14. import urllib.request
  15. import email.mime.text
  16. import sqlite3
  17. import pymysql
  18. import hashlib
  19. import smtplib
  20. import zipfile
  21. import shutil
  22. import threading
  23. import logging
  24. import random
  25. import flask
  26. import json
  27. import html
  28. import re
  29. if sys.version_info < (3, 6):
  30. import sha3
  31. from .mark import *
  32. except ImportError as e:
  33. if i == 0:
  34. print(e)
  35. print('----')
  36. if platform.system() == 'Linux':
  37. ok = os.system('python3 -m pip install --user -r requirements.txt')
  38. if ok == 0:
  39. print('----')
  40. os.execl(sys.executable, sys.executable, *sys.argv)
  41. else:
  42. raise
  43. elif platform.system() == 'Windows':
  44. ok = os.system('python -m pip install --user -r requirements.txt')
  45. if ok == 0:
  46. print('----')
  47. os.execl(sys.executable, sys.executable, *sys.argv)
  48. else:
  49. raise
  50. else:
  51. print('----')
  52. print(e)
  53. raise
  54. else:
  55. print('----')
  56. print(e)
  57. raise
  58. app_var = json.loads(open('data/app_var.json', encoding='utf8').read())
  59. def load_conn(data):
  60. global conn
  61. global curs
  62. conn = data
  63. curs = conn.cursor()
  64. load_conn2(data)
  65. def send_email(who, title, data):
  66. try:
  67. 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"'))
  68. rep_data = curs.fetchall()
  69. smtp_email = ''
  70. smtp_pass = ''
  71. smtp_server = ''
  72. smtp_security = ''
  73. smtp_port = ''
  74. smtp = ''
  75. if rep_data:
  76. smtp_email = ''
  77. smtp_pass = ''
  78. for i in rep_data:
  79. if i[0] == 'smtp_email':
  80. smtp_email = i[1]
  81. elif i[0] == 'smtp_pass':
  82. smtp_pass = i[1]
  83. elif i[0] == 'smtp_server':
  84. smtp_server = i[1]
  85. elif i[0] == 'smtp_security':
  86. smtp_security = i[1]
  87. elif i[0] == 'smtp_port':
  88. smtp_port = i[1]
  89. smtp_port = int(smtp_port)
  90. if smtp_security == 'plain':
  91. smtp = smtplib.SMTP(smtp_server, smtp_port)
  92. elif smtp_security == 'starttls':
  93. smtp = smtplib.SMTP(smtp_server, smtp_port)
  94. smtp.starttls()
  95. else: # if smtp_security == 'tls':
  96. smtp = smtplib.SMTP_SSL(smtp_server, smtp_port)
  97. smtp.login(smtp_email, smtp_pass)
  98. msg = email.mime.text.MIMEText(data)
  99. msg['Subject'] = title
  100. smtp.sendmail(smtp_email, who, msg.as_string())
  101. smtp.quit()
  102. except:
  103. print('----')
  104. print('Error : Email send error')
  105. def last_change(data):
  106. json_address = re.sub(r"(((?!\.|\/).)+)\.html$", "set.json", skin_check())
  107. try:
  108. json_data = json.loads(open(json_address, encoding='utf8').read())
  109. except:
  110. json_data = 0
  111. if json_data != 0:
  112. for j_data in json_data:
  113. if "class" in json_data[j_data]:
  114. if "require" in json_data[j_data]:
  115. re_data = re.compile("<((?:" + j_data + ")( (?:(?!>).)*)?)>")
  116. s_data = re_data.findall(data)
  117. for i_data in s_data:
  118. e_data = 0
  119. for j_i_data in json_data[j_data]["require"]:
  120. re_data_2 = re.compile("( |^)" + j_i_data + " *= *[\'\"]" + json_data[j_data]["require"][j_i_data] + "[\'\"]")
  121. if not re_data_2.search(i_data[1]):
  122. re_data_2 = re.compile("( |^)" + j_i_data + "=" + json_data[j_data]["require"][j_i_data] + "(?: |$)")
  123. if not re_data_2.search(i_data[1]):
  124. e_data = 1
  125. break
  126. if e_data == 0:
  127. re_data_3 = re.compile("<" + i_data[0] + ">")
  128. data = re_data_3.sub("<" + i_data[0] + " class=\"" + json_data[j_data]["class"] + "\">", data)
  129. else:
  130. re_data = re.compile("<(?P<in>" + j_data + "(?: (?:(?!>).)*)?)>")
  131. data = re_data.sub("<\g<in> class=\"" + json_data[j_data]["class"] + "\">", data)
  132. return data
  133. def easy_minify(data, tool = None):
  134. return last_change(data)
  135. def render_set(title = '', data = '', num = 0, s_data = 0, include = None):
  136. if acl_check(title, 'render') == 1:
  137. return 'HTTP Request 401.3'
  138. elif s_data == 1:
  139. return data
  140. else:
  141. if data != None:
  142. return render_do(title, data, num, include)
  143. else:
  144. return 'HTTP Request 404'
  145. def update(ver_num, set_data):
  146. print('----')
  147. # 업데이트 하위 호환 유지 함수
  148. if ver_num < 3160027:
  149. print('Add init set')
  150. set_init()
  151. if ver_num < 3170002:
  152. curs.execute(db_change("select html from html_filter where kind = 'extension'"))
  153. if not curs.fetchall():
  154. for i in ['jpg', 'jpeg', 'png', 'gif', 'webp']:
  155. curs.execute(db_change("insert into html_filter (html, kind) values (?, 'extension')"), [i])
  156. if ver_num < 3170400:
  157. curs.execute(db_change("select title, sub, code from topic where id = '1'"))
  158. change_topic = curs.fetchall()
  159. for i in change_topic:
  160. curs.execute(db_change("update topic set code = ? where title = ? and sub = ?"), [i[2], i[0], i[1]])
  161. curs.execute(db_change("update rd set code = ? where title = ? and sub = ?"), [i[2], i[0], i[1]])
  162. if ver_num < 3171800:
  163. curs.execute(db_change("select data from other where name = 'recaptcha'"))
  164. change_rec = curs.fetchall()
  165. if change_rec and change_rec[0][0] != '':
  166. new_rec = re.search(r'data-sitekey="([^"]+)"', change_rec[0][0])
  167. if new_rec:
  168. curs.execute(db_change("update other set data = ? where name = 'recaptcha'"), [new_rec.group(1)])
  169. else:
  170. curs.execute(db_change("update other set data = '' where name = 'recaptcha'"))
  171. curs.execute(db_change("update other set data = '' where name = 'sec_re'"))
  172. if ver_num < 3172800 and set_data['db_type'] == 'mysql':
  173. get_data_mysql = json.loads(open('data/mysql.json').read())
  174. with open('data/mysql.json', 'w') as f:
  175. f.write('{ "user" : "' + get_data_mysql['user'] + '", "password" : "' + get_data_mysql['password'] + '", "host" : "localhost" }')
  176. if ver_num < 3182000:
  177. curs.execute(db_change('delete from cache_data'))
  178. conn.commit()
  179. print('Update pass')
  180. def set_init():
  181. # 초기값 설정 함수
  182. curs.execute(db_change("select html from html_filter where kind = 'email'"))
  183. if not curs.fetchall():
  184. for i in ['naver.com', 'gmail.com', 'daum.net', 'kakao.com']:
  185. curs.execute(db_change("insert into html_filter (html, kind) values (?, 'email')"), [i])
  186. curs.execute(db_change("select html from html_filter where kind = 'extension'"))
  187. if not curs.fetchall():
  188. for i in ['jpg', 'jpeg', 'png', 'gif', 'webp']:
  189. curs.execute(db_change("insert into html_filter (html, kind) values (?, 'extension')"), [i])
  190. curs.execute(db_change('select data from other where name = "smtp_server" or name = "smtp_port" or name = "smtp_security"'))
  191. if not curs.fetchall():
  192. for i in [['smtp_server', 'imap.google.com'], ['smtp_port', '587'], ['smtp_security', 'tls']]:
  193. curs.execute(db_change("insert into other (name, data) values (?, ?)"), [i[0], i[1]])
  194. def pw_encode(data, data2 = '', type_d = ''):
  195. if type_d == '':
  196. curs.execute(db_change('select data from other where name = "encode"'))
  197. set_data = curs.fetchall()
  198. type_d = set_data[0][0]
  199. if type_d == 'sha256':
  200. return hashlib.sha256(bytes(data, 'utf-8')).hexdigest()
  201. else:
  202. if sys.version_info < (3, 6):
  203. return sha3.sha3_256(bytes(data, 'utf-8')).hexdigest()
  204. else:
  205. return hashlib.sha3_256(bytes(data, 'utf-8')).hexdigest()
  206. def pw_check(data, data2, type_d = 'no', id_d = ''):
  207. curs.execute(db_change('select data from other where name = "encode"'))
  208. db_data = curs.fetchall()
  209. if type_d != 'no':
  210. if type_d == '':
  211. set_data = 'sha3'
  212. else:
  213. set_data = type_d
  214. else:
  215. set_data = db_data[0][0]
  216. if pw_encode(data = data, type_d = set_data) == data2:
  217. re_data = 1
  218. else:
  219. re_data = 0
  220. if db_data[0][0] != set_data and re_data == 1 and id_d != '':
  221. curs.execute(db_change("update user set pw = ?, encode = ? where id = ?"), [pw_encode(data), db_data[0][0], id_d])
  222. return re_data
  223. def captcha_get():
  224. data = ''
  225. if ip_or_user() != 0:
  226. curs.execute(db_change('select data from other where name = "recaptcha"'))
  227. recaptcha = curs.fetchall()
  228. if recaptcha and recaptcha[0][0] != '':
  229. curs.execute(db_change('select data from other where name = "sec_re"'))
  230. sec_re = curs.fetchall()
  231. if sec_re and sec_re[0][0] != '':
  232. curs.execute(db_change('select data from other where name = "recaptcha_ver"'))
  233. rec_ver = curs.fetchall()
  234. if not rec_ver or rec_ver[0][0] == '':
  235. data += '' + \
  236. '<script src="https://www.google.com/recaptcha/api.js" async defer></script>' + \
  237. '<div class="g-recaptcha" data-sitekey="' + recaptcha[0][0] + '"></div>' + \
  238. '<hr class=\"main_hr\">' + \
  239. ''
  240. else:
  241. data += '' + \
  242. '<script src="https://www.google.com/recaptcha/api.js?render=' + recaptcha[0][0] + '"></script>' + \
  243. '<input type="hidden" id="g-recaptcha" name="g-recaptcha">' + \
  244. '<script type="text/javascript">' + \
  245. 'grecaptcha.ready(function() {' + \
  246. 'grecaptcha.execute(\'' + recaptcha[0][0] + '\', {action: \'homepage\'}).then(function(token) {' + \
  247. 'document.getElementById(\'g-recaptcha\').value = token;' + \
  248. '});' + \
  249. '});' + \
  250. '</script>' + \
  251. ''
  252. return data
  253. def captcha_post(re_data, num = 1):
  254. if num == 1:
  255. curs.execute(db_change('select data from other where name = "sec_re"'))
  256. sec_re = curs.fetchall()
  257. if sec_re and sec_re[0][0] != '' and ip_or_user() != 0 and captcha_get() != '':
  258. try:
  259. data = urllib.request.urlopen('https://www.google.com/recaptcha/api/siteverify?secret=' + sec_re[0][0] + '&response=' + re_data)
  260. except:
  261. data = None
  262. if data and data.getcode() == 200:
  263. json_data = json.loads(data.read().decode(data.headers.get_content_charset()))
  264. if json_data['success'] == True:
  265. return 0
  266. else:
  267. return 1
  268. else:
  269. return 0
  270. else:
  271. return 0
  272. else:
  273. pass
  274. def load_lang(data, num = 2, safe = 0):
  275. if num == 1:
  276. curs.execute(db_change("select data from other where name = 'language'"))
  277. rep_data = curs.fetchall()
  278. json_data = open(os.path.join('language', rep_data[0][0] + '.json'), encoding='utf8').read()
  279. lang = json.loads(json_data)
  280. if data in lang:
  281. if safe == 1:
  282. return lang[data]
  283. else:
  284. return html.escape(lang[data])
  285. else:
  286. return html.escape(data + ' (M)')
  287. else:
  288. curs.execute(db_change('select data from user_set where name = "lang" and id = ?'), [ip_check()])
  289. rep_data = curs.fetchall()
  290. if rep_data:
  291. try:
  292. json_data = open(os.path.join('language', rep_data[0][0] + '.json'), encoding='utf8').read()
  293. lang = json.loads(json_data)
  294. except:
  295. return load_lang(data, 1, safe)
  296. if data in lang:
  297. if safe == 1:
  298. return lang[data]
  299. else:
  300. return html.escape(lang[data])
  301. else:
  302. return load_lang(data, 1, safe)
  303. else:
  304. return load_lang(data, 1, safe)
  305. def load_oauth(provider):
  306. oauth_supported = ["discord", "facebook", "naver", "kakao"]
  307. if(provider == '_README'):
  308. return { "support" : oauth_supported }
  309. else:
  310. try:
  311. oauth = json.loads(open(app_var['path_oauth_setting'], encoding='utf8').read())
  312. except:
  313. return_json_data = '{ "publish_url" : "", '
  314. for i in range(len(oauth_supported)):
  315. return_json_data += '"' + oauth_supported[i] + '" : { '
  316. for j in range(2):
  317. if j == 0:
  318. load_target = 'id'
  319. elif j == 1:
  320. load_target = 'secret'
  321. return_json_data += '"client_' + load_target + '" : ""' + (',' if j == 0 else '')
  322. return_json_data += ' }'
  323. try:
  324. _ = oauth_supported[i + 1]
  325. return_json_data += ', '
  326. except:
  327. return_json_data += ' }'
  328. with open(app_var['path_oauth_setting'], 'w', encoding='utf-8') as f:
  329. f.write(return_json_data)
  330. oauth = json.loads(open(app_var['path_oauth_setting'], encoding='utf8').read())
  331. return oauth[provider]
  332. def update_oauth(provider, target, content):
  333. oauth = json.loads(open(app_var['path_oauth_setting'], encoding='utf8').read())
  334. oauth[provider][target] = content
  335. with open(app_var['path_oauth_setting'], 'w', encoding='utf8') as f:
  336. f.write(json.dumps(oauth, sort_keys = True, indent = 4))
  337. return 'Done'
  338. def ip_or_user(data = ''):
  339. if data == '':
  340. data = ip_check()
  341. if re.search(r'(\.|:)', data):
  342. return 1
  343. else:
  344. return 0
  345. def edit_button():
  346. insert_list = []
  347. curs.execute(db_change("select html, plus from html_filter where kind = 'edit_top'"))
  348. db_data = curs.fetchall()
  349. for get_data in db_data:
  350. insert_list += [[get_data[1], get_data[0]]]
  351. data = ''
  352. for insert_data in insert_list:
  353. data += '<a href="javascript:do_insert_data(\'content\', \'' + insert_data[0] + '\')">(' + insert_data[1] + ')</a> '
  354. if admin_check() == 1:
  355. data += (' ' if data != '' else '') + '<a href="/edit_top">(' + load_lang('add') + ')</a>'
  356. return data + '<hr class=\"main_hr\">'
  357. def ip_warring():
  358. if ip_or_user() != 0:
  359. curs.execute(db_change('select data from other where name = "no_login_warring"'))
  360. data = curs.fetchall()
  361. if data and data[0][0] != '':
  362. text_data = '<span>' + data[0][0] + '</span><hr class=\"main_hr\">'
  363. else:
  364. text_data = '<span>' + load_lang('no_login_warring') + '</span><hr class=\"main_hr\">'
  365. else:
  366. text_data = ''
  367. return text_data
  368. def skin_check(set_n = 0):
  369. skin = 'marisa'
  370. curs.execute(db_change('select data from other where name = "skin"'))
  371. skin_exist = curs.fetchall()
  372. if skin_exist and skin_exist[0][0] != '':
  373. if os.path.exists(os.path.abspath('./views/' + skin_exist[0][0] + '/index.html')) == 1:
  374. skin = skin_exist[0][0]
  375. curs.execute(db_change('select data from user_set where name = "skin" and id = ?'), [ip_check()])
  376. skin_exist = curs.fetchall()
  377. if skin_exist and skin_exist[0][0] != '':
  378. if os.path.exists(os.path.abspath('./views/' + skin_exist[0][0] + '/index.html')) == 1:
  379. skin = skin_exist[0][0]
  380. if set_n == 0:
  381. return './views/' + skin + '/index.html'
  382. else:
  383. return skin
  384. def next_fix(link, num, page, end = 50):
  385. list_data = ''
  386. if num == 1:
  387. if len(page) == end:
  388. list_data += '<hr class=\"main_hr\"><a href="' + link + str(num + 1) + '">(' + load_lang('next') + ')</a>'
  389. elif len(page) != end:
  390. list_data += '<hr class=\"main_hr\"><a href="' + link + str(num - 1) + '">(' + load_lang('previous') + ')</a>'
  391. else:
  392. list_data += '<hr class=\"main_hr\"><a href="' + link + str(num - 1) + '">(' + load_lang('previous') + ')</a> <a href="' + link + str(num + 1) + '">(' + load_lang('next') + ')</a>'
  393. return list_data
  394. def other2(data):
  395. for _ in range(0, 3 - len(data)):
  396. data += ['']
  397. req_list = ''
  398. main_css_ver = 35
  399. if not 'main_css_load' in flask.session or not 'main_css_ver' in flask.session or flask.session['main_css_ver'] != main_css_ver:
  400. for i_data in os.listdir(os.path.join("views", "main_css", "css")):
  401. req_list += '<link rel="stylesheet" href="/views/main_css/css/' + i_data + '?ver=' + str(main_css_ver) + '">'
  402. for i_data in os.listdir(os.path.join("views", "main_css", "js")):
  403. req_list += '<script src="/views/main_css/js/' + i_data + '?ver=' + str(main_css_ver) + '"></script>'
  404. flask.session['main_css_load'] = req_list
  405. flask.session['main_css_ver'] = main_css_ver
  406. else:
  407. req_list = flask.session['main_css_load']
  408. data = data[0:2] + ['', '''
  409. <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.18.1/styles/default.min.css">
  410. <link rel="stylesheet"
  411. href="https://cdn.jsdelivr.net/npm/katex@0.10.1/dist/katex.min.css"
  412. integrity="sha384-dbVIfZGuN1Yq7/1Ocstc1lUEm+AT+/rCkibIcC/OmWo5f0EA48Vf8CytHzGrSwbQ"
  413. crossorigin="anonymous">
  414. <script src="https://cdn.jsdelivr.net/npm/katex@0.10.1/dist/katex.min.js"
  415. integrity="sha384-2BKqo+exmr9su6dir+qCw08N2ZKRucY4PrGQPPWU1A7FtlCGjmEGFqXCv5nyM5Ij"
  416. crossorigin="anonymous"></script>
  417. <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.18.1/highlight.min.js"></script>
  418. ''' + req_list + '<script>main_css_skin_load();</script>'] + data[2:]
  419. return data
  420. def cut_100(data):
  421. if re.search(r'^\/w\/', flask.request.path):
  422. data = re.sub(r'<script>((\n*(((?!<\/script>).)+)\n*)+)<\/script>', '', data)
  423. data = re.sub(r'<hr class="main_hr">((\n*((.+)\n*))+)$', '', data)
  424. data = re.sub(r'<div id="cate_all">((\n*((.+)\n*))+)$', '', data)
  425. data = re.sub(r'<(((?!>).)*)>', ' ', data)
  426. data = re.sub(r'\n', ' ', data)
  427. data = re.sub(r'^ +', '', data)
  428. data = re.sub(r' +$', '', data)
  429. data = re.sub(r' {2,}', ' ', data)
  430. return data[0:100] + '...'
  431. else:
  432. return ''
  433. def wiki_set(num = 1):
  434. if num == 1:
  435. data_list = []
  436. curs.execute(db_change('select data from other where name = ?'), ['name'])
  437. db_data = curs.fetchall()
  438. if db_data and db_data[0][0] != '':
  439. data_list += [db_data[0][0]]
  440. else:
  441. data_list += ['Wiki']
  442. curs.execute(db_change('select data from other where name = "license"'))
  443. db_data = curs.fetchall()
  444. if db_data and db_data[0][0] != '':
  445. data_list += [db_data[0][0]]
  446. else:
  447. data_list += ['ARR']
  448. data_list += ['', '']
  449. curs.execute(db_change('select data from other where name = "logo"'))
  450. db_data = curs.fetchall()
  451. if db_data and db_data[0][0] != '':
  452. data_list += [db_data[0][0]]
  453. else:
  454. data_list += [data_list[0]]
  455. head_data = ''
  456. curs.execute(db_change("select data from other where name = 'head' and coverage = ''"))
  457. db_data = curs.fetchall()
  458. if db_data and db_data[0][0] != '':
  459. head_data += db_data[0][0]
  460. curs.execute(db_change("select data from other where name = 'head' and coverage = ?"), [skin_check(1)])
  461. db_data = curs.fetchall()
  462. if db_data and db_data[0][0] != '':
  463. head_data += db_data[0][0]
  464. data_list += [head_data]
  465. return data_list
  466. if num == 2:
  467. var_data = 'FrontPage'
  468. curs.execute(db_change('select data from other where name = "frontpage"'))
  469. elif num == 3:
  470. var_data = '2'
  471. curs.execute(db_change('select data from other where name = "upload"'))
  472. db_data = curs.fetchall()
  473. if db_data and db_data[0][0] != '':
  474. return db_data[0][0]
  475. else:
  476. return var_data
  477. def admin_check(num = None, what = None, name = ''):
  478. if name == '':
  479. ip = ip_check()
  480. else:
  481. ip = name
  482. curs.execute(db_change("select acl from user where id = ?"), [ip])
  483. user = curs.fetchall()
  484. if user:
  485. reset = 0
  486. back_num = num
  487. while 1:
  488. if num == 1:
  489. check = 'ban'
  490. elif num == 2:
  491. check = 'nothing'
  492. elif num == 3:
  493. check = 'toron'
  494. elif num == 4:
  495. check = 'check'
  496. elif num == 5:
  497. check = 'acl'
  498. elif num == 6:
  499. check = 'hidel'
  500. elif num == 7:
  501. check = 'give'
  502. else:
  503. check = 'owner'
  504. curs.execute(db_change('select name from alist where name = ? and acl = ?'), [user[0][0], check])
  505. if curs.fetchall():
  506. if what:
  507. curs.execute(db_change("insert into re_admin (who, what, time) values (?, ?, ?)"), [ip, what, get_time()])
  508. conn.commit()
  509. return 1
  510. else:
  511. if back_num == 'all':
  512. if num == 'all':
  513. num = 1
  514. elif num != 8:
  515. num += 1
  516. else:
  517. break
  518. elif num:
  519. num = None
  520. else:
  521. break
  522. return 0
  523. def ip_pas(raw_ip, type_d = 0):
  524. hide = 0
  525. if ip_or_user(raw_ip) != 0:
  526. curs.execute(db_change("select data from other where name = 'ip_view'"))
  527. data = curs.fetchall()
  528. if data and data[0][0] != '':
  529. if re.search(r'\.', raw_ip):
  530. ip = re.sub(r'\.([^.]*)\.([^.]*)$', '.*.*', raw_ip)
  531. else:
  532. ip = re.sub(r':([^:]*):([^:]*)$', ':*:*', raw_ip)
  533. if not admin_check(1):
  534. hide = 1
  535. else:
  536. ip = raw_ip
  537. else:
  538. if type_d == 0:
  539. curs.execute(db_change("select title from data where title = ?"), ['user:' + raw_ip])
  540. if curs.fetchall():
  541. ip = '<a href="/w/' + url_pas('user:' + raw_ip) + '">' + raw_ip + '</a>'
  542. else:
  543. ip = '<a id="not_thing" href="/w/' + url_pas('user:' + raw_ip) + '">' + raw_ip + '</a>'
  544. if admin_check('all', None, raw_ip) == 1:
  545. ip = '<b>' + ip + '</b>'
  546. else:
  547. ip = raw_ip
  548. if type_d == 0:
  549. if ban_check(raw_ip) == 1:
  550. ip = '<s>' + ip + '</s>'
  551. if hide == 0:
  552. ip += ' <a href="/tool/' + url_pas(raw_ip) + '">(' + load_lang('tool') + ')</a>'
  553. return ip
  554. def custom():
  555. if 'head' in flask.session:
  556. if len(re.findall('<', flask.session['head'])) % 2 != 1:
  557. user_head = flask.session['head']
  558. else:
  559. user_head = ''
  560. else:
  561. user_head = ''
  562. ip = ip_check()
  563. if ip_or_user(ip) == 0:
  564. user_icon = 1
  565. user_name = ip
  566. curs.execute(db_change('select data from user_set where name = "email" and id = ?'), [ip])
  567. data = curs.fetchall()
  568. if data:
  569. email = data[0][0]
  570. else:
  571. email = ''
  572. if admin_check('all') == 1:
  573. user_admin = '1'
  574. user_acl_list = []
  575. curs.execute(db_change("select acl from user where id = ?"), [ip])
  576. curs.execute(db_change('select acl from alist where name = ?'), [curs.fetchall()[0][0]])
  577. user_acl = curs.fetchall()
  578. for i in user_acl:
  579. user_acl_list += [i[0]]
  580. if user_acl != []:
  581. user_acl_list = user_acl_list
  582. else:
  583. user_acl_list = '0'
  584. else:
  585. user_admin = '0'
  586. user_acl_list = '0'
  587. curs.execute(db_change("select count(name) from alarm where name = ?"), [ip])
  588. count = curs.fetchall()
  589. if count:
  590. user_notice = str(count[0][0])
  591. else:
  592. user_notice = '0'
  593. else:
  594. user_icon = 0
  595. user_name = load_lang('user')
  596. email = ''
  597. user_admin = '0'
  598. user_acl_list = '0'
  599. user_notice = '0'
  600. curs.execute(db_change("select title from rd where title = ? and stop = ''"), ['user:' + ip])
  601. if curs.fetchall():
  602. user_topic = '1'
  603. else:
  604. user_topic = '0'
  605. return [
  606. '',
  607. '',
  608. user_icon,
  609. user_head,
  610. email,
  611. user_name,
  612. user_admin,
  613. str(ban_check()),
  614. user_notice,
  615. user_acl_list,
  616. ip,
  617. user_topic
  618. ]
  619. def load_skin(data = '', set_n = 0):
  620. skin_return_data = ''
  621. system_file = ['main_css']
  622. if data == '':
  623. ip = ip_check()
  624. curs.execute(db_change('select data from user_set where name = "skin" and id = ?'), [ip])
  625. data = curs.fetchall()
  626. if not data:
  627. curs.execute(db_change('select data from other where name = "skin"'))
  628. data = curs.fetchall()
  629. if not data or data[0][0] == '':
  630. data = [['marisa']]
  631. if set_n == 0:
  632. for skin_data in os.listdir(os.path.abspath('views')):
  633. if not skin_data in system_file:
  634. if data[0][0] == skin_data:
  635. skin_return_data = '<option value="' + skin_data + '">' + skin_data + '</option>' + skin_return_data
  636. else:
  637. skin_return_data += '<option value="' + skin_data + '">' + skin_data + '</option>'
  638. else:
  639. skin_return_data = []
  640. for skin_data in os.listdir(os.path.abspath('views')):
  641. if not skin_data in system_file:
  642. if data[0][0] == skin_data:
  643. skin_return_data = [skin_data] + skin_return_data
  644. else:
  645. skin_return_data += [skin_data]
  646. else:
  647. if set_n == 0:
  648. for skin_data in os.listdir(os.path.abspath('views')):
  649. if not skin_data in system_file:
  650. if data == skin_data:
  651. skin_return_data = '<option value="' + skin_data + '">' + skin_data + '</option>' + skin_return_data
  652. else:
  653. skin_return_data += '<option value="' + skin_data + '">' + skin_data + '</option>'
  654. else:
  655. skin_return_data = []
  656. for skin_data in os.listdir(os.path.abspath('views')):
  657. if not skin_data in system_file:
  658. if data == skin_data:
  659. skin_return_data = [skin_data] + skin_return_data
  660. else:
  661. skin_return_data += [skin_data]
  662. return skin_return_data
  663. def slow_edit_check():
  664. curs.execute(db_change("select data from other where name = 'slow_edit'"))
  665. slow_edit = curs.fetchall()
  666. if slow_edit and slow_edit != '0' and admin_check(5) != 1:
  667. slow_edit = slow_edit[0][0]
  668. curs.execute(db_change("select date from history where ip = ? order by date desc limit 1"), [ip_check()])
  669. last_edit_data = curs.fetchall()
  670. if last_edit_data:
  671. last_edit_data = int(re.sub(' |:|-', '', last_edit_data[0][0]))
  672. now_edit_data = int((datetime.datetime.now() - datetime.timedelta(seconds = int(slow_edit))).strftime("%Y%m%d%H%M%S"))
  673. if last_edit_data > now_edit_data:
  674. return 1
  675. return 0
  676. def acl_check(name = 'test', tool = '', topic_num = '1'):
  677. ip = ip_check()
  678. get_ban = ban_check()
  679. if name:
  680. acl_c = re.search(r"^user:((?:(?!\/).)*)", name)
  681. else:
  682. acl_c = None
  683. if tool == '' and acl_c:
  684. acl_n = acl_c.groups()
  685. if get_ban == 1:
  686. return 1
  687. if admin_check(5) == 1:
  688. return 0
  689. curs.execute(db_change("select decu from acl where title = ?"), ['user:' + acl_n[0]])
  690. acl_data = curs.fetchall()
  691. if acl_data:
  692. if acl_data[0][0] == 'all':
  693. return 0
  694. elif acl_data[0][0] == 'user' and not ip_or_user(ip) == 1:
  695. return 0
  696. elif ip == acl_n[0] and not ip_or_user(ip) == 1:
  697. return 0
  698. else:
  699. if ip == acl_n[0] and not ip_or_user(ip) == 1 and not ip_or_user(acl_n[0]) == 1:
  700. return 0
  701. return 1
  702. if tool == '' or tool == 'edit_req':
  703. if acl_check(name, 'render') == 1:
  704. return 1
  705. if tool == '':
  706. end = 3
  707. elif tool == 'topic':
  708. if not name:
  709. curs.execute(db_change("select title from rd where code = ?"), [topic_num])
  710. topic_data = curs.fetchall()
  711. if topic_data:
  712. name = topic_data[0][0]
  713. else:
  714. name = 'test'
  715. end = 3
  716. elif tool == 'render':
  717. end = 2
  718. else:
  719. end = 1
  720. for i in range(0, end):
  721. if tool == '':
  722. if i == 0:
  723. curs.execute(db_change("select decu from acl where title = ?"), [name])
  724. elif i == 1:
  725. curs.execute(db_change('select data from other where name = "edit"'))
  726. else:
  727. curs.execute(db_change("select view from acl where title = ?"), [name])
  728. num = 5
  729. elif tool == 'topic':
  730. if i == 0 and topic_num:
  731. curs.execute(db_change("select acl from rd where code = ?"), [topic_num])
  732. elif i == 1:
  733. curs.execute(db_change("select dis from acl where title = ?"), [name])
  734. else:
  735. curs.execute(db_change('select data from other where name = "discussion"'))
  736. num = 3
  737. elif tool == 'upload':
  738. curs.execute(db_change("select data from other where name = 'upload_acl'"))
  739. num = 5
  740. elif tool == 'many_upload':
  741. curs.execute(db_change("select data from other where name = 'many_upload_acl'"))
  742. num = 5
  743. elif tool == 'edit_req':
  744. curs.execute(db_change("select data from other where name = 'edit_req_acl'"))
  745. num = 5
  746. else:
  747. if i == 0:
  748. curs.execute(db_change("select view from acl where title = ?"), [name])
  749. if i == 1:
  750. curs.execute(db_change("select data from other where name = 'all_view_acl'"))
  751. num = 5
  752. acl_data = curs.fetchall()
  753. if (not acl_data and i == (end - 1)) and get_ban == 1 and tool != 'render':
  754. return 1
  755. elif acl_data and acl_data[0][0] != 'normal' and acl_data[0][0] != '':
  756. if acl_data[0][0] != 'ban' and get_ban == 1 and tool != 'render':
  757. return 1
  758. if acl_data[0][0] == 'all' or acl_data[0][0] == 'ban':
  759. return 0
  760. elif acl_data[0][0] == 'user':
  761. if ip_or_user(ip) != 1:
  762. return 0
  763. elif acl_data[0][0] == 'admin':
  764. if ip_or_user(ip) != 1:
  765. if admin_check(num) == 1:
  766. return 0
  767. elif acl_data[0][0] == '50_edit':
  768. if ip_or_user(ip) != 1:
  769. if admin_check(num) == 1:
  770. return 0
  771. else:
  772. curs.execute(db_change("select count(title) from history where ip = ?"), [ip])
  773. count = curs.fetchall()
  774. count = count[0][0] if count else 0
  775. if count >= 50:
  776. return 0
  777. elif acl_data[0][0] == 'email':
  778. if ip_or_user(ip) != 1:
  779. if admin_check(num) == 1:
  780. return 0
  781. else:
  782. curs.execute(db_change("select data from user_set where id = ? and name = 'email'"), [ip])
  783. if curs.fetchall():
  784. return 0
  785. elif acl_data[0][0] == 'owner':
  786. if admin_check() == 1:
  787. return 0
  788. return 1
  789. else:
  790. if i == (end - 1):
  791. if tool == 'topic':
  792. if topic_num:
  793. curs.execute(db_change("select title from rd where code = ? and stop != ''"), [topic_num])
  794. if curs.fetchall():
  795. if admin_check(3, 'topic (code ' + topic_num + ')') == 1:
  796. return 0
  797. else:
  798. return 0
  799. else:
  800. return 0
  801. else:
  802. return 0
  803. return 1
  804. def ban_check(ip = None, tool = None):
  805. if not ip:
  806. ip = ip_check()
  807. if admin_check(None, None, ip) == 1:
  808. return 0
  809. band = re.search(r"^([0-9]{1,3}\.[0-9]{1,3})", ip)
  810. if band:
  811. band_it = band.group(1)
  812. else:
  813. band_it = '-'
  814. curs.execute(db_change("delete from ban where (end < ? and end like '2%')"), [get_time()])
  815. conn.commit()
  816. curs.execute(db_change("select login, block from ban where ((end > ? and end like '2%') or end = '') and band = 'regex'"), [get_time()])
  817. regex_d = curs.fetchall()
  818. for test_r in regex_d:
  819. g_regex = re.compile(test_r[1])
  820. if g_regex.search(ip):
  821. if tool and tool == 'login':
  822. if test_r[0] != 'O':
  823. return 1
  824. else:
  825. return 1
  826. curs.execute(db_change("select login from ban where ((end > ? and end like '2%') or end = '') and block = ? and band = 'O'"), [get_time(), band_it])
  827. band_d = curs.fetchall()
  828. if band_d:
  829. if tool and tool == 'login':
  830. if band_d[0][0] != 'O':
  831. return 1
  832. else:
  833. return 1
  834. curs.execute(db_change("select login from ban where ((end > ? and end like '2%') or end = '') and block = ? and band = ''"), [get_time(), ip])
  835. ban_d = curs.fetchall()
  836. if ban_d:
  837. if tool and tool == 'login':
  838. if ban_d[0][0] != 'O':
  839. return 1
  840. else:
  841. return 1
  842. return 0
  843. def ban_insert(name, end, why, login, blocker, type_d = None):
  844. now_time = get_time()
  845. if type_d:
  846. band = type_d
  847. else:
  848. if re.search(r"^([0-9]{1,3}\.[0-9]{1,3})$", name):
  849. band = 'O'
  850. else:
  851. band = ''
  852. curs.execute(db_change("delete from ban where (end < ? and end like '2%')"), [get_time()])
  853. curs.execute(db_change("select block from ban where ((end > ? and end like '2%') or end = '') and block = ? and band = ?"), [get_time(), name, band])
  854. if curs.fetchall():
  855. curs.execute(db_change("insert into rb (block, end, today, blocker, why, band) values (?, ?, ?, ?, ?, ?)"), [
  856. name,
  857. 'release',
  858. now_time,
  859. blocker,
  860. '',
  861. band
  862. ])
  863. curs.execute(db_change("delete from ban where block = ? and band = ?"), [name, band])
  864. else:
  865. if login != '':
  866. login = 'O'
  867. else:
  868. login = ''
  869. if end != '0':
  870. end = int(number_check(end))
  871. time = datetime.datetime.now()
  872. plus = datetime.timedelta(seconds = end)
  873. r_time = (time + plus).strftime("%Y-%m-%d %H:%M:%S")
  874. else:
  875. r_time = ''
  876. curs.execute(db_change("insert into rb (block, end, today, blocker, why, band) values (?, ?, ?, ?, ?, ?)"), [
  877. name,
  878. r_time,
  879. now_time,
  880. blocker,
  881. why,
  882. band
  883. ])
  884. curs.execute(db_change("insert into ban (block, end, why, band, login) values (?, ?, ?, ?, ?)"), [
  885. name,
  886. r_time,
  887. why,
  888. band,
  889. login
  890. ])
  891. conn.commit()
  892. def rd_plus(topic_num, date, name = None, sub = None):
  893. curs.execute(db_change("select code from rd where code = ?"), [topic_num])
  894. if curs.fetchall():
  895. curs.execute(db_change("update rd set date = ? where code = ?"), [date, topic_num])
  896. else:
  897. curs.execute(db_change("insert into rd (title, sub, code, date) values (?, ?, ?, ?)"), [name, sub, topic_num, date])
  898. conn.commit()
  899. def history_plus(title, data, date, ip, send, leng, t_check = '', d_type = ''):
  900. curs.execute(db_change("select id from history where title = ? and type = '' order by id + 0 desc limit 1"), [title])
  901. id_data = curs.fetchall()
  902. id_data = str(int(id_data[0][0]) + 1) if id_data else '1'
  903. if d_type != 'req':
  904. curs.execute(db_change("select title from history where title = ? and id = ? and type = 'req'"), [title, id_data])
  905. if curs.fetchall():
  906. curs.execute(db_change("update history set type = 'req_close' where title = ? and id = ? and type = 'req'"), [
  907. title,
  908. id_data
  909. ])
  910. send = re.sub(r'\(|\)|<|>', '', send)
  911. send = send[:128] if len(send) > 128 else send
  912. send = send + ' (' + t_check + ')' if t_check != '' else send
  913. curs.execute(db_change("insert into history (id, title, data, date, ip, send, leng, hide, type) values (?, ?, ?, ?, ?, ?, ?, '', ?)"), [
  914. id_data,
  915. title,
  916. data,
  917. date,
  918. ip,
  919. send,
  920. leng,
  921. d_type
  922. ])
  923. def leng_check(first, second):
  924. if first < second:
  925. all_plus = '+' + str(second - first)
  926. elif second < first:
  927. all_plus = '-' + str(first - second)
  928. else:
  929. all_plus = '0'
  930. return all_plus
  931. def number_check(data):
  932. try:
  933. int(data)
  934. return data
  935. except:
  936. return '1'
  937. def edit_filter_do(data):
  938. if admin_check(1) != 1:
  939. curs.execute(db_change("select regex, sub from filter where regex != ''"))
  940. for data_list in curs.fetchall():
  941. match = re.compile(data_list[0], re.I)
  942. if match.search(data):
  943. ban_insert(
  944. ip_check(),
  945. '0' if data_list[1] == 'X' else data_list[1],
  946. 'edit filter',
  947. None,
  948. 'tool:edit filter'
  949. )
  950. return 1
  951. return 0
  952. def redirect(data = '/'):
  953. return flask.redirect(data)
  954. def get_acl_list(type_d = None):
  955. acl_data = ['', 'all', 'user', 'admin', 'owner', '50_edit', 'email', 'ban']
  956. if type_d:
  957. if type_d == 'user':
  958. acl_data = ['', 'user', 'all']
  959. return acl_data
  960. def re_error(data):
  961. conn.commit()
  962. if data == '/ban':
  963. if ban_check() == 1:
  964. end = '<div id="get_user_info"></div><script>load_user_info("' + ip_check() + '");</script>'
  965. else:
  966. end = load_lang('authority_error')
  967. return easy_minify(flask.render_template(skin_check(),
  968. imp = [load_lang('error'), wiki_set(1), custom(), other2([0, 0])],
  969. data = '<h2>' + load_lang('error') + '</h2>' + end,
  970. menu = 0
  971. ))
  972. else:
  973. num = int(number_check(data.replace('/error/', '')))
  974. if num == 1:
  975. data = load_lang('no_login_error')
  976. elif num == 2:
  977. data = load_lang('no_exist_user_error')
  978. elif num == 3:
  979. data = load_lang('authority_error')
  980. elif num == 4:
  981. data = load_lang('no_admin_block_error')
  982. elif num == 5:
  983. data = load_lang('skin_error')
  984. elif num == 6:
  985. data = load_lang('same_id_exist_error')
  986. elif num == 7:
  987. data = load_lang('long_id_error')
  988. elif num == 8:
  989. data = load_lang('id_char_error') + ' <a href="/name_filter">(' + load_lang('id_filter_list') + ')</a>'
  990. elif num == 9:
  991. data = load_lang('file_exist_error')
  992. elif num == 10:
  993. data = load_lang('password_error')
  994. elif num == 11:
  995. data = load_lang('topic_long_error')
  996. elif num == 12:
  997. data = load_lang('email_error')
  998. elif num == 13:
  999. data = load_lang('recaptcha_error')
  1000. elif num == 14:
  1001. data = load_lang('file_extension_error') + ' <a href="/extension_filter">(' + load_lang('extension_filter_list') + ')</a>'
  1002. elif num == 15:
  1003. data = load_lang('edit_record_error')
  1004. elif num == 16:
  1005. data = load_lang('same_file_error')
  1006. elif num == 17:
  1007. data = load_lang('file_capacity_error') + wiki_set(3)
  1008. elif num == 19:
  1009. data = load_lang('decument_exist_error')
  1010. elif num == 20:
  1011. data = load_lang('password_diffrent_error')
  1012. elif num == 21:
  1013. data = load_lang('edit_filter_error')
  1014. elif num == 22:
  1015. data = load_lang('file_name_error')
  1016. elif num == 23:
  1017. data = load_lang('regex_error')
  1018. elif num == 24:
  1019. curs.execute(db_change("select data from other where name = 'slow_edit'"))
  1020. slow_data = curs.fetchall()
  1021. data = load_lang('fast_edit_error') + slow_data[0][0]
  1022. elif num == 25:
  1023. data = load_lang('too_many_dec_error')
  1024. elif num == 26:
  1025. data = load_lang('application_not_found')
  1026. elif num == 27:
  1027. data = load_lang("invalid_password_error")
  1028. elif num == 28:
  1029. data = load_lang('watchlist_overflow_error')
  1030. elif num == 29:
  1031. data = load_lang('copyright_disagreed')
  1032. else:
  1033. data = '???'
  1034. if num == 5:
  1035. get_url = flask.request.path
  1036. return easy_minify(flask.render_template(skin_check(),
  1037. imp = [(load_lang('skin_set') if get_url != '/main_skin_set' else load_lang('main_skin_set')), wiki_set(1), custom(), other2([0, 0])],
  1038. data = '' + \
  1039. '<div id="main_skin_set">' + \
  1040. '<h2>' + load_lang('error') + '</h2>' + \
  1041. '<ul>' + \
  1042. '<li>' + data + '</li>' + \
  1043. '</ul>' + \
  1044. '</div>' + \
  1045. ('<script>window.addEventListener(\'DOMContentLoaded\', function() { main_css_skin_set(); });</script>' if get_url == '/main_skin_set' else ''),
  1046. menu = ([['main_skin_set', load_lang('main_skin_set')]] if get_url != '/main_skin_set' else [['skin_set', load_lang('skin_set')]])
  1047. ))
  1048. else:
  1049. return easy_minify(flask.render_template(skin_check(),
  1050. imp = [load_lang('error'), wiki_set(1), custom(), other2([0, 0])],
  1051. data = '<h2>' + load_lang('error') + '</h2><ul><li>' + data + '</li></ul>',
  1052. menu = 0
  1053. )), 401