func.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. # 모듈들 불러옴
  2. from css_html_js_minify import html_minify, js_minify, css_minify
  3. from flask import session, render_template
  4. from urllib import parse
  5. import json
  6. import sqlite3
  7. import hashlib
  8. import requests
  9. import re
  10. import html
  11. import time
  12. import os
  13. # 일부 툴 불러옴
  14. from set_mark.tool import *
  15. # 나무마크 불러옴
  16. from mark import *
  17. # 서브 언어팩 불러옴
  18. json_data = open(os.path.join('language', 'en-US.json'), 'rt', encoding='utf-8').read()
  19. else_lang = json.loads(json_data)
  20. def load_conn(data):
  21. global conn
  22. global curs
  23. conn = data
  24. curs = conn.cursor()
  25. load_conn2(data)
  26. def captcha_get():
  27. data = ''
  28. if custom()[2] == 0:
  29. curs.execute('select data from other where name = "recaptcha"')
  30. recaptcha = curs.fetchall()
  31. if recaptcha and recaptcha[0][0] != '':
  32. curs.execute('select data from other where name = "sec_re"')
  33. sec_re = curs.fetchall()
  34. if sec_re and sec_re[0][0] != '':
  35. data += recaptcha[0][0] + '<hr>'
  36. return data
  37. def captcha_post(test, num = 1):
  38. if num == 1:
  39. if custom()[2] == 0 and captcha_get() != '':
  40. curs.execute('select data from other where name = "sec_re"')
  41. sec_re = curs.fetchall()
  42. if sec_re and sec_re[0][0] != '':
  43. data = requests.get('https://www.google.com/recaptcha/api/siteverify', params = { 'secret' : sec_re, 'response' : test })
  44. if not data:
  45. return 0
  46. else:
  47. json_data = data.json()
  48. if data.status_code == 200 and json_data['success'] == True:
  49. return 0
  50. else:
  51. return 1
  52. else:
  53. return 0
  54. else:
  55. return 0
  56. else:
  57. pass
  58. def load_lang(data):
  59. global lang
  60. try:
  61. if lang:
  62. pass
  63. except:
  64. curs.execute("select data from other where name = 'language'")
  65. rep_data = curs.fetchall()
  66. json_data = open(os.path.join('language', rep_data[0][0] + '.json'), 'rt', encoding='utf-8').read()
  67. lang = json.loads(json_data)
  68. if data == 'please_all':
  69. return lang
  70. else:
  71. if data in lang:
  72. return lang[data]
  73. else:
  74. return else_lang[data]
  75. def edit_help_button():
  76. # https://stackoverflow.com/questions/11076975/insert-text-into-textarea-at-cursor-position-javascript
  77. '''<script>
  78. function insertAtCursor(myField, myValue) {
  79. if (document.selection) {
  80. document.getElementById(myField).focus();
  81. sel = document.selection.createRange();
  82. sel.text = myValue;
  83. } else if (document.getElementById(myField).selectionStart || document.getElementById(myField).selectionStart == '0') {
  84. var startPos = document.getElementById(myField).selectionStart;
  85. var endPos = document.getElementById(myField).selectionEnd;
  86. document.getElementById(myField).value = document.getElementById(myField).value.substring(0, startPos) + myValue + document.getElementById(myField).value.substring(endPos, document.getElementById(myField).value.length);
  87. } else {
  88. document.getElementById(myField).value += myValue;
  89. }
  90. }
  91. </script>
  92. '''
  93. '<a href="javascript:void(0);" onclick="insertAtCursor(\'content\', \'[[]]\');">(링크)</a> <a href="javascript:void(0);" onclick="insertAtCursor(\'content\', \'[macro()]\');">(매크로)</a> <a href="javascript:void(0);" onclick="insertAtCursor(\'content\', \'{{{#! }}}\');">(중괄호)</a><hr>'
  94. return ['', '']
  95. def ip_warring():
  96. if custom()[2] == 0:
  97. curs.execute('select data from other where name = "no_login_warring"')
  98. data = curs.fetchall()
  99. if data and data[0][0] != '':
  100. text_data = '<span>' + data[0][0] + '</span><hr>'
  101. else:
  102. text_data = '<span>' + load_lang('no_login_warring') + '</span><hr>'
  103. else:
  104. text_data = ''
  105. return text_data
  106. def skin_check():
  107. skin = './views/acme/'
  108. try:
  109. curs.execute('select skin from user where id = ?', [ip_check()])
  110. skin_exist = curs.fetchall()
  111. if skin_exist and skin_exist[0][0] != '':
  112. if os.path.exists(os.path.abspath('./views/' + skin_exist[0][0] + '/index.html')) == 1:
  113. skin = './views/' + skin_exist[0][0] + '/'
  114. else:
  115. curs.execute('select data from other where name = "skin"')
  116. skin_exist = curs.fetchall()
  117. if skin_exist:
  118. if os.path.exists(os.path.abspath('./views/' + skin_exist[0][0] + '/index.html')) == 1:
  119. skin = './views/' + skin_exist[0][0] + '/'
  120. except:
  121. pass
  122. return skin + 'index.html'
  123. def next_fix(link, num, page, end = 50):
  124. list_data = ''
  125. if num == 1:
  126. if len(page) == end:
  127. list_data += '<hr><a href="' + link + str(num + 1) + '">(' + load_lang('next') + ')</a>'
  128. elif len(page) != end:
  129. list_data += '<hr><a href="' + link + str(num - 1) + '">(' + load_lang('previous') + ')</a>'
  130. else:
  131. list_data += '<hr><a href="' + link + str(num - 1) + '">(' + load_lang('previous') + ')</a> <a href="' + link + str(num + 1) + '">(' + load_lang('next') + ')</a>'
  132. return list_data
  133. def other2(origin):
  134. return origin + ['Deleted']
  135. def wiki_set(num):
  136. if num == 1:
  137. data_list = []
  138. curs.execute('select data from other where name = ?', ['name'])
  139. db_data = curs.fetchall()
  140. if db_data and db_data[0][0] != '':
  141. data_list += [db_data[0][0]]
  142. else:
  143. data_list += ['Wiki']
  144. curs.execute('select data from other where name = "license"')
  145. db_data = curs.fetchall()
  146. if db_data and db_data[0][0] != '':
  147. data_list += [db_data[0][0]]
  148. else:
  149. data_list += ['CC 0']
  150. data_list += ['', '']
  151. curs.execute('select data from other where name = "logo"')
  152. db_data = curs.fetchall()
  153. if db_data and db_data[0][0] != '':
  154. data_list += [db_data[0][0]]
  155. else:
  156. data_list += [data_list[0]]
  157. curs.execute("select data from other where name = 'head'")
  158. db_data = curs.fetchall()
  159. if db_data and db_data[0][0] != '':
  160. data_list += [db_data[0][0]]
  161. else:
  162. data_list += ['']
  163. return data_list
  164. if num == 2:
  165. var_data = 'FrontPage'
  166. curs.execute('select data from other where name = "frontpage"')
  167. elif num == 3:
  168. var_data = '2'
  169. curs.execute('select data from other where name = "upload"')
  170. db_data = curs.fetchall()
  171. if db_data and db_data[0][0] != '':
  172. return db_data[0][0]
  173. else:
  174. return var_data
  175. def diff(seqm):
  176. output = []
  177. for opcode, a0, a1, b0, b1 in seqm.get_opcodes():
  178. if opcode == 'equal':
  179. output += [seqm.a[a0:a1]]
  180. elif opcode == 'insert':
  181. output += ["<span style='background:#CFC;'>" + seqm.b[b0:b1] + "</span>"]
  182. elif opcode == 'delete':
  183. output += ["<span style='background:#FDD;'>" + seqm.a[a0:a1] + "</span>"]
  184. elif opcode == 'replace':
  185. output += ["<span style='background:#FDD;'>" + seqm.a[a0:a1] + "</span>"]
  186. output += ["<span style='background:#CFC;'>" + seqm.b[b0:b1] + "</span>"]
  187. return ''.join(output)
  188. def admin_check(num, what):
  189. ip = ip_check()
  190. curs.execute("select acl from user where id = ?", [ip])
  191. user = curs.fetchall()
  192. if user:
  193. reset = 0
  194. while 1:
  195. if num == 1 and reset == 0:
  196. check = 'ban'
  197. elif num == 2 and reset == 0:
  198. check = 'mdel'
  199. elif num == 3 and reset == 0:
  200. check = 'toron'
  201. elif num == 4 and reset == 0:
  202. check = 'check'
  203. elif num == 5 and reset == 0:
  204. check = 'acl'
  205. elif num == 6 and reset == 0:
  206. check = 'hidel'
  207. elif num == 7 and reset == 0:
  208. check = 'give'
  209. else:
  210. check = 'owner'
  211. curs.execute('select name from alist where name = ? and acl = ?', [user[0][0], check])
  212. if curs.fetchall():
  213. if what:
  214. curs.execute("insert into re_admin (who, what, time) values (?, ?, ?)", [ip, what, get_time()])
  215. conn.commit()
  216. return 1
  217. else:
  218. if reset == 0:
  219. reset = 1
  220. else:
  221. break
  222. def ip_pas(raw_ip):
  223. hide = 0
  224. if re.search("(\.|:)", raw_ip):
  225. if not re.search("^" + load_lang('tool') + ":", raw_ip):
  226. curs.execute("select data from other where name = 'ip_view'")
  227. data = curs.fetchall()
  228. if data and data[0][0] != '':
  229. ip = '<span style="font-size: 75%;">' + hashlib.md5(bytes(raw_ip, 'utf-8')).hexdigest() + '</span>'
  230. if not admin_check('ban', None):
  231. hide = 1
  232. else:
  233. ip = raw_ip
  234. else:
  235. ip = raw_ip
  236. hide = 1
  237. else:
  238. curs.execute("select title from data where title = ?", ['' + load_lang('user') + ':' + raw_ip])
  239. if curs.fetchall():
  240. ip = '<a href="/w/' + url_pas('' + load_lang('user') + ':' + raw_ip) + '">' + raw_ip + '</a>'
  241. else:
  242. ip = '<a id="not_thing" href="/w/' + url_pas('' + load_lang('user') + ':' + raw_ip) + '">' + raw_ip + '</a>'
  243. if hide == 0:
  244. ip += ' <a href="/record/' + url_pas(raw_ip) + '">(' + load_lang('record') + ')</a>'
  245. return ip
  246. def custom():
  247. if 'MyMaiToNight' in session:
  248. user_head = session['MyMaiToNight']
  249. else:
  250. user_head = ''
  251. if 'Now' in session and session['Now'] == 1:
  252. curs.execute('select name from alarm where name = ? limit 1', [ip_check()])
  253. if curs.fetchall():
  254. user_icon = 2
  255. else:
  256. user_icon = 1
  257. else:
  258. user_icon = 0
  259. if user_icon != 0:
  260. curs.execute('select email from user where id = ?', [ip_check()])
  261. data = curs.fetchall()
  262. if data:
  263. email = data[0][0]
  264. else:
  265. email = ''
  266. else:
  267. email = ''
  268. if user_icon != 0:
  269. user_name = ip_check()
  270. else:
  271. user_name = '' + load_lang('user') + ''
  272. return ['', '', user_icon, user_head, email, user_name]
  273. def acl_check(name):
  274. ip = ip_check()
  275. if ban_check() == 1:
  276. return 1
  277. acl_c = re.search("^" + load_lang('user') + ":([^/]*)", name)
  278. if acl_c:
  279. acl_n = acl_c.groups()
  280. if admin_check(5, None) == 1:
  281. return 0
  282. curs.execute("select dec from acl where title = ?", ['' + load_lang('user') + ':' + acl_n[0]])
  283. acl_data = curs.fetchall()
  284. if acl_data:
  285. if acl_data[0][0] == 'all':
  286. return 0
  287. if acl_data[0][0] == 'user' and not re.search("(\.|:)", ip):
  288. return 0
  289. if ip != acl_n[0] or re.search("(\.|:)", ip):
  290. return 1
  291. if ip == acl_n[0] and not re.search("(\.|:)", ip) and not re.search("(\.|:)", acl_n[0]):
  292. return 0
  293. else:
  294. return 1
  295. file_c = re.search("^" + load_lang('file') + ":(.*)", name)
  296. if file_c and admin_check(5, 'edit (' + name + ')') != 1:
  297. return 1
  298. curs.execute("select acl from user where id = ?", [ip])
  299. user_data = curs.fetchall()
  300. curs.execute("select dec from acl where title = ?", [name])
  301. acl_data = curs.fetchall()
  302. if acl_data:
  303. if acl_data[0][0] == 'user':
  304. if not user_data:
  305. return 1
  306. if acl_data[0][0] == 'admin':
  307. if not user_data:
  308. return 1
  309. if not admin_check(5, 'edit (' + name + ')') == 1:
  310. return 1
  311. curs.execute('select data from other where name = "edit"')
  312. set_data = curs.fetchall()
  313. if set_data:
  314. if set_data[0][0] == 'user':
  315. if not user_data:
  316. return 1
  317. if set_data[0][0] == 'admin':
  318. if not user_data:
  319. return 1
  320. if not admin_check(5, None) == 1:
  321. return 1
  322. return 0
  323. def ban_check():
  324. ip = ip_check()
  325. band = re.search("^([0-9]{1,3}\.[0-9]{1,3})", ip)
  326. if band:
  327. band_it = band.groups()[0]
  328. else:
  329. band_it = 'Not'
  330. curs.execute("select block from ban where block = ?", [band_it])
  331. band_d = curs.fetchall()
  332. curs.execute("select block from ban where block = ?", [ip])
  333. ban_d = curs.fetchall()
  334. if band_d or ban_d:
  335. return 1
  336. return 0
  337. def topic_check(name, sub):
  338. ip = ip_check()
  339. if ban_check() == 1:
  340. return 1
  341. curs.execute("select acl from user where id = ?", [ip])
  342. user_data = curs.fetchall()
  343. curs.execute("select dis from acl where title = ?", [name])
  344. acl_data = curs.fetchall()
  345. if acl_data:
  346. if acl_data[0][0] == 'user':
  347. if not user_data:
  348. return 1
  349. if acl_data[0][0] == 'admin':
  350. if not user_data:
  351. return 1
  352. if not admin_check(3, 'topic (' + name + ')') == 1:
  353. return 1
  354. curs.execute("select title from stop where title = ? and sub = ?", [name, sub])
  355. if curs.fetchall():
  356. if not admin_check(3, 'topic (' + name + ')') == 1:
  357. return 1
  358. return 0
  359. def ban_insert(name, end, why, login, blocker):
  360. time = get_time()
  361. if re.search("^([0-9]{1,3}\.[0-9]{1,3})$", name):
  362. band = 'O'
  363. else:
  364. band = ''
  365. curs.execute("select block from ban where block = ?", [name])
  366. if curs.fetchall():
  367. curs.execute("insert into rb (block, end, today, blocker, why, band) values (?, ?, ?, ?, ?, ?)", [name, '' + load_lang('release') + '', time, blocker, '', band])
  368. curs.execute("delete from ban where block = ?", [name])
  369. else:
  370. if login != '':
  371. login = 'O'
  372. else:
  373. login = ''
  374. if end != '':
  375. end += ' 00:00:00'
  376. curs.execute("insert into rb (block, end, today, blocker, why, band) values (?, ?, ?, ?, ?, ?)", [name, end, time, blocker, why, band])
  377. curs.execute("insert into ban (block, end, why, band, login) values (?, ?, ?, ?, ?)", [name, end, why, band, login])
  378. conn.commit()
  379. def rd_plus(title, sub, date):
  380. curs.execute("select title from rd where title = ? and sub = ?", [title, sub])
  381. if curs.fetchall():
  382. curs.execute("update rd set date = ? where title = ? and sub = ?", [date, title, sub])
  383. else:
  384. curs.execute("insert into rd (title, sub, date) values (?, ?, ?)", [title, sub, date])
  385. def history_plus(title, data, date, ip, send, leng):
  386. curs.execute("select id from history where title = ? order by id + 0 desc limit 1", [title])
  387. id_data = curs.fetchall()
  388. if id_data:
  389. curs.execute("insert into history (id, title, data, date, ip, send, leng) values (?, ?, ?, ?, ?, ?, ?)", [str(int(id_data[0][0]) + 1), title, data, date, ip, send, leng])
  390. else:
  391. curs.execute("insert into history (id, title, data, date, ip, send, leng) values ('1', ?, ?, ?, ?, ?, ?)", [title, data, date, ip, send + ' (' + load_lang('new') + ' ' + load_lang('document') + ')', leng])
  392. def leng_check(first, second):
  393. if first < second:
  394. all_plus = '+' + str(second - first)
  395. elif second < first:
  396. all_plus = '-' + str(first - second)
  397. else:
  398. all_plus = '0'
  399. return all_plus
  400. def redirect(data):
  401. return '<meta http-equiv="refresh" content="0; url=' + data + '">'
  402. def re_error(data):
  403. if data == '/ban':
  404. ip = ip_check()
  405. end = '<li>Why : 권한이 맞지 않는 상태 입니다.</li>'
  406. if ban_check() == 1:
  407. curs.execute("select end, why from ban where block = ?", [ip])
  408. end_data = curs.fetchall()
  409. if not end_data:
  410. match = re.search("^([0-9]{1,3}\.[0-9]{1,3})", ip)
  411. if match:
  412. curs.execute("select end, why from ban where block = ?", [match.groups()[0]])
  413. end_data = curs.fetchall()
  414. if end_data:
  415. end = '<li>Info : '
  416. if end_data[0][0]:
  417. now = int(re.sub('(\-| |:)', '', get_time()))
  418. day = int(re.sub('(\-| |:)', '', end_data[0][0]))
  419. if now >= day:
  420. curs.execute("delete from ban where block = ?", [ip])
  421. conn.commit()
  422. end += 'Re Try.'
  423. else:
  424. end += 'Ban : ' + end_data[0][0]
  425. else:
  426. end += 'Ban : No End'
  427. end += '</li>'
  428. if end_data[0][1] != '':
  429. end += '<li>Why : ' + end_data[0][1] + '</li>'
  430. return html_minify(render_template(skin_check(),
  431. imp = ['Authority Error', wiki_set(1), custom(), other2([0, 0])],
  432. data = '<h2>Info</h2><ul>' + end + '</ul>',
  433. menu = 0
  434. ))
  435. error_data = re.search('\/error\/([0-9]+)', data)
  436. if error_data:
  437. num = int(error_data.groups()[0])
  438. if num == 1:
  439. title = 'Authority Error'
  440. data = '비 로그인 상태 입니다.'
  441. elif num == 2:
  442. title = 'Authority Error'
  443. data = '이 계정이 없습니다.'
  444. elif num == 3:
  445. title = 'Authority Error'
  446. data = '권한이 모자랍니다.'
  447. elif num == 4:
  448. title = 'Authority Error'
  449. data = '관리자는 차단, 검사 할 수 없습니다.'
  450. elif num == 5:
  451. title = 'User Error'
  452. data = '그런 계정이 없습니다.'
  453. elif num == 6:
  454. title = 'Register Error'
  455. data = '동일한 아이디의 사용자가 있습니다.'
  456. elif num == 7:
  457. title = 'Register Error'
  458. data = '아이디는 20글자보다 짧아야 합니다.'
  459. elif num == 8:
  460. title = 'Register Error'
  461. data = '아이디에는 한글과 알파벳과 공백만 허용 됩니다.'
  462. elif num == 9:
  463. title = 'Upload Error'
  464. data = '파일이 없습니다.'
  465. elif num == 10:
  466. title = 'PassWord Error'
  467. data = '비밀번호가 다릅니다.'
  468. elif num == 11:
  469. title = 'Login Error'
  470. data = '이미 로그인 되어 있습니다.'
  471. elif num == 13:
  472. title = 'reCAPTCHA Error'
  473. data = '리캡차를 통과하세요.'
  474. elif num == 14:
  475. title = 'Upload Error'
  476. data = 'jpg, gif, jpeg, png, webp만 가능 합니다.'
  477. elif num == 15:
  478. title = 'Edit Error'
  479. data = '편집 기록은 500자를 넘을 수 없습니다.'
  480. elif num == 16:
  481. title = 'Upload Error'
  482. data = '동일한 이름의 파일이 있습니다.'
  483. elif num == 17:
  484. title = 'Upload Error'
  485. data = '파일 용량은 ' + wiki_set(3) + 'MB를 넘길 수 없습니다.'
  486. elif num == 18:
  487. title = 'Edit Error'
  488. data = '내용이 원래 문서와 동일 합니다.'
  489. elif num == 19:
  490. title = 'Move Error'
  491. data = '이동 하려는 곳에 문서가 이미 있습니다.'
  492. elif num == 20:
  493. title = 'PassWord Error'
  494. data = '재 확인이랑 비밀번호가 다릅니다.'
  495. elif num == 21:
  496. title = 'Edit Error'
  497. data = '편집 필터에 의해 검열 되었습니다.'
  498. elif num == 22:
  499. title = 'Upload Error'
  500. data = '파일 이름은 알파벳, 한글, 띄어쓰기, 언더바, 빼기표만 허용 됩니다.'
  501. else:
  502. title = 'Error'
  503. data = '???'
  504. if title:
  505. return html_minify(render_template(skin_check(),
  506. imp = [title, wiki_set(1), custom(), other2([0, 0])],
  507. data = '<h2>Error</h2><ul><li>' + data + '</li></ul>',
  508. menu = 0
  509. ))
  510. else:
  511. return redirect('/')
  512. else:
  513. return redirect('/')