Browse Source

imap utf8 encoding fix for subject

euromark 13 years ago
parent
commit
12cdf9033c
4 changed files with 166 additions and 28 deletions
  1. 18 21
      Lib/Auth.php
  2. 8 3
      Lib/ImapLib.php
  3. 14 0
      Test/Case/Lib/AuthTest.php
  4. 126 4
      Test/Case/Lib/ImapLibTest.php

+ 18 - 21
Lib/Auth.php

@@ -114,31 +114,28 @@ class Auth {
 		} else {
 			$roles = self::roles();
 		}
-		if (is_array($ownRoles)) {
-			if (empty($ownRoles)) {
-				return false;
-			}
-			$count = 0;
-			foreach ($ownRoles as $role) {
-				if (self::hasRole($role, $roles)) {
-					if ($oneRoleIsEnough) {
-						return true;
-					}
-					$count++;
-				} else {
-					if (!$oneRoleIsEnough) {
-						return false;
-					}
+		$ownRoles = (array)$ownRoles;
+		if (empty($ownRoles)) {
+			return false;
+		}
+		$count = 0;
+		foreach ($ownRoles as $role) {
+			if (self::hasRole($role, $roles)) {
+				if ($oneRoleIsEnough) {
+					return true;
+				}
+				$count++;
+			} else {
+				if (!$oneRoleIsEnough) {
+					return false;
 				}
 			}
+		}
 
-			if ($count == count($ownRoles)) {
-				return true;
-			}
-			return false;
-		} else {
-			return self::hasRole($ownRoles, $roles);
+		if ($count === count($ownRoles)) {
+			return true;
 		}
+		return false;
 	}
 
 }

+ 8 - 3
Lib/ImapLib.php

@@ -209,7 +209,7 @@ class ImapLib {
 
 					// Simple array
 					if (!is_array($value)) {
-						$return[$msgNo][$id] = $value;
+						$return[$msgNo][$id] = imap_utf8($value);
 					} else {
 						foreach ($value as $newid => $array_value) {
 							foreach ($value[0] as $key => $aValue) {
@@ -239,7 +239,7 @@ class ImapLib {
 					} else {
 						foreach ($value as $newid => $array_value) {
 							foreach ($value[0] as $key => $aValue) {
-								$return[$header->Msgno][$id][$key] = $this->_quoted_printable_encode($aValue);
+								$return[$header->Msgno][$id][$key] = quoted_printable_decode($aValue);
 							}
 						}
 					}
@@ -313,7 +313,11 @@ class ImapLib {
 				$attachments[] = $attachment;
 			} else { // inline attachments etc
 				$attachment["pid"] = $i;
-				$attachment["type"][$i] = $message["attachment"]["type"][$part->type] . "/" . strtolower($part->subtype);
+				$type = '';
+				if (!empty($message["attachment"]["type"][$part->type])) {
+					$type = $message["attachment"]["type"][$part->type] . "/";
+				}
+				$attachment["type"][$i] = $type . strtolower($part->subtype);
 				$attachment["subtype"][$i] = strtolower($part->subtype);
 				$ext = $part->subtype;
 				$params = $part->parameters;
@@ -518,6 +522,7 @@ class ImapLib {
 
 }
 
+// Currently NOT IN USE: //
 
 /**
  * IMAP Postf�cher mit CakePHP abfragen

+ 14 - 0
Test/Case/Lib/AuthTest.php

@@ -15,6 +15,17 @@ class AuthTest extends MyCakeTestCase {
 
 	public function tearDown() {
 		ClassRegistry::flush();
+
+		CakeSession::delete('Auth');
+	}
+
+	public function testId() {
+		$id = Auth::id();
+		$this->assertNull($id);
+
+		CakeSession::write('Auth.User.id', 1);
+		$id = Auth::id();
+		$this->assertEquals(1, $id);
 	}
 
 	public function testHasRole() {
@@ -41,6 +52,9 @@ class AuthTest extends MyCakeTestCase {
 		$res = Auth::hasRoles(array(3), true, array(2, 3, 6));
 		$this->assertTrue($res);
 
+		$res = Auth::hasRoles(3, true, array(2, 3, 6));
+		$this->assertTrue($res);
+
 		$res = Auth::hasRoles(array(), true, array(2, 3, 6));
 		$this->assertFalse($res);
 

+ 126 - 4
Test/Case/Lib/ImapLibTest.php

@@ -1,12 +1,34 @@
 <?php
 
 App::uses('ImapLib', 'Tools.Lib');
+App::uses('EmailLib', 'Tools.Lib');
 App::uses('MyCakeTestCase', 'Tools.TestSuite');
 
+/**
+ * Testing IMAP send/receive
+ *
+ * The following config is needed:
+ *   Configure::write('Mailbox.DEVTEST.address', 'devtest@host');
+ *   Configure::write('Mailbox.DEVTEST.password', 'devtest');
+ *   Configure::write('Mailbox.DEVTEST.host', 'host');
+ *
+ * The following files are needed in /APP/Test/test_files/:
+ *   Sample_Email_PDF.pdf
+ *   Sample_Email_PDF_UTF8.pdf
+ *
+ * 2012-11-27 ms
+ */
 class ImapLibTest extends MyCakeTestCase {
 
+	public $Imap;
+
 	public function setUp() {
-		$this->ImapLib = new ImapLib();
+		$this->skipIf(!function_exists('imap_open'), 'No Imap class installed');
+		$this->skipIf(!Configure::read('Mailbox.DEVTEST'), 'No test account available');
+
+		$this->Imap = new ImapLib();
+
+		$this->testFilePath = APP . 'Test' . DS . 'test_files' . DS;
 	}
 
 	public function tearDown() {
@@ -14,11 +36,111 @@ class ImapLibTest extends MyCakeTestCase {
 	}
 
 	public function testObject() {
-		$this->assertTrue(is_a($this->ImapLib, 'ImapLib'));
+		$this->assertTrue(is_a($this->Imap, 'ImapLib'));
+	}
+
+	public function testCount() {
+		$count = $this->_count();
+		debug($count); ob_flush();
+		$this->assertSame(0, $count);
+	}
+
+	public function testReceive() {
+		$file = $this->testFilePath . 'Sample_Email_PDF.pdf';
+		$this->_send($file);
+		sleep(2);
+
+		$messages = $this->_read();
+		debug($messages); ob_flush();
+		$this->assertTrue(!empty($messages));
+		$message = array_shift($messages);
+		$this->assertTrue(!empty($message['subject']));
+	}
+
+	public function testReceiveUtf8() {
+		$file = $this->testFilePath . 'Sample_Email_PDF_UTF8.pdf';
+		$this->_send($file);
+		sleep(2);
+
+		$messages = $this->_read();
+		debug($messages); ob_flush();
+		$this->assertTrue(!empty($messages));
+		$message = array_shift($messages);
+		$this->assertTrue(!empty($message['subject']));
+	}
+
+	protected function _send($file, $contentDisposition = false) {
+		Configure::write('debug', 0);
+
+		$this->Email = new EmailLib();
+		$this->Email->to(Configure::read('Mailbox.DEVTEST.address'));
+		$this->Email->subject('UTF8 ÄÖÜ Test Mail '.date(FORMAT_DB_DATETIME));
+		$this->Email->layout('blank');
+		$this->Email->template('simple_email');
+		$this->Email->addAttachment($file, 'test.php', array('contentDisposition' => $contentDisposition));
+		$text = '';
+		$this->Email->viewVars(compact('text'));
+		if ($this->Email->send()) {
+			Configure::write('debug', 2);
+			return true;
+		}
+		Configure::write('debug', 2);
+		trigger_error($this->Email->getError());
+		return false;
 	}
 
-	public function testX() {
-		//TODO
+	protected function _count($code = 'DEVTEST') {
+		$account = Configure::read('Mailbox.'.$code);
+		if (!isset($account['host'])) {
+			$account['host'] = Configure::read('Mailbox.host');
+		}
+
+		$Imap = new ImapLib();
+		$Imap->set(ImapLib::S_SERVICE, 'imap');
+		$Imap->set(ImapLib::S_NORSH, true);
+		$res = $Imap->connect($account['address'], $account['password'], $account['host']);
+		if (!$res) {
+			throw new InternalErrorException('Error connecting: '.$account['address'].' - '.$account['host'].' ('.$account['password'].')');
+		}
+		$count = $Imap->msgCount();
+		$Imap->close();
+		return $count;
+	}
+
+	protected function _read($code = 'DEVTEST', $delete = true) {
+		$account = Configure::read('Mailbox.'.$code);
+		if (!isset($account['host'])) {
+			$account['host'] = Configure::read('Mailbox.host');
+		}
+
+		$Imap = new ImapLib();
+		$Imap->set(ImapLib::S_SERVICE, 'imap');
+		$Imap->set(ImapLib::S_NORSH, true);
+		//$Imap->set(ImapLib::S_NOTLS, true);
+		//$Imap->set(ImapLib::S_TLS, true);
+		/*
+		if (($pos = strpos($account['address'], '@')) !== false) {
+			$account['address'] = substr($account['address'], 0, $pos);
+		}
+		*/
+		$res = $Imap->connect($account['address'], $account['password'], $account['host']);
+		if (!$res) {
+			//trigger_error($account['address'].' - '.Configure::read('Mailbox.host').' ('.$account['password'].')');
+			throw new InternalErrorException('Error connecting: '.$account['address'].' - '.$account['host'].' ('.$account['password'].')');
+			//return array();
+		}
+		//$count = $Imap->msgCount();
+		$messages = $Imap->msgList();
+		if ($delete) {
+			$messageNumbers = Set::extract('/Msgno', $messages);
+			//TODO: FIX Delete
+			$res = $Imap->delete($messageNumbers, true);
+		}
+		$Imap->close();
+		if (!is_array($messages)) {
+			return array();
+		}
+		return $messages;
 	}
 
 }