Skip to content
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
30 changes: 15 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# Custom Page Headers Symphony Extension

- Version: 1.2
- Version: 1.2.1
- Author: Henry Singleton
- Build Date: 06 06 2012
- Requirements: Symphony 2.2.x - Symphony 2.3.x
- Build Date: 29 10 2014
- Requirements: Symphony 2.2.x - Symphony 2.5.x

## Overview

This Symphony CMS extension allows you to define headers by page output. This lets you build headers using data from datasources or other page information. Anything that's accessible from a standard page really!

Great for generating redirect urls for 301/302 redirects if you need information from existing Symphony entries, or want to store redirect stats via an event etc.
Great for generating redirect urls for 301/302 redirects if you need information from existing Symphony entries, or want to store redirect stats via an event etc.

## Installation

Expand All @@ -32,46 +32,46 @@ Create a new page, give it a page type of 'headers' and save the following as th

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="xml"
omit-xml-declaration="yes"
encoding="UTF-8"
indent="yes" />

<xsl:template match="/">
HTTP/1.1 301 Moved Permanently
Location: http://www.google.com/
</xsl:template>

</xsl:stylesheet>

Here is an exmpale of custom 404 page, implemented in a different way. Note that you'll need to ensure there are two empty lines
between your headers and any page content you want to output (as per the HTTP spec).

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="xml"
omit-xml-declaration="yes"
encoding="UTF-8"
indent="yes" />

<xsl:template match="/">
<xsl:text>HTTP/1.0 404 Not Found

</xsl:text>

<h1>Page not found</h1>

<p>Try our homepage</p>

</xsl:template>

</xsl:stylesheet>

## Changelog

- **1.2.1** Updated extension.meta.xml to extend compatibility to Symphony 2.5.x.
- **1.2** Fixes bug where page output would be blank if a Symphony page error was generated.
- **1.1** Updated extension.meta.xml to include namespace for Symphony 2.3 compatibility. (Thanks to andrewminton)
- **1.0** Initial release for internal project.

184 changes: 92 additions & 92 deletions extension.driver.php
Original file line number Diff line number Diff line change
@@ -1,93 +1,93 @@
<?php
class extension_page_headers extends Extension {
private $headersTrigger;
public function about() {
return array(
'name' => 'Page Headers',
'version' => '1.2',
'release-date' => '2012-06-06',
'author' => array(
'name' => 'Henry Singleton',
'website' => 'http://henrysingleton.com'
),
'description' => 'Allows headers to be output as part of page content, overwriting existing headers.'
);
}
public function uninstall() {
return true;
}
public function install() {
return true;
}
public function getSubscribedDelegates() {
return array(
array(
'page' => '/frontend/',
'delegate' => 'FrontendPageResolved',
'callback' => 'checkHeadersPageType'
),
array(
'page' => '/frontend/',
'delegate' => 'FrontendOutputPostGenerate',
'callback' => 'processPageContent'
)
);
}
public function checkheadersPageType($page) {
//Check that the page type has been sent to 'headers' so we only process a page when we need to.
if (is_array($page) &&
is_array($page['page_data']) &&
array_key_exists('type', $page['page_data']) &&
array_search('headers',$page['page_data']['type']) !== false
) {
$this->headersTrigger = true;
}
}
public function processPageContent($page) {
//If this page has 'headers' set as a page type, process the content.
if ($this->headersTrigger === true) {
$content = $page['output'];
if (
//If the content is false that means there is likely a symphony error being shown. If so, don't do anything.
$content !== false &&
//Just check that the page content doesn't start with an angle bracket, as that would mean XML/HTML was probably being output and not the sweet juicy headers we crave.
strpos($content, '<') !== 0 &&
//Check we haven't already sent the headers, to avoid nasty PHP error.
!headers_sent()
) {
//split response by two consecutive newlines (as per http sepc)
$parts = explode("\n\n", $content);
//grab the block before the first two newlines, and then split by newlines for each header
$headers = explode("\n",array_shift($parts));
if (is_array($headers)) {
//Send each found header
foreach ($headers as $header) {
header(trim($header));
}
//Turn parts back into string, with headers absent due to array_unshift above
$page['output'] = trim(implode("\n", $parts));
//If only headers were used in the page output, kill the script now, as we have nothing else to send, and a Symphony error will be generated if the output is empty.
if (strlen($page['output']) === 0) die();
}
}
}
}
}

class extension_page_headers extends Extension {

private $headersTrigger;

public function about() {
return array(
'name' => 'Page Headers',
'version' => '1.2',
'release-date' => '2012-06-06',
'author' => array(
'name' => 'Henry Singleton',
'website' => 'http://henrysingleton.com'
),
'description' => 'Allows headers to be output as part of page content, overwriting existing headers.'
);
}

public function uninstall() {
return true;
}

public function install() {
return true;
}

public function getSubscribedDelegates() {
return array(
array(
'page' => '/frontend/',
'delegate' => 'FrontendPageResolved',
'callback' => 'checkHeadersPageType'
),
array(
'page' => '/frontend/',
'delegate' => 'FrontendOutputPostGenerate',
'callback' => 'processPageContent'
)
);
}

public function checkheadersPageType($page) {
//Check that the page type has been sent to 'headers' so we only process a page when we need to.
if (is_array($page) &&
is_array($page['page_data']) &&
array_key_exists('type', $page['page_data']) &&
array_search('headers',$page['page_data']['type']) !== false
) {
$this->headersTrigger = true;
}
}

public function processPageContent($page) {
//If this page has 'headers' set as a page type, process the content.
if ($this->headersTrigger === true) {
$content = $page['output'];

if (

//If the content is false that means there is likely a symphony error being shown. If so, don't do anything.
$content !== false &&

//Just check that the page content doesn't start with an angle bracket, as that would mean XML/HTML was probably being output and not the sweet juicy headers we crave.
strpos($content, '<') !== 0 &&

//Check we haven't already sent the headers, to avoid nasty PHP error.
!headers_sent()

) {
//split response by two consecutive newlines (as per http sepc)
$parts = explode("\n\n", $content);

//grab the block before the first two newlines, and then split by newlines for each header
$headers = explode("\n",array_shift($parts));

if (is_array($headers)) {

//Send each found header
foreach ($headers as $header) {
header(trim($header));
}

//Turn parts back into string, with headers absent due to array_unshift above
$page['output'] = trim(implode("\n", $parts));

//If only headers were used in the page output, kill the script now, as we have nothing else to send, and a Symphony error will be generated if the output is empty.
if (strlen($page['output']) === 0) die();
}
}
}
}
}
38 changes: 20 additions & 18 deletions extension.meta.xml
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<extension id="page_headers" status="released" xmlns="http://symphony-cms.com/schemas/extension/1.0">
<name>Page Headers</name>
<description>Customise HTTP headers by standard page output.</description>
<repo type="github">https://github.com/henrysingleton/page_headers</repo>
<types>
<type>Other</type>
</types>
<authors>
<author>
<name github="henrysingleton" symphony="henry">Henry Singleton</name>
<website>http://henrysingleton.com/</website>
</author>
</authors>
<releases>
<release version="1.2" date="2012-06-06" min="2.2" max="2.3.x" />
<release version="1.1" date="2012-06-04" min="2.2" max="2.3.x" />
<release version="1.0" date="2012-04-11" min="2.2" max="2.2.x" />
</releases>
</extension>
<name>Page Headers</name>
<description>Customise HTTP headers by standard page output.</description>
<repo type="github">https://github.com/henrysingleton/page_headers</repo>
<url type="discuss">http://www.getsymphony.com/discuss/thread/86515/</url>
<types>
<type>Other</type>
</types>
<authors>
<author>
<name github="henrysingleton" symphony="henry">Henry Singleton</name>
<website>http://henrysingleton.com/</website>
</author>
</authors>
<releases>
<release version="1.2.1" date="2014-10-29" min="2.2" max="2.5.x" />
<release version="1.2" date="2012-06-06" min="2.2" max="2.3.x" />
<release version="1.1" date="2012-06-04" min="2.2" max="2.3.x" />
<release version="1.0" date="2012-04-11" min="2.2" max="2.2.x" />
</releases>
</extension>