git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@1207 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
56 lines
1.8 KiB
PHP
56 lines
1.8 KiB
PHP
<?php
|
|
class http
|
|
{
|
|
/** Choose the best choice from user choices.
|
|
Can be used for languages (HTTP_ACCEPT_LANGUAGE), type of pages
|
|
(HTTP_ACCEPT)...
|
|
Ex. fr, en-gb;q=0.8, en;q=0.7
|
|
Ex. text/html,application/xhtml+xml,application/xml;q=0.9,* /*;q=0.8
|
|
If available is empty, then return the best priority defined by user,
|
|
and throw an exception if nothing is provided for by the user.
|
|
If nothing match, return $default
|
|
*/
|
|
function bestChoice ($uservar, $available=array(), $default=FALSE)
|
|
{
|
|
$uservar = str_replace (" ", "", $uservar);
|
|
$userchoices = explode (",", $uservar);
|
|
if (!isset ($userchoices[0]))
|
|
{
|
|
if ($default === FALSE)
|
|
throw new Exception ("No user choice provided to bestChoice", 500);
|
|
return $default;
|
|
}
|
|
// Remove weights (provided by the order of the user)
|
|
// TODO : Reorganise the order if the weights are not in the right order
|
|
foreach ($userchoices as $key=>$val)
|
|
{
|
|
$vals = @explode (";q", $val);
|
|
$userchoices[$key] = $vals[0];
|
|
}
|
|
|
|
if (count ($available) === 0)
|
|
return $userchoices[0];
|
|
|
|
// Look at the best existing solution
|
|
foreach ($userchoices as $choice)
|
|
{
|
|
foreach ($available as $avail)
|
|
{
|
|
if (strtolower ($avail) === strtolower ($choice))
|
|
return $avail;
|
|
// Case en_US
|
|
$availTmp = str_replace ("_", "-", $avail);
|
|
if (strtolower ($availTmp) === strtolower ($choice))
|
|
return $avail;
|
|
// Case text/xml, application/csv (just compare xml or csv)
|
|
$mimes = explode ("/", $choice);
|
|
if (isset ($mimes[1]) && strtolower ($availTmp) === $mimes[1])
|
|
return $avail;
|
|
}
|
|
}
|
|
|
|
// No best solution found. Use the default available solution
|
|
return $default;
|
|
}
|
|
}
|