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 /* Configuration file location */
22 define ("CONFIG_DIR", "/etc/gosa");
23 define ("CONFIG_TEMPLATE_DIR", "../contrib/");
24 define ("HELP_BASEDIR", "/home/cajus/");
26 /* Define globals for revision comparing */
27 $svn_path = '$HeadURL$';
28 $svn_revision = '$Revision$';
30 /* Include required files */
31 require_once ("class_ldap.inc");
32 require_once ("class_config.inc");
33 require_once ("class_userinfo.inc");
34 require_once ("class_plugin.inc");
35 require_once ("class_pluglist.inc");
36 require_once ("class_tabs.inc");
37 require_once ("class_mail-methods.inc");
38 require_once("class_password-methods.inc");
39 require_once ("functions_debug.inc");
41 /* Define constants for debugging */
42 define ("DEBUG_TRACE", 1);
43 define ("DEBUG_LDAP", 2);
44 define ("DEBUG_MYSQL", 4);
45 define ("DEBUG_SHELL", 8);
46 define ("DEBUG_POST", 16);
47 define ("DEBUG_SESSION",32);
48 define ("DEBUG_CONFIG", 64);
50 /* Rewrite german 'umlauts' and spanish 'accents'
51 to get better results */
52 $REWRITE= array( "ä" => "ae",
53 "ö" => "oe",
54 "ü" => "ue",
55 "Ä" => "Ae",
56 "Ö" => "Oe",
57 "Ü" => "Ue",
58 "ß" => "ss",
59 "á" => "a",
60 "é" => "e",
61 "í" => "i",
62 "ó" => "o",
63 "ú" => "u",
64 "Á" => "A",
65 "É" => "E",
66 "Í" => "I",
67 "Ó" => "O",
68 "Ú" => "U",
69 "ñ" => "ny",
70 "Ñ" => "Ny" );
73 /* Function to include all class_ files starting at a
74 given directory base */
75 function get_dir_list($folder= ".")
76 {
77 $currdir=getcwd();
78 if ($folder){
79 chdir("$folder");
80 }
82 $dh = opendir(".");
83 while(false !== ($file = readdir($dh))){
85 // Smarty is included by include/php_setup.inc require("smarty/Smarty.class.php");
86 // Skip all files and dirs in "./.svn/" we don't need any information from them
87 // Skip all Template, so they won't be checked twice in the following preg_matches
88 // Skip . / ..
90 // Result : from 1023 ms to 490 ms i think thats great...
91 if(preg_match("/.*\.svn.*/i",$file)||preg_match("/.*smarty.*/i",$file)||preg_match("/.*\.tpl.*/",$file)||($file==".")||($file==".."))
92 continue;
95 /* Recurse through all "common" directories */
96 if(is_dir($file) &&$file!="CVS"){
97 get_dir_list($file);
98 continue;
99 }
101 /* Include existing class_ files */
102 if (!is_dir($file) && preg_match("/^class_.*\.inc$/", $file)) {
103 require_once($file);
104 }
105 }
107 closedir($dh);
108 chdir($currdir);
109 }
112 /* Create seed with microseconds */
113 function make_seed() {
114 list($usec, $sec) = explode(' ', microtime());
115 return (float) $sec + ((float) $usec * 100000);
116 }
119 /* Debug level action */
120 function DEBUG($level, $line, $function, $file, $data, $info="")
121 {
122 if ($_SESSION['DEBUGLEVEL'] & $level){
123 $output= "DEBUG[$level] ";
124 if ($function != ""){
125 $output.= "($file:$function():$line) - $info: ";
126 } else {
127 $output.= "($file:$line) - $info: ";
128 }
129 echo $output;
130 if (is_array($data)){
131 print_a($data);
132 } else {
133 echo "'$data'";
134 }
135 echo "<br>";
136 }
137 }
140 /* Simple function to get browser language and convert it to
141 xx_XY needed by locales. Ignores sublanguages and weights. */
142 function get_browser_language()
143 {
144 global $BASE_DIR;
146 /* Get list of languages */
147 if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])){
148 $lang= preg_replace("/\s+/", "", $_SERVER['HTTP_ACCEPT_LANGUAGE']);
149 $languages= split (',', $lang);
150 $languages[]= "C";
151 } else {
152 $languages= array("C");
153 }
155 /* Walk through languages and get first supported */
156 foreach ($languages as $val){
158 /* Strip off weight */
159 $lang= preg_replace("/;q=.*$/i", "", $val);
161 /* Simplify sub language handling */
162 $lang= preg_replace("/-.*$/", "", $lang);
164 /* Cancel loop if available in GOsa, or the last
165 entry has been reached */
166 if (is_dir("$BASE_DIR/locale/$lang")){
167 break;
168 }
169 }
171 return (strtolower($lang)."_".strtoupper($lang));
172 }
175 /* Rewrite ui object to another dn */
176 function change_ui_dn($dn, $newdn)
177 {
178 $ui= $_SESSION['ui'];
179 if ($ui->dn == $dn){
180 $ui->dn= $newdn;
181 $_SESSION['ui']= $ui;
182 }
183 }
186 /* Return theme path for specified file */
187 function get_template_path($filename= '', $plugin= FALSE, $path= "")
188 {
189 global $config, $BASE_DIR;
191 if (!@isset($config->data['MAIN']['THEME'])){
192 $theme= 'default';
193 } else {
194 $theme= $config->data['MAIN']['THEME'];
195 }
197 /* Return path for empty filename */
198 if ($filename == ''){
199 return ("themes/$theme/");
200 }
202 /* Return plugin dir or root directory? */
203 if ($plugin){
204 if ($path == ""){
205 $nf= preg_replace("!^".$BASE_DIR."/!", "", $_SESSION['plugin_dir']);
206 } else {
207 $nf= preg_replace("!^".$BASE_DIR."/!", "", $path);
208 }
209 if (file_exists("$BASE_DIR/ihtml/themes/$theme/$nf")){
210 return ("$BASE_DIR/ihtml/themes/$theme/$nf/$filename");
211 }
212 if (file_exists("$BASE_DIR/ihtml/themes/default/$nf")){
213 return ("$BASE_DIR/ihtml/themes/default/$nf/$filename");
214 }
215 if ($path == ""){
216 return ($_SESSION['plugin_dir']."/$filename");
217 } else {
218 return ($path."/$filename");
219 }
220 } else {
221 if (file_exists("themes/$theme/$filename")){
222 return ("themes/$theme/$filename");
223 }
224 if (file_exists("$BASE_DIR/ihtml/themes/$theme/$filename")){
225 return ("$BASE_DIR/ihtml/themes/$theme/$filename");
226 }
227 if (file_exists("themes/default/$filename")){
228 return ("themes/default/$filename");
229 }
230 if (file_exists("$BASE_DIR/ihtml/themes/default/$filename")){
231 return ("$BASE_DIR/ihtml/themes/default/$filename");
232 }
233 return ($filename);
234 }
235 }
238 function array_remove_entries($needles, $haystack)
239 {
240 $tmp= array();
242 /* Loop through entries to be removed */
243 foreach ($haystack as $entry){
244 if (!in_array($entry, $needles)){
245 $tmp[]= $entry;
246 }
247 }
249 return ($tmp);
250 }
253 function gosa_log ($message)
254 {
255 global $ui;
257 /* Preset to something reasonable */
258 $username= " unauthenticated";
260 /* Replace username if object is present */
261 if (isset($ui)){
262 if ($ui->username != ""){
263 $username= "[$ui->username]";
264 } else {
265 $username= "unknown";
266 }
267 }
269 syslog(LOG_INFO,"GOsa$username: $message");
270 }
273 function ldap_init ($server, $base, $binddn='', $pass='')
274 {
275 global $config;
277 $ldap = new LDAP ($binddn, $pass, $server, isset($config->current['RECURSIVE']) && $config->current['RECURSIVE'] == "true",
278 isset($config->current['TLS']) && $config->current['TLS'] == "true");
280 /* Sadly we've no proper return values here. Use the error message instead. */
281 if (!preg_match("/Success/i", $ldap->error)){
282 print_red(sprintf(_("Error when connecting the LDAP. Server said '%s'."),
283 $ldap->get_error()));
284 echo $_SESSION['errors'];
286 /* Hard error. We'd like to use the LDAP, anyway... */
287 exit;
288 }
290 /* Preset connection base to $base and return to caller */
291 $ldap->cd ($base);
292 return $ldap;
293 }
296 function ldap_login_user ($username, $password)
297 {
298 global $config;
300 /* look through the entire ldap */
301 $ldap = $config->get_ldap_link();
302 if (!preg_match("/Success/i", $ldap->error)){
303 print_red(sprintf(_("User login failed. LDAP server said '%s'."), $ldap->get_error()));
304 echo $_SESSION['errors'];
305 exit;
306 }
307 $ldap->cd($config->current['BASE']);
308 $ldap->search("(&(uid=$username)(objectClass=gosaAccount))", array("uid"));
310 /* get results, only a count of 1 is valid */
311 switch ($ldap->count()){
313 /* user not found */
314 case 0: return (NULL);
316 /* valid uniq user */
317 case 1:
318 break;
320 /* found more than one matching id */
321 default:
322 print_red(_("Username / UID is not unique. Please check your LDAP database."));
323 return (NULL);
324 }
326 /* LDAP schema is not case sensitive. Perform additional check. */
327 $attrs= $ldap->fetch();
328 if ($attrs['uid'][0] != $username){
329 return(NULL);
330 }
332 /* got user dn, fill acl's */
333 $ui= new userinfo($config, $ldap->getDN());
334 $ui->username= $username;
336 /* password check, bind as user with supplied password */
337 $ldap->disconnect();
338 $ldap= new LDAP($ui->dn, $password, $config->current['SERVER'],
339 isset($config->current['RECURSIVE']) &&
340 $config->current['RECURSIVE'] == "true",
341 isset($config->current['TLS'])
342 && $config->current['TLS'] == "true");
343 if (!preg_match("/Success/i", $ldap->error)){
344 return (NULL);
345 }
347 /* Username is set, load subtreeACL's now */
348 $ui->loadACL();
350 return ($ui);
351 }
354 function add_lock ($object, $user)
355 {
356 global $config;
358 /* Just a sanity check... */
359 if ($object == "" || $user == ""){
360 print_red(_("Error while adding a lock. Parameters are not set correctly, please check the source!"));
361 return;
362 }
364 /* Check for existing entries in lock area */
365 $ldap= $config->get_ldap_link();
366 $ldap->cd ($config->current['CONFIG']);
367 $ldap->search("(&(objectClass=gosaLockEntry)(gosaUser=$user)(gosaObject=$object))",
368 array("gosaUser"));
369 if (!preg_match("/Success/i", $ldap->error)){
370 print_red (sprintf(_("Can't set locking information in LDAP database. Please check the 'config' entry in gosa.conf! LDAP server says '%s'."), $ldap->get_error()));
371 return;
372 }
374 /* Add lock if none present */
375 if ($ldap->count() == 0){
376 $attrs= array();
377 $name= md5($object);
378 $ldap->cd("cn=$name,".$config->current['CONFIG']);
379 $attrs["objectClass"] = "gosaLockEntry";
380 $attrs["gosaUser"] = $user;
381 $attrs["gosaObject"] = $object;
382 $attrs["cn"] = "$name";
383 $ldap->add($attrs);
384 if (!preg_match("/Success/i", $ldap->error)){
385 print_red(sprintf(_("Adding a lock failed. LDAP server says '%s'."),
386 $ldap->get_error()));
387 return;
388 }
389 }
390 }
393 function del_lock ($object)
394 {
395 global $config;
397 /* Sanity check */
398 if ($object == ""){
399 return;
400 }
402 /* Check for existance and remove the entry */
403 $ldap= $config->get_ldap_link();
404 $ldap->cd ($config->current['CONFIG']);
405 $ldap->search ("(&(objectClass=gosaLockEntry)(gosaObject=$object))", array("gosaObject"));
406 $attrs= $ldap->fetch();
407 if ($ldap->getDN() != "" && preg_match("/Success/i", $ldap->error)){
408 $ldap->rmdir ($ldap->getDN());
410 if (!preg_match("/Success/i", $ldap->error)){
411 print_red(sprintf(_("Removing a lock failed. LDAP server says '%s'."),
412 $ldap->get_error()));
413 return;
414 }
415 }
416 }
419 function del_user_locks($userdn)
420 {
421 global $config;
423 /* Get LDAP ressources */
424 $ldap= $config->get_ldap_link();
425 $ldap->cd ($config->current['CONFIG']);
427 /* Remove all objects of this user, drop errors silently in this case. */
428 $ldap->search("(&(objectClass=gosaLockEntry)(gosaUser=$userdn))", array("gosaUser"));
429 while ($attrs= $ldap->fetch()){
430 $ldap->rmdir($attrs['dn']);
431 }
432 }
435 function get_lock ($object)
436 {
437 global $config;
439 /* Sanity check */
440 if ($object == ""){
441 print_red(_("Getting the lock from LDAP failed. Parameters are not set correctly, please check the source!"));
442 return("");
443 }
445 /* Get LDAP link, check for presence of the lock entry */
446 $user= "";
447 $ldap= $config->get_ldap_link();
448 $ldap->cd ($config->current['CONFIG']);
449 $ldap->search("(&(objectClass=gosaLockEntry)(gosaObject=$object))", array("gosaUser"));
450 if (!preg_match("/Success/i", $ldap->error)){
451 print_red (_("Can't get locking information in LDAP database. Please check the 'config' entry in gosa.conf!"));
452 return("");
453 }
455 /* Check for broken locking information in LDAP */
456 if ($ldap->count() > 1){
458 /* Hmm. We're removing broken LDAP information here and issue a warning. */
459 print_red(_("Found multiple locks for object to be locked. This should not be possible - cleaning up multiple references."));
461 /* Clean up these references now... */
462 while ($attrs= $ldap->fetch()){
463 $ldap->rmdir($attrs['dn']);
464 }
466 return("");
468 } elseif ($ldap->count() == 1){
469 $attrs = $ldap->fetch();
470 $user= $attrs['gosaUser'][0];
471 }
473 return ($user);
474 }
477 function get_list2($subtreeACL, $filter, $subsearch= TRUE, $base="", $attrs= array(), $flag= FALSE)
478 {
479 global $config;
481 /* Base the search on default base if not set */
482 $ldap= $config->get_ldap_link($flag);
483 if ($base == ""){
484 $ldap->cd ($config->current['BASE']);
485 } else {
486 $ldap->cd ($base);
487 }
489 /* Perform ONE or SUB scope searches? */
490 $ldap->ls ($filter);
492 /* Check for size limit exceeded messages for GUI feedback */
493 if (preg_match("/size limit/i", $ldap->error)){
494 $_SESSION['limit_exceeded']= TRUE;
495 } else {
496 $_SESSION['limit_exceeded']= FALSE;
497 }
498 $result= array();
501 /* Crawl through reslut entries and perform the migration to the
502 result array */
503 while($attrs = $ldap->fetch()) {
504 $dn= preg_replace("/[ ]*,[ ]*/", ",", $ldap->getDN());
505 foreach ($subtreeACL as $key => $value){
506 if (preg_match("/$key/", $dn)){
507 $attrs["dn"]= convert_department_dn($dn);
508 $result[]= $attrs;
509 break;
510 }
511 }
512 }
515 return ($result);
517 }
519 function get_list($subtreeACL, $filter, $subsearch= TRUE, $base="", $attrs= array(), $flag= FALSE)
520 {
521 global $config;
523 /* Base the search on default base if not set */
524 $ldap= $config->get_ldap_link($flag);
525 if ($base == ""){
526 $ldap->cd ($config->current['BASE']);
527 } else {
528 $ldap->cd ($base);
529 }
531 /* Perform ONE or SUB scope searches? */
532 if ($subsearch) {
533 $ldap->search ($filter, $attrs);
534 } else {
535 $ldap->ls ($filter);
536 }
538 /* Check for size limit exceeded messages for GUI feedback */
539 if (preg_match("/size limit/i", $ldap->error)){
540 $_SESSION['limit_exceeded']= TRUE;
541 } else {
542 $_SESSION['limit_exceeded']= FALSE;
543 }
545 /* Crawl through reslut entries and perform the migration to the
546 result array */
547 $result= array();
548 while($attrs = $ldap->fetch()) {
549 $dn= preg_replace("/[ ]*,[ ]*/", ",", $ldap->getDN());
550 foreach ($subtreeACL as $key => $value){
551 if (preg_match("/$key/", $dn)){
552 $attrs["dn"]= $dn;
553 $result[]= $attrs;
554 break;
555 }
556 }
557 }
559 return ($result);
560 }
563 function check_sizelimit()
564 {
565 /* Ignore dialog? */
566 if (isset($_SESSION['size_ignore']) && $_SESSION['size_ignore']){
567 return ("");
568 }
570 /* Eventually show dialog */
571 if (isset($_SESSION['limit_exceeded']) && $_SESSION['limit_exceeded']){
572 $smarty= get_smarty();
573 $smarty->assign('warning', sprintf(_("The size limit of %d entries is exceed!"),
574 $_SESSION['size_limit']));
575 $smarty->assign('limit_message', sprintf(_("Set the new size limit to %s and show me this message if the limit still exceeds"), '<input type="text" name="new_limit" maxlength="10" size="5" value="'.($_SESSION['size_limit']+100).'">'));
576 return($smarty->fetch(get_template_path('sizelimit.tpl')));
577 }
579 return ("");
580 }
583 function print_sizelimit_warning()
584 {
585 if (isset($_SESSION['size_limit']) && $_SESSION['size_limit'] >= 10000000 ||
586 (isset($_SESSION['limit_exceeded']) && $_SESSION['limit_exceeded'])){
587 $config= "<input type='submit' name='edit_sizelimit' value="._("Configure").">";
588 } else {
589 $config= "";
590 }
591 if (isset($_SESSION['limit_exceeded']) && $_SESSION['limit_exceeded']){
592 return ("("._("incomplete").") $config");
593 }
594 return ("");
595 }
598 function eval_sizelimit()
599 {
600 if (isset($_POST['set_size_action'])){
602 /* User wants new size limit? */
603 if (is_id($_POST['new_limit']) &&
604 isset($_POST['action']) && $_POST['action']=="newlimit"){
606 $_SESSION['size_limit']= validate($_POST['new_limit']);
607 $_SESSION['size_ignore']= FALSE;
608 }
610 /* User wants no limits? */
611 if (isset($_POST['action']) && $_POST['action']=="ignore"){
612 $_SESSION['size_limit']= 0;
613 $_SESSION['size_ignore']= TRUE;
614 }
616 /* User wants incomplete results */
617 if (isset($_POST['action']) && $_POST['action']=="limited"){
618 $_SESSION['size_ignore']= TRUE;
619 }
620 }
622 /* Allow fallback to dialog */
623 if (isset($_POST['edit_sizelimit'])){
624 $_SESSION['size_ignore']= FALSE;
625 }
626 }
629 function get_permissions ($dn, $subtreeACL)
630 {
631 global $config;
633 $base= $config->current['BASE'];
634 $tmp= "d,".$dn;
635 $sacl= array();
637 /* Sort subacl's for lenght to simplify matching
638 for subtrees */
639 foreach ($subtreeACL as $key => $value){
640 $sacl[$key]= strlen($key);
641 }
642 arsort ($sacl);
643 reset ($sacl);
645 /* Successively remove leading parts of the dn's until
646 it doesn't contain commas anymore */
647 while (preg_match('/,/', $tmp)){
648 $tmp= ltrim(strstr($tmp, ","), ",");
650 /* Check for acl that may apply */
651 foreach ($sacl as $key => $value){
652 if (preg_match("/$key$/", $tmp)){
653 return ($subtreeACL[$key]);
654 }
655 }
656 }
658 return array("");
659 }
662 function get_module_permission($acl_array, $module, $dn)
663 {
664 global $ui;
666 $final= "";
667 foreach($acl_array as $acl){
669 /* Check for selfflag (!) in ACL to determine if
670 the user is allowed to change parts of his/her
671 own account */
672 if (preg_match("/^!/", $acl)){
673 if ($dn != "" && $dn != $ui->dn){
675 /* No match for own DN, give up on this ACL */
676 continue;
678 } else {
680 /* Matches own DN, remove the selfflag */
681 $acl= preg_replace("/^!/", "", $acl);
683 }
684 }
686 /* Remove leading garbage */
687 $acl= preg_replace("/^:/", "", $acl);
689 /* Discover if we've access to the submodule by comparing
690 all allowed submodules specified in the ACL */
691 $tmp= split(",", $acl);
692 foreach ($tmp as $mod){
693 if (preg_match("/$module#/", $mod)){
694 $final= strstr($mod, "#")."#";
695 continue;
696 }
697 if (preg_match("/[^#]$module$/", $mod)){
698 return ("#all#");
699 }
700 if (preg_match("/^all$/", $mod)){
701 return ("#all#");
702 }
703 }
704 }
706 /* Return assembled ACL, or none */
707 if ($final != ""){
708 return (preg_replace('/##/', '#', $final));
709 }
711 /* Nothing matches - disable access for this object */
712 return ("#none#");
713 }
716 function get_userinfo()
717 {
718 global $ui;
720 return $ui;
721 }
724 function get_smarty()
725 {
726 global $smarty;
728 return $smarty;
729 }
732 function convert_department_dn($dn)
733 {
734 $dep= "";
736 /* Build a sub-directory style list of the tree level
737 specified in $dn */
738 foreach (split (",", $dn) as $val){
740 /* We're only interested in organizational units... */
741 if (preg_match ("/ou=/", $val)){
742 $dep= preg_replace("/ou=([^,]+)/", "\\1", $val)."/$dep";
743 }
745 /* ... and location objects */
746 if (preg_match ("/l=/", $val)){
747 $dep= preg_replace("/l=([^,]+)/", "\\1", $val)."/$dep";
748 }
749 }
751 /* Return and remove accidently trailing slashes */
752 return rtrim($dep, "/");
753 }
755 function convert_department_dn2($dn)
756 {
757 $dep= "";
759 /* Build a sub-directory style list of the tree level
760 specified in $dn */
761 $deps = array_flip($_SESSION['config']->idepartments);
763 if(isset($deps[$dn])){
764 $dn= $deps[$dn];
765 $tmp = split (",", $dn);
766 $dep = preg_replace("/^.*=/","",$tmp[0]);
767 }else{
768 $tmp = split (",", $dn);
769 $dep= preg_replace("%^.*/([^/]+)$%", "\\1", $tmp[0]);
770 }
772 /* Return and remove accidently trailing slashes */
773 $tmp = rtrim($dep, "/");
774 return $tmp;
775 }
778 function get_ou($name)
779 {
780 global $config;
782 $ou= $config->current[$name];
783 if ($ou != ""){
784 if (!preg_match('/^[^=]+=[^=]+/', $ou)){
785 return "ou=$ou,";
786 } else {
787 return "$ou,";
788 }
789 } else {
790 return "";
791 }
792 }
795 function get_people_ou()
796 {
797 return (get_ou("PEOPLE"));
798 }
801 function get_groups_ou()
802 {
803 return (get_ou("GROUPS"));
804 }
807 function get_winstations_ou()
808 {
809 return (get_ou("WINSTATIONS"));
810 }
813 function get_base_from_people($dn)
814 {
815 global $config;
817 $pattern= "/^[^,]+,".preg_quote(get_people_ou())."/";
818 $base= preg_replace($pattern, '', $dn);
820 /* Set to base, if we're not on a correct subtree */
821 if (!isset($config->idepartments[$base])){
822 $base= $config->current['BASE'];
823 }
825 return ($base);
826 }
829 function get_departments($ignore_dn= "")
830 {
831 global $config;
833 /* Initialize result hash */
834 $result= array();
835 $result['/']= $config->current['BASE'];
837 /* Get list of department objects */
838 $ldap= $config->get_ldap_link();
839 $ldap->cd ($config->current['BASE']);
840 $ldap->search ("(objectClass=gosaDepartment)", array("ou"));
841 while ($attrs= $ldap->fetch()){
842 $dn= $ldap->getDN();
843 if ($dn == $ignore_dn){
844 continue;
845 }
846 $result[convert_department_dn($dn)]= $dn;
847 }
849 return ($result);
850 }
853 function chkacl($acl, $name)
854 {
855 /* Look for attribute in ACL */
856 if (preg_match("/#$name#/", $acl) || $acl == "#all#"){
857 return ("");
858 }
860 /* Optically disable html object for no match */
861 return (" disabled ");
862 }
865 function is_phone_nr($nr)
866 {
867 if ($nr == ""){
868 return (TRUE);
869 }
871 return preg_match ("/^[0-9 ()+*-]+$/", $nr);
872 }
875 function is_url($url)
876 {
877 if ($url == ""){
878 return (TRUE);
879 }
881 return preg_match ("/^(http|https):\/\/((?:[a-zA-Z0-9_-]+\.?)+):?(\d*)/", $url);
882 }
885 function is_dn($dn)
886 {
887 if ($dn == ""){
888 return (TRUE);
889 }
891 return preg_match ("/^[a-z0-9 _-]+$/i", $dn);
892 }
895 function is_uid($uid)
896 {
897 global $config;
899 if ($uid == ""){
900 return (TRUE);
901 }
903 /* STRICT adds spaces and case insenstivity to the uid check.
904 This is dangerous and should not be used. */
905 if (isset($config->current['STRICT']) && preg_match('/^no$/i', $config->current['STRICT'])){
906 return preg_match ("/^[a-z0-9 _.-]+$/i", $uid);
907 } else {
908 return preg_match ("/^[a-z0-9_-]+$/", $uid);
909 }
910 }
913 function is_id($id)
914 {
915 if ($id == ""){
916 return (FALSE);
917 }
919 return preg_match ("/^[0-9]+$/", $id);
920 }
923 function is_path($path)
924 {
925 if ($path == ""){
926 return (TRUE);
927 }
928 if (!preg_match('/^[a-z0-9%\/_.+-]+$/i', $path)){
929 return (FALSE);
930 }
932 return preg_match ("/\/.+$/", $path);
933 }
936 function is_email($address, $template= FALSE)
937 {
938 if ($address == ""){
939 return (TRUE);
940 }
941 if ($template){
942 return preg_match ("/^[._a-z0-9%-]+@[_a-z0-9-]+(\.[a-z0-9-]+)(\.[a-z0-9-]+)*$/i",
943 $address);
944 } else {
945 return preg_match ("/^[._a-z0-9-]+@[_a-z0-9-]+(\.[a-z0-9i-]+)(\.[a-z0-9-]+)*$/i",
946 $address);
947 }
948 }
951 function print_red()
952 {
953 /* Check number of arguments */
954 if (func_num_args() < 1){
955 return;
956 }
958 /* Get arguments, save string */
959 $array = func_get_args();
960 $string= $array[0];
962 /* Step through arguments */
963 for ($i= 1; $i<count($array); $i++){
964 $string= preg_replace ("/%s/", $array[$i], $string, 1);
965 }
967 /* If DEBUGLEVEL is set, we're in web mode, use textual output in
968 the other case... */
969 if (isset($_SESSION['DEBUGLEVEL'])){
970 $_SESSION['errors'].= "<div align=\"left\" style=\"border-width:5px;".
971 "border-style:solid;border-color:red; background-color:black;".
972 "margin-bottom:10px; padding:8px;\"><table summary=''><tr><td><img alt=\"\" src=\"".
973 get_template_path('images/warning.png')."\"></td>".
974 "<td width=\"100%\" align=\"center\"><font color=\"#FFFFFF\">".
975 "<b style='font-size:16px;'>$string</b></font></td><td>".
976 "<img alt=\"\"src=\"".get_template_path('images/warning.png').
977 "\"></td></tr></table></div>\n";
978 } else {
979 echo "Error: $string\n";
980 }
981 }
984 function gen_locked_message($user, $dn)
985 {
986 global $plug, $config;
988 $_SESSION['dn']= $dn;
989 $ldap= $config->get_ldap_link();
990 $ldap->cat ($user);
991 $attrs= $ldap->fetch();
992 $uid= $attrs["uid"][0];
994 /* Prepare and show template */
995 $smarty= get_smarty();
996 $smarty->assign ("dn", $dn);
997 $smarty->assign ("message", sprintf(_("You're going to edit the LDAP entry '%s' which appears to be used by '%s'. Please contact the person in order to clarify proceedings."), $dn, "<a href=\"main.php?plug=0&viewid=$uid\">$uid</a>"));
999 return ($smarty->fetch (get_template_path('islocked.tpl')));
1000 }
1003 function to_string ($value)
1004 {
1005 /* If this is an array, generate a text blob */
1006 if (is_array($value)){
1007 $ret= "";
1008 foreach ($value as $line){
1009 $ret.= $line."<br>\n";
1010 }
1011 return ($ret);
1012 } else {
1013 return ($value);
1014 }
1015 }
1018 function get_printer_list($cups_server)
1019 {
1020 global $config;
1022 $res= array();
1024 /* Use CUPS, if we've access to it */
1025 if (function_exists('cups_get_dest_list')){
1026 $dest_list= cups_get_dest_list ($cups_server);
1028 foreach ($dest_list as $prt){
1029 $attr= cups_get_printer_attributes ($cups_server, $prt->name);
1031 foreach ($attr as $prt_info){
1032 if ($prt_info->name == "printer-info"){
1033 $info= $prt_info->value;
1034 break;
1035 }
1036 }
1037 $res[$prt->name]= "$info [$prt->name]";
1038 }
1040 /* CUPS is not available, try lpstat as a replacement */
1041 } else {
1042 $ar = false;
1043 exec("lpstat -p", $ar);
1044 foreach($ar as $val){
1045 list($dummy, $printer, $rest)= split(' ', $val, 3);
1046 if (preg_match('/^[^@]+$/', $printer)){
1047 $res[$printer]= "$printer";
1048 }
1049 }
1050 }
1052 /* Merge in printers from LDAP */
1053 $ldap= $config->get_ldap_link();
1054 $ldap->cd ($config->current['BASE']);
1055 $ldap->search('(objectClass=gotoPrinter)', array('cn'));
1056 while ($attrs= $ldap->fetch()){
1057 $res[$attrs["cn"][0]]= $attrs["cn"][0];
1058 }
1060 return $res;
1061 }
1064 function sess_del ($var)
1065 {
1066 /* New style */
1067 unset ($_SESSION[$var]);
1069 /* ... work around, since the first one
1070 doesn't seem to work all the time */
1071 session_unregister ($var);
1072 }
1075 function show_errors($message)
1076 {
1077 $complete= "";
1079 /* Assemble the message array to a plain string */
1080 foreach ($message as $error){
1081 if ($complete == ""){
1082 $complete= $error;
1083 } else {
1084 $complete= "$error<br>$complete";
1085 }
1086 }
1088 /* Fill ERROR variable with nice error dialog */
1089 print_red($complete);
1090 }
1093 function show_ldap_error($message)
1094 {
1095 if (!preg_match("/Success/i", $message)){
1096 print_red (_("LDAP error:")." $message");
1097 return TRUE;
1098 } else {
1099 return FALSE;
1100 }
1101 }
1104 function rewrite($s)
1105 {
1106 global $REWRITE;
1108 foreach ($REWRITE as $key => $val){
1109 $s= preg_replace("/$key/", "$val", $s);
1110 }
1112 return ($s);
1113 }
1116 function dn2base($dn)
1117 {
1118 global $config;
1120 if (get_people_ou() != ""){
1121 $dn= preg_replace('/,'.get_people_ou().'/' , ',', $dn);
1122 }
1123 if (get_groups_ou() != ""){
1124 $dn= preg_replace('/,'.get_groups_ou().'/' , ',', $dn);
1125 }
1126 $base= preg_replace ('/^[^,]+,/i', '', $dn);
1128 return ($base);
1129 }
1133 function check_command($cmdline)
1134 {
1135 $cmd= preg_replace("/ .*$/", "", $cmdline);
1137 /* Check if command exists in filesystem */
1138 if (!file_exists($cmd)){
1139 return (FALSE);
1140 }
1142 /* Check if command is executable */
1143 if (!is_executable($cmd)){
1144 return (FALSE);
1145 }
1147 return (TRUE);
1148 }
1151 function print_header($image, $headline, $info= "")
1152 {
1153 $display= "<div class=\"plugtop\">\n";
1154 $display.= " <img src=\"$image\" align=\"middle\" alt=\"*\"> $headline\n";
1155 $display.= "</div>\n";
1157 if ($info != ""){
1158 $display.= "<div class=\"pluginfo\">\n";
1159 $display.= "$info";
1160 $display.= "</div>\n";
1161 } else {
1162 $display.= "<div style=\"height:5px;\">\n";
1163 $display.= " ";
1164 $display.= "</div>\n";
1165 }
1167 return ($display);
1168 }
1171 function register_global($name, $object)
1172 {
1173 $_SESSION[$name]= $object;
1174 }
1177 function is_global($name)
1178 {
1179 return isset($_SESSION[$name]);
1180 }
1183 function get_global($name)
1184 {
1185 return $_SESSION[$name];
1186 }
1189 function range_selector($dcnt,$start,$range=25)
1190 {
1192 /* Entries shown left and right from the selected entry */
1193 $max_entries= 10;
1195 /* Initialize and take care that max_entries is even */
1196 $output="";
1197 if ($max_entries & 1){
1198 $max_entries++;
1199 }
1201 /* Prevent output to start or end out of range */
1202 if ($start < 0 ){
1203 $start= 0 ;
1204 }
1205 if ($start >= $dcnt){
1206 $start= $range * (int)(($dcnt / $range) + 0.5);
1207 }
1209 $numpages= (($dcnt / $range));
1210 if(((int)($numpages))!=($numpages)){
1211 $numpages = (int)$numpages + 1;
1212 }
1213 if (((int)$numpages) <= 1 ){
1214 return ("");
1215 }
1216 $ppage= (int)(($start / $range) + 0.5);
1219 /* Align selected page to +/- max_entries/2 */
1220 $begin= $ppage - $max_entries/2;
1221 $end= $ppage + $max_entries/2;
1223 /* Adjust begin/end, so that the selected value is somewhere in
1224 the middle and the size is max_entries if possible */
1225 if ($begin < 0){
1226 $end-= $begin + 1;
1227 $begin= 0;
1228 }
1229 if ($end > $numpages) {
1230 $end= $numpages;
1231 }
1232 if (($end - $begin) < $max_entries && ($end - $max_entries) > 0){
1233 $begin= $end - $max_entries;
1234 }
1236 $output.= "<div style='border:1px solid #E0E0E0; background-color:#FFFFFF;'>";
1238 /* Draw decrement */
1239 if ($start > 0 ) {
1240 $output.=" <a href= \"main.php?plug=".validate($_GET['plug'])."&start=".
1241 (($start-$range))."\">".
1242 "<img alt=\"\" src=\"images/back.png\" border=0 align=\"middle\"></a>";
1243 }
1245 /* Draw pages */
1246 for ($i= $begin; $i < $end; $i++) {
1247 if ($ppage == $i){
1248 $output.= "<a style=\"background-color:#D0D0D0;\" href=\"main.php?plug=".
1249 validate($_GET['plug'])."&start=".
1250 ($i*$range)."\"> ".($i+1)." </a>";
1251 } else {
1252 $output.= "<a href=\"main.php?plug=".validate($_GET['plug']).
1253 "&start=".($i*$range)."\"> ".($i+1)." </a>";
1254 }
1255 }
1257 /* Draw increment */
1258 if($start < ($dcnt-$range)) {
1259 $output.=" <a href= \"main.php?plug=".validate($_GET['plug'])."&start=".
1260 (($start+($range)))."\">".
1261 "<img alt=\"\" src=\"images/forward.png\" border=0 align=\"middle\"></a>";
1262 }
1264 $output.= "</div>";
1266 return($output);
1267 }
1270 function apply_filter()
1271 {
1272 $apply= "";
1274 $apply= ''.
1275 '<table summary="" width="100%" style="background:#EEEEEE;border-top:1px solid #B0B0B0;"><tr><td width="100%" align="right">'.
1276 '<input type="submit" name="apply" value="'._("Apply").'"></td></tr></table>';
1278 return ($apply);
1279 }
1282 function back_to_main()
1283 {
1284 $string= '<br><p class="plugbottom"><input type=submit name="password_back" value="'.
1285 _("Back").'"></p><input type="hidden" name="ignore">';
1287 return ($string);
1288 }
1291 function normalize_netmask($netmask)
1292 {
1293 /* Check for notation of netmask */
1294 if (!preg_match('/^([0-9]+\.){3}[0-9]+$/', $netmask)){
1295 $num= (int)($netmask);
1296 $netmask= "";
1298 for ($byte= 0; $byte<4; $byte++){
1299 $result=0;
1301 for ($i= 7; $i>=0; $i--){
1302 if ($num-- > 0){
1303 $result+= pow(2,$i);
1304 }
1305 }
1307 $netmask.= $result.".";
1308 }
1310 return (preg_replace('/\.$/', '', $netmask));
1311 }
1313 return ($netmask);
1314 }
1317 function netmask_to_bits($netmask)
1318 {
1319 list($nm0, $nm1, $nm2, $nm3)= split('\.', $netmask);
1320 $res= 0;
1322 for ($n= 0; $n<4; $n++){
1323 $start= 255;
1324 $name= "nm$n";
1326 for ($i= 0; $i<8; $i++){
1327 if ($start == (int)($$name)){
1328 $res+= 8 - $i;
1329 break;
1330 }
1331 $start-= pow(2,$i);
1332 }
1333 }
1335 return ($res);
1336 }
1339 function recurse($rule, $variables)
1340 {
1341 $result= array();
1343 if (!count($variables)){
1344 return array($rule);
1345 }
1347 reset($variables);
1348 $key= key($variables);
1349 $val= current($variables);
1350 unset ($variables[$key]);
1352 foreach($val as $possibility){
1353 $nrule= preg_replace("/\{$key\}/", $possibility, $rule);
1354 $result= array_merge($result, recurse($nrule, $variables));
1355 }
1357 return ($result);
1358 }
1361 function expand_id($rule, $attributes)
1362 {
1363 /* Check for id rule */
1364 if(preg_match('/^id(:|#)\d+$/',$rule)){
1365 return (array("\{$rule}"));
1366 }
1368 /* Check for clean attribute */
1369 if (preg_match('/^%[a-zA-Z0-9]+$/', $rule)){
1370 $rule= preg_replace('/^%/', '', $rule);
1371 $val= rewrite(preg_replace('/ /', '', strtolower($attributes[$rule])));
1372 return (array($val));
1373 }
1375 /* Check for attribute with parameters */
1376 if (preg_match('/^%[a-zA-Z0-9]+\[[0-9-]+\]$/', $rule)){
1377 $param= preg_replace('/^[^[]+\[([^]]+)]$/', '\\1', $rule);
1378 $part= preg_replace('/^%/', '', preg_replace('/\[.*$/', '', $rule));
1379 $val= rewrite(preg_replace('/ /', '', strtolower($attributes[$part])));
1380 $start= preg_replace ('/-.*$/', '', $param);
1381 $stop = preg_replace ('/^[^-]+-/', '', $param);
1383 /* Assemble results */
1384 $result= array();
1385 for ($i= $start; $i<= $stop; $i++){
1386 $result[]= substr($val, 0, $i);
1387 }
1388 return ($result);
1389 }
1391 echo "Error in idgen string: don't know how to handle rule $rule.\n";
1392 return (array($rule));
1393 }
1396 function gen_uids($rule, $attributes)
1397 {
1398 global $config;
1400 /* Search for keys and fill the variables array with all
1401 possible values for that key. */
1402 $part= "";
1403 $trigger= false;
1404 $stripped= "";
1405 $variables= array();
1407 for ($pos= 0; $pos < strlen($rule); $pos++){
1409 if ($rule[$pos] == "{" ){
1410 $trigger= true;
1411 $part= "";
1412 continue;
1413 }
1415 if ($rule[$pos] == "}" ){
1416 $variables[$pos]= expand_id($part, $attributes);
1417 $stripped.= "\{$pos}";
1418 $trigger= false;
1419 continue;
1420 }
1422 if ($trigger){
1423 $part.= $rule[$pos];
1424 } else {
1425 $stripped.= $rule[$pos];
1426 }
1427 }
1429 /* Recurse through all possible combinations */
1430 $proposed= recurse($stripped, $variables);
1432 /* Get list of used ID's */
1433 $used= array();
1434 $ldap= $config->get_ldap_link();
1435 $ldap->cd($config->current['BASE']);
1436 $ldap->search('(uid=*)');
1438 while($attrs= $ldap->fetch()){
1439 $used[]= $attrs['uid'][0];
1440 }
1442 /* Remove used uids and watch out for id tags */
1443 $ret= array();
1444 foreach($proposed as $uid){
1446 /* Check for id tag and modify uid if needed */
1447 if(preg_match('/\{id:\d+}/',$uid)){
1448 $size= preg_replace('/^.*{id:(\d+)}.*$/', '\\1', $uid);
1450 for ($i= 0; $i < pow(10,$size); $i++){
1451 $number= sprintf("%0".$size."d", $i);
1452 $res= preg_replace('/{id:(\d+)}/', $number, $uid);
1453 if (!in_array($res, $used)){
1454 $uid= $res;
1455 break;
1456 }
1457 }
1458 }
1460 if(preg_match('/\{id#\d+}/',$uid)){
1461 $size= preg_replace('/^.*{id#(\d+)}.*$/', '\\1', $uid);
1463 while (true){
1464 mt_srand((double) microtime()*1000000);
1465 $number= sprintf("%0".$size."d", mt_rand(0, pow(10, $size)-1));
1466 $res= preg_replace('/{id#(\d+)}/', $number, $uid);
1467 if (!in_array($res, $used)){
1468 $uid= $res;
1469 break;
1470 }
1471 }
1472 }
1474 /* Don't assign used ones */
1475 if (!in_array($uid, $used)){
1476 $ret[]= $uid;
1477 }
1478 }
1480 return(array_unique($ret));
1481 }
1484 function array_search_r($needle, $key, $haystack){
1486 foreach($haystack as $index => $value){
1487 $match= 0;
1489 if (is_array($value)){
1490 $match= array_search_r($needle, $key, $value);
1491 }
1493 if ($index==$key && !is_array($value) && preg_match("/$needle/i", $value)){
1494 $match=1;
1495 }
1497 if ($match){
1498 return 1;
1499 }
1500 }
1502 return 0;
1503 }
1506 /* Sadly values like memory_limit are perpended by K, M, G, etc.
1507 Need to convert... */
1508 function to_byte($value) {
1509 $value= strtolower(trim($value));
1511 if(!is_numeric(substr($value, -1))) {
1513 switch(substr($value, -1)) {
1514 case 'g':
1515 $mult= 1073741824;
1516 break;
1517 case 'm':
1518 $mult= 1048576;
1519 break;
1520 case 'k':
1521 $mult= 1024;
1522 break;
1523 }
1525 return ($mult * (int)substr($value, 0, -1));
1526 } else {
1527 return $value;
1528 }
1529 }
1532 function in_array_ics($value, $items)
1533 {
1534 if (!is_array($items)){
1535 return (FALSE);
1536 }
1538 foreach ($items as $item){
1539 if (strtolower($item) == strtolower($value)) {
1540 return (TRUE);
1541 }
1542 }
1544 return (FALSE);
1545 }
1548 function generate_alphabet($count= 10)
1549 {
1550 $characters= _("*ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
1551 $alphabet= "";
1552 $c= 0;
1554 /* Fill cells with charaters */
1555 for ($i= 0; $i<mb_strlen($characters, 'UTF8'); $i++){
1556 if ($c == 0){
1557 $alphabet.= "<tr>";
1558 }
1560 $ch = mb_substr($characters, $i, 1, "UTF8");
1561 $alphabet.= "<td><a class=\"alphaselect\" href=\"main.php?plug=".
1562 validate($_GET['plug'])."&search=".$ch."\"> ".$ch." </a></td>";
1564 if ($c++ == $count){
1565 $alphabet.= "</tr>";
1566 $c= 0;
1567 }
1568 }
1570 /* Fill remaining cells */
1571 while ($c++ <= $count){
1572 $alphabet.= "<td> </td>";
1573 }
1575 return ($alphabet);
1576 }
1579 function validate($string)
1580 {
1581 return (strip_tags(preg_replace('/\0/', '', $string)));
1582 }
1584 function get_gosa_version()
1585 {
1586 global $svn_revision, $svn_path;
1588 /* Extract informations */
1589 $revision= preg_replace('/^[^0-9]*([0-9]+)[^0-9]*$/', '\1', $svn_revision);
1591 /* Release or development? */
1592 if (preg_match('%/gosa/trunk/%', $svn_path)){
1593 return (sprintf(_("GOsa development snapshot (Rev %s)"), $revision));
1594 } else {
1595 $release= preg_replace('%^.*/([^/]+)/include/functions.inc.*$%', '\1', $svn_path);
1596 return (sprintf(_("GOsa $release"), $revision));
1597 }
1598 }
1601 function rmdirRecursive($path, $followLinks=false) {
1602 $dir= opendir($path);
1603 while($entry= readdir($dir)) {
1604 if(is_file($path."/".$entry) || ((!$followLinks) && is_link($path."/".$entry))) {
1605 unlink($path."/".$entry);
1606 } elseif (is_dir($path."/".$entry) && $entry!='.' && $entry!='..') {
1607 rmdirRecursive($path."/".$entry);
1608 }
1609 }
1610 closedir($dir);
1611 return rmdir($path);
1612 }
1614 function scan_directory($path,$sort_desc=false)
1615 {
1616 $ret = false;
1618 /* is this a dir ? */
1619 if(is_dir($path)) {
1621 /* is this path a readable one */
1622 if(is_readable($path)){
1624 /* Get contents and write it into an array */
1625 $ret = array();
1627 $dir = opendir($path);
1629 /* Is this a correct result ?*/
1630 if($dir){
1631 while($fp = readdir($dir))
1632 $ret[]= $fp;
1633 }
1634 }
1635 }
1636 /* Sort array ascending , like scandir */
1637 sort($ret);
1639 /* Sort descending if parameter is sort_desc is set */
1640 if($sort_desc) {
1641 $ret = array_reverse($ret);
1642 }
1644 return($ret);
1645 }
1647 function clean_smarty_compile_dir($directory)
1648 {
1649 global $svn_revision;
1651 if(is_dir($directory) && is_readable($directory)) {
1652 // Set revision filename to REVISION
1653 $revision_file= $directory."/REVISION";
1655 /* Is there a stamp containing the current revision? */
1656 if(!file_exists($revision_file)) {
1657 // create revision file
1658 create_revision($revision_file, $svn_revision);
1659 } else {
1660 # check for "$config->...['CONFIG']/revision" and the
1661 # contents should match the revision number
1662 if(!compare_revision($revision_file, $svn_revision)){
1663 // If revision differs, clean compile directory
1664 foreach(scan_directory($directory) as $file) {
1665 if(($file==".")||($file=="..")) continue;
1666 if( is_file($directory."/".$file) &&
1667 is_writable($directory."/".$file)) {
1668 // delete file
1669 if(!unlink($directory."/".$file)) {
1670 print_red("File ".$directory."/".$file." could not be deleted.");
1671 // This should never be reached
1672 }
1673 } elseif(is_dir($directory."/".$file) &&
1674 is_writable($directory."/".$file)) {
1675 // Just recursively delete it
1676 rmdirRecursive($directory."/".$file);
1677 }
1678 }
1679 // We should now create a fresh revision file
1680 clean_smarty_compile_dir($directory);
1681 } else {
1682 // Revision matches, nothing to do
1683 }
1684 }
1685 } else {
1686 // Smarty compile dir is not accessible
1687 // (Smarty will warn about this)
1688 }
1689 }
1691 function create_revision($revision_file, $revision)
1692 {
1693 $result= false;
1695 if(is_dir(dirname($revision_file)) && is_writable(dirname($revision_file))) {
1696 if($fh= fopen($revision_file, "w")) {
1697 if(fwrite($fh, $revision)) {
1698 $result= true;
1699 }
1700 }
1701 fclose($fh);
1702 } else {
1703 print_red("Can not write to revision file");
1704 }
1706 return $result;
1707 }
1709 function compare_revision($revision_file, $revision)
1710 {
1711 // false means revision differs
1712 $result= false;
1714 if(file_exists($revision_file) && is_readable($revision_file)) {
1715 // Open file
1716 if($fh= fopen($revision_file, "r")) {
1717 // Compare File contents with current revision
1718 if($revision == fread($fh, filesize($revision_file))) {
1719 $result= true;
1720 }
1721 } else {
1722 print_red("Can not open revision file");
1723 }
1724 // Close file
1725 fclose($fh);
1726 }
1728 return $result;
1729 }
1731 function progressbar($percentage,$width=100,$height=15,$showvalue=false)
1732 {
1733 $str = ""; // Our return value will be saved in this var
1735 $color = dechex($percentage+150);
1736 $color2 = dechex(150 - $percentage);
1737 $bgcolor= $showvalue?"FFFFFF":"DDDDDD";
1739 $progress = (int)(($percentage /100)*$width);
1741 /* Abort printing out percentage, if divs are to small */
1744 /* If theres a better solution for this, use it... */
1745 $str = "
1746 <div style=\" width:".($width)."px;
1747 height:".($height)."px;
1748 background-color:#000000;
1749 padding:1px;\">
1751 <div style=\" width:".($width)."px;
1752 background-color:#$bgcolor;
1753 height:".($height)."px;\">
1755 <div style=\" width:".$progress."px;
1756 height:".$height."px;
1757 background-color:#".$color2.$color2.$color."; \">";
1760 if(($height >10)&&($showvalue)){
1761 $str.= "<font style=\"font-size:".($height-2)."px;color:#FF0000;align:middle;padding-left:".((int)(($width*0.4)))."px;\">
1762 <b>".$percentage."%</b>
1763 </font>";
1764 }
1766 $str.= "</div></div></div>";
1768 return($str);
1769 }
1772 function search_config($arr, $name, $return)
1773 {
1774 if (is_array($arr)){
1775 foreach ($arr as $a){
1776 if (isset($a['CLASS']) &&
1777 strtolower($a['CLASS']) == strtolower($name)){
1779 if (isset($a[$return])){
1780 return ($a[$return]);
1781 } else {
1782 return ("");
1783 }
1784 } else {
1785 $res= search_config ($a, $name, $return);
1786 if ($res != ""){
1787 return $res;
1788 }
1789 }
1790 }
1791 }
1792 return ("");
1793 }
1796 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1797 ?>