Skip to content
This repository has been archived by the owner on Sep 23, 2023. It is now read-only.

Commit

Permalink
Allow wikis to be redirected when deleting
Browse files Browse the repository at this point in the history
Fixes #54
  • Loading branch information
edg2s committed May 5, 2021
1 parent dd2f7aa commit f39dcc7
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 32 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ repositories
composer
composer.lock
vendor
redirects.txt
node_modules
37 changes: 33 additions & 4 deletions 404.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,40 @@
header( 'HTTP/1.0 404 Not Found' );
require_once "includes.php";

// Check for redirect
$redirects = get_if_file_exists( 'redirects.txt' );
$redirect = false;
if ( $redirects ) {
$uri = $_SERVER['REQUEST_URI'];
$lines = explode( "\n", $redirects );
foreach ( $lines as $line ) {
if ( !$line ) {
continue;
}
$parts = explode( ' ', $line );
if ( strpos( $uri, $parts[0] ) !== false ) {
$uri = str_replace( $parts[0], $parts[1], $uri );
$redirect = true;
}
}
}

include "header.php";

echo new \OOUI\MessageWidget( [
'type' => 'error',
'label' => 'Page not found. The wiki you are looking for may have been deleted.'
] );
if ( $redirect ) {
echo new \OOUI\MessageWidget( [
'type' => 'info',
'icon' => 'articleRedirect',
'label' => new \OOUI\HtmlSnippet(
'This wiki has been deleted. The following wiki was selected as a direct replacement: ' .
'<a href="' . htmlspecialchars( $uri ) . '">' . $uri . '</a>'
)
] );
} else {
echo new \OOUI\MessageWidget( [
'type' => 'error',
'label' => 'Page not found. The wiki you are looking for may have been deleted.'
] );
}

include "footer.html";
115 changes: 88 additions & 27 deletions delete.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,35 +33,96 @@
'</tr>' .
'</table>';

echo '<form method="POST">' .
'<p>Are you sure you want to delete this wiki?</p>' .
'<p>This cannot be undone.</p>' .
new OOUI\ButtonInputWidget( [
'type' => 'submit',
'name' => 'confirm',
'label' => 'Delete',
'flags' => [ 'primary', 'destructive' ]
] ) .
new OOUI\HiddenInputWidget( [
'name' => 'csrf_token',
'value' => get_csrf_token(),
] ) .
'</form>';
die();
}
$wikilist = [
[
'data' => '',
'label' => 'None',
]
];
$results = $mysqli->query( 'SELECT wiki, creator, UNIX_TIMESTAMP( created ) created FROM wikis WHERE !deleted ORDER BY created DESC' );
if ( !$results ) {
die( $mysqli->error );
}
while ( $data = $results->fetch_assoc() ) {
if ( $data[ 'wiki' ] === $wiki ) {
continue;
}
$wikilist[] = [
'data' => $data[ 'wiki' ],
'label' => substr( $data[ 'wiki' ], 0, 10 ) . ' - ' . $data[ 'creator' ] . ' (' . date( 'Y-m-d H:i:s', $data[ 'created' ] ) . ')',
];
}
echo new OOUI\FormLayout( [
'method' => 'POST',
'items' => [
new OOUI\FieldsetLayout( [
'label' => new OOUI\HtmlSnippet(
'<br>Are you sure you want to delete this wiki? This cannot be undone.'
),
'items' => array_filter( [
count( $wikilist ) > 1 ?
new OOUI\FieldLayout(
new OOUI\DropdownInputWidget( [
'name' => 'redirect',
'options' => $wikilist,
] ),
[
'label' => 'Leave a redirect another wiki (optional):',
'align' => 'left',
]
) :
null,
new OOUI\FieldLayout(
new OOUI\ButtonInputWidget( [
'type' => 'submit',
'name' => 'confirm',
'label' => 'Delete',
'flags' => [ 'primary', 'destructive' ]
] ),
[
'label' => ' ',
'align' => 'left',
]
),
] )
] )
]
] );

if ( !isset( $_POST['csrf_token'] ) || !check_csrf_token( $_POST['csrf_token'] ) ) {
die( "Invalid session." );
}
} else {
if ( !isset( $_POST['csrf_token'] ) || !check_csrf_token( $_POST['csrf_token'] ) ) {
die( "Invalid session." );
}

ob_implicit_flush( true );
ob_implicit_flush( true );

echo '<div class="consoleLog">';
$error = delete_wiki( $wiki );
echo '</div>';
echo '<div class="consoleLog">';
$error = delete_wiki( $wiki );
echo '</div>';

if ( $error ) {
die( '<p>Error deleting wiki:<br>' . htmlentities( $error ) . '</p>' );
} else {
echo '<p>Wiki deleted.</p>';
if ( $error ) {
die( '<p>Error deleting wiki:<br>' . htmlentities( $error ) . '</p>' );
} else {
echo '<p>Wiki deleted.</p>';
}

function isValidHash( $hash ) {
return preg_match( '/^[0-9a-f]{10,32}$/', $hash );
}

$redirect = $_POST['redirect'] ?? null;

if (
$redirect &&
isValidHash( $redirect ) &&
isValidHash( $wiki )
) {
// TODO: Avoid duplication in redirect file
file_put_contents(
'redirects.txt',
$wiki . ' ' . $redirect . "\n",
FILE_APPEND | LOCK_EX
);
echo ' Redirected to <a href="wikis/' . $redirect . '/w">' . $redirect . '</a>.';
}
}
1 change: 0 additions & 1 deletion index.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,6 @@
'classes' => [ 'form-submit' ],
'label' => 'Create demo',
'type' => 'submit',
// 'disabled' => true,
'flags' => [ 'progressive', 'primary' ]
] ),
[
Expand Down

0 comments on commit f39dcc7

Please sign in to comment.