TeraWurflUserAgentMatchers
[ class tree: TeraWurflUserAgentMatchers ] [ index: TeraWurflUserAgentMatchers ] [ all elements ]

Source for file UserAgentMatcher.php

Documentation is available at UserAgentMatcher.php

  1. <?php
  2. /**
  3.  * Tera_WURFL - PHP MySQL driven WURFL
  4.  * 
  5.  * Tera-WURFL was written by Steve Kamerman, and is based on the
  6.  * Java WURFL Evolution package by Luca Passani and WURFL PHP Tools by Andrea Trassati.
  7.  * This version uses a MySQL database to store the entire WURFL file, multiple patch
  8.  * files, and a persistent caching mechanism to provide extreme performance increases.
  9.  * 
  10.  * @package TeraWurflUserAgentMatchers
  11.  * @author Steve Kamerman <stevekamerman AT gmail.com>
  12.  * @version Stable 2.1.3 $Date: 2010/09/18 15:43:21
  13.  * @license http://www.mozilla.org/MPL/ MPL Vesion 1.1
  14.  */
  15. /**
  16.  * An abstract class that all UserAgentMatchers must extend.
  17.  * @package TeraWurflUserAgentMatchers
  18.  */
  19. abstract class UserAgentMatcher {
  20.     
  21.     /**
  22.      * @var TeraWurfl Running instance of Tera-WURFL
  23.      */
  24.     protected $wurfl;
  25.     /**
  26.      * WURFL IDs that are hardcoded in this connector.  Used for compatibility testing against new WURFLs
  27.      * @var array 
  28.      */
  29.     public static $constantIDs array();
  30.     /**
  31.      * @var Array List of WURFL IDs => User Agents.  Typically used for matching user agents.
  32.      */
  33.     public $deviceList;
  34.     
  35.     public function __construct(TeraWurfl $wurfl{
  36.         $this->wurfl = $wurfl;
  37.     }
  38.     
  39.     /**
  40.      * Attempts to find a conclusively matching WURFL ID from a given user agent
  41.      * @param String User agent
  42.      * @return String Matching WURFL ID
  43.      */
  44.     abstract public function applyConclusiveMatch($ua);
  45.     
  46.     /**
  47.      * Attempts to find a loosely matching WURFL ID from a given user agent
  48.      * @param String User agent
  49.      * @return String Matching WURFL ID
  50.      */
  51.     public function applyRecoveryMatch($ua{
  52.         return $this->recoveryMatch($ua);
  53.     }
  54.     /**
  55.      * Overide this method in order to have an alternative matching algorithm
  56.      * @param String User agent
  57.      * @return String Matching WURFL ID
  58.      */
  59.     public function recoveryMatch($ua){
  60.         return "generic";
  61.     }
  62.     /**
  63.      * Updates the deviceList Array to contain all the WURFL IDs that are related to the current UserAgentMatcher
  64.      * @return void 
  65.      */
  66.     protected function updateDeviceList(){
  67.         if(is_array($this->deviceList&& count($this->deviceList)>0return;
  68.         $this->deviceList = $this->wurfl->db->getFullDeviceList($this->wurfl->fullTableName());
  69.     }
  70.     /**
  71.      * Attempts to match given user agent string to a device from the database by comparing less and less of the strings until a match is found (RIS, Reduction in String)
  72.      * @param String User agent
  73.      * @param int Tolerance, how many characters must match from left to right
  74.      * @return String WURFL ID
  75.      */
  76.     public function risMatch($ua,$tolerance){
  77.         if($this->wurfl->db->db_implements_ris){
  78.             return $this->wurfl->db->getDeviceFromUA_RIS($ua,$tolerance,$this);
  79.         }
  80.         $this->updateDeviceList();
  81.         return UserAgentUtils::risMatch($ua,$tolerance,$this);
  82.     }
  83.     /**
  84.      * Attempts to match given user agent string to a device from the database by calculating their Levenshtein Distance (LD)
  85.      * @param String User agent
  86.      * @param int Tolerance, how much difference is allowed
  87.      * @return String WURFL ID
  88.      */
  89.     public function ldMatch($ua,$tolerance=NULL){
  90.         if($this->wurfl->db->db_implements_ld){
  91.             return $this->wurfl->db->getDeviceFromUA_LD($ua,$tolerance,$this);
  92.         }
  93.         $this->updateDeviceList();
  94.         return UserAgentUtils::ldMatch($ua,$tolerance,$this);
  95.     }
  96.     /**
  97.      * Returns the name of the UserAgentMatcher in use
  98.      * @return String UserAgentMatcher name
  99.      */
  100.     public function matcherName(){
  101.         return get_class($this);
  102.     }
  103.     /**
  104.      * Returns the database table suffix for the current UserAgentMatcher
  105.      * @return String Table suffix
  106.      */
  107.     public function tableSuffix(){
  108.         $cname $this->matcherName();
  109.         return substr($cname,0,strpos($cname,"UserAgentMatcher"));
  110.     }
  111.     /**
  112.      * Check if user agent contains target string
  113.      * @param String User agent
  114.      * @param String Target string or array of strings
  115.      * @return Bool 
  116.      */
  117.     public static function contains($ua,$find){
  118.         if(is_array($find)){
  119.             foreach($find as $part){
  120.                 if(strpos($ua,$part)!==false){
  121.                     return true;
  122.                 }
  123.             }
  124.             return false;
  125.         }else{
  126.             return (strpos($ua,$find)!==false);
  127.         }
  128.     }
  129.     /**
  130.      * Check if user agent starts with target string
  131.      * @param String User agent
  132.      * @param String Target string or array of strings
  133.      * @return Bool 
  134.      */
  135.     public static function startsWith($ua,$find){
  136.         if(is_array($find)){
  137.             foreach($find as $part){
  138.                 if(strpos($ua,$part)===0){
  139.                     return true;
  140.                 }
  141.             }
  142.             return false;
  143.         }else{
  144.             return (strpos($ua,$find)===0);
  145.         }
  146.     }
  147.     /**
  148.      * Check if user agent contains another string using PCRE (Perl Compatible Reqular Expressions)
  149.      * @param String User agent
  150.      * @param $find Target regex string or array of regex strings
  151.      * @return Bool 
  152.      */
  153.     public static function regexContains($ua,$find){
  154.         if(is_array($find)){
  155.             foreach($find as $part){
  156.                 if(preg_match($part,$ua)){
  157.                     return true;
  158.                 }
  159.             }
  160.             return false;
  161.         }else{
  162.             return (preg_match($find,$ua));
  163.         }
  164.     }
  165. }

Documentation generated on Sun, 19 Sep 2010 00:16:07 +0000 by phpDocumentor 1.4.3