ソースを参照

cleanup and coding standards

euromark 12 年 前
コミット
8ebe85cecc

+ 12 - 10
Lib/CaptchaLib.php

@@ -6,17 +6,18 @@ if (!defined('FORMAT_DB_DATE')) {
 }
 
 /**
- * used by captcha helper and behavior
+ * Main utility for captchas.
+ * Used by captcha helper and behavior.
  */
 class CaptchaLib {
 
-	public static $defaults = array (
-			'dummyField' => 'homepage',
-			'method' => 'hash',
-			'type' => 'both',
-			'checkSession' => false,
-			'checkIp' => false,
-			'salt' => '',
+	public static $defaults = array(
+		'dummyField' => 'homepage',
+		'method' => 'hash',
+		'type' => 'both',
+		'checkSession' => false,
+		'checkIp' => false,
+		'salt' => '',
 	);
 
 	# what type of captcha
@@ -31,7 +32,7 @@ class CaptchaLib {
 	 * @param array $options:
 	 * - salt (required)
 	 * - checkSession, checkIp, hashType (all optional)
-	 * 2011-06-11 ms
+	 * @return string
 	 */
 	public static function buildHash($data, $options, $init = false) {
 		if ($init) {
@@ -45,7 +46,8 @@ class CaptchaLib {
 		if (empty($options['type']) || $options['type'] !== 'passive') {
 			$hashValue .= $data['captcha'];
 		}
-		return Security::hash($hashValue, isset($options['hashType']) ? $options['hashType'] : null, $options['salt']);
+		$type = isset($options['hashType']) ? $options['hashType'] : null;
+		return Security::hash($hashValue, $type, $options['salt']);
 	}
 
 }

+ 14 - 12
Lib/Utility/TimeLib.php

@@ -868,18 +868,18 @@ class TimeLib extends CakeTime {
 			$p = $s;
 		} else {
 			$s = array(
-		'm' => ' '.__('Month'), # translated
-				'd' => ' '.__('Day'),
-				'h' => ' '.__('Hour'),
-				'i' => ' '.__('Minute'),
-				's' => ' '.__('Second'),
+		'm' => ' ' . __('Month'), # translated
+				'd' => ' ' .__('Day'),
+				'h' => ' ' .__('Hour'),
+				'i' => ' ' .__('Minute'),
+				's' => ' ' .__('Second'),
 			);
-			$p = array (
-		'm' => ' '.__('Months'), # translated
-				'd' => ' '.__('Days'),
-				'h' => ' '.__('Hours'),
-				'i' => ' '.__('Minutes'),
-				's' => ' '.__('Seconds'),
+			$p = array(
+		'm' => ' ' . __('Months'), # translated
+				'd' => ' ' . __('Days'),
+				'h' => ' ' . __('Hours'),
+				'i' => ' ' . __('Minutes'),
+				's' => ' ' . __('Seconds'),
 			);
 		}
 
@@ -969,7 +969,9 @@ class TimeLib extends CakeTime {
 			$type = 0;
 		}
 
-		$defaults = array('verbose'=>__('justNow'), 'zero'=>false,'separator'=>', ', 'future'=>__('In %s'), 'past'=>__('%s ago'),'default'=>'');
+		$defaults = array(
+			'verbose' => __('justNow'), 'zero' => false, 'separator' => ', ',
+			'future' => __('In %s'), 'past' => __('%s ago'), 'default' => '');
 		$options = array_merge($defaults, $options);
 
 		$ret = self::lengthOfTime($sec, $format, $options);

+ 17 - 41
Lib/Utility/Utility.php

@@ -32,6 +32,9 @@ class Utility {
 	 * Multibyte analogue of preg_match_all() function. Only that this returns the result.
 	 * By default this works properly with UTF8 strings.
 	 *
+	 * Do not forget to use preg_quote() first on strings that could potentially contain
+	 * unescaped characters.
+	 *
 	 * Note that you still need to add the u modifier (for UTF8) to your pattern yourself.
 	 *
 	 * Example: /some(.*)pattern/u
@@ -52,6 +55,9 @@ class Utility {
 	 * Multibyte analogue of preg_match() function. Only that this returns the result.
 	 * By default this works properly with UTF8 strings.
 	 *
+	 * Do not forget to use preg_quote() first on strings that could potentially contain
+	 * unescaped characters.
+	 *
 	 * Note that you still need to add the u modifier (for UTF8) to your pattern yourself.
 	 *
 	 * Example: /some(.*)pattern/u
@@ -90,44 +96,13 @@ class Utility {
 	}
 
 	/**
-	 * Will escape a string to be used as a regular expression pattern.
-	 *
-	 * - Escapes the following
-	 *   - \ ^ . $ | ( ) [ ] * + ? { } ,
-	 *
-	 * - Example
-	 *   - Utility::patternEscape('http://www.example.com/s?q=php.net+docs')
-	 *   - http:\/\/www\.example\.com\/s\?q=php\.net\+docs
+	 * Get the current IP address.
 	 *
-	 * @see http://www.php.net/manual/en/function.preg-replace.php#92456
-	 * @author alammar at gmail dot com
-	 *
-	 * @param string $str the stuff you want escaped
-	 * @return string the escaped string
-	 */
-	public static function patternEscape($str) {
-		$patterns = array(
-			'/\//', '/\^/', '/\./', '/\$/', '/\|/',
-			'/\(/', '/\)/', '/\[/', '/\]/', '/\*/',
-			'/\+/', '/\?/', '/\{/', '/\}/', '/\,/'
-		);
-
-		$replace = array('\/', '\^', '\.', '\$', '\|', '\(', '\)',
-		'\[', '\]', '\*', '\+', '\?', '\{', '\}', '\,');
-
-		return preg_replace($patterns, $replace, $str);
-	}
-
-	/**
-	 * get the current ip address
 	 * @param bool $safe
-	 * @return string $ip
+	 * @return string IP address
 	 * 2011-11-02 ms
 	 */
-	public static function getClientIp($safe = null) {
-		if ($safe === null) {
-			$safe = false;
-		}
+	public static function getClientIp($safe = true) {
 		if (!$safe && env('HTTP_X_FORWARDED_FOR')) {
 			$ipaddr = preg_replace('/(?:,.*)/', '', env('HTTP_X_FORWARDED_FOR'));
 		} else {
@@ -149,7 +124,8 @@ class Utility {
 	}
 
 	/**
-	 * get the current referer
+	 * Get the current referrer if available.
+	 *
 	 * @param bool $full (defaults to false and leaves the url untouched)
 	 * @return string $referer (local or foreign)
 	 * 2011-11-02 ms
@@ -164,13 +140,13 @@ class Utility {
 			return $ref;
 		}
 		if ($full) {
-			$ref = Router::url($full);
+			$ref = Router::url($ref, $full);
 		}
 		return $ref;
 	}
 
 	/**
-	 * remove unnessary stuff + add http:// for external urls
+	 * Remove unnessary stuff + add http:// for external urls
 	 * TODO: protocol to lower!
 	 *
 	 * @param string $url
@@ -208,7 +184,7 @@ class Utility {
 	}
 
 	/**
-	 * Parse headers
+	 * Parse headers from a specific URL content.
 	 *
 	 * @param string $url
 	 * @return mixed array of headers or FALSE on failure
@@ -240,7 +216,7 @@ class Utility {
 	}
 
 	/**
-	 * add protocol prefix if necessary (and possible)
+	 * Add protocol prefix if necessary (and possible)
 	 *
 	 * @param string $url
 	 * 2010-06-02 ms
@@ -293,7 +269,7 @@ class Utility {
 	 * //TODO: maybe move to bootstrap?
 	 *
 	 * @param array $array
-	 * @return bool $result
+	 * @return bool Result
 	 * 2011-11-02 ms
 	 */
 	public static function logicalAnd($array) {
@@ -313,7 +289,7 @@ class Utility {
 	 * //TODO: maybe move to bootstrap?
 	 *
 	 * @param array $array
-	 * @return bool $result
+	 * @return bool Result
 	 *
 	 * 2011-11-02 ms
 	 */

+ 22 - 5
Test/Case/Lib/Utility/UtilityTest.php

@@ -71,13 +71,24 @@ class UtilityTest extends MyCakeTestCase {
 	}
 
 	/**
-	 * UtilityTest::testPatternEscape()
+	 * UtilityTest::testPregMatchWithPatternEscape()
 	 *
 	 * @return void
 	 */
-	public function testPatternEscape() {
-		$res = Utility::patternEscape('http://www.example.com/s?q=php.net+docs');
-		$this->assertSame('http:\/\/www\.example\.com\/s\?q=php\.net\+docs', $res);
+	public function testPregMatchWithPatternEscape() {
+		$string = 'http://www.example.com/s?q=php.net+docs';
+		$res = preg_quote($string);
+		$this->assertSame('http\://www\.example\.com/s\?q\=php\.net\+docs', $res);
+
+		$string = 'http://www.example.com/s?q=php.net+docs';
+		$res = preg_quote($string, '/');
+		$this->assertSame('http\:\/\/www\.example\.com\/s\?q\=php\.net\+docs', $res);
+
+		$res = '/a\s*' . $res . '\s*z/i';
+		$string = 'a ' . $string . ' z';
+		$matches = Utility::pregMatch($res, $string);
+		$expected = array($string);
+		$this->assertSame($expected, $matches);
 	}
 
 	/**
@@ -165,8 +176,14 @@ class UtilityTest extends MyCakeTestCase {
 	 */
 	public function testGetReferer() {
 		$res = Utility::getReferer();
-		//$this->assertTrue(env(''), $res);
 		$this->assertEquals(env('HTTP_REFERER'), $res);
+
+		$res = Utility::getReferer(true);
+		$this->assertEquals(env('HTTP_REFERER'), $res);
+
+		$_SERVER['HTTP_REFERER'] = '/foo/bar';
+		$res = Utility::getReferer(true);
+		$this->assertEquals(HTTP_BASE . env('HTTP_REFERER'), $res);
 	}
 
 	/**

+ 3 - 3
Test/Case/Model/Behavior/LinkableBehaviorTest.php

@@ -39,7 +39,7 @@ class LinkableBehaviorTest extends CakeTestCase {
 	public function testBelongsTo() {
 		$arrayExpected = array(
 			'LinkableUser' => array('id' => 1, 'username' => 'CakePHP'),
-			'LinkableProfile' => array ('id' => 1, 'user_id' => 1, 'biography' => 'CakePHP is a rapid development framework for PHP that provides an extensible architecture for developing, maintaining, and deploying applications.')
+			'LinkableProfile' => array('id' => 1, 'user_id' => 1, 'biography' => 'CakePHP is a rapid development framework for PHP that provides an extensible architecture for developing, maintaining, and deploying applications.')
 		);
 
 		$arrayResult = $this->User->find('first', array(
@@ -327,13 +327,13 @@ class LinkableBehaviorTest extends CakeTestCase {
 				'LinkableUser' => array(
 					'id' => 4
 				),
-				'LinkableProfile' => array ('user_id' => 4)
+				'LinkableProfile' => array('user_id' => 4)
 			),
 			1 => array(
 				'LinkableUser' => array(
 					'id' => 3
 				),
-				'LinkableProfile' => array ('user_id' => 3)
+				'LinkableProfile' => array('user_id' => 3)
 			)
 		);
 

+ 1 - 1
View/Helper/CommonHelper.php

@@ -6,7 +6,7 @@ App::uses('AppHelper', 'View/Helper');
  */
 class CommonHelper extends AppHelper {
 
-	public $helpers = array ('Session', 'Html');
+	public $helpers = array('Session', 'Html');
 
 	public $packages = array(
 		'Tools.Jquery' //Used by showDebug

+ 17 - 19
View/Helper/HcardHelper.php

@@ -9,26 +9,25 @@ App::uses('AppHelper', 'View/Helper');
  */
 class HcardHelper extends AppHelper {
 
-	public $data = array (
-	'given_name' => 'Firstname',
-	'middle_name' => 'Middlename',
-	'family_name' => 'Lastname',
-	'organization' => 'OrganizationName',
-	'street' => '123 Street',
-	'city' => 'City',
-	'province' => 'Province/State',
-	'postal_code' => 'Postal/Zip',
-	'country' => 'Country',
-	'phone' => 'phonenumber',
-	'email' => 'email@yoursite.com',
-	'url' => 'http://yoursite.com',
-	'aim_screenname' => 'aimname',
-	'yim_screenname' => 'yimname',
-	'avatar' => '/images/your_photo.png',
-	'title' => 'title',
+	public $data = array(
+		'given_name' => 'Firstname',
+		'middle_name' => 'Middlename',
+		'family_name' => 'Lastname',
+		'organization' => 'OrganizationName',
+		'street' => '123 Street',
+		'city' => 'City',
+		'province' => 'Province/State',
+		'postal_code' => 'Postal/Zip',
+		'country' => 'Country',
+		'phone' => 'phonenumber',
+		'email' => 'email@yoursite.com',
+		'url' => 'http://yoursite.com',
+		'aim_screenname' => 'aimname',
+		'yim_screenname' => 'yimname',
+		'avatar' => '/images/your_photo.png',
+		'title' => 'title',
 	);
 
-
 	/**
 	 * TODO
 	 */
@@ -82,7 +81,6 @@ class HcardHelper extends AppHelper {
 
 	}
 
-
 	/**
 	 * TODO
 	 */