Skip to content

Commit

Permalink
Wiki is leaking
Browse files Browse the repository at this point in the history
add source

Update wiki.php
  • Loading branch information
Reqrefusion committed Sep 12, 2024
1 parent b0137dc commit e1bf8e4
Show file tree
Hide file tree
Showing 3 changed files with 250 additions and 12 deletions.
1 change: 1 addition & 0 deletions .htaccess
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-d
RewriteRule ^/?wiki(/.*)?$ %{DOCUMENT_ROOT}/wiki/index.php [L]
RewriteRule ^wiki-([^/]*)\.php$ /wiki.php?page=$1 [QSA,L]

#remove php file extension-e.g. https://example.com/file.php will become https://example.com/file
RewriteCond %{REQUEST_FILENAME} !-d
Expand Down
24 changes: 12 additions & 12 deletions header.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,32 +84,32 @@
<a class="nav-link my-2 rounded-pill dropdown-toggle" data-bs-toggle="dropdown" href="#" role="button" aria-haspopup="true" aria-expanded="false"><?php echo _('Documentation'); ?></a>

<div class="dropdown-menu-mobile-right dropdown-community dropdown-menu dropdown-menu-right">
<a class="dropdown-item text-dark" href="<?php echo _('https://wiki.freecad.org/Main_Page'); ?>"><?php echo _('Documentation index'); ?></a>
<a class="dropdown-item text-dark" href="<?php echo _('https://wiki.freecad.org/Getting_started'); ?>"><?php echo _('Getting started'); ?></a>
<a class="dropdown-item text-dark" href="<?php echo _('https://wiki.freecad.org/User_hub'); ?>"><?php echo _('Users documentation'); ?></a>
<a class="dropdown-item text-dark" href="<?php echo _('https://wiki.freecad.org/Manual'); ?>"><?php echo _('The FreeCAD manual'); ?></a>
<a class="dropdown-item text-dark" href="<?php echo _('https://wiki.freecad.org/Workbenches'); ?>"><?php echo _('Workbenches documentation'); ?></a>
<a class="dropdown-item text-dark" href="<?php echo _('https://wiki.freecad.org/Power_users_hub'); ?>"><?php echo _('Python coding documentation'); ?></a>
<a class="dropdown-item text-dark" href="<?php echo _('https://wiki.freecad.org/Developer_hub'); ?>"><?php echo _('C++ coding documentation'); ?></a>
<a class="dropdown-item text-dark" href="<?php echo _('https://wiki.freecad.org/Tutorials'); ?>"><?php echo _('Tutorials'); ?></a>
<a class="dropdown-item text-dark" href="<?php echo _('https://wiki.freecad.org/Frequently_asked_questions'); ?>"><?php echo _('Frequently asked questions'); ?></a>
<a class="dropdown-item text-dark" href="wiki-Main_Page.php<?php echo $langStr; ?>"><?php echo _('Documentation index'); ?></a>
<a class="dropdown-item text-dark" href="wiki-Getting_started.php<?php echo $langStr; ?>"><?php echo _('Getting started'); ?></a>
<a class="dropdown-item text-dark" href="wiki-User_hub.php<?php echo $langStr; ?>"><?php echo _('Users documentation'); ?></a>
<a class="dropdown-item text-dark" href="wiki-Manual.php<?php echo $langStr; ?>"><?php echo _('The FreeCAD manual'); ?></a>
<a class="dropdown-item text-dark" href="wiki-Workbenches.php<?php echo $langStr; ?>"><?php echo _('Workbenches documentation'); ?></a>
<a class="dropdown-item text-dark" href="wiki-Power_users_hub.php<?php echo $langStr; ?>"><?php echo _('Python coding documentation'); ?></a>
<a class="dropdown-item text-dark" href="wiki-Developer_hub.php<?php echo $langStr; ?>"><?php echo _('C++ coding documentation'); ?></a>
<a class="dropdown-item text-dark" href="wiki-Tutorials.php<?php echo $langStr; ?>"><?php echo _('Tutorials'); ?></a>
<a class="dropdown-item text-dark" href="wiki-Frequently_asked_questions.php<?php echo $langStr; ?>"><?php echo _('Frequently asked questions'); ?></a>
<a class="dropdown-item text-dark" href="<?php echo $privacyurl; ?>"><?php echo _('Privacy policy'); ?></a>
<a class="dropdown-item text-dark" href="<?php echo _('https://wiki.freecad.org/About_FreeCAD'); ?>"><?php echo _('About FreeCAD'); ?></a>
<a class="dropdown-item text-dark" href="wiki-About_FreeCAD.php<?php echo $langStr; ?>"><?php echo _('About FreeCAD'); ?></a>
</div>
</li>

<li class="nav-item dropdown">
<a class="nav-link my-2 rounded-pill dropdown-toggle" data-bs-toggle="dropdown" href="#" role="button" aria-haspopup="true" aria-expanded="false"><?php echo _('Contribute'); ?></a>

<div class="dropdown-menu-mobile-left dropdown-community dropdown-menu dropdown-menu-right">
<a class="dropdown-item text-dark" href="<?php echo _('https://wiki.freecad.org/Help_FreeCAD'); ?>"><?php echo _('How to help'); ?></a>
<a class="dropdown-item text-dark" href="wiki-Help_FreeCAD.php<?php echo $langStr; ?>"><?php echo _('How to help'); ?></a>
<a class="dropdown-item text-dark" href="<?php echo $sponsorurl; ?>"><?php echo _('Donate / Sponsor'); ?></a>
<a class="dropdown-item text-dark" href="https://github.com/FreeCAD/FreeCAD/issues"><?php echo _('Report a bug'); ?></a>
<a class="dropdown-item text-dark" href="https://github.com/FreeCAD/FreeCAD/pulls"><?php echo _('Make a pull request'); ?></a>
<a class="dropdown-item text-dark" href="https://fpa.freecad.org/programs.html"><?php echo _('Jobs and funding'); ?></a>
<a class="dropdown-item text-dark" href="<?php echo $contributingurl; ?>"><?php echo _('Contribution guidelines'); ?></a>
<a class="dropdown-item text-dark" href="https://freecad.github.io/DevelopersHandbook/"><?php echo _('Developers handbook'); ?></a>
<a class="dropdown-item text-dark" href="<?php echo _('https://wiki.freecad.org/Localisation'); ?>"><?php echo _('Translations'); ?></a>
<a class="dropdown-item text-dark" href="wiki-Localisation.php<?php echo $langStr; ?>"><?php echo _('Translations'); ?></a>
</div>
</li>

Expand Down
237 changes: 237 additions & 0 deletions wiki.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,237 @@
<?php
$page = $_GET["page"];
$currentpage = "wiki-".$page.".php";
include("header.php");
?>

<style>
.mw-parser-output {
margin-top: 3em;
}

.mw-parser-output a.external {
background-position: center right;
background-repeat: no-repeat;
background-size: 0.857em;
}

.mw-parser-output a {
word-wrap: break-word;
}

pre, .mw-code {
padding: 1em;
white-space: pre-wrap;
overflow-x: hidden;
word-wrap: break-word;
}

.wikitable {
background-color: #f8f9fa;
color: #202122;
margin: 1em 0;
margin-left: 0;
border: 1px solid #a2a9b1;
border-collapse: collapse;
}

.wikitable > tr > th,
.wikitable > * > tr > th {
background-color: #eaecf0;
text-align: center;
}

.wikitable > tr > th,
.wikitable > tr > td,
.wikitable > * > tr > th,
.wikitable > * > tr > td {
border: 1px solid #a2a9b1;
padding: 0.2em 0.4em;
}

figure[typeof~="mw:File"].mw-halign-left,
figure[typeof~="mw:File/Frameless"].mw-halign-left {
margin: 0 0.5em 0.5em 0;
clear: left;
float: left;
}

figure[typeof~="mw:File"].mw-halign-center,
figure[typeof~="mw:File/Frameless"].mw-halign-center {
margin: 0 auto;
display: table;
border-collapse: collapse;
clear: none;
float: none;
}

figure[typeof~="mw:File"] > figcaption,
figure[typeof~="mw:File/Frameless"] > figcaption {
display: none;
}

.mw-selflink {
color: #000;
}

ul.gallery.gallery.gallery {
margin: 2px;
padding: 2px;
display: block;
}

li.gallerybox {
vertical-align: top;
display: inline-block;
}

.docnav, .manualtoc {
overflow: hidden;
background-color: rgba(52, 58, 64, 0.5);
}

.mw-parser-output img {
max-width: 660px;
height: auto;
}

@media (max-width: 786px) {
.mw-parser-output img {
max-width: 100%;
}
}


</style>

<div id="main" class="container-fluid whitelinks md">

<?php

function createDomAndXpath($html) {
$dom = new DOMDocument();
libxml_use_internal_errors(true);
$dom->loadHTML(mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8'), LIBXML_HTML_NODEFDTD | LIBXML_HTML_NOIMPLIED);
libxml_clear_errors();
return array($dom, new DOMXPath($dom));
}

function removeNodesByXPath($xpath, $xpathQuery) {
$nodes = $xpath->query($xpathQuery);
foreach ($nodes as $node) {
$node->parentNode->removeChild($node);
}
}

function removeDivsByClass($xpath, $classes) {
$xpathQuery = "//div[contains(@class, '" . implode("') or contains(@class, '", $classes) . "')]";
removeNodesByXPath($xpath, $xpathQuery);
}

function removeDivsById($xpath, $ids) {
$xpathQuery = "//div[@id='" . implode("' or @id='", $ids) . "']";
removeNodesByXPath($xpath, $xpathQuery);
}

function convertRelativeLinksToWikiFormat($xpath, $lang = 'en') {
foreach ($xpath->query("//a[@href]") as $node) {
$href = $node->getAttribute('href');
if (!preg_match('/^(http|https):\/\//', $href)) {
$href = trim($href, "/");
if (preg_match('/\/([a-z]{2})$/i', $href, $matches)) {
$langCode = $matches[1];
$newHref = "/wiki-" . trim($href, '/' . $langCode) . ".php?lang=" . $langCode;
} else {
$newHref = "/wiki-" . trim($href, '/') . ".php";
if ($lang != "en") {
$newHref .= "?lang=" . $lang;
}
}
$node->setAttribute('href', $newHref);
}
}
}

function removeFileLinks($xpath) {
foreach ($xpath->query("//a[contains(@href, 'File:')]") as $node) {
$parent = $node->parentNode;
while ($node->firstChild) {
$parent->insertBefore($node->firstChild, $node);
}
$parent->removeChild($node);
}
}

function removeInlineStylesByClasses($xpath, $classNames) {
foreach ($classNames as $className) {
foreach ($xpath->query("//div[contains(@class, '$className')]") as $node) {
$node->removeAttribute('style');
}
}
}

function addPrefixToImageSrc($xpath, $baseImageUrl) {
foreach ($xpath->query("//img[@src]") as $node) {
$src = $node->getAttribute('src');
if (!preg_match('/^(http|https):\/\//', $src)) {
$newSrc = rtrim($baseImageUrl, '/') . '/' . ltrim($src, '/');
$node->setAttribute('src', $newSrc);
}
}
}

function wrapTablesInDiv($xpath, $divClasses = array(), $dom) {
foreach ($xpath->query("//table") as $table) {
$wrapperDiv = $dom->createElement('div');
if (!empty($divClasses)) {
$wrapperDiv->setAttribute('class', implode(' ', $divClasses));
}
$table->parentNode->replaceChild($wrapperDiv, $table);
$wrapperDiv->appendChild($table);
}
}

function fetchWikiContent($page, $lang = 'en') {
if (isset($lang)) {
$page .= "/" . $lang;
}

$url = "https://wiki.freecad.org/api.php?action=parse&page=" . $page . "&format=json&disableeditsection=true&disabletoc=true";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);

return json_decode($response, true);
}

function processWikiContent($page, $lang = 'en') {
$data = fetchWikiContent($page, $lang);
$htmlContent = $data['parse']['text']['*'];

list($dom, $xpath) = createDomAndXpath($htmlContent);

addPrefixToImageSrc($xpath, "https://wiki.freecad.org");
removeDivsByClass($xpath, array('NavFrame', 'mw-pt-languages'));
removeDivsById($xpath, array('itsstillfree', 'itsfree'));
removeInlineStylesByClasses($xpath, array('docnav'));
removeFileLinks($xpath);
convertRelativeLinksToWikiFormat($xpath, $lang);
wrapTablesInDiv($xpath, array('table-responsive'),$dom);

return $dom->saveHTML();
}

echo processWikiContent($page, $lang);

?>

<p>
<?php echo _("This page is retrieved from"); ?>
<a href="<?php echo "https://wiki.freecad.org/".$page; ?>"><?php echo "https://wiki.freecad.org/".$page;?></a>
</p>
</div>
<?php
include 'footer.php';
?>

0 comments on commit e1bf8e4

Please sign in to comment.