Skip to content

Instantly share code, notes, and snippets.

@tranduyhung
Created February 11, 2016 10:47
Show Gist options
  • Save tranduyhung/09ad558cb0a53b5f6be2 to your computer and use it in GitHub Desktop.
Save tranduyhung/09ad558cb0a53b5f6be2 to your computer and use it in GitHub Desktop.
Fix "Error: Invalid file" error in Pico Admin plugin when running Pico in subfolder.
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'] : '';
$script_name = $_SERVER['SCRIPT_NAME'];
$sub_folder = str_replace('/index.php', '', $script_name);
if ($sub_folder != '')
{
$file = substr($file_url, strpos($file_url, $sub_folder) + strlen($sub_folder));
}
else
{
$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_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'] : '';
$script_name = $_SERVER['SCRIPT_NAME'];
$sub_folder = str_replace('/index.php', '', $script_name);
if ($sub_folder != '')
{
$file = substr($file_url, strpos($file_url, $sub_folder) + strlen($sub_folder));
}
else
{
$file = basename(strip_tags($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);
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'] : '';
$script_name = $_SERVER['SCRIPT_NAME'];
$sub_folder = str_replace('/index.php', '', $script_name);
if ($sub_folder != '')
{
$file = substr($file_url, strpos($file_url, $sub_folder) + strlen($sub_folder));
}
else
{
$file = basename(strip_tags($file_url));
}
if(!$file) die('Error: Invalid file');
$file .= CONTENT_EXT;
if(file_exists(CONTENT_DIR . $file)) die(unlink(CONTENT_DIR . $file));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment