Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 29 additions & 11 deletions pico_editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,37 +111,55 @@ private function do_open()
{
if(!isset($_SESSION['pico_logged_in']) || !$_SESSION['pico_logged_in']) die(json_encode(array('error' => 'Error: Unathorized')));
$file_url = isset($_POST['file']) && $_POST['file'] ? $_POST['file'] : '';
$file = basename(strip_tags($file_url));
if(!$file) die('Error: Invalid file');

$file .= CONTENT_EXT;
if(file_exists(CONTENT_DIR . $file)) die(file_get_contents(CONTENT_DIR . $file));
$parse_file_url = parse_url($file_url);
$file = $parse_file_url['path']; // Get path from $file_url
if(!$file) die('Error: Invalid file');

$file = CONTENT_DIR . $file; // Get file system path
if(file_exists($file . CONTENT_EXT)) $file = $file . CONTENT_EXT; // Make sure samename/ doesn't override samename.md
else if (is_dir($file) && file_exists($file . '/index' . CONTENT_EXT)) $file = $file . '/index' . CONTENT_EXT;
else die('Error: Invalid file');

die(file_get_contents($file));
}

private function do_save()
{
if(!isset($_SESSION['pico_logged_in']) || !$_SESSION['pico_logged_in']) die(json_encode(array('error' => 'Error: Unathorized')));
$file_url = isset($_POST['file']) && $_POST['file'] ? $_POST['file'] : '';
$file = basename(strip_tags($file_url));

$parse_file_url = parse_url($file_url);
$file = $parse_file_url['path']; // Get path from $file_url
if(!$file) die('Error: Invalid file');

$content = isset($_POST['content']) && $_POST['content'] ? $_POST['content'] : '';
if(!$content) die('Error: Invalid content');

$file .= CONTENT_EXT;
file_put_contents(CONTENT_DIR . $file, $content);
$file = CONTENT_DIR . $file; // Get file system path
if(file_exists($file . CONTENT_EXT)) $file = $file . CONTENT_EXT; // Make sure samename/ doesn't override samename.md
else if (is_dir($file) && file_exists($file . '/index' . CONTENT_EXT)) $file = $file . '/index' . CONTENT_EXT;
else die('Error: Invalid file');

file_put_contents($file, $content);
die($content);
}

private function do_delete()
{
if(!isset($_SESSION['pico_logged_in']) || !$_SESSION['pico_logged_in']) die(json_encode(array('error' => 'Error: Unathorized')));
$file_url = isset($_POST['file']) && $_POST['file'] ? $_POST['file'] : '';
$file = basename(strip_tags($file_url));

$parse_file_url = parse_url($file_url);
$file = $parse_file_url['path']; // Get path from $file_url
if(!$file) die('Error: Invalid file');

$file .= CONTENT_EXT;
if(file_exists(CONTENT_DIR . $file)) die(unlink(CONTENT_DIR . $file));
$file = CONTENT_DIR . $file; // Get file system path
if(file_exists($file . CONTENT_EXT)) $file = $file . CONTENT_EXT; // Make sure samename/ doesn't override samename.md
else if (is_dir($file) && file_exists($file . '/index' . CONTENT_EXT)) $file = $file . '/index' . CONTENT_EXT;
else die('Error: Invalid file');

die(unlink($file));
}

private function slugify($text)
Expand Down Expand Up @@ -171,4 +189,4 @@ private function slugify($text)

}

?>
?>