Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug 500 error when invalid url #825

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions src/SitemapParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,13 @@ public function getQueue() {
* @throws WP2StaticException
*/
public function parse( $url, $url_content = null ) {
$check_url = $url;
$clean_url = preg_replace( '/(?<!:)\/\/+/', '/', $url );
$this->clean();
$this->current_url = $this->urlEncode( $url );
if ( $clean_url ) {
$check_url = $clean_url;
}
$this->current_url = $this->urlEncode( $check_url );
if ( ! $this->urlValidate( $this->current_url ) ) {
throw new WP2StaticException( 'Invalid URL' );
}
Expand Down Expand Up @@ -242,6 +247,16 @@ protected function getContent() {
}
}

/**
* callable trim function
*
* @param string $string
* @return string
*/
protected function trim( $string ) {
return trim( $string );
}

/**
* Search for sitemaps in the robots.txt content
*
Expand All @@ -252,7 +267,7 @@ protected function parseRobotstxt( $robotstxt ) {
// Split lines into array
$lines = array_filter(
array_map(
'trim',
[ $this, 'trim' ],
(array) preg_split( '/\r\n|\n|\r/', $robotstxt )
)
);
Expand All @@ -269,7 +284,7 @@ protected function parseRobotstxt( $robotstxt ) {
$line = $line[0];

// Split by directive and rule
$pair = array_map( 'trim', (array) preg_split( '/:/', $line, 2 ) );
$pair = array_map( [ $this, 'trim' ], (array) preg_split( '/:/', $line, 2 ) );
// Check if the line contains a sitemap
if (
strtolower( $pair[0] ) !== self::XML_TAG_SITEMAP ||
Expand Down Expand Up @@ -374,7 +389,12 @@ protected function parseString( $string ) {
// Strings are not part of any documented sitemap standard
return false;
}
$array = array_filter( array_map( 'trim', (array) preg_split( '/\r\n|\n|\r/', $string ) ) );
$array = array_filter(
array_map(
[ $this, 'trim' ],
(array) preg_split( '/\r\n|\n|\r/', $string )
)
);
foreach ( $array as $line ) {
if ( $this->isSitemapURL( $line ) ) {
$this->addArray( self::XML_TAG_SITEMAP, [ 'loc' => $line ] );
Expand Down