/standard/tags/release-1.5.0RC1/library/Zend/OpenId/Consumer/Storage/File.php

https://github.com/bhaumik25/zend-framework · PHP · 333 lines · 198 code · 14 blank · 121 comment · 37 complexity · da06757981eeacd0946935d0cc28da28 MD5 · raw file

  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_OpenId
  17. * @subpackage Zend_OpenId_Consumer
  18. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * @see Zend_OpenId_Consumer_Storage
  24. */
  25. require_once "Zend/OpenId/Consumer/Storage.php";
  26. /**
  27. * External storage implemmentation using serialized files
  28. *
  29. * @category Zend
  30. * @package Zend_OpenId
  31. * @subpackage Zend_OpenId_Consumer
  32. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. */
  35. class Zend_OpenId_Consumer_Storage_File extends Zend_OpenId_Consumer_Storage
  36. {
  37. /**
  38. * Directory name to store data files in
  39. *
  40. * @var string $_dir
  41. */
  42. private $_dir;
  43. /**
  44. * Constructs storage object and creates storage directory
  45. *
  46. * @param string $dir directory name to store data files in
  47. * @throws Zend_OpenId_Exception
  48. */
  49. public function __construct($dir = null)
  50. {
  51. if (is_null($dir)) {
  52. $tmp = getenv('TMP');
  53. if (empty($tmp)) {
  54. $tmp = getenv('TEMP');
  55. if (empty($tmp)) {
  56. $tmp = "/tmp";
  57. }
  58. }
  59. $user = get_current_user();
  60. if (is_string($user) && !empty($user)) {
  61. $tmp .= '/' . $user;
  62. }
  63. $dir = $tmp . '/openid/consumer';
  64. }
  65. $this->_dir = $dir;
  66. if (!is_dir($this->_dir)) {
  67. if (!@mkdir($this->_dir, 0700, 1)) {
  68. throw new Zend_OpenId_Exception(
  69. 'Cannot access storage directory ' . $dir,
  70. Zend_OpenId_Exception::ERROR_STORAGE);
  71. }
  72. }
  73. }
  74. /**
  75. * Stores information about association identified by $url/$handle
  76. *
  77. * @param string $url OpenID server URL
  78. * @param string $handle assiciation handle
  79. * @param string $macFunc HMAC function (sha1 or sha256)
  80. * @param string $secret shared secret
  81. * @param long $expires expiration UNIX time
  82. * @return bool
  83. */
  84. public function addAssociation($url, $handle, $macFunc, $secret, $expires)
  85. {
  86. $name1 = $this->_dir . '/assoc_url_' . md5($url);
  87. $name2 = $this->_dir . '/assoc_handle_' . md5($handle);
  88. $f = @fopen($name1, 'w+');
  89. if ($f === false) {
  90. return false;
  91. }
  92. flock($f, LOCK_EX);
  93. $data = serialize(array($url, $handle, $macFunc, $secret, $expires));
  94. fwrite($f, $data);
  95. if (function_exists('symlink')) {
  96. symlink($name1, $name2);
  97. } else {
  98. $f2 = @fopen($name2, 'w+');
  99. if ($f2) {
  100. flock($f2, LOCK_EX);
  101. fwrite($f2, $data);
  102. flock($f2, LOCK_UN);
  103. fclose($f2);
  104. }
  105. }
  106. flock($f, LOCK_UN);
  107. fclose($f);
  108. return true;
  109. }
  110. /**
  111. * Gets information about association identified by $url
  112. * Returns true if given association found and not expired and false
  113. * otherwise
  114. *
  115. * @param string $url OpenID server URL
  116. * @param string &$handle assiciation handle
  117. * @param string &$macFunc HMAC function (sha1 or sha256)
  118. * @param string &$secret shared secret
  119. * @param long &$expires expiration UNIX time
  120. * @return bool
  121. */
  122. public function getAssociation($url, &$handle, &$macFunc, &$secret, &$expires)
  123. {
  124. $name1 = $this->_dir . '/assoc_url_' . md5($url);
  125. $f = @fopen($name1, 'r');
  126. if ($f === false) {
  127. return false;
  128. }
  129. $ret = false;
  130. flock($f, LOCK_EX);
  131. $data = stream_get_contents($f);
  132. if (!empty($data)) {
  133. list($storedUrl, $handle, $macFunc, $secret, $expires) = unserialize($data);
  134. if ($url === $storedUrl && $expires > time()) {
  135. $ret = true;
  136. } else {
  137. $name2 = $this->_dir . '/assoc_handle_' . md5($handle);
  138. unlink($name2);
  139. unlink($name1);
  140. }
  141. }
  142. flock($f, LOCK_UN);
  143. fclose($f);
  144. return $ret;
  145. }
  146. /**
  147. * Gets information about association identified by $handle
  148. * Returns true if given association found and not expired and false
  149. * otherwise
  150. *
  151. * @param string $handle assiciation handle
  152. * @param string &$url OpenID server URL
  153. * @param string &$macFunc HMAC function (sha1 or sha256)
  154. * @param string &$secret shared secret
  155. * @param long &$expires expiration UNIX time
  156. * @return bool
  157. */
  158. public function getAssociationByHandle($handle, &$url, &$macFunc, &$secret, &$expires)
  159. {
  160. $name2 = $this->_dir . '/assoc_handle_' . md5($handle);
  161. $f = @fopen($name2, 'r');
  162. if ($f === false) {
  163. return false;
  164. }
  165. $ret = false;
  166. flock($f, LOCK_EX);
  167. $data = stream_get_contents($f);
  168. if (!empty($data)) {
  169. list($url, $storedHandle, $macFunc, $secret, $expires) = unserialize($data);
  170. if ($handle === $storedHandle && $expires > time()) {
  171. $ret = true;
  172. } else {
  173. unlink($name2);
  174. $name1 = $this->_dir . '/assoc_url_' . md5($url);
  175. unlink($name1);
  176. }
  177. }
  178. flock($f, LOCK_UN);
  179. fclose($f);
  180. return $ret;
  181. }
  182. /**
  183. * Deletes association identified by $url
  184. *
  185. * @param string $url OpenID server URL
  186. * @return bool
  187. */
  188. public function delAssociation($url)
  189. {
  190. $name1 = $this->_dir . '/assoc_url_' . md5($url);
  191. $f = @fopen($name1, 'r');
  192. if ($f === false) {
  193. return false;
  194. }
  195. flock($f, LOCK_EX);
  196. $data = stream_get_contents($f);
  197. if (!empty($data)) {
  198. list($storedUrl, $handle, $macFunc, $secret, $expires) = unserialize($data);
  199. if ($url === $storedUrl) {
  200. $name2 = $this->_dir . '/assoc_handle_' . md5($handle);
  201. unlink($name2);
  202. unlink($name1);
  203. }
  204. }
  205. flock($f, LOCK_UN);
  206. fclose($f);
  207. return true;
  208. }
  209. /**
  210. * Stores information discovered from identity $id
  211. *
  212. * @param string $id identity
  213. * @param string $realId discovered real identity URL
  214. * @param string $server discovered OpenID server URL
  215. * @param float $version discovered OpenID protocol version
  216. * @param long $expires expiration UNIX time
  217. * @return bool
  218. */
  219. public function addDiscoveryInfo($id, $realId, $server, $version, $expires)
  220. {
  221. $name = $this->_dir . '/discovery_' . md5($id);
  222. $f = @fopen($name, 'w+');
  223. if ($f === false) {
  224. return false;
  225. }
  226. flock($f, LOCK_EX);
  227. $data = serialize(array($id, $realId, $server, $version, $expires));
  228. fwrite($f, $data);
  229. flock($f, LOCK_UN);
  230. fclose($f);
  231. return true;
  232. }
  233. /**
  234. * Gets information discovered from identity $id
  235. * Returns true if such information exists and false otherwise
  236. *
  237. * @param string $id identity
  238. * @param string &$realId discovered real identity URL
  239. * @param string &$server discovered OpenID server URL
  240. * @param float &$version discovered OpenID protocol version
  241. * @param long &$expires expiration UNIX time
  242. * @return bool
  243. */
  244. public function getDiscoveryInfo($id, &$realId, &$server, &$version, &$expires)
  245. {
  246. $name = $this->_dir . '/discovery_' . md5($id);
  247. $f = @fopen($name, 'r');
  248. if ($f === false) {
  249. return false;
  250. }
  251. $ret = false;
  252. flock($f, LOCK_EX);
  253. $data = stream_get_contents($f);
  254. if (!empty($data)) {
  255. list($storedId, $realId, $server, $version, $expires) = unserialize($data);
  256. if ($id === $storedId && $expires > time()) {
  257. $ret = true;
  258. } else {
  259. unlink($name);
  260. }
  261. }
  262. flock($f, LOCK_UN);
  263. fclose($f);
  264. return $ret;
  265. }
  266. /**
  267. * Removes cached information discovered from identity $id
  268. *
  269. * @param string $id identity
  270. * @return bool
  271. */
  272. public function delDiscoveryInfo($id)
  273. {
  274. $name = $this->_dir . '/discovery_' . md5($id);
  275. @unlink($name);
  276. return true;
  277. }
  278. /**
  279. * The function checks the uniqueness of openid.response_nonce
  280. *
  281. * @nonce string openid.response_nonce field from authentication response
  282. * @return bool
  283. */
  284. public function isUniqueNonce($nonce)
  285. {
  286. $name = $this->_dir . '/nonce_' . md5($nonce);
  287. $f = @fopen($name, 'x');
  288. if ($f === false) {
  289. return false;
  290. }
  291. fwrite($f, $nonce);
  292. fclose($f);
  293. return true;
  294. }
  295. /**
  296. * Removes data from the uniqueness database that is older then given date
  297. *
  298. * @param mixed $date date of expired data
  299. */
  300. public function purgeNonces($date=null)
  301. {
  302. if (!is_int($date) && !is_string($date)) {
  303. foreach (glob($this->_dir . '/nonce_*') as $name) {
  304. unlink($name);
  305. }
  306. } else {
  307. if (is_string($date)) {
  308. $time = time($date);
  309. } else {
  310. $time = $date;
  311. }
  312. foreach (glob($this->_dir . '/nonce_*') as $name) {
  313. if (filectime($name) < $time) {
  314. unlink($name);
  315. }
  316. }
  317. }
  318. }
  319. }