Guest User

Untitled

a guest
Jun 25th, 2018
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.83 KB | None | 0 0
  1. <?php
  2.  
  3.     set_time_limit(0);
  4.     ob_start();
  5.  
  6.     function x($str) { return mysql_real_escape_string($str); }
  7.  
  8.     $db = mysql_connect('localhost', 'user', 'pass');
  9.     mysql_select_db('db');
  10.    
  11.     // Truncate the existing users table.
  12.     mysql_query("TRUNCATE users");
  13.     // Truncate the existing logs table.
  14.     mysql_query("TRUNCATE logs_access");
  15.     // Set auto_increment to default.
  16.     mysql_query("ALTER TABLE users AUTO_INCREMENT=1");
  17.     mysql_query("ALTER TABLE logs_access AUTO_INCREMENT=1");
  18.  
  19.     // Grab dummy data.
  20.     $names = explode("\n", file_get_contents("names.txt"));
  21.    
  22.     // How many do we want to have?
  23.     $count = 100; // initial population of 100 students
  24.    
  25.     // Separate the name parts.
  26.     for($x = 0; $x < $count; $x++) {
  27.         $names[$x] = explode(" ", $names[$x]);
  28.     }
  29.    
  30.     for($x = 0; $x < $count; $x++) {
  31.         $name = Array(trim($names[$x][0]), trim($names[$x][1]));
  32.         $username = strtolower($name[0]) . "." . strtolower($name[1]);
  33.        
  34.         mysql_query(sprintf("INSERT INTO users VALUES (NULL, '%s', SHA1('%s'), 'demo.hh-login.com', 1, '%s', '%s');", x($username), x($username), x($name[0], x($name[1])));
  35.         $users[] = Array("id" => mysql_insert_id(), "username" => $username, "firstName" => $name[0], "lastName" => $name[1]);
  36.     }
  37.     echo "<h1>Created $count users.</h1>"; ob_flush();
  38.    
  39.     // create logins and logouts
  40.     for($i = 0; $i < 40; $i++) {
  41.         echo "<h1>Week #$i</h1>"; ob_flush();
  42.         // First 100 users will have four logins per week
  43.         // length of login will be between 2.5 and 4 hours
  44.         // start time is between 3pm and 6pm.
  45.         $k = 0;
  46.         foreach($users as $user) {
  47.             $k++;
  48.             if($k < $count/3) {
  49.                 for($j = 0; $j < 7; $j++) {
  50.                     if(rand(1, 7) <= 3) continue; // take three days off on average
  51.                     $randomHour = rand(15, 18);
  52.                     $randomMinute = rand(1, 59);
  53.                     $randomSecond = rand(1, 59);
  54.                    
  55.                     $durationHour = rand(2, 4);
  56.                     $durationMinute = rand($durationHour == 2 ? 30 : 1, 59);
  57.                     $durationSecond = rand(1, 59);
  58.                    
  59.                     $randomDay = date("Y-m-d h:i:s", strtotime(sprintf("%s +$j days +$i weeks", date("Y-m-d h:i:s", mktime($randomHour, $randomMinute, $randomSecond, 9, 3, 2000)))));
  60.                     mysql_query(sprintf("INSERT INTO logs_access VALUES (NULL, %d, 'login', '%s')", $user['id'], $randomDay));
  61.                     mysql_query(sprintf("INSERT INTO logs_access VALUES (NULL, %d, 'logout', '%s')", $user['id'], date("Y-m-d h:i:s", strtotime("$randomDay +$durationHour hours +$durationMinute minutes +$durationSecond seconds"))));
  62.                    
  63.                     echo "(Group 1) User " . $user['firstName'] . " " . $user['lastName'] . " logged in at $randomDay. They logged out $durationHour hours $durationMinute minutes $durationSecond seconds later.<br>";
  64.                 }
  65.                 ob_flush();
  66.             } else if($k < 2*$count/3) {
  67.                 for($j = 1; $j < 6; $j++) {
  68.                     if(rand(1, 7) <= 2) continue; // take four days off on average
  69.                     $randomHour = rand(20, 22);
  70.                     $randomMinute = rand(1, 59);
  71.                     $randomSecond = rand(1, 59);
  72.                    
  73.                     $durationHour = rand(1, 3);
  74.                     $durationMinute = rand(1, 59);
  75.                     $durationSecond = rand(1, 59);
  76.                    
  77.                     $randomDay = date("Y-m-d h:i:s", strtotime(sprintf("%s +$j days $i weeks", date("Y-m-d h:i:s", mktime($randomHour, $randomMinute, $randomSecond, 9, 3, 2000)))));
  78.                     mysql_query(sprintf("INSERT INTO logs_access VALUES (NULL, %d, 'login', '%s')", $user['id'], $randomDay));
  79.                     mysql_query(sprintf("INSERT INTO logs_access VALUES (NULL, %d, 'logout', '%s')", $user['id'], date("Y-m-d h:i:s", strtotime("$randomDay +$durationHour hours +$durationMinute minutes +$durationSecond seconds"))));
  80.                    
  81.                     echo "(Group 2) User " . $user['firstName'] . " " . $user['lastName'] . " logged in at $randomDay. They logged out $durationHour hours $durationMinute minutes $durationSecond seconds later.<br>";
  82.                 }
  83.                 ob_flush();
  84.             } else {
  85.                 for($j = 1; $j < 6; $j++) {
  86.                     if(rand(1, 7) <= 2) continue; // take four days off on average
  87.                     $randomHour = rand(20, 22);
  88.                     $randomMinute = rand(1, 59);
  89.                     $randomSecond = rand(1, 59);
  90.                    
  91.                     $durationHour = rand(1, 3);
  92.                     $durationMinute = rand(1, 59);
  93.                     $durationSecond = rand(1, 59);
  94.                    
  95.                     $randomDay = date("Y-m-d h:i:s", strtotime(sprintf("%s +$j days $i weeks", date("Y-m-d h:i:s", mktime($randomHour, $randomMinute, $randomSecond, 9, 3, 2000)))));
  96.                     mysql_query(sprintf("INSERT INTO logs_access VALUES (NULL, %d, 'login', '%s')", $user['id'], $randomDay));
  97.                     mysql_query(sprintf("INSERT INTO logs_access VALUES (NULL, %d, 'logout', '%s')", $user['id'], date("Y-m-d h:i:s", strtotime("$randomDay +$durationHour hours +$durationMinute minutes +$durationSecond seconds"))));
  98.                    
  99.                     echo "(Group 3) User " . $user['firstName'] . " " . $user['lastName'] . " logged in at $randomDay. They logged out $durationHour hours $durationMinute minutes $durationSecond seconds later.<br>";
  100.                 }
  101.                 ob_flush();
  102.             }
  103.         }
  104.     }
  105.    
  106.     echo "<h1>DONE</h1>";
  107.    
  108.     ob_flush();
  109.     exit;
  110.  
  111. ?>
Add Comment
Please, Sign In to add comment