#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
/* 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[].
*/
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)
}
/* }}} */
+/* {{{ 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!
*/
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
#endif
#include "php.h"
+#include "ext/standard/php_string.h"
#define DBX_PERSISTENT (1<<0)
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
/* 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 */
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
/* 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 */
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
/* 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 */
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
/* 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 */
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
/* 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 */
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
/* 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 */
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
/* 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 */
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);