2
0

view_read.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. from .tool.func import *
  2. def view_read(name = 'Test', doc_rev = '', doc_from = '', do_type = ''):
  3. with get_db_connect() as conn:
  4. curs = conn.cursor()
  5. sub = 0
  6. menu = []
  7. user_doc = ''
  8. category_total = ''
  9. file_data = ''
  10. doc_type = ''
  11. now_time = get_time()
  12. ip = ip_check()
  13. uppage = re.sub(r"/([^/]+)$", '', name)
  14. uppage = 0 if uppage == name else uppage
  15. num = str(doc_rev)
  16. curs.execute(db_change("select sub from rd where title = ? and not stop = 'O' order by date desc"), [name])
  17. topic = 1 if curs.fetchall() else 0
  18. curs.execute(db_change("select title from data where title like ?"), [name + '/%'])
  19. down = 1 if curs.fetchall() else 0
  20. if re.search(r'^category:', name):
  21. name_view = name
  22. doc_type = 'category'
  23. category_doc = ''
  24. category_sub = ''
  25. count_sub_category = 0
  26. count_category = 0
  27. curs.execute(db_change("select distinct link from back where title = ? and type = 'cat' order by link asc"), [name])
  28. category_sql = curs.fetchall()
  29. for data in category_sql:
  30. link_view = data[0]
  31. if get_main_skin_set(curs, flask.session, 'main_css_category_change_title', ip) != 'off':
  32. curs.execute(db_change("select data from back where title = ? and link = ? and type = 'cat_view' limit 1"), [name, data[0]])
  33. db_data = curs.fetchall()
  34. if db_data and db_data[0][0] != '':
  35. link_view = db_data[0][0]
  36. link_blur = ''
  37. curs.execute(db_change("select data from back where title = ? and link = ? and type = 'cat_blur' limit 1"), [name, data[0]])
  38. db_data = curs.fetchall()
  39. if db_data:
  40. link_blur = 'opennamu_category_blur'
  41. if data[0].startswith('category:'):
  42. category_sub += '<li><a class="' + link_blur + '" href="/w/' + url_pas(data[0]) + '">' + html.escape(link_view) + '</a></li>'
  43. count_sub_category += 1
  44. else:
  45. category_doc += '' + \
  46. '<li>' + \
  47. '<a class="' + link_blur + '" href="/w/' + url_pas(data[0]) + '">' + html.escape(link_view) + '</a> ' + \
  48. '<a class="opennamu_link_inter" href="/xref/' + url_pas(data[0]) + '">(' + load_lang('backlink') + ')</a>' + \
  49. '</li>' + \
  50. ''
  51. count_category += 1
  52. if category_sub != '':
  53. category_total += '' + \
  54. '<h2 id="cate_under">' + load_lang('under_category') + '</h2>' + \
  55. '<ul class="opennamu_ul">' + \
  56. '<li>' + load_lang('all') + ' : ' + str(count_sub_category) + '</li>' + \
  57. category_sub + \
  58. '</ul>' + \
  59. ''
  60. if category_doc != '':
  61. category_total += '' + \
  62. '<h2 id="cate_normal">' + load_lang('category_title') + '</h2>' + \
  63. '<ul class="opennamu_ul">' + \
  64. '<li>' + load_lang('all') + ' : ' + str(count_category) + '</li>' + \
  65. category_doc + \
  66. '</ul>' + \
  67. ''
  68. elif re.search(r"^user:([^/]*)", name):
  69. name_view = name
  70. doc_type = 'user'
  71. match = re.search(r"^user:([^/]*)", name)
  72. user_name = html.escape(match.group(1))
  73. user_doc = ''
  74. # S admin or owner 특수 틀 추가
  75. if admin_check('all', None, user_name) == 1:
  76. if admin_check(None, None, user_name) == 1:
  77. curs.execute(db_change('select data from other where name = "phrase_user_page_owner"'))
  78. db_data = curs.fetchall()
  79. if db_data and db_data[0][0] != '':
  80. user_doc += db_data[0][0] + '<br>'
  81. else:
  82. curs.execute(db_change('select data from other where name = "phrase_user_page_admin"'))
  83. db_data = curs.fetchall()
  84. if db_data and db_data[0][0] != '':
  85. user_doc += db_data[0][0] + '<br>'
  86. else:
  87. curs.execute(db_change('select data from other where name = "phrase_user_page_admin"'))
  88. db_data = curs.fetchall()
  89. if db_data and db_data[0][0] != '':
  90. user_doc += db_data[0][0] + '<br>'
  91. # E
  92. user_doc += '''
  93. <div id="opennamu_get_user_info">''' + html.escape(user_name) + '''</div>
  94. <hr class="main_hr">
  95. '''
  96. if name == 'user:' + user_name:
  97. menu += [['w/' + url_pas(name) + '/' + url_pas(now_time.split()[0]), load_lang('today_doc')]]
  98. elif re.search(r"^file:", name):
  99. curs.execute(db_change('select id from history where title = ? order by date desc limit 1'), [name])
  100. db_data = curs.fetchall()
  101. rev = db_data[0][0] if db_data else '1'
  102. name_view = name
  103. doc_type = 'file'
  104. mime_type = re.search(r'([^.]+)$', name)
  105. if mime_type:
  106. mime_type = mime_type.group(1).lower()
  107. else:
  108. mime_type = 'jpg'
  109. file_name = re.sub(r'\.([^.]+)$', '', name)
  110. file_name = re.sub(r'^file:', '', file_name)
  111. file_all_name = sha224_replace(file_name) + '.' + mime_type
  112. file_path_name = os.path.join(load_image_url(), file_all_name)
  113. if os.path.exists(file_path_name):
  114. try:
  115. img = Image.open(file_path_name)
  116. width, height = img.size
  117. file_res = str(width) + 'x' + str(height)
  118. except:
  119. file_res = 'Vector'
  120. file_size = str(round(os.path.getsize(file_path_name) / 1000, 1))
  121. file_data = '''
  122. <img src="/image/''' + url_pas(file_all_name) + '''.cache_v''' + rev + '''">
  123. <h2>''' + load_lang('data') + '''</h2>
  124. <table>
  125. <tr><td>URL</td><td><a href="/image/''' + url_pas(file_all_name) + '''">''' + load_lang('link') + '''</a></td></tr>
  126. <tr><td>''' + load_lang('volume') + '''</td><td>''' + file_size + '''KB</td></tr>
  127. <tr><td>''' + load_lang('resolution') + '''</td><td>''' + file_res + '''</td></tr>
  128. </table>
  129. <h2>''' + load_lang('content') + '''</h2>
  130. '''
  131. menu += [['delete_file/' + url_pas(name), load_lang('file_delete')]]
  132. else:
  133. file_data = ''
  134. else:
  135. name_view = name
  136. if num != '':
  137. curs.execute(db_change("select title from history where title = ? and id = ? and hide = 'O'"), [name, num])
  138. if curs.fetchall() and admin_check(6) != 1:
  139. return redirect('/history/' + url_pas(name))
  140. curs.execute(db_change("select data from history where title = ? and id = ?"), [name, num])
  141. else:
  142. curs.execute(db_change("select data from data where title = ?"), [name])
  143. data = curs.fetchall()
  144. end_data = render_set(
  145. doc_name = name,
  146. doc_data = data[0][0] if data else None,
  147. data_type = 'from' if do_type == 'from' else 'view'
  148. )
  149. if end_data == 'HTTP Request 401.3':
  150. response_data = 401
  151. curs.execute(db_change('select data from other where name = "error_401"'))
  152. sql_d = curs.fetchall()
  153. if sql_d and sql_d[0][0] != '':
  154. end_data = '<h2>' + load_lang('error') + '</h2><ul class="opennamu_ul"><li>' + sql_d[0][0] + '</li></ul>'
  155. else:
  156. end_data = '<h2>' + load_lang('error') + '</h2><ul class="opennamu_ul"><li>' + load_lang('authority_error') + '</li></ul>'
  157. elif end_data == 'HTTP Request 404':
  158. response_data = 404
  159. curs.execute(db_change('select data from other where name = "error_404"'))
  160. sql_d = curs.fetchall()
  161. if sql_d and sql_d[0][0] != '':
  162. end_data = '<h2>' + load_lang('error') + '</h2><ul class="opennamu_ul"><li>' + sql_d[0][0] + '</li></ul>'
  163. else:
  164. end_data = '<h2>' + load_lang('error') + '</h2><ul class="opennamu_ul"><li>' + load_lang('decument_404_error') + '</li></ul>'
  165. curs.execute(db_change('' + \
  166. 'select ip, date, leng, send, id from history ' + \
  167. 'where title = ? and hide != "O" order by id + 0 desc limit 3' + \
  168. ''), [name])
  169. sql_d = curs.fetchall()
  170. if sql_d:
  171. end_data += '<h2>' + load_lang('history') + '</h2><ul class="opennamu_ul">'
  172. for i in sql_d:
  173. if re.search(r"\+", i[2]):
  174. leng = '<span style="color:green;">(' + i[2] + ')</span>'
  175. elif re.search(r"\-", i[2]):
  176. leng = '<span style="color:red;">(' + i[2] + ')</span>'
  177. else:
  178. leng = '<span style="color:gray;">(' + i[2] + ')</span>'
  179. end_data += '<li>' + i[1] + ' | r' + i[4] + ' | ' + ip_pas(i[0]) + ' | ' + leng + (' | ' + i[3] if i[3] != '' else '') + '</li>'
  180. end_data += '<li><a href="/history/' + url_pas(name) + '">(...)</a></li></ul>'
  181. else:
  182. response_data = 200
  183. if num != '':
  184. menu += [['history/' + url_pas(name), load_lang('return')]]
  185. sub = ' (r' + str(num) + ')'
  186. acl = 0
  187. r_date = 0
  188. else:
  189. curs.execute(db_change("select title from acl where title = ?"), [name])
  190. acl = 1 if curs.fetchall() else 0
  191. menu_acl = 1 if acl_check(name) == 1 else 0
  192. if response_data == 404:
  193. menu += [['edit/' + url_pas(name), load_lang('create'), menu_acl]]
  194. else:
  195. menu += [['edit/' + url_pas(name), load_lang('edit'), menu_acl]]
  196. menu += [
  197. ['topic/' + url_pas(name), load_lang('discussion'), topic],
  198. ['history/' + url_pas(name), load_lang('history')],
  199. ['xref/' + url_pas(name), load_lang('backlink')],
  200. ['acl/' + url_pas(name), load_lang('setting'), acl],
  201. ]
  202. if flask.session and 'lastest_document' in flask.session:
  203. if type(flask.session['lastest_document']) != type([]):
  204. flask.session['lastest_document'] = []
  205. else:
  206. flask.session['lastest_document'] = []
  207. if do_type == 'from':
  208. menu += [['w/' + url_pas(name), load_lang('pass')]]
  209. last_page = ''
  210. for for_a in reversed(range(0, len(flask.session['lastest_document']))):
  211. last_page = flask.session['lastest_document'][for_a]
  212. curs.execute(db_change("select link from back where (title = ? or link = ?) and type = 'redirect' limit 1"), [last_page, last_page])
  213. if curs.fetchall():
  214. break
  215. redirect_text = '{0} ➤ {1}'
  216. curs.execute(db_change('select data from other where name = "redirect_text"'))
  217. db_data = curs.fetchall()
  218. if db_data and db_data[0][0] != '':
  219. redirect_text = db_data[0][0]
  220. try:
  221. redirect_text = redirect_text.format('<a href="/w_from/' + url_pas(last_page) + '">' + html.escape(last_page) + '</a>', '<b>' + html.escape(name) + '</b>')
  222. except:
  223. redirect_text = '{0} ➤ {1}'
  224. redirect_text = redirect_text.format('<a href="/w_from/' + url_pas(last_page) + '">' + html.escape(last_page) + '</a>', '<b>' + html.escape(name) + '</b>')
  225. end_data = '''
  226. <div id="redirect">
  227. ''' + redirect_text + '''
  228. </div>
  229. <hr class="main_hr">
  230. ''' + end_data
  231. if len(flask.session['lastest_document']) >= 10:
  232. flask.session['lastest_document'] = flask.session['lastest_document'][-9:] + [name]
  233. else:
  234. flask.session['lastest_document'] += [name]
  235. flask.session['lastest_document'] = list(reversed(dict.fromkeys(reversed(flask.session['lastest_document']))))
  236. view_history_on = get_main_skin_set(curs, flask.session, 'main_css_view_history', ip)
  237. if view_history_on == 'on':
  238. end_data = '' + \
  239. '<div id="redirect">' + \
  240. load_lang('trace') + ' : ' + \
  241. ' ➥ '.join(
  242. [
  243. '<a href="/w/' + url_pas(for_a) + '">' + html.escape(for_a) + '</a>'
  244. for for_a in flask.session['lastest_document']
  245. ]
  246. ) + \
  247. '</div>' + \
  248. '<hr class="main_hr">' + \
  249. '' + end_data
  250. if uppage != 0:
  251. menu += [['w/' + url_pas(uppage), load_lang('upper')]]
  252. if down:
  253. menu += [['down/' + url_pas(name), load_lang('sub')]]
  254. curs.execute(db_change("select set_data from data_set where doc_name = ? and set_name = 'last_edit'"), [name])
  255. r_date = curs.fetchall()
  256. r_date = r_date[0][0] if r_date else 0
  257. div = file_data + user_doc + end_data + category_total
  258. if num != '':
  259. curs.execute(db_change('select data from other where name = "phrase_old_page_warning"'))
  260. db_data = curs.fetchall()
  261. if db_data and db_data[0][0] != '':
  262. div = db_data[0][0] + '<hr class="main_hr">' + div
  263. doc_type = 'rev'
  264. if doc_type == '':
  265. curs.execute(db_change('select data from other where name = "outdated_doc_warning_date"'))
  266. db_data = curs.fetchall()
  267. if db_data and db_data[0][0] != '' and r_date != 0:
  268. time_1 = datetime.datetime.strptime(r_date, '%Y-%m-%d %H:%M:%S') + datetime.timedelta(days = int(number_check(db_data[0][0])))
  269. time_2 = datetime.datetime.strptime(now_time, '%Y-%m-%d %H:%M:%S')
  270. if time_2 > time_1:
  271. curs.execute(db_change('select data from other where name = "outdated_doc_warning"'))
  272. db_data = curs.fetchall()
  273. div = (db_data[0][0] if db_data and db_data[0][0] != '' else load_lang('old_page_warning')) + '<hr class="main_hr">' + div
  274. curs.execute(db_change("select data from other where name = 'body'"))
  275. body = curs.fetchall()
  276. div = (body[0][0] + div) if body else div
  277. curs.execute(db_change("select data from other where name = 'bottom_body'"))
  278. body = curs.fetchall()
  279. div += body[0][0] if body else ''
  280. if ip_or_user(ip) == 0:
  281. curs.execute(db_change("select title from scan where user = ? and title = ?"), [ip, name])
  282. watch_list = 2 if curs.fetchall() else 1
  283. else:
  284. watch_list = 0
  285. return easy_minify(flask.render_template(skin_check(),
  286. imp = [name_view, wiki_set(), wiki_custom(), wiki_css([sub, r_date, watch_list])],
  287. data = div,
  288. menu = menu
  289. )), response_data