|
| 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(); |
0 commit comments