@@ -0,0 +1,97 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Class to group HTTP response related functionality
|
||||
*
|
||||
* @author Time.ly Network Inc.
|
||||
* @since 2.0
|
||||
*
|
||||
* @package AI1EC
|
||||
* @subpackage AI1EC.Http.Response
|
||||
*/
|
||||
class Ai1ec_Http_Response_Helper {
|
||||
|
||||
/**
|
||||
* Perform redirect to desired location and stop script execution after that
|
||||
*
|
||||
* When debug mode is activated redirect doesn't happen but instead link
|
||||
* is outputted to screen, to allow developer to tamper with the flow, debug
|
||||
* it and make changes as desired.
|
||||
*
|
||||
* @param string $location Location to redirect user to
|
||||
* @param int $code HTTP response code to use in redirects
|
||||
*
|
||||
* @uses wp_redirect To create actual headers.
|
||||
*
|
||||
* @return int|NULL Method does call {@see self::stop()} to halt further
|
||||
* script execution unless mocked
|
||||
*/
|
||||
public static function redirect( $location, $code = 302 ) {
|
||||
header( 'Location: ' . $location, true, $code );
|
||||
return self::stop();
|
||||
}
|
||||
|
||||
/**
|
||||
* Mockable method to halt script execution
|
||||
*
|
||||
* @param int $code Code to be used in `exit` statement
|
||||
*
|
||||
* @return void Method does not return
|
||||
*/
|
||||
public static function stop( $code = 0 ) {
|
||||
exit( $code );
|
||||
}
|
||||
|
||||
/**
|
||||
* ai1ec_utf8 function
|
||||
*
|
||||
* Encode value as safe UTF8 - discarding unrecognized characters.
|
||||
* NOTE: objects will be cast as array.
|
||||
*
|
||||
* @uses iconv To change encoding
|
||||
* @uses mb_convert_encoding To change encoding if `iconv` is not available
|
||||
*
|
||||
* @param mixed $input Value to encode
|
||||
*
|
||||
* @return mixed UTF8 encoded value
|
||||
*
|
||||
* @throws Exception If no trans-coding method is available
|
||||
*/
|
||||
public static function utf8( $input ) {
|
||||
if ( NULL === $input ) {
|
||||
return NULL;
|
||||
}
|
||||
if ( is_scalar( $input ) ) {
|
||||
if ( function_exists( 'iconv' ) ) {
|
||||
return iconv( 'UTF-8', 'UTF-8//IGNORE', $input );
|
||||
}
|
||||
if ( function_exists( 'mb_convert_encoding' ) ) {
|
||||
return mb_convert_encoding( $input, 'UTF-8' );
|
||||
}
|
||||
throw new Exception(
|
||||
'Either `iconv` or `mb_convert_encoding` must be available.'
|
||||
);
|
||||
}
|
||||
if ( ! is_array( $input ) ) {
|
||||
$input = (array)$input;
|
||||
}
|
||||
return array_map( array( __CLASS__, 'utf8' ), $input );
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes the protocla from the url
|
||||
*
|
||||
* @param string $url
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function remove_protocols( $url ) {
|
||||
$disallowed = array( 'http:', 'https:' );
|
||||
foreach ( $disallowed as $d ) {
|
||||
if ( strpos( $url, $d ) === 0 ) {
|
||||
return str_replace( $d, '', $url );
|
||||
}
|
||||
}
|
||||
return $url;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
/**
|
||||
* Abstract strategy class to render the Request.
|
||||
*
|
||||
* @author Time.ly Network Inc.
|
||||
* @since 2.0
|
||||
*
|
||||
* @package AI1EC
|
||||
* @subpackage AI1EC.Http.Response.Render
|
||||
*/
|
||||
abstract class Ai1ec_Http_Response_Render_Strategy extends Ai1ec_Base {
|
||||
|
||||
/**
|
||||
* Dump output buffers before starting output
|
||||
*
|
||||
* @return bool True unless an error occurs
|
||||
*/
|
||||
protected function _dump_buffers() {
|
||||
$this->_registry->get( 'dbi.dbi' )->disable_debug();
|
||||
|
||||
|
||||
return $this
|
||||
->_registry
|
||||
->get( 'compatibility.ob' )
|
||||
->end_clean_all();
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Render the output.
|
||||
*
|
||||
* @param array $params
|
||||
*/
|
||||
abstract public function render( array $params );
|
||||
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
/**
|
||||
* Render the request as csv
|
||||
*
|
||||
* @author Time.ly Network Inc.
|
||||
* @since 2.0
|
||||
*
|
||||
* @package AI1EC
|
||||
* @subpackage AI1EC.Http.Response.Render.Strategy
|
||||
*/
|
||||
class Ai1ec_Render_Strategy_Csv extends Ai1ec_Http_Response_Render_Strategy {
|
||||
|
||||
/* (non-PHPdoc)
|
||||
* @see Ai1ec_Http_Response_Render_Strategy::render()
|
||||
*/
|
||||
public function render( array $params ) {
|
||||
$this->_dump_buffers();
|
||||
|
||||
$now = gmdate( 'D, d M Y H:i:s' );
|
||||
$filename = $params['filename'];
|
||||
|
||||
header( 'Expires: Tue, 03 Jul 2001 06:00:00 GMT' );
|
||||
header( 'Cache-Control: max-age=0, no-cache, must-revalidate, proxy-revalidate' );
|
||||
header( 'Last-Modified: ' . $now . ' GMT' );
|
||||
|
||||
// force download
|
||||
header( 'Content-Type: application/force-download' );
|
||||
header( 'Content-Type: application/octet-stream' );
|
||||
header( 'Content-Type: application/download' );
|
||||
|
||||
// disposition / encoding on response body
|
||||
header( 'Content-Disposition: attachment;filename="' . addcslashes(
|
||||
$filename, '"' ) . '"' );
|
||||
header( 'Content-Transfer-Encoding: binary' );
|
||||
|
||||
$columns = $params['columns'];
|
||||
for ( $i = 0; $i < count( $columns ); $i++ ) {
|
||||
if ( $i > 0 ) {
|
||||
echo( ',' );
|
||||
}
|
||||
echo( $columns[$i] );
|
||||
}
|
||||
echo( "\n" );
|
||||
|
||||
$data = $params['data'];
|
||||
for ( $i = 0; $i < count( $data ); $i++ ) {
|
||||
$row = $data[$i];
|
||||
for ( $j = 0; $j < count( $row ); $j++ ) {
|
||||
if ( $j > 0 ) {
|
||||
echo( ',' );
|
||||
}
|
||||
echo( $row[$j] );
|
||||
}
|
||||
echo( "\n" );
|
||||
}
|
||||
|
||||
return Ai1ec_Http_Response_Helper::stop( 0 );
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,119 @@
|
||||
<?php
|
||||
/**
|
||||
* Render the request as html.
|
||||
*
|
||||
* @author Time.ly Network Inc.
|
||||
* @since 2.0
|
||||
*
|
||||
* @package AI1EC
|
||||
* @subpackage AI1EC.Http.Response.Render.Strategy
|
||||
*/
|
||||
class Ai1ec_Render_Strategy_Html extends Ai1ec_Http_Response_Render_Strategy {
|
||||
|
||||
/**
|
||||
* Twig page content placeholder.
|
||||
*/
|
||||
const CALENDAR_PLACEHOLDER = '<!-- AI1EC_PAGE_CONTENT_PLACEHOLDER -->';
|
||||
|
||||
/**
|
||||
* @var string the event html.
|
||||
*/
|
||||
protected $_html;
|
||||
|
||||
/**
|
||||
* @var string The html for the footer of the event.
|
||||
*/
|
||||
protected $_html_footer = '';
|
||||
|
||||
/**
|
||||
* Caller identifier. Just for paranoid check in append_content method.
|
||||
* Expected 'calendar' or none.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $_caller = '';
|
||||
|
||||
/**
|
||||
* Registers proper filters for content modifications.
|
||||
*
|
||||
* @param array $params Function params.
|
||||
*
|
||||
* @return void Method does not return.
|
||||
*/
|
||||
public function render( array $params ) {
|
||||
$this->_html = $params['data'];
|
||||
if ( isset( $params['caller'] ) ) {
|
||||
$this->_caller = $params['caller'];
|
||||
}
|
||||
if ( isset( $params['footer'] ) ) {
|
||||
$this->_html_footer = $params['footer'];
|
||||
}
|
||||
if ( isset( $params['is_event'] ) ) {
|
||||
// Filter event post content, in single- and multi-post views
|
||||
add_filter( 'the_content', array( $this, 'event_content' ), PHP_INT_MAX - 1 );
|
||||
return;
|
||||
}
|
||||
// Replace page content - make sure it happens at (almost) the very end of
|
||||
add_filter( 'the_content', array( $this, 'append_content' ), PHP_INT_MAX - 1 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Append locally generated content to normal page content. By default,
|
||||
* first checks if we are in The Loop before outputting to prevent multiple
|
||||
* calendar display - unless setting is turned on to skip this check.
|
||||
* We should not append full calendar body to single event content as it
|
||||
* leads to "calendar" nesting if default calendar page contains calendar
|
||||
* shortcode.
|
||||
*
|
||||
* @param string $content Post/Page content
|
||||
* @return string Modified Post/Page content
|
||||
*/
|
||||
public function append_content( $content ) {
|
||||
if (
|
||||
'calendar' === $this->_caller &&
|
||||
! $this->_registry->get( 'calendar.state' )->append_content()
|
||||
) {
|
||||
return $content;
|
||||
}
|
||||
$settings = $this->_registry->get( 'model.settings' );
|
||||
|
||||
// Include any admin-provided page content in the placeholder specified in
|
||||
// the calendar theme template.
|
||||
if ( $settings->get( 'skip_in_the_loop_check' ) || in_the_loop() ) {
|
||||
$content = str_replace(
|
||||
self::CALENDAR_PLACEHOLDER,
|
||||
$content,
|
||||
$this->_html
|
||||
);
|
||||
$content .= $this->_html_footer;
|
||||
}
|
||||
return $content;
|
||||
}
|
||||
|
||||
/**
|
||||
* event_content function
|
||||
*
|
||||
* Filter event post content by inserting relevant details of the event
|
||||
* alongside the regular post content.
|
||||
*
|
||||
* @param string $content Post/Page content
|
||||
*
|
||||
* @return string Post/Page content
|
||||
**/
|
||||
public function event_content( $content ) {
|
||||
if ( ! $this->_registry->get( 'calendar.state' )->append_content() ) {
|
||||
$content = '';
|
||||
}
|
||||
$to_return = $this->_html . $content;
|
||||
if ( isset( $this->_html_footer ) ) {
|
||||
$to_return .= $this->_html_footer;
|
||||
}
|
||||
// Pass the orginal content to the filter so that it can be modified
|
||||
return apply_filters(
|
||||
'ai1ec_event_content',
|
||||
$to_return,
|
||||
$content
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Render the request as ical.
|
||||
*
|
||||
* @author Time.ly Network Inc.
|
||||
* @since 2.0
|
||||
*
|
||||
* @package AI1EC
|
||||
* @subpackage AI1EC.Http.Response.Render.Strategy
|
||||
*/
|
||||
class Ai1ec_Render_Strategy_Ical extends Ai1ec_Http_Response_Render_Strategy {
|
||||
|
||||
/* (non-PHPdoc)
|
||||
* @see Ai1ec_Http_Response_Render_Strategy::render()
|
||||
*/
|
||||
public function render( array $params ) {
|
||||
$this->_dump_buffers();
|
||||
header( 'Content-type: text/calendar; charset=utf-8' );
|
||||
echo $params['data'];
|
||||
return Ai1ec_Http_Response_Helper::stop( 0 );
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
/**
|
||||
* Render the request as json.
|
||||
*
|
||||
* @author Time.ly Network Inc.
|
||||
* @since 2.0
|
||||
*
|
||||
* @package AI1EC
|
||||
* @subpackage AI1EC.Http.Response.Render.Strategy
|
||||
*/
|
||||
class Ai1ec_Render_Strategy_Json extends Ai1ec_Render_Strategy_Jsonp {
|
||||
|
||||
/* (non-PHPdoc)
|
||||
* @see Ai1ec_Http_Response_Render_Strategy::render()
|
||||
*/
|
||||
public function render( array $params ) {
|
||||
$params['callback'] = '';
|
||||
return parent::render( $params );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
/**
|
||||
* Render the request as jsonp.
|
||||
*
|
||||
* @author Time.ly Network Inc.
|
||||
* @since 2.0
|
||||
*
|
||||
* @package AI1EC
|
||||
* @subpackage AI1EC.Http.Response.Render.Strategy
|
||||
*/
|
||||
class Ai1ec_Render_Strategy_Jsonp extends Ai1ec_Http_Response_Render_Strategy {
|
||||
|
||||
/* (non-PHPdoc)
|
||||
* @see Ai1ec_Http_Response_Render_Strategy::render()
|
||||
*/
|
||||
public function render( array $params ) {
|
||||
$this->_dump_buffers();
|
||||
header( 'HTTP/1.1 200 OK' );
|
||||
header( 'Content-Type: application/json; charset=UTF-8' );
|
||||
$data = Ai1ec_Http_Response_Helper::utf8( $params['data'] );
|
||||
$output = json_encode( $data );
|
||||
if ( ! empty( $params['callback'] ) ) {
|
||||
$output = $params['callback'] . '(' . $output . ')';
|
||||
} else if ( isset( $_GET['callback'] ) ) {
|
||||
$output = $_GET['callback'] . '(' . $output . ')';
|
||||
}
|
||||
|
||||
echo $output;
|
||||
return Ai1ec_Http_Response_Helper::stop( 0 );
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Render the request as ical.
|
||||
*
|
||||
* @author Time.ly Network Inc.
|
||||
* @since 2.0
|
||||
*
|
||||
* @package AI1EC
|
||||
* @subpackage AI1EC.Http.Response.Render.Strategy
|
||||
*/
|
||||
class Ai1ec_Render_Strategy_Redirect extends Ai1ec_Http_Response_Render_Strategy {
|
||||
|
||||
/* (non-PHPdoc)
|
||||
* @see Ai1ec_Http_Response_Render_Strategy::render()
|
||||
*/
|
||||
public function render( array $params ) {
|
||||
Ai1ec_Wp_Uri_Helper::local_redirect(
|
||||
$params['url'],
|
||||
$params['query_args']
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Do not render anything.
|
||||
*
|
||||
* @author Time.ly Network Inc.
|
||||
* @since 2.0
|
||||
*
|
||||
* @package AI1EC
|
||||
* @subpackage AI1EC.Http.Response.Render.Strategy
|
||||
*/
|
||||
class Ai1ec_Render_Strategy_Void extends Ai1ec_Http_Response_Render_Strategy {
|
||||
|
||||
/* (non-PHPdoc)
|
||||
* @see Ai1ec_Http_Response_Render_Strategy::render()
|
||||
*/
|
||||
public function render( array $params ) {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Render the request as xcal.
|
||||
*
|
||||
* @author Time.ly Network Inc.
|
||||
* @since 2.3
|
||||
*
|
||||
* @package AI1EC
|
||||
* @subpackage AI1EC.Http.Response.Render.Strategy
|
||||
*/
|
||||
class Ai1ec_Render_Strategy_Xcal extends Ai1ec_Http_Response_Render_Strategy {
|
||||
|
||||
/* (non-PHPdoc)
|
||||
* @see Ai1ec_Http_Response_Render_Strategy::render()
|
||||
*/
|
||||
public function render( array $params ) {
|
||||
$this->_dump_buffers();
|
||||
header( 'Content-Type: application/force-download; name="calendar.xml"' );
|
||||
header( 'Content-type: text/xml' );
|
||||
header( 'Content-Transfer-Encoding: binary' );
|
||||
header( 'Content-Disposition: attachment; filename="calendar.xml"' );
|
||||
header( 'Expires: 0' );
|
||||
header( 'Cache-Control: no-cache, must-revalidate' );
|
||||
header( 'Pragma: no-cache' );
|
||||
echo $params['data'];
|
||||
return Ai1ec_Http_Response_Helper::stop( 0 );
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
/**
|
||||
* Render the request as xml.
|
||||
*
|
||||
* @author Time.ly Network Inc.
|
||||
* @since 2.0
|
||||
*
|
||||
* @package AI1EC
|
||||
* @subpackage AI1EC.Http.Response.Render.Strategy
|
||||
*/
|
||||
class Ai1ec_Render_Strategy_Xml extends Ai1ec_Http_Response_Render_Strategy {
|
||||
|
||||
/* (non-PHPdoc)
|
||||
* @see Ai1ec_Http_Response_Render_Strategy::render()
|
||||
*/
|
||||
public function render( array $params ) {
|
||||
$this->_dump_buffers();
|
||||
header( 'HTTP/1.1 200 OK' );
|
||||
header( 'Content-Type: text/xml; charset=UTF-8' );
|
||||
$data = Ai1ec_Http_Response_Helper::utf8( $params['data'] );
|
||||
$output = Ai1ec_XML_Builder::serialize_to_xml( $data );
|
||||
echo $output;
|
||||
return Ai1ec_Http_Response_Helper::stop( 0 );
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user