diff --git a/src/app/controller/submit.php b/src/app/controller/submit.php index cc13f71..d72840a 100644 --- a/src/app/controller/submit.php +++ b/src/app/controller/submit.php @@ -64,6 +64,7 @@ class AppControllerSubmit 'attribute' => (object) [ 'value' => null, + 'required' => $this->_validator->getPageTitleRequired(), 'minlength' => $this->_validator->getPageTitleLengthMin(), 'maxlength' => $this->_validator->getPageTitleLengthMax(), 'placeholder' => sprintf( @@ -79,6 +80,7 @@ class AppControllerSubmit 'attribute' => (object) [ 'value' => null, + 'required' => $this->_validator->getPageDescriptionRequired(), 'minlength' => $this->_validator->getPageDescriptionLengthMin(), 'maxlength' => $this->_validator->getPageDescriptionLengthMax(), 'placeholder' => sprintf( @@ -94,6 +96,7 @@ class AppControllerSubmit 'attribute' => (object) [ 'value' => null, + 'required' => $this->_validator->getPageKeywordsRequired(), 'minlength' => $this->_validator->getPageKeywordsLengthMin(), 'maxlength' => $this->_validator->getPageKeywordsLengthMax(), 'placeholder' => sprintf( @@ -111,6 +114,7 @@ class AppControllerSubmit 'attribute' => (object) [ 'value' => null, + 'required' => $this->_validator->getPageTorrentRequired(), 'placeholder' => sprintf( _('Torrent file (use Ctrl to select multiple files)') ), @@ -123,9 +127,52 @@ class AppControllerSubmit ], ]; + // Submit request if (isset($_POST)) { + if (isset($_POST['title'])) + { + $error = []; + if ($this->_validator->pageTitle($_POST['title'], $error)) + { + $form->title->error = $error; + } + + // @TODO check for page duplicates + + $form->title->attribute->value = htmlentities($_POST['title']); + } + + if (isset($_POST['description'])) + { + $error = []; + + if ($this->_validator->pageDescription($_POST['description'], $error)) + { + $form->description->error = $error; + } + + $form->description->attribute->value = htmlentities($_POST['description']); + } + + if (isset($_POST['keywords'])) + { + $error = []; + + if ($this->_validator->pageKeywords($_POST['keywords'], $error)) + { + $form->keywords->error = $error; + } + + $form->keywords->attribute->value = htmlentities($_POST['keywords']); + } + + // Request valid + if (empty($error)) + { + // @TODO redirect + } } // Render diff --git a/src/app/model/validator.php b/src/app/model/validator.php index dfb99f7..8b3da69 100644 --- a/src/app/model/validator.php +++ b/src/app/model/validator.php @@ -14,6 +14,11 @@ class AppModelValidator // Page /// Page title + public function getPageTitleRequired() : bool + { + return $this->_config->page->title->required; + } + public function getPageTitleLengthMin() : int { return $this->_config->page->title->length->min; @@ -29,7 +34,65 @@ class AppModelValidator return $this->_config->page->title->regex; } + public function pageTitle(mixed $value, array &$error = []) : bool + { + if (!is_string($value)) + { + array_push( + $error, + _('Invalid page title data type') + ); + + return false; + } + + if (empty($value) && $this->getPageTitleRequired()) + { + array_push( + $error, + _('Page title required') + ); + + return false; + } + + if (!preg_match($this->getPageTitleRegex(), $value)) + { + array_push( + $error, + sprintf( + _('Page title format does not match condition "%s"'), + $this->getPageTitleRegex() + ) + ); + + return false; + } + + if (mb_strlen($value) < $this->getPageTitleLengthMin() || + mb_strlen($value) > $this->getPageTitleLengthMax()) + { + array_push( + $error, + sprintf( + _('Page title out of %s-%s chars range'), + $this->getPageTitleLengthMin(), + $this->getPageTitleLengthMax() + ) + ); + + return false; + } + + return true; + } + /// Page description + public function getPageDescriptionRequired() : bool + { + return $this->_config->page->description->required; + } + public function getPageDescriptionLengthMin() : int { return $this->_config->page->description->length->min; @@ -45,7 +108,65 @@ class AppModelValidator return $this->_config->page->description->regex; } + public function pageDescription(mixed $value, array &$error = []) : bool + { + if (!is_string($value)) + { + array_push( + $error, + _('Invalid page description data type') + ); + + return false; + } + + if (empty($value) && $this->getPageDescriptionRequired()) + { + array_push( + $error, + _('Page description required') + ); + + return false; + } + + if (!preg_match($this->getPageDescriptionRegex(), $value)) + { + array_push( + $error, + sprintf( + _('Page description format does not match condition "%s"'), + $this->getPageDescriptionRegex() + ) + ); + + return false; + } + + if (mb_strlen($value) < $this->getPageDescriptionLengthMin() || + mb_strlen($value) > $this->getPageDescriptionLengthMax()) + { + array_push( + $error, + sprintf( + _('Page description out of %s-%s chars range'), + $this->getPageDescriptionLengthMin(), + $this->getPageDescriptionLengthMax() + ) + ); + + return false; + } + + return true; + } + /// Page keywords + public function getPageKeywordsRequired() : bool + { + return $this->_config->page->keywords->required; + } + public function getPageKeywordsLengthMin() : int { return $this->_config->page->keywords->length->min; @@ -71,6 +192,141 @@ class AppModelValidator return $this->_config->page->keywords->regex; } + public function pageKeywords(mixed $value, array &$error = []) : bool + { + if (!is_array($value)) + { + array_push( + $error, + _('Invalid page keywords data type') + ); + + return false; + } + + if (empty($value) && $this->getPageKeywordsRequired()) + { + array_push( + $error, + _('Page keywords required') + ); + + return false; + } + + $total = 0; + + foreach ($value as $keywords) + { + if (!is_string($kt)) + { + array_push( + $error, + _('Invalid magnet keyword value data type') + ); + + return false; + } + + if (!preg_match(MAGNET_KT_REGEX, $kt)) + { + array_push( + $error, + sprintf( + _('Magnet keyword format does not match condition "%s"'), + MAGNET_KT_REGEX + ) + ); + + return false; + } + + if (mb_strlen($kt) < MAGNET_KT_MIN_LENGTH || + mb_strlen($kt) > MAGNET_KT_MAX_LENGTH) + { + array_push( + $error, + sprintf( + _('Magnet keyword out of %s-%s chars range'), + MAGNET_KT_MIN_LENGTH, + MAGNET_KT_MAX_LENGTH + ) + ); + + return false; + } + + $total++; + } + + if ($total < MAGNET_KT_MIN_QUANTITY || + $total > MAGNET_KT_MAX_QUANTITY) + { + array_push( + $error, + sprintf( + _('Magnet keywords quantity out of %s-%s range'), + MAGNET_KT_MIN_QUANTITY, + MAGNET_KT_MAX_QUANTITY + ) + ); + + return false; + } + + return true; + } + + public function pageKeyword(mixed $value, array &$error = []) : bool + { + if (!is_string($value)) + { + array_push( + $error, + _('Invalid page keyword data type') + ); + + return false; + } + + if (!preg_match($this->getPageKeywordsRegex(), $value)) + { + array_push( + $error, + sprintf( + _('Page keyword "%s" format does not match condition "%s"'), + $value, + $this->getPageKeywordsRegex() + ) + ); + + return false; + } + + if (mb_strlen($value) < $this->getPageDescriptionLengthMin() || + mb_strlen($value) > $this->getPageDescriptionLengthMax()) + { + array_push( + $error, + sprintf( + _('Page description out of %s-%s chars range'), + $this->getPageDescriptionLengthMin(), + $this->getPageDescriptionLengthMax() + ) + ); + + return false; + } + + return true; + } + + /// Page torrent + public function getPageTorrentRequired() : bool + { + return $this->_config->page->torrent->required; + } + // Common public function host(mixed $value, array &$error = []) : bool { diff --git a/src/app/view/theme/default/submit.phtml b/src/app/view/theme/default/submit.phtml index 2b250d8..6a16a49 100644 --- a/src/app/view/theme/default/submit.phtml +++ b/src/app/view/theme/default/submit.phtml @@ -32,6 +32,7 @@ type="text" name="title" id="title" + title->attribute->required ? 'required="required"' : false ?> value="title->attribute->value ?>" placeholder="title->attribute->placeholder ?>" minlength="title->attribute->minlength ?>" @@ -55,6 +56,7 @@ @@ -76,7 +78,7 @@ @@ -101,6 +103,7 @@ multiple="multiple" name="torrent" id="torrent" + torrent->attribute->required ? 'required="required"' : false ?> value="torrent->attribute->value ?>" />