Author Topic: sessions.php from 4images 1.7 => 1.7.2 with PHPKIT MOD  (Read 7286 times)

0 Members and 1 Guest are viewing this topic.

Offline Catman

  • Newbie
  • *
  • Posts: 21
    • View Profile
sessions.php from 4images 1.7 => 1.7.2 with PHPKIT MOD
« on: May 02, 2006, 08:46:35 PM »
I need help to change my sessions.php from 4images version 1.7 to 1.7.2

I use 4images in my PHPKIT with a single sign on. In version 1.7 all whas ok.
Now I will change my gallery in version 1.7.2. My problem is the Code in includes/sessions.php.
I test it by myself, but no chance. I am a PHP-noob.

If I use the old (1.7) sessions.php in 4images 1.7.2 it looks good, but I think it whas better to change it in 1.7.2.

Here is my old sessions.php, I have marked the MOD - Changes in the Code like /* ....... */

AND THE SAME IN GERMAN

Da ich nicht so gut in englisch bin, das ganze nochmal in deutsch.

Zur Zeit nutze ich 4images 1.7 in meinem PHPKIT mit einem MOD der mit einer Loginübernahme arbeitet.
Meine User brauchen sich so nur einmal anzumelden. Das funktioniert auch alles wunderbar.

Nun möchte ich aber 4images auf 1.7.2 umstellen. Hierbei habe ich als PHP-noob aber das Problem, das ich die sessions.php nicht umgesetzt bekomme. Habe es jetzt schon eine Woche probiert jedoch erfolglos.

Es wäre nett wenn jemand der sich besser damit auskennt, sich die Datei mal anschauen könnte.
Wenn ich diese "alte" Datei in 1.7.2 nutze klappt es auch, aber ich denke es wäre besser auch hier auf die neue Version umzustellen.
Zur besseren Ersichtlichkeit habe ich alle MOD-Änderungen im Code gekennzeichnet, ungefähr so /* ....... */

Code: [Select]
<?php
/**************************************************************************
 *                                                                        *
 *    4images - A Web Based Image Gallery Management System               *
 *    ----------------------------------------------------------------    *
 *                                                                        *
 *             File: sessions.php                                         *
 *        Copyright: (C) 2002 Jan Sorgalla                                *
 *            Email: jan@4homepages.de                                    *
 *              Web: http://www.4homepages.de                             *
 *    Scriptversion: 1.7                                                  *
 *     // [MOD] KIT4IMG   36 modifications //                             *
 *    Never released without support from: Nicky (http://www.nicky.net)   *
 *                                                                        *
 **************************************************************************
 *                                                                    *
 *    Dieses Script ist KEINE Freeware. Bitte lesen Sie die Lizenz-       *
 *    bedingungen (Lizenz.txt) für weitere Informationen.                 *
 *    ---------------------------------------------------------------     *
 *    This script is NOT freeware! Please read the Copyright Notice       *
 *    (Licence.txt) for further information.                              *
 *                                                                        *
 *************************************************************************/
if (!defined('ROOT_PATH')) {
  die("Security violation");
}

// modification= Start Configuration
//-----------------------------------------------------
//--- Start Configuration -----------------------------
//-----------------------------------------------------

define('SESSION_NAME''sessionid');

$user_table_fields = array(
  "user_id" => "user_id",
  "user_level" => "user_level",
  "user_name" => "user_name",
  "user_password" => "user_pw" /* mod1- "user_password" => "user_pw"   */
  "user_email" => "user_email",
  "user_showemail" => "user_emailshow" /* mod2- "user_showemail" => "user_emailshow"    */
  "user_allowemails" => "user_imoption" /* mod3- "user_allowemails" => "user_imoption"   */
  "user_invisible" => "user_ghost" /* mod4- "user_invisible" => "user_ghost"   */
  "user_joindate" => "signin " /* mod5- "user_joindate" => "signin "         */
  "user_activationkey" => "user_activationkey",
  "user_lastaction" => "user_lastaction",
  "user_location" => "user_location",
  "user_lastvisit" => "lastlog" /* mod6- "user_lastvisit" => "lastlog"           */
  "user_comments" => "user_comcount" /* mod7- "user_comments" => "user_comcount"       */
  "user_homepage" => "user_hpage" /* mod8- "user_homepage" => "user_hpage"    */
  "user_icq" => "user_icqid"  /* mod9- "user_icq" => "user_icqid"        */
);

//-----------------------------------------------------
//--- End Configuration -------------------------------
//-----------------------------------------------------

function get_user_table_field($add$user_field) {
  global $user_table_fields;
  return (!empty($user_table_fields[$user_field])) ? $add.$user_table_fields[$user_field] : "";
}

class 
Session {

  var $session_id;
  var $user_ip;
  var $user_location;
  var $current_time;
  var $session_timeout;
  var $mode "get";
  var $session_info = array();
  var $user_info = array();

  function Session() {
    global $config;
    $this->session_timeout $config['session_timeout'] * 60;
    $this->user_ip $this->get_user_ip();
    $this->user_location $this->get_user_location();
    $this->current_time time();
    $this->demand_session();
  }

  function set_cookie_data($name$value$permanent 1) {
    $cookie_expire = ($permanent) ? $this->current_time 60 60 24 365 0;
    $cookie_name COOKIE_NAME.$name;
    setcookie($cookie_name$value$cookie_expireCOOKIE_PATHCOOKIE_DOMAINCOOKIE_SECURE);
  }

  function read_cookie_data($name) {
    global $HTTP_COOKIE_VARS;
    $cookie_name COOKIE_NAME.$name;
    return (isset($HTTP_COOKIE_VARS[$cookie_name])) ? $HTTP_COOKIE_VARS[$cookie_name] : false;
  }

// modification= function get_session_id
  function get_session_id() {
    global $HTTP_GET_VARS$HTTP_POST_VARS;
    if ($this->session_id $this->read_cookie_data("PHPKITSID")) { /* mod10- "sid" =>  "PHPKITSID" */
      $this->mode "cookie";
    }
    else {
      if (isset($HTTP_GET_VARS[SESSION_NAME])) {
        $this->session_id $HTTP_GET_VARS[SESSION_NAME];
      }
      elseif (isset($HTTP_POST_VARS[SESSION_NAME])) {
        $this->session_id $HTTP_POST_VARS[SESSION_NAME];
      }
      else {
        $this->session_id false;
      }
    }
  }

// modification= function demand_session
  function demand_session() {
    $this->get_session_id();
    if (!$this->load_session_info()) {
      $this->delete_old_sessions();
      $user_id = ($this->read_cookie_data("userid")) ? intval($this->read_cookie_data("userid")) : GUEST;
      $this->start_session($user_id);
    }
    else {
      $this->user_info $this->load_user_info($this->session_info['session_user_id']);
      $update_cutoff = ($this->user_info['user_id'] != GUEST) ? $this->current_time $this->user_info['user_lastaction'] : $this->current_time $this->session_info['session_lastaction'];
      if ($update_cutoff 60) {
        $this->update_session();
        $this->delete_old_sessions();
      }
    }
  }

// modification= function start_session
  function start_session($user_id GUEST$login_process 0) {
    global $site_db;

    $this->user_info $this->load_user_info($user_id);
    if ($this->user_info['user_id'] != GUEST && !$login_process) {
      if ($this->read_cookie_data("user_pw") == $this->user_info['user_password'] && $this->user_info['user_level'] > USER_AWAITING) { /* mod11- "userpass" => "user_pw" */
        $this->set_cookie_data("user_pw"$this->user_info['user_password']);  /* mod12- "userpass" => "user_pw" */
      }
      else {
        $this->set_cookie_data("user_pw"""0);  /* mod13- "userpass" => "user_pw" */
        $this->user_info $this->load_user_info(GUEST);
      }
    }

    if (!$login_process) {
      $this->session_id $this->generate_session_id();
      $sql "INSERT INTO ".SESSIONS_TABLE."
              (session_id, session_userid, session_expire, session_url, session_ip)
              VALUES
              ('
$this->session_id', ".$this->user_info['user_id'].", $this->current_time, '$this->user_location', '$this->user_ip')" /* mod14- (session_id, session_user_id, session_lastaction, session_location, session_ip) => (session_id, session_userid, session_expire, session_url, session_ip) */
      $site_db->query($sql);
      $this->session_info['session_user_id'] = $this->user_info['user_id'];
      $this->session_info['session_lastaction'] = $this->current_time;
      $this->session_info['session_location'] = $this->user_location;
      $this->session_info['session_ip'] = $this->user_ip;
      $this->set_cookie_data("PHPKITSID"$this->session_id0);  /* mod15- "sid" => "PHPKITSID" */
    }

    if ($this->user_info['user_id'] != GUEST) {
      $this->user_info['user_lastvisit'] = (!empty($this->user_info['user_lastaction'])) ? $this->user_info['user_lastaction'] : $this->current_time;
      $sql "UPDATE ".USERS_TABLE."
              SET "
.get_user_table_field("""user_lastaction")." = $this->current_time, ".get_user_table_field("""user_location")." = '$this->user_location', ".get_user_table_field("""user_lastvisit")." = ".$this->user_info['user_lastvisit']."
              WHERE "
.get_user_table_field("""user_id")." = ".$this->user_info['user_id'];
      $site_db->query($sql);
    }
    $this->set_cookie_data("lastvisit"$this->user_info['user_lastvisit']);
    $this->set_cookie_data("user_id"$this->user_info['user_id']);  /*mod16- "userid" => "user_id" */
    return true;
  }

// modification= function login
  function login($user_name ""$user_password ""$auto_login 0$set_auto_login 1) {
    global $site_db$user_table_fields;

    if (empty($user_name) || empty($user_password)) {
      return false;
    }
    $sql "SELECT ".get_user_table_field("""user_id").get_user_table_field(", ""user_password")."
            FROM "
.USERS_TABLE."
            WHERE "
.get_user_table_field("""user_name")." = '$user_name' AND ".get_user_table_field("""user_level")." <> ".USER_AWAITING;
    $row $site_db->query_firstrow($sql);

    $user_id = (isset($row[$user_table_fields['user_id']])) ? $row[$user_table_fields['user_id']] : GUEST;
    $user_password md5($user_password);
    if ($user_id != GUEST) {
      if ($row[$user_table_fields['user_password']] == $user_password) {
        $ip_sql = ($this->mode == "get") ? " AND session_ip = '$this->user_ip'" "";
        $sql "UPDATE ".SESSIONS_TABLE."
                SET session_userid = 
$user_id
                WHERE session_id = '
$this->session_id'
                
$ip_sql";
        $site_db->query($sql);
        if ($set_auto_login) {
          $this->set_cookie_data("user_pw", ($auto_login) ? $user_password "");  /* mod17- "userpass" => "user_pw" */
        }
        $this->start_session($user_id1);
        return true;
      }
    }
    return false;
  }

// modification= function logout
  function logout($user_id) {
    global $site_db;
    $sql "DELETE FROM ".SESSIONS_TABLE."
            WHERE session_id = '
$this->session_id' OR session_userid = $user_id" /* mod18- session_user_id => session_userid */
    $site_db->query($sql);
    $this->set_cookie_data("user_pw"""0);  /* mod19- "userpass" => "user_pw" */
    $this->set_cookie_data("user_id"GUEST);  /* mod20- "userid" => "user_id" */
    return true;
  }

// modification= function delete_old_sessions
  function delete_old_sessions() {
    global $site_db;
    $expiry_time $this->current_time $this->session_timeout;
    $sql "DELETE FROM ".SESSIONS_TABLE."
            WHERE session_expire < 
$expiry_time" /* mod21- session_lastaction => session_expire */
    $site_db->query($sql);

    $sql "SELECT session_id
            FROM "
.SESSIONS_TABLE;
    $result $site_db->query($sql);
    if ($result) {
      $session_ids_sql "";
      while ($row $site_db->fetch_array($result)) {
        $session_ids_sql .= (($session_ids_sql != "") ? ", " "") . "'".$row['session_id']."'";
      }
    }
    if (!empty($session_ids_sql)) {
      $sql "DELETE FROM ".SESSIONVARS_TABLE."
              WHERE session_id NOT IN (
$session_ids_sql)";
      $site_db->query($sql);
    }
    return true;
  }

// modification= function update_session
  function update_session() {
    global $site_db;
    $ip_sql = ($this->mode == "get") ? " AND session_ip = '$this->user_ip'" "";
    $sql "UPDATE ".SESSIONS_TABLE."
            SET session_expire = 
$this->current_time, session_url = '$this->user_location' /* mod22- session_lastaction => session_expire  ,  session_location => session_url */
            WHERE session_id = '
$this->session_id'
            
$ip_sql";
    $site_db->query($sql);
    if ($this->user_info['user_id'] != GUEST) {
      $sql "UPDATE ".USERS_TABLE."
              SET "
.get_user_table_field("""user_lastaction")." = $this->current_time, ".get_user_table_field("""user_location")." = '$this->user_location'
              WHERE "
.get_user_table_field("""user_id")." = ".$this->user_info['user_id'];
      $site_db->query($sql);
    }
    return;
  }

  function generate_session_id() {
    global $site_db;
    $sid md5(uniqid(microtime()));
    $i 0;
    while ($i == 0) {
      $sql "SELECT session_id
              FROM "
.SESSIONS_TABLE."
              WHERE session_id = '
$sid'";
      if ($site_db->is_empty($sql)) {
        $i 1;
      }
      else {
        $i 0;
        $sid md5(uniqid(microtime()));
      }
    }
    return $sid;
  }

  function return_session_info() {
    return $this->session_info;
  }

  function return_user_info() {
    return $this->user_info;
  }

  function freeze() {
    return;
  }

// modification= function load_session_info
  function load_session_info() {
    global $site_db;
    if (!$this->session_id) {
      return false;
    }
    $ip_sql = ($this->mode == "get") ? " AND session_ip = '$this->user_ip'" "";
    $this->session_info = array();
    $sql "SELECT session_id, session_expire, session_url, session_ip, session_userid /* mod23- session_lastaction, session_location => session_expire, session_url */
            FROM "
.SESSIONS_TABLE."
            WHERE session_id = '
$this->session_id'
            
$ip_sql";
    $this->session_info $site_db->query_firstrow($sql);
    $this->session_info['session_user_id'] = $this->session_info['session_userid'];  /* mod24- KIT4IMG <= */
    $this->session_info['session_lastaction'] = $this->session_info['session_expire'];  /* mod25- KIT4IMG <= */
    $this->session_info['session_location'] = $this->session_info['session_url'];  /* mod26- KIT4IMG <= */
    if (!isset($this->session_info['session_user_id'])) {
      return false;
    }
    else {
      $sql "SELECT sessionvars_name, sessionvars_value
              FROM "
.SESSIONVARS_TABLE."
              WHERE session_id = '
$this->session_id'";
      $result $site_db->query($sql);
      while ($row $site_db->fetch_array($result)) {
        $this->session_info[$row['sessionvars_name']] = $row['sessionvars_value'];
      }
      return $this->session_info;
    }
  }

  function load_user_info($user_id GUEST) {
    global $site_db$user_table_fields;

    if ($user_id != GUEST) {
      $sql "SELECT u.*, l.*
              FROM "
.USERS_TABLE." u, ".LIGHTBOXES_TABLE." l
              WHERE "
.get_user_table_field("u.""user_id")." = $user_id AND l.user_id = ".get_user_table_field("u.""user_id");
      $user_info $site_db->query_firstrow($sql);
      if (!$user_info) {
        $sql "SELECT *
                FROM "
.USERS_TABLE."
                WHERE "
.get_user_table_field("""user_id")." = $user_id";
        $user_info $site_db->query_firstrow($sql);
        if ($user_info) {
          $lightbox_id get_random_key(LIGHTBOXES_TABLE"lightbox_id");
          $sql "INSERT INTO ".LIGHTBOXES_TABLE."
                  (lightbox_id, user_id, lightbox_lastaction, lightbox_image_ids)
                  VALUES
                  ('
$lightbox_id', ".$user_info[$user_table_fields['user_id']].", $this->current_time, '')";
          $site_db->query($sql);
          $user_info['lightbox_lastaction'] = $this->current_time;
          $user_info['lightbox_image_ids'] = "";
        }
      }
    }
    if (empty($user_info[$user_table_fields['user_id']])) {
      $user_info = array();
      $user_info['user_id'] = GUEST;
      $user_info['user_level'] = GUEST;
      $user_info['user_lastaction'] = $this->current_time;
      $user_info['user_lastvisit'] = ($this->read_cookie_data("lastvisit")) ? $this->read_cookie_data("lastvisit") : $this->current_time;
    }
    foreach ($user_table_fields as $key => $val) {
      if (isset($user_info[$val])) {
        $user_info[$key] = $user_info[$val];
      }
      elseif (!isset($user_info[$key])) {
        $user_info[$key] = "";
      }
    }
    return $user_info;
  }

  function set_session_var($var_name$value) {
    global $site_db;
    $sql "SELECT session_id
            FROM "
.SESSIONVARS_TABLE."
            WHERE sessionvars_name = '
$var_name' AND session_id = '$this->session_id'";
    if ($site_db->is_empty($sql)) {
      $sql "INSERT INTO ".SESSIONVARS_TABLE."
              (session_id, sessionvars_name, sessionvars_value)
              VALUES
              ('
$this->session_id', '$var_name', '$value')";
      $site_db->query($sql);
    }
    else {
      $sql "UPDATE ".SESSIONVARS_TABLE."
              SET sessionvars_value = '
$value'
              WHERE sessionvars_name = '
$var_name' AND session_id = '$this->session_id'";
      $site_db->query($sql);
    }
    $this->session_info[$var_name] = $value;
    return true;
  }

  function get_session_var($var_name) {
    global $site_db;
    if (isset($this->session_info[$var_name])) {
      return $this->session_info[$var_name];
    }
    else {
      $sql "SELECT sessionvars_value
              FROM "
.SESSIONVARS_TABLE."
              WHERE sessionvars_name = '
$var_name' AND session_id = '$this->session_id'";
      $value $site_db->query_firstrow($sql);
      if ($value) {
        $this->session_info[$var_name] = $value['sessionvars_value'];
        return $value['sessionvars_value'];
      }
      else {
        return "";
      }
    }
  }

  function drop_session_var($var_name) {
    global $site_db;
    $sql "DELETE FROM ".SESSIONVARS_TABLE."
            WHERE sessionvars_name = '
$var_name' AND session_id = '$this->session_id'";
    return ($site_db->query($sql)) ? 0;
  }

  function get_user_ip() {
    global $HTTP_SERVER_VARS$HTTP_ENV_VARS;
    $ip = (!empty($HTTP_SERVER_VARS['REMOTE_ADDR'])) ? $HTTP_SERVER_VARS['REMOTE_ADDR'] : ((!empty($HTTP_ENV_VARS['REMOTE_ADDR'])) ? $HTTP_ENV_VARS['REMOTE_ADDR'] : getenv("REMOTE_ADDR"));
    $ip preg_replace("/[^\.0-9]+/"""$ip);
    return substr($ip050);
  }

  function get_user_location() {
    global $self_url;
    return (defined("IN_CP")) ? "Control Panel" preg_replace(array("/([?|&])action=[^?|&]*/""/([?|&])mode=[^?|&]*/""/([?|&])phpinfo=[^?|&]*/""/([?|&])printstats=[^?|&]*/""/[?|&]".URL_ID."=[^?|&]*/""/[?|&]l=[^?|&]*/""/[&?]+$/"), array(""""""""""""""), addslashes($self_url));
  }

  function url($url$amp "&amp;") {
    global $l;
    $dummy_array explode("#"$url);
    $url $dummy_array[0];

    if ($this->mode == "get" && !preg_match("/".SESSION_NAME."=/i"$url)) {
      $url .= preg_match("/\?/"$url) ? "$amp"?";
      $url .= SESSION_NAME."=".$this->session_id;
    }

    if (!empty($l)) {
      $url .= preg_match("/\?/"$url) ? "$amp"?";
      $url .= "l=".$l;
    }

    $url .= (isset($dummy_array[1])) ? "#".$dummy_array[1] : "";
    return $url;
  }
//end of class

// modification= Start Session
//-----------------------------------------------------
//--- Start Session -----------------------------------
//-----------------------------------------------------
define('COOKIE_NAME'$phpkit_cookie_name);  /* mod27- '4images_' => $phpkit_cookie_name */
define('COOKIE_PATH'$phpkit_cookie_path);  /* mod28- '/' => $phpkit_cookie_path */
define('COOKIE_DOMAIN'$phpkit_cookie_domain);  /* mod29- '' => $phpkit_cookie_domain */
define('COOKIE_SECURE'$phpkit_cookie_secure);  /* mod30- '0' => $phpkit_cookie_secure */

$site_sess = new Session();

// Get Userinfo
$session_info $site_sess->return_session_info();
$user_info $site_sess->return_user_info();

// modification= Get User Caches
//-----------------------------------------------------
//--- Get User Caches ---------------------------------
//-----------------------------------------------------
$num_total_online 0;
$num_visible_online 0;
$num_invisible_online 0;
$num_registered_online 0;
$num_guests_online 0;
$user_online_list "";
$prev_user_ids = array();
$prev_session_ips = array();

if (
defined("GET_USER_ONLINE") && ($config['display_whosonline'] == || $user_info['user_level'] == ADMIN)) {
  $time_out time() - 300;
  $sql "SELECT s.session_userid, s.session_expire, s.session_ip".get_user_table_field(", u.""user_id").get_user_table_field(", u.""user_level").get_user_table_field(", u.""user_name").get_user_table_field(", u.""user_invisible"). /* mod31- s.session_user_id, s.session_lastaction => s.session_userid, s.session_expire */
  FROM "
.SESSIONS_TABLE." s
  LEFT JOIN "
.USERS_TABLE." u ON (".get_user_table_field("u.""user_id")." = s.session_userid)
  WHERE s.session_expire >= 
$time_out  /* mod32- s.session_lastaction => s.session_expire */
  ORDER BY "
.get_user_table_field("u.""user_id")." ASC, s.session_ip ASC";
  $result $site_db->query($sql);
  while ($row $site_db->fetch_array($result)) {
    $row['session_user_id'] = $row['session_userid'];  /* mod33- KIT4IMG <= */
    $row['session_lastaction'] = $row['session_expire'];  /*mod34- KIT4IMG <= */
    $row['session_location'] = $row['session_url'];  /*mod35- KIT4IMG <= */
    if ($row['session_user_id'] != GUEST && (isset($row['user_id']) && $row['user_id'] != GUEST)) {
      if (!isset($prev_user_ids[$row['session_user_id']])) {
        $is_invisible = (isset($row[$user_table_fields['user_invisible']]) && $row[$user_table_fields['user_invisible']] == 1) ? 0;
        $invisibleuser = ($is_invisible) ? "*" "";
        $username = (isset($row['user_level']) && $row['user_level'] == ADMIN && $config['highlight_admin'] == 1) ? sprintf("<b>%s</b>"$row[$user_table_fields['user_name']]) : $row[$user_table_fields['user_name']];  /* mod37- isset($row[$user_table_fields['user_level']]) && $row[$user_table_fields['user_level']] => isset($row['user_level']) && $row['user_level'] */
        if (!$is_invisible || $user_info['user_level'] == ADMIN) {
          $user_online_list .= ($user_online_list != "") ? ", " "";
          $user_profile_link = (!empty($url_show_profile)) ? preg_replace("/{user_id}/"$row['session_user_id'], $url_show_profile) : ROOT_PATH."member.php?action=showprofile&amp;".URL_USER_ID."=".$row['session_user_id'];
          $user_online_list .= "<a href=\"".$site_sess->url($user_profile_link)."\">".$username."</a>".$invisibleuser;
        }
        (!$is_invisible) ? $num_visible_online++ : $num_invisible_online++;
        $num_registered_online++;
      }
      $prev_user_ids[$row['session_user_id']] = 1;
    }
    else {
      if (!isset($prev_session_ips[$row['session_ip']])) {
        $num_guests_online++;
      }
    }
    $prev_session_ips[$row['session_ip']] = 1;
  }
  $num_total_online $num_registered_online $num_guests_online;
  //$num_invisible_online = $num_registered_online - $num_visible_online;  // original //

  $site_template->register_vars(array(
    "num_total_online" => $num_total_online,
    "num_invisible_online" => $num_invisible_online,
    "num_registered_online" => $num_registered_online,
    "num_guests_online" => $num_guests_online,
    "user_online_list" => $user_online_list
  
));
  $whos_online $site_template->parse_template("whos_online");
  $site_template->register_vars("whos_online"$whos_online);
  unset($whos_online);
  unset($prev_user_ids);
  unset($prev_session_ips);
}
?>


Im vorraus schon mal allerbesten Dank für Eure Hilfe.

Thank for your help.

Offline Catman

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: sessions.php from 4images 1.7 => 1.7.2 with PHPKIT MOD
« Reply #1 on: May 13, 2006, 02:52:18 PM »
Nach langem probieren habe ich es nun soweit geschafft, das alles zu funktionieren scheint. Aber...

Login und Logout klappen nur wenn ich meinen IE schließe und neu starte.
Mache ich das nicht, werde ich in der whois-online-Anzeige zwar richtig angezeigt, doch das User-Level scheint dann nicht richtig zu sein, da mir der Leuchtkasten auch als Gast angezeigt wird.

Außerdem habe ich einen MOD mit automatischem Design-Wechsel eingebaut (abgeleitet vom Members Template Select-MOD), da ist der selbe Effekt.

Woran kann das nur liegen, bin völlig am verzweifeln.

(mein englisch ist jetzt nicht so gut das alles zu übersetzen, hoffe das ich mich so wenigstens verständlich genug ausgedrückt habe)

Offline V@no

  • If you don't tell me what to do, I won't tell you where you should go :)
  • Global Moderator
  • 4images Guru
  • *****
  • Posts: 17.849
  • mmm PHP...
    • View Profile
    • 4images MODs Demo
Re: sessions.php from 4images 1.7 => 1.7.2 with PHPKIT MOD
« Reply #2 on: May 14, 2006, 09:29:33 PM »
what is the problem?
Your first three "must do" before you ask a question:
Please do not PM me asking for help unless you've been specifically asked to do so. Such PMs will be deleted without answer. (forum rule #6)
Extension for Firefox/Thunderbird: Master Password+    Back/Forward History Tweaks (restartless)    Cookies Manager+    Fit Images (restartless for Thunderbird)

Offline Catman

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: sessions.php from 4images 1.7 => 1.7.2 with PHPKIT MOD
« Reply #3 on: May 15, 2006, 06:33:48 AM »
Hi, Vano

My problem is the login and logout. It`s only right when I close my browser and start it new. (IE & firefox)

I use 4images in my PHPKIT with a single sign on MOD. Now I want to change 4images from 1.7 => 1.7.2.

The used files for this MOD are config.php, includes/constants.php, includes/sessions.php and admin/home.php.

Only the includes/sessions.php is my problem (I think), the code looks to different for me ("REPLACE INTO") to transport the MOD in this new file.

After two weeks of testing, it looks so:

I can login in 4images with my PHPKIT-login and can use the ACP, at the whoisonline all looks fine.
Then I logout, whoisonline is right, but the lightbox looks as I whas login. ( I can see it too by my Userdesign-Changing-MOD, my own variant of the Members Templates Select-MOD)
It only takes the right effect, if I close my browser and start him new.
The cookie looks right, I mean it´s the looks like the cookie with 1.7. The cachsystem are not activated.
For these tests I use xampp on my WinXP-PC.

Sorry for my bad english, I hope you understand what I mean.
This is my latest sessions test-file (with comment lines):

Code: [Select]
<?php
/**************************************************************************
 *                                                                        *
 *    4images - A Web Based Image Gallery Management System               *
 *    ----------------------------------------------------------------    *
 *                                                                        *
 *             File: sessions.php                                         *
 *        Copyright: (C) 2002 Jan Sorgalla                                *
 *            Email: jan@4homepages.de                                    *
 *              Web: http://www.4homepages.de                             *
 *    Scriptversion: 1.7.2                                                *
 *    [MOD] KIT4IMG                                                       *
 *    Never released without support from: Nicky (http://www.nicky.net)   *
 *                                                                        *
 **************************************************************************
 *                                                                        *
 *    Dieses Script ist KEINE Freeware. Bitte lesen Sie die Lizenz-       *
 *    bedingungen (Lizenz.txt) für weitere Informationen.                 *
 *    ---------------------------------------------------------------     *
 *    This script is NOT freeware! Please read the Copyright Notice       *
 *    (Licence.txt) for further information.                              *
 *                                                                        *
 *************************************************************************/
if (!defined('ROOT_PATH')) {
  die("Security violation");
}

//-----------------------------------------------------
//--- Start Configuration -----------------------------
//-----------------------------------------------------
// [MOD] KIT4IMG : "user_password" => "user_pw" | "user_showemail" => "user_emailshow" | "user_allowemails" => "user_imoption" | "user_invisible" => "user_ghost" | "user_joindate" => "signin " | "user_lastvisit" => "lastlog" | "user_comments" => "user_comcount" | "user_homepage" => "user_hpage" | "user_icq" => "user_icqid"

define('SESSION_NAME''sessionid');

$user_table_fields = array(
  "user_id" => "user_id",
  "user_level" => "user_level",
  "user_name" => "user_name",
  "user_password" => "user_pw",
  "user_email" => "user_email",
  "user_showemail" => "user_emailshow",
  "user_allowemails" => "user_imoption",
  "user_invisible" => "user_ghost",
  "user_joindate" => "signin ",
  "user_activationkey" => "user_activationkey",
  "user_lastaction" => "user_lastaction",
  "user_location" => "user_location",
  "user_lastvisit" => "lastlog",
  "user_comments" => "user_comcount",
  "user_homepage" => "user_hpage",
  "user_icq" => "user_icqid"
);

//-----------------------------------------------------
//--- End Configuration -------------------------------
//-----------------------------------------------------

function get_user_table_field($add$user_field) {
  global $user_table_fields;
  return (!empty($user_table_fields[$user_field])) ? $add.$user_table_fields[$user_field] : "";
}

class 
Session {

  var $session_id;
  var $user_ip;
  var $user_location;
  var $current_time;
  var $session_timeout;
  var $mode "get";
  var $session_info = array();
  var $user_info = array();

  function Session() {
    global $config;
    $this->session_timeout $config['session_timeout'] * 60;
    $this->user_ip $this->get_user_ip();
    $this->user_location $this->get_user_location();
    $this->current_time time();

    // Stop adding SID to URLs
    @ini_set('session.use_trans_sid'0);

    //@ini_set('session.cookie_lifetime', $this->session_timeout);

    session_name(urlencode(SESSION_NAME));
    session_start();

    $this->demand_session();
  }

  function set_cookie_data($name$value$permanent 1) {
    $cookie_expire = ($permanent) ? $this->current_time 60 60 24 365 0;
    $cookie_name COOKIE_NAME.$name;
    setcookie($cookie_name$value$cookie_expireCOOKIE_PATHCOOKIE_DOMAINCOOKIE_SECURE);
  }

  function read_cookie_data($name) {
    global $HTTP_COOKIE_VARS;
    $cookie_name COOKIE_NAME.$name;
    return (isset($HTTP_COOKIE_VARS[$cookie_name])) ? $HTTP_COOKIE_VARS[$cookie_name] : false;
  }

  function get_session_id() {
[
color=Red]    if (SID == '') { // 'PHPKITSID'
//    if ($this->session_id = $this->read_cookie_data("PHPKITSID")) { //////////////////////////////////[/color]
      $this->mode "cookie";
    }

    $this->session_id session_id();
  }

  function demand_session() {
    $this->get_session_id();
    if (!$this->load_session_info()) {
      $this->delete_old_sessions();
      $user_id = ($this->read_cookie_data("user_id")) ? intval($this->read_cookie_data("user_id")) : GUEST;
      $this->start_session($user_id);
    }
    else {
      $this->user_info $this->load_user_info($this->session_info['session_user_id']);
      $update_cutoff = ($this->user_info['user_id'] != GUEST) ? $this->current_time $this->user_info['user_lastaction'] : $this->current_time $this->session_info['session_lastaction'];
      if ($update_cutoff 60) {
        $this->update_session();
        $this->delete_old_sessions();
      }
    }
  }

  function start_session($user_id GUEST$login_process 0) {
    global $site_db;

    $this->user_info $this->load_user_info($user_id);
    if ($this->user_info['user_id'] != GUEST && !$login_process) {
      if ($this->read_cookie_data("user_pw") === $this->user_info['user_password'] && $this->user_info['user_level'] > USER_AWAITING) {
        $this->set_cookie_data("user_pw"$this->user_info['user_password']);
      }
      else {
        $this->set_cookie_data("user_pw"""0);
        $this->user_info $this->load_user_info(GUEST);
      }
    }

    //if (!$login_process) {
      $sql "REPLACE INTO ".SESSIONS_TABLE."
              (session_id, session_userid, session_expire, session_url, session_ip)
              VALUES
              ('
$this->session_id', ".$this->user_info['user_id'].", $this->current_time, '$this->user_location', '$this->user_ip')";
      $site_db->query($sql);
    //}

    $this->session_info['session_user_id'] = $this->user_info['user_id'];
    $this->session_info['session_lastaction'] = $this->current_time;
    $this->session_info['session_location'] = $this->user_location;
    $this->session_info['session_ip'] = $this->user_ip;
[
color=Red]//      $this->set_cookie_data("PHPKITSID", $this->session_id, 0); //////////////////////////////////////////////[/color]
    if ($this->user_info['user_id'] != GUEST) {
      $this->user_info['user_lastvisit'] = (!empty($this->user_info['user_lastaction'])) ? $this->user_info['user_lastaction'] : $this->current_time;
      $sql "UPDATE ".USERS_TABLE."
              SET "
.get_user_table_field("""user_lastaction")." = $this->current_time, ".get_user_table_field("""user_location")." = '$this->user_location', ".get_user_table_field("""user_lastvisit")." = ".$this->user_info['user_lastvisit']."
              WHERE "
.get_user_table_field("""user_id")." = ".$this->user_info['user_id'];
      $site_db->query($sql);
    }
    $this->set_cookie_data("lastvisit"$this->user_info['user_lastvisit']);
    $this->set_cookie_data("user_id"$this->user_info['user_id']);
    return true;
  }

  function login($user_name ""$user_password ""$auto_login 0$set_auto_login 1) {
    global $site_db$user_table_fields;

    if (empty($user_name) || empty($user_password)) {
      return false;
    }
    $sql "SELECT ".get_user_table_field("""user_id").get_user_table_field(", ""user_password")."
            FROM "
.USERS_TABLE."
            WHERE "
.get_user_table_field("""user_name")." = '$user_name' AND ".get_user_table_field("""user_level")." <> ".USER_AWAITING;
    $row $site_db->query_firstrow($sql);

    $user_id = (isset($row[$user_table_fields['user_id']])) ? $row[$user_table_fields['user_id']] : GUEST;
    $user_password md5($user_password);
    if ($user_id != GUEST) {
      if ($row[$user_table_fields['user_password']] == $user_password) {
        $sql "UPDATE ".SESSIONS_TABLE."
                SET session_userid = 
$user_id
                WHERE session_id = '
$this->session_id'";
        $site_db->query($sql);
        if ($set_auto_login) {
          $this->set_cookie_data("user_pw", ($auto_login) ? $user_password "");
        }
        $this->start_session($user_id1);
        return true;
      }
    }
    return false;
  }

  function logout($user_id) {
    global $site_db;
    $sql "DELETE FROM ".SESSIONS_TABLE."
            WHERE session_id = '
$this->session_id' OR session_userid = $user_id";
    $site_db->query($sql);
    $this->set_cookie_data("user_pw"""0);
    $this->set_cookie_data("user_id"GUEST);

    $this->session_info = array();

    return true;
  }

  function delete_old_sessions() {
    global $site_db;
    $expiry_time $this->current_time $this->session_timeout;
    $sql "DELETE FROM ".SESSIONS_TABLE."
            WHERE session_expire < 
$expiry_time";
    $site_db->query($sql);

    return true;
  }

  function update_session() {
    global $site_db;

$sql "REPLACE INTO ".SESSIONS_TABLE."
           (session_id, session_userid, session_expire, session_url, session_ip)
           VALUES
           ('
$this->session_id', ".$this->user_info['user_id'].", $this->current_time, '$this->user_location', '$this->user_ip')";
    $site_db->query($sql);

    $this->session_info['session_lastaction'] = $this->current_time;
    $this->session_info['session_location'] = $this->user_location;
    $this->session_info['session_ip'] = $this->user_ip;

    if ($this->user_info['user_id'] != GUEST) {
      $sql "UPDATE ".USERS_TABLE."
              SET "
.get_user_table_field("""user_lastaction")." = $this->current_time, ".get_user_table_field("""user_location")." = '$this->user_location'
              WHERE "
.get_user_table_field("""user_id")." = ".$this->user_info['user_id'];
      $site_db->query($sql);
    }
    return;
  }

  function generate_session_id() {
    return session_id();
  }

  function return_session_info() {
    return $this->session_info;
  }

  function return_user_info() {
    return $this->user_info;
  }

  function freeze() {
    return;
  }

  function load_session_info() {
    if (@ini_get('register_globals')) {
      session_register('__session');

      if (!isset($GLOBALS['__session'])) {
        $GLOBALS['__session'] = array();
      }

      $this->session_info = &$GLOBALS['__session'];

    } else {
      if (isset($_SESSION)) {
        if (!isset($_SESSION['__session'])) {
          $_SESSION['__session'] = array();
        }

        $this->session_info = &$_SESSION['__session'];

      } else {
        if (!isset($GLOBALS['HTTP_SESSION_VARS']['__session'])) {
          $GLOBALS['HTTP_SESSION_VARS']['__session'] = array();
        }

        $this->session_info = &$GLOBALS['HTTP_SESSION_VARS']['__session'];
      }
    }

    if (!isset($this->session_info['session_ip'])) {
      $this->session_info = array();
      return false;
    }

    if ($this->mode == "get" && $this->session_info['session_ip'] != $this->user_ip) {
      if (function_exists('session_regenerate_id')) {
        @session_regenerate_id();
      }
      $this->get_session_id();
      $this->session_info = array();
      return false;
    }

    return $this->session_info;
  }

  function load_user_info($user_id GUEST) {
    global $site_db$user_table_fields;

    if ($user_id != GUEST) {
      $sql "SELECT u.*, l.*
              FROM "
.USERS_TABLE." u, ".LIGHTBOXES_TABLE." l
              WHERE "
.get_user_table_field("u.""user_id")." = $user_id AND l.user_id = ".get_user_table_field("u.""user_id");
      $user_info $site_db->query_firstrow($sql);
      if (!$user_info) {
        $sql "SELECT *
                FROM "
.USERS_TABLE."
                WHERE "
.get_user_table_field("""user_id")." = $user_id";
        $user_info $site_db->query_firstrow($sql);
        if ($user_info) {
          $lightbox_id get_random_key(LIGHTBOXES_TABLE"lightbox_id");
          $sql "INSERT INTO ".LIGHTBOXES_TABLE."
                  (lightbox_id, user_id, lightbox_lastaction, lightbox_image_ids)
                  VALUES
                  ('
$lightbox_id', ".$user_info[$user_table_fields['user_id']].", $this->current_time, '')";
          $site_db->query($sql);
          $user_info['lightbox_lastaction'] = $this->current_time;
          $user_info['lightbox_image_ids'] = "";
        }
      }
    }
    if (empty($user_info[$user_table_fields['user_id']])) {
      $user_info = array();
      $user_info['user_id'] = GUEST;
      $user_info['user_level'] = GUEST;
      $user_info['user_lastaction'] = $this->current_time;
      $user_info['user_lastvisit'] = ($this->read_cookie_data("lastvisit")) ? $this->read_cookie_data("lastvisit") : $this->current_time;
    }
    foreach ($user_table_fields as $key => $val) {
      if (isset($user_info[$val])) {
        $user_info[$key] = $user_info[$val];
      }
      elseif (!isset($user_info[$key])) {
        $user_info[$key] = "";
      }
    }
    return $user_info;
  }

  function set_session_var($var_name$value) {
    $this->session_info[$var_name] = $value;
    return true;
  }

  function get_session_var($var_name) {
    if (isset($this->session_info[$var_name])) {
      return $this->session_info[$var_name];
    }

    return '';
  }

  function drop_session_var($var_name) {
    unset($this->session_info[$var_name]);
  }

  function get_user_ip() {
    global $HTTP_SERVER_VARS$HTTP_ENV_VARS;
    $ip = (!empty($HTTP_SERVER_VARS['REMOTE_ADDR'])) ? $HTTP_SERVER_VARS['REMOTE_ADDR'] : ((!empty($HTTP_ENV_VARS['REMOTE_ADDR'])) ? $HTTP_ENV_VARS['REMOTE_ADDR'] : getenv("REMOTE_ADDR"));
    $ip preg_replace("/[^\.0-9]+/"""$ip);
    return substr($ip050);
  }

  function get_user_location() {
    global $self_url;
    return (defined("IN_CP")) ? "Control Panel" preg_replace(array("/([?|&])action=[^?|&]*/""/([?|&])mode=[^?|&]*/""/([?|&])phpinfo=[^?|&]*/""/([?|&])printstats=[^?|&]*/""/[?|&]".URL_ID."=[^?|&]*/""/[?|&]l=[^?|&]*/""/[&?]+$/"), array(""""""""""""""), addslashes($self_url));
  }

  function url($url$amp "&amp;") {
    global $l;
    $dummy_array explode("#"$url);
    $url $dummy_array[0];

    if ($this->mode == "get" && strpos($url$this->session_id) === false) {
      $url .= strpos($url'?') !== false $amp "?";
      $url .= SESSION_NAME."=".$this->session_id;
    }

    if (!empty($l)) {
      $url .= strpos($url'?') !== false $amp "?";
      $url .= "l=".$l;
    }

    $url .= (isset($dummy_array[1])) ? "#".$dummy_array[1] : "";
    return $url;
  }
//end of class

//-----------------------------------------------------
//--- Start Session -----------------------------------
//-----------------------------------------------------
define('COOKIE_NAME'$phpkit_cookie_name);
define('COOKIE_PATH'$phpkit_cookie_path);
define('COOKIE_DOMAIN'$phpkit_cookie_domain);
define('COOKIE_SECURE'$phpkit_cookie_secure);

$site_sess = new Session();

// Get Userinfo
$session_info $site_sess->return_session_info();
$user_info $site_sess->return_user_info();

//-----------------------------------------------------
//--- Get User Caches ---------------------------------
//-----------------------------------------------------
$num_total_online 0;
$num_visible_online 0;
$num_invisible_online 0;
$num_registered_online 0;
$num_guests_online 0;
$user_online_list "";
$prev_user_ids = array();
$prev_session_ips = array();

// [MOD] PHPKIT Change Design -->
//-----------------------------------------------------
//--- Templates ---------------------------------------
//-----------------------------------------------------

$dir opendir(ROOT_PATH.TEMPLATE_DIR);
$templates_cache = array();
while(
$dir_cache readdir($dir))
{
  if (@is_dir(ROOT_PATH.TEMPLATE_DIR."/".$dir_cache) && $dir_cache != "." && $dir_cache != "..")
  {
    $templates_cache[] = $dir_cache;
  }
}
closedir($dir);

$user_info['user_design'] = (isset($HTTP_GET_VARS['user_design'])) ? $HTTP_GET_VARS['user_design'] : ((isset($HTTP_POST_VARS['user_design'])) ? $HTTP_POST_VARS['user_design'] : $user_info['user_design']);
$user_template = ($user_info['user_design']) ? ((in_array($user_info['user_design'], $templates_cache)) ? $user_info['user_design'] : $config['template_dir']) : $config['template_dir'];
define('TEMPLATE_PATH'ROOT_PATH.TEMPLATE_DIR."/".$user_template);
define('ICON_PATH'ROOT_PATH.TEMPLATE_DIR."/".$user_template."/icons");
include(
ROOT_PATH.'includes/template.php');
$site_template = new Template(TEMPLATE_PATH);
//--- End Templates -----------------------------------
// [MOD] PHPKIT Change Design <--

if (defined("GET_USER_ONLINE") && ($config['display_whosonline'] == || $user_info['user_level'] == ADMIN)) {
  $time_out time() - 300;
  $sql "SELECT s.session_userid, s.session_expire, s.session_ip".get_user_table_field(", u.""user_id").get_user_table_field(", u.""user_level").get_user_table_field(", u.""user_name").get_user_table_field(", u.""user_invisible")."
  FROM "
.SESSIONS_TABLE." s
  LEFT JOIN "
.USERS_TABLE." u ON (".get_user_table_field("u.""user_id")." = s.session_userid)
  WHERE s.session_expire >= 
$time_out
  ORDER BY "
.get_user_table_field("u.""user_id")." ASC, s.session_ip ASC";
  $result $site_db->query($sql);
  while ($row $site_db->fetch_array($result)) {
    $row['session_user_id'] = $row['session_userid'];
    $row['session_lastaction'] = $row['session_expire'];
    $row['session_location'] = $row['session_url'];
    if ($row['session_user_id'] != GUEST && (isset($row[$user_table_fields['user_id']]) && $row[$user_table_fields['user_id']] != GUEST)) {
      if (!isset($prev_user_ids[$row['session_user_id']])) {
        $is_invisible = (isset($row[$user_table_fields['user_invisible']]) && $row[$user_table_fields['user_invisible']] == 1) ? 0;
        $invisibleuser = ($is_invisible) ? "*" "";
        $username = (isset($row[$user_table_fields['user_level']]) && $row[$user_table_fields['user_level']] == ADMIN && $config['highlight_admin'] == 1) ? sprintf("<b>%s</b>"$row[$user_table_fields['user_name']]) : $row[$user_table_fields['user_name']];
        if (!$is_invisible || $user_info['user_level'] == ADMIN) {
          $user_online_list .= ($user_online_list != "") ? ", " "";
          $user_profile_link = (!empty($url_show_profile)) ? preg_replace("/{user_id}/"$row['session_user_id'], $url_show_profile) : ROOT_PATH."member.php?action=showprofile&amp;".URL_USER_ID."=".$row['session_user_id'];
          $user_online_list .= "<a href=\"".$site_sess->url($user_profile_link)."\">".$username."</a>".$invisibleuser;
        }
        (!$is_invisible) ? $num_visible_online++ : $num_invisible_online++;
        $num_registered_online++;
      }
      $prev_user_ids[$row['session_user_id']] = 1;
    }
    else {
      if (!isset($prev_session_ips[$row['session_ip']])) {
        $num_guests_online++;
      }
    }
    $prev_session_ips[$row['session_ip']] = 1;
  }
  $num_total_online $num_registered_online $num_guests_online;
  //$num_invisible_online = $num_registered_online - $num_visible_online;

  $site_template->register_vars(array(
    "num_total_online" => $num_total_online,
    "num_invisible_online" => $num_invisible_online,
    "num_registered_online" => $num_registered_online,
    "num_guests_online" => $num_guests_online,
    "user_online_list" => $user_online_list,
    "lang_user_online" => str_replace('{num_total_online}'$num_total_online$lang['user_online']),
    "lang_user_online_detail" => str_replace(array('{num_registered_online}','{num_invisible_online}','{num_guests_online}'), array($num_registered_online,$num_invisible_online,$num_guests_online), $lang['user_online_detail']),
  ));
  $whos_online $site_template->parse_template("whos_online");
  $site_template->register_vars("whos_online"$whos_online);
  unset($whos_online);
  unset($prev_user_ids);
  unset($prev_session_ips);
}
?>

I hope you can help me to get this right.
I think my biggest problems are the parts function Session() , function get_session_id() , //if (!$login_process) , function update_session()
Where must I set the "PHPKITSID"

Edit:
Login and logout takes only effect if I close my browser and start him new!
« Last Edit: May 15, 2006, 08:42:15 PM by Catman »

Offline Catman

  • Newbie
  • *
  • Posts: 21
    • View Profile
Integration PHPKIT 1.6.03 with 4images 1.7.2
« Reply #4 on: May 29, 2006, 05:12:53 PM »
Sorry that I ask onemore...

Have anyone finished the integration PHPKIT 1.6.03 with 4images 1.7.2 ???

Or another idea.

It is possible to use the sessions.php and the home.php from 1.7 in 1.7.2 ?

It seems ok... but is it? :roll: