func.py 56 KB

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