Example #1
0
/* {{{ posix_addlimit
 */
static int posix_addlimit(int limit, char *name, zval *return_value) {
	int result;
	struct rlimit rl;
	char hard[80];
	char soft[80];

	snprintf(hard, 80, "hard %s", name);
	snprintf(soft, 80, "soft %s", name);

	result = getrlimit(limit, &rl);
	if (result < 0) {
		POSIX_G(last_error) = errno;
		return FAILURE;
	}

	if (rl.rlim_cur == RLIM_INFINITY) {
		add_assoc_stringl(return_value, soft, UNLIMITED_STRING, sizeof(UNLIMITED_STRING)-1);
	} else {
		add_assoc_long(return_value, soft, rl.rlim_cur);
	}

	if (rl.rlim_max == RLIM_INFINITY) {
		add_assoc_stringl(return_value, hard, UNLIMITED_STRING, sizeof(UNLIMITED_STRING)-1);
	} else {
		add_assoc_long(return_value, hard, rl.rlim_max);
	}

	return SUCCESS;
}
Example #2
0
void bbs_make_article_array(zval * array, struct fileheader *fh, char *flags, size_t flags_len)
{
    const struct boardheader *bh;
    char title[ARTICLE_TITLE_LEN];
    add_assoc_string(array, "FILENAME", fh->filename, 1);
    if (fh->o_bid > 0 && ((bh = getboard(fh->o_bid)) != NULL))
        add_assoc_string(array, "O_BOARD", (char*)(bh->filename), 1); /* for compitible */
    else
        add_assoc_string(array, "O_BOARD", "", 1); /* for compitible */
    add_assoc_long(array, "O_BID", fh->o_bid);
    add_assoc_long(array, "O_ID", fh->o_id);
    add_assoc_long(array, "ID", fh->id);
    add_assoc_long(array, "GROUPID", fh->groupid);
    add_assoc_long(array, "REID", fh->reid);
    add_assoc_long(array, "POSTTIME", get_posttime(fh));
    add_assoc_stringl(array, "INNFLAG", fh->innflag, sizeof(fh->innflag), 1);
    add_assoc_string(array, "OWNER", fh->owner, 1);
    strcpy(title, fh->title);
    //process_control_chars(title,NULL);
    add_assoc_string(array, "TITLE", title, 1);
    /*    add_assoc_long(array, "LEVEL", fh->level);*/
    add_assoc_stringl(array, "FLAGS", flags, flags_len, 1);
    add_assoc_long(array, "ATTACHPOS", fh->attachment);
    add_assoc_long(array, "EFFSIZE", fh->eff_size);
    add_assoc_long(array, "IS_TEX", fh->accessed[1] & FILE_TEX);
#ifdef HAVE_REPLY_COUNT
    add_assoc_long(array, "REPLYCOUNT", fh->replycount);
    add_assoc_string(array, "LAST_OWNER", fh->last_owner, 1);
    add_assoc_long(array, "LAST_POSTTIME", fh->last_posttime);
#endif /* HAVE_REPLY_COUNT */
}
Example #3
0
File: db.cpp Project: 191919/hhvm
PHP_METHOD(MongoDB, createCollection)
{
	zval *data = NULL, *temp, *options = NULL;
	char *collection;
	int   collection_len;
	zend_bool capped = 0;
	long size = 0, max = 0;

	if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() TSRMLS_CC, "s|bll", &collection, &collection_len, &capped, &size, &max) == SUCCESS) {
		MAKE_STD_ZVAL(data);
		array_init(data);

		add_assoc_stringl(data, "create", collection, collection_len, 1);

		if (size) {
			add_assoc_long(data, "size", size);
		}

		if (capped) {
			php_error_docref(NULL TSRMLS_CC, MONGO_E_DEPRECATED, "This method now accepts arguments as an options array instead of the three optional arguments for capped, size and max elements");
			add_assoc_bool(data, "capped", 1);
			if (max) {
				add_assoc_long(data, "max", max);
			}
		}

	} else if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|a", &collection, &collection_len, &options) == SUCCESS) {
		zval *tmp_copy;

		/* We create a new array here, instead of just tagging "create" =>
		 * <name> at the end of the array. This is because MongoDB wants the
		 * name of the command as first element in the array. */
		MAKE_STD_ZVAL(data);
		array_init(data);
		add_assoc_stringl(data, "create", collection, collection_len, 1);
		if (options) {
			zend_hash_merge(Z_ARRVAL_P(data), Z_ARRVAL_P(options), (copy_ctor_func_t) zval_add_ref, (void *) &tmp_copy, sizeof(zval *), 0);
		}
	} else {

		return;
	}

	MAKE_STD_ZVAL(temp);
	MONGO_METHOD1(MongoDB, command, temp, getThis(), data);
	zval_ptr_dtor(&temp);

	zval_ptr_dtor(&data);

	if (!EG(exception)) {
		zval *zcollection;

		/* get the collection we just created */
		MAKE_STD_ZVAL(zcollection);
		ZVAL_STRINGL(zcollection, collection, collection_len, 1);
		MONGO_METHOD1(MongoDB, selectCollection, return_value, getThis(), zcollection);
		zval_ptr_dtor(&zcollection);
	}
}
/* {{{ proto array accelerator_get_scripts()
   Get the scripts which are accelerated by ZendAccelerator */
static zval* accelerator_get_scripts(TSRMLS_D)
{
	uint i;
	zval *return_value,*persistent_script_report;
	zend_accel_hash_entry *cache_entry;
	struct tm *ta;
	struct timeval exec_time;
	struct timeval fetch_time;

	if (!ZCG(enabled) || !accel_startup_ok || !ZCSG(accelerator_enabled) || accelerator_shm_read_lock(TSRMLS_C) != SUCCESS) {
		return 0;
	}

	MAKE_STD_ZVAL(return_value);
	array_init(return_value);
	for (i = 0; i<ZCSG(hash).max_num_entries; i++) {
		for (cache_entry = ZCSG(hash).hash_table[i]; cache_entry; cache_entry = cache_entry->next) {
			zend_persistent_script *script;
			char *str;
			size_t len;

			if (cache_entry->indirect) continue;

			script = (zend_persistent_script *)cache_entry->data;

			MAKE_STD_ZVAL(persistent_script_report);
			array_init(persistent_script_report);
			add_assoc_stringl(persistent_script_report, "full_path", script->full_path, script->full_path_len, 1);
			add_assoc_long(persistent_script_report, "hits", script->dynamic_members.hits);
			add_assoc_long(persistent_script_report, "memory_consumption", script->dynamic_members.memory_consumption);
			ta = localtime(&script->dynamic_members.last_used);
			str = asctime(ta);
			len = strlen(str);
			if (len > 0 && str[len - 1] == '\n') len--;
			add_assoc_stringl(persistent_script_report, "last_used", str, len, 1);
			add_assoc_long(persistent_script_report, "last_used_timestamp", script->dynamic_members.last_used);
			if (ZCG(accel_directives).validate_timestamps) {
				add_assoc_long(persistent_script_report, "timestamp", (long)script->timestamp);
			}
			timerclear(&exec_time);
			timerclear(&fetch_time);

			zend_hash_update(return_value->value.ht, cache_entry->key, cache_entry->key_length, &persistent_script_report, sizeof(zval *), NULL);
		}
	}
	accelerator_shm_read_unlock(TSRMLS_C);

	return return_value;
}
/** {{{ public ZeActiveString::put($condition, $val)
 */
PHP_METHOD(ze_activestring, put) {
    zval * self       = NULL;
    zval * items      = NULL;
    char * cond       = NULL;
    int    cond_len   = 0;
    char * val        = NULL;
    int    val_len    = 0;



    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss"
                            , &cond, &cond_len
                            , &val , &val_len
                            ) == FAILURE) {
        WRONG_PARAM_COUNT;
    }

    self = getThis();

    items = zend_read_property(ze_activestring_ce, self, ZEND_STRL(ZE_ITEMS), 0 TSRMLS_CC);

    add_assoc_stringl(items, cond, val, val_len, 1);


    zend_update_property(ze_activestring_ce, self, ZEND_STRL(ZE_ITEMS), items TSRMLS_CC);

    RETURN_ZVAL(self, 1, 0);
}
Example #6
0
static zval *phannot_ret_annotation(phannot_parser_token *name, zval *arguments, phannot_scanner_state *state)
{

	zval *ret;

	MAKE_STD_ZVAL(ret);
	array_init(ret);

	add_assoc_long(ret, "type", PHANNOT_T_ANNOTATION);

	if (name) {
		add_assoc_stringl(ret, "name", name->token, name->token_len, 0);
		efree(name);
	}

	if (arguments) {
		add_assoc_zval(ret, "arguments", arguments);
	}

	Z_ADDREF_P(state->active_file);
	add_assoc_zval(ret, "file", state->active_file);
	add_assoc_long(ret, "line", state->active_line);

	return ret;
}
/* {{{ proto XmlIndexSpecification::getIndexes(...)
   iterates indices */
PHP_DBXML_METHOD_BEGIN(XmlIndexSpecification, getIndexes)
{
  PHP_DBXML_STUFFED_THIS(XmlIndexSpecification);
  if (ZEND_NUM_ARGS() == 0) {
    std::string uri, name, index;
    array_init(return_value);
    while(This.next(uri, name, index)) {
      zval *el;
      MAKE_STD_ZVAL(el);
      array_init(el);
      add_assoc_stringl(el, "uri", (char *) uri.data(), (int)uri.length(), (int)1);
      add_assoc_stringl(el, "name", (char *) name.data(), (int)name.length(), (int)1);
      add_assoc_stringl(el, "index", (char *) index.data(), (int)index.length(), (int)1);
      add_next_index_zval(return_value, el);
    } 
    return;
  } else {
    WRONG_PARAM_COUNT;
  }
} PHP_DBXML_METHOD_END()
/*
 ******************************************************************************************************
 Lists the UDF modules registered with the server.
 *
 * @param aerospike_obj_p           The C client's aerospike object.
 * @param error_p                   The C client's as_error to be set to the encountered error.
 * @param array_of_modules_p        An array of registered UDF modules
 *                                  against the Aerospike DB to be populated by
 *                                  this function.
 * @param language                  Optionally filters a subset of modules
 *                                  matching the given type.
 * @options_p                       The optional parameters to the
 *                                  Aerospike::listRegistered().
 *
 * @return AEROSPIKE_OK if success. Otherwise AEROSPIKE_x.
 ******************************************************************************************************
 */
extern as_status
aerospike_list_registered_udf_modules(Aerospike_object* aerospike_obj_p,
        as_error* error_p, zval* array_of_modules_p, long language,
        zval* options_p)
{
    as_udf_files            udf_files;
    uint32_t                init_udf_files = 0; 
    uint32_t                i=0;
    as_policy_info          info_policy;

    if ((language != -1) && ((language & AS_UDF_TYPE) != AS_UDF_TYPE)) {
            PHP_EXT_SET_AS_ERR(error_p, AEROSPIKE_ERR_PARAM,
                    "Invalid Value for language");
            DEBUG_PHP_EXT_ERROR("Invalid value for language");
            goto exit;
    }

    set_policy(NULL, NULL, NULL, NULL, &info_policy, NULL, options_p, error_p);
    if (AEROSPIKE_OK != (error_p->code)) {
        DEBUG_PHP_EXT_DEBUG("Unable to set policy");
        goto exit;
    }

    as_udf_files_init(&udf_files, 0);
    init_udf_files = 1;
    if (AEROSPIKE_OK != aerospike_udf_list(aerospike_obj_p->as_ref_p->as_p,
                error_p, &info_policy, &udf_files)) {
        DEBUG_PHP_EXT_ERROR(error_p->message);
        goto exit;
    }

    for (i = 0; i < udf_files.size; i++) {
        if ((language != -1) && ((udf_files.entries[i].type) != 
                    (language - AS_UDF_TYPE))) {
                continue;
        }
        zval* module_p = NULL;
        MAKE_STD_ZVAL(module_p);
        array_init(module_p);
        add_assoc_stringl(module_p, UDF_MODULE_NAME, udf_files.entries[i].name,
                strlen(udf_files.entries[i].name), 1);
        add_assoc_long(module_p, UDF_MODULE_TYPE,
                (udf_files.entries[i].type + AS_UDF_TYPE));
        add_next_index_zval(array_of_modules_p, module_p);
    }

exit:
    if (init_udf_files) {
        as_udf_files_destroy(&udf_files);
    }
    return error_p->code;
}
Example #9
0
/* {{{ php_parsekit_parse_arginfo */
static void php_parsekit_parse_arginfo(zval *return_value, zend_uint num_args, zend_arg_info *arginfo, long options TSRMLS_DC)
{
	zend_uint i;

	array_init(return_value);

	for(i = 0; i < num_args; i++) {
		zval *tmpzval;

		MAKE_STD_ZVAL(tmpzval);
		array_init(tmpzval);
		add_assoc_stringl(tmpzval, "name", arginfo[i].name, arginfo[i].name_len, 1);
		if (arginfo[i].class_name_len) {
			add_assoc_stringl(tmpzval, "class_name", arginfo[i].class_name, arginfo[i].class_name_len, 1);
		} else if (options & PHP_PARSEKIT_ALWAYS_SET) {
			add_assoc_null(tmpzval, "class_name");
		}
		add_assoc_bool(tmpzval, "allow_null", arginfo[i].allow_null);
		add_assoc_bool(tmpzval, "pass_by_reference", arginfo[i].pass_by_reference);

		add_next_index_zval(return_value, tmpzval);
	}	
}
Example #10
0
static zval *phannot_ret_literal_zval(int type, phannot_parser_token *T)
{
	zval *ret;

	MAKE_STD_ZVAL(ret);
	array_init(ret);
	add_assoc_long(ret, "type", type);
	if (T) {
		add_assoc_stringl(ret, "value", T->token, T->token_len, 0);
		efree(T);
	}

	return ret;
}
Example #11
0
static zval *phannot_ret_named_item(phannot_parser_token *name, zval *expr)
{
	zval *ret;

	MAKE_STD_ZVAL(ret);
	array_init(ret);
	add_assoc_zval(ret, "expr", expr);
	if (name != NULL) {
		add_assoc_stringl(ret, "name", name->token, name->token_len, 0);
		efree(name);
	}

	return ret;
}
Example #12
0
PHP_METHOD(jsonrpc_server, bind)
{
  zval *procedure, *classname, *method;
  zval *classes;
  zval *val;
  zval *object;

  object = getThis();

  if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zzz", 
    &procedure, &classname, &method) == FAILURE)
  {
    return ;
  }

  classes = zend_read_property(
    php_jsonrpc_server_entry, object, "classes", sizeof("classes")-1, 0 TSRMLS_CC
  );

  MAKE_STD_ZVAL(val);
  array_init(val);
  if (Z_TYPE_P(classname) == IS_STRING){
    add_assoc_stringl(val, "class", Z_STRVAL_P(classname), Z_STRLEN_P(classname), 1);
  }else if (Z_TYPE_P(classname) == IS_OBJECT){
    add_assoc_zval(val, "class", classname);
  }else {
    return ;
  }
  add_assoc_stringl(val, "method", Z_STRVAL_P(method), Z_STRLEN_P(method), 1);

  add_assoc_zval(classes, Z_STRVAL_P(procedure), val);

  zend_update_property(php_jsonrpc_server_entry, object, "classes", sizeof("classes")-1, classes TSRMLS_CC);

  RETURN_ZVAL(object, 1, 0);
}
Example #13
0
/* {{{ proto array accelerator_get_scripts()
   Get the scripts which are accelerated by ZendAccelerator */
static int accelerator_get_scripts(zval *return_value)
{
	uint32_t i;
	zval persistent_script_report;
	zend_accel_hash_entry *cache_entry;
	struct tm *ta;
	struct timeval exec_time;
	struct timeval fetch_time;

	if (!ZCG(enabled) || !accel_startup_ok || !ZCSG(accelerator_enabled) || accelerator_shm_read_lock() != SUCCESS) {
		return 0;
	}

	array_init(return_value);
	for (i = 0; i<ZCSG(hash).max_num_entries; i++) {
		for (cache_entry = ZCSG(hash).hash_table[i]; cache_entry; cache_entry = cache_entry->next) {
			zend_persistent_script *script;
			char *str;
			size_t len;

			if (cache_entry->indirect) continue;

			script = (zend_persistent_script *)cache_entry->data;

			array_init(&persistent_script_report);
			add_assoc_str(&persistent_script_report, "full_path", zend_string_dup(script->script.filename, 0));
			add_assoc_long(&persistent_script_report, "hits", (zend_long)script->dynamic_members.hits);
			add_assoc_long(&persistent_script_report, "memory_consumption", script->dynamic_members.memory_consumption);
			ta = localtime(&script->dynamic_members.last_used);
			str = asctime(ta);
			len = strlen(str);
			if (len > 0 && str[len - 1] == '\n') len--;
			add_assoc_stringl(&persistent_script_report, "last_used", str, len);
			add_assoc_long(&persistent_script_report, "last_used_timestamp", script->dynamic_members.last_used);
			if (ZCG(accel_directives).validate_timestamps) {
				add_assoc_long(&persistent_script_report, "timestamp", (zend_long)script->timestamp);
			}
			timerclear(&exec_time);
			timerclear(&fetch_time);

			zend_hash_str_update(Z_ARRVAL_P(return_value), cache_entry->key, cache_entry->key_length, &persistent_script_report);
		}
	}
	accelerator_shm_read_unlock();

	return 1;
}
Example #14
0
    void KPHPArrayObject::AddKrollValueToPHPArray(KValueRef value, zval *phpArray, const char *key)
    {
        if (value->IsNull() || value->IsUndefined())
        {
            add_assoc_null(phpArray, (char *) key);
        }
        else if (value->IsBool())
        {
            if (value->ToBool())
                add_assoc_bool(phpArray, (char *) key, 1);
            else
                add_assoc_bool(phpArray, (char *) key, 0);
        }
        else if (value->IsNumber())
        {
            /* No way to check whether the number is an
               integer or a double here. All Kroll numbers
               are doubles, so return a double. This could
               cause some PHP to function incorrectly if it's
               doing strict type checking. */
            add_assoc_double(phpArray, (char *) key, value->ToNumber());
        }
        else if (value->IsString())
        {
            add_assoc_stringl(phpArray, (char *) key, (char *) value->ToString(), strlen(value->ToString()), 1);
        }
        else if (value->IsObject())
        {
            /*TODO: Implement*/
        }
        else if (value->IsMethod())
        {
            /*TODO: Implement*/
        }
        else if (value->IsList())
        {
            zval *phpValue;
            AutoPtr<KPHPArrayObject> pl = value->ToList().cast<KPHPArrayObject>();
            if (!pl.isNull())
                phpValue = pl->ToPHP();
            else
                phpValue = PHPUtils::ToPHPValue(value);

            add_assoc_zval(phpArray, (char *) key, phpValue);
        }
    }
Example #15
0
/**
 * Convert a map to a php Array
 *
 * @param t the map to convert
 * @return the php Array
 */
zval *php_Array_from_map(map* t){
  zval *mapArray;
  zval *mapArrayTmp;
  map* tmp=t;
  int tres=0;
  MAKE_STD_ZVAL(mapArray);
  tres=array_init(mapArray);
  while(tmp!=NULL){
    map* sMap=getMapArray(tmp,"size",0);    
    if(strncmp(tmp->name,"value",5)==0 && sMap!=NULL && tmp->value != NULL){
      tres=add_assoc_stringl(mapArray,tmp->name,tmp->value,atoi(sMap->value),1);
    } 
    else if (tmp->value != NULL) {
      tres=add_assoc_string(mapArray,tmp->name,tmp->value,1);
    }
    tmp=tmp->next;
  }
  return mapArray;
}
/* {{{ clm_get_platform_info() */
static zval *clm_get_platform_info(cl_platform_id platform TSRMLS_DC)
{
	const platform_info_param_t *param = platform_info_list;
	cl_int err = CL_SUCCESS;
	char buf[1024] = { 0 };
	size_t len = 0;
	zval *zinfo;

	MAKE_STD_ZVAL(zinfo);
	array_init_size(zinfo, 8);

	while (param->key != NULL) {
		err = clGetPlatformInfo(platform, param->name, sizeof(buf), buf, &len);
		if (err == CL_SUCCESS) {
			add_assoc_stringl(zinfo, param->key, buf, len, 1);
		} else {
			add_assoc_null(zinfo, param->key);
		}
		param++;
	}

	return zinfo;
}
Example #17
0
/* {{{ php_parsekit_derive_arginfo 
	ZE1 Func Arg loading is done via opcodes "RECV"ing from the caller */
static void php_parsekit_derive_arginfo(zval *return_value, zend_op_array *op_array, long options TSRMLS_DC)
{
	int i;
	zend_op *opcodes = op_array->opcodes;

	array_init(return_value);

	/* Received vars come in pairs:
		A ZEND_FETCH_W, and a ZEND_RECV */
	for(i = 0; i < op_array->arg_types[0]; i++) {
		if (opcodes[i*2].opcode   == ZEND_FETCH_W &&
			opcodes[i*2].op1.op_type == IS_CONST &&
			opcodes[i*2].op1.u.constant.type == IS_STRING &&
			(opcodes[(i*2)+1].opcode == ZEND_RECV || opcodes[(i*2)+1].opcode == ZEND_RECV_INIT)) {
			zval *tmpzval;

			MAKE_STD_ZVAL(tmpzval);
			array_init(tmpzval);

			add_assoc_stringl(tmpzval, "name", opcodes[i*2].op1.u.constant.value.str.val, opcodes[i*2].op1.u.constant.value.str.len, 1);
			add_assoc_bool(tmpzval, "pass_by_reference", op_array->arg_types[i+1]);
			if (opcodes[(i*2)+1].opcode == ZEND_RECV_INIT &&
				opcodes[(i*2)+1].op2.op_type == IS_CONST) {
				zval *def;
	
				MAKE_STD_ZVAL(def);
				*def = opcodes[(i*2)+1].op2.u.constant;
				zval_copy_ctor(def);
				add_assoc_zval(tmpzval, "default", def);
				Z_SET_REFCOUNT_P(tmpzval, 1);
			}

			add_next_index_zval(return_value, tmpzval);
		}
	}
}
Example #18
0
long tw_span_create(char *category, size_t category_len TSRMLS_DC)
{
	zval span, starts, stops, annotations;
	int idx;
	long parent = 0;

	if (TWG(spans) == NULL) {
		return -1;
	}

	idx = zend_hash_num_elements(Z_ARRVAL_P(TWG(spans)));

	// Hardcode a limit of 1500 spans for now, Daemon will re-filter again to 1000.
	// We assume web-requests and non-spammy worker/crons here, need a way to support
	// very long running scripts at some point.
	if (idx >= 1500) {
		return -1;
	}

	array_init(&span);
	array_init(&starts);
	array_init(&stops);
	array_init(&annotations);

	add_assoc_stringl(&span, "n", category, category_len);
	add_assoc_zval(&span, "b", &starts);
	add_assoc_zval(&span, "e", &stops);

	if (parent > 0) {
		add_assoc_long(&span, "p", parent);
	}

	zend_hash_index_update(Z_ARRVAL_P(TWG(spans)), idx, &span);

	return idx;
}
Example #19
0
PHP_METHOD(MongoDB, createCollection)
{
	zval *cmd = NULL, *temp, *options = NULL;
	char *collection;
	int   collection_len;
	zend_bool capped = 0;
	long size = 0, max = 0;
	mongo_db *db;

	if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() TSRMLS_CC, "s|bll", &collection, &collection_len, &capped, &size, &max) == SUCCESS) {
		MAKE_STD_ZVAL(cmd);
		array_init(cmd);

		add_assoc_stringl(cmd, "create", collection, collection_len, 1);

		if (size) {
			add_assoc_long(cmd, "size", size);
		}

		if (capped) {
			php_error_docref(NULL TSRMLS_CC, E_DEPRECATED, "This method now accepts arguments as an options array instead of the three optional arguments for capped, size and max elements");
			add_assoc_bool(cmd, "capped", 1);
			if (max) {
				add_assoc_long(cmd, "max", max);
			}
		}

	} else if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|a", &collection, &collection_len, &options) == SUCCESS) {
		zval *tmp_copy;

		/* We create a new array here, instead of just tagging "create" =>
		 * <name> at the end of the array. This is because MongoDB wants the
		 * name of the command as first element in the array. */
		MAKE_STD_ZVAL(cmd);
		array_init(cmd);
		add_assoc_stringl(cmd, "create", collection, collection_len, 1);
		if (options) {
			zend_hash_merge(Z_ARRVAL_P(cmd), Z_ARRVAL_P(options), (copy_ctor_func_t) zval_add_ref, (void *) &tmp_copy, sizeof(zval *), 0);
		}
	} else {

		return;
	}

	PHP_MONGO_GET_DB(getThis());

	temp = php_mongo_runcommand(db->link, &db->read_pref, Z_STRVAL_P(db->name), Z_STRLEN_P(db->name), cmd, options, 0, NULL TSRMLS_CC);

	zval_ptr_dtor(&cmd);
	if (temp) {
		zval_ptr_dtor(&temp);
	}

	if (!EG(exception)) {
		zval *zcollection;

		/* get the collection we just created */
		zcollection = php_mongo_db_selectcollection(getThis(), collection, collection_len TSRMLS_CC);
		if (zcollection) {
			/* Only copy the zval into return_value if it worked. If
			 * zcollection is NULL here, an exception is set */
			RETURN_ZVAL(zcollection, 0, 1);
		}
	}
}
Example #20
0
/* {{{ php_parsekit_parse_op_array */
static void php_parsekit_parse_op_array(zval *return_value, zend_op_array *ops, long options TSRMLS_DC)
{
	zend_op *op;
	zval *tmpzval;
	int i = 0;

	/* TODO: Reorder / Organize */

	array_init(return_value);

	add_assoc_long(return_value, "type", (long)(ops->type));
	add_assoc_string(return_value, "type_name", php_parsekit_define_name(ops->type, php_parsekit_function_types, PHP_PARSEKIT_FUNCTYPE_UNKNOWN), 1);
	if (ops->function_name) {
		add_assoc_string(return_value, "function_name", ops->function_name, 1);
	} else if (options & PHP_PARSEKIT_ALWAYS_SET) {
		add_assoc_null(return_value, "function_name");
	}

#ifdef ZEND_ENGINE_2
/* ZE2 op_array members */
	if (ops->scope && ops->scope->name) {
		add_assoc_stringl(return_value, "scope", ops->scope->name, ops->scope->name_length, 1);
	} else if (options & PHP_PARSEKIT_ALWAYS_SET) {
		add_assoc_null(return_value, "scope");
	}
	add_assoc_long(return_value, "fn_flags", ops->fn_flags);
	if (ops->function_name && ops->prototype) {
		MAKE_STD_ZVAL(tmpzval);
		array_init(tmpzval);
		add_assoc_long(tmpzval, "type", ops->prototype->type);
		add_assoc_string(return_value, "type_name", php_parsekit_define_name(ops->prototype->type, php_parsekit_function_types, PHP_PARSEKIT_FUNCTYPE_UNKNOWN), 1);
		if (ops->prototype->common.function_name) {
			add_assoc_string(tmpzval, "function_name", ops->prototype->common.function_name, 1);
		} else if (options & PHP_PARSEKIT_ALWAYS_SET) {
			add_assoc_null(tmpzval, "function_name");
		}
		if (ops->prototype->common.scope && ops->prototype->common.scope->name) {
			add_assoc_stringl(tmpzval, "scope", ops->prototype->common.scope->name, ops->prototype->common.scope->name_length, 1);
		} else if (options & PHP_PARSEKIT_ALWAYS_SET) {
			add_assoc_null(tmpzval, "scope");
		}
		add_assoc_zval(return_value, "prototype", tmpzval);		
	} else if (options & PHP_PARSEKIT_ALWAYS_SET) {
		add_assoc_null(return_value, "prototype");
	}
	add_assoc_long(return_value, "num_args", ops->num_args);
	add_assoc_long(return_value, "required_num_args", ops->required_num_args);
	add_assoc_bool(return_value, "pass_rest_by_reference", ops->pass_rest_by_reference);

	if (ops->num_args && ops->arg_info) {
		MAKE_STD_ZVAL(tmpzval);
		php_parsekit_parse_arginfo(tmpzval, ops->num_args, ops->arg_info, options TSRMLS_CC);
		add_assoc_zval(return_value, "arg_info", tmpzval);
	} else if (options & PHP_PARSEKIT_ALWAYS_SET) {
		add_assoc_null(return_value, "arg_info");
	}

	if (ops->last_try_catch > 0) {
		MAKE_STD_ZVAL(tmpzval);
		array_init(tmpzval);
		for(i = 0; i < ops->last_try_catch; i++) {
			zval *tmp_zval;

			MAKE_STD_ZVAL(tmp_zval);
			array_init(tmp_zval);
			add_assoc_long(tmp_zval, "try_op", ops->try_catch_array[i].try_op);
			add_assoc_long(tmp_zval, "catch_op", ops->try_catch_array[i].catch_op);
			add_index_zval(tmpzval, i, tmp_zval);
		}
		add_assoc_zval(return_value, "try_catch_array", tmpzval);
	} else if (options & PHP_PARSEKIT_ALWAYS_SET) {
		add_assoc_null(return_value, "try_catch_array");
	}

#ifndef ZEND_ACC_CLOSURE
/* PHP<5.3 */
	add_assoc_bool(return_value, "uses_this", ops->uses_this);
#endif
	add_assoc_long(return_value, "line_start", ops->line_start);
	add_assoc_long(return_value, "line_end", ops->line_end);

	if (ops->doc_comment && ops->doc_comment_len) {
		add_assoc_stringl(return_value, "doc_comment", ops->doc_comment, ops->doc_comment_len, 1);
	} else if (options & PHP_PARSEKIT_ALWAYS_SET) {
		add_assoc_null(return_value, "doc_comment");
	}

#else
/* ZE1 op_array members */

	if (ops->arg_types) {
		zend_uchar *arg_types = ops->arg_types;
		int numargs = *(ops->arg_types);

		MAKE_STD_ZVAL(tmpzval);
		array_init(tmpzval);
		add_assoc_long(tmpzval, "arg_count", numargs);

		for(i = 0; i < numargs; i++) {
			add_next_index_long(tmpzval, arg_types[i+1]);
		}

		add_assoc_zval(return_value, "arg_types", tmpzval);

		/* Emulated arg_info */
		MAKE_STD_ZVAL(tmpzval);
		php_parsekit_derive_arginfo(tmpzval, ops, options TSRMLS_CC);
		add_assoc_zval(return_value, "arg_info", tmpzval);
	} else {
		MAKE_STD_ZVAL(tmpzval);
		array_init(tmpzval);
		add_assoc_long(tmpzval, "arg_count", 0);

		add_assoc_zval(return_value, "arg_types", tmpzval);
		add_assoc_null(return_value, "arg_info");
	}

	add_assoc_bool(return_value, "uses_global", ops->uses_globals);
#endif
/* ZE1 and ZE2 */

	add_assoc_bool(return_value, "return_reference", ops->return_reference);
	add_assoc_long(return_value, "refcount", *(ops->refcount));
	add_assoc_long(return_value, "last", ops->last);
	add_assoc_long(return_value, "size", ops->size);
	add_assoc_long(return_value, "T", ops->T);
	add_assoc_long(return_value, "last_brk_cont", ops->last_brk_cont);
	add_assoc_long(return_value, "current_brk_cont", ops->current_brk_cont);
	add_assoc_long(return_value, "backpatch_count", ops->backpatch_count);
	add_assoc_bool(return_value, "done_pass_two", ops->done_pass_two);

	if (ops->last_brk_cont > 0) {
		MAKE_STD_ZVAL(tmpzval);
		array_init(tmpzval);
		for(i = 0; i < ops->last_brk_cont; i++) {
			zval *tmp_zval;

			MAKE_STD_ZVAL(tmp_zval);
			array_init(tmp_zval);
			add_assoc_long(tmp_zval, "cont", ops->brk_cont_array[i].cont);
			add_assoc_long(tmp_zval, "brk", ops->brk_cont_array[i].brk);
			add_assoc_long(tmp_zval, "parent", ops->brk_cont_array[i].parent);
			add_index_zval(tmpzval, i, tmp_zval);
		}
		add_assoc_zval(return_value, "brk_cont_array", tmpzval);
	} else if (options & PHP_PARSEKIT_ALWAYS_SET) {
		add_assoc_null(return_value, "brk_cont_array");
	}

	if (ops->static_variables) {
		zval *tmp_zval;

		MAKE_STD_ZVAL(tmpzval);
		array_init(tmpzval);
		zend_hash_copy(HASH_OF(tmpzval), ops->static_variables, (copy_ctor_func_t) zval_add_ref, (void *) &tmp_zval, sizeof(zval *));
		add_assoc_zval(return_value, "static_variables", tmpzval);
	} else if (options & PHP_PARSEKIT_ALWAYS_SET) {
		add_assoc_null(return_value, "static_variables");
	}

	if (ops->start_op) {
		char sop[(sizeof(void *) * 2) + 1];

		snprintf(sop, sizeof(sop), "%X", (unsigned int)ops->start_op); 
		add_assoc_string(return_value, "start_op", sop, 1);
	} else if (options & PHP_PARSEKIT_ALWAYS_SET) {
		add_assoc_null(return_value, "start_op");
	}

	if (ops->filename) {
		add_assoc_string(return_value, "filename", ops->filename, 1);
	} else {
		add_assoc_null(return_value, "filename");
	}

	/* Leave this last, it simplifies readability */
	MAKE_STD_ZVAL(tmpzval);
	array_init(tmpzval);
	for(op = ops->opcodes, i = 0; op && i < ops->size; op++, i++) {
		zval *zop;

		MAKE_STD_ZVAL(zop);
		php_parsekit_parse_op(zop, ops, op, options TSRMLS_CC);
		add_next_index_zval(tmpzval, zop);
	}	
	add_assoc_zval(return_value, "opcodes", tmpzval);

}
Example #21
0
/* {{{ php_parserr */
static void php_parserr(PDNS_RECORD pRec, int type_to_fetch, int store, int raw, zval *subarray)
{
	int type;
	u_long ttl;

	type = pRec->wType;
	ttl = pRec->dwTtl;

	if (type_to_fetch != DNS_TYPE_ANY && type != type_to_fetch) {
		return;
	}

	if (!store) {
		return;
	}

	array_init(subarray);

	add_assoc_string(subarray, "host", pRec->pName);
	add_assoc_string(subarray, "class", "IN");
	add_assoc_long(subarray, "ttl", ttl);

	if (raw) {
		add_assoc_long(subarray, "type", type);
		add_assoc_stringl(subarray, "data", (char*) &pRec->Data, (uint) pRec->wDataLength);
		return;
	}

	switch (type) {
		case DNS_TYPE_A: {
			IN_ADDR ipaddr;
			ipaddr.S_un.S_addr = (pRec->Data.A.IpAddress);
			add_assoc_string(subarray, "type", "A");
			add_assoc_string(subarray, "ip", inet_ntoa(ipaddr));
			break;
		}

		case DNS_TYPE_MX:
			add_assoc_string(subarray, "type", "MX");
			add_assoc_long(subarray, "pri", pRec->Data.Srv.wPriority);
			/* no break; */

		case DNS_TYPE_CNAME:
			if (type == DNS_TYPE_CNAME) {
				add_assoc_string(subarray, "type", "CNAME");
			}
			/* no break; */

		case DNS_TYPE_NS:
			if (type == DNS_TYPE_NS) {
				add_assoc_string(subarray, "type", "NS");
			}
			/* no break; */

		case DNS_TYPE_PTR:
			if (type == DNS_TYPE_PTR) {
				add_assoc_string(subarray, "type", "PTR");
			}
			add_assoc_string(subarray, "target", pRec->Data.MX.pNameExchange);
			break;

		/* Not available on windows, the query is possible but there is no DNS_HINFO_DATA structure */
		case DNS_TYPE_HINFO:
		case DNS_TYPE_TEXT:
			{
				DWORD i = 0;
				DNS_TXT_DATA *data_txt = &pRec->Data.TXT;
				DWORD count = data_txt->dwStringCount;
				zend_string *txt;
				char *txt_dst;
				long txt_len = 0;
				zval entries;

				add_assoc_string(subarray, "type", "TXT");
				
				array_init(&entries);
				
				for (i = 0; i < count; i++) {
					txt_len += strlen(data_txt->pStringArray[i]) + 1;
				}

				txt = zend_string_safe_alloc(txt_len, 2, 0, 0);
				txt_dst = txt->val;
				for (i = 0; i < count; i++) {
					int len = strlen(data_txt->pStringArray[i]);
					memcpy(txt_dst, data_txt->pStringArray[i], len);
					add_next_index_stringl(&entries, data_txt->pStringArray[i], len);
					txt_dst += len;
				}
				txt->len = txt_dst - txt->val;
				add_assoc_str(subarray, "txt", txt);
				add_assoc_zval(subarray, "entries", &entries);
			}
			break;

		case DNS_TYPE_SOA:
			{
				DNS_SOA_DATA *data_soa = &pRec->Data.Soa;

				add_assoc_string(subarray, "type", "SOA");

				add_assoc_string(subarray, "mname", data_soa->pNamePrimaryServer);
				add_assoc_string(subarray, "rname", data_soa->pNameAdministrator);
				add_assoc_long(subarray, "serial", data_soa->dwSerialNo);
				add_assoc_long(subarray, "refresh", data_soa->dwRefresh);
				add_assoc_long(subarray, "retry", data_soa->dwRetry);
				add_assoc_long(subarray, "expire", data_soa->dwExpire);
				add_assoc_long(subarray, "minimum-ttl", data_soa->dwDefaultTtl);
			}
			break;

		case DNS_TYPE_AAAA:
			{
				DNS_AAAA_DATA *data_aaaa = &pRec->Data.AAAA;
				char buf[sizeof("AAAA:AAAA:AAAA:AAAA:AAAA:AAAA:AAAA:AAAA")];
				char *tp = buf;
				int i;
				unsigned short out[8];
				int have_v6_break = 0, in_v6_break = 0;

				for (i = 0; i < 4; ++i) {
					DWORD chunk = data_aaaa->Ip6Address.IP6Dword[i];
					out[i * 2]     = htons(LOWORD(chunk));
					out[i * 2 + 1] = htons(HIWORD(chunk));
				}

				for(i=0; i < 8; i++) {
					if (out[i] != 0) {
						if (tp > (u_char *)buf) {
							in_v6_break = 0;
							tp[0] = ':';
							tp++;
						}
						tp += sprintf((char*)tp,"%x", out[i]);
					} else {
						if (!have_v6_break) {
							have_v6_break = 1;
							in_v6_break = 1;
							tp[0] = ':';
							tp++;
						} else if (!in_v6_break) {
							tp[0] = ':';
							tp++;
							tp[0] = '0';
							tp++;
						}
					}
				}

				if (have_v6_break && in_v6_break) {
					tp[0] = ':';
					tp++;
				}
				tp[0] = '\0';

				add_assoc_string(subarray, "type", "AAAA");
				add_assoc_string(subarray, "ipv6", buf);
			}
			break;

#if 0
		/* Won't be implemented. A6 is deprecated. (Pierre) */
		case DNS_TYPE_A6:
			break;
#endif

		case DNS_TYPE_SRV:
			{
				DNS_SRV_DATA *data_srv = &pRec->Data.Srv;

				add_assoc_string(subarray, "type", "SRV");
				add_assoc_long(subarray, "pri", data_srv->wPriority);
				add_assoc_long(subarray, "weight", data_srv->wWeight);
				add_assoc_long(subarray, "port", data_srv->wPort);
				add_assoc_string(subarray, "target", data_srv->pNameTarget);
			}
			break;

#if _MSC_VER >= 1500
		case DNS_TYPE_NAPTR:
			{
				DNS_NAPTR_DATA * data_naptr = &pRec->Data.Naptr;

				add_assoc_string(subarray, "type", "NAPTR");
				add_assoc_long(subarray, "order", data_naptr->wOrder);
				add_assoc_long(subarray, "pref", data_naptr->wPreference);
				add_assoc_string(subarray, "flags", data_naptr->pFlags);
				add_assoc_string(subarray, "services", data_naptr->pService);
				add_assoc_string(subarray, "regex", data_naptr->pRegularExpression);
				add_assoc_string(subarray, "replacement", data_naptr->pReplacement);
			}
			break;
#endif

		default:
			/* unknown type */
			ZVAL_UNDEF(subarray);
			return;
	}

}
Example #22
0
/* {{{ php_parsekit_parse_node */
static void php_parsekit_parse_node(zval *return_value, zend_op_array *op_array, znode *node, long flags, long options TSRMLS_DC)
{
	array_init(return_value);
	add_assoc_long(return_value, "type", node->op_type);
	add_assoc_string(return_value, "type_name", php_parsekit_define_name(node->op_type, php_parsekit_nodetype_names, PHP_PARSEKIT_NODETYPE_UNKNOWN), 1);
	if (node->op_type == IS_CONST) {
		zval *tmpzval;
		MAKE_STD_ZVAL(tmpzval);
		*tmpzval = node->u.constant;
		zval_copy_ctor(tmpzval);
		Z_SET_REFCOUNT_P(tmpzval, 1);
		add_assoc_zval(return_value, "constant", tmpzval);
#ifdef IS_CV
/* PHP >= 5.1 */
	} else if (node->op_type == IS_CV) {
		add_assoc_long(return_value, "var", node->u.var);
		add_assoc_stringl(return_value, "varname", op_array->vars[node->u.var].name, op_array->vars[node->u.var].name_len, 1);
#endif
	} else {
		/* IS_VAR || IS_TMP_VAR || IS_UNUSED */
		char sop[(sizeof(void *) * 2) + 1];

		snprintf(sop, (sizeof(void *) * 2) + 1, "%X", (unsigned int)node->u.var); 

		if ((flags & PHP_PARSEKIT_VAR) ||
			(options & PHP_PARSEKIT_ALL_ELEMENTS)) {
			add_assoc_long(return_value, "var", node->u.var / sizeof(temp_variable));
		} else if (options & PHP_PARSEKIT_ALWAYS_SET) {
			add_assoc_null(return_value, "var");
		}

		if ((flags & PHP_PARSEKIT_OPLINE) ||
			(options & PHP_PARSEKIT_ALL_ELEMENTS)) {
			add_assoc_string(return_value, "opline_num", sop, 1);
		} else if (options & PHP_PARSEKIT_ALWAYS_SET) {
			add_assoc_null(return_value, "opline_num");
		}

		if ((flags & PHP_PARSEKIT_OPARRAY) ||
			(options & PHP_PARSEKIT_ALL_ELEMENTS)) {
			add_assoc_string(return_value, "op_array", sop, 1);
		} else if (options & PHP_PARSEKIT_ALWAYS_SET) {
			add_assoc_null(return_value, "op_array");
		}

#ifdef ZEND_ENGINE_2
/* ZE2 Only */
		if ((flags & PHP_PARSEKIT_JMP_ADDR) ||
			(options & PHP_PARSEKIT_ALL_ELEMENTS)) {
			add_assoc_string(return_value, "jmp_addr", sop, 1);
			snprintf(sop, sizeof(sop)-1, "%u",
					((unsigned int)((char*)node->u.var - (char*)op_array->opcodes))/sizeof(zend_op));
			add_assoc_string(return_value, "jmp_offset", sop, 1); 
		} else if (options & PHP_PARSEKIT_ALWAYS_SET) {
			add_assoc_null(return_value, "jmp_addr");
		}
#endif

		if ((flags & PHP_PARSEKIT_EA_TYPE) ||
			(options & PHP_PARSEKIT_ALL_ELEMENTS)) {
			add_assoc_long(return_value, "EA.type", node->u.EA.type);
		} else if (options & PHP_PARSEKIT_ALWAYS_SET) {
			add_assoc_null(return_value, "EA.type");
		}
	}
}
/* {{{ clm_get_device_info() */
static zval *clm_get_device_info(cl_device_id device TSRMLS_DC)
{
	const device_info_param_t *param = device_info_list;
	cl_int err = CL_SUCCESS;
	char buf[1024] = { 0 };
	size_t len = 0;
	cl_uint max_work_item_dimensions = 0;
	zval *zinfo;

	err = clGetDeviceInfo(device, CL_DEVICE_MAX_WORK_ITEM_DIMENSIONS,
	                      sizeof(max_work_item_dimensions),
	                      &max_work_item_dimensions, NULL);
	if (err != CL_SUCCESS) {
		max_work_item_dimensions = 0;
	}

	MAKE_STD_ZVAL(zinfo);
	array_init_size(zinfo, 64);

	while (param->key != NULL) {
		switch (param->type) {
			case PARAM_TYPE_BITFIELD: {
				cl_bitfield val = 0;
				err = clGetDeviceInfo(device, param->name, sizeof(val), &val, NULL);
				if (err == CL_SUCCESS) {
					long lval = (long)val;
					add_assoc_long(zinfo, param->key, lval);
				}
			}
			break;

			case PARAM_TYPE_BOOL: {
				cl_bool val = 0;
				err = clGetDeviceInfo(device, param->name, sizeof(val), &val, NULL);
				if (err == CL_SUCCESS) {
					zend_bool bval = (zend_bool)val;
					add_assoc_bool(zinfo, param->key, bval);
				}
			}
			break;

			case PARAM_TYPE_SIZE: {
				size_t val = 0;
				err = clGetDeviceInfo(device, param->name, sizeof(val), &val, NULL);
				if (err == CL_SUCCESS) {
					long lval = (long)val;
					add_assoc_long(zinfo, param->key, lval);
				}
			}
			break;

			case PARAM_TYPE_UINT: {
				cl_uint val = 0;
				err = clGetDeviceInfo(device, param->name, sizeof(val), &val, NULL);
				if (err == CL_SUCCESS) {
					long lval = (long)val;
					add_assoc_long(zinfo, param->key, lval);
				}
			}
			break;

			case PARAM_TYPE_ULONG: {
				cl_ulong val = 0;
				err = clGetDeviceInfo(device, param->name, sizeof(val), &val, NULL);
				if (err == CL_SUCCESS) {
					long lval = (long)val;
					add_assoc_long(zinfo, param->key, lval);
				}
			}
			break;

			case PARAM_TYPE_STRING: {
				err = clGetDeviceInfo(device, param->name, sizeof(buf), buf, &len);
				if (err == CL_SUCCESS) {
					add_assoc_stringl(zinfo, param->key, buf, len, 1);
				}
			}
			break;

			case PARAM_TYPE_PLATFORM: {
				cl_platform_id platform;
				err = clGetDeviceInfo(device, param->name, sizeof(platform), &platform, NULL);
				if (err == CL_SUCCESS) {
					zval *pinfo = clm_get_platform_info(platform TSRMLS_CC);
					add_assoc_zval(zinfo, param->key, pinfo);
				}
			}
			break;

			case PARAM_TYPE_MAX_WORK_ITEM_SIZES: {
				size_t siz = sizeof(size_t) * max_work_item_dimensions;
				size_t *sizes = ecalloc(max_work_item_dimensions, sizeof(size_t));
				if (!sizes) {
					break;
				}

				err = clGetDeviceInfo(device, CL_DEVICE_MAX_WORK_ITEM_SIZES, siz, sizes, NULL);
				if (err == CL_SUCCESS) {
					cl_uint i;
					zval *zsizes;
					MAKE_STD_ZVAL(zsizes);
					array_init_size(zsizes, max_work_item_dimensions);
					for (i = 0; i < max_work_item_dimensions; i++) {
						long lval = (long)sizes[i];
						add_next_index_long(zsizes, lval);
					}
					add_assoc_zval(zinfo, param->key, zsizes);
				}

				efree(sizes);
			}
			break;
		}

		if (err != CL_SUCCESS) {
			add_assoc_null(zinfo, param->key);
		}

		param++;
	}

	return zinfo;
}
Example #24
0
short int php_tmpl_pre_parse(t_template* tmpl) {
ulong_pair		*point;
register uchar	*p;

uchar			*buf;
uint			buf_len, buf_alloc, buf_this;

uint			i, j, len, close_idx;
t_tmpl_tag		*tag, *context;
zval			**ztag;

	if(FAILURE == php_tmpl_pre_parse_config(tmpl)) return FAILURE;

	/* Initialize variables */
	PAIR_INIT(point);
	buf_alloc = TMPL_MAX_TAG_LEN+4;
	buf = (uchar*)emalloc(buf_alloc);
	/* Searching algorythm will require larger buffer */
	(char*)ZV(tmpl->original) = erealloc(ZV(tmpl->original), 
		ZL(tmpl->original) + MAX(
			ZL(tmpl->tag_left), MAX(
				ZL(tmpl->tag_right), MAX(
					ZL(tmpl->ctx_ol), MAX(
						ZL(tmpl->ctx_or), MAX(
							ZL(tmpl->ctx_cl), ZL(tmpl->ctx_cr)
						)
					)
				)
			)
		)
	);

	/* Obtain positions of all tags and contexts */
	php_tmpl_pre_parse_search(tmpl, &point, TMPL_TAG, ZV(tmpl->tag_left), ZL(tmpl->tag_left));
	php_tmpl_pre_parse_search(tmpl, &point, TMPL_TAG_END, ZV(tmpl->tag_right), ZL(tmpl->tag_right));
	php_tmpl_pre_parse_search(tmpl, &point, TMPL_CONTEXT_OPEN_LEFT, ZV(tmpl->ctx_ol), ZL(tmpl->ctx_ol));
	php_tmpl_pre_parse_search(tmpl, &point, TMPL_CONTEXT_OPEN_RIGHT, ZV(tmpl->ctx_or), ZL(tmpl->ctx_or));
	php_tmpl_pre_parse_search(tmpl, &point, TMPL_CONTEXT_CLOSE_LEFT, ZV(tmpl->ctx_cl), ZL(tmpl->ctx_cl));
	if(ZL(tmpl->ctx_cr)) php_tmpl_pre_parse_search(tmpl, &point, TMPL_CONTEXT_CLOSE_RIGHT, ZV(tmpl->ctx_cr), ZL(tmpl->ctx_cr));

	if(0 == point[0].r) { TMPL_PRE_PARSE_CLEANUP; return SUCCESS; }

	qsort(&point[1], point[0].r, sizeof(ulong_pair), ULONG_PAIR_COMPARE);

	strcpy(buf, "/"); buf_len = buf_this = 1;

	/* Add root context */
	TAG_INIT(tag);
 	tag->loff = 0;
	tag->roff = ZL(tmpl->original); 
	tag->typ = TMPL_CONTEXT;
	ZVAL_STRINGL(tag->name, "/", 1, 1);
	add_assoc_stringl(tmpl->tags, buf, (char*)tag, sizeof(t_tmpl_tag), 0);
	context = tag;
	context->size = ZL(tmpl->original);

	/* Pre parse template */
	for(i=1; i <= point[0].r; i++) {
		switch(point[i].r) {
		case TMPL_TAG:
			p = ZV(tmpl->original) + point[i].l + ZL(tmpl->tag_left);

			TMPL_PRE_PARSE_GET_LEN(TMPL_TAG_END, ZL(tmpl->tag_left));

			if(buf_alloc < buf_len+len+1) {
				while(buf_alloc < buf_len+len+1) buf_alloc += TMPL_MAX_TAG_LEN;
				buf = (char*)erealloc(buf, buf_alloc);
			}
			if(buf_len > 1) buf[buf_len++] = '/';
			for(j=0; j < len; j++) buf[buf_len++] = tolower(*(p-len+j));
			buf[buf_len] = 0;

			TAG_INIT(tag);
			tag->loff = point[i].l;
			tag->roff = point[close_idx].l + ZL(tmpl->tag_right);
			tag->size = (tag->roff - tag->loff);
			tag->typ = TMPL_TAG;
			tag->tag_num = 1;
			tag->ctx = context;
			ZVAL_STRINGL(tag->name, buf+buf_len-len, len, 1);

			if(FAILURE == zend_hash_find(Z_ARRVAL_P(tmpl->tags), buf, buf_len+1, (void*)&ztag)) {
				/* There's no the tag defined in the current context. Creating one */
				add_assoc_stringl(tmpl->tags, buf, (char*)tag, sizeof(t_tmpl_tag), 0);
				context->tag_num++;
			} else {	/* add another instance of the tag in the same context */
				(Z_TMPL_TAG(ztag)->tag_num)++;
				add_next_index_stringl(tmpl->dup_tag, (char*)tag, sizeof(t_tmpl_tag), 0);
			}
			context->size -= tag->size;

			while(buf_len > 1 && buf[buf_len-1] != '/') buf[--buf_len] = 0;
			if(buf_len > 1) buf[--buf_len] = 0;

			i = close_idx;
			break;

		case TMPL_CONTEXT_OPEN_LEFT:
			p = ZV(tmpl->original) + point[i].l + ZL(tmpl->ctx_ol);

			TMPL_PRE_PARSE_GET_LEN(TMPL_CONTEXT_OPEN_RIGHT, ZL(tmpl->ctx_ol));

			if(buf_alloc < buf_len+len+1) {
				while(buf_alloc < buf_len+len+1) buf_alloc += TMPL_MAX_TAG_LEN;
				buf = (char*)erealloc(buf, buf_alloc);
			}
			if(buf_len > 1) buf[buf_len++] = '/';
			buf_this = buf_len;
			for(j=0; j < len; j++) buf[buf_len++] = tolower(*(p-len+j));
			buf[buf_len] = 0;

			if(SUCCESS == zend_hash_find(Z_ARRVAL_P(tmpl->tags), buf, buf_len+1, (void*)&ztag)) {
				php_error(E_ERROR, "Duplicate context \"%s\" in template (line: %d)", buf, php_tmpl_line_num(tmpl, p));
				TMPL_PRE_PARSE_CLEANUP;
				return FAILURE;
			}

			TAG_INIT(tag);
			tag->loff = point[i].l;
			tag->typ = TMPL_CONTEXT;
			tag->ctx = context;
			ZVAL_STRINGL(tag->name, buf+buf_len-len, len, 1);
			add_assoc_stringl(tmpl->tags, buf, (char*)tag, sizeof(t_tmpl_tag), 0);
			context->tag_num++;
			context = tag;

			i = close_idx;
			break;

		case TMPL_CONTEXT_CLOSE_LEFT:
			p = ZV(tmpl->original) + point[i].l + ZL(tmpl->ctx_cl);

			if(ZL(tmpl->ctx_cr)) {
	
				TMPL_PRE_PARSE_GET_LEN(TMPL_CONTEXT_CLOSE_RIGHT, ZL(tmpl->ctx_cl));

				for(j=0; j < len; j++)
					if(buf[buf_this+j] != tolower(*(p-len+j))) break;
				if(j < len) continue;

			}

			tag = context;
			tag->roff = ZL(tmpl->ctx_cr) ? point[close_idx].l + ZL(tmpl->ctx_cr) : point[i].l + ZL(tmpl->ctx_cl);
			tag->size += (tag->roff - tag->loff);

			while(buf_len > 1 && buf[buf_len-1] != '/') buf[--buf_len] = 0;
			if(buf_len > 1) buf[--buf_len] = 0;
			buf_this = buf_len;
			while(buf_this > 1 && buf[buf_this-1] != '/') --buf_this;
			if(FAILURE == zend_hash_find(Z_ARRVAL_P(tmpl->tags), buf, buf_len+1, (void*)&ztag)) {
				php_error(E_ERROR, "Can't find parent context in template. You should not see this message");
				TMPL_PRE_PARSE_CLEANUP;
				return FAILURE;
			}
			context = (t_tmpl_tag*)Z_STRVAL_PP(ztag);
			context->size -= tag->size;

			if(ZL(tmpl->ctx_cr)) i = close_idx;
			break;
		}
	}
	if(buf_len != 1) {
		php_error(E_ERROR, "Can't continue with an unterminated context \"%s\" in template (line:%d)", buf, php_tmpl_line_num(tmpl, ZV(tmpl->original) + context->loff));
		TMPL_PRE_PARSE_CLEANUP;
		return FAILURE;
	}
	tmpl->size = context->size;

	TMPL_PRE_PARSE_CLEANUP;
	return SUCCESS;
}
Example #25
0
static int add_stringl(zval* list, char* id, char* string, uint32_t length) {
	if(id) return add_assoc_stringl(list, id, string, length);
	else   return add_next_index_stringl(list, string, length);
}
static struct php_mimeheader_with_attributes *php_mimeheader_alloc_from_tok(php_rfc822_tokenized_t *toks)
{
	struct php_mimeheader_with_attributes *attr;
	int i, first_semi, next_semi, comments_before_semi, netscape_bug = 0;
	char *name_buf = NULL;
	smart_string value_buf = {0};
	int is_rfc2231_name = 0;
	char *check_name;
	int charset_p, prevcharset_p = 0;
	int namechanged, currentencoded = 0;

	attr = ecalloc(1, sizeof(struct php_mimeheader_with_attributes));

	array_init(&attr->attributes);

/*  php_rfc822_print_tokens(toks); */

	/* look for optional ; which separates optional attributes from the main value */
	for (first_semi = 2; first_semi < toks->ntokens; first_semi++)
		if (toks->tokens[first_semi].token == ';')
			break;

	attr->value = php_rfc822_recombine_tokens(toks, 2, first_semi - 2,
			PHP_RFC822_RECOMBINE_STRTOLOWER | PHP_RFC822_RECOMBINE_IGNORE_COMMENTS);

	if (first_semi < toks->ntokens)
		first_semi++;

	/* Netscape Bug: Messenger sometimes omits the semi when wrapping the
     * the header.
	 * That means we have to be even more clever than the spec says that
	 * we need to :-/
	 * */

	while (first_semi < toks->ntokens) {
		/* find the next ; */
		comments_before_semi = 0;
		for (next_semi = first_semi; next_semi < toks->ntokens; next_semi++) {
			if (toks->tokens[next_semi].token == ';')
				break;
			if (toks->tokens[next_semi].token == '(')
				comments_before_semi++;
		}


		i = first_semi;
		if (i < next_semi)	{
			i++;

			/* ignore comments */
			while (i < next_semi && toks->tokens[i].token == '(')
				i++;

			if (i < next_semi && toks->tokens[i].token == '=') {
				char *name, *value;

				/* Here, next_semi --> "name" and i --> "=", so skip "=" sign */
				i++;

				/* count those tokens; we expect "token = token" (3 tokens); if there are
				 * more than that, then something is quite possibly wrong - Netscape Bug! */
				if (next_semi < toks->ntokens
						&& toks->tokens[next_semi].token != ';'
						&& next_semi - first_semi - comments_before_semi > 3) {
					next_semi = i + 1;
					netscape_bug = 1;
				}

				name = php_rfc822_recombine_tokens(toks, first_semi, 1,
						PHP_RFC822_RECOMBINE_STRTOLOWER|PHP_RFC822_RECOMBINE_IGNORE_COMMENTS);
				value = php_rfc822_recombine_tokens(toks, i, next_semi - i,
						PHP_RFC822_RECOMBINE_IGNORE_COMMENTS);

				/* support rfc2231 mime parameter value
				 *
				 * Parameter Value Continuations:
				 *
				 * Content-Type: message/external-body; access-type=URL;
				 *	URL*0="ftp://";
				 *	URL*1="cs.utk.edu/pub/moore/bulk-mailer/bulk-mailer.tar"
				 *
				 * is semantically identical to
				 *
				 * Content-Type: message/external-body; access-type=URL;
				 *	URL="ftp://cs.utk.edu/pub/moore/bulk-mailer/bulk-mailer.tar"
				 *
				 * Original rfc2231 support by IceWarp Ltd. <*****@*****.**>
				 */
				check_name = strchr(name, '*');
				if (check_name) {
				  currentencoded = 1;

					/* Is last char * - charset encoding */
					charset_p = *(name+strlen(name)-1) == '*';

					/* Leave only attribute name without * */
					*check_name = 0;

					/* New item or continuous */
					if (NULL == name_buf) {
						namechanged = 0;
						name_buf = name;
					} else {
						namechanged = (strcmp(name_buf, name) != 0);
						if (!namechanged) {
							efree(name);
							name = 0;
						}
					}

					/* Check if name changed*/
					if (!namechanged) {

						/* Append string to buffer - check if to be encoded...	*/
						rfc2231_to_mime(&value_buf, value, charset_p, prevcharset_p);

						/* Mark previous */
						prevcharset_p = charset_p;
					}

					is_rfc2231_name = 1;
				}

				/* Last item was encoded	*/
				if (1 == is_rfc2231_name) {
					/* Name not null and name differs with new name*/
					if (name && strcmp(name_buf, name) != 0) {
						/* Finalize packet */
						rfc2231_to_mime(&value_buf, NULL, 0, prevcharset_p);

						add_assoc_stringl(&attr->attributes, name_buf, value_buf.c, value_buf.len);
						efree(name_buf);
						smart_string_free(&value_buf);

						prevcharset_p = 0;
						is_rfc2231_name = 0;
						name_buf = NULL;

						/* New non encoded name*/
						if (!currentencoded) {
							/* Add string*/
							add_assoc_string(&attr->attributes, name, value);
							efree(name);
						} else {		/* Encoded name changed*/
							if (namechanged) {
								/* Append string to buffer - check if to be encoded...	*/
								rfc2231_to_mime(&value_buf, value, charset_p, prevcharset_p);

								/* Mark */
								is_rfc2231_name = 1;
								name_buf = name;
								prevcharset_p = charset_p;
							}
						}

						namechanged = 0;
					}
				} else {
					add_assoc_string(&attr->attributes, name, value);
					efree(name);
				}

				efree(value);
			}
		}

		if (next_semi < toks->ntokens && !netscape_bug) {
			next_semi++;
		}

		first_semi = next_semi;
		netscape_bug = 0;
	}

	if (1 == is_rfc2231_name) {
		/* Finalize packet */
		rfc2231_to_mime(&value_buf, NULL, 0, prevcharset_p);

		add_assoc_stringl(&attr->attributes, name_buf, value_buf.c, value_buf.len);
		efree(name_buf);
		smart_string_free(&value_buf);
	}


	return attr;
}
Example #27
0
short int php_tmpl_pre_parse_config(t_template* tmpl) {
zval		*zparam;

char	*nam, *val;
uint	nam_len, val_len;

uchar	quote;
register char	*p;
register char	*start;

	nam = (char*)emalloc(TMPL_MAX_TAG_LEN); nam_len = 0;
	val = (char*)emalloc(TMPL_MAX_TAG_LEN); val_len = 0;
	MAKE_STD_ZVAL(zparam); array_init(zparam);

	sprintf(nam, "<%s", TMPL_CONFIG_TAG_NAME);
	nam_len = strlen(nam);

	if(!(p = strstrl_ex(ZV(tmpl->original), ZL(tmpl->original), nam, nam_len))) {
		TMPL_PRE_PARSE_CONFIG_CLEANUP;
		return SUCCESS;
	}

	start = p;
	p += nam_len;

	while(1) {

		/* skip delimiters and check for end of the tag */
		while(*p && '>' != *p && IS_DELIM(*p)) p++;
		if(!(*p) || '>' == *p) {	/* end of tag */
			if('>' == *p) {			/* hide this tag from result output */
				for(++p; *p;) *(start++) = *(++p);
				*start = 0;
				ZL(tmpl->original) -= (p-start);
			}
			break;
		}

		/* get parameter name */
		for(nam_len=0; *p && nam_len < TMPL_MAX_TAG_LEN && !IS_DELIM(*p) && '=' != *p; p++) nam[nam_len++] = *p;
		if(!(*p)) break; else nam[nam_len] = 0;

		if('=' != *p) {
			php_error(E_ERROR, "Invalid configuration tag parameter in template (line:%d)", php_tmpl_line_num(tmpl, p));
			TMPL_PRE_PARSE_CONFIG_CLEANUP;
			return FAILURE;
		}

		/* check if the value is quoted and get the value */
		p++;
		quote = IS_QUOTE(*p) ? *(p++) : 0;

		for(val_len=0; *p && val_len < TMPL_MAX_TAG_LEN && quote ? quote != *p : !IS_DELIM(*p); p++) val[val_len++] = *p;
		if(!(*p)) break; else val[val_len] = 0;

		if(quote && quote != *p) {
			php_error(E_ERROR, "Invalid parameter value in configuration tag in template (line:%d)", php_tmpl_line_num(tmpl, p));
			TMPL_PRE_PARSE_CONFIG_CLEANUP;
			return FAILURE;
		}
		if(quote) p++;

		add_assoc_stringl(zparam, nam, val, val_len, 1);
	}

	php_tmpl_process_param_array(tmpl, zparam);

	TMPL_PRE_PARSE_CONFIG_CLEANUP;
	return SUCCESS;
}
Example #28
0
/* {{{ apc_iterator_item */
static apc_iterator_item_t* apc_iterator_item_ctor(apc_iterator_t *iterator, slot_t **slot_pp) {
    zval *zvalue;
    char md5str[33];
    slot_t *slot = *slot_pp;
    apc_context_t ctxt = {0, };
    apc_iterator_item_t *item = ecalloc(1, sizeof(apc_iterator_item_t));

    if (slot->key.type == APC_CACHE_KEY_FILE) {
        /* keys should be unique and with stat=1 we could have multiple files with the same name, so use '<device> <inode>' instead */
#ifdef PHP_WIN32
        item->key_len = spprintf(&item->key, 0, "%I64d %I64d", slot->key.data.file.device, slot->key.data.file.inode);
#else
        item->key_len = spprintf(&item->key, 0, "%ld %ld", (ulong)slot->key.data.file.device, (ulong)slot->key.data.file.inode);
#endif
        item->filename_key = estrdup(slot->value->data.file.filename);
    } else if (slot->key.type == APC_CACHE_KEY_USER) {
        item->key = estrndup((char*)slot->key.data.user.identifier, slot->key.data.user.identifier_len);
        item->key_len = slot->key.data.user.identifier_len;
        item->filename_key = item->key; 
    } else if (slot->key.type == APC_CACHE_KEY_FPFILE) {
        item->key = estrndup((char*)slot->key.data.fpfile.fullpath, slot->key.data.fpfile.fullpath_len);
        item->key_len = slot->key.data.fpfile.fullpath_len;
    } else {
        apc_eprint("Internal error, invalid entry type.");
    }

    ALLOC_INIT_ZVAL(item->value);
    array_init(item->value);

    if (APC_ITER_TYPE & iterator->format) {
        if(slot->value->type == APC_CACHE_ENTRY_FILE) {
            add_assoc_string(item->value, "type", "file", 1);
        } else if(slot->value->type == APC_CACHE_ENTRY_USER) {
            add_assoc_string(item->value, "type", "user", 1);
        }
    }
    if (APC_ITER_FILENAME & iterator->format) {
        if(slot->value->type == APC_CACHE_ENTRY_FILE) {
            if (slot->key.type == APC_CACHE_KEY_FILE) {
              add_assoc_string(item->value, "filename", slot->value->data.file.filename, 1);
            } else {  /* APC_CACHE_FPFILE */
              add_assoc_string(item->value, "filename", (char*)slot->key.data.fpfile.fullpath, 1);
            }
        }
    }
    if (APC_ITER_DEVICE & iterator->format) {
        if(slot->key.type == APC_CACHE_KEY_FILE) {
#ifdef PHP_WIN32
			char buf[20];
			sprintf(buf, "%I64d", slot->key.data.file.device);
			add_assoc_string(item->value, "device", buf, 1);
#else
            add_assoc_long(item->value, "device", slot->key.data.file.device);
#endif
        }
    }
    if (APC_ITER_INODE & iterator->format) {
        if(slot->key.type == APC_CACHE_KEY_FILE) {
#ifdef PHP_WIN32
			char buf[20];
			sprintf(buf, "%I64d", slot->key.data.file.device);
			add_assoc_string(item->value, "device", buf, 1);
#else
            add_assoc_long(item->value, "inode", slot->key.data.file.inode);
#endif
        }
    }
    if (APC_ITER_KEY & iterator->format) {
        add_assoc_stringl(item->value, "key", item->key, item->key_len, 1);
    }
    if (APC_ITER_VALUE & iterator->format) {
        if(slot->value->type == APC_CACHE_ENTRY_USER) {

            ctxt.pool = apc_pool_create(APC_UNPOOL, apc_php_malloc, apc_php_free, NULL, NULL);
            ctxt.copy = APC_COPY_OUT_USER;

            MAKE_STD_ZVAL(zvalue);
            apc_cache_fetch_zval(zvalue, slot->value->data.user.val, &ctxt);
            apc_pool_destroy(ctxt.pool);
            add_assoc_zval(item->value, "value", zvalue);
        }
    }
    if (APC_ITER_MD5 & iterator->format) {
        if(slot->value->type == APC_CACHE_ENTRY_FILE) {
            if(slot->key.md5) {
                make_digest(md5str, slot->key.md5);
                add_assoc_string(item->value, "md5", md5str, 1);
            }
        }
    }
    if (APC_ITER_NUM_HITS & iterator->format) {
        add_assoc_long(item->value, "num_hits", slot->num_hits);
    }
    if (APC_ITER_MTIME & iterator->format) {
        add_assoc_long(item->value, "mtime", slot->key.mtime);
    }
    if (APC_ITER_CTIME & iterator->format) {
        add_assoc_long(item->value, "creation_time", slot->creation_time);
    }
    if (APC_ITER_DTIME & iterator->format) {
        add_assoc_long(item->value, "deletion_time", slot->deletion_time);
    }
    if (APC_ITER_ATIME & iterator->format) {
        add_assoc_long(item->value, "access_time", slot->access_time);
    }
    if (APC_ITER_REFCOUNT & iterator->format) {
        add_assoc_long(item->value, "ref_count", slot->value->ref_count);
    }
    if (APC_ITER_MEM_SIZE & iterator->format) {
        add_assoc_long(item->value, "mem_size", slot->value->mem_size);
    }
    if (APC_ITER_TTL & iterator->format) {
        if(slot->value->type == APC_CACHE_ENTRY_USER) {
            add_assoc_long(item->value, "ttl", slot->value->data.user.ttl);
        }
    }

    return item;
}
Example #29
0
/* {{{ apc_cache_info */
PHP_APCU_API zval apc_cache_info(apc_cache_t* cache, zend_bool limited)
{
    zval info;
    zval list;
    zval gc;
    zval slots;
    apc_cache_slot_t* p;
    zend_ulong i, j;

    if (!cache) {
		ZVAL_NULL(&info);

        return info;
    }

    /* read lock header */
    APC_RLOCK(cache->header);

    array_init(&info);
    add_assoc_long(&info, "num_slots", cache->nslots);
    add_assoc_long(&info, "ttl", cache->ttl);
    add_assoc_double(&info, "num_hits", (double)cache->header->nhits);
    add_assoc_double(&info, "num_misses", (double)cache->header->nmisses);
    add_assoc_double(&info, "num_inserts", (double)cache->header->ninserts);
    add_assoc_long(&info,   "num_entries", cache->header->nentries);
    add_assoc_double(&info, "expunges", (double)cache->header->nexpunges);
    add_assoc_long(&info, "start_time", cache->header->stime);
    add_assoc_double(&info, "mem_size", (double)cache->header->mem_size);

#if APC_MMAP
    add_assoc_stringl(&info, "memory_type", "mmap", sizeof("mmap")-1);
#else
    add_assoc_stringl(&info, "memory_type", "IPC shared", sizeof("IPC shared")-1);
#endif

    if (!limited) {
        /* For each hashtable slot */
        array_init(&list);
        array_init(&slots);

        for (i = 0; i < cache->nslots; i++) {
            p = cache->slots[i];
            j = 0;
            for (; p != NULL; p = p->next) {
                zval link = apc_cache_link_info(cache, p);
                add_next_index_zval(&list, &link);
                j++;
            }
            if(j != 0) {
                add_index_long(&slots, (ulong)i, j);
            }
        }

        /* For each slot pending deletion */
        array_init(&gc);

        for (p = cache->header->gc; p != NULL; p = p->next) {
            zval link = apc_cache_link_info(cache, p);
            add_next_index_zval(&gc, &link);
        }
        
        add_assoc_zval(&info, "cache_list", &list);
        add_assoc_zval(&info, "deleted_list", &gc);
        add_assoc_zval(&info, "slot_distribution", &slots);
    }
	
	/* unlock header */
	APC_RUNLOCK(cache->header);

    return info;
}
Example #30
0
static int http_onReceive(swFactory *factory, swEventData *req)
{
    TSRMLS_FETCH_FROM_CTX(sw_thread_ctx ? sw_thread_ctx : NULL);

    int fd = req->info.fd;


//    swTrace("on receive:%s pid:%d\n", zdata, getpid());
    swConnection *conn = swServer_connection_get(SwooleG.serv, fd);

    if(conn->websocket_status == WEBSOCKET_STATUS_HANDSHAKE)  //websocket callback
    {
        zval *zdata = php_swoole_get_data(req TSRMLS_CC);
        swTrace("on message callback\n");
        char *buf = Z_STRVAL_P(zdata);
        long fin = buf[0] ? 1 : 0;
        long opcode = buf[1] ? 1 : 0;
        buf+=2;
        zval *zresponse;
        MAKE_STD_ZVAL(zresponse);
        object_init_ex(zresponse, swoole_http_wsresponse_class_entry_ptr);
        //socket fd
        zend_update_property_long(swoole_http_wsresponse_class_entry_ptr, zresponse, ZEND_STRL("fd"), fd TSRMLS_CC);
        zend_update_property_long(swoole_http_wsresponse_class_entry_ptr, zresponse, ZEND_STRL("fin"), fin TSRMLS_CC);
        zend_update_property_long(swoole_http_wsresponse_class_entry_ptr, zresponse, ZEND_STRL("opcode"), opcode TSRMLS_CC);
        zend_update_property_stringl(swoole_http_wsresponse_class_entry_ptr, zresponse, ZEND_STRL("data"), buf, (Z_STRLEN_P(zdata)-2) TSRMLS_CC);

        zval **args[1];
        args[0] = &zresponse;
        zval *retval;
        if (call_user_function_ex(EG(function_table), NULL, php_sw_http_server_callbacks[1], &retval, 1, args, 0, NULL TSRMLS_CC) == FAILURE)
        {
            zval_ptr_dtor(&zdata);
            php_error_docref(NULL TSRMLS_CC, E_WARNING, "onMessage handler error");
        }
        swTrace("===== message callback end======");
        if (EG(exception))
        {
            zval_ptr_dtor(&zdata);
            zend_exception_error(EG(exception), E_ERROR TSRMLS_CC);
        }
        if (retval)
        {
            zval_ptr_dtor(&retval);
        }
        zval_ptr_dtor(&zdata);
        return SW_OK;
    }

    http_client *client = swHashMap_find_int(php_sw_http_clients, fd);



    if (!client)
    {
        client = http_client_new(fd TSRMLS_CC);
    }


    php_http_parser *parser = &client->parser;

    /**
     * create request and response object
     */
    http_request_new(client TSRMLS_CC);

    parser->data = client;
    php_http_parser_init(parser, PHP_HTTP_REQUEST);



    zval *zdata = php_swoole_get_data(req TSRMLS_CC);
    //server info
    zval *_request;
    MAKE_STD_ZVAL(_request);
    array_init(_request);
    zend_update_property(swoole_http_request_class_entry_ptr, client->zrequest, ZEND_STRL("request"), _request TSRMLS_CC);
    size_t n = php_http_parser_execute(parser, &http_parser_settings, Z_STRVAL_P(zdata), Z_STRLEN_P(zdata));
    zval_ptr_dtor(&zdata);
    if (n < 0)
    {
        swWarn("php_http_parser_execute failed.");
        if(conn->websocket_status == WEBSOCKET_STATUS_CONNECTION)
        {
            SwooleG.serv->factory.end(&SwooleG.serv->factory, fd);
        }

    }
    else
    {
        if(conn->websocket_status == WEBSOCKET_STATUS_CONNECTION) // need handshake
        {
            if(php_sw_http_server_callbacks[2] == NULL) {
                int ret = websocket_handshake(client);
                http_request_free(client TSRMLS_CC);
                if (ret == SW_ERR) {
                    swTrace("websocket handshake error\n");
                    SwooleG.serv->factory.end(&SwooleG.serv->factory, fd);
                } else {
                    handshake_success(fd);
                    swTrace("websocket handshake_success\n");
                    return SW_OK;
                }
                return ret;
            }
        }

        zval *retval;
        zval **args[2];
        zval *zrequest = client->zrequest;

    	//server info
    	zval *zserver;
    	MAKE_STD_ZVAL(zserver);
    	array_init(zserver);
    	zend_update_property(swoole_http_request_class_entry_ptr, zrequest, ZEND_STRL("server"), zserver TSRMLS_CC);

        switch (parser->method) {
            case PHP_HTTP_GET: add_assoc_string(zserver, "REQUEST_METHOD", "GET", 1);break;
            case PHP_HTTP_POST: add_assoc_string(zserver, "REQUEST_METHOD", "POST", 1);break;
            case PHP_HTTP_HEAD: add_assoc_string(zserver, "REQUEST_METHOD", "HEAD", 1);break;
            case PHP_HTTP_PUT: add_assoc_string(zserver, "REQUEST_METHOD", "PUT", 1);break;
            case PHP_HTTP_DELETE: add_assoc_string(zserver, "REQUEST_METHOD", "DELETE", 1);break;
            case PHP_HTTP_PATCH: add_assoc_string(zserver, "REQUEST_METHOD", "PATCH", 1);break;
                /* pathological */
            case PHP_HTTP_CONNECT: add_assoc_string(zserver, "REQUEST_METHOD", "CONNECT", 1);break;
            case PHP_HTTP_OPTIONS: add_assoc_string(zserver, "REQUEST_METHOD", "OPTIONS", 1);break;
            case PHP_HTTP_TRACE: add_assoc_string(zserver, "REQUEST_METHOD", "TRACE", 1);break;
                /* webdav */
            case PHP_HTTP_COPY: add_assoc_string(zserver, "REQUEST_METHOD", "COPY", 1);break;
            case PHP_HTTP_LOCK: add_assoc_string(zserver, "REQUEST_METHOD", "LOCK", 1);break;
            case PHP_HTTP_MKCOL: add_assoc_string(zserver, "REQUEST_METHOD", "MKCOL", 1);break;
            case PHP_HTTP_MOVE: add_assoc_string(zserver, "REQUEST_METHOD", "MOVE", 1);break;
            case PHP_HTTP_PROPFIND: add_assoc_string(zserver, "REQUEST_METHOD", "PROPFIND", 1);break;
            case PHP_HTTP_PROPPATCH: add_assoc_string(zserver, "REQUEST_METHOD", "PROPPATCH", 1);break;
            case PHP_HTTP_UNLOCK: add_assoc_string(zserver, "REQUEST_METHOD", "UNLOCK", 1);break;
                /* subversion */
            case PHP_HTTP_REPORT: add_assoc_string(zserver, "REQUEST_METHOD", "REPORT", 1);break;
            case PHP_HTTP_MKACTIVITY: add_assoc_string(zserver, "REQUEST_METHOD", "MKACTIVITY", 1);break;
            case PHP_HTTP_CHECKOUT: add_assoc_string(zserver, "REQUEST_METHOD", "CHECKOUT", 1);break;
            case PHP_HTTP_MERGE: add_assoc_string(zserver, "REQUEST_METHOD", "MERGE", 1);break;
                /* upnp */
            case PHP_HTTP_MSEARCH: add_assoc_string(zserver, "REQUEST_METHOD", "MSEARCH", 1);break;
            case PHP_HTTP_NOTIFY: add_assoc_string(zserver, "REQUEST_METHOD", "NOTIFY", 1);break;
            case PHP_HTTP_SUBSCRIBE: add_assoc_string(zserver, "REQUEST_METHOD", "SUBSCRIBE", 1);break;
            case PHP_HTTP_UNSUBSCRIBE: add_assoc_string(zserver, "REQUEST_METHOD", "UNSUBSCRIBE", 1);break;
            case PHP_HTTP_NOT_IMPLEMENTED: add_assoc_string(zserver, "REQUEST_METHOD", "GET", 1);break;
        }

//    	if (parser->method == PHP_HTTP_POST)
//    	{
//    		add_assoc_string(zserver, "REQUEST_METHOD", "POST", 1);
//    	}
//    	else
//    	{
//    		add_assoc_string(zserver, "REQUEST_METHOD", "GET", 1);
//    	}

    	add_assoc_stringl(zserver, "REQUEST_URI", client->request.path, client->request.path_len, 1);
    	add_assoc_stringl(zserver, "PATH_INFO", client->request.path, client->request.path_len, 1);
    	add_assoc_long_ex(zserver,  ZEND_STRS("REQUEST_TIME"), SwooleGS->now);

    	swConnection *conn = swServer_connection_get(SwooleG.serv, fd);
    	add_assoc_long(zserver, "SERVER_PORT", SwooleG.serv->connection_list[conn->from_fd].addr.sin_port);
    	add_assoc_long(zserver, "REMOTE_PORT", ntohs(conn->addr.sin_port));
    	add_assoc_string(zserver, "REMOTE_ADDR", inet_ntoa(conn->addr.sin_addr), 1);

    	if (client->request.version == 101)
    	{
    		add_assoc_string(zserver, "SERVER_PROTOCOL", "HTTP/1.1", 1);
    	}
    	else
    	{
            add_assoc_string(zserver, "SERVER_PROTOCOL", "HTTP/1.0", 1);
        }
        add_assoc_string(zserver, "SERVER_SOFTWARE", SW_HTTP_SERVER_SOFTWARE, 1);
        add_assoc_string(zserver, "GATEWAY_INTERFACE", SW_HTTP_SERVER_SOFTWARE, 1);
//        ZEND_SET_SYMBOL(&EG(symbol_table), "_SERVER", zserver);


    	zval *zresponse;
    	MAKE_STD_ZVAL(zresponse);
    	object_init_ex(zresponse, swoole_http_response_class_entry_ptr);

    	//socket fd
    	zend_update_property_long(swoole_http_response_class_entry_ptr, zresponse, ZEND_STRL("fd"), client->fd TSRMLS_CC);
    	client->zresponse = zresponse;

        args[0] = &zrequest;
        args[1] = &zresponse;

        int called = 0;
        if(conn->websocket_status == WEBSOCKET_STATUS_CONNECTION)
        {
            called = 2;
        }
        if (call_user_function_ex(EG(function_table), NULL, php_sw_http_server_callbacks[called], &retval, 2, args, 0, NULL TSRMLS_CC) == FAILURE)
        {
            php_error_docref(NULL TSRMLS_CC, E_WARNING, "onRequest handler error");
        }
        if (EG(exception))
        {
            zend_exception_error(EG(exception), E_ERROR TSRMLS_CC);
        }
        if (retval)
        {
            zval_ptr_dtor(&retval);
        }
        swTrace("======call end======\n");
        if(called == 2) {
            handshake_success(fd);
        }
    }
    return SW_OK;
}