]> git.sur5r.net Git - contagged/blob - ajax.php
JavaScript updates (migration to JQuery + Interface)
[contagged] / ajax.php
1 <?
2 require_once('init.php');
3 ldap_login();
4
5 header('Content-Type: text/xml; charset=utf-8');
6
7 /*
8 echo '<bla><[!CDATA[';
9 print_r($_REQUEST);
10 echo ']]></bla>';
11 */
12
13 $FIELD = preg_replace('/entry\[/','',$_REQUEST['field']);
14 $FIELD = preg_replace('/\W+/','',$FIELD);
15
16 if($FIELD == 'marker'||$FIELD == 'markers'){
17   ajax_taglookup($_REQUEST['value']);
18 }else{
19   ajax_lookup($FIELD,$_REQUEST['value']);
20 }
21
22 /*
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']);
31 }
32 */
33
34
35 /**
36  * Add a note to the existing notes
37  */
38 function ajax_addnote($dn,$note){
39   global $conf;
40   global $LDAP_CON;
41   global $FIELDS;
42
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);
47   }
48   $note = $note."\n\n".$result[0][$FIELDS['note']][0];
49   $note = preg_replace("!\n\n\n+!","\n\n",$note);
50
51   $entry[$FIELDS['note']] = $note;
52   ldap_modify($LDAP_CON,$dn,$entry);
53
54
55   require_once(dirname(__FILE__).'/smarty/plugins/modifier.noteparser.php');
56   print smarty_modifier_noteparser($note);
57 }
58
59 /**
60  * Sett tags for a contact
61  */
62 function ajax_settags($dn,$tags){
63   global $conf;
64   global $LDAP_CON;
65   global $FIELDS;
66   if(!$FIELDS['_marker']) return;
67
68   $tags = explode(',',$tags);
69   $tags = array_map('trim',$tags);
70   $tags = array_unique($tags);
71   $tags = array_diff($tags, array('')); //strip empty ones
72
73   $entry[$FIELDS['_marker']] = $tags;
74   ldap_mod_replace($LDAP_CON,$dn,$entry);
75
76   foreach ($tags as $tag){
77     print '<a href="index.php?marker=';
78     print rawurlencode($tag);
79     print '" class="tag">';
80     print htmlspecialchars($tag);
81     print '</a> ';
82   }
83 }
84
85 /**
86  * Find all tags (markers) starting with the given
87  * string
88  */
89 function ajax_taglookup($tag){
90   global $conf;
91   global $LDAP_CON;
92   global $FIELDS;
93   if(!$FIELDS['_marker']) return;
94
95   $search = ldap_filterescape($tag);
96   $filter = "(&(objectClass=inetOrgPerson)(".$FIELDS['_marker']."=$search*))";
97   $result = ldap_queryabooks($filter,$FIELDS['_marker']);
98
99   if(!count($result)) return;
100
101   $tags = array();
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));
107         }
108       }
109     }
110   }
111
112   $tags = array_unique($tags);
113   sort($tags,SORT_STRING);
114
115   echo '<?xml version="1.0"?>'.NL;
116   echo '<ajaxresponse>'.NL;
117   foreach($tags as $out){
118     echo '<item>'.NL;
119     echo '<value>'.htmlspecialchars($out).'</value>'.NL;
120     echo '<text>'.htmlspecialchars($out).'</text>'.NL;
121     echo '</item>'.NL;
122   }
123   echo '</ajaxresponse>'.NL;
124 }
125
126 /**
127  * Do a simple lookup in any simple field
128  */
129 function ajax_lookup($field,$search){
130     global $conf;
131     global $LDAP_CON;
132     global $FIELDS;
133
134     if(!$FIELDS[$field]) return;
135     $field = $FIELDS[$field];
136
137     $search = ldap_filterescape($search);
138     $filter = "(&(objectClass=inetOrgPerson)($field=$search*))";
139     $result = ldap_queryabooks($filter,$field);
140     if(!count($result)) return;
141
142     $items = array();
143     foreach ($result as $entry){
144         if(isset($entry[$field]) && !empty($entry[$field])){
145             $items[] = $entry[$field][0];
146         }
147     }
148
149     $items = array_unique($items);
150     sort($items,SORT_STRING);
151
152     echo '<?xml version="1.0"?>'.NL;
153     echo '<ajaxresponse>'.NL;
154     foreach($items as $out){
155         echo '<item>'.NL;
156         echo '<value>'.htmlspecialchars($out).'</value>'.NL;
157         echo '<text>'.htmlspecialchars($out).'</text>'.NL;
158         echo '</item>'.NL;
159     }
160     echo '</ajaxresponse>'.NL;
161 }
162
163 ?>