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