src/API.php
public function postMap(): array
    {
        $auth = $this->auth;
        return [
            ['POST', '/Navigation', function (array $request) use ($auth) {
                if ($auth->allow(["min" => 2, "max" => 1]) === false) {
                    return [
                        "headers" => [
                            [
                                'X-PHP-Response-Code: 401',
                                true,
                                401
                            ]
                        ],
                        "response" => "Not logged in with proper credentials"
                    ];
                }
                if (isset($request['class']) === true && $request['class'] === '') {
                    unset($request['class']);
                }
                if (isset($request['type']) === true && $request['type'] === '') {
                    unset($request['type']);
                }
                if (isset($request['navigation_block']) === true && $request['navigation_block'] === '') {
                    unset($request['navigation_block']);
                }
                if (isset($request['navigation_id']) === true && $request['navigation_id'] === '') {
                    unset($request['navigation_id']);
                }
                $validator = new \Valitron\Validator($request);
                $validator->rule(function ($field, $value, $params, $fields) {
                    if ((empty($value) === true && empty($fields['link']) === true)
                        || (empty($value) === false && empty($fields['link']) === false)) {
                        return false;
                    } elseif (empty($value) === false) {
                        $page = Model\Page::find($value);
                        if ($page === null) {
                            return false;
                        }
                    }
                    return true;
                }, "page_id")->message("{field} is required, must be unique, without a link");
                $validator->rule(function ($field, $value, $params, $fields) {
                    if ((empty($value) === true && empty($fields['page_id']) === true)
                        || (empty($value) === false && empty($fields['page_id']) === false)) {
                        return false;
                    }
                    return (bool) filter_var("https://google.com" . $value, FILTER_VALIDATE_URL);
                }, "link")->message("{field} is requried without a page ID");
                $validator->rule('required', ['title']);
                $validator->rule('optional', ['class', 'type', 'navigation_block', 'navigation_id']);
                $validator->rule('numeric', ['type', 'navigation_block']);
                $validator->rule(function ($field, $value, $params, $fields) {
                    if (is_array($value) === true || preg_match('/[\w\-]+(,\s?[\w\-]+)*/', $value) === 1) {
                        return true;
                    }
                    return false;
                }, "class")->message("{field} must be an array or comma-separated");
                $validator->rule(function ($field, $value, $params, $fields) {
                    $navigation = Model\Navigation::find($value);
                    if ($navigation === null) {
                        return false;
                    }
                    return true;
                }, "navigation_id")->message("{field} must be child of a valid navigation link");
                if ($validator->validate() === false) {
                    return [
                        "headers" => [
                            [
                                'X-PHP-Response-Code: 422',
                                true,
                                422
                            ]
                        ],
                        "response" => $validator->errors()
                    ];
                }
                $navigation = new Model\Navigation;
                $navigation->title = $request['title'];
                if (isset($request['page_id']) === true) {
                    $navigation->page_id = $request['page_id'];
                } elseif (isset($request['link']) === true) {
                    $navigation->page_id = null;
                    $navigation->link = $request['link'];
                } else {
                    $navigation->page_id = null;
                    $navigation->link = "#";
                }
                if (isset($request['class']) === true) {
                    if (is_string($request['class']) === true) {
                        $request['class'] = explode(",", $request['class']);
                        foreach ($request['class'] as $key => $class) {
                            $request['class'][$key] = trim($class);
                        }
                    }
                    $navigation->class = $request['class'];
                } else {
                    $navigation->class = null;
                }
                if (isset($request['type']) === true) {
                    $navigation->type = $request['type'];
                }
                if (isset($request['navigation_block']) === true) {
                    $navigation->navigation_block = $request['navigation_block'];
                }
                if (isset($request['navigation_id']) === true) {
                    $navigation->navigation_id = $request['navigation_id'];
                }
                $navigation->save();
                return [
                    "headers" => [
                        [
                            'X-PHP-Response-Code: 200',
                            true,
                            200
                        ]
                    ],
                    "response" => $navigation->id
                ];
            }],
            ['POST', '/Page', function (array $request) use ($auth) {
                if ($auth->allow(["min" => 3, "max" => 1]) === false) {
                    return [
                        "headers" => [
                            [
                                'X-PHP-Response-Code: 401',
                                true,
                                401
                            ]
                        ],
                        "response" => "Not logged in with proper credentials"
                    ];
                }
                if (isset($request['byline']) === true && $request['byline'] === '') {
                    unset($request['byline']);
                }
                if (isset($request['content']) === true && $request['content'] === '') {
                    unset($request['content']);
                }
                if (isset($request['link']) === true && $request['link'] === '') {
                    unset($request['link']);
                }
                if (isset($request['featured']) === true && $request['featured'] === '') {
                    unset($request['featured']);
                }
                if (isset($request['tags']) === true && $request['tags'] === '') {
                    unset($request['tags']);
                }
                if (isset($request['class']) === true && $request['class'] === '') {
                    unset($request['class']);
                }
                if (isset($request['template']) === true && $request['template'] === '') {
                    unset($request['template']);
                }
                if (isset($request['status']) === true && $request['status'] === '') {
                    unset($request['status']);
                }
                if (isset($request['type']) === true && $request['type'] === '') {
                    unset($request['type']);
                }
                if (isset($request['user_id']) === true && $request['user_id'] === '') {
                    unset($request['user_id']);
                }
                if (isset($request['page_id']) === true && $request['page_id'] === '') {
                    unset($request['page_id']);
                }
                $validator = new \Valitron\Validator($request);
                $validator->rule('required', ['title']);
                $validator->rule('optional', ['byline', 'content', 'link', 'featured', 'tags', 'class', 'template', 'status', 'type', 'user_id', 'page_id']);
                $validator->rule(function ($field, $value, array $params, array $fields) {
                    return is_string($value);
                }, ['title', 'byline', 'content', 'template'])->message("{field} must be a string");
                $validator->rule(function ($field, $value, array $params, array $fields) {
                    return is_bool($value) || in_array($value, [0, 1]);
                }, 'status');
                $validator->rule(function ($field, $value, array $params, array $fields) {
                    return file_exists(rtrim(public_path(), '/') . $value) || (filter_var($value, FILTER_VALIDATE_URL) !== false);
                }, 'featured')->message('{field} must be an external link or a local file path');
                $validator->rule('numeric', 'type');
                $validator->rule(function ($field, $value, $params, $fields) {
                    if (is_array($value) === true || preg_match('/[\w\-]+(,\s?[\w\-]+)*/', $value) === 1) {
                        return true;
                    }
                    return false;
                }, ["tags", "class"])->message("{field} must be an array or comma-separated");
                $validator->rule(function ($field, $value, $params, $fields) {
                    if (empty($value) === true) {
                        return true;
                    }
                    $page = Model\Page::find($value);
                    if ($page === null) {
                        return false;
                    }
                    return true;
                }, "page_id")->message("{field} must be child of a valid page");
                $validator->rule(function ($field, $value, $params, $fields) {
                    $user = Model\User::find($value);
                    if ($user === null) {
                        return false;
                    }
                    return true;
                }, "user_id")->message("{field} must be authored by a valid author");
                $validator->rule(function ($field, $value, $params, $fields) {
                    $page = Model\Page::withTrashed()->where('link', $value)->first();
                    if ($page === null) {
                        return true;
                    }
                    return false;
                }, "link")->message("{field} must be unique");
                if ($validator->validate() === false) {
                    return [
                        "headers" => [
                            [
                                'X-PHP-Response-Code: 422',
                                true,
                                422
                            ]
                        ],
                        "response" => $validator->errors()
                    ];
                }
                $page = new Model\Page;
                $page->title = $request['title'];
                if (isset($request['byline']) === true) {
                    $page->byline = $request['byline'];
                } else {
                    $page->byline = null;
                }
                if (isset($request['content']) === true) {
                    $page->content = $request['content'];
                } else {
                    $page->content = null;
                }
                if (isset($request['featured']) === true) {
                    $page->featured = $request['featured'];
                } else {
                    $page->featured = null;
                }
                if (isset($request['tags']) === true) {
                    if (is_string($request['tags']) === true) {
                        $request['tags'] = explode(",", $request['tags']);
                        foreach ($request['tags'] as $key => $tags) {
                            $request['tags'][$key] = trim($tags);
                        }
                    }
                    $page->tags = $request['tags'];
                } else {
                    $page->tags = [];
                }
                if (isset($request['class']) === true) {
                    if (is_string($request['class']) === true) {
                        $request['class'] = explode(",", $request['class']);
                        foreach ($request['class'] as $key => $class) {
                            $request['class'][$key] = trim($class);
                        }
                    }
                    $page->class = $request['class'];
                } else {
                    $page->class = [];
                }
                if (isset($request['template']) === true) {
                    $page->template = $request['template'];
                }
                if (isset($request['link']) === true) {
                    $page->link = $request['link'];
                } else {
                    $page->link = '/' . slugify($page->title);
                    $i = 1;
                    while (Model\Page::withTrashed()->where("link", $page->link)->first() != null) {
                        $i ++;
                        $page->link = '/' . slugify($page->title) . '-' . $i;
                    }
                }
                if ($page->link[0] != '/') {
                    $page->link = '/' . $page->link;
                }
                $page->link = rtrim($page->link, "/");
                if (isset($request['status']) === true) {
                    $page->status = (bool) $request['status'];
                } else {
                    $page->status = false;
                }
                if (isset($request['type']) === true) {
                    $page->type = $request['type'];
                }
                if (isset($request['page_id']) === true) {
                    $page->page_id = $request['page_id'];
                }
                if (isset($request['user_id']) === true) {
                    $page->user_id = $request['user_id'];
                } else {
                    $page->user_id = $auth->details->id;
                }
                $page->save();
                return [
                    "headers" => [
                        [
                            'X-PHP-Response-Code: 200',
                            true,
                            200
                        ]
                    ],
                    "response" => $page->id
                ];
            }],
            ['POST', '/User', function (array $request) use ($auth) {
                $password_check = ['username', 'name', 'email', 'avatar', 'description'];
                if (isset($request['name']) === true && $request['name'] === '') {
                    unset($request['name']);
                    unset($password_check[1]);
                }
                if (isset($request['email']) === true && $request['email'] === '') {
                    unset($request['email']);
                    unset($password_check[2]);
                }
                if (isset($request['avatar']) === true && $request['avatar'] === '') {
                    unset($request['avatar']);
                    unset($password_check[3]);
                }
                if (isset($request['description']) === true && $request['description'] === '') {
                    unset($request['description']);
                    unset($password_check[4]);
                }
                if (isset($request['role_id']) === true && $request['role_id'] === '') {
                    unset($request['role_id']);
                }
                $validator = new \Valitron\Validator($request);
                $validator->rule('required', ['username', 'password', 'confirm_password']);
                $validator->rule('optional', ['name', 'email', 'avatar', 'description', 'role_id']);
                $validator->rule(function ($field, $value, array $params, array $fields) {
                    return is_string($value);
                }, ['username', 'password', 'confirm_password', 'name', 'description'])->message("{field} must be a string");
                $validator->rule('equals', 'confirm_password', 'password');
                $validator->rule('lengthBetween', 'username', 6, 200);
                $validator->rule('lengthBetween', 'password', 8, 80);
                $validator->rule('notIn', 'password', $password_check);
                $validator->rule('email', 'email');
                $validator->rule(function ($field, $value, array $params, array $fields) {
                    return file_exists(rtrim(public_path(), '/') . $value) || (filter_var($value, FILTER_VALIDATE_URL) !== false);
                }, 'avatar')->message('{field} must be an external link or a local file path');
                $validator->rule(function ($field, $value, $params, $fields) {
                    $role = Model\Role::find($value);
                    if ($role === null) {
                        return false;
                    }
                    return true;
                }, "role_id")->message("{field} must be a valid role");
                if ($validator->validate() === false) {
                    return [
                        "headers" => [
                            [
                                'X-PHP-Response-Code: 422',
                                true,
                                422
                            ]
                        ],
                        "response" => $validator->errors()
                    ];
                }
                $user = new Model\User;
                $user->user = $request['username'];
                $user->pass = $request['password'];
                if (isset($request['name']) === true) {
                    $user->name = $request['name'];
                } else {
                    $user->name = null;
                }
                if (isset($request['email']) === true) {
                    $user->email = $request['email'];
                } else {
                    $user->email = null;
                }
                if (isset($request['avatar']) === true) {
                    $user->avatar = $request['avatar'];
                } else {
                    $user->avatar = null;
                }
                if (isset($request['description']) === true) {
                    $user->description = $request['description'];
                } else {
                    $user->description = null;
                }
                $confirm_code = md5(uniqid("", true));
                while (Model\User::where("confirm_code", $confirm_code)->first() != null) {
                    $confirm_code = md5(uniqid("", true));
                }
                $user->confirm_code = $confirm_code;
                $user->reset_code = null;
                if ($auth->allow(["min" => 1, "max" => 1]) === true && isset($request['role_id']) === true) {
                    $user->role_id = $request['role_id'];
                }
                $user->save();
                return [
                    "headers" => [
                        [
                            'X-PHP-Response-Code: 200',
                            true,
                            200
                        ]
                    ],
                    "response" => [
                        "id" => $user->id,
                        "confirm_code" => $confirm_code
                    ]
                ];
            }],
            ['POST', '/User/confirm/[a:confirm_code]', function (string $confirm_code = null) {
                if (isset($confirm_code) === false) {
                    throw new \InvalidArgumentException("No user found");
                }
                $user = Model\User::where("confirm_code", $confirm_code)->first();
                if ($user === null) {
                    throw new \DomainException("No user found");
                }
                $user->confirm_code = null;
                $user->save();
                return [
                    "headers" => [
                        [
                            'X-PHP-Response-Code: 200',
                            true,
                            200
                        ]
                    ],
                    "response" => true
                ];
            }],
            ['POST', '/User/forgot/[*:email]', function (string $email = null) {
                if (isset($email) === false) {
                    throw new \InvalidArgumentException("No user found");
                }
                $user = Model\User::where("email", $email)->first();
                if ($user === null) {
                    throw new \DomainException("No user found");
                }
                $reset_code = md5(uniqid("", true));
                while (Model\User::where("reset_code", $reset_code)->first() != null) {
                    $reset_code = md5(uniqid("", true));
                }
                $user->reset_code = $reset_code;
                $user->save();
                return [
                    "headers" => [
                        [
                            'X-PHP-Response-Code: 200',
                            true,
                            200
                        ]
                    ],
                    "response" => $reset_code
                ];
            }],
            ['POST', '/User/reset/[a:reset_code]', function (string $reset_code = null, array $request) {
                if (isset($reset_code) === false) {
                    throw new \InvalidArgumentException("No user found");
                }
                $user = Model\User::where("reset_code", $reset_code)->first();
                if ($user === null) {
                    throw new \DomainException("No user found");
                }
                $validator = new \Valitron\Validator($request);
                $validator->rule('required', ['password', 'confirm_password']);
                $validator->rule(function ($field, $value, array $params, array $fields) {
                    return is_string($value);
                }, ['password', 'confirm_password'])->message("{field} must be a string");
                $validator->rule('equals', 'confirm_password', 'password');
                $validator->rule('lengthBetween', 'password', 8, 80);
                if ($validator->validate() === false) {
                    return [
                        "headers" => [
                            [
                                'X-PHP-Response-Code: 422',
                                true,
                                422
                            ]
                        ],
                        "response" => $validator->errors()
                    ];
                }
                $user->pass = $request['password'];
                $user->reset_code = null;
                $user->save();
                return [
                    "headers" => [
                        [
                            'X-PHP-Response-Code: 200',
                            true,
                            200
                        ]
                    ],
                    "response" => true
                ];
            }],
            ['POST', '/User/login/[u:username]', function (string $username = null, array $request) use ($auth) {
                if (isset($username) === false) {
                    return [
                        "headers" => [
                            [
                                'X-PHP-Response-Code: 422',
                                true,
                                422
                            ]
                        ],
                        "response" => [
                            "user" => "Username required",
                        ]
                    ];
                }
                $user = Model\User::where("user", $username)->first();
                if ($user === null) {
                    return [
                        "headers" => [
                            [
                                'X-PHP-Response-Code: 422',
                                true,
                                422
                            ]
                        ],
                        "response" => [
                            "user" => "Invalid username or password",
                            "pass" => "Invalid username or password"
                        ]
                    ];
                }
                if (empty($request['password']) === true || is_string($request['password']) === false) {
                    return [
                        "headers" => [
                            [
                                'X-PHP-Response-Code: 422',
                                true,
                                422
                            ]
                        ],
                        "response" => [
                            "user" => "Invalid username or password",
                            "pass" => "Invalid username or password"
                        ]
                    ];
                }
                if ($user->checkPass($request['password']) === true) {
                    $session = new Model\Session;
                    $api_session = md5(uniqid("", true));
                    while (Model\Session::where("code", $api_session)->first() != null) {
                        $api_session = md5(uniqid("", true));
                    }
                    $session->code = $api_session;
                    $user->sessions()->save($session);
                    $user->reset_code = null;
                    $user->save();
                    $_SESSION['api_session'] = $api_session;
                    $auth->validate($api_session);
                    return [
                        "headers" => [
                            [
                                'X-PHP-Response-Code: 200',
                                true,
                                200
                            ]
                        ],
                        "response" => $api_session
                    ];
                } else {
                    return [
                        "headers" => [
                            [
                                'X-PHP-Response-Code: 422',
                                true,
                                422
                            ]
                        ],
                        "response" => [
                            "user" => "Invalid username or password",
                            "pass" => "Invalid username or password"
                        ]
                    ];
                }
            }],
        ];
    }
public function postMap(): array
    {
        $auth = $this->auth;
        return [
            ['POST', '/Navigation', function (array $request) use ($auth) {
                if ($auth->allow(["min" => 2, "max" => 1]) === false) {
                    return [
                        "headers" => [
                            [
                                'X-PHP-Response-Code: 401',
                                true,
                                401
                            ]
                        ],
                        "response" => "Not logged in with proper credentials"
                    ];
                }
                if (isset($request['class']) === true && $request['class'] === '') {
                    unset($request['class']);
                }
                if (isset($request['type']) === true && $request['type'] === '') {
                    unset($request['type']);
                }
                if (isset($request['navigation_block']) === true && $request['navigation_block'] === '') {
                    unset($request['navigation_block']);
                }
                if (isset($request['navigation_id']) === true && $request['navigation_id'] === '') {
                    unset($request['navigation_id']);
                }
                $validator = new \Valitron\Validator($request);
                $validator->rule(function ($field, $value, $params, $fields) {
                    if ((empty($value) === true && empty($fields['link']) === true)
                        || (empty($value) === false && empty($fields['link']) === false)) {
                        return false;
                    } elseif (empty($value) === false) {
                        $page = Model\Page::find($value);
                        if ($page === null) {
                            return false;
                        }
                    }
                    return true;
                }, "page_id")->message("{field} is required, must be unique, without a link");
                $validator->rule(function ($field, $value, $params, $fields) {
                    if ((empty($value) === true && empty($fields['page_id']) === true)
                        || (empty($value) === false && empty($fields['page_id']) === false)) {
                        return false;
                    }
                    return (bool) filter_var("https://google.com" . $value, FILTER_VALIDATE_URL);
                }, "link")->message("{field} is requried without a page ID");
                $validator->rule('required', ['title']);
                $validator->rule('optional', ['class', 'type', 'navigation_block', 'navigation_id']);
                $validator->rule('numeric', ['type', 'navigation_block']);
                $validator->rule(function ($field, $value, $params, $fields) {
                    if (is_array($value) === true || preg_match('/[\w\-]+(,\s?[\w\-]+)*/', $value) === 1) {
                        return true;
                    }
                    return false;
                }, "class")->message("{field} must be an array or comma-separated");
                $validator->rule(function ($field, $value, $params, $fields) {
                    $navigation = Model\Navigation::find($value);
                    if ($navigation === null) {
                        return false;
                    }
                    return true;
                }, "navigation_id")->message("{field} must be child of a valid navigation link");
                if ($validator->validate() === false) {
                    return [
                        "headers" => [
                            [
                                'X-PHP-Response-Code: 422',
                                true,
                                422
                            ]
                        ],
                        "response" => $validator->errors()
                    ];
                }
                $navigation = new Model\Navigation;
                $navigation->title = $request['title'];
                if (isset($request['page_id']) === true) {
                    $navigation->page_id = $request['page_id'];
                } elseif (isset($request['link']) === true) {
                    $navigation->page_id = null;
                    $navigation->link = $request['link'];
                } else {
                    $navigation->page_id = null;
                    $navigation->link = "#";
                }
                if (isset($request['class']) === true) {
                    if (is_string($request['class']) === true) {
                        $request['class'] = explode(",", $request['class']);
                        foreach ($request['class'] as $key => $class) {
                            $request['class'][$key] = trim($class);
                        }
                    }
                    $navigation->class = $request['class'];
                } else {
                    $navigation->class = null;
                }
                if (isset($request['type']) === true) {
                    $navigation->type = $request['type'];
                }
                if (isset($request['navigation_block']) === true) {
                    $navigation->navigation_block = $request['navigation_block'];
                }
                if (isset($request['navigation_id']) === true) {
                    $navigation->navigation_id = $request['navigation_id'];
                }
                $navigation->save();
                return [
                    "headers" => [
                        [
                            'X-PHP-Response-Code: 200',
                            true,
                            200
                        ]
                    ],
                    "response" => $navigation->id
                ];
            }],
            ['POST', '/Page', function (array $request) use ($auth) {
                if ($auth->allow(["min" => 3, "max" => 1]) === false) {
                    return [
                        "headers" => [
                            [
                                'X-PHP-Response-Code: 401',
                                true,
                                401
                            ]
                        ],
                        "response" => "Not logged in with proper credentials"
                    ];
                }
                if (isset($request['byline']) === true && $request['byline'] === '') {
                    unset($request['byline']);
                }
                if (isset($request['content']) === true && $request['content'] === '') {
                    unset($request['content']);
                }
                if (isset($request['link']) === true && $request['link'] === '') {
                    unset($request['link']);
                }
                if (isset($request['featured']) === true && $request['featured'] === '') {
                    unset($request['featured']);
                }
                if (isset($request['tags']) === true && $request['tags'] === '') {
                    unset($request['tags']);
                }
                if (isset($request['class']) === true && $request['class'] === '') {
                    unset($request['class']);
                }
                if (isset($request['template']) === true && $request['template'] === '') {
                    unset($request['template']);
                }
                if (isset($request['status']) === true && $request['status'] === '') {
                    unset($request['status']);
                }
                if (isset($request['type']) === true && $request['type'] === '') {
                    unset($request['type']);
                }
                if (isset($request['user_id']) === true && $request['user_id'] === '') {
                    unset($request['user_id']);
                }
                if (isset($request['page_id']) === true && $request['page_id'] === '') {
                    unset($request['page_id']);
                }
                $validator = new \Valitron\Validator($request);
                $validator->rule('required', ['title']);
                $validator->rule('optional', ['byline', 'content', 'link', 'featured', 'tags', 'class', 'template', 'status', 'type', 'user_id', 'page_id']);
                $validator->rule(function ($field, $value, array $params, array $fields) {
                    return is_string($value);
                }, ['title', 'byline', 'content', 'template'])->message("{field} must be a string");
                $validator->rule(function ($field, $value, array $params, array $fields) {
                    return is_bool($value) || in_array($value, [0, 1]);
                }, 'status');
                $validator->rule(function ($field, $value, array $params, array $fields) {
                    return file_exists(rtrim(public_path(), '/') . $value) || (filter_var($value, FILTER_VALIDATE_URL) !== false);
                }, 'featured')->message('{field} must be an external link or a local file path');
                $validator->rule('numeric', 'type');
                $validator->rule(function ($field, $value, $params, $fields) {
                    if (is_array($value) === true || preg_match('/[\w\-]+(,\s?[\w\-]+)*/', $value) === 1) {
                        return true;
                    }
                    return false;
                }, ["tags", "class"])->message("{field} must be an array or comma-separated");
                $validator->rule(function ($field, $value, $params, $fields) {
                    if (empty($value) === true) {
                        return true;
                    }
                    $page = Model\Page::find($value);
                    if ($page === null) {
                        return false;
                    }
                    return true;
                }, "page_id")->message("{field} must be child of a valid page");
                $validator->rule(function ($field, $value, $params, $fields) {
                    $user = Model\User::find($value);
                    if ($user === null) {
                        return false;
                    }
                    return true;
                }, "user_id")->message("{field} must be authored by a valid author");
                $validator->rule(function ($field, $value, $params, $fields) {
                    $page = Model\Page::withTrashed()->where('link', $value)->first();
                    if ($page === null) {
                        return true;
                    }
                    return false;
                }, "link")->message("{field} must be unique");
                if ($validator->validate() === false) {
                    return [
                        "headers" => [
                            [
                                'X-PHP-Response-Code: 422',
                                true,
                                422
                            ]
                        ],
                        "response" => $validator->errors()
                    ];
                }
                $page = new Model\Page;
                $page->title = $request['title'];
                if (isset($request['byline']) === true) {
                    $page->byline = $request['byline'];
                } else {
                    $page->byline = null;
                }
                if (isset($request['content']) === true) {
                    $page->content = $request['content'];
                } else {
                    $page->content = null;
                }
                if (isset($request['featured']) === true) {
                    $page->featured = $request['featured'];
                } else {
                    $page->featured = null;
                }
                if (isset($request['tags']) === true) {
                    if (is_string($request['tags']) === true) {
                        $request['tags'] = explode(",", $request['tags']);
                        foreach ($request['tags'] as $key => $tags) {
                            $request['tags'][$key] = trim($tags);
                        }
                    }
                    $page->tags = $request['tags'];
                } else {
                    $page->tags = [];
                }
                if (isset($request['class']) === true) {
                    if (is_string($request['class']) === true) {
                        $request['class'] = explode(",", $request['class']);
                        foreach ($request['class'] as $key => $class) {
                            $request['class'][$key] = trim($class);
                        }
                    }
                    $page->class = $request['class'];
                } else {
                    $page->class = [];
                }
                if (isset($request['template']) === true) {
                    $page->template = $request['template'];
                }
                if (isset($request['link']) === true) {
                    $page->link = $request['link'];
                } else {
                    $page->link = '/' . slugify($page->title);
                    $i = 1;
                    while (Model\Page::withTrashed()->where("link", $page->link)->first() != null) {
                        $i ++;
                        $page->link = '/' . slugify($page->title) . '-' . $i;
                    }
                }
                if ($page->link[0] != '/') {
                    $page->link = '/' . $page->link;
                }
                $page->link = rtrim($page->link, "/");
                if (isset($request['status']) === true) {
                    $page->status = (bool) $request['status'];
                } else {
                    $page->status = false;
                }
                if (isset($request['type']) === true) {
                    $page->type = $request['type'];
                }
                if (isset($request['page_id']) === true) {
                    $page->page_id = $request['page_id'];
                }
                if (isset($request['user_id']) === true) {
                    $page->user_id = $request['user_id'];
                } else {
                    $page->user_id = $auth->details->id;
                }
                $page->save();
                return [
                    "headers" => [
                        [
                            'X-PHP-Response-Code: 200',
                            true,
                            200
                        ]
                    ],
                    "response" => $page->id
                ];
            }],
            ['POST', '/User', function (array $request) use ($auth) {
                $password_check = ['username', 'name', 'email', 'avatar', 'description'];
                if (isset($request['name']) === true && $request['name'] === '') {
                    unset($request['name']);
                    unset($password_check[1]);
                }
                if (isset($request['email']) === true && $request['email'] === '') {
                    unset($request['email']);
                    unset($password_check[2]);
                }
                if (isset($request['avatar']) === true && $request['avatar'] === '') {
                    unset($request['avatar']);
                    unset($password_check[3]);
                }
                if (isset($request['description']) === true && $request['description'] === '') {
                    unset($request['description']);
                    unset($password_check[4]);
                }
                if (isset($request['role_id']) === true && $request['role_id'] === '') {
                    unset($request['role_id']);
                }
                $validator = new \Valitron\Validator($request);
                $validator->rule('required', ['username', 'password', 'confirm_password']);
                $validator->rule('optional', ['name', 'email', 'avatar', 'description', 'role_id']);
                $validator->rule(function ($field, $value, array $params, array $fields) {
                    return is_string($value);
                }, ['username', 'password', 'confirm_password', 'name', 'description'])->message("{field} must be a string");
                $validator->rule('equals', 'confirm_password', 'password');
                $validator->rule('lengthBetween', 'username', 6, 200);
                $validator->rule('lengthBetween', 'password', 8, 80);
                $validator->rule('notIn', 'password', $password_check);
                $validator->rule('email', 'email');
                $validator->rule(function ($field, $value, array $params, array $fields) {
                    return file_exists(rtrim(public_path(), '/') . $value) || (filter_var($value, FILTER_VALIDATE_URL) !== false);
                }, 'avatar')->message('{field} must be an external link or a local file path');
                $validator->rule(function ($field, $value, $params, $fields) {
                    $role = Model\Role::find($value);
                    if ($role === null) {
                        return false;
                    }
                    return true;
                }, "role_id")->message("{field} must be a valid role");
                if ($validator->validate() === false) {
                    return [
                        "headers" => [
                            [
                                'X-PHP-Response-Code: 422',
                                true,
                                422
                            ]
                        ],
                        "response" => $validator->errors()
                    ];
                }
                $user = new Model\User;
                $user->user = $request['username'];
                $user->pass = $request['password'];
                if (isset($request['name']) === true) {
                    $user->name = $request['name'];
                } else {
                    $user->name = null;
                }
                if (isset($request['email']) === true) {
                    $user->email = $request['email'];
                } else {
                    $user->email = null;
                }
                if (isset($request['avatar']) === true) {
                    $user->avatar = $request['avatar'];
                } else {
                    $user->avatar = null;
                }
                if (isset($request['description']) === true) {
                    $user->description = $request['description'];
                } else {
                    $user->description = null;
                }
                $confirm_code = md5(uniqid("", true));
                while (Model\User::where("confirm_code", $confirm_code)->first() != null) {
                    $confirm_code = md5(uniqid("", true));
                }
                $user->confirm_code = $confirm_code;
                $user->reset_code = null;
                if ($auth->allow(["min" => 1, "max" => 1]) === true && isset($request['role_id']) === true) {
                    $user->role_id = $request['role_id'];
                }
                $user->save();
                return [
                    "headers" => [
                        [
                            'X-PHP-Response-Code: 200',
                            true,
                            200
                        ]
                    ],
                    "response" => [
                        "id" => $user->id,
                        "confirm_code" => $confirm_code
                    ]
                ];
            }],
            ['POST', '/User/confirm/[a:confirm_code]', function (string $confirm_code = null) {
                if (isset($confirm_code) === false) {
                    throw new \InvalidArgumentException("No user found");
                }
                $user = Model\User::where("confirm_code", $confirm_code)->first();
                if ($user === null) {
                    throw new \DomainException("No user found");
                }
                $user->confirm_code = null;
                $user->save();
                return [
                    "headers" => [
                        [
                            'X-PHP-Response-Code: 200',
                            true,
                            200
                        ]
                    ],
                    "response" => true
                ];
            }],
            ['POST', '/User/forgot/[*:email]', function (string $email = null) {
                if (isset($email) === false) {
                    throw new \InvalidArgumentException("No user found");
                }
                $user = Model\User::where("email", $email)->first();
                if ($user === null) {
                    throw new \DomainException("No user found");
                }
                $reset_code = md5(uniqid("", true));
                while (Model\User::where("reset_code", $reset_code)->first() != null) {
                    $reset_code = md5(uniqid("", true));
                }
                $user->reset_code = $reset_code;
                $user->save();
                return [
                    "headers" => [
                        [
                            'X-PHP-Response-Code: 200',
                            true,
                            200
                        ]
                    ],
                    "response" => $reset_code
                ];
            }],
            ['POST', '/User/reset/[a:reset_code]', function (string $reset_code = null, array $request) {
                if (isset($reset_code) === false) {
                    throw new \InvalidArgumentException("No user found");
                }
                $user = Model\User::where("reset_code", $reset_code)->first();
                if ($user === null) {
                    throw new \DomainException("No user found");
                }
                $validator = new \Valitron\Validator($request);
                $validator->rule('required', ['password', 'confirm_password']);
                $validator->rule(function ($field, $value, array $params, array $fields) {
                    return is_string($value);
                }, ['password', 'confirm_password'])->message("{field} must be a string");
                $validator->rule('equals', 'confirm_password', 'password');
                $validator->rule('lengthBetween', 'password', 8, 80);
                if ($validator->validate() === false) {
                    return [
                        "headers" => [
                            [
                                'X-PHP-Response-Code: 422',
                                true,
                                422
                            ]
                        ],
                        "response" => $validator->errors()
                    ];
                }
                $user->pass = $request['password'];
                $user->reset_code = null;
                $user->save();
                return [
                    "headers" => [
                        [
                            'X-PHP-Response-Code: 200',
                            true,
                            200
                        ]
                    ],
                    "response" => true
                ];
            }],
            ['POST', '/User/login/[u:username]', function (string $username = null, array $request) use ($auth) {
                if (isset($username) === false) {
                    return [
                        "headers" => [
                            [
                                'X-PHP-Response-Code: 422',
                                true,
                                422
                            ]
                        ],
                        "response" => [
                            "user" => "Username required",
                        ]
                    ];
                }
                $user = Model\User::where("user", $username)->first();
                if ($user === null) {
                    return [
                        "headers" => [
                            [
                                'X-PHP-Response-Code: 422',
                                true,
                                422
                            ]
                        ],
                        "response" => [
                            "user" => "Invalid username or password",
                            "pass" => "Invalid username or password"
                        ]
                    ];
                }
                if (empty($request['password']) === true || is_string($request['password']) === false) {
                    return [
                        "headers" => [
                            [
                                'X-PHP-Response-Code: 422',
                                true,
                                422
                            ]
                        ],
                        "response" => [
                            "user" => "Invalid username or password",
                            "pass" => "Invalid username or password"
                        ]
                    ];
                }
                if ($user->checkPass($request['password']) === true) {
                    $session = new Model\Session;
                    $api_session = md5(uniqid("", true));
                    while (Model\Session::where("code", $api_session)->first() != null) {
                        $api_session = md5(uniqid("", true));
                    }
                    $session->code = $api_session;
                    $user->sessions()->save($session);
                    $user->reset_code = null;
                    $user->save();
                    $_SESSION['api_session'] = $api_session;
                    $auth->validate($api_session);
                    return [
                        "headers" => [
                            [
                                'X-PHP-Response-Code: 200',
                                true,
                                200
                            ]
                        ],
                        "response" => $api_session
                    ];
                } else {
                    return [
                        "headers" => [
                            [
                                'X-PHP-Response-Code: 422',
                                true,
                                422
                            ]
                        ],
                        "response" => [
                            "user" => "Invalid username or password",
                            "pass" => "Invalid username or password"
                        ]
                    ];
                }
            }],
        ];
    }
Returns the request map for POST requests

@return array