33bc836d86b9ff021e93452545e522b1b9ed29d3
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)(cn=*)(goGlpiAdmin=*)(goGlpiDatabase=*))",array("cn","goGlpiPassword","goGlpiAdmin","goGlpiDatabase"));
370 if ($ldap->count()){
371 $attrs= $ldap->fetch();
372 if(!isset($attrs['goGlpiPassword'])){
373 $attrs['goGlpiPassword'][0] ="";
374 }
375 $this->data['SERVERS']['GLPI']= array(
376 'SERVER' => $attrs['cn'][0],
377 'LOGIN' => $attrs['goGlpiAdmin'][0],
378 'PASSWORD' => $attrs['goGlpiPassword'][0],
379 'DB' => $attrs['goGlpiDatabase'][0]);
380 }
381 /* Get logdb server */
382 $ldap->cd ($this->current['BASE']);
383 $ldap->search ("(objectClass=goLogDBServer)");
384 if ($ldap->count()){
385 $attrs= $ldap->fetch();
386 $this->data['SERVERS']['LOG']= array( 'SERVER' => $attrs['cn'][0],
387 'LOGIN' => $attrs['goLogAdmin'][0],
388 'PASSWORD' => $attrs['goLogPassword'][0]);
389 }
391 /* Get NFS server lists */
392 $tmp= array("default");
393 $ldap->cd ($this->current['BASE']);
394 $ldap->search ("(&(objectClass=goShareServer)(goExportEntry=*))");
395 while ($attrs= $ldap->fetch()){
396 for ($i= 0; $i<$attrs["goExportEntry"]["count"]; $i++){
397 if(!preg_match('/^[^|]+\|[^|]+\|NFS\|.*$/', $attrs["goExportEntry"][$i])){
398 continue;
399 }
400 $path= preg_replace ("/^[^|]+\|[^|]+\|[^|]+\|[^|]+\|([^|]+).*$/", '\1', $attrs["goExportEntry"][$i]);
401 $tmp[]= $attrs["cn"][0].":$path";
402 }
403 }
404 $this->data['SERVERS']['NFS']= $tmp;
406 /* Load Terminalservers */
407 $ldap->cd ($this->current['BASE']);
408 $ldap->search ("(objectClass=goTerminalServer)");
409 $this->data['SERVERS']['TERMINAL']= array();
410 $this->data['SERVERS']['TERMINAL'][]= "default";
412 $this->data['SERVERS']['FONT']= array();
413 $this->data['SERVERS']['FONT'][]= "default";
414 while ($attrs= $ldap->fetch()){
415 $this->data['SERVERS']['TERMINAL'][]= $attrs["cn"][0];
416 for ($i= 0; $i<$attrs["goFontPath"]["count"]; $i++){
417 $this->data['SERVERS']['FONT'][]= $attrs["goFontPath"][$i];
418 }
419 }
421 /* Ldap Server */
422 $this->data['SERVERS']['LDAP']= array();
423 $ldap->cd ($this->current['BASE']);
424 $ldap->search ("(objectClass=goLdapServer)");
425 while ($attrs= $ldap->fetch()){
426 if (isset($attrs["goLdapBase"])){
427 for ($i= 0; $i<$attrs["goLdapBase"]["count"]; $i++){
428 $this->data['SERVERS']['LDAP'][]= $attrs["cn"][0].":".$attrs["goLdapBase"][$i];
429 }
430 }
431 }
433 /* Get misc server lists */
434 $this->data['SERVERS']['SYSLOG']= array("default");
435 $this->data['SERVERS']['NTP']= array("default");
436 $ldap->cd ($this->current['BASE']);
437 $ldap->search ("(objectClass=goNtpServer)");
438 while ($attrs= $ldap->fetch()){
439 $this->data['SERVERS']['NTP'][]= $attrs["cn"][0];
440 }
441 $ldap->cd ($this->current['BASE']);
442 $ldap->search ("(objectClass=goSyslogServer)");
443 while ($attrs= $ldap->fetch()){
444 $this->data['SERVERS']['SYSLOG'][]= $attrs["cn"][0];
445 }
447 /* Get samba servers from LDAP, in case of samba3 */
448 if ($this->current['SAMBAVERSION'] == 3){
449 $this->data['SERVERS']['SAMBA']= array();
450 $ldap->cd ($this->current['BASE']);
451 $ldap->search ("(objectClass=sambaDomain)");
452 while ($attrs= $ldap->fetch()){
453 $this->data['SERVERS']['SAMBA'][$attrs['sambaDomainName'][0]]= array(
454 "SID" => $attrs["sambaSID"][0],
455 "RIDBASE" => $attrs["sambaAlgorithmicRidBase"][0]);
456 }
458 /* If no samba servers are found, look for configured sid/ridbase */
459 if (count($this->data['SERVERS']['SAMBA']) == 0){
460 if (!isset($this->current["SID"]) || !isset($this->current["RIDBASE"])){
461 print_red(_("SID and/or RIDBASE missing in your configuration!"));
462 echo $_SESSION['errors'];
463 exit;
464 } else {
465 $this->data['SERVERS']['SAMBA']['DEFAULT']= array(
466 "SID" => $this->current["SID"],
467 "RIDBASE" => $this->current["RIDBASE"]);
468 }
469 }
470 }
471 }
474 function get_departments($ignore_dn= "")
475 {
476 global $config;
478 /* Initialize result hash */
479 $result= array();
480 $administrative= array();
481 $result['/']= $this->current['BASE'];
483 /* Get list of department objects */
484 $ldap= $this->get_ldap_link();
485 $ldap->cd ($this->current['BASE']);
486 $ldap->search ("(objectClass=gosaDepartment)", array("ou", "objectClass", "gosaUnitTag"));
487 while ($attrs= $ldap->fetch()){
488 $dn= $ldap->getDN();
490 /* Save administrative departments */
491 if (in_array_ics("gosaAdministrativeUnit", $attrs['objectClass']) &&
492 isset($attrs['gosaUnitTag'][0])){
493 $administrative[$dn]= $attrs['gosaUnitTag'][0];
494 }
496 if ($dn == $ignore_dn){
497 continue;
498 }
500 /* Only assign non-root departments */
501 if ($dn != $result['/']){
502 $result[convert_department_dn($dn)]= $dn;
503 }
504 }
506 $this->adepartments= $administrative;
507 $this->departments= $result;
508 }
511 function make_idepartments($max_size= 28)
512 {
513 global $config;
514 $base = $config->current['BASE'];
516 $arr = array();
518 $this->idepartments= array();
520 /* Create multidimensional array, with all departments. */
521 foreach ($this->departments as $key => $val){
523 /* remove base from dn */
524 $val2 = str_replace($base,"",$val);
526 /* Get every single ou */
527 $str = preg_replace("/ou=/","|ou=",$val2);
528 $elements = array_reverse(split("\|",$str));
530 /* Save last array position */
531 $last = &$arr;
533 /* Get array depth */
534 $cnt = count($elements);
536 /* Add last ou element of current dn to our array */
537 foreach($elements as $key => $ele){
539 /* skip enpty */
540 if(empty($ele)) continue;
542 /* Extract department name */
543 $elestr = preg_replace("/^ou=/","", $ele);
544 $elestr = preg_replace("/,$/","",$elestr);
546 /* Add to array */
547 if($key == ($cnt-2)){
548 $last[$elestr]['ENTRY'] = $val;
549 }
551 /* Set next array appending position */
552 $last = &$last[$elestr]['SUB'];
553 }
554 }
556 /* Add base entry */
557 $ret["/"]["ENTRY"] = $base;
558 $ret["/"]["SUB"] = $arr;
560 $this->idepartments= $this->generateDepartmentArray($ret,-1,$max_size);
561 }
564 /* Creates display friendly output from make_idepartments */
565 function generateDepartmentArray($arr,$depth = -1,$max_size){
566 $ret = array();
567 $depth ++;
569 /* Walk through array */
570 foreach($arr as $name => $entries){
572 /* If this department is the last in the current tree position
573 * remove it, to avoid generating output for it */
574 if(count($entries['SUB'])==0){
575 unset($entries['SUB']);
576 }
578 /* Fix name, if it contains a replace tag */
579 $name= @LDAP::fix($name);
581 /* Check if current name is too long, then cut it */
582 if(mb_strlen($name, 'UTF-8')> $max_size){
583 $name = mb_substr($name,0,($max_size-3), 'UTF-8')." ...";
584 }
586 /* Append the name to the list */
587 if(isset($entries['ENTRY'])){
588 $a = "";
589 for($i = 0 ; $i < $depth ; $i ++){
590 $a.=" ";
591 }
592 $ret[$entries['ENTRY']]=$a." ".$name;
593 }
595 /* recursive add of subdepartments */
596 if(isset($entries['SUB'])){
597 $ret = array_merge($ret,$this->generateDepartmentArray($entries['SUB'],$depth,$max_size));
598 }
599 }
601 return($ret);
602 }
604 /* This function returns all available Shares defined in this ldap
605 * There are two ways to call this function, if listboxEntry is true
606 * only name and path are attached to the array, in it is false, the whole
607 * entry will be parsed an atached to the result.
608 */
609 function getShareList($listboxEntry = false)
610 {
611 $ldap= $this->get_ldap_link();
612 $a_res = $ldap->search("(objectClass=goShareServer)",array("goExportEntry","cn"));
613 $return= array();
614 while($entry = $ldap->fetch($a_res)){
615 if(isset($entry['goExportEntry']['count'])){
616 unset($entry['goExportEntry']['count']);
617 }
618 if(isset($entry['goExportEntry'])){
619 foreach($entry['goExportEntry'] as $export){
620 $shareAttrs = split("\|",$export);
621 if($listboxEntry) {
622 $return[$shareAttrs[0]."|".$entry['cn'][0]] = $shareAttrs[0]." - ".$entry['cn'][0];
623 }else{
624 $return[$shareAttrs[0]."|".$entry['cn'][0]]['server'] = $entry['cn'][0];
625 $return[$shareAttrs[0]."|".$entry['cn'][0]]['name'] = $shareAttrs[0];
626 $return[$shareAttrs[0]."|".$entry['cn'][0]]['description'] = $shareAttrs[1];
627 $return[$shareAttrs[0]."|".$entry['cn'][0]]['type'] = $shareAttrs[2];
628 $return[$shareAttrs[0]."|".$entry['cn'][0]]['charset'] = $shareAttrs[3];
629 $return[$shareAttrs[0]."|".$entry['cn'][0]]['path'] = $shareAttrs[4];
630 $return[$shareAttrs[0]."|".$entry['cn'][0]]['option'] = $shareAttrs[5];
631 }
632 }
633 }
634 }
635 return($return);
636 }
638 /* This function returns all available ShareServer */
639 function getShareServerList()
640 {
641 $ldap= $this->get_ldap_link();
642 $a_res = $ldap->search("(&(objectClass=goShareServer)(goExportEntry=*))",array("goExportEntry","cn"));
643 $return= array();
644 while($entry = $ldap->fetch($a_res)){
645 if(isset($entry['goExportEntry']['count'])){
646 unset($entry['goExportEntry']['count']);
647 }
648 foreach($entry['goExportEntry'] as $share){
649 $a_share = split("\|",$share);
650 $sharename = $a_share[0];
651 $return[$entry['cn'][0]."|".$sharename] = $entry['cn'][0]." [".$sharename."]";
652 }
653 }
654 return($return);
655 }
657 /* Check if there's the specified bool value set in the configuration */
658 function boolValueIsTrue($section, $value)
659 {
660 $section= strtoupper($section);
661 $value= strtoupper($value);
662 if (isset($this->data[$section][$value])){
664 $data= $this->data[$section][$value];
665 if (preg_match("/^true$/i", $data) || preg_match("/yes/i", $data)){
666 return TRUE;
667 }
669 }
671 return FALSE;
672 }
674 }
676 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
677 ?>