Skip to content

Commit

Permalink
uriValidator and feeds update
Browse files Browse the repository at this point in the history
  • Loading branch information
Simbiat committed Apr 7, 2021
1 parent b20697a commit c66d747
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 13 deletions.
23 changes: 14 additions & 9 deletions src/Atom.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ public function Atom(string $title, array $entries, string $id = '', string $tex
{
#Validate title
if (empty($title)) {
(new \Simbiat\http20\Headers)->clientReturn('500', false);
throw new \UnexpectedValueException('No `title` provided in settings for the feed');
} else {
$feed_settings['title'] = $title;
}
#validate text type
if (!in_array(strtolower($texttype), ['text', 'html', 'xhtml'])) {
(new \Simbiat\http20\Headers)->clientReturn('500', false);
throw new \UnexpectedValueException('Unsupported text type provided for Atom feed');
}
#Validate content
Expand All @@ -37,6 +39,7 @@ public function Atom(string $title, array $entries, string $id = '', string $tex
if ($this->http20->uriValidator($id)) {
$feed_settings['id'] = $this->http20->htmlToRFC3986($id);
} else {
(new \Simbiat\http20\Headers)->clientReturn('500', false);
throw new \UnexpectedValueException('$id provided is not a valid URI');
}
}
Expand Down Expand Up @@ -146,38 +149,38 @@ public function Atom(string $title, array $entries, string $id = '', string $tex
}

#Helper function to validate some elements
private function atomElementValidator(array $elstoval, string $type = 'author', string $element = 'name'): void
private function atomElementValidator(array &$elstoval, string $type = 'author', string $element = 'name'): void
{
foreach ($elstoval as $key=>$eltoval) {
if (!is_array($eltoval)) {
throw new \UnexpectedValueException('Element `'.$key.'` in `'.$type.'` provided is not an array');
unset($elstoval[$key]);continue;
}
if (empty($eltoval[$element])) {
throw new \UnexpectedValueException('`'.$element.'` for element `'.$key.'` in `'.$type.'` is not provided');
unset($elstoval[$key]);continue;
} else {
if (!is_string($eltoval[$element])) {
throw new \UnexpectedValueException('`'.$element.'` for element `'.$key.'` in `'.$type.'` is not a string');
unset($elstoval[$key]);continue;
}
}
if ($type === 'link') {
if (!$this->http20->uriValidator($eltoval['href'])) {
throw new \UnexpectedValueException('`href` for element `'.$key.'` in `'.$type.'` is not a valid URI');
unset($elstoval[$key]);continue;
}
if (!empty($eltoval['rel'])) {
if (!in_array($eltoval['rel'], ['alternate', 'self', 'ecnlosure', 'related', 'via'])) {
throw new \UnexpectedValueException('Unsupported `rel` value ('.$eltoval['rel'] .') provided for element `'.$key.'` in `'.$type.'s');
unset($elstoval[$key]);continue;
}
}
}
if ($type === 'entry') {
if (empty($eltoval['title'])) {
throw new \UnexpectedValueException('`Title` for element `'.$key.'` in `'.$type.'` is not provided');
unset($elstoval[$key]);continue;
}
if (empty($eltoval['updated'])) {
throw new \UnexpectedValueException('`Updated` for element `'.$key.'` in `'.$type.'` is not provided');
unset($elstoval[$key]);continue;
}
if (!$this->http20->uriValidator($eltoval['link'])) {
throw new \UnexpectedValueException('`link` ('.$eltoval['link'].') for element `'.$key.'` in `'.$type.'` is not a valid URI');
unset($elstoval[$key]);continue;
}
}
}
Expand All @@ -187,6 +190,7 @@ private function atomElementValidator(array $elstoval, string $type = 'author',
private function atomAddSubElements(\DOMNode &$element, \DOMDocument &$feed, array $toptag, array $subnodes): \DOMNode
{
if (empty($subnodes)) {
(new \Simbiat\http20\Headers)->clientReturn('500', false);
throw new \UnexpectedValueException('Empty list of subnodes provided for `atomAddSubElements` function');
}
foreach ($subnodes as $subnode) {
Expand All @@ -205,6 +209,7 @@ private function atomAddSubElements(\DOMNode &$element, \DOMDocument &$feed, arr
private function atomAddAttributes(\DOMNode &$element, \DOMDocument &$feed, array $toptag, array $attributes): \DOMNode
{
if (empty($attributes)) {
(new \Simbiat\http20\Headers)->clientReturn('500', false);
throw new \UnexpectedValueException('Empty list of atrributes provided for `atomAddAttributes` function');
}
foreach ($attributes as $attribute) {
Expand Down
2 changes: 1 addition & 1 deletion src/Common.php
Original file line number Diff line number Diff line change
Expand Up @@ -822,7 +822,7 @@ public function emailValidator(string $string): bool
#Function to check if string is an URI as per RFC 3986
public function uriValidator(string $string): bool
{
if (preg_match('/^(?<scheme>[a-z][a-z0-9+.-]+):(?<authority>\/\/(?<user>[^@]+@)?(?<host>[\p{L}0-9.\-_~]+)(?<port>:\d+)?)?(?<path>(?:[\p{L}0-9-._~]|%[a-f0-9]|[!$&\'()*+,;=:@])+(?:\/(?:[\p{L}0-9-._~]|%[a-f0-9]|[!$&\'()*+,;=:@])*)*|(?:\/(?:[\p{L}0-9-._~]|%[a-f0-9]|[!$&\'()*+,;=:@])+)*)?(?<query>\?(?:[\p{L}0-9-._~]|%[a-f0-9]|[!$&\'()*+,;=:@]|[\/?])+)?(?<fragment>\#(?:[\p{L}0-9-._~]|%[a-f0-9]|[!$&\'()*+,;=:@]|[\/?])+)?$/iu', $this->htmlToRFC3986($string)) === 1) {
if (preg_match('/^(?<scheme>[a-z][a-z0-9+.-]+):(?<authority>\/\/(?<user>[^@]+@)?(?<host>[\p{L}0-9.\-_~]+)(?<port>:\d+)?)?(?<path>(?:[\p{L}0-9-._~]|%[a-f0-9]{2}|[!$&\'()*+,;=:@])+(?:\/(?:[\p{L}0-9-._~]|%[a-f0-9]{2}|[!$&\'()*+,;=:@])*)*|(?:\/(?:[\p{L}0-9-._~]|%[a-f0-9]{2}|[!$&\'()*+,;=:@])+)*)?(?<query>\?(?:[\p{L}0-9-._~]|%[a-f0-9]{2}|[!$&\'()*+,;=:@]|[\/?])+)?(?<fragment>\#(?:[\p{L}0-9-._~]|%[a-f0-9]{2}|[!$&\'()*+,;=:@]|[\/?])+)?$/iu', $this->htmlToRFC3986($string)) === 1) {
return true;
} else {
return false;
Expand Down
18 changes: 15 additions & 3 deletions src/RSS.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public function RSS(string $title, array $entries, string $feedlink = '', array
{
#Validate title
if (empty($title)) {
(new \Simbiat\http20\Headers)->clientReturn('500', false);
throw new \UnexpectedValueException('No `title` provided in settings for the feed');
} else {
$feed_settings['title'] = $title;
Expand All @@ -29,21 +30,22 @@ public function RSS(string $title, array $entries, string $feedlink = '', array
if ($this->http20->uriValidator($feedlink)) {
$feed_settings['link'] = $this->http20->htmlToRFC3986($feedlink);
} else {
(new \Simbiat\http20\Headers)->clientReturn('500', false);
throw new \UnexpectedValueException('$feedlink provided is not a valid URI');
}
}
#Validate content
if (!empty($entries)) {
foreach ($entries as $key=>$entry) {
if (empty($entry['title']) && empty($entry['description'])) {
throw new \UnexpectedValueException('Element `'.$key.'` in entries is missing both `title` and `description`');
unset($entries[$key]);continue;
}
if (!empty($entry['enclosure_url'])) {
if (empty($entry['enclosure_length']) || empty($entry['enclosure_type'])) {
throw new \UnexpectedValueException('Element `'.$key.'` in entries is missing either `enclosure_length` or `enclosure_type`');
unset($entries[$key]);continue;
} else {
if (is_numeric($entry['enclosure_length'])) {
throw new \UnexpectedValueException('Element `'.$key.'` in entries has non-numeric `enclosure_length` value');
unset($entries[$key]);continue;
}
}
}
Expand Down Expand Up @@ -76,31 +78,38 @@ public function RSS(string $title, array $entries, string $feedlink = '', array
#Check cloud
if (!empty($feed_settings['cloud'])) {
if (empty($feed_settings['cloud']['domain']) || empty($feed_settings['cloud']['port']) || empty($feed_settings['cloud']['path']) || empty($feed_settings['cloud']['registerProcedure']) || empty($feed_settings['cloud']['protocol'])) {
(new \Simbiat\http20\Headers)->clientReturn('500', false);
throw new \UnexpectedValueException('One or more atributes requried for `cloud` tag are missing in settings for the feed');
}
}
#Check TTL
if (!empty($feed_settings['ttl']) && !is_numeric($feed_settings['ttl'])) {
(new \Simbiat\http20\Headers)->clientReturn('500', false);
throw new \UnexpectedValueException('`ttl` provided in settings for the feed is not numeric');
}
#Check image
if (!empty($feed_settings['image'])) {
if (empty($feed_settings['image']['url'])) {
(new \Simbiat\http20\Headers)->clientReturn('500', false);
throw new \UnexpectedValueException('`url` property for `image` tag is missing in settings for the feed');
}
if (!empty($feed_settings['image']['width'])) {
if (!is_numeric($feed_settings['image']['width'])) {
(new \Simbiat\http20\Headers)->clientReturn('500', false);
throw new \UnexpectedValueException('`width` property for `image` tag is not numeric in settings for the feed');
}
if (intval($feed_settings['image']['width']) > 144) {
(new \Simbiat\http20\Headers)->clientReturn('500', false);
throw new \UnexpectedValueException('`width` property for `image` tag is more than 144 in settings for the feed');
}
}
if (!empty($feed_settings['image']['height'])) {
if (!is_numeric($feed_settings['image']['height'])) {
(new \Simbiat\http20\Headers)->clientReturn('500', false);
throw new \UnexpectedValueException('`height` property for `image` tag is not numeric in settings for the feed');
}
if (intval($feed_settings['image']['height']) > 400) {
(new \Simbiat\http20\Headers)->clientReturn('500', false);
throw new \UnexpectedValueException('`height` property for `image` tag is more than 400 in settings for the feed');
}
}
Expand All @@ -109,9 +118,11 @@ public function RSS(string $title, array $entries, string $feedlink = '', array
if (!empty($feed_settings['skipHours']) && is_array($feed_settings['skipHours'])) {
foreach ($feed_settings['skipHours'] as $hour) {
if (!is_numeric($hour)) {
(new \Simbiat\http20\Headers)->clientReturn('500', false);
throw new \UnexpectedValueException('Hour for for `skipHours` tag is not numeric in settings for the feed');
}
if (intval($hour) < 0 || intval($hour) > 23) {
(new \Simbiat\http20\Headers)->clientReturn('500', false);
throw new \UnexpectedValueException('Hour property for `skipHours` tag is outside of 0-23 range in settings for the feed');
}
}
Expand All @@ -120,6 +131,7 @@ public function RSS(string $title, array $entries, string $feedlink = '', array
if (!empty($feed_settings['skipDays']) && is_array($feed_settings['skipDays'])) {
foreach ($feed_settings['skipDays'] as $day) {
if (!in_array($day, ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'])) {
(new \Simbiat\http20\Headers)->clientReturn('500', false);
throw new \UnexpectedValueException('Day property for `skipDays` tag is not one of accepted values (Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday) in settings for the feed');
}
}
Expand Down

0 comments on commit c66d747

Please sign in to comment.