-
Notifications
You must be signed in to change notification settings - Fork 0
/
static-comments.php
275 lines (236 loc) · 11.8 KB
/
static-comments.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
<!-- ici le contenu de la page statique devant les commentaires et formulaire -->
<?php
# Ajout commentaires dans une page statique
# Pagination des commentaires
# reponses aux commentaires
# inclusion du capcha natif de PluXml
# @Author Gcyrillus
# licence GNU General Public License v3.0 : https://github.com/gcyrillus/scripts-pour-pluxml/blob/main/LICENSE
# $this is $plxShow
#########################################
#configuration
#########################################
# nombre de Commentaire à afficher par page
$bypage = 4;
$intermediaire = false; //mettre true au lieu de false pour afficher les liens de chaque page
$showLast = true; // mettre a true pour afficher la derniere page des commentaires par défaut
#########################################
# FIN configuration
#########################################
# fichier de stockage des commentaires
$commentsFile = PLX_ROOT.'/data/statiques/comment-'.$this->staticId().'.json';
if(!file_exists($commentsFile)) {
touch($commentsFile);
file_put_contents($commentsFile,'[]');
}
# extraction des commentaires dans un tableau
$comments = json_decode(file_get_contents($commentsFile), true);
$num = count($comments);
# format du message aprés soumission du formulaire
$row ='<p id="com_message" class="#com_class"><strong>#com_message</strong></p>';
# As t-on de nouveaux commentaires ?
if(isset($_POST)){
# color boite message par défaut
$color = 'orange';
$level='level-0';
if(isset($_POST['name']) AND isset($_POST['content'])) {
if( $_POST['name'] =='' OR $_POST['content'] =='') {
$_SESSION['msgcom'] = L_NEWCOMMENT_FIELDS_REQUIRED;
}
if(!empty($this->aConf['capcha']) AND (empty($_SESSION['capcha_token']) OR empty($_POST['capcha_token']) or ($_SESSION['capcha_token'] != $_POST['capcha_token']))) {
$_SESSION['msgcom'] .= ' '. L_NEWCOMMENT_ERR_ANTISPAM;
}
else {
# On vérifie que le capcha est correct
if($this->plxMotor->aConf['capcha'] == 0 OR $_SESSION['capcha'] == sha1($_POST['rep'])) {
if(isset($_POST['level']))
{
$level = trim($_POST['level']) ;
if($level == 'level-5' || $level =='level-max' ) {$level ='level-max';}
else {$level++;}
}
$newcomment[] = array(
'num' => trim(strip_tags($_POST['num'])),
'date' => date('d-m-Y') ,
'name' => trim(strip_tags($_POST['name'])) ,
'mail' => trim(strip_tags($_POST['mail'])) ,
'site' => trim(strip_tags($_POST['site'])),
'content' => trim(strip_tags($_POST['content'])),
'level' => trim(strip_tags($level))
);
$color = 'green';
$_SESSION['msgcom'] = L_COM_PUBLISHED;
# est ce une reponse à un commentaire particulier?
if(isset($_POST['index']))
{
array_splice($comments, trim($_POST['index'])+1, 0, $newcomment);
}
else {
array_push($comments,$newcomment[0]);
}
file_put_contents($commentsFile, json_encode($comments,true|JSON_PRETTY_PRINT) );
}
else {
$_SESSION['msgcom'] = L_NEWCOMMENT_ERR_ANTISPAM;
}
}
}
}
if(count($comments)>0) {
$tittleComment ="<h3>".count($comments).' '. L_COMMENT ."</h3>";
if(count($comments)>1) {
$tittleComment ="<h3>".count($comments).' '. L_COMMENTS ."</h3>";
}
echo $tittleComment ;
# Style barre pagination commentaires
echo '<style>:where(.page-item.page-link.active) { text-decoration:underline;font-weight:bold; padding:0.3em 2em;}.pagination.text-center.center.bordered {border-radius: 5px;width:max-content; margin:auto; border:solid 1px}#com_message:has(strong:empty){display: none;}.content_com { white-space: pre-wrap;}</style>';
#############################
# extraction et maj variables
#############################
# extraction de l'url
$url = $this->plxMotor->urlRewrite('?static' . $this->staticId() . '/' . $this->plxMotor->aStats[str_pad($this->staticId(), 3, '0', STR_PAD_LEFT)]['url'] );
# generation du lien
$link = $this->plxMotor->urlRewrite($url."/page");
// On calcule le nombre de pages total
$nbr = count($comments);
$pages = ceil( $nbr / $bypage);
$position = 1;
if($showLast==true) $position = $pages;
# extraction du numéro de page dans l'URL
$currentPage = preg_match('#\bpage(\d*)#',$_SERVER['REQUEST_URI'], $capture) ? intval($capture[1]) : $position;
# indice de début, premier article à afficher
$start = ($currentPage - 1) * $bypage;
// Calcul du 1er commentaire de la page
$premier = ($currentPage * $bypage) - $bypage;
;
$pagecomments = array_slice($comments, $premier, $bypage);
foreach($pagecomments as $com =>$val) { # On boucle sur les commentaires
$index=array_search( $val['num'], array_column( $comments, 'num' ) );
echo '<div id="id-'.$val['num'].'" class="comment '.$val['level'].'">
<div id="com-'.$val['num'].'" data-index="'.$index.'" data-level="'.$val['level'].'">
<small>
<a class="nbcom" href="'.$_SERVER['REQUEST_URI'].'#com-'.$val['num'].'" title="#commentaire '.$val['num'].'">#'.$val['num'].'</a>
<time datetime="'.$val['date'].'">'.$val['date'].'</time> - '.$val['name'].' '. $this->getLang('SAID').':
</small>
<blockquote>
<p class="content_com">'.$val['content'].'</p>
</blockquote>';
if($this->plxMotor->aConf['allow_com']){
echo '<a rel="nofollow" href="#form" onclick="replyCom(\''.trim($val['num']).'\');">'.$this->getLang('REPLY').'</a>';
}
echo '</div>
</div>';
}
?>
<!-- Affichage de la pagination -->
<?php
############################
# Affichage de la pagination
############################
if($pages>1){
?>
<nav>
<ul class="pagination text-center center bordered">
<!-- Lien vers la page précédente (si on ne se trouve pas sur la 1ère page) -->
<?= ($currentPage > 1) ? "<li class=\"page-item\" ><a href=\"".$link . ($currentPage - 1) ."\" class=\"page-link\">".L_PAGINATION_PREVIOUS."</a></li>" : "" ?>
<?php if($intermediaire == true) {
for($page = 1; $page <= $pages; $page++) {
# Lien vers chacune des pages (activé si on se trouve sur la page correspondante
echo '<li class="page-item ';
if($currentPage == $page) echo 'active';
echo "\"><a href=\"".$link.$page ."\" class=\"page-link\">".$page."</a></li>";
}
}
else {
echo "<li class=\"page-item page-link active \">
".$currentPage." / ". $pages."
</li>";
} ?>
<!-- Lien vers la page suivante (si on ne se trouve pas sur la dernière page) -->
<?= ($currentPage < $pages) ? " <li class=\"page-item\"><a href=\"".$link.($currentPage + 1 )."\" class=\"page-link\">".L_PAGINATION_NEXT."</a></li>" : "" ?>
</ul>
</nav>
<?php }
}
else {
echo L_NO_COMMENT;
}
if (!empty($_SESSION['msgcom'])) {
$message=$_SESSION['msgcom'];
$row = str_replace('#com_class', 'alert ' . $color, $row);
unset($_SESSION['msgcom']);
}
else {
$message='';
}
$row = str_replace('#com_message',$message , $row);
?>
<h3>
<?php $this->lang('WRITE_A_COMMENT') ?>
</h3>
<form id="form" action="<?php echo $_SERVER['REQUEST_URI']; ?>#form" method="post">
<fieldset>
<div class="grid">
<div class="col sml-12">
<label for="id_name"><?php $this->lang('NAME') ?>* :</label>
<input id="id_name" name="name" type="text" size="20" value="" maxlength="30" required="required" />
</div>
</div>
<div class="grid">
<div class="col sml-12 lrg-6">
<label for="id_mail"><?php $this->lang('EMAIL') ?> :</label>
<input id="id_mail" name="mail" type="text" size="20" value="" />
</div>
<div class="col sml-12 lrg-6">
<label for="id_site"><?php $this->lang('WEBSITE') ?> :</label>
<input id="id_site" name="site" type="text" size="20" value="" />
</div>
</div>
<div class="grid">
<div class="col sml-12">
<div id="id_answer"></div>
<label for="id_content" class="lab_com"><?php $this->lang('COMMENT') ?>* :</label>
<textarea id="id_content" name="content" cols="35" rows="6" required="required"></textarea>
</div>
</div>
<?php echo $row;
if($this->plxMotor->aConf['capcha']): ?>
<div class="grid">
<div class="col sml-12">
<label for="id_rep"><strong><?php echo $this->lang('ANTISPAM_WARNING') ?></strong>*</label>
<?php
$this->plxMotor->plxCapcha = new plxCapcha(); # Création objet captcha
$this->capchaQ();
?>
<input id="id_rep" name="rep" type="text" size="2" maxlength="1" style="width: auto; display: inline;" required="required" />
</div>
</div>
<?php endif; ?>
<div class="grid">
<div class="col sml-12">
<input type="hidden" id="num" name="num" value="<?php echo $num + 1 ?>"/>
<input type="hidden" id="id_parent" name="parent" value="" />
<input class="blue" type="submit" value="<?php $this->lang('SEND') ?>" />
</div>
</div>
</fieldset>
</form>
<script>
function replyCom(idCom) {
document.getElementById('id_answer').innerHTML='<?php $this->lang('REPLY_TO'); ?> :';
document.getElementById('id_answer').innerHTML+=document.getElementById('com-'+idCom).innerHTML;
document.getElementById('id_answer').innerHTML+='<input type="hidden" name="index" value="'+document.getElementById('com-'+idCom).getAttribute('data-index') +'">';
document.getElementById('id_answer').innerHTML+='<input type="hidden" name="level" value="'+document.getElementById('com-'+idCom).getAttribute('data-level') +'">';
document.getElementById('id_answer').innerHTML+='<a rel="nofollow" href="#form" onclick="cancelCom()"><?php $this->lang('CANCEL'); ?></a>';
document.getElementById('id_answer').style.display='inline-block';
document.getElementById('id_parent').value=idCom;
document.getElementById('id_content').focus();
}
function cancelCom() {
document.getElementById('id_answer').style.display='none';
document.getElementById('id_parent').value='';
document.getElementById('com_message').innerHTML='';
}
var parent = document.getElementById('id_parent').value;
if(parent!='') { replyCom(parent) }
</script>