| 
									
										
										
										
											2017-11-24 00:09:36 +01:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2021-03-18 18:26:03 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-24 00:09:36 +01:00
										 |  |  | /** | 
					
						
							| 
									
										
										
										
											2021-03-18 18:26:03 +01:00
										 |  |  |  * Class for creating and deleting tokens | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2017-11-24 00:09:36 +01:00
										 |  |  | class Token | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-01-07 20:39:10 +01:00
										 |  |  |   /** | 
					
						
							|  |  |  |    * Generates a new token from user id and randomly generated salt. | 
					
						
							| 
									
										
										
										
											2018-01-12 21:35:31 +01:00
										 |  |  |    * @param int $id user ID | 
					
						
							| 
									
										
										
										
											2018-01-07 20:39:10 +01:00
										 |  |  |    * @param String $data associated with token that are important | 
					
						
							|  |  |  |    * @param timestamp $expire expiration time | 
					
						
							|  |  |  |    * @return String token | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2018-01-13 16:51:17 +01:00
										 |  |  |   public static function add($id, $data, $expire) | 
					
						
							| 
									
										
										
										
											2017-11-24 00:09:36 +01:00
										 |  |  |   { | 
					
						
							|  |  |  |     global $mysqli; | 
					
						
							|  |  |  |     $salt = uniqid(mt_rand(), true); | 
					
						
							| 
									
										
										
										
											2021-03-18 18:26:03 +01:00
										 |  |  |     $token = hash('sha256', $id . $salt); | 
					
						
							| 
									
										
										
										
											2017-11-24 00:09:36 +01:00
										 |  |  |     $stmt = $mysqli->prepare("INSERT INTO tokens VALUES(?, ?, ?, ?)"); | 
					
						
							|  |  |  |     $stmt->bind_param("siis", $token, $id, $expire, $data); | 
					
						
							|  |  |  |     $stmt->execute(); | 
					
						
							| 
									
										
										
										
											2018-04-20 16:40:12 +02:00
										 |  |  |     $stmt->get_result(); | 
					
						
							| 
									
										
										
										
											2017-11-24 00:09:36 +01:00
										 |  |  |     return $token; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-07 20:39:10 +01:00
										 |  |  |   /** | 
					
						
							|  |  |  |    * Checks whether token exists in the database and has not expired. | 
					
						
							|  |  |  |    * @param String $token | 
					
						
							|  |  |  |    * @param int $id user ID | 
					
						
							|  |  |  |    * @param String $data | 
					
						
							|  |  |  |    * @return int count of results in database | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2018-04-20 16:49:49 +02:00
										 |  |  |   public static function validate($token, $id, $data) | 
					
						
							| 
									
										
										
										
											2017-11-24 00:09:36 +01:00
										 |  |  |   { | 
					
						
							|  |  |  |     global $mysqli; | 
					
						
							|  |  |  |     $time = time(); | 
					
						
							| 
									
										
										
										
											2017-11-29 15:32:40 +01:00
										 |  |  |     $stmt = $mysqli->prepare("SELECT count(*) as count FROM tokens WHERE token = ? AND user = ? AND expire>=? AND data LIKE ?"); | 
					
						
							| 
									
										
										
										
											2017-11-24 00:09:36 +01:00
										 |  |  |     $stmt->bind_param("siis", $token, $id, $time, $data); | 
					
						
							|  |  |  |     $stmt->execute(); | 
					
						
							|  |  |  |     $query = $stmt->get_result(); | 
					
						
							|  |  |  |     return $query->fetch_assoc()['count']; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-20 16:40:12 +02:00
										 |  |  |   /** | 
					
						
							|  |  |  |    * Returns token data | 
					
						
							|  |  |  |    * @param String $token | 
					
						
							|  |  |  |    * @param int $id user ID | 
					
						
							|  |  |  |    * @return String data | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   public static function get_data($token, $id) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     global $mysqli; | 
					
						
							|  |  |  |     $stmt = $mysqli->prepare("SELECT data as count FROM tokens WHERE token = ? AND user = ?"); | 
					
						
							|  |  |  |     $stmt->bind_param("si", $token, $id); | 
					
						
							|  |  |  |     $stmt->execute(); | 
					
						
							|  |  |  |     $query = $stmt->get_result(); | 
					
						
							|  |  |  |     return $query->fetch_assoc()['data']; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-07 20:39:10 +01:00
										 |  |  |   /** | 
					
						
							|  |  |  |    * Deletes token. | 
					
						
							|  |  |  |    * @param String $token | 
					
						
							|  |  |  |    * @return void | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2017-11-24 00:09:36 +01:00
										 |  |  |   public static function delete($token) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     global $mysqli; | 
					
						
							|  |  |  |     $time = time(); | 
					
						
							|  |  |  |     $stmt = $mysqli->prepare("DELETE FROM tokens WHERE token = ? OR expire<?"); | 
					
						
							| 
									
										
										
										
											2021-03-18 18:26:03 +01:00
										 |  |  |     $stmt->bind_param("sd", $token, $time); | 
					
						
							| 
									
										
										
										
											2017-11-24 00:09:36 +01:00
										 |  |  |     $stmt->execute(); | 
					
						
							| 
									
										
										
										
											2018-04-20 17:39:34 +02:00
										 |  |  |     $stmt->get_result(); | 
					
						
							| 
									
										
										
										
											2017-11-24 00:09:36 +01:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2021-03-18 18:26:03 +01:00
										 |  |  | } |