1
0
mirror of https://github.com/monstra-cms/monstra.git synced 2025-08-06 21:26:58 +02:00

Core Improvements: Next Round #79 #80

This commit is contained in:
Awilum
2013-01-08 18:29:30 +02:00
parent ef76f8befc
commit b7dcc5be49
34 changed files with 341 additions and 194 deletions

View File

@@ -0,0 +1,162 @@
<?php
/**
* Gelato Library
*
* This source file is part of the Gelato Library. More information,
* documentation and tutorials can be found at http://gelato.monstra.org
*
* @package Gelato
*
* @author Romanenko Sergey / Awilum
* @copyright (c) 2013 Romanenko Sergey / Awilum
* @since 1.0.0
*/
class Agent
{
/**
* Mobiles
*
* @var array
*/
public static $mobiles = array (
'ipad',
'iphone',
'ipod',
'android',
'windows ce',
'windows phone',
'mobileexplorer',
'opera mobi',
'opera mini',
'fennec',
'blackberry',
'nokia',
'kindle',
'ericsson',
'motorola',
'minimo',
'iemobile',
'symbian',
'webos',
'hiptop',
'palmos',
'palmsource',
'xiino',
'avantgo',
'docomo',
'up.browser',
'vodafone',
'portable',
'pocket',
'mobile',
'phone',
);
/**
* Robots
*
* @var array
*/
public static $robots = array(
'googlebot',
'msnbot',
'slurp',
'yahoo',
'askjeeves',
'fastcrawler',
'infoseek',
'lycos',
'ia_archiver',
);
/**
* Protected constructor since this is a static class.
*
* @access protected
*/
protected function __construct()
{
// Nothing here
}
/**
* Searches for a string in the user agent string.
*
* @param array $agents Array of strings to look for
* @return boolean
*/
protected static function find($agents)
{
// If isset HTTP_USER_AGENT ?
if (isset($_SERVER['HTTP_USER_AGENT'])) {
// Loop through $agents array
foreach ($agents as $agent) {
// If current user agent == agents[agent] then return true
if (stripos($_SERVER['HTTP_USER_AGENT'], $agent) !== false) {
return true;
}
}
}
// Else return false
return false;
}
/**
* Returns true if the user agent that made the request is identified as a mobile device.
*
* <code>
* if (Agent::isMobile()) {
* // Do something...
* }
* </code>
*
* @return boolean
*/
public static function isMobile()
{
return Agent::find(Agent::$mobiles);
}
/**
* Returns true if the user agent that made the request is identified as a robot/crawler.
*
* <code>
* if (Agent::isRobot()) {
* // Do something...
* }
* </code>
*
* @return boolean
*/
public static function isRobot()
{
return Agent::find(Agent::$robots);
}
/**
* Returns TRUE if the string you're looking for exists in the user agent string and FALSE if not.
*
* <code>
* if (Agent::is('iphone')) {
* // Do something...
* }
*
* if (Agent::is(array('iphone', 'ipod'))) {
* // Do something...
* }
* </code>
*
* @param mixed $device String or array of strings you're looking for
* @return boolean
*/
public static function is($device)
{
return Agent::find((array) $device);
}
}