source: sandbox/webservice/api/json-rpc/Catalog.php @ 5712

Revision 5712, 6.6 KB checked in by niltonneto, 12 years ago (diff)

Ticket #2507 - Adicionado em getImagePicture() busca para foto de usuário LDAP.

  • Property svn:executable set to *
Line 
1<?php
2
3include_once("Expresso.php");
4
5class Catalog extends Expresso {       
6       
7        public function getUserContacts($params){
8                if($this->isLoggedIn($params)) {
9                        $search = "";
10                        if($params) {
11                               
12                                $search = trim($params['search']);
13                                if($params['contactID'] > 0){
14                                        $query_contact = 'A.id_contact='.$params['contactID'].' and';
15                                }
16                                elseif($search != "") {
17                                        $search = mb_convert_encoding($search,"ISO_8859-1", "UTF8");
18                                }
19                               
20                                if(($search || $params['contactID']) && $params['contactType'] == 2){
21                                        return $this->getGlobalContacts($search, $params['contactID']);                                 
22                                }
23                        }
24                       
25                        $query = 'select B.id_typeof_contact_connection, A.photo, A.id_contact, A.alias, A.given_names, A.family_names, A.names_ordered, A.birthdate, A.notes, C.connection_value from phpgw_cc_contact A, '.
26                                'phpgw_cc_contact_conns B, phpgw_cc_connections C where A.id_contact = B.id_contact and B.id_connection = C.id_connection '.
27                                ' and '.$query_contact.' A.id_owner='.$this -> getUserId().' group by '.
28                                ' B.id_typeof_contact_connection, A.photo, A.id_contact, A.alias, A.given_names, A.family_names,A.names_ordered,A.birthdate, A.notes,C.connection_value order by lower(A.names_ordered)';
29                       
30                        if (!$this->getDb()->query($query))
31                                return false;
32                       
33                        $contacts = array();
34                        while($this->getDb()->next_record()) {
35                                $row = $this->getDb()->row();
36                                $id = $row['id_contact'];
37                                $contactType = ($row['id_typeof_contact_connection'] == 2 ? 'contactPhones' : 'contactMails');                         
38                               
39                                if($contacts[$id] != null){
40                                        $contacts[$id][$contactType][] = $row['connection_value'];
41                                }       
42                                else{
43                                        $contacts[$id] = array(
44                                                'contactID'             => $row['id_contact'],
45                                                $contactType    => array($row['connection_value']),                                             
46                                                'contactAlias' => ($row['alias'] != null ?  mb_convert_encoding($row['alias'],"UTF8", "ISO_8859-1") : ""),
47                                                'contactFirstName'      => ($row['given_names'] != null ?  mb_convert_encoding($row['given_names'],"UTF8", "ISO_8859-1") : ""),
48                                                'contactLastName'       => ($row['family_names'] != null ?  mb_convert_encoding($row['family_names'],"UTF8", "ISO_8859-1") : ""),
49                                                'contactFullName'       => ($row['names_ordered'] != null ? mb_convert_encoding($row['names_ordered'],"UTF8", "ISO_8859-1") : ""),
50                                                'contactBirthDate'      => ($row['birthdate'] != null ? $row['birthdate'] : ""),
51                                                'contactNotes'          => ($row['notes'] != null ?  mb_convert_encoding($row['notes'],"UTF8", "ISO_8859-1") : ""),
52                                                'contactHasImagePicture' => ($row['photo'] != null ? 1 : 0),
53                                        );
54                                        if($search != null &&
55                                                        stristr($contacts[$id]['contactAlias'], $search) == null &&
56                                                        stristr($contacts[$id]['contactFirstName'], $search) == null &&
57                                                        stristr($contacts[$id]['contactLastName'], $search) == null &&
58                                                        stristr($contacts[$id]['contactFullName'], $search) == null &&
59                                                        stristr($contacts[$id]['contactBirthDate'], $search) == null &&
60                                                        stristr($contacts[$id]['contactNotes'], $search) == null){
61                                                unset($contacts[$id]);
62                                        }
63                                }                               
64                        }
65                        $this->result = array ('contacts' => array_values($contacts));
66                }
67                return $this->getResponse();
68        }       
69       
70        public function getImagePicture($params){               
71                if($this->isLoggedIn($params) && $params['contactID'] != null) {
72                        $contact = array();
73                        // User Contact
74                        if($params['contactType'] == 1){
75                                $query = 'select A.id_contact, A.photo from phpgw_cc_contact A where A.id_contact='.$params['contactID'].' and A.id_owner='.$this -> getUserId();               
76                                if (!$this->getDb()->query($query))
77                                        return false;
78                                if($this->getDb()->next_record()) {
79                                        $row = $this->getDb()->row();
80                                        if($row['photo'] != null) {
81                                                $contact[] = array(
82                                                        'contactID'             => $row['id_contact'],
83                                                        'contactImagePicture'   => ($row['photo'] != null ? base64_encode($row['photo']) : "")
84                                                );
85                                        }
86                                }
87                        }
88                        // Global Catalog                       
89                        elseif($params['contactType'] == 2){
90                                $photo = $this->getUserLdapPhoto($params['contactID']);
91                                $contact[] = array(
92                                                'contactID'             => $params['contactID'],
93                                                'contactImagePicture'   => ($photo != null ? base64_encode($photo[0]) : "")
94                                );
95                               
96                        }                       
97                        $this->result = array ('contacts' => $contact);
98                }
99                return $this->getResponse();
100        }       
101
102        private function getUserLdapAttrs($mail)
103        {
104                $filter="(&(phpgwAccountType=u)(mail=".$mail."))";
105                $ldap_context = $_SESSION['phpgw_info']['expressomail']['ldap_server']['dn'];
106                $justthese = array("dn", 'jpegPhoto','givenName', 'sn');
107                $this->getLdapCatalog()->ldapConnect(true);
108                $ds = $this->getLdapCatalog()->ds;
109                if ($ds){
110                        $sr = @ldap_search($ds, $ldap_context, $filter, $justthese);   
111                        if ($sr) {
112                                $entry = ldap_first_entry($ds, $sr);
113                                if($entry) {                                                                   
114                                        $givenName = @ldap_get_values_len($ds, $entry, "givenname");
115                                        $sn = @ldap_get_values_len($ds, $entry, "sn");
116                                        $contactHasImagePicture = (@ldap_get_values_len($ds, $entry, "jpegphoto") ? 1 : 0);
117                                        $dn = ldap_get_dn($ds, $entry);
118                                        return array(
119                                                "contactID" => $dn,
120                                                "contactFirstName" => $givenName[0],
121                                                "contactLastName"       => $sn[0],
122                                                "contactHasImagePicture" => $contactHasImagePicture
123                                        );
124                                }
125                        }
126                }
127                return false;
128        }
129       
130        private function getUserLdapPhoto($contactID) {         
131                $ldap_context = $_SESSION['phpgw_info']['expressomail']['ldap_server']['dn'];
132                $justthese = array("dn", 'jpegPhoto','givenName', 'sn');
133                $this->getLdapCatalog()->ldapConnect(true);
134                $ds = $this->getLdapCatalog()->ds;             
135               
136                if ($ds){
137                        $resource = ldap_read($ds, $contactID, "phpgwaccounttype=u");
138                        $n_entries = ldap_count_entries($ds, $resource);
139                        error_log("entrou foto =".$n_entries);
140                        if ( $n_entries == 1) {                 
141                                $first_entry = ldap_first_entry($ds, $resource);
142                                $obj = ldap_get_attributes($ds, $first_entry);
143                               
144                                if($obj['jpegPhoto']){
145                                        return ldap_get_values_len( $ds, $first_entry, "jpegPhoto");
146                                }
147                        }                                                               
148                }
149                return false;
150        }       
151       
152        private function getGlobalContacts($search, $uidNumber){
153                $contacts = array();
154                $params = array ("search_for" => $search);
155                $result = $this->getLdapCatalog()->quicksearch($params);
156
157                foreach($result as $i => $row) {
158                        if(is_int($i)) {
159                                $contacts[$i] = array(
160                                        'contactMails'  => array($result[$i]['mail']),
161                                        'contactPhones' => array($result[$i]['phone']),
162                                        'contactAlias' => "",                                   
163                                        'contactFullName'       => ($result[$i]['cn'] != null ? mb_convert_encoding($row['cn'],"UTF8", "ISO_8859-1") : ""),
164                                        'contactBirthDate'      => "",
165                                        'contactNotes'          => ""
166                                );
167                                // Buscar atributos faltantes.
168                                if(is_array($this->getUserLdapAttrs($result[$i]['mail'])))
169                                        $contacts[$i] = array_merge($this->getUserLdapAttrs($result[$i]['mail']), $contacts[$i]);                               
170                        }
171                }
172                $this->result = array ('contacts' => $contacts);
173               
174                return $this->getResponse();
175        }
176}
Note: See TracBrowser for help on using the repository browser.