Cleanup r70571, more strict checks for getCookie() return values
[lhc/web/wiklou.git] / includes / WebRequest.php
index 68a5cac..e39a576 100644 (file)
 # http://www.gnu.org/copyleft/gpl.html
 
 
-/**
- * Some entry points may use this file without first enabling the
- * autoloader.
- */
-if ( !function_exists( '__autoload' ) ) {
-       require_once( dirname(__FILE__) . '/normal/UtfNormal.php' );
-}
-
 /**
  * The WebRequest class encapsulates getting at data passed in the
  * URL or via a POSTed form, handling remove of "magic quotes" slashes,
@@ -435,19 +427,19 @@ class WebRequest {
         * @return Boolean
         */
        public function checkSessionCookie() {
-               return isset( $_COOKIE[session_name()] );
+               return isset( $_COOKIE[ session_name() ] );
        }
 
        /**
         * Get a cookie from the $_COOKIE jar
         *
         * @param $key String: the name of the cookie
-        * @param $default Mixed: what to return if the value isn't found
         * @param $prefix String: a prefix to use for the cookie name, if not $wgCookiePrefix
+        * @param $default Mixed: what to return if the value isn't found
         * @return Mixed: cookie value or $default if the cookie not set
         */
-       public function getCookie( $key, $default = null, $prefix = '' ) {
-               if( !$prefix ) {
+       public function getCookie( $key, $prefix = null, $default = null ) {
+               if( $prefix === null ) {
                        global $wgCookiePrefix;
                        $prefix = $wgCookiePrefix;
                }
@@ -738,29 +730,39 @@ class WebRequest {
        /**
         * Parse the Accept-Language header sent by the client into an array
         * @return array( languageCode => q-value ) sorted by q-value in descending order
+        * May contain the "language" '*', which applies to languages other than those explicitly listed.
+        * This is aligned with rfc2616 section 14.4
         */
        public function getAcceptLang() {
                // Modified version of code found at http://www.thefutureoftheweb.com/blog/use-accept-language-header
-               if ( !isset( $_SERVER['HTTP_ACCEPT_LANGUAGE'] ) ) {
+               $acceptLang = $this->getHeader( 'Accept-Language' );
+               if ( !$acceptLang ) {
                        return array();
                }
                
+               // Return the language codes in lower case
+               $acceptLang = strtolower( $acceptLang );
+               
                // Break up string into pieces (languages and q factors)
                $lang_parse = null;
-               preg_match_all( '/([a-z]{1,8}(-[a-z]{1,8})?)\s*(;\s*q\s*=\s*(1|0(\.[0-9]+))?)?/i',
-                       $_SERVER['HTTP_ACCEPT_LANGUAGE'], $lang_parse );
+               preg_match_all( '/([a-z]{1,8}(-[a-z]{1,8})?|\*)\s*(;\s*q\s*=\s*(1|0(\.[0-9]+)?)?)?/',
+                       $acceptLang, $lang_parse );
                
                if ( !count( $lang_parse[1] ) ) {
                        return array();
                }
+
                // Create a list like "en" => 0.8
                $langs = array_combine( $lang_parse[1], $lang_parse[4] );
                // Set default q factor to 1
                foreach ( $langs as $lang => $val ) {
                        if ( $val === '' ) {
                                $langs[$lang] = 1;
+                       } else if ( $val == 0 ) {
+                               unset($langs[$lang]);
                        }
                }
+
                // Sort list
                arsort( $langs, SORT_NUMERIC );
                return $langs;