func.py 38 KB

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