view_read.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. from .tool.func import *
  2. def view_read(name = 'Test', doc_rev = 0, 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_doc = ''
  9. file_data = ''
  10. ip = ip_check()
  11. uppage = re.sub(r"/([^/]+)$", '', name)
  12. uppage = 0 if uppage == name else uppage
  13. num = str(doc_rev)
  14. curs.execute(db_change("select sub from rd where title = ? and not stop = 'O' order by date desc"), [name])
  15. topic = 1 if curs.fetchall() else 0
  16. curs.execute(db_change("select title from data where title like ?"), [name + '/%'])
  17. down = 1 if curs.fetchall() else 0
  18. if re.search(r'^category:', name):
  19. name_view = name
  20. category_doc = ''
  21. category_sub = ''
  22. count_sub_category = 0
  23. count_category = 0
  24. curs.execute(db_change("select link from back where title = ? and type = 'cat' order by link asc"), [name])
  25. category_sql = curs.fetchall()
  26. for data in category_sql:
  27. if data[0].startswith('category:'):
  28. category_sub += '<li><a href="/w/' + url_pas(data[0]) + '">' + html.escape(data[0]) + '</a></li>'
  29. count_sub_category += 1
  30. else:
  31. category_doc += '' + \
  32. '<li>' + \
  33. '<a href="/w/' + url_pas(data[0]) + '">' + html.escape(data[0]) + '</a> ' + \
  34. '<a id="inside" href="/xref/' + url_pas(data[0]) + '">(' + load_lang('backlink') + ')</a>' + \
  35. '</li>' + \
  36. ''
  37. count_category += 1
  38. if category_doc != '':
  39. category_doc = '' + \
  40. '<h2 id="cate_normal">' + load_lang('category_title') + '</h2>' + \
  41. '<ul class="inside_ul">' + \
  42. '<li>' + load_lang('all') + ' : ' + str(count_category) + '</li>' + \
  43. category_doc + \
  44. '</ul>' + \
  45. ''
  46. if category_sub != '':
  47. category_doc += '' + \
  48. '<h2 id="cate_under">' + load_lang('under_category') + '</h2>' + \
  49. '<ul class="inside_ul">' + \
  50. '<li>' + load_lang('all') + ' : ' + str(count_sub_category) + '</li>' + \
  51. category_sub + \
  52. '</ul>' + \
  53. ''
  54. elif re.search(r"^user:([^/]*)", name):
  55. name_view = name
  56. match = re.search(r"^user:([^/]*)", name)
  57. user_name = html.escape(match.group(1))
  58. user_doc = ''
  59. # S admin or owner 특수 틀 추가
  60. if admin_check('all', None, user_name) == 1:
  61. if admin_check(None, None, user_name) == 1:
  62. curs.execute(db_change('select data from other where name = "phrase_user_page_owner"'))
  63. db_data = curs.fetchall()
  64. if db_data and db_data[0][0] != '':
  65. user_doc += db_data[0][0] + '<br>'
  66. else:
  67. curs.execute(db_change('select data from other where name = "phrase_user_page_admin"'))
  68. db_data = curs.fetchall()
  69. if db_data and db_data[0][0] != '':
  70. user_doc += db_data[0][0] + '<br>'
  71. else:
  72. curs.execute(db_change('select data from other where name = "phrase_user_page_admin"'))
  73. db_data = curs.fetchall()
  74. if db_data and db_data[0][0] != '':
  75. user_doc += db_data[0][0] + '<br>'
  76. # E
  77. user_doc += '''
  78. <div id="opennamu_get_user_info">''' + html.escape(user_name) + '''</div>
  79. <hr class="main_hr">
  80. '''
  81. if name == 'user:' + user_name:
  82. menu += [['w/' + url_pas(name) + '/' + url_pas(get_time().split()[0]), load_lang('today_doc')]]
  83. elif re.search(r"^file:", name):
  84. name_view = name
  85. mime_type = re.search(r'([^.]+)$', name)
  86. if mime_type:
  87. mime_type = mime_type.group(1).lower()
  88. else:
  89. mime_type = 'jpg'
  90. file_name = re.sub(r'\.([^.]+)$', '', name)
  91. file_name = re.sub(r'^file:', '', file_name)
  92. file_all_name = sha224_replace(file_name) + '.' + mime_type
  93. file_path_name = os.path.join(load_image_url(), file_all_name)
  94. if os.path.exists(file_path_name):
  95. file_size = str(round(os.path.getsize(file_path_name) / 1000, 1))
  96. file_data = '''
  97. <img src="/image/''' + url_pas(file_all_name) + '''">
  98. <h2>DATA</h2>
  99. <table>
  100. <tr><td>URL</td><td><a href="/image/''' + url_pas(file_all_name) + '''">LINK</a></td></tr>
  101. <tr><td>VOLUME</td><td>''' + file_size + '''KB</td></tr>
  102. </table>
  103. <h2>CONTENT</h2>
  104. '''
  105. menu += [['delete_file/' + url_pas(name), load_lang('file_delete')]]
  106. else:
  107. file_data = ''
  108. else:
  109. name_view = name
  110. if num != '0':
  111. curs.execute(db_change("select title from history where title = ? and id = ? and hide = 'O'"), [name, num])
  112. if curs.fetchall() and admin_check(6) != 1:
  113. return redirect('/history/' + url_pas(name))
  114. curs.execute(db_change("select data from history where title = ? and id = ?"), [name, num])
  115. else:
  116. curs.execute(db_change("select data from data where title = ?"), [name])
  117. data = curs.fetchall()
  118. end_data = render_set(
  119. doc_name = name,
  120. doc_data = data[0][0] if data else None
  121. )
  122. if end_data == 'HTTP Request 401.3':
  123. response_data = 401
  124. curs.execute(db_change('select data from other where name = "error_401"'))
  125. sql_d = curs.fetchall()
  126. if sql_d and sql_d[0][0] != '':
  127. end_data = '<h2>' + load_lang('error') + '</h2><ul class="inside_ul"><li>' + sql_d[0][0] + '</li></ul>'
  128. else:
  129. end_data = '<h2>' + load_lang('error') + '</h2><ul class="inside_ul"><li>' + load_lang('authority_error') + '</li></ul>'
  130. elif end_data == 'HTTP Request 404':
  131. response_data = 404
  132. curs.execute(db_change('select data from other where name = "error_404"'))
  133. sql_d = curs.fetchall()
  134. if sql_d and sql_d[0][0] != '':
  135. end_data = '<h2>' + load_lang('error') + '</h2><ul class="inside_ul"><li>' + sql_d[0][0] + '</li></ul>'
  136. else:
  137. end_data = '<h2>' + load_lang('error') + '</h2><ul class="inside_ul"><li>' + load_lang('decument_404_error') + '</li></ul>'
  138. curs.execute(db_change('' + \
  139. 'select ip, date, leng, send, id from history ' + \
  140. 'where title = ? and hide != "O" order by id + 0 desc limit 3' + \
  141. ''), [name])
  142. sql_d = curs.fetchall()
  143. if sql_d:
  144. end_data += '<h2>' + load_lang('history') + '</h2><ul class="inside_ul">'
  145. for i in sql_d:
  146. if re.search(r"\+", i[2]):
  147. leng = '<span style="color:green;">(' + i[2] + ')</span>'
  148. elif re.search(r"\-", i[2]):
  149. leng = '<span style="color:red;">(' + i[2] + ')</span>'
  150. else:
  151. leng = '<span style="color:gray;">(' + i[2] + ')</span>'
  152. end_data += '<li>' + i[1] + ' | r' + i[4] + ' | ' + ip_pas(i[0]) + ' | ' + leng + (' | ' + i[3] if i[3] != '' else '') + '</li>'
  153. end_data += '<li><a href="/history/' + url_pas(name) + '">(...)</a></li></ul>'
  154. else:
  155. response_data = 200
  156. if num != '0':
  157. menu += [['history/' + url_pas(name), load_lang('history')]]
  158. sub = ' (r' + str(num) + ')'
  159. acl = 0
  160. r_date = 0
  161. else:
  162. curs.execute(db_change("select title from acl where title = ?"), [name])
  163. acl = 1 if curs.fetchall() else 0
  164. menu_acl = 1 if acl_check(name) == 1 else 0
  165. if response_data == 404:
  166. menu += [['edit/' + url_pas(name), load_lang('create'), menu_acl]]
  167. else:
  168. menu += [['edit/' + url_pas(name), load_lang('edit'), menu_acl]]
  169. menu += [
  170. ['topic/' + url_pas(name), load_lang('discussion'), topic],
  171. ['history/' + url_pas(name), load_lang('history')],
  172. ['xref/' + url_pas(name), load_lang('backlink')],
  173. ['acl/' + url_pas(name), load_lang('acl'), acl],
  174. ]
  175. if do_type == 'from':
  176. menu += [['w/' + url_pas(name), load_lang('pass')]]
  177. if flask.session and 'lastest_document' in flask.session:
  178. end_data = '''
  179. <div id="redirect">
  180. <a href="/w_from/''' + url_pas(flask.session['lastest_document']) + '''">''' + flask.session['lastest_document'] + '''</a> ⇨ <b>''' + name + '''</b>
  181. </div>
  182. <br>
  183. ''' + end_data
  184. flask.session['lastest_document'] = name
  185. else:
  186. flask.session['lastest_document'] = name
  187. if uppage != 0:
  188. menu += [['w/' + url_pas(uppage), load_lang('upper')]]
  189. if down:
  190. menu += [['down/' + url_pas(name), load_lang('sub')]]
  191. curs.execute(db_change("select date from history where title = ? order by date desc limit 1"), [name])
  192. r_date = curs.fetchall()
  193. r_date = r_date[0][0] if r_date else 0
  194. div = file_data + user_doc + end_data + category_doc
  195. if num != '0':
  196. curs.execute(db_change('select data from other where name = "phrase_old_page_warring"'))
  197. db_data = curs.fetchall()
  198. if db_data and db_data[0][0] != '':
  199. div = db_data[0][0] + '<hr class="main_hr">' + div
  200. curs.execute(db_change("select data from other where name = 'body'"))
  201. body = curs.fetchall()
  202. div = (body[0][0] + div) if body else div
  203. curs.execute(db_change("select data from other where name = 'bottom_body'"))
  204. body = curs.fetchall()
  205. div += body[0][0] if body else ''
  206. if ip_or_user(ip) == 0:
  207. curs.execute(db_change("select title from scan where user = ? and title = ?"), [ip, name])
  208. watch_list = 2 if curs.fetchall() else 1
  209. else:
  210. watch_list = 0
  211. return easy_minify(flask.render_template(skin_check(),
  212. imp = [name_view, wiki_set(), wiki_custom(), wiki_css([sub, r_date, watch_list])],
  213. data = div,
  214. menu = menu
  215. )), response_data