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