Skip to content

Commit

Permalink
[1.3]: novo admin controlador de feeds
Browse files Browse the repository at this point in the history
  • Loading branch information
altendorfme committed Sep 11, 2024
1 parent 5477012 commit 999b9c8
Show file tree
Hide file tree
Showing 10 changed files with 197 additions and 77 deletions.
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ RUN sed -i "s|'localhost'|'${DB_HOST}'|g" /var/www/html/config/appConfig.php \
&& sed -i "s|''|'${DB_PASSWORD}'|g" /var/www/html/config/appConfig.php \
&& sed -i "s|'lerama'|'${DB_NAME}'|g" /var/www/html/config/appConfig.php \
&& sed -i "s|'https://lerama.test'|'${SITE_URL}'|g" /var/www/html/config/appConfig.php \
&& sed -i "s|'Lerama'|'${SITE_NAME}'|g" /var/www/html/config/appConfig.php
&& sed -i "s|'Lerama'|'${SITE_NAME}'|g" /var/www/html/config/appConfig.php \
&& sed -i "s|'admin_pass'|'${ADMIN_PASSWORD}'|g" /var/www/html/config/appConfig.php

EXPOSE 80

Expand Down
11 changes: 1 addition & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,6 @@ O Lerama é um agregador de feeds ATOM e RSS2.0 feito como alternativa ao [OpenO

## Docker

Primeiro vamos gerar um arquivo de configuração dos feeds, é um array com informações do sites que terão dados coletados:

`curl -o ./lerama/config/feedsConfig.php https://raw.githubusercontent.com/manualdousuario/lerama/main/feedsConfig.php.sample`

Se um site é removido da lista, ficará como *inativo* para o sistema, mas nenhum registro vai ser apagado.
Se trocar o nome, essa informação será atualizada.
Ao adicionar ou remover, a proxima rotina irá atualizar automaticamente os dados.

Apos instalar o docker, vamos criar um *compose*:

`curl -o ./docker-compose.yml https://raw.githubusercontent.com/manualdousuario/lerama/main/docker-compose.yml`
Expand All @@ -35,8 +27,7 @@ services:
DB_NAME: BANCO_DE_DADOS
SITE_URL: https://lerama.xyz
SITE_NAME: Lerama
volumes:
- ./config/feeds_config.php:/var/www/html/config/feeds_config.php
ADMIN_PASSWORD: p@ssw0rd
depends_on:
- db
services:
Expand Down
4 changes: 4 additions & 0 deletions default.conf
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ server {
try_files $uri $uri/ /sobre.php?$query_string;
}

location /admin {
try_files $uri $uri/ /admin.php?$query_string;
}

location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass 127.0.0.1:9000;
Expand Down
3 changes: 1 addition & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ services:
DB_NAME: BANCO_DE_DADOS
SITE_URL: https://lerama.xyz
SITE_NAME: Lerama
volumes:
- ./config/feeds_config.php:/var/www/html/config/feeds_config.php
ADMIN_PASSWORD: p@ssw0rd
depends_on:
- db
services:
Expand Down
8 changes: 0 additions & 8 deletions feedsConfig.php.sample

This file was deleted.

186 changes: 186 additions & 0 deletions src/admin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
<?php
require_once __DIR__ . '/class/Database.php';

use Src\Database;

$appConfig = require __DIR__ . '/config/appConfig.php';

session_start();

if (!isset($_SESSION['admin_logged_in'])) {
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['admin_password'])) {
$submittedPassword = $_POST['admin_password'];
if ($submittedPassword === $appConfig['admin_password']) {
$_SESSION['admin_logged_in'] = true;
header('Location: /admin');
exit;
} else {
$loginError = "🚷";
}
}

include __DIR__ . '/header.php';
?>
<div class="container mt-5">
<h2>🔑</h2>
<?php if (isset($loginError)): ?>
<div class="alert alert-danger"><?php echo $loginError ?></div>
<?php endif; ?>
<form method="POST" action="/admin">
<div class="mb-3">
<label for="admin_password" class="form-label">Senha</label>
<input type="password" class="form-control" id="admin_password" name="admin_password" required>
</div>
<button type="submit" class="btn btn-primary">Entrar</button>
</form>
</div>
<?php
include __DIR__ . '/footer.php';
exit;
}

$db = Database::getInstance($appConfig['database']);

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['name'], $_POST['url'], $_POST['feed_url'])) {
$name = $_POST['name'];
$url = $_POST['url'];
$feed_url = $_POST['feed_url'];

if ($name && $url && $feed_url) {
$stmt = $db->prepare("INSERT INTO sites (name, url, feed_url, status) VALUES (:name, :url, :feed_url, 'active')");
$stmt->execute([
'name' => $name,
'url' => $url,
'feed_url' => $feed_url
]);

header('Location: /admin');
exit;
} else {
$error = "Preencha todos os campos!";
}
}

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['site_id'], $_POST['new_name'])) {
$site_id = $_POST['site_id'];
$new_name = $_POST['new_name'];

$stmt = $db->prepare("UPDATE sites SET name = :new_name WHERE id = :id");
$stmt->execute([
'new_name' => $new_name,
'id' => $site_id
]);

header('Location: /admin');
exit;
}

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['site_id'], $_POST['status']) && !isset($_POST['new_name'])) {
$site_id = $_POST['site_id'];
$status = $_POST['status'];

$stmt = $db->prepare("UPDATE sites SET status = :status WHERE id = :id");
$stmt->execute([
'status' => $status,
'id' => $site_id
]);

header('Location: /admin');
exit;
}

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['site_id'], $_POST['status'])) {
$site_id = $_POST['site_id'];
$status = $_POST['status'];

$stmt = $db->prepare("UPDATE sites SET status = :status WHERE id = :id");
$stmt->execute([
'status' => $status,
'id' => $site_id
]);

header('Location: /admin');
exit;
}

$sites = $db->query("SELECT * FROM sites")->fetchAll(PDO::FETCH_ASSOC);

include __DIR__ . '/header.php';
?>

<div class="container mt-5">
<h2>Sites</h2>

<table class="table table-striped mt-4">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>URL</th>
<th>Feed URL</th>
<th>Status</th>
<th></th>
<th>Error</th>
</tr>
</thead>
<tbody>
<?php foreach ($sites as $site): ?>
<tr class="<?php echo $site['status'] == 'inactive' ? 'table-danger' : '' ?>">
<td class="align-middle">
<small alt="Ultimo erro <?php echo $site['last_error_check'] ?>" class="text-muted"><?php echo htmlspecialchars($site['id']) ?></small>
</td>
<td class="align-middle">
<form method="POST" action="/admin">
<div class="input-group input-group-sm flex-nowrap">
<input type="hidden" name="site_id" value="<?php echo htmlspecialchars($site['id']) ?>">
<input type="text" name="new_name" value="<?php echo htmlspecialchars($site['name']) ?>" class="form-control">
<button type="submit" class="btn btn-secondary">💾</button>
</div>
</form>
</td>
<td class="align-middle"><a href="<?php echo htmlspecialchars($site['url']) ?>" target="_blank"><?php echo htmlspecialchars($site['url']) ?></a></td>
<td class="align-middle"><a href="<?php echo htmlspecialchars($site['feed_url']) ?>" target="_blank"><?php echo htmlspecialchars($site['feed_url']) ?></a></td>
<td class="text-center align-middle"><?php echo $site['status'] == 'active' ? '🟢' : '🔴' ?></td>
<td class="align-middle">
<form method="POST" action="/admin" style="display:inline;">
<input type="hidden" name="site_id" value="<?php echo htmlspecialchars($site['id']) ?>">
<select name="status" class="form-select form-select-sm" onchange="this.form.submit()">
<option value="active" <?php echo $site['status'] === 'active' ? 'selected' : '' ?>>Ativo</option>
<option value="inactive" <?php echo $site['status'] === 'inactive' ? 'selected' : '' ?>>Inativo</option>
</select>
</form>
</td>
<td class="text-center align-middle">
<?php if($site['error_count']) { ?>
<small alt="Ultimo erro <?php echo $site['last_error_check'] ?>" class="text-muted"><?php echo $site['error_count'] ?></small>
<?php } ?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>

<h3 class="mt-5">Novo Site</h3>

<?php if (isset($error)): ?>
<div class="alert alert-danger"><?php echo $error ?></div>
<?php endif; ?>

<form method="POST" action="/admin">
<div class="mb-3">
<label for="name" class="form-label">Nome</label>
<input type="text" class="form-control" id="name" name="name" required>
</div>
<div class="mb-3">
<label for="url" class="form-label">URL</label>
<input type="url" class="form-control" id="url" name="url" required>
</div>
<div class="mb-3">
<label for="feed_url" class="form-label">Feed URL</label>
<input type="url" class="form-control" id="feed_url" name="feed_url" required>
</div>
<button type="submit" class="btn btn-primary">Adicionar</button>
</form>
</div>

<?php include __DIR__ . '/footer.php'; ?>
1 change: 1 addition & 0 deletions src/config/appConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
'dbname' => getenv('DB_NAME') ?: 'lerama',
'charset' => 'utf8mb4'
],
'admin_password' => getenv('ADMIN_PASSWORD') ?: 'admin_pass',
'site' => [
'url' => getenv('SITE_URL') ?: 'https://lerama.test',
'name' => getenv('SITE_NAME') ?: 'Lerama'
Expand Down
53 changes: 0 additions & 53 deletions src/config/feedsConfig.php

This file was deleted.

3 changes: 1 addition & 2 deletions src/cron/fetchFeeds.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@
use Src\FeedFetcher;

$appConfig = require __DIR__ . '/../config/appConfig.php';
$feedsConfig = require __DIR__ . '/../config/feedsConfig.php';

echo "Processando rotinas (".date("d.m.y").").\n";

try {
$db = Database::getInstance($appConfig['database']);
$feed_fetcher = new FeedFetcher($db, $feedsConfig);
$feed_fetcher = new FeedFetcher($db);

$feed_fetcher->fetchFeeds();
echo "Feeds processados.\n";
Expand Down
2 changes: 1 addition & 1 deletion src/footer.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<footer class="bg-light text-center py-3 mt-3 mt-md-5">
<p class="m-0">Quer incluir o seu blog pessoal no Lerama? <a href="mailto:[email protected]">Envie um e-mail</a>.</p>
<p class="m-0">Com ❤️ por <a href="https://altendorfme.com/" target="_blank">altendorfme</a> · Versão 1.2.4</p>
<p class="m-0">Com ❤️ por <a href="https://altendorfme.com/" target="_blank">altendorfme</a> · Versão 1.3</p>
</footer>
<script src="//cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
</body>
Expand Down

0 comments on commit 999b9c8

Please sign in to comment.