@@ -16,8 +16,15 @@ function __construct() {
1616 $ this ->db_useragents = SITE_DB .".device_useragents " ;
1717 $ this ->db_unidentified = SITE_DB .".unidentified_useragents " ;
1818
19+ $ this ->trimming_patterns = [
20+ "[ ]+\[FB[^\]]+\]$ " ,
21+ "[ ]+\(iPhone[^\)]+scale[^\)]+gamut[^\)]+\)$ " ,
22+ "[ ]+[a-zA-Z]{2}[-_][a-zA-Z]{2}( ;|;) "
23+ ];
24+
1925 }
2026
27+
2128 // TODO: update ipad identification - look at version/mobile/webkit inconsistencies
2229
2330
@@ -33,10 +40,66 @@ function identifyDevice($useragent, $log=true, $mail=true, $details=true) {
3340// return "basic";
3441 }
3542
43+
44+ // Experiment with trimming UA before doing analysis
45+ // The goal is to remove non-identifying fragments to make regex process faster
46+ foreach ($ this ->trimming_patterns as $ pattern ) {
47+ $ useragent = preg_replace ("/ " .$ pattern ."/ " , "" , $ useragent );
48+ }
49+
50+
51+
52+ $ detection_script = "/srv/sites/parentnode/detector_dearapi_com_v3/src/library/public/detection_script.php " ;
53+ $ detection_script_local = "/srv/sites/parentnode/detector_dearapi_com/src/library/public/detection_script.php " ;
54+ if (file_exists ($ detection_script )) {
55+ include ($ detection_script );
56+ }
57+ else if (file_exists ($ detection_script_local )) {
58+ include ($ detection_script_local );
59+ }
60+
61+
3662 $ IC = new Items ();
3763 $ query = new Query ();
3864 $ DC = $ IC ->typeObject ("device " );
3965
66+
67+ // did static test return match
68+ if (isset ($ device_name ) && isset ($ device_segment )) {
69+
70+ // if log is true, use fastest method to return segment
71+ if ($ log ) {
72+
73+ // add to general id log
74+ $ this ->logString ("UA MARKER " , $ useragent , $ device_segment , "marker " );
75+
76+ // return segment
77+ return array ("segment " => $ DC ->translateNewSegments ($ device_segment ));
78+ // return array("segment" => $device_segment);
79+ }
80+
81+ // if details are required
82+ if ($ details ) {
83+
84+ // get additional information
85+ if ($ query ->sql ("SELECT item_id FROM " .$ this ->db ." WHERE name = ' $ device_name' " )) {
86+ $ device_id = $ query ->result (0 , "item_id " );
87+
88+ // get complete device
89+ $ device = $ IC ->getItem (array ("id " => $ device_id , "extend " => array ("tags " => true )));
90+ $ device ["method " ] = "marker " ;
91+ return $ device ;
92+ }
93+ }
94+ }
95+
96+ //
97+ // if(isset($device_segment) && $device_segment) {
98+ // return array("segment" => $DC->translateNewSegments($device_segment));
99+ // }
100+
101+
102+
40103// $this->perf->mark("identify", true);
41104
42105 // perfect match
@@ -625,9 +688,9 @@ function identifyDevice($useragent, $log=true, $mail=true, $details=true) {
625688
626689 // Still unidentified
627690 // register device for manual indexing
628- if ($ log ) {
629- $ this ->saveForIdentification ($ useragent );
630- }
691+ // if($log) {
692+ // $this->saveForIdentification($useragent);
693+ // }
631694
632695// $this->perf->mark("guessing - logged");
633696
@@ -943,7 +1006,7 @@ function uniqueIdTest($useragent, $device, $segment, $log, $mail, $details, $col
9431006 $ this ->logString ("UA UNIQUE TEST " , $ useragent , $ segment , "uniquetest " );
9441007
9451008 // save useragent for manuel indexing
946- $ this ->saveForIdentification ($ useragent );
1009+ // $this->saveForIdentification($useragent);
9471010
9481011 // save for email notification
9491012 $ this ->notificationString ("UNIQUE-TEST " , $ useragent , $ segment , $ collection );
@@ -992,14 +1055,20 @@ function uniqueIdTest($useragent, $device, $segment, $log, $mail, $details, $col
9921055 function saveForIdentification ($ useragent , $ device_id = "" ) {
9931056 $ query = new Query ();
9941057
995- $ comment = stringOr (getVar ("site " ), SITE_UID ).stringOr (getVar ("file " ), "? " )."\n" ;
996- $ headers = apache_request_headers ();
997- foreach ($ headers as $ key => $ value ) {
998- $ comment .= "$ key: $ value \n" ;
999- }
1058+ // only save if this exact ua hasn't already been identified
1059+ $ sql = "SELECT id FROM " .$ this ->db_useragents ." WHERE useragent = ' $ useragent' " ;
1060+ if (!$ query ->sql ($ sql )) {
10001061
1001- // TODO: update insert
1002- $ query ->sql ("INSERT INTO " .$ this ->db_unidentified ." VALUES(DEFAULT, ' $ useragent', ' $ comment', ' $ device_id', DEFAULT) " );
1062+ $ comment = stringOr (getVar ("site " ), SITE_UID ).stringOr (getVar ("file " ), "? " )."\n" ;
1063+ $ headers = apache_request_headers ();
1064+ foreach ($ headers as $ key => $ value ) {
1065+ $ comment .= "$ key: $ value \n" ;
1066+ }
1067+
1068+ // TODO: update insert
1069+ $ query ->sql ("INSERT INTO " .$ this ->db_unidentified ." VALUES(DEFAULT, ' $ useragent', ' $ comment', ' $ device_id', DEFAULT) " );
1070+
1071+ }
10031072 }
10041073
10051074
@@ -1027,6 +1096,8 @@ function notificationString($status, $useragent, $segment, $collection, $match =
10271096 function logString ($ status , $ useragent , $ segment , $ collection ) {
10281097 global $ page ;
10291098
1099+ $ this ->saveForIdentification ($ useragent , $ segment );
1100+
10301101 $ string = "$ status: " . $ segment . "; UA: " .$ useragent ;
10311102 $ page ->addLog ($ string , $ collection );
10321103 }
0 commit comments