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

This fork includes modifications to address a vulnerability caused by using an outdated version of League\Flysystem #12

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Update Package for Compatibility with Flysystem 3.0
- Updated dependencies to use Flysystem 3.0, addressing the vulnerability associated with Flysystem 1.0
- Refactored code to adopt changes in the Flysystem 3.0 API, ensuring compatibility and adherence to current best practices.
  • Loading branch information
daniele-vigano committed Oct 29, 2024
commit 31c9b46f343b9e19e3fad01b44dfd0bcb55e22d9
82 changes: 40 additions & 42 deletions src/AbstractSitemap.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Tackk\Cartographer;
namespace CreativeFactoryRV\Cartographer;

use DateTime;
use DateTimeZone;
Expand All @@ -9,13 +9,7 @@
use InvalidArgumentException;
use RuntimeException;

class MaxUrlCountExceededException extends RuntimeException
{
}


abstract class AbstractSitemap
{
abstract class AbstractSitemap {
const MAX_URLS = 50000;

/**
Expand Down Expand Up @@ -68,8 +62,7 @@ abstract protected function getNodeName();
/**
* Sets up the sitemap XML document and urlset node.
*/
public function __construct()
{
public function __construct() {
$this->document = new DOMDocument($this->xmlVersion, $this->xmlEncoding);
$this->rootNode = $this->document->createElementNS($this->xmlNamespaceUri, $this->getRootNodeName());

Expand All @@ -80,51 +73,45 @@ public function __construct()
/**
* Freeze the sitemap, and append the rootNode to the document.
*/
public function freeze()
{
public function freeze() {
$this->document->appendChild($this->rootNode);
$this->isFrozen = true;
}

public function isFrozen()
{
public function isFrozen() {
return $this->isFrozen;
}

/**
* Gets the number of Urls in the sitemap.
* @return int
*/
public function getUrlCount()
{
public function getUrlCount() {
return $this->urlCount;
}

/**
* Checks if the sitemap contains the maximum URL count.
* @return bool
*/
public function hasMaxUrlCount()
{
public function hasMaxUrlCount() {
return $this->urlCount === static::MAX_URLS;
}

/**
* Converts the Sitemap to an XML string.
* @return string
*/
public function toString()
{
return (string) $this;
public function toString() {
return (string)$this;
}

/**
* Converts the Sitemap to an XML string.
* @return string
*/
public function __toString()
{
if (!$this->isFrozen()) {
public function __toString() {
if ( !$this->isFrozen() ) {
$this->freeze();
}

Expand All @@ -133,20 +120,19 @@ public function __toString()

/**
* Adds a URL to the document with the given array of elements.
* @param array $urlArray
* @param array $urlArray
* @return $this
* @throws MaxUrlCountExceededException
*/
protected function addUrlToDocument(array $urlArray)
{
if ($this->hasMaxUrlCount()) {
protected function addUrlToDocument(array $urlArray) {
if ( $this->hasMaxUrlCount() ) {
throw new MaxUrlCountExceededException('Maximum number of URLs has been reached, cannot add more.');
}

$node = $this->document->createElement($this->getNodeName());

foreach ($urlArray as $key => $value) {
if (is_null($value)) {
foreach ( $urlArray as $key => $value ) {
if ( is_null($value) ) {
continue;
}
$node->appendChild(new DOMElement($key, $value));
Expand All @@ -159,35 +145,47 @@ protected function addUrlToDocument(array $urlArray)

/**
* Escapes a string so it can be inserted into the Sitemap
* @param string $string The string to escape.
* @param string $string The string to escape.
* @return string
*/
protected function escapeString($string)
{
$from = ['&', '\'', '"', '>', '<'];
$to = ['&amp;', '&apos;', '&quot;', '&gt;', '&lt;'];
protected function escapeString($string) {
$from = [
'&',
'\'',
'"',
'>',
'<'
];
$to = [
'&amp;',
'&apos;',
'&quot;',
'&gt;',
'&lt;'
];

return str_replace($from, $to, $string);
}

/**
* Takes a date as a string (or int in the case of a unix timestamp).
* @param string $dateString
* @param string $dateString
* @return string
* @throws InvalidArgumentException
*/
protected function formatDate($dateString)
{
protected function formatDate($dateString) {
try {
// We have to handle timestamps a little differently
if (is_numeric($dateString) && (int) $dateString == $dateString) {
$date = DateTime::createFromFormat('U', (int) $dateString, new DateTimeZone('UTC'));
} else {
if ( is_numeric($dateString) && (int)$dateString == $dateString ) {
$date = DateTime::createFromFormat('U', (int)$dateString, new DateTimeZone('UTC'));
}
else {
$date = new DateTime($dateString, new DateTimeZone('UTC'));
}

return $date->format(DateTime::W3C);
} catch (\Exception $e) {
}
catch (\Exception $e) {
throw new InvalidArgumentException("Malformed last modified date: {$dateString}", 0, $e);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/ChangeFrequency.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Tackk\Cartographer;
namespace CreativeFactoryRV\Cartographer;

class ChangeFrequency
{
Expand Down
7 changes: 7 additions & 0 deletions src/MaxUrlCountExceededException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace CreativeFactoryRV\Cartographer;

class MaxUrlCountExceededException extends \RuntimeException {

}
2 changes: 1 addition & 1 deletion src/Sitemap.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Tackk\Cartographer;
namespace CreativeFactoryRV\Cartographer;

class Sitemap extends AbstractSitemap
{
Expand Down
32 changes: 7 additions & 25 deletions src/SitemapFactory.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<?php

namespace Tackk\Cartographer;
namespace CreativeFactoryRV\Cartographer;

use ArrayObject;
use DateTime;
use Iterator;
use League\Flysystem\FilesystemInterface;
use League\Flysystem\FilesystemOperator;
use RuntimeException;

class SitemapFactory
{
/**
* @var FilesystemInterface
* @var FilesystemOperator
*/
protected $filesystem = null;

Expand All @@ -26,16 +26,16 @@ class SitemapFactory
protected $filesCreated = [];

/**
* @param FilesystemInterface $filesystem
* @param FilesystemOperator $filesystem
*/
public function __construct(FilesystemInterface $filesystem)
public function __construct(FilesystemOperator $filesystem)
{
$this->filesystem = $filesystem;
}

/**
* Gets the Filesystem.
* @return FilesystemInterface
* @return FilesystemOperator
*/
public function getFilesystem()
{
Expand Down Expand Up @@ -184,7 +184,7 @@ protected function parseEntry($entry)
*/
protected function randomHash()
{
return md5($this->randomBytes(32));
return md5(random_bytes(32));
}

/**
Expand All @@ -196,22 +196,4 @@ protected function fileUrl($file)
{
return $this->baseUrl.'/'.ltrim($file, '/');
}

/**
* Generates a string of random bytes (of given length).
* @param integer $bytes The number of bytes to return.
* @throws \RuntimeException
* @return string
* @codeCoverageIgnore
*/
protected function randomBytes($bytes = 32)
{
if (extension_loaded('openssl')) {
return openssl_random_pseudo_bytes($bytes);
} elseif (extension_loaded('mcrypt')) {
return mcrypt_create_iv($bytes, MCRYPT_DEV_URANDOM);
}

throw new RuntimeException('Extension "openssl" or "mcrypt" is required, but is not installed.');
}
}
2 changes: 1 addition & 1 deletion src/SitemapIndex.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Tackk\Cartographer;
namespace CreativeFactoryRV\Cartographer;

class SitemapIndex extends AbstractSitemap
{
Expand Down
2 changes: 1 addition & 1 deletion src/functions.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Tackk\Cartographer;
namespace CreativeFactoryRV\Cartographer;

use InvalidArgumentException;

Expand Down