view_read.py 10 KB

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