diff --git a/web/application/controllers/base.php b/web/application/controllers/base.php index 1105b54..cd722f6 100644 --- a/web/application/controllers/base.php +++ b/web/application/controllers/base.php @@ -4,9 +4,12 @@ use \application\models\Sessions; abstract class base extends \system\engine\HF_Controller { + /** @var \application\models\Users $user */ + protected $user = null; protected $session = null; protected $sessionData = null; - public function isLoggedIn() { + protected $loginRequired = true; + protected function isLoggedIn() { if (!$this->sessionData && !isset($this->sessionData->userId)) { header("Location: /login"); return false; @@ -14,27 +17,13 @@ abstract class base extends \system\engine\HF_Controller { return true; } } - public function __construct($config, $core, $tpl) - { - parent::__construct($config, $core, $tpl); - if ($this->config["DATABASE_TYPE"] == "SQLITE") { - $this->pdo = new PDO("sqlite:kritbot.sqlite3"); - \vendor\DB\DB::$c = $this->pdo; - } else { - $this->pdo = new PDO( - "mysql:dbname={$this->config['MYSQL_DBNAME']};host={$this->config['MYSQL_HOST']}", - $this->config['MYSQL_USER'], - $this->config['MYSQL_PASS'], - array( - PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8", - PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ, - PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION - ) - ); - \vendor\DB\DB::$c = $this->pdo; - } + protected function loadRender($template, $parameters=array()) { + $newParameters = array_merge($parameters, ["user" => $this->user]); + return parent::loadRender($template, $newParameters); + } + protected function isUserLoggedIn() { if (isset($_COOKIE["session"])) { $validSession = Sessions::getByField("sessionid", $_COOKIE["session"]); if ($validSession) { @@ -42,20 +31,28 @@ abstract class base extends \system\engine\HF_Controller { $this->session = $validSession[0]; $this->sessionData = json_decode($this->session->data); if ($this->sessionData == null) { - return; + return false; } $this->user = \application\models\Users::getByField("id", $this->sessionData->userId)[0]; + return true; } catch (\Exception $e) { - setcookie("session", "", time() - 3600); - header("Location: /login"); + return false; } } else { - setcookie("session", "", time() - 3600); - header("Location: /login"); + return false; } + } + return false; + } + + protected function login() { + if (isset($_COOKIE["session"])) { + if (!$this->user) { + header("Location: /login"); + } } else { $bool = true; - $bytes = openssl_random_pseudo_bytes (10, $bool); + $bytes = openssl_random_pseudo_bytes(10, $bool); $sessionId = bin2hex($bytes); $this->session = new Sessions(); $this->session->ip = $_SERVER["REMOTE_ADDR"]; @@ -64,6 +61,32 @@ abstract class base extends \system\engine\HF_Controller { $this->session->save(); setcookie("session", $sessionId, 2147483647); } + } + + public function __construct($config, $core, $tpl) + { + parent::__construct($config, $core, $tpl); + if ($this->config["DATABASE_TYPE"] == "SQLITE") { + $this->pdo = new PDO("sqlite:kritbot.sqlite3"); + \vendor\DB\DB::$c = $this->pdo; + } else { + $this->pdo = new PDO( + "mysql:dbname={$this->config['MYSQL_DBNAME']};host={$this->config['MYSQL_HOST']}", + $this->config['MYSQL_USER'], + $this->config['MYSQL_PASS'], + array( + PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8", + PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ, + PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION + ) + ); + \vendor\DB\DB::$c = $this->pdo; + } + + $this->isUserLoggedIn(); + if ($this->loginRequired) { + $this->login(); + } } } \ No newline at end of file diff --git a/web/application/controllers/history.php b/web/application/controllers/history.php new file mode 100644 index 0000000..1895267 --- /dev/null +++ b/web/application/controllers/history.php @@ -0,0 +1,42 @@ +view_private == 1 && !$this->user) { + header("Location: /login"); + return false; + } + if ($job->view_private == 1 && $this->user && $this->user->id != $job->user_id) { + header("Location: /"); + return false; + } + return true; + } + + public function view($id) { + $idArr = explode("-", $id); + if (count($idArr) == 2) { + /** @var \application\models\Histories $historyArr */ + $historyArr = \application\models\Histories::getByField("jobs_id", $idArr[1]); + /** @var \application\models\Jobs[] $jobObject */ + $jobObject = \application\models\Jobs::getByField("id", $idArr[1]); + if ($this->checkAccess($jobObject[0])) { + echo $this->loadRender("history.html", ["jobid" => $idArr[1], "histories" => $historyArr]); + } + } + } + + public function log($jobId, $logId) { + $jobObject = \application\models\Jobs::getByField("id", $jobId); + if ($this->checkAccess($jobObject[0])) { + /** @var \application\models\Histories[] $historyArr */ + $historyArr = \application\models\Histories::getByField("id", $logId); + echo $historyArr[0]->output; + } + + + } +} \ No newline at end of file diff --git a/web/application/controllers/job.php b/web/application/controllers/job.php new file mode 100644 index 0000000..d70d50a --- /dev/null +++ b/web/application/controllers/job.php @@ -0,0 +1,41 @@ +loadRender("add.html"); + } else { + $data = $_POST; + $data["user_id"] = $this->user->id; + \application\models\Jobs::create($data)->save(); + header("Location: /"); + } + } + + public function edit($id) { + /** @var \application\models\Jobs $job */ + $job = \application\models\Jobs::getByField("id", $id); + if ($job && $job[0]->user_id == $this->user->id) { //secuirty check + if (isset($_POST["jobName"])) { + $job[0]->update($_POST)->save(); + header("Location: /"); + } else { + echo $this->loadRender("add.html", ["job" => $job[0]]); + } + } else { + header("Location: /"); + } + } + + public function delete($id) { + $job = \application\models\Jobs::getByField("id", $id); + if ($job && $job[0]->user_id == $this->user->id) { //secuirty check + $job[0]->deleteRelated(["histories"]); + $job[0]->delete(); + header("Location: /"); + } else { + header("Location: /"); + } + } + +} \ No newline at end of file diff --git a/web/application/controllers/login.php b/web/application/controllers/login.php index 3b6ddea..67f0da6 100644 --- a/web/application/controllers/login.php +++ b/web/application/controllers/login.php @@ -5,6 +5,9 @@ use system\vendor\phpoauthlib2\OAuth; use application\models\Users; class login extends base { + + protected $loginRequired = false; + private function accessDenied() { return "ACCESS DENIED"; } diff --git a/web/application/controllers/main.php b/web/application/controllers/main.php index 77d71d4..b9744a9 100644 --- a/web/application/controllers/main.php +++ b/web/application/controllers/main.php @@ -4,12 +4,7 @@ class main extends base { public function index() { - - if ($this->isLoggedIn()) { - echo "Hello - " . $this->sessionData->userId; - echo "email = " . $this->user->email; - } - //echo "hello"; - + $jobs = \application\models\Jobs::getByField("user_id", $this->user->id); + echo $this->loadRender("main.html", ["jobs" => $jobs]); } } \ No newline at end of file diff --git a/web/application/migrations/1.php b/web/application/migrations/1.php index a8181ff..591bffe 100644 --- a/web/application/migrations/1.php +++ b/web/application/migrations/1.php @@ -1,5 +1,7 @@ runType) { + case "1": + return "Ran by Kritbit"; + break; + case "2": + return "External Source"; + break; + } + return ""; + } + + public function getLastRun() { + if ($this->last_run == "") { + return "Never"; + } else { + return $this->last_run; + } + + } +} \ No newline at end of file diff --git a/web/application/models/Sessions.php b/web/application/models/Sessions.php index 04be54a..cfdc3f9 100644 --- a/web/application/models/Sessions.php +++ b/web/application/models/Sessions.php @@ -3,7 +3,6 @@ namespace application\models; class Sessions extends \system\engine\HF_Model { - public $id; public $sessionid; public $ip; public $userAgent; diff --git a/web/application/models/Users.php b/web/application/models/Users.php index c6bc6d3..b80c42e 100644 --- a/web/application/models/Users.php +++ b/web/application/models/Users.php @@ -6,6 +6,5 @@ use system\engine\HF_Model; class Users extends HF_Model { - public $id; public $email; } \ No newline at end of file diff --git a/web/application/views/add.html b/web/application/views/add.html new file mode 100644 index 0000000..3473649 --- /dev/null +++ b/web/application/views/add.html @@ -0,0 +1,95 @@ +{% extends "base.html" %} + +{% block content %} + + + +
+ +
+ +
+ +
+
+
+
+ +
+ + +
+
+
+ + +
+ +
+ +
+
+
+
+ +
+ +
+ +
+
+
+
+ +
+ +
+ +
+
+ +
+ +
+ +
+
+ + + +
+
+ {% if job %} + + {% else %} + + {% endif %} +
+
+
+ + + +{% endblock %} \ No newline at end of file diff --git a/web/application/views/base.html b/web/application/views/base.html index 17e53d4..06bf58b 100644 --- a/web/application/views/base.html +++ b/web/application/views/base.html @@ -3,10 +3,10 @@ - - + + - + @@ -17,8 +17,13 @@ {{title}} + +{% include "menu.html" %} +{% block menu %}{% endblock %} {% block content %}{% endblock %} \ No newline at end of file diff --git a/web/application/views/history.html b/web/application/views/history.html new file mode 100644 index 0000000..f89249d --- /dev/null +++ b/web/application/views/history.html @@ -0,0 +1,35 @@ +{% extends "base.html" %} + +{% block content %} + + + + + + + + + + + + + + {% if !histories %} + + + + {% endif %} + {% for history in histories %} + + + + + + + {% endfor %} + +
OutputRun DateTime TakenResult
No results
View{{history.run_date}}{{history.time_taken}}{{history.result}}
+ +{% endblock %} \ No newline at end of file diff --git a/web/application/views/login.html b/web/application/views/login.html deleted file mode 100644 index 76c2e6f..0000000 --- a/web/application/views/login.html +++ /dev/null @@ -1,60 +0,0 @@ -{% extends "base.html" %} - -{% block content %} - - - - -{% endblock %} \ No newline at end of file diff --git a/web/application/views/main.html b/web/application/views/main.html new file mode 100644 index 0000000..ccb167f --- /dev/null +++ b/web/application/views/main.html @@ -0,0 +1,38 @@ +{% extends "base.html" %} + +{% block content %} + + + + + + + + + + + + + + + + + + {% for job in jobs %} + + + + + + + + + + + {% endfor %} + +
EditNameRun TypeCronLast RunLast ResultHistoryDelete
Edit{{job.jobName}}{{job.getRunType}}{{job.cron}}{{job.getLastRun}}{{job.last_result}}HistoryDelete
+ +{% endblock %} \ No newline at end of file diff --git a/web/application/views/menu.html b/web/application/views/menu.html index 2474551..d867994 100644 --- a/web/application/views/menu.html +++ b/web/application/views/menu.html @@ -1,3 +1,4 @@ +{% block menu %} \ No newline at end of file + +{% endblock %} \ No newline at end of file diff --git a/web/migrations.php b/web/migrations.php index beadd5d..92985f2 100644 --- a/web/migrations.php +++ b/web/migrations.php @@ -1,11 +1,12 @@ runMigrations(); \ No newline at end of file diff --git a/web/system/engine/HF_Controller.php b/web/system/engine/HF_Controller.php index 1c2826c..e70ff29 100644 --- a/web/system/engine/HF_Controller.php +++ b/web/system/engine/HF_Controller.php @@ -15,7 +15,7 @@ class HF_Controller $this->core = $core; } - public function loadRender($template, $parameters=array()) + protected function loadRender($template, $parameters=array()) { $this->tpl->loadTemplate($template); return $this->tpl->render($parameters); diff --git a/web/system/engine/HF_Core.php b/web/system/engine/HF_Core.php index 7e43f51..ece75e0 100644 --- a/web/system/engine/HF_Core.php +++ b/web/system/engine/HF_Core.php @@ -21,7 +21,7 @@ class HF_Core private $config = array(); private $tpl; - public function __construct() + public function __construct($migrations=false) { $config = include("system/engine/config-default.php"); if (is_file("application/config.php")) @@ -38,7 +38,8 @@ class HF_Core )); set_error_handler("\\system\\engine\\HF_Core::error_handler"); //set_exception_handler("\\system\\engine\\HF_Core::exception_handler"); - $this->findController(); + if (!$migrations) + $this->findController(); } public static function exception_handler($e) { @@ -108,7 +109,7 @@ class HF_Core include_once($path . $arr[$i] . ".php"); if ($i + 1 < count($arr)) // if there is a define after the controller name - this would be the method name { - $this->loadController(new $arr[$i]($this->config, $this, $this->tpl), $arr[$i], $arr[$i+1], array_slice ($arr, 2)); + $this->loadController(new $arr[$i]($this->config, $this, $this->tpl), $arr[$i], $arr[$i+1], array_slice ($arr, 3)); } else { // call index $this->loadController(new $arr[$i]($this->config, $this, $this->tpl), $arr[$i], "index"); } @@ -140,10 +141,10 @@ class HF_Core if (is_file(getcwd() . "/application/status.php")) { include_once (getcwd() . "/application/status.php"); - $this->loadController(new HF_Status($this->config, $this, $this->tpl), "HF_Status", "Status404"); + $this->loadController(new HF_Status($this->config, $this, $this->tpl), "\\system\\engine\\HF_Status", "Status404"); } else { include_once(getcwd() . "/system/engine/status.php"); - $this->loadController(new HF_Status($this->config, $this, $this->tpl), "HF_Status", "Status404"); + $this->loadController(new HF_Status($this->config, $this, $this->tpl), "\\system\\engine\\HF_Status", "Status404"); } } @@ -327,8 +328,13 @@ class HF_Core foreach (glob("application/migrations/*.php") as $filename) { if (!in_array($filename, $migrationArray)) { - include $filename; - DB::insert("migrations", ["migration" => $filename, "ran_at" => (new \DateTime())->format("Y-m-d")]); + 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); + } } diff --git a/web/system/engine/HF_Model.php b/web/system/engine/HF_Model.php index f408f0d..58e7634 100644 --- a/web/system/engine/HF_Model.php +++ b/web/system/engine/HF_Model.php @@ -6,20 +6,19 @@ use \vendor\DB\DB; abstract class HF_Model { - protected $id = null; - public static function saveFromArray($data) { - $fieldMap = []; - $table = strtolower(get_class()); + public $id = null; + public static function create($data) { + + $obj = new static(); + $function = new \ReflectionClass(get_called_class()); + $table = strtolower($function->getShortName()); + foreach(DB::getColumns($table) as $column) { - $fieldMap[$column] = $data[$column]; - } - if ($fieldMap["id"] == null) { - DB::insert($table, $fieldMap); - } else { - $updateFields = $fieldMap; - unset($updateFields["id"]); - DB::update($table, $updateFields, $fieldMap["id"]); + if (isset($data[$column])) { + $obj->$column = $data[$column]; + } } + return $obj; } public function save() { @@ -38,6 +37,36 @@ abstract class HF_Model { } } + public function update($data) { + $function = new \ReflectionClass(get_called_class()); + $table = strtolower($function->getShortName()); + foreach(DB::getColumns($table) as $column) { + if ($column == "id" || strpos($column, "_id") !== false) { + continue; // Don't allow to override id + } + if (isset($data[$column])) { + $this->$column = $data[$column]; + } + } + return $this; + } + + public function delete() { + $function = new \ReflectionClass(get_called_class()); + $table = strtolower($function->getShortName()); + if ($this->id) { + DB::query("DELETE FROM $table WHERE id = " . $this->id); + } + } + + public function deleteRelated($tables = []) { + $function = new \ReflectionClass(get_called_class()); + $table = strtolower($function->getShortName()); + foreach($tables as $relatedTable) { + DB::query("DELETE FROM $relatedTable WHERE $table" . "_id = " . $this->id); + } + } + public static function getByField($field, $value) { $function = new \ReflectionClass(get_called_class()); $table = strtolower($function->getShortName()); diff --git a/web/system/vendor/h2o.php b/web/system/vendor/h2o.php index 5b55079..c264b81 100644 --- a/web/system/vendor/h2o.php +++ b/web/system/vendor/h2o.php @@ -11,6 +11,7 @@ require H2O_ROOT.'h2o/tags.php'; require H2O_ROOT.'h2o/errors.php'; require H2O_ROOT.'h2o/filters.php'; require H2O_ROOT.'h2o/context.php'; +require H2O_ROOT.'h2o/parser.php'; /** * Example: