Code

Added fixes for several special characters
[gosa.git] / include / class_config.inc
1 <?php
2 /*
3  * This code is part of GOsa (https://gosa.gonicus.de)
4  * Copyright (C) 2003  Cajus Pollmeier
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
21 class config  {
23   /* XML parser */
24   var $parser;
25   var $config_found= FALSE;
26   var $tags= array();
27   var $level= 0;
28   var $gpc= 0;
29   var $section= "";
30   var $currentLocation= "";
32   /* Selected connection */
33   var $current= array();
35   /* Link to LDAP-server */
36   var $ldap= NULL;
37   var $referrals= array();
39   /* Configuration data */
40   var $data= array( 'TABS' => array(), 'LOCATIONS' => array(), 'SERVERS' => array(),
41                 'MAIN' => array( 'LANGUAGES' => array(), 'FAXFORMATS' => array() ),
42                 'MENU' => array(), 'SERVICE' => array());
43   var $basedir= "";
45   /* Keep a copy of the current deparment list */
46   var $departments= array();
47   var $idepartments= array();
49   function config($filename, $basedir= "")
50   {
51         $this->parser = xml_parser_create();
52         $this->basedir= $basedir;
54         xml_set_object($this->parser, $this);
55         xml_set_element_handler($this->parser, "tag_open", "tag_close");
57         /* Parse config file directly? */
58         if ($filename != ""){
59                 $this->parse($filename);
60         }
61   }
63   function parse($filename)
64   { 
65         $fh= fopen($filename, "r"); 
66         $xmldata= fread($fh, 100000);
67         fclose($fh); 
68         if(!xml_parse($this->parser, chop($xmldata))){
69                 print_red(sprintf(_("XML error in gosa.conf: %s at line %d"),
70                         xml_error_string(xml_get_error_code($this->parser)),
71                         xml_get_current_line_number($this->parser)));
72                 echo $_SESSION['errors'];
73                 exit;
74         }
75   }
77   function tag_open($parser, $tag, $attrs)
78   { 
79         /* Save last and current tag for reference */
80         $this->tags[$this->level]= $tag;
81         $this->level++;
83         /* Trigger on CONF section */
84         if ($tag == 'CONF'){
85                 $this->config_found= TRUE;
86         }
88         /* Return if we're not in config section */
89         if (!$this->config_found){
90                 return;
91         }
93         /* Look through attributes */
94         switch ($this->tags[$this->level-1]){
96                 /* Handle tab section */
97                 case 'TAB':     $name= $this->tags[$this->level-2];
98                 
99                                 /* Create new array? */
100                                 if (!isset($this->data['TABS'][$name])){
101                                         $this->data['TABS'][$name]= array();
102                                 }
104                                 /* Add elements */
105                                 $this->data['TABS'][$name][]= $attrs;
106                                 break;
108                 /* Handle location */
109                 case 'LOCATION':
110                                 if ($this->tags[$this->level-2] == 'MAIN'){
111                                         $name= $attrs['NAME'];
112                                         $this->currentLocation= $name;
114                                         /* Add location elements */
115                                         $this->data['LOCATIONS'][$name]= $attrs;
116                                 }
117                                 break;
119                 /* Handle referral tags */
120                 case 'REFERRAL':
121                                 if ($this->tags[$this->level-2] == 'LOCATION'){
122                                         $url= $attrs['URL'];
123                                         $server= preg_replace('!^([^:]+://[^/]+)/.*$!', '\\1', $url);
125                                         /* Add location elements */
126                                         if (!isset($this->data['LOCATIONS'][$this->currentLocation]['REFERRAL'])){
127                                                 $this->data['LOCATIONS'][$this->currentLocation]['REFERRAL']= array();
128                                         }
130                                         $this->data['LOCATIONS'][$this->currentLocation]['REFERRAL'][$server]= $attrs;
131                                 }
132                                 break;
134                 /* Handle language */
135                 case 'LANGUAGE':
136                                 if ($this->tags[$this->level-2] == 'MAIN'){
137                                         /* Add languages */
138                                         $this->data['MAIN']['LANGUAGES'][$attrs['NAME']]= 
139                                                 $attrs['TAG'];
140                                 }
141                                 break;
143                 /* Handle faxformat */
144                 case 'FAXFORMAT':       
145                                 if ($this->tags[$this->level-2] == 'MAIN'){
146                                         /* Add fax formats */
147                                         $this->data['MAIN']['FAXFORMATS'][]= $attrs['TYPE'];
148                                 }
149                                 break;
151                 /* Load main parameters */
152                 case 'MAIN':
153                                 $this->data['MAIN']= array_merge ($this->data['MAIN'], $attrs);
154                                 break;
156                 /* Load menu */
157                 case 'SECTION':
158                                 if ($this->tags[$this->level-2] == 'MENU'){
159                                         $this->section= $attrs['NAME'];
160                                         $this->data['MENU'][$this->section]= array(); ;
161                                 }
162                                 break;
164                 /* Inser plugins */
165                 case 'PLUGIN':
166                                 if ($this->tags[$this->level-3] == 'MENU' &&
167                                     $this->tags[$this->level-2] == 'SECTION'){
168                 
169                                         $this->data['MENU'][$this->section][$this->gpc++]= $attrs;
170                                 }
171                                 if ($this->tags[$this->level-2] == 'SERVICEMENU'){
172                                         $this->data['SERVICE'][$attrs['CLASS']]= $attrs;
173                                 }
174                                 break;
175         }
176   }
178   function tag_close($parser, $tag)
179   {
180         /* Close config section */
181         if ($tag == 'CONF'){
182                 $this->config_found= FALSE;
183         }
184         $this->level--;
185   }
187   function get_ldap_link($sizelimit= FALSE)
188   {
189         /* Build new connection */
190         $this->ldap= ldap_init ($this->current['SERVER'], $this->current['BASE'],
191                         $this->current['ADMIN'], $this->current['PASSWORD']);
193         /* Check for connection */
194         if (is_null($this->ldap) || (is_int($this->ldap) && $this->ldap == 0)){
195                 print_red (_("Can't bind to LDAP. Please contact the system administrator."));
196                 echo $_SESSION['errors'];
197                 exit;
198         }
200         if (!isset($_SESSION['size_limit'])){
201                 $_SESSION['size_limit']= $this->current['SIZELIMIT'];
202                 $_SESSION['size_ignore']= $this->current['SIZEIGNORE'];
203         }
205         if ($sizelimit){
206                 $this->ldap->set_size_limit($_SESSION['size_limit']);
207         } else {
208                 $this->ldap->set_size_limit(0);
209         }
211         /* Move referrals */
212         if (!isset($this->current['REFERRAL'])){
213                 $this->ldap->referrals= array();
214         } else {
215                 $this->ldap->referrals= $this->current['REFERRAL'];
216         }
218         return ($this->ldap);
219   }
221   function set_current($name)
222   {
223         $this->current= $this->data['LOCATIONS'][$name];
224         if (!isset($this->current['PEOPLE'])){
225                 $this->current['PEOPLE']= "ou=people";
226         }
227         if (!isset($this->current['GROUPS'])){
228                 $this->current['GROUPS']= "ou=groups";
229         }
230         if (!isset($this->current['WINSTATIONS'])){
231                 $this->current['WINSTATIONS']= "ou=winstations,ou=systems";
232         }
233         if (!isset($this->current['HASH'])){
234                 $this->current['HASH']= "crypt";
235         }
236         if (!isset($this->current['DNMODE'])){
237                 $this->current['DNMODE']= "cn";
238         }
239         if (!isset($this->current['MINID'])){
240                 $this->current['MINID']= 100;
241         }
242         if (!isset($this->current['SIZELIMIT'])){
243                 $this->current['SIZELIMIT']= 200;
244         }
245         if (!isset($this->current['SIZEINGORE'])){
246                 $this->current['SIZEIGNORE']= TRUE;
247         } else {
248                 if (preg_match("/true/i", $this->current['SIZEIGNORE'])){
249                         $this->current['SIZEIGNORE']= TRUE;
250                 } else {
251                         $this->current['SIZEIGNORE']= FALSE;
252                 }
253         }
255         /* Sort referrals, if present */
256         if (isset ($this->current['REFERRAL'])){
257                 $bases= array();
258                 $servers= array();
259                 foreach ($this->current['REFERRAL'] as $ref){
260                         $server= preg_replace('%^(.*)/[^/]+$%', '\\1', $ref['URL']);
261                         $base= preg_replace('%^.*/([^/]+)$%', '\\1', $ref['URL']);
262                         $bases[$base]= strlen($base);
263                         $servers[$base]= $server;
264                 }
265                 asort($bases);
266                 reset($bases);
267         }
269         /* SERVER not defined? Load the one with the shortest base */
270         if (!isset($this->current['SERVER'])){
271                 $this->current['SERVER']= $servers[key($bases)];
272         }
274         /* BASE not defined? Load the one with the shortest base */
275         if (!isset($this->current['BASE'])){
276                 $this->current['BASE']= key($bases);
277         }
279         /* Parse LDAP referral informations */
280         if (!isset($this->current['ADMIN']) || !isset($this->current['PASSWORD'])){
281                 $url= $this->current['SERVER'];
282                 $referral= $this->current['REFERRAL'][$url];
283                 $this->current['ADMIN']= $referral['ADMIN'];
284                 $this->current['PASSWORD']= $referral['PASSWORD'];
285         }
287         /* Load server informations */
288         $this->load_servers();
289   }
291   function load_servers ()
292   {
293         /* Only perform actions if current is set */
294         if ($this->current == NULL){
295                 return;
296         }
298         /* Fill imap servers */
299         $ldap= $this->get_ldap_link();
300         $ldap->cd ($this->current['BASE']);
301         $ldap->search ("(objectClass=goImapServer)");
303         $this->data['SERVERS']['IMAP']= array();
304         error_reporting(0);
305         while ($attrs= $ldap->fetch()){
306                 $name= $attrs['goImapName'][0];
307                 $this->data['SERVERS']['IMAP'][$name]= array( "connect" => $attrs['goImapConnect'][0],
308                                         "admin" => $attrs['goImapAdmin'][0],
309                                         "password" => $attrs['goImapPassword'][0],
310                                         "sieve_server" => $attrs['goImapSieveServer'][0],
311                                         "sieve_port" => $attrs['goImapSievePort'][0]);
312         }
313         error_reporting(E_ALL);
315         /* Get kerberos server. FIXME: only one is supported currently */
316         $ldap->cd ($this->current['BASE']);
317         $ldap->search ("(objectClass=goKrbServer)");
318         if ($ldap->count()){
319                 $attrs= $ldap->fetch();
320                 $this->data['SERVERS']['KERBEROS']= array( 'SERVER' => $attrs['cn'][0],
321                                                 'REALM' => $attrs['goKrbRealm'][0],
322                                                 'ADMIN' => $attrs['goKrbAdmin'][0],
323                                                 'PASSWORD' => $attrs['goKrbPassword'][0]);
324         }
326         /* Get cups server. FIXME: only one is supported currently */
327         $ldap->cd ($this->current['BASE']);
328         $ldap->search ("(objectClass=goCupsServer)");
329         if ($ldap->count()){
330                 $attrs= $ldap->fetch();
331                 $this->data['SERVERS']['CUPS']= $attrs['cn'][0];        
332         }
334         /* Get fax server. FIXME: only one is supported currently */
335         $ldap->cd ($this->current['BASE']);
336         $ldap->search ("(objectClass=goFaxServer)");
337         if ($ldap->count()){
338                 $attrs= $ldap->fetch();
339                 $this->data['SERVERS']['FAX']= array( 'SERVER' => $attrs['cn'][0],
340                                                 'LOGIN' => $attrs['goFaxAdmin'][0],
341                                                 'PASSWORD' => $attrs['goFaxPassword'][0]);
342         }
344         /* Get asterisk servers */
345         $ldap->cd ($this->current['BASE']);
346         $ldap->search ("(objectClass=goFonServer)");
347         if ($ldap->count()){
348                 $attrs= $ldap->fetch();
349                 $this->data['SERVERS']['FON']= array( 
350                                                 'SERVER'        => $attrs['cn'][0],
351                                                 'LOGIN'         => $attrs['goFonAdmin'][0],
352                                                 'PASSWORD'      => $attrs['goFonPassword'][0],
353                                                 'DB'            => "gophone",
354                                                 'SIP_TABLE'             => "sip_users",
355                                                 'EXT_TABLE'     => "extensions",
356                                                 'VOICE_TABLE'   => "voicemail_users",
357                                                 'QUEUE_TABLE'   => "queues",
358                                                 'QUEUE_MEMBER_TABLE'    => "queue_members");
359         }
360         
361         /* Get asterisk servers */
362         $ldap->cd ($this->current['BASE']);
363         $ldap->search ("(objectClass=goGlpiServer)");
364         if ($ldap->count()){
365                 $attrs= $ldap->fetch();
366                 $this->data['SERVERS']['GLPI']= array( 
367                                                 'SERVER'        => $attrs['cn'][0],
368                                                 'LOGIN'         => $attrs['goGlpiAdmin'][0],
369                                                 'PASSWORD'      => $attrs['goGlpiPassword'][0],
370                                                 'DB'            => $attrs['goGlpiDatabase'][0]);
371         }
372         /* Get logdb server */
373         $ldap->cd ($this->current['BASE']);
374         $ldap->search ("(objectClass=goLogDBServer)");
375         if ($ldap->count()){
376                 $attrs= $ldap->fetch();
377                 $this->data['SERVERS']['LOG']= array( 'SERVER' => $attrs['cn'][0],
378                                                 'LOGIN' => $attrs['goLogAdmin'][0],
379                                                 'PASSWORD' => $attrs['goLogPassword'][0]);
380         }
382         /* Get NFS server lists */
383         $tmp= array("default");
384         $ldap->cd ($this->current['BASE']);
385         $ldap->search ("(&(objectClass=goShareServer)(goExportEntry=*))");
386         while ($attrs= $ldap->fetch()){
387                 for ($i= 0; $i<$attrs["goExportEntry"]["count"]; $i++){
388                         $path= preg_replace ("/\s.*$/", "", $attrs["goExportEntry"][$i]);
389                         $tmp[]= $attrs["cn"][0].":$path";
390                 }
391         }
392         $this->data['SERVERS']['NFS']= $tmp;
395         /* Load Terminalservers */
396         $ldap->cd ($this->current['BASE']);
397         $ldap->search ("(objectClass=goTerminalServer)");
398         $this->data['SERVERS']['TERMINAL']= array();
399         $this->data['SERVERS']['TERMINAL'][]= "default";
401         $this->data['SERVERS']['FONT']= array();
402         $this->data['SERVERS']['FONT'][]= "default";
403         while ($attrs= $ldap->fetch()){
404                 $this->data['SERVERS']['TERMINAL'][]= $attrs["cn"][0];
405                 for ($i= 0; $i<$attrs["goFontPath"]["count"]; $i++){
406                         $this->data['SERVERS']['FONT'][]= $attrs["goFontPath"][$i];
407                 }
408         }
410         /* Ldap Server */
411         $this->data['SERVERS']['LDAP']= array("default");
412         $ldap->cd ($this->current['BASE']);
413         $ldap->search ("(objectClass=goLdapServer)");
414         while ($attrs= $ldap->fetch()){
415                 if (isset($attrs["goLdapBase"])){
416                         for ($i= 0; $i<$attrs["goLdapBase"]["count"]; $i++){
417                                 $this->data['SERVERS']['LDAP'][]= $attrs["cn"][0].":".$attrs["goLdapBase"][$i];
418                         }
419                 }
420         }
422         /* Get misc server lists */
423         $this->data['SERVERS']['SYSLOG']= array("default");
424         $this->data['SERVERS']['NTP']= array("default");
425         $ldap->cd ($this->current['BASE']);
426         $ldap->search ("(objectClass=goNtpServer)");
427         while ($attrs= $ldap->fetch()){
428                 $this->data['SERVERS']['NTP'][]= $attrs["cn"][0];
429         }
430         $ldap->cd ($this->current['BASE']);
431         $ldap->search ("(objectClass=goSyslogServer)");
432         while ($attrs= $ldap->fetch()){
433                 $this->data['SERVERS']['SYSLOG'][]= $attrs["cn"][0];
434         }
436         /* Get samba servers from LDAP, in case of samba3 */
437         if ($this->current['SAMBAVERSION'] == 3){
438                 $this->data['SERVERS']['SAMBA']= array();
439                 $ldap->cd ($this->current['BASE']);
440                 $ldap->search ("(objectClass=sambaDomain)");
441                 while ($attrs= $ldap->fetch()){
442                         $this->data['SERVERS']['SAMBA'][$attrs['sambaDomainName'][0]]= array(
443                                 "SID" => $attrs["sambaSID"][0],
444                                 "RIDBASE" => $attrs["sambaAlgorithmicRidBase"][0]);
445                 }
447                 /* If no samba servers are found, look for configured sid/ridbase */
448                 if (count($this->data['SERVERS']['SAMBA']) == 0){
449                         if (!isset($this->current["SID"]) || !isset($this->current["RIDBASE"])){
450                                 print_red(_("SID and/or RIDBASE missing in your configuration!"));
451                                 echo $_SESSION['errors'];
452                                 exit;
453                         } else {
454                                 $this->data['SERVERS']['SAMBA']['DEFAULT']= array(
455                                         "SID" => $this->current["SID"],
456                                         "RIDBASE" => $this->current["RIDBASE"]);
457                         }
458                 }
459         }
460   }
462   function make_idepartments($max_size= 28)
463   {
464         global $config;
465         $base = $config->current['BASE'];
466                 
467         $arr = array();
468         
469         $this->idepartments= array();
471         /* Create multidimensional array, with all departments.
472      */
473         foreach ($this->departments as $key => $val){
475                 /* remove base from dn */
476                 $val2 = str_replace($base,"",$val);
478                 /* Get every single ou */
479                 $str = preg_replace("/ou=/","|ou=",$val2);      
480                 $elements = array_reverse(split("\|",$str));            
482                 /* Save last array position */
483                 $last = &$arr;
485                 /* Get array depth  */
486                 $cnt = count($elements);
488                 /* Add last ou element of current dn to our array */
489                 foreach($elements as $key => $ele){
490                 
491                         /* skip enpty */
492                         if(empty($ele)) continue;
494                         /* Extract department name */           
495                         $elestr = preg_replace("/^ou=/","", $ele);
496                         $elestr = preg_replace("/,$/","",$elestr);      
498                         /* Add to array */      
499                         if($key == ($cnt-2)){
500                                 $last[$elestr]['ENTRY'] = $val;
501                         }
503                         /* Set next array appending position */
504                         $last = &$last[$elestr]['SUB'];
505                 }
506         }
507         
508         /* Add base entry */
509         $ret["/"]["ENTRY"]      = $base;
510         $ret["/"]["SUB"]        = $arr;
511         
512         $this->idepartments= $this->generateDepartmentArray($ret,-1,$max_size);
513   }
515         /* Creates display friendly output from make_idepartments
516      */
517         function generateDepartmentArray($arr,$depth = -1,$max_size){
518                 $ret = array();
519                 $depth ++;
520         
521                 /* Walk through array */        
522                 foreach($arr as $name => $entries){
524                         /* If this department is the last in the current tree position 
525              * remove it, to avoid generating output for it 
526              */
527                         if(count($entries['SUB'])==0){
528                                 unset($entries['SUB']);
529                         }
531                         /* Fix name, if it contains a replace tag */
532                         $name= @ldap::fix($name);
534                         /* Check if current name is too long, then cut it */
535                         if(strlen($name)> $max_size){
536                                 $name = substr($name,0,($max_size-3))." ...";
537                         }
539                         /* Append the name to the list */       
540                         if(isset($entries['ENTRY'])){
541                                 $a = "";
542                                 for($i = 0 ; $i < $depth ; $i ++){
543                                         $a.="&nbsp;";
544                                 }
545                                 $ret[$entries['ENTRY']]=$a."&nbsp;".$name;
546                         }       
547                         /* For debugging        
548                         if(isset($entries['ENTRY'])){
549                                 $a = "";
550                                 for($i = 0 ; $i < $depth ; $i ++){
551                                         $a.="&nbsp;|";
552                                 }
553                         
554                                 if(!isset($entries['SUB'])){
555                                         $ret[$entries['ENTRY']]=$a."-&nbsp;".$name;
556                                 }else{
557                                         $ret[$entries['ENTRY']]=$a."#"."&nbsp;".$name;
558                                 }
559                         }       
560                         */
562                         /* recursive add of subdepartments */
563                         if(isset($entries['SUB'])){
564                                 $ret = array_merge($ret,$this->generateDepartmentArray($entries['SUB'],$depth,$max_size));
565                         }
566                 }
567                 
568                 return($ret);
569         }
571   /* This function returns all available Shares defined in this ldap
572   * There are two ways to call this function, if listboxEntry is true
573   *  only name and path are attached to the array, in it is false, the whole
574   *  entry will be parsed an atached to the result.
575   */
576   function getShareList($listboxEntry = false)
577   {
578     $ldap= $this->get_ldap_link();
579     $a_res = $ldap->search("(objectClass=goShareServer)",array("goExportEntry","cn"));
580     $return= array();
581     while($entry = $ldap->fetch($a_res)){
582       unset($entry['goExportEntry']['count']);
583       foreach($entry['goExportEntry'] as $export){
584         $shareAttrs = split("\|",$export);
585         if($listboxEntry) {
586           $return[$shareAttrs[0]."|".$entry['cn'][0]] = $shareAttrs[0]." - ".$entry['cn'][0];
587         }else{
588           $return[$shareAttrs[0]."|".$entry['cn'][0]]['server']       = $entry['cn'][0];
589           $return[$shareAttrs[0]."|".$entry['cn'][0]]['name']         = $shareAttrs[0];
590           $return[$shareAttrs[0]."|".$entry['cn'][0]]['description']  = $shareAttrs[1];
591           $return[$shareAttrs[0]."|".$entry['cn'][0]]['type']         = $shareAttrs[2];
592           $return[$shareAttrs[0]."|".$entry['cn'][0]]['charset']      = $shareAttrs[3];
593           $return[$shareAttrs[0]."|".$entry['cn'][0]]['path']         = $shareAttrs[4];
594           $return[$shareAttrs[0]."|".$entry['cn'][0]]['option']       = $shareAttrs[5];
595         }
596       }
597     }
598     return($return);
599   }
601   /* This function returns all available ShareServer
602   */
603   function getShareServerList()
604   {
605     $ldap= $this->get_ldap_link();
606     $a_res = $ldap->search("(objectClass=goShareServer)",array("goExportEntry","cn"));
607     $return= array();
608     while($entry = $ldap->fetch($a_res)){
609       unset($entry['goExportEntry']['count']);
610       foreach($entry['goExportEntry'] as $share){
611         $a_share = split("\|",$share);
612         $sharename = $a_share[0];
613         $return[$entry['cn'][0]."|".$sharename] = $entry['cn'][0]." [".$sharename."]";
614       }
615     }
616     return($return);
617   }
621 ?>