From a9b6a8b59a0be9ff45f5675c3b032ad8d2afd6eb Mon Sep 17 00:00:00 2001
From: Andreas Gohr <andi@splitbrain.org>
Date: Sat, 16 Feb 2013 14:37:12 +0100
Subject: [PATCH] allow richer email addresses in notify and registernotify
 FS#2689

This deprecates the "richemail" config class
---
 lib/plugins/config/settings/config.class.php  | 85 +++++++++----------
 .../config/settings/config.metadata.php       |  8 +-
 2 files changed, 41 insertions(+), 52 deletions(-)

diff --git a/lib/plugins/config/settings/config.class.php b/lib/plugins/config/settings/config.class.php
index 16be9a689..5b81be782 100644
--- a/lib/plugins/config/settings/config.class.php
+++ b/lib/plugins/config/settings/config.class.php
@@ -79,7 +79,7 @@ if (!class_exists('configuration')) {
               array_shift($param);
             } else {
               $class = 'setting_undefined';
-              $param = NULL;
+              $param = null;
             }
 
             if (!in_array($class, $no_default_check) && !isset($default[$key])) {
@@ -353,13 +353,13 @@ if (!class_exists('setting')) {
   class setting {
 
     var $_key = '';
-    var $_default = NULL;
-    var $_local = NULL;
-    var $_protected = NULL;
+    var $_default = null;
+    var $_local = null;
+    var $_protected = null;
 
     var $_pattern = '';
     var $_error = false;            // only used by those classes which error check
-    var $_input = NULL;             // only used by those classes which error check
+    var $_input = null;             // only used by those classes which error check
 
     var $_cautionList = array(
         'basedir' => 'danger', 'baseurl' => 'danger', 'savedir' => 'danger', 'cookiedir' => 'danger', 'useacl' => 'danger', 'authtype' => 'danger', 'superuser' => 'danger', 'userewrite' => 'danger',
@@ -367,7 +367,7 @@ if (!class_exists('setting')) {
         'allowdebug' => 'security', 'htmlok' => 'security', 'phpok' => 'security', 'iexssprotect' => 'security', 'xmlrpc' => 'security', 'fullpath' => 'security'
     );
 
-    function setting($key, $params=NULL) {
+    function setting($key, $params=null) {
         $this->_key = $key;
 
         if (is_array($params)) {
@@ -656,6 +656,7 @@ if (!class_exists('setting_email')) {
   class setting_email extends setting_string {
     var $_pattern = SETTING_EMAIL_PATTERN;       // no longer required, retained for backward compatibility - FIXME, may not be necessary
     var $_multiple = false;
+    var $_placeholders = false;
 
     /**
      *  update setting with user provided value $input
@@ -669,15 +670,36 @@ if (!class_exists('setting_email')) {
 
         $value = is_null($this->_local) ? $this->_default : $this->_local;
         if ($value == $input) return false;
+        if($input === ''){
+            $this->_local = $input;
+            return true;
+        }
+        $mail = $input;
+
+        if($this->_placeholders){
+            // replace variables with pseudo values
+            $mail = str_replace('@USER@','joe',$mail);
+            $mail = str_replace('@NAME@','Joe Schmoe',$mail);
+            $mail = str_replace('@MAIL@','joe@example.com',$mail);
+        }
 
+        // multiple mail addresses?
         if ($this->_multiple) {
-            $mails = array_filter(array_map('trim', explode(',', $input)));
+            $mails = array_filter(array_map('trim', explode(',', $mail)));
         } else {
-            $mails = array($input);
+            $mails = array($mail);
         }
 
+        // check them all
         foreach ($mails as $mail) {
-            if (!mail_isvalid($mail)) {
+            // only check the address part
+            if(preg_match('#(.*?)<(.*?)>#', $mail, $matches)){
+                $addr = $matches[2];
+            }else{
+                $addr = $mail;
+            }
+
+            if (!mail_isvalid($addr)) {
               $this->_error = true;
               $this->_input = $input;
               return false;
@@ -690,46 +712,15 @@ if (!class_exists('setting_email')) {
   }
 }
 
+/**
+ * @deprecated 2013-02-16
+ */
 if (!class_exists('setting_richemail')) {
   class setting_richemail extends setting_email {
-
-    /**
-     *  update setting with user provided value $input
-     *  if value fails error check, save it
-     *
-     *  @return boolean true if changed, false otherwise (incl. on error)
-     */
-    function update($input) {
-        if (is_null($input)) return false;
-        if ($this->is_protected()) return false;
-
-        $value = is_null($this->_local) ? $this->_default : $this->_local;
-        if ($value == $input) return false;
-
-        // replace variables with pseudo values
-        $test = $input;
-        $test = str_replace('@USER@','joe',$test);
-        $test = str_replace('@NAME@','Joe Schmoe',$test);
-        $test = str_replace('@MAIL@','joe@example.com',$test);
-
-        // now only check the address part
-        if(preg_match('#(.*?)<(.*?)>#',$test,$matches)){
-          $text = trim($matches[1]);
-          $addr = $matches[2];
-        }else{
-          $addr = $test;
-        }
-
-        if ($test !== '' && !mail_isvalid($addr)) {
-          $this->_error = true;
-          $this->_input = $input;
-          return false;
-        }
-
-        $this->_local = $input;
-        return true;
-    }
-
+      function update($input) {
+          $this->_placeholders = true;
+          return parent::update($input);
+      }
   }
 }
 
diff --git a/lib/plugins/config/settings/config.metadata.php b/lib/plugins/config/settings/config.metadata.php
index 585015085..663eab9dd 100644
--- a/lib/plugins/config/settings/config.metadata.php
+++ b/lib/plugins/config/settings/config.metadata.php
@@ -20,9 +20,7 @@
  *   'numericopt'   - like above, but accepts empty values
  *   'onoff'        - checkbox input, setting output  0|1
  *   'multichoice'  - select input (single choice), setting output with quotes, required _choices parameter
- *   'email'        - text input, input must conform to email address format, setting output in quotes
- *   'richemail'    - text input, input must conform to email address format but accepts variables and
- *                    emails with a real name prepended (when email address is given in <>)
+ *   'email'        - text input, input must conform to email address format
  *   'password'     - password input, minimal input validation, setting output text in quotes, maybe encoded
  *                    according to the _code parameter
  *   'dirchoice'    - as multichoice, selection choices based on folders found at location specified in _dir
@@ -177,8 +175,8 @@ $meta['_notifications'] = array('fieldset');
 $meta['subscribers']    = array('onoff');
 $meta['subscribe_time'] = array('numeric');
 $meta['notify']         = array('email', '_multiple' => true);
-$meta['registernotify'] = array('email');
-$meta['mailfrom']       = array('richemail');
+$meta['registernotify'] = array('email', '_multiple' => true);
+$meta['mailfrom']       = array('email', '_placeholders' => true);
 $meta['mailprefix']     = array('string');
 $meta['htmlmail']       = array('onoff');
 
-- 
GitLab