]> git.sur5r.net Git - contagged/blob - index.php
link map info to users
[contagged] / index.php
1 <?php
2
3   require_once('inc/init.php');
4   ldap_login();
5
6   // select entry template
7   if(!empty($_REQUEST['export']) && $_REQUEST['export'] == 'csv'){
8     $entrytpl = 'list_csv_entry.tpl';
9   }elseif(!empty($_REQUEST['export']) && $_REQUEST['export'] == 'map'){
10     $entrytpl = 'list_map_entry.tpl';
11   }else{
12     $entrytpl = 'list_entry.tpl';
13   }
14
15   // check which fields are needed
16   $fields = get_fields_from_template($entrytpl);
17
18
19   //prepare filter
20   $ldapfilter = _makeldapfilter();
21
22   // fetch results
23   $result = ldap_queryabooks($ldapfilter,$fields);
24
25   $list = '';
26   if(count($result)==1 && $_REQUEST['search']){
27     //only one result on a search -> display page
28     header("Location: entry.php?dn=".$result[0]['dn']);
29     exit;
30   }elseif(count($result)){
31     $keys = array_keys($result);
32     uksort($keys,"_namesort");
33     foreach($keys as $key){
34       tpl_entry($result[$key]);
35       $list .= $smarty->fetch($entrytpl);
36     }
37   }
38
39   //prepare templates
40   tpl_std();
41   if (empty($_REQUEST['filter'])) $_REQUEST['filter']='';
42   if (empty($_REQUEST['marker'])) $_REQUEST['marker']='';
43   if (empty($_REQUEST['search'])) $_REQUEST['search']='';
44   $smarty->assign('list',$list);
45   $smarty->assign('filter',$_REQUEST['filter']);
46   $smarty->assign('marker',$_REQUEST['marker']);
47   $smarty->assign('search',$_REQUEST['search']);
48   $smarty->assign('org',$_REQUEST['org']);
49   //display templates
50   if(!empty($_REQUEST['export'])){
51     if ($conf['userlogreq'] == 1 && $user == ''){
52       header("HTTP/1.1 401 ACCESS DENIED");
53       exit();
54     }
55
56     if($_REQUEST['export'] == 'csv'){
57       header("Content-Type: text/csv");
58       header('Content-Disposition: Attachement; filename="contagged_export.csv"');
59       $smarty->display('list_csv.tpl');
60       exit;
61     }elseif($_REQUEST['export'] == 'map'){
62       header('Content-Type: text/html; charset=utf-8');
63       $smarty->display('list_map.tpl');
64       exit;
65     }
66   }else{
67     //save location in session
68     $_SESSION['ldapab']['lastlocation']=$_SERVER["REQUEST_URI"];
69
70     header('Content-Type: text/html; charset=utf-8');
71     $smarty->display('list.tpl');
72   }
73
74   //------- functions -----------//
75
76   /**
77    * callback function to sort entries by name
78    * uses global $result
79    */
80   function _namesort($a,$b){
81     global $result;
82     global $FIELDS;
83     if (empty($result[$a][$FIELDS['givenname']])) { $result[$a][$FIELDS['givenname']]=''; }
84     if (empty($result[$b][$FIELDS['givenname']])) { $result[$b][$FIELDS['givenname']]=''; }
85     $x = $result[$a][$FIELDS['name']][0].$result[$a][$FIELDS['givenname']][0];
86     $y = $result[$b][$FIELDS['name']][0].$result[$b][$FIELDS['givenname']][0];
87     return(strcasecmp($x,$y));
88   }
89
90
91   /**
92    * Creates an LDAP filter from given request variables search or filter
93    */
94   function _makeldapfilter(){
95     global $FIELDS;
96
97     //handle given filter
98
99     if (empty($_REQUEST['filter'])) { $_REQUEST['filter']=''; }
100     if (empty($_REQUEST['search'])) { $_REQUEST['search']=''; }
101     if (empty($_REQUEST['org'])) { $_REQUEST['org']=''; }
102     if (empty($_REQUEST['marker'])) { $_REQUEST['marker']=''; }
103     $filter = ldap_filterescape($_REQUEST['filter']);
104     $search = ldap_filterescape($_REQUEST['search']);
105     $org    = ldap_filterescape($_REQUEST['org']);
106     $marker = ldap_filterescape($_REQUEST['marker']);
107     $_SESSION['ldapab']['filter'] = $_REQUEST['filter'];
108     if(empty($filter)) $filter='a';
109
110     if(!empty($marker)){
111       // Search by tag
112       $ldapfilter = '(&(objectClass=contactPerson)';
113       $marker = explode(',',$marker);
114       foreach($marker as $m){
115         $m = trim($m);
116         $ldapfilter .= '('.$FIELDS['_marker'].'='.$m.')';
117       }
118       $ldapfilter .= ')';
119     }elseif(!empty($search)){
120       // Search name and organization
121       $search = trim($search);
122       $words=preg_split('/\s+/',$search);
123       $filter='';
124       foreach($words as $word){
125         $filter .= '(|(|('.$FIELDS['name'].'=*'.$word.'*)('.
126                    $FIELDS['givenname'].'=*'.$word.'*))('.
127                    $FIELDS['organization'].'=*'.$word.'*))';
128       }
129       $ldapfilter = "(&(objectClass=inetOrgPerson)$filter)";
130     }elseif(!empty($org)){
131       // List organization members
132       $ldapfilter = '(&(objectClass=inetOrgPerson)('.$FIELDS['organization']."=$org))";
133     }elseif($filter=='other'){
134       // Alphabetic listing of last names
135       $other='';
136       for ($i=ord('a');$i<=ord('z');$i++){
137         $other .= '(!('.$FIELDS['name'].'='.chr($i).'*))';
138       }
139       $ldapfilter = "(&(objectClass=inetOrgPerson)$other)";
140     }elseif($filter=='\2a'){ //escaped asterisk
141       // List all
142       $ldapfilter = "(objectClass=inetOrgPerson)";
143     }else{
144       // Search by last name start
145       $ldapfilter = '(&(objectClass=inetOrgPerson)('.$FIELDS['name']."=$filter*))";
146     }
147     return $ldapfilter;
148   }
149 ?>