You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

367 lines
10 KiB

  1. # Copyright (c) 2012 Web Notes Technologies Pvt Ltd (http://erpnext.com)
  2. #
  3. # MIT License (MIT)
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining a
  6. # copy of this software and associated documentation files (the "Software"),
  7. # to deal in the Software without restriction, including without limitation
  8. # the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. # and/or sell copies of the Software, and to permit persons to whom the
  10. # Software is furnished to do so, subject to the following conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be included in
  13. # all copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  16. # INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  17. # PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  19. # CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
  20. # OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. #
  22. from __future__ import unicode_literals
  23. import sys, os
  24. import webnotes
  25. import webnotes.utils
  26. import webnotes.sessions
  27. form = webnotes.form
  28. form_dict = webnotes.form_dict
  29. sql = None
  30. session = None
  31. errdoc = ''
  32. errdoctype = ''
  33. errmethod = ''
  34. def get_cgi_fields():
  35. """make webnotes.form_dict from cgi field storage"""
  36. import cgi
  37. import webnotes
  38. from webnotes.utils import cstr
  39. # make the form_dict
  40. webnotes.form = cgi.FieldStorage(keep_blank_values=True)
  41. for key in webnotes.form.keys():
  42. # file upload must not be decoded as it is treated as a binary
  43. # file and hence in any encoding (it does not matter)
  44. if not getattr(webnotes.form[key], 'filename', None):
  45. webnotes.form_dict[key] = cstr(webnotes.form.getvalue(key))
  46. # Logs
  47. @webnotes.whitelist(allow_guest=True)
  48. def startup():
  49. webnotes.response.update(webnotes.sessions.get())
  50. def cleanup_docs():
  51. import webnotes.model.utils
  52. if webnotes.response.get('docs') and type(webnotes.response['docs'])!=dict:
  53. webnotes.response['docs'] = webnotes.model.utils.compress(webnotes.response['docs'])
  54. @webnotes.whitelist()
  55. def runserverobj(arg=None):
  56. import webnotes.widgets.form.run_method
  57. webnotes.widgets.form.run_method.runserverobj()
  58. @webnotes.whitelist(allow_guest=True)
  59. def logout():
  60. webnotes.login_manager.logout()
  61. @webnotes.whitelist()
  62. def dt_map():
  63. import webnotes
  64. import webnotes.model.utils
  65. from webnotes.model.code import get_obj
  66. from webnotes.model.doc import Document
  67. from webnotes.model.wrapper import ModelWrapper
  68. form_dict = webnotes.form_dict
  69. dt_list = webnotes.model.utils.expand(form_dict.get('docs'))
  70. from_doctype = form_dict.get('from_doctype')
  71. to_doctype = form_dict.get('to_doctype')
  72. from_docname = form_dict.get('from_docname')
  73. from_to_list = form_dict.get('from_to_list')
  74. dm = get_obj('DocType Mapper', from_doctype +'-' + to_doctype)
  75. dl = dm.dt_map(from_doctype, to_doctype, from_docname, Document(fielddata = dt_list[0]), (len(dt_list) > 1) and ModelWrapper(dt_list).doclist or [], from_to_list)
  76. webnotes.response['docs'] = dl
  77. @webnotes.whitelist()
  78. def load_month_events():
  79. import webnotes
  80. mm = webnotes.form_dict.get('month')
  81. yy = webnotes.form_dict.get('year')
  82. m_st = str(yy) + '-' + str(mm) + '-01'
  83. m_end = str(yy) + '-' + str(mm) + '-31'
  84. import webnotes.widgets.event
  85. webnotes.response['docs'] = webnotes.widgets.event.get_cal_events(m_st, m_end)
  86. @webnotes.whitelist()
  87. def uploadfile():
  88. import webnotes.utils
  89. import webnotes.utils.file_manager
  90. import json
  91. ret = []
  92. try:
  93. if webnotes.form_dict.get('from_form'):
  94. webnotes.utils.file_manager.upload()
  95. else:
  96. if webnotes.form_dict.get('method'):
  97. m = webnotes.form_dict['method']
  98. modulename = '.'.join(m.split('.')[:-1])
  99. methodname = m.split('.')[-1]
  100. __import__(modulename)
  101. import sys
  102. moduleobj = sys.modules[modulename]
  103. ret = getattr(moduleobj, methodname)()
  104. except Exception, e:
  105. webnotes.msgprint(e)
  106. webnotes.errprint(webnotes.utils.getTraceback())
  107. webnotes.response['type'] = 'iframe'
  108. if not webnotes.response.get('result'):
  109. webnotes.response['result'] = """<script>
  110. window.parent.wn.upload.callback("%s", %s);
  111. </script>""" % (webnotes.form_dict.get('_id'),
  112. json.dumps(ret))
  113. @webnotes.whitelist(allow_guest=True)
  114. def reset_password():
  115. from webnotes.model.code import get_obj
  116. from webnotes.utils import random_string
  117. user = webnotes.form_dict.get('user', '')
  118. if webnotes.conn.sql("""select name from tabProfile where name=%s""", user):
  119. new_password = random_string(8)
  120. webnotes.conn.sql("""update `__Auth` set password=password(%s)
  121. where `user`=%s""", (new_password, user))
  122. # Hack!
  123. webnotes.session["user"] = "Administrator"
  124. profile = get_obj("Profile", user)
  125. profile.password_reset_mail(new_password)
  126. webnotes.msgprint("Password has been reset and sent to your email id.")
  127. else:
  128. webnotes.msgprint("No such user (%s)" % user)
  129. def handle():
  130. """handle request"""
  131. cmd = webnotes.form_dict['cmd']
  132. if cmd!='login':
  133. # login executed in webnotes.auth
  134. try:
  135. execute_cmd(cmd)
  136. except webnotes.ValidationError, e:
  137. #webnotes.errprint(webnotes.utils.getTraceback())
  138. webnotes.errprint(e)
  139. webnotes.conn.rollback()
  140. except:
  141. webnotes.errprint(webnotes.utils.getTraceback())
  142. webnotes.conn and webnotes.conn.rollback()
  143. print_response()
  144. if webnotes.conn:
  145. webnotes.conn.close()
  146. def execute_cmd(cmd):
  147. """execute a request as python module"""
  148. validate_cmd(cmd)
  149. method = get_method(cmd)
  150. # check if whitelisted
  151. if webnotes.session['user'] == 'Guest':
  152. if (method not in webnotes.guest_methods):
  153. webnotes.response['403'] = 1
  154. raise Exception, 'Not Allowed, %s' % str(method)
  155. else:
  156. if not method in webnotes.whitelisted:
  157. webnotes.response['403'] = 1
  158. webnotes.msgprint('Not Allowed, %s' % str(method))
  159. raise Exception, 'Not Allowed, %s' % str(method)
  160. if not webnotes.conn.in_transaction:
  161. webnotes.conn.begin()
  162. ret = call(method, webnotes.form_dict)
  163. # returns with a message
  164. if ret:
  165. webnotes.response['message'] = ret
  166. # update session
  167. webnotes.session_obj.update()
  168. if webnotes.conn.in_transaction:
  169. webnotes.conn.commit()
  170. def call(fn, args):
  171. import inspect
  172. fnargs, varargs, varkw, defaults = inspect.getargspec(fn)
  173. newargs = {}
  174. for a in fnargs:
  175. if a in args:
  176. newargs[a] = args.get(a)
  177. return fn(**newargs)
  178. def get_method(cmd):
  179. """get method object from cmd"""
  180. if '.' in cmd:
  181. cmd_parts = cmd.split('.')
  182. module_string = ".".join(cmd_parts[:-1])
  183. fn_string = cmd_parts[-1]
  184. module = __import__(module_string, fromlist=[module_string.split('.')[-1].encode('utf-8')])
  185. method = getattr(module, fn_string)
  186. else:
  187. method = globals()[cmd]
  188. return method
  189. def validate_cmd(cmd):
  190. # check if there is no direct possibility of malicious script injection
  191. if cmd.startswith('webnotes.model.code'):
  192. raise Exception, 'Cannot call any methods from webnotes.model.code directly from the handler'
  193. if cmd.startswith('webnotes.model.db_schema'):
  194. raise Exception, 'Cannot call any methods from webnotes.model.db_schema directly from the handler'
  195. if cmd.startswith('webnotes.conn'):
  196. raise Exception, 'Cannot call database connection method directly from the handler'
  197. def print_response():
  198. print_map = {
  199. 'csv': print_csv,
  200. 'iframe': print_iframe,
  201. 'download': print_raw,
  202. 'json': print_json,
  203. 'page': print_page
  204. }
  205. print_map.get(webnotes.response.get('type'), print_json)()
  206. def print_page():
  207. """print web page"""
  208. from website.utils import render
  209. render(webnotes.response['page_name'])
  210. def eprint(content):
  211. print content.encode('utf-8')
  212. def print_json():
  213. make_logs()
  214. cleanup_docs()
  215. add_cookies()
  216. eprint("Content-Type: text/html; charset: utf-8")
  217. if webnotes.cookies:
  218. print webnotes.cookies
  219. import json
  220. print_zip(json.dumps(webnotes.response, default=json_handler, separators=(',',':')))
  221. def print_csv():
  222. eprint("Content-Type: text/csv; charset: utf-8")
  223. eprint("Content-Disposition: attachment; filename=%s.csv" % webnotes.response['doctype'].replace(' ', '_'))
  224. eprint("")
  225. eprint(webnotes.response['result'])
  226. def print_iframe():
  227. eprint("Content-Type: text/html; charset: utf-8")
  228. eprint("")
  229. eprint(webnotes.response.get('result') or '')
  230. if webnotes.debug_log:
  231. import json
  232. eprint("""\
  233. <script>
  234. var messages = %(messages)s;
  235. if (messages.length) {
  236. for (var i in messages) {
  237. window.parent.msgprint(messages[i]);
  238. }
  239. }
  240. var errors = %(errors)s;
  241. if (errors.length) {
  242. for (var i in errors) {
  243. window.parent.console.log(errors[i]);
  244. }
  245. }
  246. </script>""" % {
  247. 'messages': json.dumps(webnotes.message_log).replace("'", "\\'"),
  248. 'errors': json.dumps(webnotes.debug_log).replace("'", "\\'"),
  249. })
  250. def print_raw():
  251. eprint("Content-Type: %s" % \
  252. mimetypes.guess_type(webnotes.response['filename'])[0] \
  253. or 'application/unknown'),
  254. eprint("Content-Disposition: filename=%s" % \
  255. webnotes.response['filename'].replace(' ', '_'))
  256. eprint("")
  257. eprint(webnotes.response['filecontent'])
  258. def make_logs():
  259. """make strings for msgprint and errprint"""
  260. import json
  261. from webnotes.utils import cstr
  262. if webnotes.debug_log:
  263. webnotes.response['exc'] = json.dumps("\n".join([cstr(d) for d in webnotes.debug_log]))
  264. if webnotes.message_log:
  265. webnotes.response['server_messages'] = json.dumps([cstr(d) for d in webnotes.message_log])
  266. def add_cookies():
  267. """if there ar additional cookies defined during the request, add them"""
  268. if webnotes.cookies or webnotes.add_cookies:
  269. for c in webnotes.add_cookies.keys():
  270. webnotes.cookies[c.encode('utf-8')] = \
  271. webnotes.add_cookies[c].encode('utf-8')
  272. def print_zip(response):
  273. response = response.encode('utf-8')
  274. orig_len = len(response)
  275. if accept_gzip() and orig_len>512:
  276. response = compressBuf(response)
  277. eprint("Content-Encoding: gzip")
  278. eprint("Original-Length: %d" % orig_len)
  279. eprint("Content-Length: %d" % len(response))
  280. eprint("")
  281. print response
  282. def json_handler(obj):
  283. """serialize non-serializable data for json"""
  284. import datetime
  285. # serialize date
  286. if isinstance(obj, datetime.date):
  287. return unicode(obj)
  288. if isinstance(obj, datetime.timedelta):
  289. return unicode(obj)
  290. else:
  291. raise TypeError, """Object of type %s with value of %s is not JSON serializable""" % \
  292. (type(obj), repr(obj))
  293. def accept_gzip():
  294. if "gzip" in os.environ.get("HTTP_ACCEPT_ENCODING", ""):
  295. return True
  296. def compressBuf(buf):
  297. import gzip, cStringIO
  298. zbuf = cStringIO.StringIO()
  299. zfile = gzip.GzipFile(mode = 'wb', fileobj = zbuf, compresslevel = 5)
  300. zfile.write(buf)
  301. zfile.close()
  302. return zbuf.getvalue()