c18362307b26b984cc1174c6c4aff308f8bf6840
[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 getLastError() to get
95 * 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", $this->getMailNotifyAddr());
121
122 $strFile .= "?>";
123
124 // Write to config file and unlock it
125 if(fwrite($hFile, $strFile) == false) {
126 setLastError(ER_ERROR_WRITE_FILE);
127 return false;
128 }
129 if(!flock($hFile, LOCK_UN)) {
130 setLastError(ER_ERROR_UNLOCK_FILE);
131 return false;
132 }
133 return true;
134 }
135
136 public function readConfig() {
137 global $cfgErrors;
138 require("error-reporter/config.inc");
139 }
140
141 /**
142 * @}
143 * @name Setting options
144 * @{
145 */
146
147 /**
148 * Enable or disable mail notification
149 * @param $b (bool) <tt>true</tt> to enable mail notification, <tt>false</tt> to disable it.
150 */
151 public function setMailNotify($b) { $this->bMailNotify = ($b == true); }
152
153 /**
154 * Set the mail address(es) for mail notification
155 * @param $str (string) The adresses to send the notofication mails to, comma-separated
156 */
157 public function setMailNotifyAddr($str) { $this->strMailNotifyAddr = $str; }
158
159 /**
160 * @}
161 * @name Retrieving options
162 * @{
163 */
164
165 /**
166 * Determine if mail notification is enabled
167 * @return (bool) <tt>true</tt> if mail notification is enabled, otherwise <tt>false</tt>
168 */
169 public function isMailNotify() { return $this->bMailNotify; }
170
171 /**
172 * Get the mail adress(es) for mail notification
173 * @return string
174 */
175 public function getMailNotifyAddr() { return $this->strMailNotifyAddr; }
176
177 /**
178 * Determine if the current user is allowed to see and report errors. This
179 * function tests if the user is member of a group which has the
180 * <tt>mod-errorreporter_access</tt> privilege.
181 * @return bool
182 */
183 public function userHasAccess() {
184 if($this->userIsAdmin() ||
185 !erPrivilegeAssigned("mod_errorreporter_access")) {
186 // user is admin or privilege is not assigned
187 return true;
188 } else {
189 return secure_privilege("mod_errorreporter_access");
190 }
191 }
192
193 /* legacy */
194 public function userIsAllowed() { return $this->userHasAccess(); }
195
196 /**
197 * Determine if the current user has admin rights. This function tests if
198 * the user is member of a group which has the
199 * <tt>mod-errorreporter_access</tt> privilege
200 * @return bool
201 */
202 public function userIsAdmin() {
203 return secure_privilege("mod_errorreporter_admin");
204 }
205
206 /** @} */
207 }
208 ?>
This page took 0.055074 seconds and 3 git commands to generate.