Code

Excluded encodings in a seperated file
[gosa.git] / html / getxls.php
1 <?php
2 /*
3    This code is part of GOsa (https://gosa.gonicus.de)
4    Copyright (C) 2003  Cajus Pollmeier
5    Copyright (C) 2005  Guillaume Delecourt
6    Copyright (C) 2005  Vincent Seynhaeve
7    Copyright (C) 2005  Benoit Mortier
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  */
24 require_once "../include/php_writeexcel/class.writeexcel_workbook.inc.php";
25 require_once "../include/php_writeexcel/class.writeexcel_worksheet.inc.php";
27 function dump_ldap ($mode= 0)
28 {
29   global $config;
30   $ldap= $config->get_ldap_link();
31   error_reporting (E_ALL & ~E_NOTICE);
33   $display = "";
34   if($mode == 2){       // Single Entry Export !
35     $d =  base64_decode($_GET['d']);
36     $n =  base64_decode($_GET['n']);
37     $dn=$d.$n;
38     $date=date('dS \of F Y ');
39     $fname = tempnam("/tmp", "demo.xls");
40     $workbook=& new writeexcel_workbook($fname);
42     $title_title=& $workbook->addformat(array(
43           bold    => 1,
44           color   => 'green',
45           size    => 11,
46           underline => 2,
47           font    => 'Helvetica'
48           ));
50     $title_bold=& $workbook->addformat(array(
51           bold    => 1,
52           color   => 'black',
53           size    => 10,
54           font    => 'Helvetica'
55           ));
56    # Create a format for the phone numbers
57    $f_phone =& $workbook->addformat();
58    $f_phone->set_align('left');
59    $f_phone->set_num_format('\0#');
62     switch ($d){
63       case "ou=people," : 
64         $user=                             $ldap->gen_xls($dn,"(objectClass=*)",array("uid","dateOfBirth","gender","givenName","preferredLanguage"));
65       $intitul=array(_("Birthday").":", _("Sex").":", _("Surname")."/"._("Given name").":",_("Language").":");
67       //name of the xls file
68       $name_section=_("Users");
69         
70       $worksheet=& $workbook->addworksheet(_("Users"));
71       $worksheet->set_column('A:B', 51);
73       $user_nbr=count($user);
74       $worksheet->write('A1',sprintf(_("User list of %s on %s"),$n,$date),$title_title);
75       $r=3;
76       for($i=1;$i<$user_nbr;$i++)
77       {
78         if($i>1)
79           $worksheet->write('A'.$r++,"");
80         $worksheet->write('A'.$r++,_("User ID").": ".$user[$i][0],$title_bold);
82         for($j=1;$j<5;$j++)
83         {
84           $r++;
85           $worksheet->write('A'.$r,$intitul[$j-1]);
86           $user[$i][$j]=utf8_decode ($user[$i][$j]);
87           $worksheet->write('B'.$r,$user[$i][$j]);
88         }
89         $worksheet->write('A'.$r++,"");
90       }
91       break;
93       case "ou=groups,": $groups= $ldap->gen_xls($dn,"(objectClass=*)",array("cn","memberUid"),TRUE,1);
94       $intitul=array(_("Members").":");
96       //name of the xls file
97       $name_section=_("Groups");
99       $worksheet =& $workbook->addworksheet(_("Groups"));
100       $worksheet->set_column('A:B', 51);
102       //count number of groups
103       $groups_nbr=count($groups);
104       $worksheet->write('A1', sprintf(_("Groups of %s on %s"), $n, $date),$title_title);
105       $r=3;
106       for($i=1;$i<$groups_nbr;$i++)
107       {
108         $worksheet->write('A'.$r++,_("User ID").": ".$groups[$i][0][0],$title_bold);
109         for($j=1;$j<=2;$j++)
110         {
111           $r++;
112           $worksheet->write('A'.$r,$intitul[$j-1]);
113           for($k=0;$k<= $groups[$i][$j]['count'];$k++)
114           {
115             $worksheet->write('B'.$r,$groups[$i][$j][$k]);
116             $r++;
117           }
118         }
119       }
120       break;
122       case "ou=computers,": $computers= $ldap->gen_xls($dn,"(objectClass=*)",array("cn","description","uid"));
123       $intitul=array(_("Description").":",_("User ID").":");
124       $worksheet =& $workbook->addworksheet(_("Computers"));
125       $worksheet->set_column('A:B', 32);
126       //count number of computers
127       $computers_nbr=count($computers);
128       $r=1;
129       for($i=1;$i<$computers_nbr;$i++)
130       {
131         if($i>1)
132           $worksheet->write('A'.$r++,"");
133         $worksheet->write('A'.$r++,_("Common name").": ".$computers[$i][0],$title_bold);
134         for($j=1;$j<3;$j++)
135         {
136           $r++;
137           $worksheet->write('A'.$r,$intitul[$j-1]);
138           $computers[$i][$j]=utf8_decode ($computers[$i][$j]);
139           $worksheet->write('B'.$r,$computers[$i][$j]);
140         }
141         $worksheet->write('A'.$r++,"");
142       }
143       break;
145       case "ou=servers,ou=systems,": $servers= $ldap->gen_xls($dn,"(objectClass=*)",array("cn"));
146       $intitul=array(_("Server name").":");
148       //name of the xls file
149       $name_section=_("Servers");
151       $worksheet =& $workbook->addworksheet(_("Servers"));
152       $worksheet->set_column('A:B', 51);
154       //count number of servers
155       $servers_nbr=count($servers);
156       $worksheet->write('A1',sprintf(_("Servers of %s on %s"), $n, $date),$title_title);
157       $r=3;
158       $worksheet->write('A'.$r++,_("Servers").": ",$title_bold);
159       for($i=1;$i<$servers_nbr;$i++)
160       {
161         for($j=0;$j<1;$j++)
162         {
163           $r++;
164           $worksheet->write('A'.$r,$intitul[$j]);
165           $servers[$i][$j]=utf8_decode ($servers[$i][$j]);
166           $worksheet->write('B'.$r,$servers[$i][$j]);
167         }
168       }
169       break;
171       case "dc=addressbook,": //data about addressbook
172         $address= $ldap->gen_xls($dn,"(objectClass=*)",array("cn","displayName","facsimileTelephoneNumber","givenName","homePhone","homePostalAddress","initials","l","mail","mobile","o","ou","pager","telephoneNumber","postalAddress","postalCode","sn","st","title"));
174       $intitul=array(_("Common name").":",_("Display name").":",_("Fax").":",_("Name")."/"._("Given name").":",_("Home phone").":",_("Home postal address").":",_("Initials").":",_("Location").":",_("Mail address").":",_("Mobile phone").":",_("City").":",_("Postal address").":",_("Pager").":",_("Phone number").":",_("Address").":",_("Postal code").":",_("Surname").":",_("State").":",_("Function").":");
175       
176       //name of the xls file
177       $name_section=_("Adressbook");
179       $worksheet =& $workbook->addworksheet(_("Servers"));
180       $worksheet->set_column('A:B', 51);
182       //count number of entries
183       $address_nbr=count($address);
184       $worksheet->write('A1',sprintf(_("Adressbook of %s on %s"),$n, $date),$title_title);
185       $r=3;
186       for($i=1;$i<$address_nbr;$i++)
187       {
188         if($i>1)
189           $worksheet->write('A'.$r++,"");
190         $worksheet->write('A'.$r++,_("Common Name").": ".$address[$i][0],$title_bold);
191         for($j=1;$j<19;$j++)
192         {
193           $r++;
194           $worksheet->write('A'.$r,$intitul[$j]);
195           $address[$i][$j]=utf8_decode ($address[$i][$j]);
196           $worksheet->write('B'.$r,$address[$i][$j],$f_phone);
197         }
198         $worksheet->write('A'.$r++,"");
199       }
201       break;
203       default: echo "error!!";
204     }
206     $workbook->close();
209     // We'll be outputting a xls
210     header('Content-type: application/x-msexcel');
212     // It will be called demo.xls
213     header('Content-Disposition: attachment; filename='.$name_section.".xls");
215     // The source is in original.xls
216     readfile($fname);
217     unlink ($fname);
218   }
219   elseif($mode == 3){ // Full Export !
220     $dn =  base64_decode($_GET['dn']);
222     //data about users
223     $user= $ldap->gen_xls("ou=people,".$dn,"(objectClass=*)",array("uid","dateOfBirth","gender","givenName","preferredLanguage"));
224     $user_intitul=array(_("Day of birth").":",_("Sex").":",_("Surname")."/"._("Given name").":",_("Language").":");
225     //data about groups
226     $groups= $ldap->gen_xls("ou=groups,".$dn,"(objectClass=*)",array("cn","memberUid"),TRUE,1);
227     $groups_intitul=array(_("Members").":");
228     //data about computers
229     $computers= $ldap->gen_xls("ou=computers,".$dn,"(objectClass=*)",array("cn","description","uid"));
230     $computers_intitul=array(_("Description").":",_("UID").":");
231     //data about servers
232     $servers= $ldap->gen_xls("ou=servers,ou=systems,".$dn,"(objectClass=*)",array("cn"));
233     $servers_intitul=array(_("Name").":");
234     //data about addressbook
235     $address= $ldap->gen_xls("dc=addressbook,".$dn,"(objectClass=*)",array("cn","displayName","facsimileTelephoneNumber","givenName","homePhone","homePostalAddress","initials","l","mail","mobile","o","ou","pager","telephoneNumber","postalAddress","postalCode","sn","st","title"));
236     $address_intitul=array("cn",_("Display name").":",_("Fax").":",_("Surname")."/"._("Given name").":",_("Phone number").":",_("Postal address").":",_("Initials").":",_("City").":",_("Email address").":",_("Mobile").":",_("Organization").":",_("Organizational unit").":",_("Pager").":",_("Phone number").":",_("Postal address").":",_("Postal Code").":",_("Surename").":",_("State").":",_("Title").":");
238     //name of the xls file
239     $name_section=_("Full");
240     $date=date('dS \of F Y ');
241     $fname = tempnam("/tmp", "demo.xls");
242     $workbook =& new writeexcel_workbook($fname);
243     $worksheet =& $workbook->addworksheet(_("Users"));
244     $worksheet2 =& $workbook->addworksheet(_("Groups"));
245     $worksheet3 =& $workbook->addworksheet(_("Servers"));
246     $worksheet4 =& $workbook->addworksheet(_("Computers"));
247     $worksheet5 =& $workbook->addworksheet(_("Adressbook"));
249     $worksheet->set_column('A:B', 51);
250     $worksheet2->set_column('A:B', 51);
251     $worksheet3->set_column('A:B', 51);
252     $worksheet4->set_column('A:B', 51);
253     $worksheet5->set_column('A:B', 51);
255     $title_title=& $workbook->addformat(array(
256           bold    => 1,
257           color   => 'green',
258           size    => 11,
259           font    => 'Helvetica'
260           ));
262     $title_bold =& $workbook->addformat(array(
263           bold    => 1,
264           color   => 'black',
265           size    => 10,
266           font    => 'Helvetica'
267           ));
269    # Create a format for the phone numbers
270    $f_phone =& $workbook->addformat();
271    $f_phone->set_align('left');
272    $f_phone->set_num_format('\0#');
274     //count number of users
275     $user_nbr=count($user);
276     $worksheet->write('A1',sprintf(_("User List of %s on %s"), $dn, $date),$title_title);
277     $r=3;
278     for($i=1;$i<$user_nbr;$i++)
279     {
280       if($i>1)
281         $worksheet->write('A'.$r++,"");
282       $worksheet->write('A'.$r++,_("User ID").": ".$user[$i][0],$title_bold);
283       for($j=1;$j<5;$j++)
284       {
285         $r++;
286         $worksheet->write('A'.$r,$user_intitul[$j-1]);
287         $user[$i][$j]=utf8_decode ($user[$i][$j]);
288         $worksheet->write('B'.$r,$user[$i][$j]);
289       }
290       $worksheet->write('A'.$r++,"");
291     }
293     //count number of groups
294     $groups_nbr=count($groups);
295     $worksheet2->write('A1',sprintf(_("Groups of %s on %s"), $dn, $date),$title_title);
296     $r=3;
297     for($i=1;$i<$groups_nbr;$i++)
298     {
299       $worksheet2->write('A'.$r++,_("User ID").": ".$groups[$i][0][0],$title_bold);
300       for($j=1;$j<=2;$j++)
301       {
302         $r++;
303         $worksheet2->write('A'.$r,$group_intitul[$j-1]);
304         for($k=0;$k<= $groups[$i][$j]['count'];$k++)
305         {
306           $worksheet2->write('B'.$r,$groups[$i][$j][$k]);
307           $r++;
308         }
309       }
310     }
312     //count number of servers
313     $servers_nbr=count($servers);
314     $worksheet3->write('A1',sprintf(_("Servers of %s on %s"),$dn,$date),$title_title);
315     $r=3;
316     $worksheet3->write('A'.$r++,_("Servers").": ",$title_bold);
317     for($i=1;$i<$servers_nbr;$i++)
318     {
319       for($j=0;$j<1;$j++)
320       {
321         $r++;
322         $worksheet3->write('A'.$r,$servers_intitul[$j]);
323         $servers[$i][$j]=utf8_decode ($servers[$i][$j]);
324         $worksheet3->write('B'.$r,$servers[$i][$j]);
325       }
326     }
328     //count number of computers
329     $computers_nbr=count($computers);
330     $worksheet4->write('A1',sprintf(_("Computers of %s on %s"),$dn,$date),$title_title);
331     $r=3;
332     for($i=1;$i<$computers_nbr;$i++)
333     {
334       if($i>1)
335         $worksheet->write('A'.$r++,"");
336       $worksheet4->write('A'.$r++,_("Common name").": ".$computers[$i][0],$title_bold);
337       for($j=1;$j<3;$j++)
338       {
339         $r++;
340         $worksheet4->write('A'.$r,$computers_intitul[$j-1]);
341         $computers[$i][$j]=utf8_decode ($computers[$i][$j]);
342         $worksheet4->write('B'.$r,$computers[$i][$j]);
343       }
344       $worksheet4->write('A'.$r++,"");
345     }
347     //count number of entries
348     $address_nbr=count($address);
349     $worksheet5->write('A1',sprintf(_("Adressbook of %s on %s"),$dn, $date),$title_title);
351     $r=3;
352     for($i=1;$i<$address_nbr;$i++)
353     {
354       if($i>1)
355         $worksheet5->write('A'.$r++,"");
356       $worksheet5->write('A'.$r++,_("Common name").": ".$address[$i][0],$title_bold);
357       for($j=1;$j<19;$j++)
358       {
359         $r++;
360         $worksheet5->write('A'.$r,$address_intitul[$j]);
361         $address[$i][$j]=utf8_decode ($address[$i][$j]);
362         $worksheet5->write('B'.$r,$address[$i][$j],$f_phone);
363       }
364       $worksheet5->write('A'.$r++,"");
365     }
366     $workbook->close();
369     // We'll be outputting a xls
370     header('Content-type: application/x-msexcel');
372     // It will be called demo.xls
373     header('Content-Disposition: attachment; filename='.$name_section.".xls");
375     readfile($fname);
377     unlink ($fname);
378   }
379   elseif($mode == 4){ // IVBB LDIF Export
380     $dn =  base64_decode($_GET['dn']);
381     /*$display= $ldap->gen_ldif($dn,"(objectClass=ivbbEntry)",array(
382       "GouvernmentOrganizationalUnit","houseIdentifier","vocation",
383       "ivbbLastDeliveryCollective","gouvernmentOrganizationalPersonLocality",
384       "gouvernmentOrganizationalUnitDescription","gouvernmentOrganizationalUnitSubjectArea",
385       "functionalTitle","role","certificateSerialNumber","userCertificate","publicVisible",
386       "telephoneNumber","seeAlso","description","title","x121Address","registeredAddress",
387       "destinationIndicator","preferredDeliveryMethod","telexNumber","teletexTerminalIdentifier",
388       "telephoneNumber","internationaliSDNNumber","facsimileTelephoneNumber","street",
389       "postOfficeBox","postalCode","postalAddress","physicalDeliveryOfficeName","ou",
390       "st","l","audio","businessCategory","carLicense","departmentNumber","displayName",
391       "employeeNumber","employeeType","givenName","homePhone","homePostalAddress",
392       "initials","jpegPhoto","labeledURI","mail","manager","mobile","o","pager","photo",
393       "roomNumber","secretary","userCertificate","x500uniqueIdentifier","preferredLanguage",
394       "userSMIMECertificate","userPKCS12"));*/
396     echo $display;
397   }
401 /* Basic setup, remove eventually registered sessions */
402 @require_once ("../include/php_setup.inc");
403 @require_once ("functions.inc");
404 error_reporting (E_ALL);
405 session_start ();
407 /* Logged in? Simple security check */
408 if (!isset($_SESSION['ui'])){
409   gosa_log ("Error: getldif.php called without session");
410   header ("Location: ../index.php");
411   exit;
413 $ui= $_SESSION["ui"];
414 $config= $_SESSION['config'];
416 header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
417 header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
418 header("Cache-Control: no-cache");
419 header("Pragma: no-cache");
420 header("Cache-Control: post-check=0, pre-check=0");
422 header("Content-type: text/plain");
424 /* Check ACL's */
425 $acl= get_permissions ($config->current['BASE'], $ui->subtreeACL);
426 $acl= get_module_permission($acl, "all", $config->current['BASE']);
427 if (chkacl($acl, "all") != ""){
428   header ("Location: ../index.php");
429   exit;
432 switch ($_GET['ivbb']){
433   case 2: dump_ldap (2);
434           break;
436   case 3: dump_ldap (3);
437           break;
439   case 4: dump_ldap (4);
440           break;
442   default:
443           echo "Error in ivbb parameter. Request aborted.";
445 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
446 ?>