Advertisement
Guest User

Untitled

a guest
Aug 8th, 2017
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.74 KB | None | 0 0
  1. class Users {
  2. private static $key, $table, $logtable;
  3.  
  4. public static function init($key) {
  5. self::$key = $key;
  6. self::$table = DBPREFIX.$key.'s';
  7. self::$logtable = self::$table.'_access_log';
  8. }
  9.  
  10. public static function isLogged() {
  11. if(isset($_SESSION[self::$key])) {
  12. if(isset($_SESSION[self::$key]['uid'])) {
  13. $uid = intval($_SESSION[self::$key]['uid']);
  14. $user = DB::select()->from(self::$table)->where('id', '=', $uid)->execute()->object();
  15. if(count($user) > 0) {
  16. return true;
  17. }
  18. else {
  19. return false;
  20. }
  21.  
  22. }
  23. else {
  24. return false;
  25. }
  26. }
  27. else {
  28. return false;
  29. }
  30. }
  31.  
  32. public static function userExist($login) {
  33. $user = DB::select()->from(self::$table)->where('login', '=', $login)->execute()->object();
  34. if(count($user) > 0) {
  35. return true;
  36. }
  37. else {
  38. return false;
  39. }
  40. }
  41.  
  42. public static function getUser($login) {
  43. $user = DB::select()->from(self::$table)->where('login', '=', $login)->execute()->object();
  44. if(count($user) > 0) {
  45. return $user[0];
  46. }
  47. else {
  48. return false;
  49. }
  50. }
  51.  
  52. public static function generateHash($password) {
  53. $salt = self::generateSalt();
  54. $hashedPassword = crypt($password, $salt);
  55. return array('hashed_password' => $hashedPassword, 'salt' => $salt);
  56. }
  57.  
  58. private static function generateSalt() {
  59. $blowfishPre = '$2y$10$';
  60. $blowfishEnd = '$';
  61. $allowedChars ='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789./';
  62. $charsLength = strlen($allowedChars);
  63. $saltLength = 20;
  64. $salt = "";
  65. for($i=0; $i < $saltLength; $i++)
  66. {
  67. $salt .= $allowedChars[mt_rand(0,$charsLength)];
  68. }
  69. $bcrypt_salt = $blowfishPre . $salt . $blowfishEnd;
  70. return $bcrypt_salt;
  71. }
  72.  
  73. public static function comparePasswords($input, $uid) {
  74. $user = DB::select()->from(self::$table)->where('id', '=', $uid)->execute()->object();
  75. if(count($user) > 0) {
  76. $user = $user[0];
  77. $password = $user->password;
  78. return crypt($input, $password) === $password;
  79. }
  80. else {
  81. return false;
  82. }
  83. }
  84.  
  85. public static function userExit() {
  86. if(isset($_SESSION[self::$key])) {
  87. unset($_SESSION[self::$key]);
  88. }
  89. }
  90.  
  91. public static function changePassword($password, $uid) {
  92. $newPassword = self::generateHash($password);
  93. if(isset($newPassword['hashed_password']) && isset($newPassword['salt'])) {
  94. DB::update(self::$table)->set(array('password' => $newPassword['hashed_password'], 'salt' => $newPassword['salt']))->where('id', '=', $uid)->execute();
  95. }
  96. }
  97.  
  98. public static function updateTime($uid) {
  99. DB::update(self::$table)->set(array('last_login' => time()))->where('id', '=', $uid)->execute();
  100. }
  101.  
  102. public static function writeAccessLog($uid, $success) {
  103. if($success) {
  104. DB::insert(self::$logtable)->set(array('admin_id' => $uid, 'date' => time(), 'success' => '1'))->execute();
  105. }
  106. else if(!$success) {
  107. DB::insert(self::$logtable)->set(array('admin_id' => $uid, 'date' => time(), 'success' => '0'))->execute();
  108. }
  109. }
  110. }
  111.  
  112. class Users {
  113. private static $key, $table, $logtable;
  114.  
  115. public static function init($key) {
  116. self::$key = $key;
  117. self::$table = DBPREFIX.$key.'s';
  118. self::$logtable = self::$table.'_access_log';
  119. }
  120.  
  121. public static function isLogged() {
  122. if(isset($_SESSION[self::$key]) and isset($_SESSION[self::$key]['uid'])) { # Combined ifs
  123. $uid = intval($_SESSION[self::$key]['uid']);
  124. $user = DB::select()->from(self::$table)->where('id', '=', $uid)->execute()->object();
  125. return count($user) > 0; # Simplified
  126. }
  127. return false; # Removed unnecesary elses
  128. }
  129.  
  130. public static function userExist($login) {
  131. $user = DB::select()->from(self::$table)->where('login', '=', $login)->execute()->object();
  132. return count($user) > 0; # Simplified
  133. }
  134.  
  135. public static function getUser($login) {
  136. $user = DB::select()->from(self::$table)->where('login', '=', $login)->execute()->object();
  137. if(count($user) > 0) {
  138. return $user[0];
  139. }
  140. return false; # Removed unnecessary else
  141. }
  142.  
  143. public static function generateHash($password) {
  144. $salt = self::generateSalt();
  145. $hashedPassword = crypt($password, $salt);
  146. return array('hashed_password' => $hashedPassword, 'salt' => $salt);
  147. }
  148.  
  149. private static function generateSalt($blowfishPre = '$2y$10$', $blowfishEnd = '$', $allowedChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789./', $saltLength = 20) { # Added arguments for future customization
  150. $charsLength = strlen($allowedChars);
  151. $salt = "";
  152. for($i=0; $i < $saltLength; $i++)
  153. {
  154. $salt .= $allowedChars[mt_rand(0,$charsLength)];
  155. }
  156. return $blowfishPre . $salt . $blowfishEnd; # Removed unnecessary variable
  157. }
  158.  
  159. public static function comparePasswords($input, $uid) {
  160. $user = DB::select()->from(self::$table)->where('id', '=', $uid)->execute()->object();
  161. if(count($user) > 0) {
  162. $user = $user[0];
  163. $password = $user->password;
  164. return crypt($input, $password) === $password;
  165. }
  166. return false; # Removed unnecessary else
  167. }
  168.  
  169. public static function userExit() {
  170. if(isset($_SESSION[self::$key])) {
  171. unset($_SESSION[self::$key]);
  172. }
  173. }
  174.  
  175. public static function changePassword($password, $uid) {
  176. $newPassword = self::generateHash($password);
  177. if(isset($newPassword['hashed_password']) && isset($newPassword['salt'])) {
  178. DB::update(self::$table)->set(array('password' => $newPassword['hashed_password'], 'salt' => $newPassword['salt']))->where('id', '=', $uid)->execute();
  179. }
  180. }
  181.  
  182. public static function updateTime($uid) {
  183. DB::update(self::$table)->set(array('last_login' => time()))->where('id', '=', $uid)->execute();
  184. }
  185.  
  186. public static function writeAccessLog($uid, $success) {
  187. DB::insert(self::$logtable)->set(array('admin_id' => $uid, 'date' => time(), 'success' => $success ? '1' : '0'))->execute(); # Inlined success var.
  188. }
  189. }
  190.  
  191. public static function isLogged() {
  192. if(isset($_SESSION[self::$key])) {
  193. if(isset($_SESSION[self::$key]['uid'])) {
  194. $uid = intval($_SESSION[self::$key]['uid']);
  195. $user = DB::select()->from(self::$table)->where('id', '=', $uid)->execute()->object();
  196. if(count($user) > 0) {
  197. return true;
  198. }
  199. else {
  200. return false;
  201. }
  202.  
  203. }
  204. else {
  205. return false;
  206. }
  207. }
  208. else {
  209. return false;
  210. }
  211. }
  212.  
  213. public static function isLogged() {
  214. if(
  215. !isset($_SESSION[self::$key]) ||
  216. !isset($_SESSION[self::$key]['uid'] ||
  217. !ctype_digit($_SESSION[self::$key]['uid'])
  218. ) {
  219. return false;
  220. }
  221. $uid = (int)$_SESSION[self::$key]['uid'];
  222. $user = DB::select()->from(self::$table)->where('id', '=', $uid)->execute()->object();
  223. return (count($user) > 0);
  224. }
  225.  
  226. public static function comparePasswords($input, $uid) {
  227. $password = '';
  228. $user = DB::select()->from(self::$table)->where('id', '=', $uid)->execute()->object();
  229. if(count($user) > 0) {
  230. $password = $user->password;
  231. }
  232. return hash_equals($input, $password);
  233. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement