4 * Additional functions for iserv-moderror-reporter
5 * @author Roland Hieber (roland.hieber@wilhelm-gym.net)
8 * Copyright © 2007 Roland Hieber
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:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
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
29 require_once("share.inc");
31 * @page error-reporter_errorcodes Error Codes
34 /** Access denied. This can be due to missing access rights. */
35 define("ER_ERROR_ACCESS_DENIED", _c("error-reporter:Access denied"));
36 /** Error while querying the database, maybe due to a lost connection to the server */
37 define("ER_ERROR_SQL", _c("error-reporter:Error while trying to query the database"));
38 /** Error while trying to open a file */
39 define("ER_ERROR_OPEN_FILE", _c("error-reporter:Could not open file"));
40 /** Error while trying to write a file */
41 define("ER_ERROR_WRITE_FILE", _c("error-reporter:Could not write to file"));
42 /** Error while trying to lock a file */
43 define("ER_ERROR_LOCK_FILE", _c("error-reporter:Could not lock file"));
44 /** Error while trying to unlock a file */
45 define("ER_ERROR_UNLOCK_FILE", _c("error-reporter:Could not unlock file"));
50 static $ercLastError = null;
52 * @todo use exceptions
54 * Call this function to set the error that occured last.
55 * @param $cError (constant) Error code, see @ref error-reporter_errorcodes
56 * for a list of constants.
58 function setLastError($cError) { $GLOBALS["ercLastError"] = $cError; }
60 * Get the error that occured last.
61 * @return (constant) Error code, see @ref error-reporter_errorcodes for a list of constants.
63 function getLastError() { return $GLOBALS["ercLastError"]; }
66 * Print the error that occured last
69 function printLastError() {
70 echo sprintf("<p class='err'>%s %s</p>", _c("error-reporter:An error occured:"), getLastError());
74 * Determine if the specified user exists
75 * @param $strAct (string) Account name of the user
76 * @return (bool / null) If the function fails, it returns <tt>null</tt>.
79 function erIsAct($strAct) {
80 $hQuery = db_query("SELECT * FROM users WHERE act = $1;", $strAct);
81 if(!is_resource($hQuery)) {
82 throw new Exception(ER_ERROR_SQL);
85 return (pg_num_rows($hQuery) > 0);
89 * Get the real user name for an account name
90 * @param $strAct (string) Account name of the user to look up
91 * @return (string) The real name of the user. If the function fails, it returns <tt>null</tt>.
94 function erGetRealUserName($strAct) {
95 $hQuery = db_query("SELECT firstname, lastname FROM users WHERE act = $1;", $strAct);
96 if(!is_resource($hQuery)) {
97 throw new Exception(ER_ERROR_SQL);
100 if(pg_num_rows($hQuery) == 0) {
101 return $strAct; // User not found in database, return account name
103 $arResult = pg_fetch_array($hQuery);
104 return user_join_name($arResult);
108 * Determine if a specified group exists
109 * @param $strAct (string) Account name of the group
110 * @return (bool / null) If the function fails, it returns <tt>null</tt>.
113 function erIsGroup($strAct) {
114 $hQuery = db_query(sprintf("SELECT * FROM groups WHERE act=%s;", qdb($strAct)));
115 if(!is_resource($hQuery)) {
116 throw new Exception(ER_ERROR_SQL);
119 return (pg_num_rows($hQuery) > 0);
123 * Look up the name of a group
124 * @param $strAct (string) Account name of the group
125 * @return (string) The name of the group. If the function fails, it returns <tt>null</tt>.
128 function erGetGroupName($strAct) {
129 $hQuery = db_query(sprintf("SELECT * FROM groups WHERE act=%s;", qdb($strAct)));
130 if(!is_resource($hQuery)) {
131 throw new Exception(ER_ERROR_SQL);
134 if(pg_num_rows($hQuery) == 0) {
135 return $strAct; // Group not found in database, return account name
137 $arResult = pg_fetch_array($hQuery);
138 return $arResult["name"];
142 * Create a link to write a mail to the specified account name.
143 * This function returns a link if the specified account exists, otherwise it returns the
145 * @param $strAct (string) Account name
148 function erMailToUserLink($strAct) {
149 if(!erIsAct($strAct)) {
152 return popup(relroot("msg/write/?to=".user_mail_addr($strAct)),
153 600, 400, erGetRealUserName($strAct));
157 * Determine if a specified string is a valid mail address
158 * @param $strAddr string
161 function erIsMailAddress($strAddr) {
162 return ((preg_match("/([a-zA-Z0-9_\-\.]*(@[a-zA-Z0-9\-\.]*)?(\s*,\s*)?)+/", $strAddr) > 0)
163 and (preg_match("/(\s*,\s*)$/", $strAddr) == 0));
166 function erInsertLog($sMsg) {
167 log_insert($sMsg, null, "Error Report Wizard");