-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPicoOutput.php
126 lines (121 loc) · 3.7 KB
/
PicoOutput.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
<?php
/**
* Output Pico CMS page data as raw text, html, json or xml with `?output`.
*
* @author Nicolas Liautaud
* @link https://github.com/nliautaud/pico-content-output
* @link http://picocms.org
* @license http://opensource.org/licenses/MIT The MIT License
*/
class PicoOutput extends AbstractPicoPlugin
{
const API_VERSION = 2;
/**
* Output the page data in the defined format.
*
* Triggered after Pico has rendered the page
*
* @see DummyPlugin::onPageRendering()
*
* @param string &$output contents which will be sent to the user
*
* @return void
*/
public function onPageRendered(&$output)
{
if (empty($_GET['output'])) {
return;
}
if ($this->canOutput($_GET['output'])) {
$output = $this->contentOutput($_GET['output'], $output);
}
}
/**
* Check if the requested format is enabled in config.
*
* @param string $outputFormat
*
* @return bool
*/
public function canOutput($outputFormat)
{
$enabledFormats = $this->getPluginConfig('formats');
if (!is_array($enabledFormats)) {
$enabledFormats = array();
}
$pageMeta = $this->getFileMeta();
if (isset($pageMeta['PicoOutput']) && is_array($pageMeta['PicoOutput']['formats'])) {
$enabledFormats = array_merge($enabledFormats, $pageMeta['PicoOutput']['formats']);
}
return in_array($outputFormat, $enabledFormats);
}
/**
* Get a plugin setting, on page metadata or on pico config
*
* @return mixed
*/
public function getSetting($key)
{
$pageMeta = $this->getFileMeta();
if (isset($pageMeta['PicoOutput']) && isset($pageMeta['PicoOutput'][$key])) {
return $pageMeta['PicoOutput'][$key];
}
return $this->getPluginConfig($key);
}
/**
* Return the current page data in the defined format.
*
* @param string $outputFormat
* @param string $default
*
* @return string
*/
private function contentOutput($outputFormat, $default)
{
$pico = $this->getPico();
$page = $pico->getCurrentPage();
unset($page['previous_page']);
unset($page['next_page']);
unset($page['tree_node']);
switch ($outputFormat) {
case 'raw':
return $pico->getRawContent();
case 'prepared':
return $pico->prepareFileContent($pico->getRawContent(), $pico->getFileMeta());
case 'json':
header('Content-Type: application/json;charset=utf-8');
return json_encode($page);
case 'xml':
header("Content-type: text/xml");
$xml = new SimpleXMLElement('<page/>');
PicoOutput::arrayToXML($page, $xml);
return $xml->asXML();
case 'content':
return $pico->getFileContent();
default:
return $default;
}
}
/**
* Convert an array to a SimpleXMLElement
*
* @param [arr] $arr
* @param [SimpleXMLElement] $xmlRoot
* @return void
* @see https://stackoverflow.com/a/5965940
*/
private static function arrayToXML($arr, &$xmlRoot)
{
foreach ($arr as $key => $value) {
if (is_numeric($key)) {
$key = 'item'.$key; //dealing with <0/>..<n/> issues
}
if (is_array($value)) {
$subnode = $xmlRoot->addChild($key);
PicoOutput::arrayToXML($value, $subnode);
} else {
$xmlRoot->addChild("$key", htmlspecialchars("$value"));
}
}
}
}