Skip to content

Commit fb2d8d0

Browse files
committed
Merge restapi-core
1 parent ffd4ed6 commit fb2d8d0

40 files changed

+8555
-0
lines changed

LICENSE

+662
Large diffs are not rendered by default.

restapi2.php

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
/**
3+
* Plugin that implements a REST API
4+
*
5+
* Documentation: http://resources.phplist.com/plugin/restapi
6+
*
7+
* version history:
8+
*
9+
* v 2 * phpList Api Team https://github.com/orgs/phpList/teams/api
10+
* - renamed plugin repository to phplist-plugin-restapi
11+
* - https://github.com/phpList/phplist-plugin-restapi
12+
*
13+
* v 1 * Andreas Ek, 2012-12-26
14+
* https://github.com/EkAndreas/phplistapi
15+
*/
16+
defined( 'PHPLISTINIT' ) || die;
17+
18+
class restapi extends phplistPlugin {
19+
20+
// Set plugin name presented in admin pages
21+
public $name = 'RESTAPI';
22+
// Description of the app as displayed in admin pages
23+
public $description = 'Implements a REST API interface to phpList';
24+
25+
function restapi() {
26+
parent::phplistplugin();
27+
// Set path to plugin folder
28+
$this->coderoot = dirname( __FILE__ ) . '/restapi/';
29+
}
30+
31+
// Set header nav link label, url, and category
32+
public $topMenuLinks = array(
33+
// Array key determines both label of admin menu item, & php file name
34+
// of page
35+
'main' => array( 'category' => 'system' ),
36+
);
37+
38+
// Set dashboard link label and url
39+
function adminmenu() {
40+
return array(
41+
// Array key determines link URL in dashboard; value sets link label
42+
'main' => 'RESTAPI'
43+
);
44+
}
45+
46+
// Add settings to admin interface
47+
// Note: stock text, ready for editing / customisation
48+
// public $settings = array(
49+
// "myplugin_setting1" => array (
50+
// 'value' => "some default",
51+
// 'description' => 'Description of this setting',
52+
// 'type' => "text",
53+
// 'allowempty' => 0,
54+
// "max" => 1000,
55+
// "min" => 0,
56+
// 'category'=> 'general',
57+
// ),
58+
// );
59+
60+
}

restapi2/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/vendor/

restapi2/call.php

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<?php
2+
3+
use Symfony\Component\DependencyInjection\ContainerBuilder;
4+
use Symfony\Component\DependencyInjection\Reference;
5+
use Symfony\Component\Config\FileLocator;
6+
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
7+
8+
require_once 'vendor/autoload.php';
9+
10+
// Disable HTML output as HTML cannot be easily read during HTTP POST testing
11+
ini_set( 'html_errors', 0 );
12+
// Disable xdebug HTML output
13+
if ( function_exists( 'xdebug_disable' ) ) {
14+
xdebug_disable();
15+
}
16+
17+
// Check that the plugin has been initiatlised
18+
defined( 'PHPLISTINIT' ) || die;
19+
20+
// No HTML-output, please!
21+
ob_end_clean();
22+
23+
// Getting phpList globals for this plugin
24+
$plugin = $GLOBALS['plugins'][$_GET['pi']];
25+
26+
// Create Symfony DI service container object for use by other classes
27+
$container = new ContainerBuilder();
28+
// Create new Symfony file loader to handle the YAML service config file
29+
$loader = new YamlFileLoader( $container, new FileLocator( __DIR__ ) );
30+
// Load the service config file, which is in YAML format
31+
$loader->load( 'services.yml' );
32+
33+
// Set default path to host phpList instance config file
34+
// NOTE: This config file must be in phpList 4 ini format
35+
// NOTE: Parent phpList 3 config file path available via: $GLOBALS['configfile']
36+
$configFilePath = dirname( __FILE__ ) . '/config-phplist4.php';
37+
38+
// Set necessary config class parameter
39+
$container->setParameter( 'config.configfile', $configFilePath );
40+
// Set service parameters for the RAPI database connection
41+
// NOTE: phpList4 database connection configured elsewhere
42+
// These service parameters will be used as constructor arguments for pdoEx{}
43+
$container->setParameter( 'pdoEx.hostname', $GLOBALS['database_host'] );
44+
$container->setParameter( 'pdoEx.username', $GLOBALS['database_user'] );
45+
$container->setParameter( 'pdoEx.pass', $GLOBALS['database_password'] );
46+
$container->setParameter( 'pdoEx.dbname', $GLOBALS['database_name'] );
47+
48+
// Get a phpList4 configuration object so we can configure the database
49+
$pl4Config = $container->get( 'Config' );
50+
51+
// Load phpList 4 configuration into session, taken from host globals
52+
require_once( 'phplist4-bootstrap.php');
53+
54+
if ( function_exists( 'api_request_log' ) )
55+
{
56+
api_request_log();
57+
}
58+
59+
$call = $container->get( 'Call' );
60+
$response = $container->get( 'Response' );
61+
62+
// Check if this is called outside phpList auth, this should never occur!
63+
if ( empty( $plugin->coderoot ) )
64+
{
65+
$response->outputErrorMessage( 'Not authorized! Please login with [login] and [password] as admin first!' );
66+
}
67+
68+
// Check if the request received was via HTTP post
69+
if ( $_SERVER['REQUEST_METHOD'] != "POST" ) {
70+
$response->outputErrorMessage( 'Requests must be made via HTTP POST. Method of this call: ' . $_SERVER['REQUEST_METHOD'] );
71+
}
72+
73+
// NOTE: Login authentication is handled by the main phpList application. HTTP
74+
// POST parameters 'login' and 'password' are required to validate login, else
75+
// an HTML login form will be returned.
76+
77+
// Check if a command was specified
78+
if (
79+
empty( $_REQUEST['className'] )
80+
|| empty( $_REQUEST['method'] )
81+
) {
82+
$response->outputErrorMessage( 'No action requested: specify commands via parameters \'className\' and \'method\'' );
83+
} else {
84+
// Set command for use later
85+
$className = $_REQUEST['className'];
86+
$method = $_REQUEST['method'];
87+
}
88+
89+
// Check the command is callable
90+
if ( ! $call->validateCall( $className, $method ) ) {
91+
// Add error message if not callable
92+
$response->outputErrorMessage( 'Requested command is not callable' );
93+
}
94+
95+
try {
96+
// Execute the requested call
97+
$callResult = $call->doCall( $className, $method, $_POST );
98+
} catch ( \Exception $e ) {
99+
// If call handler encounters error, turn it into a response
100+
$response->outputErrorMessage( 'Call handler error: ' . $e->getMessage() );
101+
}
102+
103+
// Format call output for making a response
104+
$resultArray = $call->callResultToArray( $callResult );
105+
106+
// Save output to response
107+
$response->setData( 'foo', $resultArray );
108+
109+
// Output the response
110+
$response->output();

restapi2/composer.json

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"require": {
3+
"symfony/dependency-injection": "~2.6"
4+
, "symfony/yaml": "~2.6"
5+
, "symfony/config": "~2.6"
6+
, "phplist/phplist4-core": "dev-master"
7+
}
8+
, "require-dev": {
9+
"symfony/debug": "2.5.*"
10+
, "phpunit/phpunit": "4.2.*"
11+
, "phpunit/dbunit": "1.3.*"
12+
, "phpunit/phpunit-selenium": "*"
13+
, "facebook/webdriver": "dev-master"
14+
, "phpdocumentor/phpdocumentor": "2.*"
15+
}
16+
, "autoload": {
17+
"psr-4": {
18+
"Rapi\\": "lib/"
19+
}
20+
}
21+
}

0 commit comments

Comments
 (0)