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

Revision 5954, 7.2 KB checked in by niltonneto, 12 years ago (diff)

Ticket #2507 - Corrigido método getUserId(). Renomeado classe http_request.

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