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