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