caccbd39f5f2ad7825be16996ccb6d2abc5508f8
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", "/var/www/doc/");
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, "#")."#";
696 // HICKERT
697 // old source : continue;
698 return($final);
699 }
700 if (preg_match("/[^#]$module$/", $mod)){
701 return ("#all#");
702 }
703 if (preg_match("/^all$/", $mod)){
704 return ("#all#");
705 }
706 }
707 }
709 /* Return assembled ACL, or none */
710 if ($final != ""){
711 return (preg_replace('/##/', '#', $final));
712 }
714 /* Nothing matches - disable access for this object */
715 return ("#none#");
716 }
719 function get_userinfo()
720 {
721 global $ui;
723 return $ui;
724 }
727 function get_smarty()
728 {
729 global $smarty;
731 return $smarty;
732 }
735 function convert_department_dn($dn)
736 {
737 $dep= "";
739 /* Build a sub-directory style list of the tree level
740 specified in $dn */
741 foreach (split (",", $dn) as $val){
743 /* We're only interested in organizational units... */
744 if (preg_match ("/ou=/", $val)){
745 $dep= preg_replace("/ou=([^,]+)/", "\\1", $val)."/$dep";
746 }
748 /* ... and location objects */
749 if (preg_match ("/l=/", $val)){
750 $dep= preg_replace("/l=([^,]+)/", "\\1", $val)."/$dep";
751 }
752 }
754 /* Return and remove accidently trailing slashes */
755 return rtrim($dep, "/");
756 }
758 function convert_department_dn2($dn)
759 {
760 $dep= "";
762 /* Build a sub-directory style list of the tree level
763 specified in $dn */
764 $deps = array_flip($_SESSION['config']->idepartments);
766 if(isset($deps[$dn])){
767 $dn= $deps[$dn];
768 $tmp = split (",", $dn);
769 $dep = preg_replace("/^.*=/","",$tmp[0]);
770 }else{
771 $tmp = split (",", $dn);
772 $dep= preg_replace("%^.*/([^/]+)$%", "\\1", $tmp[0]);
773 }
775 /* Return and remove accidently trailing slashes */
776 $tmp = rtrim($dep, "/");
777 return $tmp;
778 }
781 function get_ou($name)
782 {
783 global $config;
785 $ou= $config->current[$name];
786 if ($ou != ""){
787 if (!preg_match('/^[^=]+=[^=]+/', $ou)){
788 return "ou=$ou,";
789 } else {
790 return "$ou,";
791 }
792 } else {
793 return "";
794 }
795 }
798 function get_people_ou()
799 {
800 return (get_ou("PEOPLE"));
801 }
804 function get_groups_ou()
805 {
806 return (get_ou("GROUPS"));
807 }
810 function get_winstations_ou()
811 {
812 return (get_ou("WINSTATIONS"));
813 }
816 function get_base_from_people($dn)
817 {
818 global $config;
820 $pattern= "/^[^,]+,".preg_quote(get_people_ou())."/";
821 $base= preg_replace($pattern, '', $dn);
823 /* Set to base, if we're not on a correct subtree */
824 if (!isset($config->idepartments[$base])){
825 $base= $config->current['BASE'];
826 }
828 return ($base);
829 }
832 function get_departments($ignore_dn= "")
833 {
834 global $config;
836 /* Initialize result hash */
837 $result= array();
838 $result['/']= $config->current['BASE'];
840 /* Get list of department objects */
841 $ldap= $config->get_ldap_link();
842 $ldap->cd ($config->current['BASE']);
843 $ldap->search ("(objectClass=gosaDepartment)", array("ou"));
844 while ($attrs= $ldap->fetch()){
845 $dn= $ldap->getDN();
846 if ($dn == $ignore_dn){
847 continue;
848 }
849 $result[convert_department_dn($dn)]= $dn;
850 }
852 return ($result);
853 }
856 function chkacl($acl, $name)
857 {
858 /* Look for attribute in ACL */
859 if (preg_match("/#$name#/", $acl) || $acl == "#all#"){
860 return ("");
861 }
863 /* Optically disable html object for no match */
864 return (" disabled ");
865 }
868 function is_phone_nr($nr)
869 {
870 if ($nr == ""){
871 return (TRUE);
872 }
874 return preg_match ("/^[0-9 ()+*-]+$/", $nr);
875 }
878 function is_url($url)
879 {
880 if ($url == ""){
881 return (TRUE);
882 }
884 return preg_match ("/^(http|https):\/\/((?:[a-zA-Z0-9_-]+\.?)+):?(\d*)/", $url);
885 }
888 function is_dn($dn)
889 {
890 if ($dn == ""){
891 return (TRUE);
892 }
894 return preg_match ("/^[a-z0-9 _-]+$/i", $dn);
895 }
898 function is_uid($uid)
899 {
900 global $config;
902 if ($uid == ""){
903 return (TRUE);
904 }
906 /* STRICT adds spaces and case insenstivity to the uid check.
907 This is dangerous and should not be used. */
908 if (isset($config->current['STRICT']) && preg_match('/^no$/i', $config->current['STRICT'])){
909 return preg_match ("/^[a-z0-9 _.-]+$/i", $uid);
910 } else {
911 return preg_match ("/^[a-z0-9_-]+$/", $uid);
912 }
913 }
916 function is_id($id)
917 {
918 if ($id == ""){
919 return (FALSE);
920 }
922 return preg_match ("/^[0-9]+$/", $id);
923 }
926 function is_path($path)
927 {
928 if ($path == ""){
929 return (TRUE);
930 }
931 if (!preg_match('/^[a-z0-9%\/_.+-]+$/i', $path)){
932 return (FALSE);
933 }
935 return preg_match ("/\/.+$/", $path);
936 }
939 function is_email($address, $template= FALSE)
940 {
941 if ($address == ""){
942 return (TRUE);
943 }
944 if ($template){
945 return preg_match ("/^[._a-z0-9%-]+@[_a-z0-9-]+(\.[a-z0-9-]+)(\.[a-z0-9-]+)*$/i",
946 $address);
947 } else {
948 return preg_match ("/^[._a-z0-9-]+@[_a-z0-9-]+(\.[a-z0-9i-]+)(\.[a-z0-9-]+)*$/i",
949 $address);
950 }
951 }
954 function print_red()
955 {
956 /* Check number of arguments */
957 if (func_num_args() < 1){
958 return;
959 }
961 /* Get arguments, save string */
962 $array = func_get_args();
963 $string= $array[0];
965 /* Step through arguments */
966 for ($i= 1; $i<count($array); $i++){
967 $string= preg_replace ("/%s/", $array[$i], $string, 1);
968 }
970 /* If DEBUGLEVEL is set, we're in web mode, use textual output in
971 the other case... */
972 if (isset($_SESSION['DEBUGLEVEL'])){
973 $_SESSION['errors'].= "<div align=\"left\" style=\"border-width:5px;".
974 "border-style:solid;border-color:red; background-color:black;".
975 "margin-bottom:10px; padding:8px;\"><table style='width:100%' summary=''><tr><td><img alt=\"\" src=\"".
976 get_template_path('images/warning.png')."\"></td>".
977 "<td width=\"100%\" style=\"text-align:center\"><font color=\"#FFFFFF\">".
978 "<b style='font-size:16px;'>$string</b></font></td><td>".
979 "<img alt=\"\"src=\"".get_template_path('images/warning.png').
980 "\"></td></tr></table></div>\n";
981 } else {
982 echo "Error: $string\n";
983 }
984 }
987 function gen_locked_message($user, $dn)
988 {
989 global $plug, $config;
991 $_SESSION['dn']= $dn;
992 $ldap= $config->get_ldap_link();
993 $ldap->cat ($user);
994 $attrs= $ldap->fetch();
995 $uid= $attrs["uid"][0];
997 /* Prepare and show template */
998 $smarty= get_smarty();
999 $smarty->assign ("dn", $dn);
1000 $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>"));
1002 return ($smarty->fetch (get_template_path('islocked.tpl')));
1003 }
1006 function to_string ($value)
1007 {
1008 /* If this is an array, generate a text blob */
1009 if (is_array($value)){
1010 $ret= "";
1011 foreach ($value as $line){
1012 $ret.= $line."<br>\n";
1013 }
1014 return ($ret);
1015 } else {
1016 return ($value);
1017 }
1018 }
1021 function get_printer_list($cups_server)
1022 {
1023 global $config;
1025 $res= array();
1027 /* Use CUPS, if we've access to it */
1028 if (function_exists('cups_get_dest_list')){
1029 $dest_list= cups_get_dest_list ($cups_server);
1031 foreach ($dest_list as $prt){
1032 $attr= cups_get_printer_attributes ($cups_server, $prt->name);
1034 foreach ($attr as $prt_info){
1035 if ($prt_info->name == "printer-info"){
1036 $info= $prt_info->value;
1037 break;
1038 }
1039 }
1040 $res[$prt->name]= "$info [$prt->name]";
1041 }
1043 /* CUPS is not available, try lpstat as a replacement */
1044 } else {
1045 $ar = false;
1046 exec("lpstat -p", $ar);
1047 foreach($ar as $val){
1048 list($dummy, $printer, $rest)= split(' ', $val, 3);
1049 if (preg_match('/^[^@]+$/', $printer)){
1050 $res[$printer]= "$printer";
1051 }
1052 }
1053 }
1055 /* Merge in printers from LDAP */
1056 $ldap= $config->get_ldap_link();
1057 $ldap->cd ($config->current['BASE']);
1058 $ldap->search('(objectClass=gotoPrinter)', array('cn'));
1059 while ($attrs= $ldap->fetch()){
1060 $res[$attrs["cn"][0]]= $attrs["cn"][0];
1061 }
1063 return $res;
1064 }
1067 function sess_del ($var)
1068 {
1069 /* New style */
1070 unset ($_SESSION[$var]);
1072 /* ... work around, since the first one
1073 doesn't seem to work all the time */
1074 session_unregister ($var);
1075 }
1078 function show_errors($message)
1079 {
1080 $complete= "";
1082 /* Assemble the message array to a plain string */
1083 foreach ($message as $error){
1084 if ($complete == ""){
1085 $complete= $error;
1086 } else {
1087 $complete= "$error<br>$complete";
1088 }
1089 }
1091 /* Fill ERROR variable with nice error dialog */
1092 print_red($complete);
1093 }
1096 function show_ldap_error($message)
1097 {
1098 if (!preg_match("/Success/i", $message)){
1099 print_red (_("LDAP error:")." $message");
1100 return TRUE;
1101 } else {
1102 return FALSE;
1103 }
1104 }
1107 function rewrite($s)
1108 {
1109 global $REWRITE;
1111 foreach ($REWRITE as $key => $val){
1112 $s= preg_replace("/$key/", "$val", $s);
1113 }
1115 return ($s);
1116 }
1119 function dn2base($dn)
1120 {
1121 global $config;
1123 if (get_people_ou() != ""){
1124 $dn= preg_replace('/,'.get_people_ou().'/' , ',', $dn);
1125 }
1126 if (get_groups_ou() != ""){
1127 $dn= preg_replace('/,'.get_groups_ou().'/' , ',', $dn);
1128 }
1129 $base= preg_replace ('/^[^,]+,/i', '', $dn);
1131 return ($base);
1132 }
1136 function check_command($cmdline)
1137 {
1138 $cmd= preg_replace("/ .*$/", "", $cmdline);
1140 /* Check if command exists in filesystem */
1141 if (!file_exists($cmd)){
1142 return (FALSE);
1143 }
1145 /* Check if command is executable */
1146 if (!is_executable($cmd)){
1147 return (FALSE);
1148 }
1150 return (TRUE);
1151 }
1154 function print_header($image, $headline, $info= "")
1155 {
1156 $display= "<div class=\"plugtop\">\n";
1157 $display.= " <img src=\"$image\" align=\"middle\" alt=\"*\"> $headline\n";
1158 $display.= "</div>\n";
1160 if ($info != ""){
1161 $display.= "<div class=\"pluginfo\">\n";
1162 $display.= "$info";
1163 $display.= "</div>\n";
1164 } else {
1165 $display.= "<div style=\"height:5px;\">\n";
1166 $display.= " ";
1167 $display.= "</div>\n";
1168 }
1170 return ($display);
1171 }
1174 function register_global($name, $object)
1175 {
1176 $_SESSION[$name]= $object;
1177 }
1180 function is_global($name)
1181 {
1182 return isset($_SESSION[$name]);
1183 }
1186 function get_global($name)
1187 {
1188 return $_SESSION[$name];
1189 }
1192 function range_selector($dcnt,$start,$range=25,$post_var=false)
1193 {
1195 /* Entries shown left and right from the selected entry */
1196 $max_entries= 10;
1198 /* Initialize and take care that max_entries is even */
1199 $output="";
1200 if ($max_entries & 1){
1201 $max_entries++;
1202 }
1204 if((!empty($post_var))&&(isset($_POST[$post_var]))){
1205 $range= $_POST[$post_var];
1206 }
1208 /* Prevent output to start or end out of range */
1209 if ($start < 0 ){
1210 $start= 0 ;
1211 }
1212 if ($start >= $dcnt){
1213 $start= $range * (int)(($dcnt / $range) + 0.5);
1214 }
1216 $numpages= (($dcnt / $range));
1217 if(((int)($numpages))!=($numpages)){
1218 $numpages = (int)$numpages + 1;
1219 }
1220 if ((((int)$numpages) <= 1 )&&(!$post_var)){
1221 return ("");
1222 }
1223 $ppage= (int)(($start / $range) + 0.5);
1226 /* Align selected page to +/- max_entries/2 */
1227 $begin= $ppage - $max_entries/2;
1228 $end= $ppage + $max_entries/2;
1230 /* Adjust begin/end, so that the selected value is somewhere in
1231 the middle and the size is max_entries if possible */
1232 if ($begin < 0){
1233 $end-= $begin + 1;
1234 $begin= 0;
1235 }
1236 if ($end > $numpages) {
1237 $end= $numpages;
1238 }
1239 if (($end - $begin) < $max_entries && ($end - $max_entries) > 0){
1240 $begin= $end - $max_entries;
1241 }
1243 if($post_var){
1244 $output.= "<div style='border:1px solid #E0E0E0; background-color:#FFFFFF;'>
1245 <table width='100%'><tr><td style='width:25%'></td><td style='align:center;'>";
1246 }else{
1247 $output.= "<div style='border:1px solid #E0E0E0; background-color:#FFFFFF;'>";
1248 }
1250 /* Draw decrement */
1251 if ($start > 0 ) {
1252 $output.=" <a href= \"main.php?plug=".validate($_GET['plug'])."&start=".
1253 (($start-$range))."\">".
1254 "<img alt=\"\" src=\"images/back.png\" border=0 align=\"middle\"></a>";
1255 }
1257 /* Draw pages */
1258 for ($i= $begin; $i < $end; $i++) {
1259 if ($ppage == $i){
1260 $output.= "<a style=\"background-color:#D0D0D0;\" href=\"main.php?plug=".
1261 validate($_GET['plug'])."&start=".
1262 ($i*$range)."\"> ".($i+1)." </a>";
1263 } else {
1264 $output.= "<a href=\"main.php?plug=".validate($_GET['plug']).
1265 "&start=".($i*$range)."\"> ".($i+1)." </a>";
1266 }
1267 }
1269 /* Draw increment */
1270 if($start < ($dcnt-$range)) {
1271 $output.=" <a href= \"main.php?plug=".validate($_GET['plug'])."&start=".
1272 (($start+($range)))."\">".
1273 "<img alt=\"\" src=\"images/forward.png\" border=0 align=\"middle\"></a>";
1274 }
1276 if(($post_var)&&($numpages)){
1277 $output.= "</td><td style='width:25%;text-align:right'> "._("Entries per page")." <select name='".$post_var."' onChange='javascript:document.mainform.submit()'>";
1278 foreach(array(20,50,100,200,"all") as $num){
1279 if($num == "all"){
1280 $var = 10000;
1281 }else{
1282 $var = $num;
1283 }
1284 if($var == $range){
1285 $output.="\n<option selected value='".$var."'>".$num."</option>";
1286 }else{
1287 $output.="\n<option value='".$var."'>".$num."</option>";
1288 }
1289 }
1290 $output.= "</select></td></tr></table></div>";
1291 }else{
1292 $output.= "</div>";
1293 }
1295 return($output);
1296 }
1299 function apply_filter()
1300 {
1301 $apply= "";
1303 $apply= ''.
1304 '<table summary="" width="100%" style="background:#EEEEEE;border-top:1px solid #B0B0B0;"><tr><td width="100%" align="right">'.
1305 '<input type="submit" name="apply" value="'._("Apply filter").'"></td></tr></table>';
1307 return ($apply);
1308 }
1311 function back_to_main()
1312 {
1313 $string= '<br><p class="plugbottom"><input type=submit name="password_back" value="'.
1314 _("Back").'"></p><input type="hidden" name="ignore">';
1316 return ($string);
1317 }
1320 function normalize_netmask($netmask)
1321 {
1322 /* Check for notation of netmask */
1323 if (!preg_match('/^([0-9]+\.){3}[0-9]+$/', $netmask)){
1324 $num= (int)($netmask);
1325 $netmask= "";
1327 for ($byte= 0; $byte<4; $byte++){
1328 $result=0;
1330 for ($i= 7; $i>=0; $i--){
1331 if ($num-- > 0){
1332 $result+= pow(2,$i);
1333 }
1334 }
1336 $netmask.= $result.".";
1337 }
1339 return (preg_replace('/\.$/', '', $netmask));
1340 }
1342 return ($netmask);
1343 }
1346 function netmask_to_bits($netmask)
1347 {
1348 list($nm0, $nm1, $nm2, $nm3)= split('\.', $netmask);
1349 $res= 0;
1351 for ($n= 0; $n<4; $n++){
1352 $start= 255;
1353 $name= "nm$n";
1355 for ($i= 0; $i<8; $i++){
1356 if ($start == (int)($$name)){
1357 $res+= 8 - $i;
1358 break;
1359 }
1360 $start-= pow(2,$i);
1361 }
1362 }
1364 return ($res);
1365 }
1368 function recurse($rule, $variables)
1369 {
1370 $result= array();
1372 if (!count($variables)){
1373 return array($rule);
1374 }
1376 reset($variables);
1377 $key= key($variables);
1378 $val= current($variables);
1379 unset ($variables[$key]);
1381 foreach($val as $possibility){
1382 $nrule= preg_replace("/\{$key\}/", $possibility, $rule);
1383 $result= array_merge($result, recurse($nrule, $variables));
1384 }
1386 return ($result);
1387 }
1390 function expand_id($rule, $attributes)
1391 {
1392 /* Check for id rule */
1393 if(preg_match('/^id(:|#)\d+$/',$rule)){
1394 return (array("\{$rule}"));
1395 }
1397 /* Check for clean attribute */
1398 if (preg_match('/^%[a-zA-Z0-9]+$/', $rule)){
1399 $rule= preg_replace('/^%/', '', $rule);
1400 $val= rewrite(preg_replace('/ /', '', strtolower($attributes[$rule])));
1401 return (array($val));
1402 }
1404 /* Check for attribute with parameters */
1405 if (preg_match('/^%[a-zA-Z0-9]+\[[0-9-]+\]$/', $rule)){
1406 $param= preg_replace('/^[^[]+\[([^]]+)]$/', '\\1', $rule);
1407 $part= preg_replace('/^%/', '', preg_replace('/\[.*$/', '', $rule));
1408 $val= rewrite(preg_replace('/ /', '', strtolower($attributes[$part])));
1409 $start= preg_replace ('/-.*$/', '', $param);
1410 $stop = preg_replace ('/^[^-]+-/', '', $param);
1412 /* Assemble results */
1413 $result= array();
1414 for ($i= $start; $i<= $stop; $i++){
1415 $result[]= substr($val, 0, $i);
1416 }
1417 return ($result);
1418 }
1420 echo "Error in idgen string: don't know how to handle rule $rule.\n";
1421 return (array($rule));
1422 }
1425 function gen_uids($rule, $attributes)
1426 {
1427 global $config;
1429 /* Search for keys and fill the variables array with all
1430 possible values for that key. */
1431 $part= "";
1432 $trigger= false;
1433 $stripped= "";
1434 $variables= array();
1436 for ($pos= 0; $pos < strlen($rule); $pos++){
1438 if ($rule[$pos] == "{" ){
1439 $trigger= true;
1440 $part= "";
1441 continue;
1442 }
1444 if ($rule[$pos] == "}" ){
1445 $variables[$pos]= expand_id($part, $attributes);
1446 $stripped.= "\{$pos}";
1447 $trigger= false;
1448 continue;
1449 }
1451 if ($trigger){
1452 $part.= $rule[$pos];
1453 } else {
1454 $stripped.= $rule[$pos];
1455 }
1456 }
1458 /* Recurse through all possible combinations */
1459 $proposed= recurse($stripped, $variables);
1461 /* Get list of used ID's */
1462 $used= array();
1463 $ldap= $config->get_ldap_link();
1464 $ldap->cd($config->current['BASE']);
1465 $ldap->search('(uid=*)');
1467 while($attrs= $ldap->fetch()){
1468 $used[]= $attrs['uid'][0];
1469 }
1471 /* Remove used uids and watch out for id tags */
1472 $ret= array();
1473 foreach($proposed as $uid){
1475 /* Check for id tag and modify uid if needed */
1476 if(preg_match('/\{id:\d+}/',$uid)){
1477 $size= preg_replace('/^.*{id:(\d+)}.*$/', '\\1', $uid);
1479 for ($i= 0; $i < pow(10,$size); $i++){
1480 $number= sprintf("%0".$size."d", $i);
1481 $res= preg_replace('/{id:(\d+)}/', $number, $uid);
1482 if (!in_array($res, $used)){
1483 $uid= $res;
1484 break;
1485 }
1486 }
1487 }
1489 if(preg_match('/\{id#\d+}/',$uid)){
1490 $size= preg_replace('/^.*{id#(\d+)}.*$/', '\\1', $uid);
1492 while (true){
1493 mt_srand((double) microtime()*1000000);
1494 $number= sprintf("%0".$size."d", mt_rand(0, pow(10, $size)-1));
1495 $res= preg_replace('/{id#(\d+)}/', $number, $uid);
1496 if (!in_array($res, $used)){
1497 $uid= $res;
1498 break;
1499 }
1500 }
1501 }
1503 /* Don't assign used ones */
1504 if (!in_array($uid, $used)){
1505 $ret[]= $uid;
1506 }
1507 }
1509 return(array_unique($ret));
1510 }
1513 function array_search_r($needle, $key, $haystack){
1515 foreach($haystack as $index => $value){
1516 $match= 0;
1518 if (is_array($value)){
1519 $match= array_search_r($needle, $key, $value);
1520 }
1522 if ($index==$key && !is_array($value) && preg_match("/$needle/i", $value)){
1523 $match=1;
1524 }
1526 if ($match){
1527 return 1;
1528 }
1529 }
1531 return 0;
1532 }
1535 /* Sadly values like memory_limit are perpended by K, M, G, etc.
1536 Need to convert... */
1537 function to_byte($value) {
1538 $value= strtolower(trim($value));
1540 if(!is_numeric(substr($value, -1))) {
1542 switch(substr($value, -1)) {
1543 case 'g':
1544 $mult= 1073741824;
1545 break;
1546 case 'm':
1547 $mult= 1048576;
1548 break;
1549 case 'k':
1550 $mult= 1024;
1551 break;
1552 }
1554 return ($mult * (int)substr($value, 0, -1));
1555 } else {
1556 return $value;
1557 }
1558 }
1561 function in_array_ics($value, $items)
1562 {
1563 if (!is_array($items)){
1564 return (FALSE);
1565 }
1567 foreach ($items as $item){
1568 if (strtolower($item) == strtolower($value)) {
1569 return (TRUE);
1570 }
1571 }
1573 return (FALSE);
1574 }
1577 function generate_alphabet($count= 10)
1578 {
1579 $characters= _("*ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
1580 $alphabet= "";
1581 $c= 0;
1583 /* Fill cells with charaters */
1584 for ($i= 0; $i<mb_strlen($characters, 'UTF8'); $i++){
1585 if ($c == 0){
1586 $alphabet.= "<tr>";
1587 }
1589 $ch = mb_substr($characters, $i, 1, "UTF8");
1590 $alphabet.= "<td><a class=\"alphaselect\" href=\"main.php?plug=".
1591 validate($_GET['plug'])."&search=".$ch."\"> ".$ch." </a></td>";
1593 if ($c++ == $count){
1594 $alphabet.= "</tr>";
1595 $c= 0;
1596 }
1597 }
1599 /* Fill remaining cells */
1600 while ($c++ <= $count){
1601 $alphabet.= "<td> </td>";
1602 }
1604 return ($alphabet);
1605 }
1608 function validate($string)
1609 {
1610 return (strip_tags(preg_replace('/\0/', '', $string)));
1611 }
1613 function get_gosa_version()
1614 {
1615 global $svn_revision, $svn_path;
1617 /* Extract informations */
1618 $revision= preg_replace('/^[^0-9]*([0-9]+)[^0-9]*$/', '\1', $svn_revision);
1620 /* Release or development? */
1621 if (preg_match('%/gosa/trunk/%', $svn_path)){
1622 return (sprintf(_("GOsa development snapshot (Rev %s)"), $revision));
1623 } else {
1624 $release= preg_replace('%^.*/([^/]+)/include/functions.inc.*$%', '\1', $svn_path);
1625 return (sprintf(_("GOsa $release"), $revision));
1626 }
1627 }
1630 function rmdirRecursive($path, $followLinks=false) {
1631 $dir= opendir($path);
1632 while($entry= readdir($dir)) {
1633 if(is_file($path."/".$entry) || ((!$followLinks) && is_link($path."/".$entry))) {
1634 unlink($path."/".$entry);
1635 } elseif (is_dir($path."/".$entry) && $entry!='.' && $entry!='..') {
1636 rmdirRecursive($path."/".$entry);
1637 }
1638 }
1639 closedir($dir);
1640 return rmdir($path);
1641 }
1643 function scan_directory($path,$sort_desc=false)
1644 {
1645 $ret = false;
1647 /* is this a dir ? */
1648 if(is_dir($path)) {
1650 /* is this path a readable one */
1651 if(is_readable($path)){
1653 /* Get contents and write it into an array */
1654 $ret = array();
1656 $dir = opendir($path);
1658 /* Is this a correct result ?*/
1659 if($dir){
1660 while($fp = readdir($dir))
1661 $ret[]= $fp;
1662 }
1663 }
1664 }
1665 /* Sort array ascending , like scandir */
1666 sort($ret);
1668 /* Sort descending if parameter is sort_desc is set */
1669 if($sort_desc) {
1670 $ret = array_reverse($ret);
1671 }
1673 return($ret);
1674 }
1676 function clean_smarty_compile_dir($directory)
1677 {
1678 global $svn_revision;
1680 if(is_dir($directory) && is_readable($directory)) {
1681 // Set revision filename to REVISION
1682 $revision_file= $directory."/REVISION";
1684 /* Is there a stamp containing the current revision? */
1685 if(!file_exists($revision_file)) {
1686 // create revision file
1687 create_revision($revision_file, $svn_revision);
1688 } else {
1689 # check for "$config->...['CONFIG']/revision" and the
1690 # contents should match the revision number
1691 if(!compare_revision($revision_file, $svn_revision)){
1692 // If revision differs, clean compile directory
1693 foreach(scan_directory($directory) as $file) {
1694 if(($file==".")||($file=="..")) continue;
1695 if( is_file($directory."/".$file) &&
1696 is_writable($directory."/".$file)) {
1697 // delete file
1698 if(!unlink($directory."/".$file)) {
1699 print_red("File ".$directory."/".$file." could not be deleted.");
1700 // This should never be reached
1701 }
1702 } elseif(is_dir($directory."/".$file) &&
1703 is_writable($directory."/".$file)) {
1704 // Just recursively delete it
1705 rmdirRecursive($directory."/".$file);
1706 }
1707 }
1708 // We should now create a fresh revision file
1709 clean_smarty_compile_dir($directory);
1710 } else {
1711 // Revision matches, nothing to do
1712 }
1713 }
1714 } else {
1715 // Smarty compile dir is not accessible
1716 // (Smarty will warn about this)
1717 }
1718 }
1720 function create_revision($revision_file, $revision)
1721 {
1722 $result= false;
1724 if(is_dir(dirname($revision_file)) && is_writable(dirname($revision_file))) {
1725 if($fh= fopen($revision_file, "w")) {
1726 if(fwrite($fh, $revision)) {
1727 $result= true;
1728 }
1729 }
1730 fclose($fh);
1731 } else {
1732 print_red("Can not write to revision file");
1733 }
1735 return $result;
1736 }
1738 function compare_revision($revision_file, $revision)
1739 {
1740 // false means revision differs
1741 $result= false;
1743 if(file_exists($revision_file) && is_readable($revision_file)) {
1744 // Open file
1745 if($fh= fopen($revision_file, "r")) {
1746 // Compare File contents with current revision
1747 if($revision == fread($fh, filesize($revision_file))) {
1748 $result= true;
1749 }
1750 } else {
1751 print_red("Can not open revision file");
1752 }
1753 // Close file
1754 fclose($fh);
1755 }
1757 return $result;
1758 }
1760 function progressbar($percentage,$width=100,$height=15,$showvalue=false)
1761 {
1762 $str = ""; // Our return value will be saved in this var
1764 $color = dechex($percentage+150);
1765 $color2 = dechex(150 - $percentage);
1766 $bgcolor= $showvalue?"FFFFFF":"DDDDDD";
1768 $progress = (int)(($percentage /100)*$width);
1770 /* Abort printing out percentage, if divs are to small */
1773 /* If theres a better solution for this, use it... */
1774 $str = "
1775 <div style=\" width:".($width)."px;
1776 height:".($height)."px;
1777 background-color:#000000;
1778 padding:1px;\">
1780 <div style=\" width:".($width)."px;
1781 background-color:#$bgcolor;
1782 height:".($height)."px;\">
1784 <div style=\" width:".$progress."px;
1785 height:".$height."px;
1786 background-color:#".$color2.$color2.$color."; \">";
1789 if(($height >10)&&($showvalue)){
1790 $str.= "<font style=\"font-size:".($height-2)."px;color:#FF0000;align:middle;padding-left:".((int)(($width*0.4)))."px;\">
1791 <b>".$percentage."%</b>
1792 </font>";
1793 }
1795 $str.= "</div></div></div>";
1797 return($str);
1798 }
1801 function search_config($arr, $name, $return)
1802 {
1803 if (is_array($arr)){
1804 foreach ($arr as $a){
1805 if (isset($a['CLASS']) &&
1806 strtolower($a['CLASS']) == strtolower($name)){
1808 if (isset($a[$return])){
1809 return ($a[$return]);
1810 } else {
1811 return ("");
1812 }
1813 } else {
1814 $res= search_config ($a, $name, $return);
1815 if ($res != ""){
1816 return $res;
1817 }
1818 }
1819 }
1820 }
1821 return ("");
1822 }
1825 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1826 ?>