浏览代码

Added fix for mod_rewrite issues on apache servers

euromark 12 年之前
父节点
当前提交
1fe80d0994
共有 2 个文件被更改,包括 42 次插入7 次删除
  1. 35 0
      Controller/MyController.php
  2. 7 7
      Lib/Utility/TextLib.php

+ 35 - 0
Controller/MyController.php

@@ -30,6 +30,41 @@ class MyController extends Controller {
 	}
 
 	/**
+	 * Fix encoding issues on Apache with mod_rewrite
+	 * Uses Configure::read('App.additionalEncoding') to additionally escape
+	 *
+	 * Tip: Set it to `1` for normal mod_rewrite websites routing directly into webroot
+	 * If you use another setup (like localhost/app/webroot) where you use multiple htaccess files or rewrite
+	 * rules you need to raise it accordingly.
+	 *
+	 * 2013-03-02 ms
+	 */
+	public function redirect($url, $status = null, $exit = true) {
+		$run = Configure::read('App.additionalEncoding');
+		if ($run) {
+			foreach ($url as $key => $value) {
+				if ($key === '?') {
+					continue;
+				}
+				$value = $this->_encodeUrlPiece($value, $run);
+
+				$url[$key] = $value;
+			}
+		}
+		return parent::redirect($url, $status, $exit);
+	}
+
+	public function _encodeUrlPiece($value, $run) {
+		if (!is_array($value)) {
+			for ($i = 0; $i < $run; $i++) {
+				$value = urlencode($value);
+			}
+			return $value;
+		}
+		return $this->_encodeUrlPiece($value, $run);
+	}
+
+	/**
 	 * Init Packages class if enabled/included
 	 * @deprecated?
 	 * 2012-12-25 ms

+ 7 - 7
Lib/Utility/TextLib.php

@@ -17,13 +17,13 @@ class TextLib extends String {
 	}
 
 	/**
-   * Return an abbreviated string, with characters in the middle of the
-   * excessively long string replaced by $ending.
-   *
-   * @param string $text The original string.
-   * @param integer $length The length at which to abbreviate.
-   * @return string The abbreviated string, if longer than $length.
-   */
+	 * Return an abbreviated string, with characters in the middle of the
+	 * excessively long string replaced by $ending.
+	 *
+	 * @param string $text The original string.
+	 * @param integer $length The length at which to abbreviate.
+	 * @return string The abbreviated string, if longer than $length.
+	 */
 	public static function abbreviate($text, $length = 20, $ending = '...') {
 		return (mb_strlen($text) > $length)
 			? rtrim(mb_substr($text, 0, round(($length - 3) / 2))) . $ending . ltrim(mb_substr($text, (($length - 3) / 2) * -1))