|
| 1 | +#!/usr/bin/env php |
| 2 | +<?php |
| 3 | +include __DIR__ . "/init.php"; |
| 4 | +use Garden\Cli\Cli; |
| 5 | +use UnityWebPortal\lib\UnityUserExpirationWarningType; |
| 6 | + |
| 7 | +$cli = new Cli(); |
| 8 | +$cli->description( |
| 9 | + "Send a warning email, idlelock, or disable users, depending on their last login date. " . |
| 10 | + "It is important that this script runs exactly once per day.", |
| 11 | +)->opt("dry-run", "Print changes without actually changing anything.", false, "boolean"); |
| 12 | +$args = $cli->parse($argv, true); |
| 13 | + |
| 14 | +$idlelock_warning_emails = []; |
| 15 | +$disable_warning_emails = []; |
| 16 | +$uids_to_idlelock = []; |
| 17 | +$uids_to_disable = []; |
| 18 | + |
| 19 | +$idlelock_warning_days = CONFIG["user_expiry"]["idlelock_warning_days"]; |
| 20 | +$idlelock_day = CONFIG["user_expiry"]["idlelock_day"]; |
| 21 | +$disable_warning_days = CONFIG["user_expiry"]["idlelock_warning_days"]; |
| 22 | +$disable_day = CONFIG["user_expiry"]["disable_day"]; |
| 23 | +$now = time(); |
| 24 | +$all_users_last_logins = $SQL->getAllUserLastLogins(); |
| 25 | +$all_users_warning_emails_sent = $SQL->getAllUsersExpirationWarningDaysSent(); |
| 26 | + |
| 27 | +function doesArrayHaveOnlyIntegerValues(array $x): bool |
| 28 | +{ |
| 29 | + foreach ($x as $value) { |
| 30 | + if (!is_int($value)) { |
| 31 | + return false; |
| 32 | + } |
| 33 | + } |
| 34 | + return true; |
| 35 | +} |
| 36 | + |
| 37 | +function isArrayMonotonicallyIncreasing(array $x): bool |
| 38 | +{ |
| 39 | + if (count($x) <= 1) { |
| 40 | + return true; |
| 41 | + } |
| 42 | + $remaining_values = $x; |
| 43 | + $last_value = array_shift($remaining_values); |
| 44 | + while (count($remaining_values)) { |
| 45 | + $this_value = array_shift($remaining_values); |
| 46 | + if ($this_value < $last_value) { |
| 47 | + return false; |
| 48 | + } |
| 49 | + $last_value = $this_value; |
| 50 | + } |
| 51 | + return true; |
| 52 | +} |
| 53 | + |
| 54 | +if (!doesArrayHaveOnlyIntegerValues($idlelock_warning_days)) { |
| 55 | + _die('$CONFIG["user_expiry"]["idlelock_warning_days"] must be a list of integers!', 1); |
| 56 | +} |
| 57 | +if (!doesArrayHaveOnlyIntegerValues($disable_warning_days)) { |
| 58 | + _die('$CONFIG["user_expiry"]["disable_warning_days"] must be a list of integers!', 1); |
| 59 | +} |
| 60 | +if (!isArrayMonotonicallyIncreasing($idlelock_warning_days)) { |
| 61 | + _die('$CONFIG["user_expiry"]["idlelock_warning_days"] is not monotonically increasing!', 1); |
| 62 | +} |
| 63 | +if (!isArrayMonotonicallyIncreasing($disable_warning_days)) { |
| 64 | + _die('$CONFIG["user_expiry"]["disable_warning_days"] is not monotonically increasing!', 1); |
| 65 | +} |
| 66 | + |
| 67 | +$last_disable_warning_day = $disable_warning_days[array_key_last($disable_warning_days)]; |
| 68 | +$last_idlelock_warning_day = $idlelock_warning_days[array_key_last($idlelock_warning_days)]; |
| 69 | +if ($disable_day <= $last_disable_warning_day) { |
| 70 | + _die("disable day must be greater than the last disable warning day", 1); |
| 71 | +} |
| 72 | +if ($idlelock_day <= $last_idlelock_warning_day) { |
| 73 | + _die("idlelock day must be greater than the last idlelock warning day", 1); |
| 74 | +} |
| 75 | + |
| 76 | +$all_users_idle_days = []; |
| 77 | +foreach ($all_users_last_logins as $sql_record) { |
| 78 | + $uid = $sql_record["uid"]; |
| 79 | + $last_login_str = $sql_record["last_login"]; |
| 80 | + $last_login = strtotime($last_login_str); |
| 81 | + $seconds_since_last_login = $now - $last_login; |
| 82 | + $days_since_last_login = round($seconds_since_last_login / (60 * 60 * 24)); |
| 83 | + $all_users_idle_days[$uid] = $days_since_last_login; |
| 84 | +} |
| 85 | + |
0 commit comments