Exponential 6.0.13 Exponential Enterprise Open Source CMS - PHP Class Reference

ezpreferences.php
Go to the documentation of this file.
1 <?php
21 class eZPreferences
22 {
23 const SESSION_NAME = "eZPreferences";
24
38 static function setValue( $name, $value, $storeUserID = false )
39 {
40 $db = eZDB::instance();
41 $name = $db->escapeString( $name );
42 $rawValue = $value;
43 $value = $db->escapeString( $value );
44
45 $isCurrentUser = true;
46 if ( $storeUserID === false )
47 {
48 $user = eZUser::currentUser();
49 }
50 else
51 {
52 $currentID = eZUser::currentUserID();
53 if ( $currentID != $storeUserID )
54 $isCurrentUser = false;
55
56 $user = eZUser::fetch( $storeUserID );
57 if ( !is_object( $user ) )
58 {
59 eZDebug::writeError( "Cannot set preference for user $storeUserID, the user does not exist" );
60 return false;
61 }
62 }
63
64 // We must store the database changes if:
65 // a - The current user is logged in (ie. not anonymous)
66 // b - We have specified a specific user (not the current).
67 // in which case isRegistered() will fail.
68 if ( $storeUserID !== false or $user->isRegistered() )
69 {
70 // Only store in DB if user is logged in or we have
71 // a specific user ID defined
72 $userID = $user->attribute( 'contentobject_id' );
73 $existingRes = $db->arrayQuery( "SELECT * FROM ezpreferences WHERE user_id = $userID AND name='$name'" );
74
75 if ( count( $existingRes ) > 0 )
76 {
77 $prefID = $existingRes[0]['id'];
78 $query = "UPDATE ezpreferences SET value='$value' WHERE id = $prefID AND name='$name'";
79 $db->query( $query );
80 }
81 else
82 {
83 $query = "INSERT INTO ezpreferences ( user_id, name, value ) VALUES ( $userID, '$name', '$value' )";
84 $db->query( $query );
85 }
86 }
87
88 // We also store in session if this is the current user (anonymous or normal user)
89 // use $rawValue as value will be escaped by session code (see #014520)
90 if ( $isCurrentUser )
91 {
92 eZPreferences::storeInSession( $name, $rawValue );
93 }
94
95 return true;
96 }
97
106 static function value( $name, $user = false )
107 {
108 if ( !( $user instanceof eZUser ) )
109 $user = eZUser::currentUser();
110
111 $value = false;
112 // If the user object is not the currently logged in user we cannot use the session values
113 $http = eZHTTPTool::instance();
114 $useCache = ( $user->ContentObjectID == $http->sessionVariable( 'eZUserLoggedInID', false ) );
115 if ( $useCache and eZPreferences::isStoredInSession( $name ) )
116 return eZPreferences::storedSessionValue( $name );
117
118 // If this the anonymous user we should return false, no need to check database.
119 if ( $user->isAnonymous() )
120 return false;
121
122 $db = eZDB::instance();
123 $name = $db->escapeString( $name );
124 $userID = $user->attribute( 'contentobject_id' );
125 $existingRes = $db->arrayQuery( "SELECT value FROM ezpreferences WHERE user_id = $userID AND name = '$name'" );
126
127 if ( is_countable( $existingRes ) && count( $existingRes ) == 1 )
128 {
129 $value = $existingRes[0]['value'];
130 if ( $useCache )
131 eZPreferences::storeInSession( $name, $value );
132 }
133 else
134 {
135 if ( $useCache )
136 eZPreferences::storeInSession( $name, false );
137 }
138 return $value;
139 }
140
147 static function values( $user = false )
148 {
149 if ( !( $user instanceof eZUser ) )
150 $user = eZUser::currentUser();
151
152 if ( !$user->isAnonymous() )
153 {
154 // If the user object is not the currently logged in user we cannot use the session values
155 $http = eZHTTPTool::instance();
156 $useCache = ( $user->ContentObjectID == $http->sessionVariable( 'eZUserLoggedInID', false ) );
157
158 $returnArray = array();
159 $userID = $user->attribute( 'contentobject_id' );
160 $db = eZDB::instance();
161 $values = $db->arrayQuery( "SELECT name,value FROM ezpreferences WHERE user_id=$userID ORDER BY id" );
162 foreach ( $values as $item )
163 {
164 if ( $useCache )
165 eZPreferences::storeInSession( $item['name'], $item['value'] );
166 $returnArray[$item['name']] = $item['value'];
167 }
168 return $returnArray;
169 }
170 else
171 {
172 // For the anonymous user we just return all values, or empty array if session is un-started / value undefined
173 $http = eZHTTPTool::instance();
174 return $http->sessionVariable( eZPreferences::SESSION_NAME, array() );
175 }
176 }
177
182 static function sessionCleanup()
183 {
184 $http = eZHTTPTool::instance();
185 $http->removeSessionVariable( eZPreferences::SESSION_NAME );
186 }
187
192 static function storeInSession( $name, $value )
193 {
194 $http = eZHTTPTool::instance();
195 $preferencesInSession = array();
196 if ( $http->hasSessionVariable( eZPreferences::SESSION_NAME ) )
197 $preferencesInSession = $http->sessionVariable( eZPreferences::SESSION_NAME );
198 $preferencesInSession[$name] = $value;
199 $http->setSessionVariable( eZPreferences::SESSION_NAME, $preferencesInSession );
200 }
201
206 static function isStoredInSession( $name )
207 {
208 $http = eZHTTPTool::instance();
209 if ( !$http->hasSessionVariable( eZPreferences::SESSION_NAME, false ) )
210 return false;
211 $preferencesInSession = $http->sessionVariable( eZPreferences::SESSION_NAME );
212 return array_key_exists( $name, $preferencesInSession );
213 }
214
219 static function storedSessionValue( $name )
220 {
221 $http = eZHTTPTool::instance();
222 if ( !$http->hasSessionVariable( eZPreferences::SESSION_NAME ) )
223 return null;
224 $preferencesInSession = $http->sessionVariable( eZPreferences::SESSION_NAME );
225 if ( !array_key_exists( $name, $preferencesInSession ) )
226 return null;
227 return $preferencesInSession[$name];
228 }
229
236 static function cleanup()
237 {
238 $db = eZDB::instance();
239 $db->query( "DELETE FROM ezpreferences" );
240 }
241 }
242
243
244 ?>
eZDB\instance
static instance( $databaseImplementation=false, $databaseParameters=false, $forceNewInstance=false)
Definition: ezdb.php:117
eZDebug\writeError
static writeError( $string, $label="", $backgroundClass="")
Definition: ezdebug.php:545
eZHTTPTool\instance
static instance()
Definition: ezhttptool.php:189
eZPreferences
Handles user/session preferences.
Definition: ezpreferences.php:22
eZPreferences\storeInSession
static storeInSession( $name, $value)
Definition: ezpreferences.php:192
eZPreferences\SESSION_NAME
const SESSION_NAME
Definition: ezpreferences.php:23
eZPreferences\values
static values( $user=false)
Definition: ezpreferences.php:147
eZPreferences\value
static value( $name, $user=false)
Definition: ezpreferences.php:106
eZPreferences\sessionCleanup
static sessionCleanup()
Definition: ezpreferences.php:182
eZPreferences\cleanup
static cleanup()
Definition: ezpreferences.php:236
eZPreferences\isStoredInSession
static isStoredInSession( $name)
Definition: ezpreferences.php:206
eZPreferences\setValue
static setValue( $name, $value, $storeUserID=false)
Definition: ezpreferences.php:38
eZPreferences\storedSessionValue
static storedSessionValue( $name)
Definition: ezpreferences.php:219
eZUser
eZUser handles eZ Publish user accounts
Definition: ezuser.php:19
eZUser\fetch
static fetch( $id, $asObject=true)
Definition: ezuser.php:351
eZUser\currentUserID
static currentUserID()
Definition: ezuser.php:1731
eZUser\currentUser
static currentUser()
Definition: ezuser.php:1716
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.