]> git.sur5r.net Git - contagged/blob - ajax.php
correct URL encoding on search redirects
[contagged] / ajax.php
1 <?
2 require_once('inc/init.php');
3 ldap_login();
4
5 $FIELD = preg_replace('/entry\[/','',$_REQUEST['field']);
6 $FIELD = preg_replace('/\W+/','',$FIELD);
7
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']);
16 }else{
17   ajax_lookup($FIELD,$_REQUEST['value']);
18 }
19
20 /**
21  * Add a note to the existing notes
22  */
23 function ajax_addnote($dn,$note){
24   global $conf;
25   global $LDAP_CON;
26   global $FIELDS;
27
28   header('Content-Type: text/html; charset=utf-8');
29
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);
34   }
35   $note = $note."\n\n".$result[0][$FIELDS['note']][0];
36   $note = preg_replace("!\n\n\n+!","\n\n",$note);
37
38   $entry[$FIELDS['note']] = $note;
39   ldap_modify($LDAP_CON,$dn,$entry);
40
41
42   require_once(dirname(__FILE__).'/inc/smarty/plugins/modifier.noteparser.php');
43   print smarty_modifier_noteparser($note);
44 }
45
46 /**
47  * Set tags for a contact
48  */
49 function ajax_settags($dn,$tags){
50   global $conf;
51   global $LDAP_CON;
52   global $FIELDS;
53   if(!$FIELDS['_marker']) return;
54
55   header('Content-Type: text/html; charset=utf-8');
56
57   $tags = explode(',',$tags);
58   $tags = array_map('trim',$tags);
59   $tags = array_unique($tags);
60   $tags = array_diff($tags, array('')); //strip empty ones
61
62   $entry[$FIELDS['_marker']] = $tags;
63   ldap_mod_replace($LDAP_CON,$dn,$entry);
64
65   foreach ($tags as $tag){
66     print '<a href="index.php?marker=';
67     print rawurlencode($tag);
68     print '" class="tag">';
69     print htmlspecialchars($tag);
70     print '</a> ';
71   }
72 }
73
74 /**
75  * Load current tags of an entry
76  */
77 function ajax_loadtags($dn,$type='plain'){
78   global $conf;
79   global $LDAP_CON;
80   global $FIELDS;
81   if(!$FIELDS['_marker']) return;
82
83   header('Content-Type: text/html; charset=utf-8');
84
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);
88   $entry  = $result[0];
89
90   if($type == 'plain'){
91     echo join(', ',(array) $entry[$FIELDS['_marker']]);
92   }else{
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);
98       echo '</a> ';
99     }
100   }
101 }
102
103 /**
104  * Find all tags (markers) starting with the given
105  * string
106  */
107 function ajax_taglookup($tag){
108   header('Content-Type: text/xml; charset=utf-8');
109   global $conf;
110   global $LDAP_CON;
111   global $FIELDS;
112   if(!$FIELDS['_marker']) return;
113
114   $search = ldap_filterescape($tag);
115   $filter = "(&(objectClass=inetOrgPerson)(".$FIELDS['_marker']."=$search*))";
116   $result = ldap_queryabooks($filter,$FIELDS['_marker']);
117
118   if(!count($result)) return;
119
120   $tags = array();
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));
126         }
127       }
128     }
129   }
130
131   $tags = array_unique($tags);
132   sort($tags,SORT_STRING);
133
134   echo '<?xml version="1.0"?>'.NL;
135   echo '<ajaxresponse>'.NL;
136   foreach($tags as $out){
137     echo '<item>'.NL;
138     echo '<value>'.htmlspecialchars($out).'</value>'.NL;
139     echo '<text>'.htmlspecialchars($out).'</text>'.NL;
140     echo '</item>'.NL;
141   }
142   echo '</ajaxresponse>'.NL;
143 }
144
145 /**
146  * Do a simple lookup in any simple field
147  */
148 function ajax_lookup($field,$search){
149     header('Content-Type: text/xml; charset=utf-8');
150     global $conf;
151     global $LDAP_CON;
152     global $FIELDS;
153
154     if(!$FIELDS[$field]) return;
155     $field = $FIELDS[$field];
156
157     $search = ldap_filterescape($search);
158     $filter = "(&(objectClass=inetOrgPerson)($field=$search*))";
159     $result = ldap_queryabooks($filter,$field);
160     if(!count($result)) return;
161
162     $items = array();
163     foreach ($result as $entry){
164         if(isset($entry[$field]) && !empty($entry[$field])){
165             $items[] = $entry[$field][0];
166         }
167     }
168
169     $items = array_unique($items);
170     sort($items,SORT_STRING);
171
172     echo '<?xml version="1.0"?>'.NL;
173     echo '<ajaxresponse>'.NL;
174     foreach($items as $out){
175         echo '<item>'.NL;
176         echo '<value>'.htmlspecialchars($out).'</value>'.NL;
177         echo '<text>'.htmlspecialchars($out).'</text>'.NL;
178         echo '</item>'.NL;
179     }
180     echo '</ajaxresponse>'.NL;
181 }
182
183 ?>