diff --git a/Utils/ImageUtils.php b/Utils/ImageUtils.php new file mode 100644 index 000000000..f6e4322e1 --- /dev/null +++ b/Utils/ImageUtils.php @@ -0,0 +1,40 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ +namespace phpOMS\Utils; + +/** + * Image utils class. + * + * This class provides static helper functionalities for images. + * + * @category Framework + * @package phpOMS\Utils + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ +class ImageUtils +{ + public static function decodeBase64Image(string $img) : string + { + $img = str_replace('data:image/png;base64,', '', $img); + $img = str_replace(' ', '+', $img); + + return base64_decode($img); + } +} diff --git a/Utils/TaskSchedule/Schedule.php b/Utils/TaskSchedule/Schedule.php index 63ebe1745..16664e121 100644 --- a/Utils/TaskSchedule/Schedule.php +++ b/Utils/TaskSchedule/Schedule.php @@ -37,15 +37,9 @@ class Schedule extends TaskAbstract implements \Serializable * @since 1.0.0 * @author Dennis Eichhorn */ - public function __construct(Interval $interval = null, string $cmd = '') + public function __construct(string $name, string $cmd = '') { - if (!isset($interval)) { - $this->interval = new Interval(); - } else { - $this->interval = $interval; - } - - $this->command = $cmd; + parent::__construct($name, $cmd); } /** diff --git a/Utils/TaskSchedule/SchedulerFactory.php b/Utils/TaskSchedule/SchedulerFactory.php index 083ec0b95..09e360f85 100644 --- a/Utils/TaskSchedule/SchedulerFactory.php +++ b/Utils/TaskSchedule/SchedulerFactory.php @@ -42,7 +42,7 @@ final class SchedulerFactory * @since 1.0.0 * @author Dennis Eichhorn */ - public static function create() : ScheduleInterface + public static function create() : SchedulerAbstract { switch (OperatingSystem::getSystem()) { case SystemType::WIN: diff --git a/Utils/TaskSchedule/TaskAbstract.php b/Utils/TaskSchedule/TaskAbstract.php index 0a457ca64..7f645d660 100644 --- a/Utils/TaskSchedule/TaskAbstract.php +++ b/Utils/TaskSchedule/TaskAbstract.php @@ -37,21 +37,42 @@ abstract class TaskAbstract protected $id = ''; /** - * Interval. - * - * @var Interval - * @since 1.0.0 - */ - protected $interval = null; - - /** - * Command. + * Command used for creating the task * * @var string * @since 1.0.0 */ protected $command = ''; + /** + * Command/script to run. + * + * @var string + * @since 1.0.0 + */ + protected $run = ''; + + protected $status = ''; + + protected $nextRunTime = null; + + protected $lastRunTime = null; + + protected $start = null; + + protected $end = null; + + protected $comment = ''; + + protected $results = []; + + protected $author = ''; + + public function __construct(string $name, string $cmd = '') { + $this->id = $name; + $this->command = $cmd; + } + /** * Get id. * @@ -65,34 +86,6 @@ abstract class TaskAbstract return $this->id; } - /** - * Get interval. - * - * @return Interval - * - * @since 1.0.0 - * @author Dennis Eichhorn - */ - public function getInterval() : Interval - { - return $this->interval; - } - - /** - * Set interval. - * - * @param Interval $interval Interval - * - * @return void - * - * @since 1.0.0 - * @author Dennis Eichhorn - */ - public function setInterval(Interval $interval) - { - $this->interval = $interval; - } - /** * Get command. * @@ -120,4 +113,241 @@ abstract class TaskAbstract { $this->command = $command; } + + /** + * Get run. + * + * @return string + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function getRun() : string + { + return $this->run; + } + + /** + * Set run. + * + * @param string $run Command/script to run + * + * @return void + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function setRun(string $run) + { + $this->run = $run; + } + + /** + * Get status. + * + * @return string + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function getStatus() : string + { + return $this->status; + } + + /** + * Set status. + * + * @param string $status Status + * + * @return void + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function setStatus(string $status) + { + $this->status = $status; + } + + /** + * Get next run time. + * + * @return \DateTime + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function getNextRunTime() + { + return $this->nextRunTime; + } + + /** + * Set next run time. + * + * @param \DateTime $nextRunTime Next run time + * + * @return void + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function setNextRunTime(\DateTime $nextRunTime) + { + $this->nextRuntime = $nextRunTime; + } + + /** + * Get last run time. + * + * @return \DateTime + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function getLastRuntime() + { + return $this->lastRunTime; + } + + /** + * Set last run time. + * + * @param \DateTime $lastRunTime Last run time + * + * @return void + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function setLastRuntime(\DateTime $lastRunTime) + { + $this->lastRunTime = $lastRunTime; + } + + /** + * Get start. + * + * @return \DateTime + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function getStart() + { + return $this->start; + } + + /** + * Set start. + * + * @param \DateTime $start Start + * + * @return void + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function setStart(\DateTime $start) + { + $this->start = $start; + } + + /** + * Get end. + * + * @return \DateTime + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function getEnd() + { + return $this->end; + } + + /** + * Set end. + * + * @param \DateTime $end End + * + * @return void + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function setEnd(\DateTime $end) + { + $this->end = $end; + } + + /** + * Get author. + * + * @return string + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function getAuthor() : string + { + return $this->author; + } + + /** + * Set author. + * + * @param string $author Author + * + * @return void + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function setAuthor(string $author) + { + $this->author = $author; + } + + /** + * Get comment. + * + * @return string + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function getComment() : string + { + return $this->comment; + } + + /** + * Set comment. + * + * @param string $comment Comment + * + * @return void + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function setComment(string $comment) + { + $this->comment = $comment; + } + + /** + * Get comment. + * + * @return string + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function addResult(string $result) + { + $this->results[] = $result; + } } diff --git a/Utils/TaskSchedule/TaskFactory.php b/Utils/TaskSchedule/TaskFactory.php index 9e42e5e50..db20362d9 100644 --- a/Utils/TaskSchedule/TaskFactory.php +++ b/Utils/TaskSchedule/TaskFactory.php @@ -35,7 +35,7 @@ final class TaskFactory /** * Create task instance. * - * @param Interval $interval Task interval + * @param string $id Task id * @param string $cmd Command to run * * @return TaskInterface @@ -45,13 +45,13 @@ final class TaskFactory * @since 1.0.0 * @author Dennis Eichhorn */ - public static function create(Interval $interval = null, string $cmd = '') : TaskInterface + public static function create(string $id = null, string $cmd = '') : TaskAbstract { switch (OperatingSystem::getSystem()) { case SystemType::WIN: - return new Schedule($interval, $cmd); + return new Schedule($id, $cmd); case SystemType::LINUX: - return new CronJob($interval, $cmd); + return new CronJob($id, $cmd); default: throw new \Exception('Unsupported system.'); } diff --git a/Utils/TaskSchedule/TaskScheduler.php b/Utils/TaskSchedule/TaskScheduler.php index 81f847587..dc92396e2 100644 --- a/Utils/TaskSchedule/TaskScheduler.php +++ b/Utils/TaskSchedule/TaskScheduler.php @@ -15,6 +15,8 @@ */ namespace phpOMS\Utils\TaskSchedule; +use phpOMS\Validation\Base\DateTime; + /** * Task scheduler class. * @@ -29,7 +31,7 @@ namespace phpOMS\Utils\TaskSchedule; class TaskScheduler extends SchedulerAbstract { - const BIN = ''; + protected static $bin = 'c:/WINDOWS/system32/schtasks.exe'; /** * Env variables. @@ -39,6 +41,29 @@ class TaskScheduler extends SchedulerAbstract */ private $envOptions = []; + /** + * Test git. + * + * @return bool + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function test() : bool + { + $pipes = []; + $resource = proc_open(escapeshellarg(self::$bin), [1 => ['pipe', 'w'], 2 => ['pipe', 'w']], $pipes); + + $stdout = stream_get_contents($pipes[1]); + $stderr = stream_get_contents($pipes[2]); + + foreach ($pipes as $pipe) { + fclose($pipe); + } + + return trim(proc_close($resource)) !== 127; + } + public function save() { @@ -56,9 +81,9 @@ class TaskScheduler extends SchedulerAbstract * @since 1.0.0 * @author Dennis Eichhorn */ - public function run(string $cmd) : array + private function run(string $cmd) : string { - $cmd = 'cd ' . escapeshellarg(dirname(self::BIN)) . ' && ' . basename(self::BIN) . ' -C ' . escapeshellarg(__DIR__) . ' ' . $cmd; + $cmd = 'cd ' . escapeshellarg(dirname(self::$bin)) . ' && ' . basename(self::$bin) . ' ' . $cmd; $pipes = []; $desc = [ @@ -92,9 +117,52 @@ class TaskScheduler extends SchedulerAbstract return trim($stdout); } + private function normalize(string $raw) : string + { + return str_replace("\r\n", "\n", $raw); + } + + private function parseJobList(array $jobData) { + $job = TaskFactory::create($jobData[1], ''); + + $job->setRun($jobData[8]); + $job->setStatus($jobData[3]); + + if(DateTime::isValid($jobData[2])) { + $job->setNextRunTime(new \DateTime($jobData[2])); + } + + if(DateTime::isValid($jobData[5])) { + $job->setLastRunTime(new \DateTime($jobData[5])); + } + + $job->setAuthor($jobData[7]); + $job->setComment($jobData[10]); + + if(DateTime::isValid($jobData[20])) { + $job->setStart(new \DateTime($jobData[20])); + } + + if(DateTime::isValid($jobData[21])) { + $job->setEnd(new \DateTime($jobData[21])); + } + + $job->addResult($jobData[6]); + + return $job; + } + public function getAll() : array { - return str_getcsv($this->run('/query /v /fo CSV')); + $lines = explode("\n", $this->normalize($this->run('/query /v /fo CSV'))); + unset($lines[0]); + + $jobs = []; + foreach($lines as $line) { + $jobs[] = $this->parseJobList(str_getcsv($line)); + } + + return $jobs; } public function get(string $id) @@ -107,9 +175,32 @@ class TaskScheduler extends SchedulerAbstract } - public function getAllByName(string $name) : array + public function getAllByName(string $name, bool $exact = true) : array { - return str_getcsv($this->run('/query /v /fo CSV /tn ' . escapeshellarg($name))); + if($exact) { + $lines = $this->run('/query /v /fo CSV /tn ' . escapeshellarg($name)); + unset($lines[0]); + + $jobs = []; + foreach($lines as $line) { + $jobs[] = $this->parseJobList(str_getcsv($line)); + } + } else { + $lines = explode("\n", $this->normalize($this->run('/query /v /fo CSV'))); + $jobs = []; + + unset($lines[0]); + + foreach($lines as $key => $line) { + $line = str_getcsv($line); + + if(strpos($line[1], $name) !== false) { + $jobs[] = $this->parseJobList($line); + } + } + } + + return $jobs; } public function create(Schedule $task)