| Current Path : /home/digilove/public_html/plugins/content/aimyindexnow/ |
| Current File : /home/digilove/public_html/plugins/content/aimyindexnow/aimyindexnow.php |
<?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
*/
defined( '_JEXEC' ) or die(); require_once( __DIR__ . '/notifier.php' ); require_once( __DIR__ . '/helpers/ComponentInfoHelper.php' ); require_once( __DIR__ . '/helpers/ComponentDataHelper.php' ); require_once( __DIR__ . '/helpers/PluginHelper.php' ); require_once( __DIR__ . '/helpers/StorageHelper.php' ); require_once( __DIR__ . '/helpers/ExcludePatternHelper.php' ); use Aimy\IndexNow\Notifier; use Aimy\IndexNow\Helpers\ComponentInfoHelper; use Aimy\IndexNow\Helpers\ComponentDataHelper; use Aimy\IndexNow\Helpers\PluginHelper; use Aimy\IndexNow\Helpers\StorageHelper; use Aimy\IndexNow\Helpers\ExcludePatternHelper; use Joomla\CMS\Factory; use Joomla\CMS\Uri\Uri; use Joomla\CMS\Plugin\CMSPlugin; use Joomla\CMS\Language\Text; use Joomla\CMS\Session\Session; class plgContentAimyIndexNow extends CMSPlugin { protected $autoloadLanguage = true; protected $app; public function __construct( $subject, $config = array() ) { parent::__construct( $subject, $config ); if ( ! $this->shouldRun() ) { return; } if ( $this->params->get( 'mode', 'ask' ) == 'ask' && ( PluginHelper::inBackend() or PluginHelper::isFrontendEditing() ) ) { Factory::getDocument() ->addScript( Uri::root( true ) . '/media/plg_aimyindexnow/submit.js' ) ->addStyleDeclaration( '#AimyIndexNow-send{margin-left:24px;}' . '#AimyIndexNow-send.active{background: transparent url("' . Uri::root( true ) . '/media/plg_aimyindexnow/wait.gif' . '") no-repeat center center !important;' . 'min-width:24px;min-height:24px;border:0;}' . '.AimyIndexNow-rv div{margin:6px 0;}' . '.AimyIndexNow-rv > div.success:first-child::after{' . 'content:"\2713";color:#390;' . 'font-size:2em;margin-left:12px;}' . '.AimyIndexNow-rv .error::after{content:"\274C";color:#F80;' . 'font-size:1.2em;margin-left:12px;}' . '.AimyIndexNow-rv .notice::after{content:"\2757";' . 'color:#F90;font-size:1.2em;margin-left:12px;}' ); } if ( ! PluginHelper::isJoomla3() && $this->app->input->get( 'option', '' ) != 'com_media' ) { $sess = Factory::getSession(); if ( $sess && $sess->has( 'msgs', 'AimyIndexNow' ) ) { $msgs = $sess->get( 'msgs', false, 'AimyIndexNow' ); if ( $msgs && is_array( $msgs ) && ! empty( $msgs ) ) { foreach ( $msgs as $queue => $qMsgs ) { foreach ( $qMsgs as $i => $msg ) { $this->app->enqueueMessage( $msg, $queue ); } } } $sess->clear( 'msgs', 'AimyIndexNow' ); } } } public function onContentAfterSave( $context, $article, $isNew ) { if ( ! $this->shouldRun() ) { return; } return $this->handle( $context, $article ); } public function onContentChangeState( $context, $ids, $state ) { if ( ! $this->shouldRun() ) { return; } if ( ! $this->hasContextSupportEnabled( $context ) ) { return; } foreach ( $ids as $i => $id ) { if ( ( $data = ComponentDataHelper::get( $context, $id ) ) ) { $this->handle( $context, $data ); } else { $this->queueMessage( '<b>Aimy IndexNow</b>: ' . 'Failed to get data for item #' . $id, 'error' ); } } } public function handle( $context, &$data ) { if ( ! $this->hasContextSupportEnabled( $context ) ) { return; } $page = ComponentInfoHelper::get( $context, $data ); if ( ! $page or ! $page->path ) { return false; } if ( ! $page->isPublished or ! $page->isPublic ) { return false; } if ( isset( $page->robots ) && ! empty( $page->robots ) ) { if ( stripos( $page->robots, 'noindex' ) !== false ) { return false; } } if ( $patterns = $this->params->get( "exclude-patterns", false ) ) { $match = ExcludePatternHelper::matches( explode( "\n", $patterns ), $page->path ); if ( $match !== false ) { $this->queueMessage( '<b>Aimy IndexNow</b>: ' . JText::sprintf( 'AIMY_IN_URL_EXCLUDED_BY_PATTERN_XY', $page->url, $match ), 'notice' ); return false; } } $mode = $this->params->get( 'mode', 'ask' ); if ( $mode == 'off' ) { return $this->passUrlToSitemap( $page ); } if ( ( $lastSubmit = StorageHelper::get( $page->path ) ) ) { $elapsed = PluginHelper::getElapsedMinutes( $lastSubmit ); $interval = $this->params->get( 'min-submit-interval', 10 ); if ( $elapsed <= $interval ) { $this->queueMessage( '<b>Aimy IndexNow</b>: ' . Text::sprintf( 'AIMY_IN_URL_SUBMITTED_X_MINUTES_AGO', $elapsed ), 'notice' ); return false; } } if ( $this->params->get( 'disable-local', true ) ) { $ip = PluginHelper::getWebsiteIpAddress(); if ( $ip && PluginHelper::isLocalIpAddress( $ip ) ) { $this->queueMessage( '<b>Aimy IndexNow</b>: ' . Text::sprintf( 'AIMY_IN_NOTIFICATION_SKIPPED_LOCAL_IP', $ip ), 'info' ); return false; } } if ( $mode == 'auto' ) { return $this->submit( $page ); } return $this->showNotificationSendable( $page ); } public function submit( $page ) { if ( ! is_object( $page ) or ! isset( $page->url ) ) { throw new \RuntimeException( '<b>Aimy IndexNow</b>: invalid page data' ); } $key = false; try { $key = $this->getApiKey(); } catch ( \Exception $e ) { $this->queueMessage( '<b>Aimy IndexNow</b>: ' . $e->getMessage(), 'error' ); return false; } $this->passUrlToSitemap( $page ); try { Notifier::submitSingleUrl( $page->url, $key ); $this->queueMessage( '<b>Aimy IndexNow</b>: ' . Text::sprintf( 'AIMY_IN_NOTIFICATION_SENT_X', $page->url ) ); StorageHelper::set( $page->path, time() ); } catch ( \Exception $e ) { $this->queueMessage( '<b>Aimy IndexNow</b>: ' . $e->getMessage(), 'error' ); return false; } return true; } public function getApiKey() { $key = trim( $this->params->get( 'key', '' ) ); PluginHelper::checkKey( $key ); if ( PluginHelper::writeKeyfileIfNotExistent( $key ) ) { $this->queueMessage( '<b>Aimy IndexNow</b>: ' . Text::sprintf( 'AIMY_IN_KEYFILE_X_WRITTEN', PluginHelper::getKeyfileName( $key ) ), 'notice' ); } return $key; } private function showNotificationSendable( $page ) { $key = false; try { $key = $this->getApiKey(); } catch ( \Exception $e ) { $this->queueMessage( $e->getMessage(), 'error' ); return false; } $pageData = rawurlencode( json_encode( $page ) ); $this->queueMessage( '<b>Aimy IndexNow</b>: ' . Text::sprintf( 'AIMY_IN_NOTIFICATION_SENDABLE_X', $page->url, '<button class="btn btn-primary" id="AimyIndexNow-send" ' . 'data-page="' . $pageData . '" ' . 'data-key="' . $key . '">' . Text::_( 'AIMY_IN_NOTIFICATION_SEND_NOW' ) . '</button>' ) ); } private function shouldRun() { if ( $this->isEmbeddedInFrame() ) { return false; } if ( ! ( PluginHelper::inBackend() or PluginHelper::isFrontendEditing() ) ) { return false; } return true; } private function isEmbeddedInFrame() { $tmpl = $this->app->input->get( 'tmpl', false, 'word' ); if ( $tmpl && $tmpl == 'component' ) { $comp = $this->app->input->get( 'option', false, 'word' ); if ( ! $comp or $comp != 'com_media' ) { return true; } } return false; } private function hasContextSupportEnabled( $context ) { $comp = PluginHelper::getCtxComponent( $context ); return $this->params->get( 'support-' . $comp, false ); } private function queueMessage( $msg, $queue = 'notice' ) { if ( PluginHelper::isJoomla3() ) { $this->app->enqueueMessage( $msg, $queue ); } else { if ( $this->app->input->get( 'option' ) == 'com_media' ) { $sess = Factory::getSession(); $msgs = $sess->get( 'msgs', [], 'AimyIndexNow' ); if ( ! isset( $msgs[ $queue ] ) ) { $msgs[ $queue ] = []; } $msgs[ $queue ][] = $msg; $sess->set( 'msgs', $msgs, 'AimyIndexNow' ); } else { $this->app->enqueueMessage( $msg, $queue ); } } } private function passUrlToSitemap( $page ) { if ( ! $this->params->get( 'pass-to-sitemap', false ) ) { return false; } try { require_once( __DIR__ . '/sitemap.php' ); $rv = Aimy\IndexNow\Sitemap::addOrUpdateURL( $page ); $msg = false; if ( $rv == Aimy\IndexNow\Sitemap::URL_ADDED ) { $msg = Text::sprintf( 'AIMY_IN_AIMY_SITEMAP_ADDED_X', $page->path ); } elseif ( $rv == Aimy\IndexNow\Sitemap::URL_UPDATED ) { $msg = Text::sprintf( 'AIMY_IN_AIMY_SITEMAP_UPDATED_X', $page->path ); } if ( $msg ) { $this->queueMessage( '<b>Aimy IndexNow → Aimy Sitemap</b>: ' . $msg ); } if ( $this->params->get( 'write-sitemap', false ) ) { Aimy\IndexNow\Sitemap::writeXMLSitemap(); } } catch( \Exception $e ) { $this->queueMessage( $e->getMessage(), 'error' ); return false; } return true; } public function onExtensionAfterSave( $context, $table, $isNew, $data = array() ) { if ( strpos( JVERSION, '3.' ) !== 0 ) { return; } if ( $context != 'com_plugins.plugin' or ! is_object( $table ) or ! property_exists( $table, 'element' ) or $table->element != 'aimyindexnow' ) { return; } if ( ! is_array( $data ) or empty( $data ) or ! isset( $data[ 'params' ] ) or ! is_array( $data[ 'params' ] ) or ! isset( $data[ 'params' ][ 'dl_key' ] ) ) { return; } $key = trim( $data[ 'params' ][ 'dl_key' ] ); require_once( __DIR__ . '/helpers/DownloadKeyHelper.php' ); try { AimyIndexNow\Helpers\DownloadKeyHelper::passKeyToUpdatesites( $key ); } catch ( Exception $e ) { Factory::getApplication( 'Failed to pass download key to update sites: ' . $e->getMessage(), 'error' ); } } public function onInstallerBeforePackageDownload( &$url, &$headers ) { if ( ! strpos( $url, 'aimy-extensions.com' ) or ! strpos( $url, '/plg_AimyIndexNow/' ) ) { return; } require_once( __DIR__ . '/helpers/DownloadKeyHelper.php' ); $rv = AimyIndexNow\Helpers\DownloadKeyHelper::validateKey(); if ( is_object( $rv ) && ! $rv->valid && isset( $rv->msg ) ) { Factory::getApplication()->enqueueMessage( '<b>Aimy IndexNow: ' . $rv->msg . '</b>', 'error' ); } } public function onAjaxAimyindexnow() { if ( ! Session::checkToken() ) { throw new \RuntimeException( '<b>Aimy IndexNow</b>: ' . Text::_( 'JINVALID_TOKEN' ) ); } $key = $this->app->input->post->get( 'key', false, 'string' ); if ( $this->getApiKey() != $key ) { throw new \RuntimeException( '<b>Aimy IndexNow</b>: Key mismatch: ' . $key ); } $page = false; try { $page = json_decode( rawurldecode( $this->app->input->post->get( 'page', false, 'string' ) ), false ); if ( ! $page or ! is_object( $page ) ) { throw new \RuntimeException( 'No page data retrieved' ); } } catch ( \Exception $e ) { throw new \RuntimeException( '<b>Aimy IndexNow</b>: ' . $e->getMessage() ); } if ( ! $this->submit( $page ) ) { throw new \RuntimeException(); } return true; } }