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 $smarty= get_smarty();
197 print_red (_("Can't bind to LDAP. Please contact the system administrator."));
198 $smarty->display (get_template_path('headers.tpl'));
199 echo '<body style="background-image:none">'.$_SESSION['errors'].'</body></html>';
200 exit();
201 }
203 if (!isset($_SESSION['size_limit'])){
204 $_SESSION['size_limit']= $this->current['SIZELIMIT'];
205 $_SESSION['size_ignore']= $this->current['SIZEIGNORE'];
206 }
208 if ($sizelimit){
209 $this->ldap->set_size_limit($_SESSION['size_limit']);
210 } else {
211 $this->ldap->set_size_limit(0);
212 }
214 /* Move referrals */
215 if (!isset($this->current['REFERRAL'])){
216 $this->ldap->referrals= array();
217 } else {
218 $this->ldap->referrals= $this->current['REFERRAL'];
219 }
221 return ($this->ldap);
222 }
224 function set_current($name)
225 {
226 $this->current= $this->data['LOCATIONS'][$name];
227 if (!isset($this->current['PEOPLE'])){
228 $this->current['PEOPLE']= "ou=people";
229 }
230 if (!isset($this->current['GROUPS'])){
231 $this->current['GROUPS']= "ou=groups";
232 }
233 if (!isset($this->current['WINSTATIONS'])){
234 $this->current['WINSTATIONS']= "ou=winstations,ou=systems";
235 }
236 if (!isset($this->current['HASH'])){
237 $this->current['HASH']= "crypt";
238 }
239 if (!isset($this->current['DNMODE'])){
240 $this->current['DNMODE']= "cn";
241 }
242 if (!isset($this->current['MINID'])){
243 $this->current['MINID']= 100;
244 }
245 if (!isset($this->current['SIZELIMIT'])){
246 $this->current['SIZELIMIT']= 200;
247 }
248 if (!isset($this->current['SIZEINGORE'])){
249 $this->current['SIZEIGNORE']= TRUE;
250 } else {
251 if (preg_match("/true/i", $this->current['SIZEIGNORE'])){
252 $this->current['SIZEIGNORE']= TRUE;
253 } else {
254 $this->current['SIZEIGNORE']= FALSE;
255 }
256 }
258 /* Sort referrals, if present */
259 if (isset ($this->current['REFERRAL'])){
260 $bases= array();
261 $servers= array();
262 foreach ($this->current['REFERRAL'] as $ref){
263 $server= preg_replace('%^(.*)/[^/]+$%', '\\1', $ref['URL']);
264 $base= preg_replace('%^.*/([^/]+)$%', '\\1', $ref['URL']);
265 $bases[$base]= strlen($base);
266 $servers[$base]= $server;
267 }
268 asort($bases);
269 reset($bases);
270 }
272 /* SERVER not defined? Load the one with the shortest base */
273 if (!isset($this->current['SERVER'])){
274 $this->current['SERVER']= $servers[key($bases)];
275 }
277 /* BASE not defined? Load the one with the shortest base */
278 if (!isset($this->current['BASE'])){
279 $this->current['BASE']= key($bases);
280 }
282 /* Convert BASE to have escaped special characters */
283 $this->current['BASE']= @LDAP::convert($this->current['BASE']);
285 /* Parse LDAP referral informations */
286 if (!isset($this->current['ADMIN']) || !isset($this->current['PASSWORD'])){
287 $url= $this->current['SERVER'];
288 $referral= $this->current['REFERRAL'][$url];
289 $this->current['ADMIN']= $referral['ADMIN'];
290 $this->current['PASSWORD']= $referral['PASSWORD'];
291 }
293 /* Load server informations */
294 $this->load_servers();
295 }
297 function load_servers ()
298 {
299 /* Only perform actions if current is set */
300 if ($this->current == NULL){
301 return;
302 }
304 /* Fill imap servers */
305 $ldap= $this->get_ldap_link();
306 $ldap->cd ($this->current['BASE']);
307 $ldap->search ("(objectClass=goImapServer)");
309 $this->data['SERVERS']['IMAP']= array();
310 error_reporting(0);
311 while ($attrs= $ldap->fetch()){
312 $name= $attrs['goImapName'][0];
313 $this->data['SERVERS']['IMAP'][$name]= array( "connect" => $attrs['goImapConnect'][0],
314 "admin" => $attrs['goImapAdmin'][0],
315 "password" => $attrs['goImapPassword'][0],
316 "sieve_server" => $attrs['goImapSieveServer'][0],
317 "sieve_port" => $attrs['goImapSievePort'][0]);
318 }
319 error_reporting(E_ALL);
321 /* Get kerberos server. FIXME: only one is supported currently */
322 $ldap->cd ($this->current['BASE']);
323 $ldap->search ("(objectClass=goKrbServer)");
324 if ($ldap->count()){
325 $attrs= $ldap->fetch();
326 $this->data['SERVERS']['KERBEROS']= array( 'SERVER' => $attrs['cn'][0],
327 'REALM' => $attrs['goKrbRealm'][0],
328 'ADMIN' => $attrs['goKrbAdmin'][0],
329 'PASSWORD' => $attrs['goKrbPassword'][0]);
330 }
332 /* Get cups server. FIXME: only one is supported currently */
333 $ldap->cd ($this->current['BASE']);
334 $ldap->search ("(objectClass=goCupsServer)");
335 if ($ldap->count()){
336 $attrs= $ldap->fetch();
337 $this->data['SERVERS']['CUPS']= $attrs['cn'][0];
338 }
340 /* Get fax server. FIXME: only one is supported currently */
341 $ldap->cd ($this->current['BASE']);
342 $ldap->search ("(objectClass=goFaxServer)");
343 if ($ldap->count()){
344 $attrs= $ldap->fetch();
345 $this->data['SERVERS']['FAX']= array( 'SERVER' => $attrs['cn'][0],
346 'LOGIN' => $attrs['goFaxAdmin'][0],
347 'PASSWORD' => $attrs['goFaxPassword'][0]);
348 }
350 /* Get asterisk servers */
351 $ldap->cd ($this->current['BASE']);
352 $ldap->search ("(objectClass=goFonServer)");
353 if ($ldap->count()){
354 $attrs= $ldap->fetch();
355 $this->data['SERVERS']['FON']= array(
356 'SERVER' => $attrs['cn'][0],
357 'LOGIN' => $attrs['goFonAdmin'][0],
358 'PASSWORD' => $attrs['goFonPassword'][0],
359 'DB' => "gophone",
360 'SIP_TABLE' => "sip_users",
361 'EXT_TABLE' => "extensions",
362 'VOICE_TABLE' => "voicemail_users",
363 'QUEUE_TABLE' => "queues",
364 'QUEUE_MEMBER_TABLE' => "queue_members");
365 }
367 /* Get asterisk servers */
368 $ldap->cd ($this->current['BASE']);
369 $ldap->search ("(objectClass=goGlpiServer)");
370 if ($ldap->count()){
371 $attrs= $ldap->fetch();
372 $this->data['SERVERS']['GLPI']= array(
373 'SERVER' => $attrs['cn'][0],
374 'LOGIN' => $attrs['goGlpiAdmin'][0],
375 'PASSWORD' => $attrs['goGlpiPassword'][0],
376 'DB' => $attrs['goGlpiDatabase'][0]);
377 }
378 /* Get logdb server */
379 $ldap->cd ($this->current['BASE']);
380 $ldap->search ("(objectClass=goLogDBServer)");
381 if ($ldap->count()){
382 $attrs= $ldap->fetch();
383 $this->data['SERVERS']['LOG']= array( 'SERVER' => $attrs['cn'][0],
384 'LOGIN' => $attrs['goLogAdmin'][0],
385 'PASSWORD' => $attrs['goLogPassword'][0]);
386 }
388 /* Get NFS server lists */
389 $tmp= array("default");
390 $ldap->cd ($this->current['BASE']);
391 $ldap->search ("(&(objectClass=goShareServer)(goExportEntry=*))");
392 while ($attrs= $ldap->fetch()){
393 for ($i= 0; $i<$attrs["goExportEntry"]["count"]; $i++){
394 if(!preg_match('/^[^|]+\|[^|]+\|NFS\|.*$/', $attrs["goExportEntry"][$i])){
395 continue;
396 }
397 $path= preg_replace ("/^[^|]+\|[^|]+\|[^|]+\|[^|]+\|([^|]+).*$/", '\1', $attrs["goExportEntry"][$i]);
398 $tmp[]= $attrs["cn"][0].":$path";
399 }
400 }
401 $this->data['SERVERS']['NFS']= $tmp;
403 /* Load Terminalservers */
404 $ldap->cd ($this->current['BASE']);
405 $ldap->search ("(objectClass=goTerminalServer)");
406 $this->data['SERVERS']['TERMINAL']= array();
407 $this->data['SERVERS']['TERMINAL'][]= "default";
409 $this->data['SERVERS']['FONT']= array();
410 $this->data['SERVERS']['FONT'][]= "default";
411 while ($attrs= $ldap->fetch()){
412 $this->data['SERVERS']['TERMINAL'][]= $attrs["cn"][0];
413 for ($i= 0; $i<$attrs["goFontPath"]["count"]; $i++){
414 $this->data['SERVERS']['FONT'][]= $attrs["goFontPath"][$i];
415 }
416 }
418 /* Ldap Server */
419 $this->data['SERVERS']['LDAP']= array();
420 $ldap->cd ($this->current['BASE']);
421 $ldap->search ("(objectClass=goLdapServer)");
422 while ($attrs= $ldap->fetch()){
423 if (isset($attrs["goLdapBase"])){
424 for ($i= 0; $i<$attrs["goLdapBase"]["count"]; $i++){
425 $this->data['SERVERS']['LDAP'][]= $attrs["cn"][0].":".$attrs["goLdapBase"][$i];
426 }
427 }
428 }
430 /* Get misc server lists */
431 $this->data['SERVERS']['SYSLOG']= array("default");
432 $this->data['SERVERS']['NTP']= array("default");
433 $ldap->cd ($this->current['BASE']);
434 $ldap->search ("(objectClass=goNtpServer)");
435 while ($attrs= $ldap->fetch()){
436 $this->data['SERVERS']['NTP'][]= $attrs["cn"][0];
437 }
438 $ldap->cd ($this->current['BASE']);
439 $ldap->search ("(objectClass=goSyslogServer)");
440 while ($attrs= $ldap->fetch()){
441 $this->data['SERVERS']['SYSLOG'][]= $attrs["cn"][0];
442 }
444 /* Get samba servers from LDAP, in case of samba3 */
445 if ($this->current['SAMBAVERSION'] == 3){
446 $this->data['SERVERS']['SAMBA']= array();
447 $ldap->cd ($this->current['BASE']);
448 $ldap->search ("(objectClass=sambaDomain)");
449 while ($attrs= $ldap->fetch()){
450 $this->data['SERVERS']['SAMBA'][$attrs['sambaDomainName'][0]]= array(
451 "SID" => $attrs["sambaSID"][0],
452 "RIDBASE" => $attrs["sambaAlgorithmicRidBase"][0]);
453 }
455 /* If no samba servers are found, look for configured sid/ridbase */
456 if (count($this->data['SERVERS']['SAMBA']) == 0){
457 if (!isset($this->current["SID"]) || !isset($this->current["RIDBASE"])){
458 print_red(_("SID and/or RIDBASE missing in your configuration!"));
459 echo $_SESSION['errors'];
460 exit;
461 } else {
462 $this->data['SERVERS']['SAMBA']['DEFAULT']= array(
463 "SID" => $this->current["SID"],
464 "RIDBASE" => $this->current["RIDBASE"]);
465 }
466 }
467 }
468 }
471 function get_departments($ignore_dn= "")
472 {
473 global $config;
475 /* Initialize result hash */
476 $result= array();
477 $administrative= array();
478 $result['/']= $this->current['BASE'];
480 /* Get list of department objects */
481 $ldap= $this->get_ldap_link();
482 $ldap->cd ($this->current['BASE']);
483 $ldap->search ("(objectClass=gosaDepartment)", array("ou", "objectClass", "gosaUnitTag"));
484 while ($attrs= $ldap->fetch()){
485 $dn= $ldap->getDN();
487 /* Save administrative departments */
488 if (in_array_ics("gosaAdministrativeUnit", $attrs['objectClass']) &&
489 isset($attrs['gosaUnitTag'][0])){
490 $administrative[$dn]= $attrs['gosaUnitTag'][0];
491 }
493 if ($dn == $ignore_dn){
494 continue;
495 }
497 /* Only assign non-root departments */
498 if ($dn != $result['/']){
499 $result[convert_department_dn($dn)]= $dn;
500 }
501 }
503 $this->adepartments= $administrative;
504 $this->departments= $result;
505 }
508 function make_idepartments($max_size= 28)
509 {
510 global $config;
511 $base = $config->current['BASE'];
513 $arr = array();
515 $this->idepartments= array();
517 /* Create multidimensional array, with all departments. */
518 foreach ($this->departments as $key => $val){
520 /* remove base from dn */
521 $val2 = str_replace($base,"",$val);
523 /* Get every single ou */
524 $str = preg_replace("/ou=/","|ou=",$val2);
525 $elements = array_reverse(split("\|",$str));
527 /* Save last array position */
528 $last = &$arr;
530 /* Get array depth */
531 $cnt = count($elements);
533 /* Add last ou element of current dn to our array */
534 foreach($elements as $key => $ele){
536 /* skip enpty */
537 if(empty($ele)) continue;
539 /* Extract department name */
540 $elestr = preg_replace("/^ou=/","", $ele);
541 $elestr = preg_replace("/,$/","",$elestr);
543 /* Add to array */
544 if($key == ($cnt-2)){
545 $last[$elestr]['ENTRY'] = $val;
546 }
548 /* Set next array appending position */
549 $last = &$last[$elestr]['SUB'];
550 }
551 }
553 /* Add base entry */
554 $ret["/"]["ENTRY"] = $base;
555 $ret["/"]["SUB"] = $arr;
557 $this->idepartments= $this->generateDepartmentArray($ret,-1,$max_size);
558 }
561 /* Creates display friendly output from make_idepartments */
562 function generateDepartmentArray($arr,$depth = -1,$max_size){
563 $ret = array();
564 $depth ++;
566 /* Walk through array */
567 foreach($arr as $name => $entries){
569 /* If this department is the last in the current tree position
570 * remove it, to avoid generating output for it */
571 if(count($entries['SUB'])==0){
572 unset($entries['SUB']);
573 }
575 /* Fix name, if it contains a replace tag */
576 $name= @LDAP::fix($name);
578 /* Check if current name is too long, then cut it */
579 if(mb_strlen($name, 'UTF-8')> $max_size){
580 $name = mb_substr($name,0,($max_size-3), 'UTF-8')." ...";
581 }
583 /* Append the name to the list */
584 if(isset($entries['ENTRY'])){
585 $a = "";
586 for($i = 0 ; $i < $depth ; $i ++){
587 $a.=" ";
588 }
589 $ret[$entries['ENTRY']]=$a." ".$name;
590 }
592 /* recursive add of subdepartments */
593 if(isset($entries['SUB'])){
594 $ret = array_merge($ret,$this->generateDepartmentArray($entries['SUB'],$depth,$max_size));
595 }
596 }
598 return($ret);
599 }
601 /* This function returns all available Shares defined in this ldap
602 * There are two ways to call this function, if listboxEntry is true
603 * only name and path are attached to the array, in it is false, the whole
604 * entry will be parsed an atached to the result.
605 */
606 function getShareList($listboxEntry = false)
607 {
608 $ldap= $this->get_ldap_link();
609 $a_res = $ldap->search("(objectClass=goShareServer)",array("goExportEntry","cn"));
610 $return= array();
611 while($entry = $ldap->fetch($a_res)){
612 if(isset($entry['goExportEntry']['count'])){
613 unset($entry['goExportEntry']['count']);
614 }
615 if(isset($entry['goExportEntry'])){
616 foreach($entry['goExportEntry'] as $export){
617 $shareAttrs = split("\|",$export);
618 if($listboxEntry) {
619 $return[$shareAttrs[0]."|".$entry['cn'][0]] = $shareAttrs[0]." - ".$entry['cn'][0];
620 }else{
621 $return[$shareAttrs[0]."|".$entry['cn'][0]]['server'] = $entry['cn'][0];
622 $return[$shareAttrs[0]."|".$entry['cn'][0]]['name'] = $shareAttrs[0];
623 $return[$shareAttrs[0]."|".$entry['cn'][0]]['description'] = $shareAttrs[1];
624 $return[$shareAttrs[0]."|".$entry['cn'][0]]['type'] = $shareAttrs[2];
625 $return[$shareAttrs[0]."|".$entry['cn'][0]]['charset'] = $shareAttrs[3];
626 $return[$shareAttrs[0]."|".$entry['cn'][0]]['path'] = $shareAttrs[4];
627 $return[$shareAttrs[0]."|".$entry['cn'][0]]['option'] = $shareAttrs[5];
628 }
629 }
630 }
631 }
632 return($return);
633 }
635 /* This function returns all available ShareServer */
636 function getShareServerList()
637 {
638 $ldap= $this->get_ldap_link();
639 $a_res = $ldap->search("(&(objectClass=goShareServer)(goExportEntry=*))",array("goExportEntry","cn"));
640 $return= array();
641 while($entry = $ldap->fetch($a_res)){
642 if(isset($entry['goExportEntry']['count'])){
643 unset($entry['goExportEntry']['count']);
644 }
645 foreach($entry['goExportEntry'] as $share){
646 $a_share = split("\|",$share);
647 $sharename = $a_share[0];
648 $return[$entry['cn'][0]."|".$sharename] = $entry['cn'][0]." [".$sharename."]";
649 }
650 }
651 return($return);
652 }
654 /* Check if there's the specified bool value set in the configuration */
655 function boolValueIsTrue($section, $value)
656 {
657 $section= strtoupper($section);
658 $value= strtoupper($value);
659 if (isset($this->data[$section][$value])){
661 $data= $this->data[$section][$value];
662 if (preg_match("/^true$/i", $data) || preg_match("/yes/i", $data)){
663 return TRUE;
664 }
666 }
668 return FALSE;
669 }
671 }
673 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
674 ?>