func.py 22 KB

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