2015-10-04 18:28:44 +00:00
|
|
|
// Copyright 2015 Alexander Palaistras. All rights reserved.
|
|
|
|
// Use of this source code is governed by the MIT license that can be found in
|
|
|
|
// the LICENSE file.
|
|
|
|
|
2015-09-17 21:22:07 +00:00
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include <main/php.h>
|
|
|
|
#include <main/SAPI.h>
|
|
|
|
#include <main/php_main.h>
|
|
|
|
|
|
|
|
#include "context.h"
|
|
|
|
|
2015-10-01 23:48:33 +00:00
|
|
|
engine_context *context_new(void *parent) {
|
2015-09-17 21:22:07 +00:00
|
|
|
engine_context *context;
|
|
|
|
|
|
|
|
// Initialize context.
|
|
|
|
context = (engine_context *) malloc((sizeof(engine_context)));
|
|
|
|
if (context == NULL) {
|
2015-10-01 23:48:33 +00:00
|
|
|
errno = 1;
|
|
|
|
return NULL;
|
2015-09-17 21:22:07 +00:00
|
|
|
}
|
|
|
|
|
2015-10-01 23:48:33 +00:00
|
|
|
#ifdef ZTS
|
2015-10-02 17:12:42 +00:00
|
|
|
TSRMLS_FETCH();
|
2015-10-01 23:48:33 +00:00
|
|
|
context->ptsrm_ls = &tsrm_ls;
|
|
|
|
#endif
|
2015-09-17 21:22:07 +00:00
|
|
|
|
2015-10-01 23:48:33 +00:00
|
|
|
context->parent = parent;
|
2015-09-17 21:22:07 +00:00
|
|
|
SG(server_context) = (void *) context;
|
|
|
|
|
|
|
|
// Initialize request lifecycle.
|
|
|
|
if (php_request_startup(TSRMLS_C) == FAILURE) {
|
|
|
|
SG(server_context) = NULL;
|
|
|
|
free(context);
|
|
|
|
|
2015-10-01 23:48:33 +00:00
|
|
|
errno = 1;
|
|
|
|
return NULL;
|
2015-09-17 21:22:07 +00:00
|
|
|
}
|
|
|
|
|
2015-10-01 23:48:33 +00:00
|
|
|
errno = 0;
|
|
|
|
return context;
|
2015-09-17 21:22:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void context_exec(engine_context *context, char *filename) {
|
|
|
|
#ifdef ZTS
|
2015-10-01 23:48:33 +00:00
|
|
|
void ***tsrm_ls = *context->ptsrm_ls;
|
2015-09-17 21:22:07 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// Attempt to execute script file.
|
|
|
|
zend_first_try {
|
|
|
|
zend_file_handle script;
|
|
|
|
|
|
|
|
script.type = ZEND_HANDLE_FILENAME;
|
|
|
|
script.filename = filename;
|
|
|
|
script.opened_path = NULL;
|
|
|
|
script.free_filename = 0;
|
|
|
|
|
|
|
|
php_execute_script(&script TSRMLS_CC);
|
|
|
|
} zend_end_try();
|
|
|
|
|
2015-10-01 23:48:33 +00:00
|
|
|
errno = 0;
|
|
|
|
return NULL;
|
2015-09-17 21:22:07 +00:00
|
|
|
}
|
|
|
|
|
2015-09-20 00:16:43 +00:00
|
|
|
void context_bind(engine_context *context, char *name, void *zvalptr) {
|
|
|
|
zval *value = (zval *) zvalptr;
|
|
|
|
|
|
|
|
#ifdef ZTS
|
2015-10-01 23:48:33 +00:00
|
|
|
void ***tsrm_ls = *context->ptsrm_ls;
|
2015-09-20 00:16:43 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
ZEND_SET_SYMBOL(EG(active_symbol_table), name, value);
|
2015-09-19 20:12:32 +00:00
|
|
|
|
2015-10-01 23:48:33 +00:00
|
|
|
errno = 0;
|
|
|
|
return NULL;
|
2015-09-19 20:12:32 +00:00
|
|
|
}
|
|
|
|
|
2015-09-17 21:22:07 +00:00
|
|
|
void context_destroy(engine_context *context) {
|
|
|
|
php_request_shutdown((void *) 0);
|
|
|
|
|
|
|
|
SG(server_context) = NULL;
|
|
|
|
free(context);
|
|
|
|
}
|