PageRenderTime 73ms CodeModel.GetById 40ms RepoModel.GetById 0ms app.codeStats 0ms

/include/models/CnCNet/Player.php

https://github.com/hifi-unmaintained/cncnet3-frontend
PHP | 169 lines | 127 code | 22 blank | 20 comment | 12 complexity | 847cc5addf7c7cdd90dbe1f45d1c9d52 MD5 | raw file
  1. <?php
  2. /*
  3. * Copyright (c) 2011 Toni Spets <toni.spets@iki.fi>
  4. * Copyright (c) 2011 John Sanderson <js@9point6.com>
  5. *
  6. * Permission to use, copy, modify, and distribute this software for any
  7. * purpose with or without fee is hereby granted, provided that the above
  8. * copyright notice and this permission notice appear in all copies.
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  11. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  12. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  13. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  14. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  15. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  16. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  17. */
  18. class CnCNet_Player extends CnCNet_Db_Table_Abstract
  19. {
  20. protected $_name = 'players';
  21. private $hashalgo = 'sha1';
  22. private $s_key_salt = 'derp';
  23. public function ping ( $id )
  24. {
  25. $row = $this->select()->where('id = ?', $id)->fetchRow();
  26. if ($row) {
  27. $row->active = date('Y-m-d H:i:s');
  28. $row->save();
  29. return true;
  30. }
  31. $this->logout($id);
  32. return false;
  33. }
  34. public function register ( $username, $password, $email, $ip, $port = 8054 )
  35. {
  36. $password_salt = hash ( $this->hashalgo, rand() . $username . time( ) );
  37. $password_hash = hash ( $this->hashalgo, $password . $password_salt );
  38. $new_user = array
  39. (
  40. 'nickname' => $username,
  41. 'ip' => $ip,
  42. 'port' => $port,
  43. 'created' => date( 'Y-m-d H:i:s' ),
  44. 'active' => date( 'Y-m-d H:i:s' ),
  45. 'pass_hash' => $password_hash,
  46. 'pass_salt' => $password_salt,
  47. 'email' => $email
  48. );
  49. $player_id = -1;
  50. $player_id = $this->insert( $new_user );
  51. return $player_id;
  52. }
  53. public function login ( $user, $password, $ip, $is_user_id = false, $port = 8054 )
  54. {
  55. $row = $this->select( )->where( $is_user_id ? 'id = ?' : 'nickname = ?', $user )->fetchRow( );
  56. if ( $row )
  57. {
  58. $pass_hash = hash ( $this->hashalgo, $password . $row->pass_salt );
  59. if ( strcmp ( $pass_hash, $row->pass_hash ) == 0 )
  60. {
  61. // correct password
  62. $session_key = hash ( $this->hashalgo, $ip . $row->nickname . $pass_hash . $this->s_key_salt );
  63. $user_data = array
  64. (
  65. 'active' => date( 'Y-m-d H:i:s' ),
  66. 'ip' => $ip,
  67. 'port' => $port,
  68. 'sesh_time' => date( 'Y-m-d H:i:s' ),
  69. 'sesh_key' => $session_key
  70. );
  71. $this->update( $user_data, $this->getAdapter( )->quoteInto( 'id = ?', $row->id ) );
  72. return $session_key; // success - return session key
  73. }
  74. return -1; // error - incorrect password
  75. }
  76. return -2; // error - no user exists
  77. }
  78. public function change_pass ( $user, $oldpass, $newpass, $is_user_id = false )
  79. {
  80. $row = $this->select( )->where( $is_user_id ? 'id = ?' : 'nickname = ?', $user )->fetchRow( );
  81. if ( $row )
  82. {
  83. $pass_hash = hash ( $this->hashalgo, $oldpass . $row->pass_salt );
  84. if ( strcmp ( $pass_hash, $row->pass_hash ) == 0 )
  85. {
  86. // correct password
  87. $new_pass_salt = hash ( $this->hashalgo, rand() . $username . time( ) );
  88. $new_pass_hash = hash ( $this->hashalgo, $newpass . $new_pass_salt );
  89. $session_key = hash ( $this->hashalgo, $row->ip . $row->nickname . $new_pass_hash . $this->s_key_salt );
  90. $user_data = array
  91. (
  92. 'pass_hash' => $new_pass_hash,
  93. 'pass_salt' => $new_pass_salt,
  94. 'active' => date( 'Y-m-d H:i:s' ),
  95. 'sesh_time' => date( 'Y-m-d H:i:s' ),
  96. 'sesh_key' => $session_key
  97. );
  98. $this->update( $user_data, $this->getAdapter( )->quoteInto( 'id = ?', $row->id ) );
  99. return $session_key; // success - return new session key
  100. }
  101. return -1; // error - incorrect password
  102. }
  103. return -2; // error - no user exists
  104. }
  105. public function validate_s_key ( $s_key, $ip )
  106. {
  107. $row = $this->select( )->where( 'sesh_key = ?', $s_key )->fetchRow( );
  108. if ( $row )
  109. {
  110. if ( strcmp ( $ip, $row->ip ) == 0 )
  111. {
  112. // TODO: Make this a bit better.
  113. return 1; // success
  114. }
  115. return -1; // error - incorrect password
  116. }
  117. return -2; // error - no user exists with that key
  118. }
  119. public function get_name ( $s_key )
  120. {
  121. $row = $this->select( )->where( 'sesh_key = ?', $s_key )->fetchRow( );
  122. if ( $row )
  123. {
  124. return $row->nickname; // success - return id
  125. }
  126. return -2; // error - no user exists with that key
  127. }
  128. public function get_id ( $s_key )
  129. {
  130. $row = $this->select( )->where( 'sesh_key = ?', $s_key )->fetchRow( );
  131. if ( $row )
  132. {
  133. return $row->id; // success - return id
  134. }
  135. return -2; // error - no user exists with that key
  136. }
  137. public function logout ( $id )
  138. {
  139. $this->update( array(
  140. 'logout' => date( 'Y-m-d H:i:s' ),
  141. 'sesh_key' => null,
  142. 'sesh_time' => null
  143. ), $this->getAdapter( )->quoteInto( 'id = ?', $id ) );
  144. // TODO: validate.
  145. return true;
  146. }
  147. }