Browse Source

Allow searching by IP and email in staff user search

This involves temporarily decrypting all values being searched and
inserting them into a temporary table. Creation of this table can be
very slow when IP history is optionally searched. It might be better to
not make the temp table and just do the IP filtering from PHP.

Fixes #2
spaghetti 8 years ago
parent
commit
08fd06b79a
2 changed files with 45 additions and 16 deletions
  1. 4
    4
      sections/schedule/manually/update_geodist.php
  2. 41
    12
      sections/user/advancedsearch.php

+ 4
- 4
sections/schedule/manually/update_geodist.php View File

4
 while(list($EncIP) = $DB->next_record()) {
4
 while(list($EncIP) = $DB->next_record()) {
5
   $IPs[] = DBCrypt::decrypt($EncIP);
5
   $IPs[] = DBCrypt::decrypt($EncIP);
6
 }
6
 }
7
-$DB->query("CREATE TEMPORARY TABLE user_ips_decrypted (IP VARCHAR(45) NOT NULL)");
8
-$DB->query("INSERT INTO user_ips_decrypted (IP) VALUES('".implode("'),('", $IPs)."')");
7
+$DB->query("CREATE TEMPORARY TABLE users_ips_decrypted (IP VARCHAR(45) NOT NULL)");
8
+$DB->query("INSERT INTO users_ips_decrypted (IP) VALUES('".implode("'),('", $IPs)."')");
9
 $DB->query("TRUNCATE TABLE users_geodistribution");
9
 $DB->query("TRUNCATE TABLE users_geodistribution");
10
 $DB->query("
10
 $DB->query("
11
   INSERT INTO users_geodistribution
11
   INSERT INTO users_geodistribution
12
     (Code, Users)
12
     (Code, Users)
13
   SELECT g.Code, COUNT(u.IP) AS Users
13
   SELECT g.Code, COUNT(u.IP) AS Users
14
   FROM geoip_country AS g
14
   FROM geoip_country AS g
15
-    JOIN user_ips_decrypted AS u ON INET_ATON(u.IP) BETWEEN g.StartIP AND g.EndIP
15
+    JOIN users_ips_decrypted AS u ON INET_ATON(u.IP) BETWEEN g.StartIP AND g.EndIP
16
   GROUP BY g.Code
16
   GROUP BY g.Code
17
   ORDER BY Users DESC");
17
   ORDER BY Users DESC");
18
-$DB->query("DROP TABLE user_ips_decrypted");
18
+$DB->query("DROP TABLE users_ips_decrypted");
19
 $Cache->delete_value('geodistribution');
19
 $Cache->delete_value('geodistribution');
20
 ?>
20
 ?>

+ 41
- 12
sections/user/advancedsearch.php View File

253
     if (!empty($_GET['email'])) {
253
     if (!empty($_GET['email'])) {
254
       if (isset($_GET['email_history'])) {
254
       if (isset($_GET['email_history'])) {
255
         $Distinct = 'DISTINCT ';
255
         $Distinct = 'DISTINCT ';
256
-        $Join['he'] = ' JOIN users_history_emails AS he ON he.UserID = um1.ID ';
257
-        $Where[] = ' he.Email '.$Match.wrap($_GET['email']);
258
-      } else {
259
-        $Where[] = 'um1.Email'.$Match.wrap($_GET['email']);
260
       }
256
       }
257
+      $Join['the'] = ' JOIN users_emails_decrypted AS he ON he.ID = um1.ID ';
258
+      $Where[] = ' he.Email '.$Match.wrap($_GET['email']);
261
     }
259
     }
262
 
260
 
263
     if (!empty($_GET['email_cnt']) && is_number($_GET['email_cnt'])) {
261
     if (!empty($_GET['email_cnt']) && is_number($_GET['email_cnt'])) {
287
     if (!empty($_GET['ip'])) {
285
     if (!empty($_GET['ip'])) {
288
       if (isset($_GET['ip_history'])) {
286
       if (isset($_GET['ip_history'])) {
289
         $Distinct = 'DISTINCT ';
287
         $Distinct = 'DISTINCT ';
290
-        $Join['hi'] = ' JOIN users_history_ips AS hi ON hi.UserID = um1.ID ';
291
-        $Where[] = ' hi.IP '.$Match.wrap($_GET['ip'], '', true);
292
-      } else {
293
-        $Where[] = 'um1.IP'.$Match.wrap($_GET['ip'], '', true);
294
       }
288
       }
289
+      $Join['tip'] = ' JOIN users_ips_decrypted AS tip ON tip.ID = um1.ID ';
290
+      $Where[] = ' tip.IP '.$Match.wrap($_GET['ip'], '', true);
295
     }
291
     }
296
 
292
 
297
 
293
 
421
     if ($_GET['disabled_ip']) {
417
     if ($_GET['disabled_ip']) {
422
       $Distinct = 'DISTINCT ';
418
       $Distinct = 'DISTINCT ';
423
       if ($_GET['ip_history']) {
419
       if ($_GET['ip_history']) {
424
-        if (!isset($Join['hi'])) {
425
-          $Join['hi'] = ' JOIN users_history_ips AS hi ON hi.UserID = um1.ID ';
420
+        if (!isset($Join['tip'])) {
421
+          $Join['tip'] = ' JOIN users_ips_decrypted AS tip ON tip.ID = um1.ID ';
426
         }
422
         }
427
-        $Join['hi2'] = ' JOIN users_history_ips AS hi2 ON hi2.IP = hi.IP ';
428
-        $Join['um2'] = ' JOIN users_main AS um2 ON um2.ID = hi2.UserID AND um2.Enabled = \'2\' ';
423
+        $Join['tip2'] = ' JOIN users_ips_decrypted2 AS tip2 ON tip2.IP = tip.IP ';
424
+        $Join['um2'] = ' JOIN users_main AS um2 ON um2.ID = tip2.ID AND um2.Enabled = \'2\' ';
429
       } else {
425
       } else {
430
         $Join['um2'] = ' JOIN users_main AS um2 ON um2.IP = um1.IP AND um2.Enabled = \'2\' ';
426
         $Join['um2'] = ' JOIN users_main AS um2 ON um2.IP = um1.IP AND um2.Enabled = \'2\' ';
431
       }
427
       }
801
 </div>
797
 </div>
802
 <?
798
 <?
803
 if ($RunQuery) {
799
 if ($RunQuery) {
800
+  if (!empty($_GET['ip'])) {
801
+    if (isset($_GET['ip_history'])) {
802
+      $DB->query("SELECT UserID, IP FROM users_history_ips");
803
+    } else {
804
+      $DB->query("SELECT ID, IP FROM users_main");
805
+    }
806
+    while(list($ID, $EncIP) = $DB->next_record()) {
807
+      $IPs[] = $ID.", '".DBCrypt::decrypt($EncIP)."'";
808
+    }
809
+    $DB->query("CREATE TEMPORARY TABLE users_ips_decrypted (ID INT(10) UNSIGNED NOT NULL, IP VARCHAR(45) NOT NULL, PRIMARY KEY (ID,IP)) ENGINE=MEMORY");
810
+    $DB->query("INSERT IGNORE INTO users_ips_decrypted (ID, IP) VALUES(".implode("),(", $IPs).")");
811
+    if ($_GET['disabled_ip'] && $_GET['ip_history']) {
812
+      $DB->query("CREATE TEMPORARY TABLE users_ips_decrypted2 SELECT * FROM users_ips_decrypted");
813
+    }
814
+  }
815
+  if (!empty($_GET['email'])) {
816
+    if (isset($_GET['email_history'])) {
817
+      $DB->query("SELECT UserID, Email FROM users_history_emails");
818
+    } else {
819
+      $DB->query("SELECT ID, Email FROM users_main");
820
+    }
821
+    while(list($ID, $EncEmail) = $DB->next_record()) {
822
+      $Emails[] = $ID.", '".DBCrypt::decrypt($EncEmail)."'";
823
+    }
824
+    $DB->query("CREATE TEMPORARY TABLE users_emails_decrypted (ID INT(10) UNSIGNED NOT NULL, Email VARCHAR(255) NOT NULL, PRIMARY KEY (ID,Email)) ENGINE=MEMORY");
825
+    $DB->query("INSERT IGNORE INTO users_emails_decrypted (ID, Email) VALUES(".implode("),(", $Emails).")");
826
+  }
804
   $Results = $DB->query($SQL);
827
   $Results = $DB->query($SQL);
805
   $DB->query('SELECT FOUND_ROWS()');
828
   $DB->query('SELECT FOUND_ROWS()');
806
   list($NumResults) = $DB->next_record();
829
   list($NumResults) = $DB->next_record();
830
+  if (!empty($_GET['ip'])) {
831
+    $DB->query("DROP TABLE users_ips_decrypted");
832
+  }
833
+  if (!empty($_GET['email'])) {
834
+    $DB->query("DROP TABLE users_emails_decrypted");
835
+  }
807
   $DB->set_query_id($Results);
836
   $DB->set_query_id($Results);
808
 } else {
837
 } else {
809
   $DB->query('SET @nothing = 0');
838
   $DB->query('SET @nothing = 0');

Loading…
Cancel
Save