ee7c6dfabb5cada7a5b53f3db226e7a0140d9589
[iserv-mod-error-reporter.git] / inc / class_erConfig.inc
1 <?php
2 /**
3 * @file class_erConfig.inc
4 * Class that handles the configuration
5 * @author Roland Hieber (roland.hieber@wilhelm-gym.net)
6 * @date 21.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("sec/secure.inc");
30 require_once("mod_error-reporter/functions.inc");
31
32 /**
33 * Determines if a privilege has been assigned
34 * @param $sPriv (string) Privilege to test
35 * @return bool
36 */
37 function erPrivilegeAssigned($sPriv) {
38 $h = db_query("SELECT act FROM privileges_assign WHERE privilege = $1;",
39 $sPriv);
40 return pg_num_rows($h) > 0;
41 }
42
43 /**
44 * Retrieve all groups that have a privilege assigned
45 * @param $strPriv (string) Privilege to test
46 * @return array
47 */
48 function erPrivilegedGroups($strPriv) {
49 $aReturn = array();
50 $h = db_query("SELECT act FROM privileges_assign WHERE privilege = $1 ".
51 "ORDER BY act;", $strPriv);
52 if(pg_num_rows($h) > 0) {
53 while($a = pg_fetch_array($h)) {
54 $aReturn[] = $a["act"];
55 }
56 }
57 return $aReturn;
58 }
59
60 define("ER_CONFIGFILE_HEADER", "<?php
61 /**
62 * config.inc -- configuration file for iserv-mod-error-reporter
63 * This file is written by the configuration script.
64 */\n");
65
66 /** @todo document */
67 class erConfig {
68 /** (bool) Status of mail notification */
69 protected $bMailNotify;
70 /** (string) Address to send the notification mails to */
71 protected $strMailNotifyAddr;
72
73 /**
74 * @name Constructor
75 * @{
76 */
77 /**
78 * Constructor. Sets default values.
79 * @return erConfig
80 */
81 public function __construct() {
82 $this->setMailNotify(true);
83 $this->setMailNotifyAddr("admins, win-admins");
84 }
85
86 /**
87 * @}
88 * @name Operations
89 * @{
90 */
91
92 /**
93 * Write the current state of this instance to the config file.
94 * @return (bool) If the function fails, it returns <tt>false</tt>. Call
95 * getLastError() to get more information about the error.
96 */
97 public function writeConfig() {
98 // Open config file
99 $hFile = fopen("mod_error-reporter/config.inc", "w", true);
100 if(!is_resource($hFile)) {
101 setLastError(ER_ERROR_OPEN_FILE);
102 return false;
103 }
104 // Try to lock file repeatedly
105 for($n = 0; !flock($hFile, LOCK_EX); $n++) {
106 if($n > 10) {
107 setLastError(ER_ERROR_LOCK_FILE);
108 return false; // Give up
109 } else {
110 sleep(0.1); // Retry after 100 ms
111 }
112 }
113
114 // Create text for config file
115 $strFile = ER_CONFIGFILE_HEADER;
116
117 // Mail notification
118 $strFile .= sprintf("\$cfgErrors->setMailNotify(%s);\n",
119 $this->isMailNotify() ? "true" : "false");
120 $strFile .= sprintf("\$cfgErrors->setMailNotifyAddr('%s');\n",
121 $this->getMailNotifyAddr());
122
123 $strFile .= "?>";
124
125 // Write to config file and unlock it
126 if(fwrite($hFile, $strFile) == false) {
127 setLastError(ER_ERROR_WRITE_FILE);
128 return false;
129 }
130 if(!flock($hFile, LOCK_UN)) {
131 setLastError(ER_ERROR_UNLOCK_FILE);
132 return false;
133 }
134 return true;
135 }
136
137 public function readConfig() {
138 global $cfgErrors;
139 require("error-reporter/config.inc");
140 }
141
142 /**
143 * @}
144 * @name Setting options
145 * @{
146 */
147
148 /**
149 * Enable or disable mail notification
150 * @param $b (bool) <tt>true</tt> to enable mail notification, <tt>false</tt>
151 * to disable it.
152 */
153 public function setMailNotify($b) { $this->bMailNotify = ($b == true); }
154
155 /**
156 * Set the mail address(es) for mail notification
157 * @param $str (string) The adresses to send the notofication mails to,
158 * comma-separated
159 */
160 public function setMailNotifyAddr($str) { $this->strMailNotifyAddr = $str; }
161
162 /**
163 * @}
164 * @name Retrieving options
165 * @{
166 */
167
168 /**
169 * Determine if mail notification is enabled
170 * @return (bool) <tt>true</tt> if mail notification is enabled, otherwise
171 * <tt>false</tt>
172 */
173 public function isMailNotify() { return $this->bMailNotify; }
174
175 /**
176 * Get the mail adress(es) for mail notification
177 * @return string
178 */
179 public function getMailNotifyAddr() { return $this->strMailNotifyAddr; }
180
181 /**
182 * Determine if the current user is allowed to see and report errors. This
183 * function tests if the user is member of a group which has the
184 * <tt>mod-errorreporter_access</tt> privilege.
185 * @return bool
186 */
187 public function userHasAccess() {
188 if($this->userIsAdmin() ||
189 !erPrivilegeAssigned("mod_errorreporter_access")) {
190 // user is admin or privilege is not assigned
191 return true;
192 } else {
193 return secure_privilege("mod_errorreporter_access");
194 }
195 }
196
197 /* legacy */
198 public function userIsAllowed() { return $this->userHasAccess(); }
199
200 /**
201 * Determine if the current user has admin rights. This function tests if
202 * the user is member of a group which has the
203 * <tt>mod-errorreporter_access</tt> privilege
204 * @return bool
205 */
206 public function userIsAdmin() {
207 return secure_privilege("mod_errorreporter_admin");
208 }
209
210 /** @} */
211 }
212 ?>
This page took 0.047537 seconds and 3 git commands to generate.