]> git.sur5r.net Git - contagged/blob - index.php
CSV export
[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   }
18   
19   $result = array_merge($result1,$result2);
20
21   // select entry template
22   if($_REQUEST['export'] == 'csv'){
23     $entrytpl = 'export_list_csv_entry.tpl';
24   }else{
25     $entrytpl = 'list_entry.tpl';
26   }
27
28   $list = '';
29   if(count($result)==1 && $_REQUEST[search]){
30     //only one result on a search -> display page
31     header("Location: entry.php?dn=".$result[0][dn]);
32     exit;
33   }elseif(count($result)){
34     $keys = array_keys($result);
35     uksort($keys,"_namesort");
36     foreach($keys as $key){
37       tpl_entry($result[$key]);
38       $list .= $smarty->fetch($entrytpl);
39     }
40   }
41
42   //prepare templates
43   tpl_std();
44   tpl_markers();
45   $smarty->assign('list',$list);
46   $smarty->assign('filter',$_REQUEST['filter']);
47   //display templates
48   if($_REQUEST['export'] == 'csv'){
49     header("Content-Type: text/csv");
50     header('Content-Disposition: Attachement; filename="ldapabexport.csv"');
51     $smarty->display('export_list_csv.tpl');
52   }else{
53     //save location in session
54     $_SESSION[ldapab][lastlocation]=$_SERVER["REQUEST_URI"];
55     $smarty->display('header.tpl');
56     $smarty->display('list_filter.tpl');
57     $smarty->display('list.tpl');
58     $smarty->display('footer.tpl');
59   }
60
61   //------- functions -----------//
62
63   /**
64    * callback function to sort entries by name
65    * uses global $result
66    */
67   function _namesort($a,$b){
68     global $result;
69     $x = $result[$a][sn][0].$result[$a][givenName][0];
70     $y = $result[$b][sn][0].$result[$b][givenName][0];
71     return(strcasecmp($x,$y));
72   }
73
74
75   /**
76    * Creates an LDAP filter from given request variables search or filter
77    */
78   function _makeldapfilter(){
79     //handle given filter
80
81     $filter = $_REQUEST['filter'];
82     $search = $_REQUEST['search'];
83     $org    = $_REQUEST['org'];
84     $marker = $_REQUEST['marker'];
85     $_SESSION[ldapab][filter] = $filter;
86     if(empty($filter)) $filter='a';
87
88     if(!empty($marker)){
89       $marker = utf8_encode($marker);
90       $ldapfilter = "(&(objectClass=contactPerson)(marker=$marker))";
91     }elseif(!empty($search)){
92       $search = trim($search);
93       $words=preg_split('/\s+/',$search);
94       $filter='';
95       foreach($words as $word){
96         $word = utf8_encode($word);
97         $filter .= "(|(|(sn=*$word*)(givenName=*$word*))(o=*$word*))";
98       }
99       $ldapfilter = "(&(objectClass=inetOrgPerson)$filter)";
100     }elseif(!empty($org)){
101       $org = utf8_encode($org);
102       $ldapfilter = "(&(objectClass=inetOrgPerson)(o=$org))";
103     }elseif($filter=='other'){
104       $other='';
105       for ($i=ord('a');$i<=ord('z');$i++){
106         $other .= '(!(sn='.chr($i).'*))';
107       }
108       $ldapfilter = "(&(objectClass=inetOrgPerson)$other)";
109     }else{
110       $filter = utf8_encode($filter);
111       $ldapfilter = "(&(objectClass=inetOrgPerson)(sn=$filter*))";
112     }
113     return $ldapfilter;
114   }
115 ?>