func.py 22 KB

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