Guest User

Untitled

a guest
Oct 24th, 2017
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.82 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, 'signIn', 'signUp').
  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. default:
  41.  
  42. throw new Exception( 'Bad mode.' );
  43.  
  44. break;
  45.  
  46. case 'signUp':
  47.  
  48. $validation
  49. ->rule( 'username', 'not_empty' )
  50. ->rule( 'username', 'max_length', array( ':value', 32 ) )
  51. ->rule( 'username', 'alpha_dash', array( ':value', true ) )
  52. ->rule( 'username', 'Model_A11n::usernameExists', array( ':validation', ':value' ) )
  53. ->rule( 'password', 'not_empty' )
  54. ->rule( 'password', 'min_length', array( ':value', 6 ) )
  55. ->rule( 'password', 'max_length', array( ':value', 255 ) )
  56. ->rule( 'email', 'not_empty' )
  57. ->rule( 'email', 'email' )
  58. ->rule( 'email', 'Model_A11n::emailExists', array( ':validation', ':value' ) );
  59.  
  60. break;
  61.  
  62. case 'signIn':
  63.  
  64. $validation
  65. ->rule( 'username', 'not_empty' )
  66. ->rule( 'username', 'max_length', array( ':value', 32 ) )
  67. ->rule( 'username', 'alpha_dash', array( ':value', true ) )
  68. ->rule( 'password', 'not_empty' )
  69. ->rule( 'password', 'min_length', array( ':value', 6 ) )
  70. ->rule( 'password', 'max_length', array( ':value', 255 ) )
  71. ->rule( 'password', array( $this, 'checkPassword' ), array( ':validation', ':value' ) );
  72.  
  73. break;
  74.  
  75. }
  76.  
  77.  
  78. return $validation;
  79.  
  80. }
  81.  
  82. /**
  83. * Whether username already exists in the database?
  84. *
  85. * @param Validation Validation object.
  86. * @param string User's username.
  87. */
  88.  
  89. public static function usernameExists( Validation $validation, $username ) {
  90.  
  91. $query =
  92. DB::select( 'id' )
  93. ->from( 'users' )
  94. ->where( 'username', '=', $username );
  95.  
  96. $result =
  97. $query->execute()
  98. ->count();
  99.  
  100.  
  101. if ( $result === 1 ) {
  102.  
  103. $validation->error( 'username', 'usernameExists' );
  104.  
  105. }
  106.  
  107. }
  108.  
  109. /**
  110. * Whether e-mail already exists in the database?
  111. *
  112. * @param Validation Validation object.
  113. * @param string User's e-mail.
  114. */
  115.  
  116. public static function emailExists( Validation $validation, $email ) {
  117.  
  118. $query =
  119. DB::select( 'id' )
  120. ->from( 'users' )
  121. ->where( 'email', '=', $email );
  122.  
  123. $result =
  124. $query->execute()
  125. ->count();
  126.  
  127.  
  128. if ( $result === 1 ) {
  129.  
  130. $validation->error( 'email', 'emailExists' );
  131.  
  132. }
  133.  
  134. }
  135.  
  136. /**
  137. * Checks whether user with given username **and** given password exists. In other words, check that password is correct.
  138. *
  139. * @param Validation Validation object.
  140. * @param string User's password (plain text).
  141. */
  142.  
  143. public function checkPassword( Validation $validation, $password ) {
  144.  
  145. $username = $validation['username'];
  146. $password = $this->_hashPassword( $password, $this->_getSaltFromId( $this->_getIdFromUsername( $username ) ) );
  147.  
  148.  
  149. $query =
  150. DB::select( 'id' )
  151. ->from( 'users' )
  152. ->where( 'username', '=', $username )
  153. ->where( 'password', '=', $password );
  154.  
  155. $result =
  156. $query->execute()
  157. ->count();
  158.  
  159.  
  160. if ( $result === 0 ) {
  161.  
  162. $validation->error( 'password', 'checkPassword' );
  163.  
  164. }
  165.  
  166. }
  167.  
  168. /**
  169. * Gets user's ID from database (using username).
  170. *
  171. * @param string User's username.
  172. * @return integer User's ID from database.
  173. */
  174.  
  175. protected function _getIdFromUsername( $username ) {
  176.  
  177. $query =
  178. DB::select( 'id' )
  179. ->from( 'users' )
  180. ->where( 'username', '=', $username );
  181.  
  182. $result =
  183. $query->execute()
  184. ->get( 'id' );
  185.  
  186.  
  187. return
  188. (integer) $result;
  189.  
  190. }
  191.  
  192. /**
  193. * 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.
  194. *
  195. * @param string User's ID.
  196. * @return integer User's access from database.
  197. */
  198.  
  199. protected function _getAccessFromId( $id ) {
  200.  
  201. $query =
  202. DB::select( 'access' )
  203. ->from( 'users' )
  204. ->where( 'id', '=', $id );
  205.  
  206. $result =
  207. $query->execute()
  208. ->get( 'access' );
  209.  
  210.  
  211. return
  212. (integer) $result;
  213.  
  214. }
  215.  
  216. /**
  217. * Gets user's salt from database (using ID).
  218. *
  219. * @param string User's ID.
  220. * @return string User's salt from database.
  221. */
  222.  
  223. protected function _getSaltFromId( $id ) {
  224.  
  225. $query =
  226. DB::select( 'salt' )
  227. ->from( 'users' )
  228. ->where( 'id', '=', $id );
  229.  
  230. $result =
  231. $query->execute()
  232. ->get( 'salt' );
  233.  
  234.  
  235. return
  236. (string) $result;
  237.  
  238. }
  239.  
  240. /**
  241. * 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.
  242. *
  243. * @param string User's password.
  244. * @param string Salt (auto-generated for each user).
  245. * @param string Nuts from configuration that's stored on file-server.
  246. * @return string Hashed password.
  247. */
  248.  
  249. protected function _hashPassword( $password, $salt, $nuts = '' ) {
  250.  
  251. $nuts = ( $nuts === '' ) ? Kohana::config( 'A11n' )->get( 'nuts' ) : $nuts;
  252.  
  253.  
  254. $password =
  255. sha1(
  256. $password
  257. . $salt
  258. . $nuts
  259. );
  260.  
  261.  
  262. return $password;
  263.  
  264. }
  265.  
  266. /**
  267. * ...
  268. *
  269. * @param ... ...
  270. */
  271.  
  272. protected function _generateSalt() {
  273.  
  274. $salt = uniqId( null, true );
  275.  
  276.  
  277. return $salt;
  278.  
  279. }
  280.  
  281. /**
  282. * Adds new user in the database.
  283. *
  284. * @param Validation Validation object.
  285. */
  286.  
  287. public function signUp( $username, $password, $email ) {
  288.  
  289. $salt = $this->_generateSalt();
  290. $password = $this->_hashPassword( $password, $salt );
  291.  
  292.  
  293. DB::insert( 'users', array( 'username', 'password', 'salt', 'email' ) )
  294. ->values( array( $username, $password, $salt, $email ) )
  295. ->execute();
  296.  
  297. }
  298.  
  299. /**
  300. * ...
  301. *
  302. * @param ... ...
  303. */
  304.  
  305. public function signIn( $username ) {
  306.  
  307. $id = $this->_getIdFromUsername( $username );
  308. $access = $this->_getAccessFromId( $id );
  309.  
  310.  
  311. Session::instance()
  312. ->set(
  313. 'user',
  314. array(
  315. 'id' => $id,
  316. 'access' => $access
  317. )
  318. );
  319.  
  320. }
  321.  
  322. /**
  323. * ...
  324. *
  325. * @param ... ...
  326. */
  327.  
  328. public function signOut() {
  329.  
  330. Session::instance()
  331. ->destroy( 'user' );
  332.  
  333. }
  334.  
  335. }
Add Comment
Please, Sign In to add comment