|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Output the page content or data as raw, html, json or xml with `?output`. |
| 4 | + * |
| 5 | + * @author Nicolas Liautaud |
| 6 | + * @link https://github.com/nliautaud/pico-content-output |
| 7 | + * @link http://picocms.org |
| 8 | + * @license http://opensource.org/licenses/MIT The MIT License |
| 9 | + * @version 0.1.0 |
| 10 | + */ |
| 11 | +final class PicoOutput extends AbstractPicoPlugin |
| 12 | +{ |
| 13 | + private $serveContent; |
| 14 | + private $contentFormat; |
| 15 | + |
| 16 | + /** |
| 17 | + * Look for ?output=format in url (format as `content` by default) |
| 18 | + * |
| 19 | + * Triggered after Pico has evaluated the request URL |
| 20 | + * |
| 21 | + * @see Pico::getRequestUrl() |
| 22 | + * @param string &$url part of the URL describing the requested contents |
| 23 | + * @return void |
| 24 | + */ |
| 25 | + public function onRequestUrl(&$url) |
| 26 | + { |
| 27 | + $this->serveContent = isset($_GET['output']); |
| 28 | + if ($this->serveContent) |
| 29 | + $this->contentFormat = $_GET['output'] ? $_GET['output'] : 'content'; |
| 30 | + } |
| 31 | + /** |
| 32 | + * Output the page data in the defined format. |
| 33 | + * |
| 34 | + * Triggered after Pico has rendered the page |
| 35 | + * |
| 36 | + * @param string &$output contents which will be sent to the user |
| 37 | + * @return void |
| 38 | + */ |
| 39 | + public function onPageRendered(&$output) |
| 40 | + { |
| 41 | + if ($this->serveContent && $this->enabledFormat()) |
| 42 | + $output = $this->contentOutput(); |
| 43 | + } |
| 44 | + /** |
| 45 | + * Check if the requested format is enabled in config. |
| 46 | + * |
| 47 | + * @return bool |
| 48 | + */ |
| 49 | + public function enabledFormat() |
| 50 | + { |
| 51 | + $enabledFormats = $this->getPico()->getConfig('PicoOutput.enabledFormats'); |
| 52 | + return is_array($enabledFormats) && in_array($this->contentFormat, $enabledFormats); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Return the current page data in the defined format. |
| 57 | + * @return string |
| 58 | + */ |
| 59 | + private function contentOutput() |
| 60 | + { |
| 61 | + $pico = $this->getPico(); |
| 62 | + switch ($this->contentFormat) { |
| 63 | + case 'raw': |
| 64 | + return $pico->getRawContent(); |
| 65 | + case 'prepared': |
| 66 | + return $pico->prepareFileContent($pico->getRawContent(), $pico->getFileMeta()); |
| 67 | + case 'json': |
| 68 | + header('Content-Type: application/json;charset=utf-8'); |
| 69 | + return json_encode($pico->getCurrentPage()); |
| 70 | + case 'xml': |
| 71 | + header("Content-type: text/xml"); |
| 72 | + $xml = new SimpleXMLElement('<page/>'); |
| 73 | + $this->array_to_xml($pico->getCurrentPage(), $xml); |
| 74 | + return $xml->asXML(); |
| 75 | + default: |
| 76 | + return $pico->getFileContent(); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + // function defination to convert array to xml |
| 81 | + private function array_to_xml( $data, &$xml_data ) |
| 82 | + { |
| 83 | + foreach( $data as $key => $value ) { |
| 84 | + if( is_numeric($key) ){ |
| 85 | + $key = 'item'.$key; //dealing with <0/>..<n/> issues |
| 86 | + } |
| 87 | + if( is_array($value) ) { |
| 88 | + $subnode = $xml_data->addChild($key); |
| 89 | + $this->array_to_xml($value, $subnode); |
| 90 | + } else { |
| 91 | + $xml_data->addChild("$key",htmlspecialchars("$value")); |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | +} |
| 96 | +?> |
0 commit comments