[5595] | 1 | <?php |
---|
| 2 | |
---|
| 3 | include_once("Expresso.php"); |
---|
| 4 | |
---|
| 5 | class Catalog extends Expresso { |
---|
[5954] | 6 | var $searchCharactersNumber; |
---|
[5889] | 7 | var $userId; |
---|
| 8 | |
---|
| 9 | public function __construct(){ |
---|
[5954] | 10 | $prefs = $GLOBALS['phpgw']->preferences->read(); |
---|
| 11 | $this-> searchCharactersNumber = $prefs['expressoMail']['search_characters_number']; |
---|
[5889] | 12 | } |
---|
[5595] | 13 | |
---|
[5889] | 14 | private function getUserId(){ |
---|
| 15 | $userProfile = $this->getUserProfile(); |
---|
[5954] | 16 | return $userProfile['contactID']; |
---|
[5889] | 17 | } |
---|
| 18 | |
---|
[5595] | 19 | public function getUserContacts($params){ |
---|
| 20 | if($this->isLoggedIn($params)) { |
---|
[5707] | 21 | $search = ""; |
---|
| 22 | if($params) { |
---|
| 23 | $search = trim($params['search']); |
---|
[5889] | 24 | if($search != "") { |
---|
[5707] | 25 | $search = mb_convert_encoding($search,"ISO_8859-1", "UTF8"); |
---|
| 26 | } |
---|
| 27 | |
---|
[5889] | 28 | if($params['contactID'] > 0 && $params['contactType'] == 1){ |
---|
| 29 | $query_contact = 'A.id_contact='.$params['contactID'].' and'; |
---|
[5707] | 30 | } |
---|
[5889] | 31 | elseif($params['contactType'] == 2){ |
---|
[5954] | 32 | if($this-> searchCharactersNumber <= strlen($search)) |
---|
[5889] | 33 | return $this->getGlobalContacts($search, $params['contactID']); |
---|
| 34 | else{ |
---|
[5954] | 35 | $this-> error = Errors::get(Errors::CATALOG_MIN_ARGUMENT_SEARCH); |
---|
| 36 | $this-> error = str_replace("%1",$this-> searchCharactersNumber,$this->error); |
---|
[5889] | 37 | return $this->getResponse(); |
---|
| 38 | } |
---|
| 39 | } |
---|
[5707] | 40 | } |
---|
[5687] | 41 | |
---|
[5694] | 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, '. |
---|
[5687] | 43 | 'phpgw_cc_contact_conns B, phpgw_cc_connections C where A.id_contact = B.id_contact and B.id_connection = C.id_connection '. |
---|
[5694] | 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)'; |
---|
[5595] | 46 | |
---|
[5681] | 47 | if (!$this->getDb()->query($query)) |
---|
| 48 | return false; |
---|
[5595] | 49 | |
---|
[5681] | 50 | $contacts = array(); |
---|
| 51 | while($this->getDb()->next_record()) { |
---|
[5687] | 52 | $row = $this->getDb()->row(); |
---|
| 53 | $id = $row['id_contact']; |
---|
[5699] | 54 | $contactType = ($row['id_typeof_contact_connection'] == 2 ? 'contactPhones' : 'contactMails'); |
---|
[5681] | 55 | |
---|
[5687] | 56 | if($contacts[$id] != null){ |
---|
[5694] | 57 | $contacts[$id][$contactType][] = $row['connection_value']; |
---|
[5687] | 58 | } |
---|
| 59 | else{ |
---|
| 60 | $contacts[$id] = array( |
---|
| 61 | 'contactID' => $row['id_contact'], |
---|
[5694] | 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), |
---|
[5687] | 70 | ); |
---|
[5707] | 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 | } |
---|
[5687] | 80 | } |
---|
[5681] | 81 | } |
---|
[5687] | 82 | $this->result = array ('contacts' => array_values($contacts)); |
---|
[5681] | 83 | } |
---|
[5687] | 84 | return $this->getResponse(); |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | public function getImagePicture($params){ |
---|
[5712] | 88 | if($this->isLoggedIn($params) && $params['contactID'] != null) { |
---|
[5687] | 89 | $contact = array(); |
---|
[5712] | 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 | } |
---|
[5687] | 104 | } |
---|
[5712] | 105 | // Global Catalog |
---|
| 106 | elseif($params['contactType'] == 2){ |
---|
[5714] | 107 | $photo = $this->getUserLdapPhoto(urldecode($params['contactID'])); |
---|
[5712] | 108 | $contact[] = array( |
---|
| 109 | 'contactID' => $params['contactID'], |
---|
| 110 | 'contactImagePicture' => ($photo != null ? base64_encode($photo[0]) : "") |
---|
| 111 | ); |
---|
| 112 | |
---|
| 113 | } |
---|
[5687] | 114 | $this->result = array ('contacts' => $contact); |
---|
| 115 | } |
---|
[5595] | 116 | return $this->getResponse(); |
---|
| 117 | } |
---|
[5707] | 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( |
---|
[5714] | 136 | "contactID" => urlencode($dn), |
---|
[5707] | 137 | "contactFirstName" => $givenName[0], |
---|
| 138 | "contactLastName" => $sn[0], |
---|
| 139 | "contactHasImagePicture" => $contactHasImagePicture |
---|
| 140 | ); |
---|
| 141 | } |
---|
| 142 | } |
---|
| 143 | } |
---|
| 144 | return false; |
---|
[5712] | 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){ |
---|
[5722] | 154 | $resource = @ldap_read($ds, $contactID, "phpgwaccounttype=u"); |
---|
| 155 | $n_entries = @ldap_count_entries($ds, $resource); |
---|
| 156 | |
---|
[5712] | 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; |
---|
[5707] | 167 | } |
---|
[5595] | 168 | |
---|
[5707] | 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. |
---|
[5712] | 185 | if(is_array($this->getUserLdapAttrs($result[$i]['mail']))) |
---|
| 186 | $contacts[$i] = array_merge($this->getUserLdapAttrs($result[$i]['mail']), $contacts[$i]); |
---|
[5707] | 187 | } |
---|
| 188 | } |
---|
| 189 | $this->result = array ('contacts' => $contacts); |
---|
[5595] | 190 | |
---|
| 191 | return $this->getResponse(); |
---|
| 192 | } |
---|
[5681] | 193 | } |
---|