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>