Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PHP7 Compatibility release #17

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
22 changes: 11 additions & 11 deletions bin/request
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
<?php

Class Request_REST_API extends ShellCommand{
private $_args = NULL;
public function run(array $args=NULL){

private $_args = null;

public function run(array $args = null){
$this->_args = $args;

if(!Shell::instance()->isLoggedIn()){
throw new Exception('Valid authentication token must be supplied.');
}

if(!Shell::instance()->Author->isDeveloper()){
throw new Exception('Only developers can enable maintenance mode');
}
}

require_once(CORE . '/class.frontend.php');
// include the extension core
require_once(EXTENSIONS . '/rest_api/lib/class.rest_api.php');

$_REQUEST['token'] = Shell::instance()->Author->createAuthToken();
$_REQUEST['url'] = $this->get_argument('path', '/');
$_REQUEST['format'] = strtolower($this->get_argument('format', 'xml'));
$_SERVER['REQUEST_METHOD'] = strtolower($this->get_argument('method', 'GET'));

REST_API::init();

}

private function get_argument($name, $default) {
foreach($this->_args as $i=> $args) {
if($args == ('-' . $name)) {
Expand All @@ -36,7 +36,7 @@
}
return $default;
}

}

return 'Request_REST_API';
8 changes: 4 additions & 4 deletions data-sources/data.rest_api_entries.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require_once(TOOLKIT . '/class.datasource.php');
require_once(TOOLKIT . '/class.fieldmanager.php');
require_once(EXTENSIONS . '/rest_api/plugins/entries/rest.entries.php');

Class DatasourceREST_API_Entries extends SectionDatasource {

public $dsParamROOTELEMENT = 'response';
Expand All @@ -30,7 +30,7 @@ public function getSource(){
return REST_Entries::getSectionId();
}

public function grab(array &$param_pool = NULL){
public function grab(array &$param_pool = null){
// remove placeholder elements
unset($this->dsParamINCLUDEDELEMENTS);

Expand Down Expand Up @@ -76,7 +76,7 @@ public function grab(array &$param_pool = NULL){
foreach(REST_Entries::getDatasourceParam('filters') as $field_handle => $filter_value) {
$filter_value = rawurldecode($filter_value);
$field_id = FieldManager::fetchFieldIDFromElementName(
$field_handle,
$field_handle,
REST_Entries::getSectionId()
);
if(is_numeric($field_id)) $this->dsParamFILTERS[$field_id] = $filter_value;
Expand All @@ -87,4 +87,4 @@ public function grab(array &$param_pool = NULL){
return $this->execute($param_pool);
}

}
}
36 changes: 18 additions & 18 deletions extension.driver.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

Class extension_rest_api extends Extension{

public function getSubscribedDelegates() {
return array(
array(
Expand All @@ -16,53 +16,53 @@ public function getSubscribedDelegates() {
)
);
}

public function manipulateResolvedPage($context) {
if(!class_exists('REST_API') || (class_exists('REST_API') && !REST_API::isFrontendPageRequest())) return;
// get the page data from context
$page = $context['page_data'];

if(REST_API::getHTTPMethod() == 'get') $page['data_sources'] = 'rest_api_entries';
if(REST_API::getHTTPMethod() == 'post') $page['events'] = 'rest_api_entries';

$context['page_data'] = $page;
}

public function frontendOutputPreGenerate($context) {
if(class_exists('REST_API') && class_exists('REST_Entries') && REST_API::isFrontendPageRequest()) {
REST_Entries::sendOutput($context['xml']);
}
}

public function uninstall(){
$htaccess = @file_get_contents(DOCROOT . '/.htaccess');
if($htaccess === FALSE) return FALSE;
if($htaccess === false) return false;

$htaccess = self::__removeAPIRules($htaccess);
return @file_put_contents(DOCROOT . '/.htaccess', $htaccess);
}

public function install(){

$htaccess = @file_get_contents(DOCROOT . '/.htaccess');
if($htaccess === FALSE) return FALSE;
if($htaccess === false) return false;
$token = md5(time());

$rule = "
### START API RULES
RewriteRule ^symphony\/api(\/(.*\/?))?$ extensions/rest_api/handler.php?url={$token}&%{QUERY_STRING} [NC,L]
### END API RULES\n\n";

$htaccess = self::__removeAPIRules($htaccess);
$htaccess = preg_replace('/RewriteRule .\* - \[S=14\]\s*/i', "RewriteRule .* - [S=14]\n{$rule}\t", $htaccess);
$htaccess = str_replace($token, '$1', $htaccess);

return file_put_contents(DOCROOT . '/.htaccess', $htaccess);

}

private static function __removeAPIRules($htaccess){
return preg_replace('/### START API RULES(.)+### END API RULES[\n]/is', NULL, $htaccess);
return preg_replace('/### START API RULES(.)+### END API RULES[\n]/is', null, $htaccess);
}
}

}
5 changes: 5 additions & 0 deletions extension.meta.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
</author>
</authors>
<releases>
<release version="2.0.0" date="TBA" min="4.0.0" max="4.x.x" php-min="5.6.x" php-max="7.x.x">
- Update for Symphony 4.x
- PHP7 Compatibility
- Replace deprecated method fetch() by select()
</release>
<release version="1.4.0" date="2014-08-08" min="2.4.0" max="2.6.x">
- Update for Symphony 2.6
</release>
Expand Down
100 changes: 50 additions & 50 deletions lib/class.rest_api.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,132 +3,132 @@
if(!class_exists('XMLToArray')) require_once('class.xmltoarray.php');

Class REST_API {
private static $_is_frontend_page = FALSE;
private static $_uri = NULL;
private static $_token = NULL;
private static $_output_type = NULL;
private static $_http_method = NULL;
private static $_plugin_name = NULL;
private static $_plugin_class = NULL;

private static $_is_frontend_page = false;

private static $_uri = null;
private static $_token = null;
private static $_output_type = null;
private static $_http_method = null;
private static $_plugin_name = null;
private static $_plugin_class = null;

private static function __authenticate() {
$logged_in = Symphony::Engine()->isLoggedIn();
if (!$logged_in) self::sendError('API is private. Authentication failed.', 403);
}
public static function isFrontendPageRequest($is_frontend_page=NULL) {

public static function isFrontendPageRequest($is_frontend_page = null) {
if(isset($is_frontend_page)) self::$_is_frontend_page = $is_frontend_page;
return self::$_is_frontend_page;
}

public static function getOutputFormat() {
return self::$_output_type;
}

public static function getHTTPMethod() {
return self::$_http_method;
}

public static function init() {

// store request parameters for later
self::$_token = trim($_REQUEST['token']);
self::$_output_type = (isset($_REQUEST['format']) ? $_REQUEST['format'] : 'xml');
self::$_output_type = (isset($_REQUEST['format']) ? $_REQUEST['format'] : 'xml');
self::$_uri = explode('/', trim($_REQUEST['url'], '/'));
self::$_http_method = strtolower($_SERVER['REQUEST_METHOD']);

// get plugin name from the first segment in the URL
// and remove it from the URL segments list
$plugin_name = strtolower(self::$_uri[0]);
self::$_plugin_name = $plugin_name;
self::$_plugin_class = 'REST_' . ucfirst($plugin_name);
array_shift(self::$_uri);

// include the plugin!
require_once(EXTENSIONS . "/rest_api/plugins/$plugin_name/rest.$plugin_name.php");
if (!class_exists(self::$_plugin_class)) REST_API::sendError(sprintf("Plugin '%s' does not exist.", self::$_plugin_class), 404);

// perform global API authentication
self::__authenticate($plugin_class);

// initialise the plugin
if(method_exists(self::$_plugin_class, 'init')) call_user_func(array(self::$_plugin_class, 'init'));

// perform plugin authentication
if(method_exists(self::$_plugin_class, 'authenticate')) call_user_func(array(self::$_plugin_class, 'authenticate'));

if(method_exists(self::$_plugin_class, self::$_http_method)) {
call_user_func(array(self::$_plugin_class, self::$_http_method));
} else {
REST_API::sendError(sprintf("Plugin '%s' does not support HTTP %s.", self::$_plugin_class, strtoupper($method)), 401);
}
}

public static function getRequestURI() {
return self::$_uri;
}
public static function sendOutput($response_body=NULL, $code=200) {

public static function sendOutput($response_body = null, $code = 200) {

switch($code) {
case 200: header('HTTP/1.0 200 OK'); break;
case 401: header('HTTP/1.0 401 Bad Request'); break;
case 403: header('HTTP/1.0 403 Forbidden'); break;
case 404: header('HTTP/1.0 404 Not Found'); break;
}

$xml = $response_body;
if(is_array($xml)) $xml = reset($xml);
if($xml instanceOf XMLElement) $xml = $xml->generate(TRUE);
if($xml instanceOf XMLElement) $xml = $xml->generate(true);

switch(self::$_output_type) {

case 'json':
header('Content-Type: text/plain; charset=utf-8');
$output = json_encode(XMLToArray::convert($xml));
$output = json_encode(XMLToArray::convert($xml));
break;

case 'serialise':
case 'serialize':
header('Content-Type: text/plain; charset=utf-8');
$output = serialize(XMLToArray::convert($xml));
break;

case 'yaml':
header('Content-Type: text/plain; charset=utf-8');
require_once('spyc-0.4.5/spyc.php');
$output = Spyc::YAMLDump(XMLToArray::convert($xml));
break;

case 'xml':
header('Content-Type: text/xml; charset=utf-8');
$output = $xml;
break;

case 'csv':
header('Content-Type: text/plain; charset=utf-8');

$entries = XMLToArray::convert($xml);
$entries = $entries['response']['entry'];

$file_name = sprintf('%s/%s-%d.csv', TMP, self::$_plugin_name, time());
$csv = fopen($file_name, 'w');

$columns = array();
$rows = array();

// iterate over all entries to build columns. do not assume that the
// first entry has all fields (if a value is missing the field will not be present!)
foreach($entries as $entry) {
foreach($entry as $handle => $value) {
if(!in_array($handle, $columns)) $columns[] = $handle;
}
}

fputcsv($csv, $columns, ',', '"');

foreach($entries as $entry) {
$row = array();
// build the data for each field in this entry
Expand All @@ -149,26 +149,26 @@ public static function sendOutput($response_body=NULL, $code=200) {
}
fputcsv($csv, $row, ',', '"');
}

fclose($csv);
$output = file_get_contents($file_name);
unlink($file_name);

break;
}

echo $output;
exit;
}
public static function sendError($message=NULL, $code=200, $format=NULL) {

public static function sendError($message = null, $code = 200, $format = null) {
if($format) self::$_output_type = $format;
$response = new XMLElement('response', NULL, array(
$response = new XMLElement('response', null, array(
'result' => 'error',
'code' => $code
));
$response->appendChild(new XMLElement('error', $message));
self::sendOutput($response, $code);
}
}

}
Loading