Disable access to the administrator plugin when plugin is disabled in config.
[squirrelmail.git] / functions / abook_local_file.php
index a841c03562b26e601b139cc5a0bf1d1c517b80f0..6dca53eb0a99e07315997449e8c6461e60aac156 100644 (file)
@@ -2,7 +2,7 @@
 /**
  * abook_local_file.php
  *
- * Copyright (c) 1999-2004 The SquirrelMail Project Team
+ * Copyright (c) 1999-2005 The SquirrelMail Project Team
  * Licensed under the GNU GPL. For full terms see the file COPYING.
  *
  * @version $Id$
  *   filename  => path to addressbook file
  * ? create    => if true: file is created if it does not exist.
  * ? umask     => umask set before opening file.
- * ? name      => name of address book
+ * ? name      => name of address book.
+ * ? detect_writeable => detect address book access permissions by
+ *                checking file permissions.
+ * ? writeable => allow writing into address book. Used only when
+ *                detect_writeable is set to false.
+ * ? listing   => enable/disable listing
  *</pre>
  * NOTE. This class should not be used directly. Use the
  *       "AddressBook" class instead.
  * @package squirrelmail
  */
 class abook_local_file extends addressbook_backend {
-    /** @var string backend type */
+    /**
+     * Backend type
+     * @var string
+     */
     var $btype = 'local';
-    /** @var string backend name */
+    /**
+     * Backend name
+     * @var string
+     */
     var $bname = 'local_file';
 
-    /** @var string file used to store data */
-    var $filename   = '';
-    /** @var object file handle */
+    /**
+     * File used to store data
+     * @var string
+     */
+    var $filename = '';
+    /**
+     * File handle
+     * @var object
+     */
     var $filehandle = 0;
-    /** @var bool create file if it is not present */
-    var $create     = false;
-    /** @var string umask of the file */
+    /**
+     * Create file, if it not present
+     * @var bool
+     */
+    var $create = false;
+    /**
+     * Detect, if address book is writeable by checking file permisions
+     * @var bool
+     */
+    var $detect_writeable   = true;
+    /**
+     * Control write access to address book
+     *
+     * Option does not have any effect, if 'detect_writeable' is 'true'
+     * @var bool
+     */
+    var $writeable = false;
+    /**
+     * controls listing of address book
+     * @var bool
+     */
+    var $listing = true;
+    /**
+     * Umask of the file
+     * @var string
+     */
     var $umask;
 
     /* ========================== Private ======================= */
@@ -70,9 +110,18 @@ class abook_local_file extends addressbook_backend {
             if(isset($param['umask'])) {
                 $this->umask = $param['umask'];
             }
-            if(!empty($param['name'])) {
+            if(isset($param['name'])) {
                 $this->sname = $param['name'];
             }
+            if(isset($param['detect_writeable'])) {
+                $this->detect_writeable = $param['detect_writeable'];
+            }
+            if(!empty($param['writeable'])) {
+                $this->writeable = $param['writeable'];
+            }
+            if(isset($param['listing'])) {
+                $this->listing = $param['listing'];
+            }
 
             $this->open(true);
         } else {
@@ -92,6 +141,7 @@ class abook_local_file extends addressbook_backend {
         $this->error = '';
         $file   = $this->filename;
         $create = $this->create;
+        $fopenmode = (($this->writeable && is_writable($file)) ? 'a+' : 'r');
 
         /* Return true is file is open and $new is unset */
         if($this->filehandle && !$new) {
@@ -106,22 +156,32 @@ class abook_local_file extends addressbook_backend {
         /* Close old file, if any */
         if($this->filehandle) { $this->close(); }
 
-        /* Open file. First try to open for reading and writing,
-         * but fall back to read only. */
         umask($this->umask);
-        $fh = @fopen($file, 'a+');
-        if($fh) {
-            $this->filehandle = &$fh;
-            $this->filename   = $file;
-            $this->writeable  = true;
+        if (! $this->detect_writeable) {
+            $fh = @fopen($file,$fopenmode);
+            if ($fh) {
+                $this->filehandle = &$fh;
+                $this->filename = $file;
+            } else {
+                return $this->set_error("$file: " . _("Open failed"));
+            }
         } else {
-            $fh = @fopen($file, 'r');
+            /* Open file. First try to open for reading and writing,
+             * but fall back to read only. */
+            $fh = @fopen($file, 'a+');
             if($fh) {
                 $this->filehandle = &$fh;
                 $this->filename   = $file;
-                $this->writeable  = false;
+                $this->writeable  = true;
             } else {
-                return $this->set_error("$file: " . _("Open failed"));
+                $fh = @fopen($file, 'r');
+                if($fh) {
+                    $this->filehandle = &$fh;
+                    $this->filename   = $file;
+                    $this->writeable  = false;
+                } else {
+                    return $this->set_error("$file: " . _("Open failed"));
+                }
             }
         }
         return true;
@@ -264,6 +324,11 @@ class abook_local_file extends addressbook_backend {
      */
     function list_addr() {
         $res = array();
+
+        if(isset($this->listing) && !$this->listing) {
+            return array();
+        }
+
         $this->open();
         @rewind($this->filehandle);
 
@@ -292,8 +357,8 @@ class abook_local_file extends addressbook_backend {
         /* See if user exists already */
         $ret = $this->lookup($userdata['nickname']);
         if(!empty($ret)) {
-            return $this->set_error(sprintf(_("User '%s' already exist"),
-                   $ret['nickname']));
+            return $this->set_error(sprintf(_("User %s already exists"),
+                        '&quot;' . $ret['nickname'] . '&quot;'));
         }
 
         /* Here is the data to write */
@@ -385,8 +450,8 @@ class abook_local_file extends addressbook_backend {
         /* See if user exists */
         $ret = $this->lookup($alias);
         if(empty($ret)) {
-            return $this->set_error(sprintf(_("User '%s' does not exist"),
-                $alias));
+            return $this->set_error(sprintf(_("User %s does not exist"),
+                        '&quot;' . $alias . '&quot;'));
         }
 
         /* Lock the file to make sure we're the only process working