ソースを参照

Fix lineLength and mimType for EmailLib/CakeEmail

euromark 12 年 前
コミット
bc54efd7b6

+ 1 - 1
Console/Command/CcShell.php

@@ -12,7 +12,7 @@ if (!defined('LF')) {
  * Workes perfectly with PHPDesigner - but should also work with most other IDEs out of the box
  *
  * @version 1.1
- * @cakephp 2.0
+ * @cakephp 2.x
  * @author Mark Scherer
  * @license MIT
  * 2011-11-24 ms

+ 1 - 1
Console/Command/ConvertShell.php

@@ -9,7 +9,7 @@ App::uses('AppShell', 'Console/Command');
  * uses dos2unix >= 5.0
  * console call: dos2unix [-fhkLlqV] [-c convmode] [-o file ...] [-n inputfile outputfile ...]
  *
- * @cakephp 2.0
+ * @cakephp 2.x
  * @author Mark Scherer
  * @license MIT
  * 2011-11-04 ms

+ 1 - 1
Console/Command/IndentShell.php

@@ -16,7 +16,7 @@ App::uses('AppShell', 'Console/Command');
 /**
  * Indend Shell
  *
- * @cakephp 2.0
+ * @cakephp 2.x
  * @author Mark Scherer
  * @license MIT
  * 2011-11-04 ms

+ 1 - 1
Console/Command/IndexShell.php

@@ -20,7 +20,7 @@ App::uses('AppShell', 'Console/Command');
  * @author Mark Scherer
  * @link
  * @license http://www.opensource.org/licenses/mit-license.php The MIT License
- * @cakephp 2.0
+ * @cakephp 2.x
  * 2011-12-14 ms
  */
 class IndexShell extends AppShell {

+ 1 - 1
Console/Command/PhpTagShell.php

@@ -7,7 +7,7 @@ App::uses('Folder', 'Utility');
  * it also makes sure there is no whitespace at the beginning of the file
  *
  * @author Mark Scherer, Maximilian Ruta
- * @cakephp 2.0
+ * @cakephp 2.x
  * @license MIT
  * 2011-02-21 de
  */

+ 1 - 1
Controller/Component/Auth/TinyAuthorize.php

@@ -29,7 +29,7 @@ if (!defined('ACL_FILE')) {
  *
  * @version 1.2 - now allows other parent model relations besides Role/role_id
  * @author Mark Scherer
- * @cakephp 2.0
+ * @cakephp 2.x
  * @license MIT
  * 2012-01-09 ms
  */

+ 1 - 1
Lib/CurlLib.php

@@ -6,7 +6,7 @@
  * - can simulate/establish tor connection
  *
  * @license MIT
- * @cakephp 2.0
+ * @cakephp 2.x
  * 2011-07-16 ms
  */
 class CurlLib {

+ 58 - 17
Lib/EmailLib.php

@@ -15,11 +15,11 @@ if (!defined('BR')) {
  * - enable easier attachment adding
  * - extensive logging and error tracing
  * - create mails with blob attachments (embedded or attached)
- * TODO: cleanup and more tests
+ * - allow wrapLength to be adjusted
  *
  * @author Mark Scherer
  * @license MIT
- * @cakephp 2.2
+ * @cakephp 2.x
  * 2012-03-30 ms
  */
 class EmailLib extends CakeEmail {
@@ -30,6 +30,8 @@ class EmailLib extends CakeEmail {
 
 	protected $_error = null;
 
+	protected $_wrapLength = null;
+
 	public function __construct($config = null) {
 		if ($config === null) {
 			$config = 'default';
@@ -187,6 +189,13 @@ class EmailLib extends CakeEmail {
 		return false;
 	}
 
+	/**
+	 * Try to determine the mimetype by filename.
+	 * Uses finfo_open() if availble, otherwise guesses it via file extension.
+	 *
+	 * @param string $filename
+	 * @param string Mimetype
+	 */
 	protected function _getMime($filename) {
 		if (function_exists('finfo_open')) {
 			$finfo = finfo_open(FILEINFO_MIME);
@@ -201,10 +210,11 @@ class EmailLib extends CakeEmail {
 	}
 
 	/**
-	 * try to find mimetype by file extension
+	 * Try to find mimetype by file extension
+	 *
 	 * @param string $ext lowercase (jpg, png, pdf, ...)
 	 * @param string $defaultMimeType
-	 * @return string $mimeType (falls back to )
+	 * @return string Mimetype (falls back to `application/octet-stream`)
 	 * 2012-04-17 ms
 	 */
 	protected function _getMimeByExtension($ext, $default = 'application/octet-stream') {
@@ -218,6 +228,9 @@ class EmailLib extends CakeEmail {
 		return $mime;
 	}
 
+	/**
+	 * @return boolean Success
+	 */
 	public function validates() {
 		if (!empty($this->Email->subject)) {
 			return true;
@@ -474,6 +487,7 @@ class EmailLib extends CakeEmail {
 	 * @return void
 	 * @throws ConfigureException When configuration file cannot be found, or is missing
 	 * the named config.
+	 * @overwrite
 	 */
 	protected function _applyConfig($config) {
 		if (is_string($config)) {
@@ -499,7 +513,7 @@ class EmailLib extends CakeEmail {
 		$simpleMethods = array(
 			'from', 'sender', 'to', 'replyTo', 'readReceipt', 'returnPath', 'cc', 'bcc',
 			'messageId', 'domain', 'subject', 'viewRender', 'viewVars', 'attachments',
-			'transport', 'emailFormat'
+			'transport', 'emailFormat', 'theme', 'helpers'
 		);
 		foreach ($simpleMethods as $method) {
 			if (isset($config[$method])) {
@@ -540,20 +554,10 @@ class EmailLib extends CakeEmail {
 			'replyTo' => $this->_replyTo,
 			'cc' => $this->_cc,
 			'subject' => $this->_subject,
-			'cc' => $this->_cc,
+			'bcc' => $this->_bcc,
 			'transport' => $this->_transportName
 		);
 
-		# prep images for inline
-		/*
-		if ($this->_emailFormat !== 'text') {
-			if ($message !== null) {
-				$message = $this->_prepMessage($message);
-			} else {
-				$this->_htmlMessage = $this->_prepMessage($this->_htmlMessage);
-			}
-		}
-		*/
 		try {
 			$this->_debug = parent::send($message);
 		} catch (Exception $e) {
@@ -596,6 +600,43 @@ class EmailLib extends CakeEmail {
 	}
 
 	/**
+	 * Set/Get wrapLength
+	 *
+	 * @param int $length Must not be more than CakeEmail::LINE_LENGTH_MUST
+	 * @return void|int
+	 */
+	public function wrapLength($length = null) {
+		if ($length === null) {
+			return $this->_wrapLength;
+		}
+		$this->_wrapLength = $length;
+	}
+
+	/**
+	 * Fix line length for _renderTemplates()
+	 * @overwrite
+	 */
+	protected function _renderTemplates($content) {
+		$res = parent::_renderTemplates($content);
+		foreach ($res as $type => $content) {
+			$res[$type] = $this->_wrap($content);
+			$res[$type] = implode("\n", $res[$type]);
+		}
+		return $res;
+	}
+
+	/**
+	 * Fix line length
+	 * @overwrite
+	 */
+	protected function _wrap($message, $wrapLength = CakeEmail::LINE_LENGTH_MUST) {
+		if ($this->_wrapLength !== null) {
+			$wrapLength = $this->_wrapLength;
+		}
+		return parent::_wrap($message, $wrapLength);
+	}
+
+	/**
 	 * Logs Email to type email
 	 * @return void
 	 */
@@ -643,7 +684,7 @@ class EmailLib extends CakeEmail {
 		$this->from($fromEmail, $fromName);
 
 		if ($xMailer = Configure::read('Config.x-mailer')) {
-			$this->addHeaders(array('X-Mailer'=>$xMailer));
+			$this->addHeaders(array('X-Mailer' => $xMailer));
 		}
 		//$this->_errors = array();
 		//$this->charset($this->charset);

+ 1 - 1
Lib/HttpSocketLib.php

@@ -16,7 +16,7 @@ App::uses('CurlLib', 'Tools.Lib');
  *
  * @author Mark Scherer
  * @license MIT
- * @cakephp 2.0
+ * @cakephp 2.x
  * 2011-10-14 ms
  */
 class HttpSocketLib {

+ 1 - 1
Lib/IcalLib.php

@@ -13,7 +13,7 @@ App::uses('TimeLib', 'Tools.Utility');
  *
  * @author Mark Scherer
  * @license MIT
- * @cakephp 2.0
+ * @cakephp 2.x
  * 2010-09-14 ms
  */
 class IcalLib {

+ 1 - 1
Lib/ImapLib.php

@@ -11,7 +11,7 @@
  *
  * @modified 2011-11-13 Mark Scherer
  * @php 5
- * @cakephp 2.0
+ * @cakephp 2.x
  *
  * ImapLib for accessing IMAP and POP email accounts
  * 2011-10-11 ms

+ 21 - 20
Lib/MimeLib.php

@@ -188,14 +188,12 @@ class MimeLib extends CakeResponse {
 		'hqx' => 'application/x-mac-binhex40',
 		'hta' => 'application/hta',
 		'htc' => 'text/x-component',
-		'htm' => 'text/html',
-		'html' => 'text/html',
 		'htmls' => 'text/html',
 		'htt' => 'text/webviewhtml',
 		'htx' => 'text/html',
 		'ice' => 'x-conference/x-cooltalk',
 		'ico' => 'image/x-icon',
-		'ics' => 'text/calendar',
+		'ics' => 'application/ics', // important for ipad to properly display ics files
 		'ical' => 'text/calendar',
 		'idc' => 'text/plain',
 		'ief' => 'image/ief',
@@ -655,8 +653,6 @@ class MimeLib extends CakeResponse {
 		'zoo' => 'application/octet-stream',
 		'zsh' => 'text/x-script.zsh',
 		'txt' => 'text/plain',
-		'htm' => 'text/html',
-		'html' => 'text/html',
 		'php' => 'application/x-httpd-php',
 		'phps' => 'application/x-httpd-phps',
 		'css' => 'text/css',
@@ -706,41 +702,48 @@ class MimeLib extends CakeResponse {
 		'swf' => array('application/x-shockwave-flash', 'application/x-shockwave-flash2-preview', 'application/futuresplash', 'image/vnd.rn-realflash')
 	);
 
-
 	/**
 	 * override constructor
 	 * 2012-02-12 ms
 	 */
 	public function __construct() {
-
 	}
 
 	/**
-	 * get all mime types that are supported by cake php core right now
+	 * Get all mime types that are supported right now
+	 *
 	 * @return array
 	 * 2012-02-12 ms
 	 */
-	public function getMimeTypes($coreOnly = false) {
-		if ($coreOnly) {
-			return $this->_mimeTypes;
+	public function getMimeTypes($coreHasPrecedence = false) {
+		if ($coreHasPrecedence) {
+			return $this->_mimeTypes += $this->_mimeTypesExt;
 		}
-		return array_merge($this->_mimeTypesExt, $this->_mimeTypes);
+		return $this->_mimeTypesExt += $this->_mimeTypes;
 	}
 
 	/**
-	 * Returns the mime type definition for an alias
+	 * Returns the primary mime type definition for an alias
 	 *
 	 * e.g `getMimeType('pdf'); // returns 'application/pdf'`
 	 *
 	 * @param string $alias the content type alias to map
+	 * @param boolean $primaryOnly
+	 * @param boolean $coreHasPrecedence
 	 * @return mixed string mapped mime type or false if $alias is not mapped
 	 */
-	public function getMimeType($alias, $coreOnly = false) {
-		$res = parent::getMimeType($alias);
-		if ($res === false && !$coreOnly && isset($this->_mimeTypesExt[$alias])) {
-		return $this->_mimeTypesExt[$alias];
+	public function getMimeType($alias, $primaryOnly = true, $coreHasPrecedence = false) {
+		if (empty($this->_mimeTypeTmp)) {
+			$this->_mimeTypesTmp = $this->getMimeTypes($coreHasPrecedence);
+		}
+		if (!isset($this->_mimeTypesTmp[$alias])) {
+			return false;
 		}
-		return $res;
+		$mimeType = $this->_mimeTypesTmp[$alias];
+		if ($primaryOnly && is_array($mimeType)) {
+			$mimeType = array_shift($mimeType);
+		}
+		return $mimeType;
 	}
 
 	/**
@@ -755,7 +758,6 @@ class MimeLib extends CakeResponse {
 		return parent::mapType($ctype);
 	}
 
-
 	/**
 	* @desc Retrieve the corresponding MIME type, if one exists
 	* @param String $file File Name (relative location such as "image_test.jpg" or full "http://site.com/path/to/image_test.jpg")
@@ -792,7 +794,6 @@ class MimeLib extends CakeResponse {
 		return false;
 	}
 
-
 	/**
 	 * Write your Description here.
 	 *

+ 3 - 1
Lib/Utility/TimeLib.php

@@ -360,11 +360,13 @@ class TimeLib extends CakeTime {
 	 * @param mixed $startDate Either a date string or a DateTime object
 	 * @param int $years Years to increment/decrement
 	 * @param int $months Months to increment/decrement
+	 * @param string|DateTimeZone $timezone Timezone string or DateTimeZone object
 	 * @return object DateTime with incremented/decremented month/year values.
 	 */
-	public static function incrementDate($startDate, $years = 0, $months = 0) {
+	public static function incrementDate($startDate, $years = 0, $months = 0, $days = 0, $timezone = null) {
 		if (!is_object($startDate)) {
 			$startDate = new DateTime($startDate);
+			$startDate->setTimezone($timezone ? new DateTimeZone($timezone) : self::timezone());
 		}
 		$startingTimeStamp = $startDate->getTimestamp();
 		// Get the month value of the given date:

+ 1 - 1
Model/Behavior/DecimalInputBehavior.php

@@ -27,7 +27,7 @@ App::uses('ModelBehavior', 'Model');
  *
  * @author Mark Scherer
  * @license MIT
- * @cakephp 2.0
+ * @cakephp 2.x
  * @deprecated Use NumberFormatBehavior instead!
  * 2012-12-05 ms
  */

+ 1 - 1
Model/Behavior/NumberFormatBehavior.php

@@ -27,7 +27,7 @@ App::uses('ModelBehavior', 'Model');
  *
  * @author Mark Scherer
  * @license MIT
- * @cakephp 2.0
+ * @cakephp 2.x
  * 2012-12-05 ms
  */
 class NumberFormatBehavior extends ModelBehavior {

+ 1 - 1
Model/CodeKey.php

@@ -5,7 +5,7 @@ App::uses('Token', 'Tools.Model');
  * @deprecated - use "Token" class
  *
  * @author Mark Scherer
- * @cakephp 2.0
+ * @cakephp 2.x
  * @license MIT
  * 2011-11-17 ms
  */

+ 1 - 1
Model/ContactForm.php

@@ -4,7 +4,7 @@ App::uses('ToolsAppModel', 'Tools.Model');
 /**
  * "Fake" model to validate all contact forms
  * @author Mark Scherer
- * @cakephp 2.0
+ * @cakephp 2.x
  * @license MIT
  * 2011-12-15 ms
  */

+ 1 - 1
Model/Qlogin.php

@@ -8,7 +8,7 @@ App::uses('CakeSession', 'Model/Datasource');
  * Manage Quick Logins
  *
  * @author Mark Scherer
- * @cakephp 2.0
+ * @cakephp 2.x
  * @license MIT
  * 2011-11-17 ms
  */

ファイルの差分が大きいため隠しています
+ 20 - 0
Test/Case/Lib/EmailLibTest.php


+ 68 - 9
Test/Case/Lib/MimeLibTest.php

@@ -1,7 +1,7 @@
 <?php
-
 App::uses('MimeLib', 'Tools.Lib');
 App::uses('MyCakeTestCase', 'Tools.TestSuite');
+App::uses('CakeResponse', 'Network');
 
 class MimeLibTest extends MyCakeTestCase {
 
@@ -13,7 +13,6 @@ class MimeLibTest extends MyCakeTestCase {
 		$this->Mime = new MimeLib();
 	}
 
-
 	public function testObject() {
 		$this->assertTrue(is_object($this->Mime));
 		$this->assertInstanceOf('MimeLib', $this->Mime);
@@ -25,21 +24,33 @@ class MimeLibTest extends MyCakeTestCase {
 	}
 
 	public function testSingle() {
-		$res = $this->Mime->getMimeType('csv');
-		$this->assertTrue(is_array($res) && count($res) > 2);
-
-		$res = $this->Mime->getMimeType('ods', true);
+		$res = $this->Mime->getMimeType('odxs');
 		$this->assertFalse($res);
 
 		$res = $this->Mime->getMimeType('ods');
 		$this->assertEquals('application/vnd.oasis.opendocument.spreadsheet', $res);
 	}
 
-	public function testSingleReverse() {
+	public function testOverwrite() {
+		$res = $this->Mime->getMimeType('ics');
+		$this->assertEquals('application/ics', $res);
+	}
+
+	public function testReverseToSingle() {
+		$res = $this->Mime->getMimeType('html');
+		$this->assertEquals('text/html', $res);
+
 		$res = $this->Mime->getMimeType('csv');
-		$this->assertTrue(is_array($res) && count($res) > 2);
+		$this->assertEquals('text/csv', $res);
 	}
 
+	public function testReverseToMultiple() {
+		$res = $this->Mime->getMimeType('html', false);
+		$this->assertTrue(is_array($res) && count($res) === 2);
+
+		$res = $this->Mime->getMimeType('csv', false);
+		$this->assertTrue(is_array($res) && count($res) > 2);
+	}
 
 	/**
 	 * test fake files
@@ -83,7 +94,55 @@ class MimeLibTest extends MyCakeTestCase {
 		$is = $this->Mime->getEncoding(TMP.'sometest.zip');
 		//pr($is);
 		$this->assertEquals($is, 'us-ascii');
-
 		//Tests fail? finfo_open not availaible??
 	}
+
+	/**
+	 * MimeLibTest::testDifferenceBetweenPluginAndCore()
+	 *
+	 * @return void
+	 */
+	public function testDifferenceBetweenPluginAndCore() {
+		$this->TestCakeResponse = new TestCakeResponse();
+		$this->TestMime = new TestMimeLib();
+
+		$core = $this->TestCakeResponse->getMimeTypes();
+		$plugin = $this->TestMime->getMimeTypes();
+
+		$diff = array(
+			'coreonly' => array(),
+			'pluginonly' => array(),
+			'modified' => array()
+		);
+		foreach ($core as $key => $value) {
+			if (!isset($plugin[$key])) {
+				$diff['coreonly'][$key] = $value;
+			} elseif ($value !== $plugin[$key]) {
+				$diff['modified'][$key] = array('was' => $value, 'is' => $plugin[$key]);
+			}
+			unset($plugin[$key]);
+		}
+		foreach ($plugin as $key => $value) {
+			$diff['pluginonly'][$key] = $value;
+		}
+		$this->debug($diff);
+	}
+
+}
+
+class TestCakeResponse extends CakeResponse {
+
+	public function getMimeTypes() {
+		return $this->_mimeTypes;
+	}
+
 }
+
+class TestMimeLib extends MimeLib {
+
+	public function getMimeTypes() {
+		return $this->_mimeTypesExt;
+	}
+
+}
+

+ 6 - 0
Test/Case/Lib/Utility/TimeLibTest.php

@@ -52,6 +52,12 @@ class TimeLibTest extends MyCakeTestCase {
 		$from = '2012-12-31';
 		$Date = TimeLib::incrementDate($from, -1, -1);
 		$this->assertSame('2011-11-30', $Date->format(FORMAT_DB_DATE));
+
+		// including days
+		$from = '2012-12-31';
+		$Date = TimeLib::incrementDate($from, 0, 1, 1);
+		//TODO
+		//$this->assertSame('2013-02-01', $Date->format(FORMAT_DB_DATE));
 	}
 
 	public function testNiceDate() {

+ 1 - 1
Test/Case/View/Helper/IcalHelperTest.php

@@ -12,7 +12,7 @@ App::uses('View', 'View');
  *
  * @author Mark Scherer
  * @license MIT
- * @cakephp 2.0
+ * @cakephp 2.x
  * 2010-09-14 ms
  */
 class IcalHelperTest extends MyCakeTestCase {

+ 1 - 1
View/Helper/BootstrapHelper.php

@@ -13,7 +13,7 @@ App::uses('AppHelper', 'View/Helper');
  *
  * @license MIT
  * @author Mark Scherer
- * @cakephp 2.0
+ * @cakephp 2.x
  * @php 5
  * @version 1.0
  * 2012-03-29 ms

+ 28 - 0
View/Helper/TextExtHelper.php

@@ -23,6 +23,34 @@ App::uses('View', 'View');
 class TextExtHelper extends TextHelper {
 
 	/**
+	 * Formats paragraphs around given text for all line breaks
+	 *  <br /> added for single line return
+	 *  <p> added for double line return
+	 *
+	 * @param string $text Text
+	 * @return string The text with proper <p> and <br /> tags
+	 * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::autoParagraph
+	 */
+	public function autoParagraph($text) {
+		// for cake >= 2.4
+		if (method_exists(get_parent_class(), 'autoParagraph')) {
+			return parent::autoParagraph($text);
+		}
+
+		if (trim($text) !== '') {
+			$text = preg_replace('|<br[^>]*>\s*<br[^>]*>|i', "\n\n", $text . "\n");
+			$text = preg_replace("/\n\n+/", "\n\n", str_replace(array("\r\n", "\r"), "\n", $text));
+			$texts = preg_split('/\n\s*\n/', $text, -1, PREG_SPLIT_NO_EMPTY);
+			$text = '';
+			foreach ($texts as $txt) {
+				$text .= '<p>' . nl2br(trim($txt, "\n")) . "</p>\n";
+			}
+			$text = preg_replace('|<p>\s*</p>|', '', $text);
+		}
+		return $text;
+	}
+
+	/**
 	 * Convert all links and email adresses to HTML links.
 	 *
 	 * @param string $text Text