func.py 22 KB

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