363ce5aa33898ad1c4c6bd9136087f119a145ae3
[iserv-mod-error-reporter.git] / inc / functions.inc
1 <?php
2 /**
3 * functions.php
4 * Additional functions for iserv-moderror-reporter
5 * @author Roland Hieber (roland.hieber@wilhelm-gym.net)
6 * @date 20.10.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 /**
31 * @page error-reporter_errorcodes Error Codes
32 * @{
33 */
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"));
46 /**
47 * @}
48 */
49
50 static $ercLastError = null;
51 /**
52 * @todo use exceptions
53 * Set the last error.
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.
57 */
58 function setLastError($cError) { $GLOBALS["ercLastError"] = $cError; }
59 /**
60 * Get the error that occured last.
61 * @return (constant) Error code, see @ref error-reporter_errorcodes for a list of constants.
62 */
63 function getLastError() { return $GLOBALS["ercLastError"]; }
64
65 /**
66 * Print the error that occured last
67 * @return void
68 */
69 function printLastError() {
70 echo sprintf("<p class='err'>%s %s</p>", _c("error-reporter:An error occured:"), getLastError());
71 }
72
73 /**
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>.
77 * @throws Exception
78 */
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);
83 return null;
84 }
85 return (pg_num_rows($hQuery) > 0);
86 }
87
88 /**
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>.
92 * @throws Exception
93 */
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);
98 return null;
99 }
100 if(pg_num_rows($hQuery) == 0) {
101 return $strAct; // User not found in database, return account name
102 }
103 $arResult = pg_fetch_array($hQuery);
104 return user_join_name($arResult);
105 }
106
107 /**
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>.
111 * @throws Exception
112 */
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);
117 return null;
118 }
119 return (pg_num_rows($hQuery) > 0);
120 }
121
122 /**
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>.
126 * @throws Exception
127 */
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);
132 return null;
133 }
134 if(pg_num_rows($hQuery) == 0) {
135 return $strAct; // Group not found in database, return account name
136 }
137 $arResult = pg_fetch_array($hQuery);
138 return $arResult["name"];
139 }
140
141 /**
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
144 * account name.
145 * @param $strAct (string) Account name
146 * @return string
147 */
148 function erMailToUserLink($strAct) {
149 if(!erIsAct($strAct)) {
150 return $strAct;
151 }
152 return popup(relroot("msg/write/?to=".user_mail_addr($strAct)),
153 600, 400, erGetRealUserName($strAct));
154 }
155
156 /**
157 * Determine if a specified string is a valid mail address
158 * @param $strAddr string
159 * @return string
160 */
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));
164 }
165
166 function erInsertLog($sMsg) {
167 log_insert($sMsg, null, "Error Report Wizard");
168 }
169 ?>
This page took 0.047563 seconds and 3 git commands to generate.