Index: contrib/z-push/backend/BackendContactsExpresso.php
===================================================================
--- contrib/z-push/backend/BackendContactsExpresso.php	(revision 3637)
+++ contrib/z-push/backend/BackendContactsExpresso.php	(revision 4000)
@@ -11,13 +11,12 @@
  * Consult LICENSE file for details
  ************************************************/
-include_once('diffbackend.php');
-include_once('include/z_RTF.php');
-include_once "include/dbconnect.php";
+require_once("diffbackend.php");
+require_once("include/z_RTF.php");
+require_once("include/dbconnect.php");
+require_once("include/authldap.php");
 
 class BackendContactsExpresso extends BackendDiff {
-	var $_user;
-	var $_devid;
-	var $_protocolversion;
 	var $db;
+	var $_uidnumber;
 
 	function __construct()
@@ -26,7 +25,13 @@
 
 		if (!$this->db) {
-			echo "A DB connect error occured.\n";
+			echo "Erro de conexao com o Banco.\n";
 			exit;
 		}
+	}
+
+	function Logon($username, $domain, $password) {
+		$this->_uidnumber = $this->authUser($this->parseUser($username), $password);
+		if(! $this->_uidnumber) return false;
+		else return true;
 	}
 
@@ -35,5 +40,4 @@
 		$this->_devid = $devid;
 		$this->_protocolversion = $protocolversion;
-		//TODO: Autenticar Usuario no LDAP
 
 		return true;
@@ -49,8 +53,5 @@
 
 	function GetMessageList($folderid, $cutoffdate) {
-		$id_owner = $this->parseOwner($this->_user);
-		if ($id_owner == false) return false;
-
-		debugLog('ContactsExpresso::GetMessageList('.$id_owner.')');
+		debugLog('ContactsExpresso::GetMessageList('.$this->_uidnumber.')');
 		$messages = array();
 		$ids = array();
@@ -58,5 +59,5 @@
 			$result = pg_query($this->db,"BEGIN;");
 			if ($result == FALSE) throw new Exception(pg_last_error($this->db));
-			$result = pg_query($this->db, "select given_names, family_names, last_update, id_contact from phpgw_cc_contact where id_owner = " . $id_owner . ";");
+			$result = pg_query($this->db, "select given_names, family_names, last_update, id_contact from phpgw_cc_contact where id_owner = " . $this->_uidnumber . ";");
 			if ($result == FALSE) throw new Exception(pg_last_error($this->db));
 			while ($row = pg_fetch_row($result)) {
@@ -78,9 +79,6 @@
 	function GetFolderList() {
 		debugLog('ContactsExpresso::GetFolderList()');
-		$contacts = array();
 		$folder = $this->StatFolder("root");
-		$contacts[] = $folder;
 		return $folder;
-		//return $contacts;
 	}
 
@@ -117,7 +115,4 @@
 		debugLog('ContactsExpresso::StatMessage('.$folderid.', '.$id.')');
 		if($folderid != "root") return false;
-
-		$id_owner = $this->parseOwner($this->_user);
-		if ($id_owner == false) return false;
 
 		try {
@@ -149,7 +144,4 @@
 
 		// Parse the database into object
-		$id_owner = $this->parseOwner($this->_user);
-		if ($id_owner == false) return;
-
 		$message = new SyncContact();
 		try {
@@ -354,7 +346,4 @@
 		debugLog('ContactsExpresso::DeleteMessage('.$folderid.', '.$id.', ..)');
 
-		$id_owner = $this->parseOwner($this->_user);
-		if ($id_owner == false) return false;
-
 		$result = pg_query($this->db,"BEGIN;");
 		try {
@@ -416,6 +405,4 @@
 		debugLog('ContactsExpresso::ChangeMessage('.$folderid.', '.$id.', ..)');
 
-		$id_owner = $this->parseOwner($this->_user);
-		if ($id_owner == false) return false;
 		try {
 			$result = pg_query($this->db,"BEGIN;");
@@ -666,5 +653,5 @@
 			if (!$found_id_contact){
 				$arrayContact["id_contact"] = $id_contact;
-				$arrayContact["id_owner"] = $id_owner;
+				$arrayContact["id_owner"] = $this->_uidnumber;
 				$result = pg_insert($this->db, 'phpgw_cc_contact', $arrayContact);
 				if ($result == FALSE) throw new Exception(pg_last_error($this->db));
@@ -1032,5 +1019,5 @@
 				if ($result == FALSE) throw new Exception(pg_last_error($this->db));
 			}
-			
+
 			//TODO : Permitir mais de um número de telefone para Trabalho, Celular e Casa. O Expresso ainda não suporta isso :(
 
@@ -1068,5 +1055,4 @@
 		$data = str_replace("\r", "\n", $data);
 		$data = str_replace(array('\\', ';', ',', "\n"), array('\\\\', '\\;', '\\,', '\\n'), $data);
-		//return u2wi($data);
 		return $data;
 	}
@@ -1088,17 +1074,27 @@
 	}
 
-	function parseOwner($id_owner)
+	function parseUser($id_user)
 	{
-		$pos = strripos($id_owner, '\\');
+		$pos = strripos($id_user, '\\');
 		if ($pos === false) {
-			if (strlen($id_owner) <= 2) return false;
-			else return substr($id_owner, 0, strlen($id_owner)-2);
+			if (strlen($id_user) < 1) return false;
+			else return $id_user;
 		} else {
-			if (strlen($id_owner) <= $pos + 3) {
+			if (strlen($id_user) <= $pos + 1) {
 				return false;
 			} else {
-				return substr(substr($id_owner, $pos + 1), 0, strlen(substr($id_owner, $pos + 1))-2);
+				return substr(substr($id_user, $pos + 1), 0, strlen(substr($id_user, $pos + 1)));
 			}
 		}
+	}
+
+	// Authenticate user and return UIDNumber attribute on success
+	function authUser($user, $pwd) {
+		$ldap = new AuthLDAP();
+		$uid_number = ($ldap->bind($user,$pwd));
+		$ldap->disconnect();
+		unset($ldap);
+		if (! $uid_number) return false;
+		return $uid_number;
 	}
 };
