func_new.py 58 KB

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