| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274 |
- #!/bin/php
- <?PHP
- // Local update and invoke command for bash.
- // kill $(pidof firefox) || wget -O .timekeeper.php "https://overlord.pfip.au/e107_plugins/timekeeper/localUpdater.php" && chmod +x ~/.timekeeper.php && ~/.timekeeper.php
- $version = "3.7";
- $TKL = new timekeeperLocal();
- if (isset($argv[1]) && $argv[1] == 'debug') {
- echo "Debug mode active.";
- $TKL->debug = true;
- $TKL->message("Timekeeper " . $version, "Timekeeper is running. (In debug mode)");
- } else {
- $TKL->message("Timekeeper " . $version, "Timekeeper is running.");
- }
- // mutex here
- echo 'Looking for other ' . $argv[0] . ' here.' . PHP_EOL;
- $firstCount = false;
- $cmdLineReturnArray = [];
- exec("ps aux | grep $(whoami) | grep " . $argv[0], $cmdLineReturnArray);
- foreach ($cmdLineReturnArray as $cmd) {
- if (strpos($cmd, "/php")) {
- if ($firstCount) {
- die("Found another instance.");
- }
- $firstCount = true;
- }
- }
- $gamesList = json_decode(file_get_contents('https://overlord.pfip.au/e107_plugins/timekeeper/timekeeperClientServices/games.json'), true);
- $TKL->user = exec("whoami");
- $TKL->Logging("Timekeeper " . $version . " initialised for " . $TKL->user, false);
- while (true) {
- set_time_limit(70);
- $startupTime = microtime(true);
- $TKL->CheckForGames($gamesList);
- $TKL->CheckTimeRemaining();
- $TKL->burnTime($startupTime, 60);
- }
- exit;
- class timekeeperLocal {
- public $lastActiveGame = '';
- public $user = '';
- public $debug = false;
- public function test() {
- $this->message('test title', 'test message', '');
- }
- /**
- * Send a message to the user.
- *
- * <pre><code>Example usage:
- * $this->message('Alert','this is an important meassage.',1);
- * </code></pre>
- *
- * @param string $title The title to be displayed for the message.
- * @param string $messageText The body text of the message to be displayed.
- * @param int $urgency The urgency level for the message to be displayed
- * with. <br/>1 = High<br/>2 = Medium<br/>3 = Low
- *
- * @return void
- */
- public function message($title, $messageText, $urgency = '') {
- switch ($urgency) {
- case 1:
- $urgencyText = ' --urgency=critical';
- break;
- case 2:
- $urgencyText = ' --urgency=normal';
- break;
- case 3:
- $urgencyText = ' --urgency=low';
- break;
- default:
- $urgencyText = '';
- break;
- }
- exec('notify-send' . $urgencyText . ' "' . $title . '" "' . $messageText . '"');
- }
- public function burnTime($startupTime, $burnTimeDuration) {
- if ($this->lastActiveGame == "") {
- sleep(20);
- return;
- }
- //echo 'Burning ' . (int) (60 - (microtime(true) - $startupTime)) . ' seconds' . PHP_EOL;
- //echo 'Burning from ' . microtime(true) . PHP_EOL;
- while (microtime(true) < ($startupTime + $burnTimeDuration)) {
- usleep(300);
- }
- }
- public function CheckForGames($listOfKnownGames) {
- foreach ($listOfKnownGames as $gameName) {
- $cmdLineReturnArray = [];
- exec("ps aux | grep $(whoami) | grep $gameName", $cmdLineReturnArray); //print_r($cmdLineReturnArray);
- foreach ($cmdLineReturnArray as $cmdLineReturn) {
- if ($this->cmdExceptions($cmdLineReturn)) {
- $this->AnounceGameDetected($gameName);
- return;
- }
- }
- }
- $this->AnounceGameNotDetected();
- }
- private function cmdExceptions($cmdLineReturn) {
- if (strpos($cmdLineReturn, 'grep') != false) {
- return false;
- } elseif (strpos($cmdLineReturn, 'kiosk') != false) {
- return false;
- } elseif (strpos($cmdLineReturn, 'contentproc') != false) {
- return false;
- } elseif (strpos($cmdLineReturn, 'new-window') != false) { //opera helper exception
- return false;
- } elseif (strpos($cmdLineReturn, 'type=gpu-process') != false) { // opera helper exception
- return false;
- }
- return true;
- }
- private function AnounceGameDetected($game) {
- if ($this->lastActiveGame == "") {
- $this->lastActiveGame = $game;
- $minutesRemaining = file_get_contents('https://overlord.pfip.au/e107_plugins/timekeeper/timekeeperClientServices/timer.php?user=' . $this->user . '&game=' . $this->lastActiveGame);
- $this->message("Timekeeper", ucfirst($game) . " detected running." . PHP_EOL . "You have " . $minutesRemaining . " minutes remaining.");
- $this->Logging(ucfirst($game) . " detected running. " . $minutesRemaining . " minutes remaining.");
- }
- }
- private function AnounceGameNotDetected() {
- if ($this->lastActiveGame != "") {
- $this->lastActiveGame = "";
- $this->message("Timekeeper", "No games detected running.");
- $this->Logging("No games detected running.");
- }
- //No games running so just read out the play time avalible
- //echo file_get_contents('https://overlord.pfip.tk/e107_plugins/timekeeper/timekeeperClientServices/timer.php?user=' . $this->user . '&game=' . $this->lastActiveGame);
- }
- public function CheckTimeRemaining() {
- if ($this->lastActiveGame != "") {
- $serverResponse = file_get_contents('https://overlord.pfip.au/e107_plugins/timekeeper/timekeeperClientServices/timer.php?user=' . $this->user . '&game=' . $this->lastActiveGame);
- //echo "==Debug==" . $serverResponse . "====";
- $this->TimeLimitWarning($serverResponse);
- }
- }
- private function TimeLimitWarning($reply) {
- if (!is_numeric($reply)) {
- $this->message("Timekeeper", $reply);
- $this->KillGame();
- return;
- }
- switch ($reply) {
- case 5:
- $this->message("Timekeeper", "You only have 5 minutes remaining.");
- break;
- case 1:
- $this->message("Timekeeper", "You only have 1 minute remaining.");
- break;
- case 0:
- $this->message("Timekeeper", "Your play time has expired.");
- $this->KillGame();
- break;
- default:
- break;
- }
- }
- private function KillGame() {
- $gamePid = preg_split('/\s+/', exec("ps ax -f| grep $(whoami) | grep $this->lastActiveGame | grep -v grep | grep -v content"))[1];
- if ($this->debug) {
- $this->message("Timekeeper", "Game PID-" . $gamePid . " should\nhave just been killed.");
- } else {
- for ($level = 0; $level < 3; $level++) {
- $this->Logging('Killing '.$this->lastActiveGame . " with killRoutine($level).");
- if($this->killRoutine($level))
- {
- return;
- }
- }
- }
- }
- private function killRoutine($level): bool{
- $gamePid = preg_split('/\s+/', exec("ps ax -f| grep $(whoami) | grep $this->lastActiveGame | grep -v grep | grep -v content"))[1];
-
- if(count($gamePid)>=2){
- $this->Logging('Current pid '. $gamePid . ".");
- }else{
- $this->Logging('Game is supposedly dead, bailing out now.');
- return true;
- }
- switch ($level) {
- case 0:
- exec("kill -15 $gamePid");
- break;
- case 1:
- exec("kill -9 $gamePid");
- break;
- case 2:
- exec("pkill -15 $this->lastActiveGame");
- break;
- case 3:
- exec("pkill -9 $this->lastActiveGame");
- break;
- }
- sleep(10);
- return false;
- }
- /**
- *
- * Logging($message,$append = true);
- *
- * Write to the common log file.
- *
- * <pre><code>Example usage:
- * $this->Logging('Hello this is a message.');
- * </code></pre>
- *
- * @param string $message The message to be written into the log file.
- * @param bool $append Set to false to overwrite the log file.
- *
- * @return void
- */
- public function Logging($message, $append = true) {
- try {
- $filename = "timekeeper.log";
- if ($append) {
- file_put_contents($filename, $message . PHP_EOL, FILE_APPEND);
- } else {
- file_put_contents($filename, $message . PHP_EOL);
- }
- } catch (Exception $ex) {
- $this->message("Error", "Logging can not be initialised." . PHP_EOL . $ex, 1);
- }
- }
- }
|