pastebin - collaborative debugging

pastebin is a collaborative debugging tool allowing you to share and modify code snippets while chatting on IRC, IM or a message board.

This site is developed to XHTML and CSS2 W3C standards. If you see this paragraph, your browser does not support those standards and you need to upgrade. Visit WaSP for a variety of options.

PHP pastebin - collaborative debugging tool View Help


Posted by Ignace Knops on Sat 6 Jun 11:42
report abuse | View followups from Ignace Knops | download | new post

  1. <?php
  2.  
  3. /**
  4.  * This program is free software: you can redistribute it and/or modify
  5.  * it under the terms of the GNU General Public License as published by
  6.  * the Free Software Foundation, either version 3 of the License, or
  7.  * (at your option) any later version.
  8.  *
  9.  * This program is distributed in the hope that it will be useful,
  10.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.  * GNU General Public License for more details.
  13.  *
  14.  * You should have received a copy of the GNU General Public License
  15.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  16.  *
  17.  * @author Ignace Knops <developer.ignace@gmail.com>
  18.  * @license http://www.gnu.org/licenses/gpl.txt GNU GPL v3
  19.  */
  20. class ContentNegotiation
  21. {
  22.     /**
  23.      *
  24.      * @var <array>
  25.      */
  26.     protected $_mimeTypes = array();
  27.  
  28.     /**
  29.      *
  30.      * @var <array>
  31.      */
  32.     protected $_languages = array();
  33.  
  34.     /**
  35.      *
  36.      * @var <array>
  37.      */
  38.     protected $_encodings = array();
  39.  
  40.     /**
  41.      *
  42.      * @var <array>
  43.      */
  44.     protected $_charsets = array();
  45.  
  46.     /**
  47.      * Class constructor
  48.      */
  49.     public function __construct() {
  50.         $this->_parseAcceptHeader();
  51.         $this->_parseAcceptLanguageHeader();
  52.         $this->_parseAcceptEncodingHeader();
  53.         $this->_parseAcceptCharsetHeader();
  54.     }
  55.  
  56.     /**
  57.      *
  58.      * @return <array>
  59.      */
  60.     public function getMimeTypes() {
  61.         return $this->_mimeTypes;
  62.     }
  63.  
  64.     /**
  65.      *
  66.      * @return <array>
  67.      */
  68.     public function getLanguages() {
  69.         return $this->_languages;
  70.     }
  71.  
  72.     /**
  73.      *
  74.      * @return <array>
  75.      */
  76.     public function getEncodings() {
  77.         return $this->_encodings;
  78.     }
  79.  
  80.     /**
  81.      *
  82.      * @return <array>
  83.      */
  84.     public function getCharsets() {
  85.         return $this->_charsets;
  86.     }
  87.  
  88.     /**
  89.      * Returns the optimal mime-type
  90.      *
  91.      * @param <string> $mimeTypesString
  92.      * @return <string>
  93.      */
  94.     public function returnOptimalMimeType($mimeTypesString) {
  95.         return $this->_returnOptimal($mimeTypesString, $this->getMimeTypes());
  96.     }
  97.  
  98.     /**
  99.      * Returns the optimal language
  100.      *
  101.      * @param <string> $languagesString
  102.      * @return <string>
  103.      */
  104.     public function returnOptimalLanguage($languagesString) {
  105.         return $this->_returnOptimal($languagesString, $this->getLanguages());
  106.     }
  107.  
  108.     /**
  109.      * Returns the optimal encoding
  110.      *
  111.      * @param <string> $encodingsString
  112.      * @return <string>
  113.      */
  114.     public function returnOptimalEncoding($encodingsString) {
  115.         return $this->_returnOptimal($encodingsString, $this->getEncodings());
  116.     }
  117.  
  118.     /**
  119.      * Returns the optimal charset
  120.      *
  121.      * @param <string> $charsetsString
  122.      * @return <string>
  123.      */
  124.     public function returnOptimalCharset($charsetsString) {
  125.         return $this->_returnOptimal($charsetsString, $this->getCharsets());
  126.     }
  127.  
  128.     /**
  129.      * Returns the item with the best quality value
  130.      *
  131.      * @param <string> $str
  132.      * @param <array> $array
  133.      * @return <string>
  134.      */
  135.     protected function _returnOptimal($str, $array) {
  136.         $optimalItem = '';
  137.         foreach (explode(',', $str) as $item) {
  138.             if (empty($optimalItem)) {
  139.                 $optimalItem = $item;
  140.                 continue;
  141.             }
  142.             $q1 = $this->_getQuality($item, $array);
  143.             $q2 = $this->_getQuality($optimalItem, $array);
  144.             if ($this->_compare($q1, $q2) < 0) {
  145.                 $optimalItem = $item;
  146.             }
  147.         }
  148.         return $optimalItem;
  149.     }
  150.  
  151.     /**
  152.      * Returns -1 if $mt1 is greater, 0 if they are equal and 1 if $mt2 is greater
  153.      *
  154.      * @param <string> $mt1
  155.      * @param <string> $mt2
  156.      */
  157.     public function compareMimeTypeQuality($mt1, $mt2) {
  158.         $q1 = $this->_getMimeTypeQuality($mt1);
  159.         $q2 = $this->_getMimeTypeQuality($mt2);
  160.         return $this->_compare($q1, $q2);
  161.     }
  162.  
  163.     /**
  164.      * Returns -1 if $mt1 is greater, 0 if they are equal and 1 if $mt2 is greater
  165.      *
  166.      * @param <string> $lang1
  167.      * @param <string> $lang2
  168.      */
  169.     public function compareLanguageQuality($lang1, $lang2) {
  170.         $q1 = $this->_getLanguageQuality($lang1);
  171.         $q2 = $this->_getLanguageQuality($lang2);
  172.         return $this->_compare($q1, $q2);
  173.     }
  174.  
  175.     /**
  176.      * Returns -1 if $mt1 is greater, 0 if they are equal and 1 if $mt2 is greater
  177.      *
  178.      * @param <string> $enc1
  179.      * @param <string> $enc2
  180.      */
  181.     public function compareEncodingQuality($enc1, $enc2) {
  182.         $q1 = $this->_getEncodingQuality($enc1);
  183.         $q2 = $this->_getEncodingQuality($enc2);
  184.         return $this->_compare($q1, $q2);
  185.     }
  186.  
  187.     /**
  188.      * Returns -1 if $mt1 is greater, 0 if they are equal and 1 if $mt2 is greater
  189.      *
  190.      * @param <string> $char1
  191.      * @param <string> $char2
  192.      */
  193.     public function compareCharsetQuality($char1, $char2) {
  194.         $q1 = $this->_getCharsetQuality($char1);
  195.         $q2 = $this->_getCharsetQuality($char2);
  196.         return $this->_compare($q1, $q2);
  197.     }
  198.  
  199.     /**
  200.      * Compares and returns results in a unified format
  201.      *
  202.      * @param <int> $qualityValue1
  203.      * @param <int> $qualityValue2
  204.      * @return <int>
  205.      */
  206.     protected function _compare($qualityValue1, $qualityValue2) {
  207.         if ($qualityValue1 > $qualityValue2) {
  208.             return -1;
  209.         } else if ($qualityValue2 > $qualityValue1) {
  210.             return 1;
  211.         } else {
  212.             return 0;
  213.         }
  214.     }
  215.  
  216.     /**
  217.      * Returns the mime-type quality
  218.      *
  219.      * @param <string> $mimeType
  220.      * @return <int>
  221.      */
  222.     protected function _getMimeTypeQuality($mimeType) {
  223.         return $this->_getQuality($mimeType, $this->getMimeTypes());
  224.     }
  225.  
  226.     /**
  227.      * Returns the language quality
  228.      *
  229.      * @param <string> $language
  230.      * @return <int>
  231.      */
  232.     protected function _getLanguageQuality($language) {
  233.         return $this->_getQuality($language, $this->getLanguages());
  234.     }
  235.  
  236.     /**
  237.      * Returns the encoding quality
  238.      *
  239.      * @param <string> $encoding
  240.      * @return <int>
  241.      */
  242.     protected function _getEncodingQuality($encoding) {
  243.         return $this->_getQuality($encoding, $this->getEncodings());
  244.     }
  245.  
  246.     /**
  247.      * Returns the charset quality
  248.      *
  249.      * @param <string> $charset
  250.      * @return <int>
  251.      */
  252.     protected function _getCharsetQuality($charset) {
  253.         return $this->_getQuality($charset, $this->getCharsets());
  254.     }
  255.  
  256.     /**
  257.      * Retrieves the quality from $search and returns $default otherwise
  258.      *
  259.      * @param <string> $item
  260.      * @param <array> $search
  261.      * @param <mixed> $default
  262.      * @return <int>
  263.      */
  264.     protected function _getQuality($item, $search, $default = 0) {
  265.         $item = strtoupper($item);
  266.         return array_key_exists($item, $search) ? $search[$item] : $default;
  267.     }
  268.  
  269.     /**
  270.      * Parses the HTTP_ACCEPT header and merges it with $this->_mimeTypes
  271.      */
  272.     protected function _parseAcceptHeader() {
  273.         $this->_mergeMimeTypes($this->_parse($_SERVER['HTTP_ACCEPT']));
  274.     }
  275.  
  276.     /**
  277.      * Parses the HTTP_ACCEPT_LANGUAGE header and merges it with $this->_languages
  278.      */
  279.     protected function _parseAcceptLanguageHeader() {
  280.         $this->_mergeLanguages($this->_parse($_SERVER['HTTP_ACCEPT_LANGUAGE']));
  281.     }
  282.  
  283.     /**
  284.      * Parses the HTTP_ACCEPT_ENCODING header and merges it with $this->_encodings
  285.      */
  286.     protected function _parseAcceptEncodingHeader() {
  287.         $this->_mergeEncodings($this->_parse($_SERVER['HTTP_ACCEPT_ENCODING']));
  288.     }
  289.  
  290.     /**
  291.      * Parses the HTTP_ACCEPT_CHARSET header and merges it with $this->_charsets
  292.      */
  293.     protected function _parseAcceptCharsetHeader() {
  294.         $this->_mergeCharsets($this->_parse($_SERVER['HTTP_ACCEPT_CHARSET']));
  295.     }
  296.  
  297.     /**
  298.      * Performs the actual parsing of accept headers
  299.      *
  300.      * @param <string> $str
  301.      * @return <array>
  302.      */
  303.     protected function _parse($str) {
  304.         $mimeTypesQuality = array();
  305.         $mimeTypes = explode(',', $str);
  306.         foreach ($mimeTypes as $mimeType) {
  307.             if (strpos($mimeType, ';')) {
  308.                 list($mimeType, $qualityValue) = explode(';', $mimeType);
  309.                 list(, $qualityValue) = explode('=', $qualityValue);
  310.             } else {
  311.                 $qualityValue = 1;
  312.             }
  313.             if (strpos($mimeType, '*')) {
  314.                 continue;
  315.             }
  316.             $mimeTypesQuality[strtoupper($mimeType)] = $qualityValue;
  317.         }
  318.         return $mimeTypesQuality;
  319.     }
  320.  
  321.     /**
  322.      * Merges $mimeTypes with $this->_mimeTypes
  323.      *
  324.      * @param <array> $mimeTypes
  325.      */
  326.     protected function _mergeMimeTypes($mimeTypes) {
  327.         $this->_mimeTypes = $this->_merge($this->getMimeTypes(), $mimeTypes);
  328.     }
  329.  
  330.     /**
  331.      * Merges $languages with $this->_languages
  332.      *
  333.      * @param <array> $languages
  334.      */
  335.     protected function _mergeLanguages($languages) {
  336.         $this->_languages = $this->_merge($this->getLanguages(), $languages);
  337.     }
  338.  
  339.     /**
  340.      * Merges $encodings with $this->_encodings
  341.      *
  342.      * @param <array> $encodings
  343.      */
  344.     protected function _mergeEncodings($encodings) {
  345.         $this->_encodings = $this->_merge($this->getEncodings(), $encodings);
  346.     }
  347.  
  348.     /**
  349.      * Merges $charsets with $this->_charsets
  350.      *
  351.      * @param <array> $charsets
  352.      */
  353.     protected function _mergeCharsets($charsets) {
  354.         $this->_charsets = $this->_merge($this->getCharsets(), $charsets);
  355.     }
  356.  
  357.     /**
  358.      * Merges two arrays
  359.      *
  360.      * @param <array> $array1
  361.      * @param <array> $array2
  362.      * @return <array>
  363.      */
  364.     protected function _merge($array1, $array2) {
  365.         return array_merge($array1, $array2);
  366.     }
  367. }

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with @@


Remember me so that I can delete my post