func.py 56 KB

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