PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/www/system/library/Zend/OpenId/Provider/Storage/File.php

https://bitbucket.org/vmihailenco/zf-blog
PHP | 442 lines | 315 code | 15 blank | 112 comment | 49 complexity | e2703cc6d624f71e95e8b33a9f0b8d4c 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_Provider
  18. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: File.php 20096 2010-01-06 02:05:09Z bkarwin $
  21. */
  22. /**
  23. * @see Zend_OpenId_Provider_Storage
  24. */
  25. /**
  26. * External storage implemmentation using serialized files
  27. *
  28. * @category Zend
  29. * @package Zend_OpenId
  30. * @subpackage Zend_OpenId_Provider
  31. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  32. * @license http://framework.zend.com/license/new-bsd New BSD License
  33. */
  34. class Zend_OpenId_Provider_Storage_File extends Zend_OpenId_Provider_Storage
  35. {
  36. /**
  37. * Directory name to store data files in
  38. *
  39. * @var string $_dir
  40. */
  41. private $_dir;
  42. /**
  43. * Constructs storage object and creates storage directory
  44. *
  45. * @param string $dir directory name to store data files in
  46. * @throws Zend_OpenId_Exception
  47. */
  48. public function __construct($dir = null)
  49. {
  50. if ($dir === null) {
  51. $tmp = getenv('TMP');
  52. if (empty($tmp)) {
  53. $tmp = getenv('TEMP');
  54. if (empty($tmp)) {
  55. $tmp = "/tmp";
  56. }
  57. }
  58. $user = get_current_user();
  59. if (is_string($user) && !empty($user)) {
  60. $tmp .= '/' . $user;
  61. }
  62. $dir = $tmp . '/openid/provider';
  63. }
  64. $this->_dir = $dir;
  65. if (!is_dir($this->_dir)) {
  66. if (!@mkdir($this->_dir, 0700, 1)) {
  67. throw new Zend_OpenId_Exception(
  68. "Cannot access storage directory $dir",
  69. Zend_OpenId_Exception::ERROR_STORAGE);
  70. }
  71. }
  72. if (($f = fopen($this->_dir.'/assoc.lock', 'w+')) === null) {
  73. throw new Zend_OpenId_Exception(
  74. 'Cannot create a lock file in the directory ' . $dir,
  75. Zend_OpenId_Exception::ERROR_STORAGE);
  76. }
  77. fclose($f);
  78. if (($f = fopen($this->_dir.'/user.lock', 'w+')) === null) {
  79. throw new Zend_OpenId_Exception(
  80. 'Cannot create a lock file in the directory ' . $dir,
  81. Zend_OpenId_Exception::ERROR_STORAGE);
  82. }
  83. fclose($f);
  84. }
  85. /**
  86. * Stores information about session identified by $handle
  87. *
  88. * @param string $handle assiciation handle
  89. * @param string $macFunc HMAC function (sha1 or sha256)
  90. * @param string $secret shared secret
  91. * @param string $expires expiration UNIX time
  92. * @return bool
  93. */
  94. public function addAssociation($handle, $macFunc, $secret, $expires)
  95. {
  96. $name = $this->_dir . '/assoc_' . md5($handle);
  97. $lock = @fopen($this->_dir . '/assoc.lock', 'w+');
  98. if ($lock === false) {
  99. return false;
  100. }
  101. if (!flock($lock, LOCK_EX)) {
  102. fclose($lock);
  103. return false;
  104. }
  105. try {
  106. $f = @fopen($name, 'w+');
  107. if ($f === false) {
  108. fclose($lock);
  109. return false;
  110. }
  111. $data = serialize(array($handle, $macFunc, $secret, $expires));
  112. fwrite($f, $data);
  113. fclose($f);
  114. fclose($lock);
  115. return true;
  116. } catch (Exception $e) {
  117. fclose($lock);
  118. throw $e;
  119. }
  120. }
  121. /**
  122. * Gets information about association identified by $handle
  123. * Returns true if given association found and not expired and false
  124. * otherwise
  125. *
  126. * @param string $handle assiciation handle
  127. * @param string &$macFunc HMAC function (sha1 or sha256)
  128. * @param string &$secret shared secret
  129. * @param string &$expires expiration UNIX time
  130. * @return bool
  131. */
  132. public function getAssociation($handle, &$macFunc, &$secret, &$expires)
  133. {
  134. $name = $this->_dir . '/assoc_' . md5($handle);
  135. $lock = @fopen($this->_dir . '/assoc.lock', 'w+');
  136. if ($lock === false) {
  137. return false;
  138. }
  139. if (!flock($lock, LOCK_EX)) {
  140. fclose($lock);
  141. return false;
  142. }
  143. try {
  144. $f = @fopen($name, 'r');
  145. if ($f === false) {
  146. fclose($lock);
  147. return false;
  148. }
  149. $ret = false;
  150. $data = stream_get_contents($f);
  151. if (!empty($data)) {
  152. list($storedHandle, $macFunc, $secret, $expires) = unserialize($data);
  153. if ($handle === $storedHandle && $expires > time()) {
  154. $ret = true;
  155. } else {
  156. fclose($f);
  157. @unlink($name);
  158. fclose($lock);
  159. return false;
  160. }
  161. }
  162. fclose($f);
  163. fclose($lock);
  164. return $ret;
  165. } catch (Exception $e) {
  166. fclose($lock);
  167. throw $e;
  168. }
  169. }
  170. /**
  171. * Removes information about association identified by $handle
  172. *
  173. * @param string $handle assiciation handle
  174. * @return bool
  175. */
  176. public function delAssociation($handle)
  177. {
  178. $name = $this->_dir . '/assoc_' . md5($handle);
  179. $lock = @fopen($this->_dir . '/assoc.lock', 'w+');
  180. if ($lock === false) {
  181. return false;
  182. }
  183. if (!flock($lock, LOCK_EX)) {
  184. fclose($lock);
  185. return false;
  186. }
  187. try {
  188. @unlink($name);
  189. fclose($lock);
  190. return true;
  191. } catch (Exception $e) {
  192. fclose($lock);
  193. throw $e;
  194. }
  195. }
  196. /**
  197. * Register new user with given $id and $password
  198. * Returns true in case of success and false if user with given $id already
  199. * exists
  200. *
  201. * @param string $id user identity URL
  202. * @param string $password encoded user password
  203. * @return bool
  204. */
  205. public function addUser($id, $password)
  206. {
  207. $name = $this->_dir . '/user_' . md5($id);
  208. $lock = @fopen($this->_dir . '/user.lock', 'w+');
  209. if ($lock === false) {
  210. return false;
  211. }
  212. if (!flock($lock, LOCK_EX)) {
  213. fclose($lock);
  214. return false;
  215. }
  216. try {
  217. $f = @fopen($name, 'x');
  218. if ($f === false) {
  219. fclose($lock);
  220. return false;
  221. }
  222. $data = serialize(array($id, $password, array()));
  223. fwrite($f, $data);
  224. fclose($f);
  225. fclose($lock);
  226. return true;
  227. } catch (Exception $e) {
  228. fclose($lock);
  229. throw $e;
  230. }
  231. }
  232. /**
  233. * Returns true if user with given $id exists and false otherwise
  234. *
  235. * @param string $id user identity URL
  236. * @return bool
  237. */
  238. public function hasUser($id)
  239. {
  240. $name = $this->_dir . '/user_' . md5($id);
  241. $lock = @fopen($this->_dir . '/user.lock', 'w+');
  242. if ($lock === false) {
  243. return false;
  244. }
  245. if (!flock($lock, LOCK_SH)) {
  246. fclose($lock);
  247. return false;
  248. }
  249. try {
  250. $f = @fopen($name, 'r');
  251. if ($f === false) {
  252. fclose($lock);
  253. return false;
  254. }
  255. $ret = false;
  256. $data = stream_get_contents($f);
  257. if (!empty($data)) {
  258. list($storedId, $storedPassword, $trusted) = unserialize($data);
  259. if ($id === $storedId) {
  260. $ret = true;
  261. }
  262. }
  263. fclose($f);
  264. fclose($lock);
  265. return $ret;
  266. } catch (Exception $e) {
  267. fclose($lock);
  268. throw $e;
  269. }
  270. }
  271. /**
  272. * Verify if user with given $id exists and has specified $password
  273. *
  274. * @param string $id user identity URL
  275. * @param string $password user password
  276. * @return bool
  277. */
  278. public function checkUser($id, $password)
  279. {
  280. $name = $this->_dir . '/user_' . md5($id);
  281. $lock = @fopen($this->_dir . '/user.lock', 'w+');
  282. if ($lock === false) {
  283. return false;
  284. }
  285. if (!flock($lock, LOCK_SH)) {
  286. fclose($lock);
  287. return false;
  288. }
  289. try {
  290. $f = @fopen($name, 'r');
  291. if ($f === false) {
  292. fclose($lock);
  293. return false;
  294. }
  295. $ret = false;
  296. $data = stream_get_contents($f);
  297. if (!empty($data)) {
  298. list($storedId, $storedPassword, $trusted) = unserialize($data);
  299. if ($id === $storedId && $password === $storedPassword) {
  300. $ret = true;
  301. }
  302. }
  303. fclose($f);
  304. fclose($lock);
  305. return $ret;
  306. } catch (Exception $e) {
  307. fclose($lock);
  308. throw $e;
  309. }
  310. }
  311. /**
  312. * Removes information abou specified user
  313. *
  314. * @param string $id user identity URL
  315. * @return bool
  316. */
  317. public function delUser($id)
  318. {
  319. $name = $this->_dir . '/user_' . md5($id);
  320. $lock = @fopen($this->_dir . '/user.lock', 'w+');
  321. if ($lock === false) {
  322. return false;
  323. }
  324. if (!flock($lock, LOCK_EX)) {
  325. fclose($lock);
  326. return false;
  327. }
  328. try {
  329. @unlink($name);
  330. fclose($lock);
  331. return true;
  332. } catch (Exception $e) {
  333. fclose($lock);
  334. throw $e;
  335. }
  336. }
  337. /**
  338. * Returns array of all trusted/untrusted sites for given user identified
  339. * by $id
  340. *
  341. * @param string $id user identity URL
  342. * @return array
  343. */
  344. public function getTrustedSites($id)
  345. {
  346. $name = $this->_dir . '/user_' . md5($id);
  347. $lock = @fopen($this->_dir . '/user.lock', 'w+');
  348. if ($lock === false) {
  349. return false;
  350. }
  351. if (!flock($lock, LOCK_SH)) {
  352. fclose($lock);
  353. return false;
  354. }
  355. try {
  356. $f = @fopen($name, 'r');
  357. if ($f === false) {
  358. fclose($lock);
  359. return false;
  360. }
  361. $ret = false;
  362. $data = stream_get_contents($f);
  363. if (!empty($data)) {
  364. list($storedId, $storedPassword, $trusted) = unserialize($data);
  365. if ($id === $storedId) {
  366. $ret = $trusted;
  367. }
  368. }
  369. fclose($f);
  370. fclose($lock);
  371. return $ret;
  372. } catch (Exception $e) {
  373. fclose($lock);
  374. throw $e;
  375. }
  376. }
  377. /**
  378. * Stores information about trusted/untrusted site for given user
  379. *
  380. * @param string $id user identity URL
  381. * @param string $site site URL
  382. * @param mixed $trusted trust data from extension or just a boolean value
  383. * @return bool
  384. */
  385. public function addSite($id, $site, $trusted)
  386. {
  387. $name = $this->_dir . '/user_' . md5($id);
  388. $lock = @fopen($this->_dir . '/user.lock', 'w+');
  389. if ($lock === false) {
  390. return false;
  391. }
  392. if (!flock($lock, LOCK_EX)) {
  393. fclose($lock);
  394. return false;
  395. }
  396. try {
  397. $f = @fopen($name, 'r+');
  398. if ($f === false) {
  399. fclose($lock);
  400. return false;
  401. }
  402. $ret = false;
  403. $data = stream_get_contents($f);
  404. if (!empty($data)) {
  405. list($storedId, $storedPassword, $sites) = unserialize($data);
  406. if ($id === $storedId) {
  407. if ($trusted === null) {
  408. unset($sites[$site]);
  409. } else {
  410. $sites[$site] = $trusted;
  411. }
  412. rewind($f);
  413. ftruncate($f, 0);
  414. $data = serialize(array($id, $storedPassword, $sites));
  415. fwrite($f, $data);
  416. $ret = true;
  417. }
  418. }
  419. fclose($f);
  420. fclose($lock);
  421. return $ret;
  422. } catch (Exception $e) {
  423. fclose($lock);
  424. throw $e;
  425. }
  426. }
  427. }