2
0

func.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  1. # 모듈들 불러옴
  2. import css_html_js_minify
  3. import flask
  4. import json
  5. import sqlite3
  6. import hashlib
  7. import requests
  8. import re
  9. import html
  10. import os
  11. # 일부 툴 불러옴
  12. from set_mark.tool import *
  13. # 나무마크 불러옴
  14. from mark import *
  15. # 서브 언어팩 불러옴
  16. json_data = open(os.path.join('language', 'en-US.json'), 'rt', encoding='utf-8').read()
  17. else_lang = json.loads(json_data)
  18. def load_conn(data):
  19. global conn
  20. global curs
  21. conn = data
  22. curs = conn.cursor()
  23. load_conn2(data)
  24. def captcha_get():
  25. data = ''
  26. if custom()[2] == 0:
  27. curs.execute('select data from other where name = "recaptcha"')
  28. recaptcha = curs.fetchall()
  29. if recaptcha and recaptcha[0][0] != '':
  30. curs.execute('select data from other where name = "sec_re"')
  31. sec_re = curs.fetchall()
  32. if sec_re and sec_re[0][0] != '':
  33. data += recaptcha[0][0] + '<hr>'
  34. return data
  35. def update():
  36. # 호환성 설정
  37. try:
  38. curs.execute('select title, re from hidhi')
  39. for rep in curs.fetchall():
  40. curs.execute("update history set hide = 'O' where title = ? and id = ?", [rep[0], rep[1]])
  41. curs.execute("drop table if exists hidhi")
  42. print('move table hidhi')
  43. except:
  44. pass
  45. try:
  46. curs.execute("select title, acl from data where acl != ''")
  47. for rep in curs.fetchall():
  48. curs.execute("insert into acl (title, dec, dis, why) values (?, ?, '', '')", [rep[0], rep[1]])
  49. curs.execute("alter table data drop acl")
  50. print('data table delete column acl')
  51. except:
  52. pass
  53. try:
  54. curs.execute('select name, sub from filter where sub != "X" and sub != ""')
  55. filter_name = curs.fetchall()
  56. if filter_name:
  57. for filter_delete in filter_name:
  58. if filter_delete[1] != '' or filter_delete[1] != 'X':
  59. curs.execute("update filter set sub = '' where name = ?", [filter_delete[0]])
  60. print('filter data fix')
  61. except:
  62. pass
  63. # 3.0.5 사용자 문서, 파일 문서, 분류 문서 영어화
  64. try:
  65. all_rep = [['사용자:', 'user:'], ['파일:', 'file:'], ['분류:', 'category:']]
  66. all_rep2 = ['data', 'history', 'acl', 'topic', 'back']
  67. test = 0
  68. for i in range(3):
  69. for j in range(6):
  70. if not j == 5:
  71. curs.execute('select title from ' + all_rep2[j] + ' where title like ?', [all_rep[i][0] + '%'])
  72. else:
  73. curs.execute('select link from back where link like ?', [all_rep[i][0] + '%'])
  74. user_rep = curs.fetchall()
  75. for user_rep2 in user_rep:
  76. test = 1
  77. first = re.sub('^' + all_rep[i][0], all_rep[i][1], user_rep2[0])
  78. if j == 0:
  79. curs.execute("update data set title = ? where title = ?", [first, user_rep2[0]])
  80. elif j == 1:
  81. curs.execute("update history set title = ? where title = ?", [first, user_rep2[0]])
  82. elif j == 2:
  83. curs.execute("update acl set title = ? where title = ?", [first, user_rep2[0]])
  84. elif j == 3:
  85. curs.execute("update topic set title = ? where title = ?", [first, user_rep2[0]])
  86. elif j == 4:
  87. curs.execute("update back set title = ? where title = ?", [first, user_rep2[0]])
  88. elif j == 5:
  89. curs.execute("update back set link = ? where link = ?", [first, user_rep2[0]])
  90. if test == 1:
  91. print('사용자 to user, 파일 to file, 분류 to category')
  92. except:
  93. pass
  94. def captcha_post(re_data, num = 1):
  95. if num == 1:
  96. if custom()[2] == 0 and captcha_get() != '':
  97. curs.execute('select data from other where name = "sec_re"')
  98. sec_re = curs.fetchall()
  99. if sec_re and sec_re[0][0] != '':
  100. data = requests.get('https://www.google.com/recaptcha/api/siteverify', params = { 'secret' : sec_re, 'response' : re_data })
  101. if not data:
  102. return 0
  103. else:
  104. json_data = data.json()
  105. if data.status_code == 200 and json_data['success'] == True:
  106. return 0
  107. else:
  108. return 1
  109. else:
  110. return 0
  111. else:
  112. return 0
  113. else:
  114. pass
  115. def load_lang(data):
  116. global lang
  117. try:
  118. if lang:
  119. pass
  120. except:
  121. curs.execute("select data from other where name = 'language'")
  122. rep_data = curs.fetchall()
  123. json_data = open(os.path.join('language', rep_data[0][0] + '.json'), 'rt', encoding='utf-8').read()
  124. lang = json.loads(json_data)
  125. if data == 'please_all':
  126. return lang
  127. else:
  128. if data in lang:
  129. return lang[data]
  130. else:
  131. if data in else_lang:
  132. return else_lang[data]
  133. else:
  134. return data + ' (Missing)'
  135. def ip_or_user(data):
  136. if re.search('(\.|:)', data):
  137. return 1
  138. else:
  139. return 0
  140. def edit_help_button():
  141. # https://stackoverflow.com/questions/11076975/insert-text-into-textarea-at-cursor-position-javascript
  142. '''
  143. <script>
  144. function insertAtCursor(myField, myValue) {
  145. if (document.selection) {
  146. document.getElementById(myField).focus();
  147. sel = document.selection.createRange();
  148. sel.text = myValue;
  149. } else if (document.getElementById(myField).selectionStart || document.getElementById(myField).selectionStart == '0') {
  150. var startPos = document.getElementById(myField).selectionStart;
  151. var endPos = document.getElementById(myField).selectionEnd;
  152. document.getElementById(myField).value = document.getElementById(myField).value.substring(0, startPos) + myValue + document.getElementById(myField).value.substring(endPos, document.getElementById(myField).value.length);
  153. } else {
  154. document.getElementById(myField).value += myValue;
  155. }
  156. }
  157. </script>
  158. '''
  159. insert_list = [['[[]]', '링크'], ['[()]', '매크로'], ['{{{#!}}}', '중괄호']]
  160. '<a href="javascript:void(0);" onclick="insertAtCursor(\'content\', \'B\');">(A)</a>'
  161. return ['', '']
  162. def ip_warring():
  163. if custom()[2] == 0:
  164. curs.execute('select data from other where name = "no_login_warring"')
  165. data = curs.fetchall()
  166. if data and data[0][0] != '':
  167. text_data = '<span>' + data[0][0] + '</span><hr>'
  168. else:
  169. text_data = '<span>' + load_lang('no_login_warring') + '</span><hr>'
  170. else:
  171. text_data = ''
  172. return text_data
  173. def skin_check():
  174. skin = './views/acme/'
  175. try:
  176. curs.execute('select skin from user where id = ?', [ip_check()])
  177. skin_exist = curs.fetchall()
  178. if skin_exist and skin_exist[0][0] != '':
  179. if os.path.exists(os.path.abspath('./views/' + skin_exist[0][0] + '/index.html')) == 1:
  180. skin = './views/' + skin_exist[0][0] + '/'
  181. else:
  182. curs.execute('select data from other where name = "skin"')
  183. skin_exist = curs.fetchall()
  184. if skin_exist:
  185. if os.path.exists(os.path.abspath('./views/' + skin_exist[0][0] + '/index.html')) == 1:
  186. skin = './views/' + skin_exist[0][0] + '/'
  187. except:
  188. pass
  189. return skin + 'index.html'
  190. def next_fix(link, num, page, end = 50):
  191. list_data = ''
  192. if num == 1:
  193. if len(page) == end:
  194. list_data += '<hr><a href="' + link + str(num + 1) + '">(' + load_lang('next') + ')</a>'
  195. elif len(page) != end:
  196. list_data += '<hr><a href="' + link + str(num - 1) + '">(' + load_lang('previous') + ')</a>'
  197. else:
  198. list_data += '<hr><a href="' + link + str(num - 1) + '">(' + load_lang('previous') + ')</a> <a href="' + link + str(num + 1) + '">(' + load_lang('next') + ')</a>'
  199. return list_data
  200. def other2(data):
  201. return data + ['Deleted']
  202. def wiki_set(num = 1):
  203. if num == 1:
  204. data_list = []
  205. curs.execute('select data from other where name = ?', ['name'])
  206. db_data = curs.fetchall()
  207. if db_data and db_data[0][0] != '':
  208. data_list += [db_data[0][0]]
  209. else:
  210. data_list += ['Wiki']
  211. curs.execute('select data from other where name = "license"')
  212. db_data = curs.fetchall()
  213. if db_data and db_data[0][0] != '':
  214. data_list += [db_data[0][0]]
  215. else:
  216. data_list += ['CC 0']
  217. data_list += ['', '']
  218. curs.execute('select data from other where name = "logo"')
  219. db_data = curs.fetchall()
  220. if db_data and db_data[0][0] != '':
  221. data_list += [db_data[0][0]]
  222. else:
  223. data_list += [data_list[0]]
  224. curs.execute("select data from other where name = 'head'")
  225. db_data = curs.fetchall()
  226. if db_data and db_data[0][0] != '':
  227. data_list += [db_data[0][0]]
  228. else:
  229. data_list += ['']
  230. return data_list
  231. if num == 2:
  232. var_data = 'FrontPage'
  233. curs.execute('select data from other where name = "frontpage"')
  234. elif num == 3:
  235. var_data = '2'
  236. curs.execute('select data from other where name = "upload"')
  237. db_data = curs.fetchall()
  238. if db_data and db_data[0][0] != '':
  239. return db_data[0][0]
  240. else:
  241. return var_data
  242. def diff(seqm):
  243. output = []
  244. for opcode, a0, a1, b0, b1 in seqm.get_opcodes():
  245. if opcode == 'equal':
  246. output += [seqm.a[a0:a1]]
  247. elif opcode == 'insert':
  248. output += ["<span style='background:#CFC;'>" + seqm.b[b0:b1] + "</span>"]
  249. elif opcode == 'delete':
  250. output += ["<span style='background:#FDD;'>" + seqm.a[a0:a1] + "</span>"]
  251. elif opcode == 'replace':
  252. output += ["<span style='background:#FDD;'>" + seqm.a[a0:a1] + "</span>"]
  253. output += ["<span style='background:#CFC;'>" + seqm.b[b0:b1] + "</span>"]
  254. return ''.join(output)
  255. def admin_check(num, what):
  256. ip = ip_check()
  257. curs.execute("select acl from user where id = ?", [ip])
  258. user = curs.fetchall()
  259. if user:
  260. reset = 0
  261. while 1:
  262. if num == 1 and reset == 0:
  263. check = 'ban'
  264. elif num == 3 and reset == 0:
  265. check = 'toron'
  266. elif num == 4 and reset == 0:
  267. check = 'check'
  268. elif num == 5 and reset == 0:
  269. check = 'acl'
  270. elif num == 6 and reset == 0:
  271. check = 'hidel'
  272. elif num == 7 and reset == 0:
  273. check = 'give'
  274. else:
  275. check = 'owner'
  276. curs.execute('select name from alist where name = ? and acl = ?', [user[0][0], check])
  277. if curs.fetchall():
  278. if what:
  279. curs.execute("insert into re_admin (who, what, time) values (?, ?, ?)", [ip, what, get_time()])
  280. conn.commit()
  281. return 1
  282. else:
  283. if reset == 0:
  284. reset = 1
  285. else:
  286. break
  287. def ip_pas(raw_ip):
  288. hide = 0
  289. if re.search("(\.|:)", raw_ip):
  290. if not re.search("^" + load_lang('tool') + ":", raw_ip):
  291. curs.execute("select data from other where name = 'ip_view'")
  292. data = curs.fetchall()
  293. if data and data[0][0] != '':
  294. ip = '<span style="font-size: 75%;">' + hashlib.md5(bytes(raw_ip, 'utf-8')).hexdigest() + '</span>'
  295. if not admin_check('ban', None):
  296. hide = 1
  297. else:
  298. ip = raw_ip
  299. else:
  300. ip = raw_ip
  301. hide = 1
  302. else:
  303. curs.execute("select title from data where title = ?", ['user:' + raw_ip])
  304. if curs.fetchall():
  305. ip = '<a href="/w/' + url_pas('user:' + raw_ip) + '">' + raw_ip + '</a>'
  306. else:
  307. ip = '<a id="not_thing" href="/w/' + url_pas('user:' + raw_ip) + '">' + raw_ip + '</a>'
  308. if hide == 0:
  309. ip += ' <a href="/record/' + url_pas('user:' + raw_ip) + '">(' + load_lang('record') + ')</a>'
  310. return ip
  311. def custom():
  312. if 'MyMaiToNight' in flask.session:
  313. user_head = flask.session['MyMaiToNight']
  314. else:
  315. user_head = ''
  316. if 'Now' in flask.session and flask.session['Now'] == 1:
  317. curs.execute('select name from alarm where name = ? limit 1', [ip_check()])
  318. if curs.fetchall():
  319. user_icon = 2
  320. else:
  321. user_icon = 1
  322. else:
  323. user_icon = 0
  324. if user_icon != 0:
  325. curs.execute('select email from user where id = ?', [ip_check()])
  326. data = curs.fetchall()
  327. if data:
  328. email = data[0][0]
  329. else:
  330. email = ''
  331. else:
  332. email = ''
  333. if user_icon != 0:
  334. user_name = ip_check()
  335. else:
  336. user_name = load_lang('user')
  337. return ['', '', user_icon, user_head, email, user_name]
  338. def acl_check(name):
  339. ip = ip_check()
  340. if ban_check() == 1:
  341. return 1
  342. acl_c = re.search("^user:([^/]*)", name)
  343. if acl_c:
  344. acl_n = acl_c.groups()
  345. if admin_check(5, None) == 1:
  346. return 0
  347. curs.execute("select dec from acl where title = ?", ['user:' + acl_n[0]])
  348. acl_data = curs.fetchall()
  349. if acl_data:
  350. if acl_data[0][0] == 'all':
  351. return 0
  352. if acl_data[0][0] == 'user' and not re.search("(\.|:)", ip):
  353. return 0
  354. if ip != acl_n[0] or re.search("(\.|:)", ip):
  355. return 1
  356. if ip == acl_n[0] and not re.search("(\.|:)", ip) and not re.search("(\.|:)", acl_n[0]):
  357. return 0
  358. else:
  359. return 1
  360. file_c = re.search("^file:(.*)", name)
  361. if file_c and admin_check(5, 'edit (' + name + ')') != 1:
  362. return 1
  363. curs.execute("select acl from user where id = ?", [ip])
  364. user_data = curs.fetchall()
  365. curs.execute("select dec from acl where title = ?", [name])
  366. acl_data = curs.fetchall()
  367. if acl_data:
  368. if acl_data[0][0] == 'user':
  369. if not user_data:
  370. return 1
  371. if acl_data[0][0] == 'admin':
  372. if not user_data:
  373. return 1
  374. if not admin_check(5, 'edit (' + name + ')') == 1:
  375. return 1
  376. curs.execute('select data from other where name = "edit"')
  377. set_data = curs.fetchall()
  378. if set_data:
  379. if set_data[0][0] == 'user':
  380. if not user_data:
  381. return 1
  382. if set_data[0][0] == 'admin':
  383. if not user_data:
  384. return 1
  385. if not admin_check(5, None) == 1:
  386. return 1
  387. return 0
  388. def ban_check():
  389. ip = ip_check()
  390. band = re.search("^([0-9]{1,3}\.[0-9]{1,3})", ip)
  391. if band:
  392. band_it = band.groups()[0]
  393. else:
  394. band_it = 'Not'
  395. curs.execute("select block from ban where block = ?", [band_it])
  396. band_d = curs.fetchall()
  397. curs.execute("select block from ban where block = ?", [ip])
  398. ban_d = curs.fetchall()
  399. if band_d or ban_d:
  400. return 1
  401. return 0
  402. def topic_check(name, sub):
  403. ip = ip_check()
  404. if ban_check() == 1:
  405. return 1
  406. curs.execute("select acl from user where id = ?", [ip])
  407. user_data = curs.fetchall()
  408. curs.execute("select dis from acl where title = ?", [name])
  409. acl_data = curs.fetchall()
  410. if acl_data:
  411. if acl_data[0][0] == 'user':
  412. if not user_data:
  413. return 1
  414. if acl_data[0][0] == 'admin':
  415. if not user_data:
  416. return 1
  417. if not admin_check(3, 'topic (' + name + ')') == 1:
  418. return 1
  419. curs.execute("select title from stop where title = ? and sub = ?", [name, sub])
  420. if curs.fetchall():
  421. if not admin_check(3, 'topic (' + name + ')') == 1:
  422. return 1
  423. return 0
  424. def ban_insert(name, end, why, login, blocker):
  425. time = get_time()
  426. if re.search("^([0-9]{1,3}\.[0-9]{1,3})$", name):
  427. band = 'O'
  428. else:
  429. band = ''
  430. curs.execute("select block from ban where block = ?", [name])
  431. if curs.fetchall():
  432. curs.execute("insert into rb (block, end, today, blocker, why, band) values (?, ?, ?, ?, ?, ?)", [name, load_lang('release'), time, blocker, '', band])
  433. curs.execute("delete from ban where block = ?", [name])
  434. else:
  435. if login != '':
  436. login = 'O'
  437. else:
  438. login = ''
  439. if end != '':
  440. end += ' 00:00:00'
  441. curs.execute("insert into rb (block, end, today, blocker, why, band) values (?, ?, ?, ?, ?, ?)", [name, end, time, blocker, why, band])
  442. curs.execute("insert into ban (block, end, why, band, login) values (?, ?, ?, ?, ?)", [name, end, why, band, login])
  443. conn.commit()
  444. def rd_plus(title, sub, date):
  445. curs.execute("select title from rd where title = ? and sub = ?", [title, sub])
  446. if curs.fetchall():
  447. curs.execute("update rd set date = ? where title = ? and sub = ?", [date, title, sub])
  448. else:
  449. curs.execute("insert into rd (title, sub, date) values (?, ?, ?)", [title, sub, date])
  450. def history_plus(title, data, date, ip, send, leng):
  451. curs.execute("select id from history where title = ? order by id + 0 desc limit 1", [title])
  452. id_data = curs.fetchall()
  453. if id_data:
  454. 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])
  455. else:
  456. 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])
  457. def leng_check(first, second):
  458. if first < second:
  459. all_plus = '+' + str(second - first)
  460. elif second < first:
  461. all_plus = '-' + str(first - second)
  462. else:
  463. all_plus = '0'
  464. return all_plus
  465. def redirect(data):
  466. return '<meta http-equiv="refresh" content="0; url=' + data + '">'
  467. def re_error(data):
  468. if data == '/ban':
  469. ip = ip_check()
  470. end = '<li>' + load_lang('why') + ' : ' + load_lang('authority_error') + '</li>'
  471. if ban_check() == 1:
  472. curs.execute("select end, why from ban where block = ?", [ip])
  473. end_data = curs.fetchall()
  474. if not end_data:
  475. match = re.search("^([0-9]{1,3}\.[0-9]{1,3})", ip)
  476. if match:
  477. curs.execute("select end, why from ban where block = ?", [match.groups()[0]])
  478. end_data = curs.fetchall()
  479. if end_data:
  480. end = '<li>' + load_lang('state') + ' : '
  481. if end_data[0][0]:
  482. now = int(re.sub('(\-| |:)', '', get_time()))
  483. day = int(re.sub('(\-| |:)', '', end_data[0][0]))
  484. if now >= day:
  485. curs.execute("delete from ban where block = ?", [ip])
  486. conn.commit()
  487. end += 'Re Try.'
  488. else:
  489. end += load_lang('why') + ' : ' + end_data[0][0]
  490. else:
  491. end += load_lang('why') + ' : ' + load_lang('limitless')
  492. end += '</li>'
  493. if end_data[0][1] != '':
  494. end += '<li>' + load_lang('why') + ' : ' + end_data[0][1] + '</li>'
  495. return css_html_js_minify.html_minify(flask.render_template(skin_check(),
  496. imp = ['Error', wiki_set(1), custom(), other2([0, 0])],
  497. data = '<h2>Error</h2><ul>' + end + '</ul>',
  498. menu = 0
  499. ))
  500. else:
  501. error_data = re.search('\/error\/([0-9]+)', data)
  502. if error_data:
  503. num = int(error_data.groups()[0])
  504. if num == 1:
  505. data = load_lang('no_login_error')
  506. elif num == 2:
  507. data = load_lang('no_exist_user_error')
  508. elif num == 3:
  509. data = load_lang('authority_error')
  510. elif num == 4:
  511. data = load_lang('no_admin_block_error')
  512. elif num == 6:
  513. data = load_lang('same_id_exist_error')
  514. elif num == 7:
  515. data = load_lang('long_id_error')
  516. elif num == 8:
  517. data = load_lang('id_char_error')
  518. elif num == 9:
  519. data = load_lang('file_exist_error')
  520. elif num == 10:
  521. data = load_lang('password_error')
  522. elif num == 13:
  523. data = load_lang('recaptcha_error')
  524. elif num == 14:
  525. data = load_lang('file_extension_error')
  526. elif num == 15:
  527. data = load_lang('edit_record_error')
  528. elif num == 16:
  529. data = load_lang('same_file_error')
  530. elif num == 17:
  531. data = load_lang('file_capacity_error') + ' ' + wiki_set(3)
  532. elif num == 19:
  533. data = load_lang('decument_exist_error')
  534. elif num == 20:
  535. data = load_lang('password_diffrent_error')
  536. elif num == 21:
  537. data = load_lang('edit_filter_error')
  538. elif num == 22:
  539. data = load_lang('file_name_error')
  540. else:
  541. data = '???'
  542. return css_html_js_minify.html_minify(flask.render_template(skin_check(),
  543. imp = ['Error', wiki_set(1), custom(), other2([0, 0])],
  544. data = '<h2>Error</h2><ul><li>' + data + '</li></ul>',
  545. menu = 0
  546. ))
  547. else:
  548. return redirect('/')