Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
fuzegit committed Mar 28, 2023
0 parents commit 472f478
Show file tree
Hide file tree
Showing 20 changed files with 462 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Компонент IndexNow InstantCMS2

Уведомление поисковых систем о новых материалах на сайте с помощью IndexNow. Поддерживаются Yandex и Bing.
Binary file added icon.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions install.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

function install_package(){
return true;
}
25 changes: 25 additions & 0 deletions manifest.ru.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
[info]
title = "IndexNow"
image = "icon.png"
addon_id = "325"

[install]
type = "component"
name = "ping"

[version]
major = "2"
minor = "1"
build = "0"
date = "20230328"

[depends]
core = "2.14.0"

[author]
name = "InstantCMS Team"
url = "https://instantcms.ru/"

[description]
text[] = "Уведомление поисковых систем о новых материалах на сайте "
text[] = "с помощью IndexNow. Поддерживаются Yandex и Bing."
33 changes: 33 additions & 0 deletions package/system/controllers/ping/backend.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

class backendPing extends cmsBackend {

public $useDefaultOptionsAction = true;

public $queue = [
'queues' => ['ping'],
'queue_name' => LANG_PING_CONTROLLER,
'use_queue_action' => true
];

public function __construct(cmsRequest $request) {

parent::__construct($request);

array_unshift($this->backend_menu,
[
'title' => LANG_OPTIONS,
'url' => href_to($this->root_url, 'options'),
'options' => [
'icon' => 'cog'
]
]
);

}

public function actionIndex() {
$this->redirectToAction('options');
}

}
45 changes: 45 additions & 0 deletions package/system/controllers/ping/backend/forms/form_options.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

class formPingOptions extends cmsForm {

public function init($options = []) {

return [
[
'type' => 'fieldset',
'title' => LANG_PING_KEYS,
'childs' => [
new fieldString('ya_key', [
'title' => sprintf(LANG_PING_KEY, 'Yandex'),
'hint' => LANG_PING_KEY_HINT,
'default' => string_random(),
'rules' => [
['required'],
['regexp', '#^[a-zA-Z0-9\-]+$#u'],
['min_length', 8],
['max_length', 128]
]
]),
new fieldString('bing_key', [
'title' => sprintf(LANG_PING_KEY, 'Bing'),
'hint' => LANG_PING_KEY_HINT,
'default' => string_random(),
'rules' => [
['required'],
['regexp', '#^[a-zA-Z0-9\-]+$#u'],
['min_length', 8],
['max_length', 128]
]
])
]
],
[
'type' => 'html',
'content' => $options ? sprintf(LANG_PING_KEY_PATHS,
href_to($options['ya_key'].'.txt'), href_to_abs($options['ya_key'].'.txt'),
href_to($options['bing_key'].'.txt'), href_to_abs($options['bing_key'].'.txt')) : ''
]
];
}

}
135 changes: 135 additions & 0 deletions package/system/controllers/ping/frontend.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
<?php

class ping extends cmsFrontend {

protected $useOptions = true;

/**
* Поддерживаемые ПС
*
* @var array
*/
private $searchengines = [
'ya_key' => 'https://yandex.com/',
'bing_key' => 'https://www.bing.com/'
];

/**
* Экшен для TXT файлов
*
* @param string $key ключ
*/
public function actionGetKey($key) {

foreach ($this->searchengines as $key_name => $url) {

if ($this->options[$key_name] === $key) {

header('Content-Disposition: inline; filename="'.$key.'.txt"');
header('Content-type: text/plain');

die($key);
}
}

return cmsCore::error404();
}

/**
* Ставит URL в очередь
*
* @param string $page_url URL страницы для отправки
*/
public function processPing($page_url) {

cmsQueue::pushOn('ping', [
'controller' => $this->name,
'hook' => 'queue_ping',
'params' => [
$page_url
]
]);

}

/**
* Выполняет запросы из очереди, см. хук onPingQueuePing
*
* @param string $page_url URL страницы для отправки
*/
public function sendPing($page_url) {

$model = new cmsModel();

// Администраторы сайта
$admin_ids = $model->filterEqual('is_admin', 1)->
selectOnly('id')->
get('{users}', function($item, $model){
return $item['id'];
}, false);

foreach ($this->searchengines as $key_name => $url) {

$httpcode = $this->processSearchenginePing($key_name, $page_url);

if($httpcode != '200'){

// Отправляем уведомления
$this->controller_messages->addRecipients($admin_ids)->sendNoticePM([
'content' => sprintf(LANG_PING_NOTICE, $url, $httpcode, $page_url)
]);

$this->controller_messages->clearRecipients();
}
}
}

/**
* Непосредственно собирает и выполняет запрос
*
* @param string $key_name Ключ опции
* @param string $page_url URL страницы для отправки
* @return string
*/
private function processSearchenginePing($key_name, $page_url) {

$curl = curl_init();

curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 3);

if(is_array($page_url)){

$request = [
'host' => $this->cms_config->host,
'key' => $this->options[$key_name],
'keyLocation' => href_to_abs($this->options[$key_name] . '.txt'),
'urlList' => $page_url
];

curl_setopt($curl, CURLOPT_URL, $this->searchengines[$key_name].'indexnow');
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, ['Content-Type: application/json; charset=utf-8']);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($request));

} else {

$request = [
'url' => $page_url,
'key' => $this->options[$key_name]
];

curl_setopt($curl, CURLOPT_URL, $this->searchengines[$key_name].'indexnow?' . http_build_query($request));
}

curl_setopt($curl, CURLOPT_HEADER, true);
curl_exec($curl);

$httpcode = curl_getinfo($curl, CURLINFO_RESPONSE_CODE);

curl_close($curl);

return $httpcode;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

class onPingContentAfterAddApprove extends cmsAction {

public function run($data) {

$ctype = cmsCore::getModel('content')->getContentTypeByName($data['ctype_name']);
$item = $data['item'];

if (empty($ctype['options']['is_ping'])) {
return $data;
}

$this->processPing(href_to_abs($ctype['name'], $item['slug'] . '.html'));

return $data;
}

}
19 changes: 19 additions & 0 deletions package/system/controllers/ping/hooks/content_after_delete.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

class onPingContentAfterDelete extends cmsAction {

public function run($data) {

$ctype = $data['ctype'];
$item = $data['item'];

if (empty($ctype['options']['is_ping'])) {
return $data;
}

$this->processPing(href_to_abs($ctype['name'], $item['slug'] . '.html'));

return $data;
}

}
20 changes: 20 additions & 0 deletions package/system/controllers/ping/hooks/content_after_restore.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

class onPingContentAfterRestore extends cmsAction {

public function run($data) {

list($ctype_name, $item) = $data;

$ctype = cmsCore::getModel('content')->getContentTypeByName($ctype_name);

if (empty($ctype['options']['is_ping'])) {
return $data;
}

$this->processPing(href_to_abs($ctype['name'], $item['slug'] . '.html'));

return $data;
}

}
20 changes: 20 additions & 0 deletions package/system/controllers/ping/hooks/content_after_trash_put.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

class onPingContentAfterTrashPut extends cmsAction {

public function run($data) {

list($ctype_name, $item) = $data;

$ctype = cmsCore::getModel('content')->getContentTypeByName($ctype_name);

if (empty($ctype['options']['is_ping'])) {
return $data;
}

$this->processPing(href_to_abs($ctype['name'], $item['slug'] . '.html'));

return $data;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

class onPingContentAfterUpdateApprove extends cmsAction {

public function run($data) {

$ctype = cmsCore::getModel('content')->getContentTypeByName($data['ctype_name']);
$item = $data['item'];

if (empty($ctype['options']['is_ping'])) {
return $data;
}

$this->processPing(href_to_abs($ctype['name'], $item['slug'] . '.html'));

return $data;
}

}
16 changes: 16 additions & 0 deletions package/system/controllers/ping/hooks/ctype_basic_form.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

class onPingCtypeBasicForm extends cmsAction {

public function run($form) {

$fieldset = $form->addFieldsetAfter('tags', LANG_PING_CONTROLLER, 'ping', ['is_collapsed' => true]);

$form->addField($fieldset, new fieldCheckbox('options:is_ping', [
'title' => LANG_PING_ENABLE
]));

return $form;
}

}
22 changes: 22 additions & 0 deletions package/system/controllers/ping/hooks/engine_start.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

class onPingEngineStart extends cmsAction {

public function run() {

$matches = [];

$regexp = '#^([a-zA-Z0-9\-]+)\.txt$#u';

if(preg_match($regexp, $this->cms_core->uri_action, $matches) ||
preg_match($regexp, $this->cms_core->uri_controller, $matches)){

$this->cms_core->uri_controller = 'ping';
$this->cms_core->uri_action = 'get_key';
$this->cms_core->uri_params = [$matches[1]];
}

return true;
}

}

0 comments on commit 472f478

Please sign in to comment.