]> git.sur5r.net Git - contagged/blob - ajax.php
fix for removing tags through ajax
[contagged] / ajax.php
1 <?
2 require_once('init.php');
3 ldap_login();
4
5 header('Content-Type: text/html; charset=utf-8');
6
7 if($_REQUEST['taglookup']){
8   ajax_taglookup($_REQUEST['taglookup']);
9 }elseif($_REQUEST['addnote']){
10   ajax_addnote($_REQUEST['addnote'],$_REQUEST['note']);
11 }elseif($_REQUEST['settags']){
12   ajax_settags($_REQUEST['settags'],$_REQUEST['tags']);
13 }
14
15 /**
16  * Add a note to the existing notes
17  */
18 function ajax_addnote($dn,$note){
19   global $conf;
20   global $LDAP_CON;
21
22   // fetch the existing note
23   $result = ldap_search($LDAP_CON,$dn,'(objectClass=inetOrgPerson)',array('description'));
24   if(ldap_count_entries($LDAP_CON,$result)){
25     $result = ldap_get_binentries($LDAP_CON, $result);
26   }
27   $note = $note."\n\n".$result[0]['description'][0];
28   $note = preg_replace("!\n\n\n+!","\n\n",$note);
29
30   $entry['description'] = $note;
31   ldap_modify($LDAP_CON,$dn,$entry);
32
33
34   require_once(dirname(__FILE__).'/smarty/plugins/modifier.noteparser.php');
35   print smarty_modifier_noteparser($note);
36 }
37
38 /**
39  * Sett tags for a contact
40  */
41 function ajax_settags($dn,$tags){
42   global $conf;
43   global $LDAP_CON;
44   if(!$conf[extended]) return;
45
46   $tags = explode(',',$tags);
47   $tags = array_map('trim',$tags);
48   $tags = array_unique($tags);
49   $tags = array_diff($tags, array('')); //strip empty ones
50
51   $entry['marker'] = $tags;
52   ldap_mod_replace($LDAP_CON,$dn,$entry);
53
54   foreach ($tags as $tag){
55     print '<a href="index.php?marker=';
56     print rawurlencode($tag);
57     print '" class="tag">';
58     print htmlspecialchars($tag);
59     print '</a> ';
60   }
61 }
62
63 /**
64  * Find all tags (markers) starting with the given
65  * string
66  */
67 function ajax_taglookup($tag){
68   global $conf;
69   global $LDAP_CON;
70   if(!$conf[extended]) return;
71
72   $search = ldap_filterescape($tag);
73   $filter = "(&(objectClass=contactPerson)(marker=$search*))";
74   $result = ldap_queryabooks($filter,'marker');
75
76   if(!count($result)) return;
77
78   $tags = array();
79   foreach ($result as $entry){
80     if(count($entry['marker'])){
81       foreach($entry['marker'] as $marker){
82         if(preg_match('/^'.preg_quote($tag,'/').'/i',$marker)){
83           array_push($tags, strtolower($marker));
84         }
85       }
86     }
87   }
88
89   $tags = array_unique($tags);
90   sort($tags,SORT_STRING);
91
92   print '<ul>';
93   foreach($tags as $out){
94     print '<li>'.htmlspecialchars($out).'</li>';
95   }
96   print '</ul>';
97 }
98
99 ?>