<commit_before><?php

 include_once SITE_ROOT.'/core/lib/lib_locate_ip.php';

 global $session_refreshed; $session_refreshed=FALSE;

 class Session extends Model {

  var $data;
  public function Construct() {
   $this->data=array();
  }

  // Creates a new session
  public function Create( $r_Auth ) {
   plog("Session::Create for User ID: ".$r_Auth);
   $now=strtotime('now');
   $data=array(
    "login"    => $now,
    "REFERRER" => getenv('HTTP_REFERER'),
    "IP"       => getenv('REMOTE_ADDR'),
//    "ip_info"  => get_ip_info( getenv('REMOTE_ADDR') ),
    "BROWSER"  => getenv('HTTP_USER_AGENT'),
    "r_Auth"   => $r_Auth,
    "status"   => 1,
    "refreshed"=> $now,
    "last_refreshed"=> $now
   );
   global $session_id,$auth;
   $session_id=$this->Insert( $data );
   plog('New session ID: '.vars($session_id));
   cook( "username", $auth['username'], timeout );
   cook( "session",  $session_id, timeout );
   return $session_id;
  }

  public function LoggedOut( $session ) {
   return intval($session['status'])===0 || $this->Timedout($session) || intval($session['logout']) > 0;
  }

  public function Timedout( $session ) {
   return (strtotime('now')-intval($session['refreshed']) >= timeout);
  }

  public function ActiveUsers() {
   $users=$this->Select('status=1');
   $active=array();
   foreach ($users as $session) {
    if ( !$this->LoggedOut($session) ) $active[]=$session;
    else $this->Set($session['ID'],array('status'=>0));
   }
   return $active;
  }

  public function Refresh( $session ) {
   global $no_session_refresh;
   global $session_refreshed;
   if ( $session_refreshed === FALSE )
   if ( $no_session_refresh === TRUE ) {} else {
    $this->Set( $session['ID'],
     array( "last_refreshed" => $session['refreshed'],
           "refreshed" => strtotime('now'),
           "requests" => intval($session['requests'])+1
     )
    );
    global $database;
    $a_model=new Auth($database);
    $a=$a_model->Get($session['r_Auth']);
    cook( "username", $a['username'], timeout );
    cook( "session",  $session['ID'], timeout );
   }
   $session_refreshed=TRUE;
  }

  // Tests if there is a current session, called in the bootstrap
  public function Active( $refresh=TRUE ) {
   global $session_model,$auth_model,$profile_model;
   global $is_logged_in;
   global $session;
   global $user;
   global $auth;
   $session=$this->Get( base64_decode($_COOKIE['session']) );
   if ( !is_array($session) || !isset($session['r_Auth']) ) return ($is_logged_in=false);
   if ( $this->LoggedOut($session) ) return ($is_logged_in=false);
   $auth=$auth_model->Get( $session['r_Auth'] );
   if ( !is_array($auth) || !isset($auth['r_Profile']) ) return ($is_logged_in=false);
   else $user=$profile_model->Get( $auth['r_Profile'] );
   if ( !is_array($user) ) return ($is_logged_in=false);
   if ( $auth_model->ACL('locked') ) {
    plog('Account is locked, logging user '.$auth['ID'].' off.');
    $this->Logout();
    $is_logged_in=false;
    Page::Redirect("login?m=4");
   }
   $this->Refresh($session);
   $url=current_page_url();
   // Ignore any ajaxy stuff
   if ( stripos($url,"ajax.") === FALSE ) $this->Set( $session['ID'], array( 'last_url'=>current_page_url() ) );
   return ($is_logged_in=true);
  }

  static function logged_in() { global $is_logged_in; return $is_logged_in; }

  // Redirect a user if they don't meet the security requirements for this page.
  function Security( $logged_in, $where_to_redirect="login", $acl=FALSE ) {
   global $is_logged_in;
   if ( boolval($logged_in) !== boolval($is_logged_in) ) {
    header( "Location: $where_to_redirect");
    return FALSE;
   }
   if ( $acl !== FALSE ) {
    global $auth;
    if ( Auth::ACL($auth['acl'],$acl) === FALSE ) {
     header( "Location: $where_to_redirect");
     return FALSE;
    }<commit_msg>support mobile clients<commit_after><?php

 include_once SITE_ROOT.'/core/lib/lib_locate_ip.php';

 global $session_refreshed; $session_refreshed=FALSE;

 class Session extends Model {

  var $data;
  public function Construct() {
   $this->data=array();
  }

  // Creates a new session
  public function Create( $r_Auth ) {
   plog("Session::Create for User ID: ".$r_Auth);
   $now=strtotime('now');
   $data=array(
    "login"    => $now,
    "REFERRER" => getenv('HTTP_REFERER'),
    "IP"       => getenv('REMOTE_ADDR'),
//    "ip_info"  => get_ip_info( getenv('REMOTE_ADDR') ),
    "BROWSER"  => getenv('HTTP_USER_AGENT'),
    "r_Auth"   => $r_Auth,
    "status"   => 1,
    "refreshed"=> $now,
    "last_refreshed"=> $now
   );
   global $session_id,$auth;
   $session_id=$this->Insert( $data );
   plog('New session ID: '.vars($session_id));
   cook( "username", $auth['username'], timeout );
   cook( "session",  $session_id, timeout );
   return $session_id;
  }

  public function LoggedOut( $session ) {
   return intval($session['status'])===0 || $this->Timedout($session) || intval($session['logout']) > 0;
  }

  public function Timedout( $session ) {
   return (strtotime('now')-intval($session['refreshed']) >= timeout);
  }

  public function ActiveUsers() {
   $users=$this->Select('status=1');
   $active=array();
   foreach ($users as $session) {
    if ( !$this->LoggedOut($session) ) $active[]=$session;
    else $this->Set($session['ID'],array('status'=>0));
   }
   return $active;
  }

  public function Refresh( $session ) {
   global $no_session_refresh;
   global $session_refreshed;
   if ( $session_refreshed === FALSE )
   if ( $no_session_refresh === TRUE ) {} else {
    $this->Set( $session['ID'],
     array( "last_refreshed" => $session['refreshed'],
           "refreshed" => strtotime('now'),
           "requests" => intval($session['requests'])+1
     )
    );
    global $database;
    $a_model=new Auth($database);
    $a=$a_model->Get($session['r_Auth']);
    cook( "username", $a['username'], timeout );
    cook( "session",  $session['ID'], timeout );
   }
   $session_refreshed=TRUE;
  }

  // Tests if there is a current session, called in the bootstrap
  public function Active( $refresh=TRUE ) {
   global $session_model,$auth_model,$profile_model;
   global $is_logged_in;
   global $session;
   global $user;
   global $auth;
   $session=$this->Get( base64_decode($_COOKIE['session']) );
   if ( !is_array($session) || !isset($session['r_Auth']) ) return ($is_logged_in=false);
   if ( $this->LoggedOut($session) ) return ($is_logged_in=false);
   $auth=$auth_model->Get( $session['r_Auth'] );
   if ( !is_array($auth) || !isset($auth['r_Profile']) ) return ($is_logged_in=false);
   else $user=$profile_model->Get( $auth['r_Profile'] );
   if ( !is_array($user) ) return ($is_logged_in=false);
   if ( $auth_model->ACL('locked') ) {
    plog('Account is locked, logging user '.$auth['ID'].' off.');
    $this->Logout();
    $is_logged_in=false;
    Page::Redirect("login?m=4");
   }
   $this->Refresh($session);
   $url=current_page_url();
   // Ignore any ajaxy stuff
   if ( stripos($url,"ajax.") === FALSE ) $this->Set( $session['ID'], array( 'last_url'=>current_page_url() ) );
   return ($is_logged_in=true);
  }
  
    // Tests if there is a current session, called when you don't have a cookie to depend on (mobile client)
  public function isActive( $id, $refresh=TRUE ) {
   global $session_model,$auth_model,$profile_model;
   global $is_logged_in;
   global $session;
   global $user;
   global $auth;
   $session=$this->Get( $id );
   if ( !is_array($session) || !isset($session['r_Auth']) ) return ($is_logged_in=false);
   if ( $this->LoggedOut($session) ) return ($is_logged_in=false);
   $auth=$auth_model->Get( $session['r_Auth'] );
   if ( !is_array($auth) || !isset($auth['r_Profile']) ) return ($is_logged_in=false);
   else $user=$profile_model->Get( $auth['r_Profile'] );
   if ( !is_array($user) ) return ($is_logged_in=false);
   if ( $auth_model->ACL('locked') ) {
    plog('Account is locked, logging user '.$auth['ID'].' off.');
    $this->Logout();
    return ($is_logged_in=false);
   }
   $this->Refresh($session);
   $url=current_page_url();
   // Ignore any ajaxy stuff
   if ( stripos($url,"ajax.") === FALSE ) $this->Set( $session['ID'], array( 'last_url'=>current_page$
   return ($is_logged_in=true);
  }

  static function logged_in() { global $is_logged_in; return $is_logged_in; }

  // Redirect a user if they don't meet the security requirements for this page.
  function Security( $logged_in, $where_to_redirect="login", $acl=FALSE ) {
   global $is_logged_in;
   if ( boolval($logged_in) !== boolval($is_logged_in) ) {
    header( "Location: $where_to_redirect");
    return FALSE;
   }
   if ( $acl !== FALSE ) {
    global $auth;
    if ( Auth::ACL($auth['acl'],$acl) === FALSE ) {
     header( "Location: $where_to_redirect");
     return FALSE;
    }
{'fraction_non_alphanumeric': 0.18804198995892288, 'fraction_numerical': 0.002053856686444546, 'mean_word_length': 4.0431530494821635, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 76, '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': '6227936', 'n_tokens_mistral': 3093, 'n_tokens_neox': 3048, 'n_words': 614}