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