ZwiiCMS/module/search/search.php

386 lines
14 KiB
PHP
Raw Normal View History

2020-08-14 15:24:14 +02:00
<?php
/**
* This file is part of Zwii.
*
* For full copyright and license information, please see the LICENSE
* file that was distributed with this source code.
*
* @author Rémi Jean <remi.jean@outlook.com>
* @copyright Copyright (C) 2008-2018, Rémi Jean
2020-08-16 10:55:52 +02:00
* @author Frédéric Tempez <frederic.tempez@outlook.com>
2021-02-17 13:49:58 +01:00
* @copyright Copyright (C) 2018-2021, Frédéric Tempez
2021-04-05 08:59:24 +02:00
* @author Sylvain Lelièvre <lelievresylvain@free.fr>
* @copyright Copyright (C) 2020-2021, Sylvain Lelièvre
2020-08-14 15:24:14 +02:00
* @license GNU General Public License, version 3
* @link http://zwiicms.fr/
2020-08-14 15:24:14 +02:00
*
*/
2020-08-15 09:35:43 +02:00
class search extends common {
2020-08-14 15:24:14 +02:00
2021-04-05 08:59:24 +02:00
const VERSION = '2.0';
const REALNAME = 'Recherche';
const DELETE = true;
2021-02-25 07:53:57 +01:00
const UPDATE = '0.0';
2021-02-19 09:08:56 +01:00
const DATADIRECTORY = []; // Contenu localisé inclus par défaut (page.json et module.json)
2020-08-14 15:24:14 +02:00
public static $actions = [
'index' => self::GROUP_VISITOR,
'config' => self::GROUP_MODERATOR
2020-08-14 15:24:14 +02:00
];
2020-08-20 16:11:11 +02:00
// Variables pour l'affichage des résultats
2020-08-14 16:00:42 +02:00
public static $resultList = '';
public static $resultError = '';
2020-08-14 16:00:42 +02:00
public static $resultTitle = '';
2020-08-14 15:24:14 +02:00
2020-08-20 16:11:11 +02:00
// Variables pour le dialogue avec le formulaire
2020-08-15 09:35:43 +02:00
public static $motclef = '';
2020-08-25 21:01:10 +02:00
public static $motentier = true;
2020-08-20 16:11:11 +02:00
public static $previewLength = [
100 => '100 caractères',
200 => '200 caractères',
300 => '300 caractères',
400 => '400 caractères',
];
2020-08-15 09:35:43 +02:00
2020-08-14 16:09:46 +02:00
2021-04-05 08:59:24 +02:00
/**
* Mise à jour du module
* Appelée par les fonctions index et config
*/
private function update() {
2021-04-05 15:32:04 +02:00
// Version 2.0
2021-04-05 08:59:24 +02:00
if (version_compare($this->getData(['module', $this->getUrl(0), 'config', 'versionData']), '2.0', '<') ) {
2021-04-05 15:32:04 +02:00
$class = get_called_class();
$moduleId = $this->getUrl(0);
// Données de l'instance
2021-04-05 08:59:24 +02:00
$data = $this->getData(['module', $this->getUrl(0)]);
$this->setData(['module', $this->getUrl(0), 'config', [
'submitText' => $this->getData(['module', $this->getUrl(0), 'submitText']),
'placeHolder' => $this->getData(['module', $this->getUrl(0), 'placeHolder']),
'resultHideContent' => $this->getData(['module', $this->getUrl(0), 'resultHideContent']),
'previewLength' => $this->getData(['module', $this->getUrl(0), 'previewLength']),
2021-04-05 15:32:04 +02:00
'keywordColor' => $this->getData(['module', $this->getUrl(0), 'keywordColor']),
'style' => self::DATA_DIR . 'modules/' . $class . '/' . $moduleId . '.css',
'versionData' => '2.0'
2021-04-05 08:59:24 +02:00
]]);
$this->deleteData(['module', $this->getUrl(0), 'submitText']);
$this->deleteData(['module', $this->getUrl(0), 'placeHolder']);
$this->deleteData(['module', $this->getUrl(0), 'resultHideContent']);
$this->deleteData(['module', $this->getUrl(0), 'previewLength']);
$this->deleteData(['module', $this->getUrl(0), 'keywordColor']);
}
}
2021-04-05 15:32:04 +02:00
/**
* Initialisation du module
* Appelée par les fonctions index et config
*/
private function init(){
// Création des valeurs de réglage par défaut
if ( !is_array($this->getData(['module', $this->getUrl(0), 'config']) ) ) {
require_once('module/search/ressource/defaultdata.php');
$class = get_called_class();
$moduleId = $this->getUrl(0);
// Sauver les données par défaut
init::$defaultData['style'] = self::DATA_DIR . 'modules/' . $class . '/' . $moduleId . '.css';
$this->setData(['module', $this->getUrl(0), 'config', init::$defaultData]);
$style = '.searchItem {background:' . $this->getData(['module', $this->getUrl(0), 'config', 'keywordColor']). ';}';
// Dossier de l'instance
if (!is_dir(self::DATA_DIR . 'modules/' . $class)) {
mkdir (self::DATA_DIR . 'modules/' . $class, 0777, true);
}
$success = file_put_contents(self::DATA_DIR . 'modules/' . $class . '/' . $moduleId . '.css' , $style );
}
}
2021-04-05 08:59:24 +02:00
// Configuration vide
public function config() {
2021-04-05 15:32:04 +02:00
// Initialisation d'un nouveau module
$this->init();
2021-04-05 08:59:24 +02:00
// Mise à jour des données de module
$this->update();
2020-08-16 15:59:37 +02:00
if($this->isPost()) {
2021-04-05 08:59:24 +02:00
// Générer la feuille de CSS
$class = get_called_class();
$moduleId = $this->getUrl(0);
$style = '.searchItem {background:' . $this->getInput('searchKeywordColor') . ';}';
// Dossier de l'instance
if (!is_dir(self::DATA_DIR . 'modules/' . $class)) {
mkdir (self::DATA_DIR . 'modules/' . $class, 0777, true);
}
$success = file_put_contents(self::DATA_DIR . 'modules/' . $class . '/' . $moduleId . '.css' , $style );
// Fin feuille de style
2020-08-16 15:59:37 +02:00
// Soumission du formulaire
2021-04-05 08:59:24 +02:00
$this->setData(['module', $this->getUrl(0), 'config',[
2020-08-16 15:59:37 +02:00
'submitText' => $this->getInput('searchSubmitText'),
2020-08-16 16:24:09 +02:00
'placeHolder' => $this->getInput('searchPlaceHolder'),
'resultHideContent' => $this->getInput('searchResultHideContent',helper::FILTER_BOOLEAN),
'previewLength' => $this->getInput('searchPreviewLength',helper::FILTER_INT),
2021-04-05 08:59:24 +02:00
'keywordColor' => $this->getInput('searchKeywordColor'),
'style' => $success ? self::DATA_DIR . 'modules/' . $class . '/' . $moduleId . '.css' : '',
'versionData' => $this->getData(['module', $this->getUrl(0), 'config', 'versionData'])
2020-08-16 15:59:37 +02:00
]]);
2020-08-16 15:59:37 +02:00
// Valeurs en sortie, affichage du formulaire
$this->addOutput([
'redirect' => helper::baseUrl() . $this->getUrl(),
'notification' => $success !== FALSE ? 'Modifications enregistrées' : 'Modifications non enregistrées !',
'state' => $success !== FALSE
2020-08-16 15:59:37 +02:00
]);
2020-08-16 15:59:37 +02:00
}
// Valeurs en sortie, affichage du formulaire
$this->addOutput([
2020-08-16 15:59:37 +02:00
'title' => 'Configuration du module',
'view' => 'config',
'vendor' => [
'tinycolorpicker'
]
]);
}
2020-08-14 16:09:46 +02:00
2020-08-14 15:24:14 +02:00
public function index() {
2021-04-05 15:32:04 +02:00
// Initialisation d'un nouveau module
$this->init();
2021-04-05 08:59:24 +02:00
// Mise à jour des données de module
$this->update();
2020-08-20 19:31:11 +02:00
2020-08-21 09:45:59 +02:00
if($this->isPost()) {
2020-08-14 15:24:14 +02:00
//Initialisations variables
$success = true;
2020-08-17 16:51:18 +02:00
$result = [];
2020-08-14 15:24:14 +02:00
$notification = '';
$total='';
// Récupération du mot clef passé par le formulaire de ...view/index.php, avec caractères accentués
2020-08-15 09:35:43 +02:00
self::$motclef=$this->getInput('searchMotphraseclef');
2021-04-05 08:59:24 +02:00
// Variable de travail, on conserve la variable globale pour l'affichage du résultat
$motclef = self::$motclef;
// Traduction du mot clé si le script Google Trad est actif
// Le multi langue est sélectionné
if ( $this->getData(['config','translate','scriptGoogle']) === true
AND
// et la traduction de la langue courante est automatique
( isset($_COOKIE['googtrans'])
AND ( $this->getData(['config','translate', substr($_COOKIE['googtrans'],4,2)]) === 'script'
// Ou traduction automatique
OR $this->getData(['config','translate','autoDetect']) === true )
)
// Cas des pages d'administration
// Pas connecté
AND ( $this->getUser('password') !== $this->getInput('ZWII_USER_PASSWORD')
// Ou connecté avec option active
OR ($this->getUser('password') === $this->getInput('ZWII_USER_PASSWORD')
AND $this->getData(['config','translate','admin']) === true
)
)
AND !isset($_COOKIE['ZWII_I18N_SITE'])
)
{
// Découper la chaîne
$f = str_getcsv($motclef, ' ');
// Supprimer les espaces et les guillemets
$f = str_replace(' ','',$f);
$f = str_replace('"','',$f);
// Lire le cookie GoogTrans et déterminer les langues cibles
$language['origin'] = substr($_COOKIE['googtrans'],4,2);
$language['target'] = substr($_COOKIE['googtrans'],1,2);
if ($language['target'] !== $language['origin']) {
foreach ($f as $key => $value) {
$e = $this->translate($language['origin'],$language['target'],$value);
$motclef = str_replace($value,$e,$motclef);
}
}
}
// Suppression des mots < 3 caractères et des articles > 2 caractères de la chaîne $motclef
$arraymotclef = explode(' ', $motclef);
$motclef = '';
foreach($arraymotclef as $key=>$value){
if( strlen($value)>2 && $value!=='les' && $value!=='des' && $value!=='une' && $value!=='aux') $motclef.=$value.' ';
}
// Suppression du dernier ' '
if($motclef !== '') $motclef = substr($motclef,0, strlen($motclef)-1);
2020-08-14 15:24:14 +02:00
// Récupération de l'état de l'option mot entier passé par le même formulaire
2020-08-15 09:35:43 +02:00
self::$motentier=$this->getInput('searchMotentier', helper::FILTER_BOOLEAN);
2020-08-14 15:24:14 +02:00
2021-04-05 08:59:24 +02:00
if ($motclef !== '' ) {
2020-08-14 15:24:14 +02:00
foreach($this->getHierarchy(null,false,null) as $parentId => $childIds) {
if ($this->getData(['page', $parentId, 'disable']) === false &&
$this->getUser('group') >= $this->getData(['page', $parentId, 'group']) &&
$this->getData(['page', $parentId, 'block']) !== 'bar') {
$url = $parentId;
$titre = $this->getData(['page', $parentId, 'title']);
2020-09-15 20:35:32 +02:00
$contenu = ' ' . $titre . ' ' . $this->getData(['page', $parentId, 'content']);
2020-08-14 15:24:14 +02:00
// Pages sauf pages filles et articles de blog
2021-04-05 08:59:24 +02:00
$tempData = $this->occurrence($url, $titre, $contenu, $motclef, self::$motentier);
if (is_array($tempData) ) {
$result [] = $tempData;
2020-08-17 16:51:18 +02:00
}
2020-08-14 15:24:14 +02:00
}
foreach($childIds as $childId) {
// Sous page
if ($this->getData(['page', $childId, 'disable']) === false &&
2020-12-12 18:40:08 +01:00
$this->getUser('group') >= $this->getData(['page', $parentId, 'group']) &&
$this->getData(['page', $parentId, 'block']) !== 'bar') {
2020-08-14 15:24:14 +02:00
$url = $childId;
$titre = $this->getData(['page', $childId, 'title']);
2020-09-15 20:35:32 +02:00
$contenu = ' ' . $titre . ' ' . $this->getData(['page', $childId, 'content']);
2020-08-14 15:24:14 +02:00
//Pages filles
2021-04-05 08:59:24 +02:00
$tempData = $this->occurrence($url, $titre, $contenu, $motclef, self::$motentier);
if (is_array($tempData) ) {
$result [] = $tempData;
2020-08-17 16:51:18 +02:00
}
2020-08-14 15:24:14 +02:00
}
// Articles d'une sous-page blog
2021-04-05 08:59:24 +02:00
if ($this->getData(['page', $childId, 'moduleId']) === 'blog' &&
$this->getData(['module',$parentId,'posts']) )
2020-08-14 15:24:14 +02:00
{
2020-12-14 12:34:02 +01:00
foreach($this->getData(['module',$childId,'posts']) as $articleId => $article) {
if($this->getData(['module',$childId,'posts',$articleId,'state']) === true) {
2020-08-14 15:24:14 +02:00
$url = $childId . '/' . $articleId;
$titre = $article['title'];
2020-09-15 20:35:32 +02:00
$contenu = ' ' . $titre . ' ' . $article['content'];
2020-08-14 15:24:14 +02:00
// Articles de sous-page de type blog
2021-04-05 08:59:24 +02:00
$tempData = $this->occurrence($url, $titre, $contenu, $motclef, self::$motentier);
if (is_array($tempData) ) {
$result [] = $tempData;
2020-08-17 16:51:18 +02:00
}
2020-08-14 15:24:14 +02:00
}
}
}
}
// Articles d'un blog
2021-04-05 08:59:24 +02:00
if ($this->getData(['page', $parentId, 'moduleId']) === 'blog' &&
$this->getData(['module',$parentId,'posts']) ) {
2020-12-14 12:34:02 +01:00
foreach($this->getData(['module',$parentId,'posts']) as $articleId => $article) {
if($this->getData(['module',$parentId,'posts',$articleId,'state']) === true)
2020-08-14 15:24:14 +02:00
{
$url = $parentId. '/' . $articleId;
$titre = $article['title'];
2020-09-15 20:35:32 +02:00
$contenu = ' ' . $titre . ' ' . $article['content'];
2020-08-14 15:24:14 +02:00
// Articles de Blog
2021-04-05 08:59:24 +02:00
$tempData = $this->occurrence($url, $titre, $contenu, $motclef, self::$motentier);
if (is_array($tempData) ) {
$result [] = $tempData;
2020-08-17 16:51:18 +02:00
}
2020-08-14 15:24:14 +02:00
}
}
}
}
2020-08-17 10:39:44 +02:00
// Message de synthèse de la recherche
if (count($result) === 0) {
self::$resultTitle = 'Aucun résultat';
self::$resultError = 'Avez-vous pens&eacute; aux accents ?';
2020-08-14 15:24:14 +02:00
} else {
self::$resultError = '';
self::$resultTitle = ' Résultat de votre recherche';
rsort($result);
foreach ($result as $key => $value) {
$r [] = $value['preview'];
2020-08-17 16:51:18 +02:00
}
// Générer une chaine de caractères
2020-08-19 16:40:23 +02:00
self::$resultList= implode("", $r);
2020-08-17 16:51:18 +02:00
}
}
2020-08-14 15:24:14 +02:00
// Valeurs en sortie, affichage du résultat
$this->addOutput([
2020-08-16 15:59:37 +02:00
'view' => 'index',
'showBarEditButton' => true,
2021-04-05 08:59:24 +02:00
'showPageContent' => !$this->getData(['module', $this->getUrl(0), 'config', 'resultHideContent']),
'style' => $this->getData(['module', $this->getUrl(0), 'config', 'style'])
2020-08-14 15:24:14 +02:00
]);
} else {
// Valeurs en sortie, affichage du formulaire
$this->addOutput([
'view' => 'index',
'showBarEditButton' => true,
'showPageContent' => true
]);
}
}
// Fonction de recherche des occurrences dans $contenu
// Renvoie le résultat sous forme de chaîne
private function occurrence($url, $titre, $contenu, $motclef, $motentier)
{
// Nettoyage de $contenu : on enlève tout ce qui est inclus entre < et >
2020-08-17 10:39:44 +02:00
$contenu = preg_replace ('/<[^>]*>/', ' ', $contenu);
2020-08-14 15:24:14 +02:00
// Accentuation
$contenu = html_entity_decode($contenu);
// Découper le chaîne en tenant compte des quillemets
$a = str_getcsv(html_entity_decode($motclef), ' ');
// Construire la clé de recherche selon options de recherche
$keywords = '/(';
2021-04-05 08:59:24 +02:00
foreach ($a as $key => $value) {
2021-04-05 08:59:24 +02:00
$keywords .= $motentier === true ? $value . '|' : '\b' . $value . '\b|' ;
}
$keywords = substr($keywords,0,strlen($keywords) - 1);
$keywords .= ')/i';
$keywords = str_replace ('+', ' ',$keywords);
// Rechercher
$valid = preg_match_all($keywords,$contenu,$matches,PREG_OFFSET_CAPTURE);
if ($valid > 0 ) {
if (($matches[0][0][1]) > 0) {
$resultat = '<h2><a href="./?'.$url.'" target="_blank" rel="noopener">' . $titre . '</a></h2>';
// Création de l'aperçu
// Eviter de découper avec une valeur négative
$d = $matches[0][0][1] - 50 < 0 ? 1 : $matches[0][0][1] - 50;
// Rechercher l'espace le plus proche
2020-09-15 20:35:32 +02:00
$d = $d >= 1 ? strpos($contenu,' ',$d) : $d;
// Découper l'aperçu
2021-04-05 08:59:24 +02:00
$t = substr($contenu, $d ,$this->getData(['module',$this->getUrl(0), 'config', 'previewLength']));
// Applique une mise en évidence
2021-04-05 08:59:24 +02:00
$t = preg_replace($keywords, '<span class= "searchItem">\1</span>',$t);
// Sauver résultat
$resultat .= '<p class="searchResult">'.$t.'...</p>';
$resultat .= '<p class="searchTitle">' . count($matches[0]) . (count($matches[0]) === 1 ? ' correspondance<p>' : ' correspondances<p>');
//}
return ([
'matches' => count($matches[0]),
'preview' => $resultat
]);
2020-08-14 15:24:14 +02:00
}
}
}
2021-04-05 08:59:24 +02:00
// Requête de traduction avec le script Google
private function translate($from_lan, $to_lan, $text) {
$arrayjson = json_decode(file_get_contents('https://translate.googleapis.com/translate_a/single?client=gtx&sl='.$from_lan.'&tl=fr&dt=t&q='.$text),true);
return $arrayjson[0][0][0];
}
2020-08-14 15:24:14 +02:00
}