moved db_user calls from includes to actual script, buggy when included from navigation
[iserv-mod-room-reservation.git] / inc / functions.inc
1 <?php
2 /**
3 * @file functions.inc
4 * additional functions for iserv-mod-room-reservation
5 * @author Roland Hieber (roland.hieber@wilhelm-gym.net)
6 * @date 22.12.2007
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("share.inc");
30 require_once("db.inc");
31 require_once("user.inc");
32 require_once("sec/secure.inc");
33
34 /**
35 * @page errorcodes Error Codes
36 * @{
37 */
38 /** Access denied. This can be due to missing access rights. */
39 define("MOD_ROOM_RESERVATION_ERROR_ACCESS_DENIED",
40 _c("room-reservation:Access denied."));
41 /** Error while querying the database */
42 define("MOD_ROOM_RESERVATION_ERROR_SQL",
43 _c("room-reservation:Error while trying to query the database."));
44 /** Error while trying to open a file */
45 define("MOD_ROOM_RESERVATION_ERROR_OPEN_FILE",
46 _c("room-reservation:Could not open file."));
47 /** Error while trying to write a file */
48 define("MOD_ROOM_RESERVATION_ERROR_WRITE_FILE",
49 _c("room-reservation:Could not write to file."));
50 /** Error while trying to lock a file */
51 define("MOD_ROOM_RESERVATION_ERROR_LOCK_FILE",
52 _c("room-reservation:Could not lock file."));
53 /** Error while trying to unlock a file */
54 define("MOD_ROOM_RESERVATION_ERROR_UNLOCK_FILE",
55 _c("room-reservation:Could not unlock file."));
56 /** The current booking overlaps with an existing booking */
57 define("MOD_ROOM_RESERVATION_ERROR_BOOKING_OVERLAPS", _c("room-reservation:".
58 "The current booking overlaps with an existing booking."));
59 /** The booking ends before it begins */
60 define("MOD_ROOM_RESERVATION_ERROR_END_BEFORE_BEGIN", _c("room-reservation:".
61 "The ending of your booking cannot lie before the beginning."));
62 define("MOD_ROOM_RESERVATION_ERROR_NO_REASON", _c("room-reservation:Please ".
63 "give a reason."));
64 /** A new timeslice overlaps with an existing timeslice */
65 define("MOD_ROOM_RESERVATION_ERROR_CONFIG_OVERLAPPING_TIMESLICE",
66 _c("room-reservation:The period overlaps with an existing one."));
67 /** There is no such timeslice */
68 define("MOD_ROOM_RESERVATION_ERROR_CONFIG_NO_SUCH_TIMESLICE",
69 _c("room-reservation:The specified period does not exist."));
70 /** There is no such account */
71 define("MOD_ROOM_RESERVATION_ERROR_NO_SUCH_ACCOUNT", _c("room-reservation:".
72 "The specified account does not exist."));
73 /** The room is not available for booking */
74 define("MOD_ROOM_RESERVATION_ERROR_ROOM_NOT_WHITELISTED",
75 _c("room-reservation:This room is not available for booking."));
76 /**
77 * @}
78 */
79
80 /**
81 * Determine if the specified user exists. Throws an SQLException if an error
82 * occured.
83 * @param $strAct (string) Account name of the user
84 * @throws SQLException
85 * @return (bool)
86 */
87 function isAct($strAct) {
88 $hQuery = db_query("SELECT * FROM users WHERE act = $1;", $strAct);
89 if(!is_resource($hQuery)) {
90 throw new SQLException(MOD_ROOM_RESERVATION_ERROR_SQL);
91 return null;
92 }
93 return (pg_num_rows($hQuery) > 0);
94 }
95
96 /**
97 * Get the real user name for an account name. If the function fails, it throws
98 * an Exception.
99 * @param $strAct (string) Account name of the user to look up
100 * @return (string) The real name of the user.
101 * @throws Exception
102 */
103 function getRealUserName($strAct) {
104 $hQuery = db_query("SELECT firstname, lastname FROM users WHERE act = $1;",
105 $strAct);
106 if(!is_resource($hQuery)) {
107 throw new Exception(RS_ERROR_SQL);
108 }
109 if(pg_num_rows($hQuery) == 0) {
110 return $strAct; // User not found in database, return account name
111 }
112 $arResult = pg_fetch_array($hQuery);
113 return user_join_name($arResult);
114 }
115
116 /**
117 * Determine if a specified group exists. If the function fails, it throws an
118 * Exception.
119 * @param $strAct (string) Account name of the group
120 * @return bool
121 * @throws Exception
122 */
123 function isGroup($strAct) {
124 $hQuery = db_query("SELECT * FROM groups WHERE act = $1;", $strAct);
125 if(!is_resource($hQuery)) {
126 throw new Exception(RS_ERROR_SQL);
127 }
128 return (pg_num_rows($hQuery) > 0);
129 }
130
131 /**
132 * Look up the name of a group. If the function fails, it throws an Exception.
133 * @param $strAct (string) Account name of the group
134 * @return (string) The name of the group.
135 */
136 function getGroupName($strAct) {
137 $hQuery = db_query("SELECT * FROM groups WHERE act = $1;", $strAct);
138 if(!is_resource($hQuery)) {
139 throw new Exception(RS_ERROR_SQL);
140 }
141 if(pg_num_rows($hQuery) == 0) {
142 return $strAct; // Group not found in database, return account name
143 }
144 $arResult = pg_fetch_array($hQuery);
145 return $arResult["name"];
146 }
147
148 /**
149 * Create a link to write a mail to the specified account name. Returns a link
150 * if the specified account exists, otherwise it returns the account name.
151 * @param $strAct (string) Account name
152 * @param $strColor (string) Background color for icon()
153 * @param $strParams (string) additional URL parameters
154 * @return string
155 */
156 function mailToUserLink($strAct, $strColor = "bl", $strParams = "") {
157 if(!isAct($strAct)) {
158 return $strAct;
159 }
160 return popup(relroot("msg/write/?to=".user_mail_addr($strAct).$strParams),
161 600, 400, nobr(icon("mail-reply-usr", array("size" => 16, "bg" =>
162 $strColor)) . getRealUserName($strAct)));
163 }
164
165 /**
166 * Determine if a specified string is a valid mail address
167 * @param $strAddr string
168 * @return string
169 */
170 function isMailAddress($strAddr) {
171 return ((preg_match("/([a-zA-Z0-9_\-\.]*(@[a-zA-Z0-9\-\.]*)?(\s*,\s*)?)+/",
172 $strAddr) > 0) && (preg_match("/(\s*,\s*)$/", $strAddr) == 0));
173 }
174
175 /**
176 * Module-specific logging function. Prefixes the log message with a
177 * module-specific string and writes it to the logs.
178 * @param $strLog (string) Log message
179 * @return void
180 */
181 function rrInsertLog($strLog) {
182 log_insert($strLog, null, "Room Reservation Schedule");
183 }
184
185 /**
186 * Get the SQL day number from a given timestamp.
187 * @param $ts (timestamp)
188 * @return (int) 0-6 for Sunday to Monday
189 */
190 function rrDateToSQLDayNumber($ts) {
191 $aDays = array("Sun" => 0, "Mon" => 1, "Tue" => 2,
192 "Wed" => 3, "Thu" => 4, "Fri" => 5, "Sat" => 6);
193 return $aDays[date("D", $ts)];
194 }
195
196 /**
197 * Convert a UNIX timestamp to an SQL date string
198 * @param $ts (timestamp)
199 * @return string
200 */
201 function dateToSql($ts) { return date("Y\-m\-d", intval($ts)); }
202
203 /**
204 * Calculate the timestamp of the monday in the current week
205 * @param $ts (timestamp) Calculate the value relative to this date
206 * @return timestamp
207 */
208 function rrGetMonday($ts = null) {
209 if($ts === null) {
210 $ts = time();
211 }
212 if(date("D", $ts) == "Mon") {
213 // Today is monday
214 return strtotime("00:00", $ts);
215 } else {
216 return strtotime("last monday", $ts);
217 }
218 }
219
220 /** (array of strings) Additional CSS rules */
221 $GLOBALS["rrLocalCss"] = array();
222
223 /**
224 * Add CSS rules to the page
225 * @param $s (string)
226 * @return void
227 */
228 function rrAddCss($s) {
229 rrDebug("rrAddCss: add \"$s\"");
230 $GLOBALS["rrLocalCss"][] = $s;
231 }
232
233 /**
234 * Get CSS rules that have been added with rrAddCss()
235 * @return string
236 */
237 function rrGetCss() {
238 rrDebug("rrGetCss: Local CSS is ".var_export($GLOBALS["rrLocalCss"], true));
239 return implode("\n", $GLOBALS["rrLocalCss"]);
240 }
241
242 function rrDebug($s, $bReturn = false) {
243 if(isset($_GET["debug"])) {
244 if(!$bReturn) {
245 echo "<!-- $s -->\n";
246 } else {
247 return $s;
248 }
249 }
250 }
251
252 /**
253 * sprintf with support for ordinal numbers.
254 * This version of sprintf replaces all substrings of the type <tt>/\\d+#/</tt>
255 * (i.e. a decimal number with a hash sign appended) in the input string with
256 * the corresponding english ordinal number prefices (st, nd, rd, th).
257 * @param $str (string) Input string
258 * @param $args (mixed) Additional parameters to be passed to sprintf()
259 * @return (string)
260 */
261 function _sprintf_ord($str, $args /*leave this parameters for doxygen*/) {
262 $args = func_get_args();
263 if(preg_match_all("/%[bcdufosxX]/", $args[0], $temp) != func_num_args()-1) {
264 trigger_error("Too few arguments", E_USER_ERROR);
265 return false;
266 }
267 $str = call_user_func_array("sprintf", $args);
268 while(preg_match("/(.*)(\d+)#(.*)/", $str, $m))
269 $str = $m[1]._(append_ord_suffix($m[2])).$m[3];
270 return $str;
271 }
272 ?>
This page took 0.064492 seconds and 5 git commands to generate.