]> granicus.if.org Git - php/commitdiff
Added dbx_escape_string function
authorMarc Boeren <mboeren@php.net>
Tue, 29 Oct 2002 14:08:40 +0000 (14:08 +0000)
committerMarc Boeren <mboeren@php.net>
Tue, 29 Oct 2002 14:08:40 +0000 (14:08 +0000)
# tested on odbc, oci8 and mysql
@Added dbx_escape_string function to dbx module. (Marc)

17 files changed:
ext/dbx/dbx.c
ext/dbx/dbx.h
ext/dbx/dbx_fbsql.c
ext/dbx/dbx_fbsql.h
ext/dbx/dbx_mssql.c
ext/dbx/dbx_mssql.h
ext/dbx/dbx_mysql.c
ext/dbx/dbx_mysql.h
ext/dbx/dbx_oci8.c
ext/dbx/dbx_oci8.h
ext/dbx/dbx_odbc.c
ext/dbx/dbx_odbc.h
ext/dbx/dbx_pgsql.c
ext/dbx/dbx_pgsql.h
ext/dbx/dbx_sybasect.c
ext/dbx/dbx_sybasect.h
ext/dbx/php_dbx.h

index 1d04d91b1b994964049ea1290a6a18cdc8b8b396..77403fb0a3ce0f028fae9eee7796187f295db88b 100644 (file)
@@ -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
index 338061855a3c628fedaa7442bd3fe9b152950187..0b428856aa1ce2bcfe94900f284aca6cd19c2573 100644 (file)
@@ -30,6 +30,7 @@
 #endif
 
 #include "php.h"
+#include "ext/standard/php_string.h"
 
 #define DBX_PERSISTENT         (1<<0)
 
index 3ab8bbd232fc49d019627c4b1ac8cec1bcfeaa02..56c3764f0ffb15f06dd3794905b742b6c7659e1f 100644 (file)
@@ -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
index e36504b0cbae0ec013392b5cce27a26ea91dcc64..4304c5ae109cf6e2b8b17698eee9255772b1cc90 100644 (file)
@@ -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 */
 
index f905ed10bed1f61149b89db277b3f15b5d7bed64..9ea38ca1eb592291d51e723d43327a9d103ec566 100644 (file)
@@ -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
index 2051da357fe7ed3abfe26a422ae7ad2a9c97c366..8fd7c7ef3a2f360b8538417579599159f6741bef 100644 (file)
@@ -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 */
 
index b9873147b69d84b4dae04b33c0635985372c3f60..3efd427038e6c60321aae8a0bded1ebc35d0ac3d 100644 (file)
@@ -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
index f9b14a1170b98ed2c198d370fe6b2ef494741ad0..d6bb78ed45778d71526e59bc3773c2072faf79bd 100644 (file)
@@ -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 */
 
index efdfbea472e84366dd1a35a4bd627ff127670bdf..da50844334c577f23bd6e379a04f31f340ff9c1d 100644 (file)
@@ -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
index be483df6c4a52b8caad4288f0499479462217753..f5bd0f05807d50ab59c422944b5c6bdd51d0f508 100644 (file)
@@ -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 */
 
index 01a0a82aecfe1035d420d587bb8dff90c76b54a3..139855026bafd3ee6c908d44856e09e758829723 100644 (file)
@@ -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
index 4cb8de2580117ba66ecf222482c0ea4a1fe73db5..2f97c0c3773933c7ed73dbedc9d2efdd30d9720c 100644 (file)
@@ -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 */
 
index 88e28816eef9fcdbcd9214cd4c137df8b4632364..c8cac1908c6962c74911b19d9e9e1db8f83e546b 100644 (file)
@@ -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
index 8da190bdcbff33f04475f9b81cc4ebb81cac8b83..a5730889f024839a49dd67d302c7a37d734d047e 100644 (file)
@@ -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 */
 
index 3521adf8b440a15092f25a65a30e5c715d8bf0ac..3cc48301650cc34b75973d5eb1c96094cbaff676 100644 (file)
@@ -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
index 8b599c209a5e6a63b0a2870dbe2f5fa8a59f9d6f..bcde3ddb497f6f910c14452fa8d22a0f665cdd2c 100644 (file)
@@ -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 */
 
index d6e0286624aa37fadeea3c49bbbd9b657f268737..411ff5b98e55574805a71eb971af90f390e0b93b 100644 (file)
@@ -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);