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

add possibility to create files in subfolders #5

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
20 changes: 11 additions & 9 deletions editor.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head>
<meta charset="utf-8" />
<title>Pico Editor</title>
<link rel="stylesheet" href="{{ base_url }}/plugins/pico_editor/pico_editor.css" type="text/css" />
<link rel="stylesheet" href="{{ base_url }}/{{ plugin_path }}/pico_editor.css" type="text/css" />
<meta name="robots" content="noindex, nofollow" />
</head>
<body>
Expand All @@ -17,7 +17,8 @@
</div>
<ul class="nav">
{% for page in pages %}
<li><a href="#" data-url="{{ page.url }}" class="post"><span data-icon="3" aria-hidden="true"></span>{% if page.title %}{{ page.title }}{% else %}Untitled{% endif %}</a>
<!--<li><a href="#" data-url="{{ page.url }}" class="post"><span data-icon="3" aria-hidden="true"></span>{% if page.title %}{{ page.title }}{% else %}Untitled{% endif %}</a>-->
<li><a href="#" data-url="{{ page.url }}" class="post"><span data-icon="3" aria-hidden="true"></span>{{ page.url }}</a>
<a href="{{ page.url }}" target="_blank" class="view" title="View">5</a>
<a href="#" data-url="{{ page.url }}" class="delete" title="Delete">4</a></li>
{% endfor %}
Expand All @@ -28,24 +29,24 @@
<div id="epiceditor"></div>

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script src="{{ base_url }}/plugins/pico_editor/epiceditor/js/epiceditor.min.js"></script>
<script src="{{ base_url }}/{{ plugin_path }}/epiceditor/js/epiceditor.min.js"></script>
<script>
$(document).ready(function() {

var unsaved = false;
var editor = new EpicEditor({
container: 'epiceditor',
basePath: '{{ base_url }}/plugins/pico_editor/epiceditor',
basePath: '{{ base_url }}/{{ plugin_path }}/epiceditor',
clientSideStorage: false,
file: {
name: 'epiceditor',
defaultContent: '',
autoSave: 5000
},
theme: {
base: '{{ base_url }}/plugins/pico_editor/epiceditor/themes/base/epiceditor.css',
preview: '{{ base_url }}/plugins/pico_editor/epiceditor/themes/preview/github.css',
editor: '{{ base_url }}/plugins/pico_editor/epiceditor/themes/editor/epic-light.css'
base: '{{ base_url }}/{{ plugin_path }}/epiceditor/themes/base/epiceditor.css',
preview: '{{ base_url }}/{{ plugin_path }}/epiceditor/themes/preview/github.css',
editor: '{{ base_url }}/{{ plugin_path }}/epiceditor/themes/editor/epic-light.css'
},
button: {
preview: true,
Expand All @@ -64,9 +65,10 @@
// New
$('.controls .new').on('click', function(e){
e.preventDefault();
var dir = prompt('Please enter the dir(path without host and page/file name)', '');
var title = prompt('Please enter a post title', '');
if(title != null && title != ''){
$.post('admin/new', { title: title }, function(data){
$.post('admin/new', { title: title, dir: dir }, function(data){
if(data.error){
alert(data.error);
} else {
Expand All @@ -75,7 +77,7 @@
editor.importFile('epiceditor', data.content);
unsaved = false;
document.title = document.title.replace(' *', '');
$('.nav').prepend('<li><a href="#" data-url="{{ base_url }}/'+ data.file +'" class="post open"><span data-icon="3" aria-hidden="true"></span>'+ data.title +'</a><a href="{{ base_url }}/'+ data.file +'" target="_blank" class="view" title="View">5</a><a href="#" data-url="{{ base_url }}/'+ data.file +'" class="delete" title="Delete">4</a></li>')
$('.nav').prepend('<li><a href="#" data-url="{{ base_url }}/'+ data.file +'" class="post open"><span data-icon="3" aria-hidden="true"></span>{{ base_url }}/'+ data.file +'</a><a href="{{ base_url }}/'+ data.file +'" target="_blank" class="view" title="View">5</a><a href="#" data-url="{{ base_url }}/'+ data.file +'" class="delete" title="Delete">4</a></li>')
}
}, 'json');
}
Expand Down
73 changes: 52 additions & 21 deletions pico_editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ public function before_render(&$twig_vars, &$twig)
header('Location: '. $twig_vars['base_url'] .'/admin');
exit;
}


$pathArray = explode('/', dirname(__FILE__));
$twig_vars['plugin_path'] = $pathArray[count($pathArray)-2] . '/' . $pathArray[count($pathArray) - 1 ];

if($this->is_admin){
header($_SERVER['SERVER_PROTOCOL'].' 200 OK'); // Override 404 header
$loader = new Twig_Loader_Filesystem($this->plugin_path);
Expand All @@ -73,7 +76,7 @@ public function before_render(&$twig_vars, &$twig)
exit;
}
}

echo $twig_editor->render('editor.html', $twig_vars); // Render editor.html
exit; // Don't continue to render template
}
Expand All @@ -83,6 +86,17 @@ private function do_new()
{
if(!isset($_SESSION['pico_logged_in']) || !$_SESSION['pico_logged_in']) die(json_encode(array('error' => 'Error: Unathorized')));
$title = isset($_POST['title']) && $_POST['title'] ? strip_tags($_POST['title']) : '';
$dir = isset($_POST['dir']) && $_POST['dir'] ? strip_tags($_POST['dir']) : '';

$contentDir = CONTENT_DIR . $dir;
if($contentDir[strlen(count($contentDir)-1)] != '/') $contentDir .= '/';

if(!is_dir($contentDir)) {
if (!mkdir($contentDir, 0777, true)) {
die(json_encode(array('error' => 'Can\'t create directory...')));
}
}

$file = $this->slugify(basename($title));
if(!$file) die(json_encode(array('error' => 'Error: Invalid file name')));

Expand All @@ -93,16 +107,16 @@ private function do_new()
Author:
Date: '. date('Y/m/d') .'
*/';
if(file_exists(CONTENT_DIR . $file)){
if(file_exists($contentDir . $file)){
$error = 'Error: A post already exists with this title';
} else {
file_put_contents(CONTENT_DIR . $file, $content);
file_put_contents($contentDir . $file, $content);
}

die(json_encode(array(
'title' => $title,
'content' => $content,
'file' => basename(str_replace(CONTENT_EXT, '', $file)),
'file' => $dir.basename(str_replace(CONTENT_EXT, '', $file)),
'error' => $error
)));
}
Expand All @@ -111,37 +125,54 @@ 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');

$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));
if(!$file) die('Error: Invalid file');

$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);
die($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
4 changes: 2 additions & 2 deletions pico_editor_config.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
* This should be a sha1 hash of your password.
* Use a tool like http://www.sha1-online.com to generate.
*/
$pico_editor_password = '';
$pico_editor_password = "";

?>
?>