load(); $dotenv->required(['SERVER_HOST', 'SERVER_USER', 'SERVER_PASS']); $server = $_SERVER['SERVER_HOST']; $user = $_SERVER['SERVER_USER']; $pass = $_SERVER['SERVER_PASS']; // Firstly, init \ATDev\RocketChat\Chat::setUrl($server); // No trailing / on the https://host.domain // Now, login $result = \ATDev\RocketChat\Chat::login($user, $pass); if (!$result) { // Log the error $error = \ATDev\RocketChat\Chat::getError(); // And out } else { $userObj = new \ATDev\RocketChat\Users\User; $listing = $userObj->listing(0, 150); // Will retrieve use list inc last login if (!$listing) { // Log the error $error = \ATDev\RocketChat\Users\User::getError(); } else { //var_dump($listing); $x = 1; $userArr = array(); foreach ($listing as $user) { $userId = $user->getUserName($user); $lastLogin = $user->getLastLogin($user); $userArr[$userId] = ($lastLogin); } unset($userObj); //asort($userArr); $protectedUsers = array("rocket.cat", "dani", "botpress-connector.bot", "jitsi.bot", "admin", "john"); $bestAfterDate = "2023"; foreach ($userArr as $ID => $date) { // If $ID not in array if (! in_array($ID, $protectedUsers)) { $dateTimeInstance = new DateTime($date); $formattedFromObject = $dateTimeInstance->format('Y'); if ($formattedFromObject >= $bestAfterDate) { echo "Keep Key=" . $ID . ", Value=" . $formattedFromObject; echo "\n"; } else { $user = new \ATDev\RocketChat\Users\User($ID); $active = $user->getActive(); echo "Delete Key=" . $ID . ", Value=" . $formattedFromObject; echo "\n"; } } else { echo "Protected User=" . $ID; echo "\n"; } } } $result = \ATDev\RocketChat\Chat::logout(); }