This repository has been archived by the owner on Sep 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathPicoContentEditor.php
258 lines (241 loc) · 8.04 KB
/
PicoContentEditor.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
<?php
require __DIR__ . '/vendor/autoload.php';
use PicoContentEditor\Status;
use PicoContentEditor\Auth;
use PicoContentEditor\Edits;
use PicoContentEditor\Uploads;
use PicoContentEditor\EditorsHandlers\AbstractEditorHandler;
/**
* A content editor plugin for Pico 2, using ContentTools.
*
* Supports PicoUsers plugin for authentification
* {@link https://github.com/nliautaud/pico-users}
*
* @author Nicolas Liautaud
* @license http://opensource.org/licenses/MIT The MIT License
* @link https://github.com/nliautaud/pico-content-editor
* @link http://picocms.org
*/
class PicoContentEditor extends AbstractPicoPlugin
{
/**
* Pico API version.
* @var int
*/
const API_VERSION = 2;
/**
* EditorHandler (ContentToolsHandler, QuillHandler...)
* @var AbstractEditorHandler
*/
private $editor;
/**
* Files edits manager.
* @var Edits
*/
private $edits;
/**
* Uploads manager.
* @var Uploads
*/
private $uploads;
/**
* Init plugin, process page edits and file uploads.
*
* The end-editable mark @see{Edits::ENDMARK} need to be
* striped away because it's somewhat breaking the page rendering,
* and thus @see{Edits::saveRegions()} has to be done here
* in addition to @see{self::onPageRendered()}.
*
* Triggered after Pico has read the contents of the file to serve
*
* @see Pico::getRawContent()
* @param string &$rawContent raw file contents
* @return void
*/
public function onContentLoaded(&$rawContent)
{
if ($this->getPluginSetting('debug')) {
ini_set('display_startup_errors', 1);
ini_set('display_errors', 1);
error_reporting(-1);
}
Auth::init($this);
$editorName = $this->getPluginSetting('editor');
$this->editor = AbstractEditorHandler::getEditor($editorName);
$this->uploads = new Uploads($this);
$this->edits = new Edits();
$this->processPageEdits($rawContent);
// remove the end-editable mark
$rawContent = preg_replace('`'.Edits::ENDMARK.'`', '', $rawContent);
}
/**
* Register `{{ content_editor }}` who outputs the editor scripts
* and `{{ content_editor_meta }}` who outputs the metadata editor.
*
* Triggered before Pico renders the page
*
* @see DummyPlugin::onPageRendered()
* @param string &$templateName file name of the template
* @param array &$twigVariables template variables
* @return void
*/
public function onPageRendering(&$templateName, array &$twigVariables)
{
if (!$this->getPluginSetting('show', true) || !Auth::can(Auth::EDIT)) {
return;
}
$pluginDirUrl = $this->getBaseUrl() . basename($this->getPluginsDir());
$assetsUrl = "$pluginDirUrl/PicoContentEditor/src/assets/";
$twigVariables['content_editor'] = <<<EOF
<link href="$assetsUrl/noty/lib/noty.css" rel="stylesheet">
<link href="$assetsUrl/noty/themes/mint.css" rel="stylesheet">
<script src="$assetsUrl/noty/lib/noty.min.js" type="text/javascript"></script>
EOF;
$twigVariables['content_editor'] .= $this->editor::assets($this, $assetsUrl);
$twigVariables['content_editor_meta'] = <<<EOF
<div class="ContentEditor">
<pre class="ContentEditor_Meta" data-fixture data-meta>{$this->getRawMeta()}</pre>
</div>
EOF;
}
/**
* If the call is a save query, save the edited regions and output the JSON response.
*
* Triggered after Pico has rendered the page
*
* @param string &$output contents which will be sent to the user
* @return void
*/
public function onPageRendered(&$output)
{
$this->processOverallEdits($output);
if (!$this->edits->beenReceived() && !$this->uploads->beenReceived()) {
return;
}
// output response
$response = new stdClass();
$response->status = Status::getStatus();
$response->edited = $this->edits->output();
$response->file = $this->uploads->output();
$response->debug = $this->getPluginSetting('debug', false);
$output = json_encode($response);
}
/**
* Save page metadata and edited regions if authorized.
*
* @param string $rawContent raw file contents
* @return void
*/
public function processPageEdits($rawContent)
{
if (!$this->edits->beenReceived()) {
return;
}
if (Auth::can(Auth::SAVE)) {
$this->edits->saveMeta(
$rawContent,
$this->getRawMeta(),
$this->getRequestFile()
);
$saved = $this->edits->saveRegions($rawContent, $this, $this->editor);
if ($saved) {
Status::add(true, 'The page have been saved');
}
} else {
Status::add(false, 'You don\'t have the rights to save content');
}
}
/**
* Save edited regions from overall output if authorized, including themes files.
*
* @param string $output contents which will be sent to the user
* @return void
*/
public function processOverallEdits($output)
{
if (!$this->edits->beenReceived()) {
return;
}
if (Auth::can(Auth::SAVE)) {
$saved = $this->edits->saveRegions($output, $this, $this->editor);
if ($saved) {
Status::add(true, 'The theme files have been saved');
}
}
}
/**
* Return the current page raw metadata.
*
* @return string
*/
private function getRawMeta()
{
$pattern = "/^(\/(\*)|---)[[:blank:]]*(?:\r)?\n"
. "(?:(.*?)(?:\r)?\n)?(?(2)\*\/|---)[[:blank:]]*(?:(?:\r)?\n|$)/s";
if (preg_match($pattern, $this->getRawContent(), $rawMetaMatches)
&& isset($rawMetaMatches[3])) {
return $rawMetaMatches[3];
}
}
/**
* Return a plugin setting, either on the page metadata or on the pico config file.
*
* @param string $name name of a setting
* @param mixed $default optional default value to return when the setting doesn't exist
* @return mixed return the setting value from the page metadata, or from the config file,
* or the given default value, or NULL
*/
public function getPluginSetting($name, $default = null, $caseSensitive = false)
{
if ($name === null) {
return null;
}
if (!$caseSensitive) {
$name = strtolower($name);
}
static $c;
static $clow;
if (!$c) {
$c = get_called_class();
if (!$caseSensitive) {
$clow = strtolower($c);
}
}
// from page metadata
static $pageMeta;
if (!$pageMeta) {
$pageMeta = $this->getFileMeta();
if ($pageMeta && !$caseSensitive) {
$pageMeta = self::deepArrayKeyCase($pageMeta, CASE_LOWER);
}
}
if ($pageMeta && isset($pageMeta[$clow]) && isset($pageMeta[$clow][$name])) {
return $pageMeta[$clow][$name];
}
// from config file
static $pluginConfig;
if (!$pluginConfig) {
$pluginConfig = $this->getConfig($c, array());
if (!$caseSensitive) {
$pluginConfig = self::deepArrayKeyCase($pluginConfig, CASE_LOWER);
}
}
return isset($pluginConfig[$name]) ? $pluginConfig[$name] : $default;
}
/**
* Change the case of every array keys, recursively.
*
* @param array $arr
* @param CASE_UPPER|CASE_LOWER $case
* @return array
*/
private static function deepArrayKeyCase($arr, $case)
{
return array_map(function ($item) use ($case) {
if (is_array($item)) {
$item = self::deepArrayKeyCase($item, $case);
}
return $item;
}, array_change_key_case($arr, $case));
}
}