diff --git a/core/class/autoload.php b/core/class/autoload.php index 9d6ccd8..bf408d6 100644 --- a/core/class/autoload.php +++ b/core/class/autoload.php @@ -7,11 +7,7 @@ class autoload { require_once 'core/class/helper.class.php'; require_once 'core/class/template.class.php'; require_once 'core/class/layout.class.php'; - require_once 'core/class/sitemap/IConfig.class.php'; - require_once 'core/class/sitemap/Config.class.php'; - require_once 'core/class/sitemap/IRuntime.class.php'; require_once 'core/class/sitemap/Runtime.class.php'; - require_once 'core/class/sitemap/IFileSystem.class.php'; require_once 'core/class/sitemap/FileSystem.class.php'; require_once 'core/class/sitemap/SitemapGenerator.class.php'; require_once 'core/class/phpmailer/PHPMailer.class.php'; diff --git a/core/class/sitemap/Config.class.php b/core/class/sitemap/Config.class.php deleted file mode 100644 index ca6ae77..0000000 --- a/core/class/sitemap/Config.class.php +++ /dev/null @@ -1,115 +0,0 @@ -fs = null; - $this->runtime = null; - } - - /** - * @return string - */ - public function getBaseURL(): string - { - return $this->baseURL; - } - - /** - * @param string $baseURL - * @return Config - */ - public function setBaseURL(string $baseURL): Config - { - $this->baseURL = $baseURL; - return $this; - } - - /** - * @return string - */ - public function getSaveDirectory(): string - { - return $this->saveDirectory; - } - - /** - * @param string $saveDirectory - * @return Config - */ - public function setSaveDirectory(string $saveDirectory): Config - { - $this->saveDirectory = $saveDirectory; - return $this; - } - - /** - * @return IFileSystem|null - */ - public function getFS(): IFileSystem|null - { - return $this->fs; - } - - /** - * @param IFileSystem|null $fs - * @return Config - */ - public function setFS(IFileSystem|null $fs): Config - { - $this->fs = $fs; - return $this; - } - - /** - * @return IRuntime|null - */ - public function getRuntime(): IRuntime|null - { - return $this->runtime; - } - - /** - * @param IRuntime|null $runtime - * @return Config - */ - public function setRuntime(IRuntime|null $runtime): Config - { - $this->runtime = $runtime; - return $this; - } - - public function getSitemapIndexURL(): string - { - return $this->sitemapIndexURL; - } - - public function setSitemapIndexURL(string $sitemapIndexURL): Config - { - $this->sitemapIndexURL = $sitemapIndexURL; - return $this; - } -} \ No newline at end of file diff --git a/core/class/sitemap/IConfig.class.php b/core/class/sitemap/IConfig.class.php deleted file mode 100644 index c736a71..0000000 --- a/core/class/sitemap/IConfig.class.php +++ /dev/null @@ -1,31 +0,0 @@ -\n") - private int $sitemapURLCount = 0; - private array $generatedFiles = []; + private $urlsetClosingTagLen = 10; // strlen("\n") + private $sitemapUrlCount = 0; + private $generatedFiles = []; /** - * @param IConfig $config Configuration object. - * @throws InvalidArgumentException + * @param string $baseURL You site URL + * @param string $basePath Relative path where sitemap and robots should be stored. + * @param FileSystem|null $fs + * @param Runtime|null $runtime */ - public function __construct(IConfig $config) + public function __construct(string $baseURL, string $basePath = "", FileSystem $fs = null, Runtime $runtime = null) { - if ($config->getBaseURL() === '') { - throw new InvalidArgumentException('baseURL config parameter is required'); - } + $this->urls = []; + $this->baseURL = rtrim($baseURL, '/'); - $this->baseURL = rtrim($config->getBaseURL(), '/'); - $this->sitemapIndexURL = rtrim($config->getBaseURL(), '/'); - - if ($config->getSitemapIndexURL()) { - $this->sitemapIndexURL = rtrim($config->getSitemapIndexURL(), '/'); - } - - $configFS = $config->getFS(); - if ($configFS === null) { + if ($fs === null) { $this->fs = new FileSystem(); } else { - $this->fs = $configFS; + $this->fs = $fs; } - $configRuntime = $config->getRuntime(); - if ($configRuntime === null) { + if ($runtime === null) { $this->runtime = new Runtime(); } else { - $this->runtime = $configRuntime; + $this->runtime = $runtime; } - if ($this->runtime->is_writable($config->getSaveDirectory()) === false) { + if ($this->runtime->is_writable($basePath) === false) { throw new InvalidArgumentException( - sprintf('the provided basePath (%s) should be a writable directory,', $config->getSaveDirectory()) . + sprintf('the provided basePath (%s) should be a writable directory,', $basePath) . ' please check its existence and permissions' ); } - - $this->saveDirectory = $config->getSaveDirectory(); - if (strlen($this->saveDirectory) > 0 && substr($this->saveDirectory, -1) != DIRECTORY_SEPARATOR) { - $this->saveDirectory = $this->saveDirectory . DIRECTORY_SEPARATOR; + if (strlen($basePath) > 0 && substr($basePath, -1) != DIRECTORY_SEPARATOR) { + $basePath = $basePath . DIRECTORY_SEPARATOR; } + $this->basePath = $basePath; $this->xmlWriter = $this->createXmlWriter(); $this->flushedSitemapFilenameFormat = sprintf("sm-%%d-%d.xml", time()); @@ -267,10 +267,7 @@ class SitemapGenerator /** * @param string $filename - * * @return SitemapGenerator - * - * @throws InvalidArgumentException */ public function setSitemapFilename(string $filename = ''): SitemapGenerator { @@ -284,26 +281,9 @@ class SitemapGenerator return $this; } - /** - * @param string $path - * @return SitemapGenerator - * @throws InvalidArgumentException - */ - public function setSitemapStylesheet(string $path): SitemapGenerator - { - if (strlen($path) === 0) { - throw new InvalidArgumentException('sitemap stylesheet path should not be empty'); - } - $this->sitemapStylesheetLink = $path; - return $this; - } - /** * @param string $filename - * * @return $this - * - * @throws InvalidArgumentException */ public function setSitemapIndexFilename(string $filename = ''): SitemapGenerator { @@ -317,7 +297,6 @@ class SitemapGenerator /** * @param string $filename * @return $this - * @throws InvalidArgumentException */ public function setRobotsFileName(string $filename): SitemapGenerator { @@ -331,16 +310,15 @@ class SitemapGenerator /** * @param int $value * @return $this - * @throws OutOfRangeException */ - public function setMaxURLsPerSitemap(int $value): SitemapGenerator + public function setMaxUrlsPerSitemap(int $value): SitemapGenerator { if ($value < 1 || self::MAX_URLS_PER_SITEMAP < $value) { throw new OutOfRangeException( sprintf('value %d is out of range 1-%d', $value, self::MAX_URLS_PER_SITEMAP) ); } - $this->maxURLsPerSitemap = $value; + $this->maxUrlsPerSitemap = $value; return $this; } @@ -361,19 +339,13 @@ class SitemapGenerator return $this->isCompressionEnabled; } - /** - * @param string $path - * @param string|null $changeFrequency - * @param float|null $priority - * @param array $extensions - * @return void - * @throws InvalidArgumentException - */ public function validate( - string $path, - ?string $changeFrequency = null, - ?float $priority = null, - array $extensions = []): void + string $path, + DateTime $lastModified = null, + string $changeFrequency = null, + float $priority = null, + array $alternates = null, + array $extensions = []) { if (!(1 <= mb_strlen($path) && mb_strlen($path) <= self::MAX_URL_LEN)) { throw new InvalidArgumentException( @@ -388,14 +360,8 @@ class SitemapGenerator if ($priority !== null && !in_array($priority, $this->validPriorities)) { throw new InvalidArgumentException("Priority argument should be a float number in the range [0.0..1.0]"); } - if (count($extensions) > 0) { - if (isset($extensions['google_video'])) { - GoogleVideoExtension::validate($this->baseURL . $path, $extensions['google_video']); - } - - if (isset($extensions['google_image'])) { - GoogleImageExtension::validateEntryFields($extensions['google_image']); - } + if ($extensions !== null && isset($extensions['google_video'])) { + GoogleVideoExtension::validate($this->baseURL . $path, $extensions['google_video']); } } @@ -411,22 +377,19 @@ class SitemapGenerator * @param array|null $alternates * @param array $extensions * @return $this - * @throws OutOfRangeException - * @throws UnexpectedValueException - * @throws InvalidArgumentException */ public function addURL( - string $path, - ?DateTime $lastModified = null, - ?string $changeFrequency = null, - ?float $priority = null, - ?array $alternates = null, - array $extensions = [] + string $path, + DateTime $lastModified = null, + string $changeFrequency = null, + float $priority = null, + array $alternates = null, + array $extensions = [] ): SitemapGenerator { - $this->validate($path, $changeFrequency, $priority, $extensions); + $this->validate($path, $lastModified, $changeFrequency, $priority, $alternates, $extensions); - if ($this->totalURLCount >= self::TOTAL_MAX_URLS) { + if ($this->totalUrlCount >= self::TOTAL_MAX_URLS) { throw new OutOfRangeException( sprintf("Max url limit reached (%d)", self::TOTAL_MAX_URLS) ); @@ -437,24 +400,20 @@ class SitemapGenerator $this->writeSitemapUrl($this->baseURL . $path, $lastModified, $changeFrequency, $priority, $alternates, $extensions); - if ($this->totalURLCount % 1000 === 0 || $this->sitemapURLCount >= $this->maxURLsPerSitemap) { + if ($this->totalUrlCount % 1000 === 0 || $this->sitemapUrlCount >= $this->maxUrlsPerSitemap) { $this->flushWriter(); } - if ($this->sitemapURLCount === $this->maxURLsPerSitemap) { + if ($this->sitemapUrlCount === $this->maxUrlsPerSitemap) { $this->writeSitemapEnd(); } return $this; } - protected function writeSitemapStart(): void + private function writeSitemapStart() { $this->xmlWriter->startDocument("1.0", "UTF-8"); - if ($this->sitemapStylesheetLink != "") { - $this->xmlWriter->writePi('xml-stylesheet', - sprintf('type="text/xsl" href="%s"', $this->sitemapStylesheetLink)); - } $this->xmlWriter->writeComment(sprintf('generator-class="%s"', get_class($this))); $this->xmlWriter->writeComment(sprintf('generator-version="%s"', $this->classVersion)); $this->xmlWriter->writeComment(sprintf('generated-on="%s"', date('c'))); @@ -462,48 +421,15 @@ class SitemapGenerator $this->xmlWriter->writeAttribute('xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9'); $this->xmlWriter->writeAttribute('xmlns:xhtml', 'http://www.w3.org/1999/xhtml'); $this->xmlWriter->writeAttribute('xmlns:video', 'http://www.google.com/schemas/sitemap-video/1.1'); - $this->xmlWriter->writeAttribute('xmlns:image', 'http://www.google.com/schemas/sitemap-image/1.1'); $this->xmlWriter->writeAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance'); $this->xmlWriter->writeAttribute('xsi:schemaLocation', 'http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd'); $this->isSitemapStarted = true; } - /** - * @param string $url - * @return string - * @throws UnexpectedValueException - */ - private function encodeEscapeURL(string $url): string + private function writeSitemapUrl($loc, $lastModified, $changeFrequency, $priority, $alternates, $extensions) { - // In-place encoding only on non-ASCII characters, like browsers do. - $encoded = preg_replace_callback('/[^\x20-\x7f]/', function ($match) { - return urlencode($match[0]); - }, $url); - if (!is_string($encoded)) { - throw new UnexpectedValueException('Failed to encode URL'); - } - return htmlspecialchars($encoded, ENT_QUOTES, 'UTF-8'); - } - - /** - * @param string $loc - * @param DateTime|null $lastModified - * @param string|null $changeFrequency - * @param float|null $priority - * @param array|null $alternates - * @param array $extensions - * @throws UnexpectedValueException - */ - private function writeSitemapUrl( - string $loc, - ?DateTime $lastModified = null, - ?string $changeFrequency = null, - ?float $priority = null, - ?array $alternates = null, - array $extensions = [] - ): void { $this->xmlWriter->startElement('url'); - $this->xmlWriter->writeElement('loc', $this->encodeEscapeURL($loc)); + $this->xmlWriter->writeElement('loc', htmlspecialchars($loc, ENT_QUOTES)); if ($lastModified !== null) { $this->xmlWriter->writeElement('lastmod', $lastModified->format(DateTime::ATOM)); @@ -533,20 +459,17 @@ class SitemapGenerator if ($extName === 'google_video') { GoogleVideoExtension::writeVideoTag($this->xmlWriter, $loc, $extFields); } - if ($extName === 'google_image') { - GoogleImageExtension::writeImageTag($this->xmlWriter, $extFields); - } } $this->xmlWriter->endElement(); // url - $this->sitemapURLCount++; - $this->totalURLCount++; + $this->sitemapUrlCount++; + $this->totalUrlCount++; } - private function flushWriter(): void + private function flushWriter() { - $targetSitemapFilepath = $this->saveDirectory . sprintf($this->flushedSitemapFilenameFormat, $this->flushedSitemapCounter); - $flushedString = $this->xmlWriter->outputMemory(); + $targetSitemapFilepath = $this->basePath . sprintf($this->flushedSitemapFilenameFormat, $this->flushedSitemapCounter); + $flushedString = $this->xmlWriter->outputMemory(true); $flushedStringLen = mb_strlen($flushedString); if ($flushedStringLen === 0) { @@ -562,23 +485,22 @@ class SitemapGenerator $this->fs->file_put_contents($targetSitemapFilepath, $flushedString, FILE_APPEND); } - private function writeSitemapEnd(): void + private function writeSitemapEnd() { - $targetSitemapFilepath = $this->saveDirectory . sprintf($this->flushedSitemapFilenameFormat, $this->flushedSitemapCounter); + $targetSitemapFilepath = $this->basePath . sprintf($this->flushedSitemapFilenameFormat, $this->flushedSitemapCounter); $this->xmlWriter->endElement(); // urlset $this->xmlWriter->endDocument(); - $this->fs->file_put_contents($targetSitemapFilepath, $this->xmlWriter->flush(), FILE_APPEND); + $this->fs->file_put_contents($targetSitemapFilepath, $this->xmlWriter->flush(true), FILE_APPEND); $this->isSitemapStarted = false; $this->flushedSitemaps[] = $targetSitemapFilepath; $this->flushedSitemapCounter++; - $this->sitemapURLCount = 0; - $this->flushedSitemapSize = 0; + $this->sitemapUrlCount = 0; } /** * Flush all stored urls from memory to the disk and close all necessary tags. */ - public function flush(): void + public function flush() { $this->flushWriter(); if ($this->isSitemapStarted) { @@ -588,9 +510,8 @@ class SitemapGenerator /** * Move flushed files to their final location. Compress if necessary. - * @throws RuntimeException */ - public function finalize(): void + public function finalize() { $this->generatedFiles = []; @@ -600,7 +521,7 @@ class SitemapGenerator $targetSitemapFilename .= '.gz'; } - $targetSitemapFilepath = $this->saveDirectory . $targetSitemapFilename; + $targetSitemapFilepath = $this->basePath . $targetSitemapFilename; if ($this->isCompressionEnabled) { $this->fs->copy($this->flushedSitemaps[0], 'compress.zlib://' . $targetSitemapFilepath); @@ -609,7 +530,7 @@ class SitemapGenerator $this->fs->rename($this->flushedSitemaps[0], $targetSitemapFilepath); } $this->generatedFiles['sitemaps_location'] = [$targetSitemapFilepath]; - $this->generatedFiles['sitemaps_index_url'] = $this->sitemapIndexURL . '/' . $targetSitemapFilename; + $this->generatedFiles['sitemaps_index_url'] = $this->baseURL . '/' . $targetSitemapFilename; } else if (count($this->flushedSitemaps) > 1) { $ext = '.' . pathinfo($this->sitemapFileName, PATHINFO_EXTENSION); $targetExt = $ext; @@ -620,8 +541,8 @@ class SitemapGenerator $sitemapsUrls = []; $targetSitemapFilepaths = []; foreach ($this->flushedSitemaps as $i => $flushedSitemap) { - $targetSitemapFilename = str_replace($ext, ((int)$i + 1) . $targetExt, $this->sitemapFileName); - $targetSitemapFilepath = $this->saveDirectory . $targetSitemapFilename; + $targetSitemapFilename = str_replace($ext, ($i + 1) . $targetExt, $this->sitemapFileName); + $targetSitemapFilepath = $this->basePath . $targetSitemapFilename; if ($this->isCompressionEnabled) { $this->fs->copy($flushedSitemap, 'compress.zlib://' . $targetSitemapFilepath); @@ -629,24 +550,23 @@ class SitemapGenerator } else { $this->fs->rename($flushedSitemap, $targetSitemapFilepath); } - $sitemapsUrls[] = htmlspecialchars( - $this->sitemapIndexURL . '/' . $targetSitemapFilename, ENT_QUOTES); + $sitemapsUrls[] = htmlspecialchars($this->baseURL . '/' . $targetSitemapFilename, ENT_QUOTES); $targetSitemapFilepaths[] = $targetSitemapFilepath; } - $targetSitemapIndexFilepath = $this->saveDirectory . $this->sitemapIndexFileName; + $targetSitemapIndexFilepath = $this->basePath . $this->sitemapIndexFileName; $this->createSitemapIndex($sitemapsUrls, $targetSitemapIndexFilepath); $this->generatedFiles['sitemaps_location'] = $targetSitemapFilepaths; $this->generatedFiles['sitemaps_index_location'] = $targetSitemapIndexFilepath; - $this->generatedFiles['sitemaps_index_url'] = $this->sitemapIndexURL . '/' . $this->sitemapIndexFileName; + $this->generatedFiles['sitemaps_index_url'] = $this->baseURL . '/' . $this->sitemapIndexFileName; } else { throw new RuntimeException('failed to finalize, please add urls and flush first'); } } - private function createSitemapIndex(array $sitemapsUrls, string $sitemapIndexFileName): void + private function createSitemapIndex($sitemapsUrls, $sitemapIndexFileName) { - $this->xmlWriter->flush(); + $this->xmlWriter->flush(true); $this->writeSitemapIndexStart(); foreach ($sitemapsUrls as $sitemapsUrl) { $this->writeSitemapIndexUrl($sitemapsUrl); @@ -654,11 +574,12 @@ class SitemapGenerator $this->writeSitemapIndexEnd(); $this->fs->file_put_contents( $sitemapIndexFileName, - $this->xmlWriter->flush(), + $this->xmlWriter->flush(true), + FILE_APPEND ); } - protected function writeSitemapIndexStart(): void + private function writeSitemapIndexStart() { $this->xmlWriter->startDocument("1.0", "UTF-8"); $this->xmlWriter->writeComment(sprintf('generator-class="%s"', get_class($this))); @@ -670,19 +591,15 @@ class SitemapGenerator $this->xmlWriter->writeAttribute('xsi:schemaLocation', 'http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd'); } - /** - * @param string $url - * @throws UnexpectedValueException - */ - private function writeSitemapIndexUrl(string $url): void + private function writeSitemapIndexUrl($url) { $this->xmlWriter->startElement('sitemap'); - $this->xmlWriter->writeElement('loc', $this->encodeEscapeURL($url)); + $this->xmlWriter->writeElement('loc', htmlspecialchars($url, ENT_QUOTES)); $this->xmlWriter->writeElement('lastmod', date('c')); $this->xmlWriter->endElement(); // sitemap } - private function writeSitemapIndexEnd(): void + private function writeSitemapIndexEnd() { $this->xmlWriter->endElement(); // sitemapindex $this->xmlWriter->endDocument(); @@ -698,40 +615,34 @@ class SitemapGenerator /** * Will inform search engines about newly created sitemaps. - * Google and Yandex will be notified. + * Google, Ask, Bing and Yahoo will be noticed. + * If You don't pass yahooAppId, Yahoo still will be informed, + * but this method can be used once per day. If You will do this often, + * message that limit was exceeded will be returned from Yahoo. + * @param string $yahooAppId Your site Yahoo appid. * @return array of messages and http codes from each search engine * @access public * @throws BadMethodCallException */ - public function submitSitemap(): array + public function submitSitemap($yahooAppId = null): array { if (count($this->generatedFiles) === 0) { throw new BadMethodCallException("To update robots.txt, call finalize() first."); } if (!$this->runtime->extension_loaded('curl')) { - throw new BadMethodCallException("curl extension is needed to do submission."); + throw new BadMethodCallException("cURL extension is needed to do submission."); } $searchEngines = $this->searchEngines; + $searchEngines[0] = isset($yahooAppId) ? + str_replace("USERID", $yahooAppId, $searchEngines[0][0]) : + $searchEngines[0][1]; $result = []; for ($i = 0; $i < count($searchEngines); $i++) { $submitUrl = $searchEngines[$i] . htmlspecialchars($this->generatedFiles['sitemaps_index_url'], ENT_QUOTES); - $curlResource = $this->runtime->curl_init($submitUrl); - if (is_bool($curlResource) && !$curlResource) { - throw new RuntimeException("failed to execute curl_init for url " . $submitUrl); - } - if (!$this->runtime->curl_setopt($curlResource, CURLOPT_RETURNTRANSFER, true)) { - throw new RuntimeException( - "failed to set curl option CURLOPT_RETURNTRANSFER to true, error: " - . $this->runtime->curl_error($curlResource) - ); - } - $responseContent = $this->runtime->curl_exec($curlResource); - if (is_bool($responseContent) && !$responseContent) { - throw new RuntimeException( - "failed to run curl_exec, error: " . $this->runtime->curl_error($curlResource) - ); - } - $response = $this->runtime->curl_getinfo($curlResource); + $submitSite = $this->runtime->curl_init($submitUrl); + $this->runtime->curl_setopt($submitSite, CURLOPT_RETURNTRANSFER, true); + $responseContent = $this->runtime->curl_exec($submitSite); + $response = $this->runtime->curl_getinfo($submitSite); $submitSiteShort = array_reverse(explode(".", parse_url($searchEngines[$i], PHP_URL_HOST))); $result[] = [ "site" => $submitSiteShort[1] . "." . $submitSiteShort[0], @@ -759,7 +670,7 @@ class SitemapGenerator throw new BadMethodCallException("To update robots.txt, call finalize() first."); } - $robotsFilePath = $this->saveDirectory . $this->robotsFileName; + $robotsFilePath = $this->basePath . $this->robotsFileName; $robotsFileContent = $this->createNewRobotsContentFromFile($robotsFilePath); @@ -769,38 +680,28 @@ class SitemapGenerator } /** - * @param string $filepath + * @param $filepath * @return string - * @throws RuntimeException */ - private function createNewRobotsContentFromFile(string $filepath): string + private function createNewRobotsContentFromFile($filepath): string { if ($this->fs->file_exists($filepath)) { - $existingContent = $this->fs->file_get_contents($filepath); - // if $existingContent is bool and false, it means that file exists but is not readable - if (is_bool($existingContent) && !$existingContent) { - throw new RuntimeException("Failed to read existing robots.txt file: $filepath"); - } - if (is_string($existingContent)) { - $contentLines = explode(PHP_EOL, $existingContent); - } else { - $contentLines = []; - } - $newContent = ""; - foreach ($contentLines as $key => $line) { - if (str_starts_with($line, 'Sitemap:')) { - unset($contentLines[$key]); + $robotsFileContent = ""; + $robotsFile = explode(PHP_EOL, $this->fs->file_get_contents($filepath)); + foreach ($robotsFile as $key => $value) { + if (substr($value, 0, 8) == 'Sitemap:') { + unset($robotsFile[$key]); } else { - $newContent .= $line . PHP_EOL; + $robotsFileContent .= $value . PHP_EOL; } } } else { - $newContent = $this->getSampleRobotsContent(); + $robotsFileContent = $this->getSampleRobotsContent(); } - $newContent .= "Sitemap: {$this->generatedFiles['sitemaps_index_url']}"; + $robotsFileContent .= "Sitemap: {$this->generatedFiles['sitemaps_index_url']}"; - return $newContent; + return $robotsFileContent; } /** diff --git a/core/core.php b/core/core.php index 0610b09..1cbdaad 100644 --- a/core/core.php +++ b/core/core.php @@ -201,7 +201,22 @@ class common // Descripteur de données Entrées / Sorties // Liste ici tous les fichiers de données - public $dataFiles = []; + public $dataFiles = [ + 'admin' => null, + 'blacklist' => null, + 'config' => null, + 'core' => null, + 'course' => null, + 'font' => null, + 'module' => null, + 'page' => null, + 'theme' => null, + 'user' => null, + 'language' => null, + 'profil' => null, + 'enrolment' => null, + 'category' => null, + ]; private $configFiles = [ 'admin' => '', @@ -1105,133 +1120,126 @@ class common */ - public function updateSitemap() - { - // Le drapeau prend true quand au moins une page est trouvée - $flag = false; + public function updateSitemap() + { + // Le drapeau prend true quand au moins une page est trouvée + $flag = false; + + // Rafraîchit la liste des pages après une modification de pageId notamment + $this->buildHierarchy(); + + // Actualise la liste des pages pour TinyMCE + $this->tinyMcePages(); + + //require_once 'core/vendor/sitemap/SitemapGenerator.php'; + + $timezone = $this->getData(['config', 'timezone']); + $outputDir = getcwd(); + $sitemap = new \Icamys\SitemapGenerator\SitemapGenerator(helper::baseurl(false), $outputDir); + + // will create also compressed (gzipped) sitemap : option buguée + // $sitemap->enableCompression(); + + // determine how many urls should be put into one file + // according to standard protocol 50000 is maximum value (see http://www.sitemaps.org/protocol.html) + $sitemap->setMaxUrlsPerSitemap(50000); + + // sitemap file name + $sitemap->setSitemapFileName('sitemap.xml'); + + + // Set the sitemap index file name + $sitemap->setSitemapIndexFileName('sitemap-index.xml'); + + $datetime = new DateTime(date('c')); + $datetime->format(DateTime::ATOM); // Updated ISO8601 + + foreach ($this->getHierarchy() as $parentPageId => $childrenPageIds) { + // Exclure les barres et les pages non publiques et les pages masquées + if ( + $this->getData(['page', $parentPageId, 'group']) !== 0 || + $this->getData(['page', $parentPageId, 'block']) === 'bar' + ) { + continue; + } + // Page désactivée, traiter les sous-pages sans prendre en compte la page parente. + if ($this->getData(['page', $parentPageId, 'disable']) !== true) { + // Cas de la page d'accueil ne pas dupliquer l'URL + $pageId = ($parentPageId !== $this->homePageId()) ? $parentPageId : ''; + $sitemap->addUrl('/' . $pageId, $datetime); + $flag = true; + } + // Articles du blog + if ( + $this->getData(['page', $parentPageId, 'moduleId']) === 'blog' + && !empty($this->getData(['module', $parentPageId])) + && $this->getData(['module', $parentPageId, 'posts']) + ) { + foreach ($this->getData(['module', $parentPageId, 'posts']) as $articleId => $article) { + if ($this->getData(['module', $parentPageId, 'posts', $articleId, 'state']) === true) { + $date = $this->getData(['module', $parentPageId, 'posts', $articleId, 'publishedOn']); + $sitemap->addUrl('/' . $parentPageId . '/' . $articleId, DateTime::createFromFormat('U', $date)); + $flag = true; + } + } + } + // Sous-pages + foreach ($childrenPageIds as $childKey) { + if ($this->getData(['page', $childKey, 'group']) !== 0 || $this->getData(['page', $childKey, 'disable']) === true) { + continue; + } + // Cas de la page d'accueil ne pas dupliquer l'URL + $pageId = ($childKey !== $this->homePageId()) ? $childKey : ''; + $sitemap->addUrl('/' . $childKey, $datetime); + $flag = true; + + // La sous-page est un blog + if ( + $this->getData(['page', $childKey, 'moduleId']) === 'blog' && + !empty($this->getData(['module', $childKey])) + ) { + foreach ($this->getData(['module', $childKey, 'posts']) as $articleId => $article) { + if ($this->getData(['module', $childKey, 'posts', $articleId, 'state']) === true) { + $date = $this->getData(['module', $childKey, 'posts', $articleId, 'publishedOn']); + $sitemap->addUrl('/' . $childKey . '/' . $articleId, DateTime::createFromFormat('U', $date)); + $flag = true; + } + } + } + } + } + + if ($flag === false) { + return false; + } + + // Flush all stored urls from memory to the disk and close all necessary tags. + $sitemap->flush(); + + // Move flushed files to their final location. Compress if the option is enabled. + $sitemap->finalize(); + + // Update robots.txt file in output directory + + if ($this->getData(['config', 'seo', 'robots']) === true) { + if (file_exists('robots.txt')) { + unlink('robots.txt'); + } + $sitemap->updateRobots(); + } else { + file_put_contents('robots.txt', 'User-agent: *' . PHP_EOL . 'Disallow: /'); + } + + // Submit your sitemaps to Google, Yahoo, Bing and Ask.com + if (empty($this->getData(['config', 'proxyType']) . $this->getData(['config', 'proxyUrl']) . ':' . $this->getData(['config', 'proxyPort']))) { + $sitemap->submitSitemap(); + } + + return (file_exists('sitemap.xml') && file_exists('robots.txt')); + + } + - // Rafraîchit la liste des pages après une modification de pageId notamment - $this->buildHierarchy(); - - // Actualise la liste des pages pour TinyMCE - $this->tinyMcePages(); - - //require_once 'core/vendor/sitemap/SitemapGenerator.php'; - - $timezone = $this->getData(['config', 'timezone']); - - $config = new \Icamys\SitemapGenerator\Config(); - - - // Your site URL. - $config->setBaseURL(helper::baseurl(false)); - // // OPTIONAL. Setting the current working directory to be output directory - $config->setSaveDirectory(sys_get_temp_dir()); - - - $sitemap = new \Icamys\SitemapGenerator\SitemapGenerator($config); - - // Create a compressed sitemap - $sitemap->enableCompression(); - - // will create also compressed (gzipped) sitemap : option buguée - // $sitemap->enableCompression(); - - // determine how many urls should be put into one file - // according to standard protocol 50000 is maximum value (see http://www.sitemaps.org/protocol.html) - $sitemap->setMaxUrlsPerSitemap(50000); - - // sitemap file name - $sitemap->setSitemapFileName('sitemap.xml'); - - - // Set the sitemap index file name - $sitemap->setSitemapIndexFileName('sitemap-index.xml'); - - $datetime = new DateTime(date('c')); - $datetime->format(DateTime::ATOM); // Updated ISO8601 - - foreach ($this->getHierarchy() as $parentPageId => $childrenPageIds) { - // Exclure les barres et les pages non publiques et les pages masquées - if ( - $this->getData(['page', $parentPageId, 'group']) !== 0 || - $this->getData(['page', $parentPageId, 'block']) === 'bar' - ) { - continue; - } - // Page désactivée, traiter les sous-pages sans prendre en compte la page parente. - if ($this->getData(['page', $parentPageId, 'disable']) !== true) { - // Cas de la page d'accueil ne pas dupliquer l'URL - $pageId = ($parentPageId !== $this->getData(['locale', 'homePageId'])) ? $parentPageId : ''; - $sitemap->addUrl('/' . $pageId, $datetime); - $flag = true; - } - // Articles du blog - if ( - $this->getData(['page', $parentPageId, 'moduleId']) === 'blog' - && !empty($this->getData(['module', $parentPageId])) - && $this->getData(['module', $parentPageId, 'posts']) - ) { - foreach ($this->getData(['module', $parentPageId, 'posts']) as $articleId => $article) { - if ($this->getData(['module', $parentPageId, 'posts', $articleId, 'state']) === true) { - $date = $this->getData(['module', $parentPageId, 'posts', $articleId, 'publishedOn']); - $sitemap->addUrl('/' . $parentPageId . '/' . $articleId, DateTime::createFromFormat('U', $date)); - } - } - } - // Sous-pages - foreach ($childrenPageIds as $childKey) { - if ($this->getData(['page', $childKey, 'group']) !== 0 || $this->getData(['page', $childKey, 'disable']) === true) { - continue; - } - // Cas de la page d'accueil ne pas dupliquer l'URL - $pageId = ($childKey !== $this->getData(['locale', 'homePageId'])) ? $childKey : ''; - $sitemap->addUrl('/' . $childKey, $datetime); - $flag = true; - - // La sous-page est un blog - if ( - $this->getData(['page', $childKey, 'moduleId']) === 'blog' && - !empty($this->getData(['module', $childKey])) - ) { - foreach ($this->getData(['module', $childKey, 'posts']) as $articleId => $article) { - if ($this->getData(['module', $childKey, 'posts', $articleId, 'state']) === true) { - $date = $this->getData(['module', $childKey, 'posts', $articleId, 'publishedOn']); - $sitemap->addUrl('/' . $childKey . '/' . $articleId, new DateTime("@{$date}", new DateTimeZone($timezone))); - } - } - } - } - } - - if ($flag === false) { - return false; - } - - // Flush all stored urls from memory to the disk and close all necessary tags. - $sitemap->flush(); - - // Move flushed files to their final location. Compress if the option is enabled. - $sitemap->finalize(); - - // Update robots.txt file in output directory - - if ($this->getData(['config', 'seo', 'robots']) === true) { - if (file_exists('robots.txt')) { - unlink('robots.txt'); - } - $sitemap->updateRobots(); - } else { - $this->secure_file_put_contents('robots.txt', 'User-agent: *' . PHP_EOL . 'Disallow: /'); - } - - // Submit your sitemaps to Google, Yahoo, Bing and Ask.com - if (empty($this->getData(['config', 'proxyType']) . $this->getData(['config', 'proxyUrl']) . ':' . $this->getData(['config', 'proxyPort']))) { - $sitemap->submitSitemap(); - } - - return (file_exists('sitemap.xml') && file_exists('robots.txt')); - } /* * Création d'une miniature * Fonction utilisée lors de la mise à jour d'une version 9 à une version 10