Skip to content

Instantly share code, notes, and snippets.

@blongden
Created October 8, 2011 14:03
Show Gist options
  • Save blongden/1272322 to your computer and use it in GitHub Desktop.
Save blongden/1272322 to your computer and use it in GitHub Desktop.
class Fdrop_Plugin_AcceptHandler extends Zend_Controller_Plugin_Abstract
{
protected function getQuality($media)
{
$data = explode(";q=", $media);
return isset($data[1]) ? $data[1] : 1.0;
}
protected function sortMediaByQuality($a, $b)
{
$q1 = $this->getQuality($a);
$q2 = $this->getQuality($b);
if ($q1 > $q2) {
return -1;
} elseif ($q1 < $q2) {
return 1;
}
return 0;
}
public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request)
{
$this->getResponse()->setHeader('Vary', 'Accept');
$header = $request->getHeader('Accept');
$accepts = explode(',', $header);
usort($accepts, array($this, 'sortMediaByQuality'));
foreach($accepts as $accept) {
if (substr($accept, 0, 9) == 'text/html') {
break;
} else if(substr($accept, 0, 31) == 'application/vnd.fdrop.xhtml+xml') {
$request->setParam('format', 'xhtml');
break;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment