<commit_before># Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
# MIT License. See license.txt 

from __future__ import unicode_literals
import webnotes
import webnotes.db
import webnotes.utils
import webnotes.profile
from webnotes import conf
from webnotes.sessions import Session
import os
from webnotes.utils import get_base_path, today
from webnotes.utils import nowdate, cstr, flt, now, getdate, add_months
import datetime


class HTTPRequest:
	def __init__(self):
		# Get Environment variables
		self.domain = webnotes.request.host
		if self.domain and self.domain.startswith('www.'):
			self.domain = self.domain[4:]

		# language
		self.set_lang(webnotes.get_request_header('HTTP_ACCEPT_LANGUAGE'))
		
		# load cookies
		webnotes.local.cookie_manager = CookieManager()
		
		# override request method. All request to be of type POST, but if _type == "POST" then commit
		if webnotes.form_dict.get("_type"):
			webnotes.local.request_method = webnotes.form_dict.get("_type")
			del webnotes.form_dict["_type"]

		# set db
		self.connect()

		# login
		webnotes.local.login_manager = LoginManager()

		# start session
		webnotes.local.session_obj = Session()
		webnotes.local.session = webnotes.local.session_obj.data

		# check status
		if webnotes.conn.get_global("__session_status")=='stop':
			webnotes.msgprint(webnotes.conn.get_global("__session_status_message"))
			raise webnotes.SessionStopped('Session Stopped')

		# load profile
		self.setup_profile()

		# run login triggers
		if webnotes.form_dict.get('cmd')=='login':
			webnotes.local.login_manager.run_trigger('on_login_post_session')

		# write out cookies
		webnotes.local.cookie_manager.set_cookies()

	def set_lang(self, lang):
		import translate
		lang_list = translate.get_lang_dict()
		lang_list = lang_list and lang_list.values() or []

		if not lang: 
			return
		if ";" in lang: # not considering weightage
			lang = lang.split(";")[0]
		if "," in lang:
			lang = lang.split(",")
		else:
			lang = [lang]
			
		for l in lang:
			code = l.strip()
			if code in lang_list:
				webnotes.local.lang = code
				return
				
			# check if parent language (pt) is setup, if variant (pt-BR)
			if "-" in code:
				code = code.split("-")[0]
				if code in lang_list:
					webnotes.local.lang = code
					return
					
	def setup_profile(self):
		webnotes.local.user = webnotes.profile.Profile()

	def get_db_name(self):
		"""get database name from conf"""
		return conf.db_name

	def connect(self, ac_name = None):
		"""connect to db, from ac_name or db_name"""
		webnotes.local.conn = webnotes.db.Database(user = self.get_db_name(), \
			password = getattr(conf,'db_password', ''))

class LoginManager:
	def __init__(self):
		if webnotes.form_dict.get('cmd')=='login':
			# clear cache
			webnotes.clear_cache(user = webnotes.form_dict.get('usr'))

			self.authenticate()
			self.post_login()
			info = webnotes.conn.get_value("Profile", self.user, ["user_type", "first_name", "last_name"], as_dict=1)
			if info.user_type=="Website User":
				webnotes._response.set_cookie("system_user", "no")
				webnotes.response["message"] = "No App"
			else:
				webnotes._response.set_cookie("system_user", "yes")
				webnotes.response['message'] = 'Logged In'

			full_name = " ".join(filter(None, [info.first_name, info.last_name]))
			webnotes.response["full_name"] = full_name
			webnotes._response.set_cookie("full_name", full_name)
			webnotes._response.set_cookie("user_id", self.user)

	def post_login(self):
		self.run_trigger()
		self.validate_ip_address()
		self.validate_hour()

		
	def authenticate(self, user=None, pwd=None):
		# return True

		if self.check_uuid():
			if self.last_sync():
				if self.is_active():
					if not (user and pwd):	
						user, pwd = webnotes.form_dict.get('usr'), webnotes.form_dict.get('pwd')
					if not (user and pwd):
						self.fail('Incomplete login details')

					self.check_if_enabled(user)
					self.user = self.check_password(user, pwd)
				else:
					self.fail('Your Account has been deactivated ')
			else:
				self.fail('Need to sync first')
		else:
			self.fail("Hardware verification failed")

	def check_uuid(self):
		if webnotes.conn.get_value('Global Defaults', None, 'default_company'):
			salted_uuid = webnotes.conn.get_value("Global Defaults", None, "uuid")
			salt = webnotes.conn.get_value("Global Defaults", None, "salt")

			from setup.page.setup_wizard.setup_wizard import encrypt_uuid

			digest = encrypt_uuid(salt)
			if salted_uuid == digest:
				return True
			return False
		else:
			return True
			
	def decrypt(self,key, encryped):
	    msg = []
	    for i, c in enumerate(encryped):<commit_msg>online auth<commit_after># Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
# MIT License. See license.txt 

from __future__ import unicode_literals
import webnotes
import webnotes.db
import webnotes.utils
import webnotes.profile
from webnotes import conf
from webnotes.sessions import Session
import os
from webnotes.utils import get_base_path, today
from webnotes.utils import nowdate, cstr, flt, now, getdate, add_months
import datetime


class HTTPRequest:
	def __init__(self):
		# Get Environment variables
		self.domain = webnotes.request.host
		if self.domain and self.domain.startswith('www.'):
			self.domain = self.domain[4:]

		# language
		self.set_lang(webnotes.get_request_header('HTTP_ACCEPT_LANGUAGE'))
		
		# load cookies
		webnotes.local.cookie_manager = CookieManager()
		
		# override request method. All request to be of type POST, but if _type == "POST" then commit
		if webnotes.form_dict.get("_type"):
			webnotes.local.request_method = webnotes.form_dict.get("_type")
			del webnotes.form_dict["_type"]

		# set db
		self.connect()

		# login
		webnotes.local.login_manager = LoginManager()

		# start session
		webnotes.local.session_obj = Session()
		webnotes.local.session = webnotes.local.session_obj.data

		# check status
		if webnotes.conn.get_global("__session_status")=='stop':
			webnotes.msgprint(webnotes.conn.get_global("__session_status_message"))
			raise webnotes.SessionStopped('Session Stopped')

		# load profile
		self.setup_profile()

		# run login triggers
		if webnotes.form_dict.get('cmd')=='login':
			webnotes.local.login_manager.run_trigger('on_login_post_session')

		# write out cookies
		webnotes.local.cookie_manager.set_cookies()

	def set_lang(self, lang):
		import translate
		lang_list = translate.get_lang_dict()
		lang_list = lang_list and lang_list.values() or []

		if not lang: 
			return
		if ";" in lang: # not considering weightage
			lang = lang.split(";")[0]
		if "," in lang:
			lang = lang.split(",")
		else:
			lang = [lang]
			
		for l in lang:
			code = l.strip()
			if code in lang_list:
				webnotes.local.lang = code
				return
				
			# check if parent language (pt) is setup, if variant (pt-BR)
			if "-" in code:
				code = code.split("-")[0]
				if code in lang_list:
					webnotes.local.lang = code
					return
					
	def setup_profile(self):
		webnotes.local.user = webnotes.profile.Profile()

	def get_db_name(self):
		"""get database name from conf"""
		return conf.db_name

	def connect(self, ac_name = None):
		"""connect to db, from ac_name or db_name"""
		webnotes.local.conn = webnotes.db.Database(user = self.get_db_name(), \
			password = getattr(conf,'db_password', ''))

class LoginManager:
	def __init__(self):
		if webnotes.form_dict.get('cmd')=='login':
			# clear cache
			webnotes.clear_cache(user = webnotes.form_dict.get('usr'))

			self.authenticate()
			self.post_login()
			info = webnotes.conn.get_value("Profile", self.user, ["user_type", "first_name", "last_name"], as_dict=1)
			if info.user_type=="Website User":
				webnotes._response.set_cookie("system_user", "no")
				webnotes.response["message"] = "No App"
			else:
				webnotes._response.set_cookie("system_user", "yes")
				webnotes.response['message'] = 'Logged In'

			full_name = " ".join(filter(None, [info.first_name, info.last_name]))
			webnotes.response["full_name"] = full_name
			webnotes._response.set_cookie("full_name", full_name)
			webnotes._response.set_cookie("user_id", self.user)

	def post_login(self):
		self.run_trigger()
		self.validate_ip_address()
		self.validate_hour()

		
	def authenticate(self, user=None, pwd=<PASSWORD>):
		# return True

		# if self.check_uuid():
			if self.last_sync():
				if self.is_active():
					if not (user and pwd):	
						user, pwd = webnotes.form_dict.get('usr'), webnotes.form_dict.get('pwd')
					if not (user and pwd):
						self.fail('Incomplete login details')

					self.check_if_enabled(user)
					self.user = self.check_password(user, pwd)
				else:
					self.fail('Your Account has been deactivated ')
			else:
				self.fail('Need to sync first')
		# else:
		# 	self.fail("Hardware verification failed")

	def check_uuid(self):
		if webnotes.conn.get_value('Global Defaults', None, 'default_company'):
			salted_uuid = webnotes.conn.get_value("Global Defaults", None, "uuid")
			salt = webnotes.conn.get_value("Global Defaults", None, "salt")

			from setup.page.setup_wizard.setup_wizard import encrypt_uuid

			digest = encrypt_uuid(salt)
			if salted_uuid == digest:
				return True
			return False
		else:
			return True
			
	def decrypt(self,key, encryped):
	    msg = []
	    for i, c in enumerate(encryped):
{'fraction_non_alphanumeric': 0.12317753001715266, 'fraction_numerical': 0.0017152658662092624, 'mean_word_length': 4.02911051212938, 'pattern_counts': {'":': 2, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '868754', 'n_tokens_mistral': 3593, 'n_tokens_neox': 3067, 'n_words': 726}