<?php
	class Logger {
		private $db;
		
		function __construct() {
			global $db;
			
			$this->db = $db;
		}
		
		public function logUserAccess($user=null, $result=0, $msg='', $dataProvided=null) {
			
			if (is_array($user) && !empty($user)) {
				$user_id = $user['id'];
			} else {
				$user_id = 0;
			}
			
			$data = [
				'user_id' => $user_id,
				'remote_ip' => $_SERVER['REMOTE_ADDR'],
				'access_result' => $result,
				'access_message' => $msg,
				'data_provided' => json_encode($dataProvided),
				'time_zone' => date_default_timezone_get(),
				'user_agent' => $_SERVER['HTTP_USER_AGENT'],
				'created_at' => date('Y-m-d H:i:s')
			];
			
			$this->db->insert('log_access', $data);
		}
		
		public function logUserAction($userId=0, $actionType=null, $errorMsg=null) {
			$data = [
				'user_id' => $userId,
				'remote_ip' => $_SERVER['REMOTE_ADDR'],
				'action_type' => $actionType,
				'user_agent' => $_SERVER['HTTP_USER_AGENT'],
				'error_msg' => $errorMsg,
				'created_at' => date('Y-m-d H:i:s')
			];
			
			$this->db->insert('log_actions', $data);
		}
	}