MemcachedStore.php

Go to the documentation of this file.
00001 <?php
00002 
00021 require_once 'Auth/OpenID/Interface.php';
00022 
00036 class Auth_OpenID_MemcachedStore extends Auth_OpenID_OpenIDStore {
00037 
00044     function Auth_OpenID_MemcachedStore($connection, $compress = false)
00045     {
00046         $this->connection = $connection;
00047         $this->compress = $compress ? MEMCACHE_COMPRESSED : 0;
00048     }
00049 
00055     function storeAssociation($server_url, $association)
00056     {
00057         // create memcached keys for association itself 
00058         // and list of associations for this server
00059         $associationKey = $this->associationKey($server_url, 
00060             $association->handle);
00061         $serverKey = $this->associationServerKey($server_url);
00062         
00063         // get list of associations 
00064         $serverAssociations = $this->connection->get($serverKey);
00065         
00066         // if no such list, initialize it with empty array
00067         if (!$serverAssociations) {
00068             $serverAssociations = array();
00069         }
00070         // and store given association key in it
00071         $serverAssociations[$association->issued] = $associationKey;
00072         
00073         // save associations' keys list 
00074         $this->connection->set(
00075             $serverKey,
00076             $serverAssociations,
00077             $this->compress
00078         );
00079         // save association itself
00080         $this->connection->set(
00081             $associationKey,
00082             $association, 
00083             $this->compress, 
00084             $association->issued + $association->lifetime);
00085     }
00086 
00091     function getAssociation($server_url, $handle = null)
00092     {
00093         // simple case: handle given
00094         if ($handle !== null) {
00095             // get association, return null if failed
00096             $association = $this->connection->get(
00097                 $this->associationKey($server_url, $handle));
00098             return $association ? $association : null;
00099         }
00100         
00101         // no handle given, working with list
00102         // create key for list of associations
00103         $serverKey = $this->associationServerKey($server_url);
00104         
00105         // get list of associations
00106         $serverAssociations = $this->connection->get($serverKey);
00107         // return null if failed or got empty list
00108         if (!$serverAssociations) {
00109             return null;
00110         }
00111         
00112         // get key of most recently issued association
00113         $keys = array_keys($serverAssociations);
00114         sort($keys);
00115         $lastKey = $serverAssociations[array_pop($keys)];
00116         
00117         // get association, return null if failed
00118         $association = $this->connection->get($lastKey);
00119         return $association ? $association : null;
00120     }
00121 
00125     function removeAssociation($server_url, $handle)
00126     {
00127         // create memcached keys for association itself 
00128         // and list of associations for this server
00129         $serverKey = $this->associationServerKey($server_url);
00130         $associationKey = $this->associationKey($server_url, 
00131             $handle);
00132         
00133         // get list of associations
00134         $serverAssociations = $this->connection->get($serverKey);
00135         // return null if failed or got empty list
00136         if (!$serverAssociations) {
00137             return false;
00138         }
00139         
00140         // ensure that given association key exists in list
00141         $serverAssociations = array_flip($serverAssociations);
00142         if (!array_key_exists($associationKey, $serverAssociations)) {
00143             return false;
00144         }
00145         
00146         // remove given association key from list
00147         unset($serverAssociations[$associationKey]);
00148         $serverAssociations = array_flip($serverAssociations);
00149         
00150         // save updated list
00151         $this->connection->set(
00152             $serverKey,
00153             $serverAssociations,
00154             $this->compress
00155         );
00156 
00157         // delete association 
00158         return $this->connection->delete($associationKey);
00159     }
00160 
00165     function useNonce($server_url, $timestamp, $salt)
00166     {
00167         global $Auth_OpenID_SKEW;
00168         
00169         // save one request to memcache when nonce obviously expired 
00170         if (abs($timestamp - time()) > $Auth_OpenID_SKEW) {
00171             return false;
00172         }
00173         
00174         // returns false when nonce already exists
00175         // otherwise adds nonce
00176         return $this->connection->add(
00177             'openid_nonce_' . sha1($server_url) . '_' . sha1($salt), 
00178             1, // any value here 
00179             $this->compress, 
00180             $Auth_OpenID_SKEW);
00181     }
00182     
00186     function associationKey($server_url, $handle = null) 
00187     {
00188         return 'openid_association_' . sha1($server_url) . '_' . sha1($handle);
00189     }
00190     
00194     function associationServerKey($server_url) 
00195     {
00196         return 'openid_association_server_' . sha1($server_url);
00197     }
00198     
00202     function supportsCleanup()
00203     {
00204         return false;
00205     }
00206 }
00207 
00208 ?>

Generated on Thu Feb 19 15:02:21 2009 for OXID eShop CE by  doxygen 1.5.5