|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Pterodactyl - Panel |
| 4 | + * Copyright (c) 2015 - 2016 Dane Everitt <dane@daneeveritt.com> |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in all |
| 14 | + * copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | + * SOFTWARE. |
| 23 | + */ |
| 24 | +namespace Pterodactyl\Repositories; |
| 25 | + |
| 26 | +use Cron; |
| 27 | +use Validator; |
| 28 | + |
| 29 | +use Pterodactyl\Models; |
| 30 | + |
| 31 | +use Pterodactyl\Exceptions\DisplayValidationException; |
| 32 | +use Pterodactyl\Exceptions\DisplayException; |
| 33 | + |
| 34 | +class TaskRepository |
| 35 | +{ |
| 36 | + |
| 37 | + protected $defaults = [ |
| 38 | + 'year' => '*', |
| 39 | + 'day_of_week' => '*', |
| 40 | + 'month' => '*', |
| 41 | + 'day_of_month' => '*', |
| 42 | + 'hour' => '*', |
| 43 | + 'minute' => '*/30', |
| 44 | + ]; |
| 45 | + |
| 46 | + protected $actions = [ |
| 47 | + 'command', |
| 48 | + 'power', |
| 49 | + ]; |
| 50 | + |
| 51 | + public function __construct() |
| 52 | + { |
| 53 | + // |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Create a new scheduled task for a given server. |
| 58 | + * @param int $id |
| 59 | + * @param array $data |
| 60 | + * |
| 61 | + * @throws DisplayException |
| 62 | + * @throws DisplayValidationException |
| 63 | + * @return void |
| 64 | + */ |
| 65 | + public function create($id, $data) |
| 66 | + { |
| 67 | + $server = Models\Server::findOrFail($id); |
| 68 | + |
| 69 | + $validator = Validator::make($data, [ |
| 70 | + 'action' => 'string|required', |
| 71 | + 'data' => 'string|required', |
| 72 | + 'year' => 'string|sometimes|required', |
| 73 | + 'day_of_week' => 'string|sometimes|required', |
| 74 | + 'month' => 'string|sometimes|required', |
| 75 | + 'day_of_month' => 'string|sometimes|required', |
| 76 | + 'hour' => 'string|sometimes|required', |
| 77 | + 'minute' => 'string|sometimes|required' |
| 78 | + ]); |
| 79 | + |
| 80 | + if ($validator->fails()) { |
| 81 | + throw new DisplayValidationException(json_encode($validator->errors())); |
| 82 | + } |
| 83 | + |
| 84 | + if (!in_array($data['action'], $this->actions)) { |
| 85 | + throw new DisplayException('The action provided is not valid.'); |
| 86 | + } |
| 87 | + |
| 88 | + $cron = $this->defaults; |
| 89 | + foreach ($this->defaults as $setting => $value) { |
| 90 | + if (array_key_exists($setting, $data)) { |
| 91 | + $cron[$setting] = $data[$setting]; |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + // Check that is this a valid Cron Entry |
| 96 | + try { |
| 97 | + $buildCron = Cron::factory(sprintf('%s %s %s %s %s %s', |
| 98 | + $cron['minute'], |
| 99 | + $cron['hour'], |
| 100 | + $cron['day_of_month'], |
| 101 | + $cron['month'], |
| 102 | + $cron['day_of_week'], |
| 103 | + $cron['year'] |
| 104 | + )); |
| 105 | + } catch (\Exception $ex) { |
| 106 | + throw new DisplayException($ex); |
| 107 | + } |
| 108 | + |
| 109 | + $task = new Models\Task; |
| 110 | + $task->fill([ |
| 111 | + 'server' => $server->id, |
| 112 | + 'active' => 1, |
| 113 | + 'action' => $data['action'], |
| 114 | + 'data' => $data['data'], |
| 115 | + 'queued' => 0, |
| 116 | + 'year' => $cron['year'], |
| 117 | + 'day_of_week' => $cron['day_of_week'], |
| 118 | + 'month' => $cron['month'], |
| 119 | + 'day_of_month' => $cron['day_of_month'], |
| 120 | + 'hour' => $cron['hour'], |
| 121 | + 'minute' => $cron['minute'], |
| 122 | + 'last_run' => null, |
| 123 | + 'next_run' => $buildCron->getNextRunDate() |
| 124 | + ]); |
| 125 | + |
| 126 | + return $task->save(); |
| 127 | + |
| 128 | + } |
| 129 | + |
| 130 | +} |
0 commit comments