fixed file system rights for debian/rules (+x)
[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 /**
76 * @}
77 */
78
79 /**
80 * Determine if the specified user exists. Throws an SQLException if an error
81 * occured.
82 * @param $strAct (string) Account name of the user
83 * @throws SQLException
84 * @return (bool)
85 */
86 function isAct($strAct) {
87 $hQuery = db_query("SELECT * FROM users WHERE act = $1;", $strAct);
88 if(!is_resource($hQuery)) {
89 throw new SQLException(MOD_ROOM_RESERVATION_ERROR_SQL);
90 return null;
91 }
92 return (pg_num_rows($hQuery) > 0);
93 }
94
95 /**
96 * Get the real user name for an account name
97 * @param $strAct (string) Account name of the user to look up
98 * @return (string) The real name of the user. If the function fails, it returns <tt>null</tt>.
99 * Call getLastError() to get more information about the error.
100 */
101 function getRealUserName($strAct) {
102 $hQuery = db_query("SELECT firstname, lastname FROM users WHERE act = $1;", $strAct);
103 if(!is_resource($hQuery)) {
104 // TODO throw exception
105 setLastError(RS_ERROR_SQL);
106 return null;
107 }
108 if(pg_num_rows($hQuery) == 0) {
109 return $strAct; // User not found in database, return account name
110 }
111 $arResult = pg_fetch_array($hQuery);
112 return user_join_name($arResult);
113 }
114
115 /**
116 * Determine if a specified group exists
117 * @param $strAct (string) Account name of the group
118 * @return (bool / null) If the function fails, it returns <tt>null</tt>. Call getLastError() to
119 * get more information about the error.
120 */
121 function isGroup($strAct) {
122 $hQuery = db_query("SELECT * FROM groups WHERE act = $1;", $strAct);
123 if(!is_resource($hQuery)) {
124 // TODO throw exception
125 setLastError(RS_ERROR_SQL);
126 return null;
127 }
128 return (pg_num_rows($hQuery) > 0);
129 }
130
131 /**
132 * Look up the name of a group
133 * @param $strAct (string) Account name of the group
134 * @return (string) The name of the group. If the function fails, it returns <tt>null</tt>.
135 * Call getLastError() to get more information about the error.
136 */
137 function getGroupName($strAct) {
138 $hQuery = db_query("SELECT * FROM groups WHERE act = $1;", $strAct);
139 if(!is_resource($hQuery)) {
140 // TODO throw exception
141 setLastError(RS_ERROR_SQL);
142 return null;
143 }
144 if(pg_num_rows($hQuery) == 0) {
145 return $strAct; // Group not found in database, return account name
146 }
147 $arResult = pg_fetch_array($hQuery);
148 return $arResult["name"];
149 }
150
151 /**
152 * Create a link to write a mail to the specified account name.
153 * This function returns a link if the specified account exists, otherwise it returns the
154 * account name.
155 * @param $strAct (string) Account name
156 * @param $strColor (string) Background color for icon()
157 * @param $strParams (string) additional URL parameters
158 * @return string
159 */
160 function mailToUserLink($strAct, $strColor = "bl", $strParams = "") {
161 if(!isAct($strAct)) {
162 return $strAct;
163 }
164 return popup(relroot("msg/write/?to=".user_mail_addr($strAct).$strParams),
165 600, 400, nobr(icon("mail-reply-usr", array("size" => 16, "bg" =>
166 $strColor)) . getRealUserName($strAct)));
167 }
168
169 /**
170 * Determine if a specified string is a valid mail address
171 * @param $strAddr string
172 * @return string
173 */
174 function isMailAddress($strAddr) {
175 return ((preg_match("/([a-zA-Z0-9_\-\.]*(@[a-zA-Z0-9\-\.]*)?(\s*,\s*)?)+/", $strAddr) > 0)
176 and (preg_match("/(\s*,\s*)$/", $strAddr) == 0));
177 }
178
179 /**
180 * Module-specific logging function.
181 * Prefixes the log message with a module-specific string and writes it to the logs.
182 * @param $strLog (string) Log message
183 * @return void
184 */
185 function rrInsertLog($strLog) {
186 log_insert($strLog, null, "Room Reservation Schedule");
187 }
188
189 /**
190 * Get the SQL day number from a given timestamp.
191 * @param $ts (timestamp)
192 * @return (int) 0-6 for Sunday to Monday
193 */
194 function rrDateToSQLDayNumber($ts) {
195 $aDays = array("Sun" => 0, "Mon" => 1, "Tue" => 2,
196 "Wed" => 3, "Thu" => 4, "Fri" => 5, "Sat" => 6);
197 return $aDays[date("D", $ts)];
198 }
199
200 /**
201 * Convert a UNIX timestamp to an SQL date string
202 * @param $ts (timestamp)
203 * @return string
204 */
205 function dateToSql($ts) { return date("Y\-m\-d", intval($ts)); }
206
207 /**
208 * Calculate the timestamp of the monday in the current week
209 * @param $ts (timestamp) Calculate the value relative to this date
210 * @return timestamp
211 */
212 function rrGetMonday($ts = null) {
213 if($ts === null) {
214 $ts = time();
215 }
216 if(date("D", $ts) == "Mon") {
217 // Today is monday
218 return strtotime("00:00", $ts);
219 } else {
220 return strtotime("last monday", $ts);
221 }
222 }
223
224 /** (array of strings) Additional CSS rules */
225 $rsLocalCss = array();
226
227 /**
228 * Add CSS rules to the page
229 * @param $s (string)
230 * @return void
231 */
232 function rrAddCss($s) {
233 rrDebug("rsAddCss: add \"$s\"");
234 $GLOBALS["rsLocalCss"][] = $s;
235 }
236
237 /**
238 * Get CSS rules that have been added with rmAddCss()
239 * @return string
240 */
241 function rrGetCSS() {
242 rrDebug("rsGetCss: Local CSS is ".var_export($GLOBALS["rsLocalCss"], true));
243 return implode("\n", $GLOBALS["rsLocalCss"]);
244 }
245
246 function rrDebug($s, $bReturn = false) {
247 if(isset($_GET["debug"])) {
248 if(!$bReturn) {
249 echo "<!-- $s -->\n";
250 } else {
251 return $s;
252 }
253 }
254 }
255
256 /**
257 * sprintf with support for ordinal numbers.
258 * This version of sprintf replaces all substrings of the type <tt>/\\d+#/</tt>
259 * (i.e. a decimal number with a hash sign appended) in the input string with
260 * the corresponding english ordinal number prefices (st, nd, rd, th).
261 * @param $str (string) Input string
262 * @param $args (mixed) Additional parameters to be passed to sprintf()
263 * @return (string)
264 */
265 function _sprintf_ord($str, $args /*leave this parameters for doxygen*/) {
266 $args = func_get_args();
267 if(preg_match_all("/%[bcdufosxX]/", $args[0], $temp) != func_num_args()-1) {
268 trigger_error("Too few arguments", E_USER_ERROR);
269 return false;
270 }
271 $str = call_user_func_array("sprintf", $args);
272 while(preg_match("/(.*)(\d+)#(.*)/", $str, $m))
273 $str = $m[1]._(append_ord_suffix($m[2])).$m[3];
274 return $str;
275 }
276 ?>
This page took 0.072909 seconds and 5 git commands to generate.