[Pkg-owncloud-commits] [owncloud] 46/104: Added isUserAgent() method to request

David Prévot taffit at moszumanska.debian.org
Sat Jan 18 13:33:39 UTC 2014


This is an automated email from the git hooks/post-receive script.

taffit pushed a commit to branch master
in repository owncloud.

commit 2ea37653599d434491953dd00813d09de0ae2578
Author: Vincent Petry <pvince81 at owncloud.com>
Date:   Thu Dec 12 11:32:56 2013 +0100

    Added isUserAgent() method to request
    
    - added isUserAgent() method to OC_Request which makes it possible to
      test it
    - OC_Response::setContentDisposition now uses OC_Request::isUserAgent()
---
 lib/private/request.php  | 23 ++++++++++++++++++++++
 lib/private/response.php |  3 +--
 tests/lib/request.php    | 50 ++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 74 insertions(+), 2 deletions(-)

diff --git a/lib/private/request.php b/lib/private/request.php
index b2afda3..d9d5ae0 100755
--- a/lib/private/request.php
+++ b/lib/private/request.php
@@ -7,6 +7,11 @@
  */
 
 class OC_Request {
+
+	const USER_AGENT_IE = '/MSIE/';
+	// Android Chrome user agent: https://developers.google.com/chrome/mobile/docs/user-agent
+	const USER_AGENT_ANDROID_MOBILE_CHROME = '#Android.*Chrome/[.0-9]*#';
+
 	/**
 	 * @brief Check overwrite condition
 	 * @param string $type
@@ -210,4 +215,22 @@ class OC_Request {
 			return false;
 		}
 	}
+
+	/**
+	 * Checks whether the user agent matches a given regex
+	 * @param string|array $agent agent name or array of agent names
+	 * @return boolean true if at least one of the given agent matches,
+	 * false otherwise
+	 */
+	static public function isUserAgent($agent) {
+		if (!is_array($agent)) {
+			$agent = array($agent);
+		}
+		foreach ($agent as $regex) {
+			if (preg_match($regex, $_SERVER['HTTP_USER_AGENT'])) {
+				return true;
+			}
+		}
+		return false;
+	}
 }
diff --git a/lib/private/response.php b/lib/private/response.php
index c6edda0..0474643 100644
--- a/lib/private/response.php
+++ b/lib/private/response.php
@@ -153,8 +153,7 @@ class OC_Response {
 	 * @param string $type disposition type, either 'attachment' or 'inline'
 	 */
 	static public function setContentDispositionHeader( $filename, $type = 'attachment' ) {
-		// Android Chrome user agent: https://developers.google.com/chrome/mobile/docs/user-agent
-		if ( preg_match( '/MSIE/', $_SERVER['HTTP_USER_AGENT'] ) or preg_match( '#Android.*Chrome/[.0-9]*#', $_SERVER['HTTP_USER_AGENT'] ) ) {
+		if (OC_Request::isUserAgent(array(OC_Request::USER_AGENT_IE, OC_Request::USER_AGENT_ANDROID_MOBILE_CHROME))) {
 			header( 'Content-Disposition: ' . rawurlencode($type) . '; filename="' . rawurlencode( $filename ) . '"' );
 		} else {
 			header( 'Content-Disposition: ' . rawurlencode($type) . '; filename*=UTF-8\'\'' . rawurlencode( $filename )
diff --git a/tests/lib/request.php b/tests/lib/request.php
index 090cebc..c6401a5 100644
--- a/tests/lib/request.php
+++ b/tests/lib/request.php
@@ -70,4 +70,54 @@ class Test_Request extends PHPUnit_Framework_TestCase {
 			array('/oc/core1', '/oc/core/index.php'),
 		);
 	}
+
+	/**
+	 * @dataProvider userAgentProvider
+	 */
+	public function testUserAgent($testAgent, $userAgent, $matches) {
+		$_SERVER['HTTP_USER_AGENT'] = $testAgent;
+		$this->assertEquals($matches, OC_Request::isUserAgent($userAgent));
+	}
+
+	function userAgentProvider() {
+		return array(
+			array(
+				'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)',
+				OC_Request::USER_AGENT_IE,
+				true
+			),
+			array(
+				'Mozilla/5.0 (X11; Linux i686; rv:24.0) Gecko/20100101 Firefox/24.0',
+				OC_Request::USER_AGENT_IE,
+				false
+			),
+			array(
+				'Mozilla/5.0 (Linux; Android 4.4; Nexus 4 Build/KRT16S) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.59 Mobile Safari/537.36',
+				OC_Request::USER_AGENT_ANDROID_MOBILE_CHROME,
+				true
+			),
+			array(
+				'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)',
+				OC_Request::USER_AGENT_ANDROID_MOBILE_CHROME,
+				false
+			),
+			// test two values
+			array(
+				'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)',
+				array(
+					OC_Request::USER_AGENT_IE,
+					OC_Request::USER_AGENT_ANDROID_MOBILE_CHROME,
+				),
+				true
+			),
+			array(
+				'Mozilla/5.0 (Linux; Android 4.4; Nexus 4 Build/KRT16S) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.59 Mobile Safari/537.36',
+				array(
+					OC_Request::USER_AGENT_IE,
+					OC_Request::USER_AGENT_ANDROID_MOBILE_CHROME,
+				),
+				true
+			),
+		);
+	}
 }

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-owncloud/owncloud.git



More information about the Pkg-owncloud-commits mailing list