[ Index ]

PHP Cross Reference of Akelos Framework

title

Body

[close]

/ -> AkRequestMimeType.php (source)

   1  <?php
   2  
   3  class AkRequestMimeType extends AkObject
   4  {
   5      
   6      function _lookupMimeType($type = null)
   7      {
   8          static $mime_types = array( 
   9                  'text/html'                => 'html', 
  10                  'application/xhtml+xml'    => 'html', 
  11                  'application/xml'          => 'xml', 
  12                  'text/xml'                 => 'xml', 
  13                  'text/javascript'          => 'js', 
  14                  'application/javascript'   => 'js', 
  15                  'application/x-javascript' => 'js', 
  16                  'application/json'         => 'json',
  17                  'text/x-json'              => 'json', 
  18                  'application/rss+xml'      => 'rss', 
  19                  'application/atom+xml'     => 'atom', 
  20                  '*/*'                      => 'html', 
  21                  //'application/x-www-form-urlencoded' => 'www-form', 
  22                  //'application/x-www-form-urlencoded' => 'www-form',
  23                  'default'                  => 'html', 
  24              );
  25              
  26              if ($type === null) {
  27                  return $mime_types;
  28              } else {
  29                  return isset($mime_types[$type])?$mime_types[$type]:null;
  30              }
  31      }
  32      function getAccepts()
  33      {
  34          $accept_header = isset($_SERVER['HTTP_ACCEPT'])?$_SERVER['HTTP_ACCEPT']:'';
  35          $accepts = array();
  36          foreach (explode(',',$accept_header) as $index=>$acceptable){ 
  37                   $mime_struct = AkRequestMimeType::_parseMimeType($acceptable); 
  38                   if (empty($mime_struct['q'])) $mime_struct['q'] = '1.0'; 
  39                    
  40                   //we need the original index inside this structure  
  41                   //because usort happily rearranges the array on equality 
  42                   //therefore we first compare the 'q' and then 'i' 
  43                   $mime_struct['i'] = $index; 
  44                   $accepts[] = $mime_struct; 
  45               } 
  46               usort($accepts,array('AkRequestMimeType','_sortAcceptHeader')); 
  47                
  48               //we throw away the old index 
  49               foreach ($accepts as $array){ 
  50                   unset($array['i']); 
  51               } 
  52               return $accepts; 
  53      }
  54      function getMethod()
  55      {
  56          return strtolower(isset($_SERVER['REQUEST_METHOD'])?$_SERVER['REQUEST_METHOD']:'get');
  57      }
  58      function getFormat($requestPath)
  59      {
  60          $method = AkRequestMimeType::getMethod();
  61          $format = AkRequestMimeType::_lookupMimeType('default');
  62  
  63          if (preg_match('/^([^\.]+)\.(.+)$/', $requestPath, $matches)) {
  64              $format = isset($matches[2])?strtolower($matches[2]):null;
  65              $orgformat = $format;
  66              if ($format == 'htm') {
  67                  $format = 'html';
  68              }
  69              $requestPath = preg_replace('/^(.*)\.'.$orgformat.'$/','\1',$requestPath);
  70          } else if ($method=='get' || $method == 'delete') {
  71              $format = AkRequestMimeType::_bestMimeType();
  72          } else if ($method=='post' || $method == 'put') {
  73              $format = AkRequestMimeType::_lookupMimeType(AkRequestMimeType::getContentType());
  74          }
  75          
  76          if (empty($format)) {
  77              $format = AkRequestMimeType::_lookupMimeType('default');
  78              
  79          }
  80          return array($format, $requestPath);
  81      }
  82      
  83      function _sortAcceptHeader($a,$b) 
  84      { 
  85           //preserve the original order if q is equal 
  86          return $a['q'] == $b['q'] ? ($a['i'] > $b['i']) : ($a['q'] < $b['q']); 
  87      } 
  88      function _parseMimeType($mime_type) 
  89      { 
  90          @list($type,$parameter_string) = explode(';',$mime_type); 
  91          $mime_type_struct = array(); 
  92          if ($parameter_string){ 
  93              parse_str($parameter_string,$mime_type_struct); 
  94          } 
  95          $mime_type_struct['type'] = trim($type); 
  96          return $mime_type_struct; 
  97      }
  98      function _getMimeType($acceptables)
  99      { 
 100          // we group by 'quality' 
 101          $grouped_acceptables = array(); 
 102          foreach ($acceptables as $acceptable){ 
 103              $grouped_acceptables[$acceptable['q']][] = $acceptable['type']; 
 104          } 
 105           
 106           
 107          foreach ($grouped_acceptables as $q=>$array_with_acceptables_of_same_quality){ 
 108              foreach (AkRequestMimeType::_lookupMimeType() as $mime_type=>$our_mime_type){ 
 109                  foreach ($array_with_acceptables_of_same_quality as $acceptable){ 
 110                      if ($mime_type == $acceptable){ 
 111                          return $our_mime_type; 
 112                      } 
 113                  } 
 114              } 
 115          } 
 116          return AkRequestMimeType::_lookupMimeType('default'); 
 117      } 
 118      function getContentType() 
 119      { 
 120          if (empty($_SERVER['CONTENT_TYPE'])) return false; 
 121          $mime_type_struct = AkRequestMimeType::_parseMimeType($_SERVER['CONTENT_TYPE']); 
 122          return $mime_type_struct['type']; 
 123      } 
 124   
 125      function _bestMimeType() 
 126      { 
 127          return AkRequestMimeType::_getMimeType(AkRequestMimeType::getAccepts()); 
 128      } 
 129  }
 130  ?>


Generated: Mon Oct 27 12:43:49 2008 Cross-referenced by PHPXref 0.6