X-Git-Url: https://vcs.fsf.org/?p=squirrelmail.git;a=blobdiff_plain;f=functions%2Fabook_database.php;h=68110ee67a9098939f21f717794d859567eba2b4;hp=1bc5fa4f85e905184f7112fe3d8408edcbf901bc;hb=acb4fcd7e045e250c8028f56ee3d32ef350ed0aa;hpb=058b79d2d5cc1a03e894215ca3ba2c432b455c06 diff --git a/functions/abook_database.php b/functions/abook_database.php index 1bc5fa4f..68110ee6 100644 --- a/functions/abook_database.php +++ b/functions/abook_database.php @@ -147,10 +147,10 @@ class abook_database extends addressbook_backend { /* ========================== Private ======================= */ /** - * Constructor + * Constructor (PHP5 style, required in some future version of PHP) * @param array $param address book backend options */ - function abook_database($param) { + function __construct($param) { $this->sname = _("Personal Address Book"); /* test if PDO or Pear DB classes are available and freak out if necessary */ @@ -206,6 +206,13 @@ class abook_database extends addressbook_backend { } } + /** + * Constructor (PHP4 style, kept for compatibility reasons) + * @param array $param address book backend options + */ + function abook_database($param) { + return self::__construct($param); + } /** * Open the database. @@ -228,22 +235,47 @@ class abook_database extends addressbook_backend { if ($use_pdo) { // parse and convert DSN to PDO style + // Pear's full DSN syntax is one of the following: + // phptype(dbsyntax)://username:password@protocol+hostspec/database?option=value + // phptype(syntax)://user:pass@protocol(proto_opts)/database + // // $matches will contain: // 1: database type // 2: username // 3: password - // 4: hostname - // 5: database name -//TODO: add support for unix_socket and charset + // 4: hostname (and possible port number) OR protocol (and possible protocol options) + // 5: database name (and possible options) + // 6: port number (moved from match number 4) + // 7: options (moved from match number 5) + // 8: protocol (instead of hostname) + // 9: protocol options (moved from match number 4/8) +//TODO: do we care about supporting cases where no password is given? (this is a legal DSN, but causes an error below) if (!preg_match('|^(.+)://(.+):(.+)@(.+)/(.+)$|i', $this->dsn, $matches)) { return $this->set_error(_("Could not parse prefs DSN")); } + $matches[6] = NULL; + $matches[7] = NULL; + $matches[8] = NULL; + $matches[9] = NULL; if (preg_match('|^(.+):(\d+)$|', $matches[4], $host_port_matches)) { $matches[4] = $host_port_matches[1]; $matches[6] = $host_port_matches[2]; - } else + } + if (preg_match('|^(.+?)\((.+)\)$|', $matches[4], $protocol_matches)) { + $matches[8] = $protocol_matches[1]; + $matches[9] = $protocol_matches[2]; + $matches[4] = NULL; $matches[6] = NULL; - $pdo_prefs_dsn = $matches[1] . ':host=' . $matches[4] . (!empty($matches[6]) ? ';port=' . $matches[6] : '') . ';dbname=' . $matches[5]; + } +//TODO: currently we just ignore options specified on the end of the DSN + if (preg_match('|^(.+?)\?(.+)$|', $matches[5], $database_name_options_matches)) { + $matches[5] = $database_name_options_matches[1]; + $matches[7] = $database_name_options_matches[2]; + } + if ($matches[8] === 'unix' && !empty($matches[9])) + $pdo_prefs_dsn = $matches[1] . ':unix_socket=' . $matches[9] . ';dbname=' . $matches[5]; + else + $pdo_prefs_dsn = $matches[1] . ':host=' . $matches[4] . (!empty($matches[6]) ? ';port=' . $matches[6] : '') . ';dbname=' . $matches[5]; try { $dbh = new PDO($pdo_prefs_dsn, $matches[2], $matches[3]); } catch (Exception $e) { @@ -660,8 +692,8 @@ class abook_database extends addressbook_backend { else return $this->set_error(sprintf(_("Database error: %s"), _("Could not prepare query"))); } - array_unshift($where_clause_args[], $this->owner); - if (!($res = $sth->execute(array_array($where_clause_args)))) { + array_unshift($where_clause_args, $this->owner); + if (!($res = $sth->execute($where_clause_args))) { if ($pdo_show_sql_errors) return $this->set_error(sprintf(_("Database error: %s"), implode(' - ', $sth->errorInfo()))); else