11921a6168bd41677ecb240bbbd8d5a1100a2728
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 /* Try to use users primary language */
147 $ui= get_userinfo();
148 if ($ui != NULL){
149 if ($ui->language != ""){
150 return ($ui->language);
151 }
152 }
154 /* Get list of languages */
155 if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])){
156 $lang= preg_replace("/\s+/", "", $_SERVER['HTTP_ACCEPT_LANGUAGE']);
157 $languages= split (',', $lang);
158 $languages[]= "C";
159 } else {
160 $languages= array("C");
161 }
163 /* Walk through languages and get first supported */
164 foreach ($languages as $val){
166 /* Strip off weight */
167 $lang= preg_replace("/;q=.*$/i", "", $val);
169 /* Simplify sub language handling */
170 $lang= preg_replace("/-.*$/", "", $lang);
172 /* Cancel loop if available in GOsa, or the last
173 entry has been reached */
174 if (is_dir("$BASE_DIR/locale/$lang")){
175 break;
176 }
177 }
179 return (strtolower($lang)."_".strtoupper($lang));
180 }
183 /* Rewrite ui object to another dn */
184 function change_ui_dn($dn, $newdn)
185 {
186 $ui= $_SESSION['ui'];
187 if ($ui->dn == $dn){
188 $ui->dn= $newdn;
189 $_SESSION['ui']= $ui;
190 }
191 }
194 /* Return theme path for specified file */
195 function get_template_path($filename= '', $plugin= FALSE, $path= "")
196 {
197 global $config, $BASE_DIR;
199 if (!@isset($config->data['MAIN']['THEME'])){
200 $theme= 'default';
201 } else {
202 $theme= $config->data['MAIN']['THEME'];
203 }
205 /* Return path for empty filename */
206 if ($filename == ''){
207 return ("themes/$theme/");
208 }
210 /* Return plugin dir or root directory? */
211 if ($plugin){
212 if ($path == ""){
213 $nf= preg_replace("!^".$BASE_DIR."/!", "", $_SESSION['plugin_dir']);
214 } else {
215 $nf= preg_replace("!^".$BASE_DIR."/!", "", $path);
216 }
217 if (file_exists("$BASE_DIR/ihtml/themes/$theme/$nf")){
218 return ("$BASE_DIR/ihtml/themes/$theme/$nf/$filename");
219 }
220 if (file_exists("$BASE_DIR/ihtml/themes/default/$nf")){
221 return ("$BASE_DIR/ihtml/themes/default/$nf/$filename");
222 }
223 if ($path == ""){
224 return ($_SESSION['plugin_dir']."/$filename");
225 } else {
226 return ($path."/$filename");
227 }
228 } else {
229 if (file_exists("themes/$theme/$filename")){
230 return ("themes/$theme/$filename");
231 }
232 if (file_exists("$BASE_DIR/ihtml/themes/$theme/$filename")){
233 return ("$BASE_DIR/ihtml/themes/$theme/$filename");
234 }
235 if (file_exists("themes/default/$filename")){
236 return ("themes/default/$filename");
237 }
238 if (file_exists("$BASE_DIR/ihtml/themes/default/$filename")){
239 return ("$BASE_DIR/ihtml/themes/default/$filename");
240 }
241 return ($filename);
242 }
243 }
246 function array_remove_entries($needles, $haystack)
247 {
248 $tmp= array();
250 /* Loop through entries to be removed */
251 foreach ($haystack as $entry){
252 if (!in_array($entry, $needles)){
253 $tmp[]= $entry;
254 }
255 }
257 return ($tmp);
258 }
261 function gosa_log ($message)
262 {
263 global $ui;
265 /* Preset to something reasonable */
266 $username= " unauthenticated";
268 /* Replace username if object is present */
269 if (isset($ui)){
270 if ($ui->username != ""){
271 $username= "[$ui->username]";
272 } else {
273 $username= "unknown";
274 }
275 }
277 syslog(LOG_INFO,"GOsa$username: $message");
278 }
281 function ldap_init ($server, $base, $binddn='', $pass='')
282 {
283 global $config;
285 $ldap = new LDAP ($binddn, $pass, $server, isset($config->current['RECURSIVE']) && $config->current['RECURSIVE'] == "true",
286 isset($config->current['TLS']) && $config->current['TLS'] == "true");
288 /* Sadly we've no proper return values here. Use the error message instead. */
289 if (!preg_match("/Success/i", $ldap->error)){
290 print_red(sprintf(_("Error when connecting the LDAP. Server said '%s'."),
291 $ldap->get_error()));
292 echo $_SESSION['errors'];
294 /* Hard error. We'd like to use the LDAP, anyway... */
295 exit;
296 }
298 /* Preset connection base to $base and return to caller */
299 $ldap->cd ($base);
300 return $ldap;
301 }
304 function ldap_login_user ($username, $password)
305 {
306 global $config;
308 /* look through the entire ldap */
309 $ldap = $config->get_ldap_link();
310 if (!preg_match("/Success/i", $ldap->error)){
311 print_red(sprintf(_("User login failed. LDAP server said '%s'."), $ldap->get_error()));
312 echo $_SESSION['errors'];
313 exit;
314 }
315 $ldap->cd($config->current['BASE']);
316 $ldap->search("(&(uid=$username)(objectClass=gosaAccount))", array("uid"));
318 /* get results, only a count of 1 is valid */
319 switch ($ldap->count()){
321 /* user not found */
322 case 0: return (NULL);
324 /* valid uniq user */
325 case 1:
326 break;
328 /* found more than one matching id */
329 default:
330 print_red(_("Username / UID is not unique. Please check your LDAP database."));
331 return (NULL);
332 }
334 /* LDAP schema is not case sensitive. Perform additional check. */
335 $attrs= $ldap->fetch();
336 if ($attrs['uid'][0] != $username){
337 return(NULL);
338 }
340 /* got user dn, fill acl's */
341 $ui= new userinfo($config, $ldap->getDN());
342 $ui->username= $username;
344 /* password check, bind as user with supplied password */
345 $ldap->disconnect();
346 $ldap= new LDAP($ui->dn, $password, $config->current['SERVER'],
347 isset($config->current['RECURSIVE']) &&
348 $config->current['RECURSIVE'] == "true",
349 isset($config->current['TLS'])
350 && $config->current['TLS'] == "true");
351 if (!preg_match("/Success/i", $ldap->error)){
352 return (NULL);
353 }
355 /* Username is set, load subtreeACL's now */
356 $ui->loadACL();
358 return ($ui);
359 }
362 function add_lock ($object, $user)
363 {
364 global $config;
366 /* Just a sanity check... */
367 if ($object == "" || $user == ""){
368 print_red(_("Error while adding a lock. Parameters are not set correctly, please check the source!"));
369 return;
370 }
372 /* Check for existing entries in lock area */
373 $ldap= $config->get_ldap_link();
374 $ldap->cd ($config->current['CONFIG']);
375 $ldap->search("(&(objectClass=gosaLockEntry)(gosaUser=$user)(gosaObject=$object))",
376 array("gosaUser"));
377 if (!preg_match("/Success/i", $ldap->error)){
378 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()));
379 return;
380 }
382 /* Add lock if none present */
383 if ($ldap->count() == 0){
384 $attrs= array();
385 $name= md5($object);
386 $ldap->cd("cn=$name,".$config->current['CONFIG']);
387 $attrs["objectClass"] = "gosaLockEntry";
388 $attrs["gosaUser"] = $user;
389 $attrs["gosaObject"] = $object;
390 $attrs["cn"] = "$name";
391 $ldap->add($attrs);
392 if (!preg_match("/Success/i", $ldap->error)){
393 print_red(sprintf(_("Adding a lock failed. LDAP server says '%s'."),
394 $ldap->get_error()));
395 return;
396 }
397 }
398 }
401 function del_lock ($object)
402 {
403 global $config;
405 /* Sanity check */
406 if ($object == ""){
407 return;
408 }
410 /* Check for existance and remove the entry */
411 $ldap= $config->get_ldap_link();
412 $ldap->cd ($config->current['CONFIG']);
413 $ldap->search ("(&(objectClass=gosaLockEntry)(gosaObject=$object))", array("gosaObject"));
414 $attrs= $ldap->fetch();
415 if ($ldap->getDN() != "" && preg_match("/Success/i", $ldap->error)){
416 $ldap->rmdir ($ldap->getDN());
418 if (!preg_match("/Success/i", $ldap->error)){
419 print_red(sprintf(_("Removing a lock failed. LDAP server says '%s'."),
420 $ldap->get_error()));
421 return;
422 }
423 }
424 }
427 function del_user_locks($userdn)
428 {
429 global $config;
431 /* Get LDAP ressources */
432 $ldap= $config->get_ldap_link();
433 $ldap->cd ($config->current['CONFIG']);
435 /* Remove all objects of this user, drop errors silently in this case. */
436 $ldap->search("(&(objectClass=gosaLockEntry)(gosaUser=$userdn))", array("gosaUser"));
437 while ($attrs= $ldap->fetch()){
438 $ldap->rmdir($attrs['dn']);
439 }
440 }
443 function get_lock ($object)
444 {
445 global $config;
447 /* Sanity check */
448 if ($object == ""){
449 print_red(_("Getting the lock from LDAP failed. Parameters are not set correctly, please check the source!"));
450 return("");
451 }
453 /* Get LDAP link, check for presence of the lock entry */
454 $user= "";
455 $ldap= $config->get_ldap_link();
456 $ldap->cd ($config->current['CONFIG']);
457 $ldap->search("(&(objectClass=gosaLockEntry)(gosaObject=$object))", array("gosaUser"));
458 if (!preg_match("/Success/i", $ldap->error)){
459 print_red (_("Can't get locking information in LDAP database. Please check the 'config' entry in gosa.conf!"));
460 return("");
461 }
463 /* Check for broken locking information in LDAP */
464 if ($ldap->count() > 1){
466 /* Hmm. We're removing broken LDAP information here and issue a warning. */
467 print_red(_("Found multiple locks for object to be locked. This should not be possible - cleaning up multiple references."));
469 /* Clean up these references now... */
470 while ($attrs= $ldap->fetch()){
471 $ldap->rmdir($attrs['dn']);
472 }
474 return("");
476 } elseif ($ldap->count() == 1){
477 $attrs = $ldap->fetch();
478 $user= $attrs['gosaUser'][0];
479 }
481 return ($user);
482 }
485 function get_list2($subtreeACL, $filter, $subsearch= TRUE, $base="", $attrs= array(), $flag= FALSE)
486 {
487 global $config;
489 /* Base the search on default base if not set */
490 $ldap= $config->get_ldap_link($flag);
491 if ($base == ""){
492 $ldap->cd ($config->current['BASE']);
493 } else {
494 $ldap->cd ($base);
495 }
497 /* Perform ONE or SUB scope searches? */
498 $ldap->ls ($filter);
500 /* Check for size limit exceeded messages for GUI feedback */
501 if (preg_match("/size limit/i", $ldap->error)){
502 $_SESSION['limit_exceeded']= TRUE;
503 } else {
504 $_SESSION['limit_exceeded']= FALSE;
505 }
506 $result= array();
509 /* Crawl through reslut entries and perform the migration to the
510 result array */
511 while($attrs = $ldap->fetch()) {
512 $dn= preg_replace("/[ ]*,[ ]*/", ",", $ldap->getDN());
513 foreach ($subtreeACL as $key => $value){
514 if (preg_match("/$key/", $dn)){
515 $attrs["dn"]= convert_department_dn($dn);
516 $result[]= $attrs;
517 break;
518 }
519 }
520 }
523 return ($result);
525 }
527 function get_list($subtreeACL, $filter, $subsearch= TRUE, $base="", $attrs= array(), $flag= FALSE)
528 {
529 global $config;
531 /* Base the search on default base if not set */
532 $ldap= $config->get_ldap_link($flag);
533 if ($base == ""){
534 $ldap->cd ($config->current['BASE']);
535 } else {
536 $ldap->cd ($base);
537 }
539 /* Perform ONE or SUB scope searches? */
540 if ($subsearch) {
541 $ldap->search ($filter, $attrs);
542 } else {
543 $ldap->ls ($filter);
544 }
546 /* Check for size limit exceeded messages for GUI feedback */
547 if (preg_match("/size limit/i", $ldap->error)){
548 $_SESSION['limit_exceeded']= TRUE;
549 } else {
550 $_SESSION['limit_exceeded']= FALSE;
551 }
553 /* Crawl through reslut entries and perform the migration to the
554 result array */
555 $result= array();
556 while($attrs = $ldap->fetch()) {
557 $dn= preg_replace("/[ ]*,[ ]*/", ",", $ldap->getDN());
558 foreach ($subtreeACL as $key => $value){
559 if (preg_match("/$key/", $dn)){
560 $attrs["dn"]= $dn;
561 $result[]= $attrs;
562 break;
563 }
564 }
565 }
567 return ($result);
568 }
571 function check_sizelimit()
572 {
573 /* Ignore dialog? */
574 if (isset($_SESSION['size_ignore']) && $_SESSION['size_ignore']){
575 return ("");
576 }
578 /* Eventually show dialog */
579 if (isset($_SESSION['limit_exceeded']) && $_SESSION['limit_exceeded']){
580 $smarty= get_smarty();
581 $smarty->assign('warning', sprintf(_("The size limit of %d entries is exceed!"),
582 $_SESSION['size_limit']));
583 $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).'">'));
584 return($smarty->fetch(get_template_path('sizelimit.tpl')));
585 }
587 return ("");
588 }
591 function print_sizelimit_warning()
592 {
593 if (isset($_SESSION['size_limit']) && $_SESSION['size_limit'] >= 10000000 ||
594 (isset($_SESSION['limit_exceeded']) && $_SESSION['limit_exceeded'])){
595 $config= "<input type='submit' name='edit_sizelimit' value="._("Configure").">";
596 } else {
597 $config= "";
598 }
599 if (isset($_SESSION['limit_exceeded']) && $_SESSION['limit_exceeded']){
600 return ("("._("incomplete").") $config");
601 }
602 return ("");
603 }
606 function eval_sizelimit()
607 {
608 if (isset($_POST['set_size_action'])){
610 /* User wants new size limit? */
611 if (is_id($_POST['new_limit']) &&
612 isset($_POST['action']) && $_POST['action']=="newlimit"){
614 $_SESSION['size_limit']= validate($_POST['new_limit']);
615 $_SESSION['size_ignore']= FALSE;
616 }
618 /* User wants no limits? */
619 if (isset($_POST['action']) && $_POST['action']=="ignore"){
620 $_SESSION['size_limit']= 0;
621 $_SESSION['size_ignore']= TRUE;
622 }
624 /* User wants incomplete results */
625 if (isset($_POST['action']) && $_POST['action']=="limited"){
626 $_SESSION['size_ignore']= TRUE;
627 }
628 }
630 /* Allow fallback to dialog */
631 if (isset($_POST['edit_sizelimit'])){
632 $_SESSION['size_ignore']= FALSE;
633 }
634 }
637 function get_permissions ($dn, $subtreeACL)
638 {
639 global $config;
641 $base= $config->current['BASE'];
642 $tmp= "d,".$dn;
643 $sacl= array();
645 /* Sort subacl's for lenght to simplify matching
646 for subtrees */
647 foreach ($subtreeACL as $key => $value){
648 $sacl[$key]= strlen($key);
649 }
650 arsort ($sacl);
651 reset ($sacl);
653 /* Successively remove leading parts of the dn's until
654 it doesn't contain commas anymore */
655 while (preg_match('/,/', $tmp)){
656 $tmp= ltrim(strstr($tmp, ","), ",");
658 /* Check for acl that may apply */
659 foreach ($sacl as $key => $value){
660 if (preg_match("/$key$/", $tmp)){
661 return ($subtreeACL[$key]);
662 }
663 }
664 }
666 return array("");
667 }
670 function get_module_permission($acl_array, $module, $dn)
671 {
672 global $ui;
674 $final= "";
675 foreach($acl_array as $acl){
677 /* Check for selfflag (!) in ACL to determine if
678 the user is allowed to change parts of his/her
679 own account */
680 if (preg_match("/^!/", $acl)){
681 if ($dn != "" && $dn != $ui->dn){
683 /* No match for own DN, give up on this ACL */
684 continue;
686 } else {
688 /* Matches own DN, remove the selfflag */
689 $acl= preg_replace("/^!/", "", $acl);
691 }
692 }
694 /* Remove leading garbage */
695 $acl= preg_replace("/^:/", "", $acl);
697 /* Discover if we've access to the submodule by comparing
698 all allowed submodules specified in the ACL */
699 $tmp= split(",", $acl);
700 foreach ($tmp as $mod){
701 if (preg_match("/^$module#/", $mod)){
702 $final= strstr($mod, "#")."#";
703 continue;
704 }
705 if (preg_match("/[^#]$module$/", $mod)){
706 return ("#all#");
707 }
708 if (preg_match("/^all$/", $mod)){
709 return ("#all#");
710 }
711 }
712 }
714 /* Return assembled ACL, or none */
715 if ($final != ""){
716 return (preg_replace('/##/', '#', $final));
717 }
719 /* Nothing matches - disable access for this object */
720 return ("#none#");
721 }
724 function get_userinfo()
725 {
726 global $ui;
728 return $ui;
729 }
732 function get_smarty()
733 {
734 global $smarty;
736 return $smarty;
737 }
740 function convert_department_dn($dn)
741 {
742 $dep= "";
744 /* Build a sub-directory style list of the tree level
745 specified in $dn */
746 foreach (split (",", $dn) as $val){
748 /* We're only interested in organizational units... */
749 if (preg_match ("/ou=/", $val)){
750 $dep= preg_replace("/ou=([^,]+)/", "\\1", $val)."/$dep";
751 }
753 /* ... and location objects */
754 if (preg_match ("/l=/", $val)){
755 $dep= preg_replace("/l=([^,]+)/", "\\1", $val)."/$dep";
756 }
757 }
759 /* Return and remove accidently trailing slashes */
760 return rtrim($dep, "/");
761 }
763 function convert_department_dn2($dn)
764 {
765 $dep= "";
767 /* Build a sub-directory style list of the tree level
768 specified in $dn */
769 $deps = array_flip($_SESSION['config']->idepartments);
771 if(isset($deps[$dn])){
772 $dn= $deps[$dn];
773 $tmp = split (",", $dn);
774 $dep = preg_replace("/^.*=/","",$tmp[0]);
775 }else{
776 $tmp = split (",", $dn);
777 $dep= preg_replace("%^.*/([^/]+)$%", "\\1", $tmp[0]);
778 }
780 /* Return and remove accidently trailing slashes */
781 $tmp = rtrim($dep, "/");
782 return $tmp;
783 }
786 function get_ou($name)
787 {
788 global $config;
790 $ou= $config->current[$name];
791 if ($ou != ""){
792 if (!preg_match('/^[^=]+=[^=]+/', $ou)){
793 return "ou=$ou,";
794 } else {
795 return "$ou,";
796 }
797 } else {
798 return "";
799 }
800 }
803 function get_people_ou()
804 {
805 return (get_ou("PEOPLE"));
806 }
809 function get_groups_ou()
810 {
811 return (get_ou("GROUPS"));
812 }
815 function get_winstations_ou()
816 {
817 return (get_ou("WINSTATIONS"));
818 }
821 function get_base_from_people($dn)
822 {
823 global $config;
825 $pattern= "/^[^,]+,".preg_quote(get_people_ou())."/";
826 $base= preg_replace($pattern, '', $dn);
828 /* Set to base, if we're not on a correct subtree */
829 if (!isset($config->idepartments[$base])){
830 $base= $config->current['BASE'];
831 }
833 return ($base);
834 }
837 function get_departments($ignore_dn= "")
838 {
839 global $config;
841 /* Initialize result hash */
842 $result= array();
843 $result['/']= $config->current['BASE'];
845 /* Get list of department objects */
846 $ldap= $config->get_ldap_link();
847 $ldap->cd ($config->current['BASE']);
848 $ldap->search ("(objectClass=gosaDepartment)", array("ou"));
849 while ($attrs= $ldap->fetch()){
850 $dn= $ldap->getDN();
851 if ($dn == $ignore_dn){
852 continue;
853 }
854 $result[convert_department_dn($dn)]= $dn;
855 }
857 return ($result);
858 }
861 function chkacl($acl, $name)
862 {
863 /* Look for attribute in ACL */
864 if (preg_match("/#$name#/", $acl) || $acl == "#all#"){
865 return ("");
866 }
868 /* Optically disable html object for no match */
869 return (" disabled ");
870 }
873 function is_phone_nr($nr)
874 {
875 if ($nr == ""){
876 return (TRUE);
877 }
879 return preg_match ("/^[0-9 ()+*-]+$/", $nr);
880 }
883 function is_url($url)
884 {
885 if ($url == ""){
886 return (TRUE);
887 }
889 return preg_match ("/^(http|https):\/\/((?:[a-zA-Z0-9_-]+\.?)+):?(\d*)/", $url);
890 }
893 function is_dn($dn)
894 {
895 if ($dn == ""){
896 return (TRUE);
897 }
899 return preg_match ("/^[a-z0-9 _-]+$/i", $dn);
900 }
903 function is_uid($uid)
904 {
905 global $config;
907 if ($uid == ""){
908 return (TRUE);
909 }
911 /* STRICT adds spaces and case insenstivity to the uid check.
912 This is dangerous and should not be used. */
913 if (isset($config->current['STRICT']) && preg_match('/^no$/i', $config->current['STRICT'])){
914 return preg_match ("/^[a-z0-9 _.-]+$/i", $uid);
915 } else {
916 return preg_match ("/^[a-z0-9_-]+$/", $uid);
917 }
918 }
921 function is_id($id)
922 {
923 if ($id == ""){
924 return (FALSE);
925 }
927 return preg_match ("/^[0-9]+$/", $id);
928 }
931 function is_path($path)
932 {
933 if ($path == ""){
934 return (TRUE);
935 }
936 if (!preg_match('/^[a-z0-9%\/_.+-]+$/i', $path)){
937 return (FALSE);
938 }
940 return preg_match ("/\/.+$/", $path);
941 }
944 function is_email($address, $template= FALSE)
945 {
946 if ($address == ""){
947 return (TRUE);
948 }
949 if ($template){
950 return preg_match ("/^[._a-z0-9%-]+@[_a-z0-9-]+(\.[a-z0-9-]+)(\.[a-z0-9-]+)*$/i",
951 $address);
952 } else {
953 return preg_match ("/^[._a-z0-9-]+@[_a-z0-9-]+(\.[a-z0-9i-]+)(\.[a-z0-9-]+)*$/i",
954 $address);
955 }
956 }
959 function print_red()
960 {
961 /* Check number of arguments */
962 if (func_num_args() < 1){
963 return;
964 }
966 /* Get arguments, save string */
967 $array = func_get_args();
968 $string= $array[0];
970 /* Step through arguments */
971 for ($i= 1; $i<count($array); $i++){
972 $string= preg_replace ("/%s/", $array[$i], $string, 1);
973 }
975 /* If DEBUGLEVEL is set, we're in web mode, use textual output in
976 the other case... */
977 if (isset($_SESSION['DEBUGLEVEL'])){
978 $_SESSION['errors'].= "<div align=\"left\" style=\"border-width:5px;".
979 "border-style:solid;border-color:red; background-color:black;".
980 "margin-bottom:10px; padding:8px;\"><table style='width:100%' summary=''><tr><td><img alt=\"\" src=\"".
981 get_template_path('images/warning.png')."\"></td>".
982 "<td width=\"100%\" style=\"text-align:center\"><font color=\"#FFFFFF\">".
983 "<b style='font-size:16px;'>$string</b></font></td><td>".
984 "<img alt=\"\"src=\"".get_template_path('images/warning.png').
985 "\"></td></tr></table></div>\n";
986 } else {
987 echo "Error: $string\n";
988 }
989 }
992 function gen_locked_message($user, $dn)
993 {
994 global $plug, $config;
996 $_SESSION['dn']= $dn;
997 $ldap= $config->get_ldap_link();
998 $ldap->cat ($user);
999 $attrs= $ldap->fetch();
1000 $uid= $attrs["uid"][0];
1002 /* Prepare and show template */
1003 $smarty= get_smarty();
1004 $smarty->assign ("dn", $dn);
1005 $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>"));
1007 return ($smarty->fetch (get_template_path('islocked.tpl')));
1008 }
1011 function to_string ($value)
1012 {
1013 /* If this is an array, generate a text blob */
1014 if (is_array($value)){
1015 $ret= "";
1016 foreach ($value as $line){
1017 $ret.= $line."<br>\n";
1018 }
1019 return ($ret);
1020 } else {
1021 return ($value);
1022 }
1023 }
1026 function get_printer_list($cups_server)
1027 {
1028 global $config;
1030 $res= array();
1032 /* Use CUPS, if we've access to it */
1033 if (function_exists('cups_get_dest_list')){
1034 $dest_list= cups_get_dest_list ($cups_server);
1036 foreach ($dest_list as $prt){
1037 $attr= cups_get_printer_attributes ($cups_server, $prt->name);
1039 foreach ($attr as $prt_info){
1040 if ($prt_info->name == "printer-info"){
1041 $info= $prt_info->value;
1042 break;
1043 }
1044 }
1045 $res[$prt->name]= "$info [$prt->name]";
1046 }
1048 /* CUPS is not available, try lpstat as a replacement */
1049 } else {
1050 $ar = false;
1051 exec("lpstat -p", $ar);
1052 foreach($ar as $val){
1053 list($dummy, $printer, $rest)= split(' ', $val, 3);
1054 if (preg_match('/^[^@]+$/', $printer)){
1055 $res[$printer]= "$printer";
1056 }
1057 }
1058 }
1060 /* Merge in printers from LDAP */
1061 $ldap= $config->get_ldap_link();
1062 $ldap->cd ($config->current['BASE']);
1063 $ldap->search('(objectClass=gotoPrinter)', array('cn'));
1064 while ($attrs= $ldap->fetch()){
1065 $res[$attrs["cn"][0]]= $attrs["cn"][0];
1066 }
1068 return $res;
1069 }
1072 function sess_del ($var)
1073 {
1074 /* New style */
1075 unset ($_SESSION[$var]);
1077 /* ... work around, since the first one
1078 doesn't seem to work all the time */
1079 session_unregister ($var);
1080 }
1083 function show_errors($message)
1084 {
1085 $complete= "";
1087 /* Assemble the message array to a plain string */
1088 foreach ($message as $error){
1089 if ($complete == ""){
1090 $complete= $error;
1091 } else {
1092 $complete= "$error<br>$complete";
1093 }
1094 }
1096 /* Fill ERROR variable with nice error dialog */
1097 print_red($complete);
1098 }
1101 function show_ldap_error($message)
1102 {
1103 if (!preg_match("/Success/i", $message)){
1104 print_red (_("LDAP error:")." $message");
1105 return TRUE;
1106 } else {
1107 return FALSE;
1108 }
1109 }
1112 function rewrite($s)
1113 {
1114 global $REWRITE;
1116 foreach ($REWRITE as $key => $val){
1117 $s= preg_replace("/$key/", "$val", $s);
1118 }
1120 return ($s);
1121 }
1124 function dn2base($dn)
1125 {
1126 global $config;
1128 if (get_people_ou() != ""){
1129 $dn= preg_replace('/,'.get_people_ou().'/' , ',', $dn);
1130 }
1131 if (get_groups_ou() != ""){
1132 $dn= preg_replace('/,'.get_groups_ou().'/' , ',', $dn);
1133 }
1134 $base= preg_replace ('/^[^,]+,/i', '', $dn);
1136 return ($base);
1137 }
1141 function check_command($cmdline)
1142 {
1143 $cmd= preg_replace("/ .*$/", "", $cmdline);
1145 /* Check if command exists in filesystem */
1146 if (!file_exists($cmd)){
1147 return (FALSE);
1148 }
1150 /* Check if command is executable */
1151 if (!is_executable($cmd)){
1152 return (FALSE);
1153 }
1155 return (TRUE);
1156 }
1159 function print_header($image, $headline, $info= "")
1160 {
1161 $display= "<div class=\"plugtop\">\n";
1162 $display.= " <p class=\"center\" style=\"margin:0px;padding:0px;font-size:24px;\"><img class=\"center\" src=\"$image\" align=\"middle\" alt=\"*\"> $headline</p>\n";
1163 $display.= "</div>\n";
1165 if ($info != ""){
1166 $display.= "<div class=\"pluginfo\">\n";
1167 $display.= "$info";
1168 $display.= "</div>\n";
1169 } else {
1170 $display.= "<div style=\"height:5px;\">\n";
1171 $display.= " ";
1172 $display.= "</div>\n";
1173 }
1175 return ($display);
1176 }
1179 function register_global($name, $object)
1180 {
1181 $_SESSION[$name]= $object;
1182 }
1185 function is_global($name)
1186 {
1187 return isset($_SESSION[$name]);
1188 }
1191 function get_global($name)
1192 {
1193 return $_SESSION[$name];
1194 }
1197 function range_selector($dcnt,$start,$range=25,$post_var=false)
1198 {
1200 /* Entries shown left and right from the selected entry */
1201 $max_entries= 10;
1203 /* Initialize and take care that max_entries is even */
1204 $output="";
1205 if ($max_entries & 1){
1206 $max_entries++;
1207 }
1209 if((!empty($post_var))&&(isset($_POST[$post_var]))){
1210 $range= $_POST[$post_var];
1211 }
1213 /* Prevent output to start or end out of range */
1214 if ($start < 0 ){
1215 $start= 0 ;
1216 }
1217 if ($start >= $dcnt){
1218 $start= $range * (int)(($dcnt / $range) + 0.5);
1219 }
1221 $numpages= (($dcnt / $range));
1222 if(((int)($numpages))!=($numpages)){
1223 $numpages = (int)$numpages + 1;
1224 }
1225 if ((((int)$numpages) <= 1 )&&(!$post_var)){
1226 return ("");
1227 }
1228 $ppage= (int)(($start / $range) + 0.5);
1231 /* Align selected page to +/- max_entries/2 */
1232 $begin= $ppage - $max_entries/2;
1233 $end= $ppage + $max_entries/2;
1235 /* Adjust begin/end, so that the selected value is somewhere in
1236 the middle and the size is max_entries if possible */
1237 if ($begin < 0){
1238 $end-= $begin + 1;
1239 $begin= 0;
1240 }
1241 if ($end > $numpages) {
1242 $end= $numpages;
1243 }
1244 if (($end - $begin) < $max_entries && ($end - $max_entries) > 0){
1245 $begin= $end - $max_entries;
1246 }
1248 if($post_var){
1249 $output.= "<div style='border:1px solid #E0E0E0; background-color:#FFFFFF;'>
1250 <table summary='' width='100%'><tr><td style='width:25%'></td><td style='text-align:center;'>";
1251 }else{
1252 $output.= "<div style='border:1px solid #E0E0E0; background-color:#FFFFFF;'>";
1253 }
1255 /* Draw decrement */
1256 if ($start > 0 ) {
1257 $output.=" <a href= \"main.php?plug=".validate($_GET['plug'])."&start=".
1258 (($start-$range))."\">".
1259 "<img class=\"center\" alt=\"\" src=\"images/back.png\" border=0 align=\"middle\"></a>";
1260 }
1262 /* Draw pages */
1263 for ($i= $begin; $i < $end; $i++) {
1264 if ($ppage == $i){
1265 $output.= "<a style=\"vertical-align:middle;background-color:#D0D0D0;\" href=\"main.php?plug=".
1266 validate($_GET['plug'])."&start=".
1267 ($i*$range)."\"> ".($i+1)." </a>";
1268 } else {
1269 $output.= "<a style=\"vertical-align:middle;\" href=\"main.php?plug=".validate($_GET['plug']).
1270 "&start=".($i*$range)."\"> ".($i+1)." </a>";
1271 }
1272 }
1274 /* Draw increment */
1275 if($start < ($dcnt-$range)) {
1276 $output.=" <a href= \"main.php?plug=".validate($_GET['plug'])."&start=".
1277 (($start+($range)))."\">".
1278 "<img class=\"center\" alt=\"\" src=\"images/forward.png\" border=\"0\" align=\"middle\"></a>";
1279 }
1281 if(($post_var)&&($numpages)){
1282 $output.= "</td><td style='width:25%;text-align:right;vertical-align:middle;'> "._("Entries per page")." <select style='vertical-align:middle;' name='".$post_var."' onChange='javascript:document.mainform.submit()'>";
1283 foreach(array(20,50,100,200,"all") as $num){
1284 if($num == "all"){
1285 $var = 10000;
1286 }else{
1287 $var = $num;
1288 }
1289 if($var == $range){
1290 $output.="\n<option selected='selected' value='".$var."'>".$num."</option>";
1291 }else{
1292 $output.="\n<option value='".$var."'>".$num."</option>";
1293 }
1294 }
1295 $output.= "</select></td></tr></table></div>";
1296 }else{
1297 $output.= "</div>";
1298 }
1300 return($output);
1301 }
1304 function apply_filter()
1305 {
1306 $apply= "";
1308 $apply= ''.
1309 '<table summary="" width="100%" style="background:#EEEEEE;border-top:1px solid #B0B0B0;"><tr><td width="100%" align="right">'.
1310 '<input type="submit" name="apply" value="'._("Apply filter").'"></td></tr></table>';
1312 return ($apply);
1313 }
1316 function back_to_main()
1317 {
1318 $string= '<br><p class="plugbottom"><input type=submit name="password_back" value="'.
1319 _("Back").'"></p><input type="hidden" name="ignore">';
1321 return ($string);
1322 }
1325 function normalize_netmask($netmask)
1326 {
1327 /* Check for notation of netmask */
1328 if (!preg_match('/^([0-9]+\.){3}[0-9]+$/', $netmask)){
1329 $num= (int)($netmask);
1330 $netmask= "";
1332 for ($byte= 0; $byte<4; $byte++){
1333 $result=0;
1335 for ($i= 7; $i>=0; $i--){
1336 if ($num-- > 0){
1337 $result+= pow(2,$i);
1338 }
1339 }
1341 $netmask.= $result.".";
1342 }
1344 return (preg_replace('/\.$/', '', $netmask));
1345 }
1347 return ($netmask);
1348 }
1351 function netmask_to_bits($netmask)
1352 {
1353 list($nm0, $nm1, $nm2, $nm3)= split('\.', $netmask);
1354 $res= 0;
1356 for ($n= 0; $n<4; $n++){
1357 $start= 255;
1358 $name= "nm$n";
1360 for ($i= 0; $i<8; $i++){
1361 if ($start == (int)($$name)){
1362 $res+= 8 - $i;
1363 break;
1364 }
1365 $start-= pow(2,$i);
1366 }
1367 }
1369 return ($res);
1370 }
1373 function recurse($rule, $variables)
1374 {
1375 $result= array();
1377 if (!count($variables)){
1378 return array($rule);
1379 }
1381 reset($variables);
1382 $key= key($variables);
1383 $val= current($variables);
1384 unset ($variables[$key]);
1386 foreach($val as $possibility){
1387 $nrule= preg_replace("/\{$key\}/", $possibility, $rule);
1388 $result= array_merge($result, recurse($nrule, $variables));
1389 }
1391 return ($result);
1392 }
1395 function expand_id($rule, $attributes)
1396 {
1397 /* Check for id rule */
1398 if(preg_match('/^id(:|#)\d+$/',$rule)){
1399 return (array("\{$rule}"));
1400 }
1402 /* Check for clean attribute */
1403 if (preg_match('/^%[a-zA-Z0-9]+$/', $rule)){
1404 $rule= preg_replace('/^%/', '', $rule);
1405 $val= rewrite(preg_replace('/ /', '', strtolower($attributes[$rule])));
1406 return (array($val));
1407 }
1409 /* Check for attribute with parameters */
1410 if (preg_match('/^%[a-zA-Z0-9]+\[[0-9-]+\]$/', $rule)){
1411 $param= preg_replace('/^[^[]+\[([^]]+)]$/', '\\1', $rule);
1412 $part= preg_replace('/^%/', '', preg_replace('/\[.*$/', '', $rule));
1413 $val= rewrite(preg_replace('/ /', '', strtolower($attributes[$part])));
1414 $start= preg_replace ('/-.*$/', '', $param);
1415 $stop = preg_replace ('/^[^-]+-/', '', $param);
1417 /* Assemble results */
1418 $result= array();
1419 for ($i= $start; $i<= $stop; $i++){
1420 $result[]= substr($val, 0, $i);
1421 }
1422 return ($result);
1423 }
1425 echo "Error in idgen string: don't know how to handle rule $rule.\n";
1426 return (array($rule));
1427 }
1430 function gen_uids($rule, $attributes)
1431 {
1432 global $config;
1434 /* Search for keys and fill the variables array with all
1435 possible values for that key. */
1436 $part= "";
1437 $trigger= false;
1438 $stripped= "";
1439 $variables= array();
1441 for ($pos= 0; $pos < strlen($rule); $pos++){
1443 if ($rule[$pos] == "{" ){
1444 $trigger= true;
1445 $part= "";
1446 continue;
1447 }
1449 if ($rule[$pos] == "}" ){
1450 $variables[$pos]= expand_id($part, $attributes);
1451 $stripped.= "\{$pos}";
1452 $trigger= false;
1453 continue;
1454 }
1456 if ($trigger){
1457 $part.= $rule[$pos];
1458 } else {
1459 $stripped.= $rule[$pos];
1460 }
1461 }
1463 /* Recurse through all possible combinations */
1464 $proposed= recurse($stripped, $variables);
1466 /* Get list of used ID's */
1467 $used= array();
1468 $ldap= $config->get_ldap_link();
1469 $ldap->cd($config->current['BASE']);
1470 $ldap->search('(uid=*)');
1472 while($attrs= $ldap->fetch()){
1473 $used[]= $attrs['uid'][0];
1474 }
1476 /* Remove used uids and watch out for id tags */
1477 $ret= array();
1478 foreach($proposed as $uid){
1480 /* Check for id tag and modify uid if needed */
1481 if(preg_match('/\{id:\d+}/',$uid)){
1482 $size= preg_replace('/^.*{id:(\d+)}.*$/', '\\1', $uid);
1484 for ($i= 0; $i < pow(10,$size); $i++){
1485 $number= sprintf("%0".$size."d", $i);
1486 $res= preg_replace('/{id:(\d+)}/', $number, $uid);
1487 if (!in_array($res, $used)){
1488 $uid= $res;
1489 break;
1490 }
1491 }
1492 }
1494 if(preg_match('/\{id#\d+}/',$uid)){
1495 $size= preg_replace('/^.*{id#(\d+)}.*$/', '\\1', $uid);
1497 while (true){
1498 mt_srand((double) microtime()*1000000);
1499 $number= sprintf("%0".$size."d", mt_rand(0, pow(10, $size)-1));
1500 $res= preg_replace('/{id#(\d+)}/', $number, $uid);
1501 if (!in_array($res, $used)){
1502 $uid= $res;
1503 break;
1504 }
1505 }
1506 }
1508 /* Don't assign used ones */
1509 if (!in_array($uid, $used)){
1510 $ret[]= $uid;
1511 }
1512 }
1514 return(array_unique($ret));
1515 }
1518 function array_search_r($needle, $key, $haystack){
1520 foreach($haystack as $index => $value){
1521 $match= 0;
1523 if (is_array($value)){
1524 $match= array_search_r($needle, $key, $value);
1525 }
1527 if ($index==$key && !is_array($value) && preg_match("/$needle/i", $value)){
1528 $match=1;
1529 }
1531 if ($match){
1532 return 1;
1533 }
1534 }
1536 return 0;
1537 }
1540 /* Sadly values like memory_limit are perpended by K, M, G, etc.
1541 Need to convert... */
1542 function to_byte($value) {
1543 $value= strtolower(trim($value));
1545 if(!is_numeric(substr($value, -1))) {
1547 switch(substr($value, -1)) {
1548 case 'g':
1549 $mult= 1073741824;
1550 break;
1551 case 'm':
1552 $mult= 1048576;
1553 break;
1554 case 'k':
1555 $mult= 1024;
1556 break;
1557 }
1559 return ($mult * (int)substr($value, 0, -1));
1560 } else {
1561 return $value;
1562 }
1563 }
1566 function in_array_ics($value, $items)
1567 {
1568 if (!is_array($items)){
1569 return (FALSE);
1570 }
1572 foreach ($items as $item){
1573 if (strtolower($item) == strtolower($value)) {
1574 return (TRUE);
1575 }
1576 }
1578 return (FALSE);
1579 }
1582 function generate_alphabet($count= 10)
1583 {
1584 $characters= _("*ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
1585 $alphabet= "";
1586 $c= 0;
1588 /* Fill cells with charaters */
1589 for ($i= 0; $i<mb_strlen($characters, 'UTF8'); $i++){
1590 if ($c == 0){
1591 $alphabet.= "<tr>";
1592 }
1594 $ch = mb_substr($characters, $i, 1, "UTF8");
1595 $alphabet.= "<td><a class=\"alphaselect\" href=\"main.php?plug=".
1596 validate($_GET['plug'])."&search=".$ch."\"> ".$ch." </a></td>";
1598 if ($c++ == $count){
1599 $alphabet.= "</tr>";
1600 $c= 0;
1601 }
1602 }
1604 /* Fill remaining cells */
1605 while ($c++ <= $count){
1606 $alphabet.= "<td> </td>";
1607 }
1609 return ($alphabet);
1610 }
1613 function validate($string)
1614 {
1615 return (strip_tags(preg_replace('/\0/', '', $string)));
1616 }
1618 function get_gosa_version()
1619 {
1620 global $svn_revision, $svn_path;
1622 /* Extract informations */
1623 $revision= preg_replace('/^[^0-9]*([0-9]+)[^0-9]*$/', '\1', $svn_revision);
1625 /* Release or development? */
1626 if (preg_match('%/gosa/trunk/%', $svn_path)){
1627 return (sprintf(_("GOsa development snapshot (Rev %s)"), $revision));
1628 } else {
1629 $release= preg_replace('%^.*/([^/]+)/include/functions.inc.*$%', '\1', $svn_path);
1630 return (sprintf(_("GOsa $release"), $revision));
1631 }
1632 }
1635 function rmdirRecursive($path, $followLinks=false) {
1636 $dir= opendir($path);
1637 while($entry= readdir($dir)) {
1638 if(is_file($path."/".$entry) || ((!$followLinks) && is_link($path."/".$entry))) {
1639 unlink($path."/".$entry);
1640 } elseif (is_dir($path."/".$entry) && $entry!='.' && $entry!='..') {
1641 rmdirRecursive($path."/".$entry);
1642 }
1643 }
1644 closedir($dir);
1645 return rmdir($path);
1646 }
1648 function scan_directory($path,$sort_desc=false)
1649 {
1650 $ret = false;
1652 /* is this a dir ? */
1653 if(is_dir($path)) {
1655 /* is this path a readable one */
1656 if(is_readable($path)){
1658 /* Get contents and write it into an array */
1659 $ret = array();
1661 $dir = opendir($path);
1663 /* Is this a correct result ?*/
1664 if($dir){
1665 while($fp = readdir($dir))
1666 $ret[]= $fp;
1667 }
1668 }
1669 }
1670 /* Sort array ascending , like scandir */
1671 sort($ret);
1673 /* Sort descending if parameter is sort_desc is set */
1674 if($sort_desc) {
1675 $ret = array_reverse($ret);
1676 }
1678 return($ret);
1679 }
1681 function clean_smarty_compile_dir($directory)
1682 {
1683 global $svn_revision;
1685 if(is_dir($directory) && is_readable($directory)) {
1686 // Set revision filename to REVISION
1687 $revision_file= $directory."/REVISION";
1689 /* Is there a stamp containing the current revision? */
1690 if(!file_exists($revision_file)) {
1691 // create revision file
1692 create_revision($revision_file, $svn_revision);
1693 } else {
1694 # check for "$config->...['CONFIG']/revision" and the
1695 # contents should match the revision number
1696 if(!compare_revision($revision_file, $svn_revision)){
1697 // If revision differs, clean compile directory
1698 foreach(scan_directory($directory) as $file) {
1699 if(($file==".")||($file=="..")) continue;
1700 if( is_file($directory."/".$file) &&
1701 is_writable($directory."/".$file)) {
1702 // delete file
1703 if(!unlink($directory."/".$file)) {
1704 print_red("File ".$directory."/".$file." could not be deleted.");
1705 // This should never be reached
1706 }
1707 } elseif(is_dir($directory."/".$file) &&
1708 is_writable($directory."/".$file)) {
1709 // Just recursively delete it
1710 rmdirRecursive($directory."/".$file);
1711 }
1712 }
1713 // We should now create a fresh revision file
1714 clean_smarty_compile_dir($directory);
1715 } else {
1716 // Revision matches, nothing to do
1717 }
1718 }
1719 } else {
1720 // Smarty compile dir is not accessible
1721 // (Smarty will warn about this)
1722 }
1723 }
1725 function create_revision($revision_file, $revision)
1726 {
1727 $result= false;
1729 if(is_dir(dirname($revision_file)) && is_writable(dirname($revision_file))) {
1730 if($fh= fopen($revision_file, "w")) {
1731 if(fwrite($fh, $revision)) {
1732 $result= true;
1733 }
1734 }
1735 fclose($fh);
1736 } else {
1737 print_red("Can not write to revision file");
1738 }
1740 return $result;
1741 }
1743 function compare_revision($revision_file, $revision)
1744 {
1745 // false means revision differs
1746 $result= false;
1748 if(file_exists($revision_file) && is_readable($revision_file)) {
1749 // Open file
1750 if($fh= fopen($revision_file, "r")) {
1751 // Compare File contents with current revision
1752 if($revision == fread($fh, filesize($revision_file))) {
1753 $result= true;
1754 }
1755 } else {
1756 print_red("Can not open revision file");
1757 }
1758 // Close file
1759 fclose($fh);
1760 }
1762 return $result;
1763 }
1765 function progressbar($percentage,$width=100,$height=15,$showvalue=false)
1766 {
1767 $str = ""; // Our return value will be saved in this var
1769 $color = dechex($percentage+150);
1770 $color2 = dechex(150 - $percentage);
1771 $bgcolor= $showvalue?"FFFFFF":"DDDDDD";
1773 $progress = (int)(($percentage /100)*$width);
1775 /* Abort printing out percentage, if divs are to small */
1778 /* If theres a better solution for this, use it... */
1779 $str = "
1780 <div style=\" width:".($width)."px;
1781 height:".($height)."px;
1782 background-color:#000000;
1783 padding:1px;\">
1785 <div style=\" width:".($width)."px;
1786 background-color:#$bgcolor;
1787 height:".($height)."px;\">
1789 <div style=\" width:".$progress."px;
1790 height:".$height."px;
1791 background-color:#".$color2.$color2.$color."; \">";
1794 if(($height >10)&&($showvalue)){
1795 $str.= "<font style=\"font-size:".($height-2)."px;color:#FF0000;align:middle;padding-left:".((int)(($width*0.4)))."px;\">
1796 <b>".$percentage."%</b>
1797 </font>";
1798 }
1800 $str.= "</div></div></div>";
1802 return($str);
1803 }
1806 function search_config($arr, $name, $return)
1807 {
1808 if (is_array($arr)){
1809 foreach ($arr as $a){
1810 if (isset($a['CLASS']) &&
1811 strtolower($a['CLASS']) == strtolower($name)){
1813 if (isset($a[$return])){
1814 return ($a[$return]);
1815 } else {
1816 return ("");
1817 }
1818 } else {
1819 $res= search_config ($a, $name, $return);
1820 if ($res != ""){
1821 return $res;
1822 }
1823 }
1824 }
1825 }
1826 return ("");
1827 }
1830 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1831 ?>