func.py 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700
  1. # Init
  2. import os
  3. import sys
  4. import platform
  5. import json
  6. import smtplib
  7. import zipfile
  8. import shutil
  9. import logging
  10. import random
  11. import email.mime.text
  12. import email.utils
  13. import email.header
  14. import urllib.request
  15. # Init-Version
  16. version_list = json.loads(open('version.json', encoding = 'utf8').read())
  17. print('Version : ' + version_list['beta']['r_ver'])
  18. print('DB set version : ' + version_list['beta']['c_ver'])
  19. print('Skin set version : ' + version_list['beta']['s_ver'])
  20. print('----')
  21. # Init-PIP_Install
  22. data_up_date = 1
  23. if os.path.exists(os.path.join('data', 'version.json')):
  24. data_load_ver = open(os.path.join('data', 'version.json'), encoding = 'utf8').read()
  25. if data_load_ver == version_list['beta']['r_ver']:
  26. data_up_date = 0
  27. if data_up_date == 1:
  28. with open(os.path.join('data', 'version.json'), 'w', encoding = 'utf8') as f:
  29. f.write(version_list['beta']['r_ver'])
  30. if platform.system() in ('Linux', 'Windows'):
  31. os.system(
  32. 'python' + ('3' if platform.system() != 'Windows' else '') + ' ' + \
  33. '-m pip install --upgrade --user -r requirements.txt'
  34. )
  35. print('----')
  36. try:
  37. os.execl(sys.executable, sys.executable, *sys.argv)
  38. except:
  39. pass
  40. try:
  41. os.execl(sys.executable, '"' + sys.executable + '"', *sys.argv)
  42. except:
  43. print('Error : restart failed')
  44. raise
  45. else:
  46. print('Error : automatic installation is not supported.')
  47. print('Help : try "python3 -m pip install -r requirements.txt"')
  48. else:
  49. print('PIP check pass')
  50. print('----')
  51. # Init-Load
  52. from .func_mark import *
  53. from diff_match_patch import diff_match_patch
  54. from gevent.pywsgi import WSGIServer
  55. import werkzeug.routing
  56. import werkzeug.debug
  57. import flask
  58. import requests
  59. import pymysql
  60. if sys.version_info < (3, 6):
  61. import sha3
  62. # Init-Global
  63. global_lang = {}
  64. global_wiki_set = {}
  65. global_db_set = ''
  66. data_css_ver = '116'
  67. data_css = ''
  68. conn = ''
  69. curs = ''
  70. # Func
  71. # Func-main
  72. def load_conn(data):
  73. global conn
  74. global curs
  75. conn = data
  76. curs = conn.cursor()
  77. load_conn2(data)
  78. # Func-init
  79. def get_conn(db_set = ''):
  80. global global_db_set
  81. if db_set != '':
  82. global_db_set = db_set
  83. else:
  84. db_set = global_db_set
  85. if db_set['type'] == 'sqlite':
  86. conn = sqlite3.connect(db_set['name'] + '.db')
  87. curs = conn.cursor()
  88. else:
  89. conn = pymysql.connect(
  90. host = db_set['mysql_host'],
  91. user = db_set['mysql_user'],
  92. password = db_set['mysql_pw'],
  93. charset = 'utf8mb4',
  94. port = int(db_set['mysql_port'])
  95. )
  96. curs = conn.cursor()
  97. try:
  98. curs.execute(db_change(
  99. 'create database ' + db_set['name'] + ' ' + \
  100. 'default character set utf8mb4;'
  101. ))
  102. except:
  103. pass
  104. conn.select_db(db_set['name'])
  105. load_conn(conn)
  106. return conn
  107. def update(ver_num, set_data):
  108. print('----')
  109. # 업데이트 하위 호환 유지 함수
  110. if ver_num < 3160027:
  111. print('Add init set')
  112. set_init()
  113. if ver_num < 3170002:
  114. curs.execute(db_change("select html from html_filter where kind = 'extension'"))
  115. if not curs.fetchall():
  116. for i in ['jpg', 'jpeg', 'png', 'gif', 'webp']:
  117. curs.execute(db_change(
  118. "insert into html_filter (html, kind) values (?, 'extension')"
  119. ), [i])
  120. if ver_num < 3170400:
  121. curs.execute(db_change("select title, sub, code from topic where id = '1'"))
  122. for i in curs.fetchall():
  123. curs.execute(db_change(
  124. "update topic set code = ? where title = ? and sub = ?"
  125. ), [
  126. i[2],
  127. i[0],
  128. i[1]
  129. ])
  130. curs.execute(db_change(
  131. "update rd set code = ? where title = ? and sub = ?"
  132. ), [
  133. i[2],
  134. i[0],
  135. i[1]
  136. ])
  137. if ver_num < 3171800:
  138. curs.execute(db_change("select data from other where name = 'recaptcha'"))
  139. change_rec = curs.fetchall()
  140. if change_rec and change_rec[0][0] != '':
  141. new_rec = re.search(r'data-sitekey="([^"]+)"', change_rec[0][0])
  142. if new_rec:
  143. curs.execute(db_change(
  144. "update other set data = ? where name = 'recaptcha'"
  145. ), [new_rec.group(1)])
  146. else:
  147. curs.execute(db_change("update other set data = '' where name = 'recaptcha'"))
  148. curs.execute(db_change("update other set data = '' where name = 'sec_re'"))
  149. if ver_num < 3172800 and \
  150. set_data['db_type'] == 'mysql':
  151. get_data_mysql = json.loads(open('data/mysql.json', encoding = 'utf8').read())
  152. with open('data/mysql.json', 'w') as f:
  153. f.write('{ "user" : "' + get_data_mysql['user'] + '", "password" : "' + get_data_mysql['password'] + '", "host" : "localhost" }')
  154. if ver_num < 3183603:
  155. curs.execute(db_change("select block from ban where band = 'O'"))
  156. for i in curs.fetchall():
  157. curs.execute(db_change(
  158. "update ban set block = ?, band = 'regex' where block = ? and band = 'O'"
  159. ), [
  160. '^' + i[0].replace('.', '\\.'),
  161. i[0]
  162. ])
  163. curs.execute(db_change("select block from rb where band = 'O'"))
  164. for i in curs.fetchall():
  165. curs.execute(db_change(
  166. "update rb set block = ?, band = 'regex' where block = ? and band = 'O'"
  167. ), [
  168. '^' + i[0].replace('.', '\\.'),
  169. i[0]
  170. ])
  171. if ver_num < 3190201:
  172. today_time = get_time()
  173. curs.execute(db_change("select block, end, why, band, login from ban"))
  174. for i in curs.fetchall():
  175. curs.execute(db_change(
  176. "insert into rb (block, end, today, why, band, login, ongoing) " + \
  177. "values (?, ?, ?, ?, ?, ?, ?)"
  178. ), [
  179. i[0],
  180. i[1],
  181. today_time,
  182. i[2],
  183. i[3],
  184. i[4],
  185. '1'
  186. ])
  187. if ver_num < 3191301:
  188. curs.execute(db_change('' + \
  189. 'select id, title, date from history ' + \
  190. 'where not title like "user:%" ' + \
  191. 'order by date desc ' + \
  192. 'limit 50' + \
  193. ''))
  194. data_list = curs.fetchall()
  195. for get_data in data_list:
  196. curs.execute(db_change(
  197. "insert into rc (id, title, date, type) values (?, ?, ?, 'normal')"
  198. ), [
  199. get_data[0],
  200. get_data[1],
  201. get_data[2]
  202. ])
  203. if ver_num < 3202400:
  204. curs.execute(db_change("select data from other where name = 'update'"))
  205. get_data = curs.fetchall()
  206. if get_data and get_data[0][0] == 'master':
  207. curs.execute(db_change("update other set data = 'beta' where name = 'update'"), [])
  208. if ver_num < 3202600:
  209. curs.execute(db_change("select name, regex, sub from filter"))
  210. for i in curs.fetchall():
  211. curs.execute(db_change(
  212. "insert into html_filter (html, kind, plus, plus_t) " + \
  213. "values (?, 'regex_filter', ?, ?)"
  214. ), [
  215. i[0],
  216. i[1],
  217. i[2]
  218. ])
  219. curs.execute(db_change("select title, link, icon from inter"))
  220. for i in curs.fetchall():
  221. curs.execute(db_change(
  222. "insert into html_filter (html, kind, plus, plus_t) " + \
  223. "values (?, 'inter_wiki', ?, ?)"), [
  224. i[0],
  225. i[1],
  226. i[2]
  227. ])
  228. if ver_num < 3203400:
  229. curs.execute(db_change("select user, css from custom"))
  230. for i in curs.fetchall():
  231. curs.execute(db_change(
  232. "insert into user_set (name, id, data) values ('custom_css', ?, ?)"
  233. ), [
  234. re.sub(r' \(head\)$', '', i[0]),
  235. i[1]
  236. ])
  237. if ver_num < 3205500:
  238. curs.execute(db_change("select title, decu, dis, view, why from acl"))
  239. for i in curs.fetchall():
  240. curs.execute(db_change(
  241. "insert into acl (title, data, type) values (?, ?, ?)"
  242. ), [i[0], i[1], 'decu'])
  243. curs.execute(db_change(
  244. "insert into acl (title, data, type) values (?, ?, ?)"
  245. ), [i[0], i[2], 'dis'])
  246. curs.execute(db_change(
  247. "insert into acl (title, data, type) values (?, ?, ?)"
  248. ), [i[0], i[3], 'view'])
  249. curs.execute(db_change(
  250. "insert into acl (title, data, type) values (?, ?, ?)"
  251. ), [i[0], i[4], 'why'])
  252. if ver_num < 3300101:
  253. # 캐시 초기화
  254. curs.execute(db_change('delete from cache_data'))
  255. if ver_num < 3300301:
  256. # regex_filter 오류 해결
  257. curs.execute(db_change(
  258. 'delete from html_filter where kind = "regex_filter" and html is null'
  259. ))
  260. if ver_num < 3302302:
  261. # user이랑 user_set 테이블의 통합
  262. curs.execute(db_change('select id, pw, acl, date, encode from user'))
  263. for i in curs.fetchall():
  264. curs.execute(db_change(
  265. "insert into user_set (name, id, data) values (?, ?, ?)"
  266. ), ['pw', i[0], i[1]])
  267. curs.execute(db_change(
  268. "insert into user_set (name, id, data) values (?, ?, ?)"
  269. ), ['acl', i[0], i[2]])
  270. curs.execute(db_change(
  271. "insert into user_set (name, id, data) values (?, ?, ?)"
  272. ), ['date', i[0], i[3]])
  273. curs.execute(db_change(
  274. "insert into user_set (name, id, data) values (?, ?, ?)"
  275. ), ['encode', i[0], i[4]])
  276. if ver_num < 3400101:
  277. # user_set이랑 user_application 테이블의 통합
  278. curs.execute(db_change('' + \
  279. 'select id, pw, date, encode, question, answer, ip, ua, email ' + \
  280. 'from user_application' + \
  281. ''))
  282. for i in curs.fetchall():
  283. sql_data = {}
  284. sql_data['id'] = i[0]
  285. sql_data['pw'] = i[1]
  286. sql_data['date'] = i[2]
  287. sql_data['encode'] = i[3]
  288. sql_data['question'] = i[4]
  289. sql_data['answer'] = i[5]
  290. sql_data['ip'] = i[6]
  291. sql_data['ua'] = i[7]
  292. sql_data['email'] = i[8]
  293. curs.execute(db_change(
  294. "insert into user_set (name, id, data) values (?, ?, ?)"
  295. ), ['application', i[0], json.dumps(sql_data)])
  296. conn.commit()
  297. print('Update completed')
  298. def set_init_always(ver_num):
  299. curs.execute(db_change('delete from other where name = "ver"'))
  300. curs.execute(db_change('insert into other (name, data) values ("ver", ?)'), [ver_num])
  301. curs.execute(db_change('delete from alist where name = "owner"'))
  302. curs.execute(db_change('insert into alist (name, acl) values ("owner", "owner")'))
  303. if not os.path.exists(load_image_url()):
  304. os.makedirs(load_image_url())
  305. conn.commit()
  306. def set_init():
  307. # 초기값 설정 함수
  308. curs.execute(db_change("select html from html_filter where kind = 'email'"))
  309. if not curs.fetchall():
  310. for i in ['naver.com', 'gmail.com', 'daum.net', 'kakao.com']:
  311. curs.execute(db_change(
  312. "insert into html_filter (html, kind, plus, plus_t) values (?, 'email', '', '')"
  313. ), [i])
  314. curs.execute(db_change("select html from html_filter where kind = 'extension'"))
  315. if not curs.fetchall():
  316. for i in ['jpg', 'jpeg', 'png', 'gif', 'webp']:
  317. curs.execute(db_change(
  318. "insert into html_filter (html, kind, plus, plus_t) values (?, 'extension', '', '')"
  319. ), [i])
  320. curs.execute(db_change(
  321. 'select data from other ' + \
  322. 'where name = "smtp_server" or name = "smtp_port" or name = "smtp_security"'
  323. ))
  324. if not curs.fetchall():
  325. for i in [
  326. ['smtp_server', 'smtp.gmail.com'],
  327. ['smtp_port', '587'],
  328. ['smtp_security', 'starttls']
  329. ]:
  330. curs.execute(db_change(
  331. "insert into other (name, data) values (?, ?)"
  332. ), [i[0], i[1]])
  333. curs.execute(db_change('select data from other where name = "key"'))
  334. rep_data = curs.fetchall()
  335. if not rep_data:
  336. curs.execute(db_change('insert into other (name, data) values ("key", ?)'), [load_random_key()])
  337. curs.execute(db_change('select data from other where name = "count_all_title"'))
  338. if not curs.fetchall():
  339. curs.execute(db_change('insert into other (name, data) values ("count_all_title", "0")'))
  340. conn.commit()
  341. # Func-simple
  342. def get_default_admin_group():
  343. return ['owner', 'ban']
  344. def load_image_url():
  345. curs.execute(db_change('select data from other where name = "image_where"'))
  346. image_where = curs.fetchall()
  347. image_where = image_where[0][0] if image_where else 'data/images'
  348. return image_where
  349. def http_warning():
  350. return '''
  351. <div id="http_warning_text"></div>
  352. <script>
  353. if(window.location.protocol !== 'https:') {
  354. document.getElementById('http_warning_text').innerHTML = "''' + \
  355. load_lang('http_warning') + \
  356. '''";
  357. document.getElementById('http_warning_text').style.margin = "10px 0px 0px 0px";
  358. }
  359. </script>
  360. '''
  361. def load_domain():
  362. curs.execute(db_change("select data from other where name = 'domain'"))
  363. domain = curs.fetchall()
  364. domain = domain[0][0] if domain and domain[0][0] != '' else flask.request.host_url
  365. return domain
  366. def load_random_key(long = 64):
  367. return ''.join(
  368. random.choice(
  369. "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
  370. ) for i in range(long)
  371. )
  372. def edit_button(editor_display = '0'):
  373. insert_list = []
  374. curs.execute(db_change("select html, plus from html_filter where kind = 'edit_top'"))
  375. db_data = curs.fetchall()
  376. for get_data in db_data:
  377. insert_list += [[get_data[1], get_data[0]]]
  378. data = ''
  379. for insert_data in insert_list:
  380. data += '' + \
  381. '<a href="' + \
  382. 'javascript:do_insert_data(\'textarea_edit_view\', \'' + insert_data[0] + '\', ' + editor_display + ')' + \
  383. '">(' + insert_data[1] + ')</a> ' + \
  384. ''
  385. data += (' ' if data != '' else '') + '<a href="/edit_top">(' + load_lang('add') + ')</a>'
  386. data += '<hr class="main_hr">'
  387. return data
  388. def ip_warning():
  389. if ip_or_user() != 0:
  390. curs.execute(db_change('select data from other where name = "no_login_warning"'))
  391. data = curs.fetchall()
  392. if data and data[0][0] != '':
  393. text_data = '' + \
  394. '<span>' + data[0][0] + '</span>' + \
  395. '<hr class="main_hr">' + \
  396. ''
  397. else:
  398. text_data = '' + \
  399. '<span>' + load_lang('no_login_warning') + '</span>' + \
  400. '<hr class="main_hr">' + \
  401. ''
  402. else:
  403. text_data = ''
  404. return text_data
  405. def next_fix(link, num, page, end = 50):
  406. list_data = ''
  407. if num == 1:
  408. if len(page) == end:
  409. list_data += '' + \
  410. '<hr class="main_hr">' + \
  411. '<a href="' + link + str(num + 1) + '">(' + load_lang('next') + ')</a>' + \
  412. ''
  413. elif len(page) != end:
  414. list_data += '' + \
  415. '<hr class="main_hr">' + \
  416. '<a href="' + link + str(num - 1) + '">(' + load_lang('previous') + ')</a>' + \
  417. ''
  418. else:
  419. list_data += '' + \
  420. '<hr class="main_hr">' + \
  421. '<a href="' + link + str(num - 1) + '">(' + load_lang('previous') + ')</a> <a href="' + link + str(num + 1) + '">(' + load_lang('next') + ')</a>' + \
  422. ''
  423. return list_data
  424. def leng_check(A, B):
  425. # B -> new
  426. # A -> old
  427. return '0' if A == B else (('-' + str(A - B)) if A > B else ('+' + str(B - A)))
  428. def number_check(data):
  429. try:
  430. int(data)
  431. return data
  432. except:
  433. return '1'
  434. def redirect(data = '/'):
  435. return flask.redirect(flask.request.host_url[:-1] + data)
  436. def get_acl_list(type_d = 'normal'):
  437. if type_d == 'user':
  438. return ['', 'user', 'all']
  439. else:
  440. return ['', 'all', 'user', 'admin', 'owner', '50_edit', 'email', 'ban', 'before', '30_day', 'ban_admin']
  441. # Func-login
  442. def pw_encode(data, type_d = ''):
  443. if type_d == '':
  444. curs.execute(db_change('select data from other where name = "encode"'))
  445. set_data = curs.fetchall()
  446. type_d = set_data[0][0]
  447. if type_d == 'sha256':
  448. return hashlib.sha256(bytes(data, 'utf-8')).hexdigest()
  449. else:
  450. if sys.version_info < (3, 6):
  451. return sha3.sha3_256(bytes(data, 'utf-8')).hexdigest()
  452. else:
  453. return hashlib.sha3_256(bytes(data, 'utf-8')).hexdigest()
  454. def pw_check(data, data2, type_d = 'no', id_d = ''):
  455. curs.execute(db_change('select data from other where name = "encode"'))
  456. db_data = curs.fetchall()
  457. if type_d != 'no':
  458. if type_d == '':
  459. set_data = 'sha3'
  460. else:
  461. set_data = type_d
  462. else:
  463. set_data = db_data[0][0]
  464. re_data = 1 if pw_encode(data, set_data) == data2 else 0
  465. if db_data[0][0] != set_data and re_data == 1 and id_d != '':
  466. curs.execute(db_change("update user_set set data = ? where id = ? and name = 'pw'"), [
  467. pw_encode(data),
  468. id_d
  469. ])
  470. curs.execute(db_change("update user_set set data = ? where id = ? and name = 'encode'"), [
  471. db_data[0][0],
  472. id_d
  473. ])
  474. return re_data
  475. # Func-skin
  476. def easy_minify(data, tool = None):
  477. return data
  478. def load_lang(data, safe = 0):
  479. global global_lang
  480. ip = ip_check()
  481. if ip_or_user(ip) == 0:
  482. curs.execute(db_change('select data from user_set where name = "lang" and id = ?'), [ip])
  483. rep_data = curs.fetchall()
  484. elif 'lang' in flask.session:
  485. rep_data = [[flask.session['lang']]]
  486. else:
  487. curs.execute(db_change("select data from other where name = 'language'"))
  488. rep_data = curs.fetchall()
  489. if not rep_data or rep_data[0][0] in ('', 'default'):
  490. curs.execute(db_change("select data from other where name = 'language'"))
  491. rep_data = curs.fetchall()
  492. if rep_data:
  493. lang_name = rep_data[0][0]
  494. else:
  495. lang_name = 'en-US'
  496. if lang_name in global_lang:
  497. lang = global_lang[lang_name]
  498. else:
  499. lang_list = os.listdir('lang')
  500. if (lang_name + '.json') in lang_list:
  501. lang = json.loads(open(
  502. os.path.join('lang', lang_name + '.json'),
  503. encoding = 'utf8'
  504. ).read())
  505. global_lang[lang_name] = lang
  506. else:
  507. lang = {}
  508. if data in lang:
  509. if safe == 1:
  510. return lang[data]
  511. else:
  512. return html.escape(lang[data])
  513. return html.escape(data + ' (' + lang_name + ')')
  514. def skin_check(set_n = 0):
  515. # 개편 필요?
  516. skin_list = load_skin('tenshi', 1)
  517. skin = skin_list[0]
  518. ip = ip_check()
  519. user_need_skin = ''
  520. if ip_or_user(ip) == 0:
  521. curs.execute(db_change('select data from user_set where name = "skin" and id = ?'), [ip])
  522. skin_exist = curs.fetchall()
  523. if skin_exist:
  524. user_need_skin = skin_exist[0][0]
  525. else:
  526. if 'skin' in flask.session:
  527. user_need_skin = flask.session['skin']
  528. if user_need_skin == '':
  529. curs.execute(db_change('select data from other where name = "skin"'))
  530. skin_exist = curs.fetchall()
  531. if skin_exist:
  532. user_need_skin = skin_exist[0][0]
  533. if user_need_skin != '' and user_need_skin in skin_list:
  534. skin = user_need_skin
  535. if set_n == 0:
  536. return './views/' + skin + '/index.html'
  537. else:
  538. return skin
  539. def wiki_css(data):
  540. global data_css
  541. global data_css_ver
  542. data += ['' for _ in range(0, 3 - len(data))]
  543. if data_css == '':
  544. for i_data in os.listdir(os.path.join("views", "main_css", "css")):
  545. if i_data != 'sub':
  546. data_css += '<link rel="stylesheet" href="/views/main_css/css/' + i_data + '?ver=' + data_css_ver + '">'
  547. for i_data in os.listdir(os.path.join("views", "main_css", "js")):
  548. if i_data != 'sub':
  549. data_css += '<script src="/views/main_css/js/' + i_data + '?ver=' + data_css_ver + '"></script>'
  550. data = data[0:2] + ['', '''
  551. <link rel="stylesheet"
  552. href="https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.2.0/build/styles/default.min.css">
  553. <script src="https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.2.0/build/highlight.min.js"></script>
  554. <link defer rel="stylesheet"
  555. href="https://cdn.jsdelivr.net/npm/katex@0.13.2/dist/katex.min.css"
  556. integrity="sha384-Cqd8ihRLum0CCg8rz0hYKPoLZ3uw+gES2rXQXycqnL5pgVQIflxAUDS7ZSjITLb5"
  557. crossorigin="anonymous">
  558. <script src="https://cdn.jsdelivr.net/npm/katex@0.13.2/dist/katex.min.js"
  559. integrity="sha384-1Or6BdeNQb0ezrmtGeqQHFpppNd7a/gw29xeiSikBbsb44xu3uAo8c7FwbF5jhbd"
  560. crossorigin="anonymous"></script>
  561. ''' + data_css + '<script>window.addEventListener(\'DOMContentLoaded\', main_css_skin_load);</script>'] + data[2:]
  562. return data
  563. def cut_100(data):
  564. data = re.search(r'<pre style="display: none;" id="render_content_load">([^<>]+)<\/pre>', data)
  565. if data:
  566. data = data.group(1)
  567. if len(data) > 100:
  568. return data[0:100] + '...'
  569. else:
  570. return data[0:len(data)]
  571. else:
  572. return ''
  573. def wiki_set(num = 1):
  574. if num == 1:
  575. skin_name = skin_check(1)
  576. data_list = []
  577. curs.execute(db_change('select data from other where name = ?'), ['name'])
  578. db_data = curs.fetchall()
  579. data_list += [db_data[0][0]] if db_data and db_data[0][0] != '' else ['Wiki']
  580. curs.execute(db_change('select data from other where name = "license"'))
  581. db_data = curs.fetchall()
  582. data_list += [db_data[0][0]] if db_data and db_data[0][0] != '' else ['ARR']
  583. data_list += ['', '']
  584. curs.execute(db_change('select data from other where name = "logo" and coverage = ?'), [skin_name])
  585. db_data = curs.fetchall()
  586. if db_data and db_data[0][0] != '':
  587. data_list += [db_data[0][0]]
  588. else:
  589. curs.execute(db_change('select data from other where name = "logo" and coverage = ""'))
  590. db_data = curs.fetchall()
  591. data_list += [db_data[0][0]] if db_data and db_data[0][0] != '' else [data_list[0]]
  592. head_data = ''
  593. curs.execute(db_change("select data from other where name = 'head' and coverage = ''"))
  594. db_data = curs.fetchall()
  595. head_data += db_data[0][0] if db_data and db_data[0][0] != '' else ''
  596. curs.execute(db_change("select data from other where name = 'head' and coverage = ?"), [skin_name])
  597. db_data = curs.fetchall()
  598. head_data += db_data[0][0] if db_data and db_data[0][0] != '' else ''
  599. data_list += [head_data]
  600. elif num == 2:
  601. curs.execute(db_change('select data from other where name = "frontpage"'))
  602. db_data = curs.fetchall()
  603. data_list = db_data[0][0] if db_data and db_data[0][0] != '' else 'FrontPage'
  604. elif num == 3:
  605. curs.execute(db_change('select data from other where name = "upload"'))
  606. db_data = curs.fetchall()
  607. data_list = db_data[0][0] if db_data and db_data[0][0] != '' else '2'
  608. else:
  609. data_list = ''
  610. return data_list
  611. def wiki_custom():
  612. ip = ip_check()
  613. if ip_or_user(ip) == 0:
  614. user_icon = 1
  615. user_name = ip
  616. curs.execute(db_change("select data from user_set where id = ? and name = 'custom_css'"), [ip])
  617. user_head = curs.fetchall()
  618. user_head = user_head[0][0] if user_head else ''
  619. curs.execute(db_change('select data from user_set where name = "email" and id = ?'), [ip])
  620. email = curs.fetchall()
  621. email = email[0][0] if email else ''
  622. if admin_check('all') == 1:
  623. user_admin = '1'
  624. user_acl_list = []
  625. curs.execute(db_change("select data from user_set where id = ? and name = 'acl'"), [ip])
  626. curs.execute(db_change('select acl from alist where name = ?'), [curs.fetchall()[0][0]])
  627. user_acl = curs.fetchall()
  628. for i in user_acl:
  629. user_acl_list += [i[0]]
  630. user_acl_list = user_acl_list if user_acl != [] else '0'
  631. else:
  632. user_admin = '0'
  633. user_acl_list = '0'
  634. curs.execute(db_change("select count(*) from alarm where name = ?"), [ip])
  635. count = curs.fetchall()
  636. user_notice = str(count[0][0]) if count else '0'
  637. else:
  638. user_icon = 0
  639. user_name = load_lang('user')
  640. email = ''
  641. user_admin = '0'
  642. user_acl_list = '0'
  643. user_notice = '0'
  644. user_head = flask.session['head'] if 'head' in flask.session else ''
  645. curs.execute(db_change("select title from rd where title = ? and stop = ''"), ['user:' + ip])
  646. user_topic = '1' if curs.fetchall() else '0'
  647. split_path = flask.request.path.split('/')
  648. if len(split_path) > 1:
  649. split_path = split_path[1]
  650. else:
  651. split_path = 0
  652. return [
  653. '',
  654. '',
  655. user_icon,
  656. user_head,
  657. email,
  658. user_name,
  659. user_admin,
  660. str(ban_check()),
  661. user_notice,
  662. user_acl_list,
  663. ip,
  664. user_topic,
  665. split_path
  666. ]
  667. def load_skin(data = '', set_n = 0, default = 0):
  668. # data -> 가장 앞에 있을 스킨 이름
  669. # set_n == 0 -> 스트링으로 반환
  670. # set_n == 1 -> 리스트로 반환
  671. # default == 0 -> 디폴트 미포함
  672. # default == 1 -> 디폴트 포함
  673. if set_n == 0:
  674. skin_return_data = ''
  675. else:
  676. skin_return_data = []
  677. skin_list_get = os.listdir('views')
  678. if default == 1:
  679. skin_list_get = ['default'] + skin_list_get
  680. for skin_data in skin_list_get:
  681. if skin_data != 'default':
  682. see_data = skin_data
  683. else:
  684. see_data = load_lang('default')
  685. if skin_data != 'main_css':
  686. if set_n == 0:
  687. if skin_data == data:
  688. skin_return_data = '' + \
  689. '<option value="' + skin_data + '">' + \
  690. see_data + \
  691. '</option>' + \
  692. '' + skin_return_data
  693. else:
  694. skin_return_data += '' + \
  695. '<option value="' + skin_data + '">' + \
  696. see_data + \
  697. '</option>' + \
  698. ''
  699. else:
  700. if skin_data == data:
  701. skin_return_data = [skin_data] + skin_return_data
  702. else:
  703. skin_return_data += [skin_data]
  704. return skin_return_data
  705. # Func-markup
  706. def render_set(doc_name = '', doc_data = '', data_type = 'view', data_in = '', doc_acl = ''):
  707. # data_type in ['view', 'raw', 'api_view', 'backlink']
  708. doc_acl = acl_check(doc_name, 'render') if doc_acl == '' else doc_acl
  709. doc_data = 0 if doc_data == None else doc_data
  710. if doc_acl == 1:
  711. return 'HTTP Request 401.3'
  712. else:
  713. if data_type == 'raw':
  714. return doc_data
  715. else:
  716. if doc_data != 0:
  717. return render_do(doc_name, doc_data, data_type, data_in)
  718. else:
  719. return 'HTTP Request 404'
  720. # Func-request
  721. def send_email(who, title, data):
  722. try:
  723. curs.execute(db_change('' + \
  724. 'select name, data from other ' + \
  725. 'where name = "smtp_email" or name = "smtp_pass" or name = "smtp_server" or name = "smtp_port" or name = "smtp_security"' + \
  726. ''))
  727. rep_data = curs.fetchall()
  728. smtp_email = ''
  729. smtp_pass = ''
  730. smtp_server = ''
  731. smtp_security = ''
  732. smtp_port = ''
  733. smtp = ''
  734. for i in rep_data:
  735. if i[0] == 'smtp_email':
  736. smtp_email = i[1]
  737. elif i[0] == 'smtp_pass':
  738. smtp_pass = i[1]
  739. elif i[0] == 'smtp_server':
  740. smtp_server = i[1]
  741. elif i[0] == 'smtp_security':
  742. smtp_security = i[1]
  743. elif i[0] == 'smtp_port':
  744. smtp_port = i[1]
  745. smtp_port = int(smtp_port)
  746. if smtp_security == 'plain':
  747. smtp = smtplib.SMTP(smtp_server, smtp_port)
  748. elif smtp_security == 'starttls':
  749. smtp = smtplib.SMTP(smtp_server, smtp_port)
  750. smtp.starttls()
  751. else:
  752. # if smtp_security == 'tls':
  753. smtp = smtplib.SMTP_SSL(smtp_server, smtp_port)
  754. smtp.login(smtp_email, smtp_pass)
  755. domain = load_domain()
  756. wiki_name = wiki_set()[0]
  757. msg = email.mime.text.MIMEText(data)
  758. msg['Subject'] = title
  759. msg['From'] = 'openNAMU <noreply@' + domain + '>'
  760. msg['To'] = who
  761. smtp.sendmail('openNAMU@' + domain, who, msg.as_string())
  762. smtp.quit()
  763. return 1
  764. except Exception as e:
  765. print('----')
  766. print('Error : email send error')
  767. print(e)
  768. return 0
  769. def captcha_get():
  770. data = ''
  771. if ip_or_user() != 0:
  772. curs.execute(db_change('select data from other where name = "recaptcha"'))
  773. recaptcha = curs.fetchall()
  774. curs.execute(db_change('select data from other where name = "sec_re"'))
  775. sec_re = curs.fetchall()
  776. curs.execute(db_change('select data from other where name = "recaptcha_ver"'))
  777. rec_ver = curs.fetchall()
  778. if recaptcha and recaptcha[0][0] != '' and \
  779. sec_re and sec_re[0][0] != '':
  780. if not rec_ver or rec_ver[0][0] == '':
  781. data += '' + \
  782. '<script src="https://www.google.com/recaptcha/api.js" async defer></script>' + \
  783. '<div class="g-recaptcha" data-sitekey="' + recaptcha[0][0] + '"></div>' + \
  784. '<hr class="main_hr">' + \
  785. ''
  786. else:
  787. data += '' + \
  788. '<script src="https://www.google.com/recaptcha/api.js?render=' + recaptcha[0][0] + '"></script>' + \
  789. '<input type="hidden" id="g-recaptcha" name="g-recaptcha">' + \
  790. '<script type="text/javascript">' + \
  791. 'grecaptcha.ready(function() {' + \
  792. 'grecaptcha.execute(\'' + recaptcha[0][0] + '\', {action: \'homepage\'}).then(function(token) {' + \
  793. 'document.getElementById(\'g-recaptcha\').value = token;' + \
  794. '});' + \
  795. '});' + \
  796. '</script>' + \
  797. ''
  798. return data
  799. def captcha_post(re_data, num = 1):
  800. if num == 1:
  801. curs.execute(db_change('select data from other where name = "sec_re"'))
  802. sec_re = curs.fetchall()
  803. if sec_re and sec_re[0][0] != '' and \
  804. ip_or_user() != 0 and captcha_get() != '':
  805. data = requests.get(
  806. 'https://www.google.com/recaptcha/api/siteverify' + \
  807. '?secret=' + sec_re[0][0] + '&response=' + re_data
  808. )
  809. if data.status_code == 200:
  810. json_data = json.loads(data.text)
  811. if json_data['success'] != True:
  812. return 1
  813. return 0
  814. # Func-user
  815. def ip_or_user(data = ''):
  816. # 1 == ip
  817. # 0 == reg
  818. if data == '':
  819. data = ip_check()
  820. if re.search(r'(\.|:)', data):
  821. return 1
  822. else:
  823. return 0
  824. def admin_check(num = None, what = None, name = ''):
  825. ip = ip_check() if name == '' else name
  826. time_data = get_time()
  827. pass_ok = 0
  828. if ip_or_user(ip) == 0:
  829. curs.execute(db_change(
  830. "select data from user_set where id = ? and name = 'acl'"
  831. ), [ip])
  832. user_auth = curs.fetchall()
  833. if user_auth:
  834. user_auth = user_auth[0][0]
  835. check = {
  836. 0 : 'owner',
  837. 1 : 'ban',
  838. 2 : 'nothing',
  839. 3 : 'toron',
  840. 4 : 'check',
  841. 5 : 'acl',
  842. 6 : 'hidel',
  843. 7 : 'give'
  844. }
  845. if not num:
  846. check = check[0]
  847. elif num == 'all':
  848. check = [check[i] for i in check]
  849. else:
  850. check = check[num]
  851. curs.execute(db_change(
  852. 'select name from alist where name = ? and acl = "owner"'
  853. ), [user_auth])
  854. if curs.fetchall():
  855. pass_ok = 1
  856. else:
  857. if num == 'all':
  858. curs.execute(db_change(
  859. 'select name from alist where name = ?'
  860. ), [user_auth])
  861. else:
  862. curs.execute(db_change(
  863. 'select name from alist where name = ? and acl = ?'
  864. ), [user_auth, check])
  865. if curs.fetchall():
  866. pass_ok = 1
  867. if pass_ok == 1:
  868. if what:
  869. curs.execute(db_change(
  870. "insert into re_admin (who, what, time) values (?, ?, ?)"
  871. ), [ip, what, time_data])
  872. conn.commit()
  873. return 1
  874. return 0
  875. def acl_check(name = 'test', tool = '', topic_num = '1'):
  876. ip = ip_check()
  877. get_ban = ban_check()
  878. acl_c = re.search(r"^user:((?:(?!\/).)*)", name) if name else None
  879. if tool == '' and acl_c:
  880. acl_n = acl_c.groups()
  881. if get_ban == 1:
  882. return 1
  883. if admin_check(5) == 1:
  884. return 0
  885. curs.execute(db_change(
  886. "select data from acl where title = ? and type = 'decu'"
  887. ), ['user:' + acl_n[0]])
  888. acl_data = curs.fetchall()
  889. if acl_data:
  890. if acl_data[0][0] == 'all':
  891. return 0
  892. elif acl_data[0][0] == 'user' and not ip_or_user(ip) == 1:
  893. return 0
  894. elif ip == acl_n[0] and not ip_or_user(ip) == 1:
  895. return 0
  896. else:
  897. if ip == acl_n[0] and not ip_or_user(ip) == 1 and not ip_or_user(acl_n[0]) == 1:
  898. return 0
  899. return 1
  900. if tool == 'topic':
  901. if not name:
  902. curs.execute(db_change("select title from rd where code = ?"), [topic_num])
  903. name = curs.fetchall()
  904. name = name[0][0] if name else 'test'
  905. end = 3
  906. elif tool == 'render' or tool == '' or tool == 'vote':
  907. if tool == '' and acl_check(name, 'render') == 1:
  908. return 1
  909. end = 2
  910. else:
  911. end = 1
  912. for i in range(0, end):
  913. if tool == '':
  914. if i == 0:
  915. curs.execute(db_change(
  916. "select data from acl where title = ? and type = 'decu'"
  917. ), [name])
  918. else:
  919. curs.execute(db_change('select data from other where name = "edit"'))
  920. num = 5
  921. elif tool == 'topic':
  922. if i == 0 and topic_num:
  923. curs.execute(db_change("select acl from rd where code = ?"), [topic_num])
  924. elif i == 1:
  925. curs.execute(db_change(
  926. "select data from acl where title = ? and type = 'dis'"
  927. ), [name])
  928. else:
  929. curs.execute(db_change('select data from other where name = "discussion"'))
  930. num = 3
  931. elif tool == 'upload':
  932. curs.execute(db_change("select data from other where name = 'upload_acl'"))
  933. num = 5
  934. elif tool == 'many_upload':
  935. curs.execute(db_change("select data from other where name = 'many_upload_acl'"))
  936. num = 5
  937. elif tool == 'vote':
  938. if i == 0:
  939. curs.execute(db_change(
  940. 'select acl from vote where id = ? and user = ""'
  941. ), [topic_num])
  942. else:
  943. curs.execute(db_change('select data from other where name = "vote_acl"'))
  944. num = None
  945. else:
  946. # tool == 'render'
  947. if i == 0:
  948. curs.execute(db_change(
  949. "select data from acl where title = ? and type = 'view'"
  950. ), [name])
  951. else:
  952. curs.execute(db_change("select data from other where name = 'all_view_acl'"))
  953. num = 5
  954. acl_data = curs.fetchall()
  955. if (
  956. i == (end - 1) and \
  957. (not acl_data or acl_data[0][0] == '' or acl_data[0][0] == 'normal')
  958. ) and \
  959. get_ban == 1 and \
  960. tool != 'render':
  961. return 1
  962. elif acl_data and acl_data[0][0] != 'normal' and acl_data[0][0] != '':
  963. if acl_data[0][0] != 'ban' and get_ban == 1 and tool != 'render':
  964. return 1
  965. if acl_data[0][0] == 'all' or acl_data[0][0] == 'ban':
  966. return 0
  967. elif acl_data[0][0] == 'user':
  968. if ip_or_user(ip) != 1:
  969. return 0
  970. elif acl_data[0][0] == 'admin':
  971. if ip_or_user(ip) != 1:
  972. if admin_check(num) == 1:
  973. return 0
  974. elif acl_data[0][0] == '50_edit':
  975. if ip_or_user(ip) != 1:
  976. if admin_check(num) == 1:
  977. return 0
  978. else:
  979. curs.execute(db_change(
  980. "select count(*) from history where ip = ?"
  981. ), [ip])
  982. count = curs.fetchall()
  983. count = count[0][0] if count else 0
  984. if count >= 50:
  985. return 0
  986. elif acl_data[0][0] == 'before':
  987. if ip_or_user(ip) != 1:
  988. if admin_check(num) == 1:
  989. return 0
  990. curs.execute(db_change(
  991. "select ip from history where title = ? and ip = ?"
  992. ), [name, ip])
  993. if curs.fetchall():
  994. return 0
  995. elif acl_data[0][0] == '30_day':
  996. if ip_or_user(ip) != 1:
  997. if admin_check(num) == 1:
  998. return 0
  999. else:
  1000. curs.execute(db_change(
  1001. "select data from user_set where id = ? and name = 'date'"
  1002. ), [ip])
  1003. user_date = curs.fetchall()[0][0]
  1004. time_1 = datetime.datetime.strptime(
  1005. user_date,
  1006. '%Y-%m-%d %H:%M:%S'
  1007. ) + datetime.timedelta(days = 30)
  1008. time_2 = datetime.datetime.strptime(
  1009. get_time(),
  1010. '%Y-%m-%d %H:%M:%S'
  1011. )
  1012. if time_2 > time_1:
  1013. return 0
  1014. elif acl_data[0][0] == 'email':
  1015. if ip_or_user(ip) != 1:
  1016. if admin_check(num) == 1:
  1017. return 0
  1018. else:
  1019. curs.execute(db_change(
  1020. "select data from user_set where id = ? and name = 'email'"
  1021. ), [ip])
  1022. if curs.fetchall():
  1023. return 0
  1024. elif acl_data[0][0] == 'owner':
  1025. if admin_check() == 1:
  1026. return 0
  1027. elif acl_data[0][0] == 'ban_admin':
  1028. if admin_check(1) == 1 or ban_check() == 1:
  1029. return 0
  1030. return 1
  1031. else:
  1032. if i == (end - 1):
  1033. if tool == 'topic' and topic_num:
  1034. curs.execute(db_change(
  1035. "select title from rd where code = ? and stop != ''"
  1036. ), [topic_num])
  1037. if curs.fetchall():
  1038. if admin_check(3, 'topic (code ' + topic_num + ')') == 1:
  1039. return 0
  1040. else:
  1041. return 0
  1042. else:
  1043. return 0
  1044. return 1
  1045. def ban_check(ip = None, tool = ''):
  1046. ip = ip_check() if not ip else ip
  1047. tool = '' if not tool else tool
  1048. if admin_check(None, None, ip) == 1:
  1049. return 0
  1050. curs.execute(db_change(
  1051. "update rb set ongoing = '' " + \
  1052. "where end < ? and end != '' and ongoing = '1'"
  1053. ), [get_time()])
  1054. conn.commit()
  1055. curs.execute(db_change("" + \
  1056. "select login, block from rb " + \
  1057. "where band = 'regex' and ongoing = '1'" + \
  1058. ""))
  1059. regex_d = curs.fetchall()
  1060. for test_r in regex_d:
  1061. g_regex = re.compile(test_r[1])
  1062. if g_regex.search(ip):
  1063. if tool == 'login':
  1064. if test_r[0] != 'O':
  1065. return 1
  1066. else:
  1067. return 1
  1068. curs.execute(db_change("" + \
  1069. "select login from rb " + \
  1070. "where block = ? and band = '' and ongoing = '1'" + \
  1071. "" + \
  1072. ""), [ip])
  1073. ban_d = curs.fetchall()
  1074. if ban_d:
  1075. if tool == 'login':
  1076. if ban_d[0][0] != 'O':
  1077. return 1
  1078. else:
  1079. return 1
  1080. return 0
  1081. def ip_pas(raw_ip, type_d = 0):
  1082. hide = 0
  1083. end_ip = {}
  1084. i = 0
  1085. return_data = 0
  1086. if type(raw_ip) != type([]):
  1087. get_ip = [raw_ip]
  1088. return_data = 1
  1089. else:
  1090. get_ip = raw_ip
  1091. curs.execute(db_change("select data from other where name = 'ip_view'"))
  1092. ip_view = curs.fetchall()
  1093. ip_view = ip_view[0][0] if ip_view else ''
  1094. ip_view = '' if admin_check(1) == 1 else ip_view
  1095. get_ip = list(set(get_ip))
  1096. for raw_ip in get_ip:
  1097. change_ip = 0
  1098. is_this_ip = ip_or_user(raw_ip)
  1099. if is_this_ip != 0 and ip_view != '':
  1100. ip = re.sub(r'\.([^.]*)\.([^.]*)$', '.*.*', raw_ip)
  1101. ip = re.sub(r':([^:]*):([^:]*)$', ':*:*', ip)
  1102. change_ip = 1
  1103. else:
  1104. ip = raw_ip
  1105. if type_d == 0:
  1106. if is_this_ip == 0:
  1107. ip = '<a href="/w/' + url_pas('user:' + raw_ip) + '">' + raw_ip + '</a>'
  1108. if change_ip == 0:
  1109. ip += ' <a href="/user/' + url_pas(raw_ip) + '">(' + load_lang('tool') + ')</a>'
  1110. end_ip[raw_ip] = ip
  1111. if return_data == 1:
  1112. return end_ip[raw_ip]
  1113. else:
  1114. return end_ip
  1115. # Func-edit
  1116. def slow_edit_check():
  1117. curs.execute(db_change("select data from other where name = 'slow_edit'"))
  1118. slow_edit = curs.fetchall()
  1119. if slow_edit and slow_edit != '0' and admin_check(5) != 1:
  1120. slow_edit = slow_edit[0][0]
  1121. curs.execute(db_change(
  1122. "select date from history where ip = ? order by date desc limit 1"
  1123. ), [ip_check()])
  1124. last_edit_data = curs.fetchall()
  1125. if last_edit_data:
  1126. last_edit_data = int(re.sub(' |:|-', '', last_edit_data[0][0]))
  1127. now_edit_data = int(
  1128. (datetime.datetime.now() - datetime.timedelta(
  1129. seconds = int(slow_edit))
  1130. ).strftime("%Y%m%d%H%M%S")
  1131. )
  1132. if last_edit_data > now_edit_data:
  1133. return 1
  1134. return 0
  1135. def edit_filter_do(data):
  1136. if admin_check(1) != 1:
  1137. curs.execute(db_change(
  1138. "select plus, plus_t from html_filter where kind = 'regex_filter' and plus != ''"
  1139. ))
  1140. for data_list in curs.fetchall():
  1141. match = re.compile(data_list[0], re.I)
  1142. if match.search(data):
  1143. ban_insert(
  1144. ip_check(),
  1145. '0' if data_list[1] == 'X' else data_list[1],
  1146. 'edit filter',
  1147. None,
  1148. 'tool:edit filter'
  1149. )
  1150. return 1
  1151. return 0
  1152. # Func-insert
  1153. def add_alarm(who, context):
  1154. curs.execute(db_change(
  1155. 'insert into alarm (name, data, date) values (?, ?, ?)'
  1156. ), [who, context, get_time()])
  1157. def add_user(user_name, user_pw, user_email = '', user_encode = ''):
  1158. if user_encode == '':
  1159. user_pw_hash = pw_encode(user_pw)
  1160. curs.execute(db_change('select data from other where name = "encode"'))
  1161. data_encode = curs.fetchall()
  1162. data_encode = data_encode[0][0]
  1163. else:
  1164. user_pw_hash = user_pw
  1165. data_encode = user_encode
  1166. curs.execute(db_change("select id from user_set limit 1"))
  1167. if not curs.fetchall():
  1168. user_auth = 'owner'
  1169. else:
  1170. user_auth = 'user'
  1171. curs.execute(db_change("insert into user_set (id, name, data) values (?, 'pw', ?)"), [
  1172. user_name,
  1173. user_pw_hash
  1174. ])
  1175. curs.execute(db_change("insert into user_set (id, name, data) values (?, 'acl', ?)"), [
  1176. user_name,
  1177. user_auth
  1178. ])
  1179. curs.execute(db_change("insert into user_set (id, name, data) values (?, 'date', ?)"), [
  1180. user_name,
  1181. get_time()
  1182. ])
  1183. curs.execute(db_change("insert into user_set (id, name, data) values (?, 'encode', ?)"), [
  1184. user_name,
  1185. data_encode
  1186. ])
  1187. if user_email != '':
  1188. curs.execute(db_change("insert into user_set (name, id, data) values ('email', ?, ?)"), [
  1189. user_name,
  1190. user_email
  1191. ])
  1192. conn.commit()
  1193. def ua_plus(u_id, u_ip, u_agent, time):
  1194. curs.execute(db_change("select data from other where name = 'ua_get'"))
  1195. rep_data = curs.fetchall()
  1196. if rep_data and rep_data[0][0] != '':
  1197. pass
  1198. else:
  1199. curs.execute(db_change(
  1200. "insert into ua_d (name, ip, ua, today, sub) values (?, ?, ?, ?, '')"
  1201. ), [
  1202. u_id,
  1203. u_ip,
  1204. u_agent,
  1205. time
  1206. ])
  1207. def ban_insert(name, end, why, login, blocker, type_d = None):
  1208. now_time = get_time()
  1209. band = type_d if type_d else ''
  1210. curs.execute(db_change(
  1211. "update rb set ongoing = '' where end < ? and end != '' and ongoing = '1'"
  1212. ), [now_time])
  1213. curs.execute(db_change("" + \
  1214. "select block from rb " + \
  1215. "where ((end > ? and end != '') or end = '') and block = ? and " + \
  1216. "band = ? and ongoing = '1'" + \
  1217. ""), [now_time, name, band])
  1218. if curs.fetchall():
  1219. curs.execute(db_change(
  1220. "insert into rb (block, end, today, blocker, why, band) values (?, ?, ?, ?, ?, ?)"
  1221. ), [
  1222. name,
  1223. 'release',
  1224. now_time,
  1225. blocker,
  1226. '',
  1227. band
  1228. ])
  1229. curs.execute(db_change(
  1230. "update rb set ongoing = '' where block = ? and band = ? and ongoing = '1'"
  1231. ), [name, band])
  1232. else:
  1233. login = 'O' if login != '' else ''
  1234. if end != '0':
  1235. end = int(number_check(end))
  1236. time = datetime.datetime.now()
  1237. plus = datetime.timedelta(seconds = end)
  1238. r_time = (time + plus).strftime("%Y-%m-%d %H:%M:%S")
  1239. else:
  1240. r_time = ''
  1241. curs.execute(db_change(
  1242. "insert into rb (block, end, today, blocker, why, band, ongoing, login) " + \
  1243. "values (?, ?, ?, ?, ?, ?, '1', ?)"
  1244. ), [
  1245. name,
  1246. r_time,
  1247. now_time,
  1248. blocker,
  1249. why,
  1250. band,
  1251. login
  1252. ])
  1253. conn.commit()
  1254. def rd_plus(topic_num, date, name = None, sub = None):
  1255. curs.execute(db_change("select code from rd where code = ?"), [topic_num])
  1256. if curs.fetchall():
  1257. curs.execute(db_change("update rd set date = ? where code = ?"), [date, topic_num])
  1258. else:
  1259. curs.execute(db_change(
  1260. "insert into rd (title, sub, code, date) values (?, ?, ?, ?)"
  1261. ), [name, sub, topic_num, date])
  1262. conn.commit()
  1263. def history_plus(title, data, date, ip, send, leng, t_check = '', mode = ''):
  1264. if mode == 'add':
  1265. curs.execute(db_change(
  1266. "select id from history where title = ? order by id + 0 asc limit 1"
  1267. ), [title])
  1268. id_data = curs.fetchall()
  1269. id_data = str(int(id_data[0][0]) - 1) if id_data else '0'
  1270. else:
  1271. curs.execute(db_change(
  1272. "select id from history where title = ? order by id + 0 desc limit 1"
  1273. ), [title])
  1274. id_data = curs.fetchall()
  1275. id_data = str(int(id_data[0][0]) + 1) if id_data else '1'
  1276. mode = mode if not re.search('^user:', title) else 'user'
  1277. send = re.sub(r'\(|\)|<|>', '', send)
  1278. send = send[:128] if len(send) > 128 else send
  1279. send = send + ' (' + t_check + ')' if t_check != '' else send
  1280. if mode != 'add' and mode != 'user':
  1281. curs.execute(db_change("select count(*) from rc where type = 'normal'"))
  1282. if curs.fetchall()[0][0] >= 200:
  1283. curs.execute(db_change(
  1284. "select id, title from rc where type = 'normal' order by date asc limit 1"
  1285. ))
  1286. rc_data = curs.fetchall()
  1287. if rc_data:
  1288. curs.execute(db_change(
  1289. 'delete from rc where id = ? and title = ? and type = "normal"'
  1290. ), [
  1291. rc_data[0][0],
  1292. rc_data[0][1]
  1293. ])
  1294. curs.execute(db_change(
  1295. "insert into rc (id, title, date, type) values (?, ?, ?, 'normal')"
  1296. ), [
  1297. id_data,
  1298. title,
  1299. date
  1300. ])
  1301. if mode != 'add':
  1302. curs.execute(db_change("select count(*) from rc where type = ?"), [mode])
  1303. if curs.fetchall()[0][0] >= 200:
  1304. curs.execute(db_change(
  1305. "select id, title from rc where type = ? order by date asc limit 1"
  1306. ), [mode])
  1307. rc_data = curs.fetchall()
  1308. if rc_data:
  1309. curs.execute(db_change(
  1310. 'delete from rc where id = ? and title = ? and type = ?'
  1311. ), [
  1312. rc_data[0][0],
  1313. rc_data[0][1],
  1314. mode
  1315. ])
  1316. curs.execute(db_change(
  1317. "insert into rc (id, title, date, type) values (?, ?, ?, ?)"
  1318. ), [
  1319. id_data,
  1320. title,
  1321. date,
  1322. mode
  1323. ])
  1324. curs.execute(db_change(
  1325. "insert into history (id, title, data, date, ip, send, leng, hide, type) " + \
  1326. "values (?, ?, ?, ?, ?, ?, ?, '', ?)"
  1327. ), [
  1328. id_data,
  1329. title,
  1330. data,
  1331. date,
  1332. ip,
  1333. send,
  1334. leng,
  1335. mode
  1336. ])
  1337. # Func-error
  1338. def re_error(data):
  1339. conn.commit()
  1340. if data == '/ban':
  1341. if ban_check() == 1:
  1342. end = '<div id="get_user_info"></div><script>load_user_info("' + ip_check() + '");</script>'
  1343. else:
  1344. end = '<ul class="inside_ul"><li>' + load_lang('authority_error') + '</li></ul>'
  1345. return easy_minify(flask.render_template(skin_check(),
  1346. imp = [load_lang('error'), wiki_set(1), wiki_custom(), wiki_css([0, 0])],
  1347. data = '<h2>' + load_lang('error') + '</h2>' + end,
  1348. menu = 0
  1349. )), 401
  1350. else:
  1351. num = int(number_check(data.replace('/error/', '')))
  1352. if num == 1:
  1353. data = load_lang('no_login_error')
  1354. elif num == 2:
  1355. data = load_lang('no_exist_user_error')
  1356. elif num == 3:
  1357. data = load_lang('authority_error')
  1358. elif num == 4:
  1359. data = load_lang('no_admin_block_error')
  1360. elif num == 5:
  1361. data = load_lang('skin_error')
  1362. elif num == 6:
  1363. data = load_lang('same_id_exist_error')
  1364. elif num == 7:
  1365. data = load_lang('long_id_error')
  1366. elif num == 8:
  1367. data = load_lang('id_char_error') + ' <a href="/name_filter">(' + load_lang('id_filter_list') + ')</a>'
  1368. elif num == 9:
  1369. data = load_lang('file_exist_error')
  1370. elif num == 10:
  1371. data = load_lang('password_error')
  1372. elif num == 11:
  1373. data = load_lang('topic_long_error')
  1374. elif num == 12:
  1375. data = load_lang('email_error')
  1376. elif num == 13:
  1377. data = load_lang('recaptcha_error')
  1378. elif num == 14:
  1379. data = load_lang('file_extension_error') + ' <a href="/extension_filter">(' + load_lang('extension_filter_list') + ')</a>'
  1380. elif num == 15:
  1381. data = load_lang('edit_record_error')
  1382. elif num == 16:
  1383. data = load_lang('same_file_error')
  1384. elif num == 17:
  1385. data = load_lang('file_capacity_error') + wiki_set(3)
  1386. elif num == 18:
  1387. data = load_lang('email_send_error')
  1388. elif num == 19:
  1389. data = load_lang('decument_exist_error')
  1390. elif num == 20:
  1391. data = load_lang('password_diffrent_error')
  1392. elif num == 21:
  1393. data = load_lang('edit_filter_error')
  1394. elif num == 22:
  1395. data = load_lang('file_name_error')
  1396. elif num == 23:
  1397. data = load_lang('regex_error')
  1398. elif num == 24:
  1399. curs.execute(db_change("select data from other where name = 'slow_edit'"))
  1400. slow_edit = curs.fetchall()
  1401. slow_edit = '' if not slow_edit else slow_edit[0][0]
  1402. data = load_lang('fast_edit_error') + slow_edit
  1403. elif num == 25:
  1404. data = load_lang('too_many_dec_error')
  1405. elif num == 26:
  1406. data = load_lang('application_not_found')
  1407. elif num == 27:
  1408. data = load_lang("invalid_password_error")
  1409. elif num == 28:
  1410. data = load_lang('watchlist_overflow_error')
  1411. elif num == 29:
  1412. data = load_lang('copyright_disagreed')
  1413. elif num == 30:
  1414. data = load_lang('ie_wrong_callback')
  1415. elif num == 33:
  1416. data = load_lang('restart_fail_error')
  1417. elif num == 34:
  1418. data = load_lang("update_error") + ' <a href="https://github.com/opennamu/opennamu">(Github)</a>'
  1419. elif num == 35:
  1420. data = load_lang('same_email_error')
  1421. elif num == 36:
  1422. data = load_lang('input_email_error')
  1423. else:
  1424. data = '???'
  1425. if num == 5:
  1426. if flask.request.path != '/main_skin_set':
  1427. title = load_lang('skin_set')
  1428. tool = [['main_skin_set', load_lang('main_skin_set')]]
  1429. load_skin_set = ''
  1430. else:
  1431. title = load_lang('main_skin_set')
  1432. tool = [['skin_set', load_lang('skin_set')]]
  1433. load_skin_set = '<script>main_css_skin_set();</script>'
  1434. return easy_minify(flask.render_template(skin_check(),
  1435. imp = [title, wiki_set(1), wiki_custom(), wiki_css([0, 0])],
  1436. data = '' + \
  1437. '<div id="main_skin_set">' + \
  1438. '<h2>' + load_lang('error') + '</h2>' + \
  1439. '<ul class="inside_ul">' + \
  1440. '<li>' + data + ' <a href="/main_skin_set">(' + load_lang('main_skin_set') + ')</a></li>' + \
  1441. '</ul>' + \
  1442. '</div>' + \
  1443. load_skin_set,
  1444. menu = tool
  1445. ))
  1446. else:
  1447. return easy_minify(flask.render_template(skin_check(),
  1448. imp = [load_lang('error'), wiki_set(1), wiki_custom(), wiki_css([0, 0])],
  1449. data = '' + \
  1450. '<h2>' + load_lang('error') + '</h2>' + \
  1451. '<ul class="inside_ul">' + \
  1452. '<li>' + data + '</li>' + \
  1453. '</ul>' + \
  1454. '',
  1455. menu = 0
  1456. )), 400