Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for subsites and index page #11

Closed
wants to merge 1 commit into from
Closed
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
47 changes: 31 additions & 16 deletions pico_editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,24 +106,26 @@ private function do_new()
'error' => $error
)));
}

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));
else die('Error: Invalid file');
}

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 = $this->getFile($file_url);

if(!$file) die('Error: The file is invalid or not available.');

$file .= CONTENT_EXT;
if(file_exists(CONTENT_DIR . $file)) die(file_get_contents(CONTENT_DIR . $file));
else die('Error: Invalid 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));
$file = $this->getFile($file_url);

if(!$file) die('Error: Invalid file');
$content = isset($_POST['content']) && $_POST['content'] ? $_POST['content'] : '';
if(!$content) die('Error: Invalid content');
Expand All @@ -137,7 +139,8 @@ 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));
$file = $this->getFile($file_url);

if(!$file) die('Error: Invalid file');

$file .= CONTENT_EXT;
Expand Down Expand Up @@ -167,8 +170,20 @@ private function slugify($text)
}

return $text;
}

}

protected function getFile($file_url) {
// Check if index.md
if($file_url == "/") {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my case, $file_url is something like http://localhost:8080/sub/page.
Replacing this:

if($file_url == "/") {
    $file_url = 'index';
}

with this:

$file_url = parse_url($file_url, PHP_URL_PATH);
if (substr($file_url, -1) == '/') {
    $file_url .= 'index';
}

fixed the problem.

$file_url = 'index';
}

// Remove leading /
if (strpos($file_url, '/') === 0)
$file_url = substr($file_url, 1);

return strip_tags($file_url);
}
}

?>