func.py 22 KB

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