Begin updating module to use the new path system. Currently untested.

This commit is contained in:
Starbeamrainbowlabs 2015-11-08 21:15:08 +00:00
parent d5f1050d79
commit c7348d9c74
8 changed files with 60 additions and 43 deletions

View File

@ -1,42 +1,42 @@
<?php
register_module([
"name" => "Page protection",
"version" => "0.1",
"version" => "0.2",
"author" => "Starbeamrainbowlabs",
"description" => "Exposes Pepperminty Wiki's new page protection mechanism and makes the protect button in the 'More...' menu on the top bar work.",
"id" => "action-protect",
"code" => function() {
add_action("protect", function() {
global $env, $pageindex;
// Make sure that the user is logged in as an admin / mod.
if($env->is_admin)
{
// They check out ok, toggle the page's protection.
$page = $env->page;
$toggled = false;
if(!isset($pageindex->$page->protect))
{
$pageindex->$page->protect = true;
$toggled = true;
}
if(!$toggled && $pageindex->$page->protect === true)
{
$pageindex->$page->protected = false;
$toggled = false;
}
if(!$toggled && $pageindex->$page->protect === false)
{
$pageindex->$page->protected = true;
$toggled = true;
}
// Save the pageindex
file_put_contents("./pageindex.json", json_encode($pageindex, JSON_PRETTY_PRINT));
file_put_contents($paths->pageindex, json_encode($pageindex, JSON_PRETTY_PRINT));
$state = ($pageindex->$page->protect ? "enabled" : "disabled");
$title = "Page protection $state.";
exit(page_renderer::render_main($title, "<p>Page protection for $env->page has been $state.</p><p><a href='?action=$env->defaultaction&page=$env->page'>Go back</a>."));

View File

@ -1,18 +1,18 @@
<?php
register_module([
"name" => "Raw page source",
"version" => "0.3",
"version" => "0.4",
"author" => "Starbeamrainbowlabs",
"description" => "Adds a 'raw' action that shows you the raw source of a page.",
"id" => "action-raw",
"code" => function() {
add_action("raw", function() {
global $env;
http_response_code(307);
header("x-filename: " . rawurlencode($env->page) . ".md");
header("content-type: text/markdown");
exit(file_get_contents("$env->page.md"));
exit(file_get_contents("$env->storage_prefix$env->page.md"));
exit();
});
}

View File

@ -1,7 +1,7 @@
<?php
register_module([
"name" => "Search",
"version" => "0.1",
"version" => "0.2",
"author" => "Starbeamrainbowlabs",
"description" => "Adds proper search functionality to Pepperminty Wiki. Note that this module, at the moment, just contains test code while I figure out how best to write a search engine.",
"id" => "feature-search",
@ -13,7 +13,7 @@ register_module([
header("content-type: text/plain");
$source = file_get_contents("$env->page.md");
$source = file_get_contents("$env->storage_prefix$env->page.md");
$index = search::index($source);
@ -35,11 +35,11 @@ register_module([
$search_start = microtime(true);
$invindex = search::load_invindex("invindex.json");
$invindex = search::load_invindex($paths->searchindex);
$results = search::query_invindex($_GET["query"], $invindex);
$search_end = microtime(true) - $search_start;
$title = $_GET["query"] . " - Search results - $settings->sitename";
$content = "<section>\n";
@ -65,7 +65,7 @@ register_module([
foreach($results as $result)
{
$link = "?page=" . rawurlencode($result["pagename"]);
$pagesource = file_get_contents($result["pagename"] . ".md");
$pagesource = file_get_contents($env->storage_prefix . $result["pagename"] . ".md");
$context = search::extract_context($_GET["query"], $pagesource);
$context = search::highlight_context($_GET["query"], $context);
/*if(strlen($context) == 0)
@ -102,7 +102,7 @@ class search
public static $stop_words = [
"a", "about", "above", "above", "across", "after", "afterwards", "again",
"against", "all", "almost", "alone", "along", "already", "also",
"although", "always", "am", "among", "amongst", "amoungst", "amount",
"although", "always", "am", "among", "amongst", "amoungst", "amount",
"an", "and", "another", "any", "anyhow", "anyone", "anything", "anyway",
"anywhere", "are", "around", "as", "at", "back", "be", "became",
"because", "become", "becomes", "becoming", "been", "before",
@ -192,7 +192,7 @@ class search
$invindex = [];
foreach($pageindex as $pagename => $pagedetails)
{
$pagesource = file_get_contents("$pagename.md");
$pagesource = file_get_contents("$env->storage_prefix$pagename.md");
$index = self::index($pagesource);
self::merge_into_invindex($invindex, ids::getid($pagename), $index);

View File

@ -1,7 +1,7 @@
<?php
register_module([
"name" => "Uploader",
"version" => "0.1",
"version" => "0.2",
"author" => "Starbeamrainbowlabs",
"description" => "Adds the ability to upload files to Pepperminty Wiki. Uploaded files act as pages and have the special 'File:' prefix.",
"id" => "feature-upload",
@ -92,7 +92,7 @@ register_module([
$file_extension = system_mime_type_extension($mime_type);
$new_filename = "Files/$target_name.$file_extension";
$new_filename = "$paths->upload_file_prefix$target_name.$file_extension";
$new_description_filename = "$new_filename.md";
if(isset($pageindex->$new_filename))
@ -101,20 +101,19 @@ register_module([
if(!file_exists("Files"))
mkdir("Files", 0664);
if(!move_uploaded_file($temp_filename, $new_filename))
if(!move_uploaded_file($temp_filename, $env->storage_prefix . $new_filename))
{
http_response_code(409);
exit(page_renderer::render("Upload Error - $settings->sitename", "<p>The file you uploaded was valid, but $settings->sitename couldn't verify that it was tampered with during the upload process. This probably means that $settings->sitename has been attacked. Please contact " . $settings->admindetails . ", your $settings->sitename Administrator.</p>"));
}
file_put_contents($new_description_filename, $_POST["description"]);
$description = $_POST["description"];
// Escape the raw html in the provided description if the setting is enabled
if($settings->clean_raw_html)
$description = htmlentities($description, ENT_QUOTES);
file_put_contents($new_description_filename, $description);
file_put_contents($env->storage_prefix . $new_description_filename, $description);
// Construct a new entry for the pageindex
$entry = new stdClass();
@ -133,7 +132,7 @@ register_module([
$pageindex->$new_filename = $entry;
// Save the pageindex
file_put_contents("pageindex.json", json_encode($pageindex, JSON_PRETTY_PRINT));
file_put_contents($paths->pageindex, json_encode($pageindex, JSON_PRETTY_PRINT));
header("location: ?action=view&page=$new_filename&upload=success");
@ -143,9 +142,25 @@ register_module([
add_action("preview", function() {
global $settings, $env, $pageindex;
$filepath = $pageindex->{$env->page}->uploadedfilepath;
$filepath = $env->storage_prefix . $pageindex->{$env->page}->uploadedfilepath;
$mime_type = $pageindex->{$env->page}->uploadedfilemime;
if(isset($_GET["size"]) and $_GET["size"] == "original")
{
// Get the file size
$filesize = filesize($filepath);
// Send some headers
header("content-length: $filesize");
header("content-type: $mime_type");
// Open the file and send it to the user
$handle = fopen($filepath, "rb");
fpassthru($handle);
fclose($handle);
exit();
}
// Determine the target size of the image
$target_size = 512;
if(isset($_GET["size"]))
@ -220,6 +235,8 @@ register_module([
$filepath = $pageindex->{$env->page}->uploadedfilepath;
$mime_type = $pageindex->{$env->page}->uploadedfilemime;
$image_link = "//" . $_SERVER["SERVER_NAME"] . dirname($_SERVER["SCRIPT_NAME"]) . $filepath;
if($env->storage_prefix !== "./")
$image_link = "?action=preview&size=original&page=" . rawurlencode($env->page);
$preview_sizes = [ 256, 512, 768, 1024, 1536 ];
$preview_html = "<figure class='preview'>

View File

@ -1,7 +1,7 @@
<?php
register_module([
"name" => "Page deleter",
"version" => "0.7",
"version" => "0.8",
"author" => "Starbeamrainbowlabs",
"description" => "Adds an action to allow administrators to delete pages.",
"id" => "page-delete",
@ -30,11 +30,11 @@ register_module([
// Delete the associated file if it exists
if(!empty($pageindex->$page->uploadedfile))
{
unlink($pageindex->$page->uploadedfilepath);
unlink($env->storage_prefix . $pageindex->$page->uploadedfilepath);
}
unset($pageindex->$page); //delete the page from the page index
file_put_contents("./pageindex.json", json_encode($pageindex, JSON_PRETTY_PRINT)); //save the new page index
unlink("./$env->page.md"); //delete the page from the disk
file_put_contents($paths->pageindex, json_encode($pageindex, JSON_PRETTY_PRINT)); //save the new page index
unlink("$env->storage_prefix$env->page.md"); //delete the page from the disk
exit(page_renderer::render_main("Deleting $env->page - $settings->sitename", "<p>$env->page has been deleted. <a href='index.php'>Go back to the main page</a>.</p>"));
});

View File

@ -1,7 +1,7 @@
<?php
register_module([
"name" => "Page editor",
"version" => "0.11",
"version" => "0.12",
"author" => "Starbeamrainbowlabs",
"description" => "Allows you to edit pages by adding the edit and save actions. You should probably include this one.",
"id" => "page-edit",
@ -19,7 +19,7 @@ register_module([
add_action("edit", function() {
global $pageindex, $settings, $env;
$filename = "$env->page.md";
$filename = "$env->storage_prefix$env->page.md";
$page = $env->page;
$creatingpage = !isset($pageindex->$page);
if((isset($_GET["newpage"]) and $_GET["newpage"] == "true") or $creatingpage)
@ -104,7 +104,7 @@ register_module([
{
http_response_code(403);
header("refresh: 5; url=index.php?page=$env->page");
exit("$env->page is protected, and you aren't logged in as an administrastor or moderator. Your edit was not saved. Redirecting in 5 seconds...");
exit("$env->page is protected, and you aren't logged in as an administrator or moderator. Your edit was not saved. Redirecting in 5 seconds...");
}
if(!isset($_POST["content"]))
{
@ -114,10 +114,10 @@ register_module([
}
// Make sure that the directory in which the page needs to be saved exists
if(!is_dir(dirname("$env->page.md")))
if(!is_dir(dirname("$env->storage_prefix$env->page.md")))
{
// Recursively create the directory if needed
mkdir(dirname("$env->page.md"), null, true);
mkdir(dirname("$env->storage_prefix$env->page.md"), null, true);
}
// Read in the new page content
@ -155,7 +155,7 @@ register_module([
if(file_put_contents("$env->page.md", $pagedata) !== false)
if(file_put_contents("$env->storage_prefix$env->page.md", $pagedata) !== false)
{
$page = $env->page;
// Make sure that this page's parents exist
@ -187,10 +187,10 @@ register_module([
}
if($pagedata !== $pagedata_orig)
file_put_contents("$env->page.md", $pagedata);
file_put_contents("$env->storage_prefix$env->page.md", $pagedata);
file_put_contents("./pageindex.json", json_encode($pageindex, JSON_PRETTY_PRINT));
file_put_contents($paths->pageindex, json_encode($pageindex, JSON_PRETTY_PRINT));
if(isset($_GET["newpage"]))
http_response_code(201);

View File

@ -1,7 +1,7 @@
<?php
register_module([
"name" => "Export",
"version" => "0.2",
"version" => "0.3",
"author" => "Starbeamrainbowlabs",
"description" => "Adds a page that you can use to export your wiki as a .zip file. Uses \$settings->export_only_allow_admins, which controls whether only admins are allowed to export the wiki.",
"id" => "page-export",
@ -27,7 +27,7 @@ register_module([
foreach($pageindex as $entry)
{
$zip->addFile("./$entry->filename", $entry->filename);
$zip->addFile("$env->storage_prefix$entry->filename", $entry->filename);
}
if($zip->close() !== true)

View File

@ -62,12 +62,12 @@ register_module([
// Move the file in the pageindex
$pageindex->$new_name->uploadedfilepath = $new_name;
// Move the file on disk
rename($env->page, $new_name);
rename($env->storage_prefix . $env->page, $env->storage_prefix . $new_name);
}
file_put_contents("./pageindex.json", json_encode($pageindex, JSON_PRETTY_PRINT));
//move the page on the disk
rename("$env->page.md", "$new_name.md");
rename("$env->storage_prefix$env->page.md", "$env->storage_prefix$new_name.md");
exit(page_renderer::render_main("Moving $env->page", "<p><a href='index.php?page=$env->page'>$env->page</a> has been moved to <a href='index.php?page=$new_name'>$new_name</a> successfully.</p>"));
});