Import CSS from http://www.lwis.net/
[misc/kostenrechnung] / lib / general.php
1 <?php
2
3 function check_session()
4 {
5   if (!empty($_POST['login']) && !empty($_POST['passwd'])) {
6     require_once('lib/login.php');
7     if (check_passwd()) {
8       header('Location: ./');
9       exit();
10     }
11   }
12
13   if (empty($_SESSION['sys']['login']) && empty($_GET['login'])) {
14     header('Location: ./?login=true');
15     exit();
16   }
17 }
18
19 function sanitise_filename($file)
20 {
21   return str_replace('./','x',$file);
22 }
23
24 function load_mask($name, $prefix = '')
25 {
26   global $mask;
27
28   $name = sanitise_filename($name);
29   $file = $prefix . 'masks/' . $name . '.php';
30
31   if (!file_exists($file))
32     return false;
33
34   include_once($file);
35
36   return true;
37 }
38
39 function connect_db()
40 {
41   $dsn = sprintf('host=%s dbname=%s user=%s password=%s',DBHOST,DBNAME,DBUSER,DBPASS);
42   pg_connect($dsn);
43 }
44
45 function load_js($jsfiles, $jscode)
46 {
47   $ret = '';
48   foreach ($jsfiles as $file)
49     $ret .= sprintf('<script type="text/javascript" src="%s"></script>'."\n", $file);
50
51   if (!empty($jscode))
52     $ret .= sprintf('<script type="text/javascript">'."\n%s\n</script>\n", implode("\n",$jscode));
53
54   return $ret;
55 }
56
57 function process()
58 {
59   if (!empty($_GET['login'])) {
60     require_once('lib/login.php');
61     return mask_login();
62   }
63
64   if (!empty($_GET['mask'])) {
65     require_once('lib/mask.php');
66     return mask($_GET['mask']);
67   }
68
69   $masks = array('sys_user','sys_group','sys_mask',
70                  'anbaugeraete','arbeitsarten','personal','materialien','gebiet','geraete',
71                  'kostenstellen','materialverbrauch','einsatz');
72   $ret = '';
73   foreach ($masks as $m)
74     $ret .= sprintf('<a href="index.php?mask=%s">%s</a><br>', $m, $m);
75   
76   return $ret;
77 }
78
79 function debug_log($text)
80 {
81   global $debug_info;
82
83   $debug_info .= '<br>' . $text;
84 }
85
86 function debug_info()
87 {
88   global $jsfiles;
89   global $debug_info;
90
91   if (DEBUG !== true) return '';
92
93   $jsfiles[] = 'lib/debug_joey.js';
94
95   $html = '<div style="background: #DDD; margin: 5px; padding-left: 4px; border: 1px solid #AAA;clear:both;">';
96   $html .= "\n<pre>\n\$_SESSION = " . var_export($_SESSION,true) . "\n";
97   $html .= "\n\$_COOKIE = " . var_export($_COOKIE,true) . "\n</pre>\n";
98   $html .= $debug_info;
99   $html .= '</div>';
100   return $html;
101 }
102
103 ?>