changelog for 9.12.08-1
[iserv-mod-room-reservation.git] / inc / mod_roomReservationConfig.inc
1 <?php
2 /**
3 * @file mod_roomReservationConfig.inc
4 * Handling of the configuration file
5 * @author Roland Hieber (roland.hieber@wilhelm-gym.net)
6 * @date 10.01.2008
7 *
8 * Copyright © 2007 Roland Hieber
9 *
10 * Permission is hereby granted, free of charge, to any person obtaining
11 * copy of this software and associated documentation files (the "Software"),
12 * to deal in the Software without restriction, including without limitation
13 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
14 * and/or sell copies of the Software, and to permit persons to whom the
15 * Software is furnished to do so, subject to the following conditions:
16 *
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 * THE SOFTWARE.
27 */
28
29 require_once("sec/secure.inc");
30 require_once("db.inc");
31 require_once("mod_room-reservation/functions.inc");
32 require_once("mod_room-reservation/mod_roomReservationTimeslice.inc");
33 require_once("mod_room-reservation/mod_roomReservationRoomsManager.inc");
34
35 /**
36 * Determines if a privilege has been assigned
37 * @param $sPriv (string) Privilege to test
38 * @return bool
39 */
40 function rrPrivilegeAssigned($sPriv) {
41 $h = db_query("SELECT act FROM privileges_assign WHERE privilege = $1;",
42 $sPriv);
43 return pg_num_rows($h) > 0;
44 }
45
46 /**
47 * Retrieve all groups that have a privilege assigned
48 * @param $strPriv (string) Privilege to test
49 * @return array
50 */
51 function rrPrivilegedGroups($strPriv) {
52 $aReturn = array();
53 $h = db_query("SELECT act FROM privileges_assign WHERE privilege = $1 ".
54 "ORDER BY act;", $strPriv);
55 if(pg_num_rows($h) > 0) {
56 while($a = pg_fetch_array($h)) {
57 $aReturn[] = $a["act"];
58 }
59 }
60 return $aReturn;
61 }
62
63 /**
64 * User-defined compare function to compare timeslices
65 * @param $oTs1 (mod_roomReservationTimeslice)
66 * @param $oTs2 (mod_roomReservationTimeslice)
67 * @return (int) <tt>-1</tt> if $oTs1 begins before $oTs2,
68 * <tt>0</tt> if the $oTs1 and $oTs2 have the same beginning,
69 * <tt>1</tt> if $oTs1 begins after $oTs2.
70 */
71 function rrConfigSortTimeslices(mod_roomReservationTimeslice $oTs1,
72 mod_roomReservationTimeslice $oTs2) {
73 if($oTs1->getBegin() == $oTs2->getBegin()) {
74 return 0;
75 } else {
76 return ($oTs1->getBegin() > $oTs2->getBegin()) ? 1 : -1;
77 }
78 }
79
80 define("MOD_ROOM_RESERVATION_CONFIGFILE_HEADER", "<?php
81 /**
82 * configuration file for package iserv-mod-room-reservation
83 * This file is written by the configuration script. Do not change it manually.
84 */\n");
85
86 /*****************************************************************************/
87 /**
88 * Handling of the configuration file
89 */
90 class mod_roomReservationConfig {
91
92 /** (array of rmTimeslice's) Timeslices */
93 protected $aoTimeslices;
94 /** (bool) Determine if the weekend is shown */
95 protected $bShowWeekend;
96 /** (bool) Determine if the strings "1. lesson", "2. lesson" etc are shown */
97 protected $bShowLessons;
98 /** (array of strings) error messages */
99 protected $asMessages;
100
101 /***************************************************************************/
102 /**
103 * @name Constructor
104 * @{
105 * Constructor.
106 * @return mod_roomReservationConfig
107 */
108 public function __construct() {
109 $this->flushTimeslices();
110 $this->setShowWeekend(false);
111 $this->setShowLessons(true);
112 $this->asMessages = array();
113
114 $this->readConfig();
115 }
116
117 /**
118 * **************************************************************************
119 * @}
120 * @name Access to attributes
121 * @{
122 */
123
124 /**
125 * Add a timeslice. A check is done that the timeslices do not overlap, and
126 * in this case, an Exception is thrown.
127 * @param $oTs (mod_roomReservationTimeslice)
128 * @throws Exception
129 * @return void
130 */
131 public function addTimeslice(mod_roomReservationTimeslice $oTs) {
132 // Check for overlapping timeslices
133 foreach($this->aoTimeslices as $oOldTs) {
134 if(($oOldTs->getBegin() < $oTs->getEnd() and
135 $oOldTs->getEnd() > $oTs->getBegin())) {
136 throw new Exception(
137 MOD_ROOM_RESERVATION_ERROR_CONFIG_OVERLAPPING_TIMESLICE);
138 }
139 }
140 $this->aoTimeslices[] = $oTs;
141 usort($this->aoTimeslices, "rrConfigSortTimeslices");
142 return;
143 }
144
145 /**
146 * Delete a timeslice
147 * @param $oTs (mod_roomReservationTimeslice) the timeslice to delete. If
148 * the timeslice is not found, an Exception is thrown.
149 * @throws Exception
150 * @return void
151 */
152 public function deleteTimeslice(mod_roomReservationTimeslice $oTs) {
153 for($i = 0; $i < count($this->aoTimeslices); $i++) {
154 if($this->aoTimeslices[$i]->getBegin() == $oTs->getBegin() and
155 $this->aoTimeslices[$i]->getEnd() == $oTs->getEnd()) {
156 // use array_splice because it renumbers the keys
157 array_splice($this->aoTimeslices, $i, 1);
158 return;
159 }
160 }
161 throw new Exception(MOD_ROOM_RESERVATION_ERROR_CONFIG_NO_SUCH_TIMESLICE);
162 }
163
164 /**
165 * Delete all timeslices.
166 * @return void
167 */
168 public function flushTimeslices() { $this->aoTimeslices = array(); }
169
170 /**
171 * Add a room to the list of rooms who can be booked. Throws an SQLException
172 * in case of an error.
173 * @param $sRoom (string) The name of the room
174 * @throws SQLException, Exception
175 * @return void
176 */
177 public function whitelistRoom($sRoom) {
178 if(!$this->isRoomWhitelisted($sRoom)) {
179 $r = db_store("mod_roomreservation_roomswhitelist",
180 array("rrr_name" => $sRoom));
181 if(!$r) {
182 throw new SQLException(MOD_ROOM_RESERVATION_ERROR_SQL);
183 } else {
184 log_insert(sprintf("Raum „%s“ für Buchungen zur Verfügung gestellt",
185 $sRoom));
186 }
187 }
188 }
189
190 /**
191 * Forbid bookings for a room. Throws an SQLException in case of an error.
192 * @param $sRoom The name of the room
193 * @throws SQLException
194 */
195 public function unWhitelistRoom($sRoom) {
196 $h = db_query("DELETE FROM mod_roomreservation_roomswhitelist WHERE ".
197 "rrr_name = $1;", $sRoom);
198 if(!$h) {
199 throw new SQLException(MOD_ROOM_RESERVATION_ERROR_SQL);
200 } else {
201 log_insert(sprintf("Raum „%s“ für Buchungen gesperrt", $sRoom));
202 }
203 }
204
205 /**
206 * Determine if a room is allowed for booking. Throws an SQLException
207 * in case of an error.
208 * @param $sRoom (string) The name of the room
209 * @return bool
210 * @throws SQLException
211 */
212 public function isRoomWhitelisted($sRoom) {
213 $h = db_query("SELECT * FROM mod_roomreservation_roomswhitelist WHERE ".
214 "rrr_name=$1;", $sRoom);
215 if(!$h) {
216 throw new SQLException(MOD_ROOM_RESERVATION_ERROR_SQL);
217 }
218 return (pg_num_rows($h) > 0);
219 }
220
221 /**
222 * Get all rooms that are allowed for booking. Throws an SQLException
223 * in case of an error.
224 * @throws SQLException
225 * @return array of mod_roomReservationRoomsManager objects
226 */
227 public function getWhitelistedRooms() {
228 $aor = mod_roomReservationRoomsManager::getRooms();
229 $ar = array();
230 foreach($aor as $key => $or) {
231 if($this->isRoomWhitelisted($or->getName())) {
232 $ar[] = $or;
233 }
234 }
235 return $ar;
236 }
237
238 /**
239 * Show or hide the weekend
240 * @param $b (bool)
241 */
242 public function setShowWeekend($b) { $this->bShowWeekend = ($b == true); }
243
244 /**
245 * Show or hide the lesson strings in the booking table
246 * @param $b (bool)
247 */
248 public function setShowLessons($b) { $this->bShowLessons = ($b == true); }
249
250 /**
251 * Add a message to the internal array of (error) messages
252 * @param $sMessage (string)
253 */
254 public function addMessage($sMessage) {
255 array_merge($this->asMessages, array($sMessage));
256 }
257
258 /**
259 * Get all timeslices in chronological order
260 * @return array of rmTimeslice
261 */
262 public function getTimeslices() { return $this->aoTimeslices; }
263
264 /**
265 * Return the starting times of every timeslice
266 * @param $bFormat (bool) <tt>true</tt>: Format the times according to the
267 * current locale
268 * <tt>false</tt>: return just the timestamps
269 * @return array
270 */
271 public function getTimesliceBeginnings($bFormat = false) {
272 $aot = $this->getTimeslices();
273 $aRet = array();
274 foreach($aot as $ao) {
275 $aRet[] = $bFormat ? gmstrftime(_("%#I:%M %p"), $ao->getBegin()) :
276 $ao->getBegin();
277 }
278 return $aRet;
279 }
280
281 /**
282 * Return the ending times of every timeslice
283 * @param $bFormat (bool) <tt>true</tt>: Format the times according to the
284 * current locale
285 * <tt>false</tt>: return just the timestamps
286 * @return array
287 */
288 public function getTimesliceEndings($bFormat = false) {
289 $aot = $this->getTimeslices();
290 $aRet = array();
291 foreach($aot as $ao) {
292 $aRet[] = $bFormat ? gmstrftime(_("%#I:%M %p"), $ao->getEnd()) :
293 $ao->getEnd();
294 }
295 return $aRet;
296 }
297
298 /**
299 * Get a timeslice
300 * @param $n (int) index of the timeslice in the array
301 * @return rmTimeslice
302 */
303 public function getTimeslice($n) { return $this->aoTimeslices[$n]; }
304
305 /**
306 * Determine if the weekend is shown
307 * @return bool
308 */
309 public function isShowWeekend() { return ($this->bShowWeekend == true); }
310
311 /**
312 * Determine if the lesson strings in the booking table are shown
313 * @return bool
314 */
315 public function isShowLessons() { return ($this->bShowLessons == true); }
316
317 /**
318 * Determine if the current user has admin rights. This function tests
319 * if the user is in a group which has the privilege of admin rights.
320 * @return bool
321 */
322 public function userIsAdmin() {
323 return secure_privilege("mod_roomreservation_admin");
324 }
325
326 /**
327 * Determine if the current user can book rooms. This function tests
328 * if the user is in a group which has the privilege to book rooms.
329 * If no group has this privilege, all users can book.
330 * @return bool
331 */
332 public function userCanBook() {
333 if(!rrPrivilegeAssigned("mod_roomreservation_book")) {
334 // If the privilege is not assigned to any group, all users can book
335 return true;
336 } else {
337 // If user is admin, it makes sense that he can book rooms ;-)
338 return secure_privilege("mod_roomreservation_book") ||
339 secure_privilege("mod_roomreservation_admin");
340 }
341 }
342
343 /**
344 * Determine if the current user can view bookings. This function tests
345 * if the user is in a group which has been configured as a group who
346 * can view bookings. If no groups are configured, any user can view the
347 * bookings table.
348 * @return bool
349 */
350 public function userCanView() {
351 if(!rrPrivilegeAssigned("mod_roomreservation_view")) {
352 // If the privilege is not assigned to any group, all users can view
353 return true;
354 } else {
355 // If user is admin or can book, it makes sense that he can view bookings
356 return secure_privilege("mod_roomreservation_admin") ||
357 secure_privilege("mod_roomreservation_book") ||
358 secure_privilege("mod_roomreservation_view");
359 }
360 }
361
362 /**
363 * Get the messages that have been produced
364 * @return string
365 */
366 public function getMessages() {
367 return join("\n", $this->asMessages);
368 }
369
370 /***************************************************************************/
371 /**
372 * @}
373 * @name Operations
374 * @{
375 */
376
377 /**
378 * Write the current state of this instance to the config file.
379 * @throws IOException
380 * @return bool
381 */
382 public function writeConfig() {
383 // Open config file
384 $hFile = fopen("mod_room-reservation/config.inc", "w", true);
385 if(!is_resource($hFile)) {
386 throw new IOException(MOD_ROOM_RESERVATION_ERROR_OPEN_FILE);
387 return false;
388 }
389 // Try to lock file repeatedly
390 for($n = 0; !flock($hFile, LOCK_EX); $n++) {
391 if($n > 10) {
392 throw new IOException(MOD_ROOM_RESERVATION_ERROR_OPEN_FILE);
393 return false; // Give up
394 } else {
395 sleep(0.2); // Retry after 100 ms
396 }
397 }
398
399 // Create text for config file
400 $strFile = MOD_ROOM_RESERVATION_CONFIGFILE_HEADER;
401
402 // Timeslices
403 $strFile .= "\$this->flushTimeslices();\n";
404 foreach($this->getTimeslices() as $oTs) {
405 $strFile .= sprintf("\$this->addTimeslice(new ".
406 "mod_roomReservationTimeslice(%d, %d));\n", $oTs->getBegin(),
407 $oTs->getEnd());
408 }
409
410 // Show weekend
411 $strFile .= sprintf("\$this->setShowWeekend(%s);\n",
412 $this->isShowWeekend() ? "true" : "false");
413
414 // Show lessons
415 $strFile .= sprintf("\$this->setShowLessons(%s);\n",
416 $this->isShowLessons() ? "true" : "false");
417
418 $strFile .= "?>";
419
420 // Write to config file and unlock it
421 if(fwrite($hFile, $strFile) == false) {
422 throw new IOException(MOD_ROOM_RESERVATION_ERROR_WRITE_FILE);
423 return false;
424 }
425 if(!flock($hFile, LOCK_UN)) {
426 throw new IOException(MOD_ROOM_RESERVATION_ERROR_UNLOCK_FILE);
427 return false;
428 }
429
430 rrInsertLog("Konfiguration verändert");
431 return true;
432 }
433
434 /**
435 * Read configuration from file. Returns <tt>false</tt> if an error occured,
436 * in this case getMessages() contains error messages.
437 * @return bool
438 */
439 public function readConfig() {
440 global $g_rmCfg;
441 try {
442 require("mod_room-reservation/config.inc");
443 } catch(Exception $e) {
444 $this->addMessage($e->getMessage());
445 }
446 }
447
448 /** @} */
449 }
450 ?>
This page took 0.062686 seconds and 5 git commands to generate.