Browse Source

Added dbx_escape_string function

# tested on odbc, oci8 and mysql
@Added dbx_escape_string function to dbx module. (Marc)
experimental/threaded
Marc Boeren 23 years ago
parent
commit
25e7a09229
  1. 54
      ext/dbx/dbx.c
  2. 1
      ext/dbx/dbx.h
  3. 22
      ext/dbx/dbx_fbsql.c
  4. 2
      ext/dbx/dbx_fbsql.h
  5. 21
      ext/dbx/dbx_mssql.c
  6. 2
      ext/dbx/dbx_mssql.h
  7. 37
      ext/dbx/dbx_mysql.c
  8. 2
      ext/dbx/dbx_mysql.h
  9. 21
      ext/dbx/dbx_oci8.c
  10. 2
      ext/dbx/dbx_oci8.h
  11. 21
      ext/dbx/dbx_odbc.c
  12. 2
      ext/dbx/dbx_odbc.h
  13. 24
      ext/dbx/dbx_pgsql.c
  14. 2
      ext/dbx/dbx_pgsql.h
  15. 21
      ext/dbx/dbx_sybasect.c
  16. 2
      ext/dbx/dbx_sybasect.h
  17. 1
      ext/dbx/php_dbx.h

54
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

1
ext/dbx/dbx.h

@ -30,6 +30,7 @@
#endif
#include "php.h"
#include "ext/standard/php_string.h"
#define DBX_PERSISTENT (1<<0)

22
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

2
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 */

21
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

2
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 */

37
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

2
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 */

21
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

2
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 */

21
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

2
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 */

24
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

2
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 */

21
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

2
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 */

1
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);

Loading…
Cancel
Save