From 598d28551d071774007172782541e1d140b8a3c1 Mon Sep 17 00:00:00 2001 From: HugoPoi Date: Sun, 6 Nov 2022 11:42:22 +0100 Subject: [PATCH] feat: add a power html rewriter to customized my old pages --- .../includes/hosted_by_franciliens.html | 8 +++ .../includes/warning_new_blog.php | 4 ++ www/index.php | 59 +++++++++++++++---- 3 files changed, 59 insertions(+), 12 deletions(-) create mode 100644 www/.content.EZtzwPjb/includes/hosted_by_franciliens.html create mode 100644 www/.content.EZtzwPjb/includes/warning_new_blog.php diff --git a/www/.content.EZtzwPjb/includes/hosted_by_franciliens.html b/www/.content.EZtzwPjb/includes/hosted_by_franciliens.html new file mode 100644 index 0000000..9f461fd --- /dev/null +++ b/www/.content.EZtzwPjb/includes/hosted_by_franciliens.html @@ -0,0 +1,8 @@ +
+
Auto-hébergé sur mon NAS
+
+
+ Ce blog est hébergé à la maison avec de l'Internet fournit par Franciliens.net +
+
+
diff --git a/www/.content.EZtzwPjb/includes/warning_new_blog.php b/www/.content.EZtzwPjb/includes/warning_new_blog.php new file mode 100644 index 0000000..ba765a4 --- /dev/null +++ b/www/.content.EZtzwPjb/includes/warning_new_blog.php @@ -0,0 +1,4 @@ +
+ Ceci est la version legacy de mon blog si vous voulez voir les nouveaux articles et et savoir pourquoi c'est par + ICI +
diff --git a/www/index.php b/www/index.php index 856cbf5..3663d76 100644 --- a/www/index.php +++ b/www/index.php @@ -30,7 +30,7 @@ * @link https://archivarix.com */ -@ini_set( 'display_errors', 1 ); +@ini_set( 'display_errors', 0 ); /** * Switch mode to integrate with 3rd party CMS. @@ -111,6 +111,24 @@ const ARCHIVARIX_INCLUDE_CUSTOM = array( 'REGEX' => 0, 'POSITION' => -1, ], + + // remove twitter widget in the right column on all the pages + [ + 'OPERATION' => 'REMOVE', + 'XPATH' => '//*[@id="twitter-2"]', + ], + + [ + 'OPERATION' => 'REPLACE', + 'XPATH' => '//*[@id="text-6"]', + 'FILE' => 'hosted_by_franciliens.html', + ], + + [ + 'OPERATION' => 'APPEND', + 'XPATH' => '//nav[contains(@class, "navbar")]', + 'FILE' => 'warning_new_blog.php', + ], ); /** @@ -368,7 +386,7 @@ function prepareContent( $file, $sourcePath ) $content = file_get_contents( $file ); foreach ( $LOADER['ARCHIVARIX_INCLUDE_CUSTOM'] as $includeCustom ) { - if ( $includeCustom['FILE'] ) { + if ( $includeCustom['FILE'] && $includeCustom['KEYPHRASE'] ) { global $includeRule; $includeRule = $includeCustom; ob_start(); @@ -391,19 +409,36 @@ function prepareContent( $file, $sourcePath ) } $content = preg_replace( '~' . $includeCustom['KEYPHRASE'] . '~is', $includedContent, $content, $includeCustom['LIMIT'] ); + } else if ( $includeCustom['OPERATION'] && $includeCustom['XPATH'] ) { + $doc = new DOMDocument(); + $doc->loadHTML($content); + $xpath = new DOMXpath($doc); + $elementsToChange = $xpath->query($includeCustom['XPATH']); + + foreach ( $elementsToChange as $elementToChange ) { + switch ( $includeCustom['OPERATION'] ) { + case 'APPEND': + case 'REPLACE' : + $fragment = $doc->createDocumentFragment(); + ob_start(); + require($sourcePath . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR . $includeCustom['FILE']); + $fragment->appendXML(ob_get_clean()); + if ($includeCustom['OPERATION'] === 'APPEND') { + $elementToChange->append($fragment->firstChild); + } else { + $elementToChange->replaceWith($fragment->firstChild); + } + break; + case 'REMOVE' : + $elementToChange->remove(); + break; + } + } + + $content = $doc->saveHTML(); } } - $doc = new DOMDocument(); - $doc->loadHTML($content); - $xpath = new DOMXpath($doc); - $hostedByElements = $xpath->query("//*[@id='text-6']"); - $hostedByElements->item(0)?->remove(); - $twitterWidgetElements = $xpath->query("//*[@id='twitter-2']"); - $twitterWidgetElements->item(0)?->remove(); - //$content = ''; - $content = $doc->saveHTML(); - if ( function_exists( 'mb_strlen' ) ) { header( 'Content-Length: ' . mb_strlen( $content, '8bit' ), true ); }