PageRenderTime 49ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/application/models/UserID.php

https://github.com/jverkoey/snaapilookup
PHP | 119 lines | 59 code | 12 blank | 48 comment | 2 complexity | e4cef5f25d13a20e10a1a164f5fbed12 MD5 | raw file
  1. <?php
  2. class Model_UserID {
  3. /** Model_DbTable_UserID */
  4. protected $_table;
  5. /**
  6. * Retrieve table object
  7. *
  8. * @return Model_DbTable_UserID
  9. */
  10. public function getTable() {
  11. if (null === $this->_table) {
  12. // since the dbTable is not a library item but an application item,
  13. // we must require it to use it
  14. require_once APPLICATION_PATH . '/models/DbTable/UserID.php';
  15. $this->_table = new Model_DbTable_UserID;
  16. }
  17. return $this->_table;
  18. }
  19. /**
  20. * Strip a username of non-alphanumeric characters.
  21. */
  22. public function cleanUsername($username) {
  23. return strtolower(ereg_replace("[^A-Za-z0-9_]", "", trim($username)));
  24. }
  25. /**
  26. * Generate a random salt string.
  27. */
  28. public function generateSalt() {
  29. $dynamicSalt = '';
  30. for( $i = 0; $i < 16; $i++ ) {
  31. $dynamicSalt .= chr(rand(33, 126));
  32. }
  33. return $dynamicSalt;
  34. }
  35. /**
  36. * Encrypt a password using a salt and md5.
  37. */
  38. public function hashPassword($username, $password, $salt) {
  39. return md5(Zend_Registry::get('staticSalt') . $password . $salt);
  40. }
  41. /**
  42. * Fetch a user id from a username
  43. *
  44. * @param string $openid
  45. * @return null|int
  46. */
  47. public function fetchUserId($username) {
  48. $username = $this->cleanUsername($username);
  49. $row = $this->getTable()->find($username)->toArray();
  50. return sizeof($row) ? $row[0]['user_id'] : null;
  51. }
  52. /**
  53. * Fetch an array of usernames from a user id
  54. *
  55. * @param int $id
  56. * @return null|array(string)
  57. */
  58. public function fetchUserIDsByUser($id) {
  59. $table = $this->getTable();
  60. return $table->fetchAll($table->select()->from($table, array('username'))
  61. ->where('user_id = ?', $id))
  62. ->toArray();
  63. }
  64. /**
  65. * Insert a new pairing of a username with a user id.
  66. *
  67. * @param string $username
  68. * @param string $password
  69. * @param int $id
  70. * @return true if succeeded
  71. */
  72. public function attachUserID($username, $password, $id) {
  73. $username = $this->cleanUsername($username);
  74. $salt = $this->generateSalt();
  75. $hash = $this->hashPassword($username, $password, $salt);
  76. $data = array(
  77. 'username' => $username,
  78. 'password' => $hash,
  79. 'salt' => $salt,
  80. 'user_id' => $id
  81. );
  82. $this->getTable()->insert($data);
  83. return true;
  84. }
  85. /**
  86. * Remove a pairing of a username and user id.
  87. *
  88. * @param string $username
  89. * @param int $id
  90. */
  91. public function detachUserID($username, $id) {
  92. $username = $this->cleanUsername($username);
  93. $table = $this->getTable();
  94. $this->getTable()->delete(array(
  95. $table->getAdapter()->quoteInto('username = ?', $username),
  96. $table->getAdapter()->quoteInto('user_id = ?', $id)));
  97. }
  98. /**
  99. * Remove all pairings of username and user id.
  100. *
  101. * @param int $id
  102. */
  103. public function detachUserIDsByUser($id) {
  104. $table = $this->getTable();
  105. $this->getTable()->delete($table->getAdapter()->quoteInto('user_id = ?', $id));
  106. }
  107. }