def _func(cursor, *args, **kwargs): response = attempt_login(cursor) # If it's a string they've failed to login if type(response) == str: return login_form(response) # They are logged in, now we need to make sure they have the privilages if response.has_privileges(*privileges) != []: if response.root: return no_access(missing=response.has_privileges(*privileges)) return no_access() # Try to get the page itself try: page_result = f(cursor, *args, **kwargs) except Exception as e: if common_f.cache['user'].root or False: print(error.html_render()) exit() else: return error.log_error(cursor, e, context=0) # Try logging the usage, if not we'll quietly log the error try: if config.get("log_usage") and common_f.get_val("ajax", False) == False: user_log.log_usage(cursor) except Exception as e: error.log_error(cursor, e) return page_result
def test_log_error(self): self.test_targets.append(error.log_error) cursor = database_f.get_test_cursor() # First we make sure there is no error query = """DELETE FROM errors""" try: cursor.execute(query) except Exception as e: raise Exception("Database error: %s\nQuery: %s" % (str(e.args[0]).replace("\n",""), query)) # We need to fake a user for the logging to work common_f.cache['user'] = user.User(id=1, username="******") # Fake args gui_test_utils.new_cgi_form(( ("mode", "list_users"), ("arg_1", 1), ("arg_3", 3), )) # Now we log the error try: raise Exception("This is an exception") except Exception as e: error.log_error(cursor, e, context=5) query = """SELECT * FROM errors""" try: cursor.execute(query) except Exception as e: raise Exception("Database error: %s\nQuery: %s" % (str(e.args[0]).replace("\n",""), query)) errors_found = [] for row in cursor: errors_found.append(row) self.assertEqual(len(errors_found), 1) self.assertEqual(row['user_id'], 1) self.assertEqual(row['args'], "mode = list_users\n\narg_1 = 1\n\narg_3 = 3") self.assertEqual(row['exception_type'], "Exception") self.assertEqual(row['mode'], "list_users")
def main(): # Need this so it uses the correct path try: for k, v in page_dict.items(): v[1] = v[1].replace("gui/", "{}/gui/".format(sys.path[0])) except Exception as e: print(error.html_render(context=1)) raise import cgitb cgitb.enable(context=1) cgitb.html = error.html_render # Override the shell patterns to output HTML instead cli_f.shell_patterns = cli_f.html_patterns # Connect to DB cursor = database_f.get_cursor() # Default to listing players m = common_f.get_val("mode", pages.default_page) the_page = import_page(m) output = [] try: page_results = the_page.main(cursor) except Exception as e: print("Content-type: text/html; charset=utf-8") print("") print( "There was an error executing the main function of the_page: %s" % str(the_page).replace("<", "<").replace(">", ">") ) print("<br /><br />") print(error.log_error(cursor, e, context=0)) return # Is this an AJAX request? ajax = bool(common_f.get_val("ajax", False)) # Redirect if page_results == "" and the_page.page_data.get("Redirect", "") != "": print("Location: {0}".format(the_page.page_data["Redirect"])) print("") return # Serving content print(the_page.page_data.get("Content-type", "Content-type: text/html; charset=utf-8")) print(html_f.cookies) print("") # Import the header/footer try: find = imp.find_module(the_page.page_data["Template"], ["templates"]) the_template = imp.load_module("the_template", *find) find[0].close() except KeyError: print(error.html_render(context=1, headers=False)) exit() except Exception: try: find[0].close() except Exception: pass print(error.html_render(context=1, headers=False)) print("<br /><br />Found template: {}<br />".format(the_page.page_data["Template"])) exit() # Headers if the_page.page_data.get("Headers", True) and page_results != "" and ajax != True: output.append(the_template.headers(cursor, the_page.page_data)) # Core output output.append(page_results) the_page.page_data["time"] = time.time() - start_time # Footers if the_page.page_data.get("Headers", True) and page_results != "" and ajax != True: output.append(the_template.footers(cursor, the_page.page_data)) output = de_unicode("".join([str(i) for i in output])) # We now want to print it out, sometimes there can be errors # related to unicode try: print(output) except UnicodeEncodeError as e: ignore_uni_errror = common_f.get_val("iue", 0) from profiteer import config try: f = open("%sutf8_out.html" % config.get("cache_path"), "w", encoding="utf-8") f.write(output) f.close() except Exception: pass if ignore_uni_errror: _print_ignoring_error(output) o = output print( "Unicode error at character %d, Ignore errors by adding '&iue=1', <a href='http://localhost/profiteer/utf8_out.html'>alternately view raw</a><br />" % e.start ) print( "%s<strong style='color:red;'>*</strong>%s" % ( o[e.start - 300 : e.start].replace("<", "<").replace(">", ">"), o[e.start + 1 : e.start + 20].replace("<", "<").replace(">", ">"), ) ) print("<br />") print(e.start, "<br />") print(dir(e)) exit() except Exception as e: raise