<commit_before><?php

require_once('controlli.php');

class Auth {

	private static $user = null;
	private static $fb_user = null;
	private static $user_eliminato = false;
	private static $cookie = null;

	public static function init() {
	
		global $page;
		$logout_page_name = 'logout';

		if ($user = Auth::check_session()) {
			Auth::user_update($user);
			//if ($page != $logout_page_name) echo "check_session<br />";
			return;
		} else {
			Auth::clear_session();
		}
		
		if($user = Auth::check_post()) {
			Auth::user_update($user);
			//if ($page != $logout_page_name) echo "check_post<br />";
			return;
		}
		
		if($user = Auth::check_cookie()) {
			Auth::user_update($user);
			//if ($page != $logout_page_name) echo "check_cookie<br />";
			return;
		} else {
			Auth::clear_cookie();
		}
		
		if($user = Auth::check_session_fb()) {
			Auth::user_set_session_fb(Auth::$fb_user);
			Auth::user_update($user);
			//if ($page != $logout_page_name) echo "check_session_fb<br />";
			return;
		}
		
		//Auth::user_logout();

	}
	
	public static function check_session() {
		if (isset($_SESSION['user'])
			&& isset($_SESSION['user']['id_utente'])
			&& trim($_SESSION['user']['id_utente']) != ''
			&& ($temp_user = user_get($_SESSION['user']['id_utente'])))
		{
			/*if ($temp_user['id_fb'] == 0) {
				return $temp_user;
			} else if (isset($_SESSION['fb_user']) && isset($_SESSION['fb_user']['id']) && $temp_user['id_fb'] == $_SESSION['fb_user']['id']) {
				Auth::$fb_user = $_SESSION['fb_user'];
				return $temp_user;
			} else {
				return null;
			}*/
			
			if (isset($_SESSION['fb_user']) && isset($_SESSION['fb_user']['id']) && $temp_user['id_fb'] == $_SESSION['fb_user']['id']) {
				Auth::$fb_user = $_SESSION['fb_user'];
			}
			return $temp_user;
			
		} else {
			return null;
		}
	}
	
	public static function check_session_fb() {

		$fb_user = Auth::get_session_fb();

    if ($fb_user) { // L'utente è loggato su FB ed ha autorizzato la nostra app.
			if (!($user_data_with_fb_id = user_fb_get_from_db($fb_user['id']))) {
				// L'utente non è ancora presente nel DB, lo inserisco
				user_fb_insert($fb_user);
				$user_data_with_fb_id = user_fb_get_from_db($fb_user['id']);
				Auth::$fb_user = $fb_user;
				return user_get($user_data_with_fb_id['id_utente']);
			} else {
				if (!$user_data_with_fb_id['eliminato']) {
					Auth::$fb_user = $fb_user;
					return user_get($user_data_with_fb_id['id_utente']);
				} else {
					Auth::$user_eliminato = true;
					return null;
				}
			}
    } else {
			return null;
		}

	}
	
	public static function check_post() {
    if (isset($_POST['login_form'])
			&& isset($_POST['email'])
			&& isset($_POST['password'])
			&& ($temp_user = Auth::check_credentials($_POST['email'],$_POST['password'])))
		{

			$email = $_POST['email'];
			$password = $_POST['password'];
			$setcookie = (isset($_POST['restaCollegato']) && $_POST['restaCollegato'] == 'on') ? 1 : 0;

			if ($setcookie) Auth::user_set_cookie($email, $password);
			return $temp_user;

    } else return null;
	}
	
	public static function check_cookie() {

		if (($cookie = cookie_data_get())
		&& ($temp_user = Auth::check_credentials(cleanField($cookie['user_email']),cleanField($cookie['user_password'])))) {

			Auth::$cookie = $cookie;

			$email = cleanField($cookie['user_email']);
			$password = cleanField($cookie['user_password']);

			Auth::user_set_cookie($email, $password);
			return $temp_user;

		} else return null;
	}
	
	public static function check_credentials($email,$password) {

		$user_data_with_userpass = data_get('tab_utenti', array('email' => $email, 'password' => <PASSWORD>1($password), 'confermato' => 1));
		
		if (count($user_data_with_userpass)) {
			
      if (!$user_data_with_userpass[0]['eliminato']) {
				return user_get($user_data_with_userpass[0]['id_utente']);
      } else {
      	Auth::$user_eliminato = true;
        return null;
      }
		} else {
			return null;
		}

	}

	public static function get_session_fb() {
		$fb_user = user_fb_get();
		return $fb_user;
	}
	
	public static function user_update($user) {
		Auth::user_set_session($user);
		Auth::$user = $user;
	}
	
	public static function user_set_session($user) {
		$_SESSION['user'] = $user;
	}
	
	public static function user_set_session_fb($fb_user) {
		$_SESSION['fb_user'] = $fb_user;
		$_SESSION['fb_session'] = $fb_user['id'];
		$_SESSION['fb_access_token'] = user_fb_access_token();
	}
	
	public static function user_set_cookie($email, $password) {
		global $settings;
		setcookie("user_email", $email, time() + 60 * 60 * 24 * 100, "/", '.'.$settings['sito']['dominio']);
		setcookie("user_password", base64_encode($password), time() + 60 * 60 * 24 * 100, "/", '.'.$settings['sito']['dominio']);
	}
	
	public static function clear_session() {
		session_unset();		
    //unset($_SESSION['user']);
    //unset($_SESSION['fb_user']);
    //unset($_SESSION['fb_session']);
    //unset($_SESSION['fb_access_token']);
	}
	
	public static function clear_cookie() {
		global $settings;
    setcookie("user_email", FALSE, time() - 60 * 60 * 24, "/", '.'.$settings['sito']['dominio']);
    setcookie("user_password", FALSE, time() - 60 * 60 * 24, "/", '.'.$settings['sito']['dominio']);
	}<commit_msg>Nuovo sistema di autenticazione<commit_after><?php

require_once('controlli.php');

class Auth {

	private static $user = null;
	private static $fb_user = null;
	private static $user_eliminato = false;
	private static $cookie = null;

	public static function init() {

		if ($user = Auth::check_session()) {
			Auth::user_update($user);
			return;
		} else {
			Auth::clear_session();
		}
		
		if($user = Auth::check_post()) {
			Auth::user_update($user);
			return;
		}
		
		if($user = Auth::check_cookie()) {
			Auth::user_update($user);
			return;
		} else {
			Auth::clear_cookie();
		}
		
		if($user = Auth::check_session_fb()) {
			Auth::user_set_session_fb(Auth::$fb_user);
			Auth::user_update($user);
			return;
		}
		

	}
	
	public static function check_session() {
		if (isset($_SESSION['user'])
			&& isset($_SESSION['user']['id_utente'])
			&& trim($_SESSION['user']['id_utente']) != ''
			&& ($temp_user = user_get($_SESSION['user']['id_utente'])))
		{
			if (isset($_SESSION['fb_user']) && isset($_SESSION['fb_user']['id']) && $temp_user['id_fb'] == $_SESSION['fb_user']['id']) {
				Auth::$fb_user = $_SESSION['fb_user'];
			}
			return $temp_user;
			
		} else {
			return null;
		}
	}
	
	public static function check_session_fb() {

		$fb_user = Auth::get_session_fb();

    if ($fb_user) { // L'utente è loggato su FB ed ha autorizzato la nostra app.
			if (!($user_data_with_fb_id = user_fb_get_from_db($fb_user['id']))) {
				// L'utente non è ancora presente nel DB, lo inserisco
				user_fb_insert($fb_user);
				$user_data_with_fb_id = user_fb_get_from_db($fb_user['id']);
				Auth::$fb_user = $fb_user;
				return user_get($user_data_with_fb_id['id_utente']);
			} else {
				if (!$user_data_with_fb_id['eliminato']) {
					Auth::$fb_user = $fb_user;
					return user_get($user_data_with_fb_id['id_utente']);
				} else {
					Auth::$user_eliminato = true;
					return null;
				}
			}
    } else {
			return null;
		}

	}
	
	public static function check_post() {
    if (isset($_POST['login_form'])
			&& isset($_POST['email'])
			&& isset($_POST['password'])
			&& ($temp_user = Auth::check_credentials($_POST['email'],$_POST['password'])))
		{

			$email = $_POST['email'];
			$password = $_POST['password'];
			$setcookie = (isset($_POST['restaCollegato']) && $_POST['restaCollegato'] == 'on') ? 1 : 0;

			if ($setcookie) Auth::user_set_cookie($email, $password);
			return $temp_user;

    } else return null;
	}
	
	public static function check_cookie() {

		if (($cookie = cookie_data_get())
		&& ($temp_user = Auth::check_credentials(cleanField($cookie['user_email']),cleanField($cookie['user_password'])))) {

			Auth::$cookie = $cookie;

			$email = cleanField($cookie['user_email']);
			$password = cleanField($cookie['user_password']);

			Auth::user_set_cookie($email, $password);
			return $temp_user;

		} else return null;
	}
	
	public static function check_credentials($email,$password) {

		$user_data_with_userpass = data_get('tab_utenti', array('email' => $email, 'password' => <PASSWORD>($password), 'confermato' => 1));
		
		if (count($user_data_with_userpass)) {
			
      if (!$user_data_with_userpass[0]['eliminato']) {
				return user_get($user_data_with_userpass[0]['id_utente']);
      } else {
      	Auth::$user_eliminato = true;
        return null;
      }
		} else {
			return null;
		}

	}

	public static function get_session_fb() {
		$fb_user = user_fb_get();
		return $fb_user;
	}
	
	public static function user_update($user) {
		Auth::user_set_session($user);
		Auth::$user = $user;
	}
	
	public static function user_set_session($user) {
		$_SESSION['user'] = $user;
	}
	
	public static function user_set_session_fb($fb_user) {
		$_SESSION['fb_user'] = $fb_user;
		$_SESSION['fb_session'] = $fb_user['id'];
		$_SESSION['fb_access_token'] = user_fb_access_token();
	}
	
	public static function user_set_cookie($email, $password) {
		global $settings;
		setcookie("user_email", $email, time() + 60 * 60 * 24 * 100, "/", '.'.$settings['sito']['dominio']);
		setcookie("user_password", base64_encode($password), time() + 60 * 60 * 24 * 100, "/", '.'.$settings['sito']['dominio']);
	}
	
	public static function clear_session() {
		session_unset();
	}
	
	public static function clear_cookie() {
		global $settings;
    setcookie("user_email", FALSE, time() - 60 * 60 * 24, "/", '.'.$settings['sito']['dominio']);
    setcookie("user_password", FALSE, time() - 60 * 60 * 24, "/", '.'.$settings['sito']['dominio']);
	}
{'fraction_non_alphanumeric': 0.18921144739551163, 'fraction_numerical': 0.007823759522338892, 'mean_word_length': 4.317460317460317, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '6575132', 'n_tokens_mistral': 3910, 'n_tokens_neox': 3401, 'n_words': 604}