// $Id$ #include "PHPIncludes.h" #include "PHP.h" #include "ztsthread.h" #import #import #import #include #include #include #include "NSString+PHP.h" static char *php_ogo_read_cookies(); static int php_ogo_deactivate(); static int php_ogo_ub_write(const char *, uint); static int php_ogo_header_handler(sapi_header_struct *, sapi_headers_struct *); static int php_ogo_send_headers(sapi_headers_struct *); static void php_ogo_log_message(char *); static void php_ogo_register_variables(zval *); static int php_ogo_startup(sapi_module_struct *); static void php_register_variable_string(char *, NSString *, pval *); static int php_ogo_read_post(char *buffer, uint count_bytes TSRMLS_DC); sapi_module_struct php_ogo_sapi = { "ogo", /* name */ "OpenGroupware SAPI", /* pretty name */ php_ogo_startup, /* startup */ php_module_shutdown_wrapper, /* shutdown */ NULL, /* activate */ php_ogo_deactivate, /* deactivate */ php_ogo_ub_write, /* unbuffered write */ NULL, /* flush */ NULL, /* get uid */ NULL, /* getenv */ php_error, /* error handler */ php_ogo_header_handler, /* header handler */ php_ogo_send_headers, /* send headers handler */ NULL, /* send header handler */ php_ogo_read_post, /* read POST data */ php_ogo_read_cookies, /* read Cookies */ php_ogo_register_variables, /* register server variables */ php_ogo_log_message, /* Log message */ NULL, /* Block interruptions */ NULL, /* Unblock interruptions */ STANDARD_SAPI_MODULE_PROPERTIES }; static char* php_ogo_read_cookies(TSRMLS_D) { return NULL; } static int php_ogo_deactivate(TSRMLS_D) { return SUCCESS; } static int php_ogo_ub_write(const char *str, uint len TSRMLS_DC) { NSData *d; WOResponse *r; r = [(WOContext *) SG(server_context) response]; d = [NSData dataWithBytes:str length:len]; [r appendContentData:d]; return len; } static int php_ogo_send_headers(sapi_headers_struct *sapi_headers TSRMLS_DC) { return SAPI_HEADER_SENT_SUCCESSFULLY; } static int php_ogo_header_handler(sapi_header_struct *sapi_header, sapi_headers_struct *sapi_headers TSRMLS_DC) { NSString *headerString, *keyString, *valueString; WOResponse *r; NSRange sep; r = [(WOContext *) SG(server_context) response]; headerString = [NSString stringWithCString:sapi_header->header length:sapi_header->header_len]; sep = [headerString rangeOfString:@": "]; keyString = [headerString substringToIndex:sep.location]; valueString = [headerString substringFromIndex:sep.location+2]; if(sapi_header->replace) [r setHeader:valueString forKey:keyString]; else [r appendHeader:valueString forKey:keyString]; return 0; } static void php_ogo_log_message(char *message) { NSLog(@"PHP: %s", message); } static void php_register_variable_string(char *var, NSString *val, pval *track_vars_array TSRMLS_DC) { zval new_entry; // hh: commented out: assert(strval != NULL); Z_STRLEN(new_entry) = [val cStringLength]; if (PG(magic_quotes_gpc)) { char *str; str = [val phpString]; Z_STRVAL(new_entry) = php_addslashes(str, Z_STRLEN(new_entry), &Z_STRLEN(new_entry), 1 TSRMLS_CC); } else Z_STRVAL(new_entry) = [val phpString]; Z_TYPE(new_entry) = IS_STRING; php_register_variable_ex(var, &new_entry, track_vars_array TSRMLS_CC); } static void php_ogo_register_variables(zval *track_vars_array TSRMLS_DC) { WOContext *ctx; WORequest *request; WOResponse *response; NSURL *url; ctx = (WOContext *) SG(server_context); request = [ctx request]; response = [ctx response]; php_register_variable("SERVER_SOFTWARE", SERVER_SOFTWARE, track_vars_array TSRMLS_CC); php_register_variable("GATEWAY_INTERFACE", "CGI/1.1", track_vars_array TSRMLS_CC); php_register_variable("REQUEST_URI", SG(request_info).request_uri, track_vars_array TSRMLS_CC); php_register_variable("AUTH_USER", SG(request_info).auth_user, track_vars_array TSRMLS_CC); php_register_variable("REMOTE_USER", SG(request_info).auth_user, track_vars_array TSRMLS_CC); php_register_variable_string("REQUEST_METHOD", [request method], track_vars_array TSRMLS_CC); php_register_variable_string("SERVER_PROTOCOL", [response httpVersion], track_vars_array TSRMLS_CC); url = [NSURL URLWithString:[request uri] relativeToURL:[ctx serverURL]]; php_register_variable_string("SCRIPT_NAME", [url relativePath], track_vars_array TSRMLS_CC); php_register_variable_string("SERVER_NAME", [request headerForKey:@"host"], track_vars_array TSRMLS_CC); php_register_variable_string("OGO_SESSION", [[ctx session] sessionID], track_vars_array TSRMLS_CC); } static int php_ogo_startup(sapi_module_struct *sapi_module) { if (php_module_startup(sapi_module, &php_ogo_module, 1)==FAILURE) return FAILURE; return SUCCESS; } static int php_ogo_read_post(char *buffer, uint count_bytes TSRMLS_DC) { // TODO: fake implementation return 0; }