2 require_once('inc/init.php');
5 if ($conf['userlogreq'] && !isset($_SESSION['ldapab']['username'])){
6 header("HTTP/1.1 401 Access Denied");
7 echo '<h1>Access Denied</h1>';
12 $FIELD = preg_replace('/entry\[/','',$_REQUEST['field']);
13 $FIELD = preg_replace('/\W+/','',$FIELD);
15 if($_REQUEST['dn'] && $_REQUEST['addnote']){
16 ajax_addnote($_REQUEST['dn'],$_REQUEST['addnote']);
17 }elseif($_REQUEST['dn'] && $_REQUEST['settags']){
18 ajax_settags($_REQUEST['dn'],$_REQUEST['settags']);
19 }elseif($_REQUEST['dn'] && $_REQUEST['loadtags']){
20 ajax_loadtags($_REQUEST['dn'],$_REQUEST['loadtags']);
21 }elseif($FIELD == 'marker'||$FIELD == 'markers'){
22 ajax_taglookup($_REQUEST['value']);
24 ajax_lookup($FIELD,$_REQUEST['value']);
28 * Add a note to the existing notes
30 function ajax_addnote($dn,$note){
35 header('Content-Type: text/html; charset=utf-8');
37 // fetch the existing note
38 $result = ldap_search($LDAP_CON,$dn,'(objectClass=inetOrgPerson)',array($FIELDS['note']));
39 if(ldap_count_entries($LDAP_CON,$result)){
40 $result = ldap_get_binentries($LDAP_CON, $result);
42 $note = $note."\n\n".$result[0][$FIELDS['note']][0];
43 $note = preg_replace("!\n\n\n+!","\n\n",$note);
45 $entry[$FIELDS['note']] = $note;
46 ldap_modify($LDAP_CON,$dn,$entry);
49 require_once(dirname(__FILE__).'/inc/smarty/plugins/modifier.noteparser.php');
50 print smarty_modifier_noteparser($note);
54 * Set tags for a contact
56 function ajax_settags($dn,$tags){
60 if(!$FIELDS['_marker']) return;
62 header('Content-Type: text/html; charset=utf-8');
64 $tags = explode(',',$tags);
65 $tags = array_map('trim',$tags);
66 $tags = array_unique($tags);
67 $tags = array_diff($tags, array('')); //strip empty ones
69 $entry[$FIELDS['_marker']] = $tags;
70 ldap_mod_replace($LDAP_CON,$dn,$entry);
72 foreach ($tags as $tag){
73 print '<a href="index.php?marker=';
74 print rawurlencode($tag);
75 print '" class="tag">';
76 print htmlspecialchars($tag);
82 * Load current tags of an entry
84 function ajax_loadtags($dn,$type='plain'){
88 if(!$FIELDS['_marker']) return;
90 header('Content-Type: text/html; charset=utf-8');
92 $sr = ldap_search($LDAP_CON,$dn,'(objectClass=inetOrgPerson)',array($FIELDS['_marker']));
93 if(!ldap_count_entries($LDAP_CON,$sr)) return false;
94 $result = ldap_get_binentries($LDAP_CON, $sr);
98 echo join(', ',(array) $entry[$FIELDS['_marker']]);
100 foreach ((array) $entry[$FIELDS['_marker']] as $tag){
101 echo '<a href="index.php?marker=';
102 echo rawurlencode($tag);
103 echo '" class="tag">';
104 echo htmlspecialchars($tag);
111 * Find all tags (markers) starting with the given
114 function ajax_taglookup($tag){
115 header('Content-Type: text/xml; charset=utf-8');
119 if(!$FIELDS['_marker']) return;
121 $search = ldap_filterescape($tag);
122 $filter = "(&(objectClass=inetOrgPerson)(".$FIELDS['_marker']."=$search*))";
123 $result = ldap_queryabooks($filter,$FIELDS['_marker']);
125 if(!count($result)) return;
128 foreach ($result as $entry){
129 if(count($entry[$FIELDS['_marker']])){
130 foreach($entry[$FIELDS['_marker']] as $marker){
131 if(preg_match('/^'.preg_quote($tag,'/').'/i',$marker)){
132 array_push($tags, strtolower($marker));
138 $tags = array_unique($tags);
139 sort($tags,SORT_STRING);
141 echo '<?xml version="1.0"?>'.NL;
142 echo '<ajaxresponse>'.NL;
143 foreach($tags as $out){
145 echo '<value>'.htmlspecialchars($out).'</value>'.NL;
146 echo '<text>'.htmlspecialchars($out).'</text>'.NL;
149 echo '</ajaxresponse>'.NL;
153 * Do a simple lookup in any simple field
155 function ajax_lookup($field,$search){
156 header('Content-Type: text/xml; charset=utf-8');
161 if(!$FIELDS[$field]) return;
162 $field = $FIELDS[$field];
164 $search = ldap_filterescape($search);
165 $filter = "(&(objectClass=inetOrgPerson)($field=$search*))";
166 $result = ldap_queryabooks($filter,$field);
167 if(!count($result)) return;
170 foreach ($result as $entry){
171 if(isset($entry[$field]) && !empty($entry[$field])){
172 $items[] = $entry[$field][0];
176 $items = array_unique($items);
177 sort($items,SORT_STRING);
179 echo '<?xml version="1.0"?>'.NL;
180 echo '<ajaxresponse>'.NL;
181 foreach($items as $out){
183 echo '<value>'.htmlspecialchars($out).'</value>'.NL;
184 echo '<text>'.htmlspecialchars($out).'</text>'.NL;
187 echo '</ajaxresponse>'.NL;