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 search() { if (isset($_GET["q"])) { $histories = DB::fetch("\nSELECT\n\tjobs.id as job_id, jobs.jobName, histories.id, run_date, time_taken, result\nFROM histories\nINNER JOIN jobs ON jobs.user_id = ? AND histories.jobs_id = jobs.id\nWHERE output LIKE ?\n", [$this->user->id, "%" . $_GET["q"] . "%"]); echo $this->loadRender("search.html", ["search" => $_GET["q"], "histories" => $histories]); } }
public function __construct($migrations = false) { $config = (include "system/engine/config-default.php"); if (is_file("application/config.php")) { $newconfig = (include "application/config.php"); } $this->config = array_merge($config, $newconfig); \vendor\DB\DB::$type = $config["DATABASE_TYPE"]; if ($this->config["USE_H20_TPL"]) { $this->tpl = new \H2o(null, array("searchpath" => getcwd() . "/application/views/", "cache_dir" => "application/tmp/", 'cache' => 'file')); } set_error_handler("\\system\\engine\\HF_Core::error_handler"); //set_exception_handler("\\system\\engine\\HF_Core::exception_handler"); if (!$migrations) { $this->findController(); } }
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"); } }
<?php use vendor\DB\DB; echo "Creating job table..." . PHP_EOL; DB::query("CREATE TABLE jobs (\n id INTEGER PRIMARY KEY NOT NULL AUTO_INCREMENT,\n jobName VARCHAR(255),\n runType VARCHAR(255),\n runScript TEXT,\n cron VARCHAR(255),\n failScript TEXT,\n last_run DATETIME,\n last_result INTEGER,\n hash VARCHAR(255),\n sharedkey VARCHAR(32),\n view_private TINYINT(1),\n user_id INTEGER,\n comments TEXT,\n force_run TINYINT(1)\n);"); //DB::query("INSERT INTO jobs VALUES (null, 'test', 1, 'TESTING', '*/5 * * * *', 'TESTING', '2015-01-01', 0, '123', '123', 0, 1, 'TEST COMMENT', 0)"); //DB::query("INSERT INTO jobs VALUES (null, 'test2', 1, 'TESTING', '*/5 * * * *', 'TESTING', '2015-01-01', 0, '321', '321', 1, 1, 'TEST COMMENT2', 0)");
<?php use vendor\DB\DB; echo "Creating session table..." . PHP_EOL; DB::query("CREATE TABLE sessions (\n id INTEGER PRIMARY KEY NOT NULL AUTO_INCREMENT,\n sessionid VARCHAR(255),\n ip VARCHAR(255),\n userAgent VARCHAR(255),\n data TEXT\n)"); echo "Creating users table..." . PHP_EOL; DB::query("CREATE TABLE users (\n id INTEGER PRIMARY KEY NOT NULL AUTO_INCREMENT,\n email VARCHAR(255)\n)"); //echo "Adding user " . PHP_EOL; //DB::insert("users", ["email" => "*****@*****.**"]);
<?php use vendor\DB\DB; echo "Creating history table..."; DB::query("CREATE TABLE histories (\n id INTEGER PRIMARY KEY NOT NULL AUTO_INCREMENT,\n output TEXT,\n jobs_id INTEGER,\n run_date DATETIME,\n time_taken DECIMAL(10,5),\n result INTEGER,\n nonce VARCHAR(255)\n );"); //DB::query("INSERT INTO histories VALUES (null, 'THIS IS ONLY A TEST', 1, '2015-01-01', 10, 0, 'ABC')");
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"); } } } }
public function runMigrations() { global $argv; $this->setupDatabaseConnection(); DB::query("CREATE TABLE IF NOT EXISTS migrations (\n\t\t\t\t\t\t\t id INTEGER PRIMARY KEY NOT NULL AUTO_INCREMENT,\n\t\t\t\t\t\t\t migration INTEGER,\n\t\t\t\t\t\t\t ran_at DATETIME\n\t\t\t\t)"); switch ($argv[1]) { case "show": foreach (DB::fetch("SELECT migration, ran_at FROM migrations") as $migration) { echo $migration["migration"] . " => " . $migration["ran_at"] . PHP_EOL; } break; case "count": echo DB::column("SELECT COUNT(id) FROM migrations"); break; case "run": $migrations = DB::fetch("SELECT migration FROM migrations"); $migrationArray = []; foreach ($migrations as $migration) { $migrationArray[] = $migration["migration"]; } foreach (glob("application/migrations/*.php") as $filename) { if (!in_array($filename, $migrationArray)) { try { include $filename; DB::insert("migrations", ["migration" => $filename, "ran_at" => (new \DateTime())->format("Y-m-d")]); } catch (\Exception $e) { echo "[HF_Core] - Migration error - {$e}"; exit(1); } } } break; case "clear": DB::query("DELETE FROM migrations"); break; case "reset": switch ($this->config["DATABASE_TYPE"]) { case "SQLITE": DB::$c = null; unlink($this->config["DATABASE_FILE"]); break; case "MYSQL": DB::query("DROP DATABASE " . $this->config['MYSQL_DBNAME']); DB::query("CREATE DATABASE " . $this->config['MYSQL_DBNAME']); break; } break; } }
<?php spl_autoload_extensions(".php"); // comma-separated list spl_autoload_register(); foreach (glob("system/vendor/*.php") as $filename) { include $filename; } if (!is_cli()) { die("This script must be ran from the command line"); } $core = new \system\engine\HF_Core(true); $core->setupDatabaseConnection(); if (count($argv) == 1) { echo "Possible commands are all-clear or adduser"; exit(0); } switch ($argv[1]) { case "all-clear": \vendor\DB\DB::query("DELETE FROM histories"); \vendor\DB\DB::query("DELETE FROM users"); \vendor\DB\DB::query("DELETE FROM sessions"); \vendor\DB\DB::query("DELETE FROM jobs"); break; case "adduser": $user = $argv[2]; \vendor\DB\DB::query("INSERT INTO users VALUES (null, ?)", [$user]); break; }