webui.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. #!/usr/bin/env python
  2. #============================================================================
  3. # This file is part of Pwman3.
  4. #
  5. # Pwman3 is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License, version 2
  7. # as published by the Free Software Foundation;
  8. #
  9. # Pwman3 is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with Pwman3; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. #============================================================================
  18. # Copyright (C) 2012-2014 Oz Nahum <nahumoz@gmail.com>
  19. #============================================================================
  20. from __future__ import print_function
  21. from bottle import route, run, debug, template, request, get, redirect
  22. import os
  23. import sys
  24. import re
  25. import shutil
  26. from pwman import default_config, which
  27. from pwman import parser_options
  28. from pwman.ui import get_ui_platform
  29. from pwman.ui.tools import CLICallback
  30. from pwman.util.crypto import CryptoEngine
  31. import pwman.util.config as config
  32. import pwman.data.factory
  33. from pwman.data.tags import TagNew
  34. AUTHENTICATED = False
  35. TAGS = None
  36. tmplt = """
  37. %#template to generate a HTML table from a list of tuples (or list of lists, or tuple of tuples or ...)
  38. <form action="/" method="POST">
  39. <select name="tag" onchange="this.form.submit()">
  40. %for tag in tags:
  41. <option value="{{tag}}">{{tag}}</option>
  42. %end
  43. </select>
  44. </form>
  45. <p>The open items are as follows:</p>
  46. <table border="1">
  47. %for node in nodes:
  48. <tr>
  49. %for item in node:
  50. <td>{{item}}</td>
  51. %end
  52. </tr>
  53. <tr><td></td><td>edit</td></tr>
  54. %end
  55. </table>
  56. """
  57. login = """
  58. <p>Please enter your database password: <b>
  59. <form action="/auth" method="POST">
  60. Password: <input type="password" name="pwd">
  61. </form>"""
  62. def get_conf(args):
  63. config_dir = os.path.expanduser("~/.pwman")
  64. if not os.path.isdir(config_dir):
  65. os.mkdir(config_dir)
  66. if not os.path.exists(args.cfile):
  67. config.set_defaults(default_config)
  68. else:
  69. config.load(args.cfile)
  70. return config
  71. def set_xsel(config, OSX):
  72. if not OSX:
  73. xselpath = which("xsel")
  74. config.set_value("Global", "xsel", xselpath)
  75. elif OSX:
  76. pbcopypath = which("pbcopy")
  77. config.set_value("Global", "xsel", pbcopypath)
  78. def set_win_colors(config):
  79. if 'win' in sys.platform:
  80. try:
  81. import colorama
  82. colorama.init()
  83. except ImportError:
  84. config.set_value("Global", "colors", 'no')
  85. def set_umask(config):
  86. # set umask before creating/opening any files
  87. try:
  88. umask = config.get_value("Global", "umask")
  89. if re.search(r'^\d{4}$', umask):
  90. os.umask(int(umask))
  91. else:
  92. raise ValueError
  93. except ValueError:
  94. print("Could not determine umask from config!")
  95. sys.exit(2)
  96. def set_db(args):
  97. if args.dbase:
  98. config.set_value("Database", "filename", args.dbase)
  99. config.set_value("Global", "save", "False")
  100. def set_algorithm(args, config):
  101. if args.algo:
  102. config.set_value("Encryption", "algorithm", args.algo)
  103. config.set_value("Global", "save", "False")
  104. def get_conf_options(args, OSX):
  105. config = get_conf(args)
  106. xselpath = config.get_value("Global", "xsel")
  107. if not xselpath:
  108. set_xsel(config, OSX)
  109. set_win_colors(config)
  110. set_db(args)
  111. set_umask(config)
  112. set_algorithm(args, config)
  113. dbtype = config.get_value("Database", "type")
  114. if not dbtype:
  115. print("Could not read the Database type from the config!")
  116. sys.exit(1)
  117. return xselpath, dbtype
  118. @route('/auth', method=['GET', 'POST'])
  119. def is_authenticated():
  120. global AUTHENTICATED
  121. crypto = CryptoEngine.get()
  122. if request.method == 'POST':
  123. key = request.POST.get('pwd', '')
  124. crypto.auth(key)
  125. AUTHENTICATED = True
  126. redirect('/')
  127. else:
  128. return login
  129. @route('/', method=['GET', 'POST'])
  130. def listnodes():
  131. global AUTHENTICATED, TAGS
  132. _filter = None
  133. OSX = False
  134. args = parser_options().parse_args()
  135. xselpath, dbtype = get_conf_options(args, OSX)
  136. dbver = 0.4
  137. db = pwman.data.factory.create(dbtype, dbver)
  138. db.open()
  139. crypto = CryptoEngine.get()
  140. if not AUTHENTICATED:
  141. redirect('/auth')
  142. if 'POST' in request.method:
  143. _filter = request.POST.get('tag')
  144. if _filter:
  145. db._filtertags = [TagNew(_filter.strip())]
  146. if _filter == 'None':
  147. db._filtertags = []
  148. nodeids = db.listnodes()
  149. nodes = db.getnodes(nodeids)
  150. nodesd = [''] * len(nodes)
  151. for idx, node in enumerate(nodes):
  152. ntags = filter(None, node.tags)
  153. nodesd[idx] = ('@'.join((node.username, node.url)), ','.join(ntags))
  154. if not TAGS:
  155. TAGS = list(set([''.join(node.tags).strip() for node in nodes]))
  156. TAGS.sort()
  157. TAGS.insert(0, 'None')
  158. TAGS.insert(0, 'None')
  159. print(len(TAGS))
  160. output = template(tmplt, nodes=nodesd, tags=TAGS)
  161. return output
  162. debug(True)
  163. run(reloader=True)