2 require_once('inc/init.php');
5 $FIELD = preg_replace('/entry\[/','',$_REQUEST['field']);
6 $FIELD = preg_replace('/\W+/','',$FIELD);
8 if($_REQUEST['dn'] && $_REQUEST['addnote']){
9 ajax_addnote($_REQUEST['dn'],$_REQUEST['addnote']);
10 }elseif($_REQUEST['dn'] && $_REQUEST['settags']){
11 ajax_settags($_REQUEST['dn'],$_REQUEST['settags']);
12 }elseif($_REQUEST['dn'] && $_REQUEST['loadtags']){
13 ajax_loadtags($_REQUEST['dn'],$_REQUEST['loadtags']);
14 }elseif($FIELD == 'marker'||$FIELD == 'markers'){
15 ajax_taglookup($_REQUEST['value']);
17 ajax_lookup($FIELD,$_REQUEST['value']);
21 * Add a note to the existing notes
23 function ajax_addnote($dn,$note){
28 header('Content-Type: text/html; charset=utf-8');
30 // fetch the existing note
31 $result = ldap_search($LDAP_CON,$dn,'(objectClass=inetOrgPerson)',array($FIELDS['note']));
32 if(ldap_count_entries($LDAP_CON,$result)){
33 $result = ldap_get_binentries($LDAP_CON, $result);
35 $note = $note."\n\n".$result[0][$FIELDS['note']][0];
36 $note = preg_replace("!\n\n\n+!","\n\n",$note);
38 $entry[$FIELDS['note']] = $note;
39 ldap_modify($LDAP_CON,$dn,$entry);
42 require_once(dirname(__FILE__).'/inc/smarty/plugins/modifier.noteparser.php');
43 print smarty_modifier_noteparser($note);
47 * Set tags for a contact
49 function ajax_settags($dn,$tags){
53 if(!$FIELDS['_marker']) return;
55 header('Content-Type: text/html; charset=utf-8');
57 $tags = explode(',',$tags);
58 $tags = array_map('trim',$tags);
59 $tags = array_unique($tags);
60 $tags = array_diff($tags, array('')); //strip empty ones
62 $entry[$FIELDS['_marker']] = $tags;
63 ldap_mod_replace($LDAP_CON,$dn,$entry);
65 foreach ($tags as $tag){
66 print '<a href="index.php?marker=';
67 print rawurlencode($tag);
68 print '" class="tag">';
69 print htmlspecialchars($tag);
75 * Load current tags of an entry
77 function ajax_loadtags($dn,$type='plain'){
81 if(!$FIELDS['_marker']) return;
83 header('Content-Type: text/html; charset=utf-8');
85 $sr = ldap_search($LDAP_CON,$dn,'(objectClass=inetOrgPerson)',array($FIELDS['_marker']));
86 if(!ldap_count_entries($LDAP_CON,$sr)) return false;
87 $result = ldap_get_binentries($LDAP_CON, $sr);
91 echo join(', ',(array) $entry[$FIELDS['_marker']]);
93 foreach ((array) $entry[$FIELDS['_marker']] as $tag){
94 echo '<a href="index.php?marker=';
95 echo rawurlencode($tag);
96 echo '" class="tag">';
97 echo htmlspecialchars($tag);
104 * Find all tags (markers) starting with the given
107 function ajax_taglookup($tag){
108 header('Content-Type: text/xml; charset=utf-8');
112 if(!$FIELDS['_marker']) return;
114 $search = ldap_filterescape($tag);
115 $filter = "(&(objectClass=inetOrgPerson)(".$FIELDS['_marker']."=$search*))";
116 $result = ldap_queryabooks($filter,$FIELDS['_marker']);
118 if(!count($result)) return;
121 foreach ($result as $entry){
122 if(count($entry[$FIELDS['_marker']])){
123 foreach($entry[$FIELDS['_marker']] as $marker){
124 if(preg_match('/^'.preg_quote($tag,'/').'/i',$marker)){
125 array_push($tags, strtolower($marker));
131 $tags = array_unique($tags);
132 sort($tags,SORT_STRING);
134 echo '<?xml version="1.0"?>'.NL;
135 echo '<ajaxresponse>'.NL;
136 foreach($tags as $out){
138 echo '<value>'.htmlspecialchars($out).'</value>'.NL;
139 echo '<text>'.htmlspecialchars($out).'</text>'.NL;
142 echo '</ajaxresponse>'.NL;
146 * Do a simple lookup in any simple field
148 function ajax_lookup($field,$search){
149 header('Content-Type: text/xml; charset=utf-8');
154 if(!$FIELDS[$field]) return;
155 $field = $FIELDS[$field];
157 $search = ldap_filterescape($search);
158 $filter = "(&(objectClass=inetOrgPerson)($field=$search*))";
159 $result = ldap_queryabooks($filter,$field);
160 if(!count($result)) return;
163 foreach ($result as $entry){
164 if(isset($entry[$field]) && !empty($entry[$field])){
165 $items[] = $entry[$field][0];
169 $items = array_unique($items);
170 sort($items,SORT_STRING);
172 echo '<?xml version="1.0"?>'.NL;
173 echo '<ajaxresponse>'.NL;
174 foreach($items as $out){
176 echo '<value>'.htmlspecialchars($out).'</value>'.NL;
177 echo '<text>'.htmlspecialchars($out).'</text>'.NL;
180 echo '</ajaxresponse>'.NL;