CODE: Выделить всё
<?php
/**
* @file
* A working example for hook_post_action module.
*/
use Drupal\Core\Entity\EntityInterface;
use Drupal\Component\Utility\Html;
use Drupal\Component\Utility\Unicode;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\filter\Entity\FilterFormat;
require_once '_config.php';
define('CACHE_PATH', __DIR__ . '/cache/publish.dat');
/**
* Implements hook_entity_postsave().
*/
function mfb_forum_posting_entity_postsave(EntityInterface $entity, $op) {
$id = $entity->id();
$entity_type = $entity->getEntityTypeId();
\Drupal::logger('mfb_forum_posting')
->info("The {$op}d entity {$entity_type} id is {$id} from " . __FUNCTION__);
}
/**
* Implements hook_entity_postinsert().
*/
function mfb_forum_posting_entity_postinsert(EntityInterface $entity) {
$id = $entity->id();
$entity_type = $entity->getEntityTypeId();
\Drupal::logger('mfb_forum_posting')
->info("The inserted entity {$entity_type} id is {$id} from " . __FUNCTION__);
}
/**
* Implements hook_entity_postupdate().
*/
function mfb_forum_posting_entity_postupdate(EntityInterface $entity) {
$id = $entity->id();
$entity_type = $entity->getEntityTypeId();
\Drupal::logger('mfb_forum_posting')
->info("The updated entity {$entity_type} id is {$id} from " . __FUNCTION__);
}
/**
* Implements hook_entity_postdelete().
*/
function mfb_forum_posting_entity_postdelete(EntityInterface $entity) {
$id = $entity->id();
$entity_type = $entity->getEntityTypeId();
\Drupal::logger('mfb_forum_posting')
->info("The deleted entity {$entity_type} id is {$id} from " . __FUNCTION__);
}
function mfb_forum_posting_entity_presave(EntityInterface $entity) {
$id = $entity->id();
$entity_type = $entity->getEntityTypeId();
$poster_config = get_poster_conf();
$bundle = $entity->bundle();
if($id)
{
if ($entity_type == 'node') {
$post_title = $entity->getTitle();
$post_body = $entity->get('body')->value;
$post_teaser = $entity->get('body')->summary;
$enable_poster = true;
if(!$post_teaser)
$post_teaser = html_bbcode_format(text_summary_e($post_body));
$curr_config = $poster_config[$bundle];
if($entity->isPublished())
{
if($entity->hasField($poster_config['control_field']))
{
$items = $entity->get($poster_config['control_field']);
if(!$items[0]->value)
$enable_poster = false;
}
if($enable_poster)
{
//TODO: set id to field
$entity->set($curr_config['phpbb_link_field'], $id );
//
$options = ['absolute' => TRUE];
$post_url = \Drupal\Core\Url::fromRoute('entity.node.canonical', ['node' => $id], $options);
$post_url = $post_url->toString();
//send_post_to_forum (add or update) TODO: add node_id for update
$url_to_forum_post = send_post_to_forum($post_title, compile_template($curr_config['post_template'], $post_url, $post_body, $post_teaser) , $poster_config['poster_uri'], $curr_config['bot_id'], $curr_config['phpbb_forum_id'], $id);
\Drupal::logger('mfb_forum_posting')
->info("Post saved on forum - " . $url_to_forum_post );
}
else
{
$items = $entity->get($curr_config['phpbb_link_field']);
if(!isset($items[0]->value))
$entity->set($curr_config['phpbb_link_field'], '' );
\Drupal::logger('mfb_forum_posting')
->info("Poster API disabled by user" );
}
//DEPRECATED remove it
//$entity->set($curr_config['phpbb_link_field'], '/' . str_replace(array('./', '"'), '', $url_to_forum_post) );
}
else
{
$items = $entity->get($curr_config['phpbb_link_field']);
if(!isset($items[0]->value))
$entity->set($curr_config['phpbb_link_field'], '' );
}
}
}
\Drupal::logger('mfb_forum_posting')
->info("The presave entity {$entity_type} id is {$id} from " . __FUNCTION__);
}
/**
* Implements hook_ENTITY_TYPE_postsave().
*/
function mfb_forum_posting_node_postsave(EntityInterface $entity, $op) {
$id = $entity->id();
$bundle = $entity->bundle();
\Drupal::logger('mfb_forum_posting')
->info("The {$op}d node {$bundle} id is {$id} from " . __FUNCTION__ ) ;
}
/**
* Implements hook_ENTITY_TYPE_postinsert().
*/
function mfb_forum_posting_node_postinsert(EntityInterface $entity) {
$id = $entity->id();
$bundle = $entity->bundle();
//hack for force invoke presave()
$entity->save();
}
/**
* Implements hook_ENTITY_TYPE_postupdate().
*/
function mfb_forum_posting_node_postupdate(EntityInterface $entity) {
$id = $entity->id();
$bundle = $entity->bundle();
\Drupal::logger('mfb_forum_posting')
->info("The updated node {$bundle} id is {$id} from " . __FUNCTION__);
}
/**
* Implements hook_ENTITY_TYPE_postdelete().
*/
function mfb_forum_posting_node_postdelete(EntityInterface $entity) {
$id = $entity->id();
$bundle = $entity->bundle();
\Drupal::logger('mfb_forum_posting')
->info("The deleted node {$bundle} id is {$id} from " . __FUNCTION__);
}
function send_post_to_forum($title, $body, $poster_uri, $bot_id, $forum_id, $node_id)
{
$postdata = array(
'title' => $title,
'body' => $body,
'botid' => $bot_id,
'forumid' => $forum_id,
'nodeid' => $node_id,
'mode' => 0
);
$r = api_request($poster_uri, $postdata);
$r = json_decode($r, true);
return $r['topic_link'];
}
function api_request($apiUrl, $post_data)
{
$r = '';
$user_agent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.10134';
$post_data = http_build_query($post_data);
//use curl
if(function_exists('curl_init'))
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $apiUrl);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
$r = curl_exec($ch);
curl_close($ch);
}
else
{
$opts = array('http' =>
array(
'method' => 'POST',
'user_agent' => $user_agent,
'protocol_version' => 1.1,
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $post_data
)
);
$context = stream_context_create($opts);
$r = file_get_contents($apiUrl, false, $context);
}
return $r;
}
function compile_template($template, $url, $body, $teaser)
{
$replacements = array(
'/\[URL\](.*?)\[\/URL\]/i' => '[URL="' . $url .'"]$1[/URL]',
'/\{BODY\}/i' => html_bbcode_format($body),
'/\{TEASER\}/i' => $teaser
);
//'[URL="' . $url .'"]$1[/URL]'
foreach($replacements as $match=>$replacement){
$template = preg_replace($match, $replacement, $template);
}
return $template;
}
function html_bbcode_format($html){
$replace = str_replace(';;','',$html);
$replace = str_replace('"','"',$replace);
//quote with name
$replace = str_replace('<blockquote><div><cite>','[quote="',$replace);
$replace = str_replace(' wrote:</cite>','"]',$replace);
$replace = str_replace(' schreef:</cite>','"]',$replace);//dutch
//quote without name
$replace = str_replace('<blockquote>','[quote]',$replace);
$replace = str_replace('</blockquote>','[/quote]',$replace);
$replace = str_replace('<','<',$replace);
$replace = str_replace('>','>',$replace);
$replace = str_replace('<br />',"\n",$replace);
$replace = str_replace('</div>', '', $replace);
$replace = str_replace('<p>','',$replace);
$replace = str_replace('</p>', "\n", $replace);
$replace = preg_replace('<div align="(.*?)">', '', $replace);
$replace = preg_replace('<div>', '', $replace);
$replace = preg_replace('/\<iframe(.*?)src="(http|https):\/\/www\.youtube.com\/embed\/(.*?)"(.*?)<\/iframe>/i', '[video]https://youtube.com/watch?v=$3[/video]', $replace);
$replace = preg_replace('/\<img src="(.*?)" \/>/i', '[img]$1[/img]', $replace);
$replace = preg_replace('/\<img(.*?)src="(.*?)"(.*?)>/i', '[img]$2[/img]', $replace);
$replace = preg_replace('/\<font size=\"([1-7])\"\>((\s|.)+?)\<\/font>/i', '[size=\\1]\\2[/size]', $replace);
$replace = preg_replace('/\<font color=\"(#[a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9])\"\>((\s|.)+?)\<\/font>/i', '[color=\\1]\\2[/color]', $replace);
$replace = preg_replace('/\<font color=\"([a-zA-Z]+)\]((\s|.)+?)\<\/font>/i', '[color=\\1]\\2[/color]', $replace);
//$replace = preg_replace("/\<a href=\"((http|ftp|https|ftps|irc):\/\/[^<>\s]+?)\">((\s|.)+?)\<\/a\>/i","[url=\\1]\\3[/url]", $replace);
$replace = preg_replace("/\<a href=\"([^<>\s]+?)\">(.*?)\<\/a\>/i","[url=\\1]\\2[/url]", $replace);
$replace = preg_replace('/\<span style="font-weight: bold">((\s|.)+?)\<\/span>/i', '[b]\\1[/b]', $replace);
$replace = str_replace('<i>','[i]',$replace);
$replace = str_replace('</i>','[/i]',$replace);
$replace = str_replace('<em>','[i]',$replace);
$replace = str_replace('</em>','[/i]',$replace);
$replace = str_replace('<b>','[b]',$replace);
$replace = str_replace('</b>','[/b]',$replace);
$replace = str_replace('<strong>','[b]',$replace);
$replace = str_replace('</strong>','[/b]',$replace);
$replace = str_replace('<u>','[u]',$replace);
$replace = str_replace('</u>','[/u]',$replace);
//$replace = str_replace('<','',$replace);
//$replace = str_replace('/>','',$replace);
//$replace = str_replace('>','',$replace);
$replace = str_replace(' ',' ',$replace);
$replace = str_replace('<code>','[code]',$replace);
$replace = str_replace('</code>','
',$replace);
//lists
$find_li = array(
'/\<ul\>(.*?)\<\/ul\>/is',
'/\<li\>(.*?)\<\/li\>/is',
);
$replace_li = array(
'
',
'[*]$1[/*]',
);
$replace = preg_replace($find_li, $replace_li, $replace);
return $replace;
}
function text_summary_e($text, $format = NULL, $size = NULL) {
if (!isset($size)) {
$size = \Drupal::config('text.settings')->get('default_summary_length');
}
// Find where the delimiter is in the body
$delimiter = strpos($text, '<!--break-->');
// If the size is zero, and there is no delimiter, the entire body is the summary.
if ($size == 0 && $delimiter === FALSE) {
return $text;
}
// If a valid delimiter has been specified, use it to chop off the summary.
if ($delimiter !== FALSE) {
return substr($text, 0, $delimiter);
}
// Retrieve the filters of the specified text format, if any.
if (isset($format)) {
$filters = FilterFormat::load($format)->filters();
// If the specified format does not exist, return nothing. $text is already
// filtered text, but the remainder of this function will not be able to
// ensure a sane and secure summary.
if (!$filters) {
return '';
}
}
// If we have a short body, the entire body is the summary.
if (Unicode::strlen($text) <= $size) {
return $text;
}
// If the delimiter has not been specified, try to split at paragraph or
// sentence boundaries.
// The summary may not be longer than maximum length specified. Initial slice.
$summary = Unicode::truncate($text, $size);
// Store the actual length of the UTF8 string -- which might not be the same
// as $size.
$max_rpos = strlen($summary);
// How much to cut off the end of the summary so that it doesn't end in the
// middle of a paragraph, sentence, or word.
// Initialize it to maximum in order to find the minimum.
$min_rpos = $max_rpos;
// Store the reverse of the summary. We use strpos on the reversed needle and
// haystack for speed and convenience.
$reversed = strrev($summary);
// Build an array of arrays of break points grouped by preference.
$break_points = array();
// A paragraph near the end of sliced summary is most preferable.
$break_points[] = array('</p>' => 0, 'N_BREAK' => 1);
// If no complete paragraph then treat line breaks as paragraphs.
$line_breaks = array('<br />' => 6, '<br>' => 4);
// Newline only indicates a line break if line break converter
// filter is present.
//if (isset($format) && $filters->has('filter_autop') && $filters->get('filter_autop')->status) {
// $line_breaks["N_BREAK"] = 1;
//}
$break_points[] = $line_breaks;
// If the first paragraph is too long, split at the end of a sentence.
$break_points[] = array('. ' => 1, '! ' => 1, '? ' => 1, '。' => 0, '؟ ' => 1);
// Iterate over the groups of break points until a break point is found.
foreach ($break_points as $points) {
// Look for each break point, starting at the end of the summary.
foreach ($points as $point => $offset) {
// The summary is already reversed, but the break point isn't.
$rpos = false;
if($point=="N_BREAK")
{
$rpos = strpos($reversed, "\n");
}
else
$rpos = strpos($reversed, strrev($point));
if ($rpos !== FALSE) {
$min_rpos = min($rpos + $offset, $min_rpos);
}
}
// If a break point was found in this group, slice and stop searching.
if ($min_rpos !== $max_rpos) {
// Don't slice with length 0. Length must be <0 to slice from RHS.
$summary = ($min_rpos === 0) ? $summary : substr($summary, 0, 0 - $min_rpos);
break;
}
}
// If the htmlcorrector filter is present, apply it to the generated summary.
// if (isset($format) && $filters->has('filter_htmlcorrector') && $filters->get('filter_htmlcorrector')->status) {
$summary = html_entity_decode(Html::normalize($summary));
//}
return $summary;
}
[/code]