<commit_before>/**
 * Appcelerator Kroll - licensed under the Apache Public License 2
 * see LICENSE in the root folder for details on the license.
 * Copyright (c) 2009 Appcelerator, Inc. All Rights Reserved.
 */
#include <signal.h>
#include "php_module.h"
#include <Poco/Path.h>

#ifdef ZTS
void ***tsrm_ls;
#endif

namespace kroll
{
	KROLL_MODULE(PHPModule, STRING(MODULE_NAME), STRING(MODULE_VERSION));
	static Logger* logger = Logger::Get("PHPModule");
	const static std::string phpSuffix("module.php");
	static bool buffering = false;

	PHPModule* PHPModule::instance_ = NULL;
	std::ostringstream PHPModule::buffer;
	std::string PHPModule::mimeType("text/html");

	static int UnbufferedWrite(const char *, unsigned int TSRMLS_DC);
	static void SetIniDefault(HashTable*, const char*, const char*);
	static void IniDefaults(HashTable*);
	static void LogMessage(char*);
	static int HeaderHandler(sapi_header_struct*, sapi_header_op_enum, 
		sapi_headers_struct* TSRMLS_DC);
	static void RegisterServerVariables(zval *tracks_var_array TSRMLS_DC);

	void PHPModule::Initialize()
	{
		PHPModule::instance_ = this;
		int argc = 1;
		char *argv[2] = { "php_kroll", NULL };

		php_embed_module.ub_write = UnbufferedWrite;
		php_embed_module.log_message = LogMessage;
		php_embed_module.ini_defaults = IniDefaults;
		php_embed_module.header_handler = HeaderHandler;
		php_embed_module.register_server_variables = RegisterServerVariables;
		php_embed_module.phpinfo_as_text = 1;
		php_embed_init(argc, argv PTSRMLS_CC);

		PHPUtils::InitializePHPKrollClasses();
		this->InitializeBinding();
		host->AddModuleProvider(this);

		std::string resourcesPath(host->GetApplication()->GetResourcesPath());
		zend_alter_ini_entry("include_path", sizeof("include_path"),
			(char*) resourcesPath.c_str(), resourcesPath.size(),
			ZEND_INI_USER, ZEND_INI_STAGE_RUNTIME);
	}

	/*static*/
	void PHPModule::SetBuffering(bool newBuffering)
	{
		if (buffering)
		{
			buffer.str("");
		}
		buffering = newBuffering;
	}

	void PHPModule::Stop()
	{
		PHPModule::instance_ = NULL;

		KObjectRef global = this->host->GetGlobalObject();
		Script::GetInstance()->RemoveScriptEvaluator(this->binding);
		global->Set("PHP", Value::Undefined);
		this->binding->Set("evaluate", Value::Undefined);

		this->binding = 0;
		PHPModule::instance_ = 0;

		php_embed_shutdown(TSRMLS_C);
	}

	void PHPModule::InitializeBinding()
	{<commit_msg>Manually load some PHP ext modules on Win32.<commit_after>/**
 * Appcelerator Kroll - licensed under the Apache Public License 2
 * see LICENSE in the root folder for details on the license.
 * Copyright (c) 2009 Appcelerator, Inc. All Rights Reserved.
 */
#include <signal.h>
#include "php_module.h"
#include <Poco/Path.h>

extern "C"
{
	int php_load_extension(char *filename, int type, int start_now TSRMLS_DC);
}

#ifdef ZTS
void ***tsrm_ls;
#endif

namespace kroll
{
	KROLL_MODULE(PHPModule, STRING(MODULE_NAME), STRING(MODULE_VERSION));
	static Logger* logger = Logger::Get("PHPModule");
	const static std::string phpSuffix("module.php");
	static bool buffering = false;

	PHPModule* PHPModule::instance_ = NULL;
	std::ostringstream PHPModule::buffer;
	std::string PHPModule::mimeType("text/html");

	static int UnbufferedWrite(const char *, unsigned int TSRMLS_DC);
	static void SetIniDefault(HashTable*, const char*, const char*);
	static void IniDefaults(HashTable*);
	static void LogMessage(char*);
	static int HeaderHandler(sapi_header_struct*, sapi_header_op_enum, 
		sapi_headers_struct* TSRMLS_DC);
	static void RegisterServerVariables(zval *tracks_var_array TSRMLS_DC);

	void PHPModule::Initialize()
	{
		PHPModule::instance_ = this;
		int argc = 1;
		char *argv[2] = { "php_kroll", NULL };

		php_embed_module.ub_write = UnbufferedWrite;
		php_embed_module.log_message = LogMessage;
		php_embed_module.ini_defaults = IniDefaults;
		php_embed_module.header_handler = HeaderHandler;
		php_embed_module.register_server_variables = RegisterServerVariables;
		php_embed_module.phpinfo_as_text = 1;
		php_embed_init(argc, argv PTSRMLS_CC);

		PHPUtils::InitializePHPKrollClasses();
		this->InitializeBinding();
		host->AddModuleProvider(this);

		std::string resourcesPath(host->GetApplication()->GetResourcesPath());
		zend_alter_ini_entry("include_path", sizeof("include_path"),
			(char*) resourcesPath.c_str(), resourcesPath.size(),
			ZEND_INI_USER, ZEND_INI_STAGE_RUNTIME);

#ifdef OS_WIN32
		// Manually load some PHP extensions for Windows.
		TSRMLS_FETCH();
		std::string systemPath =
			FileUtils::Join(UTF8ToSystem(this->GetPath()).c_str(), "php_gd2.dll", 0);
		php_load_extension((char*) systemPath.c_str(), 1, 1 TSRMLS_CC);
		systemPath = FileUtils::Join(UTF8ToSystem(this->GetPath()).c_str(), "php_openssl.dll", 0);
		php_load_extension((char*) systemPath.c_str(), 1, 1 TSRMLS_CC);
		systemPath = FileUtils::Join(UTF8ToSystem(this->GetPath()).c_str(), "php_gd2.dll", 0);
		php_load_extension((char*) systemPath.c_str(), 1, 1 TSRMLS_CC);
		systemPath = FileUtils::Join(UTF8ToSystem(this->GetPath()).c_str(), "php_curl.dll", 0);
		php_load_extension((char*) systemPath.c_str(), 1, 1 TSRMLS_CC);
#endif
	}

	/*static*/
	void PHPModule::SetBuffering(bool newBuffering)
	{
		if (buffering)
		{
			buffer.str("");
		}
		buffering = newBuffering;
	}

	void PHPModule::Stop()
	{
		PHPModule::instance_ = NULL;

		KObjectRef global = this->host->GetGlobalObject();
		Script::GetInstance()->RemoveScriptEvaluator(this->binding);
		global->Set("PHP", Value::Undefined);
		this->binding->Set("evaluate", Value::Undefined);

		this->binding = 0;
		PHPModule::instance_ = 0;

		php_embed_shutdown(TSRMLS_C);
	}

	void PHPModule::InitializeBinding()
	{
{'fraction_non_alphanumeric': 0.13221535745807592, 'fraction_numerical': 0.007413945278022948, 'mean_word_length': 6.109159347553325, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4792839', 'n_tokens_mistral': 2074, 'n_tokens_neox': 1926, 'n_words': 387}