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