|
33 | 33 | '</tr>' .
|
34 | 34 | '</table>';
|
35 | 35 |
|
36 |
| - echo '<form method="POST">' . |
37 |
| - '<p>Are you sure you want to delete this wiki?</p>' . |
38 |
| - '<p>This cannot be undone.</p>' . |
39 |
| - new OOUI\ButtonInputWidget( [ |
40 |
| - 'type' => 'submit', |
41 |
| - 'name' => 'confirm', |
42 |
| - 'label' => 'Delete', |
43 |
| - 'flags' => [ 'primary', 'destructive' ] |
44 |
| - ] ) . |
45 |
| - new OOUI\HiddenInputWidget( [ |
46 |
| - 'name' => 'csrf_token', |
47 |
| - 'value' => get_csrf_token(), |
48 |
| - ] ) . |
49 |
| - '</form>'; |
50 |
| - die(); |
51 |
| -} |
| 36 | + $wikilist = [ |
| 37 | + [ |
| 38 | + 'data' => '', |
| 39 | + 'label' => 'None', |
| 40 | + ] |
| 41 | + ]; |
| 42 | + $results = $mysqli->query( 'SELECT wiki, creator, UNIX_TIMESTAMP( created ) created FROM wikis WHERE !deleted ORDER BY created DESC' ); |
| 43 | + if ( !$results ) { |
| 44 | + die( $mysqli->error ); |
| 45 | + } |
| 46 | + while ( $data = $results->fetch_assoc() ) { |
| 47 | + if ( $data[ 'wiki' ] === $wiki ) { |
| 48 | + continue; |
| 49 | + } |
| 50 | + $wikilist[] = [ |
| 51 | + 'data' => $data[ 'wiki' ], |
| 52 | + 'label' => substr( $data[ 'wiki' ], 0, 10 ) . ' - ' . $data[ 'creator' ] . ' (' . date( 'Y-m-d H:i:s', $data[ 'created' ] ) . ')', |
| 53 | + ]; |
| 54 | + } |
| 55 | + echo new OOUI\FormLayout( [ |
| 56 | + 'method' => 'POST', |
| 57 | + 'items' => [ |
| 58 | + new OOUI\FieldsetLayout( [ |
| 59 | + 'label' => new OOUI\HtmlSnippet( |
| 60 | + '<br>Are you sure you want to delete this wiki? This cannot be undone.' |
| 61 | + ), |
| 62 | + 'items' => array_filter( [ |
| 63 | + count( $wikilist ) > 1 ? |
| 64 | + new OOUI\FieldLayout( |
| 65 | + new OOUI\DropdownInputWidget( [ |
| 66 | + 'name' => 'redirect', |
| 67 | + 'options' => $wikilist, |
| 68 | + ] ), |
| 69 | + [ |
| 70 | + 'label' => 'Leave a redirect another wiki (optional):', |
| 71 | + 'align' => 'left', |
| 72 | + ] |
| 73 | + ) : |
| 74 | + null, |
| 75 | + new OOUI\FieldLayout( |
| 76 | + new OOUI\ButtonInputWidget( [ |
| 77 | + 'type' => 'submit', |
| 78 | + 'name' => 'confirm', |
| 79 | + 'label' => 'Delete', |
| 80 | + 'flags' => [ 'primary', 'destructive' ] |
| 81 | + ] ), |
| 82 | + [ |
| 83 | + 'label' => ' ', |
| 84 | + 'align' => 'left', |
| 85 | + ] |
| 86 | + ), |
| 87 | + ] ) |
| 88 | + ] ) |
| 89 | + ] |
| 90 | + ] ); |
52 | 91 |
|
53 |
| -if ( !isset( $_POST['csrf_token'] ) || !check_csrf_token( $_POST['csrf_token'] ) ) { |
54 |
| - die( "Invalid session." ); |
55 |
| -} |
| 92 | +} else { |
| 93 | + if ( !isset( $_POST['csrf_token'] ) || !check_csrf_token( $_POST['csrf_token'] ) ) { |
| 94 | + die( "Invalid session." ); |
| 95 | + } |
56 | 96 |
|
57 |
| -ob_implicit_flush( true ); |
| 97 | + ob_implicit_flush( true ); |
58 | 98 |
|
59 |
| -echo '<div class="consoleLog">'; |
60 |
| -$error = delete_wiki( $wiki ); |
61 |
| -echo '</div>'; |
| 99 | + echo '<div class="consoleLog">'; |
| 100 | + $error = delete_wiki( $wiki ); |
| 101 | + echo '</div>'; |
62 | 102 |
|
63 |
| -if ( $error ) { |
64 |
| - die( '<p>Error deleting wiki:<br>' . htmlentities( $error ) . '</p>' ); |
65 |
| -} else { |
66 |
| - echo '<p>Wiki deleted.</p>'; |
| 103 | + if ( $error ) { |
| 104 | + die( '<p>Error deleting wiki:<br>' . htmlentities( $error ) . '</p>' ); |
| 105 | + } else { |
| 106 | + echo '<p>Wiki deleted.</p>'; |
| 107 | + } |
| 108 | + |
| 109 | + function isValidHash( $hash ) { |
| 110 | + return preg_match( '/^[0-9a-f]{10,32}$/', $hash ); |
| 111 | + } |
| 112 | + |
| 113 | + $redirect = $_POST['redirect'] ?? null; |
| 114 | + |
| 115 | + if ( |
| 116 | + $redirect && |
| 117 | + isValidHash( $redirect ) && |
| 118 | + isValidHash( $wiki ) |
| 119 | + ) { |
| 120 | + // TODO: Avoid duplication in redirect file |
| 121 | + file_put_contents( |
| 122 | + 'redirects.txt', |
| 123 | + $wiki . ' ' . $redirect . "\n", |
| 124 | + FILE_APPEND | LOCK_EX |
| 125 | + ); |
| 126 | + echo ' Redirected to <a href="wikis/' . $redirect . '/w">' . $redirect . '</a>.'; |
| 127 | + } |
67 | 128 | }
|
0 commit comments