DAViCal
Loading...
Searching...
No Matches
DAViCalSession.php
1<?php
11
17$session = 1; // Fake initialisation
18
19// The Session object uses some (optional) configurable SQL to load
20// the records related to the logged-on user... (the where clause gets added).
21// It's very important that someone not be able to externally control this,
22// so we make it a function rather than a variable.
26function local_session_sql() {
27 $sql = <<<EOSQL
28SELECT session.*, usr.*, principal.*
29 FROM session JOIN usr USING(user_no) JOIN principal USING(user_no)
30EOSQL;
31 return $sql;
32}
33
37require('Session.php');
38include_once('DAVResource.php');
39
40
41@Session::_CheckLogout();
42
48class DAViCalSession extends Session
49{
50
51 public $principal_id;
52 public $type_id;
53 public $default_privileges;
54 public $displayname;
55 private $privilege_resources = array();
56
65 function __construct( $sid='' ) {
66 $this->principal_id = null;
67 parent::__construct($sid);
68 }
69
70
75 function AssignSessionDetails( $u ) {
76 if ( !isset($u->principal_id) ) {
77 // If they don't have a principal_id set then we should re-read from our local database
78 $qry = new AwlQuery('SELECT * FROM dav_principal WHERE username = :username', array(':username' => $u->username) );
79 if ( $qry->Exec() && $qry->rows() == 1 ) {
80 $u = $qry->Fetch();
81 }
82 }
83
84 parent::AssignSessionDetails( $u );
85 $this->GetRoles();
86 if ( function_exists('awl_set_locale') && isset($this->locale) && $this->locale != '' ) {
87 awl_set_locale($this->locale);
88 }
89 }
90
91
95 function GetRoles () {
96 $this->roles = array();
97 $sql = 'SELECT role_name FROM roles JOIN role_member ON roles.role_no=role_member.role_no WHERE user_no = '.$this->user_no;
98 $qry = new AwlQuery( $sql );
99 if ( $qry->Exec('DAViCalSession') && $qry->rows() > 0 ) {
100 while( $role = $qry->Fetch() ) {
101 $this->roles[$role->role_name] = 1;
102 }
103 }
104 // inherit the Admin role
105 $sql = 'SELECT role_name FROM (((group_member JOIN dav_principal first_dav_principal ON group_member.group_id=first_dav_principal.principal_id) JOIN role_member ON first_dav_principal.user_no=role_member.user_no) JOIN roles ON roles.role_no=role_member.role_no) JOIN dav_principal second_dav_principal ON group_member.member_id=second_dav_principal.principal_id WHERE second_dav_principal.user_no = '.$this->user_no;
106 $qry = new AwlQuery( $sql );
107 if ( $qry->Exec('DAViCalSession') && $qry->rows() > 0 ) {
108 while( $role = $qry->Fetch() ) {
109 if($role->role_name=='Admin')
110 $this->roles['Admin'] = 1;
111 }
112 }
113 }
114
115
123 function HavePrivilegeTo( $do_what, $path, $any = null ) {
124 if ( $this->AllowedTo('Admin') ) return true;
125 if ( !isset($this->privilege_resources[$path]) ) {
126 $this->privilege_resources[$path] = new DAVResource($path);
127 }
128 $resource = $this->privilege_resources[$path];
129 if ( isset($resource) && $resource->Exists() ) {
130 return $resource->HavePrivilegeTo($do_what,$any);
131 }
132 return false;
133 }
134
135
141 function RenderLoginPanel() {
142 global $c;
143 $action_target = htmlspecialchars(preg_replace('/\?logout.*$/','',$_SERVER['REQUEST_URI']));
144 dbg_error_log( "Login", " RenderLoginPanel: action_target='%s'", $action_target );
145 $userprompt = translate("User Name");
146 $pwprompt = translate("Password");
147 $gobutton = htmlspecialchars(translate("GO!"));
148 $gotitle = htmlspecialchars(translate("Enter your username and password then click here to log in."));
149 $temppwprompt = translate("If you have forgotten your password then");
150 $temppwbutton = htmlspecialchars(translate("Help! I've forgotten my password!"));
151 if (isset($c->password_change_override) ) {
152 $temppw_html = '<a href="' . $c->password_change_override['href'] . '">' . $c->password_change_override['label'] . '</a>';
153 } else {
154 $temppwtitle = htmlspecialchars(translate("Enter a username, if you know it, and click here, to be e-mailed a temporary password."));
155 $temppw_html = '<input type="submit" value="' . $temppwbutton . '" title="' . $temppwtitle . '" name="lostpass" class="submit" />';
156 }
157 $html = <<<EOTEXT
158<div id="logon">
159<form action="$action_target" method="post">
160<table>
161<tr>
162<th class="prompt">$userprompt:</th>
163<td class="entry">
164<input class="text" type="text" name="username" size="12" /></td>
165</tr>
166<tr>
167<th class="prompt">$pwprompt:</th>
168<td class="entry">
169<input class="password" type="password" name="password" size="12" />
170</td>
171</tr>
172<tr>
173<th class="prompt">&nbsp;</th>
174<td class="entry">
175<input type="submit" value="$gobutton" title="$gotitle" name="submit" class="submit" />
176</td>
177</tr>
178</table>
179<p>
180$temppwprompt: $temppw_html
181</p>
182</form>
183</div>
184
185EOTEXT;
186 return $html;
187 }
188
189
199 function LoginRequired( $roles = '' ) {
200 global $c, $session, $main_menu, $sub_menu, $tab_menu;
201
202 $current_domain = (isset($_SERVER['SERVER_NAME'])?$_SERVER['SERVER_NAME']:$_SERVER['SERVER_ADDR']);
203 if ( (isset($c->restrict_admin_domain) && $c->restrict_admin_domain != $current_domain)
204 || (isset($c->restrict_admin_port) && $c->restrict_admin_port != $_SERVER['SERVER_PORT'] ) ) {
205 header('Location: caldav.php');
206 dbg_error_log( 'LOG WARNING', 'Access to "%s" via "%s:%d" rejected.', $_SERVER['REQUEST_URI'], $current_domain, $_SERVER['SERVER_PORT'] );
207 @ob_flush(); exit(0);
208 }
209 if ( isset($c->restrict_admin_roles) && $roles == '' ) $roles = $c->restrict_admin_roles;
210 if ( $this->logged_in && $roles == '' ) return;
211
215 if ( isset($_SERVER['PHP_AUTH_USER']) && !$this->logged_in && $_SERVER['PHP_AUTH_USER'] != "" && $_SERVER['PHP_AUTH_PW'] != "" && ! $_COOKIE['NoAutoLogin'] ) {
216 if ( $this->Login($_SERVER['PHP_AUTH_USER'],$_SERVER['PHP_AUTH_PW'],false)) {
217 setcookie('NoAutoLogin',1,0);
218 return;
219 }
220 }
221 if ( ! $this->logged_in ) {
222 $c->messages[] = i18n('You must log in to use this system.');
223 include_once('page-header.php');
224 if ( function_exists('local_index_not_logged_in') ) {
225 local_index_not_logged_in();
226 }
227 else {
228 if ( $this->login_failed ) {
229 $c->messages[] = i18n('Invalid user name or password.');
230 }
231 echo '<h1>'.translate('Log On Please')."</h1>\n";
232 echo '<p>'.translate('For access to the')
233 .' '.translate($c->system_name).' '
234 .translate('you should log on with the username and password that have been issued to you.')
235 ."</p>\n";
236 echo '<p>'.translate('If you would like to request access, please e-mail').' '.$c->admin_email."</p>\n";
237 echo $this->RenderLoginPanel();
238 }
239 }
240 else {
241 $valid_roles = explode(',', $roles);
242 foreach( $valid_roles AS $k => $v ) {
243 if ( $this->AllowedTo($v) ) return;
244 }
245 $c->messages[] = i18n('You are not authorised to use this function.');
246 include_once('page-header.php');
247 }
248
249 include('page-footer.php');
250 @ob_flush(); exit(0);
251 }
252}
253
254$session = new DAViCalSession();
255$session->_CheckLogin();
256