All pastes #793894 Raw Edit

Unbenannt

public php v1 · immutable
·published 2007-11-22 18:48 UTC
rendered paste body
<?php	class mc_Session {		private $_userdata = false, $_logindata = false, $_session_id = false;				/******************************************		* php session starten und cookie in klasse aufnehmen		*/		public final function __construct($cookie) {			session_start();			if($cookie !== false) {				$this->_userdata = $cookie;			}			@register_shutdown_function(array(&$this, '__setLocation'));		}				/*********************************************		* gast oder member		*/		public function isGuest() {			return (($this->_logindata === false) ? true : false);		}				/********************************************		* daten aus der session holen		* @param $w (gast index || ref name)		* @use 		*	ermitteln ob mitglied oder gast:		*		if(!$session->isGuest()) //mitglied		*		else //gast		*		*	für gäste:		*		print $session->get('username');		*		*	für mitglieder		*		$session->get('', $user);		*		print $user->username		*/		public function get($w) {			return $this->_userdata[$w];		}				public function fetchUser(&$userdata) {			if($this->_logindata === false) $userdata = false; 			else $userdata = $this->_logindata;		}				/********************************************		* session starten/wiederaufnehmen, aber erst wenn location geladen wurde!		* @param $db (ref obj. mc_Mysql)		*/		public function start(&$db) {			global $location; //holt $location aus dem globalen kontext			/*********************************************			* alte sessionid übernehmen, oder neue ermitteln			*/			$sid = ((!empty($this->_cookie['session'])) ? $db->escape($this->_cookie['session']) : session_id());			$this->_session_id = $sid;			$time = time();			if($this->_userdata === false) { //neuer gast				if($db->num_rows("						select id 						from {$db->prefix}online						where session = '{$sid}'					") == 0				) { //kein eintrag mit session vorhanden					$db->query("						insert into {$db->prefix}online (							userid,							session, 							time,							location_href,							location_title						) values (							0,							'{$sid}',							{$time},							'{$location['href']}',							'{$location['title']}'						);					");					$this->_userdata = array(						'username' => 'Gast',						'userid' => 0,						'password' => false,						'session' => $sid					);				}				$this->_set_cookie();			} else { //war schonmal hier				$this->_login($db); //einloggen				if($this->_logindata === false) { //kein mitglied					$db->query("						update {$db->prefix}online						set 							time = {$time},							location_href = '{$location['href']}',							location_title = '{$location['title']}'						where session = '{$sid}'					");				} else { //registrierter user					$db->query("						update {$db->prefix}online 						set							time = {$time},							location_href = '{$location['href']}',							location_title = '{$location['title']}',							session = '{$sid}'						where userid = {$this->_logindata->userid}					");				}								$this->_refresh_cookie();			}			/********************************************			* alte sessions löschen			*/			$this->_dropOldSessions($db);		}				/*************************************************		* überprüft die daten des cookies		* @param $db (ref obj. mc_Mysql)		*/		protected function _login($db) {			if(!empty($this->_userdata['username']) && !empty($this->_userdata['password'])) { //kein gast				$query = $db->query("					select 						userid,						username,						keep_login as set_cookie					from {$db->prefix}user					where						username = '{$db->escape($this->_userdata['username'])}'						&& password = sha1('{$db->escape($this->_userdata['password'])}')						&& active = 1						&& blocked != 1					limit 1				");								if($db->num_rows($query) !== 0) {					$this->_logindata = $db->fetch_object($query);				}			}		}				/*************************************************		* löscht alte sessions aus der db		* @param $db (ref obj. mc_Mysql)		*/		protected function _dropOldSessions(&$db) {			global $mc;			if($mc->config->session_timeout != 0) {				$db->query("					delete from {$db->prefix}online					where time <= (UNIX_TIMESTAMP() - {$mc->config->session_timeout})				");			}		}				/**************************************************		* setzt einen cookie (gast)		*/		protected function _set_cookie() {			setcookie(				'cc_board',				'',				time()+(60*60*1),				'/',				false,				true			);		}				/*********************************************		* setzt einen cookie (member)		*/		protected function _refresh_cookie() {			if($this->_logindata->set_cookie != 0) {				setcookie(					'cc_board',					"{$this->_userdata['username']}|{$this->_userdata['password']}|".					"{$this->_userdata['userid']}|".session_id(),					time()+(60*60*1),					'/',					true,					false				);			}		}				/********************************************		* shutdown :: update der location		*/		public function __setLocation() {			global $db, $location;			if(gettype($db) != 'object') exit('<br /><br />Abbruch! Hier stimmt was nicht!');			$db->query("				update {$db->prefix}online				set					location_href = '{$location['href']}',					location_title = '{$location['title']}'				where session = '{$this->_session_id}'			");		}	}?>