summaryrefslogtreecommitdiff
path: root/bit_setup_inc.php
blob: 0df8156371b22b969bacd2f0e9ac1171475741d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<?php
/**
 * @version $Header: /cvsroot/bitweaver/_bit_users/bit_setup_inc.php,v 1.49 2008/10/19 10:31:00 nickpalmer Exp $
 * @package users
 */
global $gBitDbType, $gBitDbHost, $gBitDbUser, $gBitDbPassword, $gBitDbName, $gBitThemes;

$registerHash = array(
	'package_name' => 'users',
	'package_path' => dirname( __FILE__ ).'/',
	'activatable' => FALSE,
	'required_package'=> TRUE,
);
$gBitSystem->registerPackage( $registerHash );

$gBitSystem->registerNotifyEvent( array( "user_registers" => tra( "A user registers" )));

if( !defined( 'AVATAR_MAX_DIM' )) {
	define( 'AVATAR_MAX_DIM', 100 );
}
if( !defined( 'PORTRAIT_MAX_DIM' )) {
	define( 'PORTRAIT_MAX_DIM', 300 );
}
if( !defined( 'LOGO_MAX_DIM' )) {
	define( 'LOGO_MAX_DIM', 600 );
}

require_once( USERS_PKG_PATH . 'BitPermUser.php' );
// a package can decide to override the default user class
$userClass = $gBitSystem->getConfig( 'user_class', 'BitPermUser' );
$gBitUser = new $userClass();


// set session lifetime
if( $gBitSystem->isFeatureActive( 'site_session_lifetime' )) {
	ini_set( 'session.gc_maxlifetime', $gBitSystem->isFeatureActive( 'site_session_lifetime' ));
}

// is session data stored in DB or in filesystem?
if( $gBitSystem->isFeatureActive( 'site_store_session_db' ) && !empty( $gBitDbType )) {
	include_once( UTIL_PKG_PATH . 'adodb/session/adodb-session.php' );
	ADODB_Session::dataFieldName( 'session_data' );
	ADODB_Session::driver( $gBitDbType );
	ADODB_Session::host( $gBitDbHost );
	ADODB_Session::user( $gBitDbUser );
	ADODB_Session::password( $gBitDbPassword );
	ADODB_Session::database( $gBitDbName );
	ADODB_Session::table( BIT_DB_PREFIX.'sessions' );
	ini_set( 'session.save_handler', 'user' );
}

session_name( BIT_SESSION_NAME );
if( $gBitSystem->isFeatureActive( 'users_remember_me' )) {
	session_set_cookie_params( $gBitSystem->getConfig( 'site_session_lifetime' ), $gBitSystem->getConfig( 'cookie_path', BIT_ROOT_URL ), $gBitSystem->getConfig( 'cookie_domain', '' ));
} else {
	session_set_cookie_params( $gBitSystem->getConfig( 'site_session_lifetime' ), BIT_ROOT_URL, '' );
}

// just use a simple COOKIE (unique random string) that is linked to the users_cnxn table.
// This way, nuking rows in the users_cnxn table can log people out and is much more reliable than SESSIONS
global $gShellScript;
if( empty( $gShellScript ) ) {
	session_start();
}
$cookie_site = strtolower( ereg_replace( "[^a-zA-Z0-9]", "", $gBitSystem->getConfig( 'site_title', 'bitweaver' )));
global $user_cookie_site;
$user_cookie_site = 'bit-user-'.$cookie_site;


// TODO: Remove this cookie munging business. This is a temporary fix that users don't have to log in again when they visit
// first we check to see if the 'bit' and the 'tiki' version of the cookie are present
if( !empty( $_COOKIE['tiki-user-'.$cookie_site] ) && !empty( $_COOKIE[$user_cookie_site] )) {
	setcookie( 'tiki-user-'.$cookie_site, $_COOKIE['tiki-user-'.$cookie_site], 1, $gBitSystem->getConfig( 'cookie_path', BIT_ROOT_URL ), $gBitSystem->getConfig( 'cookie_domain', '' ));
	unset( $_COOKIE['tiki-user-'.$cookie_site] );
}
// if the 'tiki' version is still set, we make sure that it's copied to the 'bit' version of the cookie
if( !empty( $_COOKIE['tiki-user-'.$cookie_site] )) {
	// here we can't check to see if a user has checked the rme button. this doesn't really matter since there's only _very_ few users who will be logging in exactly now
	if( $gBitSystem->isFeatureActive( 'users_remember_me' )) {
		$cookie_time = (int)(time() + $gBitSystem->getConfig( 'users_remember_time', 86400 ));
	} else {
		$cookie_time = 0;
	}
	// we copy the existing cookie accross
	setcookie( $user_cookie_site, $_COOKIE['tiki-user-'.$cookie_site], $cookie_time, $gBitSystem->getConfig( 'cookie_path', BIT_ROOT_URL ), $gBitSystem->getConfig( 'cookie_domain', '' ));
	$_COOKIE[$user_cookie_site] = $_COOKIE['tiki-user-'.$cookie_site];
}


// load the user
global $gOverrideLoginFunction;
if( !empty( $gOverrideLoginFunction )) {
	$gBitUser->mUserId = $gOverrideLoginFunction();
	if( $gBitUser->mUserId ) {
		$gBitUser->load();
		$gBitUser->loadPermissions();
	}
} elseif( !empty( $_COOKIE[$user_cookie_site] ) && ( $gBitUser->mUserId = $gBitUser->getUserIdFromCookieHash( $_COOKIE[$user_cookie_site] ))) {
	$gBitUser->load( TRUE );
} else {
	// Now if the remember me feature is on and the user checked the user_remember_me checkbox then ...
	if( $gBitSystem->isFeatureActive( 'users_remember_me' ) && isset( $_REQUEST['rme'] ) && $_REQUEST['rme'] == 'on' ) {
		$cookie_time = (int)(time() + $gBitSystem->getConfig( 'users_remember_time', 86400 ));
	} else {
		$cookie_time = 0;
	}

	setcookie( $user_cookie_site, session_id(), $cookie_time, $gBitSystem->getConfig( 'cookie_path', BIT_ROOT_URL ), $gBitSystem->getConfig( 'cookie_domain', '' ));
	// Some firewall software blocks cookies sent the above way so try this
	// way if that failed.
	if (empty($_COOKIE[$user_cookie_site])) {
		$_COOKIE[$user_cookie_site] = session_id();
	}

	// if the auth method is 'web site', look for the username in $_SERVER
	if( $gBitSystem->getConfig( 'users_auth_method', 'tiki' ) == 'ws' ) {
		if( isset( $_SERVER['REMOTE_USER'] )) {
			if( $gBitUser->userExists( $_SERVER['REMOTE_USER'] )) {
				$gBitUser->mUserId = $_SERVER['REMOTE_USER'];
				$gBitUser->load( TRUE );
			}
		}
	}
}

// if we still don't have a user loaded, we'll load the anonymous user
if( !$gBitUser->isValid() ) {
	$gBitUser->mUserId = ANONYMOUS_USER_ID;
	$gBitUser->load( TRUE );
}

if( isset( $_COOKIE[$user_cookie_site] )) {
	$gBitUser->updateSession( $_COOKIE[$user_cookie_site] );
} elseif( function_exists( "session_id" )) {
	$gBitUser->updateSession( session_id() );
}

$gBitSmarty->assign_by_ref( 'gBitUser', $gBitUser );

// If we are processing a login then do not generate the challenge
// if we are in any other case then yes.
if( !empty( $_SERVER["REQUEST_URI"] ) && !strstr( $_SERVER["REQUEST_URI"], USERS_PKG_URL.'validate' )) {
	if( $gBitSystem->isFeatureActive( 'feature_challenge' )) {
		$_SESSION["challenge"] = $gBitUser->generateChallenge();
	}
}

if( $gBitSystem->isFeatureActive( 'users_domains' )) {
	$domain = substr( $_SERVER['HTTP_HOST'], 0, strpos( $_SERVER['HTTP_HOST'], '.' ));
	if( $domain && $domain != $gBitSystem->getConfig( 'users_default_domain', 'www' )) {
		if( $gBitSystem->mDomainInfo = $gBitUser->getUserDomain( $domain )) {
			if( empty( $_REQUEST['user_id'] )) {
				$_REQUEST['user_id'] = $gBitSystem->mDomainInfo['user_id'];
			} elseif( empty( $_REQUEST['home'] )) {
				$_REQUEST['home'] = $gBitSystem->mDomainInfo['login'];
			}

			if( !empty( $_REQUEST['lookup_user_id'] )) {
				$_REQUEST['lookup_user_id'] = $gBitSystem->mDomainInfo['user_id'];
			}
		}
	}
}

// users_themes='y' is for the entire site, 'h' is just for users homepage and is dealt with on users/index.php
if( !empty( $gBitSystem->mDomainInfo['style'] )) {
	$theme = $gBitSystem->mDomainInfo['style'];
} elseif( $gBitSystem->getConfig( 'users_themes' ) == 'y' ) {
	if( $gBitUser->isRegistered() && $gBitSystem->isFeatureActive( 'users_preferences' )) {
		if( $userStyle = $gBitUser->getPreference( 'theme' )) {
			$theme = $userStyle;
		}
	}
	if( isset( $_COOKIE['bit-theme'] )) {
		$theme = $_COOKIE['bit-theme'];
	}
}

if( !empty( $theme )) {
	$gBitThemes->setStyle( $theme );
}

// register 'my' menu
if( $gBitUser->isValid() && ( $gBitUser->isRegistered() || !$gBitSystem->isFeatureActive( 'site_hide_my_top_bar_link' ))) {
	$menuHash = array(
		'package_name'  => USERS_PKG_NAME,
		'index_url'     => ( $gBitSystem->isFeatureActive( 'users_preferences' ) ? USERS_PKG_URL.'my.php' : '' ),
		'menu_title'    => 'My '.$gBitSystem->getConfig( 'site_menu_title', $gBitSystem->getConfig( 'site_title', 'Site' )),
		'menu_template' => 'bitpackage:users/menu_users.tpl',
	);
	$gBitSystem->registerAppMenu( $menuHash );
}

require_once( USERS_PKG_PATH.'BaseAuth.php' );

?>