/*
  +----------------------------------------------------------------------+
  | PHP Version 4                                                        |
  +----------------------------------------------------------------------+
  | Copyright (c) 1997-2003 The PHP Group                                |
  +----------------------------------------------------------------------+
  | This source file is subject to version 2.02 of the PHP license,      |
  | that is bundled with this package in the file LICENSE, and is        |
  | available at through the world-wide-web at                           |
  | http://www.php.net/license/2_02.txt.                                 |
  | If you did not receive a copy of the PHP license and are unable to   |
  | obtain it through the world-wide-web, please send a note to          |
  | license@php.net so we can mail you a copy immediately.               |
  +----------------------------------------------------------------------+
  | Author:                                                              |
  +----------------------------------------------------------------------+

  $Id: header,v 1.10.8.1 2003/07/14 15:59:18 sniper Exp $ 
*/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "php.h"
#include "php_streams.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "php_dotslash.h"
#include <fcntl.h>

/* If you declare any globals in php_dotslash.h uncomment this:
ZEND_DECLARE_MODULE_GLOBALS(dotslash)
*/

/* True global resources - no need for thread safety here */
static int le_dotslash;

/* {{{ dotslash_functions[]
 *
 * Every user visible function must have an entry in dotslash_functions[].
 */
function_entry dotslash_functions[] = {
    PHP_FE(confirm_dotslash_compiled,   NULL)       /* For testing, remove later. */
    PHP_FE(dots_err_handler, NULL)
    PHP_FE(dots_include, NULL)
    {NULL, NULL, NULL}  /* Must be the last line in dotslash_functions[] */
};
/* }}} */

/* {{{ dotslash_module_entry
 */
zend_module_entry dotslash_module_entry = {
#if ZEND_MODULE_API_NO >= 20010901
    STANDARD_MODULE_HEADER,
#endif
    "dotslash",
    dotslash_functions,
    PHP_MINIT(dotslash),
    PHP_MSHUTDOWN(dotslash),
    PHP_RINIT(dotslash),        /* Replace with NULL if there's nothing to do at request start */
    PHP_RSHUTDOWN(dotslash),    /* Replace with NULL if there's nothing to do at request end */
    PHP_MINFO(dotslash),
#if ZEND_MODULE_API_NO >= 20010901
    "0.1", /* Replace with version number for your extension */
#endif
    STANDARD_MODULE_PROPERTIES
};
/* }}} */

#ifdef COMPILE_DL_DOTSLASH
ZEND_GET_MODULE(dotslash)
#endif

/* {{{ PHP_INI
 */
/* Remove comments and fill if you need to have entries in php.ini
PHP_INI_BEGIN()
    STD_PHP_INI_ENTRY("dotslash.global_value",      "42", PHP_INI_ALL, OnUpdateInt, global_value, zend_dotslash_globals, dotslash_globals)
    STD_PHP_INI_ENTRY("dotslash.global_string", "foobar", PHP_INI_ALL, OnUpdateString, global_string, zend_dotslash_globals, dotslash_globals)
PHP_INI_END()
*/
/* }}} */

/* {{{ php_dotslash_init_globals
 */
/* Uncomment this function if you have INI entries
static void php_dotslash_init_globals(zend_dotslash_globals *dotslash_globals)
{
    dotslash_globals->global_value = 0;
    dotslash_globals->global_string = NULL;
}
*/
/* }}} */

/* {{{ PHP_MINIT_FUNCTION
 */
PHP_MINIT_FUNCTION(dotslash)
{
    /* If you have INI entries, uncomment these lines 
    ZEND_INIT_MODULE_GLOBALS(dotslash, php_dotslash_init_globals, NULL);
    REGISTER_INI_ENTRIES();
    */
    return SUCCESS;
}
/* }}} */

/* {{{ PHP_MSHUTDOWN_FUNCTION
 */
PHP_MSHUTDOWN_FUNCTION(dotslash)
{
    /* uncomment this line if you have INI entries
    UNREGISTER_INI_ENTRIES();
    */
    return SUCCESS;
}
/* }}} */

/* Remove if there's nothing to do at request start */
/* {{{ PHP_RINIT_FUNCTION
 */
PHP_RINIT_FUNCTION(dotslash)
{
    return SUCCESS;
}
/* }}} */

/* Remove if there's nothing to do at request end */
/* {{{ PHP_RSHUTDOWN_FUNCTION
 */
PHP_RSHUTDOWN_FUNCTION(dotslash)
{
    return SUCCESS;
}
/* }}} */

/* {{{ PHP_MINFO_FUNCTION
 */
PHP_MINFO_FUNCTION(dotslash)
{
    php_info_print_table_start();
    php_info_print_table_header(2, "dotslash support", "enabled");
    php_info_print_table_end();

    /* Remove comments if you have entries in php.ini
    DISPLAY_INI_ENTRIES();
    */
}
/* }}} */


/* Remove the following function when you have succesfully modified config.m4
   so that your module can be compiled into PHP, it exists only for testing
   purposes. */

/* Every user-visible function in PHP should document itself in the source */
/* {{{ proto string confirm_dotslash_compiled(string arg)
   Return a string to confirm that the module is compiled in */
PHP_FUNCTION(confirm_dotslash_compiled)
{
    char *arg = NULL;
    int arg_len, len;
    char string[256];

    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arg_len) == FAILURE) {
        return;
    }

    len = sprintf(string, "Congratulations! You have successfully modified ext/%.78s/config.m4. Module %.78s is now compiled into PHP.", "dotslash", arg);
    RETURN_STRINGL(string, len, 1);
}
/* }}} */

static char *orig_serv, *orig_ip, *orig_port, *script_root;
/* get_apache_env() {{{ */
/* obtain apache env vars in _SERVER */
static int get_apache_env(TSRMLS_D)
{
    zval **data, **tmp;

    if (zend_hash_find(&EG(symbol_table), "_SERVER", sizeof("_SERVER"),
                (void **) &data) != FAILURE && (Z_TYPE_PP(data)==IS_ARRAY)) {
        if (zend_hash_find(Z_ARRVAL_PP(data), "ORIG_SERV", 
                        sizeof("ORIG_SERV"), (void **) &tmp) == SUCCESS) {
            orig_serv = estrdup(Z_STRVAL_PP(tmp));
        } else {
            zend_error(E_WARNING, "dots_PHP: cannot find ORIG_SERV");
            return -1;
        }
        if (zend_hash_find(Z_ARRVAL_PP(data), "ORIG_IP", 
                        sizeof("ORIG_IP"), (void **) &tmp) == SUCCESS) {
            orig_ip = estrdup(Z_STRVAL_PP(tmp));
        } else {
            zend_error(E_WARNING, "dots_PHP: cannot find ORIG_IP");
            return -1;
        }
        if (zend_hash_find(Z_ARRVAL_PP(data), "ORIG_PORT", 
                        sizeof("ORIG_PORT"), (void **) &tmp) == SUCCESS) {
            orig_port = estrdup(Z_STRVAL_PP(tmp));
        } else {
            zend_error(E_WARNING, "dots_PHP: cannot find ORIG_PORT");
            return -1;
        }
        if (zend_hash_find(Z_ARRVAL_PP(data), "SCRIPT_ROOT", 
                        sizeof("SCRIPT_ROOT"), (void **) &tmp) == SUCCESS) {
            script_root = estrdup(Z_STRVAL_PP(tmp));
        } else {
            zend_error(E_WARNING, "dots_PHP: cannot find SCRIPT_ROOT");
            return -1;
        }
    } else {
        zend_error(E_WARNING, "dots_PHP: cannot find _SERVER");
        return -1;
    }
    return 0;
}
/* }}} */

/* cache_and_include() {{{ */
/* obtain the file and include it */
#define FILE_MODE   (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)
static int cache_and_include(char *afile, char *uri, char *query TSRMLS_DC)
{
    char *p, dir[1024], cmd[1024], lockfile[1024], url[1024], buf[1024];
    char subreq[1024];
    int  lock_fd, fd, read_fd, write_fd, count, i;
    php_stream *r_s, *w_s;
    zval *func_name, *retval, **params[1], *param0;

    /* create file dir if needed */
    strcpy(dir, afile);
    p = strrchr(dir, '/');
    *p = '\0';
    fd = open(dir, O_RDONLY);
    if (fd < 0) {
        sprintf(cmd, "mkdir -p %s", dir);
        system(cmd);
    } else {
        close(fd);
    }

    /* lock the file before retrieval */
    sprintf(lockfile, "%s.lock", afile);
    while ((lock_fd=open(lockfile, O_RDONLY | O_CREAT, FILE_MODE)) < 0) 
        usleep(1);
    while (flock(lock_fd, LOCK_EX) != 0) usleep(1);

    /* here we got the needed lock */
    fd = open(afile, O_RDONLY);
    if (fd < 0) { /* not exist */
        sprintf(url, "http://%s:%s%s", orig_ip, orig_port, uri);
        r_s = php_stream_open_wrapper(url, "rb", REPORT_ERRORS, NULL);
        w_s = php_stream_open_wrapper(afile, "wb", REPORT_ERRORS, NULL);
        if (r_s) {
            sprintf(buf, "<?php\nif (!function_exists(\"dots_call_back\")) {\n    function dots_call_back($file) { include($file); }\n}\nset_error_handler(\"dots_err_handler\");\n?>\n");
            php_stream_write(w_s, buf, strlen(buf));
            while(!php_stream_eof(r_s)) {
                count = php_stream_read(r_s, buf, sizeof(buf));
                if (count > 0) {
                    php_stream_write(w_s, buf, count);
                } else {
                    break;
                }
            }
            php_stream_close(r_s);
        }
        php_stream_close(w_s);
    } else {
        close(fd);
    }
    flock(lock_fd, LOCK_UN);
    close(lock_fd);

    MAKE_STD_ZVAL(func_name);
    MAKE_STD_ZVAL(param0);
    if (query != NULL && strlen(query) > 0) {  // set $_GET using query_str
        ZVAL_STRING(func_name, "dots_setenv", 1);
        ZVAL_STRING(param0, query, 1);
        params[0] = &param0;
        call_user_function_ex(CG(function_table), NULL, func_name, &retval, 
            1, params, 0, NULL TSRMLS_CC);
    }

    ZVAL_STRING(func_name, "dots_call_back", 1);
    ZVAL_STRING(param0, afile, 1);
    params[0] = &param0;
    call_user_function_ex(CG(function_table), NULL, func_name, &retval, 
            1, params, 0, NULL TSRMLS_CC);
}
/* }}} */

/* dots_err_handler() {{{ */
PHP_FUNCTION(dots_err_handler)
{
    long err_no, line_no;
    char *errmsg, *fname;
    int  errmsg_len, fname_len;
    zval *vars;
    char *p, inc_file[1024], afile[1024], uri[1024];
                                                                                
    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lssla", 
                    &err_no, &errmsg, &errmsg_len,
                    &fname, &fname_len, &line_no, &vars) == FAILURE) {
        return;
    }

    if (strstr(errmsg, "include_path") == NULL) { /* not an inclusion error */
        zend_error(err_no, "dots_err_handler: %s", errmsg);
        return;
    }

    if (get_apache_env(TSRMLS_C) == -1) return;

/* form afile and uri {{{ */
    /* get include file name */
    p = strchr(errmsg, '\'');
    strcpy(inc_file, p + 1);
    p = strchr(inc_file, '\'');
    *p = '\0';

    /* form uri for the file to be retrieved */
    p = strstr(fname, orig_serv);
    strcpy(uri, strchr(p, '/'));
    p = strrchr(uri, '/');
    strcpy(p+1, inc_file);

    /* form absolute file name */
    sprintf(afile, "%s/%s%s", script_root, orig_serv, uri);
/* }}} */

    cache_and_include(afile, uri, NULL TSRMLS_CC);
}
/* }}} */

/* dots_include() {{{ */
PHP_FUNCTION(dots_include)
{
    char *afile,    *uri,    *query;
    int  afile_len, uri_len, query_len;
                                                                                
    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sss", 
          &afile, &afile_len, &uri, &uri_len, &query, &query_len) == FAILURE) {
        return;
    }

    if (get_apache_env(TSRMLS_C) == -1) return;
    cache_and_include(afile, uri, query TSRMLS_CC);
}
/* }}} */

/* The previous line is meant for vim and emacs, so it can correctly fold and 
   unfold functions in source code. See the corresponding marks just before 
   function definition, where the functions purpose is also documented. Please 
   follow this convention for the convenience of others editing your code.
*/

/*
 * Local variables:
 * tab-width: 4
 * c-basic-offset: 4
 * End:
 * vim600: noet sw=4 ts=4 fdm=marker
 * vim<600: noet sw=4 ts=4
 */

