Guest User

Untitled

a guest
Jun 19th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.34 KB | None | 0 0
  1. <?php
  2. class User {
  3. private $_db,
  4. $_data,
  5. $_sessionName,
  6. $_cookieName,
  7. $_isLoggedIn;
  8.  
  9. public function __construct($user = null) {
  10. $this->_db = DB::getInstance();
  11.  
  12. $this->_sessionName = Config::get('session/session_name');
  13. $this->_cookieName = Config::get('remember/cookie_name');
  14.  
  15. if(!$user) {
  16. if(Session::exists($this->_sessionName)) {
  17. $user = Session::get($this->_sessionName);
  18.  
  19. if($this->find($user)) {
  20. $this->_isLoggedIn = true;
  21. } else {
  22. // process Logout
  23. }
  24. }
  25. } else {
  26. $this->find($user);
  27. }
  28.  
  29. }
  30.  
  31. public function create($fields = array()) {
  32. if(!$this->_db->insert('users', $fields)) {
  33. throw new Exception('There was a problem creating an account.');
  34. }
  35. }
  36.  
  37. public function find($user = null) {
  38. if($user) {
  39. $field = (is_numeric($user)) ? 'id' : 'username';
  40. $data = $this->_db->get('users', array($field, '=', $user));
  41.  
  42. if($data->count()) {
  43. $this->_data = $data->first();
  44. return true;
  45. }
  46. }
  47. return false;
  48. }
  49.  
  50. public function login($username = null, $password = null, $remember = false) {
  51. $user = $this->find($username);
  52.  
  53. if($user) {
  54. if($this->data()->password === Hash::make($password, $this->data()->salt)) {
  55. Session::put($this->_sessionName, $this->data()->id);
  56.  
  57. if($remember) {
  58. $hash = Hash::unique();
  59. $hashCheck = $this->_db->get('users_session', array('user_id', '=', $this->data()->id));
  60.  
  61. if(!$hashCheck->count()) {
  62. $this->_db->insert('users_session', array(
  63. 'user_id' => $this->data()->id,
  64. 'hash' => $hash
  65. ));
  66. } else {
  67. $hash = $hashCheck->first()->hash;
  68. }
  69.  
  70. Cookie::put($this->_cookieName, $hash, Config::get('remember/cookie_expiry'));
  71. }
  72.  
  73. return true;
  74. }
  75. }
  76.  
  77. return false;
  78. }
  79.  
  80. public function logout() {
  81. Session::delete($this->_sessionName);
  82. }
  83.  
  84. public function data() {
  85. return $this->_data;
  86. }
  87.  
  88. public function isLoggedIn() {
  89. return $this->_isLoggedIn;
  90. }
  91. }
  92.  
  93. <?php
  94. session_start();
  95.  
  96. $GLOBALS['config'] = array(
  97. 'mysql' => array(
  98. 'host' => 'localhost',
  99. 'username' => '\\\',
  100. 'password' => '///////',
  101. 'db' => 'users-pass'
  102. ),
  103. 'remember' => array(
  104. 'cookie_name' => 'hash',
  105. 'cookie_expiry' => 604800
  106. ),
  107. 'session' => array(
  108. 'session_name' => 'user',
  109. 'token_name' => 'token'
  110. )
  111. );
  112.  
  113. spl_autoload_register(function($class) {
  114. require_once 'classes/' . $class . '.php';
  115. });
  116.  
  117. require_once 'functions/sanitize.php';
  118.  
  119. if(Cookie::exists(Config::get('remember/cookie_name')) && !Session::exists(Config::get('session/session_name'))) {
  120. echo 'User asked to be remembered';
  121. }
  122.  
  123. <?php
  124. class Cookie {
  125. public static function exists($name) {
  126. return (isset($_COOKIE[$name])) ? true : false;
  127. }
  128.  
  129. public static function get($name) {
  130. return $_COOKIE[$name];
  131. }
  132.  
  133. public static function put($name, $value, $expiry) {
  134. if(setcookie($name, $value, time() + $expiry, '/')) {
  135. return true;
  136. }
  137. return false;
  138. }
  139.  
  140. public static function delete($name) {
  141. self::put($name, '', time() -1);
  142. }
  143. }
  144.  
  145. <?php
  146. class Hash {
  147.  
  148. public static function make($string, $salt = '') {
  149. return hash('sha256', $string . $salt);
  150. }
  151.  
  152. public static function salt($length) {
  153. #return mcrypt_create_iv($length);
  154. return substr(str_shuffle(str_repeat("0123456789abcdefghijklmnopqrstuvwxyz", $length)), 0, $length);
  155. }
  156.  
  157. public static function unique() {
  158. return self::make(uniqid());
  159. }
  160. }
  161.  
  162. <?php
  163. class DB {
  164. private static $_instance = null;
  165. private $_pdo,
  166. $_query,
  167. $_error =false,
  168. $_results,
  169. $_count = 0;
  170.  
  171. private function __construct() {
  172. try {
  173. $this->_pdo = new PDO('mysql:host=' . Config::get('mysql/host') . ';dbname=' . Config::get('mysql/db'), Config::get('mysql/username'), Config::get('mysql/password'));
  174. } catch(PDOException $e) {
  175. die($e->getMessage());
  176. }
  177. }
  178.  
  179. public static function getInstance() {
  180. if(!isset(self::$_instance)) {
  181. self::$_instance = new DB();
  182. }
  183. return self::$_instance;
  184. }
  185.  
  186. public function query($sql, $params = array()) {
  187. $this->_error = false;
  188. if($this->_query = $this->_pdo->prepare($sql)) {
  189. $x = 1;
  190. if(count($params)) {
  191. foreach($params as $param) {
  192. $this->_query->bindValue($x, $param);
  193. $x++;
  194. }
  195. }
  196.  
  197. if($this->_query->execute()) {
  198. $this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ);
  199. $this->_count = $this->_query->rowCount();
  200. } else {
  201. $this->_error = true;
  202. }
  203. }
  204.  
  205. return $this;
  206. }
  207.  
  208. public function action($action, $table, $where = array()) {
  209. if(count($where) === 3) {
  210. $operators = array('=', '>', '<', '>=', '<=');
  211.  
  212. $field =$where[0];
  213. $operator =$where[1];
  214. $value =$where[2];
  215.  
  216. if(in_array($operator, $operators)) {
  217. $sql = "{$action} FROM {$table} WHERE {$field} {$operator} ?";
  218.  
  219. if(!$this->query($sql, array($value))->error()) {
  220. return $this;
  221. }
  222. }
  223. }
  224. return false;
  225. }
  226. public function get($table, $where) {
  227. return $this->action('SELECT *', $table, $where);
  228.  
  229. }
  230.  
  231. public function delete($table, $where) {
  232. return $this->action('DELETE *', $table, $where);
  233. }
  234.  
  235. public function insert($table, $fields = array()) {
  236. $keys = array_keys($fields);
  237. $values = '';
  238. $x = 1;
  239.  
  240. foreach($fields as $field) {
  241. $values .='?';
  242. if($x < count($fields)) {
  243. $values .=', ';
  244. }
  245. $x++;
  246. }
  247.  
  248. $sql= "INSERT INTO {$table} (`" . implode('`, `', $keys) . "`) VALUES ({$values})";
  249.  
  250. if(!$this->query($sql, $fields)->error()) {
  251. return true;
  252. }
  253.  
  254. return false;
  255. }
  256.  
  257. public function update($table, $id, $fields) {
  258. $set = '';
  259. $x = 1;
  260.  
  261. foreach($fields as $name => $value) {
  262. $set .= "{$name} = ?";
  263. if($x < count($fields)) {
  264. $set .= ', ';
  265. }
  266. $x++;
  267. }
  268.  
  269. $sql = "UPDATE {$table} SET {$set} WHERE id = ($id)";
  270.  
  271. if(!$this->query($sql, $fields)->error()) {
  272. return true;
  273. }
  274.  
  275. return false;
  276. }
  277.  
  278. public function results() {
  279. return $this->_results;
  280. }
  281.  
  282. Public function first() {
  283. return $this->results()[0];
  284. }
  285.  
  286. public function error() {
  287. return $this->_error;
  288. }
  289.  
  290. public function count() {
  291. return $this->_count;
  292. }
  293. }
Add Comment
Please, Sign In to add comment