diff --git a/SUMMARY.md b/SUMMARY.md index a5121d2..4c87ad6 100755 --- a/SUMMARY.md +++ b/SUMMARY.md @@ -18,7 +18,7 @@ * [Css Coding Standards]({%}?page=standards/css) ## Application Examples -* [Application Sample]({%}?page=example_app/app) +* [Application Sample]({%}?page=example_app/web_app) * [Modules]({%}?page=example_module/module) * [Packages]({%}?page=example_module/packages) * [Language Files]({%}?page=basics/language_files) diff --git a/example_app/c_app.md b/example_app/c_app.md new file mode 100644 index 0000000..12a0639 --- /dev/null +++ b/example_app/c_app.md @@ -0,0 +1,177 @@ +/** + * Karaka + * + * @package App + * @copyright Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link https://jingga.app + */ + +#include +#include +#include + +#include "cOMS/Utils/ApplicationUtils.h" +#include "cOMS/DataStorage/Database/Connection/ConnectionAbstract.h" +#include "cOMS/Utils/Parser/Json.h" +#include "cOMS/Utils/OSWrapper.h" +#include "cOMS/Router/Router.h" +#include "cOMS/Threads/Thread.h" +#include "cOMS/Application/ApplicationAbstract.h" +#include "Models/ResourceMapper.h" + +#include "Routes.h" + +#ifndef OMS_DEMO + #define OMS_DEMO false +#endif + +Application::ApplicationAbstract app = {0}; + +int main(int argc, char **argv) +{ + /* --------------- Basic setup --------------- */ + + const char *arg = Utils::ApplicationUtils::compile_arg_line(argc, argv); + + // Set program path as cwd + char *cwd = Utils::ApplicationUtils::cwd(); + if (cwd == NULL) { + printf("Couldn't get the CWD\n"); + + return -1; + } + + char *cwdT = Utils::StringUtils::search_replace(cwd, "\\", "/"); + free(cwd); + cwd = cwdT; + + Utils::ApplicationUtils::chdir_application(cwd, argv[0]); + + // Check config + if (!Utils::FileUtils::file_exists("config.json")) { + Controller::ApiController::notInstalled(argc, argv); + + return -1; + } + + /* --------------- App setup --------------- */ + + // Load config + FILE *in = fopen("config.json", "r"); + if (in == NULL) { + printf("Couldn't open config.json\n"); + + return -1; + } + + app.config = nlohmann::json::parse(in); + + fclose(in); + + // Setup db connection + DataStorage::Database::DbConnectionConfig dbdata = { + DataStorage::Database::database_type_from_str(app.config["db"]["core"]["masters"]["admin"]["db"].get_ref().c_str()), + app.config["db"]["core"]["masters"]["admin"]["database"].get_ref().c_str(), + app.config["db"]["core"]["masters"]["admin"]["host"].get_ref().c_str(), + atoi(app.config["db"]["core"]["masters"]["admin"]["port"].get_ref().c_str()), + app.config["db"]["core"]["masters"]["admin"]["login"].get_ref().c_str(), + app.config["db"]["core"]["masters"]["admin"]["password"].get_ref().c_str(), + }; + + app.pool = Threads::pool_create(app.config["app"]["threads"]["count"].get()); + + app.db = DataStorage::Database::create_connection(dbdata); + app.db->connect(); + + /* --------------- Handle request --------------- */ + + // Handle routes + Router::Router router = generate_routes(&app); + Router::RouterFunc ptr = Router::match_route(&router, arg); + + // No endpoint found + if (ptr == NULL) { + ptr = &Controller::ApiController::printHelp; + } + + // Dispatch found endpoint + (*ptr)(argc, argv); + + /* --------------- Cleanup --------------- */ + + Threads::pool_destroy(app.pool); + + DataStorage::Database::free_MapperData((DataStorage::Database::MapperData *) &Models::ResourceMapper); + + app.db->close(); + app.db = NULL; + + Router::free_router(&router); + + free((char *) arg); + arg = NULL; + + // Reset CWD (don't know if this is necessary) + chdir(cwd); + + free(cwd); +} + + +/** + * Karaka + * + * @package Models + * @copyright Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link https://jingga.app + */ +#ifndef ROUTES_H +#define ROUTES_H + +#include +#include + +#include "Controller/ApiController.h" +#include "Controller/InstallController.h" +#include "cOMS/Router/Router.h" +#include "cOMS/Application/ApplicationAbstract.h" + +Router::Router generate_routes(Application::ApplicationAbstract *app) +{ + Controller::ApiController::app = app; + + Router::Router router = Router::create_router(4); + + Router::set(&router, "^.*?\\-h *.*$", (void *) &Controller::ApiController::printHelp); + Router::set(&router, "^.*?\\-v *.*$", (void *) &Controller::ApiController::printVersion); + Router::set(&router, "^.*?\\-r *.*$", (void *) &Controller::ApiController::checkResources); + Router::set(&router, "^.*?\\-\\-install *.*$", (void *) &Controller::InstallController::installApplication); + + return router; +} + +#endif + +namespace Controller { + namespace ApiController { + static Application::ApplicationAbstract *app = NULL; + + void printHelp(int argc, char **argv) + { + printf(" The Online Resource Watcher app developed by jingga checks online or local resources\n"); + printf(" for changes and informs the user about them.\n\n"); + printf(" Run: ./App ....\n\n"); + printf(" -h: Prints the help output\n"); + printf(" -v: Prints the version\n"); + printf("\n"); + printf(" Website: https://jingga.app\n"); + printf(" Copyright: jingga (c) Dennis Eichhorn\n"); + } + } +} + +#endif \ No newline at end of file diff --git a/example_app/app.md b/example_app/web_app.md similarity index 100% rename from example_app/app.md rename to example_app/web_app.md diff --git a/general/first_steps.md b/general/first_steps.md index aad8aa7..34ca1bd 100755 --- a/general/first_steps.md +++ b/general/first_steps.md @@ -4,10 +4,10 @@ After you installed the application and configured your development environment Please note that besides the general development guide the organization also provides various other organizational documents which help to understand the processes, development status and decisions made. -* [Development process](https://github.com/Karaka-Management/Organization-Guide/blob/master/Processes/Development.md) +* [Development process](https://github.com/Karaka-Management/Organization-Guide/blob/master/Processes/01_Development.md) * [Code inspection]({%}?page=quality/inspections) -* [Project status](https://github.com/Karaka-Management/Organization-Guide/blob/master/Project/PROJECT.md) -* [Code of conduct](https://github.com/Karaka-Management/Organization-Guide/blob/master/Policies%20%26%20Guidelines/Code%20of%20conduct.md) +* [Project status](https://github.com/orgs/Karaka-Management/projects/10) +* [Code of conduct](https://github.com/Karaka-Management/Organization-Guide/blob/master/Policies%20%26%20Guidelines/Code%20of%20Conduct.md) * [Conflict of interest](https://github.com/Karaka-Management/Organization-Guide/blob/master/Policies%20%26%20Guidelines/Conflict%20of%20Interest%20Policy.md) * [Activity Policy](https://github.com/Karaka-Management/Organization-Guide/blob/master/Policies%20%26%20Guidelines/Organization%20Activity%20Policy.md) * [Organization Guidelines](https://github.com/Karaka-Management/Organization-Guide/blob/master/Policies%20%26%20Guidelines/Organization%20Guidelines.md) diff --git a/general/setup.md b/general/setup.md index 68c48a5..f4815b8 100755 --- a/general/setup.md +++ b/general/setup.md @@ -52,7 +52,7 @@ After the installation you'll have access to the following content: Instead of calling `php demoSetup/setup.php` which only uses a single thread you may also run `php demoSetup/setup.php -a 0` which will execute the install script in multiple threads leading to most likely 100% CPU and storage usage but therfore significantly reduce the execution time. -> You might want to call the setup script as a different user to ensure the same permissions `sudo -u wwww-data php demoSetup/setup.php` +> You may want to call the setup script as a different user to ensure the same permissions `sudo -u wwww-data php demoSetup/setup.php` #### Annotation diff --git a/services/validation.md b/services/validation.md index d1f1105..101bd17 100755 --- a/services/validation.md +++ b/services/validation.md @@ -2,6 +2,20 @@ The built in validation allows to quickly validate some basic data formats. +## Guarding + +The `Guard` provides basic protection function for comman mistakes and angles of attacks: + +### Path guarding + +Usually no user input should be used for path construction but in some cases this might not be possible. With the path guarding you can check if the generated path would lead outside the applications root path. + +```php +public static function isSafePath(string $path, string $base = '') : bool; +``` + +By leaving the `$base` path empty the function automatically restricts the `$path` to the parent directory of the framework. + ## Validator The `Validator` provides basic validations with: