Skip to content

Commit

Permalink
change updater
Browse files Browse the repository at this point in the history
  • Loading branch information
cheinisch committed Sep 24, 2024
1 parent c5186e6 commit 29c6136
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 55 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
![GitHub Release](https://img.shields.io/github/v/release/cheinisch/journal?label=Latest%20Version)
# Jrnl

Jrnl is a simple php based personal diary or log book.
Jrnl is a simple php based personal diary or log book. This Webapplication uses UiKit 3.15.14 for the GUI.

### Key Features

Expand Down
7 changes: 4 additions & 3 deletions system/function/dbquery.php
Original file line number Diff line number Diff line change
Expand Up @@ -341,17 +341,18 @@ function updateUserSettings($userId, $name, $email, $password, $oldPassword) {
// Wenn das Passwort aktualisiert werden soll, neu hashen
if (!empty($password)) {
$newHashedPassword = password_hash($password, PASSWORD_DEFAULT);
$stmt = $db->prepare("UPDATE users SET name = :name, email = :email, password = :password WHERE id = :userId");
$stmt = $db->prepare("UPDATE users SET username = :name, email = :email, password = :password WHERE id = :userId");
$stmt->bindParam(':password', $newHashedPassword, PDO::PARAM_STR);
} else {
$stmt = $db->prepare("UPDATE users SET name = :name, email = :email WHERE id = :userId");
$stmt = $db->prepare("UPDATE users SET username = :name, email = :email WHERE id = :userId");
}

$stmt->bindParam(':name', $name, PDO::PARAM_STR);
$stmt->bindParam(':email', $email, PDO::PARAM_STR);
$stmt->bindParam(':userId', $userId, PDO::PARAM_INT);

if ($stmt->execute()) {

return "Einstellungen erfolgreich aktualisiert.";
} else {
return "Fehler beim Aktualisieren der Einstellungen.";
Expand All @@ -361,7 +362,7 @@ function updateUserSettings($userId, $name, $email, $password, $oldPassword) {
function getUserData($userId)
{
$db = getDatabaseConnection();
$stmt = $db->prepare("SELECT name, email FROM users WHERE id = :userId");
$stmt = $db->prepare("SELECT username, email FROM users WHERE id = :userId");
$stmt->bindParam(':userId', $userId, PDO::PARAM_INT);
$stmt->execute();
$user = $stmt->fetch(PDO::FETCH_ASSOC);
Expand Down
73 changes: 37 additions & 36 deletions system/function/function.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,51 +206,52 @@ function getVersionFromFile($filePath) {
}
}

function getLatestGitHubRelease($owner, $repo, $prerelease = false) {
$url = "https://api.github.com/repos/$owner/$repo/releases";
$token = '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'; // Ersetzen Sie dies durch Ihr echtes Token

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0');
curl_setopt($ch, CURLOPT_HTTPHEADER, [
"Authorization: token $token"
]);
$response = curl_exec($ch);
curl_close($ch);

if ($response === false) {
throw new Exception("Fehler beim Abrufen der GitHub-Version.");
}

$data = json_decode($response, true);
function getLatestGitHubRelease($repoOwner, $repoName, $prerelease = false) {
// GitHub API-URL basierend auf dem gewünschten Release-Typ (stable oder prerelease)
$url = $prerelease
? "https://api.github.com/repos/$repoOwner/$repoName/releases" // Alle Releases abrufen
: "https://api.github.com/repos/$repoOwner/$repoName/releases/latest"; // Nur das neueste stable Release abrufen

// cURL Initialisierung
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0'); // GitHub benötigt einen User-Agent Header
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // Weiterleitungen folgen

// API Antwort
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

// Fehlerbehandlung, wenn die Antwort leer ist oder der HTTP-Status nicht 200 ist
if ($response === false || $httpCode !== 200) {
throw new Exception("Fehler: Die GitHub API konnte nicht erreicht werden (HTTP-Code: $httpCode)");
}

if (json_last_error() !== JSON_ERROR_NONE) {
throw new Exception("Fehler beim Verarbeiten der GitHub-Antwort: " . json_last_error_msg());
}
// JSON-Antwort decodieren
$data = json_decode($response, true);

if (!is_array($data)) {
throw new Exception("Unerwartete Antwort von GitHub.");
}
// Wenn es sich um ein Pre-Release handelt, suche das erste Prerelease
if ($prerelease && is_array($data)) {
foreach ($data as $release) {
if (isset($release['prerelease']) && $release['prerelease'] === true) {
return $release['tag_name']; // Gibt die Version des Prereleases zurück
}
}
}

foreach ($data as $release) {
if ($prerelease && $release['prerelease']) {
return $release['tag_name'];
} elseif (!$prerelease && !$release['prerelease']) {
return $release['tag_name'];
}
}
// Für stabile Releases (oder wenn kein Pre-Release gefunden wurde)
if (isset($data['tag_name'])) {
return $data['tag_name']; // Gibt die Version des neuesten stabilen Releases zurück
}

throw new Exception("Die Versionsnummer konnte nicht abgerufen werden.");
throw new Exception("Fehler: Die Versionsnummer konnte nicht abgerufen werden.");
}


function get_versionfromgit()
{
$owner = 'cheinisch';
$repo = 'journal';

try {
$repoOwner = 'cheinisch';
$repoName = 'journal'; // Ersetze 'repository' durch den Namen des Repositories
Expand Down
15 changes: 9 additions & 6 deletions system/function/update.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ function updateFromGitHub($repoOwner, $repoName, $prerelease = false) {


// 2. Neueste Release-URL von GitHub abrufen
$url = "https://api.github.com/repos/$repoOwner/$repoName/releases";
$token = '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'; // Ersetzen Sie dies durch Ihren GitHub-Personal-Access-Token
$url = $prerelease
? "https://api.github.com/repos/$repoOwner/$repoName/releases" // Alle Releases abrufen
: "https://api.github.com/repos/$repoOwner/$repoName/releases/latest"; // Nur das neueste stable Release abrufen

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0');
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: token $token"));
$response = curl_exec($ch);
curl_close($ch);

Expand All @@ -64,15 +64,18 @@ function updateFromGitHub($repoOwner, $repoName, $prerelease = false) {

$data = json_decode($response, true);

// Überprüfen, ob es sich um ein Pre-Release handelt
if ($prerelease) {
if (!is_array($data)) {
throw new Exception("Erwartete Daten als Array, aber Antwort ist kein Array.");
}

foreach ($data as $release) {
if ($release['prerelease']) {
if (isset($release['prerelease']) && $release['prerelease']) {
$data = $release;
break;
}
}
} else {
$data = $data[0];
}

if (!isset($data['zipball_url'])) {
Expand Down
33 changes: 25 additions & 8 deletions system/gui/settings-user.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
$oldPassword = $_POST['old_password'];

$message = updateUserSettings($userId['id'], $name, $email, $password, $oldPassword);
destroy_session();
create_session($name);
} elseif (isset($_GET['export']) && $_GET['export'] == 'xml') {
exportPostsAsXML($userId['id']);
}
Expand All @@ -23,20 +25,33 @@
$user = getUserData($userIdDB['id']);
?>

<h2>Benutzereinstellungen</h2>

<?php

include('template/head.php');

$users = getAllUsers();

?>

<!-- Zweispaltiges Layout -->
<div class="uk-container uk-margin-top">
<div uk-grid>
<!-- Blogposts -->
<div class="uk-width-2-3@s uk-width-1-1">
<h2>Benutzereinstellungen</h2>

<?php if (isset($message)): ?>
<p><?php echo htmlspecialchars($message); ?></p>
<?php endif; ?>

<form method="POST" action="index.php?user">
<div>
<form class="uk-form-stacked" method="POST" action="index.php?user">
<div class="uk-margin">
<label for="name">Name:</label>
<input type="text" name="name" id="name" value="<?php echo htmlspecialchars($user['name']); ?>" required>
<input class="uk-input" type="text" name="name" id="name" value="<?php echo htmlspecialchars($user['username']); ?>" required>
</div>
<div>
<div class="uk-margin">
<label for="email">E-Mail:</label>
<input type="email" name="email" id="email" value="<?php echo htmlspecialchars($user['email']); ?>" required>
<input class="uk-input" type="email" name="email" id="email" value="<?php echo htmlspecialchars($user['email']); ?>" required>
</div>
<div>
<label for="old_password">Altes Passwort:</label>
Expand All @@ -57,6 +72,8 @@

<h2>Beiträge exportieren</h2>
<a href="index.php?user&export=xml">Beiträge als XML exportieren</a>

</div>
</div>
</div>
</body>
</html>
2 changes: 1 addition & 1 deletion system/gui/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@
Version from Git: <?php echo get_versionfromgit(); ?>
</div>
<div class="uk-width-1-2@s">
<?php
<?php
if(check_update()) {
echo "<button onclick=\"location.href='index.php?update'\" class=\"uk-button uk-button-primary\" type=\"button\">Update verfügbar</button>";
}
Expand Down

0 comments on commit 29c6136

Please sign in to comment.