3 * @file class_erConfig.inc
4 * Class that handles the configuration
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("sec/secure.inc");
30 require_once("mod_error-reporter/functions.inc");
33 * Determines if a privilege has been assigned
34 * @param $sPriv (string) Privilege to test
37 function erPrivilegeAssigned($sPriv) {
38 $h = db_query("SELECT act FROM privileges_assign WHERE privilege = $1;",
40 return pg_num_rows($h) > 0;
44 * Retrieve all groups that have a privilege assigned
45 * @param $strPriv (string) Privilege to test
48 function erPrivilegedGroups($strPriv) {
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"];
60 define("ER_CONFIGFILE_HEADER", "<?php
62 * config.inc -- configuration file for iserv-mod-error-reporter
63 * This file is written by the configuration script.
68 /** (bool) Status of mail notification */
69 protected $bMailNotify;
70 /** (string) Address to send the notification mails to */
71 protected $strMailNotifyAddr;
78 * Constructor. Sets default values.
81 public function __construct() {
82 $this->setMailNotify(true);
83 $this->setMailNotifyAddr("admins, win-admins");
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.
97 public function writeConfig() {
99 $hFile = fopen("mod_error-reporter/config.inc", "w", true);
100 if(!is_resource($hFile)) {
101 setLastError(ER_ERROR_OPEN_FILE);
104 // Try to lock file repeatedly
105 for($n = 0; !flock($hFile, LOCK_EX); $n++) {
107 setLastError(ER_ERROR_LOCK_FILE);
108 return false; // Give up
110 sleep(0.1); // Retry after 100 ms
114 // Create text for config file
115 $strFile = ER_CONFIGFILE_HEADER;
118 $strFile .= sprintf("\$cfgErrors->setMailNotify(%s);\n",
119 $this->isMailNotify() ? "true" : "false");
120 $strFile .= sprintf("\$cfgErrors->setMailNotifyAddr('%s');\n",
121 $this->getMailNotifyAddr());
125 // Write to config file and unlock it
126 if(fwrite($hFile, $strFile) == false) {
127 setLastError(ER_ERROR_WRITE_FILE);
130 if(!flock($hFile, LOCK_UN)) {
131 setLastError(ER_ERROR_UNLOCK_FILE);
137 public function readConfig() {
139 require("error-reporter/config.inc");
144 * @name Setting options
149 * Enable or disable mail notification
150 * @param $b (bool) <tt>true</tt> to enable mail notification, <tt>false</tt>
153 public function setMailNotify($b) { $this->bMailNotify = ($b == true); }
156 * Set the mail address(es) for mail notification
157 * @param $str (string) The adresses to send the notofication mails to,
160 public function setMailNotifyAddr($str) { $this->strMailNotifyAddr = $str; }
164 * @name Retrieving options
169 * Determine if mail notification is enabled
170 * @return (bool) <tt>true</tt> if mail notification is enabled, otherwise
173 public function isMailNotify() { return $this->bMailNotify; }
176 * Get the mail adress(es) for mail notification
179 public function getMailNotifyAddr() { return $this->strMailNotifyAddr; }
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.
187 public function userHasAccess() {
188 if($this->userIsAdmin() ||
189 !erPrivilegeAssigned("mod_errorreporter_access")) {
190 // user is admin or privilege is not assigned
193 return secure_privilege("mod_errorreporter_access");
198 public function userIsAllowed() { return $this->userHasAccess(); }
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
206 public function userIsAdmin() {
207 return secure_privilege("mod_errorreporter_admin");