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