update-site.php
· 2.5 KiB · PHP
Raw
<?php
/**
* Sync sito statico da Forgejo
* Eseguito periodicamente via Scheduled Task Alwaysdata
*/
define('REPO_API', 'https://forgejo.it/api/v1/repos/UTENTE/REPO/commits?limit=1');
define('REPO_URL', 'https://forgejo.it/UTENTE/REPO.git');
define('WWW_PATH', __DIR__ . '/www');
define('HASH_FILE', __DIR__ . '/last_commit.txt');
define('EXCLUDE', ['.git', '.gitignore', 'README.md', 'CHANGELOG.md',
'LICENSE.md', '.forgejo', 'deploy']);
function log_msg(string $msg): void {
echo '[' . date('Y-m-d H:i:s') . '] ' . $msg . PHP_EOL;
}
function get_remote_commit(): ?string {
$ch = curl_init(REPO_API);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$response = curl_exec($ch);
curl_close($ch);
if (!$response) return null;
$data = json_decode($response, true);
return $data[0]['sha'] ?? null;
}
function get_local_commit(): ?string {
if (!file_exists(HASH_FILE)) return null;
return trim(file_get_contents(HASH_FILE));
}
function update_site(): bool {
$temp = sys_get_temp_dir() . '/site-update';
if (is_dir($temp)) {
exec('rm -rf ' . escapeshellarg($temp));
}
log_msg('Clone in corso...');
exec('git clone --depth 1 ' . escapeshellarg(REPO_URL) . ' '
. escapeshellarg($temp) . ' 2>&1', $output, $code);
if ($code !== 0) {
log_msg('Errore clone: ' . implode("\n", $output));
return false;
}
$copied = 0;
foreach (scandir($temp) as $item) {
if ($item === '.' || $item === '..' || in_array($item, EXCLUDE)) {
continue;
}
$src = $temp . '/' . $item;
$dst = WWW_PATH . '/' . $item;
if (is_dir($dst)) exec('rm -rf ' . escapeshellarg($dst));
elseif (file_exists($dst)) unlink($dst);
if (is_dir($src)) exec('cp -r ' . escapeshellarg($src) . ' ' . escapeshellarg($dst));
else copy($src, $dst);
$copied++;
}
exec('rm -rf ' . escapeshellarg($temp));
log_msg("Completato: $copied elementi copiati");
return true;
}
// Main
log_msg('=== Controllo aggiornamenti ===');
$remote = get_remote_commit();
if (!$remote) {
log_msg('Impossibile contattare API Forgejo');
exit(1);
}
$local = get_local_commit();
if ($remote === $local) {
log_msg('Nessun aggiornamento');
exit(0);
}
log_msg('Aggiornamento disponibile...');
if (update_site()) {
file_put_contents(HASH_FILE, $remote);
log_msg("Hash salvato: $remote");
} else {
log_msg('Aggiornamento fallito');
exit(1);
}
| 1 | <?php |
| 2 | /** |
| 3 | * Sync sito statico da Forgejo |
| 4 | * Eseguito periodicamente via Scheduled Task Alwaysdata |
| 5 | */ |
| 6 | |
| 7 | define('REPO_API', 'https://forgejo.it/api/v1/repos/UTENTE/REPO/commits?limit=1'); |
| 8 | define('REPO_URL', 'https://forgejo.it/UTENTE/REPO.git'); |
| 9 | define('WWW_PATH', __DIR__ . '/www'); |
| 10 | define('HASH_FILE', __DIR__ . '/last_commit.txt'); |
| 11 | define('EXCLUDE', ['.git', '.gitignore', 'README.md', 'CHANGELOG.md', |
| 12 | 'LICENSE.md', '.forgejo', 'deploy']); |
| 13 | |
| 14 | function log_msg(string $msg): void { |
| 15 | echo '[' . date('Y-m-d H:i:s') . '] ' . $msg . PHP_EOL; |
| 16 | } |
| 17 | |
| 18 | function get_remote_commit(): ?string { |
| 19 | $ch = curl_init(REPO_API); |
| 20 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
| 21 | curl_setopt($ch, CURLOPT_TIMEOUT, 10); |
| 22 | $response = curl_exec($ch); |
| 23 | curl_close($ch); |
| 24 | |
| 25 | if (!$response) return null; |
| 26 | |
| 27 | $data = json_decode($response, true); |
| 28 | return $data[0]['sha'] ?? null; |
| 29 | } |
| 30 | |
| 31 | function get_local_commit(): ?string { |
| 32 | if (!file_exists(HASH_FILE)) return null; |
| 33 | return trim(file_get_contents(HASH_FILE)); |
| 34 | } |
| 35 | |
| 36 | function update_site(): bool { |
| 37 | $temp = sys_get_temp_dir() . '/site-update'; |
| 38 | |
| 39 | if (is_dir($temp)) { |
| 40 | exec('rm -rf ' . escapeshellarg($temp)); |
| 41 | } |
| 42 | |
| 43 | log_msg('Clone in corso...'); |
| 44 | exec('git clone --depth 1 ' . escapeshellarg(REPO_URL) . ' ' |
| 45 | . escapeshellarg($temp) . ' 2>&1', $output, $code); |
| 46 | |
| 47 | if ($code !== 0) { |
| 48 | log_msg('Errore clone: ' . implode("\n", $output)); |
| 49 | return false; |
| 50 | } |
| 51 | |
| 52 | $copied = 0; |
| 53 | foreach (scandir($temp) as $item) { |
| 54 | if ($item === '.' || $item === '..' || in_array($item, EXCLUDE)) { |
| 55 | continue; |
| 56 | } |
| 57 | |
| 58 | $src = $temp . '/' . $item; |
| 59 | $dst = WWW_PATH . '/' . $item; |
| 60 | |
| 61 | if (is_dir($dst)) exec('rm -rf ' . escapeshellarg($dst)); |
| 62 | elseif (file_exists($dst)) unlink($dst); |
| 63 | |
| 64 | if (is_dir($src)) exec('cp -r ' . escapeshellarg($src) . ' ' . escapeshellarg($dst)); |
| 65 | else copy($src, $dst); |
| 66 | |
| 67 | $copied++; |
| 68 | } |
| 69 | |
| 70 | exec('rm -rf ' . escapeshellarg($temp)); |
| 71 | log_msg("Completato: $copied elementi copiati"); |
| 72 | return true; |
| 73 | } |
| 74 | |
| 75 | // Main |
| 76 | log_msg('=== Controllo aggiornamenti ==='); |
| 77 | |
| 78 | $remote = get_remote_commit(); |
| 79 | if (!$remote) { |
| 80 | log_msg('Impossibile contattare API Forgejo'); |
| 81 | exit(1); |
| 82 | } |
| 83 | |
| 84 | $local = get_local_commit(); |
| 85 | |
| 86 | if ($remote === $local) { |
| 87 | log_msg('Nessun aggiornamento'); |
| 88 | exit(0); |
| 89 | } |
| 90 | |
| 91 | log_msg('Aggiornamento disponibile...'); |
| 92 | if (update_site()) { |
| 93 | file_put_contents(HASH_FILE, $remote); |
| 94 | log_msg("Hash salvato: $remote"); |
| 95 | } else { |
| 96 | log_msg('Aggiornamento fallito'); |
| 97 | exit(1); |
| 98 | } |