Exponential 6.0.13 Exponential Enterprise Open Source CMS - PHP Class Reference

eztextfileuser.php
Go to the documentation of this file.
1 <?php
26 class eZTextFileUser extends eZUser
27 {
28 public function __construct()
29 {
30 }
31
37 static function loginUser( $login, $password, $authenticationMatch = false )
38 {
39 $http = eZHTTPTool::instance();
40 $db = eZDB::instance();
41
42 if ( $authenticationMatch === false )
43 $authenticationMatch = eZUser::authenticationMatch();
44
45 $loginEscaped = $db->escapeString( $login );
46 $passwordEscaped = $db->escapeString( $password );
47
48 $loginArray = array();
49 if ( $authenticationMatch & eZUser::AUTHENTICATE_LOGIN )
50 $loginArray[] = "login='$loginEscaped'";
51 if ( $authenticationMatch & eZUser::AUTHENTICATE_EMAIL )
52 $loginArray[] = "email='$loginEscaped'";
53 if ( count( $loginArray ) == 0 )
54 $loginArray[] = "login='$loginEscaped'";
55 $loginText = implode( ' OR ', $loginArray );
56
57 $contentObjectStatus = eZContentObject::STATUS_PUBLISHED;
58
59 $ini = eZINI::instance();
60 $textFileIni = eZINI::instance( 'textfile.ini' );
61 $databaseName = $db->databaseName();
62 // if mysql
63 if ( $databaseName === 'mysql' )
64 {
65 $query = "SELECT contentobject_id, password_hash, password_hash_type, email, login
66 FROM ezuser, ezcontentobject
67 WHERE ( $loginText ) AND
68 ezcontentobject.status='$contentObjectStatus' AND
69 ( ezcontentobject.id=contentobject_id OR ( password_hash_type=4 AND ( $loginText ) AND password_hash=PASSWORD('$passwordEscaped') ) )";
70 }
71 else
72 {
73 $query = "SELECT contentobject_id, password_hash, password_hash_type, email, login
74 FROM ezuser, ezcontentobject
75 WHERE ( $loginText ) AND
76 ezcontentobject.status='$contentObjectStatus' AND
77 ezcontentobject.id=contentobject_id";
78 }
79
80 $users = $db->arrayQuery( $query );
81 $exists = false;
82 if ( count( $users ) >= 1 )
83 {
84 foreach ( $users as $userRow )
85 {
86 $userID = $userRow['contentobject_id'];
87 $hashType = $userRow['password_hash_type'];
88 $hash = $userRow['password_hash'];
89 $exists = eZUser::authenticateHash( $userRow['login'], $password, eZUser::site(),
90 $hashType,
91 $hash );
92
93 // If hash type is MySql
94 if ( $hashType == eZUser::PASSWORD_HASH_MYSQL and $databaseName === 'mysql' )
95 {
96 $queryMysqlUser = "SELECT contentobject_id, password_hash, password_hash_type, email, login
97 FROM ezuser, ezcontentobject
98 WHERE ezcontentobject.status='$contentObjectStatus' AND
99 password_hash_type=4 AND ( $loginText ) AND password_hash=PASSWORD('$passwordEscaped') ";
100 $mysqlUsers = $db->arrayQuery( $queryMysqlUser );
101 if ( count( $mysqlUsers ) >= 1 )
102 $exists = true;
103 }
104
105 // If current user has been disabled after a few failed login attempts.
106 $canLogin = eZUser::isEnabledAfterFailedLogin( $userID );
107
108 if ( $exists )
109 {
110 eZDebugSetting::writeDebug( 'kernel-user', eZUser::createHash( $userRow['login'], $password, eZUser::site(),
111 $hashType ), "check hash" );
112 eZDebugSetting::writeDebug( 'kernel-user', $hash, "stored hash" );
113
114 // We should store userID for warning message.
115 $GLOBALS['eZFailedLoginAttemptUserID'] = $userID;
116
117 $userSetting = eZUserSetting::fetch( $userID );
118 $isEnabled = $userSetting->attribute( "is_enabled" );
119 if ( $hashType != eZUser::hashType() and
120 strtolower( $ini->variable( 'UserSettings', 'UpdateHash' ) ) == 'true' )
121 {
122 $hashType = eZUser::hashType();
123 $hash = eZUser::createHash( $login, $password, eZUser::site(),
124 $hashType );
125 $db->query( "UPDATE ezuser SET password_hash='$hash', password_hash_type='$hashType' WHERE contentobject_id='$userID'" );
126 }
127 break;
128 }
129 }
130 }
131 if ( $exists and $isEnabled and $canLogin )
132 {
133 eZDebugSetting::writeDebug( 'kernel-user', $userRow, 'user row' );
134 $user = new eZUser( $userRow );
135 eZDebugSetting::writeDebug( 'kernel-user', $user, 'user' );
136 $userID = $user->attribute( 'contentobject_id' );
137
138 eZUser::updateLastVisit( $userID );
139 eZUser::setCurrentlyLoggedInUser( $user, $userID );
140
141 // Reset number of failed login attempts
142 eZUser::setFailedLoginAttempts( $userID, 0 );
143
144 return $user;
145 }
146 else if ( $textFileIni->variable( 'TextFileSettings', 'TextFileEnabled' ) == "true" )
147 {
148 $fileName = $textFileIni->variable( 'TextFileSettings', 'FileName' );
149 $filePath = $textFileIni->variable( 'TextFileSettings', 'FilePath' );
150 $defaultUserPlacement = $ini->variable( "UserSettings", "DefaultUserPlacement" );
151 $separator = $textFileIni->variable( "TextFileSettings", "FileFieldSeparator" );
152 $loginColumnNr = $textFileIni->variable( "TextFileSettings", "LoginAttribute" );
153 $passwordColumnNr = $textFileIni->variable( "TextFileSettings", "PasswordAttribute" );
154 $emailColumnNr = $textFileIni->variable( "TextFileSettings", "EmailAttribute" );
155 $lastNameColumnNr = $textFileIni->variable( "TextFileSettings", "LastNameAttribute" );
156 $firstNameColumnNr = $textFileIni->variable( "TextFileSettings", "FirstNameAttribute" );
157 if ( $textFileIni->hasVariable( 'TextFileSettings', 'DefaultUserGroupType' ) )
158 {
159 $UserGroupType = $textFileIni->variable( 'TextFileSettings', 'DefaultUserGroupType' );
160 $UserGroup = $textFileIni->variable( 'TextFileSettings', 'DefaultUserGroup' );
161 }
162
163 if ( $UserGroupType != null )
164 {
165 if ( $UserGroupType == "name" )
166 {
167 $groupName = $UserGroup;
168 $groupQuery = "SELECT ezcontentobject_tree.node_id
169 FROM ezcontentobject, ezcontentobject_tree
170 WHERE ezcontentobject.name='$groupName'
171 AND ezcontentobject.id=ezcontentobject_tree.contentobject_id";
172 $groupObject = $db->arrayQuery( $groupQuery );
173
174 if ( count( $groupObject ) > 0 )
175 {
176 $defaultUserPlacement = $groupObject[0]['node_id'];
177 }
178 }
179 else if ( $UserGroupType == "id" )
180 {
181 $groupID = $UserGroup;
182 $groupQuery = "SELECT ezcontentobject_tree.node_id
183 FROM ezcontentobject, ezcontentobject_tree
184 WHERE ezcontentobject.id='$groupID'
185 AND ezcontentobject.id=ezcontentobject_tree.contentobject_id";
186 $groupObject = $db->arrayQuery( $groupQuery );
187
188 if ( count( $groupObject ) > 0 )
189 {
190 $defaultUserPlacement = $groupObject[0]['node_id'];
191 }
192 }
193 }
194
195 if ( $filePath != "root" and $filePath != null )
196 $fileName = $filePath . "/" . $fileName;
197
198 if ( file_exists( $fileName ) )
199 $handle = fopen ( $fileName, "r");
200 else
201 {
202 // Increase number of failed login attempts.
203 if ( isset( $userID ) )
204 eZUser::setFailedLoginAttempts( $userID );
205
206 return false;
207 }
208
209 while ( !feof( $handle ) )
210 {
211 $line = trim( fgets( $handle, 4096 ) );
212
213 if ( $line === '' )
214 continue;
215
216 if ( $separator == "tab" )
217 $userArray = explode( "\t", $line );
218 else
219 $userArray = explode( $separator, $line );
220
221 $uid = $userArray[$loginColumnNr-1];
222 $email = $userArray[$emailColumnNr-1];
223 $pass = $userArray[$passwordColumnNr-1];
224 $firstName = $userArray[ $firstNameColumnNr-1];
225 $lastName = $userArray[$lastNameColumnNr-1];
226 if ( $login == $uid )
227 {
228 if ( trim( $pass ) == $password )
229 {
230 $createNewUser = true;
231 $existUser = eZUser::fetchByName( $login );
232 if ( $existUser != null )
233 {
234 $createNewUser = false;
235 }
236 if ( $createNewUser )
237 {
238 $userClassID = $ini->variable( "UserSettings", "UserClassID" );
239 $userCreatorID = $ini->variable( "UserSettings", "UserCreatorID" );
240 $defaultSectionID = $ini->variable( "UserSettings", "DefaultSectionID" );
241
242 $remoteID = "TextFile_" . $login;
243
244 $db->begin();
245
246 // The content object may already exist if this process has failed once before, before the eZUser object was created.
247 // Therefore we try to fetch the eZContentObject before instantiating it.
248 $contentObject = eZContentObject::fetchByRemoteID( $remoteID );
249 if ( !is_object( $contentObject ) )
250 {
251 $class = eZContentClass::fetch( $userClassID );
252 $contentObject = $class->instantiate( $userCreatorID, $defaultSectionID );
253 }
254
255 $contentObject->setAttribute( 'remote_id', $remoteID );
256 $contentObject->store();
257
258 $contentObjectID = $contentObject->attribute( 'id' );
259 $userID = $contentObjectID;
260 $nodeAssignment = eZNodeAssignment::create( array( 'contentobject_id' => $contentObjectID,
261 'contentobject_version' => 1,
262 'parent_node' => $defaultUserPlacement,
263 'is_main' => 1 ) );
264 $nodeAssignment->store();
265 $version = $contentObject->version( 1 );
266 $version->setAttribute( 'modified', time() );
267 $version->setAttribute( 'status', eZContentObjectVersion::STATUS_DRAFT );
268 $version->store();
269
270 $contentObjectID = $contentObject->attribute( 'id' );
271 $contentObjectAttributes = $version->contentObjectAttributes();
272
273 $contentObjectAttributes[0]->setAttribute( 'data_text', $firstName );
274 $contentObjectAttributes[0]->store();
275
276 $contentObjectAttributes[1]->setAttribute( 'data_text', $lastName );
277 $contentObjectAttributes[1]->store();
278
279 $user = eZUser::create( $userID );
280 $user->setAttribute( 'login', $login );
281 $user->setAttribute( 'email', $email );
282 $user->setAttribute( 'password_hash', "" );
283 $user->setAttribute( 'password_hash_type', self::PASSWORD_HASH_EMPTY );
284 $user->store();
285
286 eZUser::updateLastVisit( $userID );
287 eZUser::setCurrentlyLoggedInUser( $user, $userID );
288
289 // Reset number of failed login attempts
290 eZUser::setFailedLoginAttempts( $userID, 0 );
291
292 $operationResult = eZOperationHandler::execute( 'content', 'publish', array( 'object_id' => $contentObjectID,
293 'version' => 1 ) );
294
295 $db->commit();
296
297 return $user;
298 }
299 else
300 {
301 $db->begin();
302
303 // Update user information
304 $userID = $existUser->attribute( 'contentobject_id' );
305 $contentObject = eZContentObject::fetch( $userID );
306
307 $parentNodeID = $contentObject->attribute( 'main_parent_node_id' );
308 $currentVersion = $contentObject->attribute( 'current_version' );
309
310 $version = $contentObject->attribute( 'current' );
311 $contentObjectAttributes = $version->contentObjectAttributes();
312
313 $contentObjectAttributes[0]->setAttribute( 'data_text', $firstName );
314 $contentObjectAttributes[0]->store();
315
316 $contentObjectAttributes[1]->setAttribute( 'data_text', $lastName );
317 $contentObjectAttributes[1]->store();
318
319 $existUser = eZUser::fetch( $userID );
320 $existUser->setAttribute('email', $email );
321 $existUser->setAttribute('password_hash', "" );
322 $existUser->setAttribute('password_hash_type', self::PASSWORD_HASH_EMPTY );
323 $existUser->store();
324
325 if ( $defaultUserPlacement != $parentNodeID )
326 {
327 $newVersion = $contentObject->createNewVersion();
328 $newVersion->assignToNode( $defaultUserPlacement, 1 );
329 $newVersion->removeAssignment( $parentNodeID );
330 $newVersionNr = $newVersion->attribute( 'version' );
331 $operationResult = eZOperationHandler::execute( 'content', 'publish', array( 'object_id' => $userID,
332 'version' => $newVersionNr ) );
333 }
334
335 eZUser::updateLastVisit( $userID );
336 eZUser::setCurrentlyLoggedInUser( $existUser, $userID );
337
338 // Reset number of failed login attempts
339 eZUser::setFailedLoginAttempts( $userID, 0 );
340
341 $db->commit();
342
343 return $existUser;
344 }
345 }
346 else
347 {
348 // Increase number of failed login attempts.
349 if ( isset( $userID ) )
350 eZUser::setFailedLoginAttempts( $userID );
351
352 return false;
353 }
354 }
355 }
356 fclose( $handle );
357 }
358 // Increase number of failed login attempts.
359 if ( isset( $userID ) )
360 eZUser::setFailedLoginAttempts( $userID );
361
362 return false;
363 }
364 }
365
366 ?>
eZContentClass\fetch
static fetch( $id, $asObject=true, $version=eZContentClass::VERSION_STATUS_DEFINED, $user_id=false, $parent_id=null)
Definition: ezcontentclass.php:1116
eZContentObjectVersion\STATUS_DRAFT
const STATUS_DRAFT
Definition: ezcontentobjectversion.php:29
eZContentObject\STATUS_PUBLISHED
const STATUS_PUBLISHED
Definition: ezcontentobject.php:20
eZContentObject\fetch
static fetch( $id, $asObject=true)
Definition: ezcontentobject.php:885
eZContentObject\fetchByRemoteID
static fetchByRemoteID( $remoteID, $asObject=true)
Definition: ezcontentobject.php:865
eZDB\instance
static instance( $databaseImplementation=false, $databaseParameters=false, $forceNewInstance=false)
Definition: ezdb.php:117
eZDebugSetting\writeDebug
static writeDebug( $conditionName, $string, $label="")
Definition: ezdebugsetting.php:127
eZHTTPTool\instance
static instance()
Definition: ezhttptool.php:189
eZNodeAssignment\create
static create( $parameters=array())
Definition: eznodeassignment.php:193
eZOperationHandler\execute
static execute( $moduleName, $operationName, $operationParameters, $lastTriggerName=null, $useTriggers=true)
Definition: ezoperationhandler.php:42
eZPersistentObject\count
static count( $def, $conds=null, $field=null)
Definition: ezpersistentobject.php:187
eZTextFileUser
Handles logins for users defined a simple text file.
Definition: eztextfileuser.php:27
eZTextFileUser\loginUser
static loginUser( $login, $password, $authenticationMatch=false)
Definition: eztextfileuser.php:37
eZTextFileUser\__construct
__construct()
Definition: eztextfileuser.php:28
eZUserSetting\fetch
static fetch( $userID, $asObject=true)
Definition: ezusersetting.php:77
eZUser
eZUser handles eZ Publish user accounts
Definition: ezuser.php:19
eZUser\authenticateHash
static authenticateHash( $user, $password, $site, $type, $hash)
Definition: ezuser.php:1741
eZUser\authenticationMatch
static authenticationMatch()
Definition: ezuser.php:719
eZUser\updateLastVisit
static updateLastVisit( $userID, $updateLoginCount=false)
Definition: ezuser.php:1529
eZUser\hashType
static hashType()
Definition: ezuser.php:675
eZUser\site
static site()
Definition: ezuser.php:687
eZUser\isEnabledAfterFailedLogin
static isEnabledAfterFailedLogin( $userID, $ignoreTrusted=false)
Definition: ezuser.php:1014
eZUser\fetchByName
static fetchByName( $login, $asObject=true)
Definition: ezuser.php:361
eZUser\create
static create( $contentObjectID)
Definition: ezuser.php:237
eZUser\setCurrentlyLoggedInUser
static setCurrentlyLoggedInUser( $user, $userID, $flags=0)
Definition: ezuser.php:1054
eZUser\setFailedLoginAttempts
static setFailedLoginAttempts( $userID, $value=false, $setByForce=false)
Definition: ezuser.php:1594
eZUser\fetch
static fetch( $id, $asObject=true)
Definition: ezuser.php:351
eZUser\eZUser
eZUser( $row=array())
Definition: ezuser.php:78
eZUser\PASSWORD_HASH_MYSQL
const PASSWORD_HASH_MYSQL
Definition: ezuser.php:31
eZUser\AUTHENTICATE_LOGIN
const AUTHENTICATE_LOGIN
Authenticate by matching the login field.
Definition: ezuser.php:51
eZUser\AUTHENTICATE_EMAIL
const AUTHENTICATE_EMAIL
Authenticate by matching the email field.
Definition: ezuser.php:53
eZUser\createHash
static createHash( $user, $password, $site, $type, $hash=false)
Definition: ezuser.php:1846
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.