/** * @param null $userAgent * @return null|string */ public function setAgentToDetector($userAgent = null) { if (is_null($userAgent)) { $this->agentDetector->setUserAgent($this->getUseragent()); } return $this->agentDetector->setUserAgent($userAgent); }
public function testVersions() { $agent = new Agent(); foreach ($this->browserVersions as $version => $ua) { $agent->setUserAgent($ua); $browser = $agent->browser(); $this->assertEquals($version, $agent->version($browser)); } foreach ($this->operatingSystemVersions as $version => $ua) { $agent->setUserAgent($ua); $platform = $agent->platform(); $this->assertEquals($version, $agent->version($platform)); } }
/** * @medium * @dataProvider userAgentData */ public function testUserAgents($userAgent, $isMobile, $isTablet, $version, $model, $vendor) { //make sure we're passed valid data if (!is_string($userAgent) || !is_bool($isMobile) || !is_bool($isTablet)) { $this->markTestIncomplete("The User-Agent {$userAgent} does not have sufficient information for testing."); return; } //setup $md = new Agent(); $md->setUserAgent($userAgent); //is mobile? $this->assertEquals($md->isMobile(), $isMobile); //is tablet? $this->assertEquals($md->isTablet(), $isTablet); if (isset($version)) { foreach ($version as $condition => $assertion) { $this->assertEquals($assertion, $md->version($condition), 'FAILED UA (version("' . $condition . '")): ' . $userAgent); } } //version property tests if (isset($version)) { foreach ($version as $property => $stringVersion) { $v = $md->version($property); $this->assertSame($stringVersion, $v); } } //@todo: model test, not sure how exactly yet //@todo: vendor test. The below is theoretical, but fails 50% of the tests... /*if (isset($vendor)) { $method = "is$vendor"; $this->assertTrue($md->{$method}(), "Expected Mobile_Detect::{$method}() to be true."); }*/ }
public function testDevices() { $agent = new Agent(); foreach ($this->devices as $device => $ua) { $agent->setUserAgent($ua); $this->assertTrue($agent->isMobile()); $this->assertEquals($device, $agent->device()); if (!strpos($device, ' ')) { $method = "is{$device}"; $this->assertTrue($agent->{$method}()); } } }
/** * Redirect to long url * @return rediect to long url */ public function redirect(Request $request, $shortUrl) { $agent = new Agent(); $agent->setUserAgent($request->headers); $urls = Url::where('short_url', '=', $shortUrl)->get(); if (!$urls->first()) { echo "Not Found"; return; } $url = $urls[0]; $longUrl = $url['long_url']; $url->clicks = $url['clicks'] + 1; $url->save(); $url->hits()->create(['client_ip' => $_SERVER['REMOTE_ADDR'], 'language' => $agent->languages()[0], 'device' => $agent->device(), 'platform' => $agent->platform(), 'browser' => $agent->browser()]); //$agent->languages()[0],$agent->device(), $agent->platform(),$agent->browser(),$agent->robot();;;; return redirect($url['long_url']); }
public function insertUserInformation($ipAddress, $userAgentString, $sapiName, GeocoderResult $geocoderResult = null) { $userAgent = new UserAgent(); $userAgent->setUserAgent($userAgentString); $browser = $userAgent->browser(); $platform = $userAgent->platform(); $data = ['ip_address' => $ipAddress, 'sapi_name' => substr($sapiName, 0, 32), 'user_agent' => substr($userAgentString, 0, 255), 'user_agent_browser' => substr($browser, 0, 32), 'user_agent_device' => substr($userAgent->device(), 0, 32), 'user_agent_browser_version' => substr($userAgent->version($browser), 0, 32), 'user_agent_platform_version' => substr($userAgent->version($platform), 0, 32), 'user_agent_platform' => substr($platform, 0, 32), 'user_agent_robot' => substr($userAgent->robot(), 0, 32)]; if ($geocoderResult) { $region = $geocoderResult->getAdminLevels()->first(); $data['geo_city'] = substr($geocoderResult->getLocality(), 0, 32); $data['geo_region'] = $region ? substr($region->getCode(), 0, 32) : null; $data['geo_country'] = substr($geocoderResult->getCountry(), 0, 32); $data['geo_country_code'] = substr($geocoderResult->getCountryCode(), 0, 6); $data['geo_latitude'] = substr($geocoderResult->getLatitude(), 0, 32); $data['geo_longitude'] = substr($geocoderResult->getLongitude(), 0, 32); } $this->getAdapter()->insert('dewdrop_activity_log_user_information', $data); return $this->getAdapter()->lastInsertId(); }
$key = !empty($item['referrer']) ? $item['referrer'] : 'unkwn'; $counters['referrers'][] = array("value" => $item['c'], 'label' => $key); } // get origin $list = $logdb->query("SELECT origin, count(*) as c FROM tracker WHERE origin !='-' GROUP BY origin"); foreach ($list->fetchAll(PDO::FETCH_ASSOC) as $item) { $key = !empty($item['origin']) ? $item['origin'] : 'unkwn'; $counters['origins'][] = array("value" => $item['c'], 'label' => $key); } $br = array(); $os = array(); // get agents $list = $logdb->query("SELECT browser, count(*) as c FROM tracker GROUP BY browser"); foreach ($list->fetchAll(PDO::FETCH_ASSOC) as $item) { // echo $item['browser']; $agent->setUserAgent($item['browser']); $browser = $agent->browser(); $platform = $agent->platform(); // echo $agent->platform(); // echo $agent->browser(); $key = !empty($browser) ? $browser : 'unkwn'; if (!array_key_exists($key, $br)) { $br[$key] = 0; } $br[$key] += $item['c']; $key = !empty($platform) ? $platform : 'unkwn'; if (!array_key_exists($key, $os)) { $os[$key] = 0; } $os[$key] += $item['c']; }
/** * Redirects to Original Url */ public function redirect(Request $request, $shortUrl) { // Initiate Agent object for parsing header information $agent = new Agent(); // Setting header to agent $agent->setUserAgent($request->headers); // Query the url object from DB by shortUrl column $urls = Url::where('short_url', '=', config('app.url') . '/' . $shortUrl)->get(); // Check whether Url object exists if (!$urls->first()) { $error = Utility::getError(null, 404, 'Error', 'Url Not Found'); return response()->json($error, 404); } // get the queried url from collection $url = $urls[0]; // Get the long_url value for managing query string $longUrl = $url['long_url']; try { // Generate a version 1 (time-based) UUID object if (!isset($_COOKIE['_id'])) { $cookieUuid1 = Uuid::uuid1(); setcookie('_id', $cookieUuid1->toString(), time() + 10 * 365 * 24 * 3600, "/"); } else { $cookieUuid1 = $_COOKIE['_id']; } if (!isset($_COOKIE['_sid'])) { $sessionUuid1 = Uuid::uuid1(); setcookie('_sid', $sessionUuid1->toString(), 0, "/"); } else { $sessionUuid1 = $_COOKIE['_sid']; } } catch (UnsatisfiedDependencyException $e) { $error = Utility::getError($e, 500, 'Error', 'Please try again...'); return response()->json($error, 500); } // Default value of Http Referrer for saving into db $httpReferrer = 'Undefined'; // If Http referrer exists, replace the default value by it if (!empty($_SERVER['HTTP_REFERER'])) { $httpReferrer = $_SERVER['HTTP_REFERER']; } try { $city = Utility::getCity($_SERVER["REMOTE_ADDR"]); $state = Utility::getState($_SERVER["REMOTE_ADDR"]); $country = Utility::getCountry($_SERVER["REMOTE_ADDR"]); $countryIsoCode = strtolower(Utility::getCountryIsoCode($_SERVER["REMOTE_ADDR"])); } catch (AddressNotFoundException $e) { // Make logging for AddressNotFoundException exception //Utility::log($e); $city = ''; $state = ''; $country = ''; $countryIsoCode = ''; } // Increment clicks column by one $url->clicks = $url['clicks'] + 1; // Save the url instance $url->save(); // create the new Hit instance for the url instance $url->hits()->create(['client_ip' => $_SERVER['REMOTE_ADDR'], 'language' => $agent->languages()[0], 'device' => $agent->device(), 'platform' => $agent->platform(), 'browser' => $agent->browser(), 'cookie_id' => $cookieUuid1, 'session_id' => $sessionUuid1, 'user_agent' => $_SERVER['HTTP_USER_AGENT'], 'referrers' => $httpReferrer, 'remote_addr' => $_SERVER["REMOTE_ADDR"], 'remote_port' => $_SERVER["REMOTE_PORT"], 'remote_method' => $_SERVER["REQUEST_METHOD"], 'city' => $city, 'state' => $state, 'country' => $country, 'country_iso_code' => $countryIsoCode]); // Get all query parameter of short url $shortQuery = $request->all(); // Get all url parameter of long url $parsedLongUrl = parse_url($longUrl); // Code for merging query parameter of short Url and long Url keeping the short url as prior // Redirect to the original Url if (isset($parsedLongUrl["query"])) { parse_str($parsedLongUrl["query"], $longQuery); $commonKeys = array_intersect_key($longQuery, $shortQuery); $finalQuery = array_diff($shortQuery, $commonKeys) + array_diff($longQuery, $commonKeys) + $commonKeys; if (isset($parsedLongUrl['port'])) { return redirect($parsedLongUrl['scheme'] . '://' . $parsedLongUrl['host'] . ':' . $parsedLongUrl['port'] . $parsedLongUrl['path'] . '?' . http_build_query($finalQuery)); } else { return redirect($parsedLongUrl['scheme'] . '://' . $parsedLongUrl['host'] . $parsedLongUrl['path'] . '?' . http_build_query($finalQuery)); } } else { if (empty($shortQuery)) { return redirect($longUrl); } else { return redirect($longUrl . '?' . http_build_query($shortQuery)); } } }
/** * @covers Agent::setUserAgent, Agent::getUserAgent */ public function testSetUserAgent() { $md = new Agent(array()); $md->setUserAgent('hello world'); $this->assertSame('hello world', $md->getUserAgent()); }
public static function parse($userAgentString) { $browser = new UserAgentLib(); $browser->setUserAgent($userAgentString); return array('os' => $browser->platform(), 'name' => $browser->browser(), 'version' => $browser->version($browser->browser())); }
/** * Set the User-Agent to be used. * * @param string $userAgent The user agent string to set. * @return string|null * @static */ public static function setUserAgent($userAgent = null) { //Method inherited from \Mobile_Detect return \Jenssegers\Agent\Agent::setUserAgent($userAgent); }
<?php printf("Сьогодні: %s", Carbon::now()); ?> </p> <p> <?php echo Dates::getBirthDay(); ?> </p> <p> <?php use Jenssegers\Agent\Agent; $agent = new Agent(); $agent->setUserAgent($_SERVER['HTTP_USER_AGENT']); $os = $agent->platform(); $browser = $agent->browser(); echo "OS: " . $os . ", Browser:" . $browser; ?> </p> <p> <?php $faker = Faker\Factory::create(); ?> <img src="<?php echo $faker->imageUrl(); ?> " alt=""/> </p>
/** * Set the user agent field * * @param string $value * @return string */ public function setUserAgentAttribute($value) { $agent = new Agent(); $agent->setUserAgent($value); $this->browser = $agent->browser(); $this->browser_version = $agent->version($this->browser); $this->platform = $agent->platform(); $this->platform_version = $agent->version($this->platform); $this->device = $agent->device() && !$agent->isDesktop() ? $agent->device() : ''; $this->device_type = $agent->isMobile() ? 'mobile' : ($agent->isTablet() ? 'tablet' : ($agent->isDesktop() ? 'desktop' : '')); $this->attributes['user_agent'] = $value; }
public function setUserAgentAttribute($value) { $agent = new Agent(); $agent->setUserAgent($value); if ($agent->isDesktop()) { $platform = 'Desktop'; } elseif ($agent->isMobile()) { if ($agent->isTablet()) { $platform = 'Tablet'; } else { $platform = 'Mobile'; } } $this->attributes['remote_platform'] = $platform; $this->attributes['user_agent'] = $value; }