10e663e1d8f2e85602d0275853ead0c8324670b8
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, "#")."#";
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 style='width:100%' summary=''><tr><td><img alt=\"\" src=\"".
973 get_template_path('images/warning.png')."\"></td>".
974 "<td width=\"100%\" style=\"text-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,$post_var=false)
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 if((!empty($post_var))&&(isset($_POST[$post_var]))){
1202 $range= $_POST[$post_var];
1203 }
1205 /* Prevent output to start or end out of range */
1206 if ($start < 0 ){
1207 $start= 0 ;
1208 }
1209 if ($start >= $dcnt){
1210 $start= $range * (int)(($dcnt / $range) + 0.5);
1211 }
1213 $numpages= (($dcnt / $range));
1214 if(((int)($numpages))!=($numpages)){
1215 $numpages = (int)$numpages + 1;
1216 }
1217 if ((((int)$numpages) <= 1 )&&(!$post_var)){
1218 return ("");
1219 }
1220 $ppage= (int)(($start / $range) + 0.5);
1223 /* Align selected page to +/- max_entries/2 */
1224 $begin= $ppage - $max_entries/2;
1225 $end= $ppage + $max_entries/2;
1227 /* Adjust begin/end, so that the selected value is somewhere in
1228 the middle and the size is max_entries if possible */
1229 if ($begin < 0){
1230 $end-= $begin + 1;
1231 $begin= 0;
1232 }
1233 if ($end > $numpages) {
1234 $end= $numpages;
1235 }
1236 if (($end - $begin) < $max_entries && ($end - $max_entries) > 0){
1237 $begin= $end - $max_entries;
1238 }
1240 if($post_var){
1241 $output.= "<div style='border:1px solid #E0E0E0; background-color:#FFFFFF;'>
1242 <table width='100%'><tr><td style='width:20%'></td><td style='align:center;'>";
1243 }else{
1244 $output.= "<div style='border:1px solid #E0E0E0; background-color:#FFFFFF;'>";
1245 }
1247 /* Draw decrement */
1248 if ($start > 0 ) {
1249 $output.=" <a href= \"main.php?plug=".validate($_GET['plug'])."&start=".
1250 (($start-$range))."\">".
1251 "<img alt=\"\" src=\"images/back.png\" border=0 align=\"middle\"></a>";
1252 }
1254 /* Draw pages */
1255 for ($i= $begin; $i < $end; $i++) {
1256 if ($ppage == $i){
1257 $output.= "<a style=\"background-color:#D0D0D0;\" href=\"main.php?plug=".
1258 validate($_GET['plug'])."&start=".
1259 ($i*$range)."\"> ".($i+1)." </a>";
1260 } else {
1261 $output.= "<a href=\"main.php?plug=".validate($_GET['plug']).
1262 "&start=".($i*$range)."\"> ".($i+1)." </a>";
1263 }
1264 }
1266 /* Draw increment */
1267 if($start < ($dcnt-$range)) {
1268 $output.=" <a href= \"main.php?plug=".validate($_GET['plug'])."&start=".
1269 (($start+($range)))."\">".
1270 "<img alt=\"\" src=\"images/forward.png\" border=0 align=\"middle\"></a>";
1271 }
1273 if(($post_var)&&($numpages)){
1274 $output.= "</td><td style='width:20%;text-align:right'> "._("Entries per page")." <select name='".$post_var."' onChange='javascript:document.mainform.submit()'>";
1275 foreach(array(20,50,100,200,"all") as $num){
1276 if($num == "all"){
1277 $var = 10000;
1278 }else{
1279 $var = $num;
1280 }
1281 if($var == $range){
1282 $output.="\n<option selected value='".$var."'>".$num."</option>";
1283 }else{
1284 $output.="\n<option value='".$var."'>".$num."</option>";
1285 }
1286 }
1287 $output.= "</select></td></tr></table></div>";
1288 }else{
1289 $output.= "</div>";
1290 }
1292 return($output);
1293 }
1296 function apply_filter()
1297 {
1298 $apply= "";
1300 $apply= ''.
1301 '<table summary="" width="100%" style="background:#EEEEEE;border-top:1px solid #B0B0B0;"><tr><td width="100%" align="right">'.
1302 '<input type="submit" name="apply" value="'._("Apply filter").'"></td></tr></table>';
1304 return ($apply);
1305 }
1308 function back_to_main()
1309 {
1310 $string= '<br><p class="plugbottom"><input type=submit name="password_back" value="'.
1311 _("Back").'"></p><input type="hidden" name="ignore">';
1313 return ($string);
1314 }
1317 function normalize_netmask($netmask)
1318 {
1319 /* Check for notation of netmask */
1320 if (!preg_match('/^([0-9]+\.){3}[0-9]+$/', $netmask)){
1321 $num= (int)($netmask);
1322 $netmask= "";
1324 for ($byte= 0; $byte<4; $byte++){
1325 $result=0;
1327 for ($i= 7; $i>=0; $i--){
1328 if ($num-- > 0){
1329 $result+= pow(2,$i);
1330 }
1331 }
1333 $netmask.= $result.".";
1334 }
1336 return (preg_replace('/\.$/', '', $netmask));
1337 }
1339 return ($netmask);
1340 }
1343 function netmask_to_bits($netmask)
1344 {
1345 list($nm0, $nm1, $nm2, $nm3)= split('\.', $netmask);
1346 $res= 0;
1348 for ($n= 0; $n<4; $n++){
1349 $start= 255;
1350 $name= "nm$n";
1352 for ($i= 0; $i<8; $i++){
1353 if ($start == (int)($$name)){
1354 $res+= 8 - $i;
1355 break;
1356 }
1357 $start-= pow(2,$i);
1358 }
1359 }
1361 return ($res);
1362 }
1365 function recurse($rule, $variables)
1366 {
1367 $result= array();
1369 if (!count($variables)){
1370 return array($rule);
1371 }
1373 reset($variables);
1374 $key= key($variables);
1375 $val= current($variables);
1376 unset ($variables[$key]);
1378 foreach($val as $possibility){
1379 $nrule= preg_replace("/\{$key\}/", $possibility, $rule);
1380 $result= array_merge($result, recurse($nrule, $variables));
1381 }
1383 return ($result);
1384 }
1387 function expand_id($rule, $attributes)
1388 {
1389 /* Check for id rule */
1390 if(preg_match('/^id(:|#)\d+$/',$rule)){
1391 return (array("\{$rule}"));
1392 }
1394 /* Check for clean attribute */
1395 if (preg_match('/^%[a-zA-Z0-9]+$/', $rule)){
1396 $rule= preg_replace('/^%/', '', $rule);
1397 $val= rewrite(preg_replace('/ /', '', strtolower($attributes[$rule])));
1398 return (array($val));
1399 }
1401 /* Check for attribute with parameters */
1402 if (preg_match('/^%[a-zA-Z0-9]+\[[0-9-]+\]$/', $rule)){
1403 $param= preg_replace('/^[^[]+\[([^]]+)]$/', '\\1', $rule);
1404 $part= preg_replace('/^%/', '', preg_replace('/\[.*$/', '', $rule));
1405 $val= rewrite(preg_replace('/ /', '', strtolower($attributes[$part])));
1406 $start= preg_replace ('/-.*$/', '', $param);
1407 $stop = preg_replace ('/^[^-]+-/', '', $param);
1409 /* Assemble results */
1410 $result= array();
1411 for ($i= $start; $i<= $stop; $i++){
1412 $result[]= substr($val, 0, $i);
1413 }
1414 return ($result);
1415 }
1417 echo "Error in idgen string: don't know how to handle rule $rule.\n";
1418 return (array($rule));
1419 }
1422 function gen_uids($rule, $attributes)
1423 {
1424 global $config;
1426 /* Search for keys and fill the variables array with all
1427 possible values for that key. */
1428 $part= "";
1429 $trigger= false;
1430 $stripped= "";
1431 $variables= array();
1433 for ($pos= 0; $pos < strlen($rule); $pos++){
1435 if ($rule[$pos] == "{" ){
1436 $trigger= true;
1437 $part= "";
1438 continue;
1439 }
1441 if ($rule[$pos] == "}" ){
1442 $variables[$pos]= expand_id($part, $attributes);
1443 $stripped.= "\{$pos}";
1444 $trigger= false;
1445 continue;
1446 }
1448 if ($trigger){
1449 $part.= $rule[$pos];
1450 } else {
1451 $stripped.= $rule[$pos];
1452 }
1453 }
1455 /* Recurse through all possible combinations */
1456 $proposed= recurse($stripped, $variables);
1458 /* Get list of used ID's */
1459 $used= array();
1460 $ldap= $config->get_ldap_link();
1461 $ldap->cd($config->current['BASE']);
1462 $ldap->search('(uid=*)');
1464 while($attrs= $ldap->fetch()){
1465 $used[]= $attrs['uid'][0];
1466 }
1468 /* Remove used uids and watch out for id tags */
1469 $ret= array();
1470 foreach($proposed as $uid){
1472 /* Check for id tag and modify uid if needed */
1473 if(preg_match('/\{id:\d+}/',$uid)){
1474 $size= preg_replace('/^.*{id:(\d+)}.*$/', '\\1', $uid);
1476 for ($i= 0; $i < pow(10,$size); $i++){
1477 $number= sprintf("%0".$size."d", $i);
1478 $res= preg_replace('/{id:(\d+)}/', $number, $uid);
1479 if (!in_array($res, $used)){
1480 $uid= $res;
1481 break;
1482 }
1483 }
1484 }
1486 if(preg_match('/\{id#\d+}/',$uid)){
1487 $size= preg_replace('/^.*{id#(\d+)}.*$/', '\\1', $uid);
1489 while (true){
1490 mt_srand((double) microtime()*1000000);
1491 $number= sprintf("%0".$size."d", mt_rand(0, pow(10, $size)-1));
1492 $res= preg_replace('/{id#(\d+)}/', $number, $uid);
1493 if (!in_array($res, $used)){
1494 $uid= $res;
1495 break;
1496 }
1497 }
1498 }
1500 /* Don't assign used ones */
1501 if (!in_array($uid, $used)){
1502 $ret[]= $uid;
1503 }
1504 }
1506 return(array_unique($ret));
1507 }
1510 function array_search_r($needle, $key, $haystack){
1512 foreach($haystack as $index => $value){
1513 $match= 0;
1515 if (is_array($value)){
1516 $match= array_search_r($needle, $key, $value);
1517 }
1519 if ($index==$key && !is_array($value) && preg_match("/$needle/i", $value)){
1520 $match=1;
1521 }
1523 if ($match){
1524 return 1;
1525 }
1526 }
1528 return 0;
1529 }
1532 /* Sadly values like memory_limit are perpended by K, M, G, etc.
1533 Need to convert... */
1534 function to_byte($value) {
1535 $value= strtolower(trim($value));
1537 if(!is_numeric(substr($value, -1))) {
1539 switch(substr($value, -1)) {
1540 case 'g':
1541 $mult= 1073741824;
1542 break;
1543 case 'm':
1544 $mult= 1048576;
1545 break;
1546 case 'k':
1547 $mult= 1024;
1548 break;
1549 }
1551 return ($mult * (int)substr($value, 0, -1));
1552 } else {
1553 return $value;
1554 }
1555 }
1558 function in_array_ics($value, $items)
1559 {
1560 if (!is_array($items)){
1561 return (FALSE);
1562 }
1564 foreach ($items as $item){
1565 if (strtolower($item) == strtolower($value)) {
1566 return (TRUE);
1567 }
1568 }
1570 return (FALSE);
1571 }
1574 function generate_alphabet($count= 10)
1575 {
1576 $characters= _("*ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
1577 $alphabet= "";
1578 $c= 0;
1580 /* Fill cells with charaters */
1581 for ($i= 0; $i<mb_strlen($characters, 'UTF8'); $i++){
1582 if ($c == 0){
1583 $alphabet.= "<tr>";
1584 }
1586 $ch = mb_substr($characters, $i, 1, "UTF8");
1587 $alphabet.= "<td><a class=\"alphaselect\" href=\"main.php?plug=".
1588 validate($_GET['plug'])."&search=".$ch."\"> ".$ch." </a></td>";
1590 if ($c++ == $count){
1591 $alphabet.= "</tr>";
1592 $c= 0;
1593 }
1594 }
1596 /* Fill remaining cells */
1597 while ($c++ <= $count){
1598 $alphabet.= "<td> </td>";
1599 }
1601 return ($alphabet);
1602 }
1605 function validate($string)
1606 {
1607 return (strip_tags(preg_replace('/\0/', '', $string)));
1608 }
1610 function get_gosa_version()
1611 {
1612 global $svn_revision, $svn_path;
1614 /* Extract informations */
1615 $revision= preg_replace('/^[^0-9]*([0-9]+)[^0-9]*$/', '\1', $svn_revision);
1617 /* Release or development? */
1618 if (preg_match('%/gosa/trunk/%', $svn_path)){
1619 return (sprintf(_("GOsa development snapshot (Rev %s)"), $revision));
1620 } else {
1621 $release= preg_replace('%^.*/([^/]+)/include/functions.inc.*$%', '\1', $svn_path);
1622 return (sprintf(_("GOsa $release"), $revision));
1623 }
1624 }
1627 function rmdirRecursive($path, $followLinks=false) {
1628 $dir= opendir($path);
1629 while($entry= readdir($dir)) {
1630 if(is_file($path."/".$entry) || ((!$followLinks) && is_link($path."/".$entry))) {
1631 unlink($path."/".$entry);
1632 } elseif (is_dir($path."/".$entry) && $entry!='.' && $entry!='..') {
1633 rmdirRecursive($path."/".$entry);
1634 }
1635 }
1636 closedir($dir);
1637 return rmdir($path);
1638 }
1640 function scan_directory($path,$sort_desc=false)
1641 {
1642 $ret = false;
1644 /* is this a dir ? */
1645 if(is_dir($path)) {
1647 /* is this path a readable one */
1648 if(is_readable($path)){
1650 /* Get contents and write it into an array */
1651 $ret = array();
1653 $dir = opendir($path);
1655 /* Is this a correct result ?*/
1656 if($dir){
1657 while($fp = readdir($dir))
1658 $ret[]= $fp;
1659 }
1660 }
1661 }
1662 /* Sort array ascending , like scandir */
1663 sort($ret);
1665 /* Sort descending if parameter is sort_desc is set */
1666 if($sort_desc) {
1667 $ret = array_reverse($ret);
1668 }
1670 return($ret);
1671 }
1673 function clean_smarty_compile_dir($directory)
1674 {
1675 global $svn_revision;
1677 if(is_dir($directory) && is_readable($directory)) {
1678 // Set revision filename to REVISION
1679 $revision_file= $directory."/REVISION";
1681 /* Is there a stamp containing the current revision? */
1682 if(!file_exists($revision_file)) {
1683 // create revision file
1684 create_revision($revision_file, $svn_revision);
1685 } else {
1686 # check for "$config->...['CONFIG']/revision" and the
1687 # contents should match the revision number
1688 if(!compare_revision($revision_file, $svn_revision)){
1689 // If revision differs, clean compile directory
1690 foreach(scan_directory($directory) as $file) {
1691 if(($file==".")||($file=="..")) continue;
1692 if( is_file($directory."/".$file) &&
1693 is_writable($directory."/".$file)) {
1694 // delete file
1695 if(!unlink($directory."/".$file)) {
1696 print_red("File ".$directory."/".$file." could not be deleted.");
1697 // This should never be reached
1698 }
1699 } elseif(is_dir($directory."/".$file) &&
1700 is_writable($directory."/".$file)) {
1701 // Just recursively delete it
1702 rmdirRecursive($directory."/".$file);
1703 }
1704 }
1705 // We should now create a fresh revision file
1706 clean_smarty_compile_dir($directory);
1707 } else {
1708 // Revision matches, nothing to do
1709 }
1710 }
1711 } else {
1712 // Smarty compile dir is not accessible
1713 // (Smarty will warn about this)
1714 }
1715 }
1717 function create_revision($revision_file, $revision)
1718 {
1719 $result= false;
1721 if(is_dir(dirname($revision_file)) && is_writable(dirname($revision_file))) {
1722 if($fh= fopen($revision_file, "w")) {
1723 if(fwrite($fh, $revision)) {
1724 $result= true;
1725 }
1726 }
1727 fclose($fh);
1728 } else {
1729 print_red("Can not write to revision file");
1730 }
1732 return $result;
1733 }
1735 function compare_revision($revision_file, $revision)
1736 {
1737 // false means revision differs
1738 $result= false;
1740 if(file_exists($revision_file) && is_readable($revision_file)) {
1741 // Open file
1742 if($fh= fopen($revision_file, "r")) {
1743 // Compare File contents with current revision
1744 if($revision == fread($fh, filesize($revision_file))) {
1745 $result= true;
1746 }
1747 } else {
1748 print_red("Can not open revision file");
1749 }
1750 // Close file
1751 fclose($fh);
1752 }
1754 return $result;
1755 }
1757 function progressbar($percentage,$width=100,$height=15,$showvalue=false)
1758 {
1759 $str = ""; // Our return value will be saved in this var
1761 $color = dechex($percentage+150);
1762 $color2 = dechex(150 - $percentage);
1763 $bgcolor= $showvalue?"FFFFFF":"DDDDDD";
1765 $progress = (int)(($percentage /100)*$width);
1767 /* Abort printing out percentage, if divs are to small */
1770 /* If theres a better solution for this, use it... */
1771 $str = "
1772 <div style=\" width:".($width)."px;
1773 height:".($height)."px;
1774 background-color:#000000;
1775 padding:1px;\">
1777 <div style=\" width:".($width)."px;
1778 background-color:#$bgcolor;
1779 height:".($height)."px;\">
1781 <div style=\" width:".$progress."px;
1782 height:".$height."px;
1783 background-color:#".$color2.$color2.$color."; \">";
1786 if(($height >10)&&($showvalue)){
1787 $str.= "<font style=\"font-size:".($height-2)."px;color:#FF0000;align:middle;padding-left:".((int)(($width*0.4)))."px;\">
1788 <b>".$percentage."%</b>
1789 </font>";
1790 }
1792 $str.= "</div></div></div>";
1794 return($str);
1795 }
1798 function search_config($arr, $name, $return)
1799 {
1800 if (is_array($arr)){
1801 foreach ($arr as $a){
1802 if (isset($a['CLASS']) &&
1803 strtolower($a['CLASS']) == strtolower($name)){
1805 if (isset($a[$return])){
1806 return ($a[$return]);
1807 } else {
1808 return ("");
1809 }
1810 } else {
1811 $res= search_config ($a, $name, $return);
1812 if ($res != ""){
1813 return $res;
1814 }
1815 }
1816 }
1817 }
1818 return ("");
1819 }
1822 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1823 ?>