From 821cc02b81cb69ae08d19064d04d2448c6e23f13 Mon Sep 17 00:00:00 2001 From: Christopher Meinhold Date: Wed, 28 Aug 2024 11:03:03 +0200 Subject: [PATCH] =?UTF-8?q?Projektdateien=20hinzuf=C3=BCgen.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- PrüfungCMSOhneVersion.sln | 25 ++++ PrüfungCMSOhneVersion/Program.cs | 116 ++++++++++++++++++ .../PrüfungCMSOhneVersion.csproj | 15 +++ 3 files changed, 156 insertions(+) create mode 100644 PrüfungCMSOhneVersion.sln create mode 100644 PrüfungCMSOhneVersion/Program.cs create mode 100644 PrüfungCMSOhneVersion/PrüfungCMSOhneVersion.csproj diff --git a/PrüfungCMSOhneVersion.sln b/PrüfungCMSOhneVersion.sln new file mode 100644 index 0000000..7c68ee5 --- /dev/null +++ b/PrüfungCMSOhneVersion.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.10.35122.118 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PrüfungCMSOhneVersion", "PrüfungCMSOhneVersion\PrüfungCMSOhneVersion.csproj", "{F99A1659-EA94-46E5-A615-7082AA9563CC}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {F99A1659-EA94-46E5-A615-7082AA9563CC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {F99A1659-EA94-46E5-A615-7082AA9563CC}.Debug|Any CPU.Build.0 = Debug|Any CPU + {F99A1659-EA94-46E5-A615-7082AA9563CC}.Release|Any CPU.ActiveCfg = Release|Any CPU + {F99A1659-EA94-46E5-A615-7082AA9563CC}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {54E77090-9459-4214-9357-760E44D9ECE4} + EndGlobalSection +EndGlobal diff --git a/PrüfungCMSOhneVersion/Program.cs b/PrüfungCMSOhneVersion/Program.cs new file mode 100644 index 0000000..01f68fe --- /dev/null +++ b/PrüfungCMSOhneVersion/Program.cs @@ -0,0 +1,116 @@ +using System; +using System.Data; +using MySql.Data.MySqlClient; +using System.Net.Http; +using System.Threading.Tasks; +using Newtonsoft.Json.Linq; + +class Program +{ + // API Key und MySQL Verbindungseinstellungen + const string apiKey = "w41axaew9zz7w5bzunn4ujyk45uitln7ftxyxsm5m7321vputnloh21z6868www0hwwe58"; + const string connectionString = "Server=192.168.178.201;Database=domainchecker;User ID=root;Password=1td5rugut8;"; + + static async Task Main(string[] args) + { + using (MySqlConnection connection = new MySqlConnection(connectionString)) + { + try + { + connection.Open(); + + // SQL-Abfrage um alle Domains mit 'Unknown' Version abzurufen + string query = "SELECT `domain`, `version`, `letzte_pruefung`, `cms_name` FROM `wp_versionen` WHERE version = 'Unknown'"; + MySqlCommand command = new MySqlCommand(query, connection); + MySqlDataReader reader = command.ExecuteReader(); + + while (reader.Read()) + { + string domain = reader["domain"].ToString(); + string cmsName = reader["cms_name"].ToString(); + + // Überprüfe CMS Version mit API + string apiResponse = await CheckCMSVersion(domain); + + if (!string.IsNullOrEmpty(apiResponse)) + { + JObject jsonResponse = JObject.Parse(apiResponse); + var results = jsonResponse["results"]; + + // Überprüfe, ob das ermittelte CMS mit dem in der DB übereinstimmt + foreach (var result in results) + { + string name = result["name"].ToString(); + string version = result["version"].ToString(); + + if (name.Equals(cmsName, StringComparison.OrdinalIgnoreCase)) + { + reader.Close(); + // Aktualisiere die Version in der MySQL Datenbank + UpdateCMSVersion(connection, domain, version); + reader = command.ExecuteReader(); + break; + } + } + } + // Warte 10 Sekunden vor der nächsten API-Anfrage + await Task.Delay(10000); + } + + reader.Close(); + } + catch (Exception ex) + { + Console.WriteLine("Error: " + ex.Message); + } + } + } + + static async Task CheckCMSVersion(string domain) + { + string url = $"https://whatcms.org/API/Tech?key={apiKey}&url={domain}"; + + using (HttpClient client = new HttpClient()) + { + try + { + HttpResponseMessage response = await client.GetAsync(url); + response.EnsureSuccessStatusCode(); + + string responseBody = await response.Content.ReadAsStringAsync(); + return responseBody; + } + catch (HttpRequestException e) + { + Console.WriteLine($"Request error: {e.Message}"); + return null; + } + } + } + + static void UpdateCMSVersion(MySqlConnection connection, string domain, string version) + { + try + { + string updateQuery = "UPDATE `wp_versionen` SET `version` = @version, `letzte_pruefung` = NOW() WHERE `domain` = @domain"; + MySqlCommand updateCommand = new MySqlCommand(updateQuery, connection); + updateCommand.Parameters.AddWithValue("@version", version); + updateCommand.Parameters.AddWithValue("@domain", domain); + + int rowsAffected = updateCommand.ExecuteNonQuery(); + + if (rowsAffected > 0) + { + Console.WriteLine($"Domain '{domain}' erfolgreich aktualisiert mit Version '{version}'."); + } + else + { + Console.WriteLine($"Keine Aktualisierung für Domain '{domain}'."); + } + } + catch (Exception ex) + { + Console.WriteLine("Update error: " + ex.Message); + } + } +} diff --git a/PrüfungCMSOhneVersion/PrüfungCMSOhneVersion.csproj b/PrüfungCMSOhneVersion/PrüfungCMSOhneVersion.csproj new file mode 100644 index 0000000..f9327d1 --- /dev/null +++ b/PrüfungCMSOhneVersion/PrüfungCMSOhneVersion.csproj @@ -0,0 +1,15 @@ + + + + Exe + net8.0 + enable + enable + + + + + + + +