forked from BugHunter2k/grav-plugin-fred
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fred.php
265 lines (238 loc) · 8.57 KB
/
fred.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
<?php
namespace Grav\Plugin;
use Grav\Common\Grav;
use Grav\Common\Plugin;
use Grav\Common\Page;
use Grav\Common\Twig\Twig;
use Grav\Common\User\User;
/**
* Grav Plugin Fred
* provides a frontend editor on pages
*
* this is an early alpha!!
*
* @author Ingo Hollmann
* @link https://github.com/BugHunter2k/fred
* @license http://opensource.org/licenses/MIT
*
*/
class FredPlugin extends Plugin
{
protected $enabled=false;
/**
* @return array
*/
public static function getSubscribedEvents()
{
// initialize when plugins are ready
return [
'onPluginsInitialized' => ['onPluginsInitialized', 0]
];
}
/**
* Initialize configuration
*/
public function onPluginsInitialized()
{
// Don't load in Admin-Backend
if ($this->isAdmin()) {
$this->active = false;
return;
}
$this->initializeFred();
}
/**
* Initialize Fred
* Check for user-auth and access befor
* loading js an css etc.
*/
public function initializeFred() {
// Check for required plugins
if (!$this->grav['config']->get('plugins.login.enabled') ||
!$this->grav['config']->get('plugins.form.enabled') ||
!$this->grav['config']->get('plugins.admin.enabled') ) {
throw new \RuntimeException('One of the required plugins is missing or not enabled');
}
// Check on logged in user and authorization for page editing
$user = $this->grav['user'];
if ($user->authenticated && $user->authorize("site.editor")) {
$this->enable([
'onTwigSiteVariables' => ['onTwigSiteVariables', 0],
'onTwigTemplatePaths' => ['onTwigTemplatePaths', 0],
'onPageProcessed' => ['onPageProcessed', 0],
]);
// Save plugin stauts
$this->enabled = true;
} else {
$this->enabled = false;
}
if (
$this->grav['uri']->basename() == "fredsave.json"
&& !empty($_POST)
&& $this->enabled
) {
$this->savePage();
}
if (
$this->grav['uri']->basename() == "upload-image.json"
&& !empty($_POST)
&& $this->enabled
) {
$this->addFile();
}
}
/**
* if enabled on this page, load the JS + CSS and set the selectors.
*/
public function onTwigSiteVariables()
{
// check if enabled
if ($this->enabled) {
// List of js and css files needed for contenttools
$fredbits = array(
'plugin://fred/css/content-tools.min.css',
'plugin://fred/js/content-tools.min.js',
'plugin://fred/js/to-markdown.js',
'plugin://fred/js/fred.js',
);
// register and add assets
$assets = $this->grav['assets'];
$assets->registerCollection('fred', $fredbits);
$assets->add('fred', 100);
}
}
/**
* Insert editor div around the active article
* Check for the right item and modify the content
*
* @param Event
*/
public function onPageProcessed($e)
{
$page = $e->offsetGet('page');
if ($page->isPage() && $page->route() == $this->grav['uri']->path() ) {
$page->content('<div data-editable="true" data-name="blog_item">'.$page->content().'</div>');
$this->grav['debugger']->addMessage("[fred] Adding Editor-<div>");
}
return;
}
/**
* register plugin template
*/
public function onTwigTemplatePaths()
{
$this->grav['twig']->twig_paths[] = __DIR__ . '/templates';
}
/**
* Get Page informations
* update content
* save page
*
* @return void - calls ajaxoutput
*/
public function savePage() {
$user = $this->grav['user'];
// Check Permissions for Save
if ($user->authenticated && $user->authorize("site.editor")) {
$page = $this->getPage("uri");
// get changes
// TODO preapre multipage content
$blogItem = filter_input(INPUT_POST, "blog_item", FILTER_SANITIZE_SPECIAL_CHARS);
// Get correct linebreaks
$blogItem = html_entity_decode($blogItem, ENT_QUOTES, 'UTF-8');
// set content and save page
if (!empty($blogItem)) {
$page->rawMarkdown((string) $blogItem);
$page->save();
}
// Set results json
$this->json_response = ['status' => 'success', 'message' => 'Your changes has been saved'];
} else {
// Non valid users should not change the article
$this->json_response = ['status' => 'unauthorized', 'message' => 'You have insufficient permissions for editing. Make sure you logged in.'];
}
echo json_encode($this->json_response);
die;
}
/**
* Handles files upload
* Triggerd vie upload-image.json
*
* @param void
* @return void - creates json - output for ajax
*/
public function addFile() {
$user = $this->grav['user'];
// Check Permissions for Save
if ($user->authenticated && $user->authorize("site.editor")) {
$page = $this->getPage("uri");
/** @var Config $config */
$config = $this->grav['config'];
if (!isset($_FILES['file']['error']) || is_array($_FILES['file']['error'])) {
$this->json_response = ['status' => 'error', 'message' => "Invalid Parameters"];
return false;
}
// Check $_FILES['file']['error'] value.
switch ($_FILES['file']['error']) {
case UPLOAD_ERR_OK:
break;
case UPLOAD_ERR_NO_FILE:
$this->json_response = ['status' => 'error', 'message' => "No file error"];
return false;
case UPLOAD_ERR_INI_SIZE:
case UPLOAD_ERR_FORM_SIZE:
$this->json_response = ['status' => 'error', 'message' => "Filesize error"];
return false;
default:
$this->json_response = ['status' => 'error', 'message' => "Unkown error"];
return false;
}
$grav_limit = $config->get('system.media.upload_limit', 0);
// You should also check filesize here.
if ($grav_limit > 0 && $_FILES['file']['size'] > $grav_limit) {
$this->json_response = ['status' => 'error', 'message' => "File to big"];
return false;
}
// Check extension
$fileParts = pathinfo($_FILES['file']['name']);
$fileExt = '';
if (isset($fileParts['extension'])) {
$fileExt = strtolower($fileParts['extension']);
}
// If not a supported type, return
if (!$fileExt || !$config->get("media.{$fileExt}")) {
$this->json_response = ['status' => 'error', 'message' => 'Invalid filetype: '.$fileExt];
return false;
}
// Upload it
$imagefile = sprintf('%s/%s', $page->path(), $_FILES['file']['name']);
if (!move_uploaded_file($_FILES['file']['tmp_name'], $imagefile)) {
$this->json_response = ['status' => 'error', 'message' => 'Failed to move file'];
return false;
}
// Get image Size
$size = getimagesize($imagefile);
$this->json_response = ['status' => 'success', 'url' => sprintf('%s/%s', $page->route(), $_FILES['file']['name']), 'size'=> [$size[0], $size[1]] , 'message' => 'Upload successfull'];
echo json_encode($this->json_response);
die;
}
}
/**
* Get page from POST parameter
*
* @param string POST field to get URI from
* @return Page object
*/
protected function getPage($param) {
// Get pages object and initialize
$pages = $this->grav['pages'];
$pages->init();
// Filter uri from parameters
$uri_string = filter_input(INPUT_POST, $param, FILTER_SANITIZE_SPECIAL_CHARS);
// Parse uri to get the plain path
$uri = parse_url($uri_string);
// get the page connected with $uri.path
$page = $pages->dispatch($uri['path']);
return $page;
}
}