Skip to content

Commit

Permalink
Merge restapi-core
Browse files Browse the repository at this point in the history
  • Loading branch information
michield committed Nov 3, 2015
1 parent ffd4ed6 commit fb2d8d0
Show file tree
Hide file tree
Showing 40 changed files with 8,555 additions and 0 deletions.
662 changes: 662 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

60 changes: 60 additions & 0 deletions restapi2.php
@@ -0,0 +1,60 @@
<?php
/**
* Plugin that implements a REST API
*
* Documentation: http://resources.phplist.com/plugin/restapi
*
* version history:
*
* v 2 * phpList Api Team https://github.com/orgs/phpList/teams/api
* - renamed plugin repository to phplist-plugin-restapi
* - https://github.com/phpList/phplist-plugin-restapi
*
* v 1 * Andreas Ek, 2012-12-26
* https://github.com/EkAndreas/phplistapi
*/
defined( 'PHPLISTINIT' ) || die;

class restapi extends phplistPlugin {

// Set plugin name presented in admin pages
public $name = 'RESTAPI';
// Description of the app as displayed in admin pages
public $description = 'Implements a REST API interface to phpList';

function restapi() {
parent::phplistplugin();
// Set path to plugin folder
$this->coderoot = dirname( __FILE__ ) . '/restapi/';
}

// Set header nav link label, url, and category
public $topMenuLinks = array(
// Array key determines both label of admin menu item, & php file name
// of page
'main' => array( 'category' => 'system' ),
);

// Set dashboard link label and url
function adminmenu() {
return array(
// Array key determines link URL in dashboard; value sets link label
'main' => 'RESTAPI'
);
}

// Add settings to admin interface
// Note: stock text, ready for editing / customisation
// public $settings = array(
// "myplugin_setting1" => array (
// 'value' => "some default",
// 'description' => 'Description of this setting',
// 'type' => "text",
// 'allowempty' => 0,
// "max" => 1000,
// "min" => 0,
// 'category'=> 'general',
// ),
// );

}
1 change: 1 addition & 0 deletions restapi2/.gitignore
@@ -0,0 +1 @@
/vendor/
110 changes: 110 additions & 0 deletions restapi2/call.php
@@ -0,0 +1,110 @@
<?php

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;

require_once 'vendor/autoload.php';

// Disable HTML output as HTML cannot be easily read during HTTP POST testing
ini_set( 'html_errors', 0 );
// Disable xdebug HTML output
if ( function_exists( 'xdebug_disable' ) ) {
xdebug_disable();
}

// Check that the plugin has been initiatlised
defined( 'PHPLISTINIT' ) || die;

// No HTML-output, please!
ob_end_clean();

// Getting phpList globals for this plugin
$plugin = $GLOBALS['plugins'][$_GET['pi']];

// Create Symfony DI service container object for use by other classes
$container = new ContainerBuilder();
// Create new Symfony file loader to handle the YAML service config file
$loader = new YamlFileLoader( $container, new FileLocator( __DIR__ ) );
// Load the service config file, which is in YAML format
$loader->load( 'services.yml' );

// Set default path to host phpList instance config file
// NOTE: This config file must be in phpList 4 ini format
// NOTE: Parent phpList 3 config file path available via: $GLOBALS['configfile']
$configFilePath = dirname( __FILE__ ) . '/config-phplist4.php';

// Set necessary config class parameter
$container->setParameter( 'config.configfile', $configFilePath );
// Set service parameters for the RAPI database connection
// NOTE: phpList4 database connection configured elsewhere
// These service parameters will be used as constructor arguments for pdoEx{}
$container->setParameter( 'pdoEx.hostname', $GLOBALS['database_host'] );
$container->setParameter( 'pdoEx.username', $GLOBALS['database_user'] );
$container->setParameter( 'pdoEx.pass', $GLOBALS['database_password'] );
$container->setParameter( 'pdoEx.dbname', $GLOBALS['database_name'] );

// Get a phpList4 configuration object so we can configure the database
$pl4Config = $container->get( 'Config' );

// Load phpList 4 configuration into session, taken from host globals
require_once( 'phplist4-bootstrap.php');

if ( function_exists( 'api_request_log' ) )
{
api_request_log();
}

$call = $container->get( 'Call' );
$response = $container->get( 'Response' );

// Check if this is called outside phpList auth, this should never occur!
if ( empty( $plugin->coderoot ) )
{
$response->outputErrorMessage( 'Not authorized! Please login with [login] and [password] as admin first!' );
}

// Check if the request received was via HTTP post
if ( $_SERVER['REQUEST_METHOD'] != "POST" ) {
$response->outputErrorMessage( 'Requests must be made via HTTP POST. Method of this call: ' . $_SERVER['REQUEST_METHOD'] );
}

// NOTE: Login authentication is handled by the main phpList application. HTTP
// POST parameters 'login' and 'password' are required to validate login, else
// an HTML login form will be returned.

// Check if a command was specified
if (
empty( $_REQUEST['className'] )
|| empty( $_REQUEST['method'] )
) {
$response->outputErrorMessage( 'No action requested: specify commands via parameters \'className\' and \'method\'' );
} else {
// Set command for use later
$className = $_REQUEST['className'];
$method = $_REQUEST['method'];
}

// Check the command is callable
if ( ! $call->validateCall( $className, $method ) ) {
// Add error message if not callable
$response->outputErrorMessage( 'Requested command is not callable' );
}

try {
// Execute the requested call
$callResult = $call->doCall( $className, $method, $_POST );
} catch ( \Exception $e ) {
// If call handler encounters error, turn it into a response
$response->outputErrorMessage( 'Call handler error: ' . $e->getMessage() );
}

// Format call output for making a response
$resultArray = $call->callResultToArray( $callResult );

// Save output to response
$response->setData( 'foo', $resultArray );

// Output the response
$response->output();
21 changes: 21 additions & 0 deletions restapi2/composer.json
@@ -0,0 +1,21 @@
{
"require": {
"symfony/dependency-injection": "~2.6"
, "symfony/yaml": "~2.6"
, "symfony/config": "~2.6"
, "phplist/phplist4-core": "dev-master"
}
, "require-dev": {
"symfony/debug": "2.5.*"
, "phpunit/phpunit": "4.2.*"
, "phpunit/dbunit": "1.3.*"
, "phpunit/phpunit-selenium": "*"
, "facebook/webdriver": "dev-master"
, "phpdocumentor/phpdocumentor": "2.*"
}
, "autoload": {
"psr-4": {
"Rapi\\": "lib/"
}
}
}

0 comments on commit fb2d8d0

Please sign in to comment.