From 0f02788f70cff1ed8f010fd9103ba6e545ed09f7 Mon Sep 17 00:00:00 2001 From: martinlanghoff Date: Tue, 16 Jan 2007 05:24:20 +0000 Subject: [PATCH] auth/db/auth.php - fewer queries against the ext db Previously sync script would query the ext db once for each field that might be wanted (repeated for each user). Changed to query once for each user. Originally by Peter Bulmer Somewhat adapted by Martin Langhoff ;-) --- auth/db/auth.php | 38 ++++++++++++++++++++++++++------------ 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/auth/db/auth.php b/auth/db/auth.php index 124f5f0cc9..06ba17eadb 100644 --- a/auth/db/auth.php +++ b/auth/db/auth.php @@ -117,20 +117,34 @@ class auth_plugin_db { "department", "address", "city", "country", "description", "idnumber", "lang"); - $result = array(); + //Array to map local fieldnames we want, to external fieldnames + $selectfields = array(); foreach ($fields as $field) { - if ($this->config->{'field_map_' . $field}) { - if ($rs = $authdb->Execute("SELECT " . $this->config->{'field_map_' . $field} . " as myfield FROM {$this->config->table} - WHERE {$this->config->fielduser} = '$username'")) { - if ( $rs->RecordCount() == 1 ) { - if (!empty($CFG->unicodedb)) { - $result["$field"] = addslashes(stripslashes($rs->fields['myfield'])); - } else { - $result["$field"] = addslashes(stripslashes(utf8_decode($rs->fields['myfield']))); - } - } - } + if (!empty($this->config->{'field_map_' . $field})) { + $selectfields[$field] = $this->config->{'field_map_' . $field}; + } + } + $result = array(); + //If at least one field is mapped from external db, get that mapped data: + if ($selectfields) { + $select = ''; + foreach ($selectfields as $localname=>$externalname) { + $select .= ", $externalname AS $localname"; + } + $select = 'SELECT ' . substr($select,1); + $sql = $select . + " FROM {$this->config->table}" . + " WHERE {$this->config->fielduser} = '$username'"; + if ($rs = $authdb->Execute($sql)) { + if ( $rs->RecordCount() == 1 ) { + foreach ($selectfields as $localname=>$externalname) { + if (empty($CFG->unicodedb)) { + $rs->fields[$localname] = utf8_decode($rs->fields[$localname]); + } + $result[$localname] = addslashes(stripslashes($rs->fields[$localname])); + } + } } } $authdb->Close(); -- 2.39.5