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

Revision 5889, 7.1 KB checked in by niltonneto, 12 years ago (diff)

Ticket #2507 - Implementado métodos getUserProfile() e getUserPreferences().

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