📄 php_core_lib.cpp
字号:
return; } if ( result ) { cast_value_dnum(result); result->type = PHP_VAL_STRING; result->str_val = strdup(gettext(str->str_val)); }}void php_native_gettext_noop(PHP_VALUE_NODE *result){ PHP_SCOPE_ITEM *si_str = get_scope_item(g_current_scope, "__param_0"); PHP_VALUE_NODE *str = &si_str->var->value; if ( si_str ) { cast_value_str(str); } else { php_report_error(PHP_ERROR, "Invalid or missing argument 'msgid' for 'gettext_noop'"); return; } if ( result ) { cast_value_dnum(result); result->type = PHP_VAL_STRING; result->str_val = strdup(str->str_val); }}void php_native_ngettext(PHP_VALUE_NODE *result){ PHP_SCOPE_ITEM *si_msgid = get_scope_item(g_current_scope, "__param_0"); PHP_VALUE_NODE *msgid = &si_msgid->var->value; if ( si_msgid ) { cast_value_str(msgid); } else { php_report_error(PHP_ERROR, "Invalid or missing argument 'msgid' for 'ngettext'"); return; } PHP_SCOPE_ITEM *si_msgid_plural = get_scope_item(g_current_scope, "__param_1"); PHP_VALUE_NODE *msgid_plural = &si_msgid_plural->var->value; if ( si_msgid_plural ) { cast_value_str(msgid_plural); } else { php_report_error(PHP_ERROR, "Invalid or missing argument 'msgid_plural' for 'ngettext'"); return; } PHP_SCOPE_ITEM *si_count = get_scope_item(g_current_scope, "__param_2"); PHP_VALUE_NODE *count = &si_count->var->value; if ( si_count ) { cast_value_dnum(count); } else { php_report_error(PHP_ERROR, "Invalid or missing argument 'count' for 'ngettext'"); return; } if ( result ) { cast_value_dnum(result); result->type = PHP_VAL_STRING; result->str_val = strdup(ngettext(msgid->str_val, msgid_plural->str_val, count->int_val)); }}#endifPHP_BLTIN_FUNC_DEF core_lib_funcs[] = { { "var_dump", 1, php_native_var_dump, }, { "strlen", 1, php_native_strlen, }, { "count", 1, php_native_count, }, { "isset", 1, php_native_isset, }, { "usort", 2, php_native_usort, }, { "split", 3, php_native_split, },#ifdef ENABLE_NLS { "_", 1, php_native_gettext, }, { "gettext", 1, php_native_gettext, }, { "gettext_noop", 1, php_native_gettext_noop, }, { "ngettext", 3, php_native_ngettext, },#endif { 0, 0, 0, },};void php_init_core_lib(){ // load function definitions PHP_BLTIN_FUNC_DEF *curr_def = core_lib_funcs; while ( curr_def->name ) { php_add_native_func(curr_def); curr_def++; }}//// lexer has no include file//extern "C"void php_set_input_buffer(char *buf, int len);CPhPLibContext::CPhPLibContext(CWebServerBase *server, const char *file){ g_curr_context = this; m_server = server; php_engine_init(); yyin = fopen(file, "r"); if ( !yyin ) { return; } yyparse(); m_syn_tree_top = g_syn_tree_top; m_global_scope = g_global_scope;}CPhPLibContext::CPhPLibContext(CWebServerBase *server, char *php_buf, int len){ g_curr_context = this; m_server = server; php_engine_init(); m_global_scope = g_global_scope; php_set_input_buffer(php_buf, len); yyparse(); m_syn_tree_top = g_syn_tree_top;}CPhPLibContext::~CPhPLibContext(){ SetContext(); php_engine_free();}void CPhPLibContext::SetContext(){ g_syn_tree_top = m_syn_tree_top; g_global_scope = m_global_scope;}void CPhPLibContext::Execute(CWriteStrBuffer *buf){ m_curr_str_buffer = buf; PHP_VALUE_NODE val; php_execute(g_syn_tree_top, &val);}CPhPLibContext *CPhPLibContext::g_curr_context = 0;/* * For simplicity and performance sake, this function can * only handle limited-length printf's. In should be NOT be used * for string concatenation like printf("xyz %s %s", s1, s2). * * Engine will call Print for "print" and "echo" */void CPhPLibContext::Printf(const char *str, ...){ va_list args; va_start(args, str); if ( !g_curr_context || !g_curr_context->m_curr_str_buffer ) { vprintf(str, args); } else { char buf[4096]; vsnprintf(buf, sizeof(buf), str, args); g_curr_context->m_curr_str_buffer->Write(buf); } va_end(args);}void CPhPLibContext::Print(const char *str){ if ( !g_curr_context || !g_curr_context->m_curr_str_buffer ) { printf("%s", str); } else { g_curr_context->m_curr_str_buffer->Write(str); }}CPhpFilter::CPhpFilter(CWebServerBase *server, CSession *sess, const char *file, CWriteStrBuffer *buff){ FILE *f = fopen(file, "r"); if ( !f ) { return; } if ( fseek(f, 0, SEEK_END) != 0 ) { return; } int size = ftell(f); char *buf = new char [size+1]; rewind(f); fread(buf, 1, size, f); buf[size] = 0; fclose(f); char *scan_ptr = buf; char *curr_code_end = buf; while ( strlen(scan_ptr) ) { scan_ptr = strstr(scan_ptr, "<?php"); if ( !scan_ptr ) { buff->Write(curr_code_end); break; } if ( scan_ptr != curr_code_end ) { buff->Write(curr_code_end, scan_ptr - curr_code_end); } curr_code_end = strstr(scan_ptr, "?>"); if ( !curr_code_end ) { break; } curr_code_end += 2; // include "?>" in buffer int len = curr_code_end - scan_ptr; CPhPLibContext *context = new CPhPLibContext(server, scan_ptr, len); load_session_vars("HTTP_GET_VARS", sess->m_get_vars); load_session_vars("_SESSION", sess->m_vars); context->Execute(buff); save_session_vars(sess->m_vars); delete context; scan_ptr = curr_code_end; } sess->m_get_vars.clear(); delete [] buf;}/* * String buffer: almost same as regular 'string' class, but, * without reallocation when full. Instead, new buffer is * allocated, and added to list */CWriteStrBuffer::CWriteStrBuffer(){ m_alloc_size = 1024; m_total_length = 0; AllocBuf();}CWriteStrBuffer::~CWriteStrBuffer(){ for(std::list<char *>::iterator i = m_buf_list.begin(); i != m_buf_list.end(); i++) { delete [] *i; } delete [] m_curr_buf;}void CWriteStrBuffer::AllocBuf(){ m_curr_buf = new char [m_alloc_size]; m_buf_ptr = m_curr_buf; m_curr_buf_left = m_alloc_size;}void CWriteStrBuffer::Write(const char *s, int len){ if ( len == -1 ) { len = strlen(s); } m_total_length += len; while ( len ) { if ( (len + 1) <= m_curr_buf_left ) { strncpy(m_buf_ptr, s, len); m_buf_ptr += len; m_curr_buf_left -= len; len = 0; } else { memcpy(m_buf_ptr, s, m_curr_buf_left); int rem_len = len - m_curr_buf_left; s += m_curr_buf_left; len = rem_len; m_buf_list.push_back(m_curr_buf); AllocBuf(); } }}void CWriteStrBuffer::CopyAll(char *dst_buffer){ char *curr_ptr = dst_buffer; int rem_size = m_total_length; for(std::list<char *>::iterator i = m_buf_list.begin(); i != m_buf_list.end(); i++) { memcpy(curr_ptr, *i, m_alloc_size); rem_size -= m_alloc_size; curr_ptr += m_alloc_size; } if ( rem_size ) { memcpy(curr_ptr, m_curr_buf, rem_size); } *(curr_ptr + rem_size) = 0;}void load_session_vars(const char *target, std::map<std::string, std::string> &varmap){ PHP_EXP_NODE *sess_vars_exp_node = get_var_node(target); PHP_VAR_NODE *sess_vars = sess_vars_exp_node->var_si_node->var; // i'm not building exp tree, node not needed delete sess_vars_exp_node; cast_value_array(&sess_vars->value); for(std::map<std::string, std::string>::iterator i = varmap.begin(); i != varmap.end(); i++) { PHP_VAR_NODE *curr_var = array_get_by_str_key(&sess_vars->value, i->first); PHP_VALUE_NODE val; val.type = PHP_VAL_STRING; val.str_val = const_cast<char *>(i->second.c_str()); value_value_assign(&curr_var->value, &val); }}void save_session_vars(std::map<std::string, std::string> &varmap){ PHP_EXP_NODE *sess_vars_exp_node = get_var_node("_SESSION"); PHP_VAR_NODE *sess_vars = sess_vars_exp_node->var_si_node->var; delete sess_vars_exp_node; if ( sess_vars->value.type != PHP_VAL_ARRAY ) { return; } for(int i = 0; i < array_get_size(&sess_vars->value); i++) { std::string s = array_get_ith_key(&sess_vars->value, i); PHP_VAR_NODE *var = array_get_by_str_key(&sess_vars->value, s); cast_value_str(&var->value); varmap[s] = var->value.str_val; }}// File_checked_for_headers
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -