]> git.sur5r.net Git - contagged/blob - ajax.php
Merge pull request #15 from cweiske/master
[contagged] / ajax.php
1 <?php
2 require_once('inc/init.php');
3 ldap_login();
4
5 if ($conf['userlogreq'] && !isset($_SESSION['ldapab']['username'])){
6   header("HTTP/1.1 401 Access Denied");
7   echo '<h1>Access Denied</h1>';
8   exit();
9 }
10
11
12 $FIELD = preg_replace('/entry\[/','',$_REQUEST['field']);
13 $FIELD = preg_replace('/\W+/','',$FIELD);
14
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']);
23 }else{
24   ajax_lookup($FIELD,$_REQUEST['value']);
25 }
26
27 /**
28  * Add a note to the existing notes
29  */
30 function ajax_addnote($dn,$note){
31   global $conf;
32   global $LDAP_CON;
33   global $FIELDS;
34
35   header('Content-Type: text/html; charset=utf-8');
36
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);
41   }
42   $note = $note."\n\n".$result[0][$FIELDS['note']][0];
43   $note = preg_replace("!\n\n\n+!","\n\n",$note);
44
45   $entry[$FIELDS['note']] = $note;
46   ldap_modify($LDAP_CON,$dn,$entry);
47
48
49   require_once(dirname(__FILE__).'/inc/smarty/plugins/modifier.noteparser.php');
50   print smarty_modifier_noteparser($note);
51 }
52
53 /**
54  * Set tags for a contact
55  */
56 function ajax_settags($dn,$tags){
57   global $conf;
58   global $LDAP_CON;
59   global $FIELDS;
60   if(!$FIELDS['_marker']) return;
61
62   header('Content-Type: text/html; charset=utf-8');
63
64   $tags = explode(',',$tags);
65   $tags = array_map('trim',$tags);
66   $tags = array_unique($tags);
67   $tags = array_diff($tags, array('')); //strip empty ones
68
69   $entry[$FIELDS['_marker']] = $tags;
70   ldap_mod_replace($LDAP_CON,$dn,$entry);
71
72   foreach ($tags as $tag){
73     print '<a href="index.php?marker=';
74     print rawurlencode($tag);
75     print '" class="tag">';
76     print htmlspecialchars($tag);
77     print '</a> ';
78   }
79 }
80
81 /**
82  * Load current tags of an entry
83  */
84 function ajax_loadtags($dn,$type='plain'){
85   global $conf;
86   global $LDAP_CON;
87   global $FIELDS;
88   if(!$FIELDS['_marker']) return;
89
90   header('Content-Type: text/html; charset=utf-8');
91
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);
95   $entry  = $result[0];
96
97   if($type == 'plain'){
98     echo join(', ',(array) $entry[$FIELDS['_marker']]);
99   }else{
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);
105       echo '</a> ';
106     }
107   }
108 }
109
110 /**
111  * Find all tags (markers) starting with the given
112  * string
113  */
114 function ajax_taglookup($tag){
115   header('Content-Type: text/xml; charset=utf-8');
116   global $conf;
117   global $LDAP_CON;
118   global $FIELDS;
119   if(!$FIELDS['_marker']) return;
120
121   $search = ldap_filterescape($tag);
122   $filter = "(&(objectClass=inetOrgPerson)(".$FIELDS['_marker']."=$search*))";
123   $result = ldap_queryabooks($filter,$FIELDS['_marker']);
124
125   if(!count($result)) return;
126
127   $tags = array();
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));
133         }
134       }
135     }
136   }
137
138   $tags = array_unique($tags);
139   sort($tags,SORT_STRING);
140
141   echo '<?xml version="1.0"?>'.NL;
142   echo '<ajaxresponse>'.NL;
143   foreach($tags as $out){
144     echo '<item>'.NL;
145     echo '<value>'.htmlspecialchars($out).'</value>'.NL;
146     echo '<text>'.htmlspecialchars($out).'</text>'.NL;
147     echo '</item>'.NL;
148   }
149   echo '</ajaxresponse>'.NL;
150 }
151
152 /**
153  * Do a simple lookup in any simple field
154  */
155 function ajax_lookup($field,$search){
156     header('Content-Type: text/xml; charset=utf-8');
157     global $conf;
158     global $LDAP_CON;
159     global $FIELDS;
160
161     if(!$FIELDS[$field]) return;
162     $field = $FIELDS[$field];
163
164     $search = ldap_filterescape($search);
165     $filter = "(&(objectClass=inetOrgPerson)($field=$search*))";
166     $result = ldap_queryabooks($filter,$field);
167     if(!count($result)) return;
168
169     $items = array();
170     foreach ($result as $entry){
171         if(isset($entry[$field]) && !empty($entry[$field])){
172             $items[] = $entry[$field][0];
173         }
174     }
175
176     $items = array_unique($items);
177     sort($items,SORT_STRING);
178
179     echo '<?xml version="1.0"?>'.NL;
180     echo '<ajaxresponse>'.NL;
181     foreach($items as $out){
182         echo '<item>'.NL;
183         echo '<value>'.htmlspecialchars($out).'</value>'.NL;
184         echo '<text>'.htmlspecialchars($out).'</text>'.NL;
185         echo '</item>'.NL;
186     }
187     echo '</ajaxresponse>'.NL;
188 }
189
190 ?>