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