1 <?php
2 /*
3 * This code is part of GOsa (https://gosa.gonicus.de)
4 * Copyright (C) 2003-2006 - Cajus Pollmeier <pollmeier@gonicus.de>
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();
48 var $adepartments= array();
50 function config($filename, $basedir= "")
51 {
52 $this->parser = xml_parser_create();
53 $this->basedir= $basedir;
55 xml_set_object($this->parser, $this);
56 xml_set_element_handler($this->parser, "tag_open", "tag_close");
58 /* Parse config file directly? */
59 if ($filename != ""){
60 $this->parse($filename);
61 }
62 }
64 function parse($filename)
65 {
66 $fh= fopen($filename, "r");
67 $xmldata= fread($fh, 100000);
68 fclose($fh);
69 if(!xml_parse($this->parser, chop($xmldata))){
70 print_red(sprintf(_("XML error in gosa.conf: %s at line %d"),
71 xml_error_string(xml_get_error_code($this->parser)),
72 xml_get_current_line_number($this->parser)));
73 echo $_SESSION['errors'];
74 exit;
75 }
76 }
78 function tag_open($parser, $tag, $attrs)
79 {
80 /* Save last and current tag for reference */
81 $this->tags[$this->level]= $tag;
82 $this->level++;
84 /* Trigger on CONF section */
85 if ($tag == 'CONF'){
86 $this->config_found= TRUE;
87 }
89 /* Return if we're not in config section */
90 if (!$this->config_found){
91 return;
92 }
94 /* Look through attributes */
95 switch ($this->tags[$this->level-1]){
97 /* Handle tab section */
98 case 'TAB': $name= $this->tags[$this->level-2];
100 /* Create new array? */
101 if (!isset($this->data['TABS'][$name])){
102 $this->data['TABS'][$name]= array();
103 }
105 /* Add elements */
106 $this->data['TABS'][$name][]= $attrs;
107 break;
109 /* Handle location */
110 case 'LOCATION':
111 if ($this->tags[$this->level-2] == 'MAIN'){
112 $name= $attrs['NAME'];
113 $this->currentLocation= $name;
115 /* Add location elements */
116 $this->data['LOCATIONS'][$name]= $attrs;
117 }
118 break;
120 /* Handle referral tags */
121 case 'REFERRAL':
122 if ($this->tags[$this->level-2] == 'LOCATION'){
123 $url= $attrs['URL'];
124 $server= preg_replace('!^([^:]+://[^/]+)/.*$!', '\\1', $url);
126 /* Add location elements */
127 if (!isset($this->data['LOCATIONS'][$this->currentLocation]['REFERRAL'])){
128 $this->data['LOCATIONS'][$this->currentLocation]['REFERRAL']= array();
129 }
131 $this->data['LOCATIONS'][$this->currentLocation]['REFERRAL'][$server]= $attrs;
132 }
133 break;
135 /* Handle language */
136 case 'LANGUAGE':
137 if ($this->tags[$this->level-2] == 'MAIN'){
138 /* Add languages */
139 $this->data['MAIN']['LANGUAGES'][$attrs['NAME']]=
140 $attrs['TAG'];
141 }
142 break;
144 /* Handle faxformat */
145 case 'FAXFORMAT':
146 if ($this->tags[$this->level-2] == 'MAIN'){
147 /* Add fax formats */
148 $this->data['MAIN']['FAXFORMATS'][]= $attrs['TYPE'];
149 }
150 break;
152 /* Load main parameters */
153 case 'MAIN':
154 $this->data['MAIN']= array_merge ($this->data['MAIN'], $attrs);
155 break;
157 /* Load menu */
158 case 'SECTION':
159 if ($this->tags[$this->level-2] == 'MENU'){
160 $this->section= $attrs['NAME'];
161 $this->data['MENU'][$this->section]= array(); ;
162 }
163 break;
165 /* Inser plugins */
166 case 'PLUGIN':
167 if ($this->tags[$this->level-3] == 'MENU' &&
168 $this->tags[$this->level-2] == 'SECTION'){
170 $this->data['MENU'][$this->section][$this->gpc++]= $attrs;
171 }
172 if ($this->tags[$this->level-2] == 'SERVICEMENU'){
173 $this->data['SERVICE'][$attrs['CLASS']]= $attrs;
174 }
175 break;
176 }
177 }
179 function tag_close($parser, $tag)
180 {
181 /* Close config section */
182 if ($tag == 'CONF'){
183 $this->config_found= FALSE;
184 }
185 $this->level--;
186 }
188 function get_ldap_link($sizelimit= FALSE)
189 {
190 /* Build new connection */
191 $this->ldap= ldap_init ($this->current['SERVER'], $this->current['BASE'],
192 $this->current['ADMIN'], $this->current['PASSWORD']);
194 /* Check for connection */
195 if (is_null($this->ldap) || (is_int($this->ldap) && $this->ldap == 0)){
196 print_red (_("Can't bind to LDAP. Please contact the system administrator."));
197 echo $_SESSION['errors'];
198 exit;
199 }
201 if (!isset($_SESSION['size_limit'])){
202 $_SESSION['size_limit']= $this->current['SIZELIMIT'];
203 $_SESSION['size_ignore']= $this->current['SIZEIGNORE'];
204 }
206 if ($sizelimit){
207 $this->ldap->set_size_limit($_SESSION['size_limit']);
208 } else {
209 $this->ldap->set_size_limit(0);
210 }
212 /* Move referrals */
213 if (!isset($this->current['REFERRAL'])){
214 $this->ldap->referrals= array();
215 } else {
216 $this->ldap->referrals= $this->current['REFERRAL'];
217 }
219 return ($this->ldap);
220 }
222 function set_current($name)
223 {
224 $this->current= $this->data['LOCATIONS'][$name];
225 if (!isset($this->current['PEOPLE'])){
226 $this->current['PEOPLE']= "ou=people";
227 }
228 if (!isset($this->current['GROUPS'])){
229 $this->current['GROUPS']= "ou=groups";
230 }
231 if (!isset($this->current['WINSTATIONS'])){
232 $this->current['WINSTATIONS']= "ou=winstations,ou=systems";
233 }
234 if (!isset($this->current['HASH'])){
235 $this->current['HASH']= "crypt";
236 }
237 if (!isset($this->current['DNMODE'])){
238 $this->current['DNMODE']= "cn";
239 }
240 if (!isset($this->current['MINID'])){
241 $this->current['MINID']= 100;
242 }
243 if (!isset($this->current['SIZELIMIT'])){
244 $this->current['SIZELIMIT']= 200;
245 }
246 if (!isset($this->current['SIZEINGORE'])){
247 $this->current['SIZEIGNORE']= TRUE;
248 } else {
249 if (preg_match("/true/i", $this->current['SIZEIGNORE'])){
250 $this->current['SIZEIGNORE']= TRUE;
251 } else {
252 $this->current['SIZEIGNORE']= FALSE;
253 }
254 }
256 /* Sort referrals, if present */
257 if (isset ($this->current['REFERRAL'])){
258 $bases= array();
259 $servers= array();
260 foreach ($this->current['REFERRAL'] as $ref){
261 $server= preg_replace('%^(.*)/[^/]+$%', '\\1', $ref['URL']);
262 $base= preg_replace('%^.*/([^/]+)$%', '\\1', $ref['URL']);
263 $bases[$base]= strlen($base);
264 $servers[$base]= $server;
265 }
266 asort($bases);
267 reset($bases);
268 }
270 /* SERVER not defined? Load the one with the shortest base */
271 if (!isset($this->current['SERVER'])){
272 $this->current['SERVER']= $servers[key($bases)];
273 }
275 /* BASE not defined? Load the one with the shortest base */
276 if (!isset($this->current['BASE'])){
277 $this->current['BASE']= key($bases);
278 }
280 /* Convert BASE to have escaped special characters */
281 $this->current['BASE']= @LDAP::convert($this->current['BASE']);
283 /* Parse LDAP referral informations */
284 if (!isset($this->current['ADMIN']) || !isset($this->current['PASSWORD'])){
285 $url= $this->current['SERVER'];
286 $referral= $this->current['REFERRAL'][$url];
287 $this->current['ADMIN']= $referral['ADMIN'];
288 $this->current['PASSWORD']= $referral['PASSWORD'];
289 }
291 /* Load server informations */
292 $this->load_servers();
293 }
295 function load_servers ()
296 {
297 /* Only perform actions if current is set */
298 if ($this->current == NULL){
299 return;
300 }
302 /* Fill imap servers */
303 $ldap= $this->get_ldap_link();
304 $ldap->cd ($this->current['BASE']);
305 $ldap->search ("(objectClass=goImapServer)");
307 $this->data['SERVERS']['IMAP']= array();
308 error_reporting(0);
309 while ($attrs= $ldap->fetch()){
310 $name= $attrs['goImapName'][0];
311 $this->data['SERVERS']['IMAP'][$name]= array( "connect" => $attrs['goImapConnect'][0],
312 "admin" => $attrs['goImapAdmin'][0],
313 "password" => $attrs['goImapPassword'][0],
314 "sieve_server" => $attrs['goImapSieveServer'][0],
315 "sieve_port" => $attrs['goImapSievePort'][0]);
316 }
317 error_reporting(E_ALL);
319 /* Get kerberos server. FIXME: only one is supported currently */
320 $ldap->cd ($this->current['BASE']);
321 $ldap->search ("(objectClass=goKrbServer)");
322 if ($ldap->count()){
323 $attrs= $ldap->fetch();
324 $this->data['SERVERS']['KERBEROS']= array( 'SERVER' => $attrs['cn'][0],
325 'REALM' => $attrs['goKrbRealm'][0],
326 'ADMIN' => $attrs['goKrbAdmin'][0],
327 'PASSWORD' => $attrs['goKrbPassword'][0]);
328 }
330 /* Get cups server. FIXME: only one is supported currently */
331 $ldap->cd ($this->current['BASE']);
332 $ldap->search ("(objectClass=goCupsServer)");
333 if ($ldap->count()){
334 $attrs= $ldap->fetch();
335 $this->data['SERVERS']['CUPS']= $attrs['cn'][0];
336 }
338 /* Get fax server. FIXME: only one is supported currently */
339 $ldap->cd ($this->current['BASE']);
340 $ldap->search ("(objectClass=goFaxServer)");
341 if ($ldap->count()){
342 $attrs= $ldap->fetch();
343 $this->data['SERVERS']['FAX']= array( 'SERVER' => $attrs['cn'][0],
344 'LOGIN' => $attrs['goFaxAdmin'][0],
345 'PASSWORD' => $attrs['goFaxPassword'][0]);
346 }
348 /* Get asterisk servers */
349 $ldap->cd ($this->current['BASE']);
350 $ldap->search ("(objectClass=goFonServer)");
351 if ($ldap->count()){
352 $attrs= $ldap->fetch();
353 $this->data['SERVERS']['FON']= array(
354 'SERVER' => $attrs['cn'][0],
355 'LOGIN' => $attrs['goFonAdmin'][0],
356 'PASSWORD' => $attrs['goFonPassword'][0],
357 'DB' => "gophone",
358 'SIP_TABLE' => "sip_users",
359 'EXT_TABLE' => "extensions",
360 'VOICE_TABLE' => "voicemail_users",
361 'QUEUE_TABLE' => "queues",
362 'QUEUE_MEMBER_TABLE' => "queue_members");
363 }
365 /* Get asterisk servers */
366 $ldap->cd ($this->current['BASE']);
367 $ldap->search ("(objectClass=goGlpiServer)");
368 if ($ldap->count()){
369 $attrs= $ldap->fetch();
370 $this->data['SERVERS']['GLPI']= array(
371 'SERVER' => $attrs['cn'][0],
372 'LOGIN' => $attrs['goGlpiAdmin'][0],
373 'PASSWORD' => $attrs['goGlpiPassword'][0],
374 'DB' => $attrs['goGlpiDatabase'][0]);
375 }
376 /* Get logdb server */
377 $ldap->cd ($this->current['BASE']);
378 $ldap->search ("(objectClass=goLogDBServer)");
379 if ($ldap->count()){
380 $attrs= $ldap->fetch();
381 $this->data['SERVERS']['LOG']= array( 'SERVER' => $attrs['cn'][0],
382 'LOGIN' => $attrs['goLogAdmin'][0],
383 'PASSWORD' => $attrs['goLogPassword'][0]);
384 }
386 /* Get NFS server lists */
387 $tmp= array("default");
388 $ldap->cd ($this->current['BASE']);
389 $ldap->search ("(&(objectClass=goShareServer)(goExportEntry=*))");
390 while ($attrs= $ldap->fetch()){
391 for ($i= 0; $i<$attrs["goExportEntry"]["count"]; $i++){
392 $path= preg_replace ("/\s.*$/", "", $attrs["goExportEntry"][$i]);
393 $tmp[]= $attrs["cn"][0].":$path";
394 }
395 }
396 $this->data['SERVERS']['NFS']= $tmp;
399 /* Load Terminalservers */
400 $ldap->cd ($this->current['BASE']);
401 $ldap->search ("(objectClass=goTerminalServer)");
402 $this->data['SERVERS']['TERMINAL']= array();
403 $this->data['SERVERS']['TERMINAL'][]= "default";
405 $this->data['SERVERS']['FONT']= array();
406 $this->data['SERVERS']['FONT'][]= "default";
407 while ($attrs= $ldap->fetch()){
408 $this->data['SERVERS']['TERMINAL'][]= $attrs["cn"][0];
409 for ($i= 0; $i<$attrs["goFontPath"]["count"]; $i++){
410 $this->data['SERVERS']['FONT'][]= $attrs["goFontPath"][$i];
411 }
412 }
414 /* Ldap Server */
415 $this->data['SERVERS']['LDAP']= array("default");
416 $ldap->cd ($this->current['BASE']);
417 $ldap->search ("(objectClass=goLdapServer)");
418 while ($attrs= $ldap->fetch()){
419 if (isset($attrs["goLdapBase"])){
420 for ($i= 0; $i<$attrs["goLdapBase"]["count"]; $i++){
421 $this->data['SERVERS']['LDAP'][]= $attrs["cn"][0].":".$attrs["goLdapBase"][$i];
422 }
423 }
424 }
426 /* Get misc server lists */
427 $this->data['SERVERS']['SYSLOG']= array("default");
428 $this->data['SERVERS']['NTP']= array("default");
429 $ldap->cd ($this->current['BASE']);
430 $ldap->search ("(objectClass=goNtpServer)");
431 while ($attrs= $ldap->fetch()){
432 $this->data['SERVERS']['NTP'][]= $attrs["cn"][0];
433 }
434 $ldap->cd ($this->current['BASE']);
435 $ldap->search ("(objectClass=goSyslogServer)");
436 while ($attrs= $ldap->fetch()){
437 $this->data['SERVERS']['SYSLOG'][]= $attrs["cn"][0];
438 }
440 /* Get samba servers from LDAP, in case of samba3 */
441 if ($this->current['SAMBAVERSION'] == 3){
442 $this->data['SERVERS']['SAMBA']= array();
443 $ldap->cd ($this->current['BASE']);
444 $ldap->search ("(objectClass=sambaDomain)");
445 while ($attrs= $ldap->fetch()){
446 $this->data['SERVERS']['SAMBA'][$attrs['sambaDomainName'][0]]= array(
447 "SID" => $attrs["sambaSID"][0],
448 "RIDBASE" => $attrs["sambaAlgorithmicRidBase"][0]);
449 }
451 /* If no samba servers are found, look for configured sid/ridbase */
452 if (count($this->data['SERVERS']['SAMBA']) == 0){
453 if (!isset($this->current["SID"]) || !isset($this->current["RIDBASE"])){
454 print_red(_("SID and/or RIDBASE missing in your configuration!"));
455 echo $_SESSION['errors'];
456 exit;
457 } else {
458 $this->data['SERVERS']['SAMBA']['DEFAULT']= array(
459 "SID" => $this->current["SID"],
460 "RIDBASE" => $this->current["RIDBASE"]);
461 }
462 }
463 }
464 }
467 function get_departments($ignore_dn= "")
468 {
469 global $config;
471 /* Initialize result hash */
472 $result= array();
473 $administrative= array();
474 $result['/']= $this->current['BASE'];
476 /* Get list of department objects */
477 $ldap= $this->get_ldap_link();
478 $ldap->cd ($this->current['BASE']);
479 $ldap->search ("(objectClass=gosaDepartment)", array("ou", "objectClass", "gosaUnitTag"));
480 while ($attrs= $ldap->fetch()){
481 $dn= $ldap->getDN();
483 /* Save administrative departments */
484 if (in_array_ics("gosaAdministrativeUnit", $attrs['objectClass']) &&
485 isset($attrs['gosaUnitTag'][0])){
486 $administrative[$dn]= $attrs['gosaUnitTag'][0];
487 }
489 if ($dn == $ignore_dn){
490 continue;
491 }
493 /* Only assign non-root departments */
494 if ($dn != $result['/']){
495 $result[convert_department_dn($dn)]= $dn;
496 }
497 }
499 $this->adepartments= $administrative;
500 $this->departments= $result;
501 }
504 function make_idepartments($max_size= 28)
505 {
506 global $config;
507 $base = $config->current['BASE'];
509 $arr = array();
511 $this->idepartments= array();
513 /* Create multidimensional array, with all departments. */
514 foreach ($this->departments as $key => $val){
516 /* remove base from dn */
517 $val2 = str_replace($base,"",$val);
519 /* Get every single ou */
520 $str = preg_replace("/ou=/","|ou=",$val2);
521 $elements = array_reverse(split("\|",$str));
523 /* Save last array position */
524 $last = &$arr;
526 /* Get array depth */
527 $cnt = count($elements);
529 /* Add last ou element of current dn to our array */
530 foreach($elements as $key => $ele){
532 /* skip enpty */
533 if(empty($ele)) continue;
535 /* Extract department name */
536 $elestr = preg_replace("/^ou=/","", $ele);
537 $elestr = preg_replace("/,$/","",$elestr);
539 /* Add to array */
540 if($key == ($cnt-2)){
541 $last[$elestr]['ENTRY'] = $val;
542 }
544 /* Set next array appending position */
545 $last = &$last[$elestr]['SUB'];
546 }
547 }
549 /* Add base entry */
550 $ret["/"]["ENTRY"] = $base;
551 $ret["/"]["SUB"] = $arr;
553 $this->idepartments= $this->generateDepartmentArray($ret,-1,$max_size);
554 }
557 /* Creates display friendly output from make_idepartments */
558 function generateDepartmentArray($arr,$depth = -1,$max_size){
559 $ret = array();
560 $depth ++;
562 /* Walk through array */
563 foreach($arr as $name => $entries){
565 /* If this department is the last in the current tree position
566 * remove it, to avoid generating output for it */
567 if(count($entries['SUB'])==0){
568 unset($entries['SUB']);
569 }
571 /* Fix name, if it contains a replace tag */
572 $name= @LDAP::fix($name);
574 /* Check if current name is too long, then cut it */
575 if(mb_strlen($name, 'UTF-8')> $max_size){
576 $name = mb_substr($name,0,($max_size-3), 'UTF-8')." ...";
577 }
579 /* Append the name to the list */
580 if(isset($entries['ENTRY'])){
581 $a = "";
582 for($i = 0 ; $i < $depth ; $i ++){
583 $a.=" ";
584 }
585 $ret[$entries['ENTRY']]=$a." ".$name;
586 }
588 /* recursive add of subdepartments */
589 if(isset($entries['SUB'])){
590 $ret = array_merge($ret,$this->generateDepartmentArray($entries['SUB'],$depth,$max_size));
591 }
592 }
594 return($ret);
595 }
597 /* This function returns all available Shares defined in this ldap
598 * There are two ways to call this function, if listboxEntry is true
599 * only name and path are attached to the array, in it is false, the whole
600 * entry will be parsed an atached to the result.
601 */
602 function getShareList($listboxEntry = false)
603 {
604 $ldap= $this->get_ldap_link();
605 $a_res = $ldap->search("(objectClass=goShareServer)",array("goExportEntry","cn"));
606 $return= array();
607 while($entry = $ldap->fetch($a_res)){
608 if(isset($entry['goExportEntry']['count'])){
609 unset($entry['goExportEntry']['count']);
610 }
611 if(isset($entry['goExportEntry'])){
612 foreach($entry['goExportEntry'] as $export){
613 $shareAttrs = split("\|",$export);
614 if($listboxEntry) {
615 $return[$shareAttrs[0]."|".$entry['cn'][0]] = $shareAttrs[0]." - ".$entry['cn'][0];
616 }else{
617 $return[$shareAttrs[0]."|".$entry['cn'][0]]['server'] = $entry['cn'][0];
618 $return[$shareAttrs[0]."|".$entry['cn'][0]]['name'] = $shareAttrs[0];
619 $return[$shareAttrs[0]."|".$entry['cn'][0]]['description'] = $shareAttrs[1];
620 $return[$shareAttrs[0]."|".$entry['cn'][0]]['type'] = $shareAttrs[2];
621 $return[$shareAttrs[0]."|".$entry['cn'][0]]['charset'] = $shareAttrs[3];
622 $return[$shareAttrs[0]."|".$entry['cn'][0]]['path'] = $shareAttrs[4];
623 $return[$shareAttrs[0]."|".$entry['cn'][0]]['option'] = $shareAttrs[5];
624 }
625 }
626 }
627 }
628 return($return);
629 }
631 /* This function returns all available ShareServer */
632 function getShareServerList()
633 {
634 $ldap= $this->get_ldap_link();
635 $a_res = $ldap->search("(&(objectClass=goShareServer)(goExportEntry=*))",array("goExportEntry","cn"));
636 $return= array();
637 while($entry = $ldap->fetch($a_res)){
638 if(isset($entry['goExportEntry']['count'])){
639 unset($entry['goExportEntry']['count']);
640 }
641 foreach($entry['goExportEntry'] as $share){
642 $a_share = split("\|",$share);
643 $sharename = $a_share[0];
644 $return[$entry['cn'][0]."|".$sharename] = $entry['cn'][0]." [".$sharename."]";
645 }
646 }
647 return($return);
648 }
650 /* Check if there's the specified bool value set in the configuration */
651 function boolValueIsTrue($section, $value)
652 {
653 $section= strtoupper($section);
654 $value= strtoupper($value);
655 if (isset($this->data[$section][$value])){
657 $data= $this->data[$section][$value];
658 if (preg_match("/^true$/i", $data) || preg_match("/yes/i", $data)){
659 return TRUE;
660 }
662 }
664 return FALSE;
665 }
667 }
669 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
670 ?>