@@ -69,64 +69,35 @@ public function process() {
6969 list ($ success , $ form ) = $ this ->process_form (array ('contact_type ' , 'contact_source ' , 'contact_id ' ));
7070 if ($ success && $ form ['contact_type ' ] == 'ldap ' && in_array ($ form ['contact_source ' ], $ sources , true )) {
7171 $ config = $ ldap_config [$ form ['contact_source ' ]];
72- $ contact = $ contacts ->get ($ form ['contact_id ' ]);
73- if (!$ contact ) {
74- $ all_contacts = $ contacts ->dump ();
75- $ found_contact = null ;
76- $ target_dn = null ;
77-
78- if (isset ($ this ->request ->post ['ldap_dn ' ]) && !empty ($ this ->request ->post ['ldap_dn ' ])) {
79- $ target_dn = $ this ->request ->post ['ldap_dn ' ];
80- }
81-
82- if (!$ target_dn && isset ($ this ->request ->get ['dn ' ])) {
83- $ target_dn = $ this ->request ->get ['dn ' ];
84- }
85-
86- foreach ($ all_contacts as $ contact_id => $ contact_obj ) {
87- if ($ contact_obj ->value ('source ' ) == $ form ['contact_source ' ] &&
88- $ contact_obj ->value ('type ' ) == 'ldap ' ) {
89-
90- $ all_fields = $ contact_obj ->value ('all_fields ' );
91-
92- if ($ target_dn && isset ($ all_fields ['dn ' ]) && $ all_fields ['dn ' ] === $ target_dn ) {
93- $ found_contact = $ contact_obj ;
94- break ;
95- }
96- }
97- }
98-
99- if (!$ found_contact ) {
100- $ ldap_contacts = array ();
101- foreach ($ all_contacts as $ contact_id => $ contact_obj ) {
102- if ($ contact_obj ->value ('source ' ) == $ form ['contact_source ' ] &&
103- $ contact_obj ->value ('type ' ) == 'ldap ' ) {
104- $ ldap_contacts [] = array ('id ' => $ contact_id , 'obj ' => $ contact_obj );
105- }
106- }
107-
108- if (count ($ ldap_contacts ) == 1 ) {
109- $ found_contact = $ ldap_contacts [0 ]['obj ' ];
110- }
111- }
112-
113- if (!$ found_contact ) {
114- Hm_Msgs::add ('Unable to find contact to delete ' , 'danger ' );
115- return ;
116- }
117- $ contact = $ found_contact ;
72+
73+ // For LDAP contacts, skip contact ID lookup and use DN-based lookup directly
74+ $ contacts = $ this ->get ('contact_store ' );
75+ $ dn = null ;
76+ $ contact = null ;
77+
78+ // Get DN from request data
79+ $ target_dn = null ;
80+ if (isset ($ this ->request ->post ['ldap_dn ' ]) && !empty ($ this ->request ->post ['ldap_dn ' ])) {
81+ $ target_dn = $ this ->request ->post ['ldap_dn ' ];
82+ } elseif (isset ($ this ->request ->get ['dn ' ])) {
83+ $ target_dn = Hm_LDAP_Contact::decodeDN ($ this ->request ->get ['dn ' ]);
84+ } elseif (isset ($ this ->request ->post ['dn ' ])) {
85+ $ target_dn = Hm_LDAP_Contact::decodeDN ($ this ->request ->post ['dn ' ]);
86+ }
87+
88+ if ($ target_dn ) {
89+ $ contact = Hm_LDAP_Contact::findByDN ($ contacts , $ target_dn , $ form ['contact_source ' ]);
90+ $ dn = $ target_dn ;
91+ }
92+
93+ if (!$ contact || !$ dn ) {
94+ Hm_Msgs::add ('Unable to find contact to delete ' , 'danger ' );
95+ return ;
11896 }
11997
12098 $ ldap = new Hm_LDAP_Contacts ($ config );
12199 if ($ ldap ->connect ()) {
122- $ flds = $ contact ->value ('all_fields ' );
123-
124- if (!$ flds || !isset ($ flds ['dn ' ]) || empty ($ flds ['dn ' ])) {
125- Hm_Msgs::add ('Contact DN not found, cannot delete ' , 'danger ' );
126- return ;
127- }
128-
129- if ($ ldap ->delete ($ flds ['dn ' ])) {
100+ if ($ ldap ->delete ($ dn )) {
130101 Hm_Msgs::add ('Contact Deleted ' );
131102 $ this ->out ('contact_deleted ' , 1 );
132103 }
@@ -383,40 +354,22 @@ public function process() {
383354 array_key_exists ('contact_id ' , $ this ->request ->get )) {
384355
385356 $ contacts = $ this ->get ('contact_store ' );
386- $ contact = $ contacts ->get ($ this ->request ->get ['contact_id ' ]);
387-
388357 $ contact_id = $ this ->request ->get ['contact_id ' ];
389358 $ contact_source = $ this ->request ->get ['contact_source ' ];
390- $ target_dn = isset ( $ this -> request -> get [ ' dn ' ]) ? urldecode ( $ this -> request -> get [ ' dn ' ]) : null ;
391-
392- if (! $ contact ) {
393- $ all_contacts = $ contacts -> dump ();
394- $ found_contact = null ;
359+
360+ $ target_dn = isset ( $ this -> request -> get [ ' dn ' ]) ? Hm_LDAP_Contact:: decodeDN ( $ this -> request -> get [ ' dn ' ]) : null ;
361+
362+ if ( $ target_dn ) {
363+ $ contact = Hm_LDAP_Contact:: findByDN ( $ contacts , $ target_dn , $ contact_source ) ;
395364
396- if ($ target_dn ) {
397- foreach ($ all_contacts as $ contact_id_key => $ contact_obj ) {
398- if ($ contact_obj ->value ('source ' ) == $ contact_source &&
399- $ contact_obj ->value ('type ' ) == 'ldap ' ) {
400-
401- $ all_fields = $ contact_obj ->value ('all_fields ' );
402-
403- if (isset ($ all_fields ['dn ' ]) && $ all_fields ['dn ' ] === $ target_dn ) {
404- $ found_contact = $ contact_obj ;
405- break ;
406- }
407- }
408- }
365+ if ($ contact ) {
366+ $ current = $ contact ->export ();
367+ $ current ['id ' ] = $ contact_id ;
368+ $ this ->out ('current_ldap_contact ' , $ current );
369+ $ this ->handler_response ['ldap_edit ' ] = true ;
409370 }
410-
411- $ contact = $ found_contact ;
412371 }
413372
414- if (is_object ($ contact )) {
415- $ current = $ contact ->export ();
416- $ current ['id ' ] = $ contact_id ;
417- $ this ->out ('current_ldap_contact ' , $ current );
418- $ this ->handler_response ['ldap_edit ' ] = true ;
419- }
420373 } else {
421374 $ missing = array ();
422375 if (!array_key_exists ('contact_source ' , $ this ->request ->get )) $ missing [] = 'contact_source ' ;
@@ -482,6 +435,34 @@ public function process() {
482435 }
483436}
484437
438+ /**
439+ * @subpackage ldap_contacts/handler
440+ */
441+ class Hm_Handler_ldap_send_to_contact extends Hm_Handler_Module {
442+ public function process () {
443+ if (!$ this ->get ('compose_draft ' ) &&
444+ array_key_exists ('contact_id ' , $ this ->request ->get ) &&
445+ array_key_exists ('contact_type ' , $ this ->request ->get ) &&
446+ array_key_exists ('contact_source ' , $ this ->request ->get ) &&
447+ array_key_exists ('dn ' , $ this ->request ->get ) &&
448+ $ this ->request ->get ['contact_type ' ] == 'ldap ' ) {
449+
450+ $ contacts = $ this ->get ('contact_store ' );
451+ $ contact_id = $ this ->request ->get ['contact_id ' ];
452+ $ contact_source = $ this ->request ->get ['contact_source ' ];
453+
454+ // For LDAP contacts, use DN-based lookup directly (contact_id is unreliable for LDAP)
455+ $ target_dn = Hm_LDAP_Contact::decodeDN ($ this ->request ->get ['dn ' ]);
456+ $ contact = Hm_LDAP_Contact::findByDN ($ contacts , $ target_dn , $ contact_source );
457+
458+ if ($ contact ) {
459+ $ to = sprintf ('%s <%s> ' , $ contact ->value ('display_name ' ), $ contact ->value ('email_address ' ));
460+ $ this ->out ('compose_draft ' , array ('draft_to ' => $ to , 'draft_subject ' => '' , 'draft_body ' => '' ));
461+ }
462+ }
463+ }
464+ }
465+
485466/**
486467 * @subpackage ldap_contacts/output
487468 */
0 commit comments