public static function getByField($field, $value) { $function = new \ReflectionClass(get_called_class()); $table = strtolower($function->getShortName()); $fields = implode(", ", DB::getColumns($table)); return DB::fetchObject("SELECT {$fields} FROM {$table} WHERE {$field} = ?", get_called_class(), [$value]); }
public function view($id) { $idArr = explode("-", $id); try { if (count($idArr) == 2) { /** @var \application\models\Histories $historyArr */ $historyArr = DB::fetchObject("SELECT * FROM histories WHERE jobs_id = ? ORDER BY run_date DESC", '\\application\\models\\Histories', [$idArr[1]]); /** @var \application\models\Jobs[] $jobObject */ $jobObject = \application\models\Jobs::getByField("id", $idArr[1]); if ($this->checkAccess($jobObject[0])) { $csv = []; /** @var \application\models\histories $history */ foreach ($historyArr as $history) { $csv[] = "\"" . str_replace("-", "/", $history->run_date) . "," . $history->time_taken . "," . $history->getRawSize() . "," . $history->result . "\\n\""; } echo $this->loadRender("history.html", ["csv" => implode("+", $csv), "job" => $jobObject[0], "histories" => $historyArr]); } } } catch (\Exception $e) { header("Location: /kritbit"); } }
public function run() { if (in_array($_SERVER["REMOTE_ADDR"], $this->config["ACCEPTED_IPS"])) { // not very secure - but worst case they fire off the run early if (!file_exists("/tmp/kritbot")) { touch("/tmp/kritbot"); try { /** @var \application\models\Jobs[] $jobs */ $jobs = DB::fetchObject("SELECT * FROM jobs", "\\application\\models\\Jobs"); foreach ($jobs as $job) { if ($job->runType == 1) { $cron = Cron\CronExpression::factory($job->cron); if ($cron->isDue() || $job->force_run == 1) { $output = []; $returnVar = 0; $jobName = isset($job->jobName) && !empty($job->jobName) && $job->jobName ? $job->jobName : "----NOT-SET----"; $dir = __DIR__ . DIRECTORY_SEPARATOR . ".." . DIRECTORY_SEPARATOR . "tmp" . DIRECTORY_SEPARATOR . $jobName; if (is_dir($dir)) { $this->rrmdir($dir . DIRECTORY_SEPARATOR); mkdir($dir, 0777, true); } else { mkdir($dir, 0777, true); } $start = microtime(true); // grumble grumble something something windows if (stripos(php_uname("s"), "Win") !== false) { file_put_contents("{$dir}/kritscript.bat", $job->runScript); exec("c:\\windows\\system32\\cmd.exe /c {$dir}\\kritscript.bat", $output, $returnVar); } else { file_put_contents("{$dir}/kritscript", $job->runScript); chmod("{$dir}/kritscript", 0777); exec("{$dir}/kritscript", $output, $returnVar); } $end = microtime(true); $delta = $end - $start; $scriptOutput = implode("\n", $output); if ($returnVar != 0) { if (stripos(php_uname("s"), "Win") !== false) { file_put_contents("{$dir}/failkritscript.bat", $job->failScript); exec("c:\\windows\\system32\\cmd.exe /c {$dir}\failkirtscript.bat"); } else { file_put_contents("{$dir}/failkritscript", $job->failScript); chmod("{$dir}/failkritscript", 0777); exec("{$dir}/failkritscript", $output, $returnVar); } } $historyObj = new \application\models\Histories(); $historyObj->output = $scriptOutput; $historyObj->result = $returnVar; $historyObj->time_taken = $delta; $historyObj->jobs_id = $job->id; $now = date("Y-m-d H:i:s"); $historyObj->run_date = $now; $historyObj->save(); $job->force_run = 0; $job->last_run = $now; $job->last_result = $returnVar; $job->save(); } } } unlink("/tmp/kritbot"); } catch (\Exception $e) { unlink("/tmp/kritbot"); } } } }