func.py 22 KB

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