bool PDOPgSqlConnection::create(const Array &options){ long connect_timeout = pdo_attr_lval(options, PDO_ATTR_TIMEOUT, 30); struct pdo_data_src_parser vars[] = { { "host", "localhost", 0 }, { "port", "5432", 0 }, { "dbname", "", 0 }, { "user", nullptr, 0 }, { "password", nullptr, 0 } }; php_pdo_parse_data_source(data_source.data(), data_source.size(), vars, 5); std::stringstream conninfo; conninfo << "host='"; std::string host(vars[0].optval); ReplaceStringInPlace(host, "\\", "\\\\"); ReplaceStringInPlace(host, "'", "\\'"); conninfo << host << "' port='"; std::string port(vars[1].optval); ReplaceStringInPlace(port, "\\", "\\\\"); ReplaceStringInPlace(port, "'", "\\'"); conninfo << port << "' dbname='"; std::string dbname(vars[2].optval); ReplaceStringInPlace(dbname, "\\", "\\\\"); ReplaceStringInPlace(dbname, "'", "\\'"); conninfo << dbname << "' password='******'", "\\'"); conninfo << password << "' user='******'", "\\'"); conninfo << username << "'"; conninfo << " connect_timeout=" << connect_timeout; m_server = new PQ::Connection(conninfo.str()); if(m_server->status() == CONNECTION_OK){ return true; } else { handleError(nullptr, PHP_PDO_PGSQL_CONNECTION_FAILURE_SQLSTATE, m_server->errorMessage()); return false; } }
static int pdo_dblib_handle_factory(pdo_dbh_t *dbh, zval *driver_options) { pdo_dblib_db_handle *H; int i, nvars, nvers, ret = 0; const pdo_dblib_keyval tdsver[] = { {"4.2",DBVERSION_42} ,{"4.6",DBVERSION_46} ,{"5.0",DBVERSION_70} /* FIXME: This does not work with Sybase, but environ will */ ,{"6.0",DBVERSION_70} ,{"7.0",DBVERSION_70} #ifdef DBVERSION_71 ,{"7.1",DBVERSION_71} #endif #ifdef DBVERSION_72 ,{"7.2",DBVERSION_72} ,{"8.0",DBVERSION_72} #endif #ifdef DBVERSION_73 ,{"7.3",DBVERSION_73} #endif #ifdef DBVERSION_74 ,{"7.4",DBVERSION_74} #endif ,{"10.0",DBVERSION_100} ,{"auto",0} /* Only works with FreeTDS. Other drivers will bork */ }; struct pdo_data_src_parser vars[] = { { "charset", NULL, 0 } ,{ "appname", "PHP " PDO_DBLIB_FLAVOUR, 0 } ,{ "host", "127.0.0.1", 0 } ,{ "dbname", NULL, 0 } ,{ "secure", NULL, 0 } /* DBSETLSECURE */ ,{ "version", NULL, 0 } /* DBSETLVERSION */ }; nvars = sizeof(vars)/sizeof(vars[0]); nvers = sizeof(tdsver)/sizeof(tdsver[0]); php_pdo_parse_data_source(dbh->data_source, dbh->data_source_len, vars, nvars); H = pecalloc(1, sizeof(*H), dbh->is_persistent); H->login = dblogin(); H->err.sqlstate = dbh->error_code; H->stringify_uniqueidentifier = 0; if (!H->login) { goto cleanup; } if (driver_options) { int connect_timeout = pdo_attr_lval(driver_options, PDO_DBLIB_ATTR_CONNECTION_TIMEOUT, -1); int query_timeout = pdo_attr_lval(driver_options, PDO_DBLIB_ATTR_QUERY_TIMEOUT, -1); int timeout = pdo_attr_lval(driver_options, PDO_ATTR_TIMEOUT, 30); if (connect_timeout == -1) { connect_timeout = timeout; } if (query_timeout == -1) { query_timeout = timeout; } dbsetlogintime(connect_timeout); /* Connection/Login Timeout */ dbsettime(query_timeout); /* Statement Timeout */ H->stringify_uniqueidentifier = pdo_attr_lval(driver_options, PDO_DBLIB_ATTR_STRINGIFY_UNIQUEIDENTIFIER, 0); } DBERRHANDLE(H->login, (EHANDLEFUNC) pdo_dblib_error_handler); DBMSGHANDLE(H->login, (MHANDLEFUNC) pdo_dblib_msg_handler); if(vars[5].optval) { for(i=0;i<nvers;i++) { if(strcmp(vars[5].optval,tdsver[i].key) == 0) { if(FAIL==dbsetlversion(H->login, tdsver[i].value)) { pdo_raise_impl_error(dbh, NULL, "HY000", "PDO_DBLIB: Failed to set version specified in connection string."); goto cleanup; } break; } } if (i==nvers) { printf("Invalid version '%s'\n", vars[5].optval); pdo_raise_impl_error(dbh, NULL, "HY000", "PDO_DBLIB: Invalid version specified in connection string."); goto cleanup; /* unknown version specified */ } } if (dbh->username) { if(FAIL == DBSETLUSER(H->login, dbh->username)) { goto cleanup; } } if (dbh->password) { if(FAIL == DBSETLPWD(H->login, dbh->password)) { goto cleanup; } } #if !PHP_DBLIB_IS_MSSQL if (vars[0].optval) { DBSETLCHARSET(H->login, vars[0].optval); } #endif DBSETLAPP(H->login, vars[1].optval); /* DBSETLDBNAME is only available in FreeTDS 0.92 or above */ #ifdef DBSETLDBNAME if (vars[3].optval) { if(FAIL == DBSETLDBNAME(H->login, vars[3].optval)) goto cleanup; } #endif H->link = dbopen(H->login, vars[2].optval); if (!H->link) { goto cleanup; } /* * FreeTDS < 0.92 does not support the DBSETLDBNAME option * Send use database here after login (Will not work with SQL Azure) */ #ifndef DBSETLDBNAME if (vars[3].optval) { if(FAIL == dbuse(H->link, vars[3].optval)) goto cleanup; } #endif #if PHP_DBLIB_IS_MSSQL /* dblib do not return more than this length from text/image */ DBSETOPT(H->link, DBTEXTLIMIT, "2147483647"); #endif /* limit text/image from network */ DBSETOPT(H->link, DBTEXTSIZE, "2147483647"); /* allow double quoted indentifiers */ DBSETOPT(H->link, DBQUOTEDIDENT, "1"); ret = 1; dbh->max_escaped_char_length = 2; dbh->alloc_own_columns = 1; cleanup: for (i = 0; i < nvars; i++) { if (vars[i].freeme) { efree(vars[i].optval); } } dbh->methods = &dblib_methods; dbh->driver_data = H; if (!ret) { zend_throw_exception_ex(php_pdo_get_exception(), DBLIB_G(err).dberr, "SQLSTATE[%s] %s (severity %d)", DBLIB_G(err).sqlstate, DBLIB_G(err).dberrstr, DBLIB_G(err).severity); } return ret; }
/* {{{ pdo_mysql_handle_factory */ static int pdo_mysql_handle_factory(pdo_dbh_t *dbh, zval *driver_options) { pdo_mysql_db_handle *H; size_t i; int ret = 0; char *host = NULL, *unix_socket = NULL; unsigned int port = 3306; char *dbname; struct pdo_data_src_parser vars[] = { { "charset", NULL, 0 }, { "dbname", "", 0 }, { "host", "localhost", 0 }, { "port", "3306", 0 }, { "unix_socket", PDO_DEFAULT_MYSQL_UNIX_ADDR, 0 }, }; int connect_opts = 0 #ifdef CLIENT_MULTI_RESULTS |CLIENT_MULTI_RESULTS #endif ; #if defined(PDO_USE_MYSQLND) size_t dbname_len = 0; size_t password_len = 0; #endif #ifdef CLIENT_MULTI_STATEMENTS if (!driver_options) { connect_opts |= CLIENT_MULTI_STATEMENTS; } else if (pdo_attr_lval(driver_options, PDO_MYSQL_ATTR_MULTI_STATEMENTS, 1)) { connect_opts |= CLIENT_MULTI_STATEMENTS; } #endif PDO_DBG_ENTER("pdo_mysql_handle_factory"); PDO_DBG_INF_FMT("dbh=%p", dbh); #ifdef CLIENT_MULTI_RESULTS PDO_DBG_INF("multi results"); #endif php_pdo_parse_data_source(dbh->data_source, dbh->data_source_len, vars, 5); H = pecalloc(1, sizeof(pdo_mysql_db_handle), dbh->is_persistent); H->einfo.errcode = 0; H->einfo.errmsg = NULL; /* allocate an environment */ /* handle for the server */ if (!(H->server = pdo_mysql_init(dbh->is_persistent))) { pdo_mysql_error(dbh); goto cleanup; } #if defined(PDO_USE_MYSQLND) if (dbh->is_persistent) { mysqlnd_restart_psession(H->server); } #endif dbh->driver_data = H; #ifndef PDO_USE_MYSQLND H->max_buffer_size = 1024*1024; #endif H->assume_national_character_set_strings = 0; H->buffered = H->emulate_prepare = 1; /* handle MySQL options */ if (driver_options) { zend_long connect_timeout = pdo_attr_lval(driver_options, PDO_ATTR_TIMEOUT, 30); zend_long local_infile = pdo_attr_lval(driver_options, PDO_MYSQL_ATTR_LOCAL_INFILE, 0); zend_string *init_cmd = NULL; #ifndef PDO_USE_MYSQLND zend_string *default_file = NULL, *default_group = NULL; #endif zend_long compress = 0; zend_string *ssl_key = NULL, *ssl_cert = NULL, *ssl_ca = NULL, *ssl_capath = NULL, *ssl_cipher = NULL; H->buffered = pdo_attr_lval(driver_options, PDO_MYSQL_ATTR_USE_BUFFERED_QUERY, 1); H->emulate_prepare = pdo_attr_lval(driver_options, PDO_MYSQL_ATTR_DIRECT_QUERY, H->emulate_prepare); H->emulate_prepare = pdo_attr_lval(driver_options, PDO_ATTR_EMULATE_PREPARES, H->emulate_prepare); H->assume_national_character_set_strings = pdo_attr_lval(driver_options, PDO_ATTR_DEFAULT_STR_PARAM, 0) == PDO_PARAM_STR_NATL; #ifndef PDO_USE_MYSQLND H->max_buffer_size = pdo_attr_lval(driver_options, PDO_MYSQL_ATTR_MAX_BUFFER_SIZE, H->max_buffer_size); #endif if (pdo_attr_lval(driver_options, PDO_MYSQL_ATTR_FOUND_ROWS, 0)) { connect_opts |= CLIENT_FOUND_ROWS; } if (pdo_attr_lval(driver_options, PDO_MYSQL_ATTR_IGNORE_SPACE, 0)) { connect_opts |= CLIENT_IGNORE_SPACE; } if (mysql_options(H->server, MYSQL_OPT_CONNECT_TIMEOUT, (const char *)&connect_timeout)) { pdo_mysql_error(dbh); goto cleanup; } #ifndef PDO_USE_MYSQLND if (PG(open_basedir) && PG(open_basedir)[0] != '\0') { local_infile = 0; } #endif #if defined(MYSQL_OPT_LOCAL_INFILE) || defined(PDO_USE_MYSQLND) if (mysql_options(H->server, MYSQL_OPT_LOCAL_INFILE, (const char *)&local_infile)) { pdo_mysql_error(dbh); goto cleanup; } #endif #ifdef MYSQL_OPT_RECONNECT /* since 5.0.3, the default for this option is 0 if not specified. * we want the old behaviour * mysqlnd doesn't support reconnect, thus we don't have "|| defined(PDO_USE_MYSQLND)" */ { zend_long reconnect = 1; mysql_options(H->server, MYSQL_OPT_RECONNECT, (const char*)&reconnect); } #endif init_cmd = pdo_attr_strval(driver_options, PDO_MYSQL_ATTR_INIT_COMMAND, NULL); if (init_cmd) { if (mysql_options(H->server, MYSQL_INIT_COMMAND, (const char *)ZSTR_VAL(init_cmd))) { zend_string_release_ex(init_cmd, 0); pdo_mysql_error(dbh); goto cleanup; } zend_string_release_ex(init_cmd, 0); } #ifndef PDO_USE_MYSQLND default_file = pdo_attr_strval(driver_options, PDO_MYSQL_ATTR_READ_DEFAULT_FILE, NULL); if (default_file) { if (mysql_options(H->server, MYSQL_READ_DEFAULT_FILE, (const char *)ZSTR_VAL(default_file))) { zend_string_release_ex(default_file, 0); pdo_mysql_error(dbh); goto cleanup; } zend_string_release_ex(default_file, 0); } default_group = pdo_attr_strval(driver_options, PDO_MYSQL_ATTR_READ_DEFAULT_GROUP, NULL); if (default_group) { if (mysql_options(H->server, MYSQL_READ_DEFAULT_GROUP, (const char *)ZSTR_VAL(default_group))) { zend_string_release_ex(default_group, 0); pdo_mysql_error(dbh); goto cleanup; } zend_string_release_ex(default_group, 0); } #endif compress = pdo_attr_lval(driver_options, PDO_MYSQL_ATTR_COMPRESS, 0); if (compress) { if (mysql_options(H->server, MYSQL_OPT_COMPRESS, 0)) { pdo_mysql_error(dbh); goto cleanup; } } ssl_key = pdo_attr_strval(driver_options, PDO_MYSQL_ATTR_SSL_KEY, NULL); ssl_cert = pdo_attr_strval(driver_options, PDO_MYSQL_ATTR_SSL_CERT, NULL); ssl_ca = pdo_attr_strval(driver_options, PDO_MYSQL_ATTR_SSL_CA, NULL); ssl_capath = pdo_attr_strval(driver_options, PDO_MYSQL_ATTR_SSL_CAPATH, NULL); ssl_cipher = pdo_attr_strval(driver_options, PDO_MYSQL_ATTR_SSL_CIPHER, NULL); if (ssl_key || ssl_cert || ssl_ca || ssl_capath || ssl_cipher) { mysql_ssl_set(H->server, ssl_key? ZSTR_VAL(ssl_key) : NULL, ssl_cert? ZSTR_VAL(ssl_cert) : NULL, ssl_ca? ZSTR_VAL(ssl_ca) : NULL, ssl_capath? ZSTR_VAL(ssl_capath) : NULL, ssl_cipher? ZSTR_VAL(ssl_cipher) : NULL); if (ssl_key) { zend_string_release_ex(ssl_key, 0); } if (ssl_cert) { zend_string_release_ex(ssl_cert, 0); } if (ssl_ca) { zend_string_release_ex(ssl_ca, 0); } if (ssl_capath) { zend_string_release_ex(ssl_capath, 0); } if (ssl_cipher) { zend_string_release_ex(ssl_cipher, 0); } } #if MYSQL_VERSION_ID > 50605 || defined(PDO_USE_MYSQLND) { zend_string *public_key = pdo_attr_strval(driver_options, PDO_MYSQL_ATTR_SERVER_PUBLIC_KEY, NULL); if (public_key) { if (mysql_options(H->server, MYSQL_SERVER_PUBLIC_KEY, ZSTR_VAL(public_key))) { pdo_mysql_error(dbh); zend_string_release_ex(public_key, 0); goto cleanup; } zend_string_release_ex(public_key, 0); } } #endif #ifdef PDO_USE_MYSQLND { zend_long ssl_verify_cert = pdo_attr_lval(driver_options, PDO_MYSQL_ATTR_SSL_VERIFY_SERVER_CERT, -1); if (ssl_verify_cert != -1) { connect_opts |= ssl_verify_cert ? CLIENT_SSL_VERIFY_SERVER_CERT: CLIENT_SSL_DONT_VERIFY_SERVER_CERT; } } #endif } #ifdef PDO_MYSQL_HAS_CHARSET if (vars[0].optval && mysql_options(H->server, MYSQL_SET_CHARSET_NAME, vars[0].optval)) { pdo_mysql_error(dbh); goto cleanup; } #endif dbname = vars[1].optval; host = vars[2].optval; if(vars[3].optval) { port = atoi(vars[3].optval); } #ifdef PHP_WIN32 if (vars[2].optval && !strcmp(".", vars[2].optval)) { #else if (vars[2].optval && !strcmp("localhost", vars[2].optval)) { #endif unix_socket = vars[4].optval; } /* TODO: - Check zval cache + ZTS */ #ifdef PDO_USE_MYSQLND if (dbname) { dbname_len = strlen(dbname); } if (dbh->password) { password_len = strlen(dbh->password); } if (mysqlnd_connect(H->server, host, dbh->username, dbh->password, password_len, dbname, dbname_len, port, unix_socket, connect_opts, MYSQLND_CLIENT_NO_FLAG) == NULL) { #else if (mysql_real_connect(H->server, host, dbh->username, dbh->password, dbname, port, unix_socket, connect_opts) == NULL) { #endif pdo_mysql_error(dbh); goto cleanup; } if (!dbh->auto_commit) { mysql_handle_autocommit(dbh); } H->attached = 1; dbh->alloc_own_columns = 1; dbh->max_escaped_char_length = 2; dbh->methods = &mysql_methods; ret = 1; cleanup: for (i = 0; i < sizeof(vars)/sizeof(vars[0]); i++) { if (vars[i].freeme) { efree(vars[i].optval); } } dbh->methods = &mysql_methods; PDO_DBG_RETURN(ret); } /* }}} */ const pdo_driver_t pdo_mysql_driver = { PDO_DRIVER_HEADER(mysql), pdo_mysql_handle_factory };