owls

diff Loginsys.class.php @ 13:3e3fa7725abb

reorganized filesystem strukture
author meillo@marmaro.de
date Sun, 27 May 2007 02:37:55 +0200
parents Includes/Loginsys.class.php@144bc36a6c27
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/Loginsys.class.php	Sun May 27 02:37:55 2007 +0200
     1.3 @@ -0,0 +1,102 @@
     1.4 +<?php
     1.5 +/**
     1.6 + * simple loginsystem for owls
     1.7 + *
     1.8 + * @brief simple loginsystem for owls
     1.9 + * @author Meillo  r e t u r n s <meillo@marmaro.de>
    1.10 + */
    1.11 +class Loginsys {
    1.12 +
    1.13 +  /// Consts
    1.14 +
    1.15 +  /**
    1.16 +   * time in seconds till the session expires
    1.17 +   */
    1.18 +  const TIME_SESSION_EXPIRES = 1800;
    1.19 +
    1.20 +
    1.21 +
    1.22 +  /// Constructors
    1.23 +
    1.24 +  /**
    1.25 +   * starts the session
    1.26 +   * and puts an activity timestamp in it
    1.27 +   */
    1.28 +  public function __construct() {
    1.29 +    session_start();
    1.30 +    $this->heartbeat();
    1.31 +  }
    1.32 +
    1.33 +
    1.34 +
    1.35 +
    1.36 +  /// Methods
    1.37 +
    1.38 +
    1.39 +  /**
    1.40 +   * trys to log in with the given login data
    1.41 +   *
    1.42 +   * @param $loginname username
    1.43 +   * @param $password_md5 md5 password hash
    1.44 +   */
    1.45 +  public function login($loginname, $password_md5) {    // login ------------------------------------
    1.46 +    $rowuser = mysql_fetch_array(mysql_query("select * from ". DB_PREFIX ."User where loginname='$loginname' and password='$password_md5' "));
    1.47 +    if (mysql_affected_rows() == 1) {  // valid login
    1.48 +      $_SESSION[login][id] = $rowuser[id];
    1.49 +      $_SESSION[login][loginname] = $loginname;
    1.50 +      $_SESSION[login][logintime] = time();
    1.51 +    }
    1.52 +  }
    1.53 +
    1.54 +
    1.55 +  /**
    1.56 +   * logs the user out
    1.57 +   */
    1.58 +  public function logout() {    // logout ------------------------------------------------------------
    1.59 +    unset($_SESSION[login]);
    1.60 +  }
    1.61 +
    1.62 +
    1.63 +  /**
    1.64 +   * proves if the user is logged in and if he wan't to long inactive
    1.65 +   *
    1.66 +   * @return bool 'true' if user is logged in, else 'false'
    1.67 +   */
    1.68 +  public function loggedIn() {    // return login-status ---------------------------------------------
    1.69 +    return (isset($_SESSION[login][id]) && $_SESSION[login][logintime] > time()-self::TIME_SESSION_EXPIRES);
    1.70 +  }
    1.71 +
    1.72 +
    1.73 +  /**
    1.74 +   * writes the current timestamp into the session
    1.75 +   * this is relevant for the time of inactivity
    1.76 +   */
    1.77 +  public function heartbeat() {
    1.78 +    if ($this->loggedIn()) {
    1.79 +      $_SESSION[login][logintime] = time();
    1.80 +    }
    1.81 +  }
    1.82 +
    1.83 +
    1.84 +
    1.85 +  /// Getter and Setter
    1.86 +
    1.87 +
    1.88 +  /**
    1.89 +   * @return user id
    1.90 +   */
    1.91 +  public function getUserId() {
    1.92 +    return $_SESSION[login][id];
    1.93 +  }
    1.94 +
    1.95 +  /**
    1.96 +   * @return user name
    1.97 +   */
    1.98 +  public function getLoginname() {
    1.99 +    return $_SESSION[login][loginname];
   1.100 +  }
   1.101 +
   1.102 +}
   1.103 +
   1.104 +
   1.105 +?>