Jon Jon - 1 month ago 5
PHP Question

Using custom PHP module, why isn't $_POST being populated?

I have compiled

PHP 5.6.27
on
macOS 10.12.1 Sierra
as a static library (no small feat in and of itself). I have linked my app to all required libraries and it is building without error. Using the static PHP library, I am able to both execute arbitrary PHP code and execute PHP scripts. However, if I try to process a script using form data (retrieved from an embedded HTTP server in the same app), the
$_POST
global is never populated (the
$_GET
global is set just fine if the request is a
GET
with a query string in the URL).

Without including the whole project, here is my PHP processor class with my custom PHP module and a stub for testing the processor without an actual HTTP request but with the same content a request would generate that my class requires. You can see at the bottom, in the truncated results, the PHP script is run and the read_post method is being called and properly loading the supplied buffer that PHP should then use to populate the
$_POST
array but the
$_POST
array remains empty (though initialized). Any hints would be most appreciated.

PHPProcessor.h:

#import <Cocoa/Cocoa.h>

@interface PHPProcessor : NSObject
@property (strong) NSString *requestMethod;
@property (strong) NSURL *requestURL;
@property (strong) NSString *requestContentType;
@property (strong) NSData *bodyData;
@property (readonly) NSString *resultString;
+ (instancetype)phpProcessor;
- (BOOL)executeScript:(NSString *)filepath;
@end


PHPProcessor.m:

#import "PHPProcessor.h"
#include "php.h"
#include "php_main.h"
#include "php_variables.h"

@interface PHPProcessor ()
@property (assign) NSInteger bodyDataOffset;
@property (strong) NSString *resultString;
@end

static int phpp_startup(sapi_module_struct *sapi_module) {
return ((php_module_startup(sapi_module, NULL, 0) == FAILURE) ? FAILURE : SUCCESS);
}

static int phpp_shutdown(sapi_module_struct *sapi_module) {
return ((php_module_shutdown_wrapper(sapi_module) == FAILURE) ? FAILURE : SUCCESS);
}

static int phpp_ub_write(const char *str, unsigned int str_length TSRMLS_DC) {
PHPProcessor *phpProcessor = (__bridge PHPProcessor *)SG(server_context);
NSString *newString = [NSString stringWithUTF8String:str];
if (!newString) newString = [NSString stringWithCString:str encoding:NSASCIIStringEncoding];
if (newString) {
NSString *resultString = phpProcessor.resultString;
if (!resultString) resultString = [NSString string];
resultString = [resultString stringByAppendingString:newString];
phpProcessor.resultString = resultString;
return str_length;
}
return 0;
}

static int phpp_send_headers(sapi_headers_struct *sapi_headers TSRMLS_DC) {
return SAPI_HEADER_SENT_SUCCESSFULLY;
}

static int phpp_read_post(char *buffer, uint count_bytes TSRMLS_DC) {
PHPProcessor *phpProcessor = (__bridge PHPProcessor *)SG(server_context);
NSData *bodyData = phpProcessor.bodyData;
uint remaining_bytes = (uint)(bodyData.length - phpProcessor.bodyDataOffset);
count_bytes = MIN(count_bytes, remaining_bytes);
[bodyData getBytes:buffer range:NSMakeRange(phpProcessor.bodyDataOffset, count_bytes)];
phpProcessor.bodyDataOffset += count_bytes;
#if DEBUG
NSLog(@"verifying buffer from read_post: %@", [[NSString alloc] initWithBytes:buffer length:phpProcessor.bodyDataOffset encoding:NSASCIIStringEncoding]);
#endif
return count_bytes;
}

static void phpp_register_variable(int arg, zval *track_vars_array, const char *key, const char *val TSRMLS_DC) {
char *new_val = (char *)val; uint new_val_len;
if (val && sapi_module.input_filter(arg, (char *)key, &new_val, (unsigned int)strlen(val), &new_val_len TSRMLS_CC)) php_register_variable_safe((char *)key, new_val, new_val_len, track_vars_array TSRMLS_CC);
}

static void phpp_register_server_variables(zval *track_vars_array TSRMLS_DC) {
phpp_register_variable(PARSE_SERVER, track_vars_array, "SERVER_SOFTWARE", [[[NSBundle mainBundle] objectForInfoDictionaryKey:(id)kCFBundleNameKey] UTF8String]);
}

static void phpp_log_message(char *message TSRMLS_DC) {
printf("%s\n", message);
}

sapi_module_struct phpp_module = {
"phpp", // char *name;
"PHP Processor Module", // char *pretty_name;
phpp_startup, // int (*startup)(struct _sapi_module_struct *sapi_module);
phpp_shutdown, // int (*shutdown)(struct _sapi_module_struct *sapi_module);
NULL, // int (*activate)(TSRMLS_D);
NULL, // int (*deactivate)(TSRMLS_D);
phpp_ub_write, // int (*ub_write)(const char *str, unsigned int str_length TSRMLS_DC);
NULL, // void (*flush)(void *server_context);
NULL, // struct stat *(*get_stat)(TSRMLS_D);
NULL, // char *(*getenv)(char *name, size_t name_len TSRMLS_DC);
NULL, // void (*sapi_error)(int type, const char *error_msg, ...);
NULL, // int (*header_handler)(sapi_header_struct *sapi_header, sapi_header_op_enum op, sapi_headers_struct *sapi_headers TSRMLS_DC);
phpp_send_headers, // int (*send_headers)(sapi_headers_struct *sapi_headers TSRMLS_DC);
NULL, // void (*send_header)(sapi_header_struct *sapi_header, void *server_context TSRMLS_DC);
phpp_read_post, // int (*read_post)(char *buffer, uint count_bytes TSRMLS_DC);
NULL, // char *(*read_cookies)(TSRMLS_D);
phpp_register_server_variables, // void (*register_server_variables)(zval *track_vars_array TSRMLS_DC);
phpp_log_message, // void (*log_message)(char *message TSRMLS_DC);
NULL, // double (*get_request_time)(TSRMLS_D);
NULL, // void (*terminate_process)(TSRMLS_D);
"", // char *php_ini_path_override; // "" causes $_ENV to be set
STANDARD_SAPI_MODULE_PROPERTIES
};

@implementation PHPProcessor

+ (void)initialize {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sapi_module_struct *sapi_module = &phpp_module;
sapi_startup(sapi_module);
sapi_module->startup(sapi_module);
});
}

+ (instancetype)phpProcessor { return [self new]; }

- (instancetype)init {
if (self = [super init]) {
}
return self;
}

- (BOOL)executeScript:(NSString *)filepath {
self.resultString = nil;
self.bodyDataOffset = 0;
SG(request_info).request_method = (char *)self.requestMethod.UTF8String;
SG(request_info).content_type = (char *)self.requestContentType.UTF8String;
SG(request_info).content_length = (long)self.bodyData.length;
SG(request_info).request_uri = (char *)self.requestURL.relativePath.UTF8String;
SG(request_info).path_translated = (char *)filepath.UTF8String;
SG(request_info).query_string = (char *)self.requestURL.query.UTF8String;
if (php_request_startup(TSRMLS_C) == SUCCESS) {
SG(server_context) = (void *)CFBridgingRetain(self);
zend_file_handle file_handle;
file_handle.type = ZEND_HANDLE_FILENAME;
file_handle.filename = filepath.UTF8String;
file_handle.free_filename = 0;
file_handle.opened_path = NULL;
php_execute_script(&file_handle TSRMLS_CC);
php_request_shutdown(NULL);
SG(server_context) = NULL;
CFRelease(self);
return YES;
}
return NO;
}

@end


test.php:

<?php
echo("<pre>\n");
echo("GET: ");
print_r($_GET);
echo("POST: ");
print_r($_POST);
echo("SERVER: ");
print_r($_SERVER);
echo("ENV: ");
print_r($_ENV);
echo("</pre>\n");
phpinfo();
?>


Code to use PHPProcessor:

NSString *filepath = @"~/Desktop/test.php".stringByExpandingTildeInPath;
PHPProcessor *phpProcessor = [PHPProcessor phpProcessor];
phpProcessor.requestMethod = @"POST";
phpProcessor.requestURL = [NSURL fileURLWithPath:filepath];
phpProcessor.requestContentType = @"application/x-www-form-urlencoded";
phpProcessor.bodyData = [@"a=123&b=456" dataUsingEncoding:NSUTF8StringEncoding];
[phpProcessor executeScript:filepath];
NSLog(@"resultString:\n\n%@", phpProcessor.resultString);


Result:

2016-10-30 17:26:10.429404 PHPProcessor Test[38190:2886757] verifying buffer from read_post: a=123&b=456
2016-10-30 17:26:10.429931 PHPProcessor Test[38190:2886757] resultString:

<pre>
GET: Array
(
)
POST: Array
(
)
SERVER: Array
(
[SERVER_SOFTWARE] => PHPProcessor Test
[REQUEST_TIME_FLOAT] => 1477862770.425
[REQUEST_TIME] => 1477862770
[argv] => Array
(
)

[argc] => 0
)
ENV: Array
(
[SHELL] => /bin/bash
...
)
</pre>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head>
...
<title>phpinfo()</title>
...
<body><div class="center">
<table>
<tr class="h"><td>
<a href="http://www.php.net/"><img border="0" src="data:image/png;base64,..." alt="PHP logo" /></a><h1 class="p">PHP Version 5.6.27</h1>
...
</table>
</div></body></html>

Jon Jon
Answer

For those interested in the solution, basically it came down to moving the sapi_module_struct startup outside the class initialization method and calling it from an instance method. I also cleaned up the rest of the code a little bit and, for some reason, to avoid a crash, I had to add a function for reading cookies even though I wasn't originally interested in them (returning a valid cookie string does set the $_COOKIE array). Hope this can help some others.

Revised PHPProcessor.m:

#import "PHPProcessor.h"
#include "php.h"
#include "php_main.h"
#include "php_variables.h"

@interface PHPProcessor ()
@property (assign) NSInteger bodyDataOffset;
@property (strong) NSMutableString *mutableResultString;
@end

static int phpp_startup(sapi_module_struct *sapi_module) {
    return ((php_module_startup(sapi_module, NULL, 0) == FAILURE) ? FAILURE : SUCCESS);
}

static int phpp_shutdown(sapi_module_struct *sapi_module) {
    return ((php_module_shutdown_wrapper(sapi_module) == FAILURE) ? FAILURE : SUCCESS);
}

static int phpp_ub_write(const char *str, unsigned int str_length TSRMLS_DC) {
    PHPProcessor *phpProcessor = (__bridge PHPProcessor *)SG(server_context);
    NSString *newString = [NSString stringWithUTF8String:str];
    if (!newString) newString = [NSString stringWithCString:str encoding:NSASCIIStringEncoding];
    if (newString) {
        [phpProcessor.mutableResultString appendString:newString];
        return str_length;
    }
    return 0;
}

static int phpp_send_headers(sapi_headers_struct *sapi_headers TSRMLS_DC) {
    return SAPI_HEADER_SENT_SUCCESSFULLY;
}

static int phpp_read_post(char *buffer, uint count_bytes TSRMLS_DC) {
    PHPProcessor *phpProcessor = (__bridge PHPProcessor *)SG(server_context);
    NSData *bodyData = phpProcessor.bodyData;
    uint remaining_bytes = (uint)(bodyData.length - phpProcessor.bodyDataOffset);
    count_bytes = MIN(count_bytes, remaining_bytes);
    [bodyData getBytes:buffer range:NSMakeRange(phpProcessor.bodyDataOffset, count_bytes)];
    phpProcessor.bodyDataOffset += count_bytes;
#if DEBUG
    NSLog(@"verifying buffer from read_post: %@", [[NSString alloc] initWithBytes:buffer length:phpProcessor.bodyDataOffset encoding:NSASCIIStringEncoding]);
#endif
    return count_bytes;
}

static char *phpp_read_cookies(TSRMLS_D) {
    return NULL;
}

static void phpp_register_variable(int arg, zval *track_vars_array, const char *key, const char *val TSRMLS_DC) {
    char *new_val = (char *)val; uint new_val_len;
    if (val && sapi_module.input_filter(arg, (char *)key, &new_val, (unsigned int)strlen(val), &new_val_len TSRMLS_CC)) php_register_variable_safe((char *)key, new_val, new_val_len, track_vars_array TSRMLS_CC);
}

static void phpp_register_server_variables(zval *track_vars_array TSRMLS_DC) {
    phpp_register_variable(PARSE_SERVER, track_vars_array, "SERVER_SOFTWARE", [[[NSBundle mainBundle] objectForInfoDictionaryKey:(id)kCFBundleNameKey] UTF8String]);
}

static void phpp_log_message(char *message TSRMLS_DC) {
    printf("%s\n", message);
}

sapi_module_struct phpp_module = {
    "phpp",                     // char *name;
    "PHP Processor Module",         // char *pretty_name;
    phpp_startup,                   // int (*startup)(struct _sapi_module_struct *sapi_module);
    phpp_shutdown,                  // int (*shutdown)(struct _sapi_module_struct *sapi_module);
    NULL,                       // int (*activate)(TSRMLS_D);
    NULL,                       // int (*deactivate)(TSRMLS_D);
    phpp_ub_write,                  // int (*ub_write)(const char *str, unsigned int str_length TSRMLS_DC);
    NULL,                       // void (*flush)(void *server_context);
    NULL,                       // struct stat *(*get_stat)(TSRMLS_D);
    NULL,                       // char *(*getenv)(char *name, size_t name_len TSRMLS_DC);
    php_error,                  // void (*sapi_error)(int type, const char *error_msg, ...);
    NULL,                       // int (*header_handler)(sapi_header_struct *sapi_header, sapi_header_op_enum op, sapi_headers_struct *sapi_headers TSRMLS_DC);
    phpp_send_headers,              // int (*send_headers)(sapi_headers_struct *sapi_headers TSRMLS_DC);
    NULL,                       // void (*send_header)(sapi_header_struct *sapi_header, void *server_context TSRMLS_DC);
    phpp_read_post,             // int (*read_post)(char *buffer, uint count_bytes TSRMLS_DC);
    phpp_read_cookies,              // char *(*read_cookies)(TSRMLS_D);
    phpp_register_server_variables, // void (*register_server_variables)(zval *track_vars_array TSRMLS_DC);
    phpp_log_message,               // void (*log_message)(char *message TSRMLS_DC);
    NULL,                       // double (*get_request_time)(TSRMLS_D);
    NULL,                       // void (*terminate_process)(TSRMLS_D);
    "",                         // char *php_ini_path_override; // "" causes $_ENV to be set
    STANDARD_SAPI_MODULE_PROPERTIES
};

@implementation PHPProcessor

+ (void)initialize {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sapi_startup(&phpp_module);
    });
}

+ (instancetype)phpProcessor { return [self new]; }

- (instancetype)init {
    if (self = [super init]) {
    }
    return self;
}

- (void)shutdownModule {
    SG(server_context) = NULL;
    CFRelease(self);
    php_module_shutdown(TSRMLS_C);
}

- (BOOL)executeScript:(NSString *)filepath {
    self.bodyDataOffset = 0;
    self.mutableResultString = [NSMutableString string];
    if (phpp_module.startup(&phpp_module) == FAILURE) return NO;
    int exit_status = FAILURE;
    zend_first_try {
        SG(server_context) = (void *)CFBridgingRetain(self);
        SG(request_info).request_method = (char *)self.requestMethod.UTF8String;
        SG(request_info).content_type = (char *)self.requestContentType.UTF8String;
        SG(request_info).content_length = (long)self.bodyData.length;
        SG(request_info).request_uri = (char *)self.requestURL.relativePath.UTF8String;
        SG(request_info).path_translated = (char *)filepath.fileSystemRepresentation;
        SG(request_info).query_string = (char *)self.requestURL.query.UTF8String;
        char *argv[2] = {phpp_module.name, NULL};
        SG(request_info).argc = 1;
        SG(request_info).argv = argv;
        SG(request_info).argv0 = phpp_module.name;
        SG(sapi_headers).http_response_code = 200;
        if (php_request_startup(TSRMLS_C) == FAILURE) {
            [self shutdownModule];
            return NO;
        }
        if (SG(request_info).path_translated) {
            zend_file_handle file_handle;
            file_handle.type = ZEND_HANDLE_FILENAME;
            file_handle.filename = SG(request_info).path_translated;
            file_handle.free_filename = 0;
            file_handle.opened_path = NULL;
            if (php_fopen_primary_script(&file_handle TSRMLS_CC) == FAILURE) {
                zend_try {
                    if (errno == EACCES) {
                        SG(sapi_headers).http_response_code = 403;
                        self.mutableResultString = [NSMutableString stringWithString:@"Access denied."];
                    } else {
                        SG(sapi_headers).http_response_code = 404;
                        self.mutableResultString = [NSMutableString stringWithString:@"No input file specified."];
                    }
                } zend_catch {
                } zend_end_try();
                STR_FREE(SG(request_info).path_translated);
                php_request_shutdown((void *)0);
                [self shutdownModule];
                return NO;
            }
            php_execute_script(&file_handle TSRMLS_CC);
            exit_status = SUCCESS;
        }
    } zend_catch {
    } zend_end_try();
    [self shutdownModule];
    return (exit_status == SUCCESS);
}

- (NSString *)resultString { return [NSString stringWithString:self.mutableResultString]; }

@end

Result:

2016-11-04 20:05:48.908176 PHPProcessor Test[36988:2522597] verifying buffer from read_post: a=123&b=456
2016-11-04 20:05:48.913119 PHPProcessor Test[36988:2522597] resultString:

<pre>
GET: Array
(
)
POST: Array
(
    [a] => 123
    [b] => 456
)
REQUEST: Array
(
    [a] => 123
    [b] => 456
)
COOKIE: Array
(
)
SERVER: Array
(
    [SERVER_SOFTWARE] => PHPProcessor Test
    [REQUEST_TIME_FLOAT] => 1478304348.9085
    [REQUEST_TIME] => 1478304348
    [argv] => Array
        (
            [0] => phpp
        )

    [argc] => 1
)
ENV: Array
(
    [SHELL] => /bin/bash
    ...
)
</pre>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head>
...
<title>phpinfo()</title>
...
<body><div class="center">
<table>
<tr class="h"><td>
<a href="http://www.php.net/"><img border="0" src="data:image/png;base64,..." alt="PHP logo" /></a><h1 class="p">PHP Version 5.6.27</h1>
...
</table>
</div></body></html>
Comments