#include "php_mobideliv_api.h"
#include "php_mobideliv_api_ns.h"
#include "mobideliv.h"
#include "cfg.h"
#include "clog.h"
#include "vars.h"

#include "safestring.h"

#include <string.h>
#include <map>

#include <ext/standard/info.h>

using namespace std;

zend_object_handlers mobideliv_object_handlers;

struct mobideliv_object {
    zend_object std;
    MobiDeliv *mobideliv;
};

zend_class_entry *mobideliv_ce;

void mobideliv_free_storage(void *object TSRMLS_DC)
{
    mobideliv_object *obj = (mobideliv_object *)object;
    delete obj->mobideliv; 

    zend_hash_destroy(obj->std.properties);
    FREE_HASHTABLE(obj->std.properties);

    efree(obj);
}

zend_object_value mobideliv_create_handler(zend_class_entry *type TSRMLS_DC)
{
    zval *tmp;
    zend_object_value retval;

    mobideliv_object *obj = (mobideliv_object *)emalloc(sizeof(mobideliv_object));
    memset(obj, 0, sizeof(mobideliv_object));
    obj->std.ce = type;

    ALLOC_HASHTABLE(obj->std.properties);
    zend_hash_init(obj->std.properties, 0, NULL, ZVAL_PTR_DTOR, 0);
#if PHP_VERSION_ID < 50399
    zend_hash_copy(obj->std.properties, &type->default_properties,
        (copy_ctor_func_t)zval_add_ref, (void *)&tmp, sizeof(zval *));
#else
    object_properties_init((zend_object*) &(obj->std), type);
#endif
    retval.handle = zend_objects_store_put(obj, NULL,
        mobideliv_free_storage, NULL TSRMLS_CC);
    retval.handlers = &mobideliv_object_handlers;

    return retval;
}

#define SET_ENV( env ) \
if (zend_hash_find(Z_ARRVAL_PP(array), env, sizeof(env), (void **) &token) == SUCCESS) {\
    setenv( env, Z_STRVAL_PP(token), 1 ); \
    logfmt( logINFO, env " = %s", Z_STRVAL_PP(token) ); \
} else {\
    unsetenv( env );\
}

PHP_METHOD(MobiDeliv, __construct)
{
    MobiDeliv *mobideliv = NULL;
    zval *object = getThis();

    mobideliv = new MobiDeliv();
    mobideliv_object *obj = (mobideliv_object *)zend_object_store_get_object(object TSRMLS_CC);
    obj->mobideliv = mobideliv;

    zval **array;
    zval **token;

    zend_is_auto_global("_SERVER", sizeof("_SERVER")-1 TSRMLS_CC);
    if (zend_hash_find(&EG(symbol_table), "_SERVER", sizeof("_SERVER"), (void **) &array) == SUCCESS &&
                Z_TYPE_PP(array) == IS_ARRAY) {
	logfmt( logINFO, "_SERVER found" );
	SET_ENV( "REMOTE_ADDR" );
	SET_ENV( "SERVER_NAME" );
	SET_ENV( "HTTP_COOKIE" );
	SET_ENV( "QUERY_STRING" );
	SET_ENV( "HTTP_X_FORWARDED_FOR" );
    } else {
	logfmt( logINFO, "_SERVER not found" );
    }
}
PHP_METHOD(MobiDeliv, mobideliv_api)
{
    zval *param, *param_out;
    CVars input, output;
    MobiDeliv *mobideliv;
    mobideliv_object *obj = (mobideliv_object *)zend_object_store_get_object(
	getThis() TSRMLS_CC);
    mobideliv = obj->mobideliv;
    if (mobideliv != NULL) {
	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, const_cast<char *>("zz"), &param, &param_out ) == FAILURE ) {
	    // Bad parameters
	    RETURN_NULL();
	}
	if (!PZVAL_IS_REF(param_out)) {
	    // out is not a reference
            /* This is not relevant in PHP 5.4 */
#if PHP_VERSION_ID < 50399
	    zend_error(E_WARNING, "Second parameter wasn't passed by reference");
	    RETURN_NULL();
#endif
	}
	HashTable *hash; 
	char *key;
	uint key_len;
	zval **value;
	
	convert_to_array_ex( &param );
	hash = HASH_OF( param );
	zend_hash_internal_pointer_reset( hash );
	
	while ( zend_hash_get_current_key( hash, &key, /*num_index*/ NULL, /*duplicate*/ false ) == HASH_KEY_IS_STRING ) {
	    zend_hash_get_current_data( hash, (void**)&value );
	    convert_to_string_ex( value );
	    input[ key ] = Z_STRVAL_PP(value);
	    zend_hash_move_forward( hash );
	}
	
	long retval = mobideliv->mobideliv_api(input, output);
	
	convert_to_array_ex( &param_out ); 
	
	CVars::iterator i; 
	int j = 0;
	const int out_size = output.size(); // 200
        const int out_value_size = 200;
	char out_key[out_value_size], out_value[out_value_size];
	for ( i=output.begin(); i != output.end() && j<out_size ; i++ ) {
            bzero(out_key, out_value_size);
            bzero(out_value, out_value_size);
	    j++;
	    logfmt( logINFO, "RET: %s=%s", i->first.c_str(), i->second.c_str() );
	    strsafecpy( out_key, i->first.c_str(), out_value_size );
	    strsafecpy( out_value, i->second.c_str(), out_value_size );
	    add_assoc_string( param_out, out_key, out_value, true );
	}
	
	RETURN_LONG(retval);
    }
    RETURN_NULL();
}
PHP_METHOD(MobiDeliv, cfgReadChar)
{
    MobiDeliv *mobideliv;
    mobideliv_object *obj = (mobideliv_object *)zend_object_store_get_object(
        getThis() TSRMLS_CC);
    mobideliv = obj->mobideliv;
    if (mobideliv != NULL) {
	char *parameter;
	int parameter_len;
	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, const_cast<char *>("s"), &parameter, &parameter_len) == FAILURE) {
	    RETURN_NULL();
	}
	if (parameter_len == 0) {
	    RETURN_NULL();
	}
	RETURN_STRING(mobideliv->cfgReadChar(parameter),1);
    }
    RETURN_NULL();
}
PHP_METHOD(MobiDeliv, cfgReadLong)
{
    MobiDeliv *mobideliv;
    mobideliv_object *obj = (mobideliv_object *)zend_object_store_get_object(
        getThis() TSRMLS_CC);
    mobideliv = obj->mobideliv;
    if (mobideliv != NULL) {
	char *parameter;
	int parameter_len;
	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, const_cast<char *>("s"), &parameter, &parameter_len) == FAILURE) {
	    RETURN_NULL();
	}
	if (parameter_len == 0) {
	    RETURN_NULL();
	}
	RETURN_LONG(mobideliv->cfgReadLong(parameter));
    }
    RETURN_NULL();
}
PHP_METHOD(MobiDeliv, logMessage)
{
    MobiDeliv *mobideliv;
    mobideliv_object *obj = (mobideliv_object *)zend_object_store_get_object(
        getThis() TSRMLS_CC);
    mobideliv = obj->mobideliv;
    if (mobideliv != NULL) {
	char *message;
	int message_len;
	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, const_cast<char *>("s"), &message, &message_len) == FAILURE) {
	    RETURN_FALSE;
	}
	if (message_len == 0) {
	    RETURN_FALSE;
	}
	mobideliv->logMessage((const char *)message);
    }
    RETURN_TRUE;
}
#if PHP_VERSION_ID < 50399
function_entry mobideliv_methods[] = {
#else
zend_function_entry mobideliv_methods[] = {
#endif
    PHP_ME(MobiDeliv,  __construct,     NULL, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
    PHP_ME(MobiDeliv,  mobideliv_api,         NULL, ZEND_ACC_PUBLIC)
    PHP_ME(MobiDeliv,  cfgReadChar,     NULL, ZEND_ACC_PUBLIC)
    PHP_ME(MobiDeliv,  cfgReadLong,     NULL, ZEND_ACC_PUBLIC)
    PHP_ME(MobiDeliv,  logMessage,      NULL, ZEND_ACC_PUBLIC)
    {NULL, NULL, NULL}
};

PHP_MINIT_FUNCTION(mobideliv_api)
{
    zend_class_entry ce;
    INIT_NS_CLASS_ENTRY(ce, MOBIDELIV_API_NS, "MobiDeliv", mobideliv_methods); 
    //INIT_CLASS_ENTRY(ce, "MobiDeliv", mobideliv_methods);
    mobideliv_ce = zend_register_internal_class(&ce TSRMLS_CC);
    mobideliv_ce->create_object = mobideliv_create_handler;
    memcpy(&mobideliv_object_handlers,
        zend_get_std_object_handlers(), sizeof(zend_object_handlers));
    mobideliv_object_handlers.clone_obj = NULL;
    return SUCCESS;
}

PHP_MINFO_FUNCTION(mobideliv_api)
{
    php_info_print_table_start();
    php_info_print_table_row(2, "MOBIDELIV API support", "enabled");
    php_info_print_table_row(2, "Log File", MOBIDELIV_LOG);
    php_info_print_table_row(2, "Config File", MOBIDELIV_CONFIG"mobideliv_api.conf");
    php_info_print_table_row(2, "Build Date", __DATE__);
    php_info_print_table_row(2, "Build Time", __TIME__);
    php_info_print_table_row(2, "MOBIDELIV API Version", PHP_MOBIDELIV_API_EXTVER);
    php_info_print_table_end();
}

zend_module_entry mobideliv_api_module_entry = {
#if ZEND_MODULE_API_NO >= 20010901
    STANDARD_MODULE_HEADER,
#endif
    MOBIDELIV_API_NS,
    NULL,                  /* Functions */
    PHP_MINIT(mobideliv_api),
    NULL,                  /* MSHUTDOWN */
    NULL,                  /* RINIT */
    NULL,                  /* RSHUTDOWN */
    PHP_MINFO(mobideliv_api),    /* MINFO */
#if ZEND_MODULE_API_NO >= 20010901
    PHP_MOBIDELIV_API_EXTVER,
#endif
    STANDARD_MODULE_PROPERTIES
};

#ifdef COMPILE_DL_MOBIDELIV_API
extern "C" {
ZEND_GET_MODULE(mobideliv_api)
}
#endif
