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 $tmp = split (",", $dn);
763 $dep= preg_replace("%^.*/([^/]+)$%", "\\1", $tmp[0]);
766 /* Return and remove accidently trailing slashes */
767 $tmp = rtrim($dep, "/");
768 return $tmp;
769 }
772 function get_ou($name)
773 {
774 global $config;
776 $ou= $config->current[$name];
777 if ($ou != ""){
778 if (!preg_match('/^[^=]+=[^=]+/', $ou)){
779 return "ou=$ou,";
780 } else {
781 return "$ou,";
782 }
783 } else {
784 return "";
785 }
786 }
789 function get_people_ou()
790 {
791 return (get_ou("PEOPLE"));
792 }
795 function get_groups_ou()
796 {
797 return (get_ou("GROUPS"));
798 }
801 function get_winstations_ou()
802 {
803 return (get_ou("WINSTATIONS"));
804 }
807 function get_base_from_people($dn)
808 {
809 global $config;
811 $pattern= "/^[^,]+,".preg_quote(get_people_ou())."/";
812 $base= preg_replace($pattern, '', $dn);
814 /* Set to base, if we're not on a correct subtree */
815 if (!isset($config->idepartments[$base])){
816 $base= $config->current['BASE'];
817 }
819 return ($base);
820 }
823 function get_departments($ignore_dn= "")
824 {
825 global $config;
827 /* Initialize result hash */
828 $result= array();
829 $result['/']= $config->current['BASE'];
831 /* Get list of department objects */
832 $ldap= $config->get_ldap_link();
833 $ldap->cd ($config->current['BASE']);
834 $ldap->search ("(objectClass=gosaDepartment)", array("ou"));
835 while ($attrs= $ldap->fetch()){
836 $dn= $ldap->getDN();
837 if ($dn == $ignore_dn){
838 continue;
839 }
840 $result[convert_department_dn($dn)]= $dn;
841 }
843 return ($result);
844 }
847 function chkacl($acl, $name)
848 {
849 /* Look for attribute in ACL */
850 if (preg_match("/#$name#/", $acl) || $acl == "#all#"){
851 return ("");
852 }
854 /* Optically disable html object for no match */
855 return (" disabled ");
856 }
859 function is_phone_nr($nr)
860 {
861 if ($nr == ""){
862 return (TRUE);
863 }
865 return preg_match ("/^[0-9 ()+*-]+$/", $nr);
866 }
869 function is_url($url)
870 {
871 if ($url == ""){
872 return (TRUE);
873 }
875 return preg_match ("/^(http|https):\/\/((?:[a-zA-Z0-9_-]+\.?)+):?(\d*)/", $url);
876 }
879 function is_dn($dn)
880 {
881 if ($dn == ""){
882 return (TRUE);
883 }
885 return preg_match ("/^[a-z0-9 _-]+$/i", $dn);
886 }
889 function is_uid($uid)
890 {
891 global $config;
893 if ($uid == ""){
894 return (TRUE);
895 }
897 /* STRICT adds spaces and case insenstivity to the uid check.
898 This is dangerous and should not be used. */
899 if (isset($config->current['STRICT']) && preg_match('/^no$/i', $config->current['STRICT'])){
900 return preg_match ("/^[a-z0-9 _.-]+$/i", $uid);
901 } else {
902 return preg_match ("/^[a-z0-9_-]+$/", $uid);
903 }
904 }
907 function is_id($id)
908 {
909 if ($id == ""){
910 return (FALSE);
911 }
913 return preg_match ("/^[0-9]+$/", $id);
914 }
917 function is_path($path)
918 {
919 if ($path == ""){
920 return (TRUE);
921 }
922 if (!preg_match('/^[a-z0-9%\/_.+-]+$/i', $path)){
923 return (FALSE);
924 }
926 return preg_match ("/\/.+$/", $path);
927 }
930 function is_email($address, $template= FALSE)
931 {
932 if ($address == ""){
933 return (TRUE);
934 }
935 if ($template){
936 return preg_match ("/^[._a-z0-9%-]+@[_a-z0-9-]+(\.[a-z0-9-]+)(\.[a-z0-9-]+)*$/i",
937 $address);
938 } else {
939 return preg_match ("/^[._a-z0-9-]+@[_a-z0-9-]+(\.[a-z0-9i-]+)(\.[a-z0-9-]+)*$/i",
940 $address);
941 }
942 }
945 function print_red()
946 {
947 /* Check number of arguments */
948 if (func_num_args() < 1){
949 return;
950 }
952 /* Get arguments, save string */
953 $array = func_get_args();
954 $string= $array[0];
956 /* Step through arguments */
957 for ($i= 1; $i<count($array); $i++){
958 $string= preg_replace ("/%s/", $array[$i], $string, 1);
959 }
961 /* If DEBUGLEVEL is set, we're in web mode, use textual output in
962 the other case... */
963 if (isset($_SESSION['DEBUGLEVEL'])){
964 $_SESSION['errors'].= "<div align=\"left\" style=\"border-width:5px;".
965 "border-style:solid;border-color:red; background-color:black;".
966 "margin-bottom:10px; padding:8px;\"><table summary=''><tr><td><img alt=\"\" src=\"".
967 get_template_path('images/warning.png')."\"></td>".
968 "<td width=\"100%\" align=\"center\"><font color=\"#FFFFFF\">".
969 "<b style='font-size:16px;'>$string</b></font></td><td>".
970 "<img alt=\"\"src=\"".get_template_path('images/warning.png').
971 "\"></td></tr></table></div>\n";
972 } else {
973 echo "Error: $string\n";
974 }
975 }
978 function gen_locked_message($user, $dn)
979 {
980 global $plug, $config;
982 $_SESSION['dn']= $dn;
983 $ldap= $config->get_ldap_link();
984 $ldap->cat ($user);
985 $attrs= $ldap->fetch();
986 $uid= $attrs["uid"][0];
988 /* Prepare and show template */
989 $smarty= get_smarty();
990 $smarty->assign ("dn", $dn);
991 $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>"));
993 return ($smarty->fetch (get_template_path('islocked.tpl')));
994 }
997 function to_string ($value)
998 {
999 /* If this is an array, generate a text blob */
1000 if (is_array($value)){
1001 $ret= "";
1002 foreach ($value as $line){
1003 $ret.= $line."<br>\n";
1004 }
1005 return ($ret);
1006 } else {
1007 return ($value);
1008 }
1009 }
1012 function get_printer_list($cups_server)
1013 {
1014 global $config;
1016 $res= array();
1018 /* Use CUPS, if we've access to it */
1019 if (function_exists('cups_get_dest_list')){
1020 $dest_list= cups_get_dest_list ($cups_server);
1022 foreach ($dest_list as $prt){
1023 $attr= cups_get_printer_attributes ($cups_server, $prt->name);
1025 foreach ($attr as $prt_info){
1026 if ($prt_info->name == "printer-info"){
1027 $info= $prt_info->value;
1028 break;
1029 }
1030 }
1031 $res[$prt->name]= "$info [$prt->name]";
1032 }
1034 /* CUPS is not available, try lpstat as a replacement */
1035 } else {
1036 $ar = false;
1037 exec("lpstat -p", $ar);
1038 foreach($ar as $val){
1039 list($dummy, $printer, $rest)= split(' ', $val, 3);
1040 if (preg_match('/^[^@]+$/', $printer)){
1041 $res[$printer]= "$printer";
1042 }
1043 }
1044 }
1046 /* Merge in printers from LDAP */
1047 $ldap= $config->get_ldap_link();
1048 $ldap->cd ($config->current['BASE']);
1049 $ldap->search('(objectClass=gotoPrinter)', array('cn'));
1050 while ($attrs= $ldap->fetch()){
1051 $res[$attrs["cn"][0]]= $attrs["cn"][0];
1052 }
1054 return $res;
1055 }
1058 function sess_del ($var)
1059 {
1060 /* New style */
1061 unset ($_SESSION[$var]);
1063 /* ... work around, since the first one
1064 doesn't seem to work all the time */
1065 session_unregister ($var);
1066 }
1069 function show_errors($message)
1070 {
1071 $complete= "";
1073 /* Assemble the message array to a plain string */
1074 foreach ($message as $error){
1075 if ($complete == ""){
1076 $complete= $error;
1077 } else {
1078 $complete= "$error<br>$complete";
1079 }
1080 }
1082 /* Fill ERROR variable with nice error dialog */
1083 print_red($complete);
1084 }
1087 function show_ldap_error($message)
1088 {
1089 if (!preg_match("/Success/i", $message)){
1090 print_red (_("LDAP error:")." $message");
1091 return TRUE;
1092 } else {
1093 return FALSE;
1094 }
1095 }
1098 function rewrite($s)
1099 {
1100 global $REWRITE;
1102 foreach ($REWRITE as $key => $val){
1103 $s= preg_replace("/$key/", "$val", $s);
1104 }
1106 return ($s);
1107 }
1110 function dn2base($dn)
1111 {
1112 global $config;
1114 if (get_people_ou() != ""){
1115 $dn= preg_replace('/,'.get_people_ou().'/' , ',', $dn);
1116 }
1117 if (get_groups_ou() != ""){
1118 $dn= preg_replace('/,'.get_groups_ou().'/' , ',', $dn);
1119 }
1120 $base= preg_replace ('/^[^,]+,/i', '', $dn);
1122 return ($base);
1123 }
1127 function check_command($cmdline)
1128 {
1129 $cmd= preg_replace("/ .*$/", "", $cmdline);
1131 /* Check if command exists in filesystem */
1132 if (!file_exists($cmd)){
1133 return (FALSE);
1134 }
1136 /* Check if command is executable */
1137 if (!is_executable($cmd)){
1138 return (FALSE);
1139 }
1141 return (TRUE);
1142 }
1145 function print_header($image, $headline, $info= "")
1146 {
1147 $display= "<div class=\"plugtop\">\n";
1148 $display.= " <img src=\"$image\" align=\"middle\" alt=\"*\"> $headline\n";
1149 $display.= "</div>\n";
1151 if ($info != ""){
1152 $display.= "<div class=\"pluginfo\">\n";
1153 $display.= "$info";
1154 $display.= "</div>\n";
1155 } else {
1156 $display.= "<div style=\"height:5px;\">\n";
1157 $display.= " ";
1158 $display.= "</div>\n";
1159 }
1161 return ($display);
1162 }
1165 function register_global($name, $object)
1166 {
1167 $_SESSION[$name]= $object;
1168 }
1171 function is_global($name)
1172 {
1173 return isset($_SESSION[$name]);
1174 }
1177 function get_global($name)
1178 {
1179 return $_SESSION[$name];
1180 }
1183 function range_selector($dcnt,$start,$range=25)
1184 {
1186 /* Entries shown left and right from the selected entry */
1187 $max_entries= 10;
1189 /* Initialize and take care that max_entries is even */
1190 $output="";
1191 if ($max_entries & 1){
1192 $max_entries++;
1193 }
1195 /* Prevent output to start or end out of range */
1196 if ($start < 0 ){
1197 $start= 0 ;
1198 }
1199 if ($start >= $dcnt){
1200 $start= $range * (int)(($dcnt / $range) + 0.5);
1201 }
1203 $numpages= (($dcnt / $range));
1204 if(((int)($numpages))!=($numpages)){
1205 $numpages = (int)$numpages + 1;
1206 }
1207 if (((int)$numpages) <= 1 ){
1208 return ("");
1209 }
1210 $ppage= (int)(($start / $range) + 0.5);
1213 /* Align selected page to +/- max_entries/2 */
1214 $begin= $ppage - $max_entries/2;
1215 $end= $ppage + $max_entries/2;
1217 /* Adjust begin/end, so that the selected value is somewhere in
1218 the middle and the size is max_entries if possible */
1219 if ($begin < 0){
1220 $end-= $begin + 1;
1221 $begin= 0;
1222 }
1223 if ($end > $numpages) {
1224 $end= $numpages;
1225 }
1226 if (($end - $begin) < $max_entries && ($end - $max_entries) > 0){
1227 $begin= $end - $max_entries;
1228 }
1230 $output.= "<div style='border:1px solid #E0E0E0; background-color:#FFFFFF;'>";
1232 /* Draw decrement */
1233 if ($start > 0 ) {
1234 $output.=" <a href= \"main.php?plug=".validate($_GET['plug'])."&start=".
1235 (($start-$range))."\">".
1236 "<img alt=\"\" src=\"images/back.png\" border=0 align=\"middle\"></a>";
1237 }
1239 /* Draw pages */
1240 for ($i= $begin; $i < $end; $i++) {
1241 if ($ppage == $i){
1242 $output.= "<a style=\"background-color:#D0D0D0;\" href=\"main.php?plug=".
1243 validate($_GET['plug'])."&start=".
1244 ($i*$range)."\"> ".($i+1)." </a>";
1245 } else {
1246 $output.= "<a href=\"main.php?plug=".validate($_GET['plug']).
1247 "&start=".($i*$range)."\"> ".($i+1)." </a>";
1248 }
1249 }
1251 /* Draw increment */
1252 if($start < ($dcnt-$range)) {
1253 $output.=" <a href= \"main.php?plug=".validate($_GET['plug'])."&start=".
1254 (($start+($range)))."\">".
1255 "<img alt=\"\" src=\"images/forward.png\" border=0 align=\"middle\"></a>";
1256 }
1258 $output.= "</div>";
1260 return($output);
1261 }
1264 function apply_filter()
1265 {
1266 $apply= "";
1268 $apply= ''.
1269 '<table summary="" width="100%" style="background:#EEEEEE;border-top:1px solid #B0B0B0;"><tr><td width="100%" align="right">'.
1270 '<input type="submit" name="apply" value="'._("Apply").'"></td></tr></table>';
1272 return ($apply);
1273 }
1276 function back_to_main()
1277 {
1278 $string= '<br><p class="plugbottom"><input type=submit name="password_back" value="'.
1279 _("Back").'"></p><input type="hidden" name="ignore">';
1281 return ($string);
1282 }
1285 function normalize_netmask($netmask)
1286 {
1287 /* Check for notation of netmask */
1288 if (!preg_match('/^([0-9]+\.){3}[0-9]+$/', $netmask)){
1289 $num= (int)($netmask);
1290 $netmask= "";
1292 for ($byte= 0; $byte<4; $byte++){
1293 $result=0;
1295 for ($i= 7; $i>=0; $i--){
1296 if ($num-- > 0){
1297 $result+= pow(2,$i);
1298 }
1299 }
1301 $netmask.= $result.".";
1302 }
1304 return (preg_replace('/\.$/', '', $netmask));
1305 }
1307 return ($netmask);
1308 }
1311 function netmask_to_bits($netmask)
1312 {
1313 list($nm0, $nm1, $nm2, $nm3)= split('\.', $netmask);
1314 $res= 0;
1316 for ($n= 0; $n<4; $n++){
1317 $start= 255;
1318 $name= "nm$n";
1320 for ($i= 0; $i<8; $i++){
1321 if ($start == (int)($$name)){
1322 $res+= 8 - $i;
1323 break;
1324 }
1325 $start-= pow(2,$i);
1326 }
1327 }
1329 return ($res);
1330 }
1333 function recurse($rule, $variables)
1334 {
1335 $result= array();
1337 if (!count($variables)){
1338 return array($rule);
1339 }
1341 reset($variables);
1342 $key= key($variables);
1343 $val= current($variables);
1344 unset ($variables[$key]);
1346 foreach($val as $possibility){
1347 $nrule= preg_replace("/\{$key\}/", $possibility, $rule);
1348 $result= array_merge($result, recurse($nrule, $variables));
1349 }
1351 return ($result);
1352 }
1355 function expand_id($rule, $attributes)
1356 {
1357 /* Check for id rule */
1358 if(preg_match('/^id(:|#)\d+$/',$rule)){
1359 return (array("\{$rule}"));
1360 }
1362 /* Check for clean attribute */
1363 if (preg_match('/^%[a-zA-Z0-9]+$/', $rule)){
1364 $rule= preg_replace('/^%/', '', $rule);
1365 $val= rewrite(preg_replace('/ /', '', strtolower($attributes[$rule])));
1366 return (array($val));
1367 }
1369 /* Check for attribute with parameters */
1370 if (preg_match('/^%[a-zA-Z0-9]+\[[0-9-]+\]$/', $rule)){
1371 $param= preg_replace('/^[^[]+\[([^]]+)]$/', '\\1', $rule);
1372 $part= preg_replace('/^%/', '', preg_replace('/\[.*$/', '', $rule));
1373 $val= rewrite(preg_replace('/ /', '', strtolower($attributes[$part])));
1374 $start= preg_replace ('/-.*$/', '', $param);
1375 $stop = preg_replace ('/^[^-]+-/', '', $param);
1377 /* Assemble results */
1378 $result= array();
1379 for ($i= $start; $i<= $stop; $i++){
1380 $result[]= substr($val, 0, $i);
1381 }
1382 return ($result);
1383 }
1385 echo "Error in idgen string: don't know how to handle rule $rule.\n";
1386 return (array($rule));
1387 }
1390 function gen_uids($rule, $attributes)
1391 {
1392 global $config;
1394 /* Search for keys and fill the variables array with all
1395 possible values for that key. */
1396 $part= "";
1397 $trigger= false;
1398 $stripped= "";
1399 $variables= array();
1401 for ($pos= 0; $pos < strlen($rule); $pos++){
1403 if ($rule[$pos] == "{" ){
1404 $trigger= true;
1405 $part= "";
1406 continue;
1407 }
1409 if ($rule[$pos] == "}" ){
1410 $variables[$pos]= expand_id($part, $attributes);
1411 $stripped.= "\{$pos}";
1412 $trigger= false;
1413 continue;
1414 }
1416 if ($trigger){
1417 $part.= $rule[$pos];
1418 } else {
1419 $stripped.= $rule[$pos];
1420 }
1421 }
1423 /* Recurse through all possible combinations */
1424 $proposed= recurse($stripped, $variables);
1426 /* Get list of used ID's */
1427 $used= array();
1428 $ldap= $config->get_ldap_link();
1429 $ldap->cd($config->current['BASE']);
1430 $ldap->search('(uid=*)');
1432 while($attrs= $ldap->fetch()){
1433 $used[]= $attrs['uid'][0];
1434 }
1436 /* Remove used uids and watch out for id tags */
1437 $ret= array();
1438 foreach($proposed as $uid){
1440 /* Check for id tag and modify uid if needed */
1441 if(preg_match('/\{id:\d+}/',$uid)){
1442 $size= preg_replace('/^.*{id:(\d+)}.*$/', '\\1', $uid);
1444 for ($i= 0; $i < pow(10,$size); $i++){
1445 $number= sprintf("%0".$size."d", $i);
1446 $res= preg_replace('/{id:(\d+)}/', $number, $uid);
1447 if (!in_array($res, $used)){
1448 $uid= $res;
1449 break;
1450 }
1451 }
1452 }
1454 if(preg_match('/\{id#\d+}/',$uid)){
1455 $size= preg_replace('/^.*{id#(\d+)}.*$/', '\\1', $uid);
1457 while (true){
1458 mt_srand((double) microtime()*1000000);
1459 $number= sprintf("%0".$size."d", mt_rand(0, pow(10, $size)-1));
1460 $res= preg_replace('/{id#(\d+)}/', $number, $uid);
1461 if (!in_array($res, $used)){
1462 $uid= $res;
1463 break;
1464 }
1465 }
1466 }
1468 /* Don't assign used ones */
1469 if (!in_array($uid, $used)){
1470 $ret[]= $uid;
1471 }
1472 }
1474 return(array_unique($ret));
1475 }
1478 function array_search_r($needle, $key, $haystack){
1480 foreach($haystack as $index => $value){
1481 $match= 0;
1483 if (is_array($value)){
1484 $match= array_search_r($needle, $key, $value);
1485 }
1487 if ($index==$key && !is_array($value) && preg_match("/$needle/i", $value)){
1488 $match=1;
1489 }
1491 if ($match){
1492 return 1;
1493 }
1494 }
1496 return 0;
1497 }
1500 /* Sadly values like memory_limit are perpended by K, M, G, etc.
1501 Need to convert... */
1502 function to_byte($value) {
1503 $value= strtolower(trim($value));
1505 if(!is_numeric(substr($value, -1))) {
1507 switch(substr($value, -1)) {
1508 case 'g':
1509 $mult= 1073741824;
1510 break;
1511 case 'm':
1512 $mult= 1048576;
1513 break;
1514 case 'k':
1515 $mult= 1024;
1516 break;
1517 }
1519 return ($mult * (int)substr($value, 0, -1));
1520 } else {
1521 return $value;
1522 }
1523 }
1526 function in_array_ics($value, $items)
1527 {
1528 if (!is_array($items)){
1529 return (FALSE);
1530 }
1532 foreach ($items as $item){
1533 if (strtolower($item) == strtolower($value)) {
1534 return (TRUE);
1535 }
1536 }
1538 return (FALSE);
1539 }
1542 function generate_alphabet($count= 10)
1543 {
1544 $characters= _("*ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
1545 $alphabet= "";
1546 $c= 0;
1548 /* Fill cells with charaters */
1549 for ($i= 0; $i<mb_strlen($characters, 'UTF8'); $i++){
1550 if ($c == 0){
1551 $alphabet.= "<tr>";
1552 }
1554 $ch = mb_substr($characters, $i, 1, "UTF8");
1555 $alphabet.= "<td><a class=\"alphaselect\" href=\"main.php?plug=".
1556 validate($_GET['plug'])."&search=".$ch."\"> ".$ch." </a></td>";
1558 if ($c++ == $count){
1559 $alphabet.= "</tr>";
1560 $c= 0;
1561 }
1562 }
1564 /* Fill remaining cells */
1565 while ($c++ <= $count){
1566 $alphabet.= "<td> </td>";
1567 }
1569 return ($alphabet);
1570 }
1573 function validate($string)
1574 {
1575 return (strip_tags(preg_replace('/\0/', '', $string)));
1576 }
1578 function get_gosa_version()
1579 {
1580 global $svn_revision, $svn_path;
1582 /* Extract informations */
1583 $revision= preg_replace('/^[^0-9]*([0-9]+)[^0-9]*$/', '\1', $svn_revision);
1585 /* Release or development? */
1586 if (preg_match('%/gosa/trunk/%', $svn_path)){
1587 return (sprintf(_("GOsa development snapshot (Rev %s)"), $revision));
1588 } else {
1589 $release= preg_replace('%^.*/([^/]+)/include/functions.inc.*$%', '\1', $svn_path);
1590 return (sprintf(_("GOsa $release"), $revision));
1591 }
1592 }
1595 function rmdirRecursive($path, $followLinks=false) {
1596 $dir= opendir($path);
1597 while($entry= readdir($dir)) {
1598 if(is_file($path."/".$entry) || ((!$followLinks) && is_link($path."/".$entry))) {
1599 unlink($path."/".$entry);
1600 } elseif (is_dir($path."/".$entry) && $entry!='.' && $entry!='..') {
1601 rmdirRecursive($path."/".$entry);
1602 }
1603 }
1604 closedir($dir);
1605 return rmdir($path);
1606 }
1608 function scan_directory($path,$sort_desc=false)
1609 {
1610 $ret = false;
1612 /* is this a dir ? */
1613 if(is_dir($path)) {
1615 /* is this path a readable one */
1616 if(is_readable($path)){
1618 /* Get contents and write it into an array */
1619 $ret = array();
1621 $dir = opendir($path);
1623 /* Is this a correct result ?*/
1624 if($dir){
1625 while($fp = readdir($dir))
1626 $ret[]= $fp;
1627 }
1628 }
1629 }
1630 /* Sort array ascending , like scandir */
1631 sort($ret);
1633 /* Sort descending if parameter is sort_desc is set */
1634 if($sort_desc) {
1635 $ret = array_reverse($ret);
1636 }
1638 return($ret);
1639 }
1641 function clean_smarty_compile_dir($directory)
1642 {
1643 global $svn_revision;
1645 if(is_dir($directory) && is_readable($directory)) {
1646 // Set revision filename to REVISION
1647 $revision_file= $directory."/REVISION";
1649 /* Is there a stamp containing the current revision? */
1650 if(!file_exists($revision_file)) {
1651 // create revision file
1652 create_revision($revision_file, $svn_revision);
1653 } else {
1654 # check for "$config->...['CONFIG']/revision" and the
1655 # contents should match the revision number
1656 if(!compare_revision($revision_file, $svn_revision)){
1657 // If revision differs, clean compile directory
1658 foreach(scan_directory($directory) as $file) {
1659 if(($file==".")||($file=="..")) continue;
1660 if( is_file($directory."/".$file) &&
1661 is_writable($directory."/".$file)) {
1662 // delete file
1663 if(!unlink($directory."/".$file)) {
1664 print_red("File ".$directory."/".$file." could not be deleted.");
1665 // This should never be reached
1666 }
1667 } elseif(is_dir($directory."/".$file) &&
1668 is_writable($directory."/".$file)) {
1669 // Just recursively delete it
1670 rmdirRecursive($directory."/".$file);
1671 }
1672 }
1673 // We should now create a fresh revision file
1674 clean_smarty_compile_dir($directory);
1675 } else {
1676 // Revision matches, nothing to do
1677 }
1678 }
1679 } else {
1680 // Smarty compile dir is not accessible
1681 // (Smarty will warn about this)
1682 }
1683 }
1685 function create_revision($revision_file, $revision)
1686 {
1687 $result= false;
1689 if(is_dir(dirname($revision_file)) && is_writable(dirname($revision_file))) {
1690 if($fh= fopen($revision_file, "w")) {
1691 if(fwrite($fh, $revision)) {
1692 $result= true;
1693 }
1694 }
1695 fclose($fh);
1696 } else {
1697 print_red("Can not write to revision file");
1698 }
1700 return $result;
1701 }
1703 function compare_revision($revision_file, $revision)
1704 {
1705 // false means revision differs
1706 $result= false;
1708 if(file_exists($revision_file) && is_readable($revision_file)) {
1709 // Open file
1710 if($fh= fopen($revision_file, "r")) {
1711 // Compare File contents with current revision
1712 if($revision == fread($fh, filesize($revision_file))) {
1713 $result= true;
1714 }
1715 } else {
1716 print_red("Can not open revision file");
1717 }
1718 // Close file
1719 fclose($fh);
1720 }
1722 return $result;
1723 }
1725 function progressbar($percentage,$width=100,$height=15,$showvalue=false)
1726 {
1727 $str = ""; // Our return value will be saved in this var
1729 $color = dechex($percentage+150);
1730 $color2 = dechex(150 - $percentage);
1731 $bgcolor= $showvalue?"FFFFFF":"DDDDDD";
1733 $progress = (int)(($percentage /100)*$width);
1735 /* Abort printing out percentage, if divs are to small */
1738 /* If theres a better solution for this, use it... */
1739 $str = "
1740 <div style=\" width:".($width)."px;
1741 height:".($height)."px;
1742 background-color:#000000;
1743 padding:1px;\">
1745 <div style=\" width:".($width)."px;
1746 background-color:#$bgcolor;
1747 height:".($height)."px;\">
1749 <div style=\" width:".$progress."px;
1750 height:".$height."px;
1751 background-color:#".$color2.$color2.$color."; \">";
1754 if(($height >10)&&($showvalue)){
1755 $str.= "<font style=\"font-size:".($height-2)."px;color:#FF0000;align:middle;padding-left:".((int)(($width*0.4)))."px;\">
1756 <b>".$percentage."%</b>
1757 </font>";
1758 }
1760 $str.= "</div></div></div>";
1762 return($str);
1763 }
1766 function search_config($arr, $name, $return)
1767 {
1768 if (is_array($arr)){
1769 foreach ($arr as $a){
1770 if (isset($a['CLASS']) &&
1771 strtolower($a['CLASS']) == strtolower($name)){
1773 if (isset($a[$return])){
1774 return ($a[$return]);
1775 } else {
1776 return ("");
1777 }
1778 } else {
1779 $res= search_config ($a, $name, $return);
1780 if ($res != ""){
1781 return $res;
1782 }
1783 }
1784 }
1785 }
1786 return ("");
1787 }
1790 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1791 ?>