Changeset 2506

Show
Ignore:
Timestamp:
04/15/2008 01:59:35 PM (7 months ago)
Author:
JAAulde
Message:

fixes for #554 and #557

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/system/helpers/request.php

    r2427 r2506  
    8989         * @return  booleanean 
    9090         */ 
    91          public static function accepts ($type = NULL) 
     91        public static function accepts ($type = NULL) 
    9292         { 
    93                  if (self::$accept_types === NULL) 
    94                  { 
    95                         self::$accept_types = explode(',', $_SERVER['HTTP_ACCEPT']); 
     93                self::parse_accept_header(); 
    9694 
    97                         foreach (self::$accept_types as $key => $accept_type) 
     95                if ($type === NULL) 
     96                        return self::$accept_types; 
     97 
     98                if (is_string($type)) 
     99                { 
     100                        $type = strtolower($type); 
     101                        if(strstr($type,'/') !== false) 
    98102                        { 
    99                                 if (strpos($accept_type, ';')) 
     103                                list($mime_major,$mime_minor) = explode('/',$type); 
     104 
     105                                if(isset(self::$accept_types[$mime_major][$mime_minor])) 
     106                                        if (self::$accept_types[$mime_major][$mime_minor] > 0) 
     107                                                return true; 
     108                                        else 
     109                                                return false; 
     110 
     111                                if(isset(self::$accept_types[$mime_major]['*'])) 
     112                                        if (self::$accept_types[$mime_major]['*'] > 0) 
     113                                                return true; 
     114                                        else 
     115                                                return false; 
     116                        } 
     117                        else 
     118                        {        
     119                                $mapped_mime_types = Config::item('mimes.'.$type); 
     120                                if(is_array($mapped_mime_types)) 
    100121                                { 
    101                                         $accept_type = explode(';', $accept_type); 
    102                                         self::$accept_types[$key] = strtolower($accept_type[0]); 
     122                                        foreach ($mapped_mime_types as $type) 
     123                                        { 
     124                                                if (self::accepts($type)===true) 
     125                                                        return  true; 
     126                                        } 
    103127                                } 
    104128                        } 
    105                  } 
    106129 
    107                 if ($type === NULL) 
    108                 { 
    109                         return self::$accept_types; 
     130                        if (isset(self::$accept_types['*'])) 
     131                                if (isset(self::$accept_types['*']['*'])) 
     132                                        if (self::$accept_types['*']['*'] > 0) 
     133                                                return true; 
     134                                        else 
     135                                                return false; 
    110136                } 
    111                 elseif (is_string($type)) 
    112                 { 
    113                         $type = strtolower($type); 
     137                 
     138                return false; 
     139        } 
    114140 
    115                         // If client only accepts */*, then assume default HTML browser 
    116                         if ($type === 'html' AND self::$accept_types === array('*/*')) 
    117                                 return FALSE; 
     141        protected static function parse_accept_header() 
     142        { 
     143                if (self::$accept_types === NULL) 
     144                 { 
     145                        self::$accept_types = array(); 
     146                        if (isset($_SERVER['HTTP_ACCEPT']) && !empty($_SERVER['HTTP_ACCEPT'])) 
     147                        { 
     148                                $accept_entries = explode(',',$_SERVER['HTTP_ACCEPT']); 
     149                                foreach ($accept_entries as $accept_entry) 
     150                                { 
     151                                        $parameters_separated = explode(';', $accept_entry); 
     152                                        list($mime_major, $mime_minor) = explode('/',$parameters_separated[0],2); 
     153                                        $q = 1000; 
     154                                        if (isset($parameters_separated[1]) && !empty($parameters_separated[1]) && substr($parameters_separated[1],0,1)==='q') 
     155                                        { 
     156                                                list(,$q) = explode('=',$parameters_separated[1],2); 
     157                                                $q = (integer) ($q * 1000); 
     158                                        } 
    118159 
    119                         if ( ! in_array($type, array_keys(self::$accept_types))) 
    120                                 return FALSE; 
     160                                        if (!isset(self::$accept_types[$mime_major])) 
     161                                                self::$accept_types[$mime_major] = array(); 
    121162 
    122                         $accept_types = Config::item('mimes.'.$type); 
    123  
    124                         if (is_array($accept_types)) 
    125                         { 
    126                                 foreach ($accept_types as $type) 
    127                                 { 
    128                                         if (in_array($type, self::$accept_types)) 
    129                                                 return FALSE; 
     163                                        if (!isset(self::$accept_types[$mime_major][$mime_minor]) || $q > self::$accept_types[$mime_major][$mime_minor]) 
     164                                                self::$accept_types[$mime_major][$mime_minor] = $q; 
    130165                                } 
    131166                        } 
    132167                        else 
    133168                        { 
    134                                 if (in_array($accept_types, self::$accept_types)) 
    135                                         return FALSE; 
     169                                self::$accept_types['*'] = array('*' => 1000); 
    136170                        } 
    137  
    138                         return FALSE; 
    139171                } 
    140172        } 
    141  
     173         
    142174} // End request