From 0f4c9b850f665f0fdafb00809b4b9f100c59f331 Mon Sep 17 00:00:00 2001
From: Till Krüss <me@tillkruess.com>
Date: Tue, 15 Oct 2013 17:56:03 +0800
Subject: check if the new password matched the requirements; code cleanup

---
 plugins/password/drivers/domainfactory.php | 115 +++++++++++++++++------------
 1 file changed, 66 insertions(+), 49 deletions(-)

(limited to 'plugins/password')

diff --git a/plugins/password/drivers/domainfactory.php b/plugins/password/drivers/domainfactory.php
index 7f6b8860e..e253faa49 100644
--- a/plugins/password/drivers/domainfactory.php
+++ b/plugins/password/drivers/domainfactory.php
@@ -4,9 +4,9 @@
  * domainFACTORY Password Driver
  *
  * Driver to change passwords with the hosting provider domainFACTORY.
- * See: http://www.df.eu/
+ * http://www.df.eu/
  *
- * @version 2.0
+ * @version 2.1
  * @author Till Krüss <me@tillkruess.com>
  * @link http://tillkruess.com/projects/roundcube/
  *
@@ -14,57 +14,74 @@
 
 class rcube_domainfactory_password
 {
-    function save($curpass, $passwd)
-    {
-	    $rcmail = rcmail::get_instance();
+	function save($curpass, $passwd)
+	{
+		$rcmail = rcmail::get_instance();
 
-	    if (is_null($curpass)) {
-		    $curpass = $rcmail->decrypt($_SESSION['password']);
-	    }
+		if (is_null($curpass)) {
+			$curpass = $rcmail->decrypt($_SESSION['password']);
+		}
 
-    	if ($ch = curl_init()) {
-    		// initial login
-	    	curl_setopt_array($ch, array(
-		    	CURLOPT_RETURNTRANSFER => true,
-    			CURLOPT_URL => 'https://ssl.df.eu/chmail.php',
-	    		CURLOPT_POST => true,
-		    	CURLOPT_POSTFIELDS => array(
-			    	'login' => $rcmail->user->get_username(),
-				    'pwd' => $curpass,
-    				'action' => 'change'
-	    		)
-		    ));
+		if ($ch = curl_init()) {
 
-    		if ($result = curl_exec($ch)) {
-    			// login successful, get token!
-	    		$postfields = array(
-		    		'pwd1' => $passwd,
-			    	'pwd2' => $passwd,
-				    'action[update]' => 'Speichern'
-    			);
+			// initial login
+			curl_setopt_array($ch, array(
+				CURLOPT_RETURNTRANSFER => true,
+				CURLOPT_URL => 'https://ssl.df.eu/chmail.php',
+				CURLOPT_POST => true,
+				CURLOPT_POSTFIELDS => array(
+					'login' => $rcmail->user->get_username(),
+					'pwd' => $curpass,
+					'action' => 'change'
+				)
+			));
 
-    			preg_match_all('~<input name="(.+?)" type="hidden" value="(.+?)">~i', $result, $fields);
-	    		foreach ($fields[1] as $field_key => $field_name) {
-		    		$postfields[$field_name] = $fields[2][$field_key];
-			    }
+			if ($result = curl_exec($ch)) {
+				// login successful, get token!
+				$postfields = array(
+					'pwd1' => $passwd,
+					'pwd2' => $passwd,
+					'action[update]' => 'Speichern'
+				);
 
-    			// change password
-	    		$ch = curl_copy_handle($ch);
-		    	curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
-			    if ($result = curl_exec($ch)) {
-    				if (strpos($result, 'Einstellungen erfolgreich') !== false) {
-	    				return PASSWORD_SUCCESS;
-		    		}
-    			} else {
-	    			return PASSWORD_CONNECT_ERROR;
-		    	}
-    		} else {
-	    		return PASSWORD_CONNECT_ERROR;
-		    }
-    	} else {
-	    	return PASSWORD_CONNECT_ERROR;
-	    }
+				preg_match_all('~<input name="(.+?)" type="hidden" value="(.+?)">~i', $result, $fields);
+				foreach ($fields[1] as $field_key => $field_name) {
+					$postfields[$field_name] = $fields[2][$field_key];
+				}
 
-    	return PASSWORD_ERROR;
-    }
+				// change password
+				$ch = curl_copy_handle($ch);
+				curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
+				if ($result = curl_exec($ch)) {
+
+					// did the new password match the requirements?
+					if (strpos($result, '<div class="d-msg-text">') !== false) {
+						preg_match_all('#<div class="d-msg-text">(.*?)</div>#s', $result, $errors);
+						if (isset($errors[1])) {
+							$error_message = '';
+							foreach ( $errors[1] as $error ) {
+								$error_message .= trim(mb_convert_encoding( $error, 'UTF-8', 'ISO-8859-15' )).' ';
+							}
+							return array('code' => PASSWORD_ERROR, 'message' => $error_message);
+						}
+					}
+
+					if (strpos($result, 'Einstellungen erfolgreich') !== false) {
+						return PASSWORD_SUCCESS;
+					}
+
+				} else {
+					return PASSWORD_CONNECT_ERROR;
+				}
+
+			} else {
+				return PASSWORD_CONNECT_ERROR;
+			}
+
+		} else {
+			return PASSWORD_CONNECT_ERROR;
+		}
+
+		return PASSWORD_ERROR;
+	}
 }
-- 
cgit v1.2.3


From 910a3581be47da1d19eb15a68ea6b51c2c31ef67 Mon Sep 17 00:00:00 2001
From: Till Krüss <me@tillkruess.com>
Date: Tue, 15 Oct 2013 18:13:24 +0800
Subject: check if password was changed successfully, before looking for error
 messages

---
 plugins/password/drivers/domainfactory.php | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

(limited to 'plugins/password')

diff --git a/plugins/password/drivers/domainfactory.php b/plugins/password/drivers/domainfactory.php
index e253faa49..9128720c9 100644
--- a/plugins/password/drivers/domainfactory.php
+++ b/plugins/password/drivers/domainfactory.php
@@ -54,7 +54,12 @@ class rcube_domainfactory_password
 				curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
 				if ($result = curl_exec($ch)) {
 
-					// did the new password match the requirements?
+					// has the password been changed?
+					if (strpos($result, 'Einstellungen erfolgreich') !== false) {
+						return PASSWORD_SUCCESS;
+					}
+
+					// show error message(s) if possible
 					if (strpos($result, '<div class="d-msg-text">') !== false) {
 						preg_match_all('#<div class="d-msg-text">(.*?)</div>#s', $result, $errors);
 						if (isset($errors[1])) {
@@ -66,9 +71,6 @@ class rcube_domainfactory_password
 						}
 					}
 
-					if (strpos($result, 'Einstellungen erfolgreich') !== false) {
-						return PASSWORD_SUCCESS;
-					}
 
 				} else {
 					return PASSWORD_CONNECT_ERROR;
-- 
cgit v1.2.3