feat(archivarix): add wordpress fallback version urls

This commit is contained in:
Hugo Poissonnet 2022-05-01 16:29:44 +02:00
parent a1a702b386
commit 5ff62ba34a

View File

@ -51,27 +51,27 @@ const ARCHIVARIX_PROTOCOL = 'any';
/**
* Return 1px.png if image does not exist.
*/
const ARCHIVARIX_FIX_MISSING_IMAGES = 1;
const ARCHIVARIX_FIX_MISSING_IMAGES = 0;
/**
* Return empty.css if css does not exist.
*/
const ARCHIVARIX_FIX_MISSING_CSS = 1;
const ARCHIVARIX_FIX_MISSING_CSS = 0;
/**
* Return empty.js if javascript does not exist.
*/
const ARCHIVARIX_FIX_MISSING_JS = 1;
const ARCHIVARIX_FIX_MISSING_JS = 0;
/**
* Return empty.ico if favicon.ico does not exist.
*/
const ARCHIVARIX_FIX_MISSING_ICO = 1;
const ARCHIVARIX_FIX_MISSING_ICO = 0;
/**
* Redirect missing html pages.
*/
const ARCHIVARIX_REDIRECT_MISSING_HTML = '/';
const ARCHIVARIX_REDIRECT_MISSING_HTML = 0;
/**
* Replace a custom key-phrase with a text file or php script.
@ -145,6 +145,12 @@ const ARCHIVARIX_SITEMAP_PATH = '';
*/
const ARCHIVARIX_CATCH_MISSING = 0;
/**
* This feature is experimental. Catch urls with a missing wordpress version
* and replace them with the most recent version available
*/
const WORDPRESS_FIX_MISSING_VERSION = 1;
/**
* Do not edit under this comment
*/
@ -165,6 +171,7 @@ $LOADER = [
'ARCHIVARIX_CUSTOM_DOMAIN' => ARCHIVARIX_CUSTOM_DOMAIN,
'ARCHIVARIX_SITEMAP_PATH' => ARCHIVARIX_SITEMAP_PATH,
'ARCHIVARIX_CATCH_MISSING' => ARCHIVARIX_CATCH_MISSING,
'WORDPRESS_FIX_MISSING_VERSION' => WORDPRESS_FIX_MISSING_VERSION,
];
$ARCHIVARIX_SETTINGS = array();
@ -285,6 +292,18 @@ function getFileMetadata( $dsn, $url )
return $metadata;
}
function getOtherVersionUrls( $url )
{
global $dsn;
$path = parse_url($url, PHP_URL_PATH);
$pdo = new PDO( $dsn );
$sth = $pdo->prepare( 'SELECT rowid, * FROM structure WHERE url LIKE :urlRegexVersion AND enabled = 1 ORDER BY filetime DESC LIMIT 1' );
// TODO better LIKE statement based on domain
$sth->execute( ['urlRegexVersion' => 'http://%'.$path.'%'] );
$metadata = $sth->fetch( PDO::FETCH_ASSOC );
return $metadata;
}
/**
* @return string
*/
@ -431,6 +450,12 @@ function handle404( $sourcePath, $url )
http_response_code( 301 );
exit( 0 );
break;
case ( $LOADER['WORDPRESS_FIX_MISSING_VERSION'] ) :
$otherVersionFound = getOtherVersionUrls($url);
if ( $otherVersionFound ) {
render( $otherVersionFound, $sourcePath, $url );
break;
}
default:
http_response_code( 404 );
}