コード例 #1
0
ファイル: line.c プロジェクト: golovanov/cphalcon
/**
 * Applies a format to a message before sent it to the internal log
 *
 * @param string $message
 * @param int $type
 * @param int $timestamp
 * @return string
 */
PHP_METHOD(Phalcon_Logger_Formatter_Line, format) {

    zval *message, *type, *timestamp, *format = NULL, *date_format;
    zval *date, *date_wildcard, *new_format = NULL, *type_string;
    zval *type_wildcard, *message_wildcard, *eol;

    PHALCON_MM_GROW();

    phalcon_fetch_params(1, 3, 0, &message, &type, &timestamp);

    PHALCON_OBS_VAR(format);
    phalcon_read_property_this(&format, this_ptr, SL("_format"), PH_NOISY_CC);

    /**
     * Check if the format has the %date% placeholder
     */
    if (phalcon_memnstr_str(format, SL("%date%"))) {
        PHALCON_OBS_VAR(date_format);
        phalcon_read_property_this(&date_format, this_ptr, SL("_dateFormat"), PH_NOISY_CC);

        PHALCON_INIT_VAR(date);
        phalcon_call_func_p2(date, "date", date_format, timestamp);

        PHALCON_INIT_VAR(date_wildcard);
        ZVAL_STRING(date_wildcard, "%date%", 1);

        PHALCON_INIT_VAR(new_format);
        phalcon_fast_str_replace(new_format, date_wildcard, date, format);
    } else {
        PHALCON_CPY_WRT(new_format, format);
    }

    /**
     * Check if the format has the %type% placeholder
     */
    if (phalcon_memnstr_str(format, SL("%type%"))) {
        PHALCON_INIT_VAR(type_string);
        phalcon_call_method_p1(type_string, this_ptr, "gettypestring", type);

        PHALCON_INIT_VAR(type_wildcard);
        ZVAL_STRING(type_wildcard, "%type%", 1);

        PHALCON_INIT_NVAR(format);
        phalcon_fast_str_replace(format, type_wildcard, type_string, new_format);
    } else {
        PHALCON_CPY_WRT(format, new_format);
    }

    PHALCON_INIT_VAR(message_wildcard);
    ZVAL_STRING(message_wildcard, "%message%", 1);

    PHALCON_INIT_NVAR(new_format);
    phalcon_fast_str_replace(new_format, message_wildcard, message, format);

    PHALCON_INIT_VAR(eol);
    ZVAL_STRING(eol, PHP_EOL, 1);
    PHALCON_CONCAT_VV(return_value, new_format, eol);

    RETURN_MM();
}
コード例 #2
0
ファイル: line.c プロジェクト: dreamsxin/cphalcon7
/**
 * Applies a format to a message before sent it to the internal log
 *
 * @param string $message
 * @param int $type
 * @param int $timestamp
 * @param array $context
 * @return string
 */
PHP_METHOD(Phalcon_Logger_Formatter_Line, format){

	zval *message, *type, *timestamp, *context, format = {}, date_format = {}, date = {}, date_wildcard = {}, new_format = {}, type_string = {}, type_wildcard = {}, message_wildcard = {};

	phalcon_fetch_params(0, 4, 0, &message, &type, &timestamp, &context);

	phalcon_return_property(&format, getThis(), SL("_format"));

	/** 
	 * Check if the format has the %date% placeholder
	 */
	if (phalcon_memnstr_str(&format, SL("%date%"))) {
		phalcon_return_property(&date_format, getThis(), SL("_dateFormat"));

		phalcon_date(&date, &date_format, timestamp);

		ZVAL_STRING(&date_wildcard, "%date%");

		PHALCON_STR_REPLACE(&new_format, &date_wildcard, &date, &format);
	} else {
		PHALCON_CPY_WRT(&new_format, &format);
	}

	/** 
	 * Check if the format has the %type% placeholder
	 */
	if (phalcon_memnstr_str(&format, SL("%type%"))) {
		PHALCON_CALL_METHODW(&type_string, getThis(), "gettypestring", type);

		ZVAL_STRING(&type_wildcard, "%type%");

		PHALCON_STR_REPLACE(&format, &type_wildcard, &type_string, &new_format);
	} else {
		PHALCON_CPY_WRT(&format, &new_format);
	}

	ZVAL_STRING(&message_wildcard, "%message%");

	PHALCON_STR_REPLACE(&new_format, &message_wildcard, message, &format);

	if (Z_TYPE_P(context) == IS_ARRAY && zend_hash_num_elements(Z_ARRVAL_P(context)) > 0) {
		PHALCON_CALL_METHODW(&format, getThis(), "interpolate", &new_format, context);
	}
	else {
		PHALCON_CPY_WRT(&format, &new_format);
	}

	PHALCON_CONCAT_VS(return_value, &format, PHP_EOL);
}
コード例 #3
0
ファイル: stream.c プロジェクト: googlle/cphalcon7
/**
 * Phalcon\Logger\Adapter\Stream constructor
 *
 * @param string $name
 * @param array $options
 */
PHP_METHOD(Phalcon_Logger_Adapter_Stream, __construct){

	zval *name, *options = NULL, mode = {}, stream = {};

	phalcon_fetch_params(0, 1, 1, &name, &options);
	PHALCON_ENSURE_IS_STRING(name);

	if (!options) {
		options = &PHALCON_GLOBAL(z_null);
	}

	if (phalcon_array_isset_fetch_str(&mode, options, SL("mode"))) {
		if (phalcon_memnstr_str(&mode, SL("r"))) {
			PHALCON_THROW_EXCEPTION_STRW(phalcon_logger_exception_ce, "Stream must be opened in append or write mode");
			return;
		}
	} else {
		ZVAL_STRING(&mode, "ab");
	}

	/** 
	 * We use 'fopen' to respect to open-basedir directive
	 */
	PHALCON_CALL_FUNCTIONW(&stream, "fopen", name, &mode);
	if (Z_TYPE(stream) != IS_RESOURCE) {
		zend_throw_exception_ex(phalcon_logger_exception_ce, 0, "Cannot open stream '%s'", Z_STRVAL_P(name));
	} else {
		phalcon_update_property_this(getThis(), SL("_stream"), &stream);
	}
}
コード例 #4
0
ファイル: request.c プロジェクト: Tigerlee1987/cphalcon
/**
 * Checks whether request has been made using SOAP
 *
 * @return boolean
 */
PHP_METHOD(Phalcon_Http_Request, isSoapRequested) {

    zval *server = NULL, *content_type;
    zval *g0 = NULL;
    int eval_int;

    PHALCON_MM_GROW();

    phalcon_get_global(&g0, SL("_SERVER")+1 TSRMLS_CC);
    PHALCON_CPY_WRT(server, g0);
    eval_int = phalcon_array_isset_string(server, SS("HTTP_SOAPACTION"));
    if (eval_int) {
        PHALCON_MM_RESTORE();
        RETURN_TRUE;
    } else {
        eval_int = phalcon_array_isset_string(server, SS("CONTENT_TYPE"));
        if (eval_int) {
            PHALCON_INIT_VAR(content_type);
            phalcon_array_fetch_string(&content_type, server, SL("CONTENT_TYPE"), PH_NOISY_CC);
            if (phalcon_memnstr_str(content_type, SL("application/soap+xml") TSRMLS_CC)) {
                PHALCON_MM_RESTORE();
                RETURN_TRUE;
            }
        }
    }

    PHALCON_MM_RESTORE();
    RETURN_FALSE;
}
コード例 #5
0
ファイル: request.c プロジェクト: BlueShark/cphalcon
/**
 * Gets most possible client IPv4 Address. This method search in $_SERVER['REMOTE_ADDR'] and optionally in $_SERVER['HTTP_X_FORWARDED_FOR']
 *
 * @param boolean $trustForwardedHeader
 * @return string
 */
PHP_METHOD(Phalcon_Http_Request, getClientAddress){

	zval *trust_forwarded_header = NULL, *address = NULL, *_SERVER;
	zval *comma, *addresses, *first;

	PHALCON_MM_GROW();

	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|z", &trust_forwarded_header) == FAILURE) {
		RETURN_MM_NULL();
	}

	if (!trust_forwarded_header) {
		PHALCON_INIT_VAR(trust_forwarded_header);
		ZVAL_BOOL(trust_forwarded_header, 0);
	}
	
	PHALCON_INIT_VAR(address);
	
	/** 
	 * Proxies uses this IP
	 */
	phalcon_get_global(&_SERVER, SS("_SERVER") TSRMLS_CC);
	if (phalcon_array_isset_string(_SERVER, SS("HTTP_X_FORWARDED_FOR"))) {
		if (zend_is_true(trust_forwarded_header)) {
			PHALCON_OBS_NVAR(address);
			phalcon_array_fetch_string(&address, _SERVER, SL("HTTP_X_FORWARDED_FOR"), PH_NOISY_CC);
		}
	}
	
	if (Z_TYPE_P(address) == IS_NULL) {
		if (phalcon_array_isset_string(_SERVER, SS("REMOTE_ADDR"))) {
			PHALCON_OBS_NVAR(address);
			phalcon_array_fetch_string(&address, _SERVER, SL("REMOTE_ADDR"), PH_NOISY_CC);
		}
	}
	
	if (Z_TYPE_P(address) == IS_STRING) {
		if (phalcon_memnstr_str(address, SL(",") TSRMLS_CC)) {
			/** 
			 * The client address has multiples parts, only return the first part
			 */
			PHALCON_INIT_VAR(comma);
			ZVAL_STRING(comma, ",", 1);
	
			PHALCON_INIT_VAR(addresses);
			phalcon_fast_explode(addresses, comma, address TSRMLS_CC);
	
			PHALCON_OBS_VAR(first);
			phalcon_array_fetch_long(&first, addresses, 0, PH_NOISY_CC);
			RETURN_CCTOR(first);
		}
	
		RETURN_CCTOR(address);
	}
	
	RETURN_MM_FALSE;
}
コード例 #6
0
ファイル: file.c プロジェクト: alantonilopez/cphalcon
/**
 * Phalcon\Logger\Adapter\File constructor
 *
 * @param string $name
 * @param array $options
 */
PHP_METHOD(Phalcon_Logger_Adapter_File, __construct){

	zval *name, *options = NULL, *mode = NULL, *handler, *exception_message;
	zval *a0 = NULL;
	int eval_int;

	PHALCON_MM_GROW();

	
	PHALCON_INIT_VAR(a0);
	array_init(a0);
	zend_update_property(phalcon_logger_adapter_file_ce, this_ptr, SL("_quenue"), a0 TSRMLS_CC);
	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|z", &name, &options) == FAILURE) {
		PHALCON_MM_RESTORE();
		RETURN_NULL();
	}

	if (!options) {
		PHALCON_INIT_NVAR(options);
		array_init(options);
	}
	
	eval_int = phalcon_array_isset_string(options, SS("mode"));
	if (eval_int) {
		PHALCON_INIT_VAR(mode);
		phalcon_array_fetch_string(&mode, options, SL("mode"), PH_NOISY_CC);
	} else {
		PHALCON_INIT_NVAR(mode);
		ZVAL_STRING(mode, "ab", 1);
	}
	if (phalcon_memnstr_str(mode, SL("r") TSRMLS_CC)) {
		PHALCON_THROW_EXCEPTION_STR(phalcon_logger_exception_ce, "Logger must be opened in append or write mode");
		return;
	}
	
	PHALCON_INIT_VAR(handler);
	PHALCON_CALL_FUNC_PARAMS_2(handler, "fopen", name, mode);
	if (!zend_is_true(handler)) {
		PHALCON_INIT_VAR(exception_message);
		PHALCON_CONCAT_SVS(exception_message, "Can't open log file at '", name, "'");
		PHALCON_THROW_EXCEPTION_ZVAL(phalcon_logger_exception_ce, exception_message);
		return;
	}
	
	phalcon_update_property_zval(this_ptr, SL("_path"), name TSRMLS_CC);
	phalcon_update_property_zval(this_ptr, SL("_options"), options TSRMLS_CC);
	phalcon_update_property_zval(this_ptr, SL("_fileHandler"), handler TSRMLS_CC);
	
	PHALCON_MM_RESTORE();
}
コード例 #7
0
ファイル: url.c プロジェクト: Myleft/cphalcon7
/**
 * Generates a URL for a static resource
 *
 * @param string|array $uri
 * @param array $args
 * @return string
 */
PHP_METHOD(Phalcon_Mvc_Url, getStatic){

	zval *uri = NULL, *args = NULL, *static_base_uri, *base_uri = NULL;
	zval *matched, *pattern, *query_string;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 0, 2, &uri, &args);

	if (!uri) {
		uri = &PHALCON_GLOBAL(z_null);
	} else {
		PHALCON_ENSURE_IS_STRING(uri);

		if (strstr(Z_STRVAL_P(uri), "://")) {
			PHALCON_INIT_VAR(matched);
			PHALCON_INIT_VAR(pattern);
			ZVAL_STRING(pattern, "/^[^:\\/?#]++:/");
			RETURN_MM_ON_FAILURE(phalcon_preg_match(matched, pattern, uri, NULL));
			if (zend_is_true(matched)) {
				RETURN_CTOR(uri);
			}
		}
	}

	static_base_uri = phalcon_read_property(getThis(), SL("_staticBaseUri"), PH_NOISY);
	if (Z_TYPE_P(static_base_uri) != IS_NULL) {
		PHALCON_CONCAT_VV(return_value, static_base_uri, uri);
	} else {	
		PHALCON_CALL_METHOD(&base_uri, getThis(), "getbaseuri");
		PHALCON_CONCAT_VV(return_value, base_uri, uri);
	}

	if (args) {
		PHALCON_INIT_VAR(query_string);
		phalcon_http_build_query(query_string, args, "&");
		if (Z_TYPE_P(query_string) == IS_STRING && Z_STRLEN_P(query_string)) {
			if (phalcon_memnstr_str(return_value, "?", 1)) {
				PHALCON_SCONCAT_SV(return_value, "&", query_string);
			}
			else {
				PHALCON_SCONCAT_SV(return_value, "?", query_string);
			}
		}
	}

	RETURN_MM();
}
コード例 #8
0
ファイル: request.c プロジェクト: dreamsxin/cphalcon7
/**
 * Checks whether request has been made using SOAP
 *
 * @return boolean
 */
PHP_METHOD(Phalcon_Http_Request, isSoapRequested)
{
	zval *server, content_type = {};

	server = phalcon_get_global_str(SL("_SERVER"));
	if (phalcon_array_isset_str(server, SL("HTTP_SOAPACTION"))) {
		RETURN_TRUE;
	}

	if (phalcon_array_isset_fetch_str(&content_type, server, SL("CONTENT_TYPE"))) {
		if (phalcon_memnstr_str(&content_type, SL("application/soap+xml"))) {
			RETURN_TRUE;
		}
	}

	RETURN_FALSE;
}
コード例 #9
0
ファイル: file.c プロジェクト: DonOzone/cphalcon
/**
 * Phalcon\Logger\Adapter\File constructor
 *
 * @param string $name
 * @param array $options
 */
PHP_METHOD(Phalcon_Logger_Adapter_File, __construct){

	zval *name, *options = NULL, *mode = NULL, *handler, *exception_message;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 1, 1, &name, &options);
	
	if (!options) {
		PHALCON_INIT_VAR(options);
	}
	
	if (phalcon_array_isset_string(options, SS("mode"))) {
	
		PHALCON_OBS_VAR(mode);
		phalcon_array_fetch_string(&mode, options, SL("mode"), PH_NOISY);
		if (phalcon_memnstr_str(mode, SL("r"))) {
			PHALCON_THROW_EXCEPTION_STR(phalcon_logger_exception_ce, "Logger must be opened in append or write mode");
			return;
		}
	} else {
		PHALCON_INIT_NVAR(mode);
		ZVAL_STRING(mode, "ab", 1);
	}
	
	/** 
	 * We use 'fopen' to respect to open-basedir directive
	 */
	PHALCON_INIT_VAR(handler);
	phalcon_call_func_p2(handler, "fopen", name, mode);
	if (!zend_is_true(handler)) {
		PHALCON_INIT_VAR(exception_message);
		PHALCON_CONCAT_SVS(exception_message, "Can't open log file at '", name, "'");
		PHALCON_THROW_EXCEPTION_ZVAL(phalcon_logger_exception_ce, exception_message);
		return;
	}
	
	phalcon_update_property_this(this_ptr, SL("_path"), name TSRMLS_CC);
	phalcon_update_property_this(this_ptr, SL("_options"), options TSRMLS_CC);
	phalcon_update_property_this(this_ptr, SL("_fileHandler"), handler TSRMLS_CC);
	
	PHALCON_MM_RESTORE();
}
コード例 #10
0
ファイル: request.c プロジェクト: dreamsxin/cphalcon7
/**
 * Gets most possible client IPv4 Address. This method search in $_SERVER['REMOTE_ADDR'] and optionally in $_SERVER['HTTP_X_FORWARDED_FOR']
 *
 * @param boolean $trustForwardedHeader
 * @return string
 */
PHP_METHOD(Phalcon_Http_Request, getClientAddress){

	zval *trust_forwarded_header = NULL, *_SERVER, address = {}, addresses = {}, first = {};

	phalcon_fetch_params(0, 0, 1, &trust_forwarded_header);

	if (!trust_forwarded_header) {
		trust_forwarded_header = &PHALCON_GLOBAL(z_false);
	}

	_SERVER = phalcon_get_global_str(SL("_SERVER"));

	/**
	 * Proxies use this IP
	 */
	if (zend_is_true(trust_forwarded_header)) {
		if (!phalcon_array_isset_fetch_str(&address, _SERVER, SL("HTTP_X_FORWARDED_FOR"))) {
			if (!phalcon_array_isset_fetch_str(&address, _SERVER, SL("REMOTE_ADDR"))) {
				phalcon_array_fetch_str(&address, _SERVER, SL("REMOTE_ADDR"), PH_NOISY);
			}
		}
	} else if (!phalcon_array_isset_fetch_str(&address, _SERVER, SL("REMOTE_ADDR"))) {
		phalcon_array_fetch_str(&address, _SERVER, SL("REMOTE_ADDR"), PH_NOISY);
	}

	if (Z_TYPE(address) == IS_STRING) {
		if (phalcon_memnstr_str(&address, SL(","))) {
			/**
			 * The client address has multiples parts, only return the first part
			 */
			phalcon_fast_explode_str(&addresses, SL(","), &address);

			phalcon_array_fetch_long(&first, &addresses, 0, PH_NOISY);
			RETURN_CTORW(&first);
		}

		RETURN_CTORW(&address);
	}

	RETURN_FALSE;
}
コード例 #11
0
ファイル: request.c プロジェクト: BlueShark/cphalcon
/**
 * Checks whether request has been made using SOAP
 *
 * @return boolean
 */
PHP_METHOD(Phalcon_Http_Request, isSoapRequested){

	zval *server = NULL, *_SERVER, *content_type;

	PHALCON_MM_GROW();

	phalcon_get_global(&_SERVER, SS("_SERVER") TSRMLS_CC);
	PHALCON_CPY_WRT(server, _SERVER);
	if (phalcon_array_isset_string(server, SS("HTTP_SOAPACTION"))) {
		RETURN_MM_TRUE;
	} else {
		if (phalcon_array_isset_string(server, SS("CONTENT_TYPE"))) {
	
			PHALCON_OBS_VAR(content_type);
			phalcon_array_fetch_string(&content_type, server, SL("CONTENT_TYPE"), PH_NOISY_CC);
			if (phalcon_memnstr_str(content_type, SL("application/soap+xml") TSRMLS_CC)) {
				RETURN_MM_TRUE;
			}
		}
	}
	
	RETURN_MM_FALSE;
}
コード例 #12
0
ファイル: postgresql.c プロジェクト: 11mariom/cphalcon
/**
 * Returns an array of Phalcon\Db\Column objects describing a table
 *
 * <code>print_r($connection->describeColumns("posts")); ?></code>
 *
 * @param string $table
 * @param string $schema
 * @return Phalcon\Db\Column[]
 */
PHP_METHOD(Phalcon_Db_Adapter_Pdo_Postgresql, describeColumns){

	zval *table, *schema = NULL, *columns, *dialect, *sql, *fetch_num;
	zval *describe, *old_column = NULL, *field = NULL, *definition = NULL;
	zval *char_size = NULL, *numeric_size = NULL, *numeric_scale = NULL, *column_type = NULL;
	zval *attribute = NULL, *column_name = NULL, *column = NULL;
	HashTable *ah0;
	HashPosition hp0;
	zval **hd;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 1, 1, &table, &schema);
	
	if (!schema) {
		PHALCON_INIT_VAR(schema);
	}
	
	PHALCON_INIT_VAR(columns);
	array_init(columns);
	
	PHALCON_OBS_VAR(dialect);
	phalcon_read_property_this(&dialect, this_ptr, SL("_dialect"), PH_NOISY_CC);
	
	PHALCON_INIT_VAR(sql);
	phalcon_call_method_p2(sql, dialect, "describecolumns", table, schema);
	
	/** 
	 * We're using FETCH_NUM to fetch the columns
	 */
	PHALCON_INIT_VAR(fetch_num);
	ZVAL_LONG(fetch_num, 3);
	
	PHALCON_INIT_VAR(describe);
	phalcon_call_method_p2(describe, this_ptr, "fetchall", sql, fetch_num);
	
	/** 
	 * 0:name, 1:type, 2:size, 3:numericsize, 4: null, 5: key, 6: extra, 7: position
	 */
	PHALCON_INIT_VAR(old_column);
	
	phalcon_is_iterable(describe, &ah0, &hp0, 0, 0);
	
	while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) {
	
		PHALCON_GET_HVALUE(field);
	
		PHALCON_INIT_NVAR(definition);
		array_init_size(definition, 1);
		add_assoc_long_ex(definition, SS("bindType"), 2);
	
		PHALCON_OBS_NVAR(char_size);
		phalcon_array_fetch_long(&char_size, field, 2, PH_NOISY);
	
		PHALCON_OBS_NVAR(numeric_size);
		phalcon_array_fetch_long(&numeric_size, field, 3, PH_NOISY);

		PHALCON_OBS_NVAR(numeric_scale);
                phalcon_array_fetch_long(&numeric_scale, field, 4, PH_NOISY);
	
		PHALCON_OBS_NVAR(column_type);
		phalcon_array_fetch_long(&column_type, field, 1, PH_NOISY);
	
		/** 
		 * Check the column type to get the correct Phalcon type
		 */
		if (phalcon_memnstr_str(column_type, SL("int"))) {
			phalcon_array_update_string_long(&definition, SL("type"), 0, PH_SEPARATE);
			phalcon_array_update_string_bool(&definition, SL("isNumeric"), 1, PH_SEPARATE);
			phalcon_array_update_string(&definition, SL("size"), &numeric_size, PH_COPY | PH_SEPARATE);
			phalcon_array_update_string_long(&definition, SL("bindType"), 1, PH_SEPARATE);
		} else {
			if (phalcon_memnstr_str(column_type, SL("varying"))) {
				phalcon_array_update_string_long(&definition, SL("type"), 2, PH_SEPARATE);
				phalcon_array_update_string(&definition, SL("size"), &char_size, PH_COPY | PH_SEPARATE);
			} else {
				if (phalcon_memnstr_str(column_type, SL("date"))) {
					phalcon_array_update_string_long(&definition, SL("type"), 1, PH_SEPARATE);
					phalcon_array_update_string_long(&definition, SL("size"), 0, PH_SEPARATE);
				} else {
					if (phalcon_memnstr_str(column_type, SL("numeric"))) {
						phalcon_array_update_string_long(&definition, SL("type"), 3, PH_SEPARATE);
						phalcon_array_update_string_bool(&definition, SL("isNumeric"), 1, PH_SEPARATE);
						phalcon_array_update_string(&definition, SL("size"), &numeric_size, PH_COPY | PH_SEPARATE);
						phalcon_array_update_string(&definition, SL("scale"), &numeric_scale, PH_COPY | PH_SEPARATE);
						phalcon_array_update_string_long(&definition, SL("bindType"), 32, PH_SEPARATE);
					} else {
						if (phalcon_memnstr_str(column_type, SL("char"))) {
							phalcon_array_update_string_long(&definition, SL("type"), 5, PH_SEPARATE);
							phalcon_array_update_string(&definition, SL("size"), &char_size, PH_COPY | PH_SEPARATE);
						} else {
							if (phalcon_memnstr_str(column_type, SL("timestamp"))) {
								phalcon_array_update_string_long(&definition, SL("type"), 4, PH_SEPARATE);
								phalcon_array_update_string_long(&definition, SL("size"), 0, PH_SEPARATE);
							} else {
								if (phalcon_memnstr_str(column_type, SL("text"))) {
									phalcon_array_update_string_long(&definition, SL("type"), 6, PH_SEPARATE);
									phalcon_array_update_string(&definition, SL("size"), &char_size, PH_COPY | PH_SEPARATE);
								} else {
									if (phalcon_memnstr_str(column_type, SL("float"))) {
										phalcon_array_update_string_long(&definition, SL("type"), 7, PH_SEPARATE);
										phalcon_array_update_string_bool(&definition, SL("isNumeric"), 1, PH_SEPARATE);
										phalcon_array_update_string(&definition, SL("size"), &numeric_size, PH_COPY | PH_SEPARATE);
										phalcon_array_update_string_long(&definition, SL("bindType"), 32, PH_SEPARATE);
									} else {
										if (phalcon_memnstr_str(column_type, SL("bool"))) {
											phalcon_array_update_string_long(&definition, SL("type"), 8, PH_SEPARATE);
											phalcon_array_update_string_long(&definition, SL("size"), 0, PH_SEPARATE);
											phalcon_array_update_string_long(&definition, SL("bindType"), 5, PH_SEPARATE);
										} else {
											if (phalcon_memnstr_str(column_type, SL("uuid"))) {
												phalcon_array_update_string_long(&definition, SL("type"), 5, PH_SEPARATE);
												phalcon_array_update_string_long(&definition, SL("size"), 36, PH_SEPARATE);
											} else {
												phalcon_array_update_string_long(&definition, SL("type"), 5, PH_SEPARATE);
												phalcon_array_update_string(&definition, SL("size"), &char_size, PH_COPY | PH_SEPARATE);
											}
										}
									}
								}
							}
						}
					}
				}
			}
		}
	
		if (phalcon_memnstr_str(column_type, SL("unsigned"))) {
			phalcon_array_update_string_bool(&definition, SL("unsigned"), 1, PH_SEPARATE);
		}
	
		if (Z_TYPE_P(old_column) == IS_NULL) {
			phalcon_array_update_string_bool(&definition, SL("first"), 1, PH_SEPARATE);
		} else {
			phalcon_array_update_string(&definition, SL("after"), &old_column, PH_COPY | PH_SEPARATE);
		}
	
		/** 
		 * Check if the field is primary key
		 */
		PHALCON_OBS_NVAR(attribute);
		phalcon_array_fetch_long(&attribute, field, 6, PH_NOISY);
		if (PHALCON_IS_STRING(attribute, "PRI")) {
			phalcon_array_update_string_bool(&definition, SL("primary"), 1, PH_SEPARATE);
		}
	
		/** 
		 * Check if the column allows null values
		 */
		PHALCON_OBS_NVAR(attribute);
		phalcon_array_fetch_long(&attribute, field, 5, PH_NOISY);
		if (PHALCON_IS_STRING(attribute, "NO")) {
			phalcon_array_update_string_bool(&definition, SL("notNull"), 1, PH_SEPARATE);
		}
	
		/** 
		 * Check if the column is auto increment
		 */
		PHALCON_OBS_NVAR(attribute);
		phalcon_array_fetch_long(&attribute, field, 7, PH_NOISY);
		if (PHALCON_IS_STRING(attribute, "auto_increment")) {
			phalcon_array_update_string_bool(&definition, SL("autoIncrement"), 1, PH_SEPARATE);
		}
	
		PHALCON_OBS_NVAR(column_name);
		phalcon_array_fetch_long(&column_name, field, 0, PH_NOISY);
	
		/** 
		 * Create a Phalcon\Db\Column to abstract the column
		 */
		PHALCON_INIT_NVAR(column);
		object_init_ex(column, phalcon_db_column_ce);
		phalcon_call_method_p2_noret(column, "__construct", column_name, definition);
	
		phalcon_array_append(&columns, column, PH_SEPARATE);
		PHALCON_CPY_WRT(old_column, column_name);
	
		zend_hash_move_forward_ex(ah0, &hp0);
	}
	
	RETURN_CTOR(columns);
}
コード例 #13
0
ファイル: select.c プロジェクト: angkatan21/cphalcon
/**
 * Generates a SELECT tag
 *
 * @param array $parameters
 * @param array $data
 */
PHP_METHOD(Phalcon_Tag_Select, selectField){

	zval *parameters, *data = NULL, *params = NULL, *eol, *id = NULL, *name, *value = NULL;
	zval *use_empty = NULL, *empty_value = NULL, *empty_text = NULL, *code;
	zval *avalue = NULL, *key = NULL, *close_option, *options = NULL, *using;
	zval *resultset_options, *array_options;
	HashTable *ah0;
	HashPosition hp0;
	zval **hd;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 1, 1, &parameters, &data);
	
	if (!data) {
		PHALCON_INIT_VAR(data);
	}
	
	if (Z_TYPE_P(parameters) != IS_ARRAY) { 
		PHALCON_INIT_VAR(params);
		array_init_size(params, 2);
		phalcon_array_append(&params, parameters, PH_SEPARATE TSRMLS_CC);
		phalcon_array_append(&params, data, PH_SEPARATE TSRMLS_CC);
	} else {
		PHALCON_CPY_WRT(params, parameters);
	}
	
	PHALCON_INIT_VAR(eol);
	ZVAL_STRING(eol, PHP_EOL, 1);
	if (!phalcon_array_isset_long(params, 0)) {
		PHALCON_OBS_VAR(id);
		phalcon_array_fetch_string(&id, params, SL("id"), PH_NOISY_CC);
		phalcon_array_update_long(&params, 0, &id, PH_COPY | PH_SEPARATE TSRMLS_CC);
	}
	
	PHALCON_OBS_NVAR(id);
	phalcon_array_fetch_long(&id, params, 0, PH_NOISY_CC);
	if (!phalcon_array_isset_string(params, SS("name"))) {
		phalcon_array_update_string(&params, SL("name"), &id, PH_COPY | PH_SEPARATE TSRMLS_CC);
	} else {
		PHALCON_OBS_VAR(name);
		phalcon_array_fetch_string(&name, params, SL("name"), PH_NOISY_CC);
		if (!zend_is_true(name)) {
			phalcon_array_update_string(&params, SL("name"), &id, PH_COPY | PH_SEPARATE TSRMLS_CC);
		}
	}
	
	/** 
	 * Automatically assign the id if the name is not an array
	 */
	if (!phalcon_memnstr_str(id, SL("[") TSRMLS_CC)) {
		if (!phalcon_array_isset_string(params, SS("id"))) {
			phalcon_array_update_string(&params, SL("id"), &id, PH_COPY | PH_SEPARATE TSRMLS_CC);
		}
	}
	
	if (!phalcon_array_isset_string(params, SS("value"))) {
		PHALCON_INIT_VAR(value);
		PHALCON_CALL_STATIC_PARAMS_2(value, "phalcon\\tag", "getvalue", id, params);
	} else {
		PHALCON_OBS_NVAR(value);
		phalcon_array_fetch_string(&value, params, SL("value"), PH_NOISY_CC);
		phalcon_array_unset_string(&params, SS("value"), PH_SEPARATE);
	}
	
	PHALCON_INIT_VAR(use_empty);
	ZVAL_BOOL(use_empty, 0);
	if (phalcon_array_isset_string(params, SS("useEmpty"))) {
		if (!phalcon_array_isset_string(params, SS("emptyValue"))) {
			PHALCON_INIT_VAR(empty_value);
			ZVAL_STRING(empty_value, "", 1);
		} else {
			PHALCON_OBS_NVAR(empty_value);
			phalcon_array_fetch_string(&empty_value, params, SL("emptyValue"), PH_NOISY_CC);
			phalcon_array_unset_string(&params, SS("emptyValue"), PH_SEPARATE);
		}
		if (!phalcon_array_isset_string(params, SS("emptyText"))) {
			PHALCON_INIT_VAR(empty_text);
			ZVAL_STRING(empty_text, "Choose...", 1);
		} else {
			PHALCON_OBS_NVAR(empty_text);
			phalcon_array_fetch_string(&empty_text, params, SL("emptyText"), PH_NOISY_CC);
			phalcon_array_unset_string(&params, SS("emptyText"), PH_SEPARATE);
		}
	
		PHALCON_OBS_NVAR(use_empty);
		phalcon_array_fetch_string(&use_empty, params, SL("useEmpty"), PH_NOISY_CC);
		phalcon_array_unset_string(&params, SS("useEmpty"), PH_SEPARATE);
	}
	
	PHALCON_INIT_VAR(code);
	ZVAL_STRING(code, "<select", 1);
	if (Z_TYPE_P(params) == IS_ARRAY) { 
	
		if (!phalcon_is_iterable(params, &ah0, &hp0, 0, 0 TSRMLS_CC)) {
			return;
		}
	
		while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) {
	
			PHALCON_GET_FOREACH_KEY(key, ah0, hp0);
			PHALCON_GET_FOREACH_VALUE(avalue);
	
			if (Z_TYPE_P(key) != IS_LONG) {
				if (Z_TYPE_P(avalue) != IS_ARRAY) { 
					PHALCON_SCONCAT_SVSVS(code, " ", key, "=\"", avalue, "\"");
				}
			}
	
			zend_hash_move_forward_ex(ah0, &hp0);
		}
	
	}
	
	PHALCON_SCONCAT_SV(code, ">", eol);
	
	PHALCON_INIT_VAR(close_option);
	PHALCON_CONCAT_SV(close_option, "</option>", eol);
	if (zend_is_true(use_empty)) {
		/** 
		 * Create an empty value
		 */
		PHALCON_SCONCAT_SVSVV(code, "\t<option value=\"", empty_value, "\">", empty_text, close_option);
		phalcon_array_unset_string(&params, SS("useEmpty"), PH_SEPARATE);
	}
	
	if (phalcon_array_isset_long(params, 1)) {
		PHALCON_OBS_VAR(options);
		phalcon_array_fetch_long(&options, params, 1, PH_NOISY_CC);
	} else {
		PHALCON_CPY_WRT(options, data);
	}
	
	if (Z_TYPE_P(options) == IS_OBJECT) {
	
		/** 
		 * The options is a resultset
		 */
		if (!phalcon_array_isset_string(params, SS("using"))) {
			PHALCON_THROW_EXCEPTION_STR(phalcon_tag_exception_ce, "The 'using' parameter is required");
			return;
		} else {
			PHALCON_OBS_VAR(using);
			phalcon_array_fetch_string(&using, params, SL("using"), PH_NOISY_CC);
			if (Z_TYPE_P(using) != IS_ARRAY) { 
				if (Z_TYPE_P(using) != IS_OBJECT) {
					PHALCON_THROW_EXCEPTION_STR(phalcon_tag_exception_ce, "The 'using' parameter should be an Array");
					return;
				}
			}
		}
	
		/** 
		 * Create the SELECT's option from a resultset
		 */
		PHALCON_INIT_VAR(resultset_options);
		PHALCON_CALL_SELF_PARAMS_4(resultset_options, this_ptr, "_optionsfromresultset", options, using, value, close_option);
		phalcon_concat_self(&code, resultset_options TSRMLS_CC);
	} else {
		if (Z_TYPE_P(options) == IS_ARRAY) { 
コード例 #14
0
ファイル: builder.c プロジェクト: BlueShark/cphalcon
/**
 * Returns a PHQL statement built based on the builder parameters
 *
 * @return string
 */
PHP_METHOD(Phalcon_Mvc_Model_Query_Builder, getPhql){

	zval *dependency_injector = NULL, *models, *conditions = NULL;
	zval *one, *number_models, *invalid_condition;
	zval *model = NULL, *service_name, *meta_data, *model_instance;
	zval *no_primary = NULL, *primary_keys, *first_primary_key;
	zval *column_map = NULL, *attribute_field = NULL, *exception_message;
	zval *primary_key_condition, *phql, *columns;
	zval *selected_columns = NULL, *column = NULL, *alias = NULL, *aliased_column = NULL;
	zval *joined_columns = NULL, *selected_column = NULL, *selected_models;
	zval *selected_model = NULL, *joined_models, *joins;
	zval *join = NULL, *join_model = NULL, *join_conditions = NULL, *join_alias = NULL;
	zval *join_type = NULL, *group, *group_items, *group_item = NULL;
	zval *escaped_item = NULL, *joined_items = NULL, *having, *order;
	zval *order_items, *order_item = NULL, *limit, *number;
	zval *offset = NULL;
	HashTable *ah0, *ah1, *ah2, *ah3, *ah4, *ah5;
	HashPosition hp0, hp1, hp2, hp3, hp4, hp5;
	zval **hd;
	zend_class_entry *ce0;

	PHALCON_MM_GROW();

	PHALCON_OBS_VAR(dependency_injector);
	phalcon_read_property(&dependency_injector, this_ptr, SL("_dependencyInjector"), PH_NOISY_CC);
	if (Z_TYPE_P(dependency_injector) != IS_OBJECT) {
		PHALCON_INIT_NVAR(dependency_injector);
		PHALCON_CALL_STATIC(dependency_injector, "phalcon\\di", "getdefault");
		phalcon_update_property_zval(this_ptr, SL("_dependencyInjector"), dependency_injector TSRMLS_CC);
	}
	
	PHALCON_OBS_VAR(models);
	phalcon_read_property(&models, this_ptr, SL("_models"), PH_NOISY_CC);
	if (Z_TYPE_P(models) == IS_ARRAY) { 
		if (!phalcon_fast_count_ev(models TSRMLS_CC)) {
			PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_model_exception_ce, "At least one model is required to build the query");
			return;
		}
	} else {
		if (!zend_is_true(models)) {
			PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_model_exception_ce, "At least one model is required to build the query");
			return;
		}
	}
	
	PHALCON_OBS_VAR(conditions);
	phalcon_read_property(&conditions, this_ptr, SL("_conditions"), PH_NOISY_CC);
	if (phalcon_is_numeric(conditions)) {
	
		/** 
		 * If the conditions is a single numeric field. We internally create a condition
		 * using the related primary key
		 */
		if (Z_TYPE_P(models) == IS_ARRAY) { 
	
			PHALCON_INIT_VAR(one);
			ZVAL_LONG(one, 1);
	
			PHALCON_INIT_VAR(number_models);
			phalcon_fast_count(number_models, models TSRMLS_CC);
	
			PHALCON_INIT_VAR(invalid_condition);
			is_smaller_function(invalid_condition, one, number_models TSRMLS_CC);
			if (PHALCON_IS_TRUE(invalid_condition)) {
				PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_model_exception_ce, "Cannot build the query. Invalid condition");
				return;
			}
	
			PHALCON_OBS_VAR(model);
			phalcon_array_fetch_long(&model, models, 0, PH_NOISY_CC);
		} else {
			PHALCON_CPY_WRT(model, models);
		}
	
		PHALCON_INIT_VAR(service_name);
		ZVAL_STRING(service_name, "modelsMetadata", 1);
	
		/** 
		 * Get the models metadata service to obtain the column names, column map and
		 * primary key
		 */
		PHALCON_INIT_VAR(meta_data);
		PHALCON_CALL_METHOD_PARAMS_1(meta_data, dependency_injector, "getshared", service_name);
		ce0 = phalcon_fetch_class(model TSRMLS_CC);
	
		PHALCON_INIT_VAR(model_instance);
		object_init_ex(model_instance, ce0);
		if (phalcon_has_constructor(model_instance TSRMLS_CC)) {
			PHALCON_CALL_METHOD_PARAMS_1_NORETURN(model_instance, "__construct", dependency_injector);
		}
	
		PHALCON_INIT_VAR(no_primary);
		ZVAL_BOOL(no_primary, 1);
	
		PHALCON_INIT_VAR(primary_keys);
		PHALCON_CALL_METHOD_PARAMS_1(primary_keys, meta_data, "getprimarykeyattributes", model_instance);
		if (phalcon_fast_count_ev(primary_keys TSRMLS_CC)) {
			if (phalcon_array_isset_long(primary_keys, 0)) {
	
				PHALCON_OBS_VAR(first_primary_key);
				phalcon_array_fetch_long(&first_primary_key, primary_keys, 0, PH_NOISY_CC);
	
				/** 
				 * The PHQL contains the renamed columns if available
				 */
				if (PHALCON_GLOBAL(orm).column_renaming) {
					PHALCON_INIT_VAR(column_map);
					PHALCON_CALL_METHOD_PARAMS_1(column_map, meta_data, "getcolumnmap", model_instance);
				} else {
					PHALCON_INIT_NVAR(column_map);
				}
	
				if (Z_TYPE_P(column_map) == IS_ARRAY) { 
					if (phalcon_array_isset(column_map, first_primary_key)) {
						PHALCON_OBS_VAR(attribute_field);
						phalcon_array_fetch(&attribute_field, column_map, first_primary_key, PH_NOISY_CC);
					} else {
						PHALCON_INIT_VAR(exception_message);
						PHALCON_CONCAT_SVS(exception_message, "Column '", first_primary_key, "\" isn't part of the column map");
						PHALCON_THROW_EXCEPTION_ZVAL(phalcon_mvc_model_exception_ce, exception_message);
						return;
					}
				} else {
					PHALCON_CPY_WRT(attribute_field, first_primary_key);
				}
	
				PHALCON_INIT_VAR(primary_key_condition);
				PHALCON_CONCAT_SVSVSV(primary_key_condition, "[", model, "].[", attribute_field, "] = ", conditions);
				PHALCON_CPY_WRT(conditions, primary_key_condition);
	
				ZVAL_BOOL(no_primary, 0);
			}
		}
	
		/** 
		 * A primary key is mandatory in these cases
		 */
		if (PHALCON_IS_TRUE(no_primary)) {
			PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_model_exception_ce, "Source related to this model does not have a primary key defined");
			return;
		}
	}
	
	PHALCON_INIT_VAR(phql);
	ZVAL_STRING(phql, "SELECT ", 1);
	
	PHALCON_OBS_VAR(columns);
	phalcon_read_property(&columns, this_ptr, SL("_columns"), PH_NOISY_CC);
	if (Z_TYPE_P(columns) != IS_NULL) {
		if (Z_TYPE_P(columns) == IS_ARRAY) { 
	
			PHALCON_INIT_VAR(selected_columns);
			array_init(selected_columns);
	
			if (!phalcon_is_iterable(columns, &ah0, &hp0, 0, 0 TSRMLS_CC)) {
				return;
			}
	
			while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) {
	
				PHALCON_GET_FOREACH_KEY(alias, ah0, hp0);
				PHALCON_GET_FOREACH_VALUE(column);
	
				if (Z_TYPE_P(alias) == IS_LONG) {
					phalcon_array_append(&selected_columns, column, PH_SEPARATE TSRMLS_CC);
				} else {
					PHALCON_INIT_NVAR(aliased_column);
					PHALCON_CONCAT_VSV(aliased_column, column, " AS ", alias);
					phalcon_array_append(&selected_columns, aliased_column, PH_SEPARATE TSRMLS_CC);
				}
	
				zend_hash_move_forward_ex(ah0, &hp0);
			}
	
			PHALCON_INIT_VAR(joined_columns);
			phalcon_fast_join_str(joined_columns, SL(", "), selected_columns TSRMLS_CC);
			phalcon_concat_self(&phql, joined_columns TSRMLS_CC);
		} else {
			phalcon_concat_self(&phql, columns TSRMLS_CC);
		}
	} else {
		if (Z_TYPE_P(models) == IS_ARRAY) { 
	
			PHALCON_INIT_NVAR(selected_columns);
			array_init(selected_columns);
	
			if (!phalcon_is_iterable(models, &ah1, &hp1, 0, 0 TSRMLS_CC)) {
				return;
			}
	
			while (zend_hash_get_current_data_ex(ah1, (void**) &hd, &hp1) == SUCCESS) {
	
				PHALCON_GET_FOREACH_KEY(alias, ah1, hp1);
				PHALCON_GET_FOREACH_VALUE(model);
	
				if (Z_TYPE_P(alias) == IS_LONG) {
					PHALCON_INIT_NVAR(selected_column);
					PHALCON_CONCAT_SVS(selected_column, "[", model, "].*");
				} else {
					PHALCON_INIT_NVAR(selected_column);
					PHALCON_CONCAT_SVS(selected_column, "[", alias, "].*");
				}
				phalcon_array_append(&selected_columns, selected_column, PH_SEPARATE TSRMLS_CC);
	
				zend_hash_move_forward_ex(ah1, &hp1);
			}
	
			PHALCON_INIT_NVAR(joined_columns);
			phalcon_fast_join_str(joined_columns, SL(", "), selected_columns TSRMLS_CC);
			phalcon_concat_self(&phql, joined_columns TSRMLS_CC);
		} else {
			PHALCON_SCONCAT_SVS(phql, "[", models, "].*");
		}
	}
	
	/** 
	 * Join multiple models or use a single one if it is a string
	 */
	if (Z_TYPE_P(models) == IS_ARRAY) { 
	
		PHALCON_INIT_VAR(selected_models);
		array_init(selected_models);
	
		if (!phalcon_is_iterable(models, &ah2, &hp2, 0, 0 TSRMLS_CC)) {
			return;
		}
	
		while (zend_hash_get_current_data_ex(ah2, (void**) &hd, &hp2) == SUCCESS) {
	
			PHALCON_GET_FOREACH_KEY(alias, ah2, hp2);
			PHALCON_GET_FOREACH_VALUE(model);
	
			if (Z_TYPE_P(alias) == IS_STRING) {
				PHALCON_INIT_NVAR(selected_model);
				PHALCON_CONCAT_SVSVS(selected_model, "[", model, "] AS [", alias, "]");
			} else {
				PHALCON_INIT_NVAR(selected_model);
				PHALCON_CONCAT_SVS(selected_model, "[", model, "]");
			}
			phalcon_array_append(&selected_models, selected_model, PH_SEPARATE TSRMLS_CC);
	
			zend_hash_move_forward_ex(ah2, &hp2);
		}
	
		PHALCON_INIT_VAR(joined_models);
		phalcon_fast_join_str(joined_models, SL(", "), selected_models TSRMLS_CC);
		PHALCON_SCONCAT_SV(phql, " FROM ", joined_models);
	} else {
		PHALCON_SCONCAT_SVS(phql, " FROM [", models, "]");
	}
	
	/** 
	 * Check if joins were passed to the builders
	 */
	PHALCON_OBS_VAR(joins);
	phalcon_read_property(&joins, this_ptr, SL("_joins"), PH_NOISY_CC);
	if (Z_TYPE_P(joins) == IS_ARRAY) { 
	
		if (!phalcon_is_iterable(joins, &ah3, &hp3, 0, 0 TSRMLS_CC)) {
			return;
		}
	
		while (zend_hash_get_current_data_ex(ah3, (void**) &hd, &hp3) == SUCCESS) {
	
			PHALCON_GET_FOREACH_VALUE(join);
	
			/** 
			 * The joined table is in the first place of the array
			 */
			PHALCON_OBS_NVAR(join_model);
			phalcon_array_fetch_long(&join_model, join, 0, PH_NOISY_CC);
	
			/** 
			 * The join conditions are in the second place of the array
			 */
			PHALCON_OBS_NVAR(join_conditions);
			phalcon_array_fetch_long(&join_conditions, join, 1, PH_NOISY_CC);
	
			/** 
			 * The join alias is in the second place of the array
			 */
			PHALCON_OBS_NVAR(join_alias);
			phalcon_array_fetch_long(&join_alias, join, 2, PH_NOISY_CC);
	
			/** 
			 * Join type
			 */
			PHALCON_OBS_NVAR(join_type);
			phalcon_array_fetch_long(&join_type, join, 3, PH_NOISY_CC);
	
			/** 
			 * Create the join according to the type
			 */
			if (zend_is_true(join_type)) {
				PHALCON_SCONCAT_VSVS(phql, join_type, " JOIN [", join_model, "]");
			} else {
				PHALCON_SCONCAT_SVS(phql, " JOIN [", join_model, "]");
			}
	
			/** 
			 * Alias comes first
			 */
			if (zend_is_true(join_alias)) {
				PHALCON_SCONCAT_SVS(phql, " AS [", join_alias, "]");
			}
	
			/** 
			 * Conditions then
			 */
			if (zend_is_true(join_conditions)) {
				PHALCON_SCONCAT_SV(phql, " ON ", join_conditions);
			}
	
			zend_hash_move_forward_ex(ah3, &hp3);
		}
	
	}
	
	if (Z_TYPE_P(conditions) == IS_STRING) {
		PHALCON_SCONCAT_SV(phql, " WHERE ", conditions);
	}
	
	/** 
	 * Process group parameters
	 */
	PHALCON_OBS_VAR(group);
	phalcon_read_property(&group, this_ptr, SL("_group"), PH_NOISY_CC);
	if (Z_TYPE_P(group) != IS_NULL) {
		if (Z_TYPE_P(group) == IS_ARRAY) { 
	
			PHALCON_INIT_VAR(group_items);
			array_init(group_items);
	
			if (!phalcon_is_iterable(group, &ah4, &hp4, 0, 0 TSRMLS_CC)) {
				return;
			}
	
			while (zend_hash_get_current_data_ex(ah4, (void**) &hd, &hp4) == SUCCESS) {
	
				PHALCON_GET_FOREACH_VALUE(group_item);
	
				if (phalcon_is_numeric(group_item)) {
					phalcon_array_append(&group_items, group_item, PH_SEPARATE TSRMLS_CC);
				} else {
					if (phalcon_memnstr_str(group_item, SL(".") TSRMLS_CC)) {
						phalcon_array_append(&group_items, group_item, PH_SEPARATE TSRMLS_CC);
					} else {
						PHALCON_INIT_NVAR(escaped_item);
						PHALCON_CONCAT_SVS(escaped_item, "[", group_item, "]");
						phalcon_array_append(&group_items, escaped_item, PH_SEPARATE TSRMLS_CC);
					}
				}
	
				zend_hash_move_forward_ex(ah4, &hp4);
			}
	
			PHALCON_INIT_VAR(joined_items);
			phalcon_fast_join_str(joined_items, SL(", "), group_items TSRMLS_CC);
			PHALCON_SCONCAT_SV(phql, " GROUP BY ", joined_items);
		} else {
			if (phalcon_is_numeric(group)) {
				PHALCON_SCONCAT_SV(phql, " GROUP BY ", group);
			} else {
				if (phalcon_memnstr_str(group, SL(".") TSRMLS_CC)) {
					PHALCON_SCONCAT_SV(phql, " GROUP BY ", group);
				} else {
					PHALCON_SCONCAT_SVS(phql, " GROUP BY [", group, "]");
				}
			}
		}
	
		PHALCON_OBS_VAR(having);
		phalcon_read_property(&having, this_ptr, SL("_having"), PH_NOISY_CC);
		if (Z_TYPE_P(having) != IS_NULL) {
			PHALCON_SCONCAT_SV(phql, " HAVING ", having);
		}
	}
	
	/** 
	 * Process order clause
	 */
	PHALCON_OBS_VAR(order);
	phalcon_read_property(&order, this_ptr, SL("_order"), PH_NOISY_CC);
	if (Z_TYPE_P(order) != IS_NULL) {
		if (Z_TYPE_P(order) == IS_ARRAY) { 
	
			PHALCON_INIT_VAR(order_items);
			array_init(order_items);
	
			if (!phalcon_is_iterable(order, &ah5, &hp5, 0, 0 TSRMLS_CC)) {
				return;
			}
	
			while (zend_hash_get_current_data_ex(ah5, (void**) &hd, &hp5) == SUCCESS) {
	
				PHALCON_GET_FOREACH_VALUE(order_item);
	
				if (phalcon_is_numeric(order_item)) {
					phalcon_array_append(&order_items, order_item, PH_SEPARATE TSRMLS_CC);
				} else {
					if (phalcon_memnstr_str(order_item, SL(".") TSRMLS_CC)) {
						phalcon_array_append(&order_items, order_item, PH_SEPARATE TSRMLS_CC);
					} else {
						PHALCON_INIT_NVAR(escaped_item);
						PHALCON_CONCAT_SVS(escaped_item, "[", order_item, "]");
						phalcon_array_append(&order_items, escaped_item, PH_SEPARATE TSRMLS_CC);
					}
				}
	
				zend_hash_move_forward_ex(ah5, &hp5);
			}
	
			PHALCON_INIT_NVAR(joined_items);
			phalcon_fast_join_str(joined_items, SL(", "), order_items TSRMLS_CC);
			PHALCON_SCONCAT_SV(phql, " ORDER BY ", joined_items);
		} else {
			PHALCON_SCONCAT_SV(phql, " ORDER BY ", order);
		}
	}
	
	/** 
	 * Process limit parameters
	 */
	PHALCON_OBS_VAR(limit);
	phalcon_read_property(&limit, this_ptr, SL("_limit"), PH_NOISY_CC);
	if (Z_TYPE_P(limit) != IS_NULL) {
		if (Z_TYPE_P(limit) == IS_ARRAY) { 
	
			PHALCON_OBS_VAR(number);
			phalcon_array_fetch_string(&number, limit, SL("number"), PH_NOISY_CC);
			if (phalcon_array_isset_string(limit, SS("offset"))) {
	
				PHALCON_OBS_VAR(offset);
				phalcon_array_fetch_string(&offset, limit, SL("offset"), PH_NOISY_CC);
				if (phalcon_is_numeric(offset)) {
					PHALCON_SCONCAT_SVSV(phql, " LIMIT ", number, " OFFSET ", offset);
				} else {
					PHALCON_SCONCAT_SVS(phql, " LIMIT ", number, " OFFSET 0");
				}
			} else {
				PHALCON_SCONCAT_SV(phql, " LIMIT ", number);
			}
		} else {
			if (phalcon_is_numeric(limit)) {
				PHALCON_SCONCAT_SV(phql, " LIMIT ", limit);
	
				PHALCON_OBS_NVAR(offset);
				phalcon_read_property(&offset, this_ptr, SL("_offset"), PH_NOISY_CC);
				if (Z_TYPE_P(offset) != IS_NULL) {
					if (phalcon_is_numeric(offset)) {
						PHALCON_SCONCAT_SV(phql, " OFFSET ", offset);
					} else {
						phalcon_concat_self_str(&phql, SL(" OFFSET 0") TSRMLS_CC);
					}
				}
			}
		}
	}
	
	
	RETURN_CTOR(phql);
}
コード例 #15
0
ファイル: line.c プロジェクト: hdogan/cphalcon
/**
 * Applies a format to a message before sent it to the internal log
 *
 * @param string $message
 * @param int $type
 * @param int $timestamp
 */
PHP_METHOD(Phalcon_Logger_Formatter_Line, format) {

    zval *message, *type, *timestamp, *format = NULL, *date_format;
    zval *date, *date_wildcard, *new_format = NULL, *type_string;
    zval *type_wildcard, *message_wildcard, *eol = NULL;

    PHALCON_MM_GROW();

    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zzz", &message, &type, &timestamp) == FAILURE) {
        RETURN_MM_NULL();
    }

    PHALCON_OBS_VAR(format);
    phalcon_read_property(&format, this_ptr, SL("_format"), PH_NOISY_CC);

    /**
     * Check if the format has the %date% placeholder
     */
    if (phalcon_memnstr_str(format, SL("%date%") TSRMLS_CC)) {
        PHALCON_OBS_VAR(date_format);
        phalcon_read_property(&date_format, this_ptr, SL("_dateFormat"), PH_NOISY_CC);

        PHALCON_INIT_VAR(date);
        PHALCON_CALL_FUNC_PARAMS_2(date, "date", date_format, timestamp);

        PHALCON_INIT_VAR(date_wildcard);
        ZVAL_STRING(date_wildcard, "%date%", 1);

        PHALCON_INIT_VAR(new_format);
        phalcon_fast_str_replace(new_format, date_wildcard, date, format TSRMLS_CC);
    } else {
        PHALCON_CPY_WRT(new_format, format);
    }

    /**
     * Check if the format has the %type% placeholder
     */
    if (phalcon_memnstr_str(format, SL("%type%") TSRMLS_CC)) {
        PHALCON_INIT_VAR(type_string);
        PHALCON_CALL_METHOD_PARAMS_1(type_string, this_ptr, "gettypestring", type);

        PHALCON_INIT_VAR(type_wildcard);
        ZVAL_STRING(type_wildcard, "%type%", 1);

        PHALCON_INIT_NVAR(format);
        phalcon_fast_str_replace(format, type_wildcard, type_string, new_format TSRMLS_CC);
    } else {
        PHALCON_CPY_WRT(format, new_format);
    }

    PHALCON_INIT_VAR(message_wildcard);
    ZVAL_STRING(message_wildcard, "%message%", 1);

    PHALCON_INIT_NVAR(new_format);
    phalcon_fast_str_replace(new_format, message_wildcard, message, format TSRMLS_CC);

    PHALCON_INIT_VAR(eol);

    PHALCON_INIT_NVAR(eol);
    ZVAL_STRING(eol, PHP_EOL, 1);

    PHALCON_INIT_NVAR(format);
    PHALCON_CONCAT_VV(format, new_format, eol);

    RETURN_CCTOR(format);
}
コード例 #16
0
ファイル: tag.c プロジェクト: Tigerlee1987/cphalcon
/**
 * Builds HTML IMG tags
 *
 * @param  array $parameters
 * @return string
 */
PHP_METHOD(Phalcon_Tag, image){

	zval *parameters = NULL, *params = NULL, *first_param, *url, *url_src;
	zval *src, *code, *value = NULL, *key = NULL, *doctype;
	HashTable *ah0;
	HashPosition hp0;
	zval **hd;
	char *hash_index;
	uint hash_index_len;
	ulong hash_num;
	int hash_type;
	int eval_int;

	PHALCON_MM_GROW();

	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|z", &parameters) == FAILURE) {
		PHALCON_MM_RESTORE();
		RETURN_NULL();
	}

	if (!parameters) {
		PHALCON_INIT_NVAR(parameters);
	}
	
	if (Z_TYPE_P(parameters) != IS_ARRAY) { 
		PHALCON_INIT_VAR(params);
		array_init(params);
		phalcon_array_append(&params, parameters, PH_SEPARATE TSRMLS_CC);
	} else {
		PHALCON_CPY_WRT(params, parameters);
	}
	eval_int = phalcon_array_isset_string(params, SS("src"));
	if (!eval_int) {
		eval_int = phalcon_array_isset_long(params, 0);
		if (eval_int) {
			PHALCON_INIT_VAR(first_param);
			phalcon_array_fetch_long(&first_param, params, 0, PH_NOISY_CC);
			phalcon_array_update_string(&params, SL("src"), &first_param, PH_COPY | PH_SEPARATE TSRMLS_CC);
		} else {
			phalcon_array_update_string_string(&params, SL("src"), SL(""), PH_SEPARATE TSRMLS_CC);
		}
	}
	
	PHALCON_INIT_VAR(url);
	PHALCON_CALL_SELF(url, this_ptr, "geturlservice");
	
	PHALCON_INIT_VAR(url_src);
	phalcon_array_fetch_string(&url_src, params, SL("src"), PH_NOISY_CC);
	
	PHALCON_INIT_VAR(src);
	PHALCON_CALL_METHOD_PARAMS_1(src, url, "get", url_src, PH_NO_CHECK);
	phalcon_array_update_string(&params, SL("src"), &src, PH_COPY | PH_SEPARATE TSRMLS_CC);
	
	PHALCON_INIT_VAR(code);
	ZVAL_STRING(code, "<img", 1);
	
	if (!phalcon_valid_foreach(params TSRMLS_CC)) {
		return;
	}
	
	ah0 = Z_ARRVAL_P(params);
	zend_hash_internal_pointer_reset_ex(ah0, &hp0);
	
	ph_cycle_start_0:
	
		if (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) != SUCCESS) {
			goto ph_cycle_end_0;
		}
	
		PHALCON_GET_FOREACH_KEY(key, ah0, hp0);
		PHALCON_GET_FOREACH_VALUE(value);
	
		if (Z_TYPE_P(key) != IS_LONG) {
			PHALCON_SCONCAT_SVSVS(code, " ", key, "=\"", value, "\"");
		}
	
		zend_hash_move_forward_ex(ah0, &hp0);
		goto ph_cycle_start_0;
	
	ph_cycle_end_0:
	
	PHALCON_INIT_VAR(doctype);
	PHALCON_CALL_SELF(doctype, this_ptr, "getdoctype");
	if (phalcon_memnstr_str(doctype, SL("XHTML") TSRMLS_CC)) {
		phalcon_concat_self_str(code, SL(" />") TSRMLS_CC);
	} else {
		phalcon_concat_self_str(code, SL(">") TSRMLS_CC);
	}
	
	
	RETURN_CTOR(code);
}
コード例 #17
0
ファイル: tag.c プロジェクト: Tigerlee1987/cphalcon
/**
 * Builds generic INPUT tags
 *
 * @param   string $type
 * @param array $parameters
 * @param 	boolean $asValue
 * @return string
 */
PHP_METHOD(Phalcon_Tag, _inputField){

	zval *type, *parameters, *as_value = NULL, *params = NULL, *value = NULL;
	zval *id = NULL, *name, *code, *key = NULL, *doctype;
	HashTable *ah0;
	HashPosition hp0;
	zval **hd;
	char *hash_index;
	uint hash_index_len;
	ulong hash_num;
	int hash_type;
	int eval_int;

	PHALCON_MM_GROW();

	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zz|z", &type, &parameters, &as_value) == FAILURE) {
		PHALCON_MM_RESTORE();
		RETURN_NULL();
	}

	if (!as_value) {
		PHALCON_INIT_NVAR(as_value);
		ZVAL_BOOL(as_value, 0);
	}
	
	if (Z_TYPE_P(parameters) != IS_ARRAY) { 
		PHALCON_INIT_VAR(params);
		array_init(params);
		phalcon_array_append(&params, parameters, PH_SEPARATE TSRMLS_CC);
	} else {
		PHALCON_CPY_WRT(params, parameters);
	}
	
	PHALCON_INIT_VAR(value);
	if (PHALCON_IS_FALSE(as_value)) {
		eval_int = phalcon_array_isset_long(params, 0);
		if (!eval_int) {
			PHALCON_INIT_VAR(id);
			phalcon_array_fetch_string(&id, params, SL("id"), PH_NOISY_CC);
			phalcon_array_update_long(&params, 0, &id, PH_COPY | PH_SEPARATE TSRMLS_CC);
		}
	
		PHALCON_INIT_NVAR(id);
		phalcon_array_fetch_long(&id, params, 0, PH_NOISY_CC);
		eval_int = phalcon_array_isset_string(params, SS("name"));
		if (!eval_int) {
			phalcon_array_update_string(&params, SL("name"), &id, PH_COPY | PH_SEPARATE TSRMLS_CC);
		} else {
			PHALCON_INIT_VAR(name);
			phalcon_array_fetch_string(&name, params, SL("name"), PH_NOISY_CC);
			if (!zend_is_true(name)) {
				phalcon_array_update_string(&params, SL("name"), &id, PH_COPY | PH_SEPARATE TSRMLS_CC);
			}
		}
	
		eval_int = phalcon_array_isset_string(params, SS("id"));
		if (!eval_int) {
			phalcon_array_update_string(&params, SL("id"), &id, PH_COPY | PH_SEPARATE TSRMLS_CC);
		}
	
		eval_int = phalcon_array_isset_string(params, SS("value"));
		if (!eval_int) {
			PHALCON_CALL_SELF_PARAMS_1(value, this_ptr, "getvalue", id);
			phalcon_array_update_string(&params, SL("value"), &value, PH_COPY | PH_SEPARATE TSRMLS_CC);
		}
	} else {
		eval_int = phalcon_array_isset_long(params, 0);
		if (eval_int) {
			PHALCON_INIT_NVAR(value);
			phalcon_array_fetch_long(&value, params, 0, PH_NOISY_CC);
			phalcon_array_update_string(&params, SL("value"), &value, PH_COPY | PH_SEPARATE TSRMLS_CC);
		}
	}
	
	/** 
	 * Automatically check inputs
	 */
	if (PHALCON_COMPARE_STRING(type, "checkbox")) {
		if (zend_is_true(value)) {
			phalcon_array_update_string_string(&params, SL("checked"), SL("checked"), PH_SEPARATE TSRMLS_CC);
		}
	}
	
	PHALCON_INIT_VAR(code);
	PHALCON_CONCAT_SVS(code, "<input type=\"", type, "\"");
	
	if (!phalcon_valid_foreach(params TSRMLS_CC)) {
		return;
	}
	
	ah0 = Z_ARRVAL_P(params);
	zend_hash_internal_pointer_reset_ex(ah0, &hp0);
	
	ph_cycle_start_0:
	
		if (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) != SUCCESS) {
			goto ph_cycle_end_0;
		}
	
		PHALCON_GET_FOREACH_KEY(key, ah0, hp0);
		PHALCON_GET_FOREACH_VALUE(value);
	
		if (Z_TYPE_P(key) != IS_LONG) {
			PHALCON_SCONCAT_SVSVS(code, " ", key, "=\"", value, "\"");
		}
	
		zend_hash_move_forward_ex(ah0, &hp0);
		goto ph_cycle_start_0;
	
	ph_cycle_end_0:
	
	/** 
	 * Check if Doctype is XHTML
	 */
	PHALCON_INIT_VAR(doctype);
	PHALCON_CALL_SELF(doctype, this_ptr, "getdoctype");
	if (phalcon_memnstr_str(doctype, SL("XHTML") TSRMLS_CC)) {
		phalcon_concat_self_str(code, SL(" />") TSRMLS_CC);
	} else {
		phalcon_concat_self_str(code, SL(">") TSRMLS_CC);
	}
	
	
	RETURN_CTOR(code);
}
コード例 #18
0
ファイル: oracle.c プロジェクト: banketree/cphalcon
/**
 * Returns an array of Phalcon\Db\Column objects describing a table
 *
 * <code>print_r($connection->describeColumns("posts")); ?></code>
 *
 * @param string $table
 * @param string $schema
 * @return Phalcon\Db\Column[]
 */
PHP_METHOD(Phalcon_Db_Adapter_Pdo_Oracle, describeColumns) {

    zval *table, *schema = NULL, *columns, *dialect, *sql = NULL, *fetch_num;
    zval *describe = NULL, *old_column = NULL, *field = NULL, *definition = NULL;
    zval *column_size = NULL, *column_precision = NULL, *column_scale = NULL;
    zval *column_type = NULL, *attribute = NULL, *column_name = NULL;
    zval *column = NULL;
    HashTable *ah0;
    HashPosition hp0;
    zval **hd;

    PHALCON_MM_GROW();

    phalcon_fetch_params(1, 1, 1, &table, &schema);

    if (!schema) {
        schema = PHALCON_GLOBAL(z_null);
    }

    PHALCON_INIT_VAR(columns);
    array_init(columns);

    PHALCON_OBS_VAR(dialect);
    phalcon_read_property_this(&dialect, this_ptr, SL("_dialect"), PH_NOISY TSRMLS_CC);

    PHALCON_CALL_METHOD(&sql, dialect, "describecolumns", table, schema);

    /**
     * We're using FETCH_NUM to fetch the columns
     */
    PHALCON_INIT_VAR(fetch_num);
    ZVAL_LONG(fetch_num, PDO_FETCH_NUM);

    PHALCON_CALL_METHOD(&describe, this_ptr, "fetchall", sql, fetch_num);

    /**
     *  0:column_name, 1:data_type, 2:data_length, 3:data_precision, 4:data_scale,
     * 5:nullable, 6:constraint_type, 7:default, 8:position;
     */
    PHALCON_INIT_VAR(old_column);

    phalcon_is_iterable(describe, &ah0, &hp0, 0, 0);

    while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) {

        PHALCON_GET_HVALUE(field);

        PHALCON_INIT_NVAR(definition);
        array_init_size(definition, 1);
        add_assoc_long_ex(definition, SS("bindType"), 2);

        PHALCON_OBS_NVAR(column_size);
        phalcon_array_fetch_long(&column_size, field, 2, PH_NOISY);

        PHALCON_OBS_NVAR(column_precision);
        phalcon_array_fetch_long(&column_precision, field, 3, PH_NOISY);

        PHALCON_OBS_NVAR(column_scale);
        phalcon_array_fetch_long(&column_scale, field, 4, PH_NOISY);

        PHALCON_OBS_NVAR(column_type);
        phalcon_array_fetch_long(&column_type, field, 1, PH_NOISY);

        /**
         * Check the column type to get the correct Phalcon type
         */
        while (1) {
            /**
             * Integer
             */
            if (phalcon_memnstr_str(column_type, SL("NUMBER"))) {
                phalcon_array_update_string_long(&definition, SL("type"), PHALCON_DB_COLUMN_TYPE_DECIMAL, PH_COPY);
                phalcon_array_update_string_bool(&definition, SL("isNumeric"), 1, PH_COPY);
                phalcon_array_update_string(&definition, SL("size"), column_precision, PH_COPY);
                phalcon_array_update_string_long(&definition, SL("bindType"), 32, PH_COPY);
                if (phalcon_is_numeric(column_precision)) {
                    phalcon_array_update_string_long(&definition, SL("bytes"), Z_LVAL_P(column_precision) * 8, PH_COPY);
                } else {
                    phalcon_array_update_string_long(&definition, SL("size"), 30, PH_COPY);
                    phalcon_array_update_string_long(&definition, SL("bytes"), 80, PH_COPY);
                }
                if (phalcon_is_numeric(column_scale)) {
                    phalcon_array_update_string(&definition, SL("scale"), column_scale, PH_COPY);
                } else {
                    phalcon_array_update_string_long(&definition, SL("scale"), 6, PH_COPY);
                }
                break;
            }

            /**
             * Tinyint(1) is boolean
             */
            if (phalcon_memnstr_str(column_type, SL("TINYINT(1)"))) {
                phalcon_array_update_string_long(&definition, SL("type"), PHALCON_DB_COLUMN_TYPE_BOOLEAN, PH_COPY);
                phalcon_array_update_string_long(&definition, SL("bindType"), 5, PH_COPY);
                break;
            }

            /**
             * Smallint/Bigint/Integers/Int are int
             */
            if (phalcon_memnstr_str(column_type, SL("INTEGER"))) {
                phalcon_array_update_string_long(&definition, SL("type"), PHALCON_DB_COLUMN_TYPE_INTEGER, PH_COPY);
                phalcon_array_update_string_bool(&definition, SL("isNumeric"), 1, PH_COPY);
                phalcon_array_update_string(&definition, SL("size"), column_precision, PH_COPY);
                phalcon_array_update_string_long(&definition, SL("bindType"), 1, PH_COPY);
                phalcon_array_update_string_long(&definition, SL("bytes"), 32, PH_COPY);
                break;
            }

            /**
             * Float/Smallfloats/Decimals are float
             */
            if (phalcon_memnstr_str(column_type, SL("FLOAT"))) {
                phalcon_array_update_string_long(&definition, SL("type"), PHALCON_DB_COLUMN_TYPE_FLOAT, PH_COPY);
                phalcon_array_update_string_bool(&definition, SL("isNumeric"), 1, PH_COPY);
                phalcon_array_update_string(&definition, SL("size"), column_size, PH_COPY);
                phalcon_array_update_string(&definition, SL("scale"), column_scale, PH_COPY);
                phalcon_array_update_string_long(&definition, SL("bindType"), 32, PH_COPY);
                break;
            }

            /**
             * Date
             */
            if (phalcon_memnstr_str(column_type, SL("TIMESTAMP"))) {
                phalcon_array_update_string_long(&definition, SL("type"), PHALCON_DB_COLUMN_TYPE_DATE, PH_COPY);
                break;
            }

            /**
             * Text
             */
            if (phalcon_memnstr_str(column_type, SL("RAW"))) {
                phalcon_array_update_string_long(&definition, SL("type"), PHALCON_DB_COLUMN_TYPE_TEXT, PH_COPY);
                break;
            }

            /**
             * Text
             */
            if (phalcon_memnstr_str(column_type, SL("BLOB"))) {
                phalcon_array_update_string_long(&definition, SL("type"), PHALCON_DB_COLUMN_TYPE_TEXT, PH_COPY);
                break;
            }

            /**
             * Text
             */
            if (phalcon_memnstr_str(column_type, SL("CLOB"))) {
                phalcon_array_update_string_long(&definition, SL("type"), PHALCON_DB_COLUMN_TYPE_TEXT, PH_COPY);
                break;
            }

            /**
             * Chars2 are string
             */
            if (phalcon_memnstr_str(column_type, SL("VARCHAR2"))) {
                phalcon_array_update_string_long(&definition, SL("type"), PHALCON_DB_COLUMN_TYPE_VARCHAR, PH_COPY);
                phalcon_array_update_string(&definition, SL("size"), column_size, PH_COPY);
                break;
            }

            /**
             * Chars are chars
             */
            if (phalcon_memnstr_str(column_type, SL("CHAR"))) {
                phalcon_array_update_string_long(&definition, SL("type"), PHALCON_DB_COLUMN_TYPE_CHAR, PH_COPY);
                phalcon_array_update_string(&definition, SL("size"), column_size, PH_COPY);
                break;
            }

            /**
             * Text are varchars
             */
            if (phalcon_memnstr_str(column_type, SL("text"))) {
                phalcon_array_update_string_long(&definition, SL("type"), PHALCON_DB_COLUMN_TYPE_TEXT, PH_COPY);
                break;
            }

            /**
             * By default is string
             */
            phalcon_array_update_string_long(&definition, SL("type"), PHALCON_DB_COLUMN_TYPE_VARCHAR, PH_COPY);
            break;
        }

        if (Z_TYPE_P(old_column) == IS_NULL) {
            phalcon_array_update_string_bool(&definition, SL("first"), 1, PH_COPY);
        } else {
            phalcon_array_update_string(&definition, SL("after"), old_column, PH_COPY);
        }

        /**
         * Check if the field is primary key
         */
        PHALCON_OBS_NVAR(attribute);
        phalcon_array_fetch_long(&attribute, field, 6, PH_NOISY);
        if (PHALCON_IS_STRING(attribute, "P")) {
            phalcon_array_update_string_bool(&definition, SL("primary"), 1, PH_COPY);
        }

        /**
         * Check if the column allows null values
         */
        PHALCON_OBS_NVAR(attribute);
        phalcon_array_fetch_long(&attribute, field, 5, PH_NOISY);
        if (PHALCON_IS_STRING(attribute, "N")) {
            phalcon_array_update_string_bool(&definition, SL("notNull"), 1, PH_COPY);
        }

        PHALCON_OBS_NVAR(column_name);
        phalcon_array_fetch_long(&column_name, field, 0, PH_NOISY);

        /**
         * If the column set the default values, get it
         */
        PHALCON_OBS_NVAR(attribute);
        phalcon_array_fetch_long(&attribute, field, 7, PH_NOISY);
        if (!PHALCON_IS_EMPTY(attribute)) {
            phalcon_array_update_string(&definition, SL("default"), attribute, PH_COPY);
        }

        /**
         * Create a Phalcon\Db\Column to abstract the column
         */
        PHALCON_INIT_NVAR(column);
        object_init_ex(column, phalcon_db_column_ce);
        PHALCON_CALL_METHOD(NULL, column, "__construct", column_name, definition);

        phalcon_array_append(&columns, column, PH_COPY);
        PHALCON_CPY_WRT(old_column, column_name);

        zend_hash_move_forward_ex(ah0, &hp0);
    }

    RETURN_CTOR(columns);
}
コード例 #19
0
ファイル: postgresql.c プロジェクト: banketree/cphalcon
/**
 * Returns an array of Phalcon\Db\Column objects describing a table
 *
 * <code>print_r($connection->describeColumns("posts")); ?></code>
 *
 * @param string $table
 * @param string $schema
 * @return Phalcon\Db\Column[]
 */
PHP_METHOD(Phalcon_Db_Adapter_Pdo_Postgresql, describeColumns){

	zval *table, *schema = NULL, *columns, *dialect, *sql = NULL, *fetch_num;
	zval *describe = NULL, *old_column = NULL, *field = NULL, *definition = NULL;
	zval *char_size = NULL, *numeric_size = NULL, *numeric_scale = NULL, *column_type = NULL;
	zval *attribute = NULL, *column_name = NULL, *column = NULL;
	HashTable *ah0;
	HashPosition hp0;
	zval **hd;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 1, 1, &table, &schema);
	
	if (!schema || !zend_is_true(schema)) {
		schema = phalcon_fetch_nproperty_this(this_ptr, SL("_schema"), PH_NOISY TSRMLS_CC);
	}
	
	PHALCON_INIT_VAR(columns);
	array_init(columns);
	
	PHALCON_OBS_VAR(dialect);
	phalcon_read_property_this(&dialect, this_ptr, SL("_dialect"), PH_NOISY TSRMLS_CC);
	
	PHALCON_CALL_METHOD(&sql, dialect, "describecolumns", table, schema);
	
	/** 
	 * We're using FETCH_NUM to fetch the columns
	 */
	PHALCON_INIT_VAR(fetch_num);
	ZVAL_LONG(fetch_num, PDO_FETCH_NUM);
	
	PHALCON_CALL_METHOD(&describe, this_ptr, "fetchall", sql, fetch_num);
	
	/** 
	 * 0:name, 1:type, 2:size, 3:numeric size, 4:numeric scale, 5: null, 6: key, 7: extra, 8: position, 9: element type
	 */
	PHALCON_INIT_VAR(old_column);
	
	phalcon_is_iterable(describe, &ah0, &hp0, 0, 0);
	
	while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) {
	
		PHALCON_GET_HVALUE(field);
	
		PHALCON_INIT_NVAR(definition);
		array_init_size(definition, 1);
		add_assoc_long_ex(definition, SS("bindType"), 2);
	
		PHALCON_OBS_NVAR(char_size);
		phalcon_array_fetch_long(&char_size, field, 2, PH_NOISY);
		if (Z_TYPE_P(char_size) != IS_NULL) {
			convert_to_long(char_size);
		}

		PHALCON_OBS_NVAR(numeric_size);
		phalcon_array_fetch_long(&numeric_size, field, 3, PH_NOISY);
		if (phalcon_is_numeric(numeric_size)) {
			convert_to_long(numeric_size);
		}

		PHALCON_OBS_NVAR(numeric_scale); 
		phalcon_array_fetch_long(&numeric_scale, field, 4, PH_NOISY);
		if (phalcon_is_numeric(numeric_scale)) {
			convert_to_long(numeric_scale);
		}
	
		PHALCON_OBS_NVAR(column_type);
		phalcon_array_fetch_long(&column_type, field, 1, PH_NOISY);

		/** 
		 * Check the column type to get the correct Phalcon type
		 */
		while (1) {
			/**
			 * Tinyint(1) is boolean
			 */
			if (phalcon_memnstr_str(column_type, SL("smallint(1)"))) {
				phalcon_array_update_string_long(&definition, SL("type"), PHALCON_DB_COLUMN_TYPE_BOOLEAN, PH_COPY);
				phalcon_array_update_string_long(&definition, SL("bindType"), 5, PH_COPY);
				break;
			}

			/**
			 * Smallint/Bigint/Integers/Int are int
			 */
			if (phalcon_memnstr_str(column_type, SL("int"))) {
				phalcon_array_update_string_long(&definition, SL("type"), PHALCON_DB_COLUMN_TYPE_INTEGER, PH_COPY);
				phalcon_array_update_string_bool(&definition, SL("isNumeric"), 1, PH_COPY);
				phalcon_array_update_string(&definition, SL("size"), numeric_size, PH_COPY);
				phalcon_array_update_string_long(&definition, SL("bindType"), 1, PH_COPY);
				break;
			}

			/**
			 * Varchar
			 */
			if (phalcon_memnstr_str(column_type, SL("varying"))) {
				phalcon_array_update_string_long(&definition, SL("type"), PHALCON_DB_COLUMN_TYPE_VARCHAR, PH_COPY);
				phalcon_array_update_string(&definition, SL("size"), char_size, PH_COPY);
				break;
			}

			/**
			 * Special type for datetime
			 */
			if (phalcon_memnstr_str(column_type, SL("date"))) {
				phalcon_array_update_string_long(&definition, SL("type"), PHALCON_DB_COLUMN_TYPE_DATE, PH_COPY);
				phalcon_array_update_string_long(&definition, SL("size"), 0, PH_COPY);
				break;
			}

			/**
			 * Numeric
			 */
			if (phalcon_memnstr_str(column_type, SL("numeric"))) {
				phalcon_array_update_string_long(&definition, SL("type"), PHALCON_DB_COLUMN_TYPE_DECIMAL, PH_COPY);
				phalcon_array_update_string_bool(&definition, SL("isNumeric"), 1, PH_COPY);
				if (phalcon_is_numeric(numeric_size)) {
					phalcon_array_update_string(&definition, SL("size"), numeric_size, PH_COPY);
					phalcon_array_update_string_long(&definition, SL("bytes"), Z_LVAL_P(numeric_size) * 8, PH_COPY);
				} else {
					phalcon_array_update_string_long(&definition, SL("size"), 30, PH_COPY);
					phalcon_array_update_string_long(&definition, SL("bytes"), 80, PH_COPY);
				}
				if (phalcon_is_numeric(numeric_scale)) {
					phalcon_array_update_string(&definition, SL("scale"), numeric_scale, PH_COPY);
				} else {
					phalcon_array_update_string_long(&definition, SL("scale"), 6, PH_COPY);
				}
				phalcon_array_update_string_long(&definition, SL("bindType"), 32, PH_COPY);
				break;
			}

			/**
			 * Chars are chars
			 */
			if (phalcon_memnstr_str(column_type, SL("char"))) {
				phalcon_array_update_string_long(&definition, SL("type"), PHALCON_DB_COLUMN_TYPE_CHAR, PH_COPY);
				phalcon_array_update_string(&definition, SL("size"), char_size, PH_COPY);
				break;
			}

			/**
			 * Date
			 */
			if (phalcon_memnstr_str(column_type, SL("timestamp"))) {
				phalcon_array_update_string_long(&definition, SL("type"), PHALCON_DB_COLUMN_TYPE_DATETIME, PH_COPY);
				phalcon_array_update_string_long(&definition, SL("size"), 0, PH_COPY);
				break;
			}

			/**
			 * Text are varchars
			 */
			if (phalcon_memnstr_str(column_type, SL("text"))) {
				phalcon_array_update_string_long(&definition, SL("type"), PHALCON_DB_COLUMN_TYPE_TEXT, PH_COPY);
				phalcon_array_update_string(&definition, SL("size"), char_size, PH_COPY);
				break;
			}

			/**
			 * Float/Smallfloats/Decimals are float
			 */
			if (phalcon_memnstr_str(column_type, SL("float"))) {
				phalcon_array_update_string_long(&definition, SL("type"), PHALCON_DB_COLUMN_TYPE_FLOAT, PH_COPY);
				phalcon_array_update_string_bool(&definition, SL("isNumeric"), 1, PH_COPY);
				phalcon_array_update_string(&definition, SL("size"), numeric_size, PH_COPY);
				phalcon_array_update_string_long(&definition, SL("bindType"), 32, PH_COPY);
				break;
			}

			/**
			 * Boolean
			 */
			if (phalcon_memnstr_str(column_type, SL("bool"))) {
				phalcon_array_update_string_long(&definition, SL("type"), PHALCON_DB_COLUMN_TYPE_BOOLEAN, PH_COPY);
				phalcon_array_update_string_long(&definition, SL("size"), 0, PH_COPY);
				phalcon_array_update_string_long(&definition, SL("bindType"), 5, PH_COPY);
				break;
			}

			/**
			 * UUID
			 */
			if (phalcon_memnstr_str(column_type, SL("uuid"))) {
				phalcon_array_update_string_long(&definition, SL("type"), PHALCON_DB_COLUMN_TYPE_CHAR, PH_COPY);
				phalcon_array_update_string_long(&definition, SL("size"), 36, PH_COPY);
				break;
			}

			/**
			 * JSON
			 */
			if (phalcon_memnstr_str(column_type, SL("json"))) {
				phalcon_array_update_string_long(&definition, SL("type"), PHALCON_DB_COLUMN_TYPE_JSON, PH_COPY);
				phalcon_array_update_string(&definition, SL("size"), char_size, PH_COPY);
				break;
			}

			/**
			 * ARRAY
			 */
			if (phalcon_memnstr_str(column_type, SL("ARRAY"))) {
				phalcon_array_update_string_long(&definition, SL("type"), PHALCON_DB_COLUMN_TYPE_ARRAY, PH_COPY);
				phalcon_array_update_string(&definition, SL("size"), char_size, PH_COPY);
				break;
			}

			/**
			 * By default is string
			 */
			phalcon_array_update_string_long(&definition, SL("type"), PHALCON_DB_COLUMN_TYPE_OTHER, PH_COPY);
			break;
		}

		if (phalcon_memnstr_str(column_type, SL("unsigned"))) {
			phalcon_array_update_string_bool(&definition, SL("unsigned"), 1, PH_COPY);
		}

		if (Z_TYPE_P(old_column) == IS_NULL) {
			phalcon_array_update_string_bool(&definition, SL("first"), 1, PH_COPY);
		} else {
			phalcon_array_update_string(&definition, SL("after"), old_column, PH_COPY);
		}

		/** 
		 * Check if the field is primary key
		 */
		PHALCON_OBS_NVAR(attribute);
		phalcon_array_fetch_long(&attribute, field, 6, PH_NOISY);
		if (PHALCON_IS_STRING(attribute, "PRI")) {
			phalcon_array_update_string_bool(&definition, SL("primary"), 1, PH_COPY);
		}

		/** 
		 * Check if the column allows null values
		 */
		PHALCON_OBS_NVAR(attribute);
		phalcon_array_fetch_long(&attribute, field, 5, PH_NOISY);
		if (PHALCON_IS_STRING(attribute, "NO")) {
			phalcon_array_update_string_bool(&definition, SL("notNull"), 1, PH_COPY);
		}

		/** 
		 * Check if the column is auto increment
		 */
		PHALCON_OBS_NVAR(attribute);
		phalcon_array_fetch_long(&attribute, field, 7, PH_NOISY);
		if (PHALCON_IS_STRING(attribute, "auto_increment")) {
			phalcon_array_update_string_bool(&definition, SL("autoIncrement"), 1, PH_COPY);
		} else if (!PHALCON_IS_EMPTY(attribute)) {
			phalcon_array_update_string(&definition, SL("default"), attribute, PH_COPY);
		}
	
		PHALCON_OBS_NVAR(column_name);
		phalcon_array_fetch_long(&column_name, field, 0, PH_NOISY);

		/** 
		 * Create a Phalcon\Db\Column to abstract the column
		 */
		PHALCON_INIT_NVAR(column);
		object_init_ex(column, phalcon_db_column_ce);
		PHALCON_CALL_METHOD(NULL, column, "__construct", column_name, definition);
	
		phalcon_array_append(&columns, column, PH_COPY);
		PHALCON_CPY_WRT(old_column, column_name);
	
		zend_hash_move_forward_ex(ah0, &hp0);
	}
	
	RETURN_CTOR(columns);
}
コード例 #20
0
ファイル: header.c プロジェクト: Myleft/cphalcon
PHP_METHOD(Phalcon_Http_Client_Header, parse){

	zval *content, *content_parts = NULL, *key = NULL, *header = NULL, *header_parts = NULL, *name = NULL, *value = NULL, *trimmed = NULL;
	HashTable *ah0;
	HashPosition hp0;
	zval **hd;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 1, 0, &content);

	if (PHALCON_IS_EMPTY(content)) {
		RETURN_MM_FALSE;
	}
	
	if (Z_TYPE_P(content) == IS_STRING) {
		PHALCON_INIT_VAR(content_parts);
		phalcon_fast_explode_str(content_parts, SL("\r\n"), content);
	} else if (Z_TYPE_P(content) == IS_ARRAY) {
		PHALCON_CPY_WRT_CTOR(content_parts, content);
	} else {
		RETURN_MM_FALSE;
	}	

	phalcon_is_iterable(content_parts, &ah0, &hp0, 0, 0);	
	while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) {
	
		PHALCON_GET_HKEY(key, ah0, hp0);
		PHALCON_GET_HVALUE(header);

		if (Z_TYPE_P(header) == IS_STRING) {
			PHALCON_INIT_NVAR(header_parts);
			if (phalcon_memnstr_str(header , SL(":"))) {
				phalcon_fast_explode_str(header_parts, SL(":"), header);
			} else {
				if (phalcon_start_with_str(header , SL("HTTP/"))) {
					phalcon_fast_explode_str(header_parts, SL(" "), header);
					if (Z_TYPE_P(header_parts) == IS_ARRAY && phalcon_array_isset_long(header_parts, 1) && phalcon_array_isset_long(header_parts, 2)) {
						PHALCON_OBS_NVAR(value);
						phalcon_array_fetch_long(&value, header_parts, 1, PH_NOISY);
						phalcon_update_property_this(this_ptr, SL("_status_code"), value TSRMLS_CC);

						PHALCON_OBS_NVAR(value);
						phalcon_array_fetch_long(&value, header_parts, 2, PH_NOISY);
						phalcon_update_property_this(this_ptr, SL("_status_message"), value TSRMLS_CC);
					}
				}

				zend_hash_move_forward_ex(ah0, &hp0);
				continue;
			}
		} else {
			PHALCON_CPY_WRT_CTOR(header_parts, header);
		}

		if (Z_TYPE_P(header_parts) == IS_ARRAY && phalcon_array_isset_long(header_parts, 0) && phalcon_array_isset_long(header_parts, 1)) {
				PHALCON_OBS_NVAR(name);
				phalcon_array_fetch_long(&name, header_parts, 0, PH_NOISY);

				PHALCON_OBS_NVAR(value);
				phalcon_array_fetch_long(&value, header_parts, 1, PH_NOISY);

				PHALCON_INIT_NVAR(trimmed);
				phalcon_fast_trim(trimmed, value, NULL, PHALCON_TRIM_BOTH TSRMLS_CC);

				PHALCON_CALL_METHOD(NULL, this_ptr, "set", name, trimmed);
		}

		zend_hash_move_forward_ex(ah0, &hp0);
	}

	PHALCON_MM_RESTORE();
}
コード例 #21
0
ファイル: manager.c プロジェクト: 11mariom/cphalcon
/**
 * Fires an event in the events manager causing that active listeners be notified about it
 *
 *<code>
 *	$eventsManager->fire('db', $connection);
 *</code>
 *
 * @param string $eventType
 * @param object $source
 * @param mixed  $data
 * @param int $cancelable
 * @return mixed
 */
PHP_METHOD(Phalcon_Events_Manager, fire){

	zval *event_type, *source, *data = NULL, *cancelable = NULL, *events;
	zval *exception_message, *event_parts, *type;
	zval *event_name, *status = NULL, *collect, *event = NULL, *fire_events = NULL;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 2, 2, &event_type, &source, &data, &cancelable);
	
	if (!data) {
		PHALCON_INIT_VAR(data);
	}
	
	if (!cancelable) {
		PHALCON_INIT_VAR(cancelable);
		ZVAL_BOOL(cancelable, 1);
	}
	
	if (unlikely(Z_TYPE_P(event_type) != IS_STRING)) {
		PHALCON_THROW_EXCEPTION_STR(phalcon_events_exception_ce, "Event type must be a string");
		return;
	}
	
	PHALCON_OBS_VAR(events);
	phalcon_read_property_this(&events, this_ptr, SL("_events"), PH_NOISY_CC);
	if (Z_TYPE_P(events) != IS_ARRAY) { 
		RETURN_MM_NULL();
	}
	
	/** 
	 * All valid events must have a colon separator
	 */
	if (!phalcon_memnstr_str(event_type, SL(":"))) {
		PHALCON_INIT_VAR(exception_message);
		PHALCON_CONCAT_SV(exception_message, "Invalid event type ", event_type);
		PHALCON_THROW_EXCEPTION_ZVAL(phalcon_events_exception_ce, exception_message);
		return;
	}
	
	PHALCON_INIT_VAR(event_parts);
	phalcon_fast_explode_str(event_parts, SL(":"), event_type);
	
	PHALCON_OBS_VAR(type);
	phalcon_array_fetch_long(&type, event_parts, 0, PH_NOISY);
	
	PHALCON_OBS_VAR(event_name);
	phalcon_array_fetch_long(&event_name, event_parts, 1, PH_NOISY);
	
	PHALCON_INIT_VAR(status);
	
	/** 
	 * Responses must be traced?
	 */
	PHALCON_OBS_VAR(collect);
	phalcon_read_property_this(&collect, this_ptr, SL("_collect"), PH_NOISY_CC);
	if (zend_is_true(collect)) {
		phalcon_update_property_null(this_ptr, SL("_responses") TSRMLS_CC);
	}
	
	PHALCON_INIT_VAR(event);
	
	/** 
	 * Check if events are grouped by type
	 */
	if (phalcon_array_isset(events, type)) {
	
		PHALCON_OBS_VAR(fire_events);
		phalcon_array_fetch(&fire_events, events, type, PH_NOISY);
		if (Z_TYPE_P(fire_events) == IS_ARRAY || Z_TYPE_P(fire_events) == IS_OBJECT) {
			/** 
			 * Create the event context
			 */
			object_init_ex(event, phalcon_events_event_ce);
			phalcon_call_method_p4_noret(event, "__construct", event_name, source, data, cancelable);
	
			/** 
			 * Call the events queue
			 */
			phalcon_call_method_p2(status, this_ptr, "firequeue", fire_events, event);
		}
	}
	
	/** 
	 * Check if there are listeners for the event type itself
	 */
	if (phalcon_array_isset(events, event_type)) {
	
		PHALCON_OBS_NVAR(fire_events);
		phalcon_array_fetch(&fire_events, events, event_type, PH_NOISY);
		if (Z_TYPE_P(fire_events) == IS_ARRAY || Z_TYPE_P(fire_events) == IS_OBJECT) {
	
			/** 
			 * Create the event if it wasn't created before
			 */
			if (Z_TYPE_P(event) == IS_NULL) {
				PHALCON_INIT_NVAR(event);
				object_init_ex(event, phalcon_events_event_ce);
				phalcon_call_method_p4_noret(event, "__construct", event_name, source, data, cancelable);
	
			}
	
			/** 
			 * Call the events queue
			 */
			PHALCON_INIT_NVAR(status);
			phalcon_call_method_p2(status, this_ptr, "firequeue", fire_events, event);
		}
	}
	
	RETURN_CCTOR(status);
}
コード例 #22
0
ファイル: mysql.c プロジェクト: Dinesh-Ramakrishnan/cphalcon
/**
 * Returns an array of Phalcon\Db\Column objects describing a table
 *
 * <code>
 * print_r($connection->describeColumns("posts")); ?>
 * </code>
 *
 * @param string $table
 * @param string $schema
 * @return Phalcon\Db\Column[]
 */
PHP_METHOD(Phalcon_Db_Adapter_Pdo_Mysql, describeColumns){

	zval *table, *schema = NULL, *dialect, *ztrue, *sql, *fetch_num;
	zval *describe, *old_column = NULL, *size_pattern, *columns;
	zval *field = NULL, *definition = NULL, *column_type = NULL, *matches = NULL;
	zval *pos = NULL, *match_one = NULL, *match_two = NULL, *attribute = NULL, *column_name = NULL;
	zval *column = NULL;
	HashTable *ah0;
	HashPosition hp0;
	zval **hd;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 1, 1, &table, &schema);
	
	if (!schema) {
		PHALCON_INIT_VAR(schema);
	}
	
	PHALCON_OBS_VAR(dialect);
	phalcon_read_property_this(&dialect, this_ptr, SL("_dialect"), PH_NOISY_CC);
	
	PHALCON_INIT_VAR(ztrue);
	ZVAL_BOOL(ztrue, 1);
	
	/** 
	 * Get the SQL to describe a table
	 */
	PHALCON_INIT_VAR(sql);
	phalcon_call_method_p2(sql, dialect, "describecolumns", table, schema);
	
	/** 
	 * We're using FETCH_NUM to fetch the columns
	 */
	PHALCON_INIT_VAR(fetch_num);
	ZVAL_LONG(fetch_num, 3);
	
	/** 
	 * Get the describe
	 */
	PHALCON_INIT_VAR(describe);
	phalcon_call_method_p2(describe, this_ptr, "fetchall", sql, fetch_num);
	
	PHALCON_INIT_VAR(old_column);
	
	PHALCON_INIT_VAR(size_pattern);
	ZVAL_STRING(size_pattern, "#\\(([0-9]++)(?:,\\s*([0-9]++))?\\)#", 1);
	
	PHALCON_INIT_VAR(columns);
	array_init(columns);
	
	/** 
	 * Field Indexes: 0:name, 1:type, 2:not null, 3:key, 4:default, 5:extra
	 */
	phalcon_is_iterable(describe, &ah0, &hp0, 0, 0);
	
	while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) {
	
		PHALCON_GET_HVALUE(field);
	
		/** 
		 * By default the bind types is two
		 */
		PHALCON_INIT_NVAR(definition);
		array_init_size(definition, 1);
		add_assoc_long_ex(definition, SS("bindType"), 2);
	
		/** 
		 * By checking every column type we convert it to a Phalcon\Db\Column
		 */
		PHALCON_OBS_NVAR(column_type);
		phalcon_array_fetch_long(&column_type, field, 1, PH_NOISY);
	
		while (1) {
	
			/** 
			 * Enum are treated as char
			 */
			if (phalcon_memnstr_str(column_type, SL("enum"))) {
				phalcon_array_update_string_long(&definition, SL("type"), 5, PH_SEPARATE);
				break;
			}
	
			/** 
			 * Smallint/Bigint/Integers/Int are int
			 */
			if (phalcon_memnstr_str(column_type, SL("int"))) {
				phalcon_array_update_string_long(&definition, SL("type"), 0, PH_SEPARATE);
				phalcon_array_update_string(&definition, SL("isNumeric"), &ztrue, PH_COPY | PH_SEPARATE);
				phalcon_array_update_string_long(&definition, SL("bindType"), 1, PH_SEPARATE);
				break;
			}
	
			/** 
			 * Varchar are varchars
			 */
			if (phalcon_memnstr_str(column_type, SL("varchar"))) {
				phalcon_array_update_string_long(&definition, SL("type"), 2, PH_SEPARATE);
				break;
			}
	
			/** 
			 * Special type for datetime
			 */
			if (phalcon_memnstr_str(column_type, SL("datetime"))) {
				phalcon_array_update_string_long(&definition, SL("type"), 4, PH_SEPARATE);
				break;
			}
	
			/** 
			 * Decimals are floats
			 */
			if (phalcon_memnstr_str(column_type, SL("decimal"))) {
				phalcon_array_update_string_long(&definition, SL("type"), 3, PH_SEPARATE);
				phalcon_array_update_string(&definition, SL("isNumeric"), &ztrue, PH_COPY | PH_SEPARATE);
				phalcon_array_update_string_long(&definition, SL("bindType"), 32, PH_SEPARATE);
				break;
			}
	
			/** 
			 * Chars are chars
			 */
			if (phalcon_memnstr_str(column_type, SL("char"))) {
				phalcon_array_update_string_long(&definition, SL("type"), 5, PH_SEPARATE);
				break;
			}
	
			/** 
			 * Date/Datetime are varchars
			 */
			if (phalcon_memnstr_str(column_type, SL("date"))) {
				phalcon_array_update_string_long(&definition, SL("type"), 1, PH_SEPARATE);
				break;
			}
	
			/** 
			 * Text are varchars
			 */
			if (phalcon_memnstr_str(column_type, SL("text"))) {
				phalcon_array_update_string_long(&definition, SL("type"), 6, PH_SEPARATE);
				break;
			}
	
			/** 
			 * Float/Smallfloats/Decimals are float
			 */
			if (phalcon_memnstr_str(column_type, SL("float"))) {
				phalcon_array_update_string_long(&definition, SL("type"), 7, PH_SEPARATE);
				phalcon_array_update_string(&definition, SL("isNumeric"), &ztrue, PH_COPY | PH_SEPARATE);
				phalcon_array_update_string_long(&definition, SL("bindType"), 32, PH_SEPARATE);
				break;
			}
	
			/** 
			 * By default is string
			 */
			phalcon_array_update_string_long(&definition, SL("type"), 2, PH_SEPARATE);
			break;
		}
	
		/** 
		 * If the column type has a parentheses we try to get the column size from it
		 */
		if (phalcon_memnstr_str(column_type, SL("("))) {
	
			PHALCON_INIT_NVAR(matches);
	
			PHALCON_INIT_NVAR(pos);
			phalcon_preg_match(pos, size_pattern, column_type, matches TSRMLS_CC);

			if (zend_is_true(pos)) {
				if (phalcon_array_isset_long(matches, 1)) {
					PHALCON_OBS_NVAR(match_one);
					phalcon_array_fetch_long(&match_one, matches, 1, PH_NOISY);
					phalcon_array_update_string(&definition, SL("size"), &match_one, PH_COPY | PH_SEPARATE);
				}
				if (phalcon_array_isset_long(matches, 2)) {
                                        PHALCON_OBS_NVAR(match_two);
                                        phalcon_array_fetch_long(&match_two, matches, 2, PH_NOISY);
                                        phalcon_array_update_string(&definition, SL("scale"), &match_two, PH_COPY | PH_SEPARATE);
                                }
			}
		}
	
		/** 
		 * Check if the column is unsigned, only MySQL support this
		 */
		if (phalcon_memnstr_str(column_type, SL("unsigned"))) {
			phalcon_array_update_string(&definition, SL("unsigned"), &ztrue, PH_COPY | PH_SEPARATE);
		}
	
		/** 
		 * Positions
		 */
		if (!zend_is_true(old_column)) {
			phalcon_array_update_string(&definition, SL("first"), &ztrue, PH_COPY | PH_SEPARATE);
		} else {
			phalcon_array_update_string(&definition, SL("after"), &old_column, PH_COPY | PH_SEPARATE);
		}
	
		/** 
		 * Check if the field is primary key
		 */
		PHALCON_OBS_NVAR(attribute);
		phalcon_array_fetch_long(&attribute, field, 3, PH_NOISY);
		if (PHALCON_IS_STRING(attribute, "PRI")) {
			phalcon_array_update_string(&definition, SL("primary"), &ztrue, PH_COPY | PH_SEPARATE);
		}
	
		/** 
		 * Check if the column allows null values
		 */
		PHALCON_OBS_NVAR(attribute);
		phalcon_array_fetch_long(&attribute, field, 2, PH_NOISY);
		if (PHALCON_IS_STRING(attribute, "NO")) {
			phalcon_array_update_string(&definition, SL("notNull"), &ztrue, PH_COPY | PH_SEPARATE);
		}
	
		/** 
		 * Check if the column is auto increment
		 */
		PHALCON_OBS_NVAR(attribute);
		phalcon_array_fetch_long(&attribute, field, 5, PH_NOISY);
		if (PHALCON_IS_STRING(attribute, "auto_increment")) {
			phalcon_array_update_string(&definition, SL("autoIncrement"), &ztrue, PH_COPY | PH_SEPARATE);
		}
	
		PHALCON_OBS_NVAR(column_name);
		phalcon_array_fetch_long(&column_name, field, 0, PH_NOISY);
	
		/** 
		 * Every route is stored as a Phalcon\Db\Column
		 */
		PHALCON_INIT_NVAR(column);
		object_init_ex(column, phalcon_db_column_ce);
		phalcon_call_method_p2_noret(column, "__construct", column_name, definition);
	
		phalcon_array_append(&columns, column, PH_SEPARATE);
		PHALCON_CPY_WRT(old_column, column_name);
	
		zend_hash_move_forward_ex(ah0, &hp0);
	}
	
	RETURN_CTOR(columns);
}
コード例 #23
0
ファイル: tag.c プロジェクト: angkatan21/cphalcon
/**
 * Builds generic INPUT tags
 *
 * @param   string $type
 * @param array $parameters
 * @param 	boolean $asValue
 * @return string
 */
PHP_METHOD(Phalcon_Tag, _inputField){

	zval *type, *parameters, *as_value = NULL, *params = NULL, *value = NULL;
	zval *id = NULL, *name, *code, *key = NULL, *five, *doctype, *is_xhtml;
	HashTable *ah0;
	HashPosition hp0;
	zval **hd;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 2, 1, &type, &parameters, &as_value);
	
	if (!as_value) {
		PHALCON_INIT_VAR(as_value);
		ZVAL_BOOL(as_value, 0);
	}
	
	if (Z_TYPE_P(parameters) != IS_ARRAY) { 
		PHALCON_INIT_VAR(params);
		array_init_size(params, 1);
		phalcon_array_append(&params, parameters, PH_SEPARATE TSRMLS_CC);
	} else {
		PHALCON_CPY_WRT(params, parameters);
	}
	
	PHALCON_INIT_VAR(value);
	if (PHALCON_IS_FALSE(as_value)) {
		if (!phalcon_array_isset_long(params, 0)) {
			PHALCON_OBS_VAR(id);
			phalcon_array_fetch_string(&id, params, SL("id"), PH_NOISY_CC);
			phalcon_array_update_long(&params, 0, &id, PH_COPY | PH_SEPARATE TSRMLS_CC);
		}
	
		PHALCON_OBS_NVAR(id);
		phalcon_array_fetch_long(&id, params, 0, PH_NOISY_CC);
		if (!phalcon_array_isset_string(params, SS("name"))) {
			phalcon_array_update_string(&params, SL("name"), &id, PH_COPY | PH_SEPARATE TSRMLS_CC);
		} else {
			PHALCON_OBS_VAR(name);
			phalcon_array_fetch_string(&name, params, SL("name"), PH_NOISY_CC);
			if (!zend_is_true(name)) {
				phalcon_array_update_string(&params, SL("name"), &id, PH_COPY | PH_SEPARATE TSRMLS_CC);
			}
		}
	
		/** 
		 * Automatically assign the id if the name is not an array
		 */
		if (!phalcon_memnstr_str(id, SL("[") TSRMLS_CC)) {
			if (!phalcon_array_isset_string(params, SS("id"))) {
				phalcon_array_update_string(&params, SL("id"), &id, PH_COPY | PH_SEPARATE TSRMLS_CC);
			}
		}
	
		if (!phalcon_array_isset_string(params, SS("value"))) {
			PHALCON_CALL_SELF_PARAMS_2(value, this_ptr, "getvalue", id, params);
			phalcon_array_update_string(&params, SL("value"), &value, PH_COPY | PH_SEPARATE TSRMLS_CC);
		}
	} else {
		if (phalcon_array_isset_long(params, 0)) {
			PHALCON_OBS_NVAR(value);
			phalcon_array_fetch_long(&value, params, 0, PH_NOISY_CC);
			phalcon_array_update_string(&params, SL("value"), &value, PH_COPY | PH_SEPARATE TSRMLS_CC);
		}
	}
	
	/** 
	 * Automatically check inputs
	 */
	if (PHALCON_IS_STRING(type, "checkbox")) {
		if (zend_is_true(value)) {
			phalcon_array_update_string_string(&params, SL("checked"), SL("checked"), PH_SEPARATE TSRMLS_CC);
		}
	} else {
		if (PHALCON_IS_STRING(type, "radio")) {
			if (zend_is_true(value)) {
				phalcon_array_update_string_string(&params, SL("checked"), SL("checked"), PH_SEPARATE TSRMLS_CC);
			}
		}
	}
	
	PHALCON_INIT_VAR(code);
	PHALCON_CONCAT_SVS(code, "<input type=\"", type, "\"");
	
	if (!phalcon_is_iterable(params, &ah0, &hp0, 0, 0 TSRMLS_CC)) {
		return;
	}
	
	while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) {
	
		PHALCON_GET_FOREACH_KEY(key, ah0, hp0);
		PHALCON_GET_FOREACH_VALUE(value);
	
		if (Z_TYPE_P(key) != IS_LONG) {
			PHALCON_SCONCAT_SVSVS(code, " ", key, "=\"", value, "\"");
		}
	
		zend_hash_move_forward_ex(ah0, &hp0);
	}
	
	PHALCON_INIT_VAR(five);
	ZVAL_LONG(five, 5);
	
	PHALCON_OBS_VAR(doctype);
	phalcon_read_static_property(&doctype, SL("phalcon\\tag"), SL("_documentType") TSRMLS_CC);
	
	/** 
	 * Check if Doctype is XHTML
	 */
	PHALCON_INIT_VAR(is_xhtml);
	is_smaller_function(is_xhtml, five, doctype TSRMLS_CC);
	if (zend_is_true(is_xhtml)) {
		phalcon_concat_self_str(&code, SL(" />") TSRMLS_CC);
	} else {
		phalcon_concat_self_str(&code, SL(">") TSRMLS_CC);
	}
	
	
	RETURN_CTOR(code);
}
コード例 #24
0
ファイル: oracle.c プロジェクト: RSivakov/cphalcon
/**
 * Returns an array of Phalcon\Db\Column objects describing a table
 *
 * <code>print_r($connection->describeColumns("posts")); ?></code>
 *
 * @param string $table
 * @param string $schema
 * @return Phalcon\Db\Column[]
 */
PHP_METHOD(Phalcon_Db_Adapter_Pdo_Oracle, describeColumns){

	zval *table, *schema = NULL, *columns, *dialect, *sql, *fetch_num;
	zval *describe, *old_column = NULL, *field = NULL, *definition = NULL;
	zval *column_size = NULL, *column_precision = NULL, *column_scale = NULL;
	zval *column_type = NULL, *attribute = NULL, *column_name = NULL;
	zval *column = NULL;
	HashTable *ah0;
	HashPosition hp0;
	zval **hd;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 1, 1, &table, &schema);
	
	if (!schema) {
		PHALCON_INIT_VAR(schema);
	}
	
	PHALCON_INIT_VAR(columns);
	array_init(columns);
	
	PHALCON_OBS_VAR(dialect);
	phalcon_read_property_this(&dialect, this_ptr, SL("_dialect"), PH_NOISY_CC);
	
	PHALCON_INIT_VAR(sql);
	phalcon_call_method_p2(sql, dialect, "describecolumns", table, schema);
	
	/** 
	 * We're using FETCH_NUM to fetch the columns
	 */
	PHALCON_INIT_VAR(fetch_num);
	ZVAL_LONG(fetch_num, 3);
	
	PHALCON_INIT_VAR(describe);
	phalcon_call_method_p2(describe, this_ptr, "fetchall", sql, fetch_num);
	
	/** 
	 *  0:column_name, 1:data_type, 2:data_length, 3:data_precision, 4:data_scale,
	 * 5:nullable, 6:constraint_type, 7:default, 8:position;
	 */
	PHALCON_INIT_VAR(old_column);
	
	phalcon_is_iterable(describe, &ah0, &hp0, 0, 0);
	
	while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) {
	
		PHALCON_GET_HVALUE(field);
	
		PHALCON_INIT_NVAR(definition);
		array_init_size(definition, 1);
		add_assoc_long_ex(definition, SS("bindType"), 2);
	
		PHALCON_OBS_NVAR(column_size);
		phalcon_array_fetch_long(&column_size, field, 2, PH_NOISY_CC);
	
		PHALCON_OBS_NVAR(column_precision);
		phalcon_array_fetch_long(&column_precision, field, 3, PH_NOISY_CC);
	
		PHALCON_OBS_NVAR(column_scale);
		phalcon_array_fetch_long(&column_scale, field, 4, PH_NOISY_CC);
	
		PHALCON_OBS_NVAR(column_type);
		phalcon_array_fetch_long(&column_type, field, 1, PH_NOISY_CC);
	
		/** 
		 * Check the column type to get the correct Phalcon type
		 */
		if (phalcon_memnstr_str(column_type, SL("NUMBER") TSRMLS_CC)) {
			phalcon_array_update_string_long(&definition, SL("type"), 3, PH_SEPARATE TSRMLS_CC);
			phalcon_array_update_string_bool(&definition, SL("isNumeric"), 1, PH_SEPARATE TSRMLS_CC);
			phalcon_array_update_string(&definition, SL("size"), &column_precision, PH_COPY | PH_SEPARATE TSRMLS_CC);
			phalcon_array_update_string(&definition, SL("scale"), &column_scale, PH_COPY | PH_SEPARATE TSRMLS_CC);
			phalcon_array_update_string_long(&definition, SL("bindType"), 32, PH_SEPARATE TSRMLS_CC);
		} else {
			if (phalcon_memnstr_str(column_type, SL("INTEGER") TSRMLS_CC)) {
				phalcon_array_update_string_long(&definition, SL("type"), 0, PH_SEPARATE TSRMLS_CC);
				phalcon_array_update_string_bool(&definition, SL("isNumeric"), 1, PH_SEPARATE TSRMLS_CC);
				phalcon_array_update_string(&definition, SL("size"), &column_precision, PH_COPY | PH_SEPARATE TSRMLS_CC);
				phalcon_array_update_string_long(&definition, SL("bindType"), 1, PH_SEPARATE TSRMLS_CC);
			} else {
				if (phalcon_memnstr_str(column_type, SL("VARCHAR2") TSRMLS_CC)) {
					phalcon_array_update_string_long(&definition, SL("type"), 2, PH_SEPARATE TSRMLS_CC);
					phalcon_array_update_string(&definition, SL("size"), &column_size, PH_COPY | PH_SEPARATE TSRMLS_CC);
				} else {
					if (phalcon_memnstr_str(column_type, SL("FLOAT") TSRMLS_CC)) {
						phalcon_array_update_string_long(&definition, SL("type"), 7, PH_SEPARATE TSRMLS_CC);
						phalcon_array_update_string_bool(&definition, SL("isNumeric"), 1, PH_SEPARATE TSRMLS_CC);
						phalcon_array_update_string(&definition, SL("size"), &column_size, PH_COPY | PH_SEPARATE TSRMLS_CC);
						phalcon_array_update_string(&definition, SL("scale"), &column_scale, PH_COPY | PH_SEPARATE TSRMLS_CC);
						phalcon_array_update_string_long(&definition, SL("bindType"), 32, PH_SEPARATE TSRMLS_CC);
					} else {
						if (phalcon_memnstr_str(column_type, SL("TIMESTAMP") TSRMLS_CC)) {
							phalcon_array_update_string_long(&definition, SL("type"), 1, PH_SEPARATE TSRMLS_CC);
						} else {
							if (phalcon_memnstr_str(column_type, SL("RAW") TSRMLS_CC)) {
								phalcon_array_update_string_long(&definition, SL("type"), 6, PH_SEPARATE TSRMLS_CC);
							} else {
								if (phalcon_memnstr_str(column_type, SL("BLOB") TSRMLS_CC)) {
									phalcon_array_update_string_long(&definition, SL("type"), 6, PH_SEPARATE TSRMLS_CC);
								} else {
									if (phalcon_memnstr_str(column_type, SL("CLOB") TSRMLS_CC)) {
										phalcon_array_update_string_long(&definition, SL("type"), 6, PH_SEPARATE TSRMLS_CC);
									} else {
										if (phalcon_memnstr_str(column_type, SL("VARCHAR2") TSRMLS_CC)) {
											phalcon_array_update_string_long(&definition, SL("type"), 2, PH_SEPARATE TSRMLS_CC);
											phalcon_array_update_string(&definition, SL("size"), &column_size, PH_COPY | PH_SEPARATE TSRMLS_CC);
										} else {
											if (phalcon_memnstr_str(column_type, SL("CHAR") TSRMLS_CC)) {
												phalcon_array_update_string_long(&definition, SL("type"), 5, PH_SEPARATE TSRMLS_CC);
												phalcon_array_update_string(&definition, SL("size"), &column_size, PH_COPY | PH_SEPARATE TSRMLS_CC);
											} else {
												phalcon_array_update_string_long(&definition, SL("type"), 6, PH_SEPARATE TSRMLS_CC);
											}
										}
									}
								}
							}
						}
					}
				}
			}
		}
	
		if (Z_TYPE_P(old_column) == IS_NULL) {
			phalcon_array_update_string_bool(&definition, SL("first"), 1, PH_SEPARATE TSRMLS_CC);
		} else {
			phalcon_array_update_string(&definition, SL("after"), &old_column, PH_COPY | PH_SEPARATE TSRMLS_CC);
		}
	
		/** 
		 * Check if the field is primary key
		 */
		PHALCON_OBS_NVAR(attribute);
		phalcon_array_fetch_long(&attribute, field, 6, PH_NOISY_CC);
		if (PHALCON_IS_STRING(attribute, "P")) {
			phalcon_array_update_string_bool(&definition, SL("primary"), 1, PH_SEPARATE TSRMLS_CC);
		}
	
		/** 
		 * Check if the column allows null values
		 */
		PHALCON_OBS_NVAR(attribute);
		phalcon_array_fetch_long(&attribute, field, 5, PH_NOISY_CC);
		if (PHALCON_IS_STRING(attribute, "N")) {
			phalcon_array_update_string_bool(&definition, SL("notNull"), 1, PH_SEPARATE TSRMLS_CC);
		}
	
		PHALCON_OBS_NVAR(column_name);
		phalcon_array_fetch_long(&column_name, field, 0, PH_NOISY_CC);
	
		/** 
		 * Create a Phalcon\Db\Column to abstract the column
		 */
		PHALCON_INIT_NVAR(column);
		object_init_ex(column, phalcon_db_column_ce);
		phalcon_call_method_p2_noret(column, "__construct", column_name, definition);
	
		phalcon_array_append(&columns, column, PH_SEPARATE TSRMLS_CC);
		PHALCON_CPY_WRT(old_column, column_name);
	
		zend_hash_move_forward_ex(ah0, &hp0);
	}
	
	RETURN_CTOR(columns);
}
コード例 #25
0
ファイル: text.c プロジェクト: Myleft/cphalcon
/**
 * Returns human readable sizes
 *
 * @param int $size
 * @param string $forceUnit
 * @param string $format
 * @param boolean $si
 * @return string
 */
PHP_METHOD(Phalcon_Text, bytes){

	zval *z_size, *z_force_unit = NULL, *format = NULL, *si = NULL;
	char *force_unit;
	const char **units;
	const char *units1[] = {"B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"};
	const char *units2[] = {"B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"};
	double size;
	int mod, power = 0, found = 0, i, j = 0;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 1, 3, &z_size, &z_force_unit, &format, &si);

	PHALCON_SEPARATE_PARAM(z_size);
	convert_to_double(z_size);

	size = Z_DVAL_P(z_size);

	if (!z_force_unit) {
		PHALCON_INIT_VAR(z_force_unit);
	} else {
		PHALCON_SEPARATE_PARAM(z_force_unit);
		convert_to_string(z_force_unit);
	}

	if (!format) {
		PHALCON_INIT_VAR(format);
	} else {
		PHALCON_SEPARATE_PARAM(format);
		convert_to_string(format);
	}
		
	if (PHALCON_IS_EMPTY(format)) {
		PHALCON_INIT_NVAR(format);
		ZVAL_STRING(format, "%01.2f %s", 1);
	}

	if (!si) {
		si = PHALCON_GLOBAL(z_true);
	}

	if (!zend_is_true(si) || (!PHALCON_IS_EMPTY(z_force_unit) && phalcon_memnstr_str(z_force_unit, SL("i")))) {
		units = units2;
		mod = 1024;
	} else {
		units = units1;
		mod = 1000;
	}

	if (!PHALCON_IS_EMPTY(z_force_unit)) {
		force_unit = Z_STRVAL_P(z_force_unit);
		for (i = 0; i < sizeof(units); i++)
		{
			if (strcasecmp(force_unit, units[i]) == 0) {
				found = 1;
				power = i;
				break;
			}
		}
	}

	if (found) {
		while(j < power) {
			size /= mod;
			j++;
		}
	} else {
		while(size > mod) {
			size /= mod;
			power++;
		}
	}

	PHALCON_INIT_NVAR(z_size);
	ZVAL_DOUBLE(z_size, size);

	PHALCON_INIT_NVAR(z_force_unit);
	ZVAL_STRING(z_force_unit, units[power], 1);

	PHALCON_RETURN_CALL_FUNCTION("sprintf", format, z_size, z_force_unit);

	RETURN_MM();
}
コード例 #26
0
ファイル: stream.c プロジェクト: MyleftStudio/cphalcon
PHP_METHOD(Phalcon_Http_Client_Adapter_Stream, buildBody){

	zval *stream, *header, *data, *file, *username, *password, *authtype, *digest, *method, *entity_body;
	zval *key = NULL, *value = NULL, *realm, *qop, *nonce, *nc, *cnonce, *qoc, *ha1 = NULL, *path = NULL, *md5_entity_body = NULL, *ha2 = NULL;
	zval *http, *option = NULL, *body, *headers = NULL, *uniqid = NULL, *boundary;
	zval *path_parts = NULL, *filename, *basename, *filedata = NULL, *tmp = NULL;
	HashTable *ah0;
	HashPosition hp0;
	zval **hd;

	PHALCON_MM_GROW();

	stream = phalcon_fetch_nproperty_this(this_ptr, SL("_stream"), PH_NOISY TSRMLS_CC);
	header = phalcon_fetch_nproperty_this(this_ptr, SL("_header"), PH_NOISY TSRMLS_CC);
	data = phalcon_fetch_nproperty_this(this_ptr, SL("_data"), PH_NOISY TSRMLS_CC);
	file = phalcon_fetch_nproperty_this(this_ptr, SL("_file"), PH_NOISY TSRMLS_CC);
	username = phalcon_fetch_nproperty_this(this_ptr, SL("_username"), PH_NOISY TSRMLS_CC);
	password = phalcon_fetch_nproperty_this(this_ptr, SL("_password"), PH_NOISY TSRMLS_CC);
	authtype = phalcon_fetch_nproperty_this(this_ptr, SL("_authtype"), PH_NOISY TSRMLS_CC);
	digest = phalcon_fetch_nproperty_this(this_ptr, SL("_digest"), PH_NOISY TSRMLS_CC);
	method = phalcon_fetch_nproperty_this(this_ptr, SL("_method"), PH_NOISY TSRMLS_CC);
	entity_body = phalcon_fetch_nproperty_this(this_ptr, SL("_entity_body"), PH_NOISY TSRMLS_CC);

	if (PHALCON_IS_NOT_EMPTY(username)) {
		if (PHALCON_IS_STRING(authtype, "basic")) {
			PHALCON_INIT_NVAR(key);
			ZVAL_STRING(key, "Authorization", 1);

			PHALCON_INIT_NVAR(value);
			PHALCON_CONCAT_SVSV(value, "Basic ", username, ":", password);

			PHALCON_CALL_METHOD(NULL, header, "set", key, value);
		} else if (PHALCON_IS_STRING(authtype, "digest") && PHALCON_IS_NOT_EMPTY(digest)) {
			if (phalcon_array_isset_string_fetch(&realm, digest, SS("realm"))) {
				PHALCON_INIT_VAR(realm);
				ZVAL_NULL(realm);
			}

			PHALCON_INIT_NVAR(tmp);
			PHALCON_CONCAT_VSVSV(tmp, username, ":", realm, ":", password);

			PHALCON_CALL_FUNCTION(&ha1, "md5", tmp);

			if (!phalcon_array_isset_string_fetch(&qop, digest, SS("qop"))) {
				PHALCON_INIT_VAR(qop);
				ZVAL_NULL(qop);
			}

			if (PHALCON_IS_EMPTY(qop) || phalcon_memnstr_str(qop, SL("auth"))) {
				PHALCON_CALL_SELF(&path, "getpath");

				PHALCON_INIT_NVAR(tmp);
				PHALCON_CONCAT_VSV(tmp, method, ":", path);

				PHALCON_CALL_FUNCTION(&ha2, "md5", tmp);
				
			} else if (phalcon_memnstr_str(qop, SL("auth-int"))) {
				PHALCON_CALL_SELF(&path, "getpath");

				PHALCON_CALL_FUNCTION(&md5_entity_body, "md5", entity_body);

				PHALCON_INIT_NVAR(tmp);
				PHALCON_CONCAT_VSVSV(tmp, method, ":", path, ":", md5_entity_body);

				PHALCON_CALL_FUNCTION(&ha2, "md5", tmp);
			}

			PHALCON_INIT_NVAR(key);
			ZVAL_STRING(key, "Authorization", 1);

			if (phalcon_array_isset_string_fetch(&nonce, digest, SS("nonce"))) {
				PHALCON_INIT_VAR(nonce);
				ZVAL_NULL(nonce);
			}


			if (PHALCON_IS_EMPTY(qop)) {
				PHALCON_INIT_NVAR(tmp);
				PHALCON_CONCAT_VSVSV(tmp, ha1, ":", nonce, ":", ha2);

				PHALCON_CALL_FUNCTION(&value, "md5", tmp);

				PHALCON_INIT_NVAR(tmp);
				PHALCON_CONCAT_SV(tmp, "Digest ", value);

				PHALCON_CALL_METHOD(NULL, header, "set", key, tmp);
			} else {			
				if (phalcon_array_isset_string_fetch(&nc, digest, SS("nc"))) {
					PHALCON_INIT_VAR(nc);
					ZVAL_NULL(nc);
				}
				
				if (phalcon_array_isset_string_fetch(&cnonce, digest, SS("cnonce"))) {
					PHALCON_INIT_VAR(cnonce);
					ZVAL_NULL(cnonce);
				}
				
				if (phalcon_array_isset_string_fetch(&qoc, digest, SS("qoc"))) {
					PHALCON_INIT_VAR(qoc);
					ZVAL_NULL(qoc);
				}
				
				if (phalcon_array_isset_string_fetch(&qoc, digest, SS("qoc"))) {
					PHALCON_INIT_VAR(qoc);
					ZVAL_NULL(qoc);
				}

				PHALCON_INIT_NVAR(tmp);
				PHALCON_CONCAT_VSVSVS(tmp, ha1, ":", nonce, ":", nc, ":");

				PHALCON_SCONCAT_VSVSV(tmp, cnonce, ":", qoc, ":", ha2);

				PHALCON_CALL_FUNCTION(&value, "md5", tmp);

				PHALCON_INIT_NVAR(tmp);
				PHALCON_CONCAT_SV(tmp, "Digest ", value);

				PHALCON_CALL_METHOD(NULL, header, "set", key, tmp);
			}
		}
	}

	PHALCON_INIT_VAR(http);
	ZVAL_STRING(http, "http", 1);

	PHALCON_CALL_FUNCTION(&uniqid, "uniqid");

	PHALCON_INIT_VAR(boundary);
	PHALCON_CONCAT_SV(boundary, "--------------", uniqid);

	PHALCON_INIT_VAR(body);

	if (PHALCON_IS_NOT_EMPTY(data) && Z_TYPE_P(data) == IS_STRING){
		PHALCON_INIT_NVAR(key);
		ZVAL_STRING(key, "Content-Type", 1);

		PHALCON_INIT_NVAR(value);
		ZVAL_STRING(value, "application/x-www-form-urlencoded", 1);

		PHALCON_CALL_METHOD(NULL, header, "set", key, value);

		PHALCON_INIT_NVAR(option);
		ZVAL_LONG(option, PHALCON_HTTP_CLIENT_HEADER_BUILD_FIELDS);
		
		PHALCON_CALL_METHOD(&headers, header, "build", option);

		PHALCON_INIT_NVAR(option);
		ZVAL_STRING(option, "header", 1);

		PHALCON_CALL_FUNCTION(NULL, "stream_context_set_option", stream, http, option, headers);

		PHALCON_INIT_NVAR(option);
		ZVAL_STRING(option, "content", 1);

		PHALCON_CALL_FUNCTION(NULL, "stream_context_set_option", stream, http, option, data);
		RETURN_MM();
	}
	
	if (Z_TYPE_P(data) == IS_ARRAY) {
		phalcon_is_iterable(data, &ah0, &hp0, 0, 0);
		while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) {
			PHALCON_GET_HKEY(key, ah0, hp0);
			PHALCON_GET_HVALUE(value);

			PHALCON_SCONCAT_SVS(body, "--", boundary, "\r\n");
			PHALCON_SCONCAT_SVSVS(body, "Content-Disposition: form-data; name=\"", key, "\"\r\n\r\n", value, "\r\n");

			zend_hash_move_forward_ex(ah0, &hp0);
		}
	}

	if (PHALCON_IS_NOT_EMPTY(file)) {
		PHALCON_CALL_FUNCTION(&path_parts, "pathinfo", file);

		if (phalcon_array_isset_string_fetch(&filename, path_parts, SS("filename")) && phalcon_array_isset_string_fetch(&basename, path_parts, SS("basename"))) {
			PHALCON_CALL_FUNCTION(&filedata, "file_get_contents", file);

			PHALCON_SCONCAT_SVS(body, "--", boundary, "\r\n");
			PHALCON_SCONCAT_SVSVS(body, "Content-Disposition: form-data; name=\"", filename, "\"; filename=\"", basename, "\"\r\n");
			PHALCON_SCONCAT_SVS(body, "Content-Type: application/octet-stream\r\n\r\n", filedata, "\r\n");
		}
	}

	if (!PHALCON_IS_EMPTY(body)) {
		PHALCON_SCONCAT_SVS(body, "--", boundary, "--\r\n");

		PHALCON_INIT_NVAR(key);
		ZVAL_STRING(key, "Content-Type", 1);

		PHALCON_INIT_NVAR(value);
		PHALCON_CONCAT_SV(value, "multipart/form-data; boundary=", boundary);

		PHALCON_CALL_METHOD(NULL, header, "set", key, value);

		PHALCON_INIT_NVAR(key);
		ZVAL_STRING(key, "Content-Length", 1);		

		PHALCON_INIT_NVAR(value);
		ZVAL_LONG(value, Z_STRLEN_P(body));

		PHALCON_CALL_METHOD(NULL, header, "set", key, value);

		PHALCON_INIT_NVAR(option);
		ZVAL_LONG(option, PHALCON_HTTP_CLIENT_HEADER_BUILD_FIELDS);
		
		PHALCON_CALL_METHOD(&headers, header, "build", option);

		PHALCON_INIT_NVAR(option);
		ZVAL_STRING(option, "header", 1);

		PHALCON_CALL_FUNCTION(NULL, "stream_context_set_option", stream, http, option, headers);

		PHALCON_INIT_NVAR(option);
		ZVAL_STRING(option, "content", 1);
		
		PHALCON_CALL_FUNCTION(NULL, "stream_context_set_option", stream, http, option, body);
	}

	PHALCON_MM_RESTORE();
}
コード例 #27
0
ファイル: annotations.c プロジェクト: 9466/cphalcon
/**
 * Produce the routing parameters from the rewrite information
 *
 * @param string $uri
 */
PHP_METHOD(Phalcon_Mvc_Router_Annotations, handle){

	zval *uri = NULL, *real_uri = NULL, *processed, *annotations_service = NULL;
	zval *handlers, *controller_suffix, *scope = NULL, *prefix = NULL;
	zval *dependency_injector = NULL, *service = NULL, *handler = NULL;
	zval *controller_name = NULL;
	zval *namespace_name = NULL, *module_name = NULL, *suffixed = NULL;
	zval *handler_annotations = NULL, *class_annotations = NULL;
	zval *annotations = NULL, *annotation = NULL, *method_annotations = NULL;
	zval *collection = NULL, *method = NULL;
	HashTable *ah0, *ah1, *ah2, *ah3;
	HashPosition hp0, hp1, hp2, hp3;
	zval **hd;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 0, 1, &uri);
	
	if (!uri) {
		uri = PHALCON_GLOBAL(z_null);
	}
	
	if (!zend_is_true(uri)) {
		/** 
		 * If 'uri' isn't passed as parameter it reads $_GET['_url']
		 */
		PHALCON_CALL_METHOD(&real_uri, this_ptr, "getrewriteuri");
	} else {
		PHALCON_CPY_WRT(real_uri, uri);
	}
	
	PHALCON_OBS_VAR(processed);
	phalcon_read_property_this(&processed, this_ptr, SL("_processed"), PH_NOISY TSRMLS_CC);
	if (!zend_is_true(processed)) {
	
		PHALCON_INIT_VAR(annotations_service);
	
		PHALCON_OBS_VAR(handlers);
		phalcon_read_property_this(&handlers, this_ptr, SL("_handlers"), PH_NOISY TSRMLS_CC);
		if (Z_TYPE_P(handlers) == IS_ARRAY) { 
	
			PHALCON_OBS_VAR(controller_suffix);
			phalcon_read_property_this(&controller_suffix, this_ptr, SL("_controllerSuffix"), PH_NOISY TSRMLS_CC);
	
			phalcon_is_iterable(handlers, &ah0, &hp0, 0, 0);
	
			while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) {
	
				PHALCON_GET_HVALUE(scope);
	
				if (Z_TYPE_P(scope) == IS_ARRAY) { 
	
					/** 
					 * A prefix (if any) must be in position 0
					 */
					PHALCON_OBS_NVAR(prefix);
					phalcon_array_fetch_long(&prefix, scope, 0, PH_NOISY);
					if (Z_TYPE_P(prefix) == IS_STRING) {
						if (!phalcon_start_with(real_uri, prefix, NULL)) {
							zend_hash_move_forward_ex(ah0, &hp0);
							continue;
						}
					}
	
					if (Z_TYPE_P(annotations_service) != IS_OBJECT) {
	
						PHALCON_OBS_NVAR(dependency_injector);
						phalcon_read_property_this(&dependency_injector, this_ptr, SL("_dependencyInjector"), PH_NOISY TSRMLS_CC);
						if (Z_TYPE_P(dependency_injector) != IS_OBJECT) {
							PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_router_exception_ce, "A dependency injection container is required to access the 'annotations' service");
							return;
						}
	
						PHALCON_INIT_NVAR(service);
						ZVAL_STRING(service, "annotations", 1);
	
						PHALCON_CALL_METHOD(&annotations_service, dependency_injector, "getshared", service);
						PHALCON_VERIFY_INTERFACE(annotations_service, phalcon_annotations_adapterinterface_ce);
					}
	
					/** 
					 * The controller must be in position 1
					 */
					PHALCON_OBS_NVAR(handler);
					phalcon_array_fetch_long(&handler, scope, 1, PH_NOISY);
					if (phalcon_memnstr_str(handler, SL("\\"))) {
						/** 
						 * Extract the real class name from the namespaced class
						 */
						PHALCON_INIT_NVAR(controller_name);
						phalcon_get_class_ns(controller_name, handler, 0 TSRMLS_CC);
	
						/** 
						 * Extract the namespace from the namespaced class
						 */
						PHALCON_INIT_NVAR(namespace_name);
						phalcon_get_ns_class(namespace_name, handler, 0 TSRMLS_CC);
					} else {
						PHALCON_CPY_WRT(controller_name, handler);
	
						PHALCON_INIT_NVAR(namespace_name);
					}
	
					phalcon_update_property_null(this_ptr, SL("_routePrefix") TSRMLS_CC);
	
					/** 
					 * Check if the scope has a module associated
					 */
					if (phalcon_array_isset_long(scope, 2)) {
						PHALCON_OBS_NVAR(module_name);
						phalcon_array_fetch_long(&module_name, scope, 2, PH_NOISY);
					} else {
						PHALCON_INIT_NVAR(module_name);
					}
	
					PHALCON_INIT_NVAR(suffixed);
					PHALCON_CONCAT_VV(suffixed, handler, controller_suffix);
	
					/** 
					 * Get the annotations from the class
					 */
					PHALCON_CALL_METHOD(&handler_annotations, annotations_service, "get", suffixed);
	
					/** 
					 * Process class annotations
					 */
					PHALCON_CALL_METHOD(&class_annotations, handler_annotations, "getclassannotations");
					if (Z_TYPE_P(class_annotations) == IS_OBJECT) {
	
						/** 
						 * Process class annotations
						 */
						PHALCON_CALL_METHOD(&annotations, class_annotations, "getannotations");
						if (Z_TYPE_P(annotations) == IS_ARRAY) { 
	
							phalcon_is_iterable(annotations, &ah1, &hp1, 0, 0);
	
							while (zend_hash_get_current_data_ex(ah1, (void**) &hd, &hp1) == SUCCESS) {
	
								PHALCON_GET_HVALUE(annotation);
	
								PHALCON_CALL_METHOD(NULL, this_ptr, "processcontrollerannotation", controller_name, annotation);
	
								zend_hash_move_forward_ex(ah1, &hp1);
							}
	
						}
					}
	
					/** 
					 * Process method annotations
					 */
					PHALCON_CALL_METHOD(&method_annotations, handler_annotations, "getmethodsannotations");
					if (Z_TYPE_P(method_annotations) == IS_ARRAY) { 
	
						phalcon_is_iterable(method_annotations, &ah2, &hp2, 0, 0);
	
						while (zend_hash_get_current_data_ex(ah2, (void**) &hd, &hp2) == SUCCESS) {
	
							PHALCON_GET_HKEY(method, ah2, hp2);
							PHALCON_GET_HVALUE(collection);
	
							if (Z_TYPE_P(collection) == IS_OBJECT) {
								PHALCON_CALL_METHOD(&annotations, collection, "getannotations");
	
								phalcon_is_iterable(annotations, &ah3, &hp3, 0, 0);
	
								while (zend_hash_get_current_data_ex(ah3, (void**) &hd, &hp3) == SUCCESS) {
	
									PHALCON_GET_HVALUE(annotation);
	
									PHALCON_CALL_METHOD(NULL, this_ptr, "processactionannotation", module_name, namespace_name, controller_name, method, annotation);
	
									zend_hash_move_forward_ex(ah3, &hp3);
								}
	
							}
	
							zend_hash_move_forward_ex(ah2, &hp2);
						}
	
					}
				}
	
				zend_hash_move_forward_ex(ah0, &hp0);
			}
	
		}
	
		phalcon_update_property_bool(this_ptr, SL("_processed"), 1 TSRMLS_CC);
	}
	
	/** 
	 * Call the parent handle method()
	 */
	PHALCON_CALL_PARENT(NULL, phalcon_mvc_router_annotations_ce, this_ptr, "handle", real_uri);
	
	PHALCON_MM_RESTORE();
}
コード例 #28
0
ファイル: mysql.c プロジェクト: Tigerlee1987/cphalcon
/**
 * Returns an array of Phalcon\Db\Column objects describing a table
 *
 * <code>
 * print_r($connection->describeColumns("posts")); ?>
 * </code>
 *
 * @param string $table
 * @param string $schema
 * @return Phalcon\Db\Column[]
 */
PHP_METHOD(Phalcon_Db_Adapter_Pdo_Mysql, describeColumns){

	zval *table, *schema = NULL, *columns, *dialect, *sql, *fetch_assoc;
	zval *describe, *old_column = NULL, *size_pattern, *field = NULL;
	zval *definition = NULL, *column_type = NULL, *matches = NULL, *pos = NULL;
	zval *match_one = NULL, *attribute = NULL, *column_name = NULL, *column = NULL;
	HashTable *ah0;
	HashPosition hp0;
	zval **hd;
	int eval_int;

	PHALCON_MM_GROW();

	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|z", &table, &schema) == FAILURE) {
		PHALCON_MM_RESTORE();
		RETURN_NULL();
	}

	if (!schema) {
		PHALCON_INIT_NVAR(schema);
	}
	
	PHALCON_INIT_VAR(columns);
	array_init(columns);
	
	PHALCON_INIT_VAR(dialect);
	phalcon_read_property(&dialect, this_ptr, SL("_dialect"), PH_NOISY_CC);
	
	PHALCON_INIT_VAR(sql);
	PHALCON_CALL_METHOD_PARAMS_2(sql, dialect, "describecolumns", table, schema, PH_NO_CHECK);
	
	PHALCON_INIT_VAR(fetch_assoc);
	phalcon_get_class_constant(fetch_assoc, phalcon_db_ce, SS("FETCH_ASSOC") TSRMLS_CC);
	
	PHALCON_INIT_VAR(describe);
	PHALCON_CALL_METHOD_PARAMS_2(describe, this_ptr, "fetchall", sql, fetch_assoc, PH_NO_CHECK);
	
	PHALCON_INIT_VAR(old_column);
	
	PHALCON_INIT_VAR(size_pattern);
	ZVAL_STRING(size_pattern, "#\\(([0-9]+)(,[0-9]+)*\\)#", 1);
	
	if (!phalcon_valid_foreach(describe TSRMLS_CC)) {
		return;
	}
	
	ah0 = Z_ARRVAL_P(describe);
	zend_hash_internal_pointer_reset_ex(ah0, &hp0);
	
	ph_cycle_start_0:
	
		if (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) != SUCCESS) {
			goto ph_cycle_end_0;
		}
	
		PHALCON_GET_FOREACH_VALUE(field);
	
		PHALCON_INIT_NVAR(definition);
		array_init(definition);
		add_assoc_long_ex(definition, SS("bindType"), 2);
	
		/** 
		 * By checking every column type we convert it to a Phalcon\Db\Column
		 */
		PHALCON_INIT_NVAR(column_type);
		phalcon_array_fetch_string(&column_type, field, SL("type"), PH_NOISY_CC);
		if (phalcon_memnstr_str(column_type, SL("int") TSRMLS_CC)) {
			phalcon_array_update_string_long(&definition, SL("type"), 0, PH_SEPARATE TSRMLS_CC);
			phalcon_array_update_string_bool(&definition, SL("isNumeric"), 1, PH_SEPARATE TSRMLS_CC);
			phalcon_array_update_string_long(&definition, SL("bindType"), 1, PH_SEPARATE TSRMLS_CC);
		} else {
			if (phalcon_memnstr_str(column_type, SL("varchar") TSRMLS_CC)) {
				phalcon_array_update_string_long(&definition, SL("type"), 2, PH_SEPARATE TSRMLS_CC);
			} else {
				if (phalcon_memnstr_str(column_type, SL("date") TSRMLS_CC)) {
					phalcon_array_update_string_long(&definition, SL("type"), 1, PH_SEPARATE TSRMLS_CC);
				} else {
					if (phalcon_memnstr_str(column_type, SL("decimal") TSRMLS_CC)) {
						phalcon_array_update_string_long(&definition, SL("type"), 3, PH_SEPARATE TSRMLS_CC);
						phalcon_array_update_string_bool(&definition, SL("isNumeric"), 1, PH_SEPARATE TSRMLS_CC);
						phalcon_array_update_string_long(&definition, SL("bindType"), 32, PH_SEPARATE TSRMLS_CC);
					} else {
						if (phalcon_memnstr_str(column_type, SL("char") TSRMLS_CC)) {
							phalcon_array_update_string_long(&definition, SL("type"), 5, PH_SEPARATE TSRMLS_CC);
						} else {
							if (phalcon_memnstr_str(column_type, SL("datetime") TSRMLS_CC)) {
								phalcon_array_update_string_long(&definition, SL("type"), 4, PH_SEPARATE TSRMLS_CC);
							} else {
								if (phalcon_memnstr_str(column_type, SL("text") TSRMLS_CC)) {
									phalcon_array_update_string_long(&definition, SL("type"), 6, PH_SEPARATE TSRMLS_CC);
								} else {
									if (phalcon_memnstr_str(column_type, SL("float") TSRMLS_CC)) {
										phalcon_array_update_string_long(&definition, SL("type"), 7, PH_SEPARATE TSRMLS_CC);
										phalcon_array_update_string_bool(&definition, SL("isNumeric"), 1, PH_SEPARATE TSRMLS_CC);
										phalcon_array_update_string_long(&definition, SL("bindType"), 32, PH_SEPARATE TSRMLS_CC);
									} else {
										if (phalcon_memnstr_str(column_type, SL("enum") TSRMLS_CC)) {
											phalcon_array_update_string_long(&definition, SL("type"), 5, PH_SEPARATE TSRMLS_CC);
										} else {
											phalcon_array_update_string_long(&definition, SL("type"), 2, PH_SEPARATE TSRMLS_CC);
										}
									}
								}
							}
						}
					}
				}
			}
		}
	
		/** 
		 * If the column type has a parentheses we try to get the column size from it
		 */
		if (phalcon_memnstr_str(column_type, SL("(") TSRMLS_CC)) {
			PHALCON_INIT_NVAR(matches);
			array_init(matches);
			Z_SET_ISREF_P(matches);
	
			PHALCON_INIT_NVAR(pos);
			PHALCON_CALL_FUNC_PARAMS_3(pos, "preg_match", size_pattern, column_type, matches);
			Z_UNSET_ISREF_P(matches);
			if (zend_is_true(pos)) {
				eval_int = phalcon_array_isset_long(matches, 1);
				if (eval_int) {
					PHALCON_INIT_NVAR(match_one);
					phalcon_array_fetch_long(&match_one, matches, 1, PH_NOISY_CC);
					phalcon_array_update_string(&definition, SL("size"), &match_one, PH_COPY | PH_SEPARATE TSRMLS_CC);
				}
			}
		}
	
		/** 
		 * Check if the column is unsigned, only MySQL support this
		 */
		if (phalcon_memnstr_str(column_type, SL("unsigned") TSRMLS_CC)) {
			phalcon_array_update_string_bool(&definition, SL("unsigned"), 1, PH_SEPARATE TSRMLS_CC);
		}
	
		if (!zend_is_true(old_column)) {
			phalcon_array_update_string_bool(&definition, SL("first"), 1, PH_SEPARATE TSRMLS_CC);
		} else {
			phalcon_array_update_string(&definition, SL("after"), &old_column, PH_COPY | PH_SEPARATE TSRMLS_CC);
		}
	
		/** 
		 * Check if the field is primary key
		 */
		PHALCON_INIT_NVAR(attribute);
		phalcon_array_fetch_string(&attribute, field, SL("key"), PH_NOISY_CC);
		if (PHALCON_COMPARE_STRING(attribute, "PRI")) {
			phalcon_array_update_string_bool(&definition, SL("primary"), 1, PH_SEPARATE TSRMLS_CC);
		}
	
		/** 
		 * Check if the column allows null values
		 */
		PHALCON_INIT_NVAR(attribute);
		phalcon_array_fetch_string(&attribute, field, SL("null"), PH_NOISY_CC);
		if (PHALCON_COMPARE_STRING(attribute, "NO")) {
			phalcon_array_update_string_bool(&definition, SL("notNull"), 1, PH_SEPARATE TSRMLS_CC);
		}
	
		/** 
		 * Check if the column is auto increment
		 */
		PHALCON_INIT_NVAR(attribute);
		phalcon_array_fetch_string(&attribute, field, SL("extra"), PH_NOISY_CC);
		if (PHALCON_COMPARE_STRING(attribute, "auto_increment")) {
			phalcon_array_update_string_bool(&definition, SL("autoIncrement"), 1, PH_SEPARATE TSRMLS_CC);
		}
	
		PHALCON_INIT_NVAR(column_name);
		phalcon_array_fetch_string(&column_name, field, SL("field"), PH_NOISY_CC);
	
		PHALCON_INIT_NVAR(column);
		object_init_ex(column, phalcon_db_column_ce);
		PHALCON_CALL_METHOD_PARAMS_2_NORETURN(column, "__construct", column_name, definition, PH_CHECK);
		phalcon_array_append(&columns, column, PH_SEPARATE TSRMLS_CC);
		PHALCON_CPY_WRT(old_column, column_name);
	
		zend_hash_move_forward_ex(ah0, &hp0);
		goto ph_cycle_start_0;
	
	ph_cycle_end_0:
	
	
	RETURN_CTOR(columns);
}
コード例 #29
0
ファイル: filter.c プロジェクト: dreamsxin/cphalcon7
/**
 * Prevernt cross-site scripting (XSS) attacks
 */
void phalcon_xss_clean(zval *return_value, zval *str, zval *allow_tags, zval *allow_attributes)
{
	zval document = {}, ret = {}, tmp = {}, elements = {}, matched = {}, regexp = {}, joined_tags = {}, clean_str = {};
	zend_class_entry *ce0;
	int i, element_length;

	ce0 = phalcon_fetch_str_class(SL("DOMDocument"), ZEND_FETCH_CLASS_AUTO);

	object_init_ex(&document, ce0);
	PHALCON_CALL_METHODW(NULL, &document, "__construct");

	phalcon_update_property_bool(&document, SL("strictErrorChecking"), 0);

	if (phalcon_function_exists_ex(SL("libxml_use_internal_errors")) == SUCCESS) {
		PHALCON_CALL_FUNCTIONW(NULL, "libxml_use_internal_errors", &PHALCON_GLOBAL(z_true));
	}

	PHALCON_CALL_METHODW(&ret, &document, "loadhtml", str);

	if (phalcon_function_exists_ex(SL("libxml_clear_errors")) == SUCCESS) {
		PHALCON_CALL_FUNCTIONW(NULL, "libxml_clear_errors");
	}

	if (!zend_is_true(&ret)) {
		return;
	}

	PHALCON_STR(&tmp, "*");

	PHALCON_CALL_METHODW(&elements, &document, "getelementsbytagname", &tmp);

	PHALCON_STR(&regexp, "/e.*x.*p.*r.*e.*s.*s.*i.*o.*n/i");

	phalcon_return_property(&tmp, &elements, SL("length"));

	element_length = Z_LVAL_P(&tmp);

	for (i = 0; i < element_length; i++) {
		zval t = {}, element = {}, element_name = {}, element_attrs = {};
		int element_attrs_length, j;

		ZVAL_LONG(&t, i);
	
		PHALCON_CALL_METHODW(&element, &elements, "item", &t);

		phalcon_return_property(&element_name, &element, SL("nodeName"));

		if (Z_TYPE_P(allow_tags) == IS_ARRAY && !phalcon_fast_in_array(&element_name, allow_tags)) {
			continue;
		}

		phalcon_return_property(&element_attrs, &element, SL("attributes"));
		phalcon_return_property(&tmp, &element_attrs, SL("length"));

		element_attrs_length = Z_LVAL_P(&tmp);

		for (j = 0; j < element_attrs_length; j++) {
			zval t2 = {}, element_attr = {}, element_attr_name = {}, element_attr_value = {};
			ZVAL_LONG(&t2, j);

			PHALCON_CALL_METHODW(&element_attr, &element_attrs, "item", &t2);

			phalcon_return_property(&element_attr_name, &element_attr, SL("nodeName"));
			if (Z_TYPE_P(allow_attributes) == IS_ARRAY && !phalcon_fast_in_array(&element_attr_name, allow_attributes)) {
				PHALCON_CALL_METHODW(NULL, &element, "removeattributenode", &element_attr);
			} else if (phalcon_memnstr_str(&element_attr_name, SL("style"))) {
				phalcon_return_property(&element_attr_value, &element_attr, SL("nodeValue"));

				RETURN_ON_FAILURE(phalcon_preg_match(&matched, &regexp, &element_attr_value, NULL));

				if (zend_is_true(&matched)) {
					PHALCON_CALL_METHODW(NULL, &element, "removeattributenode", &element_attr);
				}
			}
		}
	}

	phalcon_fast_join_str(&tmp, SL("><"), allow_tags);

	PHALCON_CONCAT_SVS(&joined_tags, "<", &tmp, ">");

	PHALCON_CALL_METHODW(&ret, &document, "savehtml");
	PHALCON_CALL_FUNCTIONW(&clean_str, "strip_tags", &ret, &joined_tags);

	ZVAL_STR(return_value, phalcon_trim(&clean_str, NULL, PHALCON_TRIM_BOTH));
}
コード例 #30
0
ファイル: ini.c プロジェクト: RSivakov/cphalcon
/**
 * Phalcon\Config\Adapter\Ini constructor
 *
 * @param string $filePath
 */
PHP_METHOD(Phalcon_Config_Adapter_Ini, __construct){

	zval *file_path, *process_sections, *ini_config;
	zval *exception_message, *config, *directives = NULL;
	zval *section = NULL, *value = NULL, *key = NULL, *directive_parts = NULL, *left_part = NULL;
	zval *right_part = NULL;
	HashTable *ah0, *ah1;
	HashPosition hp0, hp1;
	zval **hd;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 1, 0, &file_path);
	
	PHALCON_INIT_VAR(process_sections);
	ZVAL_BOOL(process_sections, 1);
	
	/** 
	 * Use the standard parse_ini_file
	 */
	PHALCON_INIT_VAR(ini_config);
	phalcon_call_func_p2(ini_config, "parse_ini_file", file_path, process_sections);
	
	/** 
	 * Check if the file had errors
	 */
	if (PHALCON_IS_FALSE(ini_config)) {
		PHALCON_INIT_VAR(exception_message);
		PHALCON_CONCAT_SVS(exception_message, "Configuration file ", file_path, " can't be loaded");
		PHALCON_THROW_EXCEPTION_ZVAL(phalcon_config_exception_ce, exception_message);
		return;
	}
	
	PHALCON_INIT_VAR(config);
	array_init(config);
	
	phalcon_is_iterable(ini_config, &ah0, &hp0, 0, 0);
	
	while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) {
	
		PHALCON_GET_HKEY(section, ah0, hp0);
		PHALCON_GET_HVALUE(directives);
	
		if (unlikely(Z_TYPE_P(directives) != IS_ARRAY)) {
			Z_ADDREF_P(directives);
			if (phalcon_array_update_zval(&config, section, &directives, 0 TSRMLS_CC) != SUCCESS) {
				Z_DELREF_P(directives);
			}
			zend_hash_move_forward_ex(ah0, &hp0);
			continue;
	}
	
		phalcon_is_iterable(directives, &ah1, &hp1, 0, 0);
	
		if (zend_hash_num_elements(ah1) == 0) {
			phalcon_array_update_zval(&config, section, &directives, 0 TSRMLS_CC);
			zend_hash_move_forward_ex(ah0, &hp0);
			continue;
		}
	
		while (zend_hash_get_current_data_ex(ah1, (void**) &hd, &hp1) == SUCCESS) {
	
			PHALCON_GET_HKEY(key, ah1, hp1);
			PHALCON_GET_HVALUE(value);
	
			if (phalcon_memnstr_str(key, SL(".") TSRMLS_CC)) {
				PHALCON_INIT_NVAR(directive_parts);
				phalcon_fast_explode_str(directive_parts, SL("."), key TSRMLS_CC);
	
				PHALCON_OBS_NVAR(left_part);
				phalcon_array_fetch_long(&left_part, directive_parts, 0, PH_NOISY_CC);
	
				PHALCON_OBS_NVAR(right_part);
				phalcon_array_fetch_long(&right_part, directive_parts, 1, PH_NOISY_CC);
				phalcon_array_update_zval_zval_zval_multi_3(&config, section, left_part, right_part, &value, 0 TSRMLS_CC);
			} else {
				phalcon_array_update_multi_2(&config, section, key, &value, 0 TSRMLS_CC);
			}
	
			zend_hash_move_forward_ex(ah1, &hp1);
		}
	
		zend_hash_move_forward_ex(ah0, &hp0);
	}
	
	/** 
	 * Calls the Phalcon\Config constructor
	 */
	PHALCON_CALL_PARENT_PARAMS_1_NORETURN(this_ptr, "Phalcon\\Config\\Adapter\\Ini", "__construct", config);
	
	PHALCON_MM_RESTORE();
}