diff options
author | Marc Boeren <mboeren@php.net> | 2002-10-29 14:08:40 +0000 |
---|---|---|
committer | Marc Boeren <mboeren@php.net> | 2002-10-29 14:08:40 +0000 |
commit | 25e7a092293b4e54e25592d34834bb3d55ae0fdc (patch) | |
tree | e0ceed1f54846d8ff4e5ce455fa65c1c682373c9 /ext/dbx | |
parent | 82317945e5ff22cf6addb9251f656fe5bcc15b1d (diff) | |
download | php-git-25e7a092293b4e54e25592d34834bb3d55ae0fdc.tar.gz |
Added dbx_escape_string function
# tested on odbc, oci8 and mysql
@Added dbx_escape_string function to dbx module. (Marc)
Diffstat (limited to 'ext/dbx')
-rw-r--r-- | ext/dbx/dbx.c | 54 | ||||
-rw-r--r-- | ext/dbx/dbx.h | 1 | ||||
-rw-r--r-- | ext/dbx/dbx_fbsql.c | 22 | ||||
-rw-r--r-- | ext/dbx/dbx_fbsql.h | 2 | ||||
-rw-r--r-- | ext/dbx/dbx_mssql.c | 21 | ||||
-rw-r--r-- | ext/dbx/dbx_mssql.h | 2 | ||||
-rw-r--r-- | ext/dbx/dbx_mysql.c | 37 | ||||
-rw-r--r-- | ext/dbx/dbx_mysql.h | 2 | ||||
-rw-r--r-- | ext/dbx/dbx_oci8.c | 21 | ||||
-rw-r--r-- | ext/dbx/dbx_oci8.h | 2 | ||||
-rw-r--r-- | ext/dbx/dbx_odbc.c | 21 | ||||
-rw-r--r-- | ext/dbx/dbx_odbc.h | 2 | ||||
-rw-r--r-- | ext/dbx/dbx_pgsql.c | 24 | ||||
-rw-r--r-- | ext/dbx/dbx_pgsql.h | 2 | ||||
-rw-r--r-- | ext/dbx/dbx_sybasect.c | 21 | ||||
-rw-r--r-- | ext/dbx/dbx_sybasect.h | 2 | ||||
-rw-r--r-- | ext/dbx/php_dbx.h | 1 |
17 files changed, 236 insertions, 1 deletions
diff --git a/ext/dbx/dbx.c b/ext/dbx/dbx.c index 1d04d91b1b..77403fb0a3 100644 --- a/ext/dbx/dbx.c +++ b/ext/dbx/dbx.c @@ -30,7 +30,6 @@ #include "php_ini.h" #include "php_dbx.h" #include "ext/standard/info.h" -#include "ext/standard/php_string.h" /* defines for supported databases */ #define DBX_UNKNOWN 0 @@ -137,6 +136,8 @@ int switch_dbx_getrow(zval **rv, zval **result_handle, long row_number, INTERNAL /* returns array[0..columncount-1] as strings on success or 0 as long on failure */ int switch_dbx_error(zval **rv, zval **dbx_handle, INTERNAL_FUNCTION_PARAMETERS, zval **dbx_module); /* returns string */ +int switch_dbx_esc(zval **rv, zval **dbx_handle, zval **string, INTERNAL_FUNCTION_PARAMETERS, zval **dbx_module); + /* returns escaped string */ /* Every user visible function must have an entry in dbx_functions[]. */ @@ -145,6 +146,7 @@ function_entry dbx_functions[] = { ZEND_FE(dbx_close, NULL) ZEND_FE(dbx_query, NULL) ZEND_FE(dbx_error, NULL) + ZEND_FE(dbx_escape_string, NULL) ZEND_FE(dbx_sort, NULL) ZEND_FE(dbx_compare, NULL) @@ -574,6 +576,40 @@ ZEND_FUNCTION(dbx_error) } /* }}} */ +/* {{{ proto string dbx_esc(dbx_link_object dbx_link, string sz) + Returns escaped string or NULL on error +*/ +ZEND_FUNCTION(dbx_escape_string) +{ + int number_of_arguments=2; + zval **arguments[2]; + + int result; + zval **dbx_handle; + zval **dbx_module; + zval **dbx_database; + zval *rv; + + if (ZEND_NUM_ARGS() !=number_of_arguments || zend_get_parameters_array_ex(number_of_arguments, arguments) == FAILURE) { + WRONG_PARAM_COUNT; + } + if (!split_dbx_handle_object(arguments[0], &dbx_handle, &dbx_module, &dbx_database)) { + zend_error(E_WARNING, "dbx_esc: not a valid dbx_handle-object..."); + RETURN_NULL(); + } + convert_to_string_ex(arguments[1]); + + MAKE_STD_ZVAL(rv); + ZVAL_LONG(rv, 0); + result = switch_dbx_esc(&rv, dbx_handle, arguments[1], INTERNAL_FUNCTION_PARAM_PASSTHRU, dbx_module); + if (!result) { /* this will probably never happen */ + FREE_ZVAL(rv); + RETURN_NULL(); + } + MOVE_RETURNED_TO_RV(&return_value, rv); +} +/* }}} */ + /* * dbx functions that are database independent... like sorting result_objects! */ @@ -850,6 +886,22 @@ int switch_dbx_error(zval **rv, zval **dbx_handle, INTERNAL_FUNCTION_PARAMETERS, return 0; } +int switch_dbx_esc(zval **rv, zval **dbx_handle, zval **string, INTERNAL_FUNCTION_PARAMETERS, zval **dbx_module) +{ + /* returns escaped string */ + switch (Z_LVAL_PP(dbx_module)) { + case DBX_MYSQL: return dbx_mysql_esc(rv, dbx_handle, string, INTERNAL_FUNCTION_PARAM_PASSTHRU); + case DBX_ODBC: return dbx_odbc_esc(rv, dbx_handle, string, INTERNAL_FUNCTION_PARAM_PASSTHRU); + case DBX_PGSQL: return dbx_pgsql_esc(rv, dbx_handle, string, INTERNAL_FUNCTION_PARAM_PASSTHRU); + case DBX_MSSQL: return dbx_mssql_esc(rv, dbx_handle, string, INTERNAL_FUNCTION_PARAM_PASSTHRU); + case DBX_FBSQL: return dbx_fbsql_esc(rv, dbx_handle, string, INTERNAL_FUNCTION_PARAM_PASSTHRU); + case DBX_OCI8: return dbx_oci8_esc(rv, dbx_handle, string, INTERNAL_FUNCTION_PARAM_PASSTHRU); + case DBX_SYBASECT: return dbx_sybasect_esc(rv, dbx_handle, string, INTERNAL_FUNCTION_PARAM_PASSTHRU); + } + zend_error(E_WARNING, "dbx_esc: not supported in this module"); + return 0; +} + /* * Local variables: * tab-width: 4 diff --git a/ext/dbx/dbx.h b/ext/dbx/dbx.h index 338061855a..0b428856aa 100644 --- a/ext/dbx/dbx.h +++ b/ext/dbx/dbx.h @@ -30,6 +30,7 @@ #endif #include "php.h" +#include "ext/standard/php_string.h" #define DBX_PERSISTENT (1<<0) diff --git a/ext/dbx/dbx_fbsql.c b/ext/dbx/dbx_fbsql.c index 3ab8bbd232..56c3764f0f 100644 --- a/ext/dbx/dbx_fbsql.c +++ b/ext/dbx/dbx_fbsql.c @@ -249,6 +249,28 @@ int dbx_fbsql_error(zval **rv, zval **dbx_handle, INTERNAL_FUNCTION_PARAMETERS) return 1; } +int dbx_fbsql_esc(zval **rv, zval **dbx_handle, zval **string, INTERNAL_FUNCTION_PARAMETERS) +{ + /* returns escaped string */ + /* replace \ with \\ */ + /* ' with '' */ + char * str; + int len; + char * tmpstr; + int tmplen; + + tmpstr = estrdup(Z_STRVAL_PP(string)); + tmplen = Z_STRLEN_PP(string); + /* php_str_to_str uses a smart_str that allocates memory */ + /* this memory must be freed or passed on to rv */ + str = php_str_to_str(tmpstr, tmplen, "'", 1, "''", 2, &len); + efree(tmpstr); + + ZVAL_STRINGL(*rv, str, len, 0); + + return 1; +} + /* * Local variables: * tab-width: 4 diff --git a/ext/dbx/dbx_fbsql.h b/ext/dbx/dbx_fbsql.h index e36504b0cb..4304c5ae10 100644 --- a/ext/dbx/dbx_fbsql.h +++ b/ext/dbx/dbx_fbsql.h @@ -50,6 +50,8 @@ int dbx_fbsql_getrow(zval **rv, zval **result_handle, long row_number, INTERNAL_ /* returns array[0..columncount-1] as strings on success or 0 as long on failure */ int dbx_fbsql_error(zval **rv, zval **dbx_handle, INTERNAL_FUNCTION_PARAMETERS); /* returns string */ +int dbx_fbsql_esc(zval **rv, zval **dbx_handle, zval **string, INTERNAL_FUNCTION_PARAMETERS); + /* returns escaped string */ #endif /* ZEND_DBX_FBSQL_H */ diff --git a/ext/dbx/dbx_mssql.c b/ext/dbx/dbx_mssql.c index f905ed10be..9ea38ca1eb 100644 --- a/ext/dbx/dbx_mssql.c +++ b/ext/dbx/dbx_mssql.c @@ -249,6 +249,27 @@ int dbx_mssql_error(zval **rv, zval **dbx_handle, INTERNAL_FUNCTION_PARAMETERS) return 1; } +int dbx_mssql_esc(zval **rv, zval **dbx_handle, zval **string, INTERNAL_FUNCTION_PARAMETERS) +{ + /* returns escaped string */ + /* replace ' with '' */ + char * str; + int len; + char * tmpstr; + int tmplen; + + tmpstr = estrdup(Z_STRVAL_PP(string)); + tmplen = Z_STRLEN_PP(string); + /* php_str_to_str uses a smart_str that allocates memory */ + /* this memory must be freed or passed on to rv */ + str = php_str_to_str(tmpstr, tmplen, "'", 1, "''", 2, &len); + efree(tmpstr); + + ZVAL_STRINGL(*rv, str, len, 0); + + return 1; +} + /* * Local variables: * tab-width: 4 diff --git a/ext/dbx/dbx_mssql.h b/ext/dbx/dbx_mssql.h index 2051da357f..8fd7c7ef3a 100644 --- a/ext/dbx/dbx_mssql.h +++ b/ext/dbx/dbx_mssql.h @@ -49,6 +49,8 @@ int dbx_mssql_getrow(zval **rv, zval **result_handle, long row_number, INTERNAL_ /* returns array[0..columncount-1] as strings on success or 0 as long on failure */ int dbx_mssql_error(zval **rv, zval **dbx_handle, INTERNAL_FUNCTION_PARAMETERS); /* returns string */ +int dbx_mssql_esc(zval **rv, zval **dbx_handle, zval **string, INTERNAL_FUNCTION_PARAMETERS); + /* returns escaped string */ #endif /* ZEND_DBX_MSSQL_H */ diff --git a/ext/dbx/dbx_mysql.c b/ext/dbx/dbx_mysql.c index b9873147b6..3efd427038 100644 --- a/ext/dbx/dbx_mysql.c +++ b/ext/dbx/dbx_mysql.c @@ -255,6 +255,43 @@ int dbx_mysql_error(zval **rv, zval **dbx_handle, INTERNAL_FUNCTION_PARAMETERS) return 1; } +int dbx_mysql_esc(zval **rv, zval **dbx_handle, zval **string, INTERNAL_FUNCTION_PARAMETERS) +{ + /* returns escaped string */ + int number_of_arguments=2; + zval **arguments[2]; + zval *returned_zval=NULL; + char * str; + int len; + char * tmpstr; + int tmplen; + + arguments[0]=string; + arguments[1]=dbx_handle; + dbx_call_any_function(INTERNAL_FUNCTION_PARAM_PASSTHRU, "mysql_real_escape_string", &returned_zval, number_of_arguments, arguments); + if (!returned_zval || Z_TYPE_P(returned_zval)!=IS_STRING) { + if (returned_zval) zval_ptr_dtor(&returned_zval); + /* mysql_real_escape_string failed, just do my own escaping then */ + /* replace \ with \\ */ + /* ' with '' */ + + tmpstr = estrdup(Z_STRVAL_PP(string)); + tmplen = Z_STRLEN_PP(string); + /* php_str_to_str uses a smart_str that allocates memory */ + /* this memory must be freed or passed on to rv */ + str = php_str_to_str(tmpstr, tmplen, "\\", 1, "\\\\", 2, &len); + efree(tmpstr); + tmpstr=str; tmplen=len; + str = php_str_to_str(tmpstr, tmplen, "'", 1, "''", 2, &len); + efree(tmpstr); + + ZVAL_STRINGL(*rv, str, len, 0); + return 1; + } + MOVE_RETURNED_TO_RV(rv, returned_zval); + return 1; +} + /* * Local variables: * tab-width: 4 diff --git a/ext/dbx/dbx_mysql.h b/ext/dbx/dbx_mysql.h index f9b14a1170..d6bb78ed45 100644 --- a/ext/dbx/dbx_mysql.h +++ b/ext/dbx/dbx_mysql.h @@ -49,6 +49,8 @@ int dbx_mysql_getrow(zval **rv, zval **result_handle, long row_number, INTERNAL_ /* returns array[0..columncount-1] as strings on success or 0 as long on failure */ int dbx_mysql_error(zval **rv, zval **dbx_handle, INTERNAL_FUNCTION_PARAMETERS); /* returns string */ +int dbx_mysql_esc(zval **rv, zval **dbx_handle, zval **string, INTERNAL_FUNCTION_PARAMETERS); + /* returns escaped string */ #endif /* ZEND_DBX_MYSQL_H */ diff --git a/ext/dbx/dbx_oci8.c b/ext/dbx/dbx_oci8.c index efdfbea472..da50844334 100644 --- a/ext/dbx/dbx_oci8.c +++ b/ext/dbx/dbx_oci8.c @@ -267,6 +267,27 @@ int dbx_oci8_error(zval **rv, zval **dbx_handle, INTERNAL_FUNCTION_PARAMETERS) return 1; } +int dbx_oci8_esc(zval **rv, zval **dbx_handle, zval **string, INTERNAL_FUNCTION_PARAMETERS) +{ + /* returns escaped string */ + /* replace ' with '' */ + char * str; + int len; + char * tmpstr; + int tmplen; + + tmpstr = estrdup(Z_STRVAL_PP(string)); + tmplen = Z_STRLEN_PP(string); + /* php_str_to_str uses a smart_str that allocates memory */ + /* this memory must be freed or passed on to rv */ + str = php_str_to_str(tmpstr, tmplen, "'", 1, "''", 2, &len); + efree(tmpstr); + + ZVAL_STRINGL(*rv, str, len, 0); + + return 1; +} + /* * Local variables: * tab-width: 4 diff --git a/ext/dbx/dbx_oci8.h b/ext/dbx/dbx_oci8.h index be483df6c4..f5bd0f0580 100644 --- a/ext/dbx/dbx_oci8.h +++ b/ext/dbx/dbx_oci8.h @@ -49,6 +49,8 @@ int dbx_oci8_getrow(zval **rv, zval **result_handle, long row_number, INTERNAL_F /* returns array[0..columncount-1] as strings on success or 0 as long on failure */ int dbx_oci8_error(zval **rv, zval **dbx_handle, INTERNAL_FUNCTION_PARAMETERS); /* returns string */ +int dbx_oci8_esc(zval **rv, zval **dbx_handle, zval **string, INTERNAL_FUNCTION_PARAMETERS); + /* returns escaped string */ #endif /* ZEND_DBX_OCI8_H */ diff --git a/ext/dbx/dbx_odbc.c b/ext/dbx/dbx_odbc.c index 01a0a82aec..139855026b 100644 --- a/ext/dbx/dbx_odbc.c +++ b/ext/dbx/dbx_odbc.c @@ -272,6 +272,27 @@ int dbx_odbc_error(zval **rv, zval **dbx_handle, INTERNAL_FUNCTION_PARAMETERS) return 1; } +int dbx_odbc_esc(zval **rv, zval **dbx_handle, zval **string, INTERNAL_FUNCTION_PARAMETERS) +{ + /* returns escaped string */ + /* replace ' with '' */ + char * str; + int len; + char * tmpstr; + int tmplen; + + tmpstr = estrdup(Z_STRVAL_PP(string)); + tmplen = Z_STRLEN_PP(string); + /* php_str_to_str uses a smart_str that allocates memory */ + /* this memory must be freed or passed on to rv */ + str = php_str_to_str(tmpstr, tmplen, "'", 1, "''", 2, &len); + efree(tmpstr); + + ZVAL_STRINGL(*rv, str, len, 0); + + return 1; +} + /* * Local variables: * tab-width: 4 diff --git a/ext/dbx/dbx_odbc.h b/ext/dbx/dbx_odbc.h index 4cb8de2580..2f97c0c377 100644 --- a/ext/dbx/dbx_odbc.h +++ b/ext/dbx/dbx_odbc.h @@ -49,6 +49,8 @@ int dbx_odbc_getrow(zval **rv, zval **result_handle, long row_number, INTERNAL_F /* returns array[0..columncount-1] as strings on success or 0 as long on failure */ int dbx_odbc_error(zval **rv, zval **dbx_handle, INTERNAL_FUNCTION_PARAMETERS); /* returns string */ +int dbx_odbc_esc(zval **rv, zval **dbx_handle, zval **string, INTERNAL_FUNCTION_PARAMETERS); + /* returns escaped string */ #endif /* ZEND_DBX_ODBC_H */ diff --git a/ext/dbx/dbx_pgsql.c b/ext/dbx/dbx_pgsql.c index 88e28816ee..c8cac1908c 100644 --- a/ext/dbx/dbx_pgsql.c +++ b/ext/dbx/dbx_pgsql.c @@ -275,6 +275,30 @@ int dbx_pgsql_error(zval **rv, zval **dbx_handle, INTERNAL_FUNCTION_PARAMETERS) return 1; } +int dbx_pgsql_esc(zval **rv, zval **dbx_handle, zval **string, INTERNAL_FUNCTION_PARAMETERS) +{ + /* returns escaped string */ + /* replace \ with \\ */ + /* ' with '' */ + char * str; + int len; + char * tmpstr; + int tmplen; + + tmpstr = estrdup(Z_STRVAL_PP(string)); + tmplen = Z_STRLEN_PP(string); + /* php_str_to_str uses a smart_str that allocates memory */ + /* this memory must be freed or passed on to rv */ + str = php_str_to_str(tmpstr, tmplen, "\\", 1, "\\\\", 2, &len); + efree(tmpstr); + tmpstr=str; tmplen=len; + str = php_str_to_str(tmpstr, tmplen, "'", 1, "''", 2, &len); + efree(tmpstr); + + ZVAL_STRINGL(*rv, str, len, 0); + return 1; +} + /* * Local variables: * tab-width: 4 diff --git a/ext/dbx/dbx_pgsql.h b/ext/dbx/dbx_pgsql.h index 8da190bdcb..a5730889f0 100644 --- a/ext/dbx/dbx_pgsql.h +++ b/ext/dbx/dbx_pgsql.h @@ -45,6 +45,8 @@ int dbx_pgsql_getrow(zval **rv, zval **result_handle, long row_number, INTERNAL_ /* returns array[0..columncount-1] as strings on success or 0 as long on failure */ int dbx_pgsql_error(zval **rv, zval **dbx_handle, INTERNAL_FUNCTION_PARAMETERS); /* returns string */ +int dbx_pgsql_esc(zval **rv, zval **dbx_handle, zval **string, INTERNAL_FUNCTION_PARAMETERS); + /* returns escaped string */ #endif /* ZEND_DBX_PGSQL_H */ diff --git a/ext/dbx/dbx_sybasect.c b/ext/dbx/dbx_sybasect.c index 3521adf8b4..3cc4830165 100644 --- a/ext/dbx/dbx_sybasect.c +++ b/ext/dbx/dbx_sybasect.c @@ -274,6 +274,27 @@ int dbx_sybasect_error(zval **rv, zval **dbx_handle, INTERNAL_FUNCTION_PARAMETER return 1; } +int dbx_sybasect_esc(zval **rv, zval **dbx_handle, zval **string, INTERNAL_FUNCTION_PARAMETERS) +{ + /* returns escaped string */ + /* replace ' with '' */ + char * str; + int len; + char * tmpstr; + int tmplen; + + tmpstr = estrdup(Z_STRVAL_PP(string)); + tmplen = Z_STRLEN_PP(string); + /* php_str_to_str uses a smart_str that allocates memory */ + /* this memory must be freed or passed on to rv */ + str = php_str_to_str(tmpstr, tmplen, "'", 1, "''", 2, &len); + efree(tmpstr); + + ZVAL_STRINGL(*rv, str, len, 0); + + return 1; +} + /* * Local variables: * tab-width: 4 diff --git a/ext/dbx/dbx_sybasect.h b/ext/dbx/dbx_sybasect.h index 8b599c209a..bcde3ddb49 100644 --- a/ext/dbx/dbx_sybasect.h +++ b/ext/dbx/dbx_sybasect.h @@ -49,6 +49,8 @@ int dbx_sybasect_getrow(zval **rv, zval **result_handle, long row_number, INTERN /* returns array[0..columncount-1] as strings on success or 0 as long on failure */ int dbx_sybasect_error(zval **rv, zval **dbx_handle, INTERNAL_FUNCTION_PARAMETERS); /* returns string */ +int dbx_sybasect_esc(zval **rv, zval **dbx_handle, zval **string, INTERNAL_FUNCTION_PARAMETERS); + /* returns escaped string */ #endif /* ZEND_DBX_SYBASECT_H */ diff --git a/ext/dbx/php_dbx.h b/ext/dbx/php_dbx.h index d6e0286624..411ff5b98e 100644 --- a/ext/dbx/php_dbx.h +++ b/ext/dbx/php_dbx.h @@ -49,6 +49,7 @@ ZEND_FUNCTION(dbx_connect); ZEND_FUNCTION(dbx_close); ZEND_FUNCTION(dbx_query); ZEND_FUNCTION(dbx_error); +ZEND_FUNCTION(dbx_escape_string); ZEND_FUNCTION(dbx_sort); ZEND_FUNCTION(dbx_compare); |