/* {{{ proto void run() */ PHP_METHOD(slightphp, run) { zval *zone=NULL; zval *page=NULL; zval *entry=NULL; zval **token; zval *path_array; //{{{ int isPart; zval * path = NULL; if (ZEND_NUM_ARGS()>0 && zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z/", &path) != FAILURE) { if (Z_TYPE_P(path)!= IS_STRING){ RETURN_FALSE; } isPart = 1; }else{ isPart = 0; path = zend_read_static_property(slightphp_ce_ptr,"pathInfo",sizeof("pathInfo")-1,1 TSRMLS_CC); int s = Z_STRLEN_P(path); if(s==0){ zend_is_auto_global("_SERVER", sizeof("_SERVER") - 1 TSRMLS_CC); if(zend_hash_find(Z_ARRVAL_P(PG(http_globals)[TRACK_VARS_GET]), "PATH_INFO", sizeof("PATH_INFO"), (void **) &token) == SUCCESS ){ path = *token; }else if(zend_hash_find(Z_ARRVAL_P(PG(http_globals)[TRACK_VARS_SERVER]), "PATH_INFO", sizeof("PATH_INFO"), (void **) &token) == SUCCESS ){ path = *token; } } } //}}} MAKE_STD_ZVAL(path_array); array_init(path_array); if (path){ //{{{ zval quotedFlag; regex_t re; char *regex; regmatch_t subs[1]; int err,size; char *strp = Z_STRVAL_P(path); char *endp = strp + Z_STRLEN_P(path); zval *splitFlag = zend_read_static_property(slightphp_ce_ptr,"splitFlag",sizeof("splitFlag")-1,1 TSRMLS_CC); if(preg_quote(splitFlag,"edFlag)>0){ spprintf(®ex,0,"[%s\\/]",Z_STRVAL(quotedFlag)); }else{ spprintf(®ex,0,"[\\/]"); } err = regcomp(&re, regex, REG_ICASE); if (err) { }else{ while (!(err = regexec(&re, strp, 1, subs, 0))) { if (subs[0].rm_so == 0 && subs[0].rm_eo) { //ignore empty string strp += subs[0].rm_eo; }else if (subs[0].rm_so == 0 && subs[0].rm_eo == 0) { }else{ size = subs[0].rm_so; add_next_index_stringl(path_array, strp, size, 1); strp += size; } } if (!err || err == REG_NOMATCH) { size = endp - strp; if(size>0) add_next_index_stringl(path_array, strp, size, 1); } regfree(&re); } efree(regex); //}}} int n_elems = zend_hash_num_elements(Z_ARRVAL_P(path_array)); if(zend_hash_index_find(Z_ARRVAL_P(path_array), 0, (void **)&token) != FAILURE) { zone = *token; } if(zend_hash_index_find(Z_ARRVAL_P(path_array), 1, (void **)&token) != FAILURE) { page = *token; } if(zend_hash_index_find(Z_ARRVAL_P(path_array), 2, (void **)&token) != FAILURE) { entry = *token; } } if(!zone){ zone = zend_read_static_property(slightphp_ce_ptr,"defaultZone",sizeof("defaultZone")-1,1 TSRMLS_CC); zend_hash_next_index_insert(Z_ARRVAL_P(path_array),&zone,sizeof(zval*),NULL); } if(!page){ page = zend_read_static_property(slightphp_ce_ptr,"defaultPage",sizeof("defaultPage")-1,1 TSRMLS_CC); zend_hash_next_index_insert(Z_ARRVAL_P(path_array),&page,sizeof(zval*),NULL); } if(!entry){ entry = zend_read_static_property(slightphp_ce_ptr,"defaultEntry",sizeof("defaultEntry")-1,1 TSRMLS_CC); zend_hash_next_index_insert(Z_ARRVAL_P(path_array),&entry,sizeof(zval*),NULL); } //{{{ zval *zoneAlias = zend_read_static_property(slightphp_ce_ptr,"zoneAlias",sizeof("zoneAlias")-1,1 TSRMLS_CC); if(zoneAlias && Z_TYPE_P(zoneAlias)==IS_ARRAY){ char *string_key;uint str_key_len;ulong num_key; HashPosition pos; zval **entry2; zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(zoneAlias), &pos); while (zend_hash_get_current_data_ex(Z_ARRVAL_P(zoneAlias), (void **)&entry2, &pos) == SUCCESS) { if(strcmp(Z_STRVAL_PP(entry2) ,Z_STRVAL_P(zone))==0){ switch (zend_hash_get_current_key_ex(Z_ARRVAL_P(zoneAlias), &string_key, &str_key_len, &num_key, 0, &pos)) { case HASH_KEY_IS_STRING: ZVAL_STRING(zone,string_key,1); break; } } zend_hash_move_forward_ex(Z_ARRVAL_P(zoneAlias), &pos); } if(entry2)zval_ptr_dtor(entry2); if(string_key)efree(string_key); } //if(zoneAlias)FREE_ZVAL(zoneAlias); //}}} if(!isPart){ zend_update_static_property(slightphp_ce_ptr,"zone",sizeof("zone")-1,zone TSRMLS_CC); zend_update_static_property(slightphp_ce_ptr,"page",sizeof("page")-1,page TSRMLS_CC); zend_update_static_property(slightphp_ce_ptr,"entry",sizeof("entry")-1,entry TSRMLS_CC); }else{ if( strcmp(Z_STRVAL_P(zone),Z_STRVAL_P(zend_read_static_property(slightphp_ce_ptr,"zone",sizeof("zone")-1,1 TSRMLS_CC)))==0 && strcmp(Z_STRVAL_P(page),Z_STRVAL_P(zend_read_static_property(slightphp_ce_ptr,"page",sizeof("page")-1,1 TSRMLS_CC)))==0 && strcmp(Z_STRVAL_P(entry),Z_STRVAL_P(zend_read_static_property(slightphp_ce_ptr,"entry",sizeof("entry")-1,1 TSRMLS_CC)))==0 ){ debug("part ignored [%s]",Z_STRVAL_P(path)); return; } } zval *appDir = zend_read_static_property(slightphp_ce_ptr,"appDir",sizeof("appDir")-1,1 TSRMLS_CC); zval *params[1]; params[0]=path_array; if(slightphp_load(appDir,zone,page TSRMLS_CC) == SUCCESS){ if(slightphp_run(zone,page,entry,return_value,1,params TSRMLS_CC)==SUCCESS){ if(path_array)FREE_ZVAL(path_array); RETURN_ZVAL(return_value,1,0); }; } if(path_array)FREE_ZVAL(path_array); RETURN_FALSE; }
/* {{{ proto void run() */ PHP_METHOD(slightphp, run) { zval *zone=NULL; zval *page=NULL; zval *entry=NULL; zval path_array; //{{{ int isPart; zval *path; if (ZEND_NUM_ARGS()>0 && zend_parse_parameters(ZEND_NUM_ARGS() , "z/", &path) != FAILURE) { if (Z_TYPE_P(path)!= IS_STRING){ RETURN_FALSE; } isPart = 1; }else{ isPart = 0; zend_is_auto_global_str(ZEND_STRL("_SERVER")); zval *server_vars; if ((server_vars = zend_hash_str_find(&EG(symbol_table), ZEND_STRL("_SERVER"))) != NULL && Z_TYPE_P(server_vars) == IS_ARRAY){ if((path= zend_hash_str_find(Z_ARRVAL_P(server_vars), ZEND_STRL("PATH_INFO")))!=NULL && Z_TYPE_P(path) == IS_STRING) { // }else if((path= zend_hash_str_find(Z_ARRVAL_P(server_vars), ZEND_STRL("REQUEST_URI")))!=NULL && Z_TYPE_P(path) == IS_STRING) { // }else{ debug("path not set in params or server.path_info, server.request_uri"); RETURN_FALSE; } } } /* Skip leading / */ int len = Z_STRLEN_P(path); int start=0; for(start=0;start<len;start++){ if(*(Z_STRVAL_P(path)+start) != '/'){ break; } } zval url; php_url *resource=NULL; resource = php_url_parse(Z_STRVAL_P(path)+start); if(resource != NULL){ if(resource->path != NULL){ ZVAL_STRING(&url,resource->path); }else{ ZVAL_STRING(&url,Z_STRVAL_P(path)); } php_url_free(resource); }else{ ZVAL_STRING(&url,Z_STRVAL_P(path)); } zend_update_static_property(slightphp_ce_ptr,"pathInfo",sizeof("pathInfo")-1,&url TSRMLS_CC); //zend_print_flat_zval_r(path); //}}} array_init(&path_array); { //{{{ zval quotedFlag; regex_t re; char *regex; regmatch_t subs[1]; int err,size; char *strp = Z_STRVAL(url); char *endp = strp + Z_STRLEN(url); zval *splitFlag = zend_read_static_property(slightphp_ce_ptr,"splitFlag",sizeof("splitFlag")-1,1 ); if(preg_quote(splitFlag,"edFlag)>0){ spprintf(®ex,0,"[%s\\/]",Z_STRVAL(quotedFlag)); }else{ spprintf(®ex,0,"[\\/]"); } err = regcomp(&re, regex, REG_ICASE); if (err) { }else{ while (!(err = regexec(&re, strp, 1, subs, 0))) { if (subs[0].rm_so == 0 && subs[0].rm_eo) { //ignore empty string strp += subs[0].rm_eo; }else if (subs[0].rm_so == 0 && subs[0].rm_eo == 0) { }else{ size = subs[0].rm_so; add_next_index_stringl(&path_array, strp, size); strp += size; } } if (!err || err == REG_NOMATCH) { size = endp - strp; if(size>0) add_next_index_stringl(&path_array, strp, size); } regfree(&re); } efree(regex); zval_dtor("edFlag); //}}} if((zone = zend_hash_index_find(Z_ARRVAL(path_array), 0)) != NULL ) { } if((page = zend_hash_index_find(Z_ARRVAL(path_array), 1)) != NULL ) { } if((entry = zend_hash_index_find(Z_ARRVAL(path_array), 2)) != NULL) { } } if(!zone){ zone = zend_read_static_property(slightphp_ce_ptr,"defaultZone",sizeof("defaultZone")-1,1 ); add_next_index_string(&path_array, Z_STRVAL_P(zone)); } if(!page){ page = zend_read_static_property(slightphp_ce_ptr,"defaultPage",sizeof("defaultPage")-1,1 ); add_next_index_string(&path_array, Z_STRVAL_P(page)); } if(!entry){ entry = zend_read_static_property(slightphp_ce_ptr,"defaultEntry",sizeof("defaultEntry")-1,1 ); add_next_index_string(&path_array, Z_STRVAL_P(entry)); } //{{{ zval *zoneAlias = zend_read_static_property(slightphp_ce_ptr,"zoneAlias",sizeof("zoneAlias")-1,1 ); if(zoneAlias && Z_TYPE_P(zoneAlias)==IS_ARRAY){ zend_ulong num_key; zend_string *string_key= NULL; HashPosition pos; zval *entry2=NULL; zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(zoneAlias), &pos); for (;; zend_hash_move_forward_ex(Z_ARRVAL_P(zoneAlias), &pos)) { if (NULL == (entry2= zend_hash_get_current_data_ex(Z_ARRVAL_P(zoneAlias), &pos))) { break; } if(strcmp(Z_STRVAL_P(entry2) ,Z_STRVAL_P(zone))==0){ switch (pos = zend_hash_get_current_key_ex(Z_ARRVAL_P(zoneAlias), &string_key, &num_key,&pos)) { case HASH_KEY_IS_STRING: ZVAL_STR_COPY(zone,string_key); break; } } } } //}}} if(!isPart){ zend_update_static_property(slightphp_ce_ptr,"zone",sizeof("zone")-1,zone ); zend_update_static_property(slightphp_ce_ptr,"page",sizeof("page")-1,page ); zend_update_static_property(slightphp_ce_ptr,"entry",sizeof("entry")-1,entry ); }else{ if( strcmp(Z_STRVAL_P(zone),Z_STRVAL_P(zend_read_static_property(slightphp_ce_ptr,"zone",sizeof("zone")-1,1 )))==0 && strcmp(Z_STRVAL_P(page),Z_STRVAL_P(zend_read_static_property(slightphp_ce_ptr,"page",sizeof("page")-1,1 )))==0 && strcmp(Z_STRVAL_P(entry),Z_STRVAL_P(zend_read_static_property(slightphp_ce_ptr,"entry",sizeof("entry")-1,1 )))==0 ){ debug("part ignored [%s]",Z_STRVAL(url)); zval_dtor(&path_array); zval_dtor(&url); return; } } zval *appDir = zend_read_static_property(slightphp_ce_ptr,"appDir",sizeof("appDir")-1,1 ); if(slightphp_load(appDir,zone,page ) == SUCCESS){ zval ret; if(slightphp_run(zone,page,entry,&ret,1,&path_array)==SUCCESS){ zval_dtor(&path_array); zval_dtor(&url); RETURN_ZVAL(&ret,0,1); }; } zval_dtor(&url); zval_dtor(&path_array); RETURN_FALSE; }