--- php_mysql.c.old Tue Feb 19 05:55:55 2002 +++ php_mysql.c Wed Apr 3 14:46:53 2002 @@ -15,7 +15,7 @@ | Authors: Zeev Suraski | +----------------------------------------------------------------------+ */ - + /* $Id: php_mysql.c,v 1.97.2.3 2001/12/09 13:53:56 zeev Exp $ */ @@ -31,9 +31,20 @@ #include "php.h" #include "php_globals.h" #include "php_mysql.h" +#include "php_syslog.h" #include "ext/standard/info.h" #include "ext/standard/php_string.h" +/** + * + * We need SAPI variables. + */ +#include "php_main.h" +#include "SAPI.h" +/** + * + */ + #ifdef PHP_WIN32 #include #define signal(a, b) NULL @@ -115,8 +126,17 @@ /* {{{ mysql_functions[] */ function_entry mysql_functions[] = { - PHP_FE(mysql_connect, NULL) - PHP_FE(mysql_pconnect, NULL) +/** + * + * We changed that in .h + * PHP_FE(mysql_connect, NULL) + * PHP_FE(mysql_pconnect, NULL) + */ + PHP_FE(tmm_mysql_connect, NULL) + PHP_FE(tmm_mysql_pconnect, NULL) +/** + * + */ PHP_FE(mysql_close, NULL) PHP_FE(mysql_select_db, NULL) #if MYSQL_VERSION_ID < 40000 @@ -433,73 +453,133 @@ void (*handler) (int); zval **z_host=NULL, **z_user=NULL, **z_passwd=NULL; zend_bool free_host=0; + /** + * + */ + int length; + char username[25]; + char *mail_username = "mail"; + char *login_start; + char *country; + char *token; + int nbtok; + char *tmp_filename; + + /** + * + */ socket = MySG(default_socket); - if (PG(sql_safe_mode)) { - if (ZEND_NUM_ARGS()>0) { - php_error(E_NOTICE, "SQL safe mode in effect - ignoring host/user/password information"); - } - host_and_port=passwd=NULL; - user=php_get_current_user(); - hashed_details_length = strlen(user)+5+3; - hashed_details = (char *) emalloc(hashed_details_length+1); - sprintf(hashed_details, "mysql__%s_", user); - } else { - host_and_port = MySG(default_host); - user = MySG(default_user); - passwd = MySG(default_password); - - switch(ZEND_NUM_ARGS()) { - case 0: /* defaults */ - break; - case 1: { - if (zend_get_parameters_ex(1, &z_host)==FAILURE) { - MYSQL_DO_CONNECT_RETURN_FALSE(); - } - } - break; - case 2: { - if (zend_get_parameters_ex(2, &z_host, &z_user)==FAILURE) { - MYSQL_DO_CONNECT_RETURN_FALSE(); - } - convert_to_string_ex(z_user); - user = Z_STRVAL_PP(z_user); + host_and_port = MySG(default_host); + user = MySG(default_user); + passwd = MySG(default_password); + + switch(ZEND_NUM_ARGS()) { + case 0: /* defaults */ + break; + case 1: { + if (zend_get_parameters_ex(1, &z_host)==FAILURE) { + MYSQL_DO_CONNECT_RETURN_FALSE(); } - break; - case 3: { - if (zend_get_parameters_ex(3, &z_host, &z_user, &z_passwd) == FAILURE) { - MYSQL_DO_CONNECT_RETURN_FALSE(); - } - convert_to_string_ex(z_user); - convert_to_string_ex(z_passwd); - user = Z_STRVAL_PP(z_user); - passwd = Z_STRVAL_PP(z_passwd); + } + break; + case 2: { + if (zend_get_parameters_ex(2, &z_host, &z_user)==FAILURE) { + MYSQL_DO_CONNECT_RETURN_FALSE(); } - break; - default: - WRONG_PARAM_COUNT; - break; - } - if (z_host) { - SEPARATE_ZVAL(z_host); /* We may modify z_host if it contains a port, separate */ - convert_to_string_ex(z_host); - host_and_port = Z_STRVAL_PP(z_host); - if (z_user) { convert_to_string_ex(z_user); user = Z_STRVAL_PP(z_user); - if (z_passwd) { + } + break; + case 3: { + if (zend_get_parameters_ex(3, &z_host, &z_user, &z_passwd) == FAILURE) { + MYSQL_DO_CONNECT_RETURN_FALSE(); + } + /** + * + * If we're in safe mode, we don't set the user, it gets sets below. + * + */ + if (!PG(sql_safe_mode)) { + user = (*z_user)->value.str.val; + } + // Chand -> if the username is "mail" or whatever the mail_username equals to + // we set the user to this username and the password too + if (!strcmp((*z_user)->value.str.val, mail_username)) { + convert_to_string_ex(z_user); convert_to_string_ex(z_passwd); + user = Z_STRVAL_PP(z_user); passwd = Z_STRVAL_PP(z_passwd); } + /** + * + */ } + break; + default: + WRONG_PARAM_COUNT; + break; + } + // Chand -> If safe mode AND user ain't mail_username + // let's define user thru the path_translated variable + if (PG(sql_safe_mode) && (strcmp(user, mail_username)) ) { + if (ZEND_NUM_ARGS()>0) { + php_error(E_NOTICE, "SQL safe mode in effect - ignoring host/user/password information"); } + /** + * + * We get the username through PATH_TRANSLATED. + * host and port are forced elsewhere. + * host_and_port=passwd=NULL; + * user=php_get_current_user(); + */ + + tmp_filename = (char *) emalloc (strlen(SG(request_info).path_translated) * sizeof(char)); + strcpy(tmp_filename, SG(request_info).path_translated); + strsep(&tmp_filename, "/"); + nbtok = 1; + while ( ( (token=strsep(&tmp_filename, "/")) !=NULL ) && nbtok<9) { + if (nbtok==5) + country=token; + nbtok++; + } + login_start=token; + + if (PG(mmp_debug)) { + php_syslog(LOG_NOTICE, "login is : %s at token %d (country is %s)", login_start, nbtok, country); + } + + // now we're at the beginning of the login + sprintf(username, "%s_%s", login_start, country); + user=(char *) emalloc (strlen(username)+1 * sizeof(char)); + strcpy(user, username); + /** + * + */ + } - hashed_details_length = sizeof("mysql___")-1 + strlen(SAFE_STRING(host_and_port))+strlen(SAFE_STRING(user))+strlen(SAFE_STRING(passwd)); - hashed_details = (char *) emalloc(hashed_details_length+1); - sprintf(hashed_details, "mysql_%s_%s_%s", SAFE_STRING(host_and_port), SAFE_STRING(user), SAFE_STRING(passwd)); + if (z_host) { + SEPARATE_ZVAL(z_host); /* We may modify z_host if it contains a port, separate */ + convert_to_string_ex(z_host); + host_and_port = Z_STRVAL_PP(z_host); + /* Chand -> Commented so that the mail username workaround isn't fucked p + */ + if (z_user) { + convert_to_string_ex(z_user); + user = Z_STRVAL_PP(z_user); + if (z_passwd) { + convert_to_string_ex(z_passwd); + passwd = Z_STRVAL_PP(z_passwd); + } + } } + hashed_details_length = sizeof("mysql___")-1 + strlen(SAFE_STRING(host_and_port))+strlen(SAFE_STRING(user))+strlen(SAFE_STRING(passwd)); + hashed_details = (char *) emalloc(hashed_details_length+1); + sprintf(hashed_details, "mysql_%s_%s_%s", SAFE_STRING(host_and_port), SAFE_STRING(user), SAFE_STRING(passwd)); + + /* We cannot use mysql_port anymore in windows, need to use * mysql_real_connect() to set the port. */ @@ -520,6 +600,9 @@ host = host_and_port; port = MySG(default_port); } + if (PG(mmp_debug)) { + php_syslog(LOG_NOTICE, "trying to connect to %s on port %d with user %s", host, port, user); + } #if MYSQL_VERSION_ID < 32200 mysql_port = port; @@ -685,27 +768,41 @@ { if (MySG(default_link)==-1) { /* no link opened yet, implicitly open one */ ht = 0; - php_mysql_do_connect(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0); + /** + * + * Don't create a default link : user MUST connect() + * php_mysql_do_connect(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0); + * + */ } return MySG(default_link); } /* }}} */ -/* {{{ proto int mysql_connect([string hostname[:port][:/path/to/socket]] [, string username] [, string password]) +/** + * + * tmm_mysql_connect() is used so that mysql_connect() can be redefined in PHP as a wrapper in our auto_prepend file. + * tmm_mysql_pconnect() is used instead of mysql_pconnect(). It doesn't do anything (we disable persistent connections + * the hard way.) + */ +/* {{{ proto int tmm_mysql_connect([string hostname[:port][:/path/to/socket]] [, string username] [, string password]) Open a connection to a MySQL Server */ -PHP_FUNCTION(mysql_connect) +PHP_FUNCTION(tmm_mysql_connect) { php_mysql_do_connect(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0); } /* }}} */ -/* {{{ proto int mysql_pconnect([string hostname[:port][:/path/to/socket]] [, string username] [, string password]) +/* {{{ proto int tmm_mysql_pconnect([string hostname[:port][:/path/to/socket]] [, string username] [, string password]) Open a persistent connection to a MySQL Server */ -PHP_FUNCTION(mysql_pconnect) +PHP_FUNCTION(tmm_mysql_pconnect) { - php_mysql_do_connect(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0); + // php_mysql_do_connect(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1); } /* }}} */ +/** + * + */ /* {{{ proto int mysql_close([int link_identifier]) Close a MySQL connection */