2 require_once('init.php');
5 header('Content-Type: text/xml; charset=utf-8');
13 $FIELD = preg_replace('/entry\[/','',$_REQUEST['field']);
14 $FIELD = preg_replace('/\W+/','',$FIELD);
16 if($FIELD == 'marker'||$FIELD == 'markers'){
17 ajax_taglookup($_REQUEST['value']);
19 ajax_lookup($FIELD,$_REQUEST['value']);
23 if(!empty($_REQUEST['taglookup'])){
24 ajax_taglookup($_REQUEST['taglookup']);
25 }elseif(!empty($_REQUEST['lookup']) && !empty($_REQUEST['s'])){
26 ajax_lookup($_REQUEST['lookup'],$_REQUEST['s']);
27 }elseif(!empty($_REQUEST['addnote'])){
28 ajax_addnote($_REQUEST['addnote'],$_REQUEST['note']);
29 }elseif(!empty($_REQUEST['settags'])){
30 ajax_settags($_REQUEST['settags'],$_REQUEST['tags']);
36 * Add a note to the existing notes
38 function ajax_addnote($dn,$note){
43 // fetch the existing note
44 $result = ldap_search($LDAP_CON,$dn,'(objectClass=inetOrgPerson)',array($FIELDS['note']));
45 if(ldap_count_entries($LDAP_CON,$result)){
46 $result = ldap_get_binentries($LDAP_CON, $result);
48 $note = $note."\n\n".$result[0][$FIELDS['note']][0];
49 $note = preg_replace("!\n\n\n+!","\n\n",$note);
51 $entry[$FIELDS['note']] = $note;
52 ldap_modify($LDAP_CON,$dn,$entry);
55 require_once(dirname(__FILE__).'/smarty/plugins/modifier.noteparser.php');
56 print smarty_modifier_noteparser($note);
60 * Sett tags for a contact
62 function ajax_settags($dn,$tags){
66 if(!$FIELDS['_marker']) return;
68 $tags = explode(',',$tags);
69 $tags = array_map('trim',$tags);
70 $tags = array_unique($tags);
71 $tags = array_diff($tags, array('')); //strip empty ones
73 $entry[$FIELDS['_marker']] = $tags;
74 ldap_mod_replace($LDAP_CON,$dn,$entry);
76 foreach ($tags as $tag){
77 print '<a href="index.php?marker=';
78 print rawurlencode($tag);
79 print '" class="tag">';
80 print htmlspecialchars($tag);
86 * Find all tags (markers) starting with the given
89 function ajax_taglookup($tag){
93 if(!$FIELDS['_marker']) return;
95 $search = ldap_filterescape($tag);
96 $filter = "(&(objectClass=inetOrgPerson)(".$FIELDS['_marker']."=$search*))";
97 $result = ldap_queryabooks($filter,$FIELDS['_marker']);
99 if(!count($result)) return;
102 foreach ($result as $entry){
103 if(count($entry[$FIELDS['_marker']])){
104 foreach($entry[$FIELDS['_marker']] as $marker){
105 if(preg_match('/^'.preg_quote($tag,'/').'/i',$marker)){
106 array_push($tags, strtolower($marker));
112 $tags = array_unique($tags);
113 sort($tags,SORT_STRING);
115 echo '<?xml version="1.0"?>'.NL;
116 echo '<ajaxresponse>'.NL;
117 foreach($tags as $out){
119 echo '<value>'.htmlspecialchars($out).'</value>'.NL;
120 echo '<text>'.htmlspecialchars($out).'</text>'.NL;
123 echo '</ajaxresponse>'.NL;
127 * Do a simple lookup in any simple field
129 function ajax_lookup($field,$search){
134 if(!$FIELDS[$field]) return;
135 $field = $FIELDS[$field];
137 $search = ldap_filterescape($search);
138 $filter = "(&(objectClass=inetOrgPerson)($field=$search*))";
139 $result = ldap_queryabooks($filter,$field);
140 if(!count($result)) return;
143 foreach ($result as $entry){
144 if(isset($entry[$field]) && !empty($entry[$field])){
145 $items[] = $entry[$field][0];
149 $items = array_unique($items);
150 sort($items,SORT_STRING);
152 echo '<?xml version="1.0"?>'.NL;
153 echo '<ajaxresponse>'.NL;
154 foreach($items as $out){
156 echo '<value>'.htmlspecialchars($out).'</value>'.NL;
157 echo '<text>'.htmlspecialchars($out).'</text>'.NL;
160 echo '</ajaxresponse>'.NL;