| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace Phauthentic\Infrastructure\Http\Session\Handler; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use PDO; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use RuntimeException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use SessionHandlerInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * Generic PDO Session Handler | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | class PdoHandler implements SessionHandlerInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      * PDO Object | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      * @var \PDO | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     protected $pdo; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * Session table name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     protected $table = 'sessions'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * Session table field map | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     protected $fieldMap = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         'sid' => 'id', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         'expires' => 'expires', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         'data' => 'data' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * Constructor | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * @param \PDO $pdo PDO Object | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     public function __construct(PDO $pdo) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         $this->pdo = $pdo; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * Sets the table name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * @param string $table Table name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * @return self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     public function setTable(string $table) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         $this->table = $table; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * Close the session | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * @link http://php.net/manual/en/sessionhandlerinterface.close.php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * @return bool <p> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * The return value (usually TRUE on success, FALSE on failure). | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      * Note this value is returned internally to PHP for processing. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      * </p> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * @since 5.4.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     public function close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      * Destroy a session | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      * @link http://php.net/manual/en/sessionhandlerinterface.destroy.php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      * @param string $session_id The session ID being destroyed. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      * @return bool <p> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      * The return value (usually TRUE on success, FALSE on failure). | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      * Note this value is returned internally to PHP for processing. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      * </p> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      * @since 5.4.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     public function destroy($session_id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         $statement = $this->prepareStatement("DELETE FROM {$this->table} WHERE id = :sid"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         return $statement->execute([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |             'sid' => $session_id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |      * Cleanup old sessions | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |      * @link http://php.net/manual/en/sessionhandlerinterface.gc.php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |      * @param int $maxlifetime <p> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |      * Sessions that have not updated for | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |      * the last maxlifetime seconds will be removed. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |      * </p> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |      * @return bool <p> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      * The return value (usually TRUE on success, FALSE on failure). | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |      * Note this value is returned internally to PHP for processing. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |      * </p> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |      * @since 5.4.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     public function gc($maxlifetime) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |      * Initialize session | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |      * @link http://php.net/manual/en/sessionhandlerinterface.open.php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |      * @param string $save_path The path where to store/retrieve the session. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |      * @param string $name The session name. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |      * @return bool <p> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |      * The return value (usually TRUE on success, FALSE on failure). | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |      * Note this value is returned internally to PHP for processing. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |      * </p> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |      * @since 5.4.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |     public function open($save_path, $name) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |         return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |      * Read session data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |      * @link http://php.net/manual/en/sessionhandlerinterface.read.php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |      * @param string $session_id The session id to read data for. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |      * @return string <p> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |      * Returns an encoded string of the read data. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |      * If nothing was read, it must return an empty string. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |      * Note this value is returned internally to PHP for processing. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |      * </p> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |      * @since 5.4.0 | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 144 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 145 |  |  |     public function read($session_id) | 
            
                                                                        
                            
            
                                    
            
            
                | 146 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 147 |  |  |         $statement = $this->prepareStatement("SELECT * FROM {$this->table} WHERE id = :sid"); | 
            
                                                                        
                            
            
                                    
            
            
                | 148 |  |  |         $statement->execute([ | 
            
                                                                        
                            
            
                                    
            
            
                | 149 |  |  |             'sid' => $session_id | 
            
                                                                        
                            
            
                                    
            
            
                | 150 |  |  |         ]); | 
            
                                                                        
                            
            
                                    
            
            
                | 151 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 152 |  |  |         $result = $statement->fetch(PDO::FETCH_ASSOC); | 
            
                                                                        
                            
            
                                    
            
            
                | 153 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 154 |  |  |         if (!isset($result['data'])) { | 
            
                                                                        
                            
            
                                    
            
            
                | 155 |  |  |             return ''; | 
            
                                                                        
                            
            
                                    
            
            
                | 156 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 157 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 158 |  |  |         return $result['data']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |      * Write session data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |      * @link http://php.net/manual/en/sessionhandlerinterface.write.php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |      * @param string $session_id The session id. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |      * @param string $session_data <p> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |      * The encoded session data. This data is the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |      * result of the PHP internally encoding | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |      * the $_SESSION superglobal to a serialized | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |      * string and passing it as this parameter. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |      * Please note sessions use an alternative serialization method. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |      * </p> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |      * @return bool <p> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |      * The return value (usually TRUE on success, FALSE on failure). | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |      * Note this value is returned internally to PHP for processing. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |      * </p> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |      * @since 5.4.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |     public function write($session_id, $session_data) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |         $statement = $this->pdo->prepare("INSERT INTO {$this->table} (id, data, expires) VALUES (?,?,?)"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |         return $statement->execute([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |             $session_id, $session_data, time() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |     protected function prepareStatement(string $sql) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |         $statement = $this->pdo->prepare($sql); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |         if ($statement === false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |             throw new RuntimeException($this->pdo->errorInfo()[2]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |         return $statement; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 197 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 198 |  |  |  |