|
| 1 | +<?php |
| 2 | +include("DatabaseConnector.php"); |
| 3 | +/** |
| 4 | + * Class for working with Database |
| 5 | + * |
| 6 | + * @author Pavel Vyskočil <[email protected]> |
| 7 | + */ |
| 8 | +class DatabaseCommand |
| 9 | +{ |
| 10 | + |
| 11 | + const WHITELIST = "whiteList"; |
| 12 | + const GREYLIST = "greyList"; |
| 13 | + |
| 14 | + /** |
| 15 | + * Function returns array of all IdPs in whitelist/greylist |
| 16 | + * @param string $tableName 'whitelist' or 'greylist' |
| 17 | + * @return array of all IdPs, every IdP is represents as array |
| 18 | + */ |
| 19 | + public static function getAllIdps($tableName) { |
| 20 | + $databaseConnector = new DatabaseConnector(); |
| 21 | + $conn = $databaseConnector->getConnection(); |
| 22 | + $whiteListTableName = $databaseConnector->getWhiteListTableName(); |
| 23 | + $greyListTableName = $databaseConnector->getGreyListTableName(); |
| 24 | + $table = null; |
| 25 | + $listOfIdPs = array(); |
| 26 | + assert($conn != NULL); |
| 27 | + |
| 28 | + if ($tableName == self::WHITELIST) { |
| 29 | + $table = $whiteListTableName; |
| 30 | + } else if ($tableName == self::GREYLIST) { |
| 31 | + $table = $greyListTableName; |
| 32 | + } |
| 33 | + |
| 34 | + $stmt = $conn->prepare("SELECT * FROM " . $table); |
| 35 | + |
| 36 | + if ($stmt) { |
| 37 | + $ex =$stmt->execute(); |
| 38 | + if ($ex === false) { |
| 39 | + SimpleSAML\Logger::error("Error during select all from " . $table); |
| 40 | + } |
| 41 | + |
| 42 | + $stmt->bind_result($timestamp, $entityId, $reason); |
| 43 | + while ($stmt->fetch()) { |
| 44 | + $idp = array(); |
| 45 | + $idp['timestamp'] = $timestamp; |
| 46 | + $idp['entityid'] = $entityId; |
| 47 | + $idp['reason'] = $reason; |
| 48 | + array_push($listOfIdPs, $idp); |
| 49 | + } |
| 50 | + |
| 51 | + $stmt->close(); |
| 52 | + } else { |
| 53 | + SimpleSAML\Logger::error("Error during preparing statement"); |
| 54 | + } |
| 55 | + |
| 56 | + $conn->close(); |
| 57 | + return $listOfIdPs; |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Function returns array of all entityId in whitelist/greylist |
| 62 | + * @param string $tableName 'whitelist' or 'greylist' |
| 63 | + * @return array of entityIds |
| 64 | + */ |
| 65 | + public static function getAllEntityIds($tableName) { |
| 66 | + $databaseConnector = new DatabaseConnector(); |
| 67 | + $conn = $databaseConnector->getConnection(); |
| 68 | + $whiteListTableName = $databaseConnector->getWhiteListTableName(); |
| 69 | + $greyListTableName = $databaseConnector->getGreyListTableName(); |
| 70 | + $table = null; |
| 71 | + $listOfIdPs = array(); |
| 72 | + assert($conn != NULL); |
| 73 | + |
| 74 | + if ($tableName == self::WHITELIST) { |
| 75 | + $table = $whiteListTableName; |
| 76 | + } else if ($tableName == self::GREYLIST) { |
| 77 | + $table = $greyListTableName; |
| 78 | + } |
| 79 | + |
| 80 | + $stmt = $conn->prepare("SELECT * FROM " . $table); |
| 81 | + |
| 82 | + if ($stmt) { |
| 83 | + $ex =$stmt->execute(); |
| 84 | + if ($ex === false) { |
| 85 | + SimpleSAML\Logger::error("Error during select all entityIds from " . $table); |
| 86 | + } |
| 87 | + |
| 88 | + $stmt->bind_result($timestamp, $entityId, $reason); |
| 89 | + while ($stmt->fetch()) { |
| 90 | + array_push($listOfIdPs, $entityId); |
| 91 | + } |
| 92 | + |
| 93 | + $stmt->close(); |
| 94 | + } else { |
| 95 | + SimpleSAML\Logger::error("Error during preparing statement"); |
| 96 | + } |
| 97 | + |
| 98 | + $conn->close(); |
| 99 | + return $listOfIdPs; |
| 100 | + } |
| 101 | + |
| 102 | + |
| 103 | + /** |
| 104 | + * Function inserts the line into table with $tableName |
| 105 | + * @param string $tableName 'whitelist' or 'greylist' |
| 106 | + * @param string $entityId |
| 107 | + * @param string $reason |
| 108 | + */ |
| 109 | + public static function insertTolist($tableName, $entityId, $reason) { |
| 110 | + $databaseConnector = new DatabaseConnector(); |
| 111 | + $conn = $databaseConnector->getConnection(); |
| 112 | + $whiteListTableName = $databaseConnector->getWhiteListTableName(); |
| 113 | + $greyListTableName = $databaseConnector->getGreyListTableName(); |
| 114 | + $table = null; |
| 115 | + assert($conn != NULL); |
| 116 | + |
| 117 | + if ($tableName == self::WHITELIST) { |
| 118 | + $table = $whiteListTableName; |
| 119 | + } else if ($tableName == self::GREYLIST) { |
| 120 | + $table = $greyListTableName; |
| 121 | + } |
| 122 | + |
| 123 | + $stmt = $conn->prepare("INSERT INTO " . $table . " (entityId, reason) VALUES (?, ?)"); |
| 124 | + |
| 125 | + if ($stmt) { |
| 126 | + $stmt->bind_param("ss", $entityId, $reason); |
| 127 | + $ex =$stmt->execute(); |
| 128 | + if ($ex === false) { |
| 129 | + SimpleSAML\Logger::error("Error during inserting entityId " . $entityId . " into " . $table); |
| 130 | + } |
| 131 | + |
| 132 | + SimpleSAML\Logger::debug("EntityId " . $entityId . " was inserted into " . $table); |
| 133 | + $stmt->close(); |
| 134 | + } else { |
| 135 | + SimpleSAML\Logger::error("Error during preparing statement"); |
| 136 | + } |
| 137 | + |
| 138 | + $conn->close(); |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Function deletes the line from table with $tableName and $entityID |
| 143 | + * @param string $tableName 'whitelist' or 'greylist' |
| 144 | + * @param string $entityId |
| 145 | + */ |
| 146 | + public static function deleteFromList($tableName, $entityId) { |
| 147 | + $databaseConnector = new DatabaseConnector(); |
| 148 | + $conn = $databaseConnector->getConnection(); |
| 149 | + $whiteListTableName = $databaseConnector->getWhiteListTableName(); |
| 150 | + $greyListTableName = $databaseConnector->getGreyListTableName(); |
| 151 | + $table = null; |
| 152 | + assert($conn != NULL); |
| 153 | + |
| 154 | + if ($tableName == self::WHITELIST) { |
| 155 | + $table = $whiteListTableName; |
| 156 | + } else if ($tableName == self::GREYLIST) { |
| 157 | + $table = $greyListTableName; |
| 158 | + } |
| 159 | + |
| 160 | + $stmt = $conn->prepare("DELETE FROM " . $table . " WHERE entityId=?"); |
| 161 | + |
| 162 | + if ($stmt) { |
| 163 | + $stmt->bind_param("s", $entityId); |
| 164 | + $ex =$stmt->execute(); |
| 165 | + if ($ex === false) { |
| 166 | + SimpleSAML\Logger::error("Error during deleting entityId " . $entityId . " from " . $table); |
| 167 | + } |
| 168 | + |
| 169 | + SimpleSAML\Logger::debug("EntityId " . $entityId . " was deleted from " . $table); |
| 170 | + $stmt->close(); |
| 171 | + } else { |
| 172 | + SimpleSAML\Logger::error("Error during preparing statement"); |
| 173 | + } |
| 174 | + |
| 175 | + $conn->close(); |
| 176 | + } |
| 177 | +} |
0 commit comments