config migration in source code; config page is not needed anymore
[iserv-mod-error-reporter.git] / inc / class_erConfig.inc
index ee7c6df..d980b4c 100644 (file)
@@ -1,22 +1,22 @@
 <?php
 /**
- * @file class_erConfig.inc 
+ * @file class_erConfig.inc
  * Class that handles the configuration
  * @author Roland Hieber (roland.hieber@wilhelm-gym.net)
  * @date 21.10.2007
- * 
+ *
  * Copyright © 2007 Roland Hieber
- * 
+ *
  * Permission is hereby granted, free of charge, to any person obtaining
  * copy of this software and associated documentation files (the "Software"),
  * to deal in the Software without restriction, including without limitation
  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  * and/or sell copies of the Software, and to permit persons to whom the
  * Software is furnished to do so, subject to the following conditions:
- * 
+ *
  * The above copyright notice and this permission notice shall be included in
  * all copies or substantial portions of the Software.
- *  
+ *
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 
 require_once("sec/secure.inc");
 require_once("mod_error-reporter/functions.inc");
-
-/**
- * Determines if a privilege has been assigned
- * @param $sPriv (string) Privilege to test
- * @return bool
- */
-function erPrivilegeAssigned($sPriv) {
-  $h = db_query("SELECT act FROM privileges_assign WHERE privilege = $1;",
-    $sPriv);
-  return pg_num_rows($h) > 0;
-}
-
-/**
- * Retrieve all groups that have a privilege assigned
- * @param $strPriv (string) Privilege to test
- * @return array
- */
-function erPrivilegedGroups($strPriv) {
-  $aReturn = array();
-  $h = db_query("SELECT act FROM privileges_assign WHERE privilege = $1 ".
-    "ORDER BY act;", $strPriv);
-  if(pg_num_rows($h) > 0) {
-    while($a = pg_fetch_array($h)) {
-      $aReturn[] = $a["act"];
-    }
-  }
-  return $aReturn;
-}
-
-define("ER_CONFIGFILE_HEADER", "<?php
-/**
- * config.inc -- configuration file for iserv-mod-error-reporter
- * This file is written by the configuration script.
- */\n");
+require_once("db.inc");
 
 /** @todo document */
 class erConfig {
-  /** (bool) Status of mail notification */
-  protected $bMailNotify;
-  /** (string) Address to send the notification mails to */
-  protected $strMailNotifyAddr;
-  
-  /**
-   * @name Constructor
-   * @{
-   */
+
   /**
-   * Constructor. Sets default values.
-   * @return erConfig
+   * Determine if a privilege has been assigned
+   * @param $sPriv (string) Privilege to test
+   * @return bool
    */
-  public function __construct() {
-    $this->setMailNotify(true);
-    $this->setMailNotifyAddr("admins, win-admins");
+  public static function privilegeAssigned($sPriv) {
+    $h = db_query("SELECT act FROM privileges_assign WHERE privilege = $1;",
+    $sPriv);
+    return pg_num_rows($h) > 0;
   }
-  
-  /**
-   * @}
-   * @name Operations
-   * @{
-   */
-  
+
   /**
-   * Write the current state of this instance to the config file.
-   * @return (bool) If the function fails, it returns <tt>false</tt>. Call
-   *  getLastError() to get more information about the error.
+   * Retrieve all groups that have a privilege assigned
+   * @param $strPriv (string) Privilege to test
+   * @return array
    */
-  public function writeConfig() {
-    // Open config file
-    $hFile = fopen("mod_error-reporter/config.inc", "w", true);
-    if(!is_resource($hFile)) {
-      setLastError(ER_ERROR_OPEN_FILE);
-      return false;
-    }
-    // Try to lock file repeatedly
-    for($n = 0; !flock($hFile, LOCK_EX); $n++) {
-      if($n > 10) {
-        setLastError(ER_ERROR_LOCK_FILE);
-        return false; // Give up
-      } else {
-        sleep(0.1);   // Retry after 100 ms
+  public static function privilegedGroups($strPriv) {
+    $aReturn = array();
+    $h = db_query("SELECT act FROM privileges_assign WHERE privilege = $1 ".
+    "ORDER BY act;", $strPriv);
+    if(pg_num_rows($h) > 0) {
+      while($a = pg_fetch_array($h)) {
+        $aReturn[] = $a["act"];
       }
     }
-    
-    // Create text for config file
-    $strFile = ER_CONFIGFILE_HEADER;
-    
-    // Mail notification
-    $strFile .= sprintf("\$cfgErrors->setMailNotify(%s);\n",
-      $this->isMailNotify() ? "true" : "false");
-    $strFile .= sprintf("\$cfgErrors->setMailNotifyAddr('%s');\n",
-      $this->getMailNotifyAddr());
-    
-    $strFile .= "?>";
-    
-    // Write to config file and unlock it
-    if(fwrite($hFile, $strFile) == false) {
-      setLastError(ER_ERROR_WRITE_FILE);
-      return false;
-    }
-    if(!flock($hFile, LOCK_UN)) {
-      setLastError(ER_ERROR_UNLOCK_FILE);
-      return false;
-    }
-    return true;
+    return $aReturn;
   }
-  
-  public function readConfig() {
-    global $cfgErrors;
-    require("error-reporter/config.inc");
-  }
-  
-  /**
-   * @}
-   * @name Setting options
-   * @{
-   */
-  
-  /**
-   * Enable or disable mail notification
-   * @param $b (bool) <tt>true</tt> to enable mail notification, <tt>false</tt>
-   *  to disable it.
-   */
-  public function setMailNotify($b) { $this->bMailNotify = ($b == true); }
-
-  /**
-   * Set the mail address(es) for mail notification
-   * @param $str (string) The adresses to send the notofication mails to,
-   *  comma-separated
-   */
-  public function setMailNotifyAddr($str) { $this->strMailNotifyAddr = $str; }
-
-  /**
-   * @}
-   * @name Retrieving options
-   * @{
-   */
-  
-  /**
-   * Determine if mail notification is enabled
-   * @return (bool) <tt>true</tt> if mail notification is enabled, otherwise
-   *  <tt>false</tt>
-   */
-  public function isMailNotify() { return $this->bMailNotify; }
-
-  /**
-   * Get the mail adress(es) for mail notification
-   * @return string
-   */
-  public function getMailNotifyAddr() { return $this->strMailNotifyAddr; }
 
   /**
    * Determine if the current user is allowed to see and report errors. This
    * function tests if the user is member of a group which has the
    * <tt>mod-errorreporter_access</tt> privilege.
-   * @return bool 
+   * @return bool
    */
-  public function userHasAccess() {
-    if($this->userIsAdmin() || 
-      !erPrivilegeAssigned("mod_errorreporter_access")) {
+  public static function userHasAccess() {
+    if(erConfig::userIsAdmin() ||
+    !erConfig::privilegeAssigned("mod_errorreporter_access")) {
       // user is admin or privilege is not assigned
       return true;
     } else {
       return secure_privilege("mod_errorreporter_access");
     }
   }
-  
-  /* legacy */
-  public function userIsAllowed() { return $this->userHasAccess(); }
-  
+
   /**
-   * Determine if the current user has admin rights. This function tests if 
-   * the user is member of a group which has the 
+   * Determine if the current user has admin rights. This function tests if
+   * the user is member of a group which has the
    * <tt>mod-errorreporter_access</tt> privilege
-   * @return bool 
+   * @return bool
    */
-  public function userIsAdmin() {
+  public static function userIsAdmin() {
     return secure_privilege("mod_errorreporter_admin");
   }
-  
+
   /** @} */
 }
 ?>
This page took 0.030539 seconds and 4 git commands to generate.