Skip to content

Commit

Permalink
aligned identification logic with v3
Browse files Browse the repository at this point in the history
  • Loading branch information
kaestel committed Mar 21, 2017
1 parent eebe7b4 commit 8c7179c
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 11 deletions.
93 changes: 82 additions & 11 deletions src/classes/identify.class.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,15 @@ function __construct() {
$this->db_useragents = SITE_DB.".device_useragents";
$this->db_unidentified = SITE_DB.".unidentified_useragents";

$this->trimming_patterns = [
"[ ]+\[FB[^\]]+\]$",
"[ ]+\(iPhone[^\)]+scale[^\)]+gamut[^\)]+\)$",
"[ ]+[a-zA-Z]{2}[-_][a-zA-Z]{2}( ;|;)"
];

}


// TODO: update ipad identification - look at version/mobile/webkit inconsistencies


Expand All @@ -33,10 +40,66 @@ function identifyDevice($useragent, $log=true, $mail=true, $details=true) {
// return "basic";
}


// Experiment with trimming UA before doing analysis
// The goal is to remove non-identifying fragments to make regex process faster
foreach($this->trimming_patterns as $pattern) {
$useragent = preg_replace("/".$pattern."/", "", $useragent);
}



$detection_script = "/srv/sites/parentnode/detector_dearapi_com_v3/src/library/public/detection_script.php";
$detection_script_local = "/srv/sites/parentnode/detector_dearapi_com/src/library/public/detection_script.php";
if(file_exists($detection_script)) {
include($detection_script);
}
else if(file_exists($detection_script_local)) {
include($detection_script_local);
}


$IC = new Items();
$query = new Query();
$DC = $IC->typeObject("device");


// did static test return match
if(isset($device_name) && isset($device_segment)) {

// if log is true, use fastest method to return segment
if($log) {

// add to general id log
$this->logString("UA MARKER", $useragent, $device_segment, "marker");

// return segment
return array("segment" => $DC->translateNewSegments($device_segment));
// return array("segment" => $device_segment);
}

// if details are required
if($details) {

// get additional information
if($query->sql("SELECT item_id FROM ".$this->db." WHERE name = '$device_name'")) {
$device_id = $query->result(0, "item_id");

// get complete device
$device = $IC->getItem(array("id" => $device_id, "extend" => array("tags" => true)));
$device["method"] = "marker";
return $device;
}
}
}

//
// if(isset($device_segment) && $device_segment) {
// return array("segment" => $DC->translateNewSegments($device_segment));
// }



// $this->perf->mark("identify", true);

// perfect match
Expand Down Expand Up @@ -625,9 +688,9 @@ function identifyDevice($useragent, $log=true, $mail=true, $details=true) {

// Still unidentified
// register device for manual indexing
if($log) {
$this->saveForIdentification($useragent);
}
// if($log) {
// $this->saveForIdentification($useragent);
// }

// $this->perf->mark("guessing - logged");

Expand Down Expand Up @@ -943,7 +1006,7 @@ function uniqueIdTest($useragent, $device, $segment, $log, $mail, $details, $col
$this->logString("UA UNIQUE TEST", $useragent, $segment, "uniquetest");

// save useragent for manuel indexing
$this->saveForIdentification($useragent);
// $this->saveForIdentification($useragent);

// save for email notification
$this->notificationString("UNIQUE-TEST", $useragent, $segment, $collection);
Expand Down Expand Up @@ -992,14 +1055,20 @@ function uniqueIdTest($useragent, $device, $segment, $log, $mail, $details, $col
function saveForIdentification($useragent, $device_id = "") {
$query = new Query();

$comment = stringOr(getVar("site"), SITE_UID).stringOr(getVar("file"), "?")."\n";
$headers = apache_request_headers();
foreach($headers as $key => $value) {
$comment .= "$key: $value\n";
}
// only save if this exact ua hasn't already been identified
$sql = "SELECT id FROM ".$this->db_useragents." WHERE useragent = '$useragent'";
if(!$query->sql($sql)) {

// TODO: update insert
$query->sql("INSERT INTO ".$this->db_unidentified." VALUES(DEFAULT, '$useragent', '$comment', '$device_id', DEFAULT)");
$comment = stringOr(getVar("site"), SITE_UID).stringOr(getVar("file"), "?")."\n";
$headers = apache_request_headers();
foreach($headers as $key => $value) {
$comment .= "$key: $value\n";
}

// TODO: update insert
$query->sql("INSERT INTO ".$this->db_unidentified." VALUES(DEFAULT, '$useragent', '$comment', '$device_id', DEFAULT)");

}
}


Expand Down Expand Up @@ -1027,6 +1096,8 @@ function notificationString($status, $useragent, $segment, $collection, $match =
function logString($status, $useragent, $segment, $collection) {
global $page;

$this->saveForIdentification($useragent, $segment);

$string = "$status: " . $segment . "; UA: ".$useragent;
$page->addLog($string, $collection);
}
Expand Down
Empty file modified src/classes/items/type.device.class.php
100644 → 100755
Empty file.

0 comments on commit 8c7179c

Please sign in to comment.