public function getLocale() { $languages = $this->agent->languages(); if (!$languages) { return null; } $locales = array_values(array_filter($languages, function ($l) { return strlen($l) == 2; })); return in($locales, 0); }
public function getUA() { // Initialize UA $agent = new Agent(); $browser = $agent->browser(); // Assemble data $data = ['browser' => $browser, 'browser_version' => $agent->version($browser), 'languages' => array($agent->languages()), 'device' => $agent->device(), 'platform' => $agent->platform(), 'is_mobile' => $agent->isMobile(), 'is_tablet' => $agent->isTablet(), 'is_desktop' => $agent->isDesktop(), 'is_robot' => $agent->isRobot(), 'robot_name' => $agent->robot()]; return $data; }
/** * 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 store($inputs) { $contact = new $this->model(); $contact->name = $inputs['name']; $contact->email = $inputs['email']; $contact->text = $inputs['message']; $contact->ip = \Request::ip(); $agent = new Agent(); $lang = $agent->languages(); $lang = implode(",", $lang); $platform = $agent->platform(); $version_p = $agent->version($platform); $device = $agent->device(); $browser = $agent->browser(); $version_b = $agent->version($browser); $contact->agent = $platform . $version_p . "|" . $device . "|" . $browser . $version_b . "|" . $lang; $contact->save(); }
/** * 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)); } } }
?> locale: <?php echo $user->getLocale(); ?> <br> platform: <?php echo $user->getPlatform(); ?> <br> os: <?php echo $user->getOS(); ?> <br> locales: <pre><?php echo print_r($agent->languages()); ?> </pre> url to redirect: <?php echo $redirect->getUrl(); ?> <br> <pre> <?php print_r($_SERVER); ?> </pre>
public function testLanguages() { $agent = new Agent(); $agent->setHttpHeaders(array('HTTP_ACCEPT_LANGUAGE' => 'nl-NL,nl;q=0.8,en-US;q=0.6,en;q=0.4')); $this->assertEquals(array('nl-nl', 'nl', 'en-us', 'en'), $agent->languages()); }
/** * Get accept languages. * * @return array * @static */ public static function languages($acceptLanguage = null) { return \Jenssegers\Agent\Agent::languages($acceptLanguage); }