Last active
February 18, 2019 21:57
-
-
Save 0xFEEDC0DE64/9e3886642a8b84e5a7b6a4426f61ff4b to your computer and use it in GitHub Desktop.
small utility to manage software versions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<style> | |
dl { | |
display: grid; | |
grid-template-columns: max-content auto; | |
} | |
dt { | |
grid-column-start: 1; | |
} | |
dd { | |
grid-column-start: 2; | |
} | |
</style> | |
</head> | |
<body> | |
<?php | |
$db = new SQLite3('liste.db'); | |
if ($db === FALSE) { | |
die('could not open database.'); | |
} | |
if (!$db->exec('CREATE TABLE IF NOT EXISTS Programs (' . | |
'ID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, ' . | |
'Name VARCHAR(255) NOT NULL UNIQUE,' . | |
'Link VARCHAR(255) NOT NULL,' . | |
'InstalledVersion VARCHAR(255) NOT NULL,' . | |
'OnlineVersion VARCHAR(255) NOT NULL' . | |
');')) { | |
die('could not create table: ' . $db->lastErrorMsg()); | |
} | |
if (!isset($_GET['action'])) { | |
echo '<a href="?action=add">Hinzufügen</a>'; | |
echo '<table border="1">'; | |
echo '<thead>'; | |
echo '<tr>'; | |
echo '<th>ID</th>'; | |
echo '<th>Name</th>'; | |
echo '<th>Chocolatey Version</th>'; | |
echo '<th>Installierte Version</th>'; | |
echo '<th>Aktionen</th>'; | |
echo '</tr>'; | |
echo '</thead>'; | |
echo '<tbody>'; | |
$result = $db->query('SELECT ID, Name, Link, InstalledVersion, OnlineVersion FROM Programs;'); | |
if ($result === FALSE) { | |
die('could not list programs: ' . $db->lastErrorMsg()); | |
} | |
while ($row = $result->fetchArray()) { | |
echo '<tr>'; | |
echo '<td>' . $row['ID'] . '</td>'; | |
echo '<td>' . htmlentities($row['Name']) . '</td>'; | |
echo '<td><a href="' . htmlentities($row['Link']) . '">' . htmlentities($row['OnlineVersion']) . '</a> (<a href="?action=refresh&id=' . $row['ID'] . '">Aktualisieren</a>)</td>'; | |
echo '<td style="background-color: ' . ($row['OnlineVersion'] == $row['InstalledVersion'] ? '#AAFFAA' : '#FFAAAA') . '">' . htmlentities($row['InstalledVersion']) . ' (<a href="?action=update&id=' . $row['ID'] . '">Aktualisieren</a>)</td>'; | |
echo '<td>'; | |
echo '<a href="?action=edit&id=' . $row['ID'] . '">Bearbeiten</a> '; | |
echo '<a href="?action=delete&id=' . $row['ID'] . '">Löschen</a>'; | |
echo '</td>'; | |
echo '</tr>'; | |
} | |
echo '</tbody>'; | |
echo '</table>'; | |
} elseif ($_GET['action'] === 'add') { | |
$error = NULL; | |
if ($_SERVER['REQUEST_METHOD'] == 'POST') { | |
if (!isset($_POST['Name'], $_POST['Link'])) { | |
$error = 'Verwenden Sie nur Formulare von der Homepage!'; | |
goto showAdd; | |
} | |
if (empty($Name = trim($_POST['Name'])) || | |
empty($Link = trim($_POST['Link']))) { | |
$error = 'Füllen Sie alle Felder aus!'; | |
goto showAdd; | |
} | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $Link); | |
curl_setopt($ch, CURLOPT_HEADER, 0); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
$content = curl_exec($ch); | |
if ($content === FALSE) { | |
$message = 'Fehler beim Request für Version: ' . curl_error($ch); | |
} | |
curl_close($ch); | |
if (!empty($message)) { | |
goto showAdd; | |
} | |
if (!preg_match('/Downloads of v ([^<]+)/', $content, $matches)) { | |
$message = 'Konnte Version auf Seite nicht auslesen!'; | |
goto showAdd; | |
} | |
if (count($matches) != 2) { | |
$message = 'Konnte Version auf Seite nicht auslesen!'; | |
goto showAdd; | |
} | |
$stmt = $db->prepare('INSERT INTO Programs (Name, Link, InstalledVersion, OnlineVersion) VALUES (:Name, :Link, :InstalledVersion, :OnlineVersion);'); | |
if ($stmt === FALSE) { | |
$error = 'could not prepare insert statement: ' . $db->lastErrorMsg(); | |
goto showAdd; | |
} | |
$stmt->bindValue(':Name', $Name); | |
$stmt->bindValue(':Link', $Link); | |
$stmt->bindValue(':InstalledVersion', ''); | |
$stmt->bindValue(':OnlineVersion', $matches[1]); | |
$result = $stmt->execute(); | |
if ($result === FALSE) { | |
$error = 'could not execute insert statement: ' . $db->lastErrorMsg(); | |
goto showAdd; | |
} | |
$result->finalize(); | |
$stmt->close(); | |
header('Location: ?'); | |
die('<a href="?">Weiter</a>'); | |
} | |
showAdd: | |
if (!is_null($error)) { | |
echo '<p>' . htmlentities($error) . '</p>'; | |
} | |
echo '<form method="POST">'; | |
echo '<dl>'; | |
echo '<dt><label for="inputName">Name:</label></dt>'; | |
echo '<dd><input type="text" id="inputName" name="Name" required /></dd>'; | |
echo '<dt><label for="inputLink">Chocolatey-Link:</label></dt>'; | |
echo '<dd><input type="url" id="inputLink" name="Link" required /></dd>'; | |
echo '</dl>'; | |
echo '<button type="submit">Hinzufügen</button>'; | |
echo '</form>'; | |
} elseif ($_GET['action'] === 'edit') { | |
} elseif ($_GET['action'] === 'delete') { | |
$error = NULL; | |
if (!isset($_GET['id'])) { | |
die('id not set!'); | |
} | |
if ($_SERVER['REQUEST_METHOD'] == 'POST') { | |
$stmt = $db->prepare('DELETE FROM Programs WHERE ID = :ID;'); | |
if ($stmt === FALSE) { | |
$error = 'could not prepare delete statement: ' . $db->lastErrorMsg(); | |
goto showAdd; | |
} | |
$stmt->bindValue(':ID', $_GET['id']); | |
$result = $stmt->execute(); | |
if ($result === FALSE) { | |
$error = 'could not execute delete statement: ' . $db->lastErrorMsg(); | |
goto showAdd; | |
} | |
$result->finalize(); | |
$stmt->close(); | |
header('Location: ?'); | |
die('<a href="?">Weiter</a>'); | |
} | |
showDelete: | |
if (!is_null($error)) { | |
echo '<p>' . htmlentities($error) . '</p>'; | |
} | |
echo '<form method="POST">'; | |
echo '<p>Möchten Sie den Eintrag wirklich löschen?</p>'; | |
echo '<button type="submit">Löschen</button>'; | |
echo '</form>'; | |
} elseif ($_GET['action'] === 'refresh') { | |
if (!isset($_GET['id'])) { | |
die('id not set!'); | |
} | |
$stmt = $db->prepare('SELECT Link FROM Programs WHERE ID = :ID;'); | |
if ($stmt === FALSE) { | |
$error = 'could not prepare search statement: ' . $db->lastErrorMsg(); | |
goto showAdd; | |
} | |
$stmt->bindValue(':ID', $_GET['id']); | |
$result = $stmt->execute(); | |
if ($result === FALSE) { | |
$error = 'could not execute search statement: ' . $db->lastErrorMsg(); | |
goto showAdd; | |
} | |
$row = $result->fetchArray(); | |
if (!$row) { | |
die('could not find id!'); | |
} | |
$result->finalize(); | |
$stmt->close(); | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $row['Link']); | |
curl_setopt($ch, CURLOPT_HEADER, 0); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
$content = curl_exec($ch); | |
if ($content === FALSE) { | |
die('Fehler beim Request für Version: ' . curl_error($ch)); | |
} | |
curl_close($ch); | |
if (!preg_match('/Downloads of v ([^<]+)/', $content, $matches)) { | |
die('Konnte Version auf Seite nicht auslesen!'); | |
} | |
if (count($matches) != 2) { | |
die('Konnte Version auf Seite nicht auslesen!'); | |
} | |
$stmt = $db->prepare('UPDATE Programs SET OnlineVersion = :OnlineVersion WHERE ID = :ID;'); | |
if ($stmt === FALSE) { | |
$error = 'could not prepare update statement: ' . $db->lastErrorMsg(); | |
goto showAdd; | |
} | |
$stmt->bindValue(':ID', $_GET['id']); | |
$stmt->bindValue(':OnlineVersion', $matches[1]); | |
$result = $stmt->execute(); | |
if ($result === FALSE) { | |
$error = 'could not execute update statement: ' . $db->lastErrorMsg(); | |
goto showAdd; | |
} | |
$result->finalize(); | |
$stmt->close(); | |
header('Location: ?'); | |
die('<a href="?">Weiter</a>'); | |
} elseif ($_GET['action'] === 'update') { | |
} else { | |
die('unknown action ' . htmlentities($_GET['action'])); | |
} | |
?> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment