func.py 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268
  1. import os
  2. import sys
  3. import platform
  4. for i in range(0, 2):
  5. try:
  6. from diff_match_patch import diff_match_patch
  7. import werkzeug.routing
  8. import werkzeug.debug
  9. import flask_compress
  10. import flask_reggie
  11. import tornado.ioloop
  12. import tornado.httpserver
  13. import tornado.wsgi
  14. import urllib.request
  15. import email.mime.text
  16. import sqlite3
  17. import pymysql
  18. import hashlib
  19. import smtplib
  20. import bcrypt
  21. import zipfile
  22. import shutil
  23. import threading
  24. import logging
  25. import random
  26. import flask
  27. import json
  28. import html
  29. import re
  30. if sys.version_info < (3, 6):
  31. import sha3
  32. from .mark import *
  33. except ImportError as e:
  34. if i == 0:
  35. print(e)
  36. print('----')
  37. if platform.system() == 'Linux':
  38. ok = os.system('python3 -m pip install --user -r requirements.txt')
  39. if ok == 0:
  40. print('----')
  41. os.execl(sys.executable, sys.executable, *sys.argv)
  42. else:
  43. raise
  44. elif platform.system() == 'Windows':
  45. ok = os.system('python -m pip install --user -r requirements.txt')
  46. if ok == 0:
  47. print('----')
  48. os.execl(sys.executable, sys.executable, *sys.argv)
  49. else:
  50. raise
  51. else:
  52. print('----')
  53. print(e)
  54. raise
  55. else:
  56. print('----')
  57. print(e)
  58. raise
  59. app_var = json.loads(open('data/app_var.json', encoding='utf8').read())
  60. def load_conn(data):
  61. global conn
  62. global curs
  63. conn = data
  64. curs = conn.cursor()
  65. load_conn2(data)
  66. def send_email(who, title, data):
  67. try:
  68. curs.execute(db_change('select name, data from other where name = "smtp_email" or name = "smtp_pass" or name = "smtp_server" or name = "smtp_port" or name = "smtp_security"'))
  69. rep_data = curs.fetchall()
  70. smtp_email = ''
  71. smtp_pass = ''
  72. smtp_server = ''
  73. smtp_security = ''
  74. smtp_port = ''
  75. smtp = ''
  76. if rep_data:
  77. smtp_email = ''
  78. smtp_pass = ''
  79. for i in rep_data:
  80. if i[0] == 'smtp_email':
  81. smtp_email = i[1]
  82. elif i[0] == 'smtp_pass':
  83. smtp_pass = i[1]
  84. elif i[0] == 'smtp_server':
  85. smtp_server = i[1]
  86. elif i[0] == 'smtp_security':
  87. smtp_security = i[1]
  88. elif i[0] == 'smtp_port':
  89. smtp_port = i[1]
  90. smtp_port = int(smtp_port)
  91. if smtp_security == 'plain':
  92. smtp = smtplib.SMTP(smtp_server, smtp_port)
  93. elif smtp_security == 'starttls':
  94. smtp = smtplib.SMTP(smtp_server, smtp_port)
  95. smtp.starttls()
  96. else: # if smtp_security == 'tls':
  97. smtp = smtplib.SMTP_SSL(smtp_server, smtp_port)
  98. smtp.login(smtp_email, smtp_pass)
  99. msg = email.mime.text.MIMEText(data)
  100. msg['Subject'] = title
  101. smtp.sendmail(smtp_email, who, msg.as_string())
  102. smtp.quit()
  103. except:
  104. print('----')
  105. print('Error : Email send error')
  106. def last_change(data):
  107. json_address = re.sub("(((?!\.|\/).)+)\.html$", "set.json", skin_check())
  108. try:
  109. json_data = json.loads(open(json_address, encoding='utf8').read())
  110. except:
  111. json_data = 0
  112. if json_data != 0:
  113. for j_data in json_data:
  114. if "class" in json_data[j_data]:
  115. if "require" in json_data[j_data]:
  116. re_data = re.compile("<((?:" + j_data + ")( (?:(?!>).)*)?)>")
  117. s_data = re_data.findall(data)
  118. for i_data in s_data:
  119. e_data = 0
  120. for j_i_data in json_data[j_data]["require"]:
  121. re_data_2 = re.compile("( |^)" + j_i_data + " *= *[\'\"]" + json_data[j_data]["require"][j_i_data] + "[\'\"]")
  122. if not re_data_2.search(i_data[1]):
  123. re_data_2 = re.compile("( |^)" + j_i_data + "=" + json_data[j_data]["require"][j_i_data] + "(?: |$)")
  124. if not re_data_2.search(i_data[1]):
  125. e_data = 1
  126. break
  127. if e_data == 0:
  128. re_data_3 = re.compile("<" + i_data[0] + ">")
  129. data = re_data_3.sub("<" + i_data[0] + " class=\"" + json_data[j_data]["class"] + "\">", data)
  130. else:
  131. re_data = re.compile("<(?P<in>" + j_data + "(?: (?:(?!>).)*)?)>")
  132. data = re_data.sub("<\g<in> class=\"" + json_data[j_data]["class"] + "\">", data)
  133. return data
  134. def easy_minify(data, tool = None):
  135. return last_change(data)
  136. def render_set(title = '', data = '', num = 0, s_data = 0, include = None):
  137. if acl_check(title, 'render') == 1:
  138. return 'HTTP Request 401.3'
  139. elif s_data == 1:
  140. return data
  141. else:
  142. if data != None:
  143. return render_do(title, data, num, include)
  144. else:
  145. return 'HTTP Request 404'
  146. def update(ver_num, set_data):
  147. print('----')
  148. # 업데이트 하위 호환 유지 함수
  149. if ver_num < 3160027:
  150. print('Add init set')
  151. set_init()
  152. if ver_num < 3170002:
  153. curs.execute(db_change("select html from html_filter where kind = 'extension'"))
  154. if not curs.fetchall():
  155. for i in ['jpg', 'jpeg', 'png', 'gif', 'webp']:
  156. curs.execute(db_change("insert into html_filter (html, kind) values (?, 'extension')"), [i])
  157. if ver_num < 3170400:
  158. curs.execute(db_change("select title, sub, code from topic where id = '1'"))
  159. change_topic = curs.fetchall()
  160. for i in change_topic:
  161. curs.execute(db_change("update topic set code = ? where title = ? and sub = ?"), [i[2], i[0], i[1]])
  162. curs.execute(db_change("update rd set code = ? where title = ? and sub = ?"), [i[2], i[0], i[1]])
  163. if ver_num < 3171800:
  164. curs.execute(db_change("select data from other where name = 'recaptcha'"))
  165. change_rec = curs.fetchall()
  166. if change_rec and change_rec[0][0] != '':
  167. new_rec = re.search('data-sitekey="([^"]+)"', change_rec[0][0])
  168. if new_rec:
  169. curs.execute(db_change("update other set data = ? where name = 'recaptcha'"), [new_rec.group(1)])
  170. else:
  171. curs.execute(db_change("update other set data = '' where name = 'recaptcha'"))
  172. curs.execute(db_change("update other set data = '' where name = 'sec_re'"))
  173. if ver_num < 3172800 and set_data['db_type'] == 'mysql':
  174. get_data_mysql = json.loads(open('data/mysql.json').read())
  175. with open('data/mysql.json', 'w') as f:
  176. f.write('{ "user" : "' + get_data_mysql['user'] + '", "password" : "' + get_data_mysql['password'] + '", "host" : "localhost" }')
  177. if ver_num < 3182000:
  178. curs.execute(db_change('delete from cache_data'))
  179. conn.commit()
  180. print('Update pass')
  181. def set_init():
  182. # 초기값 설정 함수
  183. curs.execute(db_change("select html from html_filter where kind = 'email'"))
  184. if not curs.fetchall():
  185. for i in ['naver.com', 'gmail.com', 'daum.net', 'kakao.com']:
  186. curs.execute(db_change("insert into html_filter (html, kind) values (?, 'email')"), [i])
  187. curs.execute(db_change("select html from html_filter where kind = 'extension'"))
  188. if not curs.fetchall():
  189. for i in ['jpg', 'jpeg', 'png', 'gif', 'webp']:
  190. curs.execute(db_change("insert into html_filter (html, kind) values (?, 'extension')"), [i])
  191. curs.execute(db_change('select data from other where name = "smtp_server" or name = "smtp_port" or name = "smtp_security"'))
  192. if not curs.fetchall():
  193. for i in [['smtp_server', 'imap.google.com'], ['smtp_port', '587'], ['smtp_security', 'tls']]:
  194. curs.execute(db_change("insert into other (name, data) values (?, ?)"), [i[0], i[1]])
  195. def pw_encode(data, data2 = '', type_d = ''):
  196. if type_d == '':
  197. curs.execute(db_change('select data from other where name = "encode"'))
  198. set_data = curs.fetchall()
  199. type_d = set_data[0][0]
  200. if type_d == 'sha256':
  201. return hashlib.sha256(bytes(data, 'utf-8')).hexdigest()
  202. elif type_d == 'sha3':
  203. if sys.version_info < (3, 6):
  204. return sha3.sha3_256(bytes(data, 'utf-8')).hexdigest()
  205. else:
  206. return hashlib.sha3_256(bytes(data, 'utf-8')).hexdigest()
  207. else:
  208. if data2 != '':
  209. salt_data = bytes(data2, 'utf-8')
  210. else:
  211. salt_data = bcrypt.gensalt(11)
  212. return bcrypt.hashpw(bytes(data, 'utf-8'), salt_data).decode()
  213. def pw_check(data, data2, type_d = 'no', id_d = ''):
  214. curs.execute(db_change('select data from other where name = "encode"'))
  215. db_data = curs.fetchall()
  216. if type_d != 'no':
  217. if type_d == '':
  218. set_data = 'bcrypt'
  219. else:
  220. set_data = type_d
  221. else:
  222. set_data = db_data[0][0]
  223. while 1:
  224. if set_data in ['sha256', 'sha3']:
  225. data3 = pw_encode(data = data, type_d = set_data)
  226. if data3 == data2:
  227. re_data = 1
  228. else:
  229. re_data = 0
  230. break
  231. else:
  232. try:
  233. if pw_encode(data, data2, 'bcrypt') == data2:
  234. re_data = 1
  235. else:
  236. re_data = 0
  237. break
  238. except:
  239. set_data = db_data[0][0]
  240. if db_data[0][0] != set_data and re_data == 1 and id_d != '':
  241. curs.execute(db_change("update user set pw = ?, encode = ? where id = ?"), [pw_encode(data), db_data[0][0], id_d])
  242. return re_data
  243. def captcha_get():
  244. data = ''
  245. if ip_or_user() != 0:
  246. curs.execute(db_change('select data from other where name = "recaptcha"'))
  247. recaptcha = curs.fetchall()
  248. if recaptcha and recaptcha[0][0] != '':
  249. curs.execute(db_change('select data from other where name = "sec_re"'))
  250. sec_re = curs.fetchall()
  251. if sec_re and sec_re[0][0] != '':
  252. curs.execute(db_change('select data from other where name = "recaptcha_ver"'))
  253. rec_ver = curs.fetchall()
  254. if not rec_ver or rec_ver[0][0] == '':
  255. data += '' + \
  256. '<script src="https://www.google.com/recaptcha/api.js" async defer></script>' + \
  257. '<div class="g-recaptcha" data-sitekey="' + recaptcha[0][0] + '"></div>' + \
  258. '<hr class=\"main_hr\">' + \
  259. ''
  260. else:
  261. data += '' + \
  262. '<script src="https://www.google.com/recaptcha/api.js?render=' + recaptcha[0][0] + '"></script>' + \
  263. '<input type="hidden" id="g-recaptcha" name="g-recaptcha">' + \
  264. '<script type="text/javascript">' + \
  265. 'grecaptcha.ready(function() {' + \
  266. 'grecaptcha.execute(\'' + recaptcha[0][0] + '\', {action: \'homepage\'}).then(function(token) {' + \
  267. 'document.getElementById(\'g-recaptcha\').value = token;' + \
  268. '});' + \
  269. '});' + \
  270. '</script>' + \
  271. ''
  272. return data
  273. def captcha_post(re_data, num = 1):
  274. if num == 1:
  275. curs.execute(db_change('select data from other where name = "sec_re"'))
  276. sec_re = curs.fetchall()
  277. if sec_re and sec_re[0][0] != '' and ip_or_user() != 0 and captcha_get() != '':
  278. try:
  279. data = urllib.request.urlopen('https://www.google.com/recaptcha/api/siteverify?secret=' + sec_re[0][0] + '&response=' + re_data)
  280. except:
  281. data = None
  282. if data and data.getcode() == 200:
  283. json_data = json.loads(data.read().decode(data.headers.get_content_charset()))
  284. if json_data['success'] == True:
  285. return 0
  286. else:
  287. return 1
  288. else:
  289. return 0
  290. else:
  291. return 0
  292. else:
  293. pass
  294. def load_lang(data, num = 2, safe = 0):
  295. if num == 1:
  296. curs.execute(db_change("select data from other where name = 'language'"))
  297. rep_data = curs.fetchall()
  298. json_data = open(os.path.join('language', rep_data[0][0] + '.json'), encoding='utf8').read()
  299. lang = json.loads(json_data)
  300. if data in lang:
  301. if safe == 1:
  302. return lang[data]
  303. else:
  304. return html.escape(lang[data])
  305. else:
  306. return html.escape(data + ' (M)')
  307. else:
  308. curs.execute(db_change('select data from user_set where name = "lang" and id = ?'), [ip_check()])
  309. rep_data = curs.fetchall()
  310. if rep_data:
  311. try:
  312. json_data = open(os.path.join('language', rep_data[0][0] + '.json'), encoding='utf8').read()
  313. lang = json.loads(json_data)
  314. except:
  315. return load_lang(data, 1, safe)
  316. if data in lang:
  317. if safe == 1:
  318. return lang[data]
  319. else:
  320. return html.escape(lang[data])
  321. else:
  322. return load_lang(data, 1, safe)
  323. else:
  324. return load_lang(data, 1, safe)
  325. def load_oauth(provider):
  326. oauth_supported = ["discord", "facebook", "naver", "kakao"]
  327. if(provider == '_README'):
  328. return { "support" : oauth_supported }
  329. else:
  330. try:
  331. oauth = json.loads(open(app_var['path_oauth_setting'], encoding='utf8').read())
  332. except:
  333. return_json_data = '{ "publish_url" : "", '
  334. for i in range(len(oauth_supported)):
  335. return_json_data += '"' + oauth_supported[i] + '" : { '
  336. for j in range(2):
  337. if j == 0:
  338. load_target = 'id'
  339. elif j == 1:
  340. load_target = 'secret'
  341. return_json_data += '"client_' + load_target + '" : ""' + (',' if j == 0 else '')
  342. return_json_data += ' }'
  343. try:
  344. _ = oauth_supported[i + 1]
  345. return_json_data += ', '
  346. except:
  347. return_json_data += ' }'
  348. with open(app_var['path_oauth_setting'], 'w', encoding='utf-8') as f:
  349. f.write(return_json_data)
  350. oauth = json.loads(open(app_var['path_oauth_setting'], encoding='utf8').read())
  351. return oauth[provider]
  352. def update_oauth(provider, target, content):
  353. oauth = json.loads(open(app_var['path_oauth_setting'], encoding='utf8').read())
  354. oauth[provider][target] = content
  355. with open(app_var['path_oauth_setting'], 'w', encoding='utf8') as f:
  356. f.write(json.dumps(oauth, sort_keys = True, indent = 4))
  357. return 'Done'
  358. def ip_or_user(data = ''):
  359. if data == '':
  360. data = ip_check()
  361. if re.search('(\.|:)', data):
  362. return 1
  363. else:
  364. return 0
  365. def edit_button():
  366. insert_list = []
  367. curs.execute(db_change("select html, plus from html_filter where kind = 'edit_top'"))
  368. db_data = curs.fetchall()
  369. for get_data in db_data:
  370. insert_list += [[get_data[1], get_data[0]]]
  371. data = ''
  372. for insert_data in insert_list:
  373. data += '<a href="javascript:do_insert_data(\'content\', \'' + insert_data[0] + '\')">(' + insert_data[1] + ')</a> '
  374. if admin_check() == 1:
  375. data += (' ' if data != '' else '') + '<a href="/edit_top">(' + load_lang('add') + ')</a>'
  376. return data + '<hr class=\"main_hr\">'
  377. def ip_warring():
  378. if ip_or_user() != 0:
  379. curs.execute(db_change('select data from other where name = "no_login_warring"'))
  380. data = curs.fetchall()
  381. if data and data[0][0] != '':
  382. text_data = '<span>' + data[0][0] + '</span><hr class=\"main_hr\">'
  383. else:
  384. text_data = '<span>' + load_lang('no_login_warring') + '</span><hr class=\"main_hr\">'
  385. else:
  386. text_data = ''
  387. return text_data
  388. def skin_check(set_n = 0):
  389. skin = 'marisa'
  390. curs.execute(db_change('select data from other where name = "skin"'))
  391. skin_exist = curs.fetchall()
  392. if skin_exist and skin_exist[0][0] != '':
  393. if os.path.exists(os.path.abspath('./views/' + skin_exist[0][0] + '/index.html')) == 1:
  394. skin = skin_exist[0][0]
  395. curs.execute(db_change('select data from user_set where name = "skin" and id = ?'), [ip_check()])
  396. skin_exist = curs.fetchall()
  397. if skin_exist and skin_exist[0][0] != '':
  398. if os.path.exists(os.path.abspath('./views/' + skin_exist[0][0] + '/index.html')) == 1:
  399. skin = skin_exist[0][0]
  400. if set_n == 0:
  401. return './views/' + skin + '/index.html'
  402. else:
  403. return skin
  404. def next_fix(link, num, page, end = 50):
  405. list_data = ''
  406. if num == 1:
  407. if len(page) == end:
  408. list_data += '<hr class=\"main_hr\"><a href="' + link + str(num + 1) + '">(' + load_lang('next') + ')</a>'
  409. elif len(page) != end:
  410. list_data += '<hr class=\"main_hr\"><a href="' + link + str(num - 1) + '">(' + load_lang('previous') + ')</a>'
  411. else:
  412. list_data += '<hr class=\"main_hr\"><a href="' + link + str(num - 1) + '">(' + load_lang('previous') + ')</a> <a href="' + link + str(num + 1) + '">(' + load_lang('next') + ')</a>'
  413. return list_data
  414. def other2(data):
  415. for _ in range(0, 3 - len(data)):
  416. data += ['']
  417. req_list = ''
  418. main_css_ver = 31
  419. if not 'main_css_load' in flask.session or not 'main_css_ver' in flask.session or flask.session['main_css_ver'] != main_css_ver:
  420. for i_data in os.listdir(os.path.join("views", "main_css", "css")):
  421. req_list += '<link rel="stylesheet" href="/views/main_css/css/' + i_data + '?ver=' + str(main_css_ver) + '">'
  422. for i_data in os.listdir(os.path.join("views", "main_css", "js")):
  423. req_list += '<script src="/views/main_css/js/' + i_data + '?ver=' + str(main_css_ver) + '"></script>'
  424. flask.session['main_css_load'] = req_list
  425. flask.session['main_css_ver'] = main_css_ver
  426. else:
  427. req_list = flask.session['main_css_load']
  428. data = data[0:2] + ['', '''
  429. <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/default.min.css">
  430. <link rel="stylesheet"
  431. href="https://cdn.jsdelivr.net/npm/katex@0.10.1/dist/katex.min.css"
  432. integrity="sha384-dbVIfZGuN1Yq7/1Ocstc1lUEm+AT+/rCkibIcC/OmWo5f0EA48Vf8CytHzGrSwbQ"
  433. crossorigin="anonymous">
  434. <script src="https://cdn.jsdelivr.net/npm/katex@0.10.1/dist/katex.min.js"
  435. integrity="sha384-2BKqo+exmr9su6dir+qCw08N2ZKRucY4PrGQPPWU1A7FtlCGjmEGFqXCv5nyM5Ij"
  436. crossorigin="anonymous"></script>
  437. <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/highlight.min.js"></script>
  438. ''' + req_list + '<script>main_css_skin_load();</script>'] + data[2:]
  439. return data
  440. def cut_100(data):
  441. if re.search('^\/w\/', flask.request.path):
  442. data = re.sub('<script>((\n*(((?!<\/script>).)+)\n*)+)<\/script>', '', data)
  443. data = re.sub('<hr class="main_hr">((\n*((.+)\n*))+)$', '', data)
  444. data = re.sub('<div id="cate_all">((\n*((.+)\n*))+)$', '', data)
  445. data = re.sub('<(((?!>).)*)>', ' ', data)
  446. data = re.sub('\n', ' ', data)
  447. data = re.sub('^ +', '', data)
  448. data = re.sub(' +$', '', data)
  449. data = re.sub(' {2,}', ' ', data)
  450. return data[0:100] + '...'
  451. else:
  452. return ''
  453. def wiki_set(num = 1):
  454. if num == 1:
  455. data_list = []
  456. curs.execute(db_change('select data from other where name = ?'), ['name'])
  457. db_data = curs.fetchall()
  458. if db_data and db_data[0][0] != '':
  459. data_list += [db_data[0][0]]
  460. else:
  461. data_list += ['Wiki']
  462. curs.execute(db_change('select data from other where name = "license"'))
  463. db_data = curs.fetchall()
  464. if db_data and db_data[0][0] != '':
  465. data_list += [db_data[0][0]]
  466. else:
  467. data_list += ['ARR']
  468. data_list += ['', '']
  469. curs.execute(db_change('select data from other where name = "logo"'))
  470. db_data = curs.fetchall()
  471. if db_data and db_data[0][0] != '':
  472. data_list += [db_data[0][0]]
  473. else:
  474. data_list += [data_list[0]]
  475. head_data = ''
  476. curs.execute(db_change("select data from other where name = 'head' and coverage = ''"))
  477. db_data = curs.fetchall()
  478. if db_data and db_data[0][0] != '':
  479. head_data += db_data[0][0]
  480. curs.execute(db_change("select data from other where name = 'head' and coverage = ?"), [skin_check(1)])
  481. db_data = curs.fetchall()
  482. if db_data and db_data[0][0] != '':
  483. head_data += db_data[0][0]
  484. data_list += [head_data]
  485. return data_list
  486. if num == 2:
  487. var_data = 'FrontPage'
  488. curs.execute(db_change('select data from other where name = "frontpage"'))
  489. elif num == 3:
  490. var_data = '2'
  491. curs.execute(db_change('select data from other where name = "upload"'))
  492. db_data = curs.fetchall()
  493. if db_data and db_data[0][0] != '':
  494. return db_data[0][0]
  495. else:
  496. return var_data
  497. def admin_check(num = None, what = None, name = ''):
  498. if name == '':
  499. ip = ip_check()
  500. else:
  501. ip = name
  502. curs.execute(db_change("select acl from user where id = ?"), [ip])
  503. user = curs.fetchall()
  504. if user:
  505. reset = 0
  506. back_num = num
  507. while 1:
  508. if num == 1:
  509. check = 'ban'
  510. elif num == 2:
  511. check = 'nothing'
  512. elif num == 3:
  513. check = 'toron'
  514. elif num == 4:
  515. check = 'check'
  516. elif num == 5:
  517. check = 'acl'
  518. elif num == 6:
  519. check = 'hidel'
  520. elif num == 7:
  521. check = 'give'
  522. else:
  523. check = 'owner'
  524. curs.execute(db_change('select name from alist where name = ? and acl = ?'), [user[0][0], check])
  525. if curs.fetchall():
  526. if what:
  527. curs.execute(db_change("insert into re_admin (who, what, time) values (?, ?, ?)"), [ip, what, get_time()])
  528. conn.commit()
  529. return 1
  530. else:
  531. if back_num == 'all':
  532. if num == 'all':
  533. num = 1
  534. elif num != 8:
  535. num += 1
  536. else:
  537. break
  538. elif num:
  539. num = None
  540. else:
  541. break
  542. return 0
  543. def ip_pas(raw_ip, type_d = 0):
  544. hide = 0
  545. if ip_or_user(raw_ip) != 0:
  546. curs.execute(db_change("select data from other where name = 'ip_view'"))
  547. data = curs.fetchall()
  548. if data and data[0][0] != '':
  549. if re.search('\.', raw_ip):
  550. ip = re.sub('\.([^.]*)\.([^.]*)$', '.*.*', raw_ip)
  551. else:
  552. ip = re.sub(':([^:]*):([^:]*)$', ':*:*', raw_ip)
  553. if not admin_check(1):
  554. hide = 1
  555. else:
  556. ip = raw_ip
  557. else:
  558. if type_d == 0:
  559. curs.execute(db_change("select title from data where title = ?"), ['user:' + raw_ip])
  560. if curs.fetchall():
  561. ip = '<a href="/w/' + url_pas('user:' + raw_ip) + '">' + raw_ip + '</a>'
  562. else:
  563. ip = '<a id="not_thing" href="/w/' + url_pas('user:' + raw_ip) + '">' + raw_ip + '</a>'
  564. if admin_check('all', None, raw_ip) == 1:
  565. ip = '<b>' + ip + '</b>'
  566. else:
  567. ip = raw_ip
  568. if type_d == 0:
  569. if ban_check(raw_ip) == 1:
  570. ip = '<s>' + ip + '</s>'
  571. if hide == 0:
  572. ip += ' <a href="/tool/' + url_pas(raw_ip) + '">(' + load_lang('tool') + ')</a>'
  573. return ip
  574. def custom():
  575. if 'head' in flask.session:
  576. if len(re.findall('<', flask.session['head'])) % 2 != 1:
  577. user_head = flask.session['head']
  578. else:
  579. user_head = ''
  580. else:
  581. user_head = ''
  582. ip = ip_check()
  583. if ip_or_user(ip) == 0:
  584. user_icon = 1
  585. user_name = ip
  586. curs.execute(db_change('select data from user_set where name = "email" and id = ?'), [ip])
  587. data = curs.fetchall()
  588. if data:
  589. email = data[0][0]
  590. else:
  591. email = ''
  592. if admin_check('all') == 1:
  593. user_admin = '1'
  594. user_acl_list = []
  595. curs.execute(db_change("select acl from user where id = ?"), [ip])
  596. curs.execute(db_change('select acl from alist where name = ?'), [curs.fetchall()[0][0]])
  597. user_acl = curs.fetchall()
  598. for i in user_acl:
  599. user_acl_list += [i[0]]
  600. if user_acl != []:
  601. user_acl_list = user_acl_list
  602. else:
  603. user_acl_list = '0'
  604. else:
  605. user_admin = '0'
  606. user_acl_list = '0'
  607. curs.execute(db_change("select count(name) from alarm where name = ?"), [ip])
  608. count = curs.fetchall()
  609. if count:
  610. user_notice = str(count[0][0])
  611. else:
  612. user_notice = '0'
  613. else:
  614. user_icon = 0
  615. user_name = load_lang('user')
  616. email = ''
  617. user_admin = '0'
  618. user_acl_list = '0'
  619. user_notice = '0'
  620. curs.execute(db_change("select title from rd where title = ? and stop = ''"), ['user:' + ip])
  621. if curs.fetchall():
  622. user_topic = '1'
  623. else:
  624. user_topic = '0'
  625. return [
  626. '',
  627. '',
  628. user_icon,
  629. user_head,
  630. email,
  631. user_name,
  632. user_admin,
  633. str(ban_check()),
  634. user_notice,
  635. user_acl_list,
  636. ip,
  637. user_topic
  638. ]
  639. def load_skin(data = '', set_n = 0):
  640. skin_return_data = ''
  641. system_file = ['main_css']
  642. if data == '':
  643. ip = ip_check()
  644. curs.execute(db_change('select data from user_set where name = "skin" and id = ?'), [ip])
  645. data = curs.fetchall()
  646. if not data:
  647. curs.execute(db_change('select data from other where name = "skin"'))
  648. data = curs.fetchall()
  649. if not data or data[0][0] == '':
  650. data = [['marisa']]
  651. if set_n == 0:
  652. for skin_data in os.listdir(os.path.abspath('views')):
  653. if not skin_data in system_file:
  654. if data[0][0] == skin_data:
  655. skin_return_data = '<option value="' + skin_data + '">' + skin_data + '</option>' + skin_return_data
  656. else:
  657. skin_return_data += '<option value="' + skin_data + '">' + skin_data + '</option>'
  658. else:
  659. skin_return_data = []
  660. for skin_data in os.listdir(os.path.abspath('views')):
  661. if not skin_data in system_file:
  662. if data[0][0] == skin_data:
  663. skin_return_data = [skin_data] + skin_return_data
  664. else:
  665. skin_return_data += [skin_data]
  666. else:
  667. if set_n == 0:
  668. for skin_data in os.listdir(os.path.abspath('views')):
  669. if not skin_data in system_file:
  670. if data == skin_data:
  671. skin_return_data = '<option value="' + skin_data + '">' + skin_data + '</option>' + skin_return_data
  672. else:
  673. skin_return_data += '<option value="' + skin_data + '">' + skin_data + '</option>'
  674. else:
  675. skin_return_data = []
  676. for skin_data in os.listdir(os.path.abspath('views')):
  677. if not skin_data in system_file:
  678. if data == skin_data:
  679. skin_return_data = [skin_data] + skin_return_data
  680. else:
  681. skin_return_data += [skin_data]
  682. return skin_return_data
  683. def slow_edit_check():
  684. curs.execute(db_change("select data from other where name = 'slow_edit'"))
  685. slow_edit = curs.fetchall()
  686. if slow_edit and slow_edit != '0' and admin_check(5) != 1:
  687. slow_edit = slow_edit[0][0]
  688. curs.execute(db_change("select date from history where ip = ? order by date desc limit 1"), [ip_check()])
  689. last_edit_data = curs.fetchall()
  690. if last_edit_data:
  691. last_edit_data = int(re.sub(' |:|-', '', last_edit_data[0][0]))
  692. now_edit_data = int((datetime.datetime.now() - datetime.timedelta(seconds = int(slow_edit))).strftime("%Y%m%d%H%M%S"))
  693. if last_edit_data > now_edit_data:
  694. return 1
  695. return 0
  696. def acl_check(name = 'test', tool = '', topic_num = '1'):
  697. ip = ip_check()
  698. get_ban = ban_check()
  699. if name:
  700. acl_c = re.search("^user:((?:(?!\/).)*)", name)
  701. else:
  702. acl_c = None
  703. if tool == '' and acl_c:
  704. acl_n = acl_c.groups()
  705. if get_ban == 1:
  706. return 1
  707. if admin_check(5) == 1:
  708. return 0
  709. curs.execute(db_change("select decu from acl where title = ?"), ['user:' + acl_n[0]])
  710. acl_data = curs.fetchall()
  711. if acl_data:
  712. if acl_data[0][0] == 'all':
  713. return 0
  714. elif acl_data[0][0] == 'user' and not ip_or_user(ip) == 1:
  715. return 0
  716. elif ip == acl_n[0] and not ip_or_user(ip) == 1:
  717. return 0
  718. else:
  719. if ip == acl_n[0] and not ip_or_user(ip) == 1 and not ip_or_user(acl_n[0]) == 1:
  720. return 0
  721. return 1
  722. if tool == '' or tool == 'edit_req':
  723. if acl_check(name, 'render') == 1:
  724. return 1
  725. if tool == '':
  726. end = 3
  727. elif tool == 'topic':
  728. if not name:
  729. curs.execute(db_change("select title from rd where code = ?"), [topic_num])
  730. topic_data = curs.fetchall()
  731. if topic_data:
  732. name = topic_data[0][0]
  733. else:
  734. name = 'test'
  735. end = 3
  736. elif tool == 'render':
  737. end = 2
  738. else:
  739. end = 1
  740. for i in range(0, end):
  741. if tool == '':
  742. if i == 0:
  743. curs.execute(db_change("select decu from acl where title = ?"), [name])
  744. elif i == 1:
  745. curs.execute(db_change('select data from other where name = "edit"'))
  746. else:
  747. curs.execute(db_change("select view from acl where title = ?"), [name])
  748. num = 5
  749. elif tool == 'topic':
  750. if i == 0 and topic_num:
  751. curs.execute(db_change("select acl from rd where code = ?"), [topic_num])
  752. elif i == 1:
  753. curs.execute(db_change("select dis from acl where title = ?"), [name])
  754. else:
  755. curs.execute(db_change('select data from other where name = "discussion"'))
  756. num = 3
  757. elif tool == 'upload':
  758. curs.execute(db_change("select data from other where name = 'upload_acl'"))
  759. num = 5
  760. elif tool == 'many_upload':
  761. curs.execute(db_change("select data from other where name = 'many_upload_acl'"))
  762. num = 5
  763. elif tool == 'edit_req':
  764. curs.execute(db_change("select data from other where name = 'edit_req_acl'"))
  765. num = 5
  766. else:
  767. if i == 0:
  768. curs.execute(db_change("select view from acl where title = ?"), [name])
  769. if i == 1:
  770. curs.execute(db_change("select data from other where name = 'all_view_acl'"))
  771. num = 5
  772. acl_data = curs.fetchall()
  773. if (not acl_data and i == (end - 1)) and get_ban == 1 and tool != 'render':
  774. return 1
  775. elif acl_data and acl_data[0][0] != 'normal' and acl_data[0][0] != '':
  776. if acl_data[0][0] != 'ban' and get_ban == 1 and tool != 'render':
  777. return 1
  778. if acl_data[0][0] == 'all' or acl_data[0][0] == 'ban':
  779. return 0
  780. elif acl_data[0][0] == 'user':
  781. if ip_or_user(ip) != 1:
  782. return 0
  783. elif acl_data[0][0] == 'admin':
  784. if ip_or_user(ip) != 1:
  785. if admin_check(num) == 1:
  786. return 0
  787. elif acl_data[0][0] == '50_edit':
  788. if ip_or_user(ip) != 1:
  789. if admin_check(num) == 1:
  790. return 0
  791. else:
  792. curs.execute(db_change("select count(title) from history where ip = ?"), [ip])
  793. count = curs.fetchall()
  794. count = count[0][0] if count else 0
  795. if count >= 50:
  796. return 0
  797. elif acl_data[0][0] == 'email':
  798. if ip_or_user(ip) != 1:
  799. if admin_check(num) == 1:
  800. return 0
  801. else:
  802. curs.execute(db_change("select data from user_set where id = ? and name = 'email'"), [ip])
  803. if curs.fetchall():
  804. return 0
  805. elif acl_data[0][0] == 'owner':
  806. if admin_check() == 1:
  807. return 0
  808. return 1
  809. else:
  810. if i == (end - 1):
  811. if tool == 'topic':
  812. if topic_num:
  813. curs.execute(db_change("select title from rd where code = ? and stop != ''"), [topic_num])
  814. if curs.fetchall():
  815. if admin_check(3, 'topic (code ' + topic_num + ')') == 1:
  816. return 0
  817. else:
  818. return 0
  819. else:
  820. return 0
  821. else:
  822. return 0
  823. return 1
  824. def ban_check(ip = None, tool = None):
  825. if not ip:
  826. ip = ip_check()
  827. if admin_check(None, None, ip) == 1:
  828. return 0
  829. band = re.search("^([0-9]{1,3}\.[0-9]{1,3})", ip)
  830. if band:
  831. band_it = band.group(1)
  832. else:
  833. band_it = '-'
  834. curs.execute(db_change("delete from ban where (end < ? and end like '2%')"), [get_time()])
  835. conn.commit()
  836. curs.execute(db_change("select login, block from ban where ((end > ? and end like '2%') or end = '') and band = 'regex'"), [get_time()])
  837. regex_d = curs.fetchall()
  838. for test_r in regex_d:
  839. g_regex = re.compile(test_r[1])
  840. if g_regex.search(ip):
  841. if tool and tool == 'login':
  842. if test_r[0] != 'O':
  843. return 1
  844. else:
  845. return 1
  846. curs.execute(db_change("select login from ban where ((end > ? and end like '2%') or end = '') and block = ? and band = 'O'"), [get_time(), band_it])
  847. band_d = curs.fetchall()
  848. if band_d:
  849. if tool and tool == 'login':
  850. if band_d[0][0] != 'O':
  851. return 1
  852. else:
  853. return 1
  854. curs.execute(db_change("select login from ban where ((end > ? and end like '2%') or end = '') and block = ? and band = ''"), [get_time(), ip])
  855. ban_d = curs.fetchall()
  856. if ban_d:
  857. if tool and tool == 'login':
  858. if ban_d[0][0] != 'O':
  859. return 1
  860. else:
  861. return 1
  862. return 0
  863. def ban_insert(name, end, why, login, blocker, type_d = None):
  864. now_time = get_time()
  865. if type_d:
  866. band = type_d
  867. else:
  868. if re.search("^([0-9]{1,3}\.[0-9]{1,3})$", name):
  869. band = 'O'
  870. else:
  871. band = ''
  872. curs.execute(db_change("delete from ban where (end < ? and end like '2%')"), [get_time()])
  873. curs.execute(db_change("select block from ban where ((end > ? and end like '2%') or end = '') and block = ? and band = ?"), [get_time(), name, band])
  874. if curs.fetchall():
  875. curs.execute(db_change("insert into rb (block, end, today, blocker, why, band) values (?, ?, ?, ?, ?, ?)"), [
  876. name,
  877. 'release',
  878. now_time,
  879. blocker,
  880. '',
  881. band
  882. ])
  883. curs.execute(db_change("delete from ban where block = ? and band = ?"), [name, band])
  884. else:
  885. if login != '':
  886. login = 'O'
  887. else:
  888. login = ''
  889. if end != '0':
  890. end = int(number_check(end))
  891. time = datetime.datetime.now()
  892. plus = datetime.timedelta(seconds = end)
  893. r_time = (time + plus).strftime("%Y-%m-%d %H:%M:%S")
  894. else:
  895. r_time = ''
  896. curs.execute(db_change("insert into rb (block, end, today, blocker, why, band) values (?, ?, ?, ?, ?, ?)"), [
  897. name,
  898. r_time,
  899. now_time,
  900. blocker,
  901. why,
  902. band
  903. ])
  904. curs.execute(db_change("insert into ban (block, end, why, band, login) values (?, ?, ?, ?, ?)"), [
  905. name,
  906. r_time,
  907. why,
  908. band,
  909. login
  910. ])
  911. conn.commit()
  912. def rd_plus(topic_num, date, name = None, sub = None):
  913. curs.execute(db_change("select code from rd where code = ?"), [topic_num])
  914. if curs.fetchall():
  915. curs.execute(db_change("update rd set date = ? where code = ?"), [date, topic_num])
  916. else:
  917. curs.execute(db_change("insert into rd (title, sub, code, date) values (?, ?, ?, ?)"), [name, sub, topic_num, date])
  918. conn.commit()
  919. def history_plus(title, data, date, ip, send, leng, t_check = '', d_type = ''):
  920. curs.execute(db_change("select id from history where title = ? and type = '' order by id + 0 desc limit 1"), [title])
  921. id_data = curs.fetchall()
  922. id_data = str(int(id_data[0][0]) + 1) if id_data else '1'
  923. if d_type != 'req':
  924. curs.execute(db_change("select title from history where title = ? and id = ? and type = 'req'"), [title, id_data])
  925. if curs.fetchall():
  926. curs.execute(db_change("update history set type = 'req_close' where title = ? and id = ? and type = 'req'"), [
  927. title,
  928. id_data
  929. ])
  930. send = re.sub('\(|\)|<|>', '', send)
  931. send = send[:128] if len(send) > 128 else send
  932. send = send + ' (' + t_check + ')' if t_check != '' else send
  933. curs.execute(db_change("insert into history (id, title, data, date, ip, send, leng, hide, type) values (?, ?, ?, ?, ?, ?, ?, '', ?)"), [
  934. id_data,
  935. title,
  936. data,
  937. date,
  938. ip,
  939. send,
  940. leng,
  941. d_type
  942. ])
  943. def leng_check(first, second):
  944. if first < second:
  945. all_plus = '+' + str(second - first)
  946. elif second < first:
  947. all_plus = '-' + str(first - second)
  948. else:
  949. all_plus = '0'
  950. return all_plus
  951. def number_check(data):
  952. try:
  953. int(data)
  954. return data
  955. except:
  956. return '1'
  957. def edit_filter_do(data):
  958. if admin_check(1) != 1:
  959. curs.execute(db_change("select regex, sub from filter where regex != ''"))
  960. for data_list in curs.fetchall():
  961. match = re.compile(data_list[0], re.I)
  962. if match.search(data):
  963. ban_insert(
  964. ip_check(),
  965. '0' if data_list[1] == 'X' else data_list[1],
  966. 'edit filter',
  967. None,
  968. 'tool:edit filter'
  969. )
  970. return 1
  971. return 0
  972. def redirect(data = '/'):
  973. return flask.redirect(data)
  974. def get_acl_list(type_d = None):
  975. acl_data = ['', 'all', 'user', 'admin', 'owner', '50_edit', 'email', 'ban']
  976. if type_d:
  977. if type_d == 'user':
  978. acl_data = ['', 'user', 'all']
  979. return acl_data
  980. def re_error(data):
  981. conn.commit()
  982. if data == '/ban':
  983. if ban_check() == 1:
  984. end = '<div id="get_user_info"></div><script>load_user_info("' + ip_check() + '");</script>'
  985. else:
  986. end = load_lang('authority_error')
  987. return easy_minify(flask.render_template(skin_check(),
  988. imp = [load_lang('error'), wiki_set(1), custom(), other2([0, 0])],
  989. data = '<h2>' + load_lang('error') + '</h2>' + end,
  990. menu = 0
  991. ))
  992. else:
  993. num = int(number_check(data.replace('/error/', '')))
  994. if num == 1:
  995. data = load_lang('no_login_error')
  996. elif num == 2:
  997. data = load_lang('no_exist_user_error')
  998. elif num == 3:
  999. data = load_lang('authority_error')
  1000. elif num == 4:
  1001. data = load_lang('no_admin_block_error')
  1002. elif num == 5:
  1003. data = load_lang('skin_error')
  1004. elif num == 6:
  1005. data = load_lang('same_id_exist_error')
  1006. elif num == 7:
  1007. data = load_lang('long_id_error')
  1008. elif num == 8:
  1009. data = load_lang('id_char_error') + ' <a href="/name_filter">(' + load_lang('id_filter_list') + ')</a>'
  1010. elif num == 9:
  1011. data = load_lang('file_exist_error')
  1012. elif num == 10:
  1013. data = load_lang('password_error')
  1014. elif num == 11:
  1015. data = load_lang('topic_long_error')
  1016. elif num == 12:
  1017. data = load_lang('email_error')
  1018. elif num == 13:
  1019. data = load_lang('recaptcha_error')
  1020. elif num == 14:
  1021. data = load_lang('file_extension_error') + ' <a href="/extension_filter">(' + load_lang('extension_filter_list') + ')</a>'
  1022. elif num == 15:
  1023. data = load_lang('edit_record_error')
  1024. elif num == 16:
  1025. data = load_lang('same_file_error')
  1026. elif num == 17:
  1027. data = load_lang('file_capacity_error') + wiki_set(3)
  1028. elif num == 19:
  1029. data = load_lang('decument_exist_error')
  1030. elif num == 20:
  1031. data = load_lang('password_diffrent_error')
  1032. elif num == 21:
  1033. data = load_lang('edit_filter_error')
  1034. elif num == 22:
  1035. data = load_lang('file_name_error')
  1036. elif num == 23:
  1037. data = load_lang('regex_error')
  1038. elif num == 24:
  1039. curs.execute(db_change("select data from other where name = 'slow_edit'"))
  1040. slow_data = curs.fetchall()
  1041. data = load_lang('fast_edit_error') + slow_data[0][0]
  1042. elif num == 25:
  1043. data = load_lang('too_many_dec_error')
  1044. elif num == 26:
  1045. data = load_lang('application_not_found')
  1046. elif num == 27:
  1047. data = load_lang("invalid_password_error")
  1048. elif num == 28:
  1049. data = load_lang('watchlist_overflow_error')
  1050. elif num == 29:
  1051. data = load_lang('copyright_disagreed')
  1052. else:
  1053. data = '???'
  1054. if num == 5:
  1055. get_url = flask.request.path
  1056. return easy_minify(flask.render_template(skin_check(),
  1057. imp = [(load_lang('skin_set') if get_url != '/main_skin_set' else load_lang('main_skin_set')), wiki_set(1), custom(), other2([0, 0])],
  1058. data = '' + \
  1059. '<div id="main_skin_set">' + \
  1060. '<h2>' + load_lang('error') + '</h2>' + \
  1061. '<ul>' + \
  1062. '<li>' + data + '</li>' + \
  1063. '</ul>' + \
  1064. '</div>' + \
  1065. ('<script>window.addEventListener(\'DOMContentLoaded\', function() { main_css_skin_set(); });</script>' if get_url == '/main_skin_set' else ''),
  1066. menu = ([['main_skin_set', load_lang('main_skin_set')]] if get_url != '/main_skin_set' else [['skin_set', load_lang('skin_set')]])
  1067. ))
  1068. else:
  1069. return easy_minify(flask.render_template(skin_check(),
  1070. imp = [load_lang('error'), wiki_set(1), custom(), other2([0, 0])],
  1071. data = '<h2>' + load_lang('error') + '</h2><ul><li>' + data + '</li></ul>',
  1072. menu = 0
  1073. )), 401