source: contrib/z-push/backend/BackendContactsExpresso.php @ 4000

Revision 4000, 56.0 KB checked in by emersonfaria, 13 years ago (diff)

Ticket #1746 - Criada autenticacao dos Backends no LDAP e corrigido bug de login alfanumerico

  • Property svn:executable set to *
Line 
1<?php
2/***********************************************
3 * File      :   BackendContactsExpresso.php
4 * Project   :   Z-Push
5 * Descr     :   This Contact Backend is for Expresso Groupware.
6 *
7 * Created   :   29.09.2010 - emerson-faria.nobre@serpro.gov.br
8 *
9 * ï¿œ Zarafa Deutschland GmbH, www.zarafaserver.de
10 * This file is distributed under GPL v2.
11 * Consult LICENSE file for details
12 ************************************************/
13require_once("diffbackend.php");
14require_once("include/z_RTF.php");
15require_once("include/dbconnect.php");
16require_once("include/authldap.php");
17
18class BackendContactsExpresso extends BackendDiff {
19        var $db;
20        var $_uidnumber;
21
22        function __construct()
23        {
24                $this->db = dbconnect();
25
26                if (!$this->db) {
27                        echo "Erro de conexao com o Banco.\n";
28                        exit;
29                }
30        }
31
32        function Logon($username, $domain, $password) {
33                $this->_uidnumber = $this->authUser($this->parseUser($username), $password);
34                if(! $this->_uidnumber) return false;
35                else return true;
36        }
37
38        function Setup($user, $devid, $protocolversion) {
39                $this->_user = $user;
40                $this->_devid = $devid;
41                $this->_protocolversion = $protocolversion;
42
43                return true;
44        }
45
46        function SendMail($rfc822, $forward = false, $reply = false, $parent = false) {
47                return false;
48        }
49
50        function GetWasteBasket() {
51                return false;
52        }
53
54        function GetMessageList($folderid, $cutoffdate) {
55                debugLog('ContactsExpresso::GetMessageList('.$this->_uidnumber.')');
56                $messages = array();
57                $ids = array();
58                try {
59                        $result = pg_query($this->db,"BEGIN;");
60                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
61                        $result = pg_query($this->db, "select given_names, family_names, last_update, id_contact from phpgw_cc_contact where id_owner = " . $this->_uidnumber . ";");
62                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
63                        while ($row = pg_fetch_row($result)) {
64                                $message = array();
65                                $message["id"] = $row[3];
66                                $message["mod"] = substr($row[2], 0, strlen($row[2])-3);
67                                $message["flags"] = 1; // always 'read'
68                                $messages[] = $message;
69                        }
70                        $result = pg_query($this->db,"COMMIT;");
71                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
72                } catch (Exception $e) {
73                        pg_query($this->db,"ROLLBACK;");
74                        debugLog("exception -> " . $e->getMessage() . " - ARQUIVO: " . $e->getFile() . " - LINHA: " . $e->getLine());
75                }
76                return $messages;
77        }
78
79        function GetFolderList() {
80                debugLog('ContactsExpresso::GetFolderList()');
81                $folder = $this->StatFolder("root");
82                return $folder;
83        }
84
85        function GetFolder($id) {
86                debugLog('ContactsExpresso::GetFolder('.$id.')');
87                if($id == "root") {
88                        $folder = new SyncFolder();
89                        $folder->serverid = $id;
90                        $folder->parentid = "0";
91                        $folder->displayname = "Contatos";
92                        $folder->type = SYNC_FOLDER_TYPE_CONTACT;
93
94                        return $folder;
95                } else return false;
96        }
97
98        function StatFolder($id) {
99                debugLog('ContactsExpresso::StatFolder('.$id.')');
100                $folder = $this->GetFolder($id);
101
102                $stat = array();
103                $stat["id"] = $id;
104                $stat["parent"] = $folder->parentid;
105                $stat["mod"] = $folder->displayname;
106
107                return $stat;
108        }
109
110        function GetAttachmentData($attname) {
111                return false;
112        }
113
114        function StatMessage($folderid, $id) {
115                debugLog('ContactsExpresso::StatMessage('.$folderid.', '.$id.')');
116                if($folderid != "root") return false;
117
118                try {
119                        $result = pg_query($this->db,"BEGIN;");
120                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
121                        $result_contact = pg_query($this->db, "select last_update from phpgw_cc_contact where id_contact = " . $id . ";");
122                        if ($result_contact == FALSE) throw new Exception(pg_last_error($this->db));
123                        while ($row_contact = pg_fetch_row($result_contact)) {
124                                if(isset($row_contact[0])) {
125                                        $message = array();
126                                        $message["mod"] = substr($row_contact[0], 0, strlen($row_contact[0])-3);
127                                        $message["id"] = $id;
128                                        $message["flags"] = 1;
129                                        return $message;
130                                }
131                        }
132                        $result = pg_query($this->db,"COMMIT;");
133                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
134                } catch (Exception $e) {
135                        pg_query($this->db,"ROLLBACK;");
136                        debugLog("exception -> " . $e->getMessage() . " - ARQUIVO: " . $e->getFile() . " - LINHA: " . $e->getLine());
137                }
138                return false;
139        }
140
141        function GetMessage($folderid, $id, $truncsize, $mimesupport = 0) {
142                debugLog('ContactsExpresso::GetMessage('.$folderid.', '.$id.', ..)');
143                if($folderid != "root") return;
144
145                // Parse the database into object
146                $message = new SyncContact();
147                try {
148                        $result = pg_query($this->db,"BEGIN;");
149                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
150                        $result_contact = pg_query($this->db, "select id_contact, id_owner, id_status, photo, alias, id_prefix, given_names, family_names, names_ordered, id_suffix, birthdate, sex, pgp_key, notes, is_global, last_status, last_update, category, web_page, corporate_name, job_title, department from phpgw_cc_contact where id_contact = " . $id . ";");
151                        if ($result_contact == FALSE) throw new Exception(pg_last_error($this->db));
152                        while ($row_contact = pg_fetch_row($result_contact)) {
153                                //if(isset($row_contact[3])) {
154                                //      $message->picture = base64_encode($row_contact[3]);
155                                //}
156                                if(isset($row_contact[4])) {
157                                        $message->nickname = utf8_encode($row_contact[4]);
158                                }
159                                if(isset($row_contact[7])) {
160                                        $arrayFamilyName = explode(' ',trim($row_contact[7]));
161                                        if (sizeof($arrayFamilyName) > 1) {
162                                                $message->lastname = utf8_encode($arrayFamilyName[sizeof($arrayFamilyName) - 1]);
163                                                for ($i = 0; $i < (sizeof($arrayFamilyName) - 1); $i++) {
164                                                        $message->middlename .= " " . utf8_encode($arrayFamilyName[$i]);
165                                                }
166                                                $message->middlename = trim($message->middlename);
167                                        } else {
168                                                $message->lastname = utf8_encode(trim($row_contact[7]));
169                                        }
170                                        $message->fileas = $message->lastname;
171                                }
172                                if(isset($row_contact[6])) {
173                                        $message->firstname = utf8_encode(trim($row_contact[6]));
174                                        if(isset($row_contact[7])) $message->fileas .= ', ' . $message->firstname;
175                                        else $message->fileas = $message->firstname;
176                                }
177                                if(isset($row_contact[10])) {
178                                        $dataParte = explode('-',$row_contact[10]);
179                                        $data = $dataParte[2] . '-' . $dataParte[1] . '-' . $dataParte[0];
180                                        $tz = date_default_timezone_get();
181                                        date_default_timezone_set('UTC');
182                                        $message->birthday = strtotime($data);
183                                        $message->birthday += 3600 * 10; // Soma 10 horas para nao alterar a data quando mudar o Timezone
184                                        date_default_timezone_set($tz);
185                                }
186                                // TODO:Incluir campo de Aniversario na sincronizacao. O BD do Expresso ainda nao tem esse campo :-(
187                                if(isset($row_contact[13])) {
188                                        $message->body = utf8_encode(str_replace('\\n', chr(13) . chr(10), $this->escape($row_contact[13])));
189                                        $message->bodysize = strlen($message->body);
190                                        $message->bodytruncated = 0;
191                                }
192                                //TODO:Tratar o conteudo do campo de categorias
193                                //if(isset($row_contact[17])) {
194                                //      $message->categories = utf8_encode($row_contact[17]);
195                                //}
196                                if(isset($row_contact[18])) {
197                                        $message->webpage = utf8_encode($row_contact[18]);
198                                }
199                                if(isset($row_contact[19])) {
200                                        $message->companyname = utf8_encode($row_contact[19]);
201                                        if (!isset($row_contact[6]) and !isset($row_contact[7])) $message->fileas = $message->companyname;
202                                }
203                                if(isset($row_contact[20])) {
204                                        $message->jobtitle = utf8_encode($row_contact[20]);
205                                }
206                                if(isset($row_contact[21])) {
207                                        $message->department = utf8_encode($row_contact[21]);
208                                }
209
210                                // Endereço Comercial
211                                $result_addresses_comercial = pg_query($this->db,"select co.id_address, co.id_city, city_name, co.id_state, state_symbol, co.id_country, address1, address2, complement, address_other, postal_code, po_box, address_is_default from phpgw_cc_addresses co join phpgw_cc_contact_addrs ca on (co.id_address = ca.id_address) join phpgw_cc_typeof_ct_addrs tca on (ca.id_typeof_contact_address = tca.id_typeof_contact_address) left outer join phpgw_cc_city ci on (ci.id_city = co.id_city) left outer join phpgw_cc_state cs on (cs.id_state = co.id_state) where tca.contact_address_type_name = 'Comercial' and ca.id_contact = " . $row_contact[0] . ";");
212                                if ($result_addresses_comercial == FALSE) throw new Exception(pg_last_error($this->db));
213                                while ($row_addresses_comercial = pg_fetch_row($result_addresses_comercial)) {
214                                        if (isset($row_addresses_comercial[2])) {
215                                                $message->businesscity = utf8_encode($row_addresses_comercial[2]);
216                                        }
217                                        if (isset($row_addresses_comercial[5])) {
218                                                $message->businesscountry = utf8_encode($row_addresses_comercial[5]);
219                                        }
220                                        if (isset($row_addresses_comercial[10])) {
221                                                $message->businesspostalcode = utf8_encode($row_addresses_comercial[10]);
222                                        }
223                                        if (isset($row_addresses_comercial[4])) {
224                                                $message->businessstate = utf8_encode($row_addresses_comercial[4]);
225                                        }
226                                        if (isset($row_addresses_comercial[6])) {
227                                                $message->businessstreet = utf8_encode($row_addresses_comercial[6]);
228                                        }
229                                        if (isset($row_addresses_comercial[8])) {
230                                                if (isset($message->businessstreet)) {
231                                                        $message->businessstreet .= ":";
232                                                }
233                                                $message->businessstreet .= utf8_encode($row_addresses_comercial[8]);
234                                        }
235                                        if (isset($row_addresses_comercial[7])) {
236                                                if (isset($message->businessstreet)) {
237                                                        $message->businessstreet .= ":";
238                                                }
239                                                $message->businessstreet .= utf8_encode($row_addresses_comercial[7]);
240                                        }
241                                        if (isset($row_addresses_comercial[9])) {
242                                                if (isset($message->businessstreet)) {
243                                                        $message->businessstreet .= ":";
244                                                }
245                                                $message->businessstreet .= utf8_encode($row_addresses_comercial[9]);
246                                        }
247                                        if (isset($row_addresses_comercial[11])) {
248                                                if (isset($message->businessstreet)) {
249                                                        $message->businessstreet .= ":";
250                                                }
251                                                $message->businessstreet .= utf8_encode($row_addresses_comercial[11]);
252                                        }
253                                }
254                                // Endereço Residencial
255                                $result_addresses_residencial = pg_query($this->db,"select co.id_address, co.id_city, city_name, co.id_state, state_name, co.id_country, address1, address2, complement, address_other, postal_code, po_box, address_is_default from phpgw_cc_addresses co join phpgw_cc_contact_addrs ca on (co.id_address = ca.id_address) join phpgw_cc_typeof_ct_addrs tca on (ca.id_typeof_contact_address = tca.id_typeof_contact_address) left outer join phpgw_cc_city ci on (ci.id_city = co.id_city) left outer join phpgw_cc_state cs on (cs.id_state = co.id_state) where tca.contact_address_type_name = 'Residencial' and ca.id_contact = " . $row_contact[0] . ";");
256                                if ($result_addresses_residencial == FALSE) throw new Exception(pg_last_error($this->db));
257                                while ($row_addresses_residencial = pg_fetch_row($result_addresses_residencial)) {
258                                        if (isset($row_addresses_residencial[2])) {
259                                                $message->homecity = utf8_encode($row_addresses_residencial[2]);
260                                        }
261                                        if (isset($row_addresses_residencial[5])) {
262                                                $message->homecountry = utf8_encode($row_addresses_residencial[5]);
263                                        }
264                                        if (isset($row_addresses_residencial[10])) {
265                                                $message->homepostalcode = utf8_encode($row_addresses_residencial[10]);
266                                        }
267                                        if (isset($row_addresses_residencial[4])) {
268                                                $message->homestate = utf8_encode($row_addresses_residencial[4]);
269                                        }
270                                        if (isset($row_addresses_residencial[6])) {
271                                                $message->homestreet = utf8_encode($row_addresses_residencial[6]);
272                                        }
273                                        if (isset($row_addresses_residencial[8])) {
274                                                if (isset($message->homestreet)) {
275                                                        $message->homestreet .= ":";
276                                                }
277                                                $message->homestreet .= utf8_encode($row_addresses_residencial[8]);
278                                        }
279                                        if (isset($row_addresses_residencial[7])) {
280                                                if (isset($message->homestreet)) {
281                                                        $message->homestreet .= ":";
282                                                }
283                                                $message->homestreet .= utf8_encode($row_addresses_residencial[7]);
284                                        }
285                                        if (isset($row_addresses_residencial[9])) {
286                                                if (isset($message->homestreet)) {
287                                                        $message->homestreet .= ":";
288                                                }
289                                                $message->homestreet .= utf8_encode($row_addresses_residencial[9]);
290                                        }
291                                        if (isset($row_addresses_residencial[11])) {
292                                                if (isset($message->homestreet)) {
293                                                        $message->homestreet .= ":";
294                                                }
295                                                $message->homestreet .= utf8_encode($row_addresses_residencial[11]);
296                                        }
297                                }
298                                // Emails
299                                $result_emails = pg_query($this->db,"select cn.id_connection, connection_name, connection_value, connection_is_default from phpgw_cc_connections cn join phpgw_cc_contact_conns cc on (cn.id_connection = cc.id_connection) join phpgw_cc_typeof_ct_conns ct on (ct.id_typeof_contact_connection = cc.id_typeof_contact_connection) where ct.contact_connection_type_name = 'Email' and cc.id_contact = "  . $row_contact[0] . ";");
300                                if ($result_emails == FALSE) throw new Exception(pg_last_error($this->db));
301                                while ($row_emails = pg_fetch_row($result_emails)) {
302                                        if ($row_emails[1] == "Principal") {
303                                                $message->email1address = utf8_encode($row_emails[2]);
304                                        }
305                                        if ($row_emails[1] == "Alternativo") {
306                                                $message->email2address = utf8_encode($row_emails[2]);
307                                        }
308                                        //TODO : Atribuir o email3address. O Expresso ainda não tem campo para um terceiro email :(
309                                }
310                                // Telefones
311                                $result_tel = pg_query($this->db,"select cn.id_connection, connection_name, connection_value, connection_is_default from phpgw_cc_connections cn join phpgw_cc_contact_conns cc on (cn.id_connection = cc.id_connection) join phpgw_cc_typeof_ct_conns ct on (ct.id_typeof_contact_connection = cc.id_typeof_contact_connection) where ct.contact_connection_type_name = 'Telefone' and cc.id_contact = "  . $row_contact[0] . ";");
312                                if ($result_tel == FALSE) throw new Exception(pg_last_error($this->db));
313                                while ($row_tel = pg_fetch_row($result_tel)) {
314                                        if ($row_tel[1] == "Trabalho") {
315                                                $message->businessphonenumber = utf8_encode($row_tel[2]);
316                                        }
317                                        if ($row_tel[1] == "Casa") {
318                                                $message->homephonenumber = utf8_encode($row_tel[2]);
319                                        }
320                                        if ($row_tel[1] == "Celular") {
321                                                $message->mobilephonenumber = utf8_encode($row_tel[2]);
322                                        }
323                                        if ($row_tel[1] == "Fax") {
324                                                $message->businessfaxnumber = utf8_encode($row_tel[2]);
325                                        }
326                                        if ($row_tel[1] == "Principal") {
327                                                $message->business2phonenumber = utf8_encode($row_tel[2]);
328                                        }
329                                        if ($row_tel[1] == "Alternativo") {
330                                                $message->home2phonenumber = utf8_encode($row_tel[2]);
331                                        }
332                                        //TODO : Permitir mais de um número de telefone para Trabalho, Casa e Celular. O Expresso ainda não suporta isso :(
333                                }
334                        }
335                        $result = pg_query($this->db,"COMMIT;");
336                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
337                } catch (Exception $e) {
338                        pg_query($this->db,"ROLLBACK;");
339                        debugLog("exception -> " . $e->getMessage() . " - ARQUIVO: " . $e->getFile() . " - LINHA: " . $e->getLine());
340                        return;
341                }
342                return $message;
343        }
344
345        function DeleteMessage($folderid, $id) {
346                debugLog('ContactsExpresso::DeleteMessage('.$folderid.', '.$id.', ..)');
347
348                $result = pg_query($this->db,"BEGIN;");
349                try {
350                        $result_contact = pg_query($this->db, "select id_contact from phpgw_cc_contact where id_contact = " . $id . ";");
351                        if ($result_contact == FALSE) throw new Exception(pg_last_error($this->db));
352                        while ($row_contact = pg_fetch_row($result_contact)) {
353                                if(isset($row_contact[0])) {
354                                        $id_contact = $row_contact[0];
355                                }
356                        }
357                        if(!isset($id_contact)){
358                                return true;
359                        }
360
361                        $result_contact_addrs = pg_query($this->db, "select id_address from phpgw_cc_contact_addrs where id_contact = " . $id_contact . ";");
362                        if ($result_contact_addrs == FALSE) throw new Exception(pg_last_error($this->db));
363                        while ($row_contact_addrs = pg_fetch_row($result_contact_addrs)) {
364                                if(isset($row_contact_addrs[0])) {
365                                        $id_address = $row_contact_addrs[0];
366                                        $result_delete_address = pg_delete($this->db, "phpgw_cc_addresses", array('id_address' => $id_address));
367                                        if ($result_delete_address == FALSE) throw new Exception(pg_last_error($this->db));
368                                }
369                        }
370
371                        $result_delete_contact_addrs = pg_delete($this->db, "phpgw_cc_contact_addrs", array('id_contact' => $id_contact));
372                        if ($result_delete_contact_addrs == FALSE) throw new Exception(pg_last_error($this->db));
373
374                        $result_contact_conns = pg_query($this->db, "select id_connection from phpgw_cc_contact_conns where id_contact = " . $id_contact . ";");
375                        if ($result_contact_conns == FALSE) throw new Exception(pg_last_error($this->db));
376                        while ($row_contact_conns = pg_fetch_row($result_contact_conns)) {
377                                if(isset($row_contact_conns[0])) {
378                                        $id_connection = $row_contact_conns[0];
379                                        $result_delete_connections = pg_delete($this->db, "phpgw_cc_connections", array('id_connection' => $id_connection));
380                                        if ($result_delete_connections == FALSE) throw new Exception(pg_last_error($this->db));
381                                        $result_delete_contact_grps = pg_delete($this->db,"phpgw_cc_contact_grps", array('id_connection' => $id_connection));
382                                        if ($result_delete_contact_grps == FALSE) throw new Exception(pg_last_error($this->db));
383                                }
384                        }
385
386                        $result_delete_contact_conns = pg_delete($this->db, "phpgw_cc_contact_conns", array('id_contact' => $id_contact));
387                        if ($result_delete_contact_conns == FALSE) throw new Exception(pg_last_error($this->db));
388                        $result_delete_contact = pg_delete($this->db, "phpgw_cc_contact", array('id_contact' => $id_contact));
389                        if ($result_delete_contact == FALSE) throw new Exception(pg_last_error($this->db));
390                        $result = pg_query($this->db,"COMMIT;");
391                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
392                } catch (Exception $e) {
393                        pg_query($this->db,"ROLLBACK;");
394                        debugLog("exception -> " . $e->getMessage() . " - ARQUIVO: " . $e->getFile() . " - LINHA: " . $e->getLine());
395                        return false;
396                }
397                return true;
398        }
399
400        function SetReadFlag($folderid, $id, $flags) {
401                return false;
402        }
403
404        function ChangeMessage($folderid, $id, $message) {
405                debugLog('ContactsExpresso::ChangeMessage('.$folderid.', '.$id.', ..)');
406
407                try {
408                        $result = pg_query($this->db,"BEGIN;");
409                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
410
411                        // Obtem o id_contact
412                        $found_id_contact = false;
413                        if (trim($id) !== "") {
414                                $result_contact = pg_query($this->db, "select id_contact from phpgw_cc_contact where id_contact = " . $id . ";");
415                                if ($result_contact == FALSE) throw new Exception(pg_last_error($this->db));
416                                // tenta localizar id_contact para fazer Update
417                                while ($row_contact = pg_fetch_row($result_contact)) {
418                                        if(isset($row_contact[0])) {
419                                                $id_contact = $row_contact[0];
420                                                $found_id_contact = true;
421                                        }
422                                }
423                        }
424                        // se não encontrou id_contact para fazer Update, define o próximo id_contact para fazer Insert
425                        if (!isset($id_contact)) {
426                                $result = pg_query($this->db,"LOCK TABLE phpgw_cc_contact IN ACCESS EXCLUSIVE MODE;");
427                                if ($result == FALSE) throw new Exception(pg_last_error($this->db));
428                                $result_contact_max_id = pg_query($this->db, "select max(id_contact) from phpgw_cc_contact;");
429                                if ($result_contact_max_id == FALSE) throw new Exception(pg_last_error($this->db));
430                                $row_contact_max_id = pg_fetch_row($result_contact_max_id);
431                                if (isset($row_contact_max_id[0])) {
432                                        $id_contact = $row_contact_max_id[0] + 1;
433                                } else $id_contact = 1;
434                        }
435
436                        // Procura o id_address comercial e residencial
437                        $result_address = pg_query($this->db, "select co.id_address, tca.contact_address_type_name from phpgw_cc_addresses co join phpgw_cc_contact_addrs ca on (co.id_address = ca.id_address) join phpgw_cc_typeof_ct_addrs tca on (ca.id_typeof_contact_address = tca.id_typeof_contact_address) where ca.id_contact = " . $id_contact . ";");
438                        if ($result_address == FALSE) throw new Exception(pg_last_error($this->db));
439                        $found_id_address_comercial = false;
440                        $found_id_address_residencial = false;
441                        while ($row_address = pg_fetch_row($result_address)) {
442                                if(isset($row_address[0])) {
443                                        if ($row_address[1] == "Comercial") {
444                                                $id_address_comercial = $row_address[0];
445                                                $found_id_address_comercial = true;
446                                        }
447                                        if ($row_address[1] == "Residencial") {
448                                                $id_address_residencial = $row_address[0];
449                                                $found_id_address_residencial = true;
450                                        }
451                                }
452                        }
453                        // Verifica se os campos de endereco estao preenchidos na mensagem recebida do dispositivo movel
454                        $isset_business_address_fields = false;
455                        if(isset($message->businessstate) or isset($message->businesspostalcode) or isset($message->businessstreet)) {
456                                $isset_business_address_fields = true;
457                        }
458                        $isset_home_address_fields = false;
459                        if(isset($message->homestate) or isset($message->homepostalcode) or isset($message->homestreet)) {
460                                $isset_home_address_fields = true;
461                        }
462                        // Obtem o ultimo id_address
463                        if (!($found_id_address_comercial and $found_id_address_residencial) and ($isset_business_address_fields or $isset_home_address_fields)) {
464                                $result = pg_query($this->db,"LOCK TABLE phpgw_cc_addresses IN ACCESS EXCLUSIVE MODE;");
465                                if ($result == FALSE) throw new Exception(pg_last_error($this->db));
466                                $result_address_max_id = pg_query($this->db, "select max(id_address) from phpgw_cc_addresses;");
467                                if ($result_address_max_id == FALSE) throw new Exception(pg_last_error($this->db));
468                                $array_row_address_max_id = pg_fetch_row($result_address_max_id);
469                                if (isset($array_row_address_max_id)) $row_address_max_id = $array_row_address_max_id[0];
470                                if (!isset($row_address_max_id)) $row_address_max_id = 0;
471                        }
472                        $next_offset_address_to_insert = 0;
473                        // se não encontrou id_address_comercial para fazer Update, define o próximo id_address_comercial para fazer Insert
474                        if (!$found_id_address_comercial and $isset_business_address_fields) {
475                                $next_offset_address_to_insert += 1;
476                                $id_address_comercial = $row_address_max_id + $next_offset_address_to_insert;
477                        }
478                        // se não encontrou id_address_residencial para fazer Update, define o próximo id_address_residencial para fazer Insert
479                        if (!$found_id_address_residencial and $isset_home_address_fields) {
480                                $next_offset_address_to_insert += 1;
481                                $id_address_residencial = $row_address_max_id + $next_offset_address_to_insert;
482                        }
483
484                        // Procura o id_connection de Emails e Telefones
485                        $result_connection = pg_query($this->db, "select cn.id_connection, connection_name, connection_is_default, cc.id_typeof_contact_connection from phpgw_cc_connections cn join phpgw_cc_contact_conns cc on (cn.id_connection = cc.id_connection) join phpgw_cc_typeof_ct_conns ct on (ct.id_typeof_contact_connection = cc.id_typeof_contact_connection) where cc.id_contact = " . $id_contact . ";");
486                        if ($result_connection == FALSE) throw new Exception(pg_last_error($this->db));
487                        $found_id_connection_email_principal = false;
488                        $found_id_connection_email_alternativo = false;
489                        $found_id_connection_tel_trabalho = false;
490                        $found_id_connection_tel_casa = false;
491                        $found_id_connection_tel_celular = false;
492                        $found_id_connection_tel_fax = false;
493                        $found_id_connection_tel_principal = false;
494                        $found_id_connection_tel_alternativo = false;
495                        $tel_default = "";
496                        $email_default = "";
497                        while ($row_connection = pg_fetch_row($result_connection)) {
498                                if(isset($row_connection[0])) {
499                                        if ($row_connection[1] == "Principal" and $row_connection[3] == 1) {
500                                                $id_connection_email_principal = $row_connection[0];
501                                                $found_id_connection_email_principal = true;
502                                                if ($row_connection[2] == 't') $email_default = "Principal";
503                                        }
504                                        if ($row_connection[1] == "Alternativo" and $row_connection[3] == 1) {
505                                                $id_connection_email_alternativo = $row_connection[0];
506                                                $found_id_connection_email_alternativo = true;
507                                                if ($row_connection[2] == 't') $email_default = "Alternativo";
508                                        }
509                                        if ($row_connection[1] == "Trabalho") {
510                                                $id_connection_tel_trabalho = $row_connection[0];
511                                                $found_id_connection_tel_trabalho = true;
512                                                if ($row_connection[2] == 't') $tel_default = "Trabalho";
513                                        }
514                                        if ($row_connection[1] == "Casa") {
515                                                $id_connection_tel_casa = $row_connection[0];
516                                                $found_id_connection_tel_casa = true;
517                                                if ($row_connection[2] == 't') $tel_default = "Casa";
518                                        }
519                                        if ($row_connection[1] == "Celular") {
520                                                $id_connection_tel_celular = $row_connection[0];
521                                                $found_id_connection_tel_celular = true;
522                                                if ($row_connection[2] == 't') $tel_default = "Celular";
523                                        }
524                                        if ($row_connection[1] == "Fax") {
525                                                $id_connection_tel_fax = $row_connection[0];
526                                                $found_id_connection_tel_fax = true;
527                                                if ($row_connection[2] == 't') $tel_default = "Fax";
528                                        }
529                                        if ($row_connection[1] == "Principal" and $row_connection[3] == 2) {
530                                                $id_connection_tel_principal = $row_connection[0];
531                                                $found_id_connection_tel_principal = true;
532                                                if ($row_connection[2] == 't') $tel_default = "Principal";
533                                        }
534                                        if ($row_connection[1] == "Alternativo" and $row_connection[3] == 2) {
535                                                $id_connection_tel_alternativo = $row_connection[0];
536                                                $found_id_connection_tel_alternativo = true;
537                                                if ($row_connection[2] == 't') $tel_default = "Alternativo";
538                                        }
539                                }
540                        }
541                        // Obtem o ultimo id_connection
542                        if (!($found_id_connection_email_principal and $found_id_connection_email_alternativo and $found_id_connection_tel_trabalho and $found_id_connection_tel_celular and $found_id_connection_tel_casa and $found_id_connection_tel_fax and $found_id_connection_tel_principal and $found_id_connection_tel_alternativo) and (isset($message->email1address) or isset($message->email2address) or isset($message->businessphonenumber) or isset($message->homephonenumber) or isset($message->mobilephonenumber) or isset($message->businessfaxnumber) or isset($message->business2phonenumber) or isset($message->home2phonenumber))){
543                                $result = pg_query($this->db,"LOCK TABLE phpgw_cc_connections IN ACCESS EXCLUSIVE MODE;");
544                                if ($result == FALSE) throw new Exception(pg_last_error($this->db));
545                                $result_connection_max_id = pg_query($this->db, "select max(id_connection) from phpgw_cc_connections;");
546                                if ($result_connection_max_id == FALSE) throw new Exception(pg_last_error($this->db));
547                                $array_row_connection_max_id = pg_fetch_row($result_connection_max_id);
548                                if (isset($array_row_connection_max_id)) $row_connection_max_id = $array_row_connection_max_id[0];
549                                if (!isset($row_connection_max_id)) $row_connection_max_id = 0;
550                        }
551                        $next_offset_connection_to_insert = 0;
552                        // se não encontrou id_connection_email_principal para fazer Update, define o próximo id_connection_email_principal para fazer Insert
553                        if (!$found_id_connection_email_principal and isset($message->email1address)) {
554                                $next_offset_connection_to_insert += 1;
555                                $id_connection_email_principal = $row_connection_max_id + $next_offset_connection_to_insert;
556                        }
557                        // se não encontrou id_connection_email_alternativo para fazer Update, define o próximo id_connection_email_alternativo para fazer Insert
558                        if (!$found_id_connection_email_alternativo and isset($message->email2address)) {
559                                $next_offset_connection_to_insert += 1;
560                                $id_connection_email_alternativo = $row_connection_max_id + $next_offset_connection_to_insert;
561                        }
562                        // se não encontrou $id_connection_tel_trabalho para fazer Update, define o próximo $id_connection_tel_trabalho para fazer Insert
563                        if (!$found_id_connection_tel_trabalho and isset($message->businessphonenumber)) {
564                                $next_offset_connection_to_insert += 1;
565                                $id_connection_tel_trabalho = $row_connection_max_id + $next_offset_connection_to_insert;
566                        }
567                        // se não encontrou $id_connection_tel_casa para fazer Update, define o próximo $id_connection_tel_casa para fazer Insert
568                        if (!$found_id_connection_tel_casa and isset($message->homephonenumber)) {
569                                $next_offset_connection_to_insert += 1;
570                                $id_connection_tel_casa = $row_connection_max_id + $next_offset_connection_to_insert;
571                        }
572                        // se não encontrou $id_connection_tel_celular para fazer Update, define o próximo $id_connection_tel_celular para fazer Insert
573                        if (!$found_id_connection_tel_celular and isset($message->mobilephonenumber)) {
574                                $next_offset_connection_to_insert += 1;
575                                $id_connection_tel_celular = $row_connection_max_id + $next_offset_connection_to_insert;
576                        }
577                        // se não encontrou $id_connection_tel_fax para fazer Update, define o próximo $id_connection_tel_fax para fazer Insert
578                        if (!$found_id_connection_tel_fax and isset($message->businessfaxnumber)) {
579                                $next_offset_connection_to_insert += 1;
580                                $id_connection_tel_fax = $row_connection_max_id + $next_offset_connection_to_insert;
581                        }
582                        // se não encontrou $id_connection_tel_principal para fazer Update, define o próximo $id_connection_tel_principal para fazer Insert
583                        if (!$found_id_connection_tel_principal and isset($message->business2phonenumber)) {
584                                $next_offset_connection_to_insert += 1;
585                                $id_connection_tel_principal = $row_connection_max_id + $next_offset_connection_to_insert;
586                        }
587                        // se não encontrou $id_connection_tel_alternativo para fazer Update, define o próximo $id_connection_tel_alternativo para fazer Insert
588                        if (!$found_id_connection_tel_alternativo and isset($message->home2phonenumber)) {
589                                $next_offset_connection_to_insert += 1;
590                                $id_connection_tel_alternativo = $row_connection_max_id + $next_offset_connection_to_insert;
591                        }
592
593                        // Incluir/Alterar registro na tabela phpgw_cc_contact no Banco de Dados
594                        //if(isset($message->picture)) {
595                        //      $arrayContact["photo"] = base64_decode($message->picture);
596                        //}
597                        if(isset($message->nickname)) {
598                                $arrayContact["alias"] = utf8_decode($message->nickname);
599                        }
600                        if(isset($message->firstname)) {
601                                $arrayContact["given_names"] = utf8_decode($message->firstname);
602                                $arrayContact["names_ordered"] = utf8_decode($message->firstname);
603                        }
604                        if (isset($message->middlename)) {
605                                $arrayContact["family_names"] = utf8_decode($message->middlename);
606                                if(isset($message->firstname)) {
607                                        $arrayContact["names_ordered"] .= " ";
608                                }
609                                $arrayContact["names_ordered"] .= utf8_decode($message->middlename);
610                        }
611                        if(isset($message->lastname)) {
612                                if(isset($message->middlename)) {
613                                        $arrayContact["family_names"] .= " ";
614                                }
615                                if (isset($arrayContact["family_names"])) $arrayContact["family_names"] .= utf8_decode($message->lastname);
616                                else $arrayContact["family_names"] = utf8_decode($message->lastname);
617                                if(isset($message->firstname) or isset($message->middlename)) {
618                                        $arrayContact["names_ordered"] .= " ";
619                                }
620                                $arrayContact["names_ordered"] .= utf8_decode($message->lastname);
621                        }
622                        if (isset($message->birthday)) {
623                                $tz = date_default_timezone_get();
624                                date_default_timezone_set('UTC');
625                                $arrayContact["birthdate"] = date("Y-m-d",$message->birthday);
626                                date_default_timezone_set($tz);
627                        }
628                        //TODO: Incluir o campo de Aniversario na Sincronizacao. O DB do Expresso nao tem esse campo :-(
629
630                        if(isset($message->rtf)) {
631                                $rtf_to_ascii = new rtf();
632                                $rtf_to_ascii->output("ascii");
633                                $result_loadrtf = $rtf_to_ascii->loadrtf(base64_decode($message->rtf));
634                                if ($result_loadrtf == true) $rtf_to_ascii->parse();
635                                $arrayContact["notes"] = $rtf_to_ascii->out;
636                        }
637                        //TODO: Tratar o conteudo do campo de categorias
638                        //if(isset($message->categories)) {
639                        //      $arrayContact["category"] = $this->truncateString(utf8_decode($message->categories),20);
640                        //}
641                        if(isset($message->webpage)) {
642                                $arrayContact["web_page"] = $this->truncateString(utf8_decode($message->webpage),100);
643                        }
644                        if(isset($message->companyname)) {
645                                $arrayContact["corporate_name"] = $this->truncateString(utf8_decode($message->companyname),100);
646                        }
647                        if(isset($message->jobtitle)) {
648                                $arrayContact["job_title"] = $this->truncateString(utf8_decode($message->jobtitle),40);
649                        }
650                        if(isset($message->department)) {
651                                $arrayContact["department"] = $this->truncateString(utf8_decode($message->department),30);
652                        }
653                        if (!$found_id_contact){
654                                $arrayContact["id_contact"] = $id_contact;
655                                $arrayContact["id_owner"] = $this->_uidnumber;
656                                $result = pg_insert($this->db, 'phpgw_cc_contact', $arrayContact);
657                                if ($result == FALSE) throw new Exception(pg_last_error($this->db));
658                        } else {
659                                $result = pg_update($this->db, 'phpgw_cc_contact', $arrayContact, array('id_contact' => $id_contact));
660                                if ($result == FALSE) throw new Exception(pg_last_error($this->db));
661                        }
662
663                        // Incluir/Alterar Endereco Comercial na tabela phpgw_cc_addresses no Banco de Dados
664                        if(isset($message->businessstate)) {
665                                $result = pg_query($this->db, "select id_state, id_country from phpgw_cc_state where LOWER(to_ASCII(state_name)) = '" . trim(strtolower($this->removeAccents(utf8_decode($message->businessstate)))) . "' or LOWER(to_ASCII(state_symbol)) = '" . trim(strtolower($this->removeAccents(utf8_decode($message->businessstate)))) . "';");
666                                if ($result == FALSE) throw new Exception(pg_last_error($this->db));
667                                $row = pg_fetch_row($result);
668                                if(isset($row[0])) {
669                                        $arrayAddressComercial["id_state"] = $row[0];
670                                }
671                                if(isset($row[1])) {
672                                        $arrayAddressComercial["id_country"] = $row[1];
673                                }
674                                if(isset($message->businesscity) and isset($arrayAddressComercial["id_state"])) {
675                                        $result = pg_query($this->db, "select id_city from phpgw_cc_city where id_state = " . $arrayAddressComercial["id_state"] . " and LOWER(to_ASCII(city_name)) = '" . trim(strtolower($this->removeAccents(utf8_decode($message->businesscity)))) . "';");
676                                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
677                                        $row = pg_fetch_row($result);
678                                        if(isset($row[0])) {
679                                                $arrayAddressComercial["id_city"] = $row[0];
680                                        }
681                                }
682                        }
683                        if(isset($message->businesspostalcode)) {
684                                $arrayAddressComercial["postal_code"] = $this->truncateString(utf8_decode($message->businesspostalcode),15);
685                        }
686                        if(isset($message->businessstreet)) {
687                                $arrayAddressComercial["address1"] = $this->truncateString(utf8_decode($message->businessstreet),60);
688                        }
689                        if($isset_business_address_fields) {
690                                if (!$found_id_address_comercial) {
691                                        $arrayAddressComercial["id_address"] = $id_address_comercial;
692                                        $result = pg_insert($this->db, 'phpgw_cc_addresses', $arrayAddressComercial);
693                                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
694                                        $arrayContactAddressComercial["id_contact"] = $id_contact;
695                                        $arrayContactAddressComercial["id_address"] = $id_address_comercial;
696                                        $arrayContactAddressComercial["id_typeof_contact_address"] = 1; //comercial
697                                        $result = pg_insert($this->db, 'phpgw_cc_contact_addrs', $arrayContactAddressComercial);
698                                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
699                                } else {
700                                        $result = pg_update($this->db, 'phpgw_cc_addresses', $arrayAddressComercial, array('id_address' => $id_address_comercial));
701                                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
702                                }
703                        } elseif ($found_id_address_comercial) {
704                                $result = pg_delete($this->db, "phpgw_cc_contact_addrs", array('id_contact' => $id_contact, 'id_address' => $id_address_comercial));
705                                if ($result == FALSE) throw new Exception(pg_last_error($this->db));
706                                $result = pg_delete($this->db, "phpgw_cc_addresses", array('id_address' => $id_address_comercial));
707                                if ($result == FALSE) throw new Exception(pg_last_error($this->db));
708                        }
709
710                        // Incluir/Alterar Endereco Residencial na tabela phpgw_cc_addresses no Banco de Dados
711                        if(isset($message->homestate)) {
712                                $result = pg_query($this->db, "select id_state, id_country from phpgw_cc_state where LOWER(to_ASCII(state_name)) = '" . trim(strtolower($this->removeAccents(utf8_decode($message->homestate)))) . "' or LOWER(to_ASCII(state_symbol)) = '" . trim(strtolower($this->removeAccents(utf8_decode($message->homestate)))) . "';");
713                                if ($result == FALSE) throw new Exception(pg_last_error($this->db));
714                                $row = pg_fetch_row($result);
715                                if(isset($row[0])) {
716                                        $arrayAddressResidencial["id_state"] = $row[0];
717                                }
718                                if(isset($row[1])) {
719                                        $arrayAddressResidencial["id_country"] = $row[1];
720                                }
721                                if(isset($message->homecity) and isset($arrayAddressResidencial["id_state"])) {
722                                        $result = pg_query($this->db, "select id_city from phpgw_cc_city where id_state = " . $arrayAddressResidencial["id_state"] . " and LOWER(to_ASCII(city_name)) = '" . trim(strtolower($this->removeAccents(utf8_decode($message->homecity)))) . "';");
723                                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
724                                        $row = pg_fetch_row($result);
725                                        if(isset($row[0])) {
726                                                $arrayAddressResidencial["id_city"] = $row[0];
727                                        }
728                                }
729                        }
730                        if(isset($message->homepostalcode)) {
731                                $arrayAddressResidencial["postal_code"] = $this->truncateString(utf8_decode($message->homepostalcode),15);
732                        }
733                        if(isset($message->homestreet)) {
734                                $arrayAddressResidencial["address1"] = $this->truncateString(utf8_decode($message->homestreet),60);
735                        }
736                        if($isset_home_address_fields) {
737                                if (!$found_id_address_residencial) {
738                                        $arrayAddressResidencial["id_address"] = $id_address_residencial;
739                                        $result = pg_insert($this->db, 'phpgw_cc_addresses', $arrayAddressResidencial);
740                                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
741                                        $arrayContactAddressResidencial["id_contact"] = $id_contact;
742                                        $arrayContactAddressResidencial["id_address"] = $id_address_residencial;
743                                        $arrayContactAddressResidencial["id_typeof_contact_address"] = 2; //residencial
744                                        $result = pg_insert($this->db, 'phpgw_cc_contact_addrs', $arrayContactAddressResidencial);
745                                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
746                                } else {
747                                        $result = pg_update($this->db, 'phpgw_cc_addresses', $arrayAddressResidencial, array('id_address' => $id_address_residencial));
748                                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
749                                }
750                        } elseif ($found_id_address_residencial) {
751                                $result = pg_delete($this->db, "phpgw_cc_contact_addrs", array('id_contact' => $id_contact, 'id_address' => $id_address_residencial));
752                                if ($result == FALSE) throw new Exception(pg_last_error($this->db));
753                                $result = pg_delete($this->db, "phpgw_cc_addresses", array('id_address' => $id_address_residencial));
754                                if ($result == FALSE) throw new Exception(pg_last_error($this->db));
755                        }
756
757                        // Email Principal
758                        if(isset($message->email1address)) {
759                                $arrayConnectionEmailPrincipal["connection_value"] = $this->truncateString(utf8_decode($message->email1address),100);
760                                if (!$found_id_connection_email_principal){
761                                        $arrayConnectionEmailPrincipal["id_connection"] = $id_connection_email_principal;
762                                        $arrayConnectionEmailPrincipal["connection_name"] = "Principal";
763                                        if ($email_default != "Alternativo"){
764                                                $arrayConnectionEmailPrincipal["connection_is_default"] = 't';
765                                                $email_default = "Principal";
766                                        } else {
767                                                $arrayConnectionEmailPrincipal["connection_is_default"] = 'f';
768                                        }
769                                        $result = pg_insert($this->db, 'phpgw_cc_connections', $arrayConnectionEmailPrincipal);
770                                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
771                                        $arrayContactConnection["id_contact"] = $id_contact;
772                                        $arrayContactConnection["id_connection"] = $id_connection_email_principal;
773                                        $arrayContactConnection["id_typeof_contact_connection"] = 1;
774                                        $result = pg_insert($this->db, 'phpgw_cc_contact_conns', $arrayContactConnection);
775                                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
776                                } else {
777                                        $result = pg_update($this->db, 'phpgw_cc_connections', $arrayConnectionEmailPrincipal, array('id_connection' => $id_connection_email_principal));
778                                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
779                                }
780
781                        } elseif ($found_id_connection_email_principal) {
782                                $result = pg_delete($this->db, "phpgw_cc_contact_conns", array('id_contact' => $id_contact, 'id_connection' => $id_connection_email_principal));
783                                if ($result == FALSE) throw new Exception(pg_last_error($this->db));
784                                $result = pg_delete($this->db, "phpgw_cc_contact_grps", array('id_connection' => $id_connection_email_principal));
785                                if ($result == FALSE) throw new Exception(pg_last_error($this->db));
786                                $result = pg_delete($this->db, "phpgw_cc_connections", array('id_connection' => $id_connection_email_principal));
787                                if ($result == FALSE) throw new Exception(pg_last_error($this->db));
788                        }
789
790                        // Email Alternativo
791                        if(isset($message->email2address)) {
792                                $arrayConnectionEmailAlternativo["connection_value"] = $this->truncateString(utf8_decode($message->email2address),100);
793                                if (!$found_id_connection_email_alternativo){
794                                        $arrayConnectionEmailAlternativo["id_connection"] = $id_connection_email_alternativo;
795                                        $arrayConnectionEmailAlternativo["connection_name"] = "Alternativo";
796                                        if ($email_default != "Principal"){
797                                                $arrayConnectionEmailAlternativo["connection_is_default"] = 't';
798                                                $email_default = "Alternativo";
799                                        } else {
800                                                $arrayConnectionEmailAlternativo["connection_is_default"] = 'f';
801                                        }
802                                        $result = pg_insert($this->db, 'phpgw_cc_connections', $arrayConnectionEmailAlternativo);
803                                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
804                                        $arrayContactConnection["id_contact"] = $id_contact;
805                                        $arrayContactConnection["id_connection"] = $id_connection_email_alternativo;
806                                        $arrayContactConnection["id_typeof_contact_connection"] = 1;
807                                        $result = pg_insert($this->db, 'phpgw_cc_contact_conns', $arrayContactConnection);
808                                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
809                                } else {
810                                        $result = pg_update($this->db, 'phpgw_cc_connections', $arrayConnectionEmailAlternativo, array('id_connection' => $id_connection_email_alternativo));
811                                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
812                                }
813
814                        } elseif ($found_id_connection_email_alternativo) {
815                                $result = pg_delete($this->db, "phpgw_cc_contact_conns", array('id_contact' => $id_contact, 'id_connection' => $id_connection_email_alternativo));
816                                if ($result == FALSE) throw new Exception(pg_last_error($this->db));
817                                $result = pg_delete($this->db, "phpgw_cc_contact_grps", array('id_connection' => $id_connection_email_alternativo));
818                                if ($result == FALSE) throw new Exception(pg_last_error($this->db));
819                                $result = pg_delete($this->db, "phpgw_cc_connections", array('id_connection' => $id_connection_email_alternativo));
820                                if ($result == FALSE) throw new Exception(pg_last_error($this->db));
821                        }
822                        //TODO : Atribuir o email3address. O Expresso ainda não tem campo para um terceiro email :(
823
824                        // Telefone Trabalho
825                        if(isset($message->businessphonenumber)) {
826                                $arrayConnectionTelTrabalho["connection_value"] = $this->truncateString(utf8_decode($message->businessphonenumber),100);
827                                if (!$found_id_connection_tel_trabalho){
828                                        $arrayConnectionTelTrabalho["id_connection"] = $id_connection_tel_trabalho;
829                                        $arrayConnectionTelTrabalho["connection_name"] = "Trabalho";
830                                        if ($tel_default != "Celular" and $tel_default != "Casa" and $tel_default != "Fax"  and $tel_default != "Principal"  and $tel_default != "Alternativo"){
831                                                $arrayConnectionTelTrabalho["connection_is_default"] = 't';
832                                                $tel_default = "Trabalho";
833                                        } else {
834                                                $arrayConnectionTelTrabalho["connection_is_default"] = 'f';
835                                        }
836                                        $result = pg_insert($this->db, 'phpgw_cc_connections', $arrayConnectionTelTrabalho);
837                                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
838                                        $arrayContactConnection["id_contact"] = $id_contact;
839                                        $arrayContactConnection["id_connection"] = $id_connection_tel_trabalho;
840                                        $arrayContactConnection["id_typeof_contact_connection"] = 2;
841                                        $result = pg_insert($this->db, 'phpgw_cc_contact_conns', $arrayContactConnection);
842                                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
843                                } else {
844                                        $result = pg_update($this->db, 'phpgw_cc_connections', $arrayConnectionTelTrabalho, array('id_connection' => $id_connection_tel_trabalho));
845                                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
846                                }
847
848                        } elseif ($found_id_connection_tel_trabalho) {
849                                $result = pg_delete($this->db, "phpgw_cc_contact_conns", array('id_contact' => $id_contact, 'id_connection' => $id_connection_tel_trabalho));
850                                if ($result == FALSE) throw new Exception(pg_last_error($this->db));
851                                $result = pg_delete($this->db, "phpgw_cc_contact_grps", array('id_connection' => $id_connection_tel_trabalho));
852                                if ($result == FALSE) throw new Exception(pg_last_error($this->db));
853                                $result = pg_delete($this->db, "phpgw_cc_connections", array('id_connection' => $id_connection_tel_trabalho));
854                                if ($result == FALSE) throw new Exception(pg_last_error($this->db));
855                        }
856
857                        // Telefone Celular
858                        if(isset($message->mobilephonenumber)) {
859                                $arrayConnectionTelCelular["connection_value"] = $this->truncateString(utf8_decode($message->mobilephonenumber),100);
860                                if (!$found_id_connection_tel_celular){
861                                        $arrayConnectionTelCelular["id_connection"] = $id_connection_tel_celular;
862                                        $arrayConnectionTelCelular["connection_name"] = "Celular";
863                                        if ($tel_default != "Trabalho" and $tel_default != "Casa" and $tel_default != "Fax"   and $tel_default != "Principal"  and $tel_default != "Alternativo"){
864                                                $arrayConnectionTelCelular["connection_is_default"] = 't';
865                                                $tel_default = "Celular";
866                                        } else {
867                                                $arrayConnectionTelCelular["connection_is_default"] = 'f';
868                                        }
869                                        $result = pg_insert($this->db, 'phpgw_cc_connections', $arrayConnectionTelCelular);
870                                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
871                                        $arrayContactConnection["id_contact"] = $id_contact;
872                                        $arrayContactConnection["id_connection"] = $id_connection_tel_celular;
873                                        $arrayContactConnection["id_typeof_contact_connection"] = 2;
874                                        $result = pg_insert($this->db, 'phpgw_cc_contact_conns', $arrayContactConnection);
875                                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
876                                } else {
877                                        $result = pg_update($this->db, 'phpgw_cc_connections', $arrayConnectionTelCelular, array('id_connection' => $id_connection_tel_celular));
878                                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
879                                }
880
881                        } elseif ($found_id_connection_tel_celular) {
882                                $result = pg_delete($this->db, "phpgw_cc_contact_conns", array('id_contact' => $id_contact, 'id_connection' => $id_connection_tel_celular));
883                                if ($result == FALSE) throw new Exception(pg_last_error($this->db));
884                                $result = pg_delete($this->db, "phpgw_cc_contact_grps", array('id_connection' => $id_connection_tel_celular));
885                                if ($result == FALSE) throw new Exception(pg_last_error($this->db));
886                                $result = pg_delete($this->db, "phpgw_cc_connections", array('id_connection' => $id_connection_tel_celular));
887                                if ($result == FALSE) throw new Exception(pg_last_error($this->db));
888                        }
889
890                        // Telefone Casa
891                        if(isset($message->homephonenumber)) {
892                                $arrayConnectionTelCasa["connection_value"] = $this->truncateString(utf8_decode($message->homephonenumber),100);
893                                if (!$found_id_connection_tel_casa){
894                                        $arrayConnectionTelCasa["id_connection"] = $id_connection_tel_casa;
895                                        $arrayConnectionTelCasa["connection_name"] = "Casa";
896                                        if ($tel_default != "Trabalho" and $tel_default != "Celular" and $tel_default != "Fax" and $tel_default != "Principal"  and $tel_default != "Alternativo"){
897                                                $arrayConnectionTelCasa["connection_is_default"] = 't';
898                                                $tel_default = "Casa";
899                                        } else {
900                                                $arrayConnectionTelCasa["connection_is_default"] = 'f';
901                                        }
902                                        $result = pg_insert($this->db, 'phpgw_cc_connections', $arrayConnectionTelCasa);
903                                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
904                                        $arrayContactConnection["id_contact"] = $id_contact;
905                                        $arrayContactConnection["id_connection"] = $id_connection_tel_casa;
906                                        $arrayContactConnection["id_typeof_contact_connection"] = 2;
907                                        $result = pg_insert($this->db, 'phpgw_cc_contact_conns', $arrayContactConnection);
908                                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
909                                } else {
910                                        $result = pg_update($this->db, 'phpgw_cc_connections', $arrayConnectionTelCasa, array('id_connection' => $id_connection_tel_casa));
911                                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
912                                }
913
914                        } elseif ($found_id_connection_tel_casa) {
915                                $result = pg_delete($this->db, "phpgw_cc_contact_conns", array('id_contact' => $id_contact, 'id_connection' => $id_connection_tel_casa));
916                                if ($result == FALSE) throw new Exception(pg_last_error($this->db));
917                                $result = pg_delete($this->db, "phpgw_cc_contact_grps", array('id_connection' => $id_connection_tel_casa));
918                                if ($result == FALSE) throw new Exception(pg_last_error($this->db));
919                                $result = pg_delete($this->db, "phpgw_cc_connections", array('id_connection' => $id_connection_tel_casa));
920                                if ($result == FALSE) throw new Exception(pg_last_error($this->db));
921                        }
922
923                        // Fax
924                        if(isset($message->businessfaxnumber)) {
925                                $arrayConnectionFax["connection_value"] = $this->truncateString(utf8_decode($message->businessfaxnumber),100);
926                                if (!$found_id_connection_tel_fax){
927                                        $arrayConnectionFax["id_connection"] = $id_connection_tel_fax;
928                                        $arrayConnectionFax["connection_name"] = "Fax";
929                                        if ($tel_default != "Trabalho" and $tel_default != "Celular" and $tel_default != "Casa" and $tel_default != "Principal"  and $tel_default != "Alternativo"){
930                                                $arrayConnectionFax["connection_is_default"] = 't';
931                                                $tel_default = "Fax";
932                                        } else {
933                                                $arrayConnectionFax["connection_is_default"] = 'f';
934                                        }
935                                        $result = pg_insert($this->db, 'phpgw_cc_connections', $arrayConnectionFax);
936                                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
937                                        $arrayContactConnection["id_contact"] = $id_contact;
938                                        $arrayContactConnection["id_connection"] = $id_connection_tel_fax;
939                                        $arrayContactConnection["id_typeof_contact_connection"] = 2;
940                                        $result = pg_insert($this->db, 'phpgw_cc_contact_conns', $arrayContactConnection);
941                                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
942                                } else {
943                                        $result = pg_update($this->db, 'phpgw_cc_connections', $arrayConnectionFax, array('id_connection' => $id_connection_tel_fax));
944                                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
945                                }
946
947                        } elseif ($found_id_connection_tel_fax) {
948                                $result = pg_delete($this->db, "phpgw_cc_contact_conns", array('id_contact' => $id_contact, 'id_connection' => $id_connection_tel_fax));
949                                if ($result == FALSE) throw new Exception(pg_last_error($this->db));
950                                $result = pg_delete($this->db, "phpgw_cc_contact_grps", array('id_connection' => $id_connection_tel_fax));
951                                if ($result == FALSE) throw new Exception(pg_last_error($this->db));
952                                $result = pg_delete($this->db, "phpgw_cc_connections", array('id_connection' => $id_connection_tel_fax));
953                                if ($result == FALSE) throw new Exception(pg_last_error($this->db));
954                        }
955
956                        // Telefone Principal
957                        if(isset($message->business2phonenumber)) {
958                                $arrayConnectionTelPrincipal["connection_value"] = $this->truncateString(utf8_decode($message->business2phonenumber),100);
959                                if (!$found_id_connection_tel_principal){
960                                        $arrayConnectionTelPrincipal["id_connection"] = $id_connection_tel_principal;
961                                        $arrayConnectionTelPrincipal["connection_name"] = "Principal";
962                                        if ($tel_default != "Celular" and $tel_default != "Casa" and $tel_default != "Fax"  and $tel_default != "Trabalho"  and $tel_default != "Alternativo"){
963                                                $arrayConnectionTelPrincipal["connection_is_default"] = 't';
964                                                $tel_default = "Principal";
965                                        } else {
966                                                $arrayConnectionTelPrincipal["connection_is_default"] = 'f';
967                                        }
968                                        $result = pg_insert($this->db, 'phpgw_cc_connections', $arrayConnectionTelPrincipal);
969                                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
970                                        $arrayContactConnection["id_contact"] = $id_contact;
971                                        $arrayContactConnection["id_connection"] = $id_connection_tel_principal;
972                                        $arrayContactConnection["id_typeof_contact_connection"] = 2;
973                                        $result = pg_insert($this->db, 'phpgw_cc_contact_conns', $arrayContactConnection);
974                                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
975                                } else {
976                                        $result = pg_update($this->db, 'phpgw_cc_connections', $arrayConnectionTelPrincipal, array('id_connection' => $id_connection_tel_principal));
977                                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
978                                }
979
980                        } elseif ($found_id_connection_tel_principal) {
981                                $result = pg_delete($this->db, "phpgw_cc_contact_conns", array('id_contact' => $id_contact, 'id_connection' => $id_connection_tel_principal));
982                                if ($result == FALSE) throw new Exception(pg_last_error($this->db));
983                                $result = pg_delete($this->db, "phpgw_cc_contact_grps", array('id_connection' => $id_connection_tel_principal));
984                                if ($result == FALSE) throw new Exception(pg_last_error($this->db));
985                                $result = pg_delete($this->db, "phpgw_cc_connections", array('id_connection' => $id_connection_tel_principal));
986                                if ($result == FALSE) throw new Exception(pg_last_error($this->db));
987                        }
988
989                        // Telefone Alternativo
990                        if(isset($message->home2phonenumber)) {
991                                $arrayConnectionTelAlternativo["connection_value"] = $this->truncateString(utf8_decode($message->home2phonenumber),100);
992                                if (!$found_id_connection_tel_alternativo){
993                                        $arrayConnectionTelAlternativo["id_connection"] = $id_connection_tel_alternativo;
994                                        $arrayConnectionTelAlternativo["connection_name"] = "Alternativo";
995                                        if ($tel_default != "Trabalho" and $tel_default != "Celular" and $tel_default != "Fax" and $tel_default != "Principal"  and $tel_default != "Casa"){
996                                                $arrayConnectionTelAlternativo["connection_is_default"] = 't';
997                                                $tel_default = "Alternativo";
998                                        } else {
999                                                $arrayConnectionTelAlternativo["connection_is_default"] = 'f';
1000                                        }
1001                                        $result = pg_insert($this->db, 'phpgw_cc_connections', $arrayConnectionTelAlternativo);
1002                                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
1003                                        $arrayContactConnection["id_contact"] = $id_contact;
1004                                        $arrayContactConnection["id_connection"] = $id_connection_tel_alternativo;
1005                                        $arrayContactConnection["id_typeof_contact_connection"] = 2;
1006                                        $result = pg_insert($this->db, 'phpgw_cc_contact_conns', $arrayContactConnection);
1007                                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
1008                                } else {
1009                                        $result = pg_update($this->db, 'phpgw_cc_connections', $arrayConnectionTelAlternativo, array('id_connection' => $id_connection_tel_alternativo));
1010                                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
1011                                }
1012
1013                        } elseif ($found_id_connection_tel_alternativo) {
1014                                $result = pg_delete($this->db, "phpgw_cc_contact_conns", array('id_contact' => $id_contact, 'id_connection' => $id_connection_tel_alternativo));
1015                                if ($result == FALSE) throw new Exception(pg_last_error($this->db));
1016                                $result = pg_delete($this->db, "phpgw_cc_contact_grps", array('id_connection' => $id_connection_tel_alternativo));
1017                                if ($result == FALSE) throw new Exception(pg_last_error($this->db));
1018                                $result = pg_delete($this->db, "phpgw_cc_connections", array('id_connection' => $id_connection_tel_alternativo));
1019                                if ($result == FALSE) throw new Exception(pg_last_error($this->db));
1020                        }
1021
1022                        //TODO : Permitir mais de um número de telefone para Trabalho, Celular e Casa. O Expresso ainda não suporta isso :(
1023
1024                        $result = pg_query($this->db,"COMMIT;");
1025                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
1026                        if (!$id) {
1027                                $id = $id_contact;
1028                        }
1029                } catch (Exception $e) {
1030                        debugLog("exception -> " . $e->getMessage() . " - ARQUIVO: " . $e->getFile() . " - LINHA: " . $e->getLine());
1031                        pg_query($this->db,"ROLLBACK;");
1032                        return false;
1033                }
1034                return $this->StatMessage($folderid, $id);
1035        }
1036
1037        function MoveMessage($folderid, $id, $newfolderid) {
1038                return false;
1039        }
1040
1041        // -----------------------------------
1042
1043        function getPath() {
1044                return str_replace('%u', $this->_user, VCARDDIR_DIR);
1045        }
1046
1047        function escape($data){
1048                if (is_array($data)) {
1049                        foreach ($data as $key => $val) {
1050                                $data[$key] = $this->escape($val);
1051                        }
1052                        return $data;
1053                }
1054                $data = str_replace("\r\n", "\n", $data);
1055                $data = str_replace("\r", "\n", $data);
1056                $data = str_replace(array('\\', ';', ',', "\n"), array('\\\\', '\\;', '\\,', '\\n'), $data);
1057                return $data;
1058        }
1059
1060        function unescape($data){
1061                $data = str_replace(array('\\\\', '\\;', '\\,', '\\n','\\N'),array('\\', ';', ',', "\n", "\n"),$data);
1062                return $data;
1063        }
1064
1065        function  removeAccents($data){
1066                $data = strtr($data,"ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÜÚÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùüúþÿ","aaaaaaaceeeeiiii noooooxouuutbaaaaaaaceeeeiiii nooooo/ouuuty");
1067                return $data;
1068        }
1069
1070        function truncateString($string, $size)
1071        {
1072                if(strlen($string) <= $size) return $string;
1073                else return substr($string, 0, $size - 1);
1074        }
1075
1076        function parseUser($id_user)
1077        {
1078                $pos = strripos($id_user, '\\');
1079                if ($pos === false) {
1080                        if (strlen($id_user) < 1) return false;
1081                        else return $id_user;
1082                } else {
1083                        if (strlen($id_user) <= $pos + 1) {
1084                                return false;
1085                        } else {
1086                                return substr(substr($id_user, $pos + 1), 0, strlen(substr($id_user, $pos + 1)));
1087                        }
1088                }
1089        }
1090
1091        // Authenticate user and return UIDNumber attribute on success
1092        function authUser($user, $pwd) {
1093                $ldap = new AuthLDAP();
1094                $uid_number = ($ldap->bind($user,$pwd));
1095                $ldap->disconnect();
1096                unset($ldap);
1097                if (! $uid_number) return false;
1098                return $uid_number;
1099        }
1100};
1101?>
Note: See TracBrowser for help on using the repository browser.