Guest User

Untitled

a guest
Oct 26th, 2017
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.22 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * A11n (by daGrevis).
  5. *
  6. * @version 0.3
  7. * @author daGrevis
  8. * @copyright (C) 2011 by daGrevis
  9. * @license MIT License
  10. */
  11.  
  12.  
  13. defined( 'SYSPATH' ) or exit( 'No direct script access.' );
  14.  
  15.  
  16. class Model_A11n extends Model {
  17.  
  18. /**
  19. * Model's validation.
  20. *
  21. * @param string Mode (example, 'sign-in', 'sign-up').
  22. * @param Validation Validation object.
  23. */
  24.  
  25. public function validation( $mode, Validation $extraValidation = null ) {
  26.  
  27. if ( empty( $extraValidation ) ) {
  28.  
  29. $validation = Validation::factory( array() );
  30.  
  31. } else {
  32.  
  33. $validation = $extraValidation;
  34.  
  35. }
  36.  
  37.  
  38. switch ( $mode ) {
  39.  
  40. case 'sign-up':
  41.  
  42. $validation
  43. ->rule( 'username', 'not_empty' )
  44. ->rule( 'username', 'max_length', array( ':value', 32 ) )
  45. ->rule( 'username', 'alpha_dash', array( ':value', true ) )
  46. ->rule( 'username', 'Model_A11n::usernameExists', array( ':validation', ':value' ) )
  47. ->rule( 'password', 'not_empty' )
  48. ->rule( 'password', 'min_length', array( ':value', 6 ) )
  49. ->rule( 'password', 'max_length', array( ':value', 255 ) )
  50. ->rule( 'email', 'not_empty' )
  51. ->rule( 'email', 'email' )
  52. ->rule( 'email', 'Model_A11n::emailExists', array( ':validation', ':value' ) );
  53.  
  54. break;
  55.  
  56. case 'sign-in':
  57.  
  58. $validation
  59. ->rule( 'username', 'not_empty' )
  60. ->rule( 'username', 'max_length', array( ':value', 32 ) )
  61. ->rule( 'username', 'alpha_dash', array( ':value', true ) )
  62. ->rule( 'password', 'not_empty' )
  63. ->rule( 'password', 'min_length', array( ':value', 6 ) )
  64. ->rule( 'password', 'max_length', array( ':value', 255 ) )
  65. ->rule( 'password', array( $this, 'checkPassword' ), array( ':validation', ':value' ) );
  66.  
  67. break;
  68.  
  69. }
  70.  
  71.  
  72. return $validation;
  73.  
  74. }
  75.  
  76. /**
  77. * Whether username already exists in the database?
  78. *
  79. * @param Validation Validation object.
  80. * @param string User's username.
  81. */
  82.  
  83. public static function usernameExists( Validation $validation, $username ) {
  84.  
  85. $query =
  86. DB::select( 'id' )
  87. ->from( 'users' )
  88. ->where( 'username', '=', $username );
  89.  
  90. $result =
  91. $query->execute()
  92. ->count();
  93.  
  94.  
  95. if ( $result ) {
  96.  
  97. $validation->error( 'username', 'usernameExists' );
  98.  
  99. }
  100.  
  101. }
  102.  
  103. /**
  104. * Whether e-mail already exists in the database?
  105. *
  106. * @param Validation Validation object.
  107. * @param string User's e-mail.
  108. */
  109.  
  110. public static function emailExists( Validation $validation, $email ) {
  111.  
  112. $query =
  113. DB::select( 'id' )
  114. ->from( 'users' )
  115. ->where( 'email', '=', $email );
  116.  
  117. $result =
  118. $query->execute()
  119. ->count();
  120.  
  121.  
  122. if ( $result ) {
  123.  
  124. $validation->error( 'email', 'emailExists' );
  125.  
  126. }
  127.  
  128. }
  129.  
  130. /**
  131. * Checks whether user with given username **and** given password exists. In other words, check that password is correct.
  132. *
  133. * @param Validation Validation object.
  134. * @param string User's password (plain text).
  135. */
  136.  
  137. public function checkPassword( Validation $validation, $password ) {
  138.  
  139. $username = $validation['username'];
  140. $hashedPassword = $this->_hashPassword( $password, $this->_getSaltFromId( $this->_getIdFromUsername( $username ) ) );
  141.  
  142.  
  143. $query =
  144. DB::select( 'id' )
  145. ->from( 'users' )
  146. ->where( 'username', '=', $username )
  147. ->where( 'password', '=', $hashedPassword );
  148.  
  149. $result =
  150. $query->execute()
  151. ->count();
  152.  
  153.  
  154. if ( !$result ) {
  155.  
  156. $validation->error( 'password', 'checkPassword' );
  157.  
  158. }
  159.  
  160. }
  161.  
  162. /**
  163. * Gets user's ID from database (using username).
  164. *
  165. * @param string User's username.
  166. * @return integer User's ID from database.
  167. */
  168.  
  169. protected function _getIdFromUsername( $username ) {
  170.  
  171. $query =
  172. DB::select( 'id' )
  173. ->from( 'users' )
  174. ->where( 'username', '=', $username );
  175.  
  176. $result =
  177. $query->execute()
  178. ->get( 'id' );
  179.  
  180.  
  181. return
  182. (integer) $result;
  183.  
  184. }
  185.  
  186. /**
  187. * Gets user's access from database (using ID). Use this only when you know that you need value from database because access is saved in session as well.
  188. *
  189. * @param string User's ID.
  190. * @return integer User's access from database.
  191. */
  192.  
  193. protected function _getAccessFromId( $id ) {
  194.  
  195. $query =
  196. DB::select( 'access' )
  197. ->from( 'users' )
  198. ->where( 'id', '=', $id );
  199.  
  200. $result =
  201. $query->execute()
  202. ->get( 'access' );
  203.  
  204.  
  205. return
  206. (integer) $result;
  207.  
  208. }
  209.  
  210. /**
  211. * Gets user's salt from database (using ID).
  212. *
  213. * @param string User's ID.
  214. * @return string User's salt from database.
  215. */
  216.  
  217. protected function _getSaltFromId( $id ) {
  218.  
  219. $query =
  220. DB::select( 'salt' )
  221. ->from( 'users' )
  222. ->where( 'id', '=', $id );
  223.  
  224. $result =
  225. $query->execute()
  226. ->get( 'salt' );
  227.  
  228.  
  229. return
  230. (string) $result;
  231.  
  232. }
  233.  
  234. /**
  235. * Hashes password (plain text), salt (value stored in database (auto-generated for each user)) and nuts (value from configuration that's stored on file-server (MUST BE CHANGED BEFORE PRODUCTION STAGE FROM DEFAULT ("Cartman")!)) using SHA-1 algorythm.
  236. *
  237. * @param string User's password.
  238. * @param string Salt (auto-generated for each user).
  239. * @param string Nuts from configuration that's stored on file-server.
  240. * @return string Hashed password.
  241. */
  242.  
  243. protected function _hashPassword( $password, $salt, $nuts = '' ) {
  244.  
  245. if ( $nuts === '' ) { $nuts = Kohana::config( 'a11n' )->nuts; }
  246.  
  247. $password =
  248. sha1(
  249. $password
  250. . $salt
  251. . $nuts
  252. );
  253.  
  254.  
  255. return $password;
  256.  
  257. }
  258.  
  259.  
  260. /**
  261. * Adds new user in the database.
  262. *
  263. * @param Validation Validation object.
  264. */
  265.  
  266. public function signUp( Validation $validation ) {
  267.  
  268. $username = $validation['username'];
  269. $salt = uniqId( null, true );
  270. $password = $this->_hashPassword( $validation['password'], $salt );
  271. $email = $validation['email'];
  272.  
  273.  
  274. $query =
  275. DB::insert( 'users', array( 'username', 'password', 'salt', 'email' ) )
  276. ->values( array( $username, $password, $salt, $email ) );
  277.  
  278. $result =
  279. $query->execute();
  280.  
  281. }
  282.  
  283. }
Add Comment
Please, Sign In to add comment