func.py 22 KB

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