Moving 2.0 development version to main trunk.
[fa-stable.git] / includes / session.inc
1 <?php
2         /*--------------------------------------------------\
3         |               |               | session.inc       |
4         |---------------------------------------------------|
5     | front_accounting                                                                  |
6     | http://open-accounting.sourceforge.net/                   |
7     | by KylieTech Consulting                           |
8     | http://frontaccounting.com/                               |
9     | by Joe Hunt Consulting         |
10         \--------------------------------------------------*/
11
12         function output_html($text)
13         {
14           global $before_box, $Ajax, $messages;
15                 // Fatal errors are not send to error_handler, 
16                 // so we must check the output
17           if ($text && preg_match('/\bFatal error(<.*?>)?:(.*)/i', $text, $m)) {
18         $Ajax->aCommands = array();  // Don't update page on errors
19                 
20                 $messages[] = array(E_ERROR, $m[0], null, null);
21           }
22           $Ajax->run();
23           return  in_ajax() ? fmt_errors() : ($before_box.fmt_errors().$text);
24         }
25
26         if (!isset($path_to_root))
27         {
28                 $path_to_root = ".";
29         }
30
31     include_once($path_to_root . "/includes/current_user.inc");
32
33 /*
34         // Make sure this directory exists and is writable!
35         $session_save_path = dirname(__FILE__).'/tmp/';
36 */
37
38         $session_save_path = session_save_path();
39         if (strpos($session_save_path, ";") !== false)
40                 $session_save_path = substr($session_save_path, strpos($session_save_path, ";") + 1);
41
42         if (isset($session_save_path))
43         {
44                 session_save_path($session_save_path);
45                 unset($session_save_path);
46         }
47         if (!isset($path_to_root) || isset($_GET['path_to_root']) || isset($_POST['path_to_root']))
48                 die("Restricted access");
49         include_once($path_to_root . "/includes/lang/language.php");
50         include_once($path_to_root . "/config_db.php");
51         include_once($path_to_root . "/config.php");
52         include_once($path_to_root . "/includes/ajax.inc");
53         $Ajax =& new Ajax();
54
55         include_once($path_to_root . "/includes/main.inc");
56
57     //----------------------------------------------------------------------------------------
58
59         function kill_login()
60         {
61         session_unset();
62                 session_destroy();
63         }
64
65         //----------------------------------------------------------------------------------------
66
67         function login_fail()
68         {
69         echo "<center><br><br><font size='5' color='red'><b>" . _("Incorrect Password") . "<b></font><br><br>";
70         echo "<b>" . _("The user and password combination is not valid for the system.") . "<b><br><br>";
71
72         echo _("If you are not an authorized user, please contact your system administrator to obtain an account to enable you to use the system.");
73                 echo "<br><a href='javascript:history.go(-1)'>" . _("Back") . "</a>";
74         echo "</center>";
75
76         kill_login();
77         die();
78         }
79
80         //----------------------------------------------------------------------------------------
81
82         function check_page_security($page_security)
83         {
84                 if (!$_SESSION["wa_current_user"]->check_user_access())
85                 {
86                         echo "<br><br><br><center>";
87                         echo "<b>" . _("Security settings have not been defined for your user account.");
88                         echo "<br>" . _("Please contact your system administrator.") . "</b>";
89
90                         kill_login();
91                         exit;
92                 }
93
94                 if (!$_SESSION["wa_current_user"]->can_access_page($page_security))
95                 {
96                         page(_("Access denied"));
97                         echo "<center><br><br><br><b>";
98                         echo _("The security settings on your account do not permit you to access this function");
99                         echo "</b>";
100                         echo "<br><br><br><br></center>";
101                         //echo '<script type="text/javascript">';
102                         //echo 'alert("' . _("The security settings on your account do not permit you to access this function") . '");';
103                         //echo 'history.go(-1)';
104                         //echo '</script>'
105                         end_page();
106                         //kill_login();
107                         exit;
108                 }
109         }
110
111         //----------------------------------------------------------------------------------------
112         if (!isset($_SESSION["wa_current_user"]) ||
113                 (isset($_SESSION["wa_current_user"]) && !$_SESSION["wa_current_user"]->logged_in()))
114         {
115
116                 $_SESSION["wa_current_user"] = new current_user();
117
118         // Show login screen
119         if (!isset($_POST["user_name_entry_field"]) or $_POST["user_name_entry_field"] == "")
120         {
121                 include($path_to_root . "/access/login.php");
122                 $Ajax->redirect($path_to_root . "/access/login.php");
123             exit;
124         }
125         }
126         include_once($path_to_root . "/includes/ui/ui_msgs.inc");
127         // intercept all output to destroy it in case of ajax call
128         register_shutdown_function('ob_end_flush');
129         ob_start('output_html',0);
130         // colect all error msgs
131         set_error_handler('error_handler' /*, errtypes */);
132
133         if (isset($_POST["user_name_entry_field"]))
134         {
135                 $succeed = $_SESSION["wa_current_user"]->login($_POST["company_login_name"],
136                         $_POST["user_name_entry_field"],
137                         md5($_POST["password"]));
138
139                 if (!$succeed)
140                 {
141                         // Incorrect password
142                         login_fail();
143                 }
144         }
145
146     // Run with debugging messages for the system administrator(s) but not anyone else
147     /*if (in_array(15, $security_groups[$_SESSION["AccessLevel"]])) {
148         $debug = 1;
149     } else {
150         $debug = 0;
151     }*/
152
153         //----------------------------------------------------------------------------------------
154
155         check_page_security($page_security);
156
157
158
159 ?>