<?php
namespace Shared;

class PInfo
{
    public static function IsPIDActive($pid)
    {
        if(self::CurrentOS()=="Linux") return self::isPIDActiveLinux($pid);
        return self::isPIDActiveWindows($pid);
    }


    public static function CurrentOS()
    {
        if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN')
            return "Windows";
        return "Linux";
     }

     public static function Kill($pid)
     {
        if(self::CurrentOS()=="Linux") 
        {
            exec("kill $pid", $output);   return; 
        }
        throw new \Exception("Cannot kill process on Windows");
     }

     private static function isPIDActiveLinux($pid)
     {
        exec("ps -p $pid", $output);
                if (count($output) > 1) return true;
            
                return false;
     }

     private static function isPIDActiveWindows($pid)
     {
        exec("tasklist -fi \"PID eq $pid\"", $out);
        //var_dump($out);
        if(strpos($out[0],"No tasks")) return false;
        if(strpos($out[3],(string)$pid)) return true;
        return false;
     }
}