<?php
/* Copyright (c) 2022-2024 Aimy Extensions, Netzum Sorglos Software GmbH
 *
 * https://www.aimy-extensions.com/
 *
 * License: GNU GPLv2, see LICENSE.txt within distribution and/or
 *          http://www.aimy-extensions.com/software-license.html
 */
 namespace Aimy\IndexNow; defined( '_JEXEC' ) or die(); use Joomla\CMS\Http\HttpFactory; use Joomla\CMS\Language\Text; use Joomla\CMS\Uri\Uri; abstract class Notifier { static private $endpoint = 'https://api.indexnow.org/indexnow'; const MAX_URLS_PER_POST = 10000; static public function submitSingleUrl( $contentUrl, $key ) { $url = self::$endpoint . sprintf( '?url=%s&key=%s', rawurlencode( $contentUrl ), $key ); $resp = HttpFactory::getHttp()->get( $url, array(), 5 ); if ( $resp->code !== 200 && $resp->code !== 202 ) { throw new \RuntimeException( Text::sprintf( 'AIMY_IN_ERR_HTTP_RESPONSE_X', $resp->code, self::getCause( $resp->code ) ) ); } return true; } static public function submitMultipleUrls( $contentUrls, $key ) { if ( ! count( $contentUrls ) ) { return 0; } $d = new \StdClass(); $d->host = Uri::getInstance()->getHost(); $d->key = $key; $d->urlList = array(); $offset = 0; do { $d->urlList = array_slice( $contentUrls, $offset, self::MAX_URLS_PER_POST ); $offset += count( $d->urlList ); $resp = HttpFactory::getHttp()->post( self::$endpoint, json_encode( $d ), array( 'Content-Type' => 'application/json; charset=utf-8' ) ); if ( $resp->code !== 200 && $resp->code !== 202 ) { throw new \RuntimeException( Text::sprintf( 'AIMY_IN_ERR_HTTP_RESPONSE_X', $resp->code, self::getCause( $resp->code ) ) ); } } while ( $offset < count( $contentUrls ) ); return $offset; } static private function getCause( $code ) { switch ( $code ) { case 400: return 'Bad request'; break; case 403: return 'Forbidden'; break; case 422: return 'Unprocessable entity'; break; case 429: return 'Too many requests'; break; } return 'Unknown'; } } 
