| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511 |
- <?php
- /**
- * Zend Framework
- *
- * LICENSE
- *
- * This source file is subject to the new BSD license that is bundled
- * with this package in the file LICENSE.txt.
- * It is also available through the world-wide-web at this URL:
- * http://framework.zend.com/license/new-bsd
- * If you did not receive a copy of the license and are unable to
- * obtain it through the world-wide-web, please send an email
- * to license@zend.com so we can send you a copy immediately.
- *
- * @category Zend
- * @package Zend_OpenId
- * @subpackage Zend_OpenId_Consumer
- * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
- * @license http://framework.zend.com/license/new-bsd New BSD License
- * @version $Id$
- */
- /**
- * @see Zend_OpenId_Consumer_Storage
- */
- require_once "Zend/OpenId/Consumer/Storage.php";
- /**
- * External storage implemmentation using serialized files
- *
- * @category Zend
- * @package Zend_OpenId
- * @subpackage Zend_OpenId_Consumer
- * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
- * @license http://framework.zend.com/license/new-bsd New BSD License
- */
- class Zend_OpenId_Consumer_Storage_File extends Zend_OpenId_Consumer_Storage
- {
- /**
- * Directory name to store data files in
- *
- * @var string $_dir
- */
- private $_dir;
- /**
- * Constructs storage object and creates storage directory
- *
- * @param string $dir directory name to store data files in
- * @throws Zend_OpenId_Exception
- */
- public function __construct($dir = null)
- {
- if ($dir === null) {
- $tmp = getenv('TMP');
- if (empty($tmp)) {
- $tmp = getenv('TEMP');
- if (empty($tmp)) {
- $tmp = "/tmp";
- }
- }
- $user = get_current_user();
- if (is_string($user) && !empty($user)) {
- $tmp .= '/' . $user;
- }
- $dir = $tmp . '/openid/consumer';
- }
- $this->_dir = $dir;
- if (!is_dir($this->_dir)) {
- if (!@mkdir($this->_dir, 0700, 1)) {
- /**
- * @see Zend_OpenId_Exception
- */
- require_once 'Zend/OpenId/Exception.php';
- throw new Zend_OpenId_Exception(
- 'Cannot access storage directory ' . $dir,
- Zend_OpenId_Exception::ERROR_STORAGE);
- }
- }
- if (($f = fopen($this->_dir.'/assoc.lock', 'w+')) === null) {
- /**
- * @see Zend_OpenId_Exception
- */
- require_once 'Zend/OpenId/Exception.php';
- throw new Zend_OpenId_Exception(
- 'Cannot create a lock file in the directory ' . $dir,
- Zend_OpenId_Exception::ERROR_STORAGE);
- }
- fclose($f);
- if (($f = fopen($this->_dir.'/discovery.lock', 'w+')) === null) {
- /**
- * @see Zend_OpenId_Exception
- */
- require_once 'Zend/OpenId/Exception.php';
- throw new Zend_OpenId_Exception(
- 'Cannot create a lock file in the directory ' . $dir,
- Zend_OpenId_Exception::ERROR_STORAGE);
- }
- fclose($f);
- if (($f = fopen($this->_dir.'/nonce.lock', 'w+')) === null) {
- /**
- * @see Zend_OpenId_Exception
- */
- require_once 'Zend/OpenId/Exception.php';
- throw new Zend_OpenId_Exception(
- 'Cannot create a lock file in the directory ' . $dir,
- Zend_OpenId_Exception::ERROR_STORAGE);
- }
- fclose($f);
- }
- /**
- * Stores information about association identified by $url/$handle
- *
- * @param string $url OpenID server URL
- * @param string $handle assiciation handle
- * @param string $macFunc HMAC function (sha1 or sha256)
- * @param string $secret shared secret
- * @param long $expires expiration UNIX time
- * @return bool
- */
- public function addAssociation($url, $handle, $macFunc, $secret, $expires)
- {
- $name1 = $this->_dir . '/assoc_url_' . md5($url);
- $name2 = $this->_dir . '/assoc_handle_' . md5($handle);
- $lock = @fopen($this->_dir . '/assoc.lock', 'w+');
- if ($lock === false) {
- return false;
- }
- if (!flock($lock, LOCK_EX)) {
- fclose($lock);
- return false;
- }
- try {
- $f = @fopen($name1, 'w+');
- if ($f === false) {
- fclose($lock);
- return false;
- }
- $data = serialize(array($url, $handle, $macFunc, $secret, $expires));
- fwrite($f, $data);
- if (function_exists('symlink')) {
- @unlink($name2);
- if (symlink($name1, $name2)) {
- fclose($f);
- fclose($lock);
- return true;
- }
- }
- $f2 = @fopen($name2, 'w+');
- if ($f2) {
- fwrite($f2, $data);
- fclose($f2);
- @unlink($name1);
- $ret = true;
- } else {
- $ret = false;
- }
- fclose($f);
- fclose($lock);
- return $ret;
- } catch (Exception $e) {
- fclose($lock);
- throw $e;
- }
- }
- /**
- * Gets information about association identified by $url
- * Returns true if given association found and not expired and false
- * otherwise
- *
- * @param string $url OpenID server URL
- * @param string &$handle assiciation handle
- * @param string &$macFunc HMAC function (sha1 or sha256)
- * @param string &$secret shared secret
- * @param long &$expires expiration UNIX time
- * @return bool
- */
- public function getAssociation($url, &$handle, &$macFunc, &$secret, &$expires)
- {
- $name1 = $this->_dir . '/assoc_url_' . md5($url);
- $lock = @fopen($this->_dir . '/assoc.lock', 'w+');
- if ($lock === false) {
- return false;
- }
- if (!flock($lock, LOCK_EX)) {
- fclose($lock);
- return false;
- }
- try {
- $f = @fopen($name1, 'r');
- if ($f === false) {
- fclose($lock);
- return false;
- }
- $ret = false;
- $data = stream_get_contents($f);
- if (!empty($data)) {
- list($storedUrl, $handle, $macFunc, $secret, $expires) = unserialize($data);
- if ($url === $storedUrl && $expires > time()) {
- $ret = true;
- } else {
- $name2 = $this->_dir . '/assoc_handle_' . md5($handle);
- fclose($f);
- @unlink($name2);
- @unlink($name1);
- fclose($lock);
- return false;
- }
- }
- fclose($f);
- fclose($lock);
- return $ret;
- } catch (Exception $e) {
- fclose($lock);
- throw $e;
- }
- }
- /**
- * Gets information about association identified by $handle
- * Returns true if given association found and not expired and false
- * otherwise
- *
- * @param string $handle assiciation handle
- * @param string &$url OpenID server URL
- * @param string &$macFunc HMAC function (sha1 or sha256)
- * @param string &$secret shared secret
- * @param long &$expires expiration UNIX time
- * @return bool
- */
- public function getAssociationByHandle($handle, &$url, &$macFunc, &$secret, &$expires)
- {
- $name2 = $this->_dir . '/assoc_handle_' . md5($handle);
- $lock = @fopen($this->_dir . '/assoc.lock', 'w+');
- if ($lock === false) {
- return false;
- }
- if (!flock($lock, LOCK_EX)) {
- fclose($lock);
- return false;
- }
- try {
- $f = @fopen($name2, 'r');
- if ($f === false) {
- fclose($lock);
- return false;
- }
- $ret = false;
- $data = stream_get_contents($f);
- if (!empty($data)) {
- list($url, $storedHandle, $macFunc, $secret, $expires) = unserialize($data);
- if ($handle === $storedHandle && $expires > time()) {
- $ret = true;
- } else {
- fclose($f);
- @unlink($name2);
- $name1 = $this->_dir . '/assoc_url_' . md5($url);
- @unlink($name1);
- fclose($lock);
- return false;
- }
- }
- fclose($f);
- fclose($lock);
- return $ret;
- } catch (Exception $e) {
- fclose($lock);
- throw $e;
- }
- }
- /**
- * Deletes association identified by $url
- *
- * @param string $url OpenID server URL
- * @return bool
- */
- public function delAssociation($url)
- {
- $name1 = $this->_dir . '/assoc_url_' . md5($url);
- $lock = @fopen($this->_dir . '/assoc.lock', 'w+');
- if ($lock === false) {
- return false;
- }
- if (!flock($lock, LOCK_EX)) {
- fclose($lock);
- return false;
- }
- try {
- $f = @fopen($name1, 'r');
- if ($f === false) {
- fclose($lock);
- return false;
- }
- $data = stream_get_contents($f);
- if (!empty($data)) {
- list($storedUrl, $handle, $macFunc, $secret, $expires) = unserialize($data);
- if ($url === $storedUrl) {
- $name2 = $this->_dir . '/assoc_handle_' . md5($handle);
- fclose($f);
- @unlink($name2);
- @unlink($name1);
- fclose($lock);
- return true;
- }
- }
- fclose($f);
- fclose($lock);
- return true;
- } catch (Exception $e) {
- fclose($lock);
- throw $e;
- }
- }
- /**
- * Stores information discovered from identity $id
- *
- * @param string $id identity
- * @param string $realId discovered real identity URL
- * @param string $server discovered OpenID server URL
- * @param float $version discovered OpenID protocol version
- * @param long $expires expiration UNIX time
- * @return bool
- */
- public function addDiscoveryInfo($id, $realId, $server, $version, $expires)
- {
- $name = $this->_dir . '/discovery_' . md5($id);
- $lock = @fopen($this->_dir . '/discovery.lock', 'w+');
- if ($lock === false) {
- return false;
- }
- if (!flock($lock, LOCK_EX)) {
- fclose($lock);
- return false;
- }
- try {
- $f = @fopen($name, 'w+');
- if ($f === false) {
- fclose($lock);
- return false;
- }
- $data = serialize(array($id, $realId, $server, $version, $expires));
- fwrite($f, $data);
- fclose($f);
- fclose($lock);
- return true;
- } catch (Exception $e) {
- fclose($lock);
- throw $e;
- }
- }
- /**
- * Gets information discovered from identity $id
- * Returns true if such information exists and false otherwise
- *
- * @param string $id identity
- * @param string &$realId discovered real identity URL
- * @param string &$server discovered OpenID server URL
- * @param float &$version discovered OpenID protocol version
- * @param long &$expires expiration UNIX time
- * @return bool
- */
- public function getDiscoveryInfo($id, &$realId, &$server, &$version, &$expires)
- {
- $name = $this->_dir . '/discovery_' . md5($id);
- $lock = @fopen($this->_dir . '/discovery.lock', 'w+');
- if ($lock === false) {
- return false;
- }
- if (!flock($lock, LOCK_EX)) {
- fclose($lock);
- return false;
- }
- try {
- $f = @fopen($name, 'r');
- if ($f === false) {
- fclose($lock);
- return false;
- }
- $ret = false;
- $data = stream_get_contents($f);
- if (!empty($data)) {
- list($storedId, $realId, $server, $version, $expires) = unserialize($data);
- if ($id === $storedId && $expires > time()) {
- $ret = true;
- } else {
- fclose($f);
- @unlink($name);
- fclose($lock);
- return false;
- }
- }
- fclose($f);
- fclose($lock);
- return $ret;
- } catch (Exception $e) {
- fclose($lock);
- throw $e;
- }
- }
- /**
- * Removes cached information discovered from identity $id
- *
- * @param string $id identity
- * @return bool
- */
- public function delDiscoveryInfo($id)
- {
- $name = $this->_dir . '/discovery_' . md5($id);
- $lock = @fopen($this->_dir . '/discovery.lock', 'w+');
- if ($lock === false) {
- return false;
- }
- if (!flock($lock, LOCK_EX)) {
- fclose($lock);
- return false;
- }
- try {
- @unlink($name);
- fclose($lock);
- return true;
- } catch (Exception $e) {
- fclose($lock);
- throw $e;
- }
- }
- /**
- * The function checks the uniqueness of openid.response_nonce
- *
- * @param string $provider openid.openid_op_endpoint field from authentication response
- * @param string $nonce openid.response_nonce field from authentication response
- * @return bool
- */
- public function isUniqueNonce($provider, $nonce)
- {
- $name = $this->_dir . '/nonce_' . md5($provider.';'.$nonce);
- $lock = @fopen($this->_dir . '/nonce.lock', 'w+');
- if ($lock === false) {
- return false;
- }
- if (!flock($lock, LOCK_EX)) {
- fclose($lock);
- return false;
- }
- try {
- $f = @fopen($name, 'x');
- if ($f === false) {
- fclose($lock);
- return false;
- }
- fwrite($f, $provider.';'.$nonce);
- fclose($f);
- fclose($lock);
- return true;
- } catch (Exception $e) {
- fclose($lock);
- throw $e;
- }
- }
- /**
- * Removes data from the uniqueness database that is older then given date
- *
- * @param mixed $date date of expired data
- */
- public function purgeNonces($date=null)
- {
- $lock = @fopen($this->_dir . '/nonce.lock', 'w+');
- if ($lock !== false) {
- flock($lock, LOCK_EX);
- }
- try {
- if (!is_int($date) && !is_string($date)) {
- $nonceFiles = glob($this->_dir . '/nonce_*');
- foreach ((array) $nonceFiles as $name) {
- @unlink($name);
- }
- unset($nonceFiles);
- } else {
- if (is_string($date)) {
- $time = time($date);
- } else {
- $time = $date;
- }
- $nonceFiles = glob($this->_dir . '/nonce_*');
- foreach ((array) $nonceFiles as $name) {
- if (filemtime($name) < $time) {
- @unlink($name);
- }
- }
- unset($nonceFiles);
- }
- if ($lock !== false) {
- fclose($lock);
- }
- } catch (Exception $e) {
- if ($lock !== false) {
- fclose($lock);
- }
- throw $e;
- }
- }
- }
|