func.py 56 KB

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